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 36 2e 31 2e 20 ersion 3.6.6.1.
00b0: 20 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c By combining al
00c0: 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c l the individual
00d0: 20 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e C code files in
00e0: 74 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 to this .** sing
00f0: 6c 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 le large file, t
0100: 68 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 he entire code c
0110: 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 an be compiled a
0120: 73 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 s a one translat
0130: 69 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 ion.** unit. Th
0140: 69 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 is allows many c
0150: 6f 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f ompilers to do o
0160: 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 ptimizations tha
0170: 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a t would not be.*
0180: 2a 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 * possible if th
0190: 65 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d e files were com
01a0: 70 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 piled separately
01b0: 2e 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 . Performance i
01c0: 6d 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f mprovements.** o
01d0: 66 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 f 5% are more ar
01e0: 65 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 e commonly seen
01f0: 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 when SQLite is c
0200: 6f 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e ompiled as a sin
0210: 67 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 gle.** translati
0220: 6f 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 on unit..**.** T
0230: 68 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 his file is all
0240: 79 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 you need to comp
0250: 69 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 ile SQLite. To
0260: 75 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 use SQLite in ot
0270: 68 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c her.** programs,
0280: 20 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 you need this f
0290: 69 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c ile and the "sql
02a0: 69 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 ite3.h" header f
02b0: 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 ile that defines
02c0: 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d .** the programm
02d0: 69 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f ing interface to
02e0: 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
02f0: 61 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f ary. (If you do
0300: 20 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 not have .** th
0310: 65 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 e "sqlite3.h" he
0320: 61 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e ader file at han
0330: 64 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 d, you will find
0340: 20 61 20 63 6f 70 79 20 69 6e 20 74 68 65 20 66 a copy in the f
0350: 69 72 73 74 0a 2a 2a 20 36 37 32 38 20 6c 69 6e irst.** 6728 lin
0360: 65 73 20 70 61 73 74 20 74 68 69 73 20 68 65 61 es past this hea
0370: 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 29 20 20 41 der comment.) A
0380: 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 20 66 dditional code f
0390: 69 6c 65 73 20 6d 61 79 20 62 65 0a 2a 2a 20 6e iles may be.** n
03a0: 65 65 64 65 64 20 69 66 20 79 6f 75 20 77 61 6e eeded if you wan
03b0: 74 20 61 20 77 72 61 70 70 65 72 20 74 6f 20 69 t a wrapper to i
03c0: 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74 65 20 nterface SQLite
03d0: 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69 63 65 with your choice
03e0: 20 6f 66 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 69 of.** programmi
03f0: 6e 67 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 ng language. Th
0400: 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 22 e code for the "
0410: 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e 64 sqlite3" command
0420: 2d 6c 69 6e 65 20 73 68 65 6c 6c 0a 2a 2a 20 69 -line shell.** i
0430: 73 20 61 6c 73 6f 20 69 6e 20 61 20 73 65 70 61 s also in a sepa
0440: 72 61 74 65 20 66 69 6c 65 2e 20 20 54 68 69 73 rate file. This
0450: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f file contains o
0460: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65 nly code for the
0470: 20 63 6f 72 65 0a 2a 2a 20 53 51 4c 69 74 65 20 core.** SQLite
0480: 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 library..**.** T
0490: 68 69 73 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e his amalgamation
04a0: 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 6f was generated o
04b0: 6e 20 32 30 30 38 2d 31 31 2d 32 32 20 31 34 3a n 2008-11-22 14:
04c0: 33 31 3a 33 32 20 55 54 43 2e 0a 2a 2f 0a 23 64 31:32 UTC..*/.#d
04d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 efine SQLITE_COR
04e0: 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 E 1.#define SQLI
04f0: 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 TE_AMALGAMATION
0500: 31 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 1.#ifndef SQLITE
0510: 5f 50 52 49 56 41 54 45 0a 23 20 64 65 66 69 6e _PRIVATE.# defin
0520: 65 20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 e SQLITE_PRIVATE
0530: 20 73 74 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 static.#endif.#
0540: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50 ifndef SQLITE_AP
0550: 49 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 I.# define SQLIT
0560: 45 5f 41 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a E_API.#endif./**
0570: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
0580: 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 49 6e in file sqliteIn
0590: 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
05a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
05c0: 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
05d0: 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
05e0: 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
05f0: 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
0600: 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
0610: 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
0620: 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
0630: 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
0640: 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
0650: 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
0660: 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
0670: 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
0680: 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
0690: 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
06a0: 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
06b0: 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
06c0: 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
06d0: 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
06e0: 20 67 69 76 65 2e 0a 2a 2a 0a 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 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 ***.** Internal
0740: 69 6e 74 65 72 66 61 63 65 20 64 65 66 69 6e 69 interface defini
0750: 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 tions for SQLite
0760: 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 ..**.** @(#) $Id
0770: 3a 20 73 71 6c 69 74 65 49 6e 74 2e 68 2c 76 20 : sqliteInt.h,v
0780: 31 2e 37 39 38 20 32 30 30 38 2f 31 31 2f 31 39 1.798 2008/11/19
0790: 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 16:52:44 daniel
07a0: 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 k1977 Exp $.*/.#
07b0: 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 49 4e ifndef _SQLITEIN
07c0: 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 T_H_.#define _SQ
07d0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a LITEINT_H_../*.*
07e0: 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 20 63 6f * Include the co
07f0: 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 65 61 64 nfiguration head
0800: 65 72 20 6f 75 74 70 75 74 20 62 79 20 27 63 6f er output by 'co
0810: 6e 66 69 67 75 72 65 27 20 69 66 20 77 65 27 72 nfigure' if we'r
0820: 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 61 e using the.** a
0830: 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 20 62 75 utoconf-based bu
0840: 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 48 ild.*/.#ifdef _H
0850: 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 AVE_SQLITE_CONFI
0860: 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f G_H.#include "co
0870: 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 66 0a 0a nfig.h".#endif..
0880: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
0890: 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 4c 69 Include sqliteLi
08a0: 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 mit.h in the mid
08b0: 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
08c0: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a .h ***********/.
08d0: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
08e0: 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 Begin file sqlit
08f0: 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a eLimit.h *******
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0910: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
0920: 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 37 /*.** 2007 May 7
0930: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
0940: 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
0950: 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
0960: 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
0970: 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
0980: 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
0990: 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
09a0: 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
09b0: 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
09c0: 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
09d0: 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
09e0: 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
09f0: 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
0a00: 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
0a10: 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
0a20: 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
0a30: 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
0a40: 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
0a50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0a80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
0a90: 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ** .** This file
0aa0: 20 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75 73 defines various
0ab0: 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74 20 limits of what
0ac0: 53 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63 65 SQLite can proce
0ad0: 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 ss..**.** @(#) $
0ae0: 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e Id: sqliteLimit.
0af0: 68 2c 76 20 31 2e 38 20 32 30 30 38 2f 30 33 2f h,v 1.8 2008/03/
0b00: 32 36 20 31 35 3a 35 36 3a 32 32 20 64 72 68 20 26 15:56:22 drh
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 30 30 0a 23 65 6e TION_ARG 100.#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 2f 0a 23 69 ower of 2..*/.#i
18d0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
18e0: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 _PAGE_SIZE.# def
18f0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 ine SQLITE_MAX_P
1900: 41 47 45 5f 53 49 5a 45 20 33 32 37 36 38 0a 23 AGE_SIZE 32768.#
1910: 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 endif.../*.** Th
1920: 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f e default size o
1930: 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 f a database pag
1940: 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 e..*/.#ifndef SQ
1950: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 LITE_DEFAULT_PAG
1960: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 E_SIZE.# define
1970: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 SQLITE_DEFAULT_P
1980: 41 47 45 5f 53 49 5a 45 20 31 30 32 34 0a 23 65 AGE_SIZE 1024.#e
1990: 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f ndif.#if SQLITE_
19a0: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
19b0: 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 E>SQLITE_MAX_PAG
19c0: 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 E_SIZE.# undef S
19d0: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
19e0: 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 GE_SIZE.# define
19f0: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
1a00: 50 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54 45 PAGE_SIZE SQLITE
1a10: 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 _MAX_PAGE_SIZE.#
1a20: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 endif../*.** Ord
1a30: 69 6e 61 72 69 6c 79 2c 20 69 66 20 6e 6f 20 76 inarily, if no v
1a40: 61 6c 75 65 20 69 73 20 65 78 70 6c 69 63 69 74 alue is explicit
1a50: 6c 79 20 70 72 6f 76 69 64 65 64 2c 20 53 51 4c ly provided, SQL
1a60: 69 74 65 20 63 72 65 61 74 65 73 20 64 61 74 61 ite creates data
1a70: 62 61 73 65 73 0a 2a 2a 20 77 69 74 68 20 70 61 bases.** with pa
1a80: 67 65 20 73 69 7a 65 20 53 51 4c 49 54 45 5f 44 ge size SQLITE_D
1a90: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1aa0: 2e 20 48 6f 77 65 76 65 72 2c 20 62 61 73 65 64 . However, based
1ab0: 20 6f 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 64 on certain.** d
1ac0: 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 evice characteri
1ad0: 73 74 69 63 73 20 28 73 65 63 74 6f 72 2d 73 69 stics (sector-si
1ae0: 7a 65 20 61 6e 64 20 61 74 6f 6d 69 63 20 77 72 ze and atomic wr
1af0: 69 74 65 28 29 20 73 75 70 70 6f 72 74 29 2c 0a ite() support),.
1b00: 2a 2a 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68 ** SQLite may ch
1b10: 6f 6f 73 65 20 61 20 6c 61 72 67 65 72 20 76 61 oose a larger va
1b20: 6c 75 65 2e 20 54 68 69 73 20 63 6f 6e 73 74 61 lue. This consta
1b30: 6e 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 nt is the maximu
1b40: 6d 20 76 61 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 m value.** SQLit
1b50: 65 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e e will choose on
1b60: 20 69 74 73 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 its own..*/.#if
1b70: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f ndef SQLITE_MAX_
1b80: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
1b90: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
1ba0: 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
1bb0: 47 45 5f 53 49 5a 45 20 38 31 39 32 0a 23 65 6e GE_SIZE 8192.#en
1bc0: 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d dif.#if SQLITE_M
1bd0: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
1be0: 53 49 5a 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f SIZE>SQLITE_MAX_
1bf0: 50 41 47 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65 PAGE_SIZE.# unde
1c00: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 f SQLITE_MAX_DEF
1c10: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 AULT_PAGE_SIZE.#
1c20: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
1c30: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
1c40: 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f SIZE SQLITE_MAX_
1c50: 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 PAGE_SIZE.#endif
1c60: 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d .../*.** Maximum
1c70: 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1c80: 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 in one database
1c90: 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 file..**.** Thi
1ca0: 73 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 s is really just
1cb0: 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c the default val
1cc0: 75 65 20 66 6f 72 20 74 68 65 20 6d 61 78 5f 70 ue for the max_p
1cd0: 61 67 65 5f 63 6f 75 6e 74 20 70 72 61 67 6d 61 age_count pragma
1ce0: 2e 0a 2a 2a 20 54 68 69 73 20 76 61 6c 75 65 20 ..** This value
1cf0: 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 28 can be lowered (
1d00: 6f 72 20 72 61 69 73 65 64 29 20 61 74 20 72 75 or raised) at ru
1d10: 6e 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 61 n-time using tha
1d20: 74 20 74 68 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 t the.** max_pag
1d30: 65 5f 63 6f 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a e_count macro..*
1d40: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1d50: 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 0a _MAX_PAGE_COUNT.
1d60: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1d70: 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 20 31 MAX_PAGE_COUNT 1
1d80: 30 37 33 37 34 31 38 32 33 0a 23 65 6e 64 69 66 073741823.#endif
1d90: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 ../*.** Maximum
1da0: 6c 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 length (in bytes
1db0: 29 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e ) of the pattern
1dc0: 20 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c in a LIKE or GL
1dd0: 4f 42 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a OB.** operator..
1de0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1df0: 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 E_MAX_LIKE_PATTE
1e00: 52 4e 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 RN_LENGTH.# defi
1e10: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 ne SQLITE_MAX_LI
1e20: 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 KE_PATTERN_LENGT
1e30: 48 20 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a H 50000.#endif..
1e40: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1e50: 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 4c 69 6d End of sqliteLim
1e60: 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a it.h ***********
1e70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1e90: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1ea0: 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
1eb0: 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
1ec0: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
1ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1ee0: 0a 2f 2a 20 44 69 73 61 62 6c 65 20 6e 75 69 73 ./* Disable nuis
1ef0: 61 6e 63 65 20 77 61 72 6e 69 6e 67 73 20 6f 6e ance warnings on
1f00: 20 42 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c 65 Borland compile
1f10: 72 73 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 rs */.#if define
1f20: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a d(__BORLANDC__).
1f30: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 72 63 #pragma warn -rc
1f40: 68 20 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c 65 h /* unreachable
1f50: 20 63 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d 61 code */.#pragma
1f60: 20 77 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43 6f warn -ccc /* Co
1f70: 6e 64 69 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 ndition is alway
1f80: 73 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20 s true or false
1f90: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 */.#pragma warn
1fa0: 2d 61 75 73 20 2f 2a 20 41 73 73 69 67 6e 65 64 -aus /* Assigned
1fb0: 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20 value is never
1fc0: 75 73 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 used */.#pragma
1fd0: 77 61 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f 6d warn -csu /* Com
1fe0: 70 61 72 69 6e 67 20 73 69 67 6e 65 64 20 61 6e paring signed an
1ff0: 64 20 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23 70 d unsigned */.#p
2000: 72 61 67 6d 61 20 77 61 72 6e 20 2d 73 70 61 20 ragma warn -spa
2010: 2f 2a 20 53 75 73 70 69 63 6f 75 73 20 70 6f 69 /* Suspicous poi
2020: 6e 74 65 72 20 61 72 69 74 68 6d 65 74 69 63 20 nter arithmetic
2030: 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4e 65 */.#endif../* Ne
2040: 65 64 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 eded for various
2050: 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e 2e 20 definitions...
2060: 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e 55 5f */.#ifndef _GNU_
2070: 53 4f 55 52 43 45 0a 23 20 64 65 66 69 6e 65 20 SOURCE.# define
2080: 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65 6e 64 _GNU_SOURCE.#end
2090: 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 if../*.** Includ
20a0: 65 20 73 74 61 6e 64 61 72 64 20 68 65 61 64 65 e standard heade
20b0: 72 20 66 69 6c 65 73 20 61 73 20 6e 65 63 65 73 r files as neces
20c0: 73 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66 20 48 sary.*/.#ifdef H
20d0: 41 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23 69 6e AVE_STDINT_H.#in
20e0: 63 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e 68 3e clude <stdint.h>
20f0: 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48 .#endif.#ifdef H
2100: 41 56 45 5f 49 4e 54 54 59 50 45 53 5f 48 0a 23 AVE_INTTYPES_H.#
2110: 69 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79 70 65 include <inttype
2120: 73 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a s.h>.#endif../*.
2130: 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 20 ** A macro used
2140: 74 6f 20 61 69 64 20 69 6e 20 63 6f 76 65 72 61 to aid in covera
2150: 67 65 20 74 65 73 74 69 6e 67 2e 20 20 57 68 65 ge testing. Whe
2160: 6e 20 64 6f 69 6e 67 20 63 6f 76 65 72 61 67 65 n doing coverage
2170: 0a 2a 2a 20 74 65 73 74 69 6e 67 2c 20 74 68 65 .** testing, the
2180: 20 63 6f 6e 64 69 74 69 6f 6e 20 69 6e 73 69 64 condition insid
2190: 65 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6d e the argument m
21a0: 75 73 74 20 62 65 20 65 76 61 6c 75 61 74 65 64 ust be evaluated
21b0: 20 0a 2a 2a 20 62 6f 74 68 20 74 72 75 65 20 61 .** both true a
21c0: 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 64 65 nd false in orde
21d0: 72 20 74 6f 20 67 65 74 20 66 75 6c 6c 20 62 72 r to get full br
21e0: 61 6e 63 68 20 63 6f 76 65 72 61 67 65 2e 0a 2a anch coverage..*
21f0: 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 63 61 6e * This macro can
2200: 20 62 65 20 69 6e 73 65 72 74 65 64 20 74 6f 20 be inserted to
2210: 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65 20 ensure adequate
2220: 74 65 73 74 20 63 6f 76 65 72 61 67 65 0a 2a 2a test coverage.**
2230: 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 in places where
2240: 20 73 69 6d 70 6c 65 20 63 6f 6e 64 69 74 69 6f simple conditio
2250: 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 72 n/decision cover
2260: 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61 74 age is inadequat
2270: 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
2280: 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 ITE_COVERAGE_TES
2290: 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
22a0: 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 void sqlite3C
22b0: 6f 76 65 72 61 67 65 28 69 6e 74 29 3b 0a 23 20 overage(int);.#
22c0: 64 65 66 69 6e 65 20 74 65 73 74 63 61 73 65 28 define testcase(
22d0: 58 29 20 20 69 66 28 20 58 20 29 7b 20 73 71 6c X) if( X ){ sql
22e0: 69 74 65 33 43 6f 76 65 72 61 67 65 28 5f 5f 4c ite3Coverage(__L
22f0: 49 4e 45 5f 5f 29 3b 20 7d 0a 23 65 6c 73 65 0a INE__); }.#else.
2300: 23 20 64 65 66 69 6e 65 20 74 65 73 74 63 61 73 # define testcas
2310: 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a e(X).#endif../*.
2320: 2a 2a 20 54 68 65 20 41 4c 57 41 59 53 20 61 6e ** The ALWAYS an
2330: 64 20 4e 45 56 45 52 20 6d 61 63 72 6f 73 20 73 d NEVER macros s
2340: 75 72 72 6f 75 6e 64 20 62 6f 6f 6c 65 61 6e 20 urround boolean
2350: 65 78 70 72 65 73 73 69 6f 6e 73 20 77 68 69 63 expressions whic
2360: 68 20 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 h .** are intend
2370: 65 64 20 74 6f 20 61 6c 77 61 79 73 20 62 65 20 ed to always be
2380: 74 72 75 65 20 6f 72 20 66 61 6c 73 65 2c 20 72 true or false, r
2390: 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53 75 espectively. Su
23a0: 63 68 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ch.** expression
23b0: 73 20 63 6f 75 6c 64 20 62 65 20 6f 6d 69 74 74 s could be omitt
23c0: 65 64 20 66 72 6f 6d 20 74 68 65 20 63 6f 64 65 ed from the code
23d0: 20 63 6f 6d 70 6c 65 74 65 6c 79 2e 20 20 42 75 completely. Bu
23e0: 74 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 69 6e t they.** are in
23f0: 63 6c 75 64 65 64 20 69 6e 20 61 20 66 65 77 20 cluded in a few
2400: 63 61 73 65 73 20 69 6e 20 6f 72 64 65 72 20 74 cases in order t
2410: 6f 20 65 6e 68 61 6e 63 65 20 74 68 65 20 72 65 o enhance the re
2420: 73 69 6c 69 65 6e 63 65 0a 2a 2a 20 6f 66 20 53 silience.** of S
2430: 51 4c 69 74 65 20 74 6f 20 75 6e 65 78 70 65 63 QLite to unexpec
2440: 74 65 64 20 62 65 68 61 76 69 6f 72 20 2d 20 74 ted behavior - t
2450: 6f 20 6d 61 6b 65 20 74 68 65 20 63 6f 64 65 20 o make the code
2460: 22 73 65 6c 66 2d 68 65 61 6c 69 6e 67 22 0a 2a "self-healing".*
2470: 2a 20 6f 72 20 22 64 75 63 74 69 6c 65 22 20 72 * or "ductile" r
2480: 61 74 68 65 72 20 74 68 61 6e 20 62 65 69 6e 67 ather than being
2490: 20 22 62 72 69 74 74 6c 65 22 20 61 6e 64 20 63 "brittle" and c
24a0: 72 61 73 68 69 6e 67 20 61 74 20 74 68 65 20 66 rashing at the f
24b0: 69 72 73 74 0a 2a 2a 20 68 69 6e 74 20 6f 66 20 irst.** hint of
24c0: 75 6e 70 6c 61 6e 6e 65 64 20 62 65 68 61 76 69 unplanned behavi
24d0: 6f 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 64 or..**.** When d
24e0: 6f 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 oing coverage te
24f0: 73 74 69 6e 67 20 41 4c 57 41 59 53 20 61 6e 64 sting ALWAYS and
2500: 20 4e 45 56 45 52 20 61 72 65 20 68 61 72 64 2d NEVER are hard-
2510: 63 6f 64 65 64 20 74 6f 0a 2a 2a 20 62 65 20 74 coded to.** be t
2520: 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 73 6f rue and false so
2530: 20 74 68 61 74 20 74 68 65 20 75 6e 72 65 61 63 that the unreac
2540: 68 61 62 6c 65 20 63 6f 64 65 20 74 68 65 6e 20 hable code then
2550: 73 70 65 63 69 66 79 20 77 69 6c 6c 0a 2a 2a 20 specify will.**
2560: 6e 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20 61 not be counted a
2570: 73 20 75 6e 74 65 73 74 65 64 20 63 6f 64 65 2e s untested code.
2580: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
2590: 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a E_COVERAGE_TEST.
25a0: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 # define ALWAYS(
25b0: 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64 65 X) (1).# de
25c0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 fine NEVER(X)
25d0: 20 20 20 20 28 30 29 0a 23 65 6c 73 65 0a 23 20 (0).#else.#
25e0: 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29 define ALWAYS(X)
25f0: 20 20 20 20 20 20 28 58 29 0a 23 20 64 65 66 69 (X).# defi
2600: 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20 ne NEVER(X)
2610: 20 20 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a (X).#endif../*
2620: 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 75 6e .** The macro un
2630: 6c 69 6b 65 6c 79 28 29 20 69 73 20 61 20 68 69 likely() is a hi
2640: 6e 74 20 74 68 61 74 20 73 75 72 72 6f 75 6e 64 nt that surround
2650: 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 65 s a boolean.** e
2660: 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 xpression that i
2670: 73 20 75 73 75 61 6c 6c 79 20 66 61 6c 73 65 2e s usually false.
2680: 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c 79 28 29 Macro likely()
2690: 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a 20 61 20 surrounds.** a
26a0: 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 boolean expressi
26b0: 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75 61 6c on that is usual
26c0: 6c 79 20 74 72 75 65 2e 20 20 47 43 43 20 69 73 ly true. GCC is
26d0: 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 20 able to.** use
26e0: 74 68 65 73 65 20 68 69 6e 74 73 20 74 6f 20 67 these hints to g
26f0: 65 6e 65 72 61 74 65 20 62 65 74 74 65 72 20 63 enerate better c
2700: 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65 73 2e 0a ode, sometimes..
2710: 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f */.#if defined(_
2720: 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30 0a 23 20 _GNUC__) && 0.#
2730: 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 58 29 define likely(X)
2740: 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 __builtin_ex
2750: 70 65 63 74 28 28 58 29 2c 31 29 0a 23 20 64 65 pect((X),1).# de
2760: 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 29 fine unlikely(X)
2770: 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 70 65 __builtin_expe
2780: 63 74 28 28 58 29 2c 30 29 0a 23 65 6c 73 65 0a ct((X),0).#else.
2790: 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 # define likely(
27a0: 58 29 20 20 20 20 21 21 28 58 29 0a 23 20 64 65 X) !!(X).# de
27b0: 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 29 fine unlikely(X)
27c0: 20 20 21 21 28 58 29 0a 23 65 6e 64 69 66 0a 0a !!(X).#endif..
27d0: 2f 2a 0a 20 2a 20 54 68 69 73 20 6d 61 63 72 6f /*. * This macro
27e0: 20 69 73 20 75 73 65 64 20 74 6f 20 22 68 69 64 is used to "hid
27f0: 65 22 20 73 6f 6d 65 20 75 67 6c 69 6e 65 73 73 e" some ugliness
2800: 20 69 6e 20 63 61 73 74 69 6e 67 20 61 6e 20 69 in casting an i
2810: 6e 74 0a 20 2a 20 76 61 6c 75 65 20 74 6f 20 61 nt. * value to a
2820: 20 70 74 72 20 76 61 6c 75 65 20 75 6e 64 65 72 ptr value under
2830: 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 the MSVC 64-bit
2840: 20 63 6f 6d 70 69 6c 65 72 2e 20 20 20 43 61 73 compiler. Cas
2850: 74 69 6e 67 0a 20 2a 20 6e 6f 6e 20 36 34 2d 62 ting. * non 64-b
2860: 69 74 20 76 61 6c 75 65 73 20 74 6f 20 70 74 72 it values to ptr
2870: 20 74 79 70 65 73 20 72 65 73 75 6c 74 73 20 69 types results i
2880: 6e 20 61 20 22 68 61 72 64 22 20 65 72 72 6f 72 n a "hard" error
2890: 20 77 69 74 68 20 0a 20 2a 20 74 68 65 20 4d 53 with . * the MS
28a0: 56 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c VC 64-bit compil
28b0: 65 72 20 77 68 69 63 68 20 74 68 69 73 20 61 74 er which this at
28c0: 74 65 6d 70 74 73 20 74 6f 20 61 76 6f 69 64 2e tempts to avoid.
28d0: 20 20 0a 20 2a 0a 20 2a 20 41 20 73 69 6d 70 6c . *. * A simpl
28e0: 65 20 63 6f 6d 70 69 6c 65 72 20 70 72 61 67 6d e compiler pragm
28f0: 61 20 6f 72 20 63 61 73 74 69 6e 67 20 73 65 71 a or casting seq
2900: 75 65 6e 63 65 20 63 6f 75 6c 64 20 6e 6f 74 20 uence could not
2910: 62 65 20 66 6f 75 6e 64 0a 20 2a 20 74 6f 20 63 be found. * to c
2920: 6f 72 72 65 63 74 20 74 68 69 73 20 69 6e 20 61 orrect this in a
2930: 6c 6c 20 73 69 74 75 61 74 69 6f 6e 73 2c 20 73 ll situations, s
2940: 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 77 61 73 o this macro was
2950: 20 69 6e 74 72 6f 64 75 63 65 64 2e 0a 20 2a 0a introduced.. *.
2960: 20 2a 20 49 74 20 63 6f 75 6c 64 20 62 65 20 61 * It could be a
2970: 72 67 75 65 64 20 74 68 61 74 20 74 68 65 20 69 rgued that the i
2980: 6e 74 70 74 72 5f 74 20 74 79 70 65 20 63 6f 75 ntptr_t type cou
2990: 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 ld be used in th
29a0: 69 73 0a 20 2a 20 63 61 73 65 2c 20 62 75 74 20 is. * case, but
29b0: 74 68 61 74 20 74 79 70 65 20 69 73 20 6e 6f 74 that type is not
29c0: 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c available on al
29d0: 6c 20 63 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 20 l compilers, or
29e0: 0a 20 2a 20 72 65 71 75 69 72 65 73 20 74 68 65 . * requires the
29f0: 20 23 69 6e 63 6c 75 64 65 20 6f 66 20 73 70 65 #include of spe
2a00: 63 69 66 69 63 20 68 65 61 64 65 72 73 20 77 68 cific headers wh
2a10: 69 63 68 20 64 69 66 66 65 72 73 20 62 65 74 77 ich differs betw
2a20: 65 65 6e 0a 20 2a 20 70 6c 61 74 66 6f 72 6d 73 een. * platforms
2a30: 2e 0a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 .. */.#define SQ
2a40: 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 LITE_INT_TO_PTR(
2a50: 58 29 20 20 20 28 28 76 6f 69 64 2a 29 26 28 28 X) ((void*)&((
2a60: 63 68 61 72 2a 29 30 29 5b 58 5d 29 0a 23 64 65 char*)0)[X]).#de
2a70: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f fine SQLITE_PTR_
2a80: 54 4f 5f 49 4e 54 28 58 29 20 20 20 28 28 69 6e TO_INT(X) ((in
2a90: 74 29 28 28 28 63 68 61 72 2a 29 58 29 2d 28 63 t)(((char*)X)-(c
2aa0: 68 61 72 2a 29 30 29 29 0a 0a 2f 2a 0a 2a 2a 20 har*)0))../*.**
2ab0: 54 68 65 73 65 20 23 64 65 66 69 6e 65 73 20 73 These #defines s
2ac0: 68 6f 75 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 hould enable >2G
2ad0: 42 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 6f B file support o
2ae0: 6e 20 50 6f 73 69 78 20 69 66 20 74 68 65 0a 2a n Posix if the.*
2af0: 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 * underlying ope
2b00: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75 rating system su
2b10: 70 70 6f 72 74 73 20 69 74 2e 20 20 49 66 20 74 pports it. If t
2b20: 68 65 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c he OS lacks.** l
2b30: 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 arge file suppor
2b40: 74 2c 20 6f 72 20 69 66 20 74 68 65 20 4f 53 20 t, or if the OS
2b50: 69 73 20 77 69 6e 64 6f 77 73 2c 20 74 68 65 73 is windows, thes
2b60: 65 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f e should be no-o
2b70: 70 73 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 ps..**.** Ticket
2b80: 20 23 32 37 33 39 3a 20 20 54 68 65 20 5f 4c 41 #2739: The _LA
2b90: 52 47 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 6d RGEFILE_SOURCE m
2ba0: 61 63 72 6f 20 6d 75 73 74 20 61 70 70 65 61 72 acro must appear
2bb0: 20 62 65 66 6f 72 65 20 61 6e 79 0a 2a 2a 20 73 before any.** s
2bc0: 79 73 74 65 6d 20 23 69 6e 63 6c 75 64 65 73 2e ystem #includes.
2bd0: 20 20 48 65 6e 63 65 2c 20 74 68 69 73 20 62 6c Hence, this bl
2be0: 6f 63 6b 20 6f 66 20 63 6f 64 65 20 6d 75 73 74 ock of code must
2bf0: 20 62 65 20 74 68 65 20 76 65 72 79 20 66 69 72 be the very fir
2c00: 73 74 0a 2a 2a 20 63 6f 64 65 20 69 6e 20 61 6c st.** code in al
2c10: 6c 20 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 0a l source files..
2c20: 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65 **.** Large file
2c30: 20 73 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20 support can be
2c40: 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74 disabled using t
2c50: 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41 he -DSQLITE_DISA
2c60: 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a BLE_LFS switch.*
2c70: 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 * on the compile
2c80: 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20 r command line.
2c90: 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 This is necessa
2ca0: 72 79 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f ry if you are co
2cb0: 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20 mpiling.** on a
2cc0: 72 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28 recent machine (
2cd0: 65 78 3a 20 52 65 64 48 61 74 20 37 2e 32 29 20 ex: RedHat 7.2)
2ce0: 62 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f 75 but you want you
2cf0: 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a r code to work.*
2d00: 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61 * on an older ma
2d10: 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 chine (ex: RedHa
2d20: 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75 20 t 6.0). If you
2d30: 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 48 61 compile on RedHa
2d40: 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 74 t 7.2.** without
2d50: 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46 this option, LF
2d60: 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42 75 S is enable. Bu
2d70: 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20 65 t LFS does not e
2d80: 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72 6e xist in the kern
2d90: 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61 74 20 el.** in RedHat
2da0: 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f 64 65 6.0, so the code
2db0: 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65 won't work. He
2dc0: 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d nce, for maximum
2dd0: 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 74 61 binary.** porta
2de0: 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f 75 6c bility you shoul
2df0: 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a d omit LFS..**.*
2e00: 2a 20 53 69 6d 69 6c 61 72 20 69 73 20 74 72 75 * Similar is tru
2e10: 65 20 66 6f 72 20 4d 61 63 4f 53 2e 20 20 4c 46 e for MacOS. LF
2e20: 53 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 S is only suppor
2e30: 74 65 64 20 6f 6e 20 4d 61 63 4f 53 20 39 20 61 ted on MacOS 9 a
2e40: 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 69 66 nd later..*/.#if
2e50: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 ndef SQLITE_DISA
2e60: 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65 BLE_LFS.# define
2e70: 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20 _LARGE_FILE
2e80: 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46 1.# ifndef _F
2e90: 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a ILE_OFFSET_BITS.
2ea0: 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45 # define _FILE
2eb0: 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a _OFFSET_BITS 64.
2ec0: 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 # endif.# define
2ed0: 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52 _LARGEFILE_SOUR
2ee0: 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a CE 1.#endif.../*
2ef0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54 .** The SQLITE_T
2f00: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20 HREADSAFE macro
2f10: 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 must be defined
2f20: 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31 as either 0 or 1
2f30: 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69 ..** Older versi
2f40: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73 ons of SQLite us
2f50: 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54 ed an optional T
2f60: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e HREADSAFE macro.
2f70: 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74 .** We support t
2f80: 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a hat for legacy.*
2f90: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
2fa0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
2fb0: 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 ).#if defined(TH
2fc0: 52 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69 READSAFE).# defi
2fd0: 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 ne SQLITE_THREAD
2fe0: 53 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a SAFE THREADSAFE.
2ff0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
3000: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
3010: 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 1.#endif.#endif
3020: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ../*.** The SQLI
3030: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 TE_DEFAULT_MEMST
3040: 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20 ATUS macro must
3050: 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69 be defined as ei
3060: 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 ther 0 or 1..**
3070: 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 It determines wh
3080: 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
3090: 20 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65 features relate
30a0: 64 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f d to .** SQLITE_
30b0: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 CONFIG_MEMSTATUS
30c0: 20 61 72 65 20 61 76 61 69 6c 61 62 65 20 62 79 are availabe by
30d0: 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 2e default or not.
30e0: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 0a This value can.
30f0: 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e ** be overridden
3100: 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69 6e at runtime usin
3110: 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f g the sqlite3_co
3120: 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 nfig() API..*/.#
3130: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
3140: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 TE_DEFAULT_MEMST
3150: 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20 53 ATUS).# define S
3160: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 QLITE_DEFAULT_ME
3170: 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69 66 MSTATUS 1.#endif
3180: 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 20 ../*.** Exactly
3190: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
31a0: 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74 wing macros must
31b0: 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20 6f be defined in o
31c0: 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 69 rder to.** speci
31d0: 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 20 fy which memory
31e0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
31f0: 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a 0a stem to use..**.
3200: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53 59 ** SQLITE_SY
3210: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 STEM_MALLOC
3220: 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 6d // Use norm
3230: 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 al system malloc
3240: 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 ().** SQLITE
3250: 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20 20 _MEMDEBUG
3260: 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75 67 // Debug
3270: 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 20 ging version of
3280: 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a system malloc().
3290: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45 ** SQLITE_ME
32a0: 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20 MORY_SIZE
32b0: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c // internal
32c0: 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a 2a allocator #1.**
32d0: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 50 SQLITE_MMAP
32e0: 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20 20 _HEAP_SIZE
32f0: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 6d // internal m
3300: 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 0a map() allocator.
3310: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50 4f ** SQLITE_PO
3320: 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20 W2_MEMORY_SIZE
3330: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c // internal
3340: 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c power-of-two al
3350: 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 66 locator.**.** If
3360: 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f none of the abo
3370: 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 ve are defined,
3380: 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f then set SQLITE_
3390: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73 SYSTEM_MALLOC as
33a0: 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e .** the default.
33b0: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
33c0: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
33d0: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 LLOC)+defined(SQ
33e0: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c LITE_MEMDEBUG)+\
33f0: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c . defined(SQL
3400: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 ITE_MEMORY_SIZE)
3410: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f +defined(SQLITE_
3420: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b MMAP_HEAP_SIZE)+
3430: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 \. defined(SQ
3440: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 LITE_POW2_MEMORY
3450: 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f 72 _SIZE)>1.# error
3460: 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 "At most one of
3470: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 the following c
3480: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e 66 ompile-time conf
3490: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
34a0: 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 53 s\. is allows: S
34b0: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
34c0: 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 44 LOC, SQLITE_MEMD
34d0: 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 4d EBUG, SQLITE_MEM
34e0: 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c 49 ORY_SIZE,\. SQLI
34f0: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a TE_MMAP_HEAP_SIZ
3500: 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d E, SQLITE_POW2_M
3510: 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e 64 EMORY_SIZE".#end
3520: 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 if.#if defined(S
3530: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
3540: 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c LOC)+defined(SQL
3550: 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a ITE_MEMDEBUG)+\.
3560: 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 defined(SQLI
3570: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b TE_MEMORY_SIZE)+
3580: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
3590: 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c MAP_HEAP_SIZE)+\
35a0: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c . defined(SQL
35b0: 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f ITE_POW2_MEMORY_
35c0: 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 6e SIZE)==0.# defin
35d0: 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f e SQLITE_SYSTEM_
35e0: 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 0a MALLOC 1.#endif.
35f0: 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 ./*.** If SQLITE
3600: 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d _MALLOC_SOFT_LIM
3610: 49 54 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 IT is defined, t
3620: 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 70 20 hen try to keep
3630: 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f 66 20 the.** sizes of
3640: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
3650: 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20 76 61 ns below this va
3660: 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73 69 62 lue where possib
3670: 6c 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e le..*/.#if defin
3680: 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d ed(SQLITE_POW2_M
3690: 45 4d 4f 52 59 5f 53 49 5a 45 29 20 26 26 20 21 EMORY_SIZE) && !
36a0: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
36b0: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 ALLOC_SOFT_LIMIT
36c0: 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ).# define SQLIT
36d0: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 E_MALLOC_SOFT_LI
36e0: 4d 49 54 20 31 30 32 34 0a 23 65 6e 64 69 66 0a MIT 1024.#endif.
36f0: 0a 2f 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 ./*.** We need t
3700: 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f o define _XOPEN_
3710: 53 4f 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 SOURCE as follow
3720: 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e s in order to en
3730: 61 62 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 able.** recursiv
3740: 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 e mutexes on mos
3750: 74 20 75 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 t unix systems.
3760: 20 42 75 74 20 4d 61 63 20 4f 53 20 58 20 69 73 But Mac OS X is
3770: 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 different..** T
3780: 68 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 he _XOPEN_SOURCE
3790: 20 64 65 66 69 6e 65 20 63 61 75 73 65 73 20 70 define causes p
37a0: 72 6f 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 roblems for Mac
37b0: 4f 53 20 58 20 77 65 20 61 72 65 20 74 6f 6c 64 OS X we are told
37c0: 2c 0a 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d ,.** so it is om
37d0: 69 74 74 65 64 20 74 68 65 72 65 2e 20 20 53 65 itted there. Se
37e0: 65 20 74 69 63 6b 65 74 20 23 32 36 37 33 2e 0a e ticket #2673..
37f0: 2a 2a 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c **.** Later we l
3800: 65 61 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e earn that _XOPEN
3810: 5f 53 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c _SOURCE is poorl
3820: 79 20 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 y or incorrectly
3830: 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 .** implemented
3840: 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e on some systems.
3850: 20 20 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65 So we avoid de
3860: 66 69 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c fining it at all
3870: 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 6c 72 .** if it is alr
3880: 65 61 64 79 20 64 65 66 69 6e 65 64 20 6f 72 20 eady defined or
3890: 69 66 20 69 74 20 69 73 20 75 6e 6e 65 65 64 65 if it is unneede
38a0: 64 20 62 65 63 61 75 73 65 20 77 65 20 61 72 65 d because we are
38b0: 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 .** not doing a
38c0: 74 68 72 65 61 64 73 61 66 65 20 62 75 69 6c 64 threadsafe build
38d0: 2e 20 20 54 69 63 6b 65 74 20 23 32 36 38 31 2e . Ticket #2681.
38e0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 .**.** See also
38f0: 74 69 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f ticket #2741..*/
3900: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 5f 58 .#if !defined(_X
3910: 4f 50 45 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 OPEN_SOURCE) &&
3920: 21 64 65 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 !defined(__DARWI
3930: 4e 5f 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64 N__) && !defined
3940: 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
3950: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
3960: 0a 23 20 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 .# define _XOPE
3970: 4e 5f 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a N_SOURCE 500 /*
3980: 20 4e 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c Needed to enabl
3990: 65 20 70 74 68 72 65 61 64 20 72 65 63 75 72 73 e pthread recurs
39a0: 69 76 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 ive mutexes */.#
39b0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
39c0: 20 54 43 4c 20 68 65 61 64 65 72 73 20 61 72 65 TCL headers are
39d0: 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 77 68 65 only needed whe
39e0: 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 n compiling the
39f0: 54 43 4c 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f TCL bindings..*/
3a00: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
3a10: 49 54 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 ITE_TCL) || defi
3a20: 6e 65 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 ned(TCLSH).# inc
3a30: 6c 75 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e lude <tcl.h>.#en
3a40: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 dif../*.** Many
3a50: 70 65 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69 people are faili
3a60: 6e 67 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 ng to set -DNDEB
3a70: 55 47 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c UG=1 when compil
3a80: 69 6e 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 ing SQLite..** S
3a90: 65 74 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 etting NDEBUG ma
3aa0: 6b 65 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61 kes the code sma
3ab0: 6c 6c 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73 ller and run fas
3ac0: 74 65 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c ter. So the fol
3ad0: 6c 6f 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 lowing.** lines
3ae0: 61 72 65 20 61 64 64 65 64 20 74 6f 20 61 75 74 are added to aut
3af0: 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e omatically set N
3b00: 44 45 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65 DEBUG unless the
3b10: 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d -DSQLITE_DEBUG=
3b20: 31 0a 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 1.** option is s
3b30: 65 74 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 et. Thus NDEBUG
3b40: 20 62 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d becomes an opt-
3b50: 69 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 in rather than a
3b60: 6e 20 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 n opt-out.** fea
3b70: 74 75 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 ture..*/.#if !de
3b80: 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 fined(NDEBUG) &&
3b90: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
3ba0: 5f 44 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e _DEBUG) .# defin
3bb0: 65 20 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 e NDEBUG 1.#endi
3bc0: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
3bd0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 ** Include sqlit
3be0: 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 e3.h in the midd
3bf0: 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e le of sqliteInt.
3c00: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
3c10: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
3c20: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 ** Begin file sq
3c30: 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a lite3.h ********
3c40: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
3c50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
3c60: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
3c70: 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a ptember 15.**.**
3c80: 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
3c90: 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
3ca0: 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
3cb0: 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
3cc0: 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
3cd0: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
3ce0: 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
3cf0: 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
3d00: 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
3d10: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
3d20: 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
3d30: 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
3d40: 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
3d50: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
3d60: 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
3d70: 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
3d80: 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
3d90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
3da0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
3db0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
3dc0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
3dd0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
3de0: 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 s header file de
3df0: 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 fines the interf
3e00: 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51 4c ace that the SQL
3e10: 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70 ite library.** p
3e20: 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e resents to clien
3e30: 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20 t programs. If
3e40: 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74 a C-function, st
3e50: 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 70 ructure, datatyp
3e60: 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e e,.** or constan
3e70: 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65 t definition doe
3e80: 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 s not appear in
3e90: 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 this file, then
3ea0: 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 it is.** not a p
3eb0: 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20 ublished API of
3ec0: 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65 SQLite, is subje
3ed0: 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 ct to change wit
3ee0: 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20 hout.** notice,
3ef0: 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 and should not b
3f00: 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 e referenced by
3f10: 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73 programs that us
3f20: 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 e SQLite..**.**
3f30: 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69 Some of the defi
3f40: 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 nitions that are
3f50: 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72 in this file ar
3f60: 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22 e marked as.** "
3f70: 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20 experimental".
3f80: 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 Experimental int
3f90: 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d erfaces are norm
3fa0: 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74 ally new.** feat
3fb0: 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64 ures recently ad
3fc0: 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20 ded to SQLite.
3fd0: 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69 We do not antici
3fe0: 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 pate changes.**
3ff0: 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 to experimental
4000: 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 72 interfaces but r
4010: 65 73 65 72 76 65 20 74 6f 20 6d 61 6b 65 20 6d eserve to make m
4020: 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 69 66 0a inor changes if.
4030: 2a 2a 20 65 78 70 65 72 69 65 6e 63 65 20 66 72 ** experience fr
4040: 6f 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20 77 om use "in the w
4050: 69 6c 64 22 20 73 75 67 67 65 73 74 20 73 75 63 ild" suggest suc
4060: 68 20 63 68 61 6e 67 65 73 20 61 72 65 20 70 72 h changes are pr
4070: 75 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 udent..**.** The
4080: 20 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e 67 official C-lang
4090: 75 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65 6e uage API documen
40a0: 74 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69 74 tation for SQLit
40b0: 65 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 e is derived.**
40c0: 66 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 6e from comments in
40d0: 20 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68 69 this file. Thi
40e0: 73 20 66 69 6c 65 20 69 73 20 74 68 65 20 61 75 s file is the au
40f0: 74 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75 72 thoritative sour
4100: 63 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 4c ce.** on how SQL
4110: 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 ite interfaces a
4120: 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f 70 re suppose to op
4130: 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 erate..**.** The
4140: 20 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 69 name of this fi
4150: 6c 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67 75 le under configu
4160: 72 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e ration managemen
4170: 74 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e 69 t is "sqlite.h.i
4180: 6e 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66 n"..** The makef
4190: 69 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20 6d ile makes some m
41a0: 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 inor changes to
41b0: 74 68 69 73 20 66 69 6c 65 20 28 73 75 63 68 20 this file (such
41c0: 61 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a 20 as inserting.**
41d0: 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 the version numb
41e0: 65 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73 20 er) and changes
41f0: 69 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71 6c its name to "sql
4200: 69 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70 61 ite3.h" as.** pa
4210: 72 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64 20 rt of the build
4220: 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 process..**.** @
4230: 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65 2e (#) $Id: sqlite.
4240: 68 2e 69 6e 2c 76 20 31 2e 34 31 35 20 32 30 30 h.in,v 1.415 200
4250: 38 2f 31 31 2f 31 39 20 30 31 3a 32 30 3a 32 36 8/11/19 01:20:26
4260: 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 drh Exp $.*/.#i
4270: 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 5f 48 fndef _SQLITE3_H
4280: 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 _.#define _SQLIT
4290: 45 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65 20 3c E3_H_.#include <
42a0: 73 74 64 61 72 67 2e 68 3e 20 20 20 20 20 2f 2a stdarg.h> /*
42b0: 20 4e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 Needed for the
42c0: 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 76 61 definition of va
42d0: 5f 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 _list */../*.**
42e0: 4d 61 6b 65 20 73 75 72 65 20 77 65 20 63 61 6e Make sure we can
42f0: 20 63 61 6c 6c 20 74 68 69 73 20 73 74 75 66 66 call this stuff
4300: 20 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23 69 from C++..*/.#i
4310: 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b f 0.extern "C" {
4320: 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
4330: 41 64 64 20 74 68 65 20 61 62 69 6c 69 74 79 20 Add the ability
4340: 74 6f 20 6f 76 65 72 72 69 64 65 20 27 65 78 74 to override 'ext
4350: 65 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ern'.*/.#ifndef
4360: 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a 23 20 SQLITE_EXTERN.#
4370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 define SQLITE_EX
4380: 54 45 52 4e 20 65 78 74 65 72 6e 0a 23 65 6e 64 TERN extern.#end
4390: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 if../*.** These
43a0: 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61 72 65 no-op macros are
43b0: 20 75 73 65 64 20 69 6e 20 66 72 6f 6e 74 20 6f used in front o
43c0: 66 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f 20 f interfaces to
43d0: 6d 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20 69 6e mark those.** in
43e0: 74 65 72 66 61 63 65 73 20 61 73 20 65 69 74 68 terfaces as eith
43f0: 65 72 20 64 65 70 72 65 63 61 74 65 64 20 6f 72 er deprecated or
4400: 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 experimental.
4410: 4e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 New applications
4420: 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 .** should not u
4430: 73 65 20 64 65 70 72 65 63 61 74 65 64 20 69 6e se deprecated in
4440: 74 72 66 61 63 65 73 20 2d 20 74 68 65 79 20 61 trfaces - they a
4450: 72 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 62 re support for b
4460: 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 ackwards.** comp
4470: 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 20 atibility only.
4480: 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 72 69 Application wri
4490: 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 61 ters should be a
44a0: 77 61 72 65 20 74 68 61 74 0a 2a 2a 20 65 78 70 ware that.** exp
44b0: 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 erimental interf
44c0: 61 63 65 73 20 61 72 65 20 73 75 62 6a 65 63 74 aces are subject
44d0: 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 70 6f to change in po
44e0: 69 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a int releases..**
44f0: 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 .** These macros
4500: 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 used to resolve
4510: 20 74 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e 64 to various kind
4520: 73 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d 61 s of compiler ma
4530: 67 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75 6c gic that.** woul
4540: 64 20 67 65 6e 65 72 61 74 65 20 77 61 72 6e 69 d generate warni
4550: 6e 67 20 6d 65 73 73 61 67 65 73 20 77 68 65 6e ng messages when
4560: 20 74 68 65 79 20 77 65 72 65 20 75 73 65 64 2e they were used.
4570: 20 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 63 6f But that.** co
4580: 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 65 6e 64 mpiler magic end
4590: 65 64 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67 ed up generating
45a0: 20 73 75 63 68 20 61 20 66 6c 75 72 72 79 20 6f such a flurry o
45b0: 66 20 62 75 67 20 72 65 70 6f 72 74 73 0a 2a 2a f bug reports.**
45c0: 20 74 68 61 74 20 77 65 20 68 61 76 65 20 74 61 that we have ta
45d0: 6b 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20 61 ken it all out a
45e0: 6e 64 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f 20 nd gone back to
45f0: 75 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a 20 using simple.**
4600: 6e 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a noop macros..*/.
4610: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
4620: 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e EPRECATED.#defin
4630: 65 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d e SQLITE_EXPERIM
4640: 45 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 ENTAL../*.** Ens
4650: 75 72 65 20 74 68 65 73 65 20 73 79 6d 62 6f 6c ure these symbol
4660: 73 20 77 65 72 65 20 6e 6f 74 20 64 65 66 69 6e s were not defin
4670: 65 64 20 62 79 20 73 6f 6d 65 20 70 72 65 76 69 ed by some previ
4680: 6f 75 73 20 68 65 61 64 65 72 20 66 69 6c 65 2e ous header file.
4690: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
46a0: 45 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64 65 E_VERSION.# unde
46b0: 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e f SQLITE_VERSION
46c0: 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
46d0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 QLITE_VERSION_NU
46e0: 4d 42 45 52 0a 23 20 75 6e 64 65 66 20 53 51 4c MBER.# undef SQL
46f0: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 ITE_VERSION_NUMB
4700: 45 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a ER.#endif../*.**
4710: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 CAPI3REF: Compi
4720: 6c 65 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 le-Time Library
4730: 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 Version Numbers
4740: 7b 48 31 30 30 31 30 7d 20 3c 53 36 30 31 30 30 {H10010} <S60100
4750: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 >.**.** The SQLI
4760: 54 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20 53 TE_VERSION and S
4770: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 QLITE_VERSION_NU
4780: 4d 42 45 52 20 23 64 65 66 69 6e 65 73 20 69 6e MBER #defines in
4790: 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 2e .** the sqlite3.
47a0: 68 20 66 69 6c 65 20 73 70 65 63 69 66 79 20 74 h file specify t
47b0: 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 he version of SQ
47c0: 4c 69 74 65 20 77 69 74 68 20 77 68 69 63 68 0a Lite with which.
47d0: 2a 2a 20 74 68 61 74 20 68 65 61 64 65 72 20 66 ** that header f
47e0: 69 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 ile is associate
47f0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76 65 d..**.** The "ve
4800: 72 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74 65 rsion" of SQLite
4810: 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 66 20 is a string of
4820: 74 68 65 20 66 6f 72 6d 20 22 58 2e 59 2e 5a 22 the form "X.Y.Z"
4830: 2e 0a 2a 2a 20 54 68 65 20 70 68 72 61 73 65 20 ..** The phrase
4840: 22 61 6c 70 68 61 22 20 6f 72 20 22 62 65 74 61 "alpha" or "beta
4850: 22 20 6d 69 67 68 74 20 62 65 20 61 70 70 65 6e " might be appen
4860: 64 65 64 20 61 66 74 65 72 20 74 68 65 20 5a 2e ded after the Z.
4870: 0a 2a 2a 20 54 68 65 20 58 20 76 61 6c 75 65 20 .** The X value
4880: 69 73 20 6d 61 6a 6f 72 20 76 65 72 73 69 6f 6e is major version
4890: 20 6e 75 6d 62 65 72 20 61 6c 77 61 79 73 20 33 number always 3
48a0: 20 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 in SQLite3..**
48b0: 54 68 65 20 58 20 76 61 6c 75 65 20 6f 6e 6c 79 The X value only
48c0: 20 63 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 changes when ba
48d0: 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 ckwards compatib
48e0: 69 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b ility is.** brok
48f0: 65 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 en and we intend
4900: 20 74 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 to never break
4910: 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 backwards compat
4920: 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 ibility..** The
4930: 59 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d Y value is the m
4940: 69 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d inor version num
4950: 62 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 ber and only cha
4960: 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 nges when.** the
4970: 72 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 re are major fea
4980: 74 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 ture enhancement
4990: 73 20 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 s that are forwa
49a0: 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a rds compatible.*
49b0: 2a 20 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 * but not backwa
49c0: 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a rds compatible..
49d0: 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 20 69 ** The Z value i
49e0: 73 20 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 s the release nu
49f0: 6d 62 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 mber and is incr
4a00: 65 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 emented with.**
4a10: 65 61 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 each release but
4a20: 20 72 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 resets back to
4a30: 30 20 77 68 65 6e 65 76 65 72 20 59 20 69 73 20 0 whenever Y is
4a40: 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a incremented..**.
4a50: 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
4a60: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e lite3_libversion
4a70: 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
4a80: 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 _libversion_numb
4a90: 65 72 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 er()]..**.** INV
4aa0: 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
4ab0: 48 31 30 30 31 31 7d 20 54 68 65 20 53 51 4c 49 H10011} The SQLI
4ac0: 54 45 5f 56 45 52 53 49 4f 4e 20 23 64 65 66 69 TE_VERSION #defi
4ad0: 6e 65 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 ne in the sqlite
4ae0: 33 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 20 3.h header file
4af0: 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 shall.**
4b00: 20 20 65 76 61 6c 75 61 74 65 20 74 6f 20 61 20 evaluate to a
4b10: 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 74 string literal t
4b20: 68 61 74 20 69 73 20 74 68 65 20 53 51 4c 69 74 hat is the SQLit
4b30: 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 e version.**
4b40: 20 20 20 20 20 20 77 69 74 68 20 77 68 69 63 68 with which
4b50: 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 the header file
4b60: 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 2e 0a is associated..
4b70: 2a 2a 0a 2a 2a 20 7b 48 31 30 30 31 34 7d 20 54 **.** {H10014} T
4b80: 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f he SQLITE_VERSIO
4b90: 4e 5f 4e 55 4d 42 45 52 20 23 64 65 66 69 6e 65 N_NUMBER #define
4ba0: 20 73 68 61 6c 6c 20 72 65 73 6f 6c 76 65 20 74 shall resolve t
4bb0: 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 o an integer.**
4bc0: 20 20 20 20 20 20 20 20 20 77 69 74 68 20 74 68 with th
4bd0: 65 20 76 61 6c 75 65 20 28 58 2a 31 30 30 30 30 e value (X*10000
4be0: 30 30 20 2b 20 59 2a 31 30 30 30 20 2b 20 5a 29 00 + Y*1000 + Z)
4bf0: 20 77 68 65 72 65 20 58 2c 20 59 2c 20 61 6e 64 where X, Y, and
4c00: 20 5a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 Z.** a
4c10: 72 65 20 74 68 65 20 6d 61 6a 6f 72 20 76 65 72 re the major ver
4c20: 73 69 6f 6e 2c 20 6d 69 6e 6f 72 20 76 65 72 73 sion, minor vers
4c30: 69 6f 6e 2c 20 61 6e 64 20 72 65 6c 65 61 73 65 ion, and release
4c40: 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 number..*/.#def
4c50: 69 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 ine SQLITE_VERSI
4c60: 4f 4e 20 20 20 20 20 20 20 20 20 22 33 2e 36 2e ON "3.6.
4c70: 36 2e 31 22 0a 23 64 65 66 69 6e 65 20 53 51 4c 6.1".#define SQL
4c80: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 ITE_VERSION_NUMB
4c90: 45 52 20 20 33 30 30 36 30 30 36 0a 0a 2f 2a 0a ER 3006006../*.
4ca0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e ** CAPI3REF: Run
4cb0: 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65 -Time Library Ve
4cc0: 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48 rsion Numbers {H
4cd0: 31 30 30 32 30 7d 20 3c 53 36 30 31 30 30 3e 0a 10020} <S60100>.
4ce0: 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c ** KEYWORDS: sql
4cf0: 69 74 65 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a 0a ite3_version.**.
4d00: 2a 2a 20 54 68 65 73 65 20 66 65 61 74 75 72 65 ** These feature
4d10: 73 20 70 72 6f 76 69 64 65 20 74 68 65 20 73 61 s provide the sa
4d20: 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 me information a
4d30: 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 56 45 s the [SQLITE_VE
4d40: 52 53 49 4f 4e 5d 0a 2a 2a 20 61 6e 64 20 5b 53 RSION].** and [S
4d50: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 QLITE_VERSION_NU
4d60: 4d 42 45 52 5d 20 23 64 65 66 69 6e 65 73 20 69 MBER] #defines i
4d70: 6e 20 74 68 65 20 68 65 61 64 65 72 2c 20 62 75 n the header, bu
4d80: 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 t are associated
4d90: 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6c 69 62 .** with the lib
4da0: 72 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 rary instead of
4db0: 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e the header file.
4dc0: 20 20 43 61 75 74 69 6f 75 73 20 70 72 6f 67 72 Cautious progr
4dd0: 61 6d 6d 65 72 73 20 6d 69 67 68 74 0a 2a 2a 20 ammers might.**
4de0: 69 6e 63 6c 75 64 65 20 61 20 63 68 65 63 6b 20 include a check
4df0: 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69 63 61 in their applica
4e00: 74 69 6f 6e 20 74 6f 20 76 65 72 69 66 79 20 74 tion to verify t
4e10: 68 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c hat.** sqlite3_l
4e20: 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 ibversion_number
4e30: 28 29 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e () always return
4e40: 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 5b s the value.** [
4e50: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e SQLITE_VERSION_N
4e60: 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 UMBER]..**.** Th
4e70: 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 e sqlite3_libver
4e80: 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 sion() function
4e90: 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 returns the same
4ea0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 information as
4eb0: 69 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c is.** in the sql
4ec0: 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 ite3_version[] s
4ed0: 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 2e 20 tring constant.
4ee0: 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 The function is
4ef0: 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 66 6f 72 provided.** for
4f00: 20 75 73 65 20 69 6e 20 44 4c 4c 73 20 73 69 6e use in DLLs sin
4f10: 63 65 20 44 4c 4c 20 75 73 65 72 73 20 75 73 75 ce DLL users usu
4f20: 61 6c 6c 79 20 64 6f 20 6e 6f 74 20 68 61 76 65 ally do not have
4f30: 20 64 69 72 65 63 74 20 61 63 63 65 73 73 20 74 direct access t
4f40: 6f 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 o string.** cons
4f50: 74 61 6e 74 73 20 77 69 74 68 69 6e 20 74 68 65 tants within the
4f60: 20 44 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 DLL..**.** INVA
4f70: 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
4f80: 31 30 30 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 10021} The [sqli
4f90: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e te3_libversion_n
4fa0: 75 6d 62 65 72 28 29 5d 20 69 6e 74 65 72 66 61 umber()] interfa
4fb0: 63 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a ce shall return.
4fc0: 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 20 69 ** an i
4fd0: 6e 74 65 67 65 72 20 65 71 75 61 6c 20 74 6f 20 nteger equal to
4fe0: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f [SQLITE_VERSION_
4ff0: 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b NUMBER]..**.** {
5000: 48 31 30 30 32 32 7d 20 54 68 65 20 5b 73 71 6c H10022} The [sql
5010: 69 74 65 33 5f 76 65 72 73 69 6f 6e 5d 20 73 74 ite3_version] st
5020: 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 73 68 ring constant sh
5030: 61 6c 6c 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 20 all contain.**
5040: 20 20 20 20 20 20 20 20 74 68 65 20 74 65 78 74 the text
5050: 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f of the [SQLITE_
5060: 56 45 52 53 49 4f 4e 5d 20 73 74 72 69 6e 67 2e VERSION] string.
5070: 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 30 32 33 7d 20 .**.** {H10023}
5080: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 The [sqlite3_lib
5090: 76 65 72 73 69 6f 6e 28 29 5d 20 66 75 6e 63 74 version()] funct
50a0: 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72 6e ion shall return
50b0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 70 .** a p
50c0: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 ointer to the [s
50d0: 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5d 20 qlite3_version]
50e0: 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 2e string constant.
50f0: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
5100: 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 onst char sqlite
5110: 33 5f 76 65 72 73 69 6f 6e 5b 5d 3b 0a 53 51 4c 3_version[];.SQL
5120: 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
5130: 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76 ar *sqlite3_libv
5140: 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 53 51 ersion(void);.SQ
5150: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
5160: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f ite3_libversion_
5170: 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a 0a 2f number(void);../
5180: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 *.** CAPI3REF: T
5190: 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 54 68 est To See If Th
51a0: 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 68 72 e Library Is Thr
51b0: 65 61 64 73 61 66 65 20 7b 48 31 30 31 30 30 7d eadsafe {H10100}
51c0: 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S60100>.**.**
51d0: 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20 63 6f SQLite can be co
51e0: 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72 20 77 mpiled with or w
51f0: 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73 2e 20 ithout mutexes.
5200: 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 53 51 When.** the [SQ
5210: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d LITE_THREADSAFE]
5220: 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 C preprocessor
5230: 6d 61 63 72 6f 20 31 20 6f 72 20 32 2c 20 6d 75 macro 1 or 2, mu
5240: 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 texes.** are ena
5250: 62 6c 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20 bled and SQLite
5260: 69 73 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 is threadsafe.
5270: 57 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53 51 4c When the.** [SQL
5280: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 ITE_THREADSAFE]
5290: 6d 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a 2a 20 macro is 0, .**
52a0: 74 68 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 the mutexes are
52b0: 6f 6d 69 74 74 65 64 2e 20 20 57 69 74 68 6f 75 omitted. Withou
52c0: 74 20 74 68 65 20 6d 75 74 65 78 65 73 2c 20 69 t the mutexes, i
52d0: 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a 2a 2a t is not safe.**
52e0: 20 74 6f 20 75 73 65 20 53 51 4c 69 74 65 20 63 to use SQLite c
52f0: 6f 6e 63 75 72 72 65 6e 74 6c 79 20 66 72 6f 6d oncurrently from
5300: 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 74 more than one t
5310: 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45 6e 61 hread..**.** Ena
5320: 62 6c 69 6e 67 20 6d 75 74 65 78 65 73 20 69 6e bling mutexes in
5330: 63 75 72 73 20 61 20 6d 65 61 73 75 72 61 62 6c curs a measurabl
5340: 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 70 65 e performance pe
5350: 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69 66 20 nalty..** So if
5360: 73 70 65 65 64 20 69 73 20 6f 66 20 75 74 6d 6f speed is of utmo
5370: 73 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c 20 69 st importance, i
5380: 74 20 6d 61 6b 65 73 20 73 65 6e 73 65 20 74 6f t makes sense to
5390: 20 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68 65 20 disable.** the
53a0: 6d 75 74 65 78 65 73 2e 20 20 42 75 74 20 66 6f mutexes. But fo
53b0: 72 20 6d 61 78 69 6d 75 6d 20 73 61 66 65 74 79 r maximum safety
53c0: 2c 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 , mutexes should
53d0: 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 20 be enabled..**
53e0: 54 68 65 20 64 65 66 61 75 6c 74 20 62 65 68 61 The default beha
53f0: 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75 74 65 vior is for mute
5400: 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62 6c 65 xes to be enable
5410: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e d..**.** This in
5420: 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 terface can be u
5430: 73 65 64 20 62 79 20 61 20 70 72 6f 67 72 61 6d sed by a program
5440: 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
5450: 61 74 20 74 68 65 0a 2a 2a 20 76 65 72 73 69 6f at the.** versio
5460: 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68 61 74 n of SQLite that
5470: 20 69 74 20 69 73 20 6c 69 6e 6b 69 6e 67 20 61 it is linking a
5480: 67 61 69 6e 73 74 20 77 61 73 20 63 6f 6d 70 69 gainst was compi
5490: 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 led with.** the
54a0: 64 65 73 69 72 65 64 20 73 65 74 74 69 6e 67 20 desired setting
54b0: 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 of the [SQLITE_T
54c0: 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f HREADSAFE] macro
54d0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 ..**.** This int
54e0: 65 72 66 61 63 65 20 6f 6e 6c 79 20 72 65 70 6f erface only repo
54f0: 72 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 rts on the compi
5500: 6c 65 2d 74 69 6d 65 20 6d 75 74 65 78 20 73 65 le-time mutex se
5510: 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 tting.** of the
5520: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
5530: 46 45 5d 20 66 6c 61 67 2e 20 20 49 66 20 53 51 FE] flag. If SQ
5540: 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
5550: 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f with.** SQLITE_
5560: 54 48 52 45 41 44 53 41 46 45 3d 31 20 74 68 65 THREADSAFE=1 the
5570: 6e 20 6d 75 74 65 78 65 73 20 61 72 65 20 65 6e n mutexes are en
5580: 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
5590: 20 62 75 74 0a 2a 2a 20 63 61 6e 20 62 65 20 66 but.** can be f
55a0: 75 6c 6c 79 20 6f 72 20 70 61 72 74 69 61 6c 6c ully or partiall
55b0: 79 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 y disabled using
55c0: 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 a call to [sqli
55d0: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 0a 2a 2a te3_config()].**
55e0: 20 77 69 74 68 20 74 68 65 20 76 65 72 62 73 20 with the verbs
55f0: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 [SQLITE_CONFIG_S
5600: 49 4e 47 4c 45 54 48 52 45 41 44 5d 2c 20 5b 53 INGLETHREAD], [S
5610: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c QLITE_CONFIG_MUL
5620: 54 49 54 48 52 45 41 44 5d 2c 0a 2a 2a 20 6f 72 TITHREAD],.** or
5630: 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
5640: 4d 55 54 45 58 5d 2e 20 20 54 68 65 20 72 65 74 MUTEX]. The ret
5650: 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 urn value of thi
5660: 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 77 73 s function shows
5670: 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65 20 64 65 66 .** only the def
5680: 61 75 6c 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d ault compile-tim
5690: 65 20 73 65 74 74 69 6e 67 2c 20 6e 6f 74 20 61 e setting, not a
56a0: 6e 79 20 72 75 6e 2d 74 69 6d 65 20 63 68 61 6e ny run-time chan
56b0: 67 65 73 0a 2a 2a 20 74 6f 20 74 68 61 74 20 73 ges.** to that s
56c0: 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 65 etting..**.** Se
56d0: 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 e the [threading
56e0: 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 61 mode] documenta
56f0: 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f tion for additio
5700: 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
5710: 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 .**.** INVARIANT
5720: 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 31 30 31 S:.**.** {H10101
5730: 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 } The [sqlite3_t
5740: 68 72 65 61 64 73 61 66 65 28 29 5d 20 66 75 6e hreadsafe()] fun
5750: 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 ction shall retu
5760: 72 6e 20 7a 65 72 6f 20 69 66 0a 2a 2a 20 20 20 rn zero if.**
5770: 20 20 20 20 20 20 20 61 6e 64 20 6f 6e 6c 79 20 and only
5780: 69 66 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f if SQLite was co
5790: 6d 70 69 6c 65 64 20 77 69 74 68 20 6d 75 74 65 mpiled with mute
57a0: 78 69 6e 67 20 63 6f 64 65 20 6f 6d 69 74 74 65 xing code omitte
57b0: 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 31 30 32 d..**.** {H10102
57c0: 7d 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 } The value retu
57d0: 72 6e 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c rned by the [sql
57e0: 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 ite3_threadsafe(
57f0: 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 )] function.**
5800: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 72 65 shall re
5810: 6d 61 69 6e 20 74 68 65 20 73 61 6d 65 20 61 63 main the same ac
5820: 72 6f 73 73 20 63 61 6c 6c 73 20 74 6f 20 5b 73 ross calls to [s
5830: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
5840: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
5850: 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 int sqlite3_thre
5860: 61 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f adsafe(void);../
5870: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
5880: 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 atabase Connecti
5890: 6f 6e 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30 on Handle {H1200
58a0: 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 0} <S40200>.** K
58b0: 45 59 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 EYWORDS: {databa
58c0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b se connection} {
58d0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
58e0: 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 ions}.**.** Each
58f0: 20 6f 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 open SQLite dat
5900: 61 62 61 73 65 20 69 73 20 72 65 70 72 65 73 65 abase is represe
5910: 6e 74 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 nted by a pointe
5920: 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 r to an instance
5930: 20 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 of.** the opaqu
5940: 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 e structure name
5950: 64 20 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 d "sqlite3". It
5960: 20 69 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 is useful to th
5970: 69 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 ink of an sqlite
5980: 33 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 3.** pointer as
5990: 61 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 an object. The
59a0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
59b0: 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 , [sqlite3_open1
59c0: 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6()], and.** [sq
59d0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
59e0: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 interfaces are
59f0: 69 74 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 its constructors
5a00: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 , and [sqlite3_c
5a10: 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74 lose()].** is it
5a20: 73 20 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54 s destructor. T
5a30: 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74 here are many ot
5a40: 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 28 her interfaces (
5a50: 73 75 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 such as.** [sqli
5a60: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
5a70: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 ], [sqlite3_crea
5a80: 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 te_function()],
5a90: 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f and.** [sqlite3_
5aa0: 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 busy_timeout()]
5ab0: 74 6f 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65 to name but thre
5ac0: 65 29 20 74 68 61 74 20 61 72 65 20 6d 65 74 68 e) that are meth
5ad0: 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c ods on an.** sql
5ae0: 69 74 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a ite3 object..*/.
5af0: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
5b00: 71 6c 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a qlite3 sqlite3;.
5b10: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
5b20: 20 36 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 64-Bit Integer
5b30: 54 79 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c Types {H10200} <
5b40: 53 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f S10110>.** KEYWO
5b50: 52 44 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36 RDS: sqlite_int6
5b60: 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a 4 sqlite_uint64.
5b70: 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 **.** Because th
5b80: 65 72 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d ere is no cross-
5b90: 70 6c 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 platform way to
5ba0: 73 70 65 63 69 66 79 20 36 34 2d 62 69 74 20 69 specify 64-bit i
5bb0: 6e 74 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20 nteger types.**
5bc0: 53 51 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 SQLite includes
5bd0: 74 79 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d typedefs for 64-
5be0: 62 69 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 bit signed and u
5bf0: 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 nsigned integers
5c00: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
5c10: 74 65 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 te3_int64 and sq
5c20: 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65 lite3_uint64 are
5c30: 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 the preferred t
5c40: 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e ype definitions.
5c50: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69 .** The sqlite_i
5c60: 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f nt64 and sqlite_
5c70: 75 69 6e 74 36 34 20 74 79 70 65 73 20 61 72 65 uint64 types are
5c80: 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62 supported for b
5c90: 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 ackwards.** comp
5ca0: 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a atibility only..
5cb0: 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
5cc0: 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30 31 7d :.**.** {H10201}
5cd0: 20 54 68 65 20 5b 73 71 6c 69 74 65 5f 69 6e 74 The [sqlite_int
5ce0: 36 34 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 64] and [sqlite3
5cf0: 5f 69 6e 74 36 34 5d 20 74 79 70 65 20 73 68 61 _int64] type sha
5d00: 6c 6c 20 73 70 65 63 69 66 79 0a 2a 2a 20 20 20 ll specify.**
5d10: 20 20 20 20 20 20 20 61 20 36 34 2d 62 69 74 20 a 64-bit
5d20: 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a signed integer..
5d30: 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30 32 7d 20 54 **.** {H10202} T
5d40: 68 65 20 5b 73 71 6c 69 74 65 5f 75 69 6e 74 36 he [sqlite_uint6
5d50: 34 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 4] and [sqlite3_
5d60: 75 69 6e 74 36 34 5d 20 74 79 70 65 20 73 68 61 uint64] type sha
5d70: 6c 6c 20 73 70 65 63 69 66 79 0a 2a 2a 20 20 20 ll specify.**
5d80: 20 20 20 20 20 20 20 61 20 36 34 2d 62 69 74 20 a 64-bit
5d90: 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
5da0: 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
5db0: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20 20 TE_INT64_TYPE.
5dc0: 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f 49 typedef SQLITE_I
5dd0: 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65 NT64_TYPE sqlite
5de0: 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 _int64;. typede
5df0: 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49 54 f unsigned SQLIT
5e00: 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c E_INT64_TYPE sql
5e10: 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 69 ite_uint64;.#eli
5e20: 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 f defined(_MSC_V
5e30: 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f ER) || defined(_
5e40: 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20 74 _BORLANDC__). t
5e50: 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20 73 ypedef __int64 s
5e60: 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 qlite_int64;. t
5e70: 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 ypedef unsigned
5e80: 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 __int64 sqlite_u
5e90: 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20 74 int64;.#else. t
5ea0: 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e 67 ypedef long long
5eb0: 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 int sqlite_int6
5ec0: 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 4;. typedef uns
5ed0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
5ee0: 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 int sqlite_uint6
5ef0: 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65 4;.#endif.typede
5f00: 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 f sqlite_int64 s
5f10: 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a 74 79 qlite3_int64;.ty
5f20: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e pedef sqlite_uin
5f30: 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 t64 sqlite3_uint
5f40: 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 64;../*.** If co
5f50: 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 mpiling for a pr
5f60: 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63 ocessor that lac
5f70: 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e ks floating poin
5f80: 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 t support,.** su
5f90: 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72 bstitute integer
5fa0: 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f for floating-po
5fb0: 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 int..*/.#ifdef S
5fc0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
5fd0: 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69 ING_POINT.# defi
5fe0: 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 ne double sqlite
5ff0: 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 0a 0a 3_int64.#endif..
6000: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
6010: 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 62 61 Closing A Databa
6020: 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 se Connection {H
6030: 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30 3e 3c 12010} <S30100><
6040: 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S40200>.**.** Th
6050: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
6060: 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 e destructor for
6070: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f the [sqlite3] o
6080: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 bject..**.** App
6090: 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 lications should
60a0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
60b0: 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61 ze | finalize] a
60c0: 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 ll [prepared sta
60d0: 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64 20 tements].** and
60e0: 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c [sqlite3_blob_cl
60f0: 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c 6c ose | close] all
6100: 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d 20 [BLOB handles]
6110: 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a associated with.
6120: 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d ** the [sqlite3]
6130: 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74 6f object prior to
6140: 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 63 attempting to c
6150: 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74 2e lose the object.
6160: 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
6170: 5f 6e 65 78 74 5f 73 74 6d 74 28 29 5d 20 69 6e _next_stmt()] in
6180: 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 terface can be u
6190: 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 61 6c sed to locate al
61a0: 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 l.** [prepared s
61b0: 74 61 74 65 6d 65 6e 74 73 5d 20 61 73 73 6f 63 tatements] assoc
61c0: 69 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61 iated with a [da
61d0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
61e0: 6e 5d 20 69 66 20 64 65 73 69 72 65 64 2e 0a 2a n] if desired..*
61f0: 2a 20 54 79 70 69 63 61 6c 20 63 6f 64 65 20 6d * Typical code m
6200: 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74 ight look like t
6210: 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 his:.**.** <bloc
6220: 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 kquote><pre>.**
6230: 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
6240: 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c 65 28 20 28 tmt;.** while( (
6250: 70 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 5f pStmt = sqlite3_
6260: 6e 65 78 74 5f 73 74 6d 74 28 64 62 2c 20 30 29 next_stmt(db, 0)
6270: 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 6e 62 73 70 )!=0 ){.**  
6280: 3b 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 ; sqlite3_fina
6290: 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 2a 2a 20 lize(pStmt);.**
62a0: 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f }.** </pre></blo
62b0: 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 49 ckquote>.**.** I
62c0: 66 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 f [sqlite3_close
62d0: 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 ()] is invoked w
62e0: 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63 74 69 hile a transacti
62f0: 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a 2a 20 74 on is open,.** t
6300: 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
6310: 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 s automatically
6320: 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
6330: 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a ** INVARIANTS:.*
6340: 2a 0a 2a 2a 20 7b 48 31 32 30 31 31 7d 20 41 20 *.** {H12011} A
6350: 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 successful call
6360: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 to [sqlite3_clos
6370: 65 28 43 29 5d 20 73 68 61 6c 6c 20 64 65 73 74 e(C)] shall dest
6380: 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 roy the.**
6390: 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f [database co
63a0: 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 6a 65 63 74 nnection] object
63b0: 20 43 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30 31 C..**.** {H1201
63c0: 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 2} A successful
63d0: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
63e0: 5f 63 6c 6f 73 65 28 43 29 5d 20 73 68 61 6c 6c _close(C)] shall
63f0: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
6400: 4b 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30 31 33 K..**.** {H12013
6410: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
6420: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
6430: 63 6c 6f 73 65 28 43 29 5d 20 73 68 61 6c 6c 20 close(C)] shall
6440: 72 65 6c 65 61 73 65 20 61 6c 6c 0a 2a 2a 20 20 release all.**
6450: 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 61 memory a
6460: 6e 64 20 73 79 73 74 65 6d 20 72 65 73 6f 75 72 nd system resour
6470: 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ces associated w
6480: 69 74 68 20 5b 64 61 74 61 62 61 73 65 20 63 6f ith [database co
6490: 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 20 20 20 nnection].**
64a0: 20 20 20 20 20 20 43 2e 0a 2a 2a 0a 2a 2a 20 7b C..**.** {
64b0: 48 31 32 30 31 34 7d 20 41 20 63 61 6c 6c 20 74 H12014} A call t
64c0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 o [sqlite3_close
64d0: 28 43 29 5d 20 6f 6e 20 61 20 5b 64 61 74 61 62 (C)] on a [datab
64e0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
64f0: 43 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 C that.**
6500: 20 20 20 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f has one or mo
6510: 72 65 20 6f 70 65 6e 20 5b 70 72 65 70 61 72 65 re open [prepare
6520: 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 68 d statements] sh
6530: 61 6c 6c 20 66 61 69 6c 20 77 69 74 68 0a 2a 2a all fail with.**
6540: 20 20 20 20 20 20 20 20 20 20 61 6e 20 5b 53 51 an [SQ
6550: 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 6f 72 LITE_BUSY] error
6560: 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 code..**.** {H1
6570: 32 30 31 35 7d 20 41 20 63 61 6c 6c 20 74 6f 20 2015} A call to
6580: 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 [sqlite3_close(C
6590: 29 5d 20 77 68 65 72 65 20 43 20 69 73 20 61 20 )] where C is a
65a0: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61 NULL pointer sha
65b0: 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 ll.** b
65c0: 65 20 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d e a harmless no-
65d0: 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c op returning SQL
65e0: 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ITE_OK..**.** {H
65f0: 31 32 30 31 39 7d 20 57 68 65 6e 20 5b 73 71 6c 12019} When [sql
6600: 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 69 ite3_close(C)] i
6610: 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 5b s invoked on a [
6620: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
6630: 69 6f 6e 5d 20 43 0a 2a 2a 20 20 20 20 20 20 20 ion] C.**
6640: 20 20 20 74 68 61 74 20 68 61 73 20 61 20 70 65 that has a pe
6650: 6e 64 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f nding transactio
6660: 6e 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 n, the transacti
6670: 6f 6e 20 73 68 61 6c 6c 20 62 65 0a 2a 2a 20 20 on shall be.**
6680: 20 20 20 20 20 20 20 20 72 6f 6c 6c 65 64 20 62 rolled b
6690: 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d ack..**.** ASSUM
66a0: 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 PTIONS:.**.** {A
66b0: 31 32 30 31 36 7d 20 54 68 65 20 43 20 70 61 72 12016} The C par
66c0: 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 ameter to [sqlit
66d0: 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 6d 75 73 e3_close(C)] mus
66e0: 74 20 62 65 20 65 69 74 68 65 72 20 61 20 4e 55 t be either a NU
66f0: 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 LL.** p
6700: 6f 69 6e 74 65 72 20 6f 72 20 61 6e 20 5b 73 71 ointer or an [sq
6710: 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 20 70 6f lite3] object po
6720: 69 6e 74 65 72 20 6f 62 74 61 69 6e 65 64 0a 2a inter obtained.*
6730: 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 * from
6740: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
6750: 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 , [sqlite3_open1
6760: 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 6()], or.**
6770: 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6f 70 [sqlite3_op
6780: 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 6e 6f en_v2()], and no
6790: 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f t previously clo
67a0: 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 sed..*/.SQLITE_A
67b0: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
67c0: 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 29 3b lose(sqlite3 *);
67d0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 ../*.** The type
67e0: 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 for a callback
67f0: 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 function..** Thi
6800: 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e 64 20 s is legacy and
6810: 64 65 70 72 65 63 61 74 65 64 2e 20 20 49 74 20 deprecated. It
6820: 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f 72 20 is included for
6830: 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 63 6f historical.** co
6840: 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e 64 20 mpatibility and
6850: 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e 74 65 is not documente
6860: 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 69 6e d..*/.typedef in
6870: 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61 6c 6c t (*sqlite3_call
6880: 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c back)(void*,int,
6890: 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b char**, char**);
68a0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
68b0: 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65 72 79 : One-Step Query
68c0: 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74 65 72 Execution Inter
68d0: 66 61 63 65 20 7b 48 31 32 31 30 30 7d 20 3c 53 face {H12100} <S
68e0: 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 10000>.**.** The
68f0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
6900: 69 6e 74 65 72 66 61 63 65 20 69 73 20 61 20 63 interface is a c
6910: 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20 6f 66 onvenient way of
6920: 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f 72 20 running one or
6930: 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 73 74 61 74 more.** SQL stat
6940: 65 6d 65 6e 74 73 20 77 69 74 68 6f 75 74 20 68 ements without h
6950: 61 76 69 6e 67 20 74 6f 20 77 72 69 74 65 20 61 aving to write a
6960: 20 6c 6f 74 20 6f 66 20 43 20 63 6f 64 65 2e 20 lot of C code.
6970: 20 54 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 The UTF-8 encod
6980: 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d ed.** SQL statem
6990: 65 6e 74 73 20 61 72 65 20 70 61 73 73 65 64 20 ents are passed
69a0: 69 6e 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 in as the second
69b0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
69c0: 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a lite3_exec()..**
69d0: 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 73 20 The statements
69e0: 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e are evaluated on
69f0: 65 20 62 79 20 6f 6e 65 20 75 6e 74 69 6c 20 65 e by one until e
6a00: 69 74 68 65 72 20 61 6e 20 65 72 72 6f 72 20 6f ither an error o
6a10: 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 72 75 70 r.** an interrup
6a20: 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 t is encountered
6a30: 2c 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 79 20 , or until they
6a40: 61 72 65 20 61 6c 6c 20 64 6f 6e 65 2e 20 20 54 are all done. T
6a50: 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 he 3rd parameter
6a60: 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e .** is an option
6a70: 61 6c 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 al callback that
6a80: 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 is invoked once
6a90: 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 for each row of
6aa0: 20 61 6e 79 20 71 75 65 72 79 0a 2a 2a 20 72 65 any query.** re
6ab0: 73 75 6c 74 73 20 70 72 6f 64 75 63 65 64 20 62 sults produced b
6ac0: 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d y the SQL statem
6ad0: 65 6e 74 73 2e 20 20 54 68 65 20 35 74 68 20 70 ents. The 5th p
6ae0: 61 72 61 6d 65 74 65 72 20 74 65 6c 6c 73 20 77 arameter tells w
6af0: 68 65 72 65 0a 2a 2a 20 74 6f 20 77 72 69 74 65 here.** to write
6b00: 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 any error messa
6b10: 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 ges..**.** The e
6b20: 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 61 73 rror message pas
6b30: 73 65 64 20 62 61 63 6b 20 74 68 72 6f 75 67 68 sed back through
6b40: 20 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 the 5th paramet
6b50: 65 72 20 69 73 20 68 65 6c 64 0a 2a 2a 20 69 6e er is held.** in
6b60: 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
6b70: 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
6b80: 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 6f 20 61 76 alloc()]. To av
6b90: 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 oid a memory lea
6ba0: 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e k,.** the callin
6bb0: 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 g application sh
6bc0: 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 71 6c 69 74 ould call [sqlit
6bd0: 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20 61 6e e3_free()] on an
6be0: 79 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 y error.** messa
6bf0: 67 65 20 72 65 74 75 72 6e 65 64 20 74 68 72 6f ge returned thro
6c00: 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61 ugh the 5th para
6c10: 6d 65 74 65 72 20 77 68 65 6e 20 69 74 20 68 61 meter when it ha
6c20: 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 s finished using
6c30: 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 6d 65 .** the error me
6c40: 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ssage..**.** If
6c50: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
6c60: 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70 61 72 t in the 2nd par
6c70: 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 6f ameter is NULL o
6c80: 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e r an empty strin
6c90: 67 0a 2a 2a 20 6f 72 20 61 20 73 74 72 69 6e 67 g.** or a string
6ca0: 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e 6c 79 containing only
6cb0: 20 77 68 69 74 65 73 70 61 63 65 20 61 6e 64 20 whitespace and
6cc0: 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65 6e 20 6e comments, then n
6cd0: 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 o SQL.** stateme
6ce0: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 nts are evaluate
6cf0: 64 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 d and the databa
6d00: 73 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 se is not change
6d10: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c d..**.** The sql
6d20: 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e 74 65 ite3_exec() inte
6d30: 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 rface is impleme
6d40: 6e 74 65 64 20 69 6e 20 74 65 72 6d 73 20 6f 66 nted in terms of
6d50: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
6d60: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c pare_v2()], [sql
6d70: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e ite3_step()], an
6d80: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c d [sqlite3_final
6d90: 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 ize()]..** The s
6da0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72 6f qlite3_exec() ro
6db0: 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 68 69 utine does nothi
6dc0: 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
6dd0: 73 65 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 se that cannot b
6de0: 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b 73 71 e done.** by [sq
6df0: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
6e00: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 ()], [sqlite3_st
6e10: 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 ep()], and [sqli
6e20: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e te3_finalize()].
6e30: 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 .**.** INVARIANT
6e40: 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 30 31 S:.**.** {H12101
6e50: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 69 } A successful i
6e60: 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 nvocation of [sq
6e70: 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 lite3_exec(D,S,C
6e80: 2c 41 2c 45 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ,A,E)].**
6e90: 20 20 20 73 68 61 6c 6c 20 73 65 71 75 65 6e 74 shall sequent
6ea0: 69 61 6c 6c 79 20 65 76 61 6c 75 61 74 65 20 61 ially evaluate a
6eb0: 6c 6c 20 6f 66 20 74 68 65 20 55 54 46 2d 38 20 ll of the UTF-8
6ec0: 65 6e 63 6f 64 65 64 2c 0a 2a 2a 20 20 20 20 20 encoded,.**
6ed0: 20 20 20 20 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 semicolon-s
6ee0: 65 70 61 72 61 74 65 64 20 53 51 4c 20 73 74 61 eparated SQL sta
6ef0: 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 7a tements in the z
6f00: 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a ero-terminated.*
6f10: 2a 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e * strin
6f20: 67 20 53 20 77 69 74 68 69 6e 20 74 68 65 20 63 g S within the c
6f30: 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 5b 64 ontext of the [d
6f40: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
6f50: 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 on] D..**.** {H1
6f60: 32 31 30 32 7d 20 49 66 20 74 68 65 20 53 20 70 2102} If the S p
6f70: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
6f80: 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c ite3_exec(D,S,C,
6f90: 41 2c 45 29 5d 20 69 73 20 4e 55 4c 4c 20 74 68 A,E)] is NULL th
6fa0: 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 en.** t
6fb0: 68 65 20 61 63 74 69 6f 6e 73 20 6f 66 20 74 68 he actions of th
6fc0: 65 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c e interface shal
6fd0: 6c 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 l be the same as
6fe0: 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 if the.**
6ff0: 20 20 20 20 53 20 70 61 72 61 6d 65 74 65 72 20 S parameter
7000: 77 65 72 65 20 61 6e 20 65 6d 70 74 79 20 73 74 were an empty st
7010: 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 ring..**.** {H12
7020: 31 30 34 7d 20 54 68 65 20 72 65 74 75 72 6e 20 104} The return
7030: 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74 65 value of [sqlite
7040: 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c 20 3_exec()] shall
7050: 62 65 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 be [SQLITE_OK] i
7060: 66 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 f all.**
7070: 20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 SQL statements
7080: 20 72 75 6e 20 73 75 63 63 65 73 73 66 75 6c 6c run successfull
7090: 79 20 61 6e 64 20 74 6f 20 63 6f 6d 70 6c 65 74 y and to complet
70a0: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 ion..**.** {H121
70b0: 30 35 7d 20 54 68 65 20 72 65 74 75 72 6e 20 76 05} The return v
70c0: 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74 65 33 alue of [sqlite3
70d0: 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c 20 62 _exec()] shall b
70e0: 65 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 e an appropriate
70f0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 6f 6e .** non
7100: 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 -zero [error cod
7110: 65 5d 20 69 66 20 61 6e 79 20 53 51 4c 20 73 74 e] if any SQL st
7120: 61 74 65 6d 65 6e 74 20 66 61 69 6c 73 2e 0a 2a atement fails..*
7130: 2a 0a 2a 2a 20 7b 48 31 32 31 30 37 7d 20 49 66 *.** {H12107} If
7140: 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 one or more of
7150: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
7160: 74 73 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 ts handed to [sq
7170: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a lite3_exec()].**
7180: 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
7190: 20 72 65 73 75 6c 74 73 20 61 6e 64 20 74 68 65 results and the
71a0: 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 3rd parameter i
71b0: 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
71c0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
71d0: 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
71e0: 6f 6e 20 73 70 65 63 69 66 69 65 64 20 62 79 20 on specified by
71f0: 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 the 3rd paramete
7200: 72 20 73 68 61 6c 6c 20 62 65 0a 2a 2a 20 20 20 r shall be.**
7210: 20 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 6f invoked o
7220: 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 nce for each row
7230: 20 6f 66 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a of result..**.*
7240: 2a 20 7b 48 31 32 31 31 30 7d 20 49 66 20 74 68 * {H12110} If th
7250: 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 e callback retur
7260: 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 ns a non-zero va
7270: 6c 75 65 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 lue then [sqlite
7280: 33 5f 65 78 65 63 28 29 5d 0a 2a 2a 20 20 20 20 3_exec()].**
7290: 20 20 20 20 20 20 73 68 61 6c 6c 20 61 62 6f 72 shall abor
72a0: 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d t the SQL statem
72b0: 65 6e 74 20 69 74 20 69 73 20 63 75 72 72 65 6e ent it is curren
72c0: 74 6c 79 20 65 76 61 6c 75 61 74 69 6e 67 2c 0a tly evaluating,.
72d0: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 6b 69 70 ** skip
72e0: 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 all subsequent
72f0: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2c 20 SQL statements,
7300: 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 and return [SQLI
7310: 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a TE_ABORT]..**.**
7320: 20 7b 48 31 32 31 31 33 7d 20 54 68 65 20 5b 73 {H12113} The [s
7330: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 72 qlite3_exec()] r
7340: 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 70 61 73 outine shall pas
7350: 73 20 69 74 73 20 34 74 68 20 70 61 72 61 6d 65 s its 4th parame
7360: 74 65 72 20 74 68 72 6f 75 67 68 0a 2a 2a 20 20 ter through.**
7370: 20 20 20 20 20 20 20 20 61 73 20 74 68 65 20 31 as the 1
7380: 73 74 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 st parameter of
7390: 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a the callback..**
73a0: 0a 2a 2a 20 7b 48 31 32 31 31 36 7d 20 54 68 65 .** {H12116} The
73b0: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 [sqlite3_exec()
73c0: 5d 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 ] routine shall
73d0: 73 65 74 20 74 68 65 20 32 6e 64 20 70 61 72 61 set the 2nd para
73e0: 6d 65 74 65 72 20 6f 66 20 69 74 73 0a 2a 2a 20 meter of its.**
73f0: 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 61 63 callbac
7400: 6b 20 74 6f 20 62 65 20 74 68 65 20 6e 75 6d 62 k to be the numb
7410: 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
7420: 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
7430: 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of.**
7440: 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 result..**.** {H
7450: 31 32 31 31 39 7d 20 54 68 65 20 5b 73 71 6c 69 12119} The [sqli
7460: 74 65 33 5f 65 78 65 63 28 29 5d 20 72 6f 75 74 te3_exec()] rout
7470: 69 6e 65 20 73 68 61 6c 6c 20 73 65 74 20 74 68 ine shall set th
7480: 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 e 3rd parameter
7490: 6f 66 20 69 74 73 0a 2a 2a 20 20 20 20 20 20 20 of its.**
74a0: 20 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 callback to b
74b0: 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f e an array of po
74c0: 69 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 inters to string
74d0: 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a s holding the.**
74e0: 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 73 values
74f0: 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e for each column
7500: 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 in the current
7510: 72 65 73 75 6c 74 20 73 65 74 20 72 6f 77 20 61 result set row a
7520: 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 s.** ob
7530: 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c tained from [sql
7540: 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
7550: 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 ()]..**.** {H121
7560: 32 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 22} The [sqlite3
7570: 5f 65 78 65 63 28 29 5d 20 72 6f 75 74 69 6e 65 _exec()] routine
7580: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 34 shall set the 4
7590: 74 68 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 th parameter of
75a0: 69 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 its.**
75b0: 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 61 callback to be a
75c0: 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 n array of point
75d0: 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 68 ers to strings h
75e0: 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20 olding the.**
75f0: 20 20 20 20 20 20 20 6e 61 6d 65 73 20 6f 66 20 names of
7600: 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 61 result columns a
7610: 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
7620: 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
7630: 6e 61 6d 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b name()]..**.** {
7640: 48 31 32 31 32 35 7d 20 49 66 20 74 68 65 20 33 H12125} If the 3
7650: 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 rd parameter to
7660: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d [sqlite3_exec()]
7670: 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a is NULL then.**
7680: 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
7690: 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c e3_exec()] shall
76a0: 20 73 69 6c 65 6e 74 6c 79 20 64 69 73 63 61 72 silently discar
76b0: 64 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 2e d query results.
76c0: 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 33 31 7d 20 .**.** {H12131}
76d0: 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
76e0: 72 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 rs while parsing
76f0: 20 6f 72 20 65 76 61 6c 75 61 74 69 6e 67 20 61 or evaluating a
7700: 6e 79 20 6f 66 20 74 68 65 20 53 51 4c 0a 2a 2a ny of the SQL.**
7710: 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d statem
7720: 65 6e 74 73 20 69 6e 20 74 68 65 20 53 20 70 61 ents in the S pa
7730: 72 61 6d 65 74 65 72 20 6f 66 20 5b 73 71 6c 69 rameter of [sqli
7740: 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c 41 te3_exec(D,S,C,A
7750: 2c 45 29 5d 20 61 6e 64 20 69 66 0a 2a 2a 20 20 ,E)] and if.**
7760: 20 20 20 20 20 20 20 20 74 68 65 20 45 20 70 61 the E pa
7770: 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 4e rameter is not N
7780: 55 4c 4c 2c 20 74 68 65 6e 20 5b 73 71 6c 69 74 ULL, then [sqlit
7790: 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c e3_exec()] shall
77a0: 20 73 74 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 store.**
77b0: 20 20 20 69 6e 20 2a 45 20 61 6e 20 61 70 70 72 in *E an appr
77c0: 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 opriate error me
77d0: 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e ssage written in
77e0: 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
77f0: 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 ed.** f
7800: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c rom [sqlite3_mal
7810: 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 loc()]..**.** {H
7820: 31 32 31 33 34 7d 20 54 68 65 20 5b 73 71 6c 69 12134} The [sqli
7830: 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c 41 te3_exec(D,S,C,A
7840: 2c 45 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61 ,E)] routine sha
7850: 6c 6c 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 ll set the value
7860: 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of.**
7870: 2a 45 20 74 6f 20 4e 55 4c 4c 20 69 66 20 45 20 *E to NULL if E
7880: 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 is not NULL and
7890: 74 68 65 72 65 20 61 72 65 20 6e 6f 20 65 72 72 there are no err
78a0: 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 ors..**.** {H121
78b0: 33 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 37} The [sqlite3
78c0: 5f 65 78 65 63 28 44 2c 53 2c 43 2c 41 2c 45 29 _exec(D,S,C,A,E)
78d0: 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c ] function shall
78e0: 20 73 65 74 20 74 68 65 20 5b 65 72 72 6f 72 20 set the [error
78f0: 63 6f 64 65 5d 0a 2a 2a 20 20 20 20 20 20 20 20 code].**
7900: 20 20 61 6e 64 20 6d 65 73 73 61 67 65 20 61 63 and message ac
7910: 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b 73 71 cessible via [sq
7920: 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d lite3_errcode()]
7930: 2c 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b , .** [
7940: 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
7950: 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 _errcode()],.**
7960: 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
7970: 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 61 6e 64 3_errmsg()], and
7980: 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 [sqlite3_errmsg
7990: 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 16()]..**.** {H1
79a0: 32 31 33 38 7d 20 49 66 20 74 68 65 20 53 20 70 2138} If the S p
79b0: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
79c0: 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c ite3_exec(D,S,C,
79d0: 41 2c 45 29 5d 20 69 73 20 4e 55 4c 4c 20 6f 72 A,E)] is NULL or
79e0: 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 an.**
79f0: 65 6d 70 74 79 20 73 74 72 69 6e 67 20 6f 72 20 empty string or
7a00: 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 68 69 6e 67 contains nothing
7a10: 20 6f 74 68 65 72 20 74 68 61 6e 20 77 68 69 74 other than whit
7a20: 65 73 70 61 63 65 2c 20 63 6f 6d 6d 65 6e 74 73 espace, comments
7a30: 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e ,.** an
7a40: 64 2f 6f 72 20 73 65 6d 69 63 6f 6c 6f 6e 73 2c d/or semicolons,
7a50: 20 74 68 65 6e 20 72 65 73 75 6c 74 73 20 6f 66 then results of
7a60: 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 [sqlite3_errcod
7a70: 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 e()],.**
7a80: 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e [sqlite3_exten
7a90: 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a ded_errcode()],.
7aa0: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
7ab0: 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 ite3_errmsg()],
7ac0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 and [sqlite3_err
7ad0: 6d 73 67 31 36 28 29 5d 0a 2a 2a 20 20 20 20 20 msg16()].**
7ae0: 20 20 20 20 20 73 68 61 6c 6c 20 72 65 73 65 74 shall reset
7af0: 20 74 6f 20 69 6e 64 69 63 61 74 65 20 6e 6f 20 to indicate no
7b00: 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 53 errors..**.** AS
7b10: 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a SUMPTIONS:.**.**
7b20: 20 7b 41 31 32 31 34 31 7d 20 54 68 65 20 66 69 {A12141} The fi
7b30: 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f rst parameter to
7b40: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 [sqlite3_exec()
7b50: 5d 20 6d 75 73 74 20 62 65 20 61 6e 20 76 61 6c ] must be an val
7b60: 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a 20 20 id and open.**
7b70: 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 [databas
7b80: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a e connection]..*
7b90: 2a 0a 2a 2a 20 7b 41 31 32 31 34 32 7d 20 54 68 *.** {A12142} Th
7ba0: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
7bb0: 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 62 ction must not b
7bc0: 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 0a 2a e closed while.*
7bd0: 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
7be0: 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72 te3_exec()] is r
7bf0: 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 41 unning..**.** {A
7c00: 31 32 31 34 33 7d 20 54 68 65 20 63 61 6c 6c 69 12143} The calli
7c10: 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 ng function shou
7c20: 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f ld use [sqlite3_
7c30: 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a free()] to free.
7c40: 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
7c50: 6d 65 6d 6f 72 79 20 74 68 61 74 20 2a 65 72 72 memory that *err
7c60: 6d 73 67 20 69 73 20 6c 65 66 74 20 70 6f 69 6e msg is left poin
7c70: 74 69 6e 67 20 61 74 20 6f 6e 63 65 20 74 68 65 ting at once the
7c80: 20 65 72 72 6f 72 0a 2a 2a 20 20 20 20 20 20 20 error.**
7c90: 20 20 20 6d 65 73 73 61 67 65 20 69 73 20 6e 6f message is no
7ca0: 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a longer needed..
7cb0: 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34 35 7d 20 54 **.** {A12145} T
7cc0: 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
7cd0: 20 74 65 78 74 20 69 6e 20 74 68 65 20 32 6e 64 text in the 2nd
7ce0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
7cf0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a qlite3_exec()].*
7d00: 2a 20 20 20 20 20 20 20 20 20 20 6d 75 73 74 20 * must
7d10: 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 remain unchanged
7d20: 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f while [sqlite3_
7d30: 65 78 65 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 exec()] is runni
7d40: 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ng..*/.SQLITE_AP
7d50: 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 I int sqlite3_ex
7d60: 65 63 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 ec(. sqlite3*,
7d70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d90: 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 /* An open data
7da0: 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 base */. const
7db0: 63 68 61 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 char *sql,
7dc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7dd0: 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 /* SQL to b
7de0: 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 e evaluated */.
7df0: 20 69 6e 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29 int (*callback)
7e00: 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a (void*,int,char*
7e10: 2a 2c 63 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 *,char**), /* C
7e20: 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
7e30: 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 */. void *,
7e40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7e50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7e60: 20 2f 2a 20 31 73 74 20 61 72 67 75 6d 65 6e 74 /* 1st argument
7e70: 20 74 6f 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a to callback */.
7e80: 20 20 63 68 61 72 20 2a 2a 65 72 72 6d 73 67 20 char **errmsg
7e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7ea0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
7eb0: 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74 74 65 Error msg writte
7ec0: 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a n here */.);../*
7ed0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
7ee0: 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 30 32 sult Codes {H102
7ef0: 31 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 10} <S10700>.**
7f00: 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 KEYWORDS: SQLITE
7f10: 5f 4f 4b 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d _OK {error code}
7f20: 20 7b 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a {error codes}.*
7f30: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 72 65 73 * KEYWORDS: {res
7f40: 75 6c 74 20 63 6f 64 65 7d 20 7b 72 65 73 75 6c ult code} {resul
7f50: 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d t codes}.**.** M
7f60: 61 6e 79 20 53 51 4c 69 74 65 20 66 75 6e 63 74 any SQLite funct
7f70: 69 6f 6e 73 20 72 65 74 75 72 6e 20 61 6e 20 69 ions return an i
7f80: 6e 74 65 67 65 72 20 72 65 73 75 6c 74 20 63 6f nteger result co
7f90: 64 65 20 66 72 6f 6d 20 74 68 65 20 73 65 74 20 de from the set
7fa0: 73 68 6f 77 6e 0a 2a 2a 20 68 65 72 65 20 69 6e shown.** here in
7fb0: 20 6f 72 64 65 72 20 74 6f 20 69 6e 64 69 63 61 order to indica
7fc0: 74 65 73 20 73 75 63 63 65 73 73 20 6f 72 20 66 tes success or f
7fd0: 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 ailure..**.** Ne
7fe0: 77 20 65 72 72 6f 72 20 63 6f 64 65 73 20 6d 61 w error codes ma
7ff0: 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 y be added in fu
8000: 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 ture versions of
8010: 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 SQLite..**.** S
8020: 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 ee also: [SQLITE
8030: 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c 20 65 78 _IOERR_READ | ex
8040: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
8050: 64 65 73 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 des].*/.#define
8060: 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20 20 20 SQLITE_OK
8070: 20 20 20 20 30 20 20 20 2f 2a 20 53 75 63 63 65 0 /* Succe
8080: 73 73 66 75 6c 20 72 65 73 75 6c 74 20 2a 2f 0a ssful result */.
8090: 2f 2a 20 62 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d /* beginning-of-
80a0: 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 error-codes */.#
80b0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 52 define SQLITE_ER
80c0: 52 4f 52 20 20 20 20 20 20 20 20 31 20 20 20 2f ROR 1 /
80d0: 2a 20 53 51 4c 20 65 72 72 6f 72 20 6f 72 20 6d * SQL error or m
80e0: 69 73 73 69 6e 67 20 64 61 74 61 62 61 73 65 20 issing database
80f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8100: 45 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20 20 32 E_INTERNAL 2
8110: 20 20 20 2f 2a 20 49 6e 74 65 72 6e 61 6c 20 6c /* Internal l
8120: 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 53 51 ogic error in SQ
8130: 4c 69 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 Lite */.#define
8140: 53 51 4c 49 54 45 5f 50 45 52 4d 20 20 20 20 20 SQLITE_PERM
8150: 20 20 20 20 33 20 20 20 2f 2a 20 41 63 63 65 73 3 /* Acces
8160: 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e s permission den
8170: 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ied */.#define S
8180: 51 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 20 20 QLITE_ABORT
8190: 20 20 20 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61 4 /* Callba
81a0: 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 71 75 65 ck routine reque
81b0: 73 74 65 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f sted an abort */
81c0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
81d0: 42 55 53 59 20 20 20 20 20 20 20 20 20 35 20 20 BUSY 5
81e0: 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
81f0: 20 66 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 file is locked
8200: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8210: 45 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 36 E_LOCKED 6
8220: 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e /* A table in
8230: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
8240: 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 locked */.#defi
8250: 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 ne SQLITE_NOMEM
8260: 20 20 20 20 20 20 20 37 20 20 20 2f 2a 20 41 20 7 /* A
8270: 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 malloc() failed
8280: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8290: 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 E_READONLY 8
82a0: 20 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f /* Attempt to
82b0: 20 77 72 69 74 65 20 61 20 72 65 61 64 6f 6e 6c write a readonl
82c0: 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 y database */.#d
82d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 efine SQLITE_INT
82e0: 45 52 52 55 50 54 20 20 20 20 39 20 20 20 2f 2a ERRUPT 9 /*
82f0: 20 4f 70 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 Operation termi
8300: 6e 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 nated by sqlite3
8310: 5f 69 6e 74 65 72 72 75 70 74 28 29 2a 2f 0a 23 _interrupt()*/.#
8320: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f define SQLITE_IO
8330: 45 52 52 20 20 20 20 20 20 20 31 30 20 20 20 2f ERR 10 /
8340: 2a 20 53 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 * Some kind of d
8350: 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 isk I/O error oc
8360: 63 75 72 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e curred */.#defin
8370: 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 e SQLITE_CORRUPT
8380: 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 68 65 11 /* The
8390: 20 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69 database disk i
83a0: 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 mage is malforme
83b0: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
83c0: 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 ITE_NOTFOUND
83d0: 31 32 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 12 /* NOT USED
83e0: 2e 20 54 61 62 6c 65 20 6f 72 20 72 65 63 6f 72 . Table or recor
83f0: 64 20 6e 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 d not found */.#
8400: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
8410: 4c 4c 20 20 20 20 20 20 20 20 31 33 20 20 20 2f LL 13 /
8420: 2a 20 49 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c * Insertion fail
8430: 65 64 20 62 65 63 61 75 73 65 20 64 61 74 61 62 ed because datab
8440: 61 73 65 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 ase is full */.#
8450: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 define SQLITE_CA
8460: 4e 54 4f 50 45 4e 20 20 20 20 31 34 20 20 20 2f NTOPEN 14 /
8470: 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e * Unable to open
8480: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
8490: 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 le */.#define SQ
84a0: 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 LITE_PROTOCOL
84b0: 20 31 35 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 15 /* NOT USE
84c0: 44 2e 20 44 61 74 61 62 61 73 65 20 6c 6f 63 6b D. Database lock
84d0: 20 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 protocol error
84e0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
84f0: 45 5f 45 4d 50 54 59 20 20 20 20 20 20 20 31 36 E_EMPTY 16
8500: 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 /* Database i
8510: 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 s empty */.#defi
8520: 6e 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 ne SQLITE_SCHEMA
8530: 20 20 20 20 20 20 31 37 20 20 20 2f 2a 20 54 68 17 /* Th
8540: 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
8550: 61 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 a changed */.#de
8560: 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42 fine SQLITE_TOOB
8570: 49 47 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20 IG 18 /*
8580: 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 String or BLOB e
8590: 78 63 65 65 64 73 20 73 69 7a 65 20 6c 69 6d 69 xceeds size limi
85a0: 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c t */.#define SQL
85b0: 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 ITE_CONSTRAINT
85c0: 31 39 20 20 20 2f 2a 20 41 62 6f 72 74 20 64 75 19 /* Abort du
85d0: 65 20 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 e to constraint
85e0: 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 violation */.#de
85f0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d fine SQLITE_MISM
8600: 41 54 43 48 20 20 20 20 32 30 20 20 20 2f 2a 20 ATCH 20 /*
8610: 44 61 74 61 20 74 79 70 65 20 6d 69 73 6d 61 74 Data type mismat
8620: 63 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ch */.#define SQ
8630: 4c 49 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20 LITE_MISUSE
8640: 20 32 31 20 20 20 2f 2a 20 4c 69 62 72 61 72 79 21 /* Library
8650: 20 75 73 65 64 20 69 6e 63 6f 72 72 65 63 74 6c used incorrectl
8660: 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c y */.#define SQL
8670: 49 54 45 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 ITE_NOLFS
8680: 32 32 20 20 20 2f 2a 20 55 73 65 73 20 4f 53 20 22 /* Uses OS
8690: 66 65 61 74 75 72 65 73 20 6e 6f 74 20 73 75 70 features not sup
86a0: 70 6f 72 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a ported on host *
86b0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
86c0: 5f 41 55 54 48 20 20 20 20 20 20 20 20 32 33 20 _AUTH 23
86d0: 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 /* Authorizati
86e0: 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 on denied */.#de
86f0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d fine SQLITE_FORM
8700: 41 54 20 20 20 20 20 20 32 34 20 20 20 2f 2a 20 AT 24 /*
8710: 41 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 Auxiliary databa
8720: 73 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 20 se format error
8730: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8740: 45 5f 52 41 4e 47 45 20 20 20 20 20 20 20 32 35 E_RANGE 25
8750: 20 20 20 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65 /* 2nd parame
8760: 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 ter to sqlite3_b
8770: 69 6e 64 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 ind out of range
8780: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8790: 54 45 5f 4e 4f 54 41 44 42 20 20 20 20 20 20 32 TE_NOTADB 2
87a0: 36 20 20 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 6 /* File open
87b0: 65 64 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 ed that is not a
87c0: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
87d0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
87e0: 5f 52 4f 57 20 20 20 20 20 20 20 20 20 31 30 30 _ROW 100
87f0: 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 /* sqlite3_ste
8800: 70 28 29 20 68 61 73 20 61 6e 6f 74 68 65 72 20 p() has another
8810: 72 6f 77 20 72 65 61 64 79 20 2a 2f 0a 23 64 65 row ready */.#de
8820: 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 fine SQLITE_DONE
8830: 20 20 20 20 20 20 20 20 31 30 31 20 20 2f 2a 20 101 /*
8840: 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68 sqlite3_step() h
8850: 61 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 63 as finished exec
8860: 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d uting */./* end-
8870: 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a of-error-codes *
8880: 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 /../*.** CAPI3RE
8890: 46 3a 20 45 78 74 65 6e 64 65 64 20 52 65 73 75 F: Extended Resu
88a0: 6c 74 20 43 6f 64 65 73 20 7b 48 31 30 32 32 30 lt Codes {H10220
88b0: 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 } <S10700>.** KE
88c0: 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 YWORDS: {extende
88d0: 64 20 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 d error code} {e
88e0: 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f xtended error co
88f0: 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 des}.** KEYWORDS
8900: 3a 20 7b 65 78 74 65 6e 64 65 64 20 72 65 73 75 : {extended resu
8910: 6c 74 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 lt code} {extend
8920: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 7d ed result codes}
8930: 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74 73 20 64 65 .**.** In its de
8940: 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 fault configurat
8950: 69 6f 6e 2c 20 53 51 4c 69 74 65 20 41 50 49 20 ion, SQLite API
8960: 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
8970: 6f 6e 65 20 6f 66 20 32 36 20 69 6e 74 65 67 65 one of 26 intege
8980: 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 r.** [SQLITE_OK
8990: 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e | result codes].
89a0: 20 20 48 6f 77 65 76 65 72 2c 20 65 78 70 65 72 However, exper
89b0: 69 65 6e 63 65 20 68 61 73 20 73 68 6f 77 6e 20 ience has shown
89c0: 74 68 61 74 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 that many of.**
89d0: 74 68 65 73 65 20 72 65 73 75 6c 74 20 63 6f 64 these result cod
89e0: 65 73 20 61 72 65 20 74 6f 6f 20 63 6f 61 72 73 es are too coars
89f0: 65 2d 67 72 61 69 6e 65 64 2e 20 20 54 68 65 79 e-grained. They
8a00: 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 do not provide
8a10: 61 73 0a 2a 2a 20 6d 75 63 68 20 69 6e 66 6f 72 as.** much infor
8a20: 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 70 72 6f mation about pro
8a30: 62 6c 65 6d 73 20 61 73 20 70 72 6f 67 72 61 6d blems as program
8a40: 6d 65 72 73 20 6d 69 67 68 74 20 6c 69 6b 65 2e mers might like.
8a50: 20 20 49 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 In an effort t
8a60: 6f 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 68 69 o.** address thi
8a70: 73 2c 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e s, newer version
8a80: 73 20 6f 66 20 53 51 4c 69 74 65 20 28 76 65 72 s of SQLite (ver
8a90: 73 69 6f 6e 20 33 2e 33 2e 38 20 61 6e 64 20 6c sion 3.3.8 and l
8aa0: 61 74 65 72 29 20 69 6e 63 6c 75 64 65 0a 2a 2a ater) include.**
8ab0: 20 73 75 70 70 6f 72 74 20 66 6f 72 20 61 64 64 support for add
8ac0: 69 74 69 6f 6e 61 6c 20 72 65 73 75 6c 74 20 63 itional result c
8ad0: 6f 64 65 73 20 74 68 61 74 20 70 72 6f 76 69 64 odes that provid
8ae0: 65 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 e more detailed
8af0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 information.** a
8b00: 62 6f 75 74 20 65 72 72 6f 72 73 2e 20 54 68 65 bout errors. The
8b10: 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 extended result
8b20: 20 63 6f 64 65 73 20 61 72 65 20 65 6e 61 62 6c codes are enabl
8b30: 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a ed or disabled.*
8b40: 2a 20 6f 6e 20 61 20 70 65 72 20 64 61 74 61 62 * on a per datab
8b50: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 ase connection b
8b60: 61 73 69 73 20 75 73 69 6e 67 20 74 68 65 0a 2a asis using the.*
8b70: 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e * [sqlite3_exten
8b80: 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 ded_result_codes
8b90: 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 ()] API..**.** S
8ba0: 6f 6d 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c ome of the avail
8bb0: 61 62 6c 65 20 65 78 74 65 6e 64 65 64 20 72 65 able extended re
8bc0: 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 6c sult codes are l
8bd0: 69 73 74 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f isted here..** O
8be0: 6e 65 20 6d 61 79 20 65 78 70 65 63 74 20 74 68 ne may expect th
8bf0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 78 74 65 e number of exte
8c00: 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 nded result code
8c10: 73 20 77 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 s will be expand
8c20: 0a 2a 2a 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 .** over time.
8c30: 53 6f 66 74 77 61 72 65 20 74 68 61 74 20 75 73 Software that us
8c40: 65 73 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 es extended resu
8c50: 6c 74 20 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 lt codes should
8c60: 65 78 70 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 expect.** to see
8c70: 20 6e 65 77 20 72 65 73 75 6c 74 20 63 6f 64 65 new result code
8c80: 73 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 s in future rele
8c90: 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
8ca0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
8cb0: 5f 4f 4b 20 72 65 73 75 6c 74 20 63 6f 64 65 20 _OK result code
8cc0: 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 65 78 will never be ex
8cd0: 74 65 6e 64 65 64 2e 20 20 49 74 20 77 69 6c 6c tended. It will
8ce0: 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 always.** be ex
8cf0: 61 63 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a actly zero..**.*
8d00: 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
8d10: 0a 2a 2a 20 7b 48 31 30 32 32 33 7d 20 54 68 65 .** {H10223} The
8d20: 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 symbolic name f
8d30: 6f 72 20 61 6e 20 65 78 74 65 6e 64 65 64 20 72 or an extended r
8d40: 65 73 75 6c 74 20 63 6f 64 65 20 73 68 61 6c 6c esult code shall
8d50: 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20 20 20 contains.**
8d60: 20 20 20 20 20 20 61 20 72 65 6c 61 74 65 64 20 a related
8d70: 70 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20 63 primary result c
8d80: 6f 64 65 20 61 73 20 61 20 70 72 65 66 69 78 2e ode as a prefix.
8d90: 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 34 7d 20 .**.** {H10224}
8da0: 50 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20 63 Primary result c
8db0: 6f 64 65 20 6e 61 6d 65 73 20 73 68 61 6c 6c 20 ode names shall
8dc0: 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65 contain a single
8dd0: 20 22 5f 22 20 63 68 61 72 61 63 74 65 72 2e 0a "_" character..
8de0: 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 35 7d 20 45 **.** {H10225} E
8df0: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 xtended result c
8e00: 6f 64 65 20 6e 61 6d 65 73 20 73 68 61 6c 6c 20 ode names shall
8e10: 63 6f 6e 74 61 69 6e 20 74 77 6f 20 6f 72 20 6d contain two or m
8e20: 6f 72 65 20 22 5f 22 20 63 68 61 72 61 63 74 65 ore "_" characte
8e30: 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 rs..**.** {H1022
8e40: 36 7d 20 54 68 65 20 6e 75 6d 65 72 69 63 20 76 6} The numeric v
8e50: 61 6c 75 65 20 6f 66 20 61 6e 20 65 78 74 65 6e alue of an exten
8e60: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 20 ded result code
8e70: 73 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 shall contain th
8e80: 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 e.** nu
8e90: 6d 65 72 69 63 20 76 61 6c 75 65 20 6f 66 20 69 meric value of i
8ea0: 74 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ts corresponding
8eb0: 20 70 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20 primary result
8ec0: 63 6f 64 65 20 69 6e 0a 2a 2a 20 20 20 20 20 20 code in.**
8ed0: 20 20 20 20 69 74 73 20 6c 65 61 73 74 20 73 69 its least si
8ee0: 67 6e 69 66 69 63 61 6e 74 20 38 20 62 69 74 73 gnificant 8 bits
8ef0: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
8f00: 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 20 ITE_IOERR_READ
8f10: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
8f20: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 3c 3c ITE_IOERR | (1<<
8f30: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
8f40: 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 TE_IOERR_SHORT_R
8f50: 45 41 44 20 20 20 20 20 20 20 20 28 53 51 4c 49 EAD (SQLI
8f60: 54 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c 3c 38 TE_IOERR | (2<<8
8f70: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8f80: 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 20 20 E_IOERR_WRITE
8f90: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8fa0: 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c 38 29 E_IOERR | (3<<8)
8fb0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
8fc0: 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20 20 20 _IOERR_FSYNC
8fd0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
8fe0: 5f 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38 29 29 _IOERR | (4<<8))
8ff0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
9000: 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 20 IOERR_DIR_FSYNC
9010: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
9020: 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38 29 29 0a IOERR | (5<<8)).
9030: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
9040: 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 20 20 OERR_TRUNCATE
9050: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
9060: 4f 45 52 52 20 7c 20 28 36 3c 3c 38 29 29 0a 23 OERR | (6<<8)).#
9070: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f define SQLITE_IO
9080: 45 52 52 5f 46 53 54 41 54 20 20 20 20 20 20 20 ERR_FSTAT
9090: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f (SQLITE_IO
90a0: 45 52 52 20 7c 20 28 37 3c 3c 38 29 29 0a 23 64 ERR | (7<<8)).#d
90b0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
90c0: 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20 RR_UNLOCK
90d0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
90e0: 52 52 20 7c 20 28 38 3c 3c 38 29 29 0a 23 64 65 RR | (8<<8)).#de
90f0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 fine SQLITE_IOER
9100: 52 5f 52 44 4c 4f 43 4b 20 20 20 20 20 20 20 20 R_RDLOCK
9110: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 (SQLITE_IOER
9120: 52 20 7c 20 28 39 3c 3c 38 29 29 0a 23 64 65 66 R | (9<<8)).#def
9130: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
9140: 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 _DELETE
9150: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
9160: 20 7c 20 28 31 30 3c 3c 38 29 29 0a 23 64 65 66 | (10<<8)).#def
9170: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
9180: 5f 42 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 20 _BLOCKED
9190: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
91a0: 20 7c 20 28 31 31 3c 3c 38 29 29 0a 23 64 65 66 | (11<<8)).#def
91b0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
91c0: 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 20 20 _NOMEM
91d0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
91e0: 20 7c 20 28 31 32 3c 3c 38 29 29 0a 23 64 65 66 | (12<<8)).#def
91f0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
9200: 5f 41 43 43 45 53 53 20 20 20 20 20 20 20 20 20 _ACCESS
9210: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
9220: 20 7c 20 28 31 33 3c 3c 38 29 29 0a 23 64 65 66 | (13<<8)).#def
9230: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
9240: 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f _CHECKRESERVEDLO
9250: 43 4b 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 CK (SQLITE_IOERR
9260: 20 7c 20 28 31 34 3c 3c 38 29 29 0a 23 64 65 66 | (14<<8)).#def
9270: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
9280: 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 _LOCK
9290: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
92a0: 20 7c 20 28 31 35 3c 3c 38 29 29 0a 0a 2f 2a 0a | (15<<8))../*.
92b0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c 61 ** CAPI3REF: Fla
92c0: 67 73 20 46 6f 72 20 46 69 6c 65 20 4f 70 65 6e gs For File Open
92d0: 20 4f 70 65 72 61 74 69 6f 6e 73 20 7b 48 31 30 Operations {H10
92e0: 32 33 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 230} <H11120> <H
92f0: 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 12700>.**.** The
9300: 73 65 20 62 69 74 20 76 61 6c 75 65 73 20 61 72 se bit values ar
9310: 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 e intended for u
9320: 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20 33 72 64 se in the.** 3rd
9330: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
9340: 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f e [sqlite3_open_
9350: 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 v2()] interface
9360: 61 6e 64 0a 2a 2a 20 69 6e 20 74 68 65 20 34 74 and.** in the 4t
9370: 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
9380: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 he xOpen method
9390: 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 of the.** [sqlit
93a0: 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a e3_vfs] object..
93b0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
93c0: 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 E_OPEN_READONLY
93d0: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 0x000000
93e0: 30 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 01.#define SQLIT
93f0: 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
9400: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 0x000000
9410: 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 02.#define SQLIT
9420: 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 20 20 E_OPEN_CREATE
9430: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 0x000000
9440: 30 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 04.#define SQLIT
9450: 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
9460: 4c 4f 53 45 20 20 20 20 30 78 30 30 30 30 30 30 LOSE 0x000000
9470: 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 08.#define SQLIT
9480: 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
9490: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 0x000000
94a0: 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 10.#define SQLIT
94b0: 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 E_OPEN_MAIN_DB
94c0: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 31 0x000001
94d0: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 00.#define SQLIT
94e0: 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 20 E_OPEN_TEMP_DB
94f0: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 32 0x000002
9500: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 00.#define SQLIT
9510: 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 E_OPEN_TRANSIENT
9520: 5f 44 42 20 20 20 20 20 30 78 30 30 30 30 30 34 _DB 0x000004
9530: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 00.#define SQLIT
9540: 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 E_OPEN_MAIN_JOUR
9550: 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 30 38 NAL 0x000008
9560: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 00.#define SQLIT
9570: 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 E_OPEN_TEMP_JOUR
9580: 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 31 30 NAL 0x000010
9590: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 00.#define SQLIT
95a0: 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 E_OPEN_SUBJOURNA
95b0: 4c 20 20 20 20 20 20 20 30 78 30 30 30 30 32 30 L 0x000020
95c0: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 00.#define SQLIT
95d0: 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
95e0: 55 52 4e 41 4c 20 20 20 30 78 30 30 30 30 34 30 URNAL 0x000040
95f0: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 00.#define SQLIT
9600: 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 20 E_OPEN_NOMUTEX
9610: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 38 30 0x000080
9620: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 00.#define SQLIT
9630: 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 E_OPEN_FULLMUTEX
9640: 20 20 20 20 20 20 20 20 30 78 30 30 30 31 30 30 0x000100
9650: 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 00../*.** CAPI3R
9660: 45 46 3a 20 44 65 76 69 63 65 20 43 68 61 72 61 EF: Device Chara
9670: 63 74 65 72 69 73 74 69 63 73 20 7b 48 31 30 32 cteristics {H102
9680: 34 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 2a 0a 40} <H11120>.**.
9690: 2a 2a 20 54 68 65 20 78 44 65 76 69 63 65 43 61 ** The xDeviceCa
96a0: 70 61 62 69 6c 69 74 69 65 73 20 6d 65 74 68 6f pabilities metho
96b0: 64 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 d of the [sqlite
96c0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 3_io_methods].**
96d0: 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 73 20 object returns
96e0: 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 an integer which
96f0: 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 is a vector of
9700: 74 68 65 20 74 68 65 73 65 0a 2a 2a 20 62 69 74 the these.** bit
9710: 20 76 61 6c 75 65 73 20 65 78 70 72 65 73 73 69 values expressi
9720: 6e 67 20 49 2f 4f 20 63 68 61 72 61 63 74 65 72 ng I/O character
9730: 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 6d 61 istics of the ma
9740: 73 73 20 73 74 6f 72 61 67 65 0a 2a 2a 20 64 65 ss storage.** de
9750: 76 69 63 65 20 74 68 61 74 20 68 6f 6c 64 73 20 vice that holds
9760: 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 74 68 the file that th
9770: 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 e [sqlite3_io_me
9780: 74 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 65 72 73 thods].** refers
9790: 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 to..**.** The S
97a0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
97b0: 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e IC property mean
97c0: 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74 65 s that all write
97d0: 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 s of.** any size
97e0: 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 are atomic. Th
97f0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 e SQLITE_IOCAP_A
9800: 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a TOMICnnn values.
9810: 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72 69 ** mean that wri
9820: 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 tes of blocks th
9830: 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 at are nnn bytes
9840: 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 in size and.**
9850: 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 are aligned to a
9860: 6e 20 61 64 64 72 65 73 73 20 77 68 69 63 68 20 n address which
9870: 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 is an integer mu
9880: 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e ltiple of.** nnn
9890: 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 are atomic. Th
98a0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 e SQLITE_IOCAP_S
98b0: 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 AFE_APPEND value
98c0: 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 means.** that w
98d0: 68 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 65 hen data is appe
98e0: 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 nded to a file,
98f0: 74 68 65 20 64 61 74 61 20 69 73 20 61 70 70 65 the data is appe
9900: 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 nded.** first th
9910: 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 en the size of t
9920: 68 65 20 66 69 6c 65 20 69 73 20 65 78 74 65 6e he file is exten
9930: 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20 6f ded, never the o
9940: 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 ther.** way arou
9950: 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f nd. The SQLITE_
9960: 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
9970: 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 property means
9980: 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 that.** informat
9990: 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20 74 ion is written t
99a0: 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73 61 o disk in the sa
99b0: 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c 6c me order as call
99c0: 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28 29 s.** to xWrite()
99d0: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
99e0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
99f0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 0x0000
9a00: 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 0001.#define SQL
9a10: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
9a20: 35 31 32 20 20 20 20 20 20 20 30 78 30 30 30 30 512 0x0000
9a30: 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 0002.#define SQL
9a40: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
9a50: 31 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 1K 0x0000
9a60: 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 0004.#define SQL
9a70: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
9a80: 32 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 2K 0x0000
9a90: 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 0008.#define SQL
9aa0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
9ab0: 34 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 4K 0x0000
9ac0: 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0010.#define SQL
9ad0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
9ae0: 38 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 8K 0x0000
9af0: 30 30 32 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0020.#define SQL
9b00: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
9b10: 31 36 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 16K 0x0000
9b20: 30 30 34 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0040.#define SQL
9b30: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
9b40: 33 32 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 32K 0x0000
9b50: 30 30 38 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0080.#define SQL
9b60: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
9b70: 36 34 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 64K 0x0000
9b80: 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0100.#define SQL
9b90: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
9ba0: 50 50 45 4e 44 20 20 20 20 20 30 78 30 30 30 30 PPEND 0x0000
9bb0: 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0200.#define SQL
9bc0: 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e ITE_IOCAP_SEQUEN
9bd0: 54 49 41 4c 20 20 20 20 20 20 30 78 30 30 30 30 TIAL 0x0000
9be0: 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 0400../*.** CAPI
9bf0: 33 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63 6b 69 3REF: File Locki
9c00: 6e 67 20 4c 65 76 65 6c 73 20 7b 48 31 30 32 35 ng Levels {H1025
9c10: 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 31 31 0} <H11120> <H11
9c20: 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 310>.**.** SQLit
9c30: 65 20 75 73 65 73 20 6f 6e 65 20 6f 66 20 74 68 e uses one of th
9c40: 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 ese integer valu
9c50: 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 es as the second
9c60: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 .** argument to
9c70: 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65 73 20 74 calls it makes t
9c80: 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29 20 61 6e o the xLock() an
9c90: 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d 65 74 68 d xUnlock() meth
9ca0: 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 5b 73 71 ods.** of an [sq
9cb0: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
9cc0: 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 ] object..*/.#de
9cd0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b fine SQLITE_LOCK
9ce0: 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20 20 20 30 _NONE 0
9cf0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
9d00: 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20 20 20 20 LOCK_SHARED
9d10: 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 1.#define SQL
9d20: 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 ITE_LOCK_RESERVE
9d30: 44 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 D 2.#define
9d40: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e SQLITE_LOCK_PEN
9d50: 44 49 4e 47 20 20 20 20 20 20 20 33 0a 23 64 65 DING 3.#de
9d60: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b fine SQLITE_LOCK
9d70: 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 34 _EXCLUSIVE 4
9d80: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
9d90: 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f : Synchronizatio
9da0: 6e 20 54 79 70 65 20 46 6c 61 67 73 20 7b 48 31 n Type Flags {H1
9db0: 30 32 36 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 0260} <H11120>.*
9dc0: 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 *.** When SQLite
9dd0: 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 53 79 invokes the xSy
9de0: 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 61 nc() method of a
9df0: 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f n.** [sqlite3_io
9e00: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 _methods] object
9e10: 20 69 74 20 75 73 65 73 20 61 20 63 6f 6d 62 69 it uses a combi
9e20: 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 nation of.** the
9e30: 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 se integer value
9e40: 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 s as the second
9e50: 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
9e60: 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f When the SQLITE_
9e70: 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 66 6c SYNC_DATAONLY fl
9e80: 61 67 20 69 73 20 75 73 65 64 2c 20 69 74 20 6d ag is used, it m
9e90: 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a eans that the.**
9ea0: 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e 20 sync operation
9eb0: 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 66 6c only needs to fl
9ec0: 75 73 68 20 64 61 74 61 20 74 6f 20 6d 61 73 73 ush data to mass
9ed0: 20 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f 64 65 storage. Inode
9ee0: 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
9ef0: 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 6c 75 73 need not be flus
9f00: 68 65 64 2e 20 54 68 65 20 53 51 4c 49 54 45 5f hed. The SQLITE_
9f10: 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 66 6c 61 67 SYNC_NORMAL flag
9f20: 20 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 means.** to use
9f30: 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 normal fsync()
9f40: 73 65 6d 61 6e 74 69 63 73 2e 20 54 68 65 20 53 semantics. The S
9f50: 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 QLITE_SYNC_FULL
9f60: 66 6c 61 67 20 6d 65 61 6e 73 0a 2a 2a 20 74 6f flag means.** to
9f70: 20 75 73 65 20 4d 61 63 20 4f 53 20 58 20 73 74 use Mac OS X st
9f80: 79 6c 65 20 66 75 6c 6c 73 79 6e 63 20 69 6e 73 yle fullsync ins
9f90: 74 65 61 64 20 6f 66 20 66 73 79 6e 63 28 29 2e tead of fsync().
9fa0: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
9fb0: 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 20 TE_SYNC_NORMAL
9fc0: 20 20 20 20 20 20 30 78 30 30 30 30 32 0a 23 64 0x00002.#d
9fd0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e efine SQLITE_SYN
9fe0: 43 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 20 20 C_FULL
9ff0: 30 78 30 30 30 30 33 0a 23 64 65 66 69 6e 65 20 0x00003.#define
a000: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 SQLITE_SYNC_DATA
a010: 4f 4e 4c 59 20 20 20 20 20 20 30 78 30 30 30 31 ONLY 0x0001
a020: 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 0../*.** CAPI3RE
a030: 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 F: OS Interface
a040: 4f 70 65 6e 20 46 69 6c 65 20 48 61 6e 64 6c 65 Open File Handle
a050: 20 7b 48 31 31 31 31 30 7d 20 3c 53 32 30 31 31 {H11110} <S2011
a060: 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b 73 71 6c 0>.**.** An [sql
a070: 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 ite3_file] objec
a080: 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 t represents an
a090: 6f 70 65 6e 20 66 69 6c 65 20 69 6e 20 74 68 65 open file in the
a0a0: 20 4f 53 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 OS.** interface
a0b0: 20 6c 61 79 65 72 2e 20 20 49 6e 64 69 76 69 64 layer. Individ
a0c0: 75 61 6c 20 4f 53 20 69 6e 74 65 72 66 61 63 65 ual OS interface
a0d0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
a0e0: 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f will.** want to
a0f0: 20 73 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f subclass this o
a100: 62 6a 65 63 74 20 62 79 20 61 70 70 65 6e 64 69 bject by appendi
a110: 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 ng additional fi
a120: 65 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 elds.** for thei
a130: 72 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20 r own use. The
a140: 70 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69 pMethods entry i
a150: 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
a160: 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f n.** [sqlite3_io
a170: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 _methods] object
a180: 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65 that defines me
a190: 74 68 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72 thods for perfor
a1a0: 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 ming.** I/O oper
a1b0: 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 ations on the op
a1c0: 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 en file..*/.type
a1d0: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
a1e0: 65 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f e3_file sqlite3_
a1f0: 66 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c file;.struct sql
a200: 69 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f ite3_file {. co
a210: 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 nst struct sqlit
a220: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 e3_io_methods *p
a230: 4d 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 Methods; /* Met
a240: 68 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e hods for an open
a250: 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a file */.};../*.
a260: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 ** CAPI3REF: OS
a270: 49 6e 74 65 72 66 61 63 65 20 46 69 6c 65 20 56 Interface File V
a280: 69 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f irtual Methods O
a290: 62 6a 65 63 74 20 7b 48 31 31 31 32 30 7d 20 3c bject {H11120} <
a2a0: 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 S20110>.**.** Ev
a2b0: 65 72 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 ery file opened
a2c0: 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f by the [sqlite3_
a2d0: 76 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f vfs] xOpen metho
a2e0: 64 20 70 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a d populates an.*
a2f0: 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d * [sqlite3_file]
a300: 20 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 object (or, mor
a310: 65 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 e commonly, a su
a320: 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a bclass of the.**
a330: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 [sqlite3_file]
a340: 6f 62 6a 65 63 74 29 20 77 69 74 68 20 61 20 70 object) with a p
a350: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
a360: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
a370: 6a 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 ject..** This ob
a380: 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 ject defines the
a390: 20 6d 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f methods used to
a3a0: 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 perform various
a3b0: 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 operations.** a
a3c0: 67 61 69 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 gainst the open
a3d0: 66 69 6c 65 20 72 65 70 72 65 73 65 6e 74 65 64 file represented
a3e0: 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 by the [sqlite3
a3f0: 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a _file] object..*
a400: 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 *.** The flags a
a410: 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63 rgument to xSync
a420: 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b may be one of [
a430: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
a440: 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 AL] or.** [SQLIT
a450: 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 E_SYNC_FULL]. T
a460: 68 65 20 66 69 72 73 74 20 63 68 6f 69 63 65 20 he first choice
a470: 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73 is the normal fs
a480: 79 6e 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65 ync()..** The se
a490: 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73 20 61 cond choice is a
a4a0: 20 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 Mac OS X style
a4b0: 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20 5b fullsync. The [
a4c0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 SQLITE_SYNC_DATA
a4d0: 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 ONLY].** flag ma
a4e0: 79 20 62 65 20 4f 52 65 64 20 69 6e 20 74 6f 20 y be ORed in to
a4f0: 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 6f 6e indicate that on
a500: 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66 20 74 ly the data of t
a510: 68 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e he file.** and n
a520: 6f 74 20 69 74 73 20 69 6e 6f 64 65 20 6e 65 65 ot its inode nee
a530: 64 73 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e ds to be synced.
a540: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 .**.** The integ
a550: 65 72 20 76 61 6c 75 65 73 20 74 6f 20 78 4c 6f er values to xLo
a560: 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b ck() and xUnlock
a570: 28 29 20 61 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a () are one of.**
a580: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 <ul>.** <li> [S
a590: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d QLITE_LOCK_NONE]
a5a0: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ,.** <li> [SQLIT
a5b0: 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a E_LOCK_SHARED],.
a5c0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
a5d0: 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 0a LOCK_RESERVED],.
a5e0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
a5f0: 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f LOCK_PENDING], o
a600: 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 r.** <li> [SQLIT
a610: 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 E_LOCK_EXCLUSIVE
a620: 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 ]..** </ul>.** x
a630: 4c 6f 63 6b 28 29 20 69 6e 63 72 65 61 73 65 73 Lock() increases
a640: 20 74 68 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f the lock. xUnlo
a650: 63 6b 28 29 20 64 65 63 72 65 61 73 65 73 20 74 ck() decreases t
a660: 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 he lock..** The
a670: 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
a680: 63 6b 28 29 20 6d 65 74 68 6f 64 20 63 68 65 63 ck() method chec
a690: 6b 73 20 77 68 65 74 68 65 72 20 61 6e 79 20 64 ks whether any d
a6a0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
a6b0: 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e on,.** either in
a6c0: 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72 this process or
a6d0: 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 in some other p
a6e0: 72 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c 64 69 rocess, is holdi
a6f0: 6e 67 20 61 20 52 45 53 45 52 56 45 44 2c 0a 2a ng a RESERVED,.*
a700: 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58 * PENDING, or EX
a710: 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 CLUSIVE lock on
a720: 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20 72 65 the file. It re
a730: 74 75 72 6e 73 20 74 72 75 65 0a 2a 2a 20 69 66 turns true.** if
a740: 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65 78 69 such a lock exi
a750: 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20 6f 74 sts and false ot
a760: 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 herwise..**.** T
a770: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 he xFileControl(
a780: 29 20 6d 65 74 68 6f 64 20 69 73 20 61 20 67 65 ) method is a ge
a790: 6e 65 72 69 63 20 69 6e 74 65 72 66 61 63 65 20 neric interface
a7a0: 74 68 61 74 20 61 6c 6c 6f 77 73 20 63 75 73 74 that allows cust
a7b0: 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d om.** VFS implem
a7c0: 65 6e 74 61 74 69 6f 6e 73 20 74 6f 20 64 69 72 entations to dir
a7d0: 65 63 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e ectly control an
a7e0: 20 6f 70 65 6e 20 66 69 6c 65 20 75 73 69 6e 67 open file using
a7f0: 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
a800: 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d _file_control()]
a810: 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65 interface. The
a820: 20 73 65 63 6f 6e 64 20 22 6f 70 22 20 61 72 67 second "op" arg
a830: 75 6d 65 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 69 ument is an.** i
a840: 6e 74 65 67 65 72 20 6f 70 63 6f 64 65 2e 20 20 nteger opcode.
a850: 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 The third argume
a860: 6e 74 20 69 73 20 61 20 67 65 6e 65 72 69 63 20 nt is a generic
a870: 70 6f 69 6e 74 65 72 20 69 6e 74 65 6e 64 65 64 pointer intended
a880: 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 to.** point to
a890: 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 a structure that
a8a0: 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 72 67 may contain arg
a8b0: 75 6d 65 6e 74 73 20 6f 72 20 73 70 61 63 65 20 uments or space
a8c0: 69 6e 20 77 68 69 63 68 20 74 6f 0a 2a 2a 20 77 in which to.** w
a8d0: 72 69 74 65 20 72 65 74 75 72 6e 20 76 61 6c 75 rite return valu
a8e0: 65 73 2e 20 20 50 6f 74 65 6e 74 69 61 6c 20 75 es. Potential u
a8f0: 73 65 73 20 66 6f 72 20 78 46 69 6c 65 43 6f 6e ses for xFileCon
a900: 74 72 6f 6c 28 29 20 6d 69 67 68 74 20 62 65 0a trol() might be.
a910: 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 ** functions to
a920: 65 6e 61 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 enable blocking
a930: 6c 6f 63 6b 73 20 77 69 74 68 20 74 69 6d 65 6f locks with timeo
a940: 75 74 73 2c 20 74 6f 20 63 68 61 6e 67 65 20 74 uts, to change t
a950: 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 he.** locking st
a960: 72 61 74 65 67 79 20 28 66 6f 72 20 65 78 61 6d rategy (for exam
a970: 70 6c 65 20 74 6f 20 75 73 65 20 64 6f 74 2d 66 ple to use dot-f
a980: 69 6c 65 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 ile locks), to i
a990: 6e 71 75 69 72 65 0a 2a 2a 20 61 62 6f 75 74 20 nquire.** about
a9a0: 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 61 20 the status of a
a9b0: 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20 62 72 65 61 lock, or to brea
a9c0: 6b 20 73 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 k stale locks.
a9d0: 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f The SQLite.** co
a9e0: 72 65 20 72 65 73 65 72 76 65 73 20 61 6c 6c 20 re reserves all
a9f0: 6f 70 63 6f 64 65 73 20 6c 65 73 73 20 74 68 61 opcodes less tha
aa00: 6e 20 31 30 30 20 66 6f 72 20 69 74 73 20 6f 77 n 100 for its ow
aa10: 6e 20 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c n use..** A [SQL
aa20: 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 ITE_FCNTL_LOCKST
aa30: 41 54 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70 ATE | list of op
aa40: 63 6f 64 65 73 5d 20 6c 65 73 73 20 74 68 61 6e codes] less than
aa50: 20 31 30 30 20 69 73 20 61 76 61 69 6c 61 62 6c 100 is availabl
aa60: 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f e..** Applicatio
aa70: 6e 73 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 ns that define a
aa80: 20 63 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e custom xFileCon
aa90: 74 72 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75 trol method shou
aaa0: 6c 64 20 75 73 65 20 6f 70 63 6f 64 65 73 0a 2a ld use opcodes.*
aab0: 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 * greater than 1
aac0: 30 30 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 00 to avoid conf
aad0: 6c 69 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 licts..**.** The
aae0: 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d xSectorSize() m
aaf0: 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 74 68 ethod returns th
ab00: 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 e sector size of
ab10: 20 74 68 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 the.** device t
ab20: 68 61 74 20 75 6e 64 65 72 6c 69 65 73 20 74 68 hat underlies th
ab30: 65 20 66 69 6c 65 2e 20 20 54 68 65 20 73 65 63 e file. The sec
ab40: 74 6f 72 20 73 69 7a 65 20 69 73 20 74 68 65 0a tor size is the.
ab50: 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 72 69 74 65 ** minimum write
ab60: 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 65 72 that can be per
ab70: 66 6f 72 6d 65 64 20 77 69 74 68 6f 75 74 20 64 formed without d
ab80: 69 73 74 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 isturbing.** oth
ab90: 65 72 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 er bytes in the
aba0: 66 69 6c 65 2e 20 20 54 68 65 20 78 44 65 76 69 file. The xDevi
abb0: 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
abc0: 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 s().** method re
abd0: 74 75 72 6e 73 20 61 20 62 69 74 20 76 65 63 74 turns a bit vect
abe0: 6f 72 20 64 65 73 63 72 69 62 69 6e 67 20 62 65 or describing be
abf0: 68 61 76 69 6f 72 73 20 6f 66 20 74 68 65 0a 2a haviors of the.*
ac00: 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 65 76 * underlying dev
ac10: 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a ice:.**.** <ul>.
ac20: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
ac30: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a IOCAP_ATOMIC].**
ac40: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f <li> [SQLITE_IO
ac50: 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 5d 0a 2a CAP_ATOMIC512].*
ac60: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 * <li> [SQLITE_I
ac70: 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a OCAP_ATOMIC1K].*
ac80: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 * <li> [SQLITE_I
ac90: 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a OCAP_ATOMIC2K].*
aca0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 * <li> [SQLITE_I
acb0: 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a OCAP_ATOMIC4K].*
acc0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 * <li> [SQLITE_I
acd0: 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a OCAP_ATOMIC8K].*
ace0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 * <li> [SQLITE_I
acf0: 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a OCAP_ATOMIC16K].
ad00: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
ad10: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b 5d IOCAP_ATOMIC32K]
ad20: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
ad30: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b _IOCAP_ATOMIC64K
ad40: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ].** <li> [SQLIT
ad50: 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 E_IOCAP_SAFE_APP
ad60: 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 END].** <li> [SQ
ad70: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 LITE_IOCAP_SEQUE
ad80: 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a NTIAL].** </ul>.
ad90: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
ada0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 _IOCAP_ATOMIC pr
adb0: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 operty means tha
adc0: 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a t all writes of.
add0: 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72 65 20 ** any size are
ade0: 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c atomic. The SQL
adf0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
ae00: 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 nnn values.** me
ae10: 61 6e 20 74 68 61 74 20 77 72 69 74 65 73 20 6f an that writes o
ae20: 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 f blocks that ar
ae30: 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 e nnn bytes in s
ae40: 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 ize and.** are a
ae50: 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 ligned to an add
ae60: 72 65 73 73 20 77 68 69 63 68 20 69 73 20 61 6e ress which is an
ae70: 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c integer multipl
ae80: 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 e of.** nnn are
ae90: 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c atomic. The SQL
aea0: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
aeb0: 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e PPEND value mean
aec0: 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 64 s.** that when d
aed0: 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 20 ata is appended
aee0: 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65 20 64 to a file, the d
aef0: 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 0a ata is appended.
af00: 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20 74 68 ** first then th
af10: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 e size of the fi
af20: 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20 le is extended,
af30: 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65 72 0a never the other.
af40: 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 ** way around.
af50: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 The SQLITE_IOCAP
af60: 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 _SEQUENTIAL prop
af70: 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0a erty means that.
af80: 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ** information i
af90: 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 s written to dis
afa0: 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 k in the same or
afb0: 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 der as calls.**
afc0: 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a 2a 0a to xWrite()..**.
afd0: 2a 2a 20 49 66 20 78 52 65 61 64 28 29 20 72 65 ** If xRead() re
afe0: 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 4f 45 turns SQLITE_IOE
aff0: 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 74 RR_SHORT_READ it
b000: 20 6d 75 73 74 20 61 6c 73 6f 20 66 69 6c 6c 0a must also fill.
b010: 2a 2a 20 69 6e 20 74 68 65 20 75 6e 72 65 61 64 ** in the unread
b020: 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 portions of the
b030: 20 62 75 66 66 65 72 20 77 69 74 68 20 7a 65 72 buffer with zer
b040: 6f 73 2e 20 20 41 20 56 46 53 20 74 68 61 74 0a os. A VFS that.
b050: 2a 2a 20 66 61 69 6c 73 20 74 6f 20 7a 65 72 6f ** fails to zero
b060: 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 -fill short read
b070: 73 20 6d 69 67 68 74 20 73 65 65 6d 20 74 6f 20 s might seem to
b080: 77 6f 72 6b 2e 20 20 48 6f 77 65 76 65 72 2c 0a work. However,.
b090: 2a 2a 20 66 61 69 6c 75 72 65 20 74 6f 20 7a 65 ** failure to ze
b0a0: 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 ro-fill short re
b0b0: 61 64 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 ads will eventua
b0c0: 6c 6c 79 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 lly lead to.** d
b0d0: 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 atabase corrupti
b0e0: 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 on..*/.typedef s
b0f0: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f truct sqlite3_io
b100: 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 _methods sqlite3
b110: 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 _io_methods;.str
b120: 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d uct sqlite3_io_m
b130: 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 69 ethods {. int i
b140: 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 Version;. int (
b150: 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 *xClose)(sqlite3
b160: 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 _file*);. int (
b170: 2a 78 52 65 61 64 29 28 73 71 6c 69 74 65 33 5f *xRead)(sqlite3_
b180: 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e file*, void*, in
b190: 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f t iAmt, sqlite3_
b1a0: 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 int64 iOfst);.
b1b0: 69 6e 74 20 28 2a 78 57 72 69 74 65 29 28 73 71 int (*xWrite)(sq
b1c0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e lite3_file*, con
b1d0: 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 st void*, int iA
b1e0: 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 mt, sqlite3_int6
b1f0: 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 4 iOfst);. int
b200: 28 2a 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c (*xTruncate)(sql
b210: 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 ite3_file*, sqli
b220: 74 65 33 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b te3_int64 size);
b230: 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 . int (*xSync)(
b240: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
b250: 6e 74 20 66 6c 61 67 73 29 3b 0a 20 20 69 6e 74 nt flags);. int
b260: 20 28 2a 78 46 69 6c 65 53 69 7a 65 29 28 73 71 (*xFileSize)(sq
b270: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c lite3_file*, sql
b280: 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a ite3_int64 *pSiz
b290: 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4c 6f 63 e);. int (*xLoc
b2a0: 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k)(sqlite3_file*
b2b0: 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a , int);. int (*
b2c0: 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 65 33 xUnlock)(sqlite3
b2d0: 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 _file*, int);.
b2e0: 69 6e 74 20 28 2a 78 43 68 65 63 6b 52 65 73 65 int (*xCheckRese
b2f0: 72 76 65 64 4c 6f 63 6b 29 28 73 71 6c 69 74 65 rvedLock)(sqlite
b300: 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 3_file*, int *pR
b310: 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a esOut);. int (*
b320: 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71 xFileControl)(sq
b330: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
b340: 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 op, void *pArg)
b350: 3b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f ;. int (*xSecto
b360: 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 rSize)(sqlite3_f
b370: 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 ile*);. int (*x
b380: 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
b390: 73 74 69 63 73 29 28 73 71 6c 69 74 65 33 5f 66 stics)(sqlite3_f
b3a0: 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69 ile*);. /* Addi
b3b0: 74 69 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d tional methods m
b3c0: 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 ay be added in f
b3d0: 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 2a uture releases *
b3e0: 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.};../*.** CAPI
b3f0: 33 52 45 46 3a 20 53 74 61 6e 64 61 72 64 20 46 3REF: Standard F
b400: 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f ile Control Opco
b410: 64 65 73 20 7b 48 31 31 33 31 30 7d 20 3c 53 33 des {H11310} <S3
b420: 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 0800>.**.** Thes
b430: 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 e integer consta
b440: 6e 74 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 nts are opcodes
b450: 66 6f 72 20 74 68 65 20 78 46 69 6c 65 43 6f 6e for the xFileCon
b460: 74 72 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f trol method.** o
b470: 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 f the [sqlite3_i
b480: 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 o_methods] objec
b490: 74 20 61 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 t and for the [s
b4a0: 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 qlite3_file_cont
b4b0: 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 rol()].** interf
b4c0: 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b ace..**.** The [
b4d0: 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 SQLITE_FCNTL_LOC
b4e0: 4b 53 54 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 KSTATE] opcode i
b4f0: 73 20 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 s used for debug
b500: 67 69 6e 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f ging. This.** o
b510: 70 63 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 pcode causes the
b520: 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 xFileControl me
b530: 74 68 6f 64 20 74 6f 20 77 72 69 74 65 20 74 68 thod to write th
b540: 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 e current state
b550: 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 of.** the lock (
b560: 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c one of [SQLITE_L
b570: 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 OCK_NONE], [SQLI
b580: 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c TE_LOCK_SHARED],
b590: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b .** [SQLITE_LOCK
b5a0: 5f 52 45 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c _RESERVED], [SQL
b5b0: 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 ITE_LOCK_PENDING
b5c0: 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f ], or [SQLITE_LO
b5d0: 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a CK_EXCLUSIVE]).*
b5e0: 2a 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 * into an intege
b5f0: 72 20 74 68 61 74 20 74 68 65 20 70 41 72 67 20 r that the pArg
b600: 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 argument points
b610: 74 6f 2e 20 54 68 69 73 20 63 61 70 61 62 69 6c to. This capabil
b620: 69 74 79 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 ity.** is used d
b630: 75 72 69 6e 67 20 74 65 73 74 69 6e 67 20 61 6e uring testing an
b640: 64 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 d only needs to
b650: 62 65 20 73 75 70 70 6f 72 74 65 64 20 77 68 65 be supported whe
b660: 6e 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a n SQLITE_TEST.**
b670: 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a is defined..*/.
b680: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
b690: 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 20 CNTL_LOCKSTATE
b6a0: 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 43 1../*.** C
b6b0: 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 48 API3REF: Mutex H
b6c0: 61 6e 64 6c 65 20 7b 48 31 37 31 31 30 7d 20 3c andle {H17110} <
b6d0: 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20130>.**.** Th
b6e0: 65 20 6d 75 74 65 78 20 6d 6f 64 75 6c 65 20 77 e mutex module w
b6f0: 69 74 68 69 6e 20 53 51 4c 69 74 65 20 64 65 66 ithin SQLite def
b700: 69 6e 65 73 20 5b 73 71 6c 69 74 65 33 5f 6d 75 ines [sqlite3_mu
b710: 74 65 78 5d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a tex] to be an.**
b720: 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66 abstract type f
b730: 6f 72 20 61 20 6d 75 74 65 78 20 6f 62 6a 65 63 or a mutex objec
b740: 74 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 t. The SQLite c
b750: 6f 72 65 20 6e 65 76 65 72 20 6c 6f 6f 6b 73 0a ore never looks.
b760: 2a 2a 20 61 74 20 74 68 65 20 69 6e 74 65 72 6e ** at the intern
b770: 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f al representatio
b780: 6e 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 n of an [sqlite3
b790: 5f 6d 75 74 65 78 5d 2e 20 20 49 74 20 6f 6e 6c _mutex]. It onl
b7a0: 79 0a 2a 2a 20 64 65 61 6c 73 20 77 69 74 68 20 y.** deals with
b7b0: 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 pointers to the
b7c0: 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 [sqlite3_mutex]
b7d0: 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 4d 75 object..**.** Mu
b7e0: 74 65 78 65 73 20 61 72 65 20 63 72 65 61 74 65 texes are create
b7f0: 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 d using [sqlite3
b800: 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 2e _mutex_alloc()].
b810: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
b820: 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ct sqlite3_mutex
b830: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 3b 0a sqlite3_mutex;.
b840: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
b850: 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 4f 62 OS Interface Ob
b860: 6a 65 63 74 20 7b 48 31 31 31 34 30 7d 20 3c 53 ject {H11140} <S
b870: 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 20100>.**.** An
b880: 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
b890: 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 sqlite3_vfs obje
b8a0: 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 ct defines the i
b8b0: 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e nterface between
b8c0: 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 63 .** the SQLite c
b8d0: 6f 72 65 20 61 6e 64 20 74 68 65 20 75 6e 64 65 ore and the unde
b8e0: 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 rlying operating
b8f0: 20 73 79 73 74 65 6d 2e 20 20 54 68 65 20 22 76 system. The "v
b900: 66 73 22 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 fs".** in the na
b910: 6d 65 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 me of the object
b920: 20 73 74 61 6e 64 73 20 66 6f 72 20 22 76 69 72 stands for "vir
b930: 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d tual file system
b940: 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c "..**.** The val
b950: 75 65 20 6f 66 20 74 68 65 20 69 56 65 72 73 69 ue of the iVersi
b960: 6f 6e 20 66 69 65 6c 64 20 69 73 20 69 6e 69 74 on field is init
b970: 69 61 6c 6c 79 20 31 20 62 75 74 20 6d 61 79 20 ially 1 but may
b980: 62 65 20 6c 61 72 67 65 72 20 69 6e 0a 2a 2a 20 be larger in.**
b990: 66 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 future versions
b9a0: 6f 66 20 53 51 4c 69 74 65 2e 20 20 41 64 64 69 of SQLite. Addi
b9b0: 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 6d 61 tional fields ma
b9c0: 79 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f y be appended to
b9d0: 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 this.** object
b9e0: 77 68 65 6e 20 74 68 65 20 69 56 65 72 73 69 6f when the iVersio
b9f0: 6e 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 n value is incre
ba00: 61 73 65 64 2e 20 20 4e 6f 74 65 20 74 68 61 74 ased. Note that
ba10: 20 74 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a the structure.*
ba20: 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 * of the sqlite3
ba30: 5f 76 66 73 20 6f 62 6a 65 63 74 20 63 68 61 6e _vfs object chan
ba40: 67 65 73 20 69 6e 20 74 68 65 20 74 72 61 6e 73 ges in the trans
ba50: 61 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 0a 2a action between.*
ba60: 2a 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e * SQLite version
ba70: 20 33 2e 35 2e 39 20 61 6e 64 20 33 2e 36 2e 30 3.5.9 and 3.6.0
ba80: 20 61 6e 64 20 79 65 74 20 74 68 65 20 69 56 65 and yet the iVe
ba90: 72 73 69 6f 6e 20 66 69 65 6c 64 20 77 61 73 20 rsion field was
baa0: 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 2e not.** modified.
bab0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 4f 73 46 .**.** The szOsF
bac0: 69 6c 65 20 66 69 65 6c 64 20 69 73 20 74 68 65 ile field is the
bad0: 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 75 62 size of the sub
bae0: 63 6c 61 73 73 65 64 20 5b 73 71 6c 69 74 65 33 classed [sqlite3
baf0: 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74 72 75 63 74 _file].** struct
bb00: 75 72 65 20 75 73 65 64 20 62 79 20 74 68 69 73 ure used by this
bb10: 20 56 46 53 2e 20 20 6d 78 50 61 74 68 6e 61 6d VFS. mxPathnam
bb20: 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d e is the maximum
bb30: 20 6c 65 6e 67 74 68 20 6f 66 0a 2a 2a 20 61 20 length of.** a
bb40: 70 61 74 68 6e 61 6d 65 20 69 6e 20 74 68 69 73 pathname in this
bb50: 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 VFS..**.** Regi
bb60: 73 74 65 72 65 64 20 73 71 6c 69 74 65 33 5f 76 stered sqlite3_v
bb70: 66 73 20 6f 62 6a 65 63 74 73 20 61 72 65 20 6b fs objects are k
bb80: 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 ept on a linked
bb90: 6c 69 73 74 20 66 6f 72 6d 65 64 20 62 79 0a 2a list formed by.*
bba0: 2a 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e * the pNext poin
bbb0: 74 65 72 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 ter. The [sqlit
bbc0: 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 e3_vfs_register(
bbd0: 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 )].** and [sqlit
bbe0: 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 e3_vfs_unregiste
bbf0: 72 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 r()] interfaces
bc00: 6d 61 6e 61 67 65 20 74 68 69 73 20 6c 69 73 74 manage this list
bc10: 0a 2a 2a 20 69 6e 20 61 20 74 68 72 65 61 64 2d .** in a thread-
bc20: 73 61 66 65 20 77 61 79 2e 20 20 54 68 65 20 5b safe way. The [
bc30: 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 sqlite3_vfs_find
bc40: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a ()] interface.**
bc50: 20 73 65 61 72 63 68 65 73 20 74 68 65 20 6c 69 searches the li
bc60: 73 74 2e 20 20 4e 65 69 74 68 65 72 20 74 68 65 st. Neither the
bc70: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 application cod
bc80: 65 20 6e 6f 72 20 74 68 65 20 56 46 53 0a 2a 2a e nor the VFS.**
bc90: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
bca0: 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 70 should use the p
bcb0: 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a Next pointer..**
bcc0: 0a 2a 2a 20 54 68 65 20 70 4e 65 78 74 20 66 69 .** The pNext fi
bcd0: 65 6c 64 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 eld is the only
bce0: 66 69 65 6c 64 20 69 6e 20 74 68 65 20 73 71 6c field in the sql
bcf0: 69 74 65 33 5f 76 66 73 0a 2a 2a 20 73 74 72 75 ite3_vfs.** stru
bd00: 63 74 75 72 65 20 74 68 61 74 20 53 51 4c 69 74 cture that SQLit
bd10: 65 20 77 69 6c 6c 20 65 76 65 72 20 6d 6f 64 69 e will ever modi
bd20: 66 79 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c fy. SQLite will
bd30: 20 6f 6e 6c 79 20 61 63 63 65 73 73 0a 2a 2a 20 only access.**
bd40: 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 66 or modify this f
bd50: 69 65 6c 64 20 77 68 69 6c 65 20 68 6f 6c 64 69 ield while holdi
bd60: 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 ng a particular
bd70: 73 74 61 74 69 63 20 6d 75 74 65 78 2e 0a 2a 2a static mutex..**
bd80: 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e The application
bd90: 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 6d 6f should never mo
bda0: 64 69 66 79 20 61 6e 79 74 68 69 6e 67 20 77 69 dify anything wi
bdb0: 74 68 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 thin the sqlite3
bdc0: 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f _vfs.** object o
bdd0: 6e 63 65 20 74 68 65 20 6f 62 6a 65 63 74 20 68 nce the object h
bde0: 61 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 as been register
bdf0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 4e ed..**.** The zN
be00: 61 6d 65 20 66 69 65 6c 64 20 68 6f 6c 64 73 20 ame field holds
be10: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
be20: 56 46 53 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 VFS module. The
be30: 20 6e 61 6d 65 20 6d 75 73 74 0a 2a 2a 20 62 65 name must.** be
be40: 20 75 6e 69 71 75 65 20 61 63 72 6f 73 73 20 61 unique across a
be50: 6c 6c 20 56 46 53 20 6d 6f 64 75 6c 65 73 2e 0a ll VFS modules..
be60: 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 31 7d 20 53 **.** {H11141} S
be70: 51 4c 69 74 65 20 77 69 6c 6c 20 67 75 61 72 61 QLite will guara
be80: 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 7a 46 ntee that the zF
be90: 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 ilename paramete
bea0: 72 20 74 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 r to xOpen.** is
beb0: 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 20 70 either a NULL p
bec0: 6f 69 6e 74 65 72 20 6f 72 20 73 74 72 69 6e 67 ointer or string
bed0: 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f obtained.** fro
bee0: 6d 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 m xFullPathname(
bef0: 29 2e 20 20 53 51 4c 69 74 65 20 66 75 72 74 68 ). SQLite furth
bf00: 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 er guarantees th
bf10: 61 74 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 at.** the string
bf20: 20 77 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 will be valid a
bf30: 6e 64 20 75 6e 63 68 61 6e 67 65 64 20 75 6e 74 nd unchanged unt
bf40: 69 6c 20 78 43 6c 6f 73 65 28 29 20 69 73 0a 2a il xClose() is.*
bf50: 2a 20 63 61 6c 6c 65 64 2e 20 7b 45 4e 44 7d 20 * called. {END}
bf60: 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 Because of the
bf70: 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 73 previous sentens
bf80: 65 2c 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 e,.** the [sqlit
bf90: 65 33 5f 66 69 6c 65 5d 20 63 61 6e 20 73 61 66 e3_file] can saf
bfa0: 65 6c 79 20 73 74 6f 72 65 20 61 20 70 6f 69 6e ely store a poin
bfb0: 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69 ter to the.** fi
bfc0: 6c 65 6e 61 6d 65 20 69 66 20 69 74 20 6e 65 65 lename if it nee
bfd0: 64 73 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 ds to remember t
bfe0: 68 65 20 66 69 6c 65 6e 61 6d 65 20 66 6f 72 20 he filename for
bff0: 73 6f 6d 65 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20 some reason..**
c000: 49 66 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 If the zFilename
c010: 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 78 4f parameter is xO
c020: 70 65 6e 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f pen is a NULL po
c030: 69 6e 74 65 72 20 74 68 65 6e 20 78 4f 70 65 6e inter then xOpen
c040: 0a 2a 2a 20 6d 75 73 74 20 69 6e 76 69 74 65 20 .** must invite
c050: 69 74 73 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72 its own temporar
c060: 79 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 66 y name for the f
c070: 69 6c 65 2e 20 20 57 68 65 6e 65 76 65 72 20 74 ile. Whenever t
c080: 68 65 20 0a 2a 2a 20 78 46 69 6c 65 6e 61 6d 65 he .** xFilename
c090: 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 parameter is NU
c0a0: 4c 4c 20 69 74 20 77 69 6c 6c 20 61 6c 73 6f 20 LL it will also
c0b0: 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 be the case that
c0c0: 20 74 68 65 0a 2a 2a 20 66 6c 61 67 73 20 70 61 the.** flags pa
c0d0: 72 61 6d 65 74 65 72 20 77 69 6c 6c 20 69 6e 63 rameter will inc
c0e0: 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 lude [SQLITE_OPE
c0f0: 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d N_DELETEONCLOSE]
c100: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 32 7d ..**.** {H11142}
c110: 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d The flags argum
c120: 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28 29 20 69 ent to xOpen() i
c130: 6e 63 6c 75 64 65 73 20 61 6c 6c 20 62 69 74 73 ncludes all bits
c140: 20 73 65 74 20 69 6e 0a 2a 2a 20 74 68 65 20 66 set in.** the f
c150: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f lags argument to
c160: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
c170: 32 28 29 5d 2e 20 20 4f 72 20 69 66 20 5b 73 71 2()]. Or if [sq
c180: 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a lite3_open()].**
c190: 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 or [sqlite3_ope
c1a0: 6e 31 36 28 29 5d 20 69 73 20 75 73 65 64 2c 20 n16()] is used,
c1b0: 74 68 65 6e 20 66 6c 61 67 73 20 69 6e 63 6c 75 then flags inclu
c1c0: 64 65 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 des at least.**
c1d0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 [SQLITE_OPEN_REA
c1e0: 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 DWRITE] | [SQLIT
c1f0: 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 2e 20 E_OPEN_CREATE].
c200: 7b 45 4e 44 7d 0a 2a 2a 20 49 66 20 78 4f 70 65 {END}.** If xOpe
c210: 6e 28 29 20 6f 70 65 6e 73 20 61 20 66 69 6c 65 n() opens a file
c220: 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65 6e 20 read-only then
c230: 69 74 20 73 65 74 73 20 2a 70 4f 75 74 46 6c 61 it sets *pOutFla
c240: 67 73 20 74 6f 0a 2a 2a 20 69 6e 63 6c 75 64 65 gs to.** include
c250: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 [SQLITE_OPEN_RE
c260: 41 44 4f 4e 4c 59 5d 2e 20 20 4f 74 68 65 72 20 ADONLY]. Other
c270: 62 69 74 73 20 69 6e 20 2a 70 4f 75 74 46 6c 61 bits in *pOutFla
c280: 67 73 20 6d 61 79 20 62 65 20 73 65 74 2e 0a 2a gs may be set..*
c290: 2a 0a 2a 2a 20 7b 48 31 31 31 34 33 7d 20 53 51 *.** {H11143} SQ
c2a0: 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 Lite will also a
c2b0: 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f dd one of the fo
c2c0: 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f llowing flags to
c2d0: 20 74 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 the xOpen().**
c2e0: 63 61 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 call, depending
c2f0: 6f 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 on the object be
c300: 69 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a ing opened:.**.*
c310: 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
c320: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 [SQLITE_OPEN_MAI
c330: 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b N_DB].** <li> [
c340: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
c350: 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 _JOURNAL].** <li
c360: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f > [SQLITE_OPEN_
c370: 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e TEMP_DB].** <li>
c380: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 [SQLITE_OPEN_T
c390: 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 EMP_JOURNAL].**
c3a0: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
c3b0: 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d EN_TRANSIENT_DB]
c3c0: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 .** <li> [SQLIT
c3d0: 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 E_OPEN_SUBJOURNA
c3e0: 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c L].** <li> [SQL
c3f0: 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f ITE_OPEN_MASTER_
c400: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c JOURNAL].** </ul
c410: 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 > {END}.**.** Th
c420: 65 20 66 69 6c 65 20 49 2f 4f 20 69 6d 70 6c 65 e file I/O imple
c430: 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 75 73 mentation can us
c440: 65 20 74 68 65 20 6f 62 6a 65 63 74 20 74 79 70 e the object typ
c450: 65 20 66 6c 61 67 73 20 74 6f 0a 2a 2a 20 63 68 e flags to.** ch
c460: 61 6e 67 65 20 74 68 65 20 77 61 79 20 69 74 20 ange the way it
c470: 64 65 61 6c 73 20 77 69 74 68 20 66 69 6c 65 73 deals with files
c480: 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 . For example,
c490: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a an application.*
c4a0: 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 * that does not
c4b0: 63 61 72 65 20 61 62 6f 75 74 20 63 72 61 73 68 care about crash
c4c0: 20 72 65 63 6f 76 65 72 79 20 6f 72 20 72 6f 6c recovery or rol
c4d0: 6c 62 61 63 6b 20 6d 69 67 68 74 20 6d 61 6b 65 lback might make
c4e0: 0a 2a 2a 20 74 68 65 20 6f 70 65 6e 20 6f 66 20 .** the open of
c4f0: 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 a journal file a
c500: 20 6e 6f 2d 6f 70 2e 20 20 57 72 69 74 65 73 20 no-op. Writes
c510: 74 6f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 to this journal
c520: 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62 65 would.** also be
c530: 20 6e 6f 2d 6f 70 73 2c 20 61 6e 64 20 61 6e 79 no-ops, and any
c540: 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 attempt to read
c550: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 the journal wou
c560: 6c 64 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ld return.** SQL
c570: 49 54 45 5f 49 4f 45 52 52 2e 20 20 4f 72 20 74 ITE_IOERR. Or t
c580: 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
c590: 6e 20 6d 69 67 68 74 20 72 65 63 6f 67 6e 69 7a n might recogniz
c5a0: 65 20 74 68 61 74 20 61 20 64 61 74 61 62 61 73 e that a databas
c5b0: 65 0a 2a 2a 20 66 69 6c 65 20 77 69 6c 6c 20 62 e.** file will b
c5c0: 65 20 64 6f 69 6e 67 20 70 61 67 65 2d 61 6c 69 e doing page-ali
c5d0: 67 6e 65 64 20 73 65 63 74 6f 72 20 72 65 61 64 gned sector read
c5e0: 73 20 61 6e 64 20 77 72 69 74 65 73 20 69 6e 20 s and writes in
c5f0: 61 20 72 61 6e 64 6f 6d 0a 2a 2a 20 6f 72 64 65 a random.** orde
c600: 72 20 61 6e 64 20 73 65 74 20 75 70 20 69 74 73 r and set up its
c610: 20 49 2f 4f 20 73 75 62 73 79 73 74 65 6d 20 61 I/O subsystem a
c620: 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a ccordingly..**.*
c630: 2a 20 53 51 4c 69 74 65 20 6d 69 67 68 74 20 61 * SQLite might a
c640: 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 lso add one of t
c650: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 he following fla
c660: 67 73 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 gs to the xOpen
c670: 6d 65 74 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 method:.**.** <u
c680: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 l>.** <li> [SQLI
c690: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
c6a0: 43 4c 4f 53 45 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b CLOSE].** <li> [
c6b0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c SQLITE_OPEN_EXCL
c6c0: 55 53 49 56 45 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a USIVE].** </ul>.
c6d0: 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 35 7d 20 54 **.** {H11145} T
c6e0: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f he [SQLITE_OPEN_
c6f0: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 DELETEONCLOSE] f
c700: 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 lag means the fi
c710: 6c 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 le should be.**
c720: 64 65 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20 deleted when it
c730: 69 73 20 63 6c 6f 73 65 64 2e 20 20 7b 48 31 31 is closed. {H11
c740: 31 34 36 7d 20 54 68 65 20 5b 53 51 4c 49 54 45 146} The [SQLITE
c750: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
c760: 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 OSE].** will be
c770: 73 65 74 20 66 6f 72 20 54 45 4d 50 20 20 64 61 set for TEMP da
c780: 74 61 62 61 73 65 73 2c 20 6a 6f 75 72 6e 61 6c tabases, journal
c790: 73 20 61 6e 64 20 66 6f 72 20 73 75 62 6a 6f 75 s and for subjou
c7a0: 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 rnals..**.** {H1
c7b0: 31 31 34 37 7d 20 54 68 65 20 5b 53 51 4c 49 54 1147} The [SQLIT
c7c0: 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
c7d0: 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 ] flag means the
c7e0: 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 file should be
c7f0: 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 65 78 opened.** for ex
c800: 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 2e 20 clusive access.
c810: 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 This flag is se
c820: 74 20 66 6f 72 20 61 6c 6c 20 66 69 6c 65 73 20 t for all files
c830: 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20 74 68 except.** for th
c840: 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
c850: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 file..**.** {H11
c860: 31 34 38 7d 20 41 74 20 6c 65 61 73 74 20 73 7a 148} At least sz
c870: 4f 73 46 69 6c 65 20 62 79 74 65 73 20 6f 66 20 OsFile bytes of
c880: 6d 65 6d 6f 72 79 20 61 72 65 20 61 6c 6c 6f 63 memory are alloc
c890: 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a ated by SQLite.*
c8a0: 2a 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 20 5b * to hold the [
c8b0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 73 74 sqlite3_file] st
c8c0: 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61 ructure passed a
c8d0: 73 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 s the third.** a
c8e0: 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e rgument to xOpen
c8f0: 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 78 4f 70 . {END} The xOp
c900: 65 6e 20 6d 65 74 68 6f 64 20 64 6f 65 73 20 6e en method does n
c910: 6f 74 20 68 61 76 65 20 74 6f 0a 2a 2a 20 61 6c ot have to.** al
c920: 6c 6f 63 61 74 65 20 74 68 65 20 73 74 72 75 63 locate the struc
c930: 74 75 72 65 3b 20 69 74 20 73 68 6f 75 6c 64 20 ture; it should
c940: 6a 75 73 74 20 66 69 6c 6c 20 69 74 20 69 6e 2e just fill it in.
c950: 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 39 7d 20 .**.** {H11149}
c960: 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 The flags argume
c970: 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28 29 20 nt to xAccess()
c980: 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 may be [SQLITE_A
c990: 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a 2a CCESS_EXISTS].**
c9a0: 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74 68 65 to test for the
c9b0: 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 61 20 existence of a
c9c0: 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 file, or [SQLITE
c9d0: 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
c9e0: 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77 68 E] to.** test wh
c9f0: 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 ether a file is
ca00: 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 readable and wri
ca10: 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 table, or [SQLIT
ca20: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d 0a 2a E_ACCESS_READ].*
ca30: 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74 68 65 * to test whethe
ca40: 72 20 61 20 66 69 6c 65 20 69 73 20 61 74 20 6c r a file is at l
ca50: 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e 20 7b east readable. {
ca60: 45 4e 44 7d 20 20 54 68 65 20 66 69 6c 65 20 63 END} The file c
ca70: 61 6e 20 62 65 20 61 0a 2a 2a 20 64 69 72 65 63 an be a.** direc
ca80: 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 tory..**.** {H11
ca90: 31 35 30 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 150} SQLite will
caa0: 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 always allocate
cab0: 20 61 74 20 6c 65 61 73 74 20 6d 78 50 61 74 68 at least mxPath
cac0: 6e 61 6d 65 2b 31 20 62 79 74 65 73 20 66 6f 72 name+1 bytes for
cad0: 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 the.** output b
cae0: 75 66 66 65 72 20 78 46 75 6c 6c 50 61 74 68 6e uffer xFullPathn
caf0: 61 6d 65 2e 20 7b 48 31 31 31 35 31 7d 20 54 68 ame. {H11151} Th
cb00: 65 20 65 78 61 63 74 20 73 69 7a 65 20 6f 66 20 e exact size of
cb10: 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 the output buffe
cb20: 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 70 61 73 r.** is also pas
cb30: 73 65 64 20 61 73 20 61 20 70 61 72 61 6d 65 74 sed as a paramet
cb40: 65 72 20 74 6f 20 62 6f 74 68 20 20 6d 65 74 68 er to both meth
cb50: 6f 64 73 2e 20 7b 45 4e 44 7d 20 20 49 66 20 74 ods. {END} If t
cb60: 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 he output buffer
cb70: 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 .** is not large
cb80: 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45 enough, [SQLITE
cb90: 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c _CANTOPEN] shoul
cba0: 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 53 d be returned. S
cbb0: 69 6e 63 65 20 74 68 69 73 20 69 73 0a 2a 2a 20 ince this is.**
cbc0: 68 61 6e 64 6c 65 64 20 61 73 20 61 20 66 61 74 handled as a fat
cbd0: 61 6c 20 65 72 72 6f 72 20 62 79 20 53 51 4c 69 al error by SQLi
cbe0: 74 65 2c 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e te, vfs implemen
cbf0: 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65 tations should e
cc00: 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72 ndeavor.** to pr
cc10: 65 76 65 6e 74 20 74 68 69 73 20 62 79 20 73 65 event this by se
cc20: 74 74 69 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65 tting mxPathname
cc30: 20 74 6f 20 61 20 73 75 66 66 69 63 69 65 6e 74 to a sufficient
cc40: 6c 79 20 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a ly large value..
cc50: 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f **.** The xRando
cc60: 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 65 70 28 mness(), xSleep(
cc70: 29 2c 20 61 6e 64 20 78 43 75 72 72 65 6e 74 54 ), and xCurrentT
cc80: 69 6d 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 ime() interfaces
cc90: 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 73 74 72 69 .** are not stri
cca0: 63 74 6c 79 20 61 20 70 61 72 74 20 6f 66 20 74 ctly a part of t
ccb0: 68 65 20 66 69 6c 65 73 79 73 74 65 6d 2c 20 62 he filesystem, b
ccc0: 75 74 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 69 ut they are.** i
ccd0: 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 56 ncluded in the V
cce0: 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 FS structure for
ccf0: 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 0a 2a completeness..*
cd00: 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 * The xRandomnes
cd10: 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 s() function att
cd20: 65 6d 70 74 73 20 74 6f 20 72 65 74 75 72 6e 20 empts to return
cd30: 6e 42 79 74 65 73 20 62 79 74 65 73 0a 2a 2a 20 nBytes bytes.**
cd40: 6f 66 20 67 6f 6f 64 2d 71 75 61 6c 69 74 79 20 of good-quality
cd50: 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 randomness into
cd60: 7a 4f 75 74 2e 20 20 54 68 65 20 72 65 74 75 72 zOut. The retur
cd70: 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 n value is.** th
cd80: 65 20 61 63 74 75 61 6c 20 6e 75 6d 62 65 72 20 e actual number
cd90: 6f 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 of bytes of rand
cda0: 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 2e omness obtained.
cdb0: 0a 2a 2a 20 54 68 65 20 78 53 6c 65 65 70 28 29 .** The xSleep()
cdc0: 20 6d 65 74 68 6f 64 20 63 61 75 73 65 73 20 74 method causes t
cdd0: 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 he calling threa
cde0: 64 20 74 6f 20 73 6c 65 65 70 20 66 6f 72 20 61 d to sleep for a
cdf0: 74 0a 2a 2a 20 6c 65 61 73 74 20 74 68 65 20 6e t.** least the n
ce00: 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 umber of microse
ce10: 63 6f 6e 64 73 20 67 69 76 65 6e 2e 20 20 54 68 conds given. Th
ce20: 65 20 78 43 75 72 72 65 6e 74 54 69 6d 65 28 29 e xCurrentTime()
ce30: 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 .** method retur
ce40: 6e 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 ns a Julian Day
ce50: 4e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 63 Number for the c
ce60: 75 72 72 65 6e 74 20 64 61 74 65 20 61 6e 64 20 urrent date and
ce70: 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 time..*/.typedef
ce80: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
ce90: 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73 3b vfs sqlite3_vfs;
cea0: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
ceb0: 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 vfs {. int iVer
cec0: 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 sion;
ced0: 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76 65 /* Structure ve
cee0: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a rsion number */.
cef0: 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b 20 int szOsFile;
cf00: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
cf10: 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65 64 ze of subclassed
cf20: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2f sqlite3_file */
cf30: 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61 6d . int mxPathnam
cf40: 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d e; /* M
cf50: 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74 68 aximum file path
cf60: 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 name length */.
cf70: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e sqlite3_vfs *pN
cf80: 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 ext; /* Nex
cf90: 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53 t registered VFS
cfa0: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
cfb0: 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f *zName; /
cfc0: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 76 * Name of this v
cfd0: 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74 irtual file syst
cfe0: 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 em */. void *pA
cff0: 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 ppData;
d000: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 /* Pointer to a
d010: 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63 69 pplication-speci
d020: 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e fic data */. in
d030: 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74 t (*xOpen)(sqlit
d040: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 e3_vfs*, const c
d050: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c 69 har *zName, sqli
d060: 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20 20 te3_file*,.
d070: 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c int fl
d080: 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46 6c ags, int *pOutFl
d090: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 ags);. int (*xD
d0a0: 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f 76 elete)(sqlite3_v
d0b0: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 fs*, const char
d0c0: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e 63 *zName, int sync
d0d0: 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 41 Dir);. int (*xA
d0e0: 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f 76 ccess)(sqlite3_v
d0f0: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 fs*, const char
d100: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67 *zName, int flag
d110: 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 s, int *pResOut)
d120: 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c 6c 50 ;. int (*xFullP
d130: 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 athname)(sqlite3
d140: 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 _vfs*, const cha
d150: 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4f r *zName, int nO
d160: 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b ut, char *zOut);
d170: 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 4f 70 . void *(*xDlOp
d180: 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a en)(sqlite3_vfs*
d190: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
d1a0: 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f 69 64 ilename);. void
d1b0: 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 71 6c (*xDlError)(sql
d1c0: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e ite3_vfs*, int n
d1d0: 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72 72 Byte, char *zErr
d1e0: 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a Msg);. void *(*
d1f0: 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 65 33 5f xDlSym)(sqlite3_
d200: 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 6f 6e 73 vfs*,void*, cons
d210: 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 t char *zSymbol)
d220: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c ;. void (*xDlCl
d230: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 ose)(sqlite3_vfs
d240: 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 *, void*);. int
d250: 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 (*xRandomness)(
d260: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e sqlite3_vfs*, in
d270: 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a t nByte, char *z
d280: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 Out);. int (*xS
d290: 6c 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66 leep)(sqlite3_vf
d2a0: 73 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 s*, int microsec
d2b0: 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 onds);. int (*x
d2c0: 43 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c CurrentTime)(sql
d2d0: 69 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c ite3_vfs*, doubl
d2e0: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 e*);. int (*xGe
d2f0: 74 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69 tLastError)(sqli
d300: 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 te3_vfs*, int, c
d310: 68 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 har *);. /* New
d320: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 fields may be a
d330: 70 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72 ppended in figur
d340: 65 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65 e versions. The
d350: 20 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 iVersion. ** v
d360: 61 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d alue will increm
d370: 65 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 69 ent whenever thi
d380: 73 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b s happens. */.};
d390: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
d3a0: 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20 : Flags for the
d3b0: 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68 xAccess VFS meth
d3c0: 6f 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31 od {H11190} <H11
d3d0: 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 140>.**.** {H111
d3e0: 39 31 7d 20 54 68 65 73 65 20 69 6e 74 65 67 65 91} These intege
d3f0: 72 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 6e 20 r constants can
d400: 62 65 20 75 73 65 64 20 61 73 20 74 68 65 20 74 be used as the t
d410: 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 hird parameter t
d420: 6f 0a 2a 2a 20 74 68 65 20 78 41 63 63 65 73 73 o.** the xAccess
d430: 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b 73 method of an [s
d440: 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
d450: 63 74 2e 20 7b 45 4e 44 7d 20 20 54 68 65 79 20 ct. {END} They
d460: 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 61 determine.** wha
d470: 74 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69 73 t kind of permis
d480: 73 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65 73 sions the xAcces
d490: 73 20 6d 65 74 68 6f 64 20 69 73 20 6c 6f 6f 6b s method is look
d4a0: 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20 7b 48 31 31 ing for..** {H11
d4b0: 31 39 32 7d 20 57 69 74 68 20 53 51 4c 49 54 45 192} With SQLITE
d4c0: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 _ACCESS_EXISTS,
d4d0: 74 68 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 the xAccess meth
d4e0: 6f 64 0a 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 od.** simply che
d4f0: 63 6b 73 20 77 68 65 74 68 65 72 20 74 68 65 20 cks whether the
d500: 66 69 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 file exists..**
d510: 7b 48 31 31 31 39 33 7d 20 57 69 74 68 20 53 51 {H11193} With SQ
d520: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
d530: 57 52 49 54 45 2c 20 74 68 65 20 78 41 63 63 65 WRITE, the xAcce
d540: 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 ss method.** che
d550: 63 6b 73 20 77 68 65 74 68 65 72 20 74 68 65 20 cks whether the
d560: 66 69 6c 65 20 69 73 20 62 6f 74 68 20 72 65 61 file is both rea
d570: 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62 dable and writab
d580: 6c 65 2e 0a 2a 2a 20 7b 48 31 31 31 39 34 7d 20 le..** {H11194}
d590: 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 With SQLITE_ACCE
d5a0: 53 53 5f 52 45 41 44 2c 20 74 68 65 20 78 41 63 SS_READ, the xAc
d5b0: 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 cess method.** c
d5c0: 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 74 68 hecks whether th
d5d0: 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 e file is readab
d5e0: 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 le..*/.#define S
d5f0: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 QLITE_ACCESS_EXI
d600: 53 54 53 20 20 20 20 30 0a 23 64 65 66 69 6e 65 STS 0.#define
d610: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 SQLITE_ACCESS_R
d620: 45 41 44 57 52 49 54 45 20 31 0a 23 64 65 66 69 EADWRITE 1.#defi
d630: 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 ne SQLITE_ACCESS
d640: 5f 52 45 41 44 20 20 20 20 20 20 32 0a 0a 2f 2a _READ 2../*
d650: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e .** CAPI3REF: In
d660: 69 74 69 61 6c 69 7a 65 20 54 68 65 20 53 51 4c itialize The SQL
d670: 69 74 65 20 4c 69 62 72 61 72 79 20 7b 48 31 30 ite Library {H10
d680: 31 33 30 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33 130} <S20000><S3
d690: 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0100>.**.** The
d6a0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
d6b0: 7a 65 28 29 20 72 6f 75 74 69 6e 65 20 69 6e 69 ze() routine ini
d6c0: 74 69 61 6c 69 7a 65 73 20 74 68 65 0a 2a 2a 20 tializes the.**
d6d0: 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 SQLite library.
d6e0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 68 75 The sqlite3_shu
d6f0: 74 64 6f 77 6e 28 29 20 72 6f 75 74 69 6e 65 0a tdown() routine.
d700: 2a 2a 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 ** deallocates a
d710: 6e 79 20 72 65 73 6f 75 72 63 65 73 20 74 68 61 ny resources tha
d720: 74 20 77 65 72 65 20 61 6c 6c 6f 63 61 74 65 64 t were allocated
d730: 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 by sqlite3_init
d740: 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 ialize()..**.**
d750: 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 A call to sqlite
d760: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 3_initialize() i
d770: 73 20 61 6e 20 22 65 66 66 65 63 74 69 76 65 22 s an "effective"
d780: 20 63 61 6c 6c 20 69 66 20 69 74 20 69 73 0a 2a call if it is.*
d790: 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 * the first time
d7a0: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
d7b0: 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 ize() is invoked
d7c0: 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 during the life
d7d0: 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 70 time of.** the p
d7e0: 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20 69 74 rocess, or if it
d7f0: 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 69 is the first ti
d800: 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 me sqlite3_initi
d810: 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b alize() is invok
d820: 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 ed.** following
d830: 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
d840: 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 4f 3_shutdown(). O
d850: 6e 6c 79 20 61 6e 20 65 66 66 65 63 74 69 76 65 nly an effective
d860: 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 73 71 6c 69 call.** of sqli
d870: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
d880: 20 64 6f 65 73 20 61 6e 79 20 69 6e 69 74 69 61 does any initia
d890: 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 6c 6c 20 6f lization. All o
d8a0: 74 68 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 61 72 ther calls.** ar
d8b0: 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 e harmless no-op
d8c0: 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f s..**.** Among o
d8d0: 74 68 65 72 20 74 68 69 6e 67 73 2c 20 73 71 6c ther things, sql
d8e0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
d8f0: 29 20 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 0a 2a ) shall invoke.*
d900: 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 * sqlite3_os_ini
d910: 74 28 29 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c t(). Similarly,
d920: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
d930: 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c 20 69 6e 76 n().** shall inv
d940: 6f 6b 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 oke sqlite3_os_e
d950: 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 nd()..**.** The
d960: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
d970: 7a 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 ze() routine ret
d980: 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d urns [SQLITE_OK]
d990: 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 on success..**
d9a0: 49 66 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 If for some reas
d9b0: 6f 6e 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 on, sqlite3_init
d9c0: 69 61 6c 69 7a 65 28 29 20 69 73 20 75 6e 61 62 ialize() is unab
d9d0: 6c 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 le to initialize
d9e0: 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 .** the library
d9f0: 28 70 65 72 68 61 70 73 20 69 74 20 69 73 20 75 (perhaps it is u
da00: 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 nable to allocat
da10: 65 20 61 20 6e 65 65 64 65 64 20 72 65 73 6f 75 e a needed resou
da20: 72 63 65 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 rce such.** as a
da30: 20 6d 75 74 65 78 29 20 69 74 20 72 65 74 75 72 mutex) it retur
da40: 6e 73 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 ns an [error cod
da50: 65 5d 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 e] other than [S
da60: 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a QLITE_OK]..**.**
da70: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 The sqlite3_ini
da80: 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e tialize() routin
da90: 65 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 74 65 e is called inte
daa0: 72 6e 61 6c 6c 79 20 62 79 20 6d 61 6e 79 20 6f rnally by many o
dab0: 74 68 65 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 ther.** SQLite i
dac0: 6e 74 65 72 66 61 63 65 73 20 73 6f 20 74 68 61 nterfaces so tha
dad0: 74 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e t an application
dae0: 20 75 73 75 61 6c 6c 79 20 64 6f 65 73 20 6e 6f usually does no
daf0: 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 t need to.** inv
db00: 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 oke sqlite3_init
db10: 69 61 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c ialize() directl
db20: 79 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c y. For example,
db30: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 [sqlite3_open()
db40: 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73 71 6c 69 74 ].** calls sqlit
db50: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
db60: 73 6f 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 so the SQLite li
db70: 62 72 61 72 79 20 77 69 6c 6c 20 62 65 20 61 75 brary will be au
db80: 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 69 tomatically.** i
db90: 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 nitialized when
dba0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
dbb0: 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20 69 74 is called if it
dbc0: 20 68 61 73 20 6e 6f 74 20 62 65 20 69 6e 69 74 has not be init
dbd0: 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 6c 72 65 61 ialized.** alrea
dbe0: 64 79 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 dy. However, if
dbf0: 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
dc00: 6c 65 64 20 77 69 74 68 20 74 68 65 20 5b 53 51 led with the [SQ
dc10: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
dc20: 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 IT].** compile-t
dc30: 69 6d 65 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e ime option, then
dc40: 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 63 the automatic c
dc50: 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f alls to sqlite3_
dc60: 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 initialize().**
dc70: 61 72 65 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 are omitted and
dc80: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
dc90: 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 must call sqlite
dca0: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 3_initialize() d
dcb0: 69 72 65 63 74 6c 79 0a 2a 2a 20 70 72 69 6f 72 irectly.** prior
dcc0: 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 to using any ot
dcd0: 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 her SQLite inter
dce0: 66 61 63 65 2e 20 20 46 6f 72 20 6d 61 78 69 6d face. For maxim
dcf0: 75 6d 20 70 6f 72 74 61 62 69 6c 69 74 79 2c 0a um portability,.
dd00: 2a 2a 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 ** it is recomme
dd10: 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c 69 63 nded that applic
dd20: 61 74 69 6f 6e 73 20 61 6c 77 61 79 73 20 69 6e ations always in
dd30: 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 voke sqlite3_ini
dd40: 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 64 69 72 tialize().** dir
dd50: 65 63 74 6c 79 20 70 72 69 6f 72 20 74 6f 20 75 ectly prior to u
dd60: 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53 sing any other S
dd70: 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e QLite interface.
dd80: 20 20 46 75 74 75 72 65 20 72 65 6c 65 61 73 65 Future release
dd90: 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 6d s.** of SQLite m
dda0: 61 79 20 72 65 71 75 69 72 65 20 74 68 69 73 2e ay require this.
ddb0: 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
ddc0: 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 65 , the behavior e
ddd0: 78 68 69 62 69 74 65 64 0a 2a 2a 20 77 68 65 6e xhibited.** when
dde0: 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
ddf0: 6c 65 64 20 77 69 74 68 20 5b 53 51 4c 49 54 45 led with [SQLITE
de00: 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 20 _OMIT_AUTOINIT]
de10: 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 74 68 65 might become the
de20: 0a 2a 2a 20 64 65 66 61 75 6c 74 20 62 65 68 61 .** default beha
de30: 76 69 6f 72 20 69 6e 20 73 6f 6d 65 20 66 75 74 vior in some fut
de40: 75 72 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53 ure release of S
de50: 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 QLite..**.** The
de60: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
de70: 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 () routine does
de80: 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d operating-system
de90: 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 69 6e 69 specific.** ini
dea0: 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 tialization of t
deb0: 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
dec0: 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f y. The sqlite3_
ded0: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 72 6f 75 74 os_end().** rout
dee0: 69 6e 65 20 75 6e 64 6f 65 73 20 74 68 65 20 65 ine undoes the e
def0: 66 66 65 63 74 20 6f 66 20 73 71 6c 69 74 65 33 ffect of sqlite3
df00: 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 20 54 79 70 _os_init(). Typ
df10: 69 63 61 6c 20 74 61 73 6b 73 0a 2a 2a 20 70 65 ical tasks.** pe
df20: 72 66 6f 72 6d 65 64 20 62 79 20 74 68 65 73 65 rformed by these
df30: 20 72 6f 75 74 69 6e 65 73 20 69 6e 63 6c 75 64 routines includ
df40: 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 e allocation or
df50: 64 65 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 deallocation.**
df60: 6f 66 20 73 74 61 74 69 63 20 72 65 73 6f 75 72 of static resour
df70: 63 65 73 2c 20 69 6e 69 74 69 61 6c 69 7a 61 74 ces, initializat
df80: 69 6f 6e 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 ion of global va
df90: 72 69 61 62 6c 65 73 2c 0a 2a 2a 20 73 65 74 74 riables,.** sett
dfa0: 69 6e 67 20 75 70 20 61 20 64 65 66 61 75 6c 74 ing up a default
dfb0: 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6d [sqlite3_vfs] m
dfc0: 6f 64 75 6c 65 2c 20 6f 72 20 73 65 74 74 69 6e odule, or settin
dfd0: 67 20 75 70 0a 2a 2a 20 61 20 64 65 66 61 75 6c g up.** a defaul
dfe0: 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 t configuration
dff0: 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 using [sqlite3_c
e000: 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 onfig()]..**.**
e010: 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 The application
e020: 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 69 6e 76 should never inv
e030: 6f 6b 65 20 65 69 74 68 65 72 20 73 71 6c 69 74 oke either sqlit
e040: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 e3_os_init().**
e050: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e or sqlite3_os_en
e060: 64 28 29 20 64 69 72 65 63 74 6c 79 2e 20 20 54 d() directly. T
e070: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 he application s
e080: 68 6f 75 6c 64 20 6f 6e 6c 79 20 69 6e 76 6f 6b hould only invok
e090: 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 e.** sqlite3_ini
e0a0: 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 20 73 71 tialize() and sq
e0b0: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
e0c0: 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f . The sqlite3_o
e0d0: 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 69 6e 74 65 s_init().** inte
e0e0: 72 66 61 63 65 20 69 73 20 63 61 6c 6c 65 64 20 rface is called
e0f0: 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 automatically by
e100: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
e110: 69 7a 65 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c ize() and.** sql
e120: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 69 73 ite3_os_end() is
e130: 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 called by sqlit
e140: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 e3_shutdown().
e150: 41 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 69 Appropriate.** i
e160: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 mplementations f
e170: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e or sqlite3_os_in
e180: 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 it() and sqlite3
e190: 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 61 72 65 _os_end().** are
e1a0: 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 built into SQLi
e1b0: 74 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f te when it is co
e1c0: 6d 70 69 6c 65 64 20 66 6f 72 20 75 6e 69 78 2c mpiled for unix,
e1d0: 20 77 69 6e 64 6f 77 73 2c 20 6f 72 20 6f 73 2f windows, or os/
e1e0: 32 2e 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 74 2..** When built
e1f0: 20 66 6f 72 20 6f 74 68 65 72 20 70 6c 61 74 66 for other platf
e200: 6f 72 6d 73 20 28 75 73 69 6e 67 20 74 68 65 20 orms (using the
e210: 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 [SQLITE_OS_OTHER
e220: 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 =1] compile-time
e230: 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 .** option) the
e240: 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 application must
e250: 20 73 75 70 70 6c 79 20 61 20 73 75 69 74 61 62 supply a suitab
e260: 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le implementatio
e270: 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 n for.** sqlite3
e280: 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 _os_init() and s
e290: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e qlite3_os_end().
e2a0: 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e An application
e2b0: 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 -supplied.** imp
e2c0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 lementation of s
e2d0: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
e2e0: 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 or sqlite3_os_e
e2f0: 6e 64 28 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 nd().** must ret
e300: 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 urn [SQLITE_OK]
e310: 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 73 on success and s
e320: 6f 6d 65 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 ome other [error
e330: 20 63 6f 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 code] upon.** f
e340: 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 ailure..*/.SQLIT
e350: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
e360: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 3_initialize(voi
e370: 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 d);.SQLITE_API i
e380: 6e 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 nt sqlite3_shutd
e390: 6f 77 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 own(void);.SQLIT
e3a0: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
e3b0: 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 3_os_init(void);
e3c0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
e3d0: 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 sqlite3_os_end(v
e3e0: 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 oid);../*.** CAP
e3f0: 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 I3REF: Configuri
e400: 6e 67 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 ng The SQLite Li
e410: 62 72 61 72 79 20 7b 48 31 34 31 30 30 7d 20 3c brary {H14100} <
e420: 53 32 30 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a S20000><S30200>.
e430: 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
e440: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
e450: 33 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 3_config() inter
e460: 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 face is used to
e470: 6d 61 6b 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 make global conf
e480: 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 iguration.** cha
e490: 6e 67 65 73 20 74 6f 20 53 51 4c 69 74 65 20 69 nges to SQLite i
e4a0: 6e 20 6f 72 64 65 72 20 74 6f 20 74 75 6e 65 20 n order to tune
e4b0: 53 51 4c 69 74 65 20 74 6f 20 74 68 65 20 73 70 SQLite to the sp
e4c0: 65 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 0a ecific needs of.
e4d0: 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ** the applicati
e4e0: 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 on. The default
e4f0: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 configuration i
e500: 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f s recommended fo
e510: 72 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 r most.** applic
e520: 61 74 69 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 ations and so th
e530: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
e540: 75 61 6c 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 ually not necess
e550: 61 72 79 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 ary. It is.** p
e560: 72 6f 76 69 64 65 64 20 74 6f 20 73 75 70 70 6f rovided to suppo
e570: 72 74 20 72 61 72 65 20 61 70 70 6c 69 63 61 74 rt rare applicat
e580: 69 6f 6e 73 20 77 69 74 68 20 75 6e 75 73 75 61 ions with unusua
e590: 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 l needs..**.** T
e5a0: 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 he sqlite3_confi
e5b0: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 g() interface is
e5c0: 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e not threadsafe.
e5d0: 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f The applicatio
e5e0: 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 n.** must insure
e5f0: 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 that no other S
e600: 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 QLite interfaces
e610: 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 are invoked by
e620: 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 other.** threads
e630: 20 77 68 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 while sqlite3_c
e640: 6f 6e 66 69 67 28 29 20 69 73 20 72 75 6e 6e 69 onfig() is runni
e650: 6e 67 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 ng. Furthermore
e660: 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 , sqlite3_config
e670: 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 ().** may only b
e680: 65 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 e invoked prior
e690: 74 6f 20 6c 69 62 72 61 72 79 20 69 6e 69 74 69 to library initi
e6a0: 61 6c 69 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a alization using.
e6b0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 ** [sqlite3_init
e6c0: 69 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 ialize()] or aft
e6d0: 65 72 20 73 68 75 74 64 6f 77 6e 20 62 79 20 5b er shutdown by [
e6e0: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
e6f0: 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f ()]..** Note, ho
e700: 77 65 76 65 72 2c 20 74 68 61 74 20 73 71 6c 69 wever, that sqli
e710: 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e te3_config() can
e720: 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20 70 61 be called as pa
e730: 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 rt of the.** imp
e740: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 lementation of a
e750: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 n application-de
e760: 66 69 6e 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f fined [sqlite3_o
e770: 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a s_init()]..**.**
e780: 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
e790: 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 ent to sqlite3_c
e7a0: 6f 6e 66 69 67 28 29 20 69 73 20 61 6e 20 69 6e onfig() is an in
e7b0: 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 teger.** [SQLITE
e7c0: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 _CONFIG_SINGLETH
e7d0: 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 READ | configura
e7e0: 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 tion option] tha
e7f0: 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 t determines.**
e800: 77 68 61 74 20 70 72 6f 70 65 72 74 79 20 6f 66 what property of
e810: 20 53 51 4c 69 74 65 20 69 73 20 74 6f 20 62 65 SQLite is to be
e820: 20 63 6f 6e 66 69 67 75 72 65 64 2e 20 20 53 75 configured. Su
e830: 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e bsequent argumen
e840: 74 73 0a 2a 2a 20 76 61 72 79 20 64 65 70 65 6e ts.** vary depen
e850: 64 69 6e 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c ding on the [SQL
e860: 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c ITE_CONFIG_SINGL
e870: 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 ETHREAD | config
e880: 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a uration option].
e890: 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 ** in the first
e8a0: 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
e8b0: 57 68 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 When a configura
e8c0: 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 tion option is s
e8d0: 65 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 et, sqlite3_conf
e8e0: 69 67 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 ig() returns [SQ
e8f0: 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 LITE_OK]..** If
e900: 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e the option is un
e910: 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 known or SQLite
e920: 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 is unable to set
e930: 20 74 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 the option.** t
e940: 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
e950: 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a returns a non-z
e960: 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d ero [error code]
e970: 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
e980: 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 30 TS:.**.** {H1410
e990: 33 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 3} A successful
e9a0: 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 invocation of [s
e9b0: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
e9c0: 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a shall return.**
e9d0: 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 [SQLIT
e9e0: 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 E_OK]..**.** {H1
e9f0: 34 31 30 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 4106} The [sqlit
ea00: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 e3_config()] int
ea10: 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 erface shall ret
ea20: 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 urn [SQLITE_MISU
ea30: 53 45 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 SE].**
ea40: 69 66 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 if it is invoked
ea50: 20 69 6e 20 62 65 74 77 65 65 6e 20 63 61 6c 6c in between call
ea60: 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e s to [sqlite3_in
ea70: 69 74 69 61 6c 69 7a 65 28 29 5d 20 61 6e 64 0a itialize()] and.
ea80: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
ea90: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d ite3_shutdown()]
eaa0: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32 30 7d ..**.** {H14120}
eab0: 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 A successful ca
eac0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
ead0: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 onfig]([SQLITE_C
eae0: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 ONFIG_SINGLETHRE
eaf0: 41 44 5d 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 AD]).**
eb00: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 64 shall set the d
eb10: 65 66 61 75 6c 74 20 5b 74 68 72 65 61 64 69 6e efault [threadin
eb20: 67 20 6d 6f 64 65 5d 20 74 6f 20 53 69 6e 67 6c g mode] to Singl
eb30: 65 2d 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 e-thread..**.**
eb40: 7b 48 31 34 31 32 33 7d 20 41 20 73 75 63 63 65 {H14123} A succe
eb50: 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
eb60: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b qlite3_config]([
eb70: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
eb80: 4c 54 49 54 48 52 45 41 44 5d 29 0a 2a 2a 20 20 LTITHREAD]).**
eb90: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 73 65 shall se
eba0: 74 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 74 t the default [t
ebb0: 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 74 hreading mode] t
ebc0: 6f 20 4d 75 6c 74 69 2d 74 68 72 65 61 64 2e 0a o Multi-thread..
ebd0: 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32 36 7d 20 41 **.** {H14126} A
ebe0: 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c successful call
ebf0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e to [sqlite3_con
ec00: 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e fig]([SQLITE_CON
ec10: 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 5d 29 FIG_SERIALIZED])
ec20: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 .** sha
ec30: 6c 6c 20 73 65 74 20 74 68 65 20 64 65 66 61 75 ll set the defau
ec40: 6c 74 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f lt [threading mo
ec50: 64 65 5d 20 74 6f 20 53 65 72 69 61 6c 69 7a 65 de] to Serialize
ec60: 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32 39 d..**.** {H14129
ec70: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
ec80: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
ec90: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f config]([SQLITE_
eca0: 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2c 58 29 CONFIG_MUTEX],X)
ecb0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 .** whe
ecc0: 72 65 20 58 20 69 73 20 61 20 70 6f 69 6e 74 65 re X is a pointe
ecd0: 72 20 74 6f 20 61 6e 20 69 6e 69 74 69 61 6c 69 r to an initiali
ece0: 7a 65 64 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 zed [sqlite3_mut
ecf0: 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 20 ex_methods].**
ed00: 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 73 object s
ed10: 68 61 6c 6c 20 63 61 75 73 65 20 61 6c 6c 20 73 hall cause all s
ed20: 75 62 73 65 71 75 65 6e 74 20 6d 75 74 65 78 20 ubsequent mutex
ed30: 6f 70 65 72 61 74 69 6f 6e 73 20 70 65 72 66 6f operations perfo
ed40: 72 6d 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 rmed.**
ed50: 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 75 73 by SQLite to us
ed60: 65 20 74 68 65 20 6d 75 74 65 78 20 6d 65 74 68 e the mutex meth
ed70: 6f 64 73 20 74 68 61 74 20 77 65 72 65 20 70 72 ods that were pr
ed80: 65 73 65 6e 74 20 69 6e 20 58 0a 2a 2a 20 20 20 esent in X.**
ed90: 20 20 20 20 20 20 20 64 75 72 69 6e 67 20 74 68 during th
eda0: 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 e call to [sqlit
edb0: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a e3_config()]..**
edc0: 0a 2a 2a 20 7b 48 31 34 31 33 32 7d 20 41 20 73 .** {H14132} A s
edd0: 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
ede0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 o [sqlite3_confi
edf0: 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 g]([SQLITE_CONFI
ee00: 47 5f 47 45 54 4d 55 54 45 58 5d 2c 58 29 0a 2a G_GETMUTEX],X).*
ee10: 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65 * where
ee20: 20 58 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 X is a pointer
ee30: 74 6f 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 6d to an [sqlite3_m
ee40: 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 utex_methods] ob
ee50: 6a 65 63 74 20 0a 2a 2a 20 20 20 20 20 20 20 20 ject .**
ee60: 20 20 73 68 61 6c 6c 20 6f 76 65 72 77 72 69 74 shall overwrit
ee70: 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
ee80: 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f [sqlite3_mutex_
ee90: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 0a methods] object.
eea0: 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68 ** with
eeb0: 20 74 68 65 20 6d 75 74 65 78 20 6d 65 74 68 6f the mutex metho
eec0: 64 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 ds currently in
eed0: 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a use by SQLite..*
eee0: 2a 0a 2a 2a 20 7b 48 31 34 31 33 35 7d 20 41 20 *.** {H14135} A
eef0: 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 successful call
ef00: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 to [sqlite3_conf
ef10: 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 ig]([SQLITE_CONF
ef20: 49 47 5f 4d 41 4c 4c 4f 43 5d 2c 4d 29 0a 2a 2a IG_MALLOC],M).**
ef30: 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65 20 where
ef40: 4d 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 M is a pointer t
ef50: 6f 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 o an initialized
ef60: 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 [sqlite3_mem_me
ef70: 74 68 6f 64 73 5d 0a 2a 2a 20 20 20 20 20 20 20 thods].**
ef80: 20 20 20 6f 62 6a 65 63 74 20 73 68 61 6c 6c 20 object shall
ef90: 63 61 75 73 65 20 61 6c 6c 20 73 75 62 73 65 71 cause all subseq
efa0: 75 65 6e 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f uent memory allo
efb0: 63 61 74 69 6f 6e 20 6f 70 65 72 61 74 69 6f 6e cation operation
efc0: 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 65 s.** pe
efd0: 72 66 6f 72 6d 65 64 20 62 79 20 53 51 4c 69 74 rformed by SQLit
efe0: 65 20 74 6f 20 75 73 65 20 74 68 65 20 6d 65 74 e to use the met
eff0: 68 6f 64 73 20 74 68 61 74 20 77 65 72 65 20 70 hods that were p
f000: 72 65 73 65 6e 74 20 69 6e 20 0a 2a 2a 20 20 20 resent in .**
f010: 20 20 20 20 20 20 20 4d 20 64 75 72 69 6e 67 20 M during
f020: 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c the call to [sql
f030: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a ite3_config()]..
f040: 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 38 7d 20 41 **.** {H14138} A
f050: 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c successful call
f060: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e to [sqlite3_con
f070: 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e fig]([SQLITE_CON
f080: 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 5d 2c 4d FIG_GETMALLOC],M
f090: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 ).** wh
f0a0: 65 72 65 20 4d 20 69 73 20 61 20 70 6f 69 6e 74 ere M is a point
f0b0: 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74 65 er to an [sqlite
f0c0: 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 6f 3_mem_methods] o
f0d0: 62 6a 65 63 74 20 73 68 61 6c 6c 0a 2a 2a 20 20 bject shall.**
f0e0: 20 20 20 20 20 20 20 20 6f 76 65 72 77 72 69 74 overwrit
f0f0: 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
f100: 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 [sqlite3_mem_me
f110: 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 77 69 thods] object wi
f120: 74 68 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 th .**
f130: 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
f140: 61 74 69 6f 6e 20 6d 65 74 68 6f 64 73 20 63 75 ation methods cu
f150: 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 62 rrently in use b
f160: 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 51 y.** SQ
f170: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 Lite..**.** {H14
f180: 31 34 31 7d 20 41 20 73 75 63 63 65 73 73 66 75 141} A successfu
f190: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 l call to [sqlit
f1a0: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 e3_config]([SQLI
f1b0: 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 TE_CONFIG_MEMSTA
f1c0: 54 55 53 5d 2c 31 29 0a 2a 2a 20 20 20 20 20 20 TUS],1).**
f1d0: 20 20 20 20 73 68 61 6c 6c 20 65 6e 61 62 6c 65 shall enable
f1e0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
f1f0: 63 61 74 69 6f 6e 20 73 74 61 74 75 73 20 63 6f cation status co
f200: 6c 6c 65 63 74 69 6f 6e 20 6c 6f 67 69 63 2e 0a llection logic..
f210: 2a 2a 0a 2a 2a 20 7b 48 31 34 31 34 34 7d 20 41 **.** {H14144} A
f220: 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c successful call
f230: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e to [sqlite3_con
f240: 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e fig]([SQLITE_CON
f250: 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 2c 30 FIG_MEMSTATUS],0
f260: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 ).** sh
f270: 61 6c 6c 20 64 69 73 61 62 6c 65 20 74 68 65 20 all disable the
f280: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
f290: 6e 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 63 74 n status collect
f2a0: 69 6f 6e 20 6c 6f 67 69 63 2e 0a 2a 2a 0a 2a 2a ion logic..**.**
f2b0: 20 7b 48 31 34 31 34 37 7d 20 54 68 65 20 6d 65 {H14147} The me
f2c0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
f2d0: 73 74 61 74 75 73 20 63 6f 6c 6c 65 63 74 69 6f status collectio
f2e0: 6e 20 6c 6f 67 69 63 20 73 68 61 6c 6c 20 62 65 n logic shall be
f2f0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65 6e 61 .** ena
f300: 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e bled by default.
f310: 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 30 7d 20 .**.** {H14150}
f320: 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c A successful cal
f330: 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f l to [sqlite3_co
f340: 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f nfig]([SQLITE_CO
f350: 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2c 53 2c NFIG_SCRATCH],S,
f360: 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 Z,N).**
f370: 20 77 68 65 72 65 20 5a 20 61 6e 64 20 4e 20 61 where Z and N a
f380: 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 re non-negative
f390: 69 6e 74 65 67 65 72 73 20 61 6e 64 20 0a 2a 2a integers and .**
f3a0: 20 20 20 20 20 20 20 20 20 20 53 20 69 73 20 61 S is a
f3b0: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 pointer to an a
f3c0: 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79 20 62 75 ligned memory bu
f3d0: 66 66 65 72 20 6e 6f 74 20 6c 65 73 73 20 74 68 ffer not less th
f3e0: 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5a an.** Z
f3f0: 2a 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 *N bytes in size
f400: 20 73 68 61 6c 6c 20 63 61 75 73 65 20 53 20 74 shall cause S t
f410: 6f 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65 o be used by the
f420: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 63 .** [sc
f430: 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c ratch memory all
f440: 6f 63 61 74 6f 72 5d 20 66 6f 72 20 61 73 20 6d ocator] for as m
f450: 61 6e 79 20 61 73 20 4e 20 73 69 6d 75 6c 61 74 any as N simulat
f460: 61 6e 65 6f 75 73 0a 2a 2a 20 20 20 20 20 20 20 aneous.**
f470: 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 65 allocations e
f480: 61 63 68 20 6f 66 20 73 69 7a 65 20 5a 2e 0a 2a ach of size Z..*
f490: 2a 0a 2a 2a 20 7b 48 31 34 31 35 33 7d 20 41 20 *.** {H14153} A
f4a0: 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 successful call
f4b0: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 to [sqlite3_conf
f4c0: 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 ig]([SQLITE_CONF
f4d0: 49 47 5f 53 43 52 41 54 43 48 5d 2c 53 2c 5a 2c IG_SCRATCH],S,Z,
f4e0: 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 N).** w
f4f0: 68 65 72 65 20 53 20 69 73 20 61 20 4e 55 4c 4c here S is a NULL
f500: 20 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20 64 pointer shall d
f510: 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 20 20 isable the.**
f520: 20 20 20 20 20 20 20 5b 73 63 72 61 74 63 68 20 [scratch
f530: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
f540: 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 36 ]..**.** {H14156
f550: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 } A successful c
f560: 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 all to.**
f570: 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 [sqlite3_conf
f580: 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 ig]([SQLITE_CONF
f590: 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2c 53 2c IG_PAGECACHE],S,
f5a0: 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 Z,N).**
f5b0: 20 77 68 65 72 65 20 5a 20 61 6e 64 20 4e 20 61 where Z and N a
f5c0: 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 re non-negative
f5d0: 69 6e 74 65 67 65 72 73 20 61 6e 64 20 0a 2a 2a integers and .**
f5e0: 20 20 20 20 20 20 20 20 20 20 53 20 69 73 20 61 S is a
f5f0: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 pointer to an a
f600: 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79 20 62 75 ligned memory bu
f610: 66 66 65 72 20 6e 6f 74 20 6c 65 73 73 20 74 68 ffer not less th
f620: 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5a an.** Z
f630: 2a 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 *N bytes in size
f640: 20 73 68 61 6c 6c 20 63 61 75 73 65 20 53 20 74 shall cause S t
f650: 6f 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65 o be used by the
f660: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 61 .** [pa
f670: 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 gecache memory a
f680: 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f 72 20 61 73 llocator] for as
f690: 20 6d 61 6e 79 20 61 73 20 4e 20 73 69 6d 75 6c many as N simul
f6a0: 61 74 61 6e 65 6f 75 73 0a 2a 2a 20 20 20 20 20 ataneous.**
f6b0: 20 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 allocations
f6c0: 20 65 61 63 68 20 6f 66 20 73 69 7a 65 20 5a 2e each of size Z.
f6d0: 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 39 7d 20 .**.** {H14159}
f6e0: 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c A successful cal
f6f0: 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 l to.**
f700: 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 [sqlite3_config
f710: 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ]([SQLITE_CONFIG
f720: 5f 50 41 47 45 43 41 43 48 45 5d 2c 53 2c 5a 2c _PAGECACHE],S,Z,
f730: 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 N).** w
f740: 68 65 72 65 20 53 20 69 73 20 61 20 4e 55 4c 4c here S is a NULL
f750: 20 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20 64 pointer shall d
f760: 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 20 20 isable the.**
f770: 20 20 20 20 20 20 20 5b 70 61 67 65 63 61 63 68 [pagecach
f780: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
f790: 6f 72 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 or]..**.** {H141
f7a0: 36 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 62} A successful
f7b0: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
f7c0: 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 3_config]([SQLIT
f7d0: 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 5d 2c 48 E_CONFIG_HEAP],H
f7e0: 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 ,Z,N).**
f7f0: 20 20 77 68 65 72 65 20 5a 20 61 6e 64 20 4e 20 where Z and N
f800: 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 are non-negative
f810: 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 0a 2a integers and .*
f820: 2a 20 20 20 20 20 20 20 20 20 20 48 20 69 73 20 * H is
f830: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
f840: 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79 20 62 aligned memory b
f850: 75 66 66 65 72 20 6e 6f 74 20 6c 65 73 73 20 74 uffer not less t
f860: 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 han.**
f870: 5a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 Z bytes in size
f880: 73 68 61 6c 6c 20 65 6e 61 62 6c 65 20 74 68 65 shall enable the
f890: 20 5b 6d 65 6d 73 79 73 35 5d 20 6d 65 6d 6f 72 [memsys5] memor
f8a0: 79 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 20 20 y allocator.**
f8b0: 20 20 20 20 20 20 20 20 61 6e 64 20 63 61 75 73 and caus
f8c0: 65 20 69 74 20 74 6f 20 75 73 65 20 62 75 66 66 e it to use buff
f8d0: 65 72 20 53 20 61 73 20 69 74 73 20 6d 65 6d 6f er S as its memo
f8e0: 72 79 20 73 6f 75 72 63 65 20 61 6e 64 20 74 6f ry source and to
f8f0: 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 use.**
f900: 20 61 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 a minimum alloc
f910: 61 74 69 6f 6e 20 73 69 7a 65 20 6f 66 20 4e 2e ation size of N.
f920: 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 36 35 7d 20 .**.** {H14165}
f930: 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c A successful cal
f940: 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f l to [sqlite3_co
f950: 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f nfig]([SQLITE_CO
f960: 4e 46 49 47 5f 48 45 41 50 5d 2c 48 2c 5a 2c 4e NFIG_HEAP],H,Z,N
f970: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 ).** wh
f980: 65 72 65 20 48 20 69 73 20 61 20 4e 55 4c 4c 20 ere H is a NULL
f990: 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20 64 69 pointer shall di
f9a0: 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 20 20 20 sable the.**
f9b0: 20 20 20 20 20 20 5b 6d 65 6d 73 79 73 35 5d 20 [memsys5]
f9c0: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
f9d0: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 36 38 7d ..**.** {H14168}
f9e0: 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 A successful ca
f9f0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
fa00: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 onfig]([SQLITE_C
fa10: 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d ONFIG_LOOKASIDE]
fa20: 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 ,Z,N).**
fa30: 20 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74 68 shall cause th
fa40: 65 20 64 65 66 61 75 6c 74 20 5b 6c 6f 6f 6b 61 e default [looka
fa50: 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f side memory allo
fa60: 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75 72 61 cator] configura
fa70: 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 tion.**
fa80: 20 66 6f 72 20 6e 65 77 20 5b 64 61 74 61 62 61 for new [databa
fa90: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 se connections]
faa0: 74 6f 20 62 65 20 4e 20 73 6c 6f 74 73 20 6f 66 to be N slots of
fab0: 20 5a 20 62 79 74 65 73 20 65 61 63 68 2e 0a 2a Z bytes each..*
fac0: 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
fad0: 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
fae0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e int sqlite3_con
faf0: 66 69 67 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 0a fig(int, ...);..
fb00: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
fb10: 43 6f 6e 66 69 67 75 72 65 20 64 61 74 61 62 61 Configure databa
fb20: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 20 se connections
fb30: 7b 48 31 34 32 30 30 7d 20 3c 53 32 30 30 30 30 {H14200} <S20000
fb40: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
fb50: 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 L.**.** The sqli
fb60: 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 te3_db_config()
fb70: 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 interface is use
fb80: 64 20 74 6f 20 6d 61 6b 65 20 63 6f 6e 66 69 67 d to make config
fb90: 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 uration.** chang
fba0: 65 73 20 74 6f 20 61 20 5b 64 61 74 61 62 61 73 es to a [databas
fbb0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 e connection].
fbc0: 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 The interface is
fbd0: 20 73 69 6d 69 6c 61 72 20 74 6f 0a 2a 2a 20 5b similar to.** [
fbe0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
fbf0: 5d 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 ] except that th
fc00: 65 20 63 68 61 6e 67 65 73 20 61 70 70 6c 79 20 e changes apply
fc10: 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 5b to a single.** [
fc20: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
fc30: 69 6f 6e 5d 20 28 73 70 65 63 69 66 69 65 64 20 ion] (specified
fc40: 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 in the first arg
fc50: 75 6d 65 6e 74 29 2e 20 20 54 68 65 0a 2a 2a 20 ument). The.**
fc60: 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 sqlite3_db_confi
fc70: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 g() interface ca
fc80: 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 69 n only be used i
fc90: 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 mmediately after
fca0: 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
fcb0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 connection is c
fcc0: 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 reated using [sq
fcd0: 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 0a 2a lite3_open()],.*
fce0: 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 * [sqlite3_open1
fcf0: 36 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65 6()], or [sqlite
fd00: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 20 0a 3_open_v2()]. .
fd10: 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 **.** The second
fd20: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
fd30: 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 44 ite3_db_config(D
fd40: 2c 56 2c 2e 2e 2e 29 20 20 69 73 20 74 68 65 0a ,V,...) is the.
fd50: 2a 2a 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ** configuration
fd60: 20 76 65 72 62 20 2d 20 61 6e 20 69 6e 74 65 67 verb - an integ
fd70: 65 72 20 63 6f 64 65 20 74 68 61 74 20 69 6e 64 er code that ind
fd80: 69 63 61 74 65 73 20 77 68 61 74 0a 2a 2a 20 61 icates what.** a
fd90: 73 70 65 63 74 20 6f 66 20 74 68 65 20 5b 64 61 spect of the [da
fda0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
fdb0: 6e 5d 20 69 73 20 62 65 69 6e 67 20 63 6f 6e 66 n] is being conf
fdc0: 69 67 75 72 65 64 2e 0a 2a 2a 20 54 68 65 20 6f igured..** The o
fdd0: 6e 6c 79 20 63 68 6f 69 63 65 20 66 6f 72 20 74 nly choice for t
fde0: 68 69 73 20 76 61 6c 75 65 20 69 73 20 5b 53 51 his value is [SQ
fdf0: 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f LITE_DBCONFIG_LO
fe00: 4f 4b 41 53 49 44 45 5d 2e 0a 2a 2a 20 4e 65 77 OKASIDE]..** New
fe10: 20 76 65 72 62 73 20 61 72 65 20 6c 69 6b 65 6c verbs are likel
fe20: 79 20 74 6f 20 62 65 20 61 64 64 65 64 20 69 6e y to be added in
fe30: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
fe40: 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 41 of SQLite..** A
fe50: 64 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 dditional argume
fe60: 6e 74 73 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 nts depend on th
fe70: 65 20 76 65 72 62 2e 0a 2a 2a 0a 2a 2a 20 49 4e e verb..**.** IN
fe80: 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 VARIANTS:.**.**
fe90: 7b 48 31 34 32 30 33 7d 20 41 20 63 61 6c 6c 20 {H14203} A call
fea0: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 to [sqlite3_db_c
feb0: 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 5d 20 onfig(D,V,...)]
fec0: 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 shall return [SQ
fed0: 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 20 20 20 20 LITE_OK].**
fee0: 20 20 20 20 20 69 66 20 61 6e 64 20 6f 6e 6c 79 if and only
fef0: 20 69 66 20 74 68 65 20 63 61 6c 6c 20 69 73 20 if the call is
ff00: 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a successful..**.*
ff10: 2a 20 7b 48 31 34 32 30 36 7d 20 49 66 20 6f 6e * {H14206} If on
ff20: 65 20 6f 72 20 6d 6f 72 65 20 73 6c 6f 74 73 20 e or more slots
ff30: 6f 66 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 69 64 of the [lookasid
ff40: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
ff50: 6f 72 5d 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 or] for.**
ff60: 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f [database co
ff70: 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 61 72 65 20 nnection] D are
ff80: 69 6e 20 75 73 65 2c 20 74 68 65 6e 20 61 20 63 in use, then a c
ff90: 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 all to.**
ffa0: 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 [sqlite3_db_c
ffb0: 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49 54 45 onfig](D,[SQLITE
ffc0: 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 _DBCONFIG_LOOKAS
ffd0: 49 44 45 5d 2c 2e 2e 2e 29 20 73 68 61 6c 6c 0a IDE],...) shall.
ffe0: 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61 69 6c ** fail
fff0: 20 77 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 with an [SQLITE
10000 5f 42 55 53 59 5d 20 72 65 74 75 72 6e 20 63 6f _BUSY] return co
10010 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32 30 de..**.** {H1420
10020 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 9} A successful
10030 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20 20 20 call to .**
10040 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 62 [sqlite3_db
10050 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49 _config](D,[SQLI
10060 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b TE_DBCONFIG_LOOK
10070 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77 68 ASIDE],B,Z,N) wh
10080 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ere.**
10090 44 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b 64 61 D is an open [da
100a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
100b0 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e 20 61 n] and Z and N a
100c0 72 65 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 20 re positive.**
100d0 20 20 20 20 20 20 20 20 69 6e 74 65 67 65 72 73 integers
100e0 20 61 6e 64 20 42 20 69 73 20 61 6e 20 61 6c 69 and B is an ali
100f0 67 6e 65 64 20 62 75 66 66 65 72 20 61 74 20 6c gned buffer at l
10100 65 61 73 74 20 5a 2a 4e 20 62 79 74 65 73 20 69 east Z*N bytes i
10110 6e 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20 20 20 n size.**
10120 20 20 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74 shall cause t
10130 68 65 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 he [lookaside me
10140 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 mory allocator]
10150 66 6f 72 20 44 20 74 6f 20 75 73 65 20 62 75 66 for D to use buf
10160 66 65 72 20 42 20 0a 2a 2a 20 20 20 20 20 20 20 fer B .**
10170 20 20 20 77 69 74 68 20 4e 20 73 6c 6f 74 73 20 with N slots
10180 6f 66 20 5a 20 62 79 74 65 73 20 65 61 63 68 2e of Z bytes each.
10190 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32 31 32 7d 20 .**.** {H14212}
101a0 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c A successful cal
101b0 6c 20 74 6f 20 0a 2a 2a 20 20 20 20 20 20 20 20 l to .**
101c0 20 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f [sqlite3_db_co
101d0 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49 54 45 5f nfig](D,[SQLITE_
101e0 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 DBCONFIG_LOOKASI
101f0 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77 68 65 72 65 DE],B,Z,N) where
10200 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 44 20 69 .** D i
10210 73 20 61 6e 20 6f 70 65 6e 20 5b 64 61 74 61 62 s an open [datab
10220 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
10230 61 6e 64 20 5a 20 61 6e 64 20 4e 20 61 72 65 20 and Z and N are
10240 70 6f 73 69 74 69 76 65 0a 2a 2a 20 20 20 20 20 positive.**
10250 20 20 20 20 20 69 6e 74 65 67 65 72 73 20 61 6e integers an
10260 64 20 42 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e d B is NULL poin
10270 74 65 72 20 73 68 61 6c 6c 20 63 61 75 73 65 20 ter shall cause
10280 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
10290 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 [lookaside memor
102a0 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f 72 y allocator] for
102b0 20 44 20 74 6f 20 61 20 6f 62 74 61 69 6e 20 5a D to a obtain Z
102c0 2a 4e 20 62 79 74 65 20 62 75 66 66 65 72 0a 2a *N byte buffer.*
102d0 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 * from
102e0 74 68 65 20 70 72 69 6d 61 72 79 20 6d 65 6d 6f the primary memo
102f0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 61 6e 64 ry allocator and
10300 20 75 73 65 20 74 68 61 74 20 62 75 66 66 65 72 use that buffer
10310 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 .** wit
10320 68 20 4e 20 6c 6f 6f 6b 61 73 69 64 65 20 73 6c h N lookaside sl
10330 6f 74 73 20 6f 66 20 5a 20 62 79 74 65 73 20 65 ots of Z bytes e
10340 61 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32 ach..**.** {H142
10350 31 35 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 15} A successful
10360 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20 20 call to .**
10370 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 [sqlite3_d
10380 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c b_config](D,[SQL
10390 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f ITE_DBCONFIG_LOO
103a0 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77 KASIDE],B,Z,N) w
103b0 68 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 here.**
103c0 20 44 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b 64 D is an open [d
103d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
103e0 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e 20 on] and Z and N
103f0 61 72 65 20 7a 65 72 6f 20 73 68 61 6c 6c 0a 2a are zero shall.*
10400 2a 20 20 20 20 20 20 20 20 20 20 64 69 73 61 62 * disab
10410 6c 65 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 69 64 le the [lookasid
10420 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
10430 6f 72 5d 20 66 6f 72 20 44 2e 0a 2a 2a 0a 2a 2a or] for D..**.**
10440 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
10450 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
10460 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 AL int sqlite3_d
10470 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65 33 b_config(sqlite3
10480 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b *, int op, ...);
10490 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
104a0 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 : Memory Allocat
104b0 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 ion Routines {H1
104c0 30 31 35 35 7d 20 3c 53 32 30 31 32 30 3e 0a 2a 0155} <S20120>.*
104d0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
104e0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
104f0 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 of this object
10500 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 defines the inte
10510 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 53 51 rface between SQ
10520 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77 2d Lite.** and low-
10530 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
10540 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 ocation routines
10550 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a ..**.** This obj
10560 65 63 74 20 69 73 20 75 73 65 64 20 69 6e 20 6f ect is used in o
10570 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69 6e nly one place in
10580 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74 65 the SQLite inte
10590 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69 6e rface..** A poin
105a0 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e ter to an instan
105b0 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 ce of this objec
105c0 74 20 69 73 20 74 68 65 20 61 72 67 75 6d 65 6e t is the argumen
105d0 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 t to.** [sqlite3
105e0 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e 20 _config()] when
105f0 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f the configuratio
10600 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 5b n option is.** [
10610 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 SQLITE_CONFIG_MA
10620 4c 4c 4f 43 5d 2e 20 20 42 79 20 63 72 65 61 74 LLOC]. By creat
10630 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 ing an instance
10640 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a of this object.*
10650 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20 69 74 * and passing it
10660 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e to [sqlite3_con
10670 66 69 67 28 29 5d 20 64 75 72 69 6e 67 20 63 6f fig()] during co
10680 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 61 6e 0a nfiguration, an.
10690 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 ** application c
106a0 61 6e 20 73 70 65 63 69 66 79 20 61 6e 20 61 6c an specify an al
106b0 74 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72 79 ternative memory
106c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
106d0 79 73 74 65 6d 0a 2a 2a 20 66 6f 72 20 53 51 4c ystem.** for SQL
106e0 69 74 65 20 74 6f 20 75 73 65 20 66 6f 72 20 61 ite to use for a
106f0 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 ll of its dynami
10700 63 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 2e 0a c memory needs..
10710 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
10720 53 51 4c 69 74 65 20 63 6f 6d 65 73 20 77 69 74 SQLite comes wit
10730 68 20 61 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d h a built-in mem
10740 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68 ory allocator th
10750 61 74 20 69 73 0a 2a 2a 20 70 65 72 66 65 63 74 at is.** perfect
10760 6c 79 20 61 64 65 71 75 61 74 65 20 66 6f 72 20 ly adequate for
10770 74 68 65 20 6f 76 65 72 77 68 65 6c 6d 69 6e 67 the overwhelming
10780 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 61 70 70 majority of app
10790 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 6e 64 lications.** and
107a0 20 74 68 61 74 20 74 68 69 73 20 6f 62 6a 65 63 that this objec
107b0 74 20 69 73 20 6f 6e 6c 79 20 75 73 65 66 75 6c t is only useful
107c0 20 74 6f 20 61 20 74 69 6e 79 20 6d 69 6e 6f 72 to a tiny minor
107d0 69 74 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69 ity of applicati
107e0 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 73 70 65 63 ons.** with spec
107f0 69 61 6c 69 7a 65 64 20 6d 65 6d 6f 72 79 20 61 ialized memory a
10800 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 llocation requir
10810 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 20 6f 62 ements. This ob
10820 6a 65 63 74 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 ject is.** also
10830 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 used during test
10840 69 6e 67 20 6f 66 20 53 51 4c 69 74 65 20 69 6e ing of SQLite in
10850 20 6f 72 64 65 72 20 74 6f 20 73 70 65 63 69 66 order to specif
10860 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 y an alternative
10870 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 .** memory alloc
10880 61 74 6f 72 20 74 68 61 74 20 73 69 6d 75 6c 61 ator that simula
10890 74 65 73 20 6d 65 6d 6f 72 79 20 6f 75 74 2d 6f tes memory out-o
108a0 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 f-memory conditi
108b0 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 64 65 72 20 ons in.** order
108c0 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 53 to verify that S
108d0 51 4c 69 74 65 20 72 65 63 6f 76 65 72 73 20 67 QLite recovers g
108e0 72 61 63 65 66 75 6c 6c 79 20 66 72 6f 6d 20 73 racefully from s
108f0 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e uch.** condition
10900 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 61 s..**.** The xMa
10910 6c 6c 6f 63 2c 20 78 46 72 65 65 2c 20 61 6e 64 lloc, xFree, and
10920 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74 68 6f 64 xRealloc method
10930 73 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69 6b 65 s must work like
10940 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 the.** malloc()
10950 2c 20 66 72 65 65 28 29 2c 20 61 6e 64 20 72 65 , free(), and re
10960 61 6c 6c 6f 63 28 29 20 66 75 6e 63 74 69 6f 6e alloc() function
10970 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 s from the stand
10980 61 72 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a ard library..**.
10990 2a 2a 20 78 53 69 7a 65 20 73 68 6f 75 6c 64 20 ** xSize should
109a0 72 65 74 75 72 6e 20 74 68 65 20 61 6c 6c 6f 63 return the alloc
109b0 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20 6d ated size of a m
109c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
109d0 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 6f .** previously o
109e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 78 4d 61 btained from xMa
109f0 6c 6c 6f 63 20 6f 72 20 78 52 65 61 6c 6c 6f 63 lloc or xRealloc
10a00 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 65 64 . The allocated
10a10 20 73 69 7a 65 0a 2a 2a 20 69 73 20 61 6c 77 61 size.** is alwa
10a20 79 73 20 61 74 20 6c 65 61 73 74 20 61 73 20 62 ys at least as b
10a30 69 67 20 61 73 20 74 68 65 20 72 65 71 75 65 73 ig as the reques
10a40 74 65 64 20 73 69 7a 65 20 62 75 74 20 6d 61 79 ted size but may
10a50 20 62 65 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a be larger..**.*
10a60 2a 20 54 68 65 20 78 52 6f 75 6e 64 75 70 20 6d * The xRoundup m
10a70 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 77 68 ethod returns wh
10a80 61 74 20 77 6f 75 6c 64 20 62 65 20 74 68 65 20 at would be the
10a90 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f allocated size o
10aa0 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 79 20 61 6c f.** a memory al
10ab0 6c 6f 63 61 74 69 6f 6e 20 67 69 76 65 6e 20 61 location given a
10ac0 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 71 75 particular requ
10ad0 65 73 74 65 64 20 73 69 7a 65 2e 20 20 4d 6f 73 ested size. Mos
10ae0 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f t memory.** allo
10af0 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 cators round up
10b00 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
10b10 6e 73 20 61 74 20 6c 65 61 73 74 20 74 6f 20 74 ns at least to t
10b20 68 65 20 6e 65 78 74 20 6d 75 6c 74 69 70 6c 65 he next multiple
10b30 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 6f 6d 65 20 .** of 8. Some
10b40 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 allocators round
10b50 20 75 70 20 74 6f 20 61 20 6c 61 72 67 65 72 20 up to a larger
10b60 6d 75 6c 74 69 70 6c 65 20 6f 72 20 74 6f 20 61 multiple or to a
10b70 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a power of 2..**.
10b80 2a 2a 20 54 68 65 20 78 49 6e 69 74 20 6d 65 74 ** The xInit met
10b90 68 6f 64 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 hod initializes
10ba0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
10bb0 61 74 6f 72 2e 20 20 28 46 6f 72 20 65 78 61 6d ator. (For exam
10bc0 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d 69 67 68 74 ple,.** it might
10bd0 20 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72 65 allocate any re
10be0 71 75 69 72 65 20 6d 75 74 65 78 65 73 20 6f 72 quire mutexes or
10bf0 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 initialize inte
10c00 72 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 73 74 72 rnal data.** str
10c10 75 63 74 75 72 65 73 2e 20 20 54 68 65 20 78 53 uctures. The xS
10c20 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69 hutdown method i
10c30 73 20 69 6e 76 6f 6b 65 64 20 28 69 6e 64 69 72 s invoked (indir
10c40 65 63 74 6c 79 29 20 62 79 0a 2a 2a 20 5b 73 71 ectly) by.** [sq
10c50 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
10c60 5d 20 61 6e 64 20 73 68 6f 75 6c 64 20 64 65 61 ] and should dea
10c70 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72 65 73 6f llocate any reso
10c80 75 72 63 65 73 20 61 63 71 75 69 72 65 64 0a 2a urces acquired.*
10c90 2a 20 62 79 20 78 49 6e 69 74 2e 20 20 54 68 65 * by xInit. The
10ca0 20 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 65 pAppData pointe
10cb0 72 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 r is used as the
10cc0 20 6f 6e 6c 79 20 70 61 72 61 6d 65 74 65 72 20 only parameter
10cd0 74 6f 0a 2a 2a 20 78 49 6e 69 74 20 61 6e 64 20 to.** xInit and
10ce0 78 53 68 75 74 64 6f 77 6e 2e 0a 2a 2f 0a 74 79 xShutdown..*/.ty
10cf0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
10d00 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
10d10 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
10d20 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c hods;.struct sql
10d30 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
10d40 20 7b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61 {. void *(*xMa
10d50 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 20 20 20 20 lloc)(int);
10d60 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c /* Memory al
10d70 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f location functio
10d80 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 n */. void (*xF
10d90 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 ree)(void*);
10da0 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 61 20 /* Free a
10db0 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e prior allocation
10dc0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52 */. void *(*xR
10dd0 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e ealloc)(void*,in
10de0 74 29 3b 20 20 2f 2a 20 52 65 73 69 7a 65 20 61 t); /* Resize a
10df0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a n allocation */.
10e00 20 20 69 6e 74 20 28 2a 78 53 69 7a 65 29 28 76 int (*xSize)(v
10e10 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 oid*);
10e20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 /* Return the s
10e30 69 7a 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 ize of an alloca
10e40 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a tion */. int (*
10e50 78 52 6f 75 6e 64 75 70 29 28 69 6e 74 29 3b 20 xRoundup)(int);
10e60 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e /* Roun
10e70 64 20 75 70 20 72 65 71 75 65 73 74 20 73 69 7a d up request siz
10e80 65 20 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 e to allocation
10e90 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a size */. int (*
10ea0 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 20 20 xInit)(void*);
10eb0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 /* Init
10ec0 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 ialize the memor
10ed0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 y allocator */.
10ee0 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77 void (*xShutdow
10ef0 6e 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 n)(void*);
10f00 2f 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 /* Deinitialize
10f10 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
10f20 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a ator */. void *
10f30 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 pAppData;
10f40 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
10f50 6d 65 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20 ment to xInit()
10f60 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20 and xShutdown()
10f70 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.};../*.** CAP
10f80 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 I3REF: Configura
10f90 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31 tion Options {H1
10fa0 30 31 36 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 0160} <S20000>.*
10fb0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
10fc0 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 *.** These const
10fd0 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 61 ants are the ava
10fe0 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63 ilable integer c
10ff0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
11000 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e ions that.** can
11010 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 be passed as th
11020 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
11030 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 to the [sqlite3
11040 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 _config()] inter
11050 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 face..**.** New
11060 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
11070 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 tions may be add
11080 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c ed in future rel
11090 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e eases of SQLite.
110a0 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e .** Existing con
110b0 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
110c0 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 ns might be disc
110d0 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 ontinued. Appli
110e0 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c cations.** shoul
110f0 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 d check the retu
11100 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 rn code from [sq
11110 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 lite3_config()]
11120 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
11130 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f t.** the call wo
11140 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 rked. The [sqli
11150 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e te3_config()] in
11160 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 terface will ret
11170 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 urn a.** non-zer
11180 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 o [error code] i
11190 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 f a discontinued
111a0 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 or unsupported
111b0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
111c0 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b tion.** is invok
111d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a ed..**.** <dl>.*
111e0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
111f0 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 FIG_SINGLETHREAD
11200 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
11210 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 re are no argume
11220 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 nts to this opti
11230 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e on. This option
11240 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c disables.** all
11250 20 6d 75 74 65 78 69 6e 67 20 61 6e 64 20 70 75 mutexing and pu
11260 74 73 20 53 51 4c 69 74 65 20 69 6e 74 6f 20 61 ts SQLite into a
11270 20 6d 6f 64 65 20 77 68 65 72 65 20 69 74 20 63 mode where it c
11280 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 0a an only be used.
11290 2a 2a 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 ** by a single t
112a0 68 72 65 61 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a hread.</dd>.**.*
112b0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
112c0 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 3c FIG_MULTITHREAD<
112d0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 /dt>.** <dd>Ther
112e0 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e e are no argumen
112f0 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f ts to this optio
11300 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 n. This option
11310 64 69 73 61 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 disables.** mute
11320 78 69 6e 67 20 6f 6e 20 5b 64 61 74 61 62 61 73 xing on [databas
11330 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e e connection] an
11340 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 d [prepared stat
11350 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a ement] objects..
11360 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 ** The applicati
11370 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c on is responsibl
11380 65 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e e for serializin
11390 67 20 61 63 63 65 73 73 20 74 6f 0a 2a 2a 20 5b g access to.** [
113a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
113b0 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 ions] and [prepa
113c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e red statements].
113d0 20 20 42 75 74 20 6f 74 68 65 72 20 6d 75 74 65 But other mute
113e0 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c xes.** are enabl
113f0 65 64 20 73 6f 20 74 68 61 74 20 53 51 4c 69 74 ed so that SQLit
11400 65 20 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74 e will be safe t
11410 6f 20 75 73 65 20 69 6e 20 61 20 6d 75 6c 74 69 o use in a multi
11420 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 65 6e 76 -threaded.** env
11430 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 ironment as long
11440 20 61 73 20 6e 6f 20 74 77 6f 20 74 68 72 65 61 as no two threa
11450 64 73 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 ds attempt to us
11460 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 e the same.** [d
11470 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
11480 6f 6e 5d 20 61 74 20 74 68 65 20 73 61 6d 65 20 on] at the same
11490 74 69 6d 65 2e 20 20 53 65 65 20 74 68 65 20 5b time. See the [
114a0 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 0a threading mode].
114b0 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e ** documentation
114c0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
114d0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 information.</dd
114e0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
114f0 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c TE_CONFIG_SERIAL
11500 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 IZED</dt>.** <dd
11510 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 >There are no ar
11520 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 guments to this
11530 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 option. This op
11540 74 69 6f 6e 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 tion enables.**
11550 61 6c 6c 20 6d 75 74 65 78 65 73 20 69 6e 63 6c all mutexes incl
11560 75 64 69 6e 67 20 74 68 65 20 72 65 63 75 72 73 uding the recurs
11570 69 76 65 0a 2a 2a 20 6d 75 74 65 78 65 73 20 6f ive.** mutexes o
11580 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e n [database conn
11590 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 ection] and [pre
115a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
115b0 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 objects..** In
115c0 74 68 69 73 20 6d 6f 64 65 20 28 77 68 69 63 68 this mode (which
115d0 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 is the default
115e0 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 when SQLite is c
115f0 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 ompiled with.**
11600 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
11610 46 45 3d 31 5d 29 20 74 68 65 20 53 51 4c 69 74 FE=1]) the SQLit
11620 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 69 e library will i
11630 74 73 65 6c 66 20 73 65 72 69 61 6c 69 7a 65 20 tself serialize
11640 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 access.** to [da
11650 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
11660 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 ns] and [prepare
11670 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f d statements] so
11680 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 70 70 that the.** app
11690 6c 69 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 lication is free
116a0 20 74 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 to use the same
116b0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
116c0 63 74 69 6f 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a ction] or the.**
116d0 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 same [prepared
116e0 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69 statement] in di
116f0 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 73 20 fferent threads
11700 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
11710 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68 ..** See the [th
11720 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f reading mode] do
11730 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 cumentation for
11740 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
11750 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a mation.</dd>.**.
11760 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ** <dt>SQLITE_CO
11770 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e NFIG_MALLOC</dt>
11780 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
11790 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
117a0 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
117b0 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
117c0 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 o an.** instance
117d0 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
117e0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 _mem_methods] st
117f0 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 ructure. The ar
11800 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 gument specifies
11810 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 .** alternative
11820 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
11830 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 allocation rout
11840 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 ines to be used
11850 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 in place of.** t
11860 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
11870 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 62 75 tion routines bu
11880 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e ilt into SQLite.
11890 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
118a0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 SQLITE_CONFIG_GE
118b0 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 TMALLOC</dt>.**
118c0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
118d0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
118e0 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 rgument which is
118f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
11900 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
11910 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d the [sqlite3_mem
11920 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 _methods] struct
11930 75 72 65 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 ure. The [sqlit
11940 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a e3_mem_methods].
11950 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ** structure is
11960 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 filled with the
11970 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 currently define
11980 64 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 d memory allocat
11990 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a ion routines..**
119a0 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e This option can
119b0 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72 be used to over
119c0 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c 74 load the default
119d0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
119e0 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 on.** routines w
119f0 69 74 68 20 61 20 77 72 61 70 70 65 72 20 74 68 ith a wrapper th
11a00 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e 73 20 6d at simulations m
11a10 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
11a20 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a 2a 20 74 failure or.** t
11a30 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 75 73 61 racks memory usa
11a40 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e ge, for example.
11a50 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
11a60 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 SQLITE_CONFIG_ME
11a70 4d 53 54 41 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 MSTATUS</dt>.**
11a80 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
11a90 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 61 72 67 takes single arg
11aa0 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e ument of type in
11ab0 74 2c 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 t, interpreted a
11ac0 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c s a .** boolean,
11ad0 20 77 68 69 63 68 20 65 6e 61 62 6c 65 73 20 6f which enables o
11ae0 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 r disables the c
11af0 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 6d 65 6d ollection of mem
11b00 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a ory allocation .
11b10 2a 2a 20 73 74 61 74 69 73 74 69 63 73 2e 20 57 ** statistics. W
11b20 68 65 6e 20 64 69 73 61 62 6c 65 64 2c 20 74 68 hen disabled, th
11b30 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 69 e following SQLi
11b40 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 62 65 te interfaces be
11b50 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65 come .** non-ope
11b60 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c rational:.** <
11b70 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 ul>.** <li> [s
11b80 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 qlite3_memory_us
11b90 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 ed()].** <li>
11ba0 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f [sqlite3_memory_
11bb0 68 69 67 68 77 61 74 65 72 28 29 5d 0a 2a 2a 20 highwater()].**
11bc0 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
11bd0 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 soft_heap_limit(
11be0 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 )].** <li> [sq
11bf0 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 0a lite3_status()].
11c00 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f ** </ul>.** </
11c10 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
11c20 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 LITE_CONFIG_SCRA
11c30 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TCH</dt>.** <dd>
11c40 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 This option spec
11c50 69 66 69 65 73 20 61 20 73 74 61 74 69 63 20 6d ifies a static m
11c60 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 61 emory buffer tha
11c70 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65 t SQLite can use
11c80 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 63 68 20 for.** scratch
11c90 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 65 20 61 memory. There a
11ca0 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e re three argumen
11cb0 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 72 20 74 ts: A pointer t
11cc0 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74 68 o the memory, th
11cd0 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 e.** size of eac
11ce0 68 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 h scratch buffer
11cf0 20 28 73 7a 29 2c 20 61 6e 64 20 74 68 65 20 6e (sz), and the n
11d00 75 6d 62 65 72 20 6f 66 20 62 75 66 66 65 72 73 umber of buffers
11d10 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0a 2a 2a (N). The sz.**
11d20 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 argument must b
11d30 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 e a multiple of
11d40 31 36 2e 20 54 68 65 20 73 7a 20 70 61 72 61 6d 16. The sz param
11d50 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 61 eter should be a
11d60 20 66 65 77 20 62 79 74 65 73 0a 2a 2a 20 6c 61 few bytes.** la
11d70 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 61 63 rger than the ac
11d80 74 75 61 6c 20 73 63 72 61 74 63 68 20 73 70 61 tual scratch spa
11d90 63 65 20 72 65 71 75 69 72 65 64 20 64 75 65 20 ce required due
11da0 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68 65 61 internal overhea
11db0 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 0a d..** The first.
11dc0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 ** argument shou
11dd0 6c 64 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 ld point to an a
11de0 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 llocation of at
11df0 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 74 65 73 least sz*N bytes
11e00 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 of memory..** S
11e10 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 6e QLite will use n
11e20 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 o more than one
11e30 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 61 scratch buffer a
11e40 74 20 6f 6e 63 65 20 70 65 72 20 74 68 72 65 61 t once per threa
11e50 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c d, so.** N shoul
11e60 64 20 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 d be set to the
11e70 65 78 70 65 63 74 65 64 20 6d 61 78 69 6d 75 6d expected maximum
11e80 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 number of threa
11e90 64 73 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 70 ds. The sz.** p
11ea0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 arameter should
11eb0 62 65 20 36 20 74 69 6d 65 73 20 74 68 65 20 73 be 6 times the s
11ec0 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 ize of the large
11ed0 73 74 20 64 61 74 61 62 61 73 65 20 70 61 67 65 st database page
11ee0 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 72 61 74 63 size..** Scratc
11ef0 68 20 62 75 66 66 65 72 73 20 61 72 65 20 75 73 h buffers are us
11f00 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 ed as part of th
11f10 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 65 20 e btree balance
11f20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 0a 2a operation. If.*
11f30 2a 20 54 68 65 20 62 74 72 65 65 20 62 61 6c 61 * The btree bala
11f40 6e 63 65 72 20 6e 65 65 64 73 20 61 64 64 69 74 ncer needs addit
11f50 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 62 65 79 ional memory bey
11f60 6f 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 ond what is prov
11f70 69 64 65 64 20 62 79 0a 2a 2a 20 73 63 72 61 74 ided by.** scrat
11f80 63 68 20 62 75 66 66 65 72 73 20 6f 72 20 69 66 ch buffers or if
11f90 20 6e 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 no scratch buff
11fa0 65 72 20 73 70 61 63 65 20 69 73 20 73 70 65 63 er space is spec
11fb0 69 66 69 65 64 2c 20 74 68 65 6e 20 53 51 4c 69 ified, then SQLi
11fc0 74 65 0a 2a 2a 20 67 6f 65 73 20 74 6f 20 5b 73 te.** goes to [s
11fd0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
11fe0 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d to obtain the m
11ff0 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 3c emory it needs.<
12000 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
12010 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
12020 45 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c ECACHE</dt>.** <
12030 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 dd>This option s
12040 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69 pecifies a stati
12050 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 c memory buffer
12060 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 that SQLite can
12070 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 64 use for.** the d
12080 61 74 61 62 61 73 65 20 70 61 67 65 20 63 61 63 atabase page cac
12090 68 65 20 77 69 74 68 20 74 68 65 20 64 65 66 61 he with the defa
120a0 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20 69 ult page cache i
120b0 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a mplemenation. .
120c0 2a 2a 20 54 68 69 73 20 63 6f 6e 66 69 67 75 72 ** This configur
120d0 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 ation should not
120e0 20 62 65 20 75 73 65 64 20 69 66 20 61 6e 20 61 be used if an a
120f0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
12100 65 20 70 61 67 65 0a 2a 2a 20 63 61 63 68 65 20 e page.** cache
12110 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
12120 73 20 6c 6f 61 64 65 64 20 75 73 69 6e 67 20 74 s loaded using t
12130 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 he SQLITE_CONFIG
12140 5f 50 43 41 43 48 45 20 6f 70 74 69 6f 6e 2e 0a _PCACHE option..
12150 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 ** There are thr
12160 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 ee arguments to
12170 74 68 69 73 20 6f 70 74 69 6f 6e 3a 20 41 20 70 this option: A p
12180 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a ointer to the.**
12190 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 73 69 7a memory, the siz
121a0 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 62 e of each page b
121b0 75 66 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 uffer (sz), and
121c0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
121d0 67 65 73 20 28 4e 29 2e 0a 2a 2a 20 54 68 65 20 ges (N)..** The
121e0 73 7a 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 sz argument must
121f0 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 be a power of t
12200 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 wo between 512 a
12210 6e 64 20 33 32 37 36 38 2e 20 20 54 68 65 20 66 nd 32768. The f
12220 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 irst.** argument
12230 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f should point to
12240 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f an allocation o
12250 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 f at least sz*N
12260 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e bytes of memory.
12270 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 .** SQLite will
12280 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 use the memory p
12290 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 66 rovided by the f
122a0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
122b0 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a 2a 20 satisfy its.**
122c0 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66 6f 72 memory needs for
122d0 20 74 68 65 20 66 69 72 73 74 20 4e 20 70 61 67 the first N pag
122e0 65 73 20 74 68 61 74 20 69 74 20 61 64 64 73 20 es that it adds
122f0 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20 61 64 to cache. If ad
12300 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 67 65 ditional.** page
12310 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 69 73 cache memory is
12320 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64 20 77 needed beyond w
12330 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 20 hat is provided
12340 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 by this option,
12350 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 67 then.** SQLite g
12360 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f oes to [sqlite3_
12370 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 74 68 malloc()] for th
12380 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 6f e additional sto
12390 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a 20 54 rage space..** T
123a0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
123b0 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e 65 20 n might use one
123c0 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 4e or more of the N
123d0 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f 6c 64 buffers to hold
123e0 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 63 6f .** memory acco
123f0 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 unting informati
12400 6f 6e 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 on. </dd>.**.**
12410 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
12420 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a 20 3c G_HEAP</dt>.** <
12430 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 dd>This option s
12440 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69 pecifies a stati
12450 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 c memory buffer
12460 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c that SQLite will
12470 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 use.** for all
12480 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 20 6d of its dynamic m
12490 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
124a0 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 20 74 68 needs beyond th
124b0 6f 73 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 ose provided.**
124c0 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 for by [SQLITE_C
124d0 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61 ONFIG_SCRATCH] a
124e0 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 nd [SQLITE_CONFI
124f0 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a 2a 2a G_PAGECACHE]..**
12500 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 There are three
12510 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 20 70 6f arguments: A po
12520 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d inter to the mem
12530 6f 72 79 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 ory, the number
12540 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 of.** bytes in t
12550 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 he memory buffer
12560 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 , and the minimu
12570 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a m allocation siz
12580 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 66 69 e. If.** the fi
12590 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65 rst pointer (the
125a0 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 memory pointer)
125b0 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 is NULL, then S
125c0 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a 2a 2a QLite reverts.**
125d0 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20 64 65 to using its de
125e0 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c fault memory all
125f0 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79 73 74 ocator (the syst
12600 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c em malloc() impl
12610 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a 2a 20 ementation),.**
12620 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72 69 6f undoing any prio
12630 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 r invocation of
12640 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
12650 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68 65 0a ALLOC]. If the.
12660 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 ** memory pointe
12670 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e r is not NULL an
12680 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 d either [SQLITE
12690 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 5d _ENABLE_MEMSYS3]
126a0 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 or.** [SQLITE_E
126b0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d 20 61 NABLE_MEMSYS5] a
126c0 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e re defined, then
126d0 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 the alternative
126e0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 memory.** alloc
126f0 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65 64 20 ator is engaged
12700 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20 6f 66 to handle all of
12710 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72 79 20 SQLites memory
12720 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 allocation needs
12730 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
12740 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d >SQLITE_CONFIG_M
12750 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 UTEX</dt>.** <dd
12760 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b >This option tak
12770 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 es a single argu
12780 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 ment which is a
12790 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a pointer to an.**
127a0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
127b0 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f [sqlite3_mutex_
127c0 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 methods] structu
127d0 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e re. The argumen
127e0 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 t specifies.** a
127f0 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d 6c lternative low-l
12800 65 76 65 6c 20 6d 75 74 65 78 20 72 6f 75 74 69 evel mutex routi
12810 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 69 nes to be used i
12820 6e 20 70 6c 61 63 65 0a 2a 2a 20 74 68 65 20 6d n place.** the m
12830 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 62 75 utex routines bu
12840 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e ilt into SQLite.
12850 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
12860 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 SQLITE_CONFIG_GE
12870 54 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c TMUTEX</dt>.** <
12880 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 dd>This option t
12890 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 akes a single ar
128a0 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 gument which is
128b0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a a pointer to an.
128c0 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ** instance of t
128d0 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 he [sqlite3_mute
128e0 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 x_methods] struc
128f0 74 75 72 65 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 ture. The.** [s
12900 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
12910 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 hods].** structu
12920 72 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 re is filled wit
12930 68 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 h the currently
12940 64 65 66 69 6e 65 64 20 6d 75 74 65 78 20 72 6f defined mutex ro
12950 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 73 20 utines..** This
12960 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 option can be us
12970 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 ed to overload t
12980 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74 65 78 he default mutex
12990 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 allocation.** r
129a0 6f 75 74 69 6e 65 73 20 77 69 74 68 20 61 20 77 outines with a w
129b0 72 61 70 70 65 72 20 75 73 65 64 20 74 6f 20 74 rapper used to t
129c0 72 61 63 6b 20 6d 75 74 65 78 20 75 73 61 67 65 rack mutex usage
129d0 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 for performance
129e0 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 20 6f 72 .** profiling or
129f0 20 74 65 73 74 69 6e 67 2c 20 66 6f 72 20 65 78 testing, for ex
12a00 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ample.</dd>.**.*
12a10 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
12a20 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c 2f 64 FIG_LOOKASIDE</d
12a30 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
12a40 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 77 6f 20 ption takes two
12a50 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64 arguments that d
12a60 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65 66 etermine the def
12a70 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 ault.** memory a
12a80 6c 6c 63 61 74 69 6f 6e 20 6c 6f 6f 6b 61 73 69 llcation lookasi
12a90 64 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e de optimization.
12aa0 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 The first argu
12ab0 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 ment is the.** s
12ac0 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b ize of each look
12ad0 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f aside buffer slo
12ae0 74 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 t and the second
12af0 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
12b00 66 0a 2a 2a 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 f.** slots alloc
12b10 61 74 65 64 20 74 6f 20 65 61 63 68 20 64 61 74 ated to each dat
12b20 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
12b30 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
12b40 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 >SQLITE_CONFIG_P
12b50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 CACHE</dt>.** <d
12b60 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 d>This option ta
12b70 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 kes a single arg
12b80 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 ument which is a
12b90 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 pointer to.** a
12ba0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 n [sqlite3_pcach
12bb0 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 e_methods] objec
12bc0 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63 74 20 t. This object
12bd0 73 70 65 63 69 66 69 65 73 20 74 68 65 20 69 6e specifies the in
12be0 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 61 20 terface.** to a
12bf0 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 custom page cach
12c00 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
12c10 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 . SQLite makes
12c20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a a copy of the.**
12c30 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73 65 73 object and uses
12c40 20 69 74 20 66 6f 72 20 70 61 67 65 20 63 61 63 it for page cac
12c50 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
12c60 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a tions.</dd>.**.*
12c70 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
12c80 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c 2f 64 FIG_GETPCACHE</d
12c90 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
12ca0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ption takes a si
12cb0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 ngle argument wh
12cc0 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ich is a pointer
12cd0 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 to an.** [sqlit
12ce0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
12cf0 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51 4c 69 s] object. SQLi
12d00 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 te copies of the
12d10 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61 67 65 current.** page
12d20 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
12d30 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61 74 20 ation into that
12d40 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a object.</dd>.**.
12d50 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 ** </dl>.*/.#def
12d60 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ine SQLITE_CONFI
12d70 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 20 G_SINGLETHREAD
12d80 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65 1 /* nil */.#de
12d90 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 fine SQLITE_CONF
12da0 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20 20 IG_MULTITHREAD
12db0 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 2 /* nil */.#d
12dc0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
12dd0 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 20 20 FIG_SERIALIZED
12de0 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 3 /* nil */.#
12df0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
12e00 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 NFIG_MALLOC
12e10 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 33 4 /* sqlite3
12e20 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f _mem_methods* */
12e30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
12e40 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 CONFIG_GETMALLOC
12e50 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74 5 /* sqlit
12e60 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 e3_mem_methods*
12e70 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
12e80 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
12e90 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 6f 69 6 /* voi
12ea0 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 d*, int sz, int
12eb0 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c N */.#define SQL
12ec0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
12ed0 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a 20 76 ACHE 7 /* v
12ee0 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e oid*, int sz, in
12ef0 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 t N */.#define S
12f00 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 QLITE_CONFIG_HEA
12f10 50 20 20 20 20 20 20 20 20 20 20 38 20 20 2f 2a P 8 /*
12f20 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 79 74 void*, int nByt
12f30 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a 23 64 e, int min */.#d
12f40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
12f50 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 20 20 FIG_MEMSTATUS
12f60 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 6e 20 9 /* boolean
12f70 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
12f80 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 20 20 E_CONFIG_MUTEX
12f90 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 71 6c 10 /* sql
12fa0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
12fb0 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ds* */.#define S
12fc0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 QLITE_CONFIG_GET
12fd0 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 2f 2a MUTEX 11 /*
12fe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
12ff0 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20 70 72 ethods* */./* pr
13000 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54 45 5f eviously SQLITE_
13010 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c 4c 4f CONFIG_CHUNKALLO
13020 43 20 31 32 20 77 68 69 63 68 20 69 73 20 6e 6f C 12 which is no
13030 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a 23 64 w unused. */ .#d
13040 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
13050 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 FIG_LOOKASIDE
13060 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e 74 20 13 /* int int
13070 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
13080 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 E_CONFIG_PCACHE
13090 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73 71 6c 14 /* sql
130a0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
130b0 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ods* */.#define
130c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 SQLITE_CONFIG_GE
130d0 54 50 43 41 43 48 45 20 20 20 20 31 35 20 20 2f TPCACHE 15 /
130e0 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 * sqlite3_pcache
130f0 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a 2f 2a _methods* */../*
13100 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f .** CAPI3REF: Co
13110 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 nfiguration Opti
13120 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c 53 32 ons {H10170} <S2
13130 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0000>.** EXPERIM
13140 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 ENTAL.**.** Thes
13150 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 e constants are
13160 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e the available in
13170 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 61 74 teger configurat
13180 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 ion options that
13190 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 73 65 .** can be passe
131a0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
131b0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
131c0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 [sqlite3_db_conf
131d0 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e ig()] interface.
131e0 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69 .**.** New confi
131f0 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 guration options
13200 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e may be added in
13210 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 future releases
13220 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45 of SQLite..** E
13230 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 72 xisting configur
13240 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69 ation options mi
13250 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e ght be discontin
13260 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 6f ued. Applicatio
13270 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65 ns.** should che
13280 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f ck the return co
13290 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 de from [sqlite3
132a0 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f _db_config()] to
132b0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a make sure that.
132c0 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b ** the call work
132d0 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 ed. The [sqlite
132e0 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 3_db_config()] i
132f0 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 nterface will re
13300 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 turn a.** non-ze
13310 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 ro [error code]
13320 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 if a discontinue
13330 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 d or unsupported
13340 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
13350 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f ption.** is invo
13360 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a ked..**.** <dl>.
13370 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 ** <dt>SQLITE_DB
13380 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 CONFIG_LOOKASIDE
13390 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
133a0 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 s option takes t
133b0 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 hree additional
133c0 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64 arguments that d
133d0 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a 2a 2a etermine the .**
133e0 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f [lookaside memo
133f0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f ry allocator] co
13400 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 20 nfiguration for
13410 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
13420 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20 54 68 nnection]..** Th
13430 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
13440 20 28 74 68 65 20 74 68 69 72 64 20 70 61 72 61 (the third para
13450 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
13460 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 3_db_config()] i
13470 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 s a.** pointer t
13480 6f 20 61 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 o a memory buffe
13490 72 20 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f 6f r to use for loo
134a0 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 20 20 kaside memory.
134b0 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 The first.** arg
134c0 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e 55 4c ument may be NUL
134d0 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 L in which case
134e0 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f SQLite will allo
134f0 63 61 74 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 cate the lookasi
13500 64 65 0a 2a 2a 20 62 75 66 66 65 72 20 69 74 73 de.** buffer its
13510 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 elf using [sqlit
13520 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 e3_malloc()]. T
13530 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
13540 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 69 7a nt is the.** siz
13550 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b 61 73 e of each lookas
13560 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f 74 20 ide buffer slot
13570 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 and the third ar
13580 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 gument is the nu
13590 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 mber of.** slots
135a0 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 . The size of t
135b0 68 65 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 he buffer in the
135c0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
135d0 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20 must be greater
135e0 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 61 6c than.** or equal
135f0 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63 74 20 to the product
13600 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e of the second an
13610 64 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 d third argument
13620 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f s.</dd>.**.** </
13630 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 dl>.*/.#define S
13640 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c QLITE_DBCONFIG_L
13650 4f 4f 4b 41 53 49 44 45 20 20 20 20 31 30 30 31 OOKASIDE 1001
13660 20 20 2f 2a 20 76 6f 69 64 2a 20 69 6e 74 20 69 /* void* int i
13670 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 nt */.../*.** CA
13680 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f PI3REF: Enable O
13690 72 20 44 69 73 61 62 6c 65 20 45 78 74 65 6e 64 r Disable Extend
136a0 65 64 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 ed Result Codes
136b0 7b 48 31 32 32 30 30 7d 20 3c 53 31 30 37 30 30 {H12200} <S10700
136c0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
136d0 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 te3_extended_res
136e0 75 6c 74 5f 63 6f 64 65 73 28 29 20 72 6f 75 74 ult_codes() rout
136f0 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 ine enables or d
13700 69 73 61 62 6c 65 73 20 74 68 65 0a 2a 2a 20 5b isables the.** [
13710 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
13720 63 6f 64 65 73 5d 20 66 65 61 74 75 72 65 20 6f codes] feature o
13730 66 20 53 51 4c 69 74 65 2e 20 54 68 65 20 65 78 f SQLite. The ex
13740 74 65 6e 64 65 64 20 72 65 73 75 6c 74 0a 2a 2a tended result.**
13750 20 63 6f 64 65 73 20 61 72 65 20 64 69 73 61 62 codes are disab
13760 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 20 66 led by default f
13770 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 63 6f or historical co
13780 6d 70 61 74 69 62 69 6c 69 74 79 20 63 6f 6e 73 mpatibility cons
13790 69 64 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a iderations..**.*
137a0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
137b0 0a 2a 2a 20 7b 48 31 32 32 30 31 7d 20 45 61 63 .** {H12201} Eac
137c0 68 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 h new [database
137d0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 68 61 6c connection] shal
137e0 6c 20 68 61 76 65 20 74 68 65 0a 2a 2a 20 20 20 l have the.**
137f0 20 20 20 20 20 20 20 5b 65 78 74 65 6e 64 65 64 [extended
13800 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 result codes] f
13810 65 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 20 eature disabled
13820 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a by default..**.*
13830 2a 20 7b 48 31 32 32 30 32 7d 20 54 68 65 20 5b * {H12202} The [
13840 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
13850 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 44 2c _result_codes(D,
13860 46 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 F)] interface sh
13870 61 6c 6c 20 65 6e 61 62 6c 65 0a 2a 2a 20 20 20 all enable.**
13880 20 20 20 20 20 20 20 5b 65 78 74 65 6e 64 65 64 [extended
13890 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 result codes] f
138a0 6f 72 20 74 68 65 20 20 5b 64 61 74 61 62 61 73 or the [databas
138b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a e connection] D.
138c0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 74 ** if t
138d0 68 65 20 46 20 70 61 72 61 6d 65 74 65 72 20 69 he F parameter i
138e0 73 20 74 72 75 65 2c 20 6f 72 20 64 69 73 61 62 s true, or disab
138f0 6c 65 20 74 68 65 6d 20 69 66 20 46 20 69 73 20 le them if F is
13900 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 false..*/.SQLITE
13910 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
13920 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 _extended_result
13930 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 2a 2c _codes(sqlite3*,
13940 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a int onoff);../*
13950 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 61 .** CAPI3REF: La
13960 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 64 20 st Insert Rowid
13970 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 30 30 {H12220} <S10700
13980 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 74 >.**.** Each ent
13990 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 ry in an SQLite
139a0 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e 69 71 table has a uniq
139b0 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 ue 64-bit signed
139c0 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 .** integer key
139d0 63 61 6c 6c 65 64 20 74 68 65 20 22 72 6f 77 69 called the "rowi
139e0 64 22 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73 d". The rowid is
139f0 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c always availabl
13a00 65 0a 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63 e.** as an undec
13a10 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d lared column nam
13a20 65 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f ed ROWID, OID, o
13a30 72 20 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e r _ROWID_ as lon
13a40 67 20 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 g as those.** na
13a50 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f mes are not also
13a60 20 75 73 65 64 20 62 79 20 65 78 70 6c 69 63 69 used by explici
13a70 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c tly declared col
13a80 75 6d 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 umns. If.** the
13a90 74 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75 table has a colu
13aa0 6d 6e 20 6f 66 20 74 79 70 65 20 49 4e 54 45 47 mn of type INTEG
13ab0 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 74 ER PRIMARY KEY t
13ac0 68 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a hen that column.
13ad0 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c ** is another al
13ae0 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 ias for the rowi
13af0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f d..**.** This ro
13b00 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 utine returns th
13b10 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6d e rowid of the m
13b20 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 ost recent.** su
13b30 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 ccessful [INSERT
13b40 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 ] into the datab
13b50 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 ase from the [da
13b60 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
13b70 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 n].** in the fir
13b80 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 st argument. If
13b90 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b no successful [
13ba0 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 INSERT]s.** have
13bb0 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f ever occurred o
13bc0 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 n that database
13bd0 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f connection, zero
13be0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
13bf0 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 .** If an [INSER
13c00 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e T] occurs within
13c10 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e a trigger, then
13c20 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 the rowid of th
13c30 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 72 6f e inserted.** ro
13c40 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 w is returned by
13c50 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 this routine as
13c60 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72 69 long as the tri
13c70 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 2e gger is running.
13c80 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 68 65 .** But once the
13c90 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 6e 61 trigger termina
13ca0 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 72 tes, the value r
13cb0 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 eturned by this
13cc0 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 65 72 routine.** rever
13cd0 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 76 ts to the last v
13ce0 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 62 65 alue inserted be
13cf0 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72 fore the trigger
13d00 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e fired..**.** An
13d10 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 20 66 [INSERT] that f
13d20 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 63 6f ails due to a co
13d30 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 nstraint violati
13d40 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 73 on is not a.** s
13d50 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 uccessful [INSER
13d60 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 T] and does not
13d70 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 change the value
13d80 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 returned by thi
13d90 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 20 54 s.** routine. T
13da0 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 46 41 hus INSERT OR FA
13db0 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 49 47 IL, INSERT OR IG
13dc0 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f 52 20 NORE, INSERT OR
13dd0 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e 64 ROLLBACK,.** and
13de0 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f 52 54 INSERT OR ABORT
13df0 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 make no changes
13e00 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20 76 to the return v
13e10 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 alue of this.**
13e20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65 routine when the
13e30 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 61 69 ir insertion fai
13e40 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 52 54 ls. When INSERT
13e50 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 65 OR REPLACE.** e
13e60 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e 73 ncounters a cons
13e70 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e traint violation
13e80 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66 61 , it does not fa
13e90 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53 45 il. The.** INSE
13ea0 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 RT continues to
13eb0 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 72 completion after
13ec0 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 74 deleting rows t
13ed0 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74 68 hat caused.** th
13ee0 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 6f e constraint pro
13ef0 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 4f blem so INSERT O
13f00 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 61 R REPLACE will a
13f10 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a 20 lways change.**
13f20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
13f30 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 of this interfa
13f40 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 ce..**.** For th
13f50 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 e purposes of th
13f60 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 20 5b is routine, an [
13f70 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e 73 69 INSERT] is consi
13f80 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 20 73 dered to.** be s
13f90 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e 20 69 uccessful even i
13fa0 66 20 69 74 20 69 73 20 73 75 62 73 65 71 75 65 f it is subseque
13fb0 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b ntly rolled back
13fc0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
13fd0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 32 TS:.**.** {H1222
13fe0 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 1} The [sqlite3_
13ff0 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 last_insert_rowi
14000 64 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 d()] function sh
14010 61 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 72 all return the r
14020 6f 77 69 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 owid.**
14030 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
14040 65 6e 74 20 73 75 63 63 65 73 73 66 75 6c 20 5b ent successful [
14050 49 4e 53 45 52 54 5d 20 70 65 72 66 6f 72 6d 65 INSERT] performe
14060 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a d on the same.**
14070 20 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 [datab
14080 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
14090 61 6e 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 and within the s
140a0 61 6d 65 20 6f 72 20 68 69 67 68 65 72 20 6c 65 ame or higher le
140b0 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 vel.**
140c0 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c trigger context,
140d0 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72 or zero if ther
140e0 65 20 68 61 76 65 20 62 65 65 6e 20 6e 6f 20 71 e have been no q
140f0 75 61 6c 69 66 79 69 6e 67 0a 2a 2a 20 20 20 20 ualifying.**
14100 20 20 20 20 20 20 5b 49 4e 53 45 52 54 5d 20 73 [INSERT] s
14110 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a tatements..**.**
14120 20 7b 48 31 32 32 32 33 7d 20 54 68 65 20 5b 73 {H12223} The [s
14130 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 qlite3_last_inse
14140 72 74 5f 72 6f 77 69 64 28 29 5d 20 66 75 6e 63 rt_rowid()] func
14150 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72 tion shall retur
14160 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 n the.**
14170 20 20 73 61 6d 65 20 76 61 6c 75 65 20 77 68 65 same value whe
14180 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 n called from th
14190 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 20 63 e same trigger c
141a0 6f 6e 74 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 ontext.**
141b0 20 20 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 immediately b
141c0 65 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 20 efore and after
141d0 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a a [ROLLBACK]..**
141e0 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a .** ASSUMPTIONS:
141f0 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 33 32 7d 20 .**.** {A12232}
14200 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 If a separate th
14210 72 65 61 64 20 70 65 72 66 6f 72 6d 73 20 61 20 read performs a
14220 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e 20 new [INSERT] on
14230 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 the same.**
14240 20 20 20 20 20 64 61 74 61 62 61 73 65 20 63 6f database co
14250 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 nnection while t
14260 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 he [sqlite3_last
14270 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d _insert_rowid()]
14280 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e .** fun
14290 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 ction is running
142a0 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65 and thus change
142b0 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 s the last inser
142c0 74 20 72 6f 77 69 64 2c 0a 2a 2a 20 20 20 20 20 t rowid,.**
142d0 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 76 61 then the va
142e0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
142f0 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e [sqlite3_last_in
14300 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 73 sert_rowid()] is
14310 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 6e 70 .** unp
14320 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6d redictable and m
14330 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 65 ight not equal e
14340 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f 72 ither the old or
14350 20 74 68 65 20 6e 65 77 0a 2a 2a 20 20 20 20 20 the new.**
14360 20 20 20 20 20 6c 61 73 74 20 69 6e 73 65 72 74 last insert
14370 20 72 6f 77 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 rowid..*/.SQLIT
14380 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e E_API sqlite3_in
14390 74 36 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 t64 sqlite3_last
143a0 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71 _insert_rowid(sq
143b0 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 lite3*);../*.**
143c0 43 41 50 49 33 52 45 46 3a 20 43 6f 75 6e 74 20 CAPI3REF: Count
143d0 54 68 65 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f The Number Of Ro
143e0 77 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 ws Modified {H12
143f0 32 34 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 240} <S10600>.**
14400 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
14410 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 n returns the nu
14420 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 mber of database
14430 20 72 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 rows that were
14440 63 68 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69 6e changed.** or in
14450 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 serted or delete
14460 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65 d by the most re
14470 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 cently completed
14480 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a SQL statement.*
14490 2a 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 * on the [databa
144a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 se connection] s
144b0 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 pecified by the
144c0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e first parameter.
144d0 0a 2a 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 73 .** Only changes
144e0 20 74 68 61 74 20 61 72 65 20 64 69 72 65 63 74 that are direct
144f0 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ly specified by
14500 74 68 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 the [INSERT], [U
14510 50 44 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44 PDATE],.** or [D
14520 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 ELETE] statement
14530 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 41 are counted. A
14540 75 78 69 6c 69 61 72 79 20 63 68 61 6e 67 65 73 uxiliary changes
14550 20 63 61 75 73 65 64 20 62 79 0a 2a 2a 20 74 72 caused by.** tr
14560 69 67 67 65 72 73 20 61 72 65 20 6e 6f 74 20 63 iggers are not c
14570 6f 75 6e 74 65 64 2e 20 55 73 65 20 74 68 65 20 ounted. Use the
14580 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 [sqlite3_total_c
14590 68 61 6e 67 65 73 28 29 5d 20 66 75 6e 63 74 69 hanges()] functi
145a0 6f 6e 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 on.** to find th
145b0 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
145c0 66 20 63 68 61 6e 67 65 73 20 69 6e 63 6c 75 64 f changes includ
145d0 69 6e 67 20 63 68 61 6e 67 65 73 20 63 61 75 73 ing changes caus
145e0 65 64 20 62 79 20 74 72 69 67 67 65 72 73 2e 0a ed by triggers..
145f0 2a 2a 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61 **.** A "row cha
14600 6e 67 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 nge" is a change
14610 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 to a single row
14620 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 of a single tab
14630 6c 65 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 le.** caused by
14640 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 an INSERT, DELET
14650 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 E, or UPDATE sta
14660 74 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 tement. Rows th
14670 61 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 at.** are change
14680 64 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74 d as side effect
14690 73 20 6f 66 20 52 45 50 4c 41 43 45 20 63 6f 6e s of REPLACE con
146a0 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69 straint resoluti
146b0 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c on,.** rollback,
146c0 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e ABORT processin
146d0 67 2c 20 44 52 4f 50 20 54 41 42 4c 45 2c 20 6f g, DROP TABLE, o
146e0 72 20 62 79 20 61 6e 79 20 6f 74 68 65 72 0a 2a r by any other.*
146f0 2a 20 6d 65 63 68 61 6e 69 73 6d 73 20 64 6f 20 * mechanisms do
14700 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 64 69 72 not count as dir
14710 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65 73 2e ect row changes.
14720 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72 69 67 67 65 .**.** A "trigge
14730 72 20 63 6f 6e 74 65 78 74 22 20 69 73 20 61 20 r context" is a
14740 73 63 6f 70 65 20 6f 66 20 65 78 65 63 75 74 69 scope of executi
14750 6f 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 on that begins a
14760 6e 64 0a 2a 2a 20 65 6e 64 73 20 77 69 74 68 20 nd.** ends with
14770 74 68 65 20 73 63 72 69 70 74 20 6f 66 20 61 20 the script of a
14780 74 72 69 67 67 65 72 2e 20 20 4d 6f 73 74 20 53 trigger. Most S
14790 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 QL statements ar
147a0 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6f e.** evaluated o
147b0 75 74 73 69 64 65 20 6f 66 20 61 6e 79 20 74 72 utside of any tr
147c0 69 67 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 igger. This is
147d0 74 68 65 20 22 74 6f 70 20 6c 65 76 65 6c 22 0a the "top level".
147e0 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 ** trigger conte
147f0 78 74 2e 20 20 49 66 20 61 20 74 72 69 67 67 65 xt. If a trigge
14800 72 20 66 69 72 65 73 20 66 72 6f 6d 20 74 68 65 r fires from the
14810 20 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a 2a 2a top level, a.**
14820 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e new trigger con
14830 74 65 78 74 20 69 73 20 65 6e 74 65 72 65 64 20 text is entered
14840 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e for the duration
14850 20 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a 2a 20 of that one.**
14860 74 72 69 67 67 65 72 2e 20 20 53 75 62 74 72 69 trigger. Subtri
14870 67 67 65 72 73 20 63 72 65 61 74 65 20 73 75 62 ggers create sub
14880 63 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 68 65 contexts for the
14890 69 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a ir duration..**.
148a0 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 ** Calling [sqli
148b0 74 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20 5b te3_exec()] or [
148c0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
148d0 72 65 63 75 72 73 69 76 65 6c 79 20 64 6f 65 73 recursively does
148e0 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 61 .** not create a
148f0 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e new trigger con
14900 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 text..**.** This
14910 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
14920 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
14930 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 direct row chang
14940 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 es in the.** mos
14950 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c t recent INSERT,
14960 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 UPDATE, or DELE
14970 54 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 TE statement wit
14980 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 hin the same.**
14990 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e trigger context.
149a0 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 68 65 .**.** Thus, whe
149b0 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 n called from th
149c0 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69 e top level, thi
149d0 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
149e0 6e 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 ns the.** number
149f0 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 of changes in t
14a00 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 he most recent I
14a10 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f NSERT, UPDATE, o
14a20 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 r DELETE.** that
14a30 20 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61 also occurred a
14a40 74 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e t the top level.
14a50 20 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f 64 Within the bod
14a60 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c 0a y of a trigger,.
14a70 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 ** the sqlite3_c
14a80 68 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61 hanges() interfa
14a90 63 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 ce can be called
14aa0 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d to find the num
14ab0 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 ber of.** change
14ac0 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 s in the most re
14ad0 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 cently completed
14ae0 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
14af0 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 or DELETE.** st
14b00 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 atement within t
14b10 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 73 he body of the s
14b20 61 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a 20 ame trigger..**
14b30 48 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 6d However, the num
14b40 62 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f 65 ber returned doe
14b50 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 s not include ch
14b60 61 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 20 anges.** caused
14b70 62 79 20 73 75 62 74 72 69 67 67 65 72 73 20 73 by subtriggers s
14b80 69 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 20 ince those have
14b90 74 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 78 their own contex
14ba0 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 t..**.** SQLite
14bb0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 63 implements the c
14bc0 6f 6d 6d 61 6e 64 20 22 44 45 4c 45 54 45 20 46 ommand "DELETE F
14bd0 52 4f 4d 20 74 61 62 6c 65 22 20 77 69 74 68 6f ROM table" witho
14be0 75 74 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 ut a WHERE claus
14bf0 65 0a 2a 2a 20 62 79 20 64 72 6f 70 70 69 6e 67 e.** by dropping
14c00 20 61 6e 64 20 72 65 63 72 65 61 74 69 6e 67 20 and recreating
14c10 74 68 65 20 74 61 62 6c 65 2e 20 20 44 6f 69 6e the table. Doin
14c20 67 20 73 6f 20 69 73 20 6d 75 63 68 20 66 61 73 g so is much fas
14c30 74 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 0a 2a ter than going.*
14c40 2a 20 74 68 72 6f 75 67 68 20 61 6e 64 20 64 65 * through and de
14c50 6c 65 74 69 6e 67 20 69 6e 64 69 76 69 64 75 61 leting individua
14c60 6c 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 l elements from
14c70 74 68 65 20 74 61 62 6c 65 2e 20 20 42 65 63 61 the table. Beca
14c80 75 73 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f use of this.** o
14c90 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74 68 65 ptimization, the
14ca0 20 64 65 6c 65 74 69 6f 6e 73 20 69 6e 20 22 44 deletions in "D
14cb0 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 ELETE FROM table
14cc0 22 20 61 72 65 20 6e 6f 74 20 72 6f 77 20 63 68 " are not row ch
14cd0 61 6e 67 65 73 20 61 6e 64 0a 2a 2a 20 77 69 6c anges and.** wil
14ce0 6c 20 6e 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 l not be counted
14cf0 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f by the sqlite3_
14d00 63 68 61 6e 67 65 73 28 29 20 6f 72 20 5b 73 71 changes() or [sq
14d10 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e lite3_total_chan
14d20 67 65 73 28 29 5d 0a 2a 2a 20 66 75 6e 63 74 69 ges()].** functi
14d30 6f 6e 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 ons, regardless
14d40 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 of the number of
14d50 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 elements that w
14d60 65 72 65 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a ere originally.*
14d70 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 * in the table.
14d80 20 54 6f 20 67 65 74 20 61 6e 20 61 63 63 75 72 To get an accur
14d90 61 74 65 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 ate count of the
14da0 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 number of rows
14db0 64 65 6c 65 74 65 64 2c 20 75 73 65 0a 2a 2a 20 deleted, use.**
14dc0 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 "DELETE FROM tab
14dd0 6c 65 20 57 48 45 52 45 20 31 22 20 69 6e 73 74 le WHERE 1" inst
14de0 65 61 64 2e 20 20 4f 72 20 72 65 63 6f 6d 70 69 ead. Or recompi
14df0 6c 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 le using the.**
14e00 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 [SQLITE_OMIT_TRU
14e10 4e 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 NCATE_OPTIMIZATI
14e20 4f 4e 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 ON] compile-time
14e30 20 6f 70 74 69 6f 6e 20 74 6f 20 64 69 73 61 62 option to disab
14e40 6c 65 20 74 68 65 0a 2a 2a 20 6f 70 74 69 6d 69 le the.** optimi
14e50 7a 61 74 69 6f 6e 20 6f 6e 20 61 6c 6c 20 71 75 zation on all qu
14e60 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 eries..**.** INV
14e70 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
14e80 48 31 32 32 34 31 7d 20 54 68 65 20 5b 73 71 6c H12241} The [sql
14e90 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 ite3_changes()]
14ea0 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72 function shall r
14eb0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
14ec0 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 of.**
14ed0 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75 73 row changes caus
14ee0 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72 ed by the most r
14ef0 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 ecent INSERT, UP
14f00 44 41 54 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 DATE,.**
14f10 20 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 or DELETE stat
14f20 65 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 61 6d ement on the sam
14f30 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
14f40 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20 20 ction and.**
14f50 20 20 20 20 20 20 77 69 74 68 69 6e 20 74 68 65 within the
14f60 20 73 61 6d 65 20 6f 72 20 68 69 67 68 65 72 20 same or higher
14f70 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c trigger context,
14f80 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72 or zero if ther
14f90 65 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 e have.**
14fa0 20 20 20 6e 6f 74 20 62 65 65 6e 20 61 6e 79 20 not been any
14fb0 71 75 61 6c 69 66 79 69 6e 67 20 72 6f 77 20 63 qualifying row c
14fc0 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 hanges..**.** {H
14fd0 31 32 32 34 33 7d 20 53 74 61 74 65 6d 65 6e 74 12243} Statement
14fe0 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 44 s of the form "D
14ff0 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 ELETE FROM table
15000 6e 61 6d 65 22 20 77 69 74 68 20 6e 6f 0a 2a 2a name" with no.**
15010 20 20 20 20 20 20 20 20 20 20 57 48 45 52 45 20 WHERE
15020 63 6c 61 75 73 65 20 73 68 61 6c 6c 20 63 61 75 clause shall cau
15030 73 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 se subsequent ca
15040 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 lls to.**
15050 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e [sqlite3_chan
15060 67 65 73 28 29 5d 20 74 6f 20 72 65 74 75 72 6e ges()] to return
15070 20 7a 65 72 6f 2c 20 72 65 67 61 72 64 6c 65 73 zero, regardles
15080 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 s of the.**
15090 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 72 number of r
150a0 6f 77 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69 ows originally i
150b0 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a n the table..**.
150c0 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a ** ASSUMPTIONS:.
150d0 2a 2a 0a 2a 2a 20 7b 41 31 32 32 35 32 7d 20 49 **.** {A12252} I
150e0 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 f a separate thr
150f0 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 ead makes change
15100 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 s on the same da
15110 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
15120 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 n.** wh
15130 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 ile [sqlite3_cha
15140 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 nges()] is runni
15150 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 ng then the valu
15160 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 20 20 e returned.**
15170 20 20 20 20 20 20 20 69 73 20 75 6e 70 72 65 64 is unpred
15180 69 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 ictable and not
15190 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 meaningful..*/.S
151a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
151b0 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 lite3_changes(sq
151c0 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 lite3*);../*.**
151d0 43 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 CAPI3REF: Total
151e0 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d Number Of Rows M
151f0 6f 64 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d odified {H12260}
15200 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10600>.**.**
15210 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
15220 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
15230 20 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20 of row changes
15240 63 61 75 73 65 64 20 62 79 20 49 4e 53 45 52 54 caused by INSERT
15250 2c 0a 2a 2a 20 55 50 44 41 54 45 20 6f 72 20 44 ,.** UPDATE or D
15260 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 ELETE statements
15270 20 73 69 6e 63 65 20 74 68 65 20 5b 64 61 74 61 since the [data
15280 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
15290 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 was opened..**
152a0 54 68 65 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 The count includ
152b0 65 73 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 es all changes f
152c0 72 6f 6d 20 61 6c 6c 20 74 72 69 67 67 65 72 20 rom all trigger
152d0 63 6f 6e 74 65 78 74 73 2e 20 20 48 6f 77 65 76 contexts. Howev
152e0 65 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 er,.** the count
152f0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 does not includ
15300 65 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 e changes used t
15310 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 52 45 50 4c o implement REPL
15320 41 43 45 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c ACE constraints,
15330 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 .** do rollbacks
15340 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 or ABORT proces
15350 73 69 6e 67 2c 20 6f 72 20 44 52 4f 50 20 74 61 sing, or DROP ta
15360 62 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a ble processing..
15370 2a 2a 20 54 68 65 20 63 68 61 6e 67 65 73 20 61 ** The changes a
15380 72 65 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f re counted as so
15390 6f 6e 20 61 73 20 74 68 65 20 73 74 61 74 65 6d on as the statem
153a0 65 6e 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74 ent that makes t
153b0 68 65 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 hem is.** comple
153c0 74 65 64 20 28 77 68 65 6e 20 74 68 65 20 73 74 ted (when the st
153d0 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 atement handle i
153e0 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c s passed to [sql
153f0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 ite3_reset()] or
15400 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e .** [sqlite3_fin
15410 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a alize()])..**.**
15420 20 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e SQLite implemen
15430 74 73 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22 ts the command "
15440 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c DELETE FROM tabl
15450 65 22 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 e" without a WHE
15460 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79 20 RE clause.** by
15470 64 72 6f 70 70 69 6e 67 20 61 6e 64 20 72 65 63 dropping and rec
15480 72 65 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c reating the tabl
15490 65 2e 20 20 28 54 68 69 73 20 69 73 20 6d 75 63 e. (This is muc
154a0 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f h faster than go
154b0 69 6e 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61 ing.** through a
154c0 6e 64 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69 nd deleting indi
154d0 76 69 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20 vidual elements
154e0 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 2e 29 from the table.)
154f0 20 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 69 Because of thi
15500 73 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f s.** optimizatio
15510 6e 2c 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 73 n, the deletions
15520 20 69 6e 20 22 44 45 4c 45 54 45 20 46 52 4f 4d in "DELETE FROM
15530 20 74 61 62 6c 65 22 20 61 72 65 20 6e 6f 74 20 table" are not
15540 72 6f 77 20 63 68 61 6e 67 65 73 20 61 6e 64 0a row changes and.
15550 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 ** will not be c
15560 6f 75 6e 74 65 64 20 62 79 20 74 68 65 20 73 71 ounted by the sq
15570 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 lite3_changes()
15580 6f 72 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 or [sqlite3_tota
15590 6c 5f 63 68 61 6e 67 65 73 28 29 5d 0a 2a 2a 20 l_changes()].**
155a0 66 75 6e 63 74 69 6f 6e 73 2c 20 72 65 67 61 72 functions, regar
155b0 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d dless of the num
155c0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
155d0 74 68 61 74 20 77 65 72 65 20 6f 72 69 67 69 6e that were origin
155e0 61 6c 6c 79 0a 2a 2a 20 69 6e 20 74 68 65 20 74 ally.** in the t
155f0 61 62 6c 65 2e 20 20 54 6f 20 67 65 74 20 61 6e able. To get an
15600 20 61 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20 accurate count
15610 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 of the number of
15620 20 72 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75 rows deleted, u
15630 73 65 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52 se.** "DELETE FR
15640 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 31 OM table WHERE 1
15650 22 20 69 6e 73 74 65 61 64 2e 20 20 20 4f 72 20 " instead. Or
15660 72 65 63 6f 6d 70 69 6c 65 20 75 73 69 6e 67 20 recompile using
15670 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f the.** [SQLITE_O
15680 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 MIT_TRUNCATE_OPT
15690 49 4d 49 5a 41 54 49 4f 4e 5d 20 63 6f 6d 70 69 IMIZATION] compi
156a0 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74 le-time option t
156b0 6f 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a o disable the.**
156c0 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e optimization on
156d0 20 61 6c 6c 20 71 75 65 72 69 65 73 2e 0a 2a 2a all queries..**
156e0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 .** See also the
156f0 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 [sqlite3_change
15700 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a s()] interface..
15710 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
15720 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 36 31 7d :.**.** {H12261}
15730 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f The [sqlite3_to
15740 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 72 tal_changes()] r
15750 65 74 75 72 6e 73 20 74 68 65 20 74 6f 74 61 6c eturns the total
15760 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 number.**
15770 20 20 20 20 6f 66 20 72 6f 77 20 63 68 61 6e 67 of row chang
15780 65 73 20 63 61 75 73 65 64 20 62 79 20 49 4e 53 es caused by INS
15790 45 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64 ERT, UPDATE, and
157a0 2f 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 20 20 /or DELETE.**
157b0 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 statement
157c0 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 s on the same [d
157d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
157e0 6f 6e 5d 2c 20 69 6e 20 61 6e 79 0a 2a 2a 20 20 on], in any.**
157f0 20 20 20 20 20 20 20 20 74 72 69 67 67 65 72 20 trigger
15800 63 6f 6e 74 65 78 74 2c 20 73 69 6e 63 65 20 74 context, since t
15810 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
15820 65 63 74 69 6f 6e 20 77 61 73 20 63 72 65 61 74 ection was creat
15830 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 36 ed..**.** {H1226
15840 33 7d 20 53 74 61 74 65 6d 65 6e 74 73 20 6f 66 3} Statements of
15850 20 74 68 65 20 66 6f 72 6d 20 22 44 45 4c 45 54 the form "DELET
15860 45 20 46 52 4f 4d 20 74 61 62 6c 65 6e 61 6d 65 E FROM tablename
15870 22 20 77 69 74 68 20 6e 6f 0a 2a 2a 20 20 20 20 " with no.**
15880 20 20 20 20 20 20 57 48 45 52 45 20 63 6c 61 75 WHERE clau
15890 73 65 20 73 68 61 6c 6c 20 6e 6f 74 20 63 68 61 se shall not cha
158a0 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 72 65 nge the value re
158b0 74 75 72 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 turned.**
158c0 20 20 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 by [sqlite3_t
158d0 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 2e otal_changes()].
158e0 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f .**.** ASSUMPTIO
158f0 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 36 NS:.**.** {A1226
15900 34 7d 20 49 66 20 61 20 73 65 70 61 72 61 74 65 4} If a separate
15910 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 thread makes ch
15920 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d anges on the sam
15930 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
15940 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ction.**
15950 20 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 while [sqlite3
15960 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 _total_changes()
15970 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 ] is running the
15980 6e 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 20 n the value.**
15990 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 65 64 returned
159a0 20 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c is unpredictabl
159b0 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e e and not meanin
159c0 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f gful..*/.SQLITE_
159d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
159e0 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 total_changes(sq
159f0 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 lite3*);../*.**
15a00 43 41 50 49 33 52 45 46 3a 20 49 6e 74 65 72 72 CAPI3REF: Interr
15a10 75 70 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 upt A Long-Runni
15a20 6e 67 20 51 75 65 72 79 20 7b 48 31 32 32 37 30 ng Query {H12270
15a30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a } <S30500>.**.**
15a40 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 This function c
15a50 61 75 73 65 73 20 61 6e 79 20 70 65 6e 64 69 6e auses any pendin
15a60 67 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 g database opera
15a70 74 69 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e tion to abort an
15a80 64 0a 2a 2a 20 72 65 74 75 72 6e 20 61 74 20 69 d.** return at i
15a90 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f ts earliest oppo
15aa0 72 74 75 6e 69 74 79 2e 20 54 68 69 73 20 72 6f rtunity. This ro
15ab0 75 74 69 6e 65 20 69 73 20 74 79 70 69 63 61 6c utine is typical
15ac0 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 ly.** called in
15ad0 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 response to a us
15ae0 65 72 20 61 63 74 69 6f 6e 20 73 75 63 68 20 61 er action such a
15af0 73 20 70 72 65 73 73 69 6e 67 20 22 43 61 6e 63 s pressing "Canc
15b00 65 6c 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 el".** or Ctrl-C
15b10 20 77 68 65 72 65 20 74 68 65 20 75 73 65 72 20 where the user
15b20 77 61 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 wants a long que
15b30 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 ry operation to
15b40 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 halt.** immediat
15b50 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 ely..**.** It is
15b60 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 safe to call th
15b70 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 is routine from
15b80 61 20 74 68 72 65 61 64 20 64 69 66 66 65 72 65 a thread differe
15b90 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 nt from the.** t
15ba0 68 72 65 61 64 20 74 68 61 74 20 69 73 20 63 75 hread that is cu
15bb0 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20 rrently running
15bc0 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 the database ope
15bd0 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 74 0a ration. But it.
15be0 2a 2a 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 ** is not safe t
15bf0 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 o call this rout
15c00 69 6e 65 20 77 69 74 68 20 61 20 5b 64 61 74 61 ine with a [data
15c10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
15c20 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 that.** is clos
15c30 65 64 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73 ed or might clos
15c40 65 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 e before sqlite3
15c50 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 65 74 _interrupt() ret
15c60 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 urns..**.** If a
15c70 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 n SQL operation
15c80 69 73 20 76 65 72 79 20 6e 65 61 72 6c 79 20 66 is very nearly f
15c90 69 6e 69 73 68 65 64 20 61 74 20 74 68 65 20 74 inished at the t
15ca0 69 6d 65 20 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 ime when.** sqli
15cb0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 te3_interrupt()
15cc0 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 is called, then
15cd0 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 it might not hav
15ce0 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 e an opportunity
15cf0 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 .** to be interr
15d00 75 70 74 65 64 20 61 6e 64 20 6d 69 67 68 74 20 upted and might
15d10 63 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70 continue to comp
15d20 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e letion..**.** An
15d30 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 SQL operation t
15d40 68 61 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 hat is interrupt
15d50 65 64 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b ed will return [
15d60 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
15d70 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 ]..** If the int
15d80 65 72 72 75 70 74 65 64 20 53 51 4c 20 6f 70 65 errupted SQL ope
15d90 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 ration is an INS
15da0 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 ERT, UPDATE, or
15db0 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 69 DELETE.** that i
15dc0 73 20 69 6e 73 69 64 65 20 61 6e 20 65 78 70 6c s inside an expl
15dd0 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e icit transaction
15de0 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69 72 , then the entir
15df0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a e transaction.**
15e00 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 will be rolled
15e10 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c back automatical
15e20 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c ly..**.** A call
15e30 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 to sqlite3_inte
15e40 72 72 75 70 74 28 29 20 68 61 73 20 6e 6f 20 65 rrupt() has no e
15e50 66 66 65 63 74 20 6f 6e 20 53 51 4c 20 73 74 61 ffect on SQL sta
15e60 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 tements.** that
15e70 61 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 are started afte
15e80 72 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 r sqlite3_interr
15e90 75 70 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a upt() returns..*
15ea0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
15eb0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 37 31 7d 20 .**.** {H12271}
15ec0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 The [sqlite3_int
15ed0 65 72 72 75 70 74 28 29 5d 20 69 6e 74 65 72 66 errupt()] interf
15ee0 61 63 65 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 ace will force a
15ef0 6c 6c 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 20 20 ll running.**
15f00 20 20 20 20 20 20 20 53 51 4c 20 73 74 61 74 65 SQL state
15f10 6d 65 6e 74 73 20 61 73 73 6f 63 69 61 74 65 64 ments associated
15f20 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 64 with the same d
15f30 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
15f40 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 on.** t
15f50 6f 20 68 61 6c 74 20 61 66 74 65 72 20 70 72 6f o halt after pro
15f60 63 65 73 73 69 6e 67 20 61 74 20 6d 6f 73 74 20 cessing at most
15f70 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 one additional r
15f80 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a ow of data..**.*
15f90 2a 20 7b 48 31 32 32 37 32 7d 20 41 6e 79 20 53 * {H12272} Any S
15fa0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 QL statement tha
15fb0 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 t is interrupted
15fc0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 by [sqlite3_int
15fd0 65 72 72 75 70 74 28 29 5d 0a 2a 2a 20 20 20 20 errupt()].**
15fe0 20 20 20 20 20 20 77 69 6c 6c 20 72 65 74 75 72 will retur
15ff0 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 n [SQLITE_INTERR
16000 55 50 54 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 UPT]..**.** ASSU
16010 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b MPTIONS:.**.** {
16020 41 31 32 32 37 39 7d 20 49 66 20 74 68 65 20 64 A12279} If the d
16030 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
16040 6f 6e 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 on closes while
16050 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 [sqlite3_interru
16060 70 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 pt()].**
16070 20 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 is running the
16080 6e 20 62 61 64 20 74 68 69 6e 67 73 20 77 69 6c n bad things wil
16090 6c 20 6c 69 6b 65 6c 79 20 68 61 70 70 65 6e 2e l likely happen.
160a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
160b0 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 oid sqlite3_inte
160c0 72 72 75 70 74 28 73 71 6c 69 74 65 33 2a 29 3b rrupt(sqlite3*);
160d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
160e0 3a 20 44 65 74 65 72 6d 69 6e 65 20 49 66 20 41 : Determine If A
160f0 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 n SQL Statement
16100 49 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48 31 30 Is Complete {H10
16110 35 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a 2a 2a 510} <S70200>.**
16120 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
16130 65 73 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f es are useful fo
16140 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 r command-line i
16150 6e 70 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e nput to determin
16160 65 20 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 e if the.** curr
16170 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 ently entered te
16180 78 74 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d xt seems to form
16190 20 63 6f 6d 70 6c 65 74 65 20 61 20 53 51 4c 20 complete a SQL
161a0 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 statement or.**
161b0 69 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e if additional in
161c0 70 75 74 20 69 73 20 6e 65 65 64 65 64 20 62 65 put is needed be
161d0 66 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65 fore sending the
161e0 20 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 text into.** SQ
161f0 4c 69 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67 Lite for parsing
16200 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
16210 73 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 s return true if
16220 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e the input strin
16230 67 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 6f 20 g.** appears to
16240 62 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 be a complete SQ
16250 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 L statement. A
16260 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6a 75 64 statement is jud
16270 67 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d ged to be.** com
16280 70 6c 65 74 65 20 69 66 20 69 74 20 65 6e 64 73 plete if it ends
16290 20 77 69 74 68 20 61 20 73 65 6d 69 63 6f 6c 6f with a semicolo
162a0 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 20 6e n token and is n
162b0 6f 74 20 61 20 66 72 61 67 6d 65 6e 74 20 6f 66 ot a fragment of
162c0 20 61 0a 2a 2a 20 43 52 45 41 54 45 20 54 52 49 a.** CREATE TRI
162d0 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 GGER statement.
162e0 20 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 Semicolons that
162f0 20 61 72 65 20 65 6d 62 65 64 64 65 64 20 77 69 are embedded wi
16300 74 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c thin.** string l
16310 69 74 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 iterals or quote
16320 64 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d d identifier nam
16330 65 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 es or comments a
16340 72 65 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 re not.** indepe
16350 6e 64 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 ndent tokens (th
16360 65 79 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 ey are part of t
16370 68 65 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 he token in whic
16380 68 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d h they are.** em
16390 62 65 64 64 65 64 29 20 61 6e 64 20 74 68 75 73 bedded) and thus
163a0 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 do not count as
163b0 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 a statement ter
163c0 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 minator..**.** T
163d0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f hese routines do
163e0 20 6e 6f 74 20 70 61 72 73 65 20 74 68 65 20 53 not parse the S
163f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 QL statements th
16400 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 us.** will not d
16410 65 74 65 63 74 20 73 79 6e 74 61 63 74 69 63 61 etect syntactica
16420 6c 6c 79 20 69 6e 63 6f 72 72 65 63 74 20 53 51 lly incorrect SQ
16430 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 L..**.** INVARIA
16440 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 35 NTS:.**.** {H105
16450 31 31 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 11} A successful
16460 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b evaluation of [
16470 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 sqlite3_complete
16480 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 ()] or.**
16490 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 [sqlite3_comp
164a0 6c 65 74 65 31 36 28 29 5d 20 66 75 6e 63 74 69 lete16()] functi
164b0 6f 6e 73 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 ons shall.**
164c0 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 20 6e return a n
164d0 75 6d 65 72 69 63 20 31 20 69 66 20 61 6e 64 20 umeric 1 if and
164e0 6f 6e 6c 79 20 69 66 20 74 68 65 20 6c 61 73 74 only if the last
164f0 20 6e 6f 6e 2d 77 68 69 74 65 73 70 61 63 65 0a non-whitespace.
16500 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 ** toke
16510 6e 20 69 6e 20 74 68 65 69 72 20 69 6e 70 75 74 n in their input
16520 20 69 73 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 is a semicolon
16530 74 68 61 74 20 69 73 20 6e 6f 74 20 69 6e 20 62 that is not in b
16540 65 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 20 20 etween.**
16550 20 20 20 74 68 65 20 42 45 47 49 4e 20 61 6e 64 the BEGIN and
16560 20 45 4e 44 20 6f 66 20 61 20 43 52 45 41 54 45 END of a CREATE
16570 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 TRIGGER stateme
16580 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 35 31 nt..**.** {H1051
16590 32 7d 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 2} If a memory a
165a0 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 llocation error
165b0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 6e occurs during an
165c0 20 69 6e 76 6f 63 61 74 69 6f 6e 0a 2a 2a 20 20 invocation.**
165d0 20 20 20 20 20 20 20 20 6f 66 20 5b 73 71 6c 69 of [sqli
165e0 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 5d 20 te3_complete()]
165f0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 or [sqlite3_comp
16600 6c 65 74 65 31 36 28 29 5d 20 74 68 65 6e 20 74 lete16()] then t
16610 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 he.** r
16620 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74 outine shall ret
16630 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 urn [SQLITE_NOME
16640 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 M]..**.** ASSUMP
16650 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 TIONS:.**.** {A1
16660 30 35 31 32 7d 20 54 68 65 20 69 6e 70 75 74 20 0512} The input
16670 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 to [sqlite3_comp
16680 6c 65 74 65 28 29 5d 20 6d 75 73 74 20 62 65 20 lete()] must be
16690 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 a zero-terminate
166a0 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 55 54 d.** UT
166b0 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a F-8 string..**.*
166c0 2a 20 7b 41 31 30 35 31 33 7d 20 54 68 65 20 69 * {A10513} The i
166d0 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 nput to [sqlite3
166e0 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 6d _complete16()] m
166f0 75 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 ust be a zero-te
16700 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 rminated.**
16710 20 20 20 20 20 55 54 46 2d 31 36 20 73 74 72 69 UTF-16 stri
16720 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 ng in native byt
16730 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49 e order..*/.SQLI
16740 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
16750 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 e3_complete(cons
16760 74 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51 t char *sql);.SQ
16770 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
16780 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 ite3_complete16(
16790 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 29 const void *sql)
167a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
167b0 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 43 61 F: Register A Ca
167c0 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65 llback To Handle
167d0 20 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72 72 SQLITE_BUSY Err
167e0 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53 34 ors {H12310} <S4
167f0 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0400>.**.** This
16800 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 routine sets a
16810 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
16820 6e 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 n that might be
16830 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 invoked whenever
16840 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20 69 .** an attempt i
16850 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 s made to open a
16860 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 database table
16870 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 that another thr
16880 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73 ead.** or proces
16890 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a s has locked..**
168a0 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20 .** If the busy
168b0 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c callback is NULL
168c0 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42 , then [SQLITE_B
168d0 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f USY] or [SQLITE_
168e0 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a IOERR_BLOCKED].*
168f0 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d * is returned im
16900 6d 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20 65 mediately upon e
16910 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65 20 ncountering the
16920 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75 73 lock. If the bus
16930 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 y callback.** is
16940 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
16950 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c the callback wil
16960 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 l be invoked wit
16970 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e h two arguments.
16980 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
16990 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
169a0 20 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f handler is a co
169b0 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 py of the void*
169c0 70 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a pointer which.**
169d0 20 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72 is the third ar
169e0 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 gument to sqlite
169f0 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 3_busy_handler()
16a00 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 . The second ar
16a10 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 gument to.** the
16a20 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 handler callbac
16a30 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 k is the number
16a40 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 of times that th
16a50 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 68 e busy handler h
16a60 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b as.** been invok
16a70 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b ed for this lock
16a80 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20 74 ing event. If t
16a90 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 he.** busy callb
16aa0 61 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20 74 ack returns 0, t
16ab0 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61 hen no additiona
16ac0 6c 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 6d l attempts are m
16ad0 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73 ade to.** access
16ae0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e the database an
16af0 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 d [SQLITE_BUSY]
16b00 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 or [SQLITE_IOERR
16b10 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 _BLOCKED] is ret
16b20 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 urned..** If the
16b30 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
16b40 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
16b50 20 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70 74 another attempt
16b60 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 6f .** is made to o
16b70 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 pen the database
16b80 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 for reading and
16b90 20 74 68 65 20 63 79 63 6c 65 20 72 65 70 65 61 the cycle repea
16ba0 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 ts..**.** The pr
16bb0 65 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73 79 esence of a busy
16bc0 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f handler does no
16bd0 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 t guarantee that
16be0 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f it will be invo
16bf0 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72 ked.** when ther
16c00 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e e is lock conten
16c10 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65 20 tion. If SQLite
16c20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74 20 determines that
16c30 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 invoking the bus
16c40 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75 y.** handler cou
16c50 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20 64 ld result in a d
16c60 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c eadlock, it will
16c70 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65 go ahead and re
16c80 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 turn [SQLITE_BUS
16c90 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 Y].** or [SQLITE
16ca0 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 _IOERR_BLOCKED]
16cb0 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b instead of invok
16cc0 69 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e ing the busy han
16cd0 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 dler..** Conside
16ce0 72 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68 65 r a scenario whe
16cf0 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 69 re one process i
16d00 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64 s holding a read
16d10 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74 lock that.** it
16d20 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70 72 is trying to pr
16d30 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65 72 omote to a reser
16d40 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20 ved lock and.**
16d50 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 a second process
16d60 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 is holding a re
16d70 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 74 served lock that
16d80 20 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a it is trying.**
16d90 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 to promote to a
16da0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
16db0 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 72 6f . The first pro
16dc0 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63 cess cannot proc
16dd0 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20 69 eed.** because i
16de0 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 t is blocked by
16df0 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 the second and t
16e00 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 he second proces
16e10 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63 s cannot.** proc
16e20 65 65 64 20 62 65 63 61 75 73 65 20 69 74 20 69 eed because it i
16e30 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 s blocked by the
16e40 20 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74 68 first. If both
16e50 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e processes.** in
16e60 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 voke the busy ha
16e70 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72 20 ndlers, neither
16e80 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72 will make any pr
16e90 6f 67 72 65 73 73 2e 20 20 54 68 65 72 65 66 6f ogress. Therefo
16ea0 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65 re,.** SQLite re
16eb0 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55 turns [SQLITE_BU
16ec0 53 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 SY] for the firs
16ed0 74 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e t process, hopin
16ee0 67 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 77 g that this.** w
16ef0 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20 66 ill induce the f
16f00 69 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f 20 irst process to
16f10 72 65 6c 65 61 73 65 20 69 74 73 20 72 65 61 64 release its read
16f20 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a lock and allow.
16f30 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 ** the second pr
16f40 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65 64 ocess to proceed
16f50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 ..**.** The defa
16f60 75 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61 63 ult busy callbac
16f70 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a k is NULL..**.**
16f80 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55 53 The [SQLITE_BUS
16f90 59 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e 76 Y] error is conv
16fa0 65 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54 45 erted to [SQLITE
16fb0 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a _IOERR_BLOCKED].
16fc0 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 ** when SQLite i
16fd0 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 s in the middle
16fe0 6f 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e 73 of a large trans
16ff0 61 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c action where all
17000 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20 the.** changes
17010 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 will not fit int
17020 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 o the in-memory
17030 63 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20 77 cache. SQLite w
17040 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 ill.** already h
17050 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c old a RESERVED l
17060 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
17070 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69 74 ase file, but it
17080 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f needs.** to pro
17090 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 74 mote this lock t
170a0 6f 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 74 o EXCLUSIVE so t
170b0 68 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c 6c hat it can spill
170c0 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73 20 cache.** pages
170d0 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
170e0 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 68 e file without h
170f0 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e arm to concurren
17100 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20 49 t.** readers. I
17110 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74 f it is unable t
17120 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f o promote the lo
17130 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 2d ck, then the in-
17140 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20 memory.** cache
17150 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e 20 will be left in
17160 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 an inconsistent
17170 73 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68 65 state and so the
17180 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 error.** code i
17190 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20 s promoted from
171a0 74 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20 62 the relatively b
171b0 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55 enign [SQLITE_BU
171c0 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f SY] to.** the mo
171d0 72 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49 54 re severe [SQLIT
171e0 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d E_IOERR_BLOCKED]
171f0 2e 20 20 54 68 69 73 20 65 72 72 6f 72 20 63 6f . This error co
17200 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 de promotion.**
17210 66 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d 61 forces an automa
17220 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 tic rollback of
17230 74 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53 65 the changes. Se
17240 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 e the.** <a href
17250 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 3f ="/cvstrac/wiki?
17260 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c p=CorruptionFoll
17270 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22 3e owingBusyError">
17280 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f .** CorruptionFo
17290 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 llowingBusyError
172a0 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20 66 </a> wiki page f
172b0 6f 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e 20 or a discussion
172c0 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 69 of why.** this i
172d0 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a s important..**.
172e0 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c ** There can onl
172f0 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 y be a single bu
17300 73 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e sy handler defin
17310 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b ed for each.** [
17320 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
17330 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 ion]. Setting a
17340 20 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 new busy handle
17350 72 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 r clears any.**
17360 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68 previously set h
17370 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68 andler. Note th
17380 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 at calling [sqli
17390 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 te3_busy_timeout
173a0 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f ()].** will also
173b0 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68 set or clear th
173c0 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a e busy handler..
173d0 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 63 **.** The busy c
173e0 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e allback should n
173f0 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 69 ot take any acti
17400 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 79 ons which modify
17410 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
17420 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 connection that
17430 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 invoked the bus
17440 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20 y handler. Any
17450 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 such actions.**
17460 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 result in undefi
17470 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a ned behavior..**
17480 20 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a .** INVARIANTS:
17490 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 31 7d 20 .**.** {H12311}
174a0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 The [sqlite3_bus
174b0 79 5f 68 61 6e 64 6c 65 72 28 44 2c 43 2c 41 29 y_handler(D,C,A)
174c0 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c ] function shall
174d0 20 72 65 70 6c 61 63 65 0a 2a 2a 20 20 20 20 20 replace.**
174e0 20 20 20 20 20 62 75 73 79 20 63 61 6c 6c 62 61 busy callba
174f0 63 6b 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 ck in the [datab
17500 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
17510 44 20 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20 D with a new.**
17520 20 20 20 20 20 20 20 20 20 61 20 6e 65 77 20 62 a new b
17530 75 73 79 20 68 61 6e 64 6c 65 72 20 43 20 61 6e usy handler C an
17540 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 61 d application da
17550 74 61 20 70 6f 69 6e 74 65 72 20 41 2e 0a 2a 2a ta pointer A..**
17560 0a 2a 2a 20 7b 48 31 32 33 31 32 7d 20 4e 65 77 .** {H12312} New
17570 6c 79 20 63 72 65 61 74 65 64 20 5b 64 61 74 61 ly created [data
17580 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
17590 5d 20 73 68 61 6c 6c 20 68 61 76 65 20 61 20 62 ] shall have a b
175a0 75 73 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 usy.**
175b0 68 61 6e 64 6c 65 72 20 6f 66 20 4e 55 4c 4c 2e handler of NULL.
175c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 34 7d 20 .**.** {H12314}
175d0 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 When two or more
175e0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
175f0 63 74 69 6f 6e 73 5d 20 73 68 61 72 65 20 61 0a ctions] share a.
17600 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
17610 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 ite3_enable_shar
17620 65 64 5f 63 61 63 68 65 20 7c 20 63 6f 6d 6d 6f ed_cache | commo
17630 6e 20 63 61 63 68 65 5d 2c 0a 2a 2a 20 20 20 20 n cache],.**
17640 20 20 20 20 20 20 74 68 65 20 62 75 73 79 20 68 the busy h
17650 61 6e 64 6c 65 72 20 66 6f 72 20 74 68 65 20 64 andler for the d
17660 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
17670 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73 69 on currently usi
17680 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 ng.** t
17690 68 65 20 63 61 63 68 65 20 73 68 61 6c 6c 20 62 he cache shall b
176a0 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 74 e invoked when t
176b0 68 65 20 63 61 63 68 65 20 65 6e 63 6f 75 6e 74 he cache encount
176c0 65 72 73 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a ers a lock..**.*
176d0 2a 20 7b 48 31 32 33 31 36 7d 20 49 66 20 61 20 * {H12316} If a
176e0 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63 61 6c busy handler cal
176f0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 7a 65 lback returns ze
17700 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c ro, then the SQL
17710 69 74 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a ite interface.**
17720 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 70 that p
17730 72 6f 76 6f 6b 65 64 20 74 68 65 20 6c 6f 63 6b rovoked the lock
17740 69 6e 67 20 65 76 65 6e 74 20 73 68 61 6c 6c 20 ing event shall
17750 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 return [SQLITE_B
17760 55 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 USY]..**.** {H12
17770 33 31 38 7d 20 53 51 4c 69 74 65 20 73 68 61 6c 318} SQLite shal
17780 6c 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 l invokes the bu
17790 73 79 20 68 61 6e 64 6c 65 72 20 77 69 74 68 20 sy handler with
177a0 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 77 68 two arguments wh
177b0 69 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ich.**
177c0 61 72 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 are a copy of th
177d0 65 20 70 6f 69 6e 74 65 72 20 73 75 70 70 6c 69 e pointer suppli
177e0 65 64 20 62 79 20 74 68 65 20 33 72 64 20 70 61 ed by the 3rd pa
177f0 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 rameter to.**
17800 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
17810 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 20 busy_handler()]
17820 61 6e 64 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 and a count of t
17830 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 he number of pri
17840 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 or.** i
17850 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 nvocations of th
17860 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 e busy handler f
17870 6f 72 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b or the same lock
17880 69 6e 67 20 65 76 65 6e 74 2e 0a 2a 2a 0a 2a 2a ing event..**.**
17890 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a ASSUMPTIONS:.**
178a0 0a 2a 2a 20 7b 41 31 32 33 31 39 7d 20 41 20 62 .** {A12319} A b
178b0 75 73 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 usy handler must
178c0 20 6e 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64 not close the d
178d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
178e0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f on.** o
178f0 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 r [prepared stat
17900 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f ement] that invo
17910 6b 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e ked the busy han
17920 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f dler..*/.SQLITE_
17930 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
17940 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c busy_handler(sql
17950 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f ite3*, int(*)(vo
17960 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29 id*,int), void*)
17970 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
17980 46 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69 F: Set A Busy Ti
17990 6d 65 6f 75 74 20 7b 48 31 32 33 34 30 7d 20 3c meout {H12340} <
179a0 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S40410>.**.** Th
179b0 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
179c0 61 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f a [sqlite3_busy_
179d0 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 20 68 handler | busy h
179e0 61 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 6c 65 andler] that sle
179f0 65 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65 eps.** for a spe
17a00 63 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66 cified amount of
17a10 20 74 69 6d 65 20 77 68 65 6e 20 61 20 74 61 62 time when a tab
17a20 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 54 le is locked. T
17a30 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 he handler.** wi
17a40 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c ll sleep multipl
17a50 65 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 61 74 e times until at
17a60 20 6c 65 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c least "ms" mill
17a70 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 iseconds of slee
17a80 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61 63 63 ping.** have acc
17a90 75 6d 75 6c 61 74 65 64 2e 20 7b 48 31 32 33 34 umulated. {H1234
17aa0 33 7d 20 41 66 74 65 72 20 22 6d 73 22 20 6d 69 3} After "ms" mi
17ab0 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c lliseconds of sl
17ac0 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 68 eeping,.** the h
17ad0 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 30 andler returns 0
17ae0 20 77 68 69 63 68 20 63 61 75 73 65 73 20 5b 73 which causes [s
17af0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 74 qlite3_step()] t
17b00 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c o return.** [SQL
17b10 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 ITE_BUSY] or [SQ
17b20 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b LITE_IOERR_BLOCK
17b30 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 ED]..**.** Calli
17b40 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ng this routine
17b50 77 69 74 68 20 61 6e 20 61 72 67 75 6d 65 6e 74 with an argument
17b60 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
17b70 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74 ual to zero.** t
17b80 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20 62 75 73 urns off all bus
17b90 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a y handlers..**.*
17ba0 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 * There can only
17bb0 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 be a single bus
17bc0 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 y handler for a
17bd0 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 particular.** [d
17be0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
17bf0 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 67 69 76 65 on] any any give
17c00 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 20 61 6e n moment. If an
17c10 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c other busy handl
17c20 65 72 0a 2a 2a 20 77 61 73 20 64 65 66 69 6e 65 er.** was define
17c30 64 20 20 28 75 73 69 6e 67 20 5b 73 71 6c 69 74 d (using [sqlit
17c40 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 e3_busy_handler(
17c50 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c )]) prior to cal
17c60 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 ling.** this rou
17c70 74 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 65 72 tine, that other
17c80 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69 73 busy handler is
17c90 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 cleared..**.**
17ca0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
17cb0 2a 20 7b 48 31 32 33 34 31 7d 20 54 68 65 20 5b * {H12341} The [
17cc0 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d sqlite3_busy_tim
17cd0 65 6f 75 74 28 29 5d 20 66 75 6e 63 74 69 6f 6e eout()] function
17ce0 20 73 68 61 6c 6c 20 6f 76 65 72 72 69 64 65 20 shall override
17cf0 61 6e 79 20 70 72 69 6f 72 0a 2a 2a 20 20 20 20 any prior.**
17d00 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 [sqlite3_b
17d10 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 6f usy_timeout()] o
17d20 72 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f r [sqlite3_busy_
17d30 68 61 6e 64 6c 65 72 28 29 5d 20 73 65 74 74 69 handler()] setti
17d40 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f ng.** o
17d50 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 n the same [data
17d60 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
17d70 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 34 33 7d ..**.** {H12343}
17d80 20 49 66 20 74 68 65 20 32 6e 64 20 70 61 72 61 If the 2nd para
17d90 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
17da0 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 3_busy_timeout()
17db0 5d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 0a 2a ] is less than.*
17dc0 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 65 71 * or eq
17dd0 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 ual to zero, the
17de0 6e 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c n the busy handl
17df0 65 72 20 73 68 61 6c 6c 20 62 65 20 63 6c 65 61 er shall be clea
17e00 72 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 red so that.**
17e10 20 20 20 20 20 20 20 20 61 6c 6c 20 73 75 62 73 all subs
17e20 65 71 75 65 6e 74 20 6c 6f 63 6b 69 6e 67 20 65 equent locking e
17e30 76 65 6e 74 73 20 69 6d 6d 65 64 69 61 74 65 6c vents immediatel
17e40 79 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 y return [SQLITE
17e50 5f 42 55 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 _BUSY]..**.** {H
17e60 31 32 33 34 34 7d 20 49 66 20 74 68 65 20 32 6e 12344} If the 2n
17e70 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b d parameter to [
17e80 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d sqlite3_busy_tim
17e90 65 6f 75 74 28 29 5d 20 69 73 20 61 20 70 6f 73 eout()] is a pos
17ea0 69 74 69 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 itive.**
17eb0 20 20 6e 75 6d 62 65 72 20 4e 2c 20 74 68 65 6e number N, then
17ec0 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 a busy handler
17ed0 73 68 61 6c 6c 20 62 65 20 73 65 74 20 74 68 61 shall be set tha
17ee0 74 20 72 65 70 65 61 74 65 64 6c 79 20 63 61 6c t repeatedly cal
17ef0 6c 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 ls.** t
17f00 68 65 20 78 53 6c 65 65 70 28 29 20 6d 65 74 68 he xSleep() meth
17f10 6f 64 20 69 6e 20 74 68 65 20 5b 73 71 6c 69 74 od in the [sqlit
17f20 65 33 5f 76 66 73 20 7c 20 56 46 53 20 69 6e 74 e3_vfs | VFS int
17f30 65 72 66 61 63 65 5d 20 75 6e 74 69 6c 0a 2a 2a erface] until.**
17f40 20 20 20 20 20 20 20 20 20 20 65 69 74 68 65 72 either
17f50 20 74 68 65 20 6c 6f 63 6b 20 63 6c 65 61 72 73 the lock clears
17f60 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 63 75 or until the cu
17f70 6d 75 6c 61 74 69 76 65 20 73 6c 65 65 70 20 74 mulative sleep t
17f80 69 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ime.**
17f90 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20 62 79 reported back by
17fa0 20 78 53 6c 65 65 70 28 29 20 65 78 63 65 65 64 xSleep() exceed
17fb0 73 20 4e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 s N milliseconds
17fc0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
17fd0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 int sqlite3_busy
17fe0 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 _timeout(sqlite3
17ff0 2a 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a *, int ms);../*.
18000 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e ** CAPI3REF: Con
18010 76 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 venience Routine
18020 73 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 s For Running Qu
18030 65 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c eries {H12370} <
18040 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 S10000>.**.** De
18050 66 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 finition: A <b>r
18060 65 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 esult table</b>
18070 69 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 is memory data s
18080 74 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64 tructure created
18090 20 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 by the.** [sqli
180a0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d te3_get_table()]
180b0 20 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72 interface. A r
180c0 65 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f esult table reco
180d0 72 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c rds the.** compl
180e0 65 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 ete query result
180f0 73 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f s from one or mo
18100 72 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a re queries..**.*
18110 2a 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 * The table conc
18120 65 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e eptually has a n
18130 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e umber of rows an
18140 64 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a d columns. But.
18150 2a 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73 ** these numbers
18160 20 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 are not part of
18170 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c the result tabl
18180 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65 e itself. These
18190 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20 .** numbers are
181a0 6f 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74 obtained separat
181b0 65 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 ely. Let N be t
181c0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
181d0 73 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 s.** and M be th
181e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 e number of colu
181f0 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 mns..**.** A res
18200 75 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20 ult table is an
18210 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 array of pointer
18220 73 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e s to zero-termin
18230 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e ated UTF-8 strin
18240 67 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 gs..** There are
18250 20 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 (N+1)*M element
18260 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 s in the array.
18270 20 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69 The first M poi
18280 6e 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 nters point.** t
18290 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 o zero-terminate
182a0 64 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20 d strings that
182b0 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 contain the name
182c0 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 s of the columns
182d0 2e 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 ..** The remaini
182e0 6e 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 ng entries all p
182f0 6f 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 oint to query re
18300 73 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c sults. NULL val
18310 75 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e ues result.** in
18320 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 NULL pointers.
18330 20 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 All other value
18340 73 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 s are in their U
18350 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e TF-8 zero-termin
18360 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 ated.** string r
18370 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 epresentation as
18380 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 returned by [sq
18390 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
183a0 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 t()]..**.** A re
183b0 73 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74 sult table might
183c0 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 consist of one
183d0 6f 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 or more memory a
183e0 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 llocations..** I
183f0 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f t is not safe to
18400 20 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 pass a result t
18410 61 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f able directly to
18420 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
18430 5d 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 ]..** A result t
18440 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 able should be d
18450 65 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 eallocated using
18460 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 [sqlite3_free_t
18470 61 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 able()]..**.** A
18480 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 s an example of
18490 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 the result table
184a0 20 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 format, suppose
184b0 20 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a a query result.
184c0 2a 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 ** is as follows
184d0 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
184e0 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 ote><pre>.**
184f0 20 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 Name
18500 7c 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 | Age.**
18510 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
18520 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 -------.**
18530 20 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 Alice |
18540 34 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 43.** Bob
18550 20 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a | 28.**
18560 20 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 Cindy
18570 20 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 | 21.** </pr
18580 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
18590 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 **.** There are
185a0 74 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 two column (M==2
185b0 29 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 ) and three rows
185c0 20 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 (N==3). Thus t
185d0 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 he.** result tab
185e0 6c 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 le has 8 entries
185f0 2e 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 . Suppose the r
18600 65 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 esult table is s
18610 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 tored.** in an a
18620 72 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 rray names azRes
18630 75 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 ult. Then azRes
18640 75 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 ult holds this c
18650 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 ontent:.**.** <b
18660 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a lockquote><pre>.
18670 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 ** azResu
18680 6c 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d lt[0] = "Nam
18690 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a e";.** az
186a0 52 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 Result[1] =
186b0 22 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 "Age";.**
186c0 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d azResult[2]
186d0 20 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 = "Alice";.**
186e0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 azResult&#
186f0 39 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 91;3] = "43";.**
18700 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 azResult
18710 26 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b [4] = "Bob";
18720 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 .** azRes
18730 75 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 ult[5] = "28
18740 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 ";.** azR
18750 65 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 esult[6] = "
18760 43 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 Cindy";.**
18770 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 azResult[7
18780 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 ] = "21";.** </p
18790 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
187a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
187b0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 e3_get_table() f
187c0 75 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 unction evaluate
187d0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a s one or more.**
187e0 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 semicolon-separ
187f0 61 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 ated SQL stateme
18800 6e 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d nts in the zero-
18810 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
18820 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 .** string of it
18830 73 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e s 2nd parameter.
18840 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 It returns a r
18850 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 esult table to t
18860 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 he.** pointer gi
18870 76 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 ven in its 3rd p
18880 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 arameter..**.**
18890 41 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e After the callin
188a0 67 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 g function has f
188b0 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 inished using th
188c0 65 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f e result, it sho
188d0 75 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 uld.** pass the
188e0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 pointer to the r
188f0 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 esult table to s
18900 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c qlite3_free_tabl
18910 65 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a e() in order to.
18920 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d ** release the m
18930 65 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d emory that was m
18940 61 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73 alloced. Becaus
18950 65 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65 e of the way the
18960 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c .** [sqlite3_mal
18970 6c 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77 loc()] happens w
18980 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 ithin sqlite3_ge
18990 74 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63 t_table(), the c
189a0 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 alling.** functi
189b0 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 on must not try
189c0 74 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 to call [sqlite3
189d0 5f 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c _free()] directl
189e0 79 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c y. Only.** [sql
189f0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 ite3_free_table(
18a00 29 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 )] is able to re
18a10 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 lease the memory
18a20 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 properly and sa
18a30 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 fely..**.** The
18a40 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c sqlite3_get_tabl
18a50 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 e() interface is
18a60 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
18a70 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 a wrapper around
18a80 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 .** [sqlite3_exe
18a90 63 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 c()]. The sqlit
18aa0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72 e3_get_table() r
18ab0 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 outine does not
18ac0 68 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 have access.** t
18ad0 6f 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 o any internal d
18ae0 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f ata structures o
18af0 66 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73 f SQLite. It us
18b00 65 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c es only the publ
18b10 69 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 ic.** interface
18b20 64 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41 defined here. A
18b30 73 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c s a consequence,
18b40 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 errors that occ
18b50 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 ur in the.** wra
18b60 70 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 pper layer outsi
18b70 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e de of the intern
18b80 61 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 al [sqlite3_exec
18b90 28 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 ()] call are not
18ba0 0a 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e .** reflected in
18bb0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
18bc0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 s to [sqlite3_er
18bd0 72 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c rcode()] or [sql
18be0 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a ite3_errmsg()]..
18bf0 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
18c00 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37 31 7d :.**.** {H12371}
18c10 20 49 66 20 61 20 5b 73 71 6c 69 74 65 33 5f 67 If a [sqlite3_g
18c20 65 74 5f 74 61 62 6c 65 28 29 5d 20 66 61 69 6c et_table()] fail
18c30 73 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 s a memory alloc
18c40 61 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 20 ation, then.**
18c50 20 20 20 20 20 20 20 20 69 74 20 73 68 61 6c 6c it shall
18c60 20 66 72 65 65 20 74 68 65 20 72 65 73 75 6c 74 free the result
18c70 20 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e table under con
18c80 73 74 72 75 63 74 69 6f 6e 2c 20 61 62 6f 72 74 struction, abort
18c90 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
18ca0 20 71 75 65 72 79 20 69 6e 20 70 72 6f 63 65 73 query in proces
18cb0 73 2c 20 73 6b 69 70 20 61 6e 79 20 73 75 62 73 s, skip any subs
18cc0 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2c 20 equent queries,
18cd0 73 65 74 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 set the.**
18ce0 20 20 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 6f *pazResult o
18cf0 75 74 70 75 74 20 70 6f 69 6e 74 65 72 20 74 6f utput pointer to
18d00 20 4e 55 4c 4c 20 61 6e 64 20 72 65 74 75 72 6e NULL and return
18d10 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e [SQLITE_NOMEM].
18d20 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37 33 7d 20 .**.** {H12373}
18d30 49 66 20 74 68 65 20 70 6e 43 6f 6c 75 6d 6e 20 If the pnColumn
18d40 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 parameter to [sq
18d50 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 lite3_get_table(
18d60 29 5d 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 0a 2a )] is not NULL.*
18d70 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 * then
18d80 61 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 a successful inv
18d90 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 ocation of [sqli
18da0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d te3_get_table()]
18db0 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 shall.**
18dc0 20 20 20 77 72 69 74 65 20 74 68 65 20 6e 75 6d write the num
18dd0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
18de0 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 n the.**
18df0 20 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 result set of
18e00 74 68 65 20 71 75 65 72 79 20 69 6e 74 6f 20 2a the query into *
18e10 70 6e 43 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 pnColumn..**.**
18e20 7b 48 31 32 33 37 34 7d 20 49 66 20 74 68 65 20 {H12374} If the
18e30 70 6e 52 6f 77 20 70 61 72 61 6d 65 74 65 72 20 pnRow parameter
18e40 74 6f 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f to [sqlite3_get_
18e50 74 61 62 6c 65 28 29 5d 20 69 73 20 6e 6f 74 20 table()] is not
18e60 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 NULL.**
18e70 20 74 68 65 6e 20 61 20 73 75 63 63 65 73 73 66 then a successf
18e80 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 ul invocation of
18e90 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 [sqlite3_get_ta
18ea0 62 6c 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20 ble()] shall.**
18eb0 20 20 20 20 20 20 20 20 20 77 72 69 74 65 73 20 writes
18ec0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f the number of ro
18ed0 77 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 ws in the.**
18ee0 20 20 20 20 20 20 72 65 73 75 6c 74 20 73 65 74 result set
18ef0 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 69 6e of the query in
18f00 74 6f 20 2a 70 6e 52 6f 77 2e 0a 2a 2a 0a 2a 2a to *pnRow..**.**
18f10 20 7b 48 31 32 33 37 36 7d 20 41 20 73 75 63 63 {H12376} A succ
18f20 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f essful invocatio
18f30 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 67 65 n of [sqlite3_ge
18f40 74 5f 74 61 62 6c 65 28 29 5d 20 74 68 61 74 20 t_table()] that
18f50 63 6f 6d 70 75 74 65 73 0a 2a 2a 20 20 20 20 20 computes.**
18f60 20 20 20 20 20 4e 20 72 6f 77 73 20 6f 66 20 72 N rows of r
18f70 65 73 75 6c 74 20 77 69 74 68 20 43 20 63 6f 6c esult with C col
18f80 75 6d 6e 73 20 70 65 72 20 72 6f 77 20 73 68 61 umns per row sha
18f90 6c 6c 20 6d 61 6b 65 20 2a 70 61 7a 52 65 73 75 ll make *pazResu
18fa0 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 lt.** p
18fb0 6f 69 6e 74 20 74 6f 20 61 6e 20 61 72 72 61 79 oint to an array
18fc0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
18fd0 28 4e 2b 31 29 2a 43 20 73 74 72 69 6e 67 73 20 (N+1)*C strings
18fe0 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 0a where the first.
18ff0 2a 2a 20 20 20 20 20 20 20 20 20 20 43 20 73 74 ** C st
19000 72 69 6e 67 73 20 61 72 65 20 63 6f 6c 75 6d 6e rings are column
19010 20 6e 61 6d 65 73 20 61 73 20 6f 62 74 61 69 6e names as obtain
19020 65 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 ed from.**
19030 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c [sqlite3_col
19040 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61 6e 64 20 umn_name()] and
19050 74 68 65 20 72 65 73 74 20 61 72 65 20 63 6f 6c the rest are col
19060 75 6d 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 umn result value
19070 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 s.** ob
19080 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c tained from [sql
19090 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
190a0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 ()]..**.** {H123
190b0 37 39 7d 20 54 68 65 20 76 61 6c 75 65 73 20 69 79} The values i
190c0 6e 20 74 68 65 20 70 61 7a 52 65 73 75 6c 74 20 n the pazResult
190d0 61 72 72 61 79 20 72 65 74 75 72 6e 65 64 20 62 array returned b
190e0 79 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 y [sqlite3_get_t
190f0 61 62 6c 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20 able()].**
19100 20 20 20 20 73 68 61 6c 6c 20 72 65 6d 61 69 6e shall remain
19110 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 63 6c 65 valid until cle
19120 61 72 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 ared by [sqlite3
19130 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a _free_table()]..
19140 2a 2a 0a 2a 2a 20 7b 48 31 32 33 38 32 7d 20 57 **.** {H12382} W
19150 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 hen an error occ
19160 75 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75 urs during evalu
19170 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 ation of [sqlite
19180 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 0a 2a 3_get_table()].*
19190 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66 * the f
191a0 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 73 65 unction shall se
191b0 74 20 2a 70 61 7a 52 65 73 75 6c 74 20 74 6f 20 t *pazResult to
191c0 4e 55 4c 4c 2c 20 77 72 69 74 65 20 61 6e 20 65 NULL, write an e
191d0 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 rror message.**
191e0 20 20 20 20 20 20 20 20 20 69 6e 74 6f 20 6d 65 into me
191f0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
19200 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c om [sqlite3_mall
19210 6f 63 28 29 5d 2c 20 6d 61 6b 65 0a 2a 2a 20 20 oc()], make.**
19220 20 20 20 20 20 20 20 20 2a 2a 70 7a 45 72 72 6d **pzErrm
19230 73 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 sg point to that
19240 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 error message,
19250 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 and return a.**
19260 20 20 20 20 20 20 20 20 20 61 70 70 72 6f 70 72 appropr
19270 69 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 iate [error code
19280 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 ]..*/.SQLITE_API
19290 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 int sqlite3_get
192a0 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 _table(. sqlite
192b0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
192c0 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 /* An open datab
192d0 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ase */. const c
192e0 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 2f har *zSql, /
192f0 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c * SQL to be eval
19300 75 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 uated */. char
19310 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20 ***pazResult,
19320 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 74 /* Results of t
19330 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e he query */. in
19340 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 t *pnRow,
19350 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
19360 20 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72 69 result rows wri
19370 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 tten here */. i
19380 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 nt *pnColumn,
19390 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
193a0 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 f result columns
193b0 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f written here */
193c0 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d . char **pzErrm
193d0 73 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f sg /* Erro
193e0 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 r msg written he
193f0 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f re */.);.SQLITE_
19400 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
19410 5f 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 72 _free_table(char
19420 20 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a 0a **result);../*.
19430 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f 72 ** CAPI3REF: For
19440 6d 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 72 matted String Pr
19450 69 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 inting Functions
19460 20 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30 30 {H17400} <S7000
19470 30 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 0><S20000>.**.**
19480 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
19490 61 72 65 20 77 6f 72 6b 61 6c 69 6b 65 73 20 6f are workalikes o
194a0 66 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22 f the "printf()"
194b0 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74 family of funct
194c0 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 ions.** from the
194d0 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 standard C libr
194e0 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ary..**.** The s
194f0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 qlite3_mprintf()
19500 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70 and sqlite3_vmp
19510 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73 rintf() routines
19520 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20 write their.**
19530 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d results into mem
19540 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
19550 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f m [sqlite3_mallo
19560 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72 c()]..** The str
19570 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 ings returned by
19580 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69 these two routi
19590 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a nes should be.**
195a0 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71 released by [sq
195b0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20 lite3_free()].
195c0 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65 Both routines re
195d0 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70 turn a.** NULL p
195e0 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74 ointer if [sqlit
195f0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20 e3_malloc()] is
19600 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 unable to alloca
19610 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 6d te enough.** mem
19620 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ory to hold the
19630 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 resulting string
19640 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c 69 74 ..**.** In sqlit
19650 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 72 6f e3_snprintf() ro
19660 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 utine is similar
19670 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 29 22 to "snprintf()"
19680 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61 from.** the sta
19690 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e ndard C library.
196a0 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 The result is
196b0 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
196c0 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70 70 6c .** buffer suppl
196d0 69 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e ied as the secon
196e0 64 20 70 61 72 61 6d 65 74 65 72 20 77 68 6f 73 d parameter whos
196f0 65 20 73 69 7a 65 20 69 73 20 67 69 76 65 6e 20 e size is given
19700 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 by.** the first
19710 70 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74 65 20 parameter. Note
19720 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f that the order o
19730 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74 f the.** first t
19740 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20 69 73 wo parameters is
19750 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d 20 73 reversed from s
19760 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68 69 73 nprintf(). This
19770 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 6f 72 is an.** histor
19780 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20 74 68 ical accident th
19790 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 69 78 at cannot be fix
197a0 65 64 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b ed without break
197b0 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 ing.** backwards
197c0 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20 compatibility.
197d0 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 Note also that
197e0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
197f0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 ().** returns a
19800 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62 pointer to its b
19810 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 uffer instead of
19820 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
19830 2a 20 63 68 61 72 61 63 74 65 72 73 20 61 63 74 * characters act
19840 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e ually written in
19850 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e 20 20 to the buffer.
19860 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a 2a 2a We admit that.**
19870 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
19880 68 61 72 61 63 74 65 72 73 20 77 72 69 74 74 65 haracters writte
19890 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 6f 72 n would be a mor
198a0 65 20 75 73 65 66 75 6c 20 72 65 74 75 72 6e 0a e useful return.
198b0 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77 65 20 ** value but we
198c0 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 cannot change th
198d0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
198e0 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 of sqlite3_snpr
198f0 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 77 69 intf().** now wi
19900 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63 thout breaking c
19910 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a ompatibility..**
19920 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 20 74 .** As long as t
19930 68 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 69 he buffer size i
19940 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a s greater than z
19950 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 6e 70 ero, sqlite3_snp
19960 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 72 61 rintf().** guara
19970 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 20 62 ntees that the b
19980 75 66 66 65 72 20 69 73 20 61 6c 77 61 79 73 20 uffer is always
19990 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e zero-terminated.
199a0 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 The first.** p
199b0 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69 73 20 arameter "n" is
199c0 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 20 6f the total size o
199d0 66 20 74 68 65 20 62 75 66 66 65 72 2c 20 69 6e f the buffer, in
199e0 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20 66 6f cluding space fo
199f0 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 74 65 r.** the zero te
19a00 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 74 68 rminator. So th
19a10 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 6e 67 e longest string
19a20 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d that can be com
19a30 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 74 74 pletely.** writt
19a40 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20 63 en will be n-1 c
19a50 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a haracters..**.**
19a60 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
19a70 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f all implement so
19a80 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f me additional fo
19a90 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69 rmatting.** opti
19aa0 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65 ons that are use
19ab0 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63 ful for construc
19ac0 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65 ting SQL stateme
19ad0 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 nts..** All of t
19ae0 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28 he usual printf(
19af0 29 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 ) formatting opt
19b00 69 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20 ions apply. In
19b10 61 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a addition, there.
19b20 2a 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20 ** is are "%q",
19b30 22 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f "%Q", and "%z" o
19b40 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ptions..**.** Th
19b50 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72 6b e %q option work
19b60 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68 61 s like %s in tha
19b70 74 20 69 74 20 73 75 62 73 74 69 74 75 74 65 73 t it substitutes
19b80 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 a null-terminat
19b90 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f ed.** string fro
19ba0 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c m the argument l
19bb0 69 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73 ist. But %q als
19bc0 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20 o doubles every
19bd0 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a '\'' character..
19be0 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65 ** %q is designe
19bf0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
19c00 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 a string litera
19c10 6c 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e 67 20 l. By doubling
19c20 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 68 61 each '\''.** cha
19c30 72 61 63 74 65 72 20 69 74 20 65 73 63 61 70 65 racter it escape
19c40 73 20 74 68 61 74 20 63 68 61 72 61 63 74 65 72 s that character
19c50 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74 and allows it t
19c60 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e o be inserted in
19c70 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 to.** the string
19c80 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d ..**.** For exam
19c90 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 65 20 ple, assume the
19ca0 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c 65 20 string variable
19cb0 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 74 zText contains t
19cc0 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ext as follows:.
19cd0 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
19ce0 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 e><pre>.** char
19cf0 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 73 20 *zText = "It's
19d00 61 20 68 61 70 70 79 20 64 61 79 21 22 3b 0a 2a a happy day!";.*
19d10 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 * </pre></blockq
19d20 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 uote>.**.** One
19d30 63 61 6e 20 75 73 65 20 74 68 69 73 20 74 65 78 can use this tex
19d40 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 t in an SQL stat
19d50 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73 ement as follows
19d60 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
19d70 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 ote><pre>.** ch
19d80 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 ar *zSQL = sqlit
19d90 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 e3_mprintf("INSE
19da0 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 RT INTO table VA
19db0 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a 54 65 LUES('%q')", zTe
19dc0 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 xt);.** sqlite3
19dd0 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 _exec(db, zSQL,
19de0 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 0, 0, 0);.** sq
19df0 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 lite3_free(zSQL)
19e00 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f ;.** </pre></blo
19e10 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 ckquote>.**.** B
19e20 65 63 61 75 73 65 20 74 68 65 20 25 71 20 66 6f ecause the %q fo
19e30 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20 75 rmat string is u
19e40 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63 68 sed, the '\'' ch
19e50 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78 74 aracter in zText
19e60 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64 20 61 .** is escaped a
19e70 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e 65 72 nd the SQL gener
19e80 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c 6c 6f ated is as follo
19e90 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b ws:.**.** <block
19ea0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
19eb0 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c INSERT INTO tabl
19ec0 65 31 20 56 41 4c 55 45 53 28 27 49 74 27 27 73 e1 VALUES('It''s
19ed0 20 61 20 68 61 70 70 79 20 64 61 79 21 27 29 0a a happy day!').
19ee0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b ** </pre></block
19ef0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 quote>.**.** Thi
19f00 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20 20 48 s is correct. H
19f10 61 64 20 77 65 20 75 73 65 64 20 25 73 20 69 6e ad we used %s in
19f20 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74 68 65 stead of %q, the
19f30 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c 0a 2a generated SQL.*
19f40 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c 6f 6f * would have loo
19f50 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a ked like this:.*
19f60 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
19f70 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 ><pre>.** INSER
19f80 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 T INTO table1 VA
19f90 4c 55 45 53 28 27 49 74 27 73 20 61 20 68 61 70 LUES('It's a hap
19fa0 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f py day!');.** </
19fb0 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 pre></blockquote
19fc0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 >.**.** This sec
19fd0 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61 ond example is a
19fe0 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72 n SQL syntax err
19ff0 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61 or. As a genera
1a000 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f 75 6c l rule you shoul
1a010 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 20 d.** always use
1a020 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73 %q instead of %s
1a030 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 when inserting
1a040 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69 text into a stri
1a050 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a ng literal..**.*
1a060 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20 * The %Q option
1a070 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78 works like %q ex
1a080 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64 cept it also add
1a090 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20 s single quotes
1a0a0 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75 around.** the ou
1a0b0 74 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74 tside of the tot
1a0c0 61 6c 20 73 74 72 69 6e 67 2e 20 20 41 64 64 69 al string. Addi
1a0d0 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 tionally, if the
1a0e0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68 parameter in th
1a0f0 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6c 69 e.** argument li
1a100 73 74 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 st is a NULL poi
1a110 6e 74 65 72 2c 20 25 51 20 73 75 62 73 74 69 74 nter, %Q substit
1a120 75 74 65 73 20 74 68 65 20 74 65 78 74 20 22 4e utes the text "N
1a130 55 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a 2a 2a ULL" (without.**
1a140 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 29 20 single quotes)
1a150 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 in place of the
1a160 25 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f 2c 20 %Q option. So,
1a170 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f 6e 65 for example, one
1a180 20 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a could say:.**.*
1a190 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
1a1a0 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 re>.** char *zS
1a1b0 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 QL = sqlite3_mpr
1a1c0 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 intf("INSERT INT
1a1d0 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 25 O table VALUES(%
1a1e0 51 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 Q)", zText);.**
1a1f0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 sqlite3_exec(db
1a200 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29 , zSQL, 0, 0, 0)
1a210 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72 ;.** sqlite3_fr
1a220 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 ee(zSQL);.** </p
1a230 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
1a240 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 .**.** The code
1a250 61 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e 64 65 above will rende
1a260 72 20 61 20 63 6f 72 72 65 63 74 20 53 51 4c 20 r a correct SQL
1a270 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 statement in the
1a280 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61 62 6c zSQL.** variabl
1a290 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 7a 54 e even if the zT
1a2a0 65 78 74 20 76 61 72 69 61 62 6c 65 20 69 73 20 ext variable is
1a2b0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
1a2c0 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22 20 66 **.** The "%z" f
1a2d0 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e ormatting option
1a2e0 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c works exactly l
1a2f0 69 6b 65 20 22 25 73 22 20 77 69 74 68 20 74 68 ike "%s" with th
1a300 65 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20 74 68 e.** addition th
1a310 61 74 20 61 66 74 65 72 20 74 68 65 20 73 74 72 at after the str
1a320 69 6e 67 20 68 61 73 20 62 65 65 6e 20 72 65 61 ing has been rea
1a330 64 20 61 6e 64 20 63 6f 70 69 65 64 20 69 6e 74 d and copied int
1a340 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c o.** the result,
1a350 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
1a360 5d 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 ] is called on t
1a370 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 2e he input string.
1a380 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 4e 56 {END}.**.** INV
1a390 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
1a3a0 48 31 37 34 30 33 7d 20 20 54 68 65 20 5b 73 71 H17403} The [sq
1a3b0 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 5d lite3_mprintf()]
1a3c0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 76 6d and [sqlite3_vm
1a3d0 70 72 69 6e 74 66 28 29 5d 20 69 6e 74 65 72 66 printf()] interf
1a3e0 61 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 aces.**
1a3f0 20 20 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 return either
1a400 70 6f 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f pointers to zero
1a410 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d -terminated UTF-
1a420 38 20 73 74 72 69 6e 67 73 20 68 65 6c 64 20 69 8 strings held i
1a430 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6d n.** m
1a440 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
1a450 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c rom [sqlite3_mal
1a460 6c 6f 63 28 29 5d 20 6f 72 20 4e 55 4c 4c 20 70 loc()] or NULL p
1a470 6f 69 6e 74 65 72 73 20 69 66 0a 2a 2a 20 20 20 ointers if.**
1a480 20 20 20 20 20 20 20 20 61 20 63 61 6c 6c 20 74 a call t
1a490 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f o [sqlite3_mallo
1a4a0 63 28 29 5d 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a c()] fails..**.*
1a4b0 2a 20 7b 48 31 37 34 30 36 7d 20 20 54 68 65 20 * {H17406} The
1a4c0 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 [sqlite3_snprint
1a4d0 66 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 f()] interface w
1a4e0 72 69 74 65 73 20 61 20 7a 65 72 6f 2d 74 65 72 rites a zero-ter
1a4f0 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 minated.**
1a500 20 20 20 20 20 55 54 46 2d 38 20 73 74 72 69 6e UTF-8 strin
1a510 67 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 g into the buffe
1a520 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 r pointed to by
1a530 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
1a540 65 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 eter.**
1a550 20 20 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 provided that
1a560 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 the first parame
1a570 74 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 ter is greater t
1a580 68 61 6e 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 han zero..**.**
1a590 7b 48 31 37 34 30 37 7d 20 20 54 68 65 20 5b 73 {H17407} The [s
1a5a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a5b0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 )] interface doe
1a5c0 73 20 6e 6f 74 20 77 72 69 74 65 20 73 6c 6f 74 s not write slot
1a5d0 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 s of.**
1a5e0 20 20 69 74 73 20 6f 75 74 70 75 74 20 62 75 66 its output buf
1a5f0 66 65 72 20 28 74 68 65 20 73 65 63 6f 6e 64 20 fer (the second
1a600 70 61 72 61 6d 65 74 65 72 29 20 6f 75 74 73 69 parameter) outsi
1a610 64 65 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20 de the range.**
1a620 20 20 20 20 20 20 20 20 20 20 6f 66 20 30 20 74 of 0 t
1a630 68 72 6f 75 67 68 20 4e 2d 31 20 28 77 68 65 72 hrough N-1 (wher
1a640 65 20 4e 20 69 73 20 74 68 65 20 66 69 72 73 74 e N is the first
1a650 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20 20 parameter).**
1a660 20 20 20 20 20 20 20 20 20 72 65 67 61 72 64 6c regardl
1a670 65 73 73 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 ess of the lengt
1a680 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a h of the string.
1a690 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72 65 71 ** req
1a6a0 75 65 73 74 65 64 20 62 79 20 74 68 65 20 66 6f uested by the fo
1a6b0 72 6d 61 74 20 73 70 65 63 69 66 69 63 61 74 69 rmat specificati
1a6c0 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 on..*/.SQLITE_AP
1a6d0 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f I char *sqlite3_
1a6e0 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 mprintf(const ch
1a6f0 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 ar*,...);.SQLITE
1a700 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 _API char *sqlit
1a710 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 e3_vmprintf(cons
1a720 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 t char*, va_list
1a730 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 );.SQLITE_API ch
1a740 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 ar *sqlite3_snpr
1a750 69 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 intf(int,char*,c
1a760 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
1a770 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
1a780 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 F: Memory Alloca
1a790 74 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b tion Subsystem {
1a7a0 48 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e H17300} <S20000>
1a7b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 .**.** The SQLit
1a7c0 65 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 e core uses the
1a7d0 73 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 se three routine
1a7e0 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 s for all of its
1a7f0 20 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c own.** internal
1a800 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
1a810 6f 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 on needs. "Core"
1a820 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 in the previous
1a830 20 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 sentence.** doe
1a840 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 s not include op
1a850 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 erating-system s
1a860 70 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c pecific VFS impl
1a870 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 ementation. The
1a880 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 .** Windows VFS
1a890 75 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c uses native mall
1a8a0 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 oc() and free()
1a8b0 66 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 for some operati
1a8c0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ons..**.** The s
1a8d0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
1a8e0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
1a8f0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 a pointer to a b
1a900 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 lock.** of memor
1a910 79 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 y at least N byt
1a920 65 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 es in length, wh
1a930 65 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 ere N is the par
1a940 61 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 ameter..** If sq
1a950 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 lite3_malloc() i
1a960 73 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 s unable to obta
1a970 69 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 in sufficient fr
1a980 65 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 ee.** memory, it
1a990 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 returns a NULL
1a9a0 70 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 pointer. If the
1a9b0 20 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a parameter N to.
1a9c0 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ** sqlite3_mallo
1a9d0 63 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e c() is zero or n
1a9e0 65 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c egative then sql
1a9f0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 ite3_malloc() re
1aa00 74 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 turns.** a NULL
1aa10 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 pointer..**.** C
1aa20 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 alling sqlite3_f
1aa30 72 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 ree() with a poi
1aa40 6e 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 nter previously
1aa50 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 returned.** by s
1aa60 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
1aa70 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c or sqlite3_reall
1aa80 6f 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 oc() releases th
1aa90 61 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 at memory so.**
1aaa0 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 that it might be
1aab0 20 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 reused. The sq
1aac0 6c 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 lite3_free() rou
1aad0 74 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d tine is.** a no-
1aae0 6f 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 op if is called
1aaf0 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e with a NULL poin
1ab00 74 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 ter. Passing a
1ab10 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 NULL pointer.**
1ab20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 to sqlite3_free(
1ab30 29 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 ) is harmless.
1ab40 41 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 After being free
1ab50 64 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f d, memory.** sho
1ab60 75 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 uld neither be r
1ab70 65 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e ead nor written.
1ab80 20 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 Even reading p
1ab90 72 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a reviously freed.
1aba0 2a 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 ** memory might
1abb0 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d result in a segm
1abc0 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f entation fault o
1abd0 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 r other severe e
1abe0 72 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 rror..** Memory
1abf0 63 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 corruption, a se
1ac00 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 gmentation fault
1ac10 2c 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 , or other sever
1ac20 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 e error.** might
1ac30 20 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 result if sqlit
1ac40 65 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c e3_free() is cal
1ac50 6c 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e led with a non-N
1ac60 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 ULL pointer that
1ac70 0a 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 .** was not obta
1ac80 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
1ac90 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 3_malloc() or sq
1aca0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e lite3_realloc().
1acb0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
1acc0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 e3_realloc() int
1acd0 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 erface attempts
1ace0 74 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 to resize a.** p
1acf0 72 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f rior memory allo
1ad00 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 cation to be at
1ad10 6c 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 least N bytes, w
1ad20 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a here N is the.**
1ad30 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
1ad40 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 r. The memory a
1ad50 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 llocation to be
1ad60 72 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 resized is the f
1ad70 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 irst.** paramete
1ad80 72 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74 r. If the first
1ad90 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
1ada0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a lite3_realloc().
1adb0 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 ** is a NULL poi
1adc0 6e 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 nter then its be
1add0 68 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 havior is identi
1ade0 63 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a cal to calling.*
1adf0 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 * sqlite3_malloc
1ae00 28 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 (N) where N is t
1ae10 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
1ae20 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
1ae30 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 ealloc()..** If
1ae40 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
1ae50 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f eter to sqlite3_
1ae60 72 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 realloc() is zer
1ae70 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 o or.** negative
1ae80 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 then the behavi
1ae90 6f 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 or is exactly th
1aea0 65 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e e same as callin
1aeb0 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 g.** sqlite3_fre
1aec0 65 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20 e(P) where P is
1aed0 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 the first parame
1aee0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
1aef0 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c ealloc()..** sql
1af00 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 ite3_realloc() r
1af10 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
1af20 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c to a memory all
1af30 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 ocation.** of at
1af40 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 least N bytes i
1af50 6e 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 n size or NULL i
1af60 66 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d f sufficient mem
1af70 6f 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 ory is unavailab
1af80 6c 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 le..** If M is t
1af90 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 he size of the p
1afa0 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c rior allocation,
1afb0 20 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 then min(N,M) b
1afc0 79 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 ytes.** of the p
1afd0 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 rior allocation
1afe0 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 are copied into
1aff0 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
1b000 20 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 buffer returned
1b010 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 .** by sqlite3_r
1b020 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 ealloc() and the
1b030 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f prior allocatio
1b040 6e 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 n is freed..** I
1b050 66 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f f sqlite3_reallo
1b060 63 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c c() returns NULL
1b070 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 , then the prior
1b080 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 allocation.** i
1b090 73 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a s not freed..**.
1b0a0 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 ** The memory re
1b0b0 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
1b0c0 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 3_malloc() and s
1b0d0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
1b0e0 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c .** is always al
1b0f0 69 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 igned to at leas
1b100 74 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e t an 8 byte boun
1b110 64 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a dary. {END}.**.*
1b120 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d * The default im
1b130 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
1b140 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
1b150 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 ation subsystem
1b160 75 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c uses.** the mall
1b170 6f 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 oc(), realloc()
1b180 61 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 and free() provi
1b190 64 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 ded by the stand
1b1a0 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a ard C library..*
1b1b0 2a 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 * {H17382} Howev
1b1c0 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 er, if SQLite is
1b1d0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
1b1e0 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d he.** SQLITE_MEM
1b1f0 4f 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c ORY_SIZE=<i>NNN<
1b200 2f 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 /i> C preprocess
1b210 6f 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 or macro (where
1b220 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 <i>NNN</i>.** is
1b230 20 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 an integer), th
1b240 65 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 en SQLite create
1b250 20 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20 a static array
1b260 6f 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c of at least.** <
1b270 69 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 i>NNN</i> bytes
1b280 69 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 in size and uses
1b290 20 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 that array for
1b2a0 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d all of its dynam
1b2b0 69 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c ic.** memory all
1b2c0 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b ocation needs. {
1b2d0 45 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c END} Additional
1b2e0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
1b2f0 72 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 r options.** may
1b300 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 be added in fut
1b310 75 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a ure releases..**
1b320 0a 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 .** In SQLite ve
1b330 72 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 rsion 3.5.0 and
1b340 33 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 3.5.1, it was po
1b350 73 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 ssible to define
1b360 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f .** the SQLITE_O
1b370 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 MIT_MEMORY_ALLOC
1b380 41 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c ATION which woul
1b390 64 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c d cause the buil
1b3a0 74 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e t-in.** implemen
1b3b0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 tation of these
1b3c0 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f routines to be o
1b3d0 6d 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 mitted. That ca
1b3e0 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e pability.** is n
1b3f0 6f 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 o longer provide
1b400 64 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 d. Only built-i
1b410 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 n memory allocat
1b420 6f 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e ors can be used.
1b430 0a 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f .**.** The Windo
1b440 77 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 ws OS interface
1b450 6c 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 layer calls.** t
1b460 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 he system malloc
1b470 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 () and free() di
1b480 72 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 rectly when conv
1b490 65 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 erting.** filena
1b4a0 6d 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 mes between the
1b4b0 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 UTF-8 encoding u
1b4c0 73 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a sed by SQLite.**
1b4d0 20 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 and whatever fi
1b4e0 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 lename encoding
1b4f0 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 is used by the p
1b500 61 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 articular Window
1b510 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f s.** installatio
1b520 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 n. Memory alloc
1b530 61 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 ation errors are
1b540 20 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a detected, but.*
1b550 2a 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 * they are repor
1b560 74 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c ted back as [SQL
1b570 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 ITE_CANTOPEN] or
1b580 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 .** [SQLITE_IOER
1b590 52 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b R] rather than [
1b5a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a SQLITE_NOMEM]..*
1b5b0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
1b5c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 30 33 7d 20 .**.** {H17303}
1b5d0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 The [sqlite3_ma
1b5e0 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61 lloc(N)] interfa
1b5f0 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 ce returns eithe
1b600 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a r a pointer to.*
1b610 2a 20 20 20 20 20 20 20 20 20 20 20 61 20 6e 65 * a ne
1b620 77 6c 79 20 63 68 65 63 6b 65 64 2d 6f 75 74 20 wly checked-out
1b630 62 6c 6f 63 6b 20 6f 66 20 61 74 20 6c 65 61 73 block of at leas
1b640 74 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d t N bytes of mem
1b650 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ory.**
1b660 20 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20 that is 8-byte
1b670 61 6c 69 67 6e 65 64 2c 20 6f 72 20 69 74 20 72 aligned, or it r
1b680 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69 eturns NULL if i
1b690 74 20 69 73 20 75 6e 61 62 6c 65 0a 2a 2a 20 20 t is unable.**
1b6a0 20 20 20 20 20 20 20 20 20 74 6f 20 66 75 6c 66 to fulf
1b6b0 69 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 2e ill the request.
1b6c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 30 34 7d 20 .**.** {H17304}
1b6d0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 The [sqlite3_ma
1b6e0 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61 lloc(N)] interfa
1b6f0 63 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c ce returns a NUL
1b700 4c 20 70 6f 69 6e 74 65 72 20 69 66 0a 2a 2a 20 L pointer if.**
1b710 20 20 20 20 20 20 20 20 20 20 4e 20 69 73 20 6c N is l
1b720 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ess than or equa
1b730 6c 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a l to zero..**.**
1b740 20 7b 48 31 37 33 30 35 7d 20 20 54 68 65 20 5b {H17305} The [
1b750 73 71 6c 69 74 65 33 5f 66 72 65 65 28 50 29 5d sqlite3_free(P)]
1b760 20 69 6e 74 65 72 66 61 63 65 20 72 65 6c 65 61 interface relea
1b770 73 65 73 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 ses memory previ
1b780 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 ously.**
1b790 20 20 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d returned from
1b7a0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
1b7b0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
1b7c0 72 65 61 6c 6c 6f 63 28 29 5d 2c 0a 2a 2a 20 20 realloc()],.**
1b7d0 20 20 20 20 20 20 20 20 20 6d 61 6b 69 6e 67 20 making
1b7e0 69 74 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 it available for
1b7f0 20 72 65 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 reuse..**.** {H
1b800 31 37 33 30 36 7d 20 20 41 20 63 61 6c 6c 20 74 17306} A call t
1b810 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 o [sqlite3_free(
1b820 4e 55 4c 4c 29 5d 20 69 73 20 61 20 68 61 72 6d NULL)] is a harm
1b830 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a less no-op..**.*
1b840 2a 20 7b 48 31 37 33 31 30 7d 20 20 41 20 63 61 * {H17310} A ca
1b850 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 ll to [sqlite3_r
1b860 65 61 6c 6c 6f 63 28 30 2c 4e 29 5d 20 69 73 20 ealloc(0,N)] is
1b870 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 20 equivalent to a
1b880 63 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 call.**
1b890 20 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 to [sqlite3_ma
1b8a0 6c 6c 6f 63 28 4e 29 5d 2e 0a 2a 2a 0a 2a 2a 20 lloc(N)]..**.**
1b8b0 7b 48 31 37 33 31 32 7d 20 20 41 20 63 61 6c 6c {H17312} A call
1b8c0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 to [sqlite3_rea
1b8d0 6c 6c 6f 63 28 50 2c 30 29 5d 20 69 73 20 65 71 lloc(P,0)] is eq
1b8e0 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 20 63 61 uivalent to a ca
1b8f0 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ll.**
1b900 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 to [sqlite3_free
1b910 28 50 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 (P)]..**.** {H17
1b920 33 31 35 7d 20 20 54 68 65 20 53 51 4c 69 74 65 315} The SQLite
1b930 20 63 6f 72 65 20 75 73 65 73 20 5b 73 71 6c 69 core uses [sqli
1b940 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b te3_malloc()], [
1b950 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
1b960 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 )],.**
1b970 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 72 and [sqlite3_fr
1b980 65 65 28 29 5d 20 66 6f 72 20 61 6c 6c 20 6f 66 ee()] for all of
1b990 20 69 74 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f its memory allo
1b9a0 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20 cation and.**
1b9b0 20 20 20 20 20 20 20 20 64 65 61 6c 6c 6f 63 61 dealloca
1b9c0 74 69 6f 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a tion needs..**.*
1b9d0 2a 20 7b 48 31 37 33 31 38 7d 20 20 54 68 65 20 * {H17318} The
1b9e0 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
1b9f0 28 50 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 (P,N)] interface
1ba00 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 20 returns either
1ba10 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20 a pointer.**
1ba20 20 20 20 20 20 20 20 74 6f 20 61 20 62 6c 6f 63 to a bloc
1ba30 6b 20 6f 66 20 63 68 65 63 6b 65 64 2d 6f 75 74 k of checked-out
1ba40 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 memory of at le
1ba50 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 73 ast N bytes in s
1ba60 69 7a 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ize.**
1ba70 20 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20 that is 8-byte
1ba80 61 6c 69 67 6e 65 64 2c 20 6f 72 20 61 20 4e 55 aligned, or a NU
1ba90 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
1baa0 2a 20 7b 48 31 37 33 32 31 7d 20 20 57 68 65 6e * {H17321} When
1bab0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f [sqlite3_reallo
1bac0 63 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20 c(P,N)] returns
1bad0 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 a non-NULL point
1bae0 65 72 2c 20 69 74 20 66 69 72 73 74 0a 2a 2a 20 er, it first.**
1baf0 20 20 20 20 20 20 20 20 20 20 63 6f 70 69 65 73 copies
1bb00 20 74 68 65 20 66 69 72 73 74 20 4b 20 62 79 74 the first K byt
1bb10 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 66 72 es of content fr
1bb20 6f 6d 20 50 20 69 6e 74 6f 20 74 68 65 20 6e 65 om P into the ne
1bb30 77 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 wly.**
1bb40 20 61 6c 6c 6f 63 61 74 65 64 20 62 6c 6f 63 6b allocated block
1bb50 2c 20 77 68 65 72 65 20 4b 20 69 73 20 74 68 65 , where K is the
1bb60 20 6c 65 73 73 65 72 20 6f 66 20 4e 20 61 6e 64 lesser of N and
1bb70 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 the size of.**
1bb80 20 20 20 20 20 20 20 20 20 20 74 68 65 20 62 75 the bu
1bb90 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ffer P..**.** {H
1bba0 31 37 33 32 32 7d 20 20 57 68 65 6e 20 5b 73 71 17322} When [sq
1bbb0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 50 2c lite3_realloc(P,
1bbc0 4e 29 5d 20 72 65 74 75 72 6e 73 20 61 20 6e 6f N)] returns a no
1bbd0 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 n-NULL pointer,
1bbe0 69 74 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 it first.**
1bbf0 20 20 20 20 20 20 72 65 6c 65 61 73 65 73 20 74 releases t
1bc00 68 65 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a he buffer P..**.
1bc10 2a 2a 20 7b 48 31 37 33 32 33 7d 20 20 57 68 65 ** {H17323} Whe
1bc20 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c n [sqlite3_reall
1bc30 6f 63 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 oc(P,N)] returns
1bc40 20 4e 55 4c 4c 2c 20 74 68 65 20 62 75 66 66 65 NULL, the buffe
1bc50 72 20 50 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 r P is.**
1bc60 20 20 20 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 not modified
1bc70 20 6f 72 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a or released..**
1bc80 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a .** ASSUMPTIONS:
1bc90 0a 2a 2a 0a 2a 2a 20 7b 41 31 37 33 35 30 7d 20 .**.** {A17350}
1bca0 20 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67 The pointer arg
1bcb0 75 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 uments to [sqlit
1bcc0 65 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b e3_free()] and [
1bcd0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
1bce0 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 )].**
1bcf0 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e must be either N
1bd00 55 4c 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e ULL or else poin
1bd10 74 65 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72 ters obtained fr
1bd20 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 20 20 om a prior.**
1bd30 20 20 20 20 20 20 20 20 69 6e 76 6f 63 61 74 69 invocati
1bd40 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d on of [sqlite3_m
1bd50 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c alloc()] or [sql
1bd60 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 ite3_realloc()]
1bd70 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 that have.**
1bd80 20 20 20 20 20 20 20 6e 6f 74 20 79 65 74 20 62 not yet b
1bd90 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a een released..**
1bda0 0a 2a 2a 20 7b 41 31 37 33 35 31 7d 20 20 54 68 .** {A17351} Th
1bdb0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 e application mu
1bdc0 73 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77 st not read or w
1bdd0 72 69 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66 rite any part of
1bde0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 20 .** a
1bdf0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 block of memory
1be00 61 66 74 65 72 20 69 74 20 68 61 73 20 62 65 65 after it has bee
1be10 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 n released using
1be20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 .** [s
1be30 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f qlite3_free()] o
1be40 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c r [sqlite3_reall
1be50 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 oc()]..*/.SQLITE
1be60 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
1be70 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a e3_malloc(int);.
1be80 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1be90 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 *sqlite3_realloc
1bea0 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 (void*, int);.SQ
1beb0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1bec0 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 2a lite3_free(void*
1bed0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
1bee0 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 EF: Memory Alloc
1bef0 61 74 6f 72 20 53 74 61 74 69 73 74 69 63 73 20 ator Statistics
1bf00 7b 48 31 37 33 37 30 7d 20 3c 53 33 30 32 31 30 {H17370} <S30210
1bf10 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 >.**.** SQLite p
1bf20 72 6f 76 69 64 65 73 20 74 68 65 73 65 20 74 77 rovides these tw
1bf30 6f 20 69 6e 74 65 72 66 61 63 65 73 20 66 6f 72 o interfaces for
1bf40 20 72 65 70 6f 72 74 69 6e 67 20 6f 6e 20 74 68 reporting on th
1bf50 65 20 73 74 61 74 75 73 0a 2a 2a 20 6f 66 20 74 e status.** of t
1bf60 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c he [sqlite3_mall
1bf70 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f oc()], [sqlite3_
1bf80 66 72 65 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 free()], and [sq
1bf90 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d lite3_realloc()]
1bfa0 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2c 20 77 68 .** routines, wh
1bfb0 69 63 68 20 66 6f 72 6d 20 74 68 65 20 62 75 69 ich form the bui
1bfc0 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c lt-in memory all
1bfd0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
1bfe0 6d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 m..**.** INVARIA
1bff0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 NTS:.**.** {H173
1c000 37 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 71} The [sqlite3
1c010 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20 _memory_used()]
1c020 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
1c030 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
1c040 74 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 tes.**
1c050 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e of memory curren
1c060 74 6c 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 tly outstanding
1c070 28 6d 61 6c 6c 6f 63 65 64 20 62 75 74 20 6e 6f (malloced but no
1c080 74 20 66 72 65 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 t freed)..**.**
1c090 7b 48 31 37 33 37 33 7d 20 54 68 65 20 5b 73 71 {H17373} The [sq
1c0a0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 lite3_memory_hig
1c0b0 68 77 61 74 65 72 28 29 5d 20 72 6f 75 74 69 6e hwater()] routin
1c0c0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6d 61 e returns the ma
1c0d0 78 69 6d 75 6d 0a 2a 2a 20 20 20 20 20 20 20 20 ximum.**
1c0e0 20 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 value of [sqli
1c0f0 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 te3_memory_used(
1c100 29 5d 20 73 69 6e 63 65 20 74 68 65 20 68 69 67 )] since the hig
1c110 68 2d 77 61 74 65 72 20 6d 61 72 6b 0a 2a 2a 20 h-water mark.**
1c120 20 20 20 20 20 20 20 20 20 77 61 73 20 6c 61 73 was las
1c130 74 20 72 65 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 7b t reset..**.** {
1c140 48 31 37 33 37 34 7d 20 54 68 65 20 76 61 6c 75 H17374} The valu
1c150 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b es returned by [
1c160 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 sqlite3_memory_u
1c170 73 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 20 sed()] and.**
1c180 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
1c190 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 memory_highwater
1c1a0 28 29 5d 20 69 6e 63 6c 75 64 65 20 61 6e 79 20 ()] include any
1c1b0 6f 76 65 72 68 65 61 64 0a 2a 2a 20 20 20 20 20 overhead.**
1c1c0 20 20 20 20 20 61 64 64 65 64 20 62 79 20 53 51 added by SQ
1c1d0 4c 69 74 65 20 69 6e 20 69 74 73 20 69 6d 70 6c Lite in its impl
1c1e0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 5b 73 ementation of [s
1c1f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
1c200 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75 ,.** bu
1c210 74 20 6e 6f 74 20 6f 76 65 72 68 65 61 64 20 61 t not overhead a
1c220 64 64 65 64 20 62 79 20 74 68 65 20 61 6e 79 20 dded by the any
1c230 75 6e 64 65 72 6c 79 69 6e 67 20 73 79 73 74 65 underlying syste
1c240 6d 20 6c 69 62 72 61 72 79 0a 2a 2a 20 20 20 20 m library.**
1c250 20 20 20 20 20 20 72 6f 75 74 69 6e 65 73 20 74 routines t
1c260 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c hat [sqlite3_mal
1c270 6c 6f 63 28 29 5d 20 6d 61 79 20 63 61 6c 6c 2e loc()] may call.
1c280 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 37 35 7d 20 .**.** {H17375}
1c290 54 68 65 20 6d 65 6d 6f 72 79 20 68 69 67 68 2d The memory high-
1c2a0 77 61 74 65 72 20 6d 61 72 6b 20 69 73 20 72 65 water mark is re
1c2b0 73 65 74 20 74 6f 20 74 68 65 20 63 75 72 72 65 set to the curre
1c2c0 6e 74 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 20 nt value of.**
1c2d0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
1c2e0 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20 _memory_used()]
1c2f0 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 if and only if t
1c300 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a he parameter to.
1c310 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
1c320 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 ite3_memory_high
1c330 77 61 74 65 72 28 29 5d 20 69 73 20 74 72 75 65 water()] is true
1c340 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 . The value ret
1c350 75 72 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 urned.**
1c360 20 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6d 65 by [sqlite3_me
1c370 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 31 mory_highwater(1
1c380 29 5d 20 69 73 20 74 68 65 20 68 69 67 68 2d 77 )] is the high-w
1c390 61 74 65 72 20 6d 61 72 6b 0a 2a 2a 20 20 20 20 ater mark.**
1c3a0 20 20 20 20 20 20 70 72 69 6f 72 20 74 6f 20 74 prior to t
1c3b0 68 65 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c he reset..*/.SQL
1c3c0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
1c3d0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 int64 sqlite3_me
1c3e0 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b mory_used(void);
1c3f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
1c400 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
1c410 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 3_memory_highwat
1c420 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 er(int resetFlag
1c430 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
1c440 45 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f EF: Pseudo-Rando
1c450 6d 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 m Number Generat
1c460 6f 72 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30 or {H17390} <S20
1c470 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 000>.**.** SQLit
1c480 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 e contains a hig
1c490 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f h-quality pseudo
1c4a0 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 -random number g
1c4b0 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 enerator (PRNG)
1c4c0 75 73 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 used to.** selec
1c4d0 74 20 72 61 6e 64 6f 6d 20 52 4f 57 49 44 73 20 t random ROWIDs
1c4e0 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e when inserting n
1c4f0 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 ew records into
1c500 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 a table that.**
1c510 61 6c 72 65 61 64 79 20 75 73 65 73 20 74 68 65 already uses the
1c520 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c largest possibl
1c530 65 20 52 4f 57 49 44 2e 20 20 54 68 65 20 50 52 e ROWID. The PR
1c540 4e 47 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 NG is also used
1c550 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 for.** the build
1c560 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e 64 -in random() and
1c570 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53 51 randomblob() SQ
1c580 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 L functions. Th
1c590 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c is interface all
1c5a0 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 ows.** applicati
1c5b0 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 ons to access th
1c5c0 65 20 73 61 6d 65 20 50 52 4e 47 20 66 6f 72 20 e same PRNG for
1c5d0 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a other purposes..
1c5e0 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 **.** A call to
1c5f0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f this routine sto
1c600 72 65 73 20 4e 20 62 79 74 65 73 20 6f 66 20 72 res N bytes of r
1c610 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 62 andomness into b
1c620 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54 uffer P..**.** T
1c630 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 he first time th
1c640 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e is routine is in
1c650 76 6f 6b 65 64 20 28 65 69 74 68 65 72 20 69 6e voked (either in
1c660 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a 2a ternally or by.*
1c670 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f * the applicatio
1c680 6e 29 20 74 68 65 20 50 52 4e 47 20 69 73 20 73 n) the PRNG is s
1c690 65 65 64 65 64 20 75 73 69 6e 67 20 72 61 6e 64 eeded using rand
1c6a0 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 0a omness obtained.
1c6b0 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 61 6e ** from the xRan
1c6c0 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f domness method o
1c6d0 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 f the default [s
1c6e0 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
1c6f0 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75 ct..** On all su
1c700 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 bsequent invocat
1c710 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75 64 6f ions, the pseudo
1c720 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20 67 -randomness is g
1c730 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74 65 enerated.** inte
1c740 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74 68 6f rnally and witho
1c750 75 74 20 72 65 63 6f 75 72 73 65 20 74 6f 20 74 ut recourse to t
1c760 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d he [sqlite3_vfs]
1c770 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 xRandomness.**
1c780 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e method..**.** IN
1c790 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 VARIANTS:.**.**
1c7a0 7b 48 31 37 33 39 32 7d 20 54 68 65 20 5b 73 71 {H17392} The [sq
1c7b0 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
1c7c0 28 4e 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65 (N,P)] interface
1c7d0 20 77 72 69 74 65 73 20 4e 20 62 79 74 65 73 20 writes N bytes
1c7e0 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68 of.** h
1c7f0 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 igh-quality pseu
1c800 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e do-randomness in
1c810 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2f 0a to buffer P..*/.
1c820 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1c830 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
1c840 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a ss(int N, void *
1c850 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 P);../*.** CAPI3
1c860 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d REF: Compile-Tim
1c870 65 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 e Authorization
1c880 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 Callbacks {H1250
1c890 30 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S70100>.**.*
1c8a0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
1c8b0 65 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f egisters a autho
1c8c0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 rizer callback w
1c8d0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 ith a particular
1c8e0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f .** [database co
1c8f0 6e 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c nnection], suppl
1c900 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 ied in the first
1c910 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 argument..** Th
1c920 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
1c930 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
1c940 20 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e as SQL statemen
1c950 74 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d ts are being com
1c960 70 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c piled.** by [sql
1c970 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 ite3_prepare()]
1c980 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 or its variants
1c990 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
1c9a0 5f 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 _v2()],.** [sqli
1c9b0 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d te3_prepare16()]
1c9c0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 and [sqlite3_pr
1c9d0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 epare16_v2()].
1c9e0 41 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f At various.** po
1c9f0 69 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 ints during the
1ca00 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 compilation proc
1ca10 65 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 ess, as logic is
1ca20 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a being created.*
1ca30 2a 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 * to perform var
1ca40 69 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 ious actions, th
1ca50 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
1ca60 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
1ca70 20 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 to.** see if th
1ca80 6f 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 ose actions are
1ca90 61 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 allowed. The au
1caa0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1cab0 6b 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 k should.** retu
1cac0 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 rn [SQLITE_OK] t
1cad0 6f 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 o allow the acti
1cae0 6f 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f on, [SQLITE_IGNO
1caf0 52 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 RE] to disallow
1cb00 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 the.** specific
1cb10 61 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 action but allow
1cb20 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
1cb30 6e 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 nt to continue t
1cb40 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 o be.** compiled
1cb50 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e , or [SQLITE_DEN
1cb60 59 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 Y] to cause the
1cb70 65 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 entire SQL state
1cb80 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 ment to be.** re
1cb90 6a 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 jected with an e
1cba0 72 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 rror. If the au
1cbb0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1cbc0 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 k returns.** any
1cbd0 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 value other tha
1cbe0 6e 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 n [SQLITE_IGNORE
1cbf0 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 ], [SQLITE_OK],
1cc00 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d or [SQLITE_DENY]
1cc10 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 .** then the [sq
1cc20 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1cc30 28 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e ()] or equivalen
1cc40 74 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 t call that trig
1cc50 67 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 gered.** the aut
1cc60 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 horizer will fai
1cc70 6c 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 l with an error
1cc80 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 message..**.** W
1cc90 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b hen the callback
1cca0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
1ccb0 5f 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 _OK], that means
1ccc0 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a the operation.*
1ccd0 2a 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f * requested is o
1cce0 6b 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c k. When the cal
1ccf0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 lback returns [S
1cd00 51 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 QLITE_DENY], the
1cd10 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
1cd20 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 pare_v2()] or eq
1cd30 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 uivalent call th
1cd40 61 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 at triggered the
1cd50 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 .** authorizer w
1cd60 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e ill fail with an
1cd70 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 error message e
1cd80 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a xplaining that.*
1cd90 2a 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 * access is deni
1cda0 65 64 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 ed. If the auth
1cdb0 6f 72 69 7a 65 72 20 63 6f 64 65 20 69 73 20 5b orizer code is [
1cdc0 53 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 SQLITE_READ].**
1cdd0 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b and the callback
1cde0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
1cdf0 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 _IGNORE] then th
1ce00 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 e.** [prepared s
1ce10 74 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d tatement] statem
1ce20 65 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 ent is construct
1ce30 65 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 ed to substitute
1ce40 0a 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 .** a NULL value
1ce50 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 in place of the
1ce60 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 table column th
1ce70 61 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a at would have.**
1ce80 20 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 been read if [S
1ce90 51 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 QLITE_OK] had be
1cea0 65 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 en returned. Th
1ceb0 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 e [SQLITE_IGNORE
1cec0 5d 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 ].** return can
1ced0 62 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 be used to deny
1cee0 61 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 an untrusted use
1cef0 72 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 r access to indi
1cf00 76 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e vidual.** column
1cf10 73 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a s of a table..**
1cf20 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 .** The first pa
1cf30 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 rameter to the a
1cf40 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
1cf50 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 ck is a copy of
1cf60 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 the third.** par
1cf70 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 ameter to the sq
1cf80 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 lite3_set_author
1cf90 69 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 izer() interface
1cfa0 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 . The second par
1cfb0 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ameter.** to the
1cfc0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 callback is an
1cfd0 69 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f integer [SQLITE_
1cfe0 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f COPY | action co
1cff0 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 de] that specifi
1d000 65 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 es.** the partic
1d010 75 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 ular action to b
1d020 65 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 e authorized. Th
1d030 65 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 e third through
1d040 73 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 sixth parameters
1d050 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 .** to the callb
1d060 61 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 ack are zero-ter
1d070 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 minated strings
1d080 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 that contain add
1d090 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 itional.** detai
1d0a0 6c 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 ls about the act
1d0b0 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 ion to be author
1d0c0 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 ized..**.** An a
1d0d0 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 uthorizer is use
1d0e0 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f d when [sqlite3_
1d0f0 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 prepare | prepar
1d100 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 ing].** SQL stat
1d110 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 ements from an u
1d120 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c ntrusted source,
1d130 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 to ensure that
1d140 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
1d150 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 ts.** do not try
1d160 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 to access data
1d170 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c they are not all
1d180 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 owed to see, or
1d190 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 that they do not
1d1a0 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 .** try to execu
1d1b0 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 te malicious sta
1d1c0 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d tements that dam
1d1d0 61 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 age the database
1d1e0 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c . For.** exampl
1d1f0 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f e, an applicatio
1d200 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 n may allow a us
1d210 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 er to enter arbi
1d220 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 trary.** SQL que
1d230 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 ries for evaluat
1d240 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 ion by a databas
1d250 65 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c e. But the appl
1d260 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 ication does.**
1d270 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 not want the use
1d280 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 r to be able to
1d290 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 make arbitrary c
1d2a0 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a hanges to the.**
1d2b0 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 database. An a
1d2c0 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 uthorizer could
1d2d0 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 then be put in p
1d2e0 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a lace while the.*
1d2f0 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 * user-entered S
1d300 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c QL is being [sql
1d310 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 ite3_prepare | p
1d320 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a repared] that.**
1d330 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 disallows every
1d340 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 thing except [SE
1d350 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 LECT] statements
1d360 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 ..**.** Applicat
1d370 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 ions that need t
1d380 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 o process SQL fr
1d390 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 om untrusted sou
1d3a0 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c rces.** might al
1d3b0 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 so consider lowe
1d3c0 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 ring resource li
1d3d0 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 mits using [sqli
1d3e0 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 te3_limit()].**
1d3f0 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 and limiting dat
1d400 61 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 abase size using
1d410 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 the [max_page_c
1d420 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a ount] [PRAGMA].*
1d430 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f * in addition to
1d440 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 using an author
1d450 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 izer..**.** Only
1d460 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 a single author
1d470 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 izer can be in p
1d480 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 lace on a databa
1d490 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a se connection.**
1d4a0 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 at a time. Eac
1d4b0 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 h call to sqlite
1d4c0 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
1d4d0 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a overrides the.*
1d4e0 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e * previous call.
1d4f0 20 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 Disable the au
1d500 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 thorizer by inst
1d510 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 alling a NULL ca
1d520 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 llback..** The a
1d530 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 uthorizer is dis
1d540 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
1d550 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 ..**.** The auth
1d560 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
1d570 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 must not do anyt
1d580 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d hing that will m
1d590 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 odify.** the dat
1d5a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1d5b0 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 that invoked th
1d5c0 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
1d5d0 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 lback..** Note t
1d5e0 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 hat [sqlite3_pre
1d5f0 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b pare_v2()] and [
1d600 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
1d610 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 both modify thei
1d620 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f r.** database co
1d630 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 nnections for th
1d640 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f e meaning of "mo
1d650 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 dify" in this pa
1d660 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 ragraph..**.** W
1d670 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 hen [sqlite3_pre
1d680 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 pare_v2()] is us
1d690 65 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 ed to prepare a
1d6a0 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a statement, the.*
1d6b0 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 * statement migh
1d6c0 74 20 62 65 20 72 65 70 72 65 70 61 72 65 64 20 t be reprepared
1d6d0 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f during [sqlite3_
1d6e0 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61 step()] due to a
1d6f0 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e .** schema chan
1d700 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 ge. Hence, the
1d710 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 application shou
1d720 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 ld ensure that t
1d730 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 he.** correct au
1d740 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1d750 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 k remains in pla
1d760 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 ce during the [s
1d770 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a qlite3_step()]..
1d780 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
1d790 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 the authorizer c
1d7a0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b allback is invok
1d7b0 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a ed only during.*
1d7c0 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
1d7d0 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 re()] or its var
1d7e0 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a iants. Authoriz
1d7f0 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 ation is not.**
1d800 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67 performed during
1d810 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75 statement evalu
1d820 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65 ation in [sqlite
1d830 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 3_step()]..**.**
1d840 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
1d850 2a 2a 20 7b 48 31 32 35 30 31 7d 20 54 68 65 20 ** {H12501} The
1d860 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 [sqlite3_set_aut
1d870 68 6f 72 69 7a 65 72 28 44 2c 2e 2e 2e 29 5d 20 horizer(D,...)]
1d880 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
1d890 65 72 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 ers a.**
1d8a0 20 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c authorizer cal
1d8b0 6c 62 61 63 6b 20 77 69 74 68 20 64 61 74 61 62 lback with datab
1d8c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 ase connection D
1d8d0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 32 7d ..**.** {H12502}
1d8e0 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 The authorizer
1d8f0 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f callback is invo
1d900 6b 65 64 20 61 73 20 53 51 4c 20 73 74 61 74 65 ked as SQL state
1d910 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 20 20 20 ments are.**
1d920 20 20 20 20 20 20 62 65 69 6e 67 20 70 61 72 73 being pars
1d930 65 65 64 20 61 6e 64 20 63 6f 6d 70 69 6c 65 64 eed and compiled
1d940 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 33 7d ..**.** {H12503}
1d950 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 7a If the authoriz
1d960 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 er callback retu
1d970 72 6e 73 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 rns any value ot
1d980 68 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 her than.**
1d990 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 4e [SQLITE_IGN
1d9a0 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b ORE], [SQLITE_OK
1d9b0 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 ], or [SQLITE_DE
1d9c0 4e 59 5d 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 NY], then.**
1d9d0 20 20 20 20 20 20 74 68 65 20 61 70 70 6c 69 63 the applic
1d9e0 61 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 20 ation interface
1d9f0 63 61 6c 6c 20 74 68 61 74 20 63 61 75 73 65 64 call that caused
1da00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
1da10 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1da20 62 61 63 6b 20 74 6f 20 72 75 6e 20 73 68 61 6c back to run shal
1da30 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a l fail with an.*
1da40 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 * [SQLI
1da50 54 45 5f 45 52 52 4f 52 5d 20 65 72 72 6f 72 20 TE_ERROR] error
1da60 63 6f 64 65 20 61 6e 64 20 61 6e 20 61 70 70 72 code and an appr
1da70 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 opriate error me
1da80 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ssage..**.** {H1
1da90 32 35 30 34 7d 20 57 68 65 6e 20 74 68 65 20 61 2504} When the a
1daa0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
1dab0 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 ck returns [SQLI
1dac0 54 45 5f 4f 4b 5d 2c 20 74 68 65 20 6f 70 65 72 TE_OK], the oper
1dad0 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ation.**
1dae0 20 20 64 65 73 63 72 69 62 65 64 20 69 73 20 70 described is p
1daf0 72 6f 63 65 73 73 65 64 20 6e 6f 72 6d 61 6c 6c rocessed normall
1db00 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 35 y..**.** {H12505
1db10 7d 20 57 68 65 6e 20 74 68 65 20 61 75 74 68 6f } When the autho
1db20 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 rizer callback r
1db30 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 eturns [SQLITE_D
1db40 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 20 20 20 ENY], the.**
1db50 20 20 20 20 20 20 61 70 70 6c 69 63 61 74 69 6f applicatio
1db60 6e 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c n interface call
1db70 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 that caused the
1db80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 .** aut
1db90 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1dba0 20 74 6f 20 72 75 6e 20 73 68 61 6c 6c 20 66 61 to run shall fa
1dbb0 69 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 il.** w
1dbc0 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 45 ith an [SQLITE_E
1dbd0 52 52 4f 52 5d 20 65 72 72 6f 72 20 63 6f 64 65 RROR] error code
1dbe0 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 and an error me
1dbf0 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 ssage.**
1dc00 20 20 65 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 explaining tha
1dc10 74 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 t access is deni
1dc20 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 ed..**.** {H1250
1dc30 36 7d 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 6} If the author
1dc40 69 7a 65 72 20 63 6f 64 65 20 28 74 68 65 20 32 izer code (the 2
1dc50 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
1dc60 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 0a 2a the authorizer.*
1dc70 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 * callb
1dc80 61 63 6b 29 20 69 73 20 5b 53 51 4c 49 54 45 5f ack) is [SQLITE_
1dc90 52 45 41 44 5d 20 61 6e 64 20 74 68 65 20 61 75 READ] and the au
1dca0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1dcb0 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 k returns.**
1dcc0 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 [SQLITE_IG
1dcd0 4e 4f 52 45 5d 2c 20 74 68 65 6e 20 74 68 65 20 NORE], then the
1dce0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1dcf0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 nt is constructe
1dd00 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 d to.**
1dd10 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 76 insert a NULL v
1dd20 61 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 6f 66 alue in place of
1dd30 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d the table colum
1dd40 6e 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 n that would hav
1dd50 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 65 e.** be
1dd60 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51 4c 49 en read if [SQLI
1dd70 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20 TE_OK] had been
1dd80 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
1dd90 7b 48 31 32 35 30 37 7d 20 49 66 20 74 68 65 20 {H12507} If the
1dda0 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 64 65 20 authorizer code
1ddb0 28 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 (the 2nd paramet
1ddc0 65 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 er to the author
1ddd0 69 7a 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 izer.**
1dde0 20 63 61 6c 6c 62 61 63 6b 29 20 69 73 20 61 6e callback) is an
1ddf0 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 ything other tha
1de00 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 2c n [SQLITE_READ],
1de10 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 then.**
1de20 20 20 61 20 72 65 74 75 72 6e 20 6f 66 20 5b 53 a return of [S
1de30 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 68 61 QLITE_IGNORE] ha
1de40 73 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 s the same effec
1de50 74 20 61 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e t as [SQLITE_DEN
1de60 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 31 Y]..**.** {H1251
1de70 30 7d 20 54 68 65 20 66 69 72 73 74 20 70 61 72 0} The first par
1de80 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 ameter to the au
1de90 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1dea0 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a k is a copy of.*
1deb0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74 * the t
1dec0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 hird parameter t
1ded0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 o the [sqlite3_s
1dee0 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d et_authorizer()]
1def0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a interface..**.*
1df00 2a 20 7b 48 31 32 35 31 31 7d 20 54 68 65 20 73 * {H12511} The s
1df10 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
1df20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 to the callback
1df30 69 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a is an integer.**
1df40 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 [SQLIT
1df50 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 E_COPY | action
1df60 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 code] that speci
1df70 66 69 65 73 20 74 68 65 20 70 61 72 74 69 63 75 fies the particu
1df80 6c 61 72 20 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 lar action.**
1df90 20 20 20 20 20 20 20 74 6f 20 62 65 20 61 75 74 to be aut
1dfa0 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b horized..**.** {
1dfb0 48 31 32 35 31 32 7d 20 54 68 65 20 74 68 69 72 H12512} The thir
1dfc0 64 20 74 68 72 6f 75 67 68 20 73 69 78 74 68 20 d through sixth
1dfd0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 parameters to th
1dfe0 65 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 0a 2a e callback are.*
1dff0 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 2d * zero-
1e000 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e terminated strin
1e010 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 0a gs that contain.
1e020 2a 2a 20 20 20 20 20 20 20 20 20 20 61 64 64 69 ** addi
1e030 74 69 6f 6e 61 6c 20 64 65 74 61 69 6c 73 20 61 tional details a
1e040 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20 bout the action
1e050 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 to be authorized
1e060 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 30 7d ..**.** {H12520}
1e070 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 Each call to [s
1e080 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
1e090 72 69 7a 65 72 28 29 5d 20 6f 76 65 72 72 69 64 rizer()] overrid
1e0a0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 es.** a
1e0b0 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e ny previously in
1e0c0 73 74 61 6c 6c 65 64 20 61 75 74 68 6f 72 69 7a stalled authoriz
1e0d0 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 er..**.** {H1252
1e0e0 31 7d 20 41 20 4e 55 4c 4c 20 61 75 74 68 6f 72 1} A NULL author
1e0f0 69 7a 65 72 20 6d 65 61 6e 73 20 74 68 61 74 20 izer means that
1e100 6e 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e no authorization
1e110 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c .** cal
1e120 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
1e130 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 32 7d ..**.** {H12522}
1e140 20 54 68 65 20 64 65 66 61 75 6c 74 20 61 75 74 The default aut
1e150 68 6f 72 69 7a 65 72 20 69 73 20 4e 55 4c 4c 2e horizer is NULL.
1e160 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1e170 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 nt sqlite3_set_a
1e180 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c uthorizer(. sql
1e190 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78 ite3*,. int (*x
1e1a0 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c Auth)(void*,int,
1e1b0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
1e1c0 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 t char*,const ch
1e1d0 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 ar*,const char*)
1e1e0 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 ,. void *pUserD
1e1f0 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ata.);../*.** CA
1e200 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a PI3REF: Authoriz
1e210 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20 er Return Codes
1e220 7b 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30 {H12590} <H12500
1e230 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c >.**.** The [sql
1e240 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 ite3_set_authori
1e250 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 zer | authorizer
1e260 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
1e270 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 on] must.** retu
1e280 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 rn either [SQLIT
1e290 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 E_OK] or one of
1e2a0 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 these two consta
1e2b0 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 nts in order.**
1e2c0 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 to signal SQLite
1e2d0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1e2e0 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 the action is pe
1e2f0 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 rmitted. See th
1e300 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 e.** [sqlite3_se
1e310 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 t_authorizer | a
1e320 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 uthorizer docume
1e330 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 ntation] for add
1e340 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 itional.** infor
1e350 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 mation..*/.#defi
1e360 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 ne SQLITE_DENY
1e370 20 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 1 /* Abort th
1e380 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
1e390 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f with an error */
1e3a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1e3b0 49 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f IGNORE 2 /* Do
1e3c0 6e 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 n't allow access
1e3d0 2c 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 , but don't gene
1e3e0 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f rate an error */
1e3f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
1e400 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 : Authorizer Act
1e410 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35 ion Codes {H1255
1e420 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 0} <H12500>.**.*
1e430 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 * The [sqlite3_s
1e440 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d et_authorizer()]
1e450 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 interface regis
1e460 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 ters a callback
1e470 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 function.** that
1e480 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61 is invoked to a
1e490 75 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e uthorize certain
1e4a0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 SQL statement a
1e4b0 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20 ctions. The.**
1e4c0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
1e4d0 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b to the callback
1e4e0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 is an integer c
1e4f0 6f 64 65 20 74 68 61 74 20 73 70 65 63 69 66 69 ode that specifi
1e500 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f es.** what actio
1e510 6e 20 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f n is being autho
1e520 72 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72 rized. These ar
1e530 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 61 63 e the integer ac
1e540 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a tion codes that.
1e550 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 ** the authorize
1e560 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62 r callback may b
1e570 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 e passed..**.**
1e580 54 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 These action cod
1e590 65 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79 e values signify
1e5a0 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 what kind of op
1e5b0 65 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 eration is to be
1e5c0 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 .** authorized.
1e5d0 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 The 3rd and 4th
1e5e0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 parameters to t
1e5f0 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e he authorization
1e600 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e .** callback fun
1e610 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 ction will be pa
1e620 72 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c rameters or NULL
1e630 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 depending on wh
1e640 69 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 ich of these.**
1e650 63 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73 codes is used as
1e660 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
1e670 6d 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 20 meter. The 5th
1e680 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
1e690 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63 .** authorizer c
1e6a0 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e allback is the n
1e6b0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
1e6c0 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 ase ("main", "te
1e6d0 6d 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66 mp",.** etc.) if
1e6e0 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68 applicable. Th
1e6f0 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 6th parameter
1e700 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 to the authorize
1e710 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 r callback.** is
1e720 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1e730 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67 inner-most trig
1e740 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 74 ger or view that
1e750 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
1e760 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73 for.** the acces
1e770 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c s attempt or NUL
1e780 4c 20 69 66 20 74 68 69 73 20 61 63 63 65 73 73 L if this access
1e790 20 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 65 attempt is dire
1e7a0 63 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70 ctly from.** top
1e7b0 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e -level SQL code.
1e7c0 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 .**.** INVARIANT
1e7d0 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 31 S:.**.** {H12551
1e7e0 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 } The second par
1e7f0 61 6d 65 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 ameter to an.**
1e800 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
1e810 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
1e820 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 | authorizer ca
1e830 6c 6c 62 61 63 6b 5d 20 73 68 61 6c 6c 20 62 65 llback] shall be
1e840 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 an integer.**
1e850 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f [SQLITE_
1e860 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 7a 65 COPY | authorize
1e870 72 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 r code] that spe
1e880 63 69 66 69 65 73 20 77 68 61 74 20 61 63 74 69 cifies what acti
1e890 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 on.** i
1e8a0 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72 69 7a s being authoriz
1e8b0 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 ed..**.** {H1255
1e8c0 32 7d 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 2} The 3rd and 4
1e8d0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f th parameters to
1e8e0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
1e8f0 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 [sqlite3_set_au
1e900 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f thorizer | autho
1e910 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 rization callbac
1e920 6b 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 k].** s
1e930 68 61 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 hall be paramete
1e940 72 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e rs or NULL depen
1e950 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 0a 2a 2a ding on which.**
1e960 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 [SQLIT
1e970 45 5f 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 E_COPY | authori
1e980 7a 65 72 20 63 6f 64 65 5d 20 69 73 20 75 73 65 zer code] is use
1e990 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
1e9a0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a parameter..**.**
1e9b0 20 7b 48 31 32 35 35 33 7d 20 54 68 65 20 35 74 {H12553} The 5t
1e9c0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
1e9d0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
1e9e0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
1e9f0 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 orizer | authori
1ea00 7a 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68 zer callback] sh
1ea10 61 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a all be the name.
1ea20 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 ** of t
1ea30 68 65 20 64 61 74 61 62 61 73 65 20 28 65 78 61 he database (exa
1ea40 6d 70 6c 65 3a 20 22 6d 61 69 6e 22 2c 20 22 74 mple: "main", "t
1ea50 65 6d 70 22 2c 20 65 74 63 2e 29 20 69 66 20 61 emp", etc.) if a
1ea60 70 70 6c 69 63 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a pplicable..**.**
1ea70 20 7b 48 31 32 35 35 34 7d 20 54 68 65 20 36 74 {H12554} The 6t
1ea80 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
1ea90 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
1eaa0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
1eab0 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 orizer | authori
1eac0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68 zer callback] sh
1ead0 61 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a all be the name.
1eae0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 ** of t
1eaf0 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 he inner-most tr
1eb00 69 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 igger or view th
1eb10 61 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c at is responsibl
1eb20 65 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 e for.**
1eb30 20 20 74 68 65 20 61 63 63 65 73 73 20 61 74 74 the access att
1eb40 65 6d 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 empt or NULL if
1eb50 74 68 69 73 20 61 63 63 65 73 73 20 61 74 74 65 this access atte
1eb60 6d 70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 mpt is directly
1eb70 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 from.**
1eb80 20 74 6f 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 top-level SQL c
1eb90 6f 64 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ode..*/./*******
1eba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ebb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ebc0 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a **** 3rd *******
1ebd0 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a ***** 4th ******
1ebe0 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 *****/.#define S
1ebf0 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 QLITE_CREATE_IND
1ec00 45 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20 EX 1
1ec10 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 /* Index Name
1ec20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
1ec30 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1ec40 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c LITE_CREATE_TABL
1ec50 45 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f E 2 /
1ec60 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 * Table Name
1ec70 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1ec80 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1ec90 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f ITE_CREATE_TEMP_
1eca0 49 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a INDEX 3 /*
1ecb0 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 Index Name
1ecc0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1ecd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1ece0 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 TE_CREATE_TEMP_T
1ecf0 41 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 ABLE 4 /*
1ed00 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1ed10 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
1ed20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1ed30 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 E_CREATE_TEMP_TR
1ed40 49 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 IGGER 5 /* T
1ed50 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 rigger Name T
1ed60 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a able Name *
1ed70 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1ed80 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 _CREATE_TEMP_VIE
1ed90 57 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 W 6 /* Vi
1eda0 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 ew Name NU
1edb0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1edc0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1edd0 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 CREATE_TRIGGER
1ede0 20 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 7 /* Tri
1edf0 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 gger Name Tab
1ee00 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a le Name */.
1ee10 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
1ee20 52 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 REATE_VIEW
1ee30 20 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 8 /* View
1ee40 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c Name NULL
1ee50 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1ee60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
1ee70 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 LETE
1ee80 20 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 9 /* Table
1ee90 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
1eea0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1eeb0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f efine SQLITE_DRO
1eec0 50 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 P_INDEX
1eed0 20 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 10 /* Index
1eee0 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 Name Table
1eef0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
1ef00 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
1ef10 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 _TABLE
1ef20 20 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 11 /* Table N
1ef30 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 ame NULL
1ef40 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1ef50 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
1ef60 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 TEMP_INDEX
1ef70 31 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 12 /* Index Na
1ef80 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 me Table Na
1ef90 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 me */.#defi
1efa0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 ne SQLITE_DROP_T
1efb0 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 EMP_TABLE 1
1efc0 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 3 /* Table Nam
1efd0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
1efe0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
1eff0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 e SQLITE_DROP_TE
1f000 4d 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 MP_TRIGGER 14
1f010 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 /* Trigger Na
1f020 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 me Table Name
1f030 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1f040 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
1f050 50 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20 P_VIEW 15
1f060 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 /* View Name
1f070 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 NULL
1f080 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1f090 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 SQLITE_DROP_TRIG
1f0a0 47 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20 GER 16
1f0b0 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 /* Trigger Name
1f0c0 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 Table Name
1f0d0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1f0e0 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 QLITE_DROP_VIEW
1f0f0 20 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20 17
1f100 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 /* View Name
1f110 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
1f120 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1f130 4c 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 LITE_INSERT
1f140 20 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 18 /
1f150 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 * Table Name
1f160 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1f170 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1f180 49 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 ITE_PRAGMA
1f190 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 19 /*
1f1a0 20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 Pragma Name
1f1b0 20 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 1st arg or NULL
1f1c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1f1d0 54 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 TE_READ
1f1e0 20 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 20 /*
1f1f0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1f200 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 Column Name
1f210 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1f220 45 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 E_SELECT
1f230 20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 21 /* N
1f240 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e ULL N
1f250 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1f260 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1f270 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 _TRANSACTION
1f280 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4e 55 22 /* NU
1f290 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 LL NU
1f2a0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1f2b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1f2c0 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 UPDATE
1f2d0 20 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 23 /* Tab
1f2e0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c le Name Col
1f2f0 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a umn Name */.
1f300 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
1f310 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 TTACH
1f320 20 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 24 /* File
1f330 6e 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c name NULL
1f340 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1f350 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
1f360 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 TACH
1f370 20 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 25 /* Datab
1f380 61 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 ase Name NULL
1f390 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1f3a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 efine SQLITE_ALT
1f3b0 45 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 ER_TABLE
1f3c0 20 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 26 /* Databa
1f3d0 73 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 se Name Table
1f3e0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
1f3f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e fine SQLITE_REIN
1f400 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 DEX
1f410 20 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 27 /* Index N
1f420 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 ame NULL
1f430 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1f440 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 ine SQLITE_ANALY
1f450 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ZE
1f460 32 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 28 /* Table Na
1f470 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1f480 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1f490 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 ne SQLITE_CREATE
1f4a0 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 _VTABLE 2
1f4b0 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 9 /* Table Nam
1f4c0 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 e Module Na
1f4d0 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e me */.#defin
1f4e0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 e SQLITE_DROP_VT
1f4f0 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 ABLE 30
1f500 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1f510 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d Module Nam
1f520 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 e */.#define
1f530 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e SQLITE_FUNCTION
1f540 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20 31
1f550 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 /* NULL
1f560 20 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 Function Na
1f570 6d 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 me */.#define
1f580 53 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 SQLITE_COPY
1f590 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 0
1f5a0 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 /* No longer us
1f5b0 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ed */../*.** CAP
1f5c0 49 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 I3REF: Tracing A
1f5d0 6e 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e nd Profiling Fun
1f5e0 63 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 ctions {H12280}
1f5f0 3c 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 <S60400>.** EXPE
1f600 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
1f610 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 hese routines re
1f620 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 gister callback
1f630 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 functions that c
1f640 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a an be used for.*
1f650 2a 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 * tracing and pr
1f660 6f 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 ofiling the exec
1f670 75 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 ution of SQL sta
1f680 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 tements..**.** T
1f690 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 he callback func
1f6a0 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 tion registered
1f6b0 62 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 by sqlite3_trace
1f6c0 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 () is invoked at
1f6d0 0a 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 .** various time
1f6e0 73 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 s when an SQL st
1f6f0 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 atement is being
1f700 20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 run by [sqlite3
1f710 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 _step()]..** The
1f720 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
1f730 73 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 s a UTF-8 render
1f740 69 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 ing of the SQL s
1f750 74 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a tatement text.**
1f760 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e as the statemen
1f770 74 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 t first begins e
1f780 78 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 xecuting. Addit
1f790 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 ional callbacks
1f7a0 6f 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 occur.** as each
1f7b0 20 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72 triggered subpr
1f7c0 6f 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 ogram is entered
1f7d0 2e 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 . The callbacks
1f7e0 20 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a for triggers.**
1f7f0 20 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 contain a UTF-8
1f800 20 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 SQL comment tha
1f810 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 t identifies the
1f820 20 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 trigger..**.**
1f830 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e The callback fun
1f840 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 ction registered
1f850 20 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 by sqlite3_prof
1f860 69 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 ile() is invoked
1f870 0a 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 .** as each SQL
1f880 73 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 statement finish
1f890 65 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 es. The profile
1f8a0 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 callback contai
1f8b0 6e 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e ns.** the origin
1f8c0 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 al statement tex
1f8d0 74 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 t and an estimat
1f8e0 65 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 e of wall-clock
1f8f0 74 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c time.** of how l
1f900 6f 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 ong that stateme
1f910 6e 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a nt took to run..
1f920 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
1f930 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38 31 7d :.**.** {H12281}
1f940 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 The callback fu
1f950 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 nction registere
1f960 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 72 d by [sqlite3_tr
1f970 61 63 65 28 29 5d 20 0a 2a 2a 20 20 20 20 20 20 ace()] .**
1f980 20 20 20 20 73 68 61 6c 6c 20 62 65 20 69 6e 76 shall be inv
1f990 6f 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 oked.**
1f9a0 20 77 68 65 6e 65 76 65 72 20 61 6e 20 53 51 4c whenever an SQL
1f9b0 20 73 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 statement first
1f9c0 20 62 65 67 69 6e 73 20 74 6f 20 65 78 65 63 75 begins to execu
1f9d0 74 65 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 te and.**
1f9e0 20 20 20 77 68 65 6e 65 76 65 72 20 61 20 74 72 whenever a tr
1f9f0 69 67 67 65 72 20 73 75 62 70 72 6f 67 72 61 6d igger subprogram
1fa00 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 74 6f first begins to
1fa10 20 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 run..**.** {H12
1fa20 32 38 32 7d 20 45 61 63 68 20 63 61 6c 6c 20 74 282} Each call t
1fa30 6f 20 5b 73 71 6c 69 74 65 33 5f 74 72 61 63 65 o [sqlite3_trace
1fa40 28 29 5d 20 73 68 61 6c 6c 20 6f 76 65 72 72 69 ()] shall overri
1fa50 64 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c de the previousl
1fa60 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 y.** re
1fa70 67 69 73 74 65 72 65 64 20 74 72 61 63 65 20 63 gistered trace c
1fa80 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 7b allback..**.** {
1fa90 48 31 32 32 38 33 7d 20 41 20 4e 55 4c 4c 20 74 H12283} A NULL t
1faa0 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 race callback sh
1fab0 61 6c 6c 20 64 69 73 61 62 6c 65 20 74 72 61 63 all disable trac
1fac0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 ing..**.** {H122
1fad0 38 34 7d 20 54 68 65 20 66 69 72 73 74 20 61 72 84} The first ar
1fae0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72 gument to the tr
1faf0 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61 ace callback sha
1fb00 6c 6c 20 62 65 20 61 20 63 6f 70 79 20 6f 66 0a ll be a copy of.
1fb10 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
1fb20 70 6f 69 6e 74 65 72 20 77 68 69 63 68 20 77 61 pointer which wa
1fb30 73 20 74 68 65 20 33 72 64 20 61 72 67 75 6d 65 s the 3rd argume
1fb40 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 nt to [sqlite3_t
1fb50 72 61 63 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b race()]..**.** {
1fb60 48 31 32 32 38 35 7d 20 54 68 65 20 73 65 63 6f H12285} The seco
1fb70 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 nd argument to t
1fb80 68 65 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63 he trace callbac
1fb90 6b 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 k is a.**
1fba0 20 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 zero-terminat
1fbb0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 ed UTF-8 string
1fbc0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f containing the o
1fbd0 72 69 67 69 6e 61 6c 20 74 65 78 74 0a 2a 2a 20 riginal text.**
1fbe0 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 of the
1fbf0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73 SQL statement as
1fc00 20 69 74 20 77 61 73 20 70 61 73 73 65 64 20 69 it was passed i
1fc10 6e 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 nto [sqlite3_pre
1fc20 70 61 72 65 5f 76 32 28 29 5d 0a 2a 2a 20 20 20 pare_v2()].**
1fc30 20 20 20 20 20 20 20 6f 72 20 74 68 65 20 65 71 or the eq
1fc40 75 69 76 61 6c 65 6e 74 2c 20 6f 72 20 61 6e 20 uivalent, or an
1fc50 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 69 6e 64 69 SQL comment indi
1fc60 63 61 74 69 6e 67 20 74 68 65 20 62 65 67 69 6e cating the begin
1fc70 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 ning.**
1fc80 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 73 75 of a trigger su
1fc90 62 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 bprogram..**.**
1fca0 7b 48 31 32 32 38 37 7d 20 54 68 65 20 63 61 6c {H12287} The cal
1fcb0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 lback function r
1fcc0 65 67 69 73 74 65 72 65 64 20 62 79 20 5b 73 71 egistered by [sq
1fcd0 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 5d lite3_profile()]
1fce0 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 20 is invoked.**
1fcf0 20 20 20 20 20 20 20 20 61 73 20 65 61 63 68 20 as each
1fd00 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 69 SQL statement fi
1fd10 6e 69 73 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 nishes..**.** {H
1fd20 31 32 32 38 38 7d 20 54 68 65 20 66 69 72 73 74 12288} The first
1fd30 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
1fd40 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 e profile callba
1fd50 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a ck is a copy of.
1fd60 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
1fd70 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 3rd parameter to
1fd80 20 5b 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c [sqlite3_profil
1fd90 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 e()]..**.** {H12
1fda0 32 38 39 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 289} The second
1fdb0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
1fdc0 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 profile callbac
1fdd0 6b 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 k is a.**
1fde0 20 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 zero-terminat
1fdf0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 ed UTF-8 string
1fe00 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
1fe10 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 e complete text
1fe20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 of.** t
1fe30 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1fe40 20 61 73 20 69 74 20 77 61 73 20 70 72 6f 63 65 as it was proce
1fe50 73 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 ssed by [sqlite3
1fe60 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 0a 2a _prepare_v2()].*
1fe70 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 74 68 * or th
1fe80 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 0a 2a 2a e equivalent..**
1fe90 0a 2a 2a 20 7b 48 31 32 32 39 30 7d 20 54 68 65 .** {H12290} The
1fea0 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
1feb0 20 74 6f 20 74 68 65 20 70 72 6f 66 69 6c 65 20 to the profile
1fec0 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 65 callback is an e
1fed0 73 74 69 6d 61 74 65 0a 2a 2a 20 20 20 20 20 20 stimate.**
1fee0 20 20 20 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 of the numbe
1fef0 72 20 6f 66 20 6e 61 6e 6f 73 65 63 6f 6e 64 73 r of nanoseconds
1ff00 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 of wall-clock t
1ff10 69 6d 65 20 72 65 71 75 69 72 65 64 20 74 6f 0a ime required to.
1ff20 2a 2a 20 20 20 20 20 20 20 20 20 20 72 75 6e 20 ** run
1ff30 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
1ff40 74 20 66 72 6f 6d 20 73 74 61 72 74 20 74 6f 20 t from start to
1ff50 66 69 6e 69 73 68 2e 0a 2a 2f 0a 53 51 4c 49 54 finish..*/.SQLIT
1ff60 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 E_API SQLITE_EXP
1ff70 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a ERIMENTAL void *
1ff80 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 sqlite3_trace(sq
1ff90 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 lite3*, void(*xT
1ffa0 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 race)(void*,cons
1ffb0 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 t char*), void*)
1ffc0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c ;.SQLITE_API SQL
1ffd0 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
1ffe0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 void *sqlite3_p
1fff0 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c rofile(sqlite3*,
20000 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 . void(*xProfi
20010 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 le)(void*,const
20020 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 char*,sqlite3_ui
20030 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a nt64), void*);..
20040 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
20050 51 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43 Query Progress C
20060 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30 allbacks {H12910
20070 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a } <S60400>.**.**
20080 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f This routine co
20090 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62 nfigures a callb
200a0 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74 ack function - t
200b0 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63 he.** progress c
200c0 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69 allback - that i
200d0 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64 s invoked period
200e0 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f ically during lo
200f0 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61 ng.** running ca
20100 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
20110 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 exec()], [sqlite
20120 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a 3_step()] and.**
20130 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 [sqlite3_get_ta
20140 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d ble()]. An exam
20150 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73 ple use for this
20160 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 .** interface is
20170 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75 to keep a GUI u
20180 70 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20 pdated during a
20190 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a large query..**.
201a0 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 ** If the progre
201b0 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 ss callback retu
201c0 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 rns non-zero, th
201d0 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a e operation is.*
201e0 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20 * interrupted.
201f0 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e This feature can
20200 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c be used to impl
20210 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63 ement a.** "Canc
20220 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20 el" button on a
20230 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61 GUI progress dia
20240 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 log box..**.** T
20250 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 he progress hand
20260 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 ler must not do
20270 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 anything that wi
20280 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 ll modify.** the
20290 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
202a0 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 tion that invoke
202b0 64 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68 d the progress h
202c0 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20 andler..** Note
202d0 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 that [sqlite3_pr
202e0 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 epare_v2()] and
202f0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
20300 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 both modify the
20310 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 ir.** database c
20320 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 onnections for t
20330 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d he meaning of "m
20340 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 odify" in this p
20350 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 aragraph..**.**
20360 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
20370 2a 20 7b 48 31 32 39 31 31 7d 20 54 68 65 20 63 * {H12911} The c
20380 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
20390 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20 73 registered by s
203a0 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f qlite3_progress_
203b0 68 61 6e 64 6c 65 72 28 29 0a 2a 2a 20 20 20 20 handler().**
203c0 20 20 20 20 20 20 69 73 20 69 6e 76 6f 6b 65 64 is invoked
203d0 20 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64 75 periodically du
203e0 72 69 6e 67 20 6c 6f 6e 67 20 72 75 6e 6e 69 6e ring long runnin
203f0 67 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20 g calls to.**
20400 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
20410 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b step()]..**.** {
20420 48 31 32 39 31 32 7d 20 54 68 65 20 70 72 6f 67 H12912} The prog
20430 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 ress callback is
20440 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f invoked once fo
20450 72 20 65 76 65 72 79 20 4e 20 76 69 72 74 75 61 r every N virtua
20460 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 61 l.** ma
20470 63 68 69 6e 65 20 6f 70 63 6f 64 65 73 2c 20 77 chine opcodes, w
20480 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 here N is the se
20490 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
204a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
204b0 20 5b 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 [sqlite3_progre
204c0 73 73 5f 68 61 6e 64 6c 65 72 28 29 5d 20 63 61 ss_handler()] ca
204d0 6c 6c 20 74 68 61 74 20 72 65 67 69 73 74 65 72 ll that register
204e0 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 ed.** t
204f0 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 49 66 he callback. If
20500 20 4e 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 N is less than
20510 31 2c 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 1, sqlite3_progr
20520 65 73 73 5f 68 61 6e 64 6c 65 72 28 29 0a 2a 2a ess_handler().**
20530 20 20 20 20 20 20 20 20 20 20 61 63 74 73 20 61 acts a
20540 73 20 69 66 20 61 20 4e 55 4c 4c 20 70 72 6f 67 s if a NULL prog
20550 72 65 73 73 20 68 61 6e 64 6c 65 72 20 68 61 64 ress handler had
20560 20 62 65 65 6e 20 73 70 65 63 69 66 69 65 64 2e been specified.
20570 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31 33 7d 20 .**.** {H12913}
20580 54 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c The progress cal
20590 6c 62 61 63 6b 20 69 74 73 65 6c 66 20 69 73 20 lback itself is
205a0 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 identified by th
205b0 65 20 74 68 69 72 64 0a 2a 2a 20 20 20 20 20 20 e third.**
205c0 20 20 20 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 argument to
205d0 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 sqlite3_progress
205e0 5f 68 61 6e 64 6c 65 72 28 29 2e 0a 2a 2a 0a 2a _handler()..**.*
205f0 2a 20 7b 48 31 32 39 31 34 7d 20 54 68 65 20 66 * {H12914} The f
20600 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 20 74 ourth argument t
20610 6f 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 o sqlite3_progre
20620 73 73 5f 68 61 6e 64 6c 65 72 28 29 20 69 73 20 ss_handler() is
20630 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 6f a.** vo
20640 69 64 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 id pointer passe
20650 64 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73 d to the progres
20660 73 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 s callback.**
20670 20 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 function
20680 65 61 63 68 20 74 69 6d 65 20 69 74 20 69 73 20 each time it is
20690 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b invoked..**.** {
206a0 48 31 32 39 31 35 7d 20 49 66 20 61 20 63 61 6c H12915} If a cal
206b0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 l to [sqlite3_st
206c0 65 70 28 29 5d 20 72 65 73 75 6c 74 73 20 69 6e ep()] results in
206d0 20 66 65 77 65 72 20 74 68 61 6e 20 4e 20 6f 70 fewer than N op
206e0 63 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 codes.**
206f0 20 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 being executed
20700 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 , then the progr
20710 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ess callback is
20720 6e 65 76 65 72 20 69 6e 76 6f 6b 65 64 2e 0a 2a never invoked..*
20730 2a 0a 2a 2a 20 7b 48 31 32 39 31 36 7d 20 45 76 *.** {H12916} Ev
20740 65 72 79 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c ery call to [sql
20750 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 ite3_progress_ha
20760 6e 64 6c 65 72 28 29 5d 0a 2a 2a 20 20 20 20 20 ndler()].**
20770 20 20 20 20 20 6f 76 65 72 77 72 69 74 65 73 20 overwrites
20780 61 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 72 any previously r
20790 65 67 69 73 74 65 72 65 64 20 70 72 6f 67 72 65 egistered progre
207a0 73 73 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a ss handler..**.*
207b0 2a 20 7b 48 31 32 39 31 37 7d 20 49 66 20 74 68 * {H12917} If th
207c0 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c e progress handl
207d0 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e er callback is N
207e0 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 70 72 6f 67 ULL then no prog
207f0 72 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ress.**
20800 20 68 61 6e 64 6c 65 72 20 69 73 20 69 6e 76 6f handler is invo
20810 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 ked..**.** {H129
20820 31 38 7d 20 49 66 20 74 68 65 20 70 72 6f 67 72 18} If the progr
20830 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 ess callback ret
20840 75 72 6e 73 20 61 20 72 65 73 75 6c 74 20 6f 74 urns a result ot
20850 68 65 72 20 74 68 61 6e 20 30 2c 20 74 68 65 6e her than 0, then
20860 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
20870 20 62 65 68 61 76 69 6f 72 20 69 73 20 61 20 69 behavior is a i
20880 66 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 f [sqlite3_inter
20890 72 75 70 74 28 29 5d 20 68 61 64 20 62 65 65 6e rupt()] had been
208a0 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 20 20 called..**
208b0 20 20 20 20 20 3c 53 33 30 35 30 30 3e 0a 2a 2f <S30500>.*/
208c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
208d0 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 sqlite3_progres
208e0 73 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 s_handler(sqlite
208f0 33 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 3*, int, int(*)(
20900 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a void*), void*);.
20910 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
20920 20 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 Opening A New D
20930 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 atabase Connecti
20940 6f 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 on {H12700} <S40
20950 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 200>.**.** These
20960 20 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 routines open a
20970 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 n SQLite databas
20980 65 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d e file whose nam
20990 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 e is given by th
209a0 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 e.** filename ar
209b0 67 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 gument. The file
209c0 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 name argument is
209d0 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
209e0 55 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c UTF-8 for.** sql
209f0 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 ite3_open() and
20a00 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
20a10 29 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 ) and as UTF-16
20a20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 in the native by
20a30 74 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 te.** order for
20a40 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 sqlite3_open16()
20a50 2e 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f . A [database co
20a60 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 nnection] handle
20a70 20 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 is usually.** r
20a80 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 eturned in *ppDb
20a90 2c 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 , even if an err
20aa0 6f 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 or occurs. The
20ab0 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 only exception i
20ac0 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c s that.** if SQL
20ad0 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f ite is unable to
20ae0 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 allocate memory
20af0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 to hold the [sq
20b00 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a lite3] object,.*
20b10 2a 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 * a NULL will be
20b20 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 written into *p
20b30 70 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 pDb instead of a
20b40 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
20b50 5b 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a [sqlite3].** obj
20b60 65 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 ect. If the data
20b70 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 base is opened (
20b80 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 and/or created)
20b90 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 successfully, th
20ba0 65 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b en.** [SQLITE_OK
20bb0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 ] is returned.
20bc0 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 Otherwise an [er
20bd0 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 ror code] is ret
20be0 75 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b urned. The.** [
20bf0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
20c00 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 ] or [sqlite3_er
20c10 72 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e rmsg16()] routin
20c20 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 es can be used t
20c30 6f 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 o obtain.** an E
20c40 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 nglish language
20c50 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 description of t
20c60 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 he error..**.**
20c70 54 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f The default enco
20c80 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 ding for the dat
20c90 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 abase will be UT
20ca0 46 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 F-8 if.** sqlite
20cb0 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 3_open() or sqli
20cc0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 te3_open_v2() is
20cd0 20 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 called and.** U
20ce0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 TF-16 in the nat
20cf0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 ive byte order i
20d00 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 f sqlite3_open16
20d10 28 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a () is used..**.*
20d20 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 * Whether or not
20d30 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
20d40 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e when it is open
20d50 65 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a ed, resources.**
20d60 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
20d70 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
20d80 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c onnection] handl
20d90 65 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 e should be rele
20da0 61 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 ased by.** passi
20db0 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 ng it to [sqlite
20dc0 33 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 3_close()] when
20dd0 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 it is no longer
20de0 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 required..**.**
20df0 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e The sqlite3_open
20e00 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 _v2() interface
20e10 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 works like sqlit
20e20 65 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 e3_open().** exc
20e30 65 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 ept that it acce
20e40 70 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e pts two addition
20e50 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f al parameters fo
20e60 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e r additional con
20e70 74 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 trol.** over the
20e80 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f new database co
20e90 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 nnection. The f
20ea0 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 lags parameter c
20eb0 61 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a an take one of.*
20ec0 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * the following
20ed0 74 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 three values, op
20ee0 74 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 tionally combine
20ef0 64 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b d with the .** [
20f00 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 SQLITE_OPEN_NOMU
20f10 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f TEX] or [SQLITE_
20f20 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 OPEN_FULLMUTEX]
20f30 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c flags:.**.** <dl
20f40 3e 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 >.** <dt>[SQLITE
20f50 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c _OPEN_READONLY]<
20f60 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 /dt>.** <dd>The
20f70 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e database is open
20f80 65 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 ed in read-only
20f90 6d 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 mode. If the da
20fa0 74 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a tabase does not.
20fb0 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ** already exist
20fc0 2c 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 , an error is re
20fd0 74 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a turned.</dd>.**.
20fe0 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f ** <dt>[SQLITE_O
20ff0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f PEN_READWRITE]</
21000 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 dt>.** <dd>The d
21010 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 atabase is opene
21020 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e d for reading an
21030 64 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 d writing if pos
21040 73 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e sible, or readin
21050 67 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 g.** only if the
21060 20 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 file is write p
21070 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 rotected by the
21080 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
21090 2e 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 . In either.**
210a0 63 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 case the databas
210b0 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 e must already e
210c0 78 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 xist, otherwise
210d0 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 an error is retu
210e0 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a rned.</dd>.**.**
210f0 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 <dt>[SQLITE_OPE
21100 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b N_READWRITE] | [
21110 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
21120 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TE]</dt>.** <dd>
21130 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 The database is
21140 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 opened for readi
21150 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 ng and writing,
21160 61 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 and is creates i
21170 74 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 t if.** it does
21180 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 not already exis
21190 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 t. This is the b
211a0 65 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 ehavior that is
211b0 61 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a always used for.
211c0 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 ** sqlite3_open(
211d0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 ) and sqlite3_op
211e0 65 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 en16().</dd>.**
211f0 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 </dl>.**.** If t
21200 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 he 3rd parameter
21210 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e to sqlite3_open
21220 5f 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 _v2() is not one
21230 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 of the.** combi
21240 6e 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 nations shown ab
21250 6f 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 ove or one of th
21260 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 e combinations s
21270 68 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 hown above combi
21280 6e 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 ned.** with the
21290 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d [SQLITE_OPEN_NOM
212a0 55 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 UTEX] or [SQLITE
212b0 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d _OPEN_FULLMUTEX]
212c0 20 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 flags,.** then
212d0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 the behavior is
212e0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
212f0 20 49 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f If the [SQLITE_
21300 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c OPEN_NOMUTEX] fl
21310 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 ag is set, then
21320 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
21330 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 nection.** opens
21340 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 in the multi-th
21350 72 65 61 64 20 5b 74 68 72 65 61 64 69 6e 67 20 read [threading
21360 6d 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73 mode] as long as
21370 20 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 the single-thre
21380 61 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e ad.** mode has n
21390 6f 74 20 62 65 65 6e 20 73 65 74 20 61 74 20 63 ot been set at c
213a0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 ompile-time or s
213b0 74 61 72 74 2d 74 69 6d 65 2e 20 20 49 66 20 74 tart-time. If t
213c0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 he.** [SQLITE_OP
213d0 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c EN_FULLMUTEX] fl
213e0 61 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 74 ag is set then t
213f0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
21400 65 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 ection opens.**
21410 69 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 in the serialize
21420 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 d [threading mod
21430 65 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65 e] unless single
21440 2d 74 68 72 65 61 64 20 77 61 73 0a 2a 2a 20 70 -thread was.** p
21450 72 65 76 69 6f 75 73 6c 79 20 73 65 6c 65 63 74 reviously select
21460 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 ed at compile-ti
21470 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 me or start-time
21480 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 ..**.** If the f
21490 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d ilename is ":mem
214a0 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72 ory:", then a pr
214b0 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 ivate, temporary
214c0 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
214d0 61 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65 ase.** is create
214e0 64 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 d for the connec
214f0 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d tion. This in-m
21500 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 emory database w
21510 69 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a ill vanish when.
21520 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
21530 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c connection is cl
21540 6f 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 osed. Future ve
21550 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
21560 20 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75 might.** make u
21570 73 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c se of additional
21580 20 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d special filenam
21590 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 es that begin wi
215a0 74 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 th the ":" chara
215b0 63 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72 cter..** It is r
215c0 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 ecommended that
215d0 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 when a database
215e0 66 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c filename actuall
215f0 79 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74 y does begin wit
21600 68 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61 h.** a ":" chara
21610 63 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20 cter you should
21620 70 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e prefix the filen
21630 61 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e ame with a pathn
21640 61 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22 ame such as.** "
21650 2e 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62 ./" to avoid amb
21660 69 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 iguity..**.** If
21670 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 the filename is
21680 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 an empty string
21690 2c 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 , then a private
216a0 2c 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f , temporary.** o
216b0 6e 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 n-disk database
216c0 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e will be created.
216d0 20 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64 This private d
216e0 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a atabase will be.
216f0 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ** automatically
21700 20 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e deleted as soon
21710 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 as the database
21720 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 connection is c
21730 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 losed..**.** The
21740 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 fourth paramete
21750 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 r to sqlite3_ope
21760 6e 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61 n_v2() is the na
21770 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 me of the.** [sq
21780 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 lite3_vfs] objec
21790 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 t that defines t
217a0 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 he operating sys
217b0 74 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68 tem interface th
217c0 61 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 at.** the new da
217d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
217e0 6e 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49 n should use. I
217f0 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 f the fourth par
21800 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e ameter is.** a N
21810 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e ULL pointer then
21820 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 the default [sq
21830 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 lite3_vfs] objec
21840 74 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a t is used..**.**
21850 20 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64 <b>Note to Wind
21860 6f 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 ows users:</b>
21870 54 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 The encoding use
21880 64 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 d for the filena
21890 6d 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f me argument.** o
218a0 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 f sqlite3_open()
218b0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 and sqlite3_ope
218c0 6e 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55 n_v2() must be U
218d0 54 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 TF-8, not whatev
218e0 65 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 er.** codepage i
218f0 73 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 s currently defi
21900 6e 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 ned. Filenames
21910 63 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 containing inter
21920 6e 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 national.** char
21930 61 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 acters must be c
21940 6f 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d onverted to UTF-
21950 38 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 8 prior to passi
21960 6e 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 ng them into.**
21970 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f sqlite3_open() o
21980 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 r sqlite3_open_v
21990 32 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 2()..**.** INVAR
219a0 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 IANTS:.**.** {H1
219b0 32 37 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 2701} The [sqlit
219c0 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c e3_open()], [sql
219d0 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 ite3_open16()],
219e0 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 and.**
219f0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 [sqlite3_open_v2
21a00 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 63 ()] interfaces c
21a10 72 65 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20 reate a new.**
21a20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 [databas
21a30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73 e connection] as
21a40 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a sociated with.**
21a50 20 20 20 20 20 20 20 20 20 20 74 68 65 20 64 61 the da
21a60 74 61 62 61 73 65 20 66 69 6c 65 20 67 69 76 65 tabase file give
21a70 6e 20 69 6e 20 74 68 65 69 72 20 66 69 72 73 74 n in their first
21a80 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a parameter..**.*
21a90 2a 20 7b 48 31 32 37 30 32 7d 20 54 68 65 20 66 * {H12702} The f
21aa0 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 ilename argument
21ab0 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 is interpreted
21ac0 61 73 20 55 54 46 2d 38 0a 2a 2a 20 20 20 20 20 as UTF-8.**
21ad0 20 20 20 20 20 66 6f 72 20 5b 73 71 6c 69 74 65 for [sqlite
21ae0 33 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 5b 73 3_open()] and [s
21af0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
21b00 5d 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 0a ] and as UTF-16.
21b10 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 ** in t
21b20 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f he native byte o
21b30 72 64 65 72 20 66 6f 72 20 5b 73 71 6c 69 74 65 rder for [sqlite
21b40 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a 3_open16()]..**.
21b50 2a 2a 20 7b 48 31 32 37 30 33 7d 20 41 20 73 75 ** {H12703} A su
21b60 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 ccessful invocat
21b70 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f ion of [sqlite3_
21b80 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 open()], [sqlite
21b90 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 3_open16()],.**
21ba0 20 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c or [sql
21bb0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 ite3_open_v2()]
21bc0 77 72 69 74 65 73 20 61 20 70 6f 69 6e 74 65 72 writes a pointer
21bd0 20 74 6f 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 to a new.**
21be0 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 [database
21bf0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 6e 74 6f connection] into
21c00 20 2a 70 70 44 62 2e 0a 2a 2a 0a 2a 2a 20 7b 48 *ppDb..**.** {H
21c10 31 32 37 30 34 7d 20 54 68 65 20 5b 73 71 6c 69 12704} The [sqli
21c20 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 te3_open()], [sq
21c30 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c lite3_open16()],
21c40 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 and.**
21c50 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
21c60 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 2()] interfaces
21c70 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f return [SQLITE_O
21c80 4b 5d 20 75 70 6f 6e 20 73 75 63 63 65 73 73 2c K] upon success,
21c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 .** or
21ca0 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b an appropriate [
21cb0 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66 error code] on f
21cc0 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ailure..**.** {H
21cd0 31 32 37 30 36 7d 20 54 68 65 20 64 65 66 61 75 12706} The defau
21ce0 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 lt text encoding
21cf0 20 66 6f 72 20 61 20 6e 65 77 20 64 61 74 61 62 for a new datab
21d00 61 73 65 20 63 72 65 61 74 65 64 20 75 73 69 6e ase created usin
21d10 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 g.** [s
21d20 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 6f qlite3_open()] o
21d30 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f r [sqlite3_open_
21d40 76 32 28 29 5d 20 77 69 6c 6c 20 62 65 20 55 54 v2()] will be UT
21d50 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 F-8..**.** {H127
21d60 30 37 7d 20 54 68 65 20 64 65 66 61 75 6c 74 20 07} The default
21d70 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f text encoding fo
21d80 72 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 r a new database
21d90 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a created using.*
21da0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
21db0 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 20 77 69 te3_open16()] wi
21dc0 6c 6c 20 62 65 20 55 54 46 2d 31 36 2e 0a 2a 2a ll be UTF-16..**
21dd0 0a 2a 2a 20 7b 48 31 32 37 30 39 7d 20 54 68 65 .** {H12709} The
21de0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 46 [sqlite3_open(F
21df0 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 69 ,D)] interface i
21e00 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a s equivalent to.
21e10 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
21e20 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 ite3_open_v2(F,D
21e30 2c 47 2c 30 29 5d 20 77 68 65 72 65 20 74 68 65 ,G,0)] where the
21e40 20 47 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a G parameter is.
21e50 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c ** [SQL
21e60 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
21e70 54 45 5d 7c 5b 53 51 4c 49 54 45 5f 4f 50 45 4e TE]|[SQLITE_OPEN
21e80 5f 43 52 45 41 54 45 5d 2e 0a 2a 2a 0a 2a 2a 20 _CREATE]..**.**
21e90 7b 48 31 32 37 31 31 7d 20 49 66 20 74 68 65 20 {H12711} If the
21ea0 47 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b G parameter to [
21eb0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
21ec0 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74 61 69 F,D,G,V)] contai
21ed0 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 ns the.**
21ee0 20 20 20 62 69 74 20 76 61 6c 75 65 20 5b 53 51 bit value [SQ
21ef0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
21f00 4c 59 5d 20 74 68 65 6e 20 74 68 65 20 64 61 74 LY] then the dat
21f10 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 0a abase is opened.
21f20 2a 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 ** for
21f30 72 65 61 64 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2a reading only..**
21f40 0a 2a 2a 20 7b 48 31 32 37 31 32 7d 20 49 66 20 .** {H12712} If
21f50 74 68 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 the G parameter
21f60 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e to [sqlite3_open
21f70 5f 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f _v2(F,D,G,V)] co
21f80 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 ntains the.**
21f90 20 20 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 bit value
21fa0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 [SQLITE_OPEN_RE
21fb0 41 44 57 52 49 54 45 5d 20 74 68 65 6e 20 74 68 ADWRITE] then th
21fc0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 e database is op
21fd0 65 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 ened.**
21fe0 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 reading and wri
21ff0 74 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c 65 ting if possible
22000 2c 20 6f 72 20 66 6f 72 20 72 65 61 64 69 6e 67 , or for reading
22010 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 only if the.**
22020 20 20 20 20 20 20 20 20 20 66 69 6c 65 20 69 73 file is
22030 20 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64 write protected
22040 20 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e by the operatin
22050 67 20 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 g system..**.**
22060 7b 48 31 32 37 31 33 7d 20 49 66 20 74 68 65 20 {H12713} If the
22070 47 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b G parameter to [
22080 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
22090 46 2c 44 2c 47 2c 56 29 5d 20 6f 6d 69 74 73 20 F,D,G,V)] omits
220a0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
220b0 62 69 74 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 bit value [SQLIT
220c0 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 20 61 E_OPEN_CREATE] a
220d0 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nd the database
220e0 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 does not.**
220f0 20 20 20 20 20 70 72 65 76 69 6f 75 73 6c 79 20 previously
22100 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20 exist, an error
22110 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
22120 2a 2a 20 7b 48 31 32 37 31 34 7d 20 49 66 20 74 ** {H12714} If t
22130 68 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74 he G parameter t
22140 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f o [sqlite3_open_
22150 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e v2(F,D,G,V)] con
22160 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 tains the.**
22170 20 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20 bit value
22180 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 [SQLITE_OPEN_CRE
22190 41 54 45 5d 20 61 6e 64 20 74 68 65 20 64 61 74 ATE] and the dat
221a0 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a abase does not.*
221b0 2a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 69 * previ
221c0 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 74 68 65 ously exist, the
221d0 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 n an attempt is
221e0 6d 61 64 65 20 74 6f 20 63 72 65 61 74 65 20 61 made to create a
221f0 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 nd.** i
22200 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 64 61 nitialize the da
22210 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 tabase..**.** {H
22220 31 32 37 31 37 7d 20 49 66 20 74 68 65 20 66 69 12717} If the fi
22230 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 lename argument
22240 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e to [sqlite3_open
22250 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 ()], [sqlite3_op
22260 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 en16()],.**
22270 20 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 or [sqlite3
22280 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 73 20 22 _open_v2()] is "
22290 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 :memory:", then
222a0 61 6e 20 70 72 69 76 61 74 65 2c 0a 2a 2a 20 20 an private,.**
222b0 20 20 20 20 20 20 20 20 65 70 68 65 6d 65 72 61 ephemera
222c0 6c 2c 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 l, in-memory dat
222d0 61 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 abase is created
222e0 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 for the connect
222f0 69 6f 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 ion..**
22300 20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49 54 45 <todo>Is SQLITE
22310 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c _OPEN_CREATE|SQL
22320 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
22330 54 45 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 20 TE required.**
22340 20 20 20 20 20 20 20 20 69 6e 20 73 71 6c 69 74 in sqlit
22350 65 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c 2f 74 e3_open_v2()?</t
22360 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 odo>.**.** {H127
22370 31 39 7d 20 49 66 20 74 68 65 20 66 69 6c 65 6e 19} If the filen
22380 61 6d 65 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 ame is NULL or a
22390 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 20 n empty string,
223a0 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 0a then a private,.
223b0 2a 2a 20 20 20 20 20 20 20 20 20 20 65 70 68 65 ** ephe
223c0 6d 65 72 61 6c 20 6f 6e 2d 64 69 73 6b 20 64 61 meral on-disk da
223d0 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 tabase will be c
223e0 72 65 61 74 65 64 2e 0a 2a 2a 20 20 20 20 20 20 reated..**
223f0 20 20 20 20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c <todo>Is SQL
22400 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c ITE_OPEN_CREATE|
22410 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
22420 57 52 49 54 45 20 72 65 71 75 69 72 65 64 0a 2a WRITE required.*
22430 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 73 71 * in sq
22440 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 3f lite3_open_v2()?
22450 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 </todo>.**.** {H
22460 31 32 37 32 31 7d 20 54 68 65 20 5b 64 61 74 61 12721} The [data
22470 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
22480 20 63 72 65 61 74 65 64 20 62 79 20 5b 73 71 6c created by [sql
22490 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 ite3_open_v2(F,D
224a0 2c 47 2c 56 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ,G,V)].**
224b0 20 20 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 will use the
224c0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 [sqlite3_vfs] ob
224d0 6a 65 63 74 20 69 64 65 6e 74 69 66 69 65 64 20 ject identified
224e0 62 79 20 74 68 65 20 56 20 70 61 72 61 6d 65 74 by the V paramet
224f0 65 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 er,.**
22500 6f 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b or the default [
22510 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a sqlite3_vfs] obj
22520 65 63 74 20 69 66 20 56 20 69 73 20 61 20 4e 55 ect if V is a NU
22530 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
22540 2a 20 7b 48 31 32 37 32 33 7d 20 54 77 6f 20 5b * {H12723} Two [
22550 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
22560 69 6f 6e 73 5d 20 77 69 6c 6c 20 73 68 61 72 65 ions] will share
22570 20 61 20 63 6f 6d 6d 6f 6e 20 63 61 63 68 65 20 a common cache
22580 69 66 20 62 6f 74 68 20 77 65 72 65 0a 2a 2a 20 if both were.**
22590 20 20 20 20 20 20 20 20 20 6f 70 65 6e 65 64 20 opened
225a0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 46 with the same VF
225b0 53 20 77 68 69 6c 65 20 5b 73 68 61 72 65 64 20 S while [shared
225c0 63 61 63 68 65 20 6d 6f 64 65 5d 20 77 61 73 20 cache mode] was
225d0 65 6e 61 62 6c 65 64 20 61 6e 64 0a 2a 2a 20 20 enabled and.**
225e0 20 20 20 20 20 20 20 20 69 66 20 62 6f 74 68 20 if both
225f0 66 69 6c 65 6e 61 6d 65 73 20 63 6f 6d 70 61 72 filenames compar
22600 65 20 65 71 75 61 6c 20 75 73 69 6e 67 20 6d 65 e equal using me
22610 6d 63 6d 70 28 29 20 61 66 74 65 72 20 68 61 76 mcmp() after hav
22620 69 6e 67 20 62 65 65 6e 0a 2a 2a 20 20 20 20 20 ing been.**
22630 20 20 20 20 20 70 72 6f 63 65 73 73 65 64 20 62 processed b
22640 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 y the [sqlite3_v
22650 66 73 20 7c 20 78 46 75 6c 6c 50 61 74 68 6e 61 fs | xFullPathna
22660 6d 65 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 me] method of th
22670 65 20 56 46 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 e VFS..*/.SQLITE
22680 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
22690 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 _open(. const c
226a0 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 har *filename,
226b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c /* Database fil
226c0 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f ename (UTF-8) */
226d0 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 . sqlite3 **ppD
226e0 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 b /* OU
226f0 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e T: SQLite db han
22700 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 dle */.);.SQLITE
22710 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
22720 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 _open16(. const
22730 20 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c void *filename,
22740 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 /* Database f
22750 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 ilename (UTF-16)
22760 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a */. sqlite3 **
22770 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a ppDb /*
22780 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 OUT: SQLite db
22790 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c handle */.);.SQL
227a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
227b0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 te3_open_v2(. c
227c0 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e onst char *filen
227d0 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 ame, /* Databa
227e0 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 se filename (UTF
227f0 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 -8) */. sqlite3
22800 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 **ppDb,
22810 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 /* OUT: SQLite
22820 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 db handle */. i
22830 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
22840 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
22850 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
22860 2a 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 *zVfs /*
22870 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 Name of VFS modu
22880 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a le to use */.);.
22890 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
228a0 20 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 Error Codes And
228b0 20 4d 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 Messages {H1280
228c0 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 0} <S60200>.**.*
228d0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 * The sqlite3_er
228e0 72 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 rcode() interfac
228f0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 e returns the nu
22900 6d 65 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f meric [result co
22910 64 65 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e de] or.** [exten
22920 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d ded result code]
22930 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 for the most re
22940 63 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 cent failed sqli
22950 74 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a te3_* API call.*
22960 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 * associated wit
22970 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f h a [database co
22980 6e 6e 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 nnection]. If a
22990 70 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 prior API call f
229a0 61 69 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 ailed.** but the
229b0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 most recent API
229c0 20 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c call succeeded,
229d0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
229e0 65 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 e from.** sqlite
229f0 33 5f 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 3_errcode() is u
22a00 6e 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 ndefined. The s
22a10 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f qlite3_extended_
22a20 65 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 errcode().** int
22a30 65 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 erface is the sa
22a40 6d 65 20 65 78 63 65 70 74 20 74 68 61 74 20 69 me except that i
22a50 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 t always returns
22a60 20 74 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 the .** [extend
22a70 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 ed result code]
22a80 65 76 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 even when extend
22a90 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 ed result codes
22aa0 61 72 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e are.** disabled.
22ab0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
22ac0 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 e3_errmsg() and
22ad0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 sqlite3_errmsg16
22ae0 28 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 () return Englis
22af0 68 2d 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 h-language.** te
22b00 78 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65 xt that describe
22b10 73 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 s the error, as
22b20 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 either UTF-8 or
22b30 55 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 UTF-16 respectiv
22b40 65 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 ely..** Memory t
22b50 6f 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 o hold the error
22b60 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 message string
22b70 69 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 is managed inter
22b80 6e 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 nally..** The ap
22b90 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e plication does n
22ba0 6f 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 ot need to worry
22bb0 20 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 about freeing t
22bc0 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f he result..** Ho
22bd0 77 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 wever, the error
22be0 20 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 string might be
22bf0 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 overwritten or
22c00 64 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a deallocated by.*
22c10 2a 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c * subsequent cal
22c20 6c 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 ls to other SQLi
22c30 74 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e te interface fun
22c40 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 ctions..**.** Wh
22c50 65 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 en the serialize
22c60 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 d [threading mod
22c70 65 5d 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 e] is in use, it
22c80 20 6d 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a might be the.**
22c90 20 63 61 73 65 20 74 68 61 74 20 61 20 73 65 63 case that a sec
22ca0 6f 6e 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ond error occurs
22cb0 20 6f 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 on a separate t
22cc0 68 72 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e hread in between
22cd0 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 .** the time of
22ce0 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 the first error
22cf0 61 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 and the call to
22d00 74 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 these interfaces
22d10 2e 0a 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 ..** When that h
22d20 61 70 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f appens, the seco
22d30 6e 64 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 nd error will be
22d40 20 72 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 reported since
22d50 74 68 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 these.** interfa
22d60 63 65 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 ces always repor
22d70 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e t the most recen
22d80 74 20 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 t result. To av
22d90 6f 69 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 oid.** this, eac
22da0 68 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 h thread can obt
22db0 61 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 ain exclusive us
22dc0 65 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 e of the [databa
22dd0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 se connection] D
22de0 0a 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 .** by invoking
22df0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 [sqlite3_mutex_e
22e00 6e 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 nter]([sqlite3_d
22e10 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 b_mutex](D)) bef
22e20 6f 72 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a ore beginning.**
22e30 20 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e to use D and in
22e40 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f voking [sqlite3_
22e50 6d 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 mutex_leave]([sq
22e60 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 lite3_db_mutex](
22e70 44 29 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c D)) after.** all
22e80 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e calls to the in
22e90 74 65 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 terfaces listed
22ea0 68 65 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 here are complet
22eb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
22ec0 69 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 interface fails
22ed0 77 69 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 with SQLITE_MISU
22ee0 53 45 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 SE, that means t
22ef0 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 he interface.**
22f00 77 61 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f was invoked inco
22f10 72 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 rrectly by the a
22f20 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 pplication. In
22f30 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a that case, the.*
22f40 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 * error code and
22f50 20 6d 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 message may or
22f60 6d 61 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a may not be set..
22f70 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
22f80 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 31 7d :.**.** {H12801}
22f90 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 The [sqlite3_er
22fa0 72 63 6f 64 65 28 44 29 5d 20 69 6e 74 65 72 66 rcode(D)] interf
22fb0 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ace returns the
22fc0 6e 75 6d 65 72 69 63 0a 2a 2a 20 20 20 20 20 20 numeric.**
22fd0 20 20 20 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 [result code
22fe0 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 72 ] or [extended r
22ff0 65 73 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 esult code] for
23000 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
23010 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61 y.** fa
23020 69 6c 65 64 20 69 6e 74 65 72 66 61 63 65 20 63 iled interface c
23030 61 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20 77 all associated w
23040 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 ith the [databas
23050 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e e connection] D.
23060 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 32 7d 20 .**.** {H12802}
23070 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 The [sqlite3_ext
23080 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 44 29 ended_errcode(D)
23090 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ] interface retu
230a0 72 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63 0a rns the numeric.
230b0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 65 78 74 ** [ext
230c0 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 ended result cod
230d0 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 e] for the most
230e0 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 recently.**
230f0 20 20 20 20 20 66 61 69 6c 65 64 20 69 6e 74 65 failed inte
23100 72 66 61 63 65 20 63 61 6c 6c 20 61 73 73 6f 63 rface call assoc
23110 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b iated with the [
23120 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
23130 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ion] D..**.** {H
23140 31 32 38 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 12803} The [sqli
23150 74 65 33 5f 65 72 72 6d 73 67 28 44 29 5d 20 61 te3_errmsg(D)] a
23160 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d nd [sqlite3_errm
23170 73 67 31 36 28 44 29 5d 0a 2a 2a 20 20 20 20 20 sg16(D)].**
23180 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 interfaces
23190 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c return English-l
231a0 61 6e 67 75 61 67 65 20 74 65 78 74 20 74 68 61 anguage text tha
231b0 74 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 20 t describes.**
231c0 20 20 20 20 20 20 20 20 74 68 65 20 65 72 72 6f the erro
231d0 72 20 69 6e 20 74 68 65 20 6d 6f 73 74 6c 79 20 r in the mostly
231e0 72 65 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 recently failed
231f0 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 2c 0a interface call,.
23200 2a 2a 20 20 20 20 20 20 20 20 20 20 65 6e 63 6f ** enco
23210 64 65 64 20 61 73 20 65 69 74 68 65 72 20 55 54 ded as either UT
23220 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 F-8 or UTF-16 re
23230 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a spectively..**.*
23240 2a 20 7b 48 31 32 38 30 37 7d 20 54 68 65 20 73 * {H12807} The s
23250 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 trings returned
23260 62 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d by [sqlite3_errm
23270 73 67 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 sg()] and [sqlit
23280 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 0a 2a e3_errmsg16()].*
23290 2a 20 20 20 20 20 20 20 20 20 20 61 72 65 20 76 * are v
232a0 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e alid until the n
232b0 65 78 74 20 53 51 4c 69 74 65 20 69 6e 74 65 72 ext SQLite inter
232c0 66 61 63 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a face call..**.**
232d0 20 7b 48 31 32 38 30 38 7d 20 43 61 6c 6c 73 20 {H12808} Calls
232e0 74 6f 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 to API routines
232f0 74 68 61 74 20 64 6f 20 6e 6f 74 20 72 65 74 75 that do not retu
23300 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 rn an error code
23310 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 28 65 78 .** (ex
23320 61 6d 70 6c 65 3a 20 5b 73 71 6c 69 74 65 33 5f ample: [sqlite3_
23330 64 61 74 61 5f 63 6f 75 6e 74 28 29 5d 29 20 64 data_count()]) d
23340 6f 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 o not.**
23350 20 20 63 68 61 6e 67 65 20 74 68 65 20 65 72 72 change the err
23360 6f 72 20 63 6f 64 65 20 6f 72 20 6d 65 73 73 61 or code or messa
23370 67 65 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a ge returned by.*
23380 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
23390 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 te3_errcode()],
233a0 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 [sqlite3_extende
233b0 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a d_errcode()],.**
233c0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
233d0 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 6f 72 e3_errmsg()], or
233e0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 [sqlite3_errmsg
233f0 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 16()]..**.** {H1
23400 32 38 30 39 7d 20 49 6e 74 65 72 66 61 63 65 73 2809} Interfaces
23410 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 73 that are not as
23420 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 sociated with a
23430 73 70 65 63 69 66 69 63 0a 2a 2a 20 20 20 20 20 specific.**
23440 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 [database c
23450 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 65 78 61 6d onnection] (exam
23460 70 6c 65 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 ples:.**
23470 20 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e [sqlite3_mprin
23480 74 66 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 tf()] or [sqlite
23490 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 3_enable_shared_
234a0 63 61 63 68 65 28 29 5d 0a 2a 2a 20 20 20 20 20 cache()].**
234b0 20 20 20 20 20 64 6f 20 6e 6f 74 20 63 68 61 6e do not chan
234c0 67 65 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 ge the values re
234d0 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20 20 turned by.**
234e0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 [sqlite3_e
234f0 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69 rrcode()], [sqli
23500 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 te3_extended_err
23510 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 code()],.**
23520 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 [sqlite3_er
23530 72 6d 73 67 28 29 5d 2c 20 6f 72 20 5b 73 71 6c rmsg()], or [sql
23540 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d ite3_errmsg16()]
23550 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
23560 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 int sqlite3_errc
23570 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ode(sqlite3 *db)
23580 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
23590 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 sqlite3_extende
235a0 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 d_errcode(sqlite
235b0 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 3 *db);.SQLITE_A
235c0 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
235d0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 qlite3_errmsg(sq
235e0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
235f0 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
23600 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 sqlite3_errmsg16
23610 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
23620 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c ** CAPI3REF: SQL
23630 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 Statement Objec
23640 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30 t {H13000} <H130
23650 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 10>.** KEYWORDS:
23660 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 {prepared state
23670 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20 ment} {prepared
23680 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a statements}.**.*
23690 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
236a0 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 this object rep
236b0 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 resents a single
236c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a SQL statement..
236d0 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 ** This object i
236e0 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77 s variously know
236f0 6e 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64 n as a "prepared
23700 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61 statement" or a
23710 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51 .** "compiled SQ
23720 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 L statement" or
23730 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61 simply as a "sta
23740 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 tement"..**.** T
23750 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61 he life of a sta
23760 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f tement object go
23770 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b es something lik
23780 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f e this:.**.** <o
23790 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74 l>.** <li> Creat
237a0 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 e the object usi
237b0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ng [sqlite3_prep
237c0 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72 are_v2()] or a r
237d0 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66 elated.** f
237e0 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e unction..** <li>
237f0 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 Bind values to
23800 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 [host parameters
23810 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 ] using the sqli
23820 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20 te3_bind_*().**
23830 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e interfaces.
23840 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65 .** <li> Run the
23850 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20 SQL by calling
23860 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
23870 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d one or more tim
23880 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65 es..** <li> Rese
23890 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 t the statement
238a0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 using [sqlite3_r
238b0 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20 eset()] then go
238c0 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 back.** to
238d0 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73 step 2. Do this
238e0 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69 zero or more ti
238f0 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73 mes..** <li> Des
23900 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20 troy the object
23910 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 using [sqlite3_f
23920 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c inalize()]..** <
23930 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 /ol>.**.** Refer
23940 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f to documentatio
23950 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 n on individual
23960 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f methods above fo
23970 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 r additional.**
23980 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
23990 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
239a0 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69 qlite3_stmt sqli
239b0 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a te3_stmt;../*.**
239c0 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74 CAPI3REF: Run-t
239d0 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37 ime Limits {H127
239e0 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a 60} <S20600>.**.
239f0 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 ** This interfac
23a00 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a e allows the siz
23a10 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e e of various con
23a20 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69 structs to be li
23a30 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f mited.** on a co
23a40 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e nnection by conn
23a50 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54 ection basis. T
23a60 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
23a70 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61 er is the.** [da
23a80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
23a90 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69 n] whose limit i
23aa0 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71 s to be set or q
23ab0 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20 ueried. The.**
23ac0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
23ad0 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b is one of the [
23ae0 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 limit categories
23af0 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a ] that define a.
23b00 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73 ** class of cons
23b10 74 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a tructs to be siz
23b20 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20 e limited. The
23b30 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 third parameter
23b40 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 is the.** new li
23b50 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e mit for that con
23b60 73 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e struct. The fun
23b70 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
23b80 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a e old limit..**.
23b90 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69 ** If the new li
23ba0 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76 mit is a negativ
23bb0 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 e number, the li
23bc0 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 mit is unchanged
23bd0 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d ..** For the lim
23be0 69 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53 it category of S
23bf0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20 QLITE_LIMIT_XYZ
23c00 74 68 65 72 65 20 69 73 20 61 20 68 61 72 64 20 there is a hard
23c10 75 70 70 65 72 0a 2a 2a 20 62 6f 75 6e 64 20 73 upper.** bound s
23c20 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d et by a compile-
23c30 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 time C preproces
23c40 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 sor macro named
23c50 53 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 2e 0a SQLITE_MAX_XYZ..
23c60 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f ** (The "_LIMIT_
23c70 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 " in the name is
23c80 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 changed to "_MA
23c90 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 X_".).** Attempt
23ca0 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 s to increase a
23cb0 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 limit above its
23cc0 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 hard upper bound
23cd0 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 are.** silently
23ce0 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 truncated to th
23cf0 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d e hard upper lim
23d00 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 it..**.** Run ti
23d10 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e me limits are in
23d20 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 tended for use i
23d30 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 n applications t
23d40 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f hat manage.** bo
23d50 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 th their own int
23d60 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 ernal database a
23d70 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 nd also database
23d80 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 s that are contr
23d90 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 olled.** by untr
23da0 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 usted external s
23db0 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d ources. An exam
23dc0 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 ple application
23dd0 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 might be a.** we
23de0 62 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 bbrowser that ha
23df0 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 s its own databa
23e00 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 ses for storing
23e10 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 history and.** s
23e20 65 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65 eparate database
23e30 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 s controlled by
23e40 4a 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 JavaScript appli
23e50 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 cations download
23e60 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e ed.** off the In
23e70 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 ternet. The int
23e80 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 ernal databases
23e90 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 can be given the
23ea0 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 .** large, defau
23eb0 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 lt limits. Data
23ec0 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 bases managed by
23ed0 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 external source
23ee0 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 s can.** be give
23ef0 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c n much smaller l
23f00 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 imits designed t
23f10 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 o prevent a deni
23f20 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a al of service.**
23f30 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f attack. Develo
23f40 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 pers might also
23f50 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 want to use the
23f60 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 [sqlite3_set_aut
23f70 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e horizer()].** in
23f80 74 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 terface to furth
23f90 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 er control untru
23fa0 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 sted SQL. The s
23fb0 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
23fc0 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 ase.** created b
23fd0 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 y an untrusted s
23fe0 63 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e cript can be con
23ff0 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 tained using the
24000 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f .** [max_page_co
24010 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a unt] [PRAGMA]..*
24020 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d *.** New run-tim
24030 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 e limit categori
24040 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 es may be added
24050 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
24060 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 es..**.** INVARI
24070 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 ANTS:.**.** {H12
24080 37 36 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 762} A successfu
24090 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 l call to [sqlit
240a0 65 33 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d e3_limit(D,C,V)]
240b0 20 77 68 65 72 65 20 56 20 69 73 0a 2a 2a 20 20 where V is.**
240c0 20 20 20 20 20 20 20 20 70 6f 73 69 74 69 76 65 positive
240d0 20 63 68 61 6e 67 65 73 20 74 68 65 20 6c 69 6d changes the lim
240e0 69 74 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f it on the size o
240f0 66 20 63 6f 6e 73 74 72 75 63 74 20 43 20 69 6e f construct C in
24100 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
24110 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
24120 63 74 69 6f 6e 5d 20 44 20 74 6f 20 74 68 65 20 ction] D to the
24130 6c 65 73 73 65 72 20 6f 66 20 56 20 61 6e 64 20 lesser of V and
24140 74 68 65 20 68 61 72 64 20 75 70 70 65 72 0a 2a the hard upper.*
24150 2a 20 20 20 20 20 20 20 20 20 20 62 6f 75 6e 64 * bound
24160 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 on the size of
24170 43 20 74 68 61 74 20 69 73 20 73 65 74 20 61 74 C that is set at
24180 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a compile-time..*
24190 2a 0a 2a 2a 20 7b 48 31 32 37 36 36 7d 20 41 20 *.** {H12766} A
241a0 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 successful call
241b0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 to [sqlite3_limi
241c0 74 28 44 2c 43 2c 56 29 5d 20 77 68 65 72 65 20 t(D,C,V)] where
241d0 56 20 69 73 20 6e 65 67 61 74 69 76 65 0a 2a 2a V is negative.**
241e0 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73 leaves
241f0 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 the state of th
24200 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
24210 65 63 74 69 6f 6e 5d 20 44 20 75 6e 63 68 61 6e ection] D unchan
24220 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 ged..**.** {H127
24230 36 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 69} A successful
24240 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
24250 33 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20 3_limit(D,C,V)]
24260 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 20 returns the.**
24270 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 value of
24280 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 the limit on th
24290 65 20 73 69 7a 65 20 6f 66 20 63 6f 6e 73 74 72 e size of constr
242a0 75 63 74 20 43 20 69 6e 20 74 68 65 0a 2a 2a 20 uct C in the.**
242b0 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 [databa
242c0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 se connection] D
242d0 20 61 73 20 69 74 20 77 61 73 20 70 72 69 6f 72 as it was prior
242e0 20 74 6f 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2f to the call..*/
242f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
24300 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71 sqlite3_limit(sq
24310 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20 lite3*, int id,
24320 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a int newVal);../*
24330 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 .** CAPI3REF: Ru
24340 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74 n-Time Limit Cat
24350 65 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d egories {H12790}
24360 20 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59 <H12760>.** KEY
24370 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61 WORDS: {limit ca
24380 74 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63 tegory} {limit c
24390 61 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a ategories}.**.**
243a0 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 These constants
243b0 20 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 define various
243c0 61 73 70 65 63 74 73 20 6f 66 20 61 20 5b 64 61 aspects of a [da
243d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
243e0 6e 5d 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 n].** that can b
243f0 65 20 6c 69 6d 69 74 65 64 20 69 6e 20 73 69 7a e limited in siz
24400 65 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 5b 73 e by calls to [s
24410 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e qlite3_limit()].
24420 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 .** The meanings
24430 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 of the various
24440 6c 69 6d 69 74 73 20 61 72 65 20 61 73 20 66 6f limits are as fo
24450 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c llows:.**.** <dl
24460 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f >.** <dt>SQLITE_
24470 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 LIMIT_LENGTH</dt
24480 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 >.** <dd>The max
24490 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 imum size of any
244a0 20 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 string or BLOB
244b0 6f 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 or table row.<dd
244c0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
244d0 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e TE_LIMIT_SQL_LEN
244e0 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e GTH</dt>.** <dd>
244f0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 The maximum leng
24500 74 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 th of an SQL sta
24510 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a tement.</dd>.**.
24520 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 ** <dt>SQLITE_LI
24530 4d 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a MIT_COLUMN</dt>.
24540 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
24550 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c um number of col
24560 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 umns in a table
24570 64 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e definition or in
24580 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 the.** result s
24590 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 6f et of a SELECT o
245a0 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 r the maximum nu
245b0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
245c0 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f in an index.** o
245d0 72 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 r in an ORDER BY
245e0 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 or GROUP BY cla
245f0 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 use.</dd>.**.**
24600 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
24610 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e _EXPR_DEPTH</dt>
24620 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
24630 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 mum depth of the
24640 20 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 parse tree on a
24650 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f ny expression.</
24660 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
24670 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f LITE_LIMIT_COMPO
24680 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a UND_SELECT</dt>.
24690 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
246a0 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 um number of ter
246b0 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 ms in a compound
246c0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
246d0 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 t.</dd>.**.** <d
246e0 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 t>SQLITE_LIMIT_V
246f0 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c DBE_OP</dt>.** <
24700 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e dd>The maximum n
24710 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 umber of instruc
24720 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 tions in a virtu
24730 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 al machine progr
24740 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d am.** used to im
24750 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 plement an SQL s
24760 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a tatement.</dd>.*
24770 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
24780 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 LIMIT_FUNCTION_A
24790 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 RG</dt>.** <dd>T
247a0 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
247b0 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f r of arguments o
247c0 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 n a function.</d
247d0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
247e0 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 ITE_LIMIT_ATTACH
247f0 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ED</dt>.** <dd>T
24800 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
24810 72 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 r of attached da
24820 74 61 62 61 73 65 73 2e 3c 2f 64 64 3e 0a 2a 2a tabases.</dd>.**
24830 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
24840 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 IMIT_LIKE_PATTER
24850 4e 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a N_LENGTH</dt>.**
24860 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d <dd>The maximum
24870 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 length of the p
24880 61 74 74 65 72 6e 20 61 72 67 75 6d 65 6e 74 20 attern argument
24890 74 6f 20 74 68 65 20 4c 49 4b 45 20 6f 72 0a 2a to the LIKE or.*
248a0 2a 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 * GLOB operators
248b0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
248c0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 >SQLITE_LIMIT_VA
248d0 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 RIABLE_NUMBER</d
248e0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 t>.** <dd>The ma
248f0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
24900 76 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 variables in an
24910 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 SQL statement th
24920 61 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 at can.** be bou
24930 6e 64 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c nd.</dd>.** </dl
24940 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c >.*/.#define SQL
24950 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
24960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24970 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 0.#define SQ
24980 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c LITE_LIMIT_SQL_L
24990 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20 ENGTH
249a0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 1.#define S
249b0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 QLITE_LIMIT_COLU
249c0 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MN
249d0 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 2.#define
249e0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 SQLITE_LIMIT_EXP
249f0 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20 R_DEPTH
24a00 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 3.#define
24a10 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f SQLITE_LIMIT_CO
24a20 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 20 MPOUND_SELECT
24a30 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 4.#defin
24a40 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 e SQLITE_LIMIT_V
24a50 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 20 DBE_OP
24a60 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 5.#defi
24a70 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
24a80 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 20 FUNCTION_ARG
24a90 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 6.#def
24aa0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
24ab0 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 20 20 _ATTACHED
24ac0 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 7.#de
24ad0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
24ae0 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c T_LIKE_PATTERN_L
24af0 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 64 ENGTH 8.#d
24b00 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
24b10 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 IT_VARIABLE_NUMB
24b20 45 52 20 20 20 20 20 20 20 20 20 20 20 39 0a 0a ER 9..
24b30 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
24b40 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c Compiling An SQL
24b50 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 30 Statement {H130
24b60 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 10} <S10000>.**
24b70 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 KEYWORDS: {SQL s
24b80 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 tatement compile
24b90 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 r}.**.** To exec
24ba0 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 ute an SQL query
24bb0 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 20 , it must first
24bc0 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f be compiled into
24bd0 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 a byte-code.**
24be0 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e program using on
24bf0 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 e of these routi
24c00 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 nes..**.** The f
24c10 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 22 irst argument, "
24c20 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61 62 db", is a [datab
24c30 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
24c40 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a obtained from a.
24c50 2a 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f ** prior call to
24c60 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 [sqlite3_open()
24c70 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e ], [sqlite3_open
24c80 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 _v2()] or [sqlit
24c90 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a e3_open16()]..**
24ca0 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 .** The second a
24cb0 72 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c rgument, "zSql",
24cc0 20 69 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e is the statemen
24cd0 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 t to be compiled
24ce0 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 , encoded.** as
24cf0 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 either UTF-8 or
24d00 55 54 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c UTF-16. The sql
24d10 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 ite3_prepare() a
24d20 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 nd sqlite3_prepa
24d30 72 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 re_v2().** inter
24d40 66 61 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c faces use UTF-8,
24d50 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 and sqlite3_pre
24d60 70 61 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c pare16() and sql
24d70 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 ite3_prepare16_v
24d80 32 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 2().** use UTF-1
24d90 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6..**.** If the
24da0 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 nByte argument i
24db0 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f s less than zero
24dc0 2c 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 , then zSql is r
24dd0 65 61 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a ead up to the.**
24de0 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d first zero term
24df0 69 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 inator. If nByte
24e00 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 is non-negative
24e10 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 , then it is the
24e20 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 maximum.** numb
24e30 65 72 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 er of bytes rea
24e40 64 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 d from zSql. Wh
24e50 65 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d en nByte is non-
24e60 6e 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a negative, the.**
24e70 20 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 zSql string end
24e80 73 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 s at either the
24e90 66 69 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 first '\000' or
24ea0 27 5c 75 30 30 30 30 27 20 63 68 61 72 61 63 74 '\u0000' charact
24eb0 65 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 er or.** the nBy
24ec0 74 65 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 te-th byte, whic
24ed0 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 hever comes firs
24ee0 74 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 t. If the caller
24ef0 20 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 knows.** that t
24f00 68 65 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 he supplied stri
24f10 6e 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e ng is nul-termin
24f20 61 74 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 ated, then there
24f30 20 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 is a small.** p
24f40 65 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e erformance advan
24f50 74 61 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 tage to be gaine
24f60 64 20 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 d by passing an
24f70 6e 42 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 nByte parameter
24f80 74 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c that.** is equal
24f90 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
24fa0 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 f bytes in the i
24fb0 6e 70 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 nput string <i>i
24fc0 6e 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 ncluding</i>.**
24fd0 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 the nul-terminat
24fe0 6f 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 or bytes..**.**
24ff0 2a 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 *pzTail is made
25000 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
25010 66 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 first byte past
25020 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a the end of the.*
25030 2a 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 * first SQL stat
25040 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 ement in zSql.
25050 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f These routines o
25060 6e 6c 79 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 nly compile the
25070 66 69 72 73 74 0a 2a 2a 20 73 74 61 74 65 6d 65 first.** stateme
25080 6e 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a nt in zSql, so *
25090 70 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 pzTail is left p
250a0 6f 69 6e 74 69 6e 67 20 74 6f 20 77 68 61 74 20 ointing to what
250b0 72 65 6d 61 69 6e 73 0a 2a 2a 20 75 6e 63 6f 6d remains.** uncom
250c0 70 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 piled..**.** *pp
250d0 53 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 Stmt is left poi
250e0 6e 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 nting to a compi
250f0 6c 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 led [prepared st
25100 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 atement] that ca
25110 6e 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 n be.** executed
25120 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
25130 73 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 step()]. If the
25140 72 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 re is an error,
25150 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a *ppStmt is set.*
25160 2a 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 * to NULL. If t
25170 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f he input text co
25180 6e 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 ntains no SQL (i
25190 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 f the input is a
251a0 6e 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e n empty.** strin
251b0 67 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 g or a comment)
251c0 74 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 then *ppStmt is
251d0 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 set to NULL..**
251e0 7b 41 31 33 30 31 38 7d 20 54 68 65 20 63 61 6c {A13018} The cal
251f0 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 ling procedure i
25200 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
25210 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 r deleting the c
25220 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73 ompiled.** SQL s
25230 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b tatement using [
25240 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
25250 28 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 73 ()] after it has
25260 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 finished with i
25270 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 t..**.** On succ
25280 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d ess, [SQLITE_OK]
25290 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 74 is returned, ot
252a0 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f herwise an [erro
252b0 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 r code] is retur
252c0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ned..**.** The s
252d0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
252e0 32 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 2() and sqlite3_
252f0 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 69 prepare16_v2() i
25300 6e 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a 2a nterfaces are.**
25310 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 recommended for
25320 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 6d all new program
25330 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 72 s. The two older
25340 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 interfaces are
25350 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 retained.** for
25360 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 backwards compat
25370 69 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 65 ibility, but the
25380 69 72 20 75 73 65 20 69 73 20 64 69 73 63 6f 75 ir use is discou
25390 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 raged..** In the
253a0 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 73 "v2" interfaces
253b0 2c 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 , the prepared s
253c0 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 tatement.** that
253d0 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 68 is returned (th
253e0 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d e [sqlite3_stmt]
253f0 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e object) contain
25400 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a s a copy of the.
25410 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 ** original SQL
25420 74 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 65 text. This cause
25430 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 s the [sqlite3_s
25440 74 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65 tep()] interface
25450 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20 to.** behave a
25460 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74 differently in t
25470 77 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c wo ways:.**.** <
25480 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 ol>.** <li>.** I
25490 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 f the database s
254a0 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69 chema changes, i
254b0 6e 73 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e nstead of return
254c0 69 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 ing [SQLITE_SCHE
254d0 4d 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77 MA] as it.** alw
254e0 61 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20 ays used to do,
254f0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
25500 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 will automatica
25510 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68 lly recompile th
25520 65 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 e SQL.** stateme
25530 6e 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75 nt and try to ru
25540 6e 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20 n it again. If
25550 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 the schema has c
25560 68 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 77 hanged in.** a w
25570 61 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 ay that makes th
25580 65 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c e statement no l
25590 6f 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71 onger valid, [sq
255a0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 lite3_step()] wi
255b0 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 ll still.** retu
255c0 72 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d rn [SQLITE_SCHEM
255d0 41 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20 A]. But unlike
255e0 74 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 the legacy behav
255f0 69 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 ior, [SQLITE_SCH
25600 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 EMA] is.** now a
25610 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43 fatal error. C
25620 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f alling [sqlite3_
25630 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 67 prepare_v2()] ag
25640 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b ain will not mak
25650 65 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 67 e the.** error g
25660 6f 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 75 o away. Note: u
25670 73 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d se [sqlite3_errm
25680 73 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 68 sg()] to find th
25690 65 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68 65 e text.** of the
256a0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 74 parsing error t
256b0 68 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61 hat results in a
256c0 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 n [SQLITE_SCHEMA
256d0 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c ] return..** </l
256e0 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a i>.**.** <li>.**
256f0 20 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f When an error o
25700 63 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 5f ccurs, [sqlite3_
25710 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 74 step()] will ret
25720 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 urn one of the d
25730 65 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f etailed.** [erro
25740 72 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78 74 r codes] or [ext
25750 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 ended error code
25760 73 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79 20 s]. The legacy
25770 62 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 61 behavior was tha
25780 74 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 t.** [sqlite3_st
25790 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 ep()] would only
257a0 20 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72 69 return a generi
257b0 63 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d c [SQLITE_ERROR]
257c0 20 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 result code.**
257d0 61 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68 61 and you would ha
257e0 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 63 ve to make a sec
257f0 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c ond call to [sql
25800 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e ite3_reset()] in
25810 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69 6e order.** to fin
25820 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 d the underlying
25830 20 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72 cause of the pr
25840 6f 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65 20 oblem. With the
25850 22 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a 20 "v2" prepare.**
25860 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20 interfaces, the
25870 75 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73 6f underlying reaso
25880 6e 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 n for the error
25890 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 is returned imme
258a0 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 diately..** </li
258b0 3e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a >.** </ol>.**.**
258c0 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
258d0 2a 2a 20 7b 48 31 33 30 31 31 7d 20 54 68 65 20 ** {H13011} The
258e0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
258f0 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 61 (db,zSql,...)] a
25900 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b nd.** [
25910 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
25920 76 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d v2(db,zSql,...)]
25930 20 69 6e 74 65 72 66 61 63 65 73 20 69 6e 74 65 interfaces inte
25940 72 70 72 65 74 20 74 68 65 0a 2a 2a 20 20 20 20 rpret the.**
25950 20 20 20 20 20 20 74 65 78 74 20 69 6e 20 74 68 text in th
25960 65 69 72 20 7a 53 71 6c 20 70 61 72 61 6d 65 74 eir zSql paramet
25970 65 72 20 61 73 20 55 54 46 2d 38 2e 0a 2a 2a 0a er as UTF-8..**.
25980 2a 2a 20 7b 48 31 33 30 31 32 7d 20 54 68 65 20 ** {H13012} The
25990 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
259a0 31 36 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 16(db,zSql,...)]
259b0 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 and.**
259c0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
259d0 65 31 36 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 2e e16_v2(db,zSql,.
259e0 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 ..)] interfaces
259f0 69 6e 74 65 72 70 72 65 74 20 74 68 65 0a 2a 2a interpret the.**
25a00 20 20 20 20 20 20 20 20 20 20 74 65 78 74 20 69 text i
25a10 6e 20 74 68 65 69 72 20 7a 53 71 6c 20 70 61 72 n their zSql par
25a20 61 6d 65 74 65 72 20 61 73 20 55 54 46 2d 31 36 ameter as UTF-16
25a30 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 in the native b
25a40 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a yte order..**.**
25a50 20 7b 48 31 33 30 31 33 7d 20 49 66 20 74 68 65 {H13013} If the
25a60 20 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 nByte argument
25a70 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 to [sqlite3_prep
25a80 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 6e are_v2(db,zSql,n
25a90 42 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 Byte,...)].**
25aa0 20 20 20 20 20 20 20 61 6e 64 20 69 74 73 20 76 and its v
25ab0 61 72 69 61 6e 74 73 20 69 73 20 6c 65 73 73 20 ariants is less
25ac0 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 20 53 than zero, the S
25ad0 51 4c 20 74 65 78 74 20 69 73 0a 2a 2a 20 20 20 QL text is.**
25ae0 20 20 20 20 20 20 20 72 65 61 64 20 66 72 6f 6d read from
25af0 20 7a 53 71 6c 20 69 73 20 72 65 61 64 20 75 70 zSql is read up
25b00 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 to the first ze
25b10 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a ro terminator..*
25b20 2a 0a 2a 2a 20 7b 48 31 33 30 31 34 7d 20 49 66 *.** {H13014} If
25b30 20 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d the nByte argum
25b40 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ent to [sqlite3_
25b50 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 7a 53 prepare_v2(db,zS
25b60 71 6c 2c 6e 42 79 74 65 2c 2e 2e 2e 29 5d 0a 2a ql,nByte,...)].*
25b70 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 69 * and i
25b80 74 73 20 76 61 72 69 61 6e 74 73 20 69 73 20 6e ts variants is n
25b90 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 on-negative, the
25ba0 6e 20 61 74 20 6d 6f 73 74 20 6e 42 79 74 65 73 n at most nBytes
25bb0 20 62 79 74 65 73 20 6f 66 0a 2a 2a 20 20 20 20 bytes of.**
25bc0 20 20 20 20 20 20 53 51 4c 20 74 65 78 74 20 69 SQL text i
25bd0 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c s read from zSql
25be0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30 31 35 7d ..**.** {H13015}
25bf0 20 49 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 In [sqlite3_pre
25c00 70 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c pare_v2(db,zSql,
25c10 4e 2c 50 2c 70 7a 54 61 69 6c 29 5d 20 61 6e 64 N,P,pzTail)] and
25c20 20 69 74 73 20 76 61 72 69 61 6e 74 73 0a 2a 2a its variants.**
25c30 20 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 if the
25c40 20 7a 53 71 6c 20 69 6e 70 75 74 20 74 65 78 74 zSql input text
25c50 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 contains more t
25c60 68 61 6e 20 6f 6e 65 20 53 51 4c 20 73 74 61 74 han one SQL stat
25c70 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 ement.**
25c80 20 20 61 6e 64 20 70 7a 54 61 69 6c 20 69 73 20 and pzTail is
25c90 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a not NULL, then *
25ca0 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 pzTail is made t
25cb0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a o point to the.*
25cc0 2a 20 20 20 20 20 20 20 20 20 20 66 69 72 73 74 * first
25cd0 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 byte past the e
25ce0 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 nd of the first
25cf0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e SQL statement in
25d00 20 7a 53 71 6c 2e 0a 2a 2a 20 20 20 20 20 20 20 zSql..**
25d10 20 20 20 3c 74 6f 64 6f 3e 57 68 61 74 20 64 6f <todo>What do
25d20 65 73 20 2a 70 7a 54 61 69 6c 20 70 6f 69 6e 74 es *pzTail point
25d30 20 74 6f 20 69 66 20 74 68 65 72 65 20 69 73 20 to if there is
25d40 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 3f 3c 2f one statement?</
25d50 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 todo>.**.** {H13
25d60 30 31 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 016} A successfu
25d70 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 l call to [sqlit
25d80 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 e3_prepare_v2(db
25d90 2c 7a 53 71 6c 2c 4e 2c 70 70 53 74 6d 74 2c 2e ,zSql,N,ppStmt,.
25da0 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..)].**
25db0 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 or one of its v
25dc0 61 72 69 61 6e 74 73 20 77 72 69 74 65 73 20 69 ariants writes i
25dd0 6e 74 6f 20 2a 70 70 53 74 6d 74 20 61 20 70 6f nto *ppStmt a po
25de0 69 6e 74 65 72 20 74 6f 20 61 20 6e 65 77 0a 2a inter to a new.*
25df0 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 * [prep
25e00 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
25e10 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 or a pointer to
25e20 4e 55 4c 4c 20 69 66 20 7a 53 71 6c 20 63 6f 6e NULL if zSql con
25e30 74 61 69 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 tains.**
25e40 20 20 6e 6f 74 68 69 6e 67 20 6f 74 68 65 72 20 nothing other
25e50 74 68 61 6e 20 77 68 69 74 65 73 70 61 63 65 20 than whitespace
25e60 6f 72 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 0a or comments..**.
25e70 2a 2a 20 7b 48 31 33 30 31 39 7d 20 54 68 65 20 ** {H13019} The
25e80 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
25e90 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 _v2()] interface
25ea0 20 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 and its variant
25eb0 73 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 s return.**
25ec0 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d [SQLITE_OK]
25ed0 20 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 or an appropria
25ee0 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 te [error code]
25ef0 75 70 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a upon failure..**
25f00 0a 2a 2a 20 7b 48 31 33 30 32 31 7d 20 42 65 66 .** {H13021} Bef
25f10 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 ore [sqlite3_pre
25f20 70 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 pare(db,zSql,nBy
25f30 74 65 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c te,ppStmt,pzTail
25f40 29 5d 20 6f 72 20 69 74 73 0a 2a 2a 20 20 20 20 )] or its.**
25f50 20 20 20 20 20 20 76 61 72 69 61 6e 74 73 20 72 variants r
25f60 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 eturns an error
25f70 28 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72 (any value other
25f80 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b than [SQLITE_OK
25f90 5d 29 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ]),.**
25fa0 74 68 65 79 20 66 69 72 73 74 20 73 65 74 20 2a they first set *
25fb0 70 70 53 74 6d 74 20 74 6f 20 4e 55 4c 4c 2e 0a ppStmt to NULL..
25fc0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
25fd0 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 t sqlite3_prepar
25fe0 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 e(. sqlite3 *db
25ff0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
26000 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 Database handle
26010 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
26020 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 *zSql, /*
26030 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 SQL statement, U
26040 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a TF-8 encoded */.
26050 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
26060 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
26070 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a imum length of z
26080 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f Sql in bytes. */
26090 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
260a0 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 **ppStmt, /* OU
260b0 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e T: Statement han
260c0 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 dle */. const c
260d0 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 har **pzTail
260e0 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 /* OUT: Pointer
260f0 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 to unused porti
26100 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b on of zSql */.);
26110 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
26120 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
26130 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 v2(. sqlite3 *d
26140 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b, /*
26150 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 Database handle
26160 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
26170 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a *zSql, /*
26180 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 SQL statement,
26190 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f UTF-8 encoded */
261a0 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 . int nByte,
261b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
261c0 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
261d0 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a zSql in bytes. *
261e0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 /. sqlite3_stmt
261f0 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f **ppStmt, /* O
26200 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 UT: Statement ha
26210 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
26220 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 char **pzTail
26230 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 /* OUT: Pointe
26240 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 r to unused port
26250 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 ion of zSql */.)
26260 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
26270 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
26280 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 16(. sqlite3 *d
26290 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b, /*
262a0 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 Database handle
262b0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
262c0 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a *zSql, /*
262d0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 SQL statement,
262e0 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a UTF-16 encoded *
262f0 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 /. int nByte,
26300 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
26310 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 aximum length of
26320 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 zSql in bytes.
26330 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d */. sqlite3_stm
26340 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 t **ppStmt, /*
26350 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 OUT: Statement h
26360 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
26370 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 void **pzTail
26380 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 /* OUT: Point
26390 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 er to unused por
263a0 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a tion of zSql */.
263b0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
263c0 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 t sqlite3_prepar
263d0 65 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 e16_v2(. sqlite
263e0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
263f0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 /* Database ha
26400 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
26410 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 void *zSql,
26420 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 /* SQL stateme
26430 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 nt, UTF-16 encod
26440 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 ed */. int nByt
26450 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
26460 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 /* Maximum lengt
26470 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 h of zSql in byt
26480 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 es. */. sqlite3
26490 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 _stmt **ppStmt,
264a0 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 /* OUT: Stateme
264b0 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 nt handle */. c
264c0 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 onst void **pzTa
264d0 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 il /* OUT: P
264e0 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 ointer to unused
264f0 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c portion of zSql
26500 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.);../*.** CA
26510 50 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 69 PI3REF: Retrievi
26520 6e 67 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c ng Statement SQL
26530 20 7b 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30 {H13100} <H1300
26540 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 0>.**.** This in
26550 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 terface can be u
26560 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 sed to retrieve
26570 61 20 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20 a saved copy of
26580 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 the original.**
26590 53 51 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f SQL text used to
265a0 20 63 72 65 61 74 65 20 61 20 5b 70 72 65 70 61 create a [prepa
265b0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 red statement] i
265c0 66 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 f that statement
265d0 20 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 was.** compiled
265e0 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 using either [s
265f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
26600 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 2()] or [sqlite3
26610 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d _prepare16_v2()]
26620 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e ..**.** INVARIAN
26630 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 30 TS:.**.** {H1310
26640 31 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70 61 1} If the [prepa
26650 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 70 red statement] p
26660 61 73 73 65 64 20 61 73 20 74 68 65 20 61 72 67 assed as the arg
26670 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 ument to.**
26680 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 71 [sqlite3_sq
26690 6c 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c 65 l()] was compile
266a0 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b d using either [
266b0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
266c0 76 32 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 v2()] or.**
266d0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 [sqlite3_pr
266e0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2c 20 74 epare16_v2()], t
266f0 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c hen [sqlite3_sql
26700 28 29 5d 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 ()] returns.**
26710 20 20 20 20 20 20 20 20 61 20 70 6f 69 6e 74 65 a pointe
26720 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d r to a zero-term
26730 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 63 6f inated string co
26740 6e 74 61 69 6e 69 6e 67 20 61 20 55 54 46 2d 38 ntaining a UTF-8
26750 20 72 65 6e 64 65 72 69 6e 67 0a 2a 2a 20 20 20 rendering.**
26760 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6f 72 of the or
26770 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 iginal SQL state
26780 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 ment..**.** {H13
26790 31 30 32 7d 20 49 66 20 74 68 65 20 5b 70 72 65 102} If the [pre
267a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
267b0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 61 passed as the a
267c0 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 rgument to.**
267d0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
267e0 73 71 6c 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 sql()] was compi
267f0 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 led using either
26800 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
26810 65 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 e()] or.**
26820 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 [sqlite3_pre
26830 70 61 72 65 31 36 28 29 5d 2c 20 74 68 65 6e 20 pare16()], then
26840 5b 73 71 6c 69 74 65 33 5f 73 71 6c 28 29 5d 20 [sqlite3_sql()]
26850 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 returns a NULL p
26860 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ointer..**.** {H
26870 31 33 31 30 33 7d 20 54 68 65 20 73 74 72 69 6e 13103} The strin
26880 67 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 g returned by [s
26890 71 6c 69 74 65 33 5f 73 71 6c 28 53 29 5d 20 69 qlite3_sql(S)] i
268a0 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 s valid until th
268b0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 e.** [p
268c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
268d0 74 5d 20 53 20 69 73 20 64 65 6c 65 74 65 64 20 t] S is deleted
268e0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 using [sqlite3_f
268f0 69 6e 61 6c 69 7a 65 28 53 29 5d 2e 0a 2a 2f 0a inalize(S)]..*/.
26900 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
26910 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 char *sqlite3_s
26920 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ql(sqlite3_stmt
26930 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *pStmt);../*.**
26940 43 41 50 49 33 52 45 46 3a 20 44 79 6e 61 6d 69 CAPI3REF: Dynami
26950 63 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75 cally Typed Valu
26960 65 20 4f 62 6a 65 63 74 20 7b 48 31 35 30 30 30 e Object {H15000
26970 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 } <S20200>.** KE
26980 59 57 4f 52 44 53 3a 20 7b 70 72 6f 74 65 63 74 YWORDS: {protect
26990 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
269a0 7d 20 7b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 } {unprotected s
269b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a qlite3_value}.**
269c0 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 .** SQLite uses
269d0 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 the sqlite3_valu
269e0 65 20 6f 62 6a 65 63 74 20 74 6f 20 72 65 70 72 e object to repr
269f0 65 73 65 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73 esent all values
26a00 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 .** that can be
26a10 73 74 6f 72 65 64 20 69 6e 20 61 20 64 61 74 61 stored in a data
26a20 62 61 73 65 20 74 61 62 6c 65 2e 20 53 51 4c 69 base table. SQLi
26a30 74 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 20 te uses dynamic
26a40 74 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 typing.** for th
26a50 65 20 76 61 6c 75 65 73 20 69 74 20 73 74 6f 72 e values it stor
26a60 65 73 2e 20 56 61 6c 75 65 73 20 73 74 6f 72 65 es. Values store
26a70 64 20 69 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c d in sqlite3_val
26a80 75 65 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61 ue objects.** ca
26a90 6e 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66 n be integers, f
26aa0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
26ab0 6c 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 lues, strings, B
26ac0 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a LOBs, or NULL..*
26ad0 2a 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f *.** An sqlite3_
26ae0 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 value object may
26af0 20 62 65 20 65 69 74 68 65 72 20 22 70 72 6f 74 be either "prot
26b00 65 63 74 65 64 22 20 6f 72 20 22 75 6e 70 72 6f ected" or "unpro
26b10 74 65 63 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 tected"..** Some
26b20 20 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 interfaces requ
26b30 69 72 65 20 61 20 70 72 6f 74 65 63 74 65 64 20 ire a protected
26b40 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 sqlite3_value.
26b50 4f 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 Other interfaces
26b60 0a 2a 2a 20 77 69 6c 6c 20 61 63 63 65 70 74 20 .** will accept
26b70 65 69 74 68 65 72 20 61 20 70 72 6f 74 65 63 74 either a protect
26b80 65 64 20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 ed or an unprote
26b90 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
26ba0 75 65 2e 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 ue..** Every int
26bb0 65 72 66 61 63 65 20 74 68 61 74 20 61 63 63 65 erface that acce
26bc0 70 74 73 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 pts sqlite3_valu
26bd0 65 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 e arguments spec
26be0 69 66 69 65 73 0a 2a 2a 20 77 68 65 74 68 65 72 ifies.** whether
26bf0 20 6f 72 20 6e 6f 74 20 69 74 20 72 65 71 75 69 or not it requi
26c00 72 65 73 20 61 20 70 72 6f 74 65 63 74 65 64 20 res a protected
26c10 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a sqlite3_value..*
26c20 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 22 *.** The terms "
26c30 70 72 6f 74 65 63 74 65 64 22 20 61 6e 64 20 22 protected" and "
26c40 75 6e 70 72 6f 74 65 63 74 65 64 22 20 72 65 66 unprotected" ref
26c50 65 72 20 74 6f 20 77 68 65 74 68 65 72 20 6f 72 er to whether or
26c60 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20 not.** a mutex
26c70 69 73 20 68 65 6c 64 2e 20 20 41 20 69 6e 74 65 is held. A inte
26c80 72 6e 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65 rnal mutex is he
26c90 6c 64 20 66 6f 72 20 61 20 70 72 6f 74 65 63 74 ld for a protect
26ca0 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 ed.** sqlite3_va
26cb0 6c 75 65 20 6f 62 6a 65 63 74 20 62 75 74 20 6e lue object but n
26cc0 6f 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 o mutex is held
26cd0 66 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 for an unprotect
26ce0 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 ed.** sqlite3_va
26cf0 6c 75 65 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 lue object. If
26d00 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
26d10 65 64 20 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d ed to be single-
26d20 74 68 72 65 61 64 65 64 0a 2a 2a 20 28 77 69 74 threaded.** (wit
26d30 68 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 h [SQLITE_THREAD
26d40 53 41 46 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 SAFE=0] and with
26d50 20 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 [sqlite3_thread
26d60 73 61 66 65 28 29 5d 20 72 65 74 75 72 6e 69 6e safe()] returnin
26d70 67 20 30 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51 g 0).** or if SQ
26d80 4c 69 74 65 20 69 73 20 72 75 6e 20 69 6e 20 6f Lite is run in o
26d90 6e 65 20 6f 66 20 72 65 64 75 63 65 64 20 6d 75 ne of reduced mu
26da0 74 65 78 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 tex modes .** [S
26db0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e QLITE_CONFIG_SIN
26dc0 47 4c 45 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 GLETHREAD] or [S
26dd0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c QLITE_CONFIG_MUL
26de0 54 49 54 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65 TITHREAD].** the
26df0 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 n there is no di
26e00 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 stinction betwee
26e10 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 n protected and
26e20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 unprotected.** s
26e30 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
26e40 65 63 74 73 20 61 6e 64 20 74 68 65 79 20 63 61 ects and they ca
26e50 6e 20 62 65 20 75 73 65 64 20 69 6e 74 65 72 63 n be used interc
26e60 68 61 6e 67 65 61 62 6c 79 2e 20 20 48 6f 77 65 hangeably. Howe
26e70 76 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 ver,.** for maxi
26e80 6d 75 6d 20 63 6f 64 65 20 70 6f 72 74 61 62 69 mum code portabi
26e90 6c 69 74 79 20 69 74 20 69 73 20 72 65 63 6f 6d lity it is recom
26ea0 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c mended that appl
26eb0 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c ications.** stil
26ec0 6c 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69 l make the disti
26ed0 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 62 nction between b
26ee0 65 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 etween protected
26ef0 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 and unprotected
26f00 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 .** sqlite3_valu
26f10 65 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77 e objects even w
26f20 68 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 hen not strictly
26f30 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a required..**.**
26f40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c The sqlite3_val
26f50 75 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 ue objects that
26f60 61 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61 are passed as pa
26f70 72 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68 rameters into th
26f80 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e.** implementat
26f90 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 ion of [applicat
26fa0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 ion-defined SQL
26fb0 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 functions] are p
26fc0 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65 rotected..** The
26fd0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
26fe0 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 bject returned b
26ff0 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f y.** [sqlite3_co
27000 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 lumn_value()] is
27010 20 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a unprotected..**
27020 20 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c Unprotected sql
27030 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
27040 74 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 ts may only be u
27050 73 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c sed with.** [sql
27060 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 ite3_result_valu
27070 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 e()] and [sqlite
27080 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 3_bind_value()].
27090 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
270a0 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71 _value_blob | sq
270b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
270c0 28 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a ()] family of.**
270d0 20 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 interfaces requ
270e0 69 72 65 20 70 72 6f 74 65 63 74 65 64 20 73 71 ire protected sq
270f0 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
27100 63 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 cts..*/.typedef
27110 73 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 struct Mem sqlit
27120 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a e3_value;../*.**
27130 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 46 CAPI3REF: SQL F
27140 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 unction Context
27150 4f 62 6a 65 63 74 20 7b 48 31 36 30 30 31 7d 20 Object {H16001}
27160 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S20200>.**.** T
27170 68 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 he context in wh
27180 69 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 ich an SQL funct
27190 69 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 ion executes is
271a0 73 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 stored in an.**
271b0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
271c0 6f 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 object. A point
271d0 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 er to an sqlite3
271e0 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a _context object.
271f0 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72 ** is always fir
27200 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 st parameter to
27210 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 [application-def
27220 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f ined SQL functio
27230 6e 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c ns]..** The appl
27240 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
27250 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 SQL function imp
27260 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c lementation will
27270 20 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f pass this.** po
27280 69 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e inter through in
27290 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c to calls to [sql
272a0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 ite3_result_int
272b0 7c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 | sqlite3_result
272c0 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ()],.** [sqlite3
272d0 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 _aggregate_conte
272e0 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f xt()], [sqlite3_
272f0 75 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a user_data()],.**
27300 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 [sqlite3_contex
27310 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 t_db_handle()],
27320 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 [sqlite3_get_aux
27330 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f data()],.** and/
27340 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f or [sqlite3_set_
27350 61 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 auxdata()]..*/.t
27360 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
27370 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 lite3_context sq
27380 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a lite3_context;..
27390 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
273a0 42 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54 Binding Values T
273b0 6f 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 o Prepared State
273c0 6d 65 6e 74 73 20 7b 48 31 33 35 30 30 7d 20 3c ments {H13500} <
273d0 53 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S70300>.** KEYWO
273e0 52 44 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d RDS: {host param
273f0 65 74 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 eter} {host para
27400 6d 65 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 meters} {host pa
27410 72 61 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a rameter name}.**
27420 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 KEYWORDS: {SQL
27430 70 61 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 parameter} {SQL
27440 70 61 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 parameters} {par
27450 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a ameter binding}.
27460 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c **.** In the SQL
27470 20 73 74 72 69 6e 67 73 20 69 6e 70 75 74 20 74 strings input t
27480 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 o [sqlite3_prepa
27490 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 69 74 73 re_v2()] and its
274a0 20 76 61 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 variants,.** li
274b0 74 65 72 61 6c 73 20 6d 61 79 20 62 65 20 72 65 terals may be re
274c0 70 6c 61 63 65 64 20 62 79 20 61 20 70 61 72 61 placed by a para
274d0 6d 65 74 65 72 20 69 6e 20 6f 6e 65 20 6f 66 20 meter in one of
274e0 74 68 65 73 65 20 66 6f 72 6d 73 3a 0a 2a 2a 0a these forms:.**.
274f0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
27500 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e ?.** <li> ?NNN
27510 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a .** <li> :VVV.*
27520 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20 * <li> @VVV.**
27530 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f <li> $VVV.** </
27540 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 ul>.**.** In the
27550 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72 6d 73 parameter forms
27560 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 4e 4e 4e shown above NNN
27570 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c is an integer l
27580 69 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 iteral,.** and V
27590 56 56 20 69 73 20 61 6e 20 61 6c 70 68 61 2d 6e VV is an alpha-n
275a0 75 6d 65 72 69 63 20 70 61 72 61 6d 65 74 65 72 umeric parameter
275b0 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 6c 75 65 name. The value
275c0 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 61 s of these.** pa
275d0 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 63 rameters (also c
275e0 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72 61 alled "host para
275f0 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 20 meter names" or
27600 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 22 "SQL parameters"
27610 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 ).** can be set
27620 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 using the sqlite
27630 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 3_bind_*() routi
27640 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65 72 65 nes defined here
27650 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 ..**.** The firs
27660 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 t argument to th
27670 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a e sqlite3_bind_*
27680 28 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20 61 () routines is a
27690 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 lways.** a point
276a0 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 er to the [sqlit
276b0 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 20 e3_stmt] object
276c0 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a returned from.**
276d0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
276e0 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20 76 e_v2()] or its v
276f0 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 ariants..**.** T
27700 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
27710 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 nt is the index
27720 6f 66 20 74 68 65 20 53 51 4c 20 70 61 72 61 6d of the SQL param
27730 65 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e 0a eter to be set..
27740 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 ** The leftmost
27750 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68 61 SQL parameter ha
27760 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e s an index of 1.
27770 20 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65 20 When the same
27780 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 72 named.** SQL par
27790 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 20 6d ameter is used m
277a0 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 73 ore than once, s
277b0 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 econd and subseq
277c0 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 6e uent.** occurren
277d0 63 65 73 20 68 61 76 65 20 74 68 65 20 73 61 6d ces have the sam
277e0 65 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 66 e index as the f
277f0 69 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65 2e irst occurrence.
27800 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f .** The index fo
27810 72 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 r named paramete
27820 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 rs can be looked
27830 20 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a up using the.**
27840 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 [sqlite3_bind_p
27850 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 arameter_index()
27860 5d 20 41 50 49 20 69 66 20 64 65 73 69 72 65 64 ] API if desired
27870 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a 20 . The index.**
27880 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 6d for "?NNN" param
27890 65 74 65 72 73 20 69 73 20 74 68 65 20 76 61 6c eters is the val
278a0 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 ue of NNN..** Th
278b0 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 74 e NNN value must
278c0 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e be between 1 an
278d0 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c d the [sqlite3_l
278e0 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 6d imit()].** param
278f0 65 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 4d eter [SQLITE_LIM
27900 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 IT_VARIABLE_NUMB
27910 45 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61 6c ER] (default val
27920 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 ue: 999)..**.**
27930 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 The third argume
27940 6e 74 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 nt is the value
27950 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20 70 to bind to the p
27960 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 arameter..**.**
27970 49 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e 65 In those routine
27980 73 20 74 68 61 74 20 68 61 76 65 20 61 20 66 6f s that have a fo
27990 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 urth argument, i
279a0 74 73 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a ts value is the.
279b0 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 ** number of byt
279c0 65 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 es in the parame
279d0 74 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 61 ter. To be clea
279e0 72 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 r: the value is
279f0 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 the.** number of
27a00 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 6e <u>bytes</u> in
27a10 20 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 20 the value, not
27a20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 the number of ch
27a30 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 aracters..** If
27a40 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d the fourth param
27a50 65 74 65 72 20 69 73 20 6e 65 67 61 74 69 76 65 eter is negative
27a60 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 , the length of
27a70 74 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a the string is.**
27a80 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
27a90 79 74 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 ytes up to the f
27aa0 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e irst zero termin
27ab0 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ator..**.** The
27ac0 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 74 fifth argument t
27ad0 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 o sqlite3_bind_b
27ae0 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 lob(), sqlite3_b
27af0 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 0a ind_text(), and.
27b00 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f ** sqlite3_bind_
27b10 74 65 78 74 31 36 28 29 20 69 73 20 61 20 64 65 text16() is a de
27b20 73 74 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f structor used to
27b30 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 dispose of the
27b40 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 6e BLOB or.** strin
27b50 67 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 68 g after SQLite h
27b60 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 as finished with
27b70 20 69 74 2e 20 49 66 20 74 68 65 20 66 69 66 74 it. If the fift
27b80 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a h argument is.**
27b90 20 74 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c the special val
27ba0 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 ue [SQLITE_STATI
27bb0 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 C], then SQLite
27bc0 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 assumes that the
27bd0 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
27be0 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75 6e is in static, un
27bf0 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 61 6e managed space an
27c00 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 d does not need
27c10 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 20 to be freed..**
27c20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72 67 If the fifth arg
27c30 75 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76 61 ument has the va
27c40 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e lue [SQLITE_TRAN
27c50 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 SIENT], then.**
27c60 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 73 SQLite makes its
27c70 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 own private cop
27c80 79 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 6d y of the data im
27c90 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 mediately, befor
27ca0 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 e.** the sqlite3
27cb0 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e _bind_*() routin
27cc0 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a e returns..**.**
27cd0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e The sqlite3_bin
27ce0 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f 75 d_zeroblob() rou
27cf0 74 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c 4f tine binds a BLO
27d00 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 68 B of length N th
27d10 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 at.** is filled
27d20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 20 with zeroes. A
27d30 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 20 zeroblob uses a
27d40 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 fixed amount of
27d50 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 20 memory.** (just
27d60 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68 6f an integer to ho
27d70 6c 64 20 69 74 73 20 73 69 7a 65 29 20 77 68 69 ld its size) whi
27d80 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67 20 70 le it is being p
27d90 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 rocessed..** Zer
27da0 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 6e oblobs are inten
27db0 64 65 64 20 74 6f 20 73 65 72 76 65 20 61 73 20 ded to serve as
27dc0 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 placeholders for
27dd0 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a 20 BLOBs whose.**
27de0 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 72 content is later
27df0 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a 2a written using.*
27e00 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f * [sqlite3_blob_
27e10 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 open | increment
27e20 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 al BLOB I/O] rou
27e30 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 tines..** A nega
27e40 74 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20 74 tive value for t
27e50 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 75 he zeroblob resu
27e60 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 lts in a zero-le
27e70 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a ngth BLOB..**.**
27e80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e The sqlite3_bin
27e90 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 6d d_*() routines m
27ea0 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61 66 ust be called af
27eb0 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ter.** [sqlite3_
27ec0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28 61 prepare_v2()] (a
27ed0 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 nd its variants)
27ee0 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 or [sqlite3_res
27ef0 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 et()] and.** bef
27f00 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 ore [sqlite3_ste
27f10 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 p()]..** Binding
27f20 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 65 s are not cleare
27f30 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 d by the [sqlite
27f40 33 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74 69 3_reset()] routi
27f50 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 ne..** Unbound p
27f60 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 69 6e arameters are in
27f70 74 65 72 70 72 65 74 65 64 20 61 73 20 4e 55 4c terpreted as NUL
27f80 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 L..**.** These r
27f90 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 5b outines return [
27fa0 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 SQLITE_OK] on su
27fb0 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f ccess or an erro
27fc0 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 r code if.** any
27fd0 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
27fe0 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 . [SQLITE_RANGE
27ff0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 ] is returned if
28000 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a the parameter.*
28010 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f * index is out o
28020 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 54 f range. [SQLIT
28030 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75 E_NOMEM] is retu
28040 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29 rned if malloc()
28050 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 fails..** [SQLI
28060 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 74 TE_MISUSE] might
28070 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 be returned if
28080 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 these routines a
28090 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a re called on a.*
280a0 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e * virtual machin
280b0 65 20 74 68 61 74 20 69 73 20 74 68 65 20 77 72 e that is the wr
280c0 6f 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68 69 ong state or whi
280d0 63 68 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ch has already b
280e0 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a een finalized..*
280f0 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 6d * Detection of m
28100 69 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69 61 isuse is unrelia
28110 62 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f ble. Applicatio
28120 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 65 ns should not de
28130 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 pend.** on SQLIT
28140 45 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e 73 E_MISUSE returns
28150 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 . SQLITE_MISUSE
28160 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 is intended to
28170 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 20 indicate a.** a
28180 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 74 logic error in t
28190 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 he application.
281a0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 Future versions
281b0 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 of SQLite might
281c0 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65 72 .** panic rather
281d0 20 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51 4c than return SQL
281e0 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a ITE_MISUSE..**.*
281f0 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
28200 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
28210 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a ter_count()],.**
28220 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 [sqlite3_bind_p
28230 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d arameter_name()]
28240 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 , and [sqlite3_b
28250 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e ind_parameter_in
28260 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e dex()]..**.** IN
28270 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 VARIANTS:.**.**
28280 7b 48 31 33 35 30 36 7d 20 54 68 65 20 5b 53 51 {H13506} The [SQ
28290 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 L statement comp
282a0 69 6c 65 72 5d 20 72 65 63 6f 67 6e 69 7a 65 73 iler] recognizes
282b0 20 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 20 66 tokens of the f
282c0 6f 72 6d 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 orms.**
282d0 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 24 "?", "?NNN", "$
282e0 56 56 56 22 2c 20 22 3a 56 56 56 22 2c 20 61 6e VVV", ":VVV", an
282f0 64 20 22 40 56 56 56 22 20 61 73 20 53 51 4c 20 d "@VVV" as SQL
28300 70 61 72 61 6d 65 74 65 72 73 2c 0a 2a 2a 20 20 parameters,.**
28310 20 20 20 20 20 20 20 20 77 68 65 72 65 20 4e 4e where NN
28320 4e 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 N is any sequenc
28330 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 e of one or more
28340 20 64 69 67 69 74 73 0a 2a 2a 20 20 20 20 20 20 digits.**
28350 20 20 20 20 61 6e 64 20 77 68 65 72 65 20 56 56 and where VV
28360 56 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 V is any sequenc
28370 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 e of one or more
28380 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a alphanumeric.**
28390 20 20 20 20 20 20 20 20 20 20 63 68 61 72 61 63 charac
283a0 74 65 72 73 20 6f 72 20 22 3a 3a 22 20 6f 70 74 ters or "::" opt
283b0 69 6f 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64 ionally followed
283c0 20 62 79 20 61 20 73 74 72 69 6e 67 20 63 6f 6e by a string con
283d0 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 taining.**
283e0 20 20 20 20 6e 6f 20 73 70 61 63 65 73 20 61 6e no spaces an
283f0 64 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 d contained with
28400 69 6e 20 70 61 72 65 6e 74 68 65 73 65 73 2e 0a in parentheses..
28410 2a 2a 0a 2a 2a 20 7b 48 31 33 35 30 39 7d 20 54 **.** {H13509} T
28420 68 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 he initial value
28430 20 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d of an SQL param
28440 65 74 65 72 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a eter is NULL..**
28450 0a 2a 2a 20 7b 48 31 33 35 31 32 7d 20 54 68 65 .** {H13512} The
28460 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 22 3f 22 index of an "?"
28470 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69 SQL parameter i
28480 73 20 6f 6e 65 20 6c 61 72 67 65 72 20 74 68 61 s one larger tha
28490 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 n the.**
284a0 20 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78 20 largest index
284b0 6f 66 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 of SQL parameter
284c0 20 74 6f 20 74 68 65 20 6c 65 66 74 2c 20 6f 72 to the left, or
284d0 20 31 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 1 if.**
284e0 20 20 74 68 65 20 22 3f 22 20 69 73 20 74 68 65 the "?" is the
284f0 20 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61 leftmost SQL pa
28500 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b rameter..**.** {
28510 48 31 33 35 31 35 7d 20 54 68 65 20 69 6e 64 65 H13515} The inde
28520 78 20 6f 66 20 61 6e 20 22 3f 4e 4e 4e 22 20 53 x of an "?NNN" S
28530 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 QL parameter is
28540 74 68 65 20 69 6e 74 65 67 65 72 20 4e 4e 4e 2e the integer NNN.
28550 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 31 38 7d 20 .**.** {H13518}
28560 54 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 The index of an
28570 22 3a 56 56 56 22 2c 20 22 24 56 56 56 22 2c 20 ":VVV", "$VVV",
28580 6f 72 20 22 40 56 56 56 22 20 53 51 4c 20 70 61 or "@VVV" SQL pa
28590 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 20 20 rameter is.**
285a0 20 20 20 20 20 20 20 74 68 65 20 73 61 6d 65 20 the same
285b0 61 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 as the index of
285c0 6c 65 66 74 6d 6f 73 74 20 6f 63 63 75 72 72 65 leftmost occurre
285d0 6e 63 65 73 20 6f 66 20 74 68 65 20 73 61 6d 65 nces of the same
285e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 .** par
285f0 61 6d 65 74 65 72 2c 20 6f 72 20 6f 6e 65 20 6d ameter, or one m
28600 6f 72 65 20 74 68 61 6e 20 74 68 65 20 6c 61 72 ore than the lar
28610 67 65 73 74 20 69 6e 64 65 78 20 6f 76 65 72 20 gest index over
28620 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 all.**
28630 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 parameters to th
28640 65 20 6c 65 66 74 20 69 66 20 74 68 69 73 20 69 e left if this i
28650 73 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 s the first occu
28660 72 72 65 6e 63 65 0a 2a 2a 20 20 20 20 20 20 20 rrence.**
28670 20 20 20 6f 66 20 74 68 69 73 20 70 61 72 61 6d of this param
28680 65 74 65 72 2c 20 6f 72 20 31 20 69 66 20 74 68 eter, or 1 if th
28690 69 73 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f is is the leftmo
286a0 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a st parameter..**
286b0 0a 2a 2a 20 7b 48 31 33 35 32 31 7d 20 54 68 65 .** {H13521} The
286c0 20 5b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 [SQL statement
286d0 63 6f 6d 70 69 6c 65 72 5d 20 66 61 69 6c 73 20 compiler] fails
286e0 77 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f with an [SQLITE_
286f0 52 41 4e 47 45 5d 0a 2a 2a 20 20 20 20 20 20 20 RANGE].**
28700 20 20 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 error if the
28710 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 4c 20 index of an SQL
28720 70 61 72 61 6d 65 74 65 72 20 69 73 20 6c 65 73 parameter is les
28730 73 20 74 68 61 6e 20 31 0a 2a 2a 20 20 20 20 20 s than 1.**
28740 20 20 20 20 20 6f 72 20 67 72 65 61 74 65 72 20 or greater
28750 74 68 61 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 than the compile
28760 2d 74 69 6d 65 20 53 51 4c 49 54 45 5f 4d 41 58 -time SQLITE_MAX
28770 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
28780 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 .** par
28790 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ameter..**.** {H
287a0 31 33 35 32 34 7d 20 43 61 6c 6c 73 20 74 6f 20 13524} Calls to
287b0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 [sqlite3_bind_te
287c0 78 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e xt | sqlite3_bin
287d0 64 28 53 2c 4e 2c 56 2c 2e 2e 2e 29 5d 0a 2a 2a d(S,N,V,...)].**
287e0 20 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 associ
287f0 61 74 65 20 74 68 65 20 76 61 6c 75 65 20 56 20 ate the value V
28800 77 69 74 68 20 61 6c 6c 20 53 51 4c 20 70 61 72 with all SQL par
28810 61 6d 65 74 65 72 73 20 68 61 76 69 6e 67 20 61 ameters having a
28820 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e n.** in
28830 64 65 78 20 6f 66 20 4e 20 69 6e 20 74 68 65 20 dex of N in the
28840 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
28850 65 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ent] S..**.** {H
28860 31 33 35 32 37 7d 20 43 61 6c 6c 73 20 74 6f 20 13527} Calls to
28870 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 [sqlite3_bind_te
28880 78 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e xt | sqlite3_bin
28890 64 28 53 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 d(S,N,...)].**
288a0 20 20 20 20 20 20 20 20 6f 76 65 72 72 69 64 65 override
288b0 20 70 72 69 6f 72 20 63 61 6c 6c 73 20 77 69 74 prior calls wit
288c0 68 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 h the same value
288d0 73 20 6f 66 20 53 20 61 6e 64 20 4e 2e 0a 2a 2a s of S and N..**
288e0 0a 2a 2a 20 7b 48 31 33 35 33 30 7d 20 42 69 6e .** {H13530} Bin
288f0 64 69 6e 67 73 20 65 73 74 61 62 6c 69 73 68 65 dings establishe
28900 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 69 d by [sqlite3_bi
28910 6e 64 5f 74 65 78 74 20 7c 20 73 71 6c 69 74 65 nd_text | sqlite
28920 33 5f 62 69 6e 64 28 53 2c 2e 2e 2e 29 5d 0a 2a 3_bind(S,...)].*
28930 2a 20 20 20 20 20 20 20 20 20 20 70 65 72 73 69 * persi
28940 73 74 20 61 63 72 6f 73 73 20 63 61 6c 6c 73 20 st across calls
28950 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 to [sqlite3_rese
28960 74 28 53 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 t(S)]..**.** {H1
28970 33 35 33 33 7d 20 49 6e 20 63 61 6c 6c 73 20 74 3533} In calls t
28980 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f o [sqlite3_bind_
28990 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d blob(S,N,V,L,D)]
289a0 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 ,.** [s
289b0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
289c0 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 (S,N,V,L,D)], or
289d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
289e0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 lite3_bind_text1
289f0 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 6(S,N,V,L,D)] SQ
28a00 4c 69 74 65 20 62 69 6e 64 73 20 74 68 65 20 66 Lite binds the f
28a10 69 72 73 74 20 4c 0a 2a 2a 20 20 20 20 20 20 20 irst L.**
28a20 20 20 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 bytes of the
28a30 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 20 70 BLOB or string p
28a40 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 56 2c 20 ointed to by V,
28a50 77 68 65 6e 20 4c 0a 2a 2a 20 20 20 20 20 20 20 when L.**
28a60 20 20 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 is non-negati
28a70 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 33 ve..**.** {H1353
28a80 36 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 6} In calls to [
28a90 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
28aa0 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 6f 72 t(S,N,V,L,D)] or
28ab0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
28ac0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 lite3_bind_text1
28ad0 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 6(S,N,V,L,D)] SQ
28ae0 4c 69 74 65 20 62 69 6e 64 73 20 63 68 61 72 61 Lite binds chara
28af0 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 cters.**
28b00 20 20 66 72 6f 6d 20 56 20 74 68 72 6f 75 67 68 from V through
28b10 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 the first zero
28b20 63 68 61 72 61 63 74 65 72 20 77 68 65 6e 20 4c character when L
28b30 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a is negative..**
28b40 0a 2a 2a 20 7b 48 31 33 35 33 39 7d 20 49 6e 20 .** {H13539} In
28b50 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 calls to [sqlite
28b60 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 53 2c 4e 2c 3_bind_blob(S,N,
28b70 56 2c 4c 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 V,L,D)],.**
28b80 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 [sqlite3_bi
28b90 6e 64 5f 74 65 78 74 28 53 2c 4e 2c 56 2c 4c 2c nd_text(S,N,V,L,
28ba0 44 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 D)], or.**
28bb0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e [sqlite3_bin
28bc0 64 5f 74 65 78 74 31 36 28 53 2c 4e 2c 56 2c 4c d_text16(S,N,V,L
28bd0 2c 44 29 5d 20 77 68 65 6e 20 44 20 69 73 20 74 ,D)] when D is t
28be0 68 65 20 73 70 65 63 69 61 6c 0a 2a 2a 20 20 20 he special.**
28bf0 20 20 20 20 20 20 20 63 6f 6e 73 74 61 6e 74 20 constant
28c00 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c [SQLITE_STATIC],
28c10 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20 SQLite assumes
28c20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 56 that the value V
28c30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 .** is
28c40 68 65 6c 64 20 69 6e 20 73 74 61 74 69 63 20 75 held in static u
28c50 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 74 nmanaged space t
28c60 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 hat will not cha
28c70 6e 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 nge.**
28c80 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 during the lifet
28c90 69 6d 65 20 6f 66 20 74 68 65 20 62 69 6e 64 69 ime of the bindi
28ca0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 34 ng..**.** {H1354
28cb0 32 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 2} In calls to [
28cc0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
28cd0 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 0a 2a b(S,N,V,L,D)],.*
28ce0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
28cf0 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 53 2c te3_bind_text(S,
28d00 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a 2a 2a N,V,L,D)], or.**
28d10 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
28d20 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 53 e3_bind_text16(S
28d30 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 77 68 65 6e 20 ,N,V,L,D)] when
28d40 44 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c D is the special
28d50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e .** con
28d60 73 74 61 6e 74 20 5b 53 51 4c 49 54 45 5f 54 52 stant [SQLITE_TR
28d70 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 20 72 6f ANSIENT], the ro
28d80 75 74 69 6e 65 20 6d 61 6b 65 73 20 61 0a 2a 2a utine makes a.**
28d90 20 20 20 20 20 20 20 20 20 20 70 72 69 76 61 74 privat
28da0 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 e copy of the va
28db0 6c 75 65 20 56 20 62 65 66 6f 72 65 20 69 74 20 lue V before it
28dc0 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b returns..**.** {
28dd0 48 31 33 35 34 35 7d 20 49 6e 20 63 61 6c 6c 73 H13545} In calls
28de0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e to [sqlite3_bin
28df0 64 5f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 d_blob(S,N,V,L,D
28e00 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 )],.**
28e10 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 [sqlite3_bind_te
28e20 78 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 xt(S,N,V,L,D)],
28e30 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b or.** [
28e40 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
28e50 74 31 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 t16(S,N,V,L,D)]
28e60 77 68 65 6e 20 44 20 69 73 20 61 20 70 6f 69 6e when D is a poin
28e70 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 ter to.**
28e80 20 20 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 53 a function, S
28e90 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 QLite invokes th
28ea0 61 74 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 at function to d
28eb0 65 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20 estroy the.**
28ec0 20 20 20 20 20 20 20 76 61 6c 75 65 20 56 20 61 value V a
28ed0 66 74 65 72 20 69 74 20 68 61 73 20 66 69 6e 69 fter it has fini
28ee0 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 76 shed using the v
28ef0 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 alue V..**.** {H
28f00 31 33 35 34 38 7d 20 49 6e 20 63 61 6c 6c 73 20 13548} In calls
28f10 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 to [sqlite3_bind
28f20 5f 7a 65 72 6f 62 6c 6f 62 28 53 2c 4e 2c 56 2c _zeroblob(S,N,V,
28f30 4c 29 5d 20 74 68 65 20 76 61 6c 75 65 20 62 6f L)] the value bo
28f40 75 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 und.**
28f50 69 73 20 61 20 42 4c 4f 42 20 6f 66 20 4c 20 62 is a BLOB of L b
28f60 79 74 65 73 2c 20 6f 72 20 61 20 7a 65 72 6f 2d ytes, or a zero-
28f70 6c 65 6e 67 74 68 20 42 4c 4f 42 20 69 66 20 4c length BLOB if L
28f80 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a is negative..**
28f90 0a 2a 2a 20 7b 48 31 33 35 35 31 7d 20 49 6e 20 .** {H13551} In
28fa0 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 calls to [sqlite
28fb0 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 53 2c 4e 3_bind_value(S,N
28fc0 2c 56 29 5d 20 74 68 65 20 56 20 61 72 67 75 6d ,V)] the V argum
28fd0 65 6e 74 20 6d 61 79 0a 2a 2a 20 20 20 20 20 20 ent may.**
28fe0 20 20 20 20 62 65 20 65 69 74 68 65 72 20 61 20 be either a
28ff0 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
29000 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
29010 20 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 or an.**
29020 20 20 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 [unprotected
29030 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
29040 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 bject..*/.SQLITE
29050 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
29060 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 _bind_blob(sqlit
29070 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 e3_stmt*, int, c
29080 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 onst void*, int
29090 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a n, void(*)(void*
290a0 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ));.SQLITE_API i
290b0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
290c0 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 double(sqlite3_s
290d0 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c tmt*, int, doubl
290e0 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 e);.SQLITE_API i
290f0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
29100 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 int(sqlite3_stmt
29110 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
29120 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
29130 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 ite3_bind_int64(
29140 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
29150 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 nt, sqlite3_int6
29160 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 4);.SQLITE_API i
29170 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
29180 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d null(sqlite3_stm
29190 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 t*, int);.SQLITE
291a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
291b0 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 _bind_text(sqlit
291c0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 e3_stmt*, int, c
291d0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 onst char*, int
291e0 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a n, void(*)(void*
291f0 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ));.SQLITE_API i
29200 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
29210 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 text16(sqlite3_s
29220 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 tmt*, int, const
29230 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 void*, int, voi
29240 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
29250 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
29260 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 ite3_bind_value(
29270 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
29280 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 nt, const sqlite
29290 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
292a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
292b0 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 3_bind_zeroblob(
292c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
292d0 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a nt, int n);../*.
292e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d ** CAPI3REF: Num
292f0 62 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d ber Of SQL Param
29300 65 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c eters {H13600} <
29310 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S70300>.**.** Th
29320 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 is routine can b
29330 65 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 e used to find t
29340 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 he number of [SQ
29350 4c 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a L parameters].**
29360 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 in a [prepared
29370 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c statement]. SQL
29380 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 parameters are
29390 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a tokens of the.**
293a0 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e form "?", "?NNN
293b0 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 ", ":AAA", "$AAA
293c0 22 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 ", or "@AAA" tha
293d0 74 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c t serve as.** pl
293e0 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 aceholders for v
293f0 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b alues that are [
29400 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
29410 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f b | bound].** to
29420 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 the parameters
29430 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e at a later time.
29440 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
29450 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 ine actually ret
29460 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f urns the index o
29470 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 f the largest (r
29480 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 ightmost).** par
29490 61 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 ameter. For all
294a0 66 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e forms except ?NN
294b0 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 N, this will cor
294c0 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a respond to the.*
294d0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 * number of uniq
294e0 75 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 ue parameters.
294f0 49 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 If parameters of
29500 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 the ?NNN are us
29510 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 ed,.** there may
29520 20 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 be gaps in the
29530 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 list..**.** See
29540 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 also: [sqlite3_b
29550 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 ind_blob|sqlite3
29560 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 _bind()],.** [sq
29570 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
29580 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e eter_name()], an
29590 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 d.** [sqlite3_bi
295a0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 nd_parameter_ind
295b0 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 ex()]..**.** INV
295c0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
295d0 48 31 33 36 30 31 7d 20 54 68 65 20 5b 73 71 6c H13601} The [sql
295e0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
295f0 74 65 72 5f 63 6f 75 6e 74 28 53 29 5d 20 69 6e ter_count(S)] in
29600 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a terface returns.
29610 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
29620 6c 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f 66 largest index of
29630 20 61 6c 6c 20 53 51 4c 20 70 61 72 61 6d 65 74 all SQL paramet
29640 65 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 ers in the.**
29650 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 [prepared
29660 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 2c 20 6f statement] S, o
29670 72 20 30 20 69 66 20 53 20 63 6f 6e 74 61 69 6e r 0 if S contain
29680 73 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74 s no SQL paramet
29690 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ers..*/.SQLITE_A
296a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
296b0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f ind_parameter_co
296c0 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 unt(sqlite3_stmt
296d0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
296e0 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 48 REF: Name Of A H
296f0 6f 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b 48 ost Parameter {H
29700 31 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e 0a 13620} <S70300>.
29710 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
29720 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ne returns a poi
29730 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 nter to the name
29740 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 of the n-th.**
29750 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 [SQL parameter]
29760 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 in a [prepared s
29770 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 tatement]..** SQ
29780 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 L parameters of
29790 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 the form "?NNN"
297a0 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41 or ":AAA" or "@A
297b0 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a AA" or "$AAA".**
297c0 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69 have a name whi
297d0 63 68 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 ch is the string
297e0 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 "?NNN" or ":AAA
297f0 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22 " or "@AAA" or "
29800 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74 $AAA".** respect
29810 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 ively..** In oth
29820 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e er words, the in
29830 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 itial ":" or "$"
29840 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a or "@" or "?".*
29850 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73 * is included as
29860 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d part of the nam
29870 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 e..** Parameters
29880 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 of the form "?"
29890 20 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c 6f without a follo
298a0 77 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61 76 wing integer hav
298b0 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 e no name.** and
298c0 20 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72 72 are also referr
298d0 65 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 6d ed to as "anonym
298e0 6f 75 73 20 70 61 72 61 6d 65 74 65 72 73 22 2e ous parameters".
298f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
29900 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 host parameter
29910 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 has an index of
29920 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 1, not 0..**.**
29930 49 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69 If the value n i
29940 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f s out of range o
29950 72 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61 r if the n-th pa
29960 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61 rameter is.** na
29970 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c meless, then NUL
29980 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 L is returned.
29990 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 The returned str
299a0 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 ing is.** always
299b0 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 in UTF-8 encodi
299c0 6e 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e ng even if the n
299d0 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 77 amed parameter w
299e0 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 as.** originally
299f0 20 73 70 65 63 69 66 69 65 64 20 61 73 20 55 54 specified as UT
29a00 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33 F-16 in [sqlite3
29a10 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72 _prepare16()] or
29a20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
29a30 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a pare16_v2()]..**
29a40 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
29a50 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
29a60 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d |sqlite3_bind()]
29a70 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 ,.** [sqlite3_bi
29a80 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 nd_parameter_cou
29a90 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 nt()], and.** [s
29aa0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
29ab0 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a meter_index()]..
29ac0 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
29ad0 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 36 32 31 7d :.**.** {H13621}
29ae0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 The [sqlite3_bi
29af0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d nd_parameter_nam
29b00 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 e(S,N)] interfac
29b10 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 e returns.**
29b20 20 20 20 20 20 20 61 20 55 54 46 2d 38 20 72 65 a UTF-8 re
29b30 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 6e ndering of the n
29b40 61 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20 70 ame of the SQL p
29b50 61 72 61 6d 65 74 65 72 20 69 6e 0a 2a 2a 20 20 arameter in.**
29b60 20 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 65 the [pre
29b70 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
29b80 20 53 20 68 61 76 69 6e 67 20 69 6e 64 65 78 20 S having index
29b90 4e 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 N, or.**
29ba0 20 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 NULL if there
29bb0 69 73 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 is no SQL parame
29bc0 74 65 72 20 77 69 74 68 20 69 6e 64 65 78 20 4e ter with index N
29bd0 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 or if the.**
29be0 20 20 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 parameter
29bf0 20 77 69 74 68 20 69 6e 64 65 78 20 4e 20 69 73 with index N is
29c00 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 70 61 an anonymous pa
29c10 72 61 6d 65 74 65 72 20 22 3f 22 2e 0a 2a 2f 0a rameter "?"..*/.
29c20 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
29c30 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 char *sqlite3_b
29c40 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 ind_parameter_na
29c50 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a me(sqlite3_stmt*
29c60 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 , int);../*.** C
29c70 41 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f API3REF: Index O
29c80 66 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69 f A Parameter Wi
29c90 74 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 th A Given Name
29ca0 7b 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30 {H13640} <S70300
29cb0 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 >.**.** Return t
29cc0 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 he index of an S
29cd0 51 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76 QL parameter giv
29ce0 65 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 en its name. Th
29cf0 65 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 e.** index value
29d00 20 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69 returned is sui
29d10 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 table for use as
29d20 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 the second.** p
29d30 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
29d40 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 ite3_bind_blob|s
29d50 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 qlite3_bind()].
29d60 20 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 A zero.** is re
29d70 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 turned if no mat
29d80 63 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20 ching parameter
29d90 69 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 is found. The p
29da0 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 arameter.** name
29db0 20 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69 must be given i
29dc0 6e 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20 n UTF-8 even if
29dd0 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 the original sta
29de0 74 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 tement.** was pr
29df0 65 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d epared from UTF-
29e00 31 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73 16 text using [s
29e10 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
29e20 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 _v2()]..**.** Se
29e30 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
29e40 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 _bind_blob|sqlit
29e50 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b e3_bind()],.** [
29e60 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
29e70 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c ameter_count()],
29e80 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 and.** [sqlite3
29e90 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
29ea0 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 index()]..**.**
29eb0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
29ec0 2a 20 7b 48 31 33 36 34 31 7d 20 54 68 65 20 5b * {H13641} The [
29ed0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
29ee0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 53 2c 4e ameter_index(S,N
29ef0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 )] interface ret
29f00 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 urns.**
29f10 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 53 51 the index of SQ
29f20 4c 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 L parameter in t
29f30 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
29f40 74 65 6d 65 6e 74 5d 0a 2a 2a 20 20 20 20 20 20 tement].**
29f50 20 20 20 20 53 20 77 68 6f 73 65 20 6e 61 6d 65 S whose name
29f60 20 6d 61 74 63 68 65 73 20 74 68 65 20 55 54 46 matches the UTF
29f70 2d 38 20 73 74 72 69 6e 67 20 4e 2c 20 6f 72 20 -8 string N, or
29f80 30 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 0 if there is.**
29f90 20 20 20 20 20 20 20 20 20 20 6e 6f 20 6d 61 74 no mat
29fa0 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ch..*/.SQLITE_AP
29fb0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
29fc0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 nd_parameter_ind
29fd0 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a ex(sqlite3_stmt*
29fe0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e , const char *zN
29ff0 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ame);../*.** CAP
2a000 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 6c 6c I3REF: Reset All
2a010 20 42 69 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50 Bindings On A P
2a020 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e repared Statemen
2a030 74 20 7b 48 31 33 36 36 30 7d 20 3c 53 37 30 33 t {H13660} <S703
2a040 30 30 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 00>.**.** Contra
2a050 72 79 20 74 6f 20 74 68 65 20 69 6e 74 75 69 74 ry to the intuit
2a060 69 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 ion of many, [sq
2a070 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 64 lite3_reset()] d
2a080 6f 65 73 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a oes not reset.**
2a090 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 the [sqlite3_bi
2a0a0 6e 64 5f 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e nd_blob | bindin
2a0b0 67 73 5d 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 gs] on a [prepar
2a0c0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a ed statement]..*
2a0d0 2a 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 * Use this routi
2a0e0 6e 65 20 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 ne to reset all
2a0f0 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 host parameters
2a100 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 to NULL..**.** I
2a110 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a NVARIANTS:.**.**
2a120 20 7b 48 31 33 36 36 31 7d 20 54 68 65 20 5b 73 {H13661} The [s
2a130 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e qlite3_clear_bin
2a140 64 69 6e 67 73 28 53 29 5d 20 69 6e 74 65 72 66 dings(S)] interf
2a150 61 63 65 20 72 65 73 65 74 73 20 61 6c 6c 20 53 ace resets all S
2a160 51 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 QL.** p
2a170 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 arameter binding
2a180 73 20 69 6e 20 74 68 65 20 5b 70 72 65 70 61 72 s in the [prepar
2a190 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 ed statement] S
2a1a0 62 61 63 6b 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f back to NULL..*/
2a1b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2a1c0 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 sqlite3_clear_bi
2a1d0 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 ndings(sqlite3_s
2a1e0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tmt*);../*.** CA
2a1f0 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f PI3REF: Number O
2a200 66 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 f Columns In A R
2a210 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 esult Set {H1371
2a220 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 0} <S10700>.**.*
2a230 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
2a240 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
2a250 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
2a260 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
2a270 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 .** [prepared st
2a280 61 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 atement]. This r
2a290 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 outine returns 0
2a2a0 20 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 if pStmt is an
2a2b0 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 SQL.** statement
2a2c0 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 that does not r
2a2d0 65 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 eturn data (for
2a2e0 65 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 example an [UPDA
2a2f0 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 TE])..**.** INVA
2a300 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
2a310 31 33 37 31 31 7d 20 54 68 65 20 5b 73 71 6c 69 13711} The [sqli
2a320 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 te3_column_count
2a330 28 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 (S)] interface r
2a340 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
2a350 72 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 r of.**
2a360 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
2a370 72 65 73 75 6c 74 20 73 65 74 20 67 65 6e 65 72 result set gener
2a380 61 74 65 64 20 62 79 20 74 68 65 20 5b 70 72 65 ated by the [pre
2a390 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
2a3a0 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 S,.**
2a3b0 6f 72 20 30 20 69 66 20 53 20 64 6f 65 73 20 6e or 0 if S does n
2a3c0 6f 74 20 67 65 6e 65 72 61 74 65 20 61 20 72 65 ot generate a re
2a3d0 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c sult set..*/.SQL
2a3e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
2a3f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 te3_column_count
2a400 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
2a410 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 Stmt);../*.** CA
2a420 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e PI3REF: Column N
2a430 61 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c 74 ames In A Result
2a440 20 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c 53 Set {H13720} <S
2a450 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 10700>.**.** The
2a460 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 se routines retu
2a470 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 rn the name assi
2a480 67 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69 63 gned to a partic
2a490 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 ular column.** i
2a4a0 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
2a4b0 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 of a [SELECT] s
2a4c0 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 73 tatement. The s
2a4d0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 qlite3_column_na
2a4e0 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 me().** interfac
2a4f0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e e returns a poin
2a500 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 ter to a zero-te
2a510 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 rminated UTF-8 s
2a520 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 6c tring.** and sql
2a530 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 ite3_column_name
2a540 31 36 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 16() returns a p
2a550 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f ointer to a zero
2a560 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 -terminated.** U
2a570 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 TF-16 string. T
2a580 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
2a590 65 72 20 69 73 20 74 68 65 20 5b 70 72 65 70 61 er is the [prepa
2a5a0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a red statement].*
2a5b0 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 * that implement
2a5c0 73 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 73 s the [SELECT] s
2a5d0 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 tatement. The se
2a5e0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 cond parameter i
2a5f0 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 s the.** column
2a600 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 66 number. The lef
2a610 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 tmost column is
2a620 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 number 0..**.**
2a630 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 The returned str
2a640 69 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20 76 ing pointer is v
2a650 61 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68 65 alid until eithe
2a660 72 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 r the [prepared
2a670 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 statement].** is
2a680 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 destroyed by [s
2a690 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
2a6a0 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 )] or until the
2a6b0 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 next call to.**
2a6c0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
2a6d0 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33 ame() or sqlite3
2a6e0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 _column_name16()
2a6f0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c on the same col
2a700 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 umn..**.** If sq
2a710 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 lite3_malloc() f
2a720 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65 20 ails during the
2a730 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 69 processing of ei
2a740 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 ther routine.**
2a750 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 72 (for example dur
2a760 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e ing a conversion
2a770 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 from UTF-8 to U
2a780 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a 2a TF-16) then a.**
2a790 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 NULL pointer is
2a7a0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
2a7b0 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 72 The name of a r
2a7c0 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 esult column is
2a7d0 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
2a7e0 20 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f 72 "AS" clause for
2a7f0 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2c .** that column,
2a800 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 if there is an
2a810 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74 AS clause. If t
2a820 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63 6c here is no AS cl
2a830 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ause.** then the
2a840 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c name of the col
2a850 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66 69 umn is unspecifi
2a860 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e 67 ed and may chang
2a870 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 e from.** one re
2a880 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 20 lease of SQLite
2a890 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a to the next..**.
2a8a0 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a ** INVARIANTS:.*
2a8b0 2a 0a 2a 2a 20 7b 48 31 33 37 32 31 7d 20 41 20 *.** {H13721} A
2a8c0 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 successful invoc
2a8d0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 ation of the [sq
2a8e0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
2a8f0 65 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20 e(S,N)].**
2a900 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65 interface re
2a910 74 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f turns the name o
2a920 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e f the Nth column
2a930 20 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20 (where 0 is.**
2a940 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66 the lef
2a950 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f tmost column) fo
2a960 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 r the result set
2a970 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 of the.**
2a980 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 [prepared st
2a990 61 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20 atement] S as a
2a9a0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
2a9b0 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a UTF-8 string..**
2a9c0 0a 2a 2a 20 7b 48 31 33 37 32 33 7d 20 41 20 73 .** {H13723} A s
2a9d0 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 uccessful invoca
2a9e0 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c tion of the [sql
2a9f0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 ite3_column_name
2aa00 31 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 16(S,N)].**
2aa10 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 interface r
2aa20 65 74 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 eturns the name
2aa30 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d of the Nth colum
2aa40 6e 20 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a n (where 0 is.**
2aa50 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 the le
2aa60 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 ftmost column) f
2aa70 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 or the result se
2aa80 74 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 t of the.**
2aa90 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 [prepared s
2aaa0 74 61 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 tatement] S as a
2aab0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
2aac0 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a UTF-16 string.*
2aad0 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68 * in th
2aae0 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 e native byte or
2aaf0 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 der..**.** {H137
2ab00 32 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 24} The [sqlite3
2ab10 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20 _column_name()]
2ab20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c and [sqlite3_col
2ab30 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 5d 0a 2a 2a umn_name16()].**
2ab40 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 interf
2ab50 61 63 65 73 20 72 65 74 75 72 6e 20 61 20 4e 55 aces return a NU
2ab60 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68 LL pointer if th
2ab70 65 79 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f ey are unable to
2ab80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c .** all
2ab90 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 ocate memory to
2aba0 68 6f 6c 64 20 74 68 65 69 72 20 6e 6f 72 6d 61 hold their norma
2abb0 6c 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 73 l return strings
2abc0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32 35 7d ..**.** {H13725}
2abd0 20 49 66 20 74 68 65 20 4e 20 70 61 72 61 6d 65 If the N parame
2abe0 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ter to [sqlite3_
2abf0 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 53 2c 4e 29 column_name(S,N)
2ac00 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ] or.**
2ac10 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
2ac20 5f 6e 61 6d 65 31 36 28 53 2c 4e 29 5d 20 69 73 _name16(S,N)] is
2ac30 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 out of range, t
2ac40 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 hen the.**
2ac50 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72 interfaces r
2ac60 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 eturn a NULL poi
2ac70 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 nter..**.** {H13
2ac80 37 32 36 7d 20 54 68 65 20 73 74 72 69 6e 67 73 726} The strings
2ac90 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 returned by [sq
2aca0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
2acb0 65 28 53 2c 4e 29 5d 20 61 6e 64 0a 2a 2a 20 20 e(S,N)] and.**
2acc0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 [sqlite3
2acd0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 53 _column_name16(S
2ace0 2c 4e 29 5d 20 61 72 65 20 76 61 6c 69 64 20 75 ,N)] are valid u
2acf0 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a ntil the next.**
2ad00 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 20 74 call t
2ad10 6f 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 o either routine
2ad20 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 53 with the same S
2ad30 20 61 6e 64 20 4e 20 70 61 72 61 6d 65 74 65 72 and N parameter
2ad40 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 s.** or
2ad50 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f until [sqlite3_
2ad60 66 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 finalize(S)] is
2ad70 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 called..**.** {H
2ad80 31 33 37 32 37 7d 20 57 68 65 6e 20 61 20 72 65 13727} When a re
2ad90 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 sult column of a
2ada0 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d [SELECT] statem
2adb0 65 6e 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 ent contains.**
2adc0 20 20 20 20 20 20 20 20 20 61 6e 20 41 53 20 63 an AS c
2add0 6c 61 75 73 65 2c 20 74 68 65 20 6e 61 6d 65 20 lause, the name
2ade0 6f 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 of that column i
2adf0 73 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 s the identifier
2ae00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 .** to
2ae10 74 68 65 20 72 69 67 68 74 20 6f 66 20 74 68 65 the right of the
2ae20 20 41 53 20 6b 65 79 77 6f 72 64 2e 0a 2a 2f 0a AS keyword..*/.
2ae30 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
2ae40 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
2ae50 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 olumn_name(sqlit
2ae60 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 e3_stmt*, int N)
2ae70 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e ;.SQLITE_API con
2ae80 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
2ae90 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 _column_name16(s
2aea0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
2aeb0 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 t N);../*.** CAP
2aec0 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 I3REF: Source Of
2aed0 20 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 Data In A Query
2aee0 20 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d Result {H13740}
2aef0 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
2af00 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 These routines p
2af10 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 rovide a means t
2af20 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 o determine what
2af30 20 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a column of what.
2af40 2a 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 ** table in whic
2af50 68 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73 h database a res
2af60 75 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 ult of a [SELECT
2af70 5d 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 ] statement come
2af80 73 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e s from..** The n
2af90 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
2afa0 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 ase or table or
2afb0 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 column can be re
2afc0 74 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 turned as.** eit
2afd0 68 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 her a UTF-8 or U
2afe0 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 TF-16 string. T
2aff0 68 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f he _database_ ro
2b000 75 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a utines return.**
2b010 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 the database na
2b020 6d 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 me, the _table_
2b030 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
2b040 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 the table name,
2b050 61 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 and.** the origi
2b060 6e 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 n_ routines retu
2b070 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 rn the column na
2b080 6d 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 me..** The retur
2b090 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 ned string is va
2b0a0 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 lid until the [p
2b0b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2b0c0 74 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a t] is destroyed.
2b0d0 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ** using [sqlite
2b0e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 3_finalize()] or
2b0f0 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 until the same
2b100 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 information is r
2b110 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 equested.** agai
2b120 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 n in a different
2b130 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a encoding..**.**
2b140 20 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 The names retur
2b150 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 ned are the orig
2b160 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 inal un-aliased
2b170 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 names of the.**
2b180 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c database, table,
2b190 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a and column..**.
2b1a0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 ** The first arg
2b1b0 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c ument to the fol
2b1c0 6c 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 lowing calls is
2b1d0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
2b1e0 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 ement]..** These
2b1f0 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 functions retur
2b200 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 n information ab
2b210 6f 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 out the Nth colu
2b220 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a mn returned by.*
2b230 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c * the statement,
2b240 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 where N is the
2b250 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 second function
2b260 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
2b270 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d If the Nth colum
2b280 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 n returned by th
2b290 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 e statement is a
2b2a0 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a n expression or.
2b2b0 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 ** subquery and
2b2c0 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 is not a column
2b2d0 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 value, then all
2b2e0 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f of these functio
2b2f0 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c ns return.** NUL
2b300 4c 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e L. These routin
2b310 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 e might also ret
2b320 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 urn NULL if a me
2b330 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
2b340 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e error.** occurs.
2b350 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 Otherwise, the
2b360 79 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d y return the nam
2b370 65 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 e of the attache
2b380 64 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c d database, tabl
2b390 65 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 e.** and column
2b3a0 74 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c that query resul
2b3b0 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 t column was ext
2b3c0 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a racted from..**.
2b3d0 2a 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f ** As with all o
2b3e0 74 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 ther SQLite APIs
2b3f0 2c 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 , those postfixe
2b400 64 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75 d with "16" retu
2b410 72 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 rn.** UTF-16 enc
2b420 6f 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 oded strings, th
2b430 65 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e e other function
2b440 73 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 s return UTF-8.
2b450 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 {END}.**.** Thes
2b460 65 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 e APIs are only
2b470 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 available if the
2b480 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d library was com
2b490 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a piled with the.*
2b4a0 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 * [SQLITE_ENABLE
2b4b0 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
2b4c0 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 ] C-preprocessor
2b4d0 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e symbol defined.
2b4e0 0a 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a .**.** {A13751}.
2b4f0 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 ** If two or mor
2b500 65 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f e threads call o
2b510 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
2b520 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 ese routines aga
2b530 69 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a inst the same.**
2b540 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
2b550 65 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 ent and column a
2b560 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 t the same time
2b570 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 then the results
2b580 20 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 are.** undefine
2b590 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 d..**.** INVARIA
2b5a0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 NTS:.**.** {H137
2b5b0 34 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 41} The [sqlite3
2b5c0 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 _column_database
2b5d0 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 _name(S,N)] inte
2b5e0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 rface returns ei
2b5f0 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ther.**
2b600 20 74 68 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d the UTF-8 zero-
2b610 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 terminated name
2b620 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
2b630 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 0a 2a from which the.*
2b640 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72 * Nth r
2b650 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 esult column of
2b660 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 the [prepared st
2b670 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78 atement] S is ex
2b680 74 72 61 63 74 65 64 2c 0a 2a 2a 20 20 20 20 20 tracted,.**
2b690 20 20 20 20 20 6f 72 20 4e 55 4c 4c 20 69 66 20 or NULL if
2b6a0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f the Nth column o
2b6b0 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c f S is a general
2b6c0 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 expression.**
2b6d0 20 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e or if un
2b6e0 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 able to allocate
2b6f0 20 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 memory to store
2b700 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a the name..**.**
2b710 20 7b 48 31 33 37 34 32 7d 20 54 68 65 20 5b 73 {H13742} The [s
2b720 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 qlite3_column_da
2b730 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 53 2c tabase_name16(S,
2b740 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 N)] interface re
2b750 74 75 72 6e 73 20 65 69 74 68 65 72 0a 2a 2a 20 turns either.**
2b760 20 20 20 20 20 20 20 20 20 74 68 65 20 55 54 46 the UTF
2b770 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 -16 native byte
2b780 6f 72 64 65 72 20 7a 65 72 6f 2d 74 65 72 6d 69 order zero-termi
2b790 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 nated name of th
2b7a0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 e database.**
2b7b0 20 20 20 20 20 20 20 66 72 6f 6d 20 77 68 69 63 from whic
2b7c0 68 20 74 68 65 20 4e 74 68 20 72 65 73 75 6c 74 h the Nth result
2b7d0 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b column of the [
2b7e0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2b7f0 6e 74 5d 20 53 20 69 73 0a 2a 2a 20 20 20 20 20 nt] S is.**
2b800 20 20 20 20 20 65 78 74 72 61 63 74 65 64 2c 20 extracted,
2b810 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e or NULL if the N
2b820 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 th column of S i
2b830 73 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 s a general expr
2b840 65 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 ession.**
2b850 20 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 or if unable
2b860 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f to allocate memo
2b870 72 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 ry to store the
2b880 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 name..**.** {H13
2b890 37 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 743} The [sqlite
2b8a0 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 3_column_table_n
2b8b0 61 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 ame(S,N)] interf
2b8c0 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 ace returns eith
2b8d0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 er.** t
2b8e0 68 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 65 he UTF-8 zero-te
2b8f0 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66 rminated name of
2b900 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 the table from
2b910 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 20 20 20 which the.**
2b920 20 20 20 20 20 20 4e 74 68 20 72 65 73 75 6c 74 Nth result
2b930 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b column of the [
2b940 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2b950 6e 74 5d 20 53 20 69 73 20 65 78 74 72 61 63 74 nt] S is extract
2b960 65 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ed,.**
2b970 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e or NULL if the N
2b980 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 th column of S i
2b990 73 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 s a general expr
2b9a0 65 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 ession.**
2b9b0 20 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 or if unable
2b9c0 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f to allocate memo
2b9d0 72 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 ry to store the
2b9e0 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 name..**.** {H13
2b9f0 37 34 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 744} The [sqlite
2ba00 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 3_column_table_n
2ba10 61 6d 65 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 ame16(S,N)] inte
2ba20 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 rface returns ei
2ba30 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ther.**
2ba40 20 74 68 65 20 55 54 46 2d 31 36 20 6e 61 74 69 the UTF-16 nati
2ba50 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 7a 65 ve byte order ze
2ba60 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 ro-terminated na
2ba70 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a me of the table.
2ba80 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d ** from
2ba90 20 77 68 69 63 68 20 74 68 65 20 4e 74 68 20 72 which the Nth r
2baa0 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 esult column of
2bab0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 the [prepared st
2bac0 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 0a 2a 2a atement] S is.**
2bad0 20 20 20 20 20 20 20 20 20 20 65 78 74 72 61 63 extrac
2bae0 74 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 ted, or NULL if
2baf0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f the Nth column o
2bb00 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c f S is a general
2bb10 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 expression.**
2bb20 20 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e or if un
2bb30 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 able to allocate
2bb40 20 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 memory to store
2bb50 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a the name..**.**
2bb60 20 7b 48 31 33 37 34 35 7d 20 54 68 65 20 5b 73 {H13745} The [s
2bb70 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 qlite3_column_or
2bb80 69 67 69 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20 igin_name(S,N)]
2bb90 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
2bba0 73 20 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 s either.**
2bbb0 20 20 20 20 20 74 68 65 20 55 54 46 2d 38 20 7a the UTF-8 z
2bbc0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e ero-terminated n
2bbd0 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
2bbe0 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 column from whi
2bbf0 63 68 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 ch the.**
2bc00 20 20 20 4e 74 68 20 72 65 73 75 6c 74 20 63 6f Nth result co
2bc10 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 72 65 lumn of the [pre
2bc20 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
2bc30 20 53 20 69 73 20 65 78 74 72 61 63 74 65 64 2c S is extracted,
2bc40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 .** or
2bc50 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 68 20 NULL if the Nth
2bc60 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 20 61 column of S is a
2bc70 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65 73 73 general express
2bc80 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ion.**
2bc90 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 or if unable to
2bca0 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 allocate memory
2bcb0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e 61 6d to store the nam
2bcc0 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34 36 e..**.** {H13746
2bcd0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 } The [sqlite3_c
2bce0 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d olumn_origin_nam
2bcf0 65 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 e16(S,N)] interf
2bd00 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 ace returns eith
2bd10 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 er.** t
2bd20 68 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 he UTF-16 native
2bd30 20 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72 6f byte order zero
2bd40 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 -terminated name
2bd50 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a of the table.**
2bd60 20 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e column
2bd70 20 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20 from which the
2bd80 4e 74 68 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d Nth result colum
2bd90 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 n of the.**
2bda0 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 [prepared s
2bdb0 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 tatement] S is e
2bdc0 78 74 72 61 63 74 65 64 2c 20 6f 72 20 4e 55 4c xtracted, or NUL
2bdd0 4c 20 69 66 20 74 68 65 20 4e 74 68 20 63 6f 6c L if the Nth col
2bde0 75 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 umn.**
2bdf0 6f 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 of S is a genera
2be00 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 l expression or
2be10 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c if unable to all
2be20 6f 63 61 74 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ocate memory.**
2be30 20 20 20 20 20 20 20 20 20 74 6f 20 73 74 6f 72 to stor
2be40 65 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a e the name..**.*
2be50 2a 20 7b 48 31 33 37 34 38 7d 20 54 68 65 20 72 * {H13748} The r
2be60 65 74 75 72 6e 20 76 61 6c 75 65 73 20 66 72 6f eturn values fro
2be70 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 m.** [s
2be80 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 qlite3_column_da
2be90 74 61 62 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f tabase_name | co
2bea0 6c 75 6d 6e 20 6d 65 74 61 64 61 74 61 20 69 6e lumn metadata in
2beb0 74 65 72 66 61 63 65 73 5d 0a 2a 2a 20 20 20 20 terfaces].**
2bec0 20 20 20 20 20 20 61 72 65 20 76 61 6c 69 64 20 are valid
2bed0 66 6f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65 for the lifetime
2bee0 20 6f 66 20 74 68 65 20 5b 70 72 65 70 61 72 65 of the [prepare
2bef0 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 d statement].**
2bf00 20 20 20 20 20 20 20 20 20 6f 72 20 75 6e 74 69 or unti
2bf10 6c 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 l the encoding i
2bf20 73 20 63 68 61 6e 67 65 64 20 62 79 20 61 6e 6f s changed by ano
2bf30 74 68 65 72 20 6d 65 74 61 64 61 74 61 0a 2a 2a ther metadata.**
2bf40 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 interf
2bf50 61 63 65 20 63 61 6c 6c 20 66 6f 72 20 74 68 65 ace call for the
2bf60 20 73 61 6d 65 20 70 72 65 70 61 72 65 64 20 73 same prepared s
2bf70 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c tatement and col
2bf80 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d umn..**.** ASSUM
2bf90 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 PTIONS:.**.** {A
2bfa0 31 33 37 35 31 7d 20 49 66 20 74 77 6f 20 6f 72 13751} If two or
2bfb0 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 more threads ca
2bfc0 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a ll one or more.*
2bfd0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
2bfe0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 te3_column_datab
2bff0 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d ase_name | colum
2c000 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 n metadata inter
2c010 66 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20 20 20 faces].**
2c020 20 20 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 for the same
2c030 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
2c040 65 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 ent] and result
2c050 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 column.**
2c060 20 20 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 at the same t
2c070 69 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 ime then the res
2c080 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e ults are undefin
2c090 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ed..*/.SQLITE_AP
2c0a0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
2c0b0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 lite3_column_dat
2c0c0 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 abase_name(sqlit
2c0d0 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 e3_stmt*,int);.S
2c0e0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
2c0f0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
2c100 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 lumn_database_na
2c110 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d me16(sqlite3_stm
2c120 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
2c130 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
2c140 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
2c150 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 able_name(sqlite
2c160 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 3_stmt*,int);.SQ
2c170 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
2c180 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
2c190 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 umn_table_name16
2c1a0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 (sqlite3_stmt*,i
2c1b0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
2c1c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
2c1d0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 te3_column_origi
2c1e0 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 n_name(sqlite3_s
2c1f0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 tmt*,int);.SQLIT
2c200 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
2c210 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
2c220 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 _origin_name16(s
2c230 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 qlite3_stmt*,int
2c240 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
2c250 45 46 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74 EF: Declared Dat
2c260 61 74 79 70 65 20 4f 66 20 41 20 51 75 65 72 79 atype Of A Query
2c270 20 52 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d Result {H13760}
2c280 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
2c290 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
2c2a0 74 65 72 20 69 73 20 61 20 5b 70 72 65 70 61 72 ter is a [prepar
2c2b0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a ed statement]..*
2c2c0 2a 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d * If this statem
2c2d0 65 6e 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54 ent is a [SELECT
2c2e0 5d 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 ] statement and
2c2f0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f the Nth column o
2c300 66 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 f the.** returne
2c310 64 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 d result set of
2c320 74 68 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 that [SELECT] is
2c330 20 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 a table column
2c340 28 6e 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 (not an.** expre
2c350 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 ssion or subquer
2c360 79 29 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c y) then the decl
2c370 61 72 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 ared type of the
2c380 20 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e table.** column
2c390 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 is returned. I
2c3a0 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e f the Nth column
2c3b0 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 of the result s
2c3c0 65 74 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 et is an.** expr
2c3d0 65 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 ession or subque
2c3e0 72 79 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 ry, then a NULL
2c3f0 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 pointer is retur
2c400 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 ned..** The retu
2c410 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 rned string is a
2c420 6c 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f lways UTF-8 enco
2c430 64 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a ded. {END}.**.**
2c440 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 For example, gi
2c450 76 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 ven the database
2c460 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 schema:.**.** C
2c470 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 REATE TABLE t1(c
2c480 31 20 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 1 VARIANT);.**.*
2c490 2a 20 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 * and the follow
2c4a0 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f ing statement to
2c4b0 20 62 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a be compiled:.**
2c4c0 0a 2a 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20 .** SELECT c1 +
2c4d0 31 2c 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 1, c1 FROM t1;.*
2c4e0 2a 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e *.** this routin
2c4f0 65 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 e would return t
2c500 68 65 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 he string "VARIA
2c510 4e 54 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f NT" for the seco
2c520 6e 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c nd result.** col
2c530 75 6d 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 umn (i==1), and
2c540 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 a NULL pointer f
2c550 6f 72 20 74 68 65 20 66 69 72 73 74 20 72 65 73 or the first res
2c560 75 6c 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 ult column (i==0
2c570 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 )..**.** SQLite
2c580 75 73 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e uses dynamic run
2c590 2d 74 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 -time typing. S
2c5a0 6f 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 o just because a
2c5b0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 column.** is de
2c5c0 63 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 clared to contai
2c5d0 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 n a particular t
2c5e0 79 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 ype does not mea
2c5f0 6e 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 n that the.** da
2c600 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 ta stored in tha
2c610 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 t column is of t
2c620 68 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 he declared type
2c630 2e 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 . SQLite is.**
2c640 73 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 strongly typed,
2c650 62 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 but the typing i
2c660 73 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 s dynamic not st
2c670 61 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 atic. Type.** i
2c680 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
2c690 68 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c h individual val
2c6a0 75 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 ues, not with th
2c6b0 65 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 e containers.**
2c6c0 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f used to hold tho
2c6d0 73 65 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a se values..**.**
2c6e0 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
2c6f0 2a 2a 20 7b 48 31 33 37 36 31 7d 20 20 41 20 73 ** {H13761} A s
2c700 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
2c710 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d o [sqlite3_colum
2c720 6e 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d n_decltype(S,N)]
2c730 20 72 65 74 75 72 6e 73 20 61 0a 2a 2a 20 20 20 returns a.**
2c740 20 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72 zero-ter
2c750 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 minated UTF-8 st
2c760 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ring containing
2c770 74 68 65 20 64 65 63 6c 61 72 65 64 20 64 61 74 the declared dat
2c780 61 74 79 70 65 0a 2a 2a 20 20 20 20 20 20 20 20 atype.**
2c790 20 20 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 of the table
2c7a0 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 column that appe
2c7b0 61 72 73 20 61 73 20 74 68 65 20 4e 74 68 20 63 ars as the Nth c
2c7c0 6f 6c 75 6d 6e 20 28 6e 75 6d 62 65 72 65 64 0a olumn (numbered.
2c7d0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 66 72 6f ** fro
2c7e0 6d 20 30 29 20 6f 66 20 74 68 65 20 72 65 73 75 m 0) of the resu
2c7f0 6c 74 20 73 65 74 20 74 6f 20 74 68 65 20 5b 70 lt set to the [p
2c800 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
2c810 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 t] S..**.** {H13
2c820 37 36 32 7d 20 20 41 20 73 75 63 63 65 73 73 66 762} A successf
2c830 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ul call to [sqli
2c840 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 te3_column_declt
2c850 79 70 65 31 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 ype16(S,N)].**
2c860 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 returns
2c870 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 a zero-terminat
2c880 65 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 ed UTF-16 native
2c890 20 62 79 74 65 20 6f 72 64 65 72 20 73 74 72 69 byte order stri
2c8a0 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ng.**
2c8b0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 64 containing the d
2c8c0 65 63 6c 61 72 65 64 20 64 61 74 61 74 79 70 65 eclared datatype
2c8d0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f of the table co
2c8e0 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72 lumn that appear
2c8f0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 s.** a
2c900 73 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e s the Nth column
2c910 20 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 (numbered from
2c920 30 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 0) of the result
2c930 20 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 20 set to the.**
2c940 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 [prepar
2c950 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 2e ed statement] S.
2c960 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 36 33 7d 20 .**.** {H13763}
2c970 20 49 66 20 4e 20 69 73 20 6c 65 73 73 20 74 68 If N is less th
2c980 61 6e 20 30 20 6f 72 20 4e 20 69 73 20 67 72 65 an 0 or N is gre
2c990 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
2c9a0 61 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 al to.**
2c9b0 20 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 the number of
2c9c0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
2c9d0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
2c9e0 65 6e 74 5d 20 53 2c 0a 2a 2a 20 20 20 20 20 20 ent] S,.**
2c9f0 20 20 20 20 20 6f 72 20 69 66 20 74 68 65 20 4e or if the N
2ca00 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 th column of S i
2ca10 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 s an expression
2ca20 6f 72 20 73 75 62 71 75 65 72 79 20 72 61 74 68 or subquery rath
2ca30 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 er.**
2ca40 74 68 61 6e 20 61 20 74 61 62 6c 65 20 63 6f 6c than a table col
2ca50 75 6d 6e 2c 20 6f 72 20 69 66 20 61 20 6d 65 6d umn, or if a mem
2ca60 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
2ca70 61 69 6c 75 72 65 0a 2a 2a 20 20 20 20 20 20 20 ailure.**
2ca80 20 20 20 20 6f 63 63 75 72 73 20 64 75 72 69 6e occurs durin
2ca90 67 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 g encoding conve
2caa0 72 73 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a 2a 20 rsions, then.**
2cab0 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 73 20 calls
2cac0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to [sqlite3_colu
2cad0 6d 6e 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 mn_decltype(S,N)
2cae0 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ] or.**
2caf0 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d [sqlite3_colum
2cb00 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 53 2c 4e n_decltype16(S,N
2cb10 29 5d 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a )] return NULL..
2cb20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
2cb30 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
2cb40 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 3_column_decltyp
2cb50 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
2cb60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
2cb70 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
2cb80 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c ite3_column_decl
2cb90 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73 type16(sqlite3_s
2cba0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a tmt*,int);../*.*
2cbb0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 76 61 6c * CAPI3REF: Eval
2cbc0 75 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61 74 uate An SQL Stat
2cbd0 65 6d 65 6e 74 20 7b 48 31 33 32 30 30 7d 20 3c ement {H13200} <
2cbe0 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 66 S10000>.**.** Af
2cbf0 74 65 72 20 61 20 5b 70 72 65 70 61 72 65 64 20 ter a [prepared
2cc00 73 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20 62 statement] has b
2cc10 65 65 6e 20 70 72 65 70 61 72 65 64 20 75 73 69 een prepared usi
2cc20 6e 67 20 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 ng either.** [sq
2cc30 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
2cc40 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
2cc50 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 prepare16_v2()]
2cc60 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 65 or one of the le
2cc70 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 gacy.** interfac
2cc80 65 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 es [sqlite3_prep
2cc90 61 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 are()] or [sqlit
2cca0 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2c e3_prepare16()],
2ccb0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a this function.*
2ccc0 2a 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 * must be called
2ccd0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d one or more tim
2cce0 65 73 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 es to evaluate t
2ccf0 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a he statement..**
2cd00 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 .** The details
2cd10 6f 66 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 of the behavior
2cd20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 of the sqlite3_s
2cd30 74 65 70 28 29 20 69 6e 74 65 72 66 61 63 65 20 tep() interface
2cd40 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77 68 65 depend.** on whe
2cd50 74 68 65 72 20 74 68 65 20 73 74 61 74 65 6d 65 ther the stateme
2cd60 6e 74 20 77 61 73 20 70 72 65 70 61 72 65 64 20 nt was prepared
2cd70 75 73 69 6e 67 20 74 68 65 20 6e 65 77 65 72 20 using the newer
2cd80 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 0a 2a "v2" interface.*
2cd90 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
2cda0 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 re_v2()] and [sq
2cdb0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
2cdc0 76 32 28 29 5d 20 6f 72 20 74 68 65 20 6f 6c 64 v2()] or the old
2cdd0 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 er legacy.** int
2cde0 65 72 66 61 63 65 20 5b 73 71 6c 69 74 65 33 5f erface [sqlite3_
2cdf0 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b prepare()] and [
2ce00 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
2ce10 36 28 29 5d 2e 20 20 54 68 65 20 75 73 65 20 6f 6()]. The use o
2ce20 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22 76 32 f the.** new "v2
2ce30 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 " interface is r
2ce40 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6e ecommended for n
2ce50 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 ew applications
2ce60 62 75 74 20 74 68 65 20 6c 65 67 61 63 79 0a 2a but the legacy.*
2ce70 2a 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c * interface will
2ce80 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 20 continue to be
2ce90 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a supported..**.**
2cea0 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 In the legacy i
2ceb0 6e 74 65 72 66 61 63 65 2c 20 74 68 65 20 72 65 nterface, the re
2cec0 74 75 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c 20 turn value will
2ced0 62 65 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 be either [SQLIT
2cee0 45 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b 53 51 4c E_BUSY],.** [SQL
2cef0 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49 ITE_DONE], [SQLI
2cf00 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54 45 TE_ROW], [SQLITE
2cf10 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51 4c _ERROR], or [SQL
2cf20 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a 2a 2a 20 ITE_MISUSE]..**
2cf30 57 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e With the "v2" in
2cf40 74 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66 20 terface, any of
2cf50 74 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75 6c the other [resul
2cf60 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a 2a 20 5b t codes] or.** [
2cf70 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
2cf80 63 6f 64 65 73 5d 20 6d 69 67 68 74 20 62 65 20 codes] might be
2cf90 72 65 74 75 72 6e 65 64 20 61 73 20 77 65 6c 6c returned as well
2cfa0 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f ..**.** [SQLITE_
2cfb0 42 55 53 59 5d 20 6d 65 61 6e 73 20 74 68 61 74 BUSY] means that
2cfc0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e the database en
2cfd0 67 69 6e 65 20 77 61 73 20 75 6e 61 62 6c 65 20 gine was unable
2cfe0 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 0a 2a to acquire the.*
2cff0 2a 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 * database locks
2d000 20 69 74 20 6e 65 65 64 73 20 74 6f 20 64 6f 20 it needs to do
2d010 69 74 73 20 6a 6f 62 2e 20 20 49 66 20 74 68 65 its job. If the
2d020 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 statement is a
2d030 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f [COMMIT].** or o
2d040 63 63 75 72 73 20 6f 75 74 73 69 64 65 20 6f 66 ccurs outside of
2d050 20 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 an explicit tra
2d060 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 79 nsaction, then y
2d070 6f 75 20 63 61 6e 20 72 65 74 72 79 20 74 68 65 ou can retry the
2d080 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 .** statement.
2d090 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 If the statement
2d0a0 20 69 73 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49 is not a [COMMI
2d0b0 54 5d 20 61 6e 64 20 6f 63 63 75 72 73 20 77 69 T] and occurs wi
2d0c0 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69 63 thin a.** explic
2d0d0 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 it transaction t
2d0e0 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 72 hen you should r
2d0f0 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e ollback the tran
2d100 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a 2a saction before.*
2d110 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a * continuing..**
2d120 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 .** [SQLITE_DONE
2d130 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 ] means that the
2d140 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66 statement has f
2d150 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e inished executin
2d160 67 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c g.** successfull
2d170 79 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 y. sqlite3_step
2d180 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 () should not be
2d190 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e called again on
2d1a0 20 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a 2a this virtual.**
2d1b0 20 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 74 machine without
2d1c0 20 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 5b first calling [
2d1d0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
2d1e0 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 to reset the vi
2d1f0 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 rtual.** machine
2d200 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 back to its ini
2d210 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a tial state..**.*
2d220 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 * If the SQL sta
2d230 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 65 tement being exe
2d240 63 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 6e cuted returns an
2d250 79 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53 51 y data, then [SQ
2d260 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20 LITE_ROW].** is
2d270 72 65 74 75 72 6e 65 64 20 65 61 63 68 20 74 69 returned each ti
2d280 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 20 me a new row of
2d290 64 61 74 61 20 69 73 20 72 65 61 64 79 20 66 6f data is ready fo
2d2a0 72 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79 20 r processing by
2d2b0 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 the.** caller. T
2d2c0 68 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 he values may be
2d2d0 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67 20 accessed using
2d2e0 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 the [column acce
2d2f0 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a ss functions]..*
2d300 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 * sqlite3_step()
2d310 20 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 6e is called again
2d320 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 to retrieve the
2d330 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 74 next row of dat
2d340 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 a..**.** [SQLITE
2d350 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74 68 _ERROR] means th
2d360 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 at a run-time er
2d370 72 6f 72 20 28 73 75 63 68 20 61 73 20 61 20 63 ror (such as a c
2d380 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69 6f onstraint.** vio
2d390 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63 75 lation) has occu
2d3a0 72 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f 73 rred. sqlite3_s
2d3b0 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 tep() should not
2d3c0 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e be called again
2d3d0 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 4d on.** the VM. M
2d3e0 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ore information
2d3f0 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79 20 may be found by
2d400 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 calling [sqlite3
2d410 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 57 _errmsg()]..** W
2d420 69 74 68 20 74 68 65 20 6c 65 67 61 63 79 20 69 ith the legacy i
2d430 6e 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72 65 nterface, a more
2d440 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 specific error
2d450 63 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70 6c code (for exampl
2d460 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e e,.** [SQLITE_IN
2d470 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49 54 TERRUPT], [SQLIT
2d480 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c 49 E_SCHEMA], [SQLI
2d490 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e 64 TE_CORRUPT], and
2d4a0 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63 61 so forth).** ca
2d4b0 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 n be obtained by
2d4c0 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 calling [sqlite
2d4d0 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74 68 3_reset()] on th
2d4e0 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 e.** [prepared s
2d4f0 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 6e 20 74 tatement]. In t
2d500 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 he "v2" interfac
2d510 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 e,.** the more s
2d520 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63 6f pecific error co
2d530 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 64 de is returned d
2d540 69 72 65 63 74 6c 79 20 62 79 20 73 71 6c 69 74 irectly by sqlit
2d550 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a e3_step()..**.**
2d560 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d [SQLITE_MISUSE]
2d570 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
2d580 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 this routine was
2d590 20 63 61 6c 6c 65 64 20 69 6e 61 70 70 72 6f 70 called inapprop
2d5a0 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 65 72 68 riately..** Perh
2d5b0 61 70 73 20 69 74 20 77 61 73 20 63 61 6c 6c 65 aps it was calle
2d5c0 64 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 d on a [prepared
2d5d0 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 statement] that
2d5e0 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 has.** already
2d5f0 62 65 65 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 been [sqlite3_fi
2d600 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a nalize | finaliz
2d610 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 20 74 68 ed] or on one th
2d620 61 74 20 68 61 64 0a 2a 2a 20 70 72 65 76 69 6f at had.** previo
2d630 75 73 6c 79 20 72 65 74 75 72 6e 65 64 20 5b 53 usly returned [S
2d640 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6f 72 20 QLITE_ERROR] or
2d650 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 [SQLITE_DONE].
2d660 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a 2a 20 62 Or it could.** b
2d670 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 e the case that
2d680 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
2d690 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
2d6a0 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 74 77 being used by tw
2d6b0 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 68 72 o or.** more thr
2d6c0 65 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 eads at the same
2d6d0 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 6d 65 2e moment in time.
2d6e0 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20 .**.** <b>Goofy
2d6f0 49 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74 3a Interface Alert:
2d700 3c 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67 61 </b> In the lega
2d710 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 cy interface, th
2d720 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 e sqlite3_step()
2d730 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20 72 .** API always r
2d740 65 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69 63 eturns a generic
2d750 20 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53 51 error code, [SQ
2d760 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c LITE_ERROR], fol
2d770 6c 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a 20 65 72 lowing any.** er
2d780 72 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20 5b ror other than [
2d790 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e 64 SQLITE_BUSY] and
2d7a0 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d [SQLITE_MISUSE]
2d7b0 2e 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c 6c . You must call
2d7c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 .** [sqlite3_res
2d7d0 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 et()] or [sqlite
2d7e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 6e 3_finalize()] in
2d7f0 20 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20 6f order to find o
2d800 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 70 65 ne of the.** spe
2d810 63 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 cific [error cod
2d820 65 73 5d 20 74 68 61 74 20 62 65 74 74 65 72 20 es] that better
2d830 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 describes the er
2d840 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d 69 74 ror..** We admit
2d850 20 74 68 61 74 20 74 68 69 73 20 69 73 20 61 20 that this is a
2d860 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20 20 54 goofy design. T
2d870 68 65 20 70 72 6f 62 6c 65 6d 20 68 61 73 20 62 he problem has b
2d880 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77 69 74 een fixed.** wit
2d890 68 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 h the "v2" inter
2d8a0 66 61 63 65 2e 20 20 49 66 20 79 6f 75 20 70 72 face. If you pr
2d8b0 65 70 61 72 65 20 61 6c 6c 20 6f 66 20 79 6f 75 epare all of you
2d8c0 72 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 r SQL statements
2d8d0 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74 68 65 72 .** using either
2d8e0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
2d8f0 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 e_v2()] or [sqli
2d900 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 te3_prepare16_v2
2d910 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f ()] instead.** o
2d920 66 20 74 68 65 20 6c 65 67 61 63 79 20 5b 73 71 f the legacy [sq
2d930 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d lite3_prepare()]
2d940 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 and [sqlite3_pr
2d950 65 70 61 72 65 31 36 28 29 5d 20 69 6e 74 65 72 epare16()] inter
2d960 66 61 63 65 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 faces,.** then t
2d970 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 he more specific
2d980 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 61 [error codes] a
2d990 72 65 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 re returned dire
2d9a0 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c 69 74 ctly.** by sqlit
2d9b0 65 33 5f 73 74 65 70 28 29 2e 20 20 54 68 65 20 e3_step(). The
2d9c0 75 73 65 20 6f 66 20 74 68 65 20 22 76 32 22 20 use of the "v2"
2d9d0 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63 interface is rec
2d9e0 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 ommended..**.**
2d9f0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a INVARIANTS:.**.*
2da00 2a 20 7b 48 31 33 32 30 32 7d 20 20 49 66 20 74 * {H13202} If t
2da10 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
2da20 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 72 65 61 tement] S is rea
2da30 64 79 20 74 6f 20 62 65 20 72 75 6e 2c 20 74 68 dy to be run, th
2da40 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 en.**
2da50 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 [sqlite3_step(S)
2da60 5d 20 61 64 76 61 6e 63 65 73 20 74 68 61 74 20 ] advances that
2da70 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2da80 6e 74 20 75 6e 74 69 6c 0a 2a 2a 20 20 20 20 20 nt until.**
2da90 20 20 20 20 20 20 63 6f 6d 70 6c 65 74 69 6f 6e completion
2daa0 20 6f 72 20 75 6e 74 69 6c 20 69 74 20 69 73 20 or until it is
2dab0 72 65 61 64 79 20 74 6f 20 72 65 74 75 72 6e 20 ready to return
2dac0 61 6e 6f 74 68 65 72 20 72 6f 77 20 6f 66 20 74 another row of t
2dad0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 he.**
2dae0 72 65 73 75 6c 74 20 73 65 74 2c 20 6f 72 20 75 result set, or u
2daf0 6e 74 69 6c 20 61 6e 20 5b 73 71 6c 69 74 65 33 ntil an [sqlite3
2db00 5f 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 _interrupt | int
2db10 65 72 72 75 70 74 5d 0a 2a 2a 20 20 20 20 20 20 errupt].**
2db20 20 20 20 20 20 6f 72 20 61 20 72 75 6e 2d 74 69 or a run-ti
2db30 6d 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e me error occurs.
2db40 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30 34 7d 20 .**.** {H15304}
2db50 20 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 When a call to
2db60 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 [sqlite3_step(S)
2db70 5d 20 63 61 75 73 65 73 20 74 68 65 20 5b 70 72 ] causes the [pr
2db80 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2db90 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 53 ].** S
2dba0 20 74 6f 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c to run to compl
2dbb0 65 74 69 6f 6e 2c 20 74 68 65 20 66 75 6e 63 74 etion, the funct
2dbc0 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c ion returns [SQL
2dbd0 49 54 45 5f 44 4f 4e 45 5d 2e 0a 2a 2a 0a 2a 2a ITE_DONE]..**.**
2dbe0 20 7b 48 31 35 33 30 36 7d 20 20 57 68 65 6e 20 {H15306} When
2dbf0 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 a call to [sqlit
2dc00 65 33 5f 73 74 65 70 28 53 29 5d 20 73 74 6f 70 e3_step(S)] stop
2dc10 73 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 s because it is
2dc20 72 65 61 64 79 20 74 6f 0a 2a 2a 20 20 20 20 20 ready to.**
2dc30 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 6e 6f return ano
2dc40 74 68 65 72 20 72 6f 77 20 6f 66 20 74 68 65 20 ther row of the
2dc50 72 65 73 75 6c 74 20 73 65 74 2c 20 69 74 20 72 result set, it r
2dc60 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 eturns [SQLITE_R
2dc70 4f 57 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 OW]..**.** {H153
2dc80 30 38 7d 20 20 49 66 20 61 20 63 61 6c 6c 20 74 08} If a call t
2dc90 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 o [sqlite3_step(
2dca0 53 29 5d 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 S)] encounters a
2dcb0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b n.** [
2dcc0 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
2dcd0 74 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 20 6f t | interrupt] o
2dce0 72 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 r a run-time err
2dcf0 6f 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or,.**
2dd00 20 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20 61 it returns an a
2dd10 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 ppropriate error
2dd20 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 6e 6f code that is no
2dd30 74 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20 t one of.**
2dd40 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b [SQLITE_OK
2dd50 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c ], [SQLITE_ROW],
2dd60 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 or [SQLITE_DONE
2dd70 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 31 30 ]..**.** {H15310
2dd80 7d 20 20 49 66 20 61 6e 20 5b 73 71 6c 69 74 65 } If an [sqlite
2dd90 33 5f 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 3_interrupt | in
2dda0 74 65 72 72 75 70 74 5d 20 6f 72 20 61 20 72 75 terrupt] or a ru
2ddb0 6e 2d 74 69 6d 65 20 65 72 72 6f 72 0a 2a 2a 20 n-time error.**
2ddc0 20 20 20 20 20 20 20 20 20 20 6f 63 63 75 72 73 occurs
2ddd0 20 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 during a call t
2dde0 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 o [sqlite3_step(
2ddf0 53 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 S)].**
2de00 20 66 6f 72 20 61 20 5b 70 72 65 70 61 72 65 64 for a [prepared
2de10 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 63 72 statement] S cr
2de20 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 eated using.**
2de30 20 20 20 20 20 20 20 20 20 6c 65 67 61 63 79 20 legacy
2de40 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69 interfaces [sqli
2de50 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f te3_prepare()] o
2de60 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b r.** [
2de70 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
2de80 36 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 66 6()], then the f
2de90 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
2dea0 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 either.**
2deb0 20 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f [SQLITE_ERRO
2dec0 52 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 R], [SQLITE_BUSY
2ded0 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 ], or [SQLITE_MI
2dee0 53 55 53 45 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 SUSE]..*/.SQLITE
2def0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2df00 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 _step(sqlite3_st
2df10 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 mt*);../*.** CAP
2df20 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 I3REF: Number of
2df30 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 columns in a re
2df40 73 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 sult set {H13770
2df50 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a } <S10700>.**.**
2df60 20 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d Returns the num
2df70 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e ber of values in
2df80 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
2df90 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 of the result s
2dfa0 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 et..**.** INVARI
2dfb0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 ANTS:.**.** {H13
2dfc0 37 37 31 7d 20 20 41 66 74 65 72 20 61 20 63 61 771} After a ca
2dfd0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 ll to [sqlite3_s
2dfe0 74 65 70 28 53 29 5d 20 74 68 61 74 20 72 65 74 tep(S)] that ret
2dff0 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f 57 urns [SQLITE_ROW
2e000 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ],.**
2e010 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 61 74 the [sqlite3_dat
2e020 61 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f 75 74 a_count(S)] rout
2e030 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ine will return
2e040 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 0a 2a the same value.*
2e050 2a 20 20 20 20 20 20 20 20 20 20 20 61 73 20 74 * as t
2e060 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 he [sqlite3_colu
2e070 6d 6e 5f 63 6f 75 6e 74 28 53 29 5d 20 66 75 6e mn_count(S)] fun
2e080 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ction..**.** {H1
2e090 33 37 37 32 7d 20 20 41 66 74 65 72 20 5b 73 71 3772} After [sq
2e0a0 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 lite3_step(S)] h
2e0b0 61 73 20 72 65 74 75 72 6e 65 64 20 61 6e 79 20 as returned any
2e0c0 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e value other than
2e0d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 53 .** [S
2e0e0 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 62 65 QLITE_ROW] or be
2e0f0 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 fore [sqlite3_st
2e100 65 70 28 53 29 5d 20 68 61 73 20 62 65 65 6e 20 ep(S)] has been
2e110 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 0a 2a 2a called on the.**
2e120 20 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 [prep
2e130 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
2e140 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 for the first ti
2e150 6d 65 20 73 69 6e 63 65 20 69 74 20 77 61 73 0a me since it was.
2e160 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 ** [sq
2e170 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 lite3_prepare |
2e180 70 72 65 70 61 72 65 64 5d 20 6f 72 20 5b 73 71 prepared] or [sq
2e190 6c 69 74 65 33 5f 72 65 73 65 74 20 7c 20 72 65 lite3_reset | re
2e1a0 73 65 74 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 set],.**
2e1b0 20 20 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f the [sqlite3_
2e1c0 64 61 74 61 5f 63 6f 75 6e 74 28 53 29 5d 20 72 data_count(S)] r
2e1d0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a outine returns z
2e1e0 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ero..*/.SQLITE_A
2e1f0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 PI int sqlite3_d
2e200 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 ata_count(sqlite
2e210 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 3_stmt *pStmt);.
2e220 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2e230 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74 Fundamental Dat
2e240 61 74 79 70 65 73 20 7b 48 31 30 32 36 35 7d 20 atypes {H10265}
2e250 3c 53 31 30 31 31 30 3e 3c 53 31 30 31 32 30 3e <S10110><S10120>
2e260 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 .** KEYWORDS: SQ
2e270 4c 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20 LITE_TEXT.**.**
2e280 7b 48 31 30 32 36 36 7d 20 45 76 65 72 79 20 76 {H10266} Every v
2e290 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 68 alue in SQLite h
2e2a0 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 66 as one of five f
2e2b0 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 74 undamental datat
2e2c0 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e ypes:.**.** <ul>
2e2d0 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 .** <li> 64-bit
2e2e0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a signed integer.*
2e2f0 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 45 * <li> 64-bit IE
2e300 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e EE floating poin
2e310 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e t number.** <li>
2e320 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 string.** <li>
2e330 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c BLOB.** <li> NUL
2e340 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d L.** </ul> {END}
2e350 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e .**.** These con
2e360 73 74 61 6e 74 73 20 61 72 65 20 63 6f 64 65 73 stants are codes
2e370 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 6f for each of tho
2e380 73 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 se types..**.**
2e390 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 53 51 Note that the SQ
2e3a0 4c 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 74 61 LITE_TEXT consta
2e3b0 6e 74 20 77 61 73 20 61 6c 73 6f 20 75 73 65 64 nt was also used
2e3c0 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69 in SQLite versi
2e3d0 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f on 2.** for a co
2e3e0 6d 70 6c 65 74 65 6c 79 20 64 69 66 66 65 72 65 mpletely differe
2e3f0 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66 nt meaning. Sof
2e400 74 77 61 72 65 20 74 68 61 74 20 6c 69 6e 6b 73 tware that links
2e410 20 61 67 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a against both.**
2e420 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 SQLite version
2e430 32 20 61 6e 64 20 53 51 4c 69 74 65 20 76 65 72 2 and SQLite ver
2e440 73 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 75 73 sion 3 should us
2e450 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 2c 20 e SQLITE3_TEXT,
2e460 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 not.** SQLITE_TE
2e470 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 XT..*/.#define S
2e480 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 20 31 QLITE_INTEGER 1
2e490 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
2e4a0 46 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 66 69 FLOAT 2.#defi
2e4b0 6e 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20 ne SQLITE_BLOB
2e4c0 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 4.#define SQL
2e4d0 49 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23 ITE_NULL 5.#
2e4e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 ifdef SQLITE_TEX
2e4f0 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 T.# undef SQLITE
2e500 5f 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 64 65 _TEXT.#else.# de
2e510 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58 54 fine SQLITE_TEXT
2e520 20 20 20 20 20 33 0a 23 65 6e 64 69 66 0a 23 64 3.#endif.#d
2e530 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 54 45 efine SQLITE3_TE
2e540 58 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 XT 3../*.**
2e550 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 CAPI3REF: Result
2e560 20 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 20 51 Values From A Q
2e570 75 65 72 79 20 7b 48 31 33 38 30 30 7d 20 3c 53 uery {H13800} <S
2e580 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 10700>.** KEYWOR
2e590 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 63 65 DS: {column acce
2e5a0 73 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a ss functions}.**
2e5b0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
2e5c0 65 73 20 66 6f 72 6d 20 74 68 65 20 22 72 65 73 es form the "res
2e5d0 75 6c 74 20 73 65 74 20 71 75 65 72 79 22 20 69 ult set query" i
2e5e0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
2e5f0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 These routines r
2e600 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f eturn informatio
2e610 6e 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 n about a single
2e620 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 column of the c
2e630 75 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 6c 74 urrent.** result
2e640 20 72 6f 77 20 6f 66 20 61 20 71 75 65 72 79 2e row of a query.
2e650 20 20 49 6e 20 65 76 65 72 79 20 63 61 73 65 20 In every case
2e660 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
2e670 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a nt is a pointer.
2e680 2a 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 70 61 ** to the [prepa
2e690 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 red statement] t
2e6a0 68 61 74 20 69 73 20 62 65 69 6e 67 20 65 76 61 hat is being eva
2e6b0 6c 75 61 74 65 64 20 28 74 68 65 20 5b 73 71 6c luated (the [sql
2e6c0 69 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a 20 74 ite3_stmt*].** t
2e6d0 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 hat was returned
2e6e0 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70 from [sqlite3_p
2e6f0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
2e700 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72 69 61 one of its varia
2e710 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 nts).** and the
2e720 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
2e730 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 is the index of
2e740 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 the column for w
2e750 68 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e hich information
2e760 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65 .** should be re
2e770 74 75 72 6e 65 64 2e 20 20 54 68 65 20 6c 65 66 turned. The lef
2e780 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 tmost column of
2e790 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 68 the result set h
2e7a0 61 73 20 74 68 65 20 69 6e 64 65 78 20 30 2e 0a as the index 0..
2e7b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c **.** If the SQL
2e7c0 20 73 74 61 74 65 6d 65 6e 74 20 64 6f 65 73 20 statement does
2e7d0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f not currently po
2e7e0 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 72 int to a valid r
2e7f0 6f 77 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a ow, or if the.**
2e800 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 73 column index is
2e810 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 out of range, t
2e820 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64 he result is und
2e830 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 73 65 efined..** These
2e840 20 72 6f 75 74 69 6e 65 73 20 6d 61 79 20 6f 6e routines may on
2e850 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 ly be called whe
2e860 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e n the most recen
2e870 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 t call to.** [sq
2e880 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61 lite3_step()] ha
2e890 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 s returned [SQLI
2e8a0 54 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 69 74 TE_ROW] and neit
2e8b0 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f her.** [sqlite3_
2e8c0 72 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b 73 71 reset()] nor [sq
2e8d0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
2e8e0 5d 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c ] have been call
2e8f0 65 64 20 73 75 62 73 65 71 75 65 6e 74 6c 79 2e ed subsequently.
2e900 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 .** If any of th
2e910 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
2e920 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 5b 73 called after [s
2e930 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
2e940 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 or.** [sqlite3_f
2e950 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 inalize()] or af
2e960 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 ter [sqlite3_ste
2e970 70 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65 p()] has returne
2e980 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f d.** something o
2e990 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 ther than [SQLIT
2e9a0 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65 73 75 E_ROW], the resu
2e9b0 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 lts are undefine
2e9c0 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 d..** If [sqlite
2e9d0 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 3_step()] or [sq
2e9e0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f lite3_reset()] o
2e9f0 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c r [sqlite3_final
2ea00 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20 63 61 ize()].** are ca
2ea10 6c 6c 65 64 20 66 72 6f 6d 20 61 20 64 69 66 66 lled from a diff
2ea20 65 72 65 6e 74 20 74 68 72 65 61 64 20 77 68 69 erent thread whi
2ea30 6c 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 le any of these
2ea40 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 routines.** are
2ea50 70 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 pending, then th
2ea60 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e e results are un
2ea70 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 defined..**.** T
2ea80 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d he sqlite3_colum
2ea90 6e 5f 74 79 70 65 28 29 20 72 6f 75 74 69 6e 65 n_type() routine
2eaa0 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 returns the.**
2eab0 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 [SQLITE_INTEGER
2eac0 7c 20 64 61 74 61 74 79 70 65 20 63 6f 64 65 5d | datatype code]
2ead0 20 66 6f 72 20 74 68 65 20 69 6e 69 74 69 61 6c for the initial
2eae0 20 64 61 74 61 20 74 79 70 65 0a 2a 2a 20 6f 66 data type.** of
2eaf0 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 the result colu
2eb00 6d 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 mn. The returne
2eb10 64 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f d value is one o
2eb20 66 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 f [SQLITE_INTEGE
2eb30 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 R],.** [SQLITE_F
2eb40 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 LOAT], [SQLITE_T
2eb50 45 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 4c EXT], [SQLITE_BL
2eb60 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f OB], or [SQLITE_
2eb70 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61 6c 75 NULL]. The valu
2eb80 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79 e.** returned by
2eb90 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
2eba0 74 79 70 65 28 29 20 69 73 20 6f 6e 6c 79 20 6d type() is only m
2ebb0 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e 6f 20 eaningful if no
2ebc0 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 type.** conversi
2ebd0 6f 6e 73 20 68 61 76 65 20 6f 63 63 75 72 72 65 ons have occurre
2ebe0 64 20 61 73 20 64 65 73 63 72 69 62 65 64 20 62 d as described b
2ebf0 65 6c 6f 77 2e 20 20 41 66 74 65 72 20 61 20 74 elow. After a t
2ec00 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a ype conversion,.
2ec10 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 ** the value ret
2ec20 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
2ec30 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 _column_type() i
2ec40 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 46 75 s undefined. Fu
2ec50 74 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 ture.** versions
2ec60 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20 63 of SQLite may c
2ec70 68 61 6e 67 65 20 74 68 65 20 62 65 68 61 76 69 hange the behavi
2ec80 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 63 6f or of sqlite3_co
2ec90 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a 20 66 lumn_type().** f
2eca0 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 65 20 ollowing a type
2ecb0 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a conversion..**.*
2ecc0 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 * If the result
2ecd0 69 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 54 46 is a BLOB or UTF
2ece0 2d 38 20 73 74 72 69 6e 67 20 74 68 65 6e 20 74 -8 string then t
2ecf0 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d he sqlite3_colum
2ed00 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72 6f 75 n_bytes().** rou
2ed10 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
2ed20 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
2ed30 20 69 6e 20 74 68 61 74 20 42 4c 4f 42 20 6f 72 in that BLOB or
2ed40 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66 20 74 string..** If t
2ed50 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 55 he result is a U
2ed60 54 46 2d 31 36 20 73 74 72 69 6e 67 2c 20 74 68 TF-16 string, th
2ed70 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d en sqlite3_colum
2ed80 6e 5f 62 79 74 65 73 28 29 20 63 6f 6e 76 65 72 n_bytes() conver
2ed90 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 ts.** the string
2eda0 20 74 6f 20 55 54 46 2d 38 20 61 6e 64 20 74 68 to UTF-8 and th
2edb0 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e en returns the n
2edc0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 2e 0a umber of bytes..
2edd0 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 ** If the result
2ede0 20 69 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61 is a numeric va
2edf0 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 74 65 33 lue then sqlite3
2ee00 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 _column_bytes()
2ee10 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 uses.** [sqlite3
2ee20 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 _snprintf()] to
2ee30 63 6f 6e 76 65 72 74 20 74 68 61 74 20 76 61 6c convert that val
2ee40 75 65 20 74 6f 20 61 20 55 54 46 2d 38 20 73 74 ue to a UTF-8 st
2ee50 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 ring and returns
2ee60 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
2ee70 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 f bytes in that
2ee80 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 76 string..** The v
2ee90 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 64 6f alue returned do
2eea0 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 es not include t
2eeb0 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 he zero terminat
2eec0 6f 72 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a or at the end.**
2eed0 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20 of the string.
2eee0 20 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74 68 For clarity: th
2eef0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
2ef00 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
2ef10 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 f.** bytes in th
2ef20 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74 68 e string, not th
2ef30 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 e number of char
2ef40 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 acters..**.** St
2ef50 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 rings returned b
2ef60 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e y sqlite3_column
2ef70 5f 74 65 78 74 28 29 20 61 6e 64 20 73 71 6c 69 _text() and sqli
2ef80 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
2ef90 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 6d 70 6(),.** even emp
2efa0 74 79 20 73 74 72 69 6e 67 73 2c 20 61 72 65 20 ty strings, are
2efb0 61 6c 77 61 79 73 20 7a 65 72 6f 20 74 65 72 6d always zero term
2efc0 69 6e 61 74 65 64 2e 20 20 54 68 65 20 72 65 74 inated. The ret
2efd0 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66 72 6f urn.** value fro
2efe0 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e m sqlite3_column
2eff0 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61 20 7a 65 _blob() for a ze
2f000 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 20 69 ro-length BLOB i
2f010 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 0a 2a s an arbitrary.*
2f020 2a 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73 73 69 * pointer, possi
2f030 62 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c 4c 20 bly even a NULL
2f040 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 pointer..**.** T
2f050 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d he sqlite3_colum
2f060 6e 5f 62 79 74 65 73 31 36 28 29 20 72 6f 75 74 n_bytes16() rout
2f070 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 ine is similar t
2f080 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e o sqlite3_column
2f090 5f 62 79 74 65 73 28 29 0a 2a 2a 20 62 75 74 20 _bytes().** but
2f0a0 6c 65 61 76 65 73 20 74 68 65 20 72 65 73 75 6c leaves the resul
2f0b0 74 20 69 6e 20 55 54 46 2d 31 36 20 69 6e 20 6e t in UTF-16 in n
2f0c0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
2f0d0 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d instead of UTF-
2f0e0 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 20 74 8..** The zero t
2f0f0 65 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e 6f 74 erminator is not
2f100 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 69 included in thi
2f110 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 s count..**.** T
2f120 68 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e he object return
2f130 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 ed by [sqlite3_c
2f140 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 olumn_value()] i
2f150 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 s an.** [unprote
2f160 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
2f170 75 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41 6e 20 ue] object. An
2f180 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 unprotected sqli
2f190 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
2f1a0 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 .** may only be
2f1b0 75 73 65 64 20 77 69 74 68 20 5b 73 71 6c 69 74 used with [sqlit
2f1c0 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d e3_bind_value()]
2f1d0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 and [sqlite3_re
2f1e0 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a sult_value()]..*
2f1f0 2a 20 49 66 20 74 68 65 20 5b 75 6e 70 72 6f 74 * If the [unprot
2f200 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
2f210 6c 75 65 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 lue] object retu
2f220 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 rned by.** [sqli
2f230 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 te3_column_value
2f240 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e 20 61 ()] is used in a
2f250 6e 79 20 6f 74 68 65 72 20 77 61 79 2c 20 69 6e ny other way, in
2f260 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a cluding calls.**
2f270 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b to routines lik
2f280 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e [sqlite3_value
2f290 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 _int()], [sqlite
2f2a0 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 3_value_text()],
2f2b0 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f .** or [sqlite3_
2f2c0 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 value_bytes()],
2f2d0 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f then the behavio
2f2e0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a r is undefined..
2f2f0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
2f300 69 6e 65 73 20 61 74 74 65 6d 70 74 20 74 6f 20 ines attempt to
2f310 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c 75 convert the valu
2f320 65 20 77 68 65 72 65 20 61 70 70 72 6f 70 72 69 e where appropri
2f330 61 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 ate. For.** exa
2f340 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 69 6e 74 mple, if the int
2f350 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
2f360 74 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e tion is FLOAT an
2f370 64 20 61 20 74 65 78 74 20 72 65 73 75 6c 74 0a d a text result.
2f380 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c ** is requested,
2f390 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e [sqlite3_snprin
2f3a0 74 66 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e tf()] is used in
2f3b0 74 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65 72 66 ternally to perf
2f3c0 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 orm the.** conve
2f3d0 72 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69 63 61 rsion automatica
2f3e0 6c 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 lly. The follow
2f3f0 69 6e 67 20 74 61 62 6c 65 20 64 65 74 61 69 6c ing table detail
2f400 73 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e s the conversion
2f410 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 61 70 s.** that are ap
2f420 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c plied:.**.** <bl
2f430 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 ockquote>.** <ta
2f440 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a ble border="1">.
2f450 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e 74 65 ** <tr><th> Inte
2f460 72 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c 74 68 rnal<br>Type <th
2f470 3e 20 52 65 71 75 65 73 74 65 64 3c 62 72 3e 54 > Requested<br>T
2f480 79 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 ype <th> Conver
2f490 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c sion.**.** <tr><
2f4a0 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 td> NULL <td
2f4b0 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e > INTEGER <td>
2f4c0 20 52 65 73 75 6c 74 20 69 73 20 30 0a 2a 2a 20 Result is 0.**
2f4d0 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 <tr><td> NULL
2f4e0 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 <td> FLOAT
2f4f0 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 <td> Result is
2f500 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 0.0.** <tr><td>
2f510 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 NULL <td>
2f520 54 45 58 54 20 20 20 20 3c 74 64 3e 20 52 65 73 TEXT <td> Res
2f530 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e ult is NULL poin
2f540 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 ter.** <tr><td>
2f550 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 NULL <td>
2f560 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 65 73 BLOB <td> Res
2f570 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e ult is NULL poin
2f580 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 ter.** <tr><td>
2f590 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46 INTEGER <td> F
2f5a0 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e LOAT <td> Con
2f5b0 76 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65 vert from intege
2f5c0 72 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 r to float.** <t
2f5d0 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 r><td> INTEGER
2f5e0 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c <td> TEXT <
2f5f0 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 td> ASCII render
2f600 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 ing of the integ
2f610 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 er.** <tr><td> I
2f620 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42 NTEGER <td> B
2f630 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 LOB <td> Same
2f640 20 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 45 58 as INTEGER->TEX
2f650 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 T.** <tr><td> F
2f660 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54 45 LOAT <td> INTE
2f670 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 GER <td> Conve
2f680 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74 6f rt from float to
2f690 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e integer.** <tr>
2f6a0 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 <td> FLOAT <t
2f6b0 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 d> TEXT <td
2f6c0 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e > ASCII renderin
2f6d0 67 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 0a 2a g of the float.*
2f6e0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 * <tr><td> FLOA
2f6f0 54 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 T <td> BLOB
2f700 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 <td> Same as
2f710 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c FLOAT->TEXT.** <
2f720 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 tr><td> TEXT
2f730 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 <td> INTEGER
2f740 3c 74 64 3e 20 55 73 65 20 61 74 6f 69 28 29 0a <td> Use atoi().
2f750 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 ** <tr><td> TEX
2f760 54 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 T <td> FLOAT
2f770 20 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f <td> Use ato
2f780 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 f().** <tr><td>
2f790 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 20 TEXT <td>
2f7a0 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e 6f 20 BLOB <td> No
2f7b0 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 change.** <tr><t
2f7c0 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e d> BLOB <td>
2f7d0 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 INTEGER <td>
2f7e0 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 Convert to TEXT
2f7f0 74 68 65 6e 20 75 73 65 20 61 74 6f 69 28 29 0a then use atoi().
2f800 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f ** <tr><td> BLO
2f810 42 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 B <td> FLOAT
2f820 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 <td> Convert
2f830 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 to TEXT then us
2f840 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e e atof().** <tr>
2f850 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 <td> BLOB <t
2f860 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 d> TEXT <td
2f870 3e 20 41 64 64 20 61 20 7a 65 72 6f 20 74 65 72 > Add a zero ter
2f880 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 64 65 minator if neede
2f890 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a d.** </table>.**
2f8a0 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a </blockquote>.*
2f8b0 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61 *.** The table a
2f8c0 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65 72 bove makes refer
2f8d0 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72 64 ence to standard
2f8e0 20 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 C library funct
2f8f0 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a 20 61 ions atoi().** a
2f900 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c 69 nd atof(). SQLi
2f910 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c te does not real
2f920 6c 79 20 75 73 65 20 74 68 65 73 65 20 66 75 6e ly use these fun
2f930 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73 20 ctions. It has
2f940 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 69 76 its.** own equiv
2f950 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20 72 alent internal r
2f960 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61 74 outines. The at
2f970 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29 20 oi() and atof()
2f980 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 names are.** use
2f990 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 66 d in the table f
2f9a0 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64 20 62 or brevity and b
2f9b0 65 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20 ecause they are
2f9c0 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f 73 74 familiar to most
2f9d0 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d 65 72 .** C programmer
2f9e0 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 s..**.** Note th
2f9f0 61 74 20 77 68 65 6e 20 74 79 70 65 20 63 6f 6e at when type con
2fa00 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 2c 20 versions occur,
2fa10 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 pointers returne
2fa20 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 63 61 d by prior.** ca
2fa30 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 lls to sqlite3_c
2fa40 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 73 71 olumn_blob(), sq
2fa50 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
2fa60 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 t(), and/or.** s
2fa70 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
2fa80 78 74 31 36 28 29 20 6d 61 79 20 62 65 20 69 6e xt16() may be in
2fa90 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 54 79 validated..** Ty
2faa0 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 pe conversions a
2fab0 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 61 6c nd pointer inval
2fac0 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 6f idations might o
2fad0 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 66 ccur.** in the f
2fae0 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 3a 0a ollowing cases:.
2faf0 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c **.** <ul>.** <l
2fb00 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 i> The initial c
2fb10 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 4c 4f 42 ontent is a BLOB
2fb20 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c and sqlite3_col
2fb30 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 2a 2a umn_text() or.**
2fb40 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f sqlite3_co
2fb50 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 lumn_text16() is
2fb60 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 72 6f called. A zero
2fb70 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 67 68 -terminator migh
2fb80 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 20 74 t.** need t
2fb90 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 o be added to th
2fba0 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a e string.</li>.*
2fbb0 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 * <li> The initi
2fbc0 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 al content is UT
2fbd0 46 2d 38 20 74 65 78 74 20 61 6e 64 20 73 71 6c F-8 text and sql
2fbe0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
2fbf0 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 s16() or.**
2fc00 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
2fc10 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c text16() is call
2fc20 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 ed. The content
2fc30 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 must be convert
2fc40 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 ed.** to UT
2fc50 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c F-16.</li>.** <l
2fc60 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 i> The initial c
2fc70 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 31 36 ontent is UTF-16
2fc80 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 text and sqlite
2fc90 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3_column_bytes()
2fca0 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 or.** sqli
2fcb0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 te3_column_text(
2fcc0 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 ) is called. Th
2fcd0 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 e content must b
2fce0 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20 e converted.**
2fcf0 20 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c 2f 6c to UTF-8.</l
2fd00 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a i>.** </ul>.**.*
2fd10 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65 * Conversions be
2fd20 74 77 65 65 6e 20 55 54 46 2d 31 36 62 65 20 61 tween UTF-16be a
2fd30 6e 64 20 55 54 46 2d 31 36 6c 65 20 61 72 65 20 nd UTF-16le are
2fd40 61 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e 20 70 always done in p
2fd50 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a 20 6e lace and do.** n
2fd60 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 61 20 ot invalidate a
2fd70 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c 20 74 prior pointer, t
2fd80 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 hough of course
2fd90 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 the content of t
2fda0 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 74 68 61 he buffer.** tha
2fdb0 74 20 74 68 65 20 70 72 69 6f 72 20 70 6f 69 6e t the prior poin
2fdc0 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 77 69 ter points to wi
2fdd0 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 ll have been mod
2fde0 69 66 69 65 64 2e 20 20 4f 74 68 65 72 20 6b 69 ified. Other ki
2fdf0 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76 65 72 nds.** of conver
2fe00 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20 69 6e sion are done in
2fe10 20 70 6c 61 63 65 20 77 68 65 6e 20 69 74 20 69 place when it i
2fe20 73 20 70 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 s possible, but
2fe30 73 6f 6d 65 74 69 6d 65 73 20 74 68 65 79 0a 2a sometimes they.*
2fe40 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 * are not possib
2fe50 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f 73 65 20 le and in those
2fe60 63 61 73 65 73 20 70 72 69 6f 72 20 70 6f 69 6e cases prior poin
2fe70 74 65 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64 ters are invalid
2fe80 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ated..**.** The
2fe90 73 61 66 65 73 74 20 61 6e 64 20 65 61 73 69 65 safest and easie
2fea0 73 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 70 st to remember p
2feb0 6f 6c 69 63 79 20 69 73 20 74 6f 20 69 6e 76 6f olicy is to invo
2fec0 6b 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 ke these routine
2fed0 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 20 74 s.** in one of t
2fee0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 61 79 he following way
2fef0 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a s:.**.** <ul>.**
2ff00 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f <li>sqlite3_co
2ff10 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f 6c 6c lumn_text() foll
2ff20 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f owed by sqlite3_
2ff30 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f column_bytes()</
2ff40 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 li>.** <li>sqli
2ff50 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
2ff60 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 ) followed by sq
2ff70 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
2ff80 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c es()</li>.** <l
2ff90 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e i>sqlite3_column
2ffa0 5f 74 65 78 74 31 36 28 29 20 66 6f 6c 6c 6f 77 _text16() follow
2ffb0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f ed by sqlite3_co
2ffc0 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 3c 2f lumn_bytes16()</
2ffd0 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a li>.** </ul>.**.
2ffe0 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 ** In other word
2fff0 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20 63 61 s, you should ca
30000 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ll sqlite3_colum
30010 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73 71 6c n_text(),.** sql
30020 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 ite3_column_blob
30030 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 (), or sqlite3_c
30040 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 olumn_text16() f
30050 69 72 73 74 20 74 6f 20 66 6f 72 63 65 20 74 68 irst to force th
30060 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 74 6f e result.** into
30070 20 74 68 65 20 64 65 73 69 72 65 64 20 66 6f 72 the desired for
30080 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 mat, then invoke
30090 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
300a0 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 73 71 bytes() or.** sq
300b0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
300c0 65 73 31 36 28 29 20 74 6f 20 66 69 6e 64 20 74 es16() to find t
300d0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 72 he size of the r
300e0 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74 20 6d esult. Do not m
300f0 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 ix calls.** to s
30100 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
30110 78 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f xt() or sqlite3_
30120 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 77 69 column_blob() wi
30130 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 th calls to.** s
30140 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
30150 74 65 73 31 36 28 29 2c 20 61 6e 64 20 64 6f 20 tes16(), and do
30160 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20 74 6f not mix calls to
30170 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
30180 74 65 78 74 31 36 28 29 0a 2a 2a 20 77 69 74 68 text16().** with
30190 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
301a0 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3_column_bytes()
301b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e ..**.** The poin
301c0 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 61 72 ters returned ar
301d0 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 61 20 e valid until a
301e0 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 type conversion
301f0 6f 63 63 75 72 73 20 61 73 0a 2a 2a 20 64 65 73 occurs as.** des
30200 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20 6f 72 cribed above, or
30210 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f until [sqlite3_
30220 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 step()] or [sqli
30230 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a te3_reset()] or.
30240 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 ** [sqlite3_fina
30250 6c 69 7a 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 lize()] is calle
30260 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 73 d. The memory s
30270 70 61 63 65 20 75 73 65 64 20 74 6f 20 68 6f 6c pace used to hol
30280 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61 6e 64 d strings.** and
30290 20 42 4c 4f 42 73 20 69 73 20 66 72 65 65 64 20 BLOBs is freed
302a0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 automatically.
302b0 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61 Do <b>not</b> pa
302c0 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20 ss the pointers
302d0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c returned.** [sql
302e0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 ite3_column_blob
302f0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 6f ()], [sqlite3_co
30300 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 65 74 lumn_text()], et
30310 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71 6c 69 c. into.** [sqli
30320 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a te3_free()]..**.
30330 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 ** If a memory a
30340 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 llocation error
30350 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 occurs during th
30360 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 e evaluation of
30370 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 any.** of these
30380 72 6f 75 74 69 6e 65 73 2c 20 61 20 64 65 66 61 routines, a defa
30390 75 6c 74 20 76 61 6c 75 65 20 69 73 20 72 65 74 ult value is ret
303a0 75 72 6e 65 64 2e 20 20 54 68 65 20 64 65 66 61 urned. The defa
303b0 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 ult value.** is
303c0 65 69 74 68 65 72 20 74 68 65 20 69 6e 74 65 67 either the integ
303d0 65 72 20 30 2c 20 74 68 65 20 66 6c 6f 61 74 69 er 0, the floati
303e0 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 20 ng point number
303f0 30 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c 0a 2a 0.0, or a NULL.*
30400 2a 20 70 6f 69 6e 74 65 72 2e 20 20 53 75 62 73 * pointer. Subs
30410 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 equent calls to
30420 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 [sqlite3_errcode
30430 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a ()] will return.
30440 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d ** [SQLITE_NOMEM
30450 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 ]..**.** INVARIA
30460 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 NTS:.**.** {H138
30470 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 03} The [sqlite3
30480 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e _column_blob(S,N
30490 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e )] interface con
304a0 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 verts the.**
304b0 20 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e Nth column
304c0 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 in the current
304d0 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c row of the resul
304e0 74 20 73 65 74 20 66 6f 72 0a 2a 2a 20 20 20 20 t set for.**
304f0 20 20 20 20 20 20 74 68 65 20 5b 70 72 65 70 61 the [prepa
30500 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 red statement] S
30510 20 69 6e 74 6f 20 61 20 42 4c 4f 42 20 61 6e 64 into a BLOB and
30520 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 61 0a then returns a.
30530 2a 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e ** poin
30540 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 ter to the conve
30550 72 74 65 64 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a rted value..**.*
30560 2a 20 7b 48 31 33 38 30 36 7d 20 54 68 65 20 5b * {H13806} The [
30570 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
30580 79 74 65 73 28 53 2c 4e 29 5d 20 69 6e 74 65 72 ytes(S,N)] inter
30590 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 face returns the
305a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 6d .** num
305b0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
305c0 74 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 the BLOB or stri
305d0 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 ng (exclusive of
305e0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
305f0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
30600 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29 20 on the string)
30610 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 that was returne
30620 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 d by the.**
30630 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e 74 most recent
30640 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
30650 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 3_column_blob(S,
30660 4e 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 N)] or.**
30670 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 [sqlite3_colu
30680 6d 6e 5f 74 65 78 74 28 53 2c 4e 29 5d 2e 0a 2a mn_text(S,N)]..*
30690 2a 0a 2a 2a 20 7b 48 31 33 38 30 39 7d 20 54 68 *.** {H13809} Th
306a0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d e [sqlite3_colum
306b0 6e 5f 62 79 74 65 73 31 36 28 53 2c 4e 29 5d 20 n_bytes16(S,N)]
306c0 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
306d0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
306e0 20 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 number of byte
306f0 73 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 20 s in the string
30700 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 (exclusive of th
30710 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65 e.** ze
30720 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 6f 6e ro terminator on
30730 20 74 68 65 20 73 74 72 69 6e 67 29 20 74 68 61 the string) tha
30740 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 62 t was returned b
30750 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 y the.**
30760 20 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 most recent ca
30770 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
30780 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 53 2c 4e olumn_text16(S,N
30790 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 31 )]..**.** {H1381
307a0 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 2} The [sqlite3_
307b0 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 53 2c column_double(S,
307c0 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f N)] interface co
307d0 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 nverts the.**
307e0 20 20 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d Nth colum
307f0 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 n in the current
30800 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 row of the resu
30810 6c 74 20 73 65 74 20 66 6f 72 20 74 68 65 0a 2a lt set for the.*
30820 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 * [prep
30830 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
30840 53 20 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e S into a floatin
30850 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 61 6e g point value an
30860 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 d.** re
30870 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 turns a copy of
30880 74 68 61 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a that value..**.*
30890 2a 20 7b 48 31 33 38 31 35 7d 20 54 68 65 20 5b * {H13815} The [
308a0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 sqlite3_column_i
308b0 6e 74 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 nt(S,N)] interfa
308c0 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a ce converts the.
308d0 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 ** Nth
308e0 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 column in the cu
308f0 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 rrent row of the
30900 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 result set for
30910 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
30920 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
30930 65 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 36 34 ent] S into a 64
30940 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
30950 67 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 ger and.**
30960 20 20 20 20 72 65 74 75 72 6e 73 20 74 68 65 20 returns the
30970 6c 6f 77 65 72 20 33 32 20 62 69 74 73 20 6f 66 lower 32 bits of
30980 20 74 68 61 74 20 69 6e 74 65 67 65 72 2e 0a 2a that integer..*
30990 2a 0a 2a 2a 20 7b 48 31 33 38 31 38 7d 20 54 68 *.** {H13818} Th
309a0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d e [sqlite3_colum
309b0 6e 5f 69 6e 74 36 34 28 53 2c 4e 29 5d 20 69 6e n_int64(S,N)] in
309c0 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73 terface converts
309d0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
309e0 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 Nth column in t
309f0 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f he current row o
30a00 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
30a10 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 for the.**
30a20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 [prepared s
30a30 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 74 6f tatement] S into
30a40 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 a 64-bit signed
30a50 20 69 6e 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 integer and.**
30a60 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 returns
30a70 20 61 20 63 6f 70 79 20 6f 66 20 74 68 61 74 20 a copy of that
30a80 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b integer..**.** {
30a90 48 31 33 38 32 31 7d 20 54 68 65 20 5b 73 71 6c H13821} The [sql
30aa0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
30ab0 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 (S,N)] interface
30ac0 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a converts the.**
30ad0 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f Nth co
30ae0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 lumn in the curr
30af0 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 ent row of the r
30b00 65 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a esult set for.**
30b10 20 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 70 the [p
30b20 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
30b30 74 5d 20 53 20 69 6e 74 6f 20 61 20 7a 65 72 6f t] S into a zero
30b40 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d -terminated UTF-
30b50 38 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 8.** st
30b60 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 ring and returns
30b70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
30b80 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a at string..**.**
30b90 20 7b 48 31 33 38 32 34 7d 20 54 68 65 20 5b 73 {H13824} The [s
30ba0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
30bb0 78 74 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 xt16(S,N)] inter
30bc0 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 face converts th
30bd0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 74 e.** Nt
30be0 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 h column in the
30bf0 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 current row of t
30c00 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f he result set fo
30c10 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 r the.**
30c20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 [prepared stat
30c30 65 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 ement] S into a
30c40 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
30c50 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20 2-byte.**
30c60 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31 aligned UTF-1
30c70 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 6 native byte or
30c80 64 65 72 20 73 74 72 69 6e 67 20 61 6e 64 20 72 der string and r
30c90 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 eturns.**
30ca0 20 20 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 a pointer to
30cb0 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a that string..**.
30cc0 2a 2a 20 7b 48 31 33 38 32 37 7d 20 54 68 65 20 ** {H13827} The
30cd0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
30ce0 74 79 70 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 type(S,N)] inter
30cf0 66 61 63 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 face returns.**
30d00 20 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 one of
30d10 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2c 20 5b [SQLITE_NULL], [
30d20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c SQLITE_INTEGER],
30d30 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c [SQLITE_FLOAT],
30d40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 .** [SQ
30d50 4c 49 54 45 5f 54 45 58 54 5d 2c 20 6f 72 20 5b LITE_TEXT], or [
30d60 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 20 61 73 20 SQLITE_BLOB] as
30d70 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a appropriate for.
30d80 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
30d90 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 Nth column in th
30da0 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 e current row of
30db0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
30dc0 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 for.**
30dd0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 the [prepared st
30de0 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a atement] S..**.*
30df0 2a 20 7b 48 31 33 38 33 30 7d 20 54 68 65 20 5b * {H13830} The [
30e00 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 sqlite3_column_v
30e10 61 6c 75 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 alue(S,N)] inter
30e20 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 0a 2a face returns a.*
30e30 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74 * point
30e40 65 72 20 74 6f 20 61 6e 20 5b 75 6e 70 72 6f 74 er to an [unprot
30e50 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
30e60 6c 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f 72 20 lue] object for
30e70 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
30e80 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 Nth column in th
30e90 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 e current row of
30ea0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
30eb0 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 for.**
30ec0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 the [prepared st
30ed0 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 atement] S..*/.S
30ee0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
30ef0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
30f00 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 lumn_blob(sqlite
30f10 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
30f20 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 l);.SQLITE_API i
30f30 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nt sqlite3_colum
30f40 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f n_bytes(sqlite3_
30f50 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
30f60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
30f70 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
30f80 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f bytes16(sqlite3_
30f90 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
30fa0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 ;.SQLITE_API dou
30fb0 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ble sqlite3_colu
30fc0 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 mn_double(sqlite
30fd0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
30fe0 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 l);.SQLITE_API i
30ff0 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nt sqlite3_colum
31000 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 n_int(sqlite3_st
31010 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a mt*, int iCol);.
31020 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
31030 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e3_int64 sqlite3
31040 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 _column_int64(sq
31050 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
31060 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 iCol);.SQLITE_A
31070 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 PI const unsigne
31080 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f d char *sqlite3_
31090 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 column_text(sqli
310a0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 te3_stmt*, int i
310b0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 Col);.SQLITE_API
310c0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
310d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
310e0 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 16(sqlite3_stmt*
310f0 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c , int iCol);.SQL
31100 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
31110 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 te3_column_type(
31120 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
31130 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 nt iCol);.SQLITE
31140 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c _API sqlite3_val
31150 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ue *sqlite3_colu
31160 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 mn_value(sqlite3
31170 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
31180 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
31190 45 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 EF: Destroy A Pr
311a0 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 epared Statement
311b0 20 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d Object {H13300}
311c0 20 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 <S70300><S30100
311d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
311e0 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 te3_finalize() f
311f0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
31200 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 d to delete a [p
31210 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
31220 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 t]..** If the st
31230 61 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 atement was exec
31240 75 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c uted successfull
31250 79 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 y or not execute
31260 64 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a d at all, then.*
31270 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
31280 65 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 eturned. If exec
31290 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 ution of the sta
312a0 74 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 tement failed th
312b0 65 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 en an.** [error
312c0 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 code] or [extend
312d0 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 ed error code] i
312e0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
312f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
31300 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 an be called at
31310 61 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 any point during
31320 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f the execution o
31330 66 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 f the.** [prepar
31340 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 ed statement].
31350 49 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d If the virtual m
31360 61 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a achine has not.*
31370 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 * completed exec
31380 75 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 ution when this
31390 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
313a0 64 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a d, that is like.
313b0 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 ** encountering
313c0 61 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b an error or an [
313d0 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
313e0 74 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a t | interrupt]..
313f0 2a 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 ** Incomplete up
31400 64 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c dates may be rol
31410 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 led back and tra
31420 6e 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c nsactions cancel
31430 65 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 ed,.** depending
31440 20 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 on the circumst
31450 61 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a ances, and the.*
31460 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 * [error code] r
31470 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 eturned will be
31480 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a [SQLITE_ABORT]..
31490 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
314a0 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 32 7d :.**.** {H11302}
314b0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 The [sqlite3_fi
314c0 6e 61 6c 69 7a 65 28 53 29 5d 20 69 6e 74 65 72 nalize(S)] inter
314d0 66 61 63 65 20 64 65 73 74 72 6f 79 73 20 74 68 face destroys th
314e0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 e.** [p
314f0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
31500 74 5d 20 53 20 61 6e 64 20 72 65 6c 65 61 73 65 t] S and release
31510 73 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 s all.**
31520 20 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69 6c memory and fil
31530 65 20 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64 e resources held
31540 20 62 79 20 74 68 61 74 20 6f 62 6a 65 63 74 2e by that object.
31550 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 34 7d 20 .**.** {H11304}
31560 49 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 If the most rece
31570 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 nt call to [sqli
31580 74 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 te3_step(S)] for
31590 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
315a0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
315b0 6d 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 ment] S returned
315c0 20 61 6e 20 65 72 72 6f 72 2c 0a 2a 2a 20 20 20 an error,.**
315d0 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c then [sql
315e0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29 ite3_finalize(S)
315f0 5d 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 73 ] returns that s
31600 61 6d 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 ame error..*/.SQ
31610 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
31620 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 ite3_finalize(sq
31630 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
31640 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
31650 52 45 46 3a 20 52 65 73 65 74 20 41 20 50 72 65 REF: Reset A Pre
31660 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 pared Statement
31670 4f 62 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20 Object {H13330}
31680 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S70300>.**.** T
31690 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 he sqlite3_reset
316a0 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 () function is c
316b0 61 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61 alled to reset a
316c0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
316d0 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 ment].** object
316e0 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 back to its init
316f0 69 61 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79 ial state, ready
31700 20 74 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74 to be re-execut
31710 65 64 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 ed..** Any SQL s
31720 74 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c tatement variabl
31730 65 73 20 74 68 61 74 20 68 61 64 20 76 61 6c 75 es that had valu
31740 65 73 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d es bound to them
31750 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 using.** the [s
31760 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
31770 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f | sqlite3_bind_
31780 2a 28 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20 *() API] retain
31790 74 68 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a their values..**
317a0 20 55 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c Use [sqlite3_cl
317b0 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 ear_bindings()]
317c0 74 6f 20 72 65 73 65 74 20 74 68 65 20 62 69 6e to reset the bin
317d0 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 dings..**.** {H1
317e0 31 33 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 1332} The [sqlit
317f0 65 33 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74 e3_reset(S)] int
31800 65 72 66 61 63 65 20 72 65 73 65 74 73 20 74 68 erface resets th
31810 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
31820 65 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 ement] S.**
31830 20 20 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65 back to the
31840 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 beginning of it
31850 73 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a s program..**.**
31860 20 7b 48 31 31 33 33 34 7d 20 49 66 20 74 68 65 {H11334} If the
31870 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c most recent cal
31880 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 l to [sqlite3_st
31890 65 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a ep(S)] for the.*
318a0 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 * [prep
318b0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
318c0 53 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 S returned [SQLI
318d0 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 TE_ROW] or [SQLI
318e0 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 TE_DONE],.**
318f0 20 20 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c or if [sql
31900 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 ite3_step(S)] ha
31910 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 s never before b
31920 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c een called on S,
31930 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
31940 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 n [sqlite3_reset
31950 28 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 (S)] returns [SQ
31960 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 LITE_OK]..**.**
31970 7b 48 31 31 33 33 36 7d 20 49 66 20 74 68 65 20 {H11336} If the
31980 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c most recent call
31990 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 to [sqlite3_ste
319a0 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a p(S)] for the.**
319b0 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 [prepa
319c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 red statement] S
319d0 20 69 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 indicated an er
319e0 72 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 ror, then.**
319f0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 [sqlite3_r
31a00 65 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 eset(S)] returns
31a10 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
31a20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a [error code]..**
31a30 0a 2a 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65 .** {H11338} The
31a40 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
31a50 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f S)] interface do
31a60 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 es not change th
31a70 65 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 e values.**
31a80 20 20 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c of any [sql
31a90 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 ite3_bind_blob|b
31aa0 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 indings] on the
31ab0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
31ac0 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 ent] S..*/.SQLIT
31ad0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
31ae0 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 3_reset(sqlite3_
31af0 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f stmt *pStmt);../
31b00 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 *.** CAPI3REF: C
31b10 72 65 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e reate Or Redefin
31b20 65 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 e SQL Functions
31b30 7b 48 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30 {H16100} <S20200
31b40 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
31b50 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f function creatio
31b60 6e 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b n routines}.** K
31b70 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 EYWORDS: {applic
31b80 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 ation-defined SQ
31b90 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b L function}.** K
31ba0 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 EYWORDS: {applic
31bb0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 ation-defined SQ
31bc0 4c 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a L functions}.**.
31bd0 2a 2a 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e ** These two fun
31be0 63 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 ctions (collecti
31bf0 76 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 vely known as "f
31c00 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e unction creation
31c10 20 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 routines").** a
31c20 72 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 re used to add S
31c30 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 QL functions or
31c40 61 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f aggregates or to
31c50 20 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 redefine the be
31c60 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 havior.** of exi
31c70 73 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 sting SQL functi
31c80 6f 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 ons or aggregate
31c90 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 s. The only dif
31ca0 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 ference between
31cb0 74 68 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 the.** two is th
31cc0 61 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 at the second pa
31cd0 72 61 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d rameter, the nam
31ce0 65 20 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72 e of the (scalar
31cf0 29 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a ) function or.**
31d00 20 61 67 67 72 65 67 61 74 65 2c 20 69 73 20 65 aggregate, is e
31d10 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 ncoded in UTF-8
31d20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 for sqlite3_crea
31d30 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e te_function() an
31d40 64 20 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 d UTF-16.** for
31d50 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
31d60 75 6e 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a unction16()..**.
31d70 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 ** The first par
31d80 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 64 ameter is the [d
31d90 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
31da0 6f 6e 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65 on] to which the
31db0 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e SQL.** function
31dc0 20 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e is to be added.
31dd0 20 20 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72 If a single pr
31de0 6f 67 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 ogram uses more
31df0 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 than one databas
31e00 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 e.** connection
31e10 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e internally, then
31e20 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d SQL functions m
31e30 75 73 74 20 62 65 20 61 64 64 65 64 20 69 6e 64 ust be added ind
31e40 69 76 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 ividually to.**
31e50 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f each database co
31e60 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nnection..**.**
31e70 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d The second param
31e80 65 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 eter is the name
31e90 20 6f 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63 of the SQL func
31ea0 74 69 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74 tion to be creat
31eb0 65 64 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e ed or.** redefin
31ec0 65 64 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20 ed. The length
31ed0 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c of the name is l
31ee0 69 6d 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 imited to 255 by
31ef0 74 65 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f tes, exclusive o
31f00 66 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 f.** the zero-te
31f10 72 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 rminator. Note
31f20 74 68 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 that the name le
31f30 6e 67 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e ngth limit is in
31f40 20 62 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 bytes, not.** c
31f50 68 61 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20 haracters. Any
31f60 61 74 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74 attempt to creat
31f70 65 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 e a function wit
31f80 68 20 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a h a longer name.
31f90 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 ** will result i
31fa0 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d n [SQLITE_ERROR]
31fb0 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e being returned.
31fc0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 .**.** The third
31fd0 20 70 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67 parameter (nArg
31fe0 29 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 ).** is the numb
31ff0 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
32000 74 68 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e that the SQL fun
32010 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 ction or.** aggr
32020 65 67 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20 egate takes. If
32030 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 this parameter i
32040 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e s negative, then
32050 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
32060 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 n or.** aggregat
32070 65 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e e may take any n
32080 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
32090 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ts..**.** The fo
320a0 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20 urth parameter,
320b0 65 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66 eTextRep, specif
320c0 69 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c ies what.** [SQL
320d0 49 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 ITE_UTF8 | text
320e0 65 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 encoding] this S
320f0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 QL function pref
32100 65 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 ers for.** its p
32110 61 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 arameters. Any
32120 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 SQL function imp
32130 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 lementation shou
32140 6c 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f ld be able to wo
32150 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 rk.** work with
32160 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c UTF-8, UTF-16le,
32170 20 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 or UTF-16be. B
32180 75 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e ut some implemen
32190 74 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a tations may be.*
321a0 2a 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 * more efficient
321b0 20 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 with one encodi
321c0 6e 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e ng than another.
321d0 20 20 49 74 20 69 73 20 61 6c 6c 6f 77 65 64 20 It is allowed
321e0 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c to.** invoke sql
321f0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
32200 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 tion() or sqlite
32210 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
32220 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a n16() multiple.*
32230 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 * times with the
32240 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62 same function b
32250 75 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e ut with differen
32260 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78 t values of eTex
32270 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75 tRep..** When mu
32280 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 ltiple implement
32290 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 ations of the sa
322a0 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 me function are
322b0 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74 available, SQLit
322c0 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74 e.** will pick t
322d0 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f he one that invo
322e0 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 lves the least a
322f0 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f mount of data co
32300 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 nversion..** If
32310 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 there is only a
32320 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 single implement
32330 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 ation which does
32340 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74 not care what t
32350 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 ext.** encoding
32360 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68 is used, then th
32370 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e e fourth argumen
32380 74 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c t should be [SQL
32390 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 ITE_ANY]..**.**
323a0 54 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65 The fifth parame
323b0 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 ter is an arbitr
323c0 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 ary pointer. Th
323d0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
323e0 20 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74 of the.** funct
323f0 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63 ion can gain acc
32400 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e ess to this poin
32410 74 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ter using [sqlit
32420 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e e3_user_data()].
32430 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e .**.** The seven
32440 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e th, eighth and n
32450 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c inth parameters,
32460 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e xFunc, xStep an
32470 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a d xFinal, are.**
32480 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c pointers to C-l
32490 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e anguage function
324a0 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
324b0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
324c0 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 n or.** aggregat
324d0 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20 e. A scalar SQL
324e0 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 function require
324f0 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 s an implementat
32500 69 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63 ion of the xFunc
32510 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c .** callback onl
32520 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 y, NULL pointers
32530 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 should be passe
32540 64 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61 d as the xStep a
32550 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72 nd xFinal.** par
32560 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72 ameters. An aggr
32570 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 egate SQL functi
32580 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 on requires an i
32590 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
325a0 20 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46 xStep.** and xF
325b0 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68 inal and NULL sh
325c0 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66 ould be passed f
325d0 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c or xFunc. To del
325e0 65 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a ete an existing.
325f0 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 ** SQL function
32600 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61 or aggregate, pa
32610 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20 ss NULL for all
32620 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63 three function c
32630 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 allbacks..**.**
32640 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 It is permitted
32650 74 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74 to register mult
32660 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 iple implementat
32670 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 ions of the same
32680 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 .** functions wi
32690 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 th the same name
326a0 20 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72 but with either
326b0 20 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65 differing numbe
326c0 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e rs of.** argumen
326d0 74 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20 ts or differing
326e0 70 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 preferred text e
326f0 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74 ncodings. SQLit
32700 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68 e will use.** th
32710 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
32720 20 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 most closely ma
32730 74 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e tches the way in
32740 20 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 which the.** SQ
32750 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 L function is us
32760 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 ed. A function
32770 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 implementation w
32780 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 ith a non-negati
32790 76 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d ve.** nArg param
327a0 65 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 eter is a better
327b0 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 match than a fu
327c0 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nction implement
327d0 61 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 ation with.** a
327e0 6e 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 negative nArg.
327f0 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 A function where
32800 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 the preferred t
32810 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 ext encoding.**
32820 6d 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 matches the data
32830 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 base encoding is
32840 20 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 a better.** mat
32850 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 ch than a functi
32860 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 on where the enc
32870 6f 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 oding is differe
32880 6e 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 nt. .** A funct
32890 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e ion where the en
328a0 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 coding differenc
328b0 65 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 e is between UTF
328c0 31 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 16le and UTF16be
328d0 0a 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 .** is a closer
328e0 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e match than a fun
328f0 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 ction where the
32900 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 encoding differe
32910 6e 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 nce is.** betwee
32920 6e 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 n UTF8 and UTF16
32930 2e 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e ..**.** Built-in
32940 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 functions may b
32950 65 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 e overloaded by
32960 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d new application-
32970 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
32980 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 s..** The first
32990 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
329a0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 ned function wit
329b0 68 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f h a given name o
329c0 76 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 verrides all.**
329d0 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f built-in functio
329e0 6e 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b ns in the same [
329f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
32a00 69 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 ion] with the sa
32a10 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 me name..** Subs
32a20 65 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 equent applicati
32a30 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
32a40 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 ions of the same
32a50 20 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 name only overr
32a60 69 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 ide .** prior ap
32a70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
32a80 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 d functions that
32a90 20 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 are an exact ma
32aa0 74 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e tch for the.** n
32ab0 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 umber of paramet
32ac0 65 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 ers and preferre
32ad0 64 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a d encoding..**.*
32ae0 2a 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e * An application
32af0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
32b00 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 n is permitted t
32b10 6f 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 o call other.**
32b20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 SQLite interface
32b30 73 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 s. However, suc
32b40 68 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 h calls must not
32b50 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 .** close the da
32b60 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
32b70 6e 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f n nor finalize o
32b80 72 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 r reset the prep
32b90 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e ared.** statemen
32ba0 74 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 t in which the f
32bb0 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 unction is runni
32bc0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 ng..**.** INVARI
32bd0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ANTS:.**.** {H16
32be0 31 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 103} The [sqlite
32bf0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
32c00 6e 31 36 28 44 2c 58 2c 2e 2e 2e 29 5d 20 69 6e n16(D,X,...)] in
32c10 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 terface shall be
32c20 68 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 have.**
32c30 20 61 73 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 as [sqlite3_cre
32c40 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 ate_function(D,X
32c50 2c 2e 2e 2e 29 5d 20 69 6e 20 65 76 65 72 79 20 ,...)] in every
32c60 77 61 79 20 65 78 63 65 70 74 20 74 68 61 74 20 way except that
32c70 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 it.** i
32c80 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 58 20 nterprets the X
32c90 61 72 67 75 6d 65 6e 74 20 61 73 20 7a 65 72 6f argument as zero
32ca0 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d -terminated UTF-
32cb0 31 36 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 16.** n
32cc0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
32cd0 20 69 6e 73 74 65 61 64 20 6f 66 20 61 73 20 7a instead of as z
32ce0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 ero-terminated U
32cf0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 TF-8..**.** {H16
32d00 31 30 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 106} A successfu
32d10 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 l invocation of
32d20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
32d30 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
32d40 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 function(D,X,N,E
32d50 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 ,...)] interface
32d60 20 73 68 61 6c 6c 20 72 65 67 69 73 74 65 72 0a shall register.
32d70 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 72 ** or r
32d80 65 70 6c 61 63 65 73 20 63 61 6c 6c 62 61 63 6b eplaces callback
32d90 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 functions in th
32da0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
32db0 65 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 ection] D.**
32dc0 20 20 20 20 20 20 75 73 65 64 20 74 6f 20 69 6d used to im
32dd0 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 plement the SQL
32de0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 58 function named X
32df0 20 77 69 74 68 20 4e 20 70 61 72 61 6d 65 74 65 with N paramete
32e00 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 rs.** a
32e10 6e 64 20 68 61 76 69 6e 67 20 61 20 70 72 65 66 nd having a pref
32e20 65 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 erred text encod
32e30 69 6e 67 20 6f 66 20 45 2e 0a 2a 2a 0a 2a 2a 20 ing of E..**.**
32e40 7b 48 31 36 31 30 39 7d 20 41 20 73 75 63 63 65 {H16109} A succe
32e50 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
32e60 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
32e70 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 nction(D,X,N,E,P
32e80 2c 46 2c 53 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 ,F,S,L)].**
32e90 20 20 20 20 20 73 68 61 6c 6c 20 72 65 70 6c 61 shall repla
32ea0 63 65 20 74 68 65 20 50 2c 20 46 2c 20 53 2c 20 ce the P, F, S,
32eb0 61 6e 64 20 4c 20 76 61 6c 75 65 73 20 66 72 6f and L values fro
32ec0 6d 20 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c m any prior call
32ed0 73 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20 20 s with.**
32ee0 20 20 20 74 68 65 20 73 61 6d 65 20 44 2c 20 58 the same D, X
32ef0 2c 20 4e 2c 20 61 6e 64 20 45 20 76 61 6c 75 65 , N, and E value
32f00 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 31 32 s..**.** {H16112
32f10 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 } The [sqlite3_c
32f20 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 reate_function(D
32f30 2c 58 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 ,X,...)] interfa
32f40 63 65 20 73 68 61 6c 6c 20 66 61 69 6c 0a 2a 2a ce shall fail.**
32f50 20 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 if the
32f60 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6e 61 SQL function na
32f70 6d 65 20 58 20 69 73 0a 2a 2a 20 20 20 20 20 20 me X is.**
32f80 20 20 20 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 longer than
32f90 32 35 35 20 62 79 74 65 73 20 65 78 63 6c 75 73 255 bytes exclus
32fa0 69 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f 20 ive of the zero
32fb0 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a terminator..**.*
32fc0 2a 20 7b 48 31 36 31 31 38 7d 20 54 68 65 20 5b * {H16118} The [
32fd0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
32fe0 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c unction(D,X,N,E,
32ff0 50 2c 46 2c 53 2c 4c 29 5d 20 69 6e 74 65 72 66 P,F,S,L)] interf
33000 61 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ace.**
33010 73 68 61 6c 6c 20 66 61 69 6c 20 75 6e 6c 65 73 shall fail unles
33020 73 20 65 69 74 68 65 72 20 46 20 69 73 20 4e 55 s either F is NU
33030 4c 4c 20 61 6e 64 20 53 20 61 6e 64 20 4c 20 61 LL and S and L a
33040 72 65 20 6e 6f 6e 2d 4e 55 4c 4c 20 6f 72 0a 2a re non-NULL or.*
33050 2a 2a 20 20 20 20 20 20 20 20 20 46 20 69 73 20 ** F is
33060 6e 6f 6e 2d 4e 55 4c 4c 20 61 6e 64 20 53 20 61 non-NULL and S a
33070 6e 64 20 4c 20 61 72 65 20 4e 55 4c 4c 2e 0a 2a nd L are NULL..*
33080 2a 0a 2a 2a 20 7b 48 31 36 31 32 31 7d 20 54 68 *.** {H16121} Th
33090 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 e [sqlite3_creat
330a0 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 2e 2e 2e e_function(D,...
330b0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 )] interface sha
330c0 6c 6c 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e ll fails with an
330d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65 72 72 .** err
330e0 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 or code of [SQLI
330f0 54 45 5f 42 55 53 59 5d 20 69 66 20 74 68 65 72 TE_BUSY] if ther
33100 65 20 65 78 69 73 74 20 5b 70 72 65 70 61 72 65 e exist [prepare
33110 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a d statements].**
33120 20 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 associ
33130 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 ated with the [d
33140 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
33150 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 on] D..**.** {H1
33160 36 31 32 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 6124} The [sqlit
33170 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
33180 6f 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 20 69 on(D,X,N,...)] i
33190 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 66 nterface shall f
331a0 61 69 6c 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 ail with.**
331b0 20 20 20 20 20 61 6e 20 65 72 72 6f 72 20 63 6f an error co
331c0 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 45 52 de of [SQLITE_ER
331d0 52 4f 52 5d 20 69 66 20 70 61 72 61 6d 65 74 65 ROR] if paramete
331e0 72 20 4e 20 69 73 20 6c 65 73 73 0a 2a 2a 20 20 r N is less.**
331f0 20 20 20 20 20 20 20 20 74 68 61 6e 20 2d 31 20 than -1
33200 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 or greater than
33210 31 32 37 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 127..**.** {H161
33220 32 37 7d 20 57 68 65 6e 20 4e 20 69 73 20 6e 6f 27} When N is no
33230 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 n-negative, the
33240 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
33250 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 2e function(D,X,N,.
33260 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..)].**
33270 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c interface shall
33280 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61 register callba
33290 63 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 cks to be invoke
332a0 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 d for the.**
332b0 20 20 20 20 20 20 53 51 4c 20 66 75 6e 63 74 69 SQL functi
332c0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e on.** n
332d0 61 6d 65 64 20 58 20 77 68 65 6e 20 74 68 65 20 amed X when the
332e0 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 number of argume
332f0 6e 74 73 20 74 6f 20 74 68 65 20 53 51 4c 20 66 nts to the SQL f
33300 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 unction is.**
33310 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 4e exactly N
33320 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 30 7d ..**.** {H16130}
33330 20 57 68 65 6e 20 4e 20 69 73 20 2d 31 2c 20 74 When N is -1, t
33340 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 he [sqlite3_crea
33350 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c te_function(D,X,
33360 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 N,...)].**
33370 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 73 68 interface sh
33380 61 6c 6c 20 72 65 67 69 73 74 65 72 20 63 61 6c all register cal
33390 6c 62 61 63 6b 73 20 74 6f 20 62 65 20 69 6e 76 lbacks to be inv
333a0 6f 6b 65 64 20 66 6f 72 20 74 68 65 20 53 51 4c oked for the SQL
333b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e .** fun
333c0 63 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20 77 69 ction named X wi
333d0 74 68 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 th any number of
333e0 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a arguments..**.*
333f0 2a 20 7b 48 31 36 31 33 33 7d 20 57 68 65 6e 20 * {H16133} When
33400 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 calls to [sqlite
33410 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
33420 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a n(D,X,N,...)].**
33430 20 20 20 20 20 20 20 20 20 20 73 70 65 63 69 66 specif
33440 79 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 y multiple imple
33450 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 mentations of th
33460 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 e same function
33470 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e X.** an
33480 64 20 77 68 65 6e 20 6f 6e 65 20 69 6d 70 6c 65 d when one imple
33490 6d 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 4e 3e mentation has N>
334a0 3d 30 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 =0 and the other
334b0 20 68 61 73 20 4e 3d 28 2d 31 29 0a 2a 2a 20 20 has N=(-1).**
334c0 20 20 20 20 20 20 20 20 74 68 65 20 69 6d 70 6c the impl
334d0 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 20 ementation with
334e0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 4e 20 73 68 61 a non-zero N sha
334f0 6c 6c 20 62 65 20 70 72 65 66 65 72 72 65 64 2e ll be preferred.
33500 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 36 7d 20 .**.** {H16136}
33510 57 68 65 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 When calls to [s
33520 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
33530 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 2e nction(D,X,N,E,.
33540 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..)].**
33550 20 73 70 65 63 69 66 79 20 6d 75 6c 74 69 70 6c specify multipl
33560 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
33570 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 s of the same fu
33580 6e 63 74 69 6f 6e 20 58 20 77 69 74 68 0a 2a 2a nction X with.**
33590 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 61 the sa
335a0 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 me number of arg
335b0 75 6d 65 6e 74 73 20 4e 20 62 75 74 20 77 69 74 uments N but wit
335c0 68 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 20 h different.**
335d0 20 20 20 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 encoding
335e0 73 20 45 2c 20 74 68 65 6e 20 74 68 65 20 69 6d s E, then the im
335f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65 plementation whe
33600 72 65 20 45 20 6d 61 74 63 68 65 73 20 74 68 65 re E matches the
33610 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 61 74 .** dat
33620 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 73 abase encoding s
33630 68 61 6c 6c 20 70 72 65 66 65 72 72 65 64 2e 0a hall preferred..
33640 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 39 7d 20 46 **.** {H16139} F
33650 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 or an aggregate
33660 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 72 65 SQL function cre
33670 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 ated using.**
33680 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
33690 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
336a0 44 2c 58 2c 4e 2c 45 2c 50 2c 30 2c 53 2c 4c 29 D,X,N,E,P,0,S,L)
336b0 5d 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 0a ] the finalizer.
336c0 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 ** func
336d0 74 69 6f 6e 20 4c 20 73 68 61 6c 6c 20 61 6c 77 tion L shall alw
336e0 61 79 73 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 ays be invoked e
336f0 78 61 63 74 6c 79 20 6f 6e 63 65 20 69 66 20 74 xactly once if t
33700 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 he.** s
33710 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 53 20 69 tep function S i
33720 73 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 s called one or
33730 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 0a 2a more times..**.*
33740 2a 20 7b 48 31 36 31 34 32 7d 20 57 68 65 6e 20 * {H16142} When
33750 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 65 SQLite invokes e
33760 69 74 68 65 72 20 74 68 65 20 78 46 75 6e 63 20 ither the xFunc
33770 6f 72 20 78 53 74 65 70 20 66 75 6e 63 74 69 6f or xStep functio
33780 6e 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 n of.**
33790 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d an application-
337a0 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 defined SQL func
337b0 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 tion or aggregat
337c0 65 20 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20 e created.**
337d0 20 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 65 by [sqlite
337e0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
337f0 6e 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 n()] or [sqlite3
33800 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
33810 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 16()],.**
33820 20 20 20 74 68 65 6e 20 74 68 65 20 61 72 72 61 then the arra
33830 79 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 61 y of [sqlite3_va
33840 6c 75 65 5d 20 6f 62 6a 65 63 74 73 20 70 61 73 lue] objects pas
33850 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 sed as the.**
33860 20 20 20 20 20 20 20 74 68 69 72 64 20 70 61 72 third par
33870 61 6d 65 74 65 72 20 73 68 61 6c 6c 20 62 65 20 ameter shall be
33880 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
33890 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
338a0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
338b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 int sqlite3_cre
338c0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 ate_function(.
338d0 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 sqlite3 *db,. c
338e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 onst char *zFunc
338f0 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 tionName,. int
33900 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 nArg,. int eTex
33910 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 tRep,. void *pA
33920 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 pp,. void (*xFu
33930 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 nc)(sqlite3_cont
33940 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
33950 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 _value**),. voi
33960 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 d (*xStep)(sqlit
33970 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c e3_context*,int,
33980 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 sqlite3_value**)
33990 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 ,. void (*xFina
339a0 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 l)(sqlite3_conte
339b0 78 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 xt*).);.SQLITE_A
339c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
339d0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 reate_function16
339e0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
339f0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
33a00 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 FunctionName,.
33a10 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 int nArg,. int
33a20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 eTextRep,. void
33a30 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 *pApp,. void (
33a40 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f *xFunc)(sqlite3_
33a50 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
33a60 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 ite3_value**),.
33a70 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 void (*xStep)(s
33a80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
33a90 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
33aa0 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 e**),. void (*x
33ab0 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 Final)(sqlite3_c
33ac0 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a ontext*).);../*.
33ad0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 ** CAPI3REF: Tex
33ae0 74 20 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30 t Encodings {H10
33af0 32 36 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48 267} <S50200> <H
33b00 31 36 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 16100>.**.** The
33b10 73 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 se constant defi
33b20 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 ne integer codes
33b30 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 that represent
33b40 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 the various.** t
33b50 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 ext encodings su
33b60 70 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 pported by SQLit
33b70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e..*/.#define SQ
33b80 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 LITE_UTF8
33b90 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
33ba0 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 LITE_UTF16LE
33bb0 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 2.#define SQ
33bc0 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 LITE_UTF16BE
33bd0 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 3.#define SQ
33be0 4c 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 LITE_UTF16
33bf0 20 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 4 /* Use
33c00 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
33c10 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c r */.#define SQL
33c20 49 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 ITE_ANY
33c30 20 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 5 /* sqlit
33c40 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
33c50 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 on only */.#defi
33c60 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f ne SQLITE_UTF16_
33c70 41 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a ALIGNED 8 /*
33c80 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
33c90 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a collation only *
33ca0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 /../*.** CAPI3RE
33cb0 46 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75 F: Deprecated Fu
33cc0 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 nctions.** DEPRE
33cd0 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 CATED.**.** Thes
33ce0 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 e functions are
33cf0 5b 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 [deprecated]. I
33d00 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 n order to maint
33d10 61 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 ain.** backwards
33d20 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 compatibility w
33d30 69 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 ith older code,
33d40 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 these functions
33d50 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 continue .** to
33d60 62 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 be supported. H
33d70 6f 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c owever, new appl
33d80 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 ications should
33d90 61 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 avoid.** the use
33da0 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 of these functi
33db0 6f 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e ons. To help en
33dc0 63 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 courage people t
33dd0 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 o avoid.** using
33de0 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 these functions
33df0 2c 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 , we are not goi
33e00 6e 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 ng to tell you w
33e10 68 61 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a hat they do..*/.
33e20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
33e30 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 MIT_DEPRECATED.S
33e40 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
33e50 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 _DEPRECATED int
33e60 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 sqlite3_aggregat
33e70 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f e_count(sqlite3_
33e80 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 context*);.SQLIT
33e90 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
33ea0 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 RECATED int sqli
33eb0 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 te3_expired(sqli
33ec0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 te3_stmt*);.SQLI
33ed0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 TE_API SQLITE_DE
33ee0 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c PRECATED int sql
33ef0 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 ite3_transfer_bi
33f00 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 ndings(sqlite3_s
33f10 74 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 tmt*, sqlite3_st
33f20 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 mt*);.SQLITE_API
33f30 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 SQLITE_DEPRECAT
33f40 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 ED int sqlite3_g
33f50 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f lobal_recover(vo
33f60 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
33f70 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
33f80 44 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 D void sqlite3_t
33f90 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f hread_cleanup(vo
33fa0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
33fb0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
33fc0 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 D int sqlite3_me
33fd0 6d 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 mory_alarm(void(
33fe0 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 *)(void*,sqlite3
33ff0 5f 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 _int64,int),void
34000 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 *,sqlite3_int64)
34010 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
34020 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e CAPI3REF: Obtain
34030 69 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e ing SQL Function
34040 20 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 Parameter Value
34050 73 20 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32 s {H15100} <S202
34060 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 00>.**.** The C-
34070 6c 61 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 language impleme
34080 6e 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 ntation of SQL f
34090 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 unctions and agg
340a0 72 65 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 regates uses.**
340b0 74 68 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 this set of inte
340c0 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 rface routines t
340d0 6f 20 61 63 63 65 73 73 20 74 68 65 20 70 61 72 o access the par
340e0 61 6d 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e ameter values on
340f0 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e .** the function
34100 20 6f 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a or aggregate..*
34110 2a 0a 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 *.** The xFunc (
34120 66 6f 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 for scalar funct
34130 69 6f 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 ions) or xStep (
34140 66 6f 72 20 61 67 67 72 65 67 61 74 65 73 29 20 for aggregates)
34150 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f parameters.** to
34160 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
34170 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 _function()] and
34180 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
34190 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a _function16()].*
341a0 2a 20 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 * define callbac
341b0 6b 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e ks that implemen
341c0 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 t the SQL functi
341d0 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 ons and aggregat
341e0 65 73 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70 es..** The 4th p
341f0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 arameter to thes
34200 65 20 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 e callbacks is a
34210 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 n array of point
34220 65 72 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 ers to.** [prote
34230 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
34240 75 65 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 ue] objects. Th
34250 65 72 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 ere is one [sqli
34260 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
34270 74 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 t for.** each pa
34280 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 rameter to the S
34290 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 QL function. Th
342a0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
342b0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 used to.** extr
342c0 61 63 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 act values from
342d0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c the [sqlite3_val
342e0 75 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a ue] objects..**.
342f0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 ** These routine
34300 73 20 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 s work only with
34310 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 [protected sqli
34320 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
34330 74 73 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d ts..** Any attem
34340 70 74 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 pt to use these
34350 72 6f 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b routines on an [
34360 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 unprotected sqli
34370 74 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 te3_value].** ob
34380 6a 65 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 ject results in
34390 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 undefined behavi
343a0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 or..**.** These
343b0 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 routines work ju
343c0 73 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 st like the corr
343d0 65 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d esponding [colum
343e0 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f n access functio
343f0 6e 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 ns].** except th
34400 61 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e at these routin
34410 65 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 es take a single
34420 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 [protected sqli
34430 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
34440 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 t.** pointer ins
34450 74 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 tead of a [sqlit
34460 65 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 e3_stmt*] pointe
34470 72 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 r and an integer
34480 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a column number..
34490 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
344a0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 3_value_text16()
344b0 20 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 interface extra
344c0 63 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 cts a UTF-16 str
344d0 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 ing.** in the na
344e0 74 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 tive byte-order
344f0 6f 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 of the host mach
34500 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c ine. The.** sql
34510 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
34520 36 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 6be() and sqlite
34530 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 3_value_text16le
34540 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a () interfaces.**
34550 20 65 78 74 72 61 63 74 20 55 54 46 2d 31 36 20 extract UTF-16
34560 73 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65 strings as big-e
34570 6e 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 ndian and little
34580 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 -endian respecti
34590 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 vely..**.** The
345a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 sqlite3_value_nu
345b0 6d 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74 meric_type() int
345c0 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 erface attempts
345d0 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 to apply.** nume
345e0 72 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20 ric affinity to
345f0 74 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73 the value. This
34600 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61 means that an a
34610 74 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64 ttempt is.** mad
34620 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 e to convert the
34630 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 value to an int
34640 65 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 eger or floating
34650 20 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 point. If.** s
34660 75 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e uch a conversion
34670 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 is possible wit
34680 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 hout loss of inf
34690 6f 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68 ormation (in oth
346a0 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20 er.** words, if
346b0 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 the value is a s
346c0 74 72 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73 tring that looks
346d0 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a like a number).
346e0 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76 ** then the conv
346f0 65 72 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 ersion is perfor
34700 6d 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 med. Otherwise
34710 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 no conversion oc
34720 63 75 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 curs..** The [SQ
34730 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 LITE_INTEGER | d
34740 61 74 61 74 79 70 65 5d 20 61 66 74 65 72 20 63 atatype] after c
34750 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74 onversion is ret
34760 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 urned..**.** Ple
34770 61 73 65 20 70 61 79 20 70 61 72 74 69 63 75 6c ase pay particul
34780 61 72 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 ar attention to
34790 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 the fact that th
347a0 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e e pointer return
347b0 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 ed.** from [sqli
347c0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29 te3_value_blob()
347d0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 ], [sqlite3_valu
347e0 65 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a e_text()], or.**
347f0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
34800 74 65 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65 text16()] can be
34810 20 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 invalidated by
34820 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c a subsequent cal
34830 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 l to.** [sqlite3
34840 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c _value_bytes()],
34850 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
34860 62 79 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c bytes16()], [sql
34870 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
34880 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 )],.** or [sqlit
34890 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 e3_value_text16(
348a0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 )]..**.** These
348b0 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 routines must be
348c0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 called from the
348d0 20 73 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a same thread as.
348e0 2a 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 ** the SQL funct
348f0 69 6f 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65 ion that supplie
34900 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 d the [sqlite3_v
34910 61 6c 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72 alue*] parameter
34920 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 s..**.** INVARIA
34930 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 NTS:.**.** {H151
34940 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 03} The [sqlite3
34950 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29 5d 20 _value_blob(V)]
34960 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 interface conver
34970 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 ts the.**
34980 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 [protected sq
34990 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
349a0 65 63 74 20 56 20 69 6e 74 6f 20 61 20 42 4c 4f ect V into a BLO
349b0 42 20 61 6e 64 20 74 68 65 6e 0a 2a 2a 20 20 20 B and then.**
349c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 returns a
349d0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
349e0 63 6f 6e 76 65 72 74 65 64 20 76 61 6c 75 65 2e converted value.
349f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30 36 7d 20 .**.** {H15106}
34a00 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c The [sqlite3_val
34a10 75 65 5f 62 79 74 65 73 28 56 29 5d 20 69 6e 74 ue_bytes(V)] int
34a20 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 erface returns t
34a30 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e he.** n
34a40 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
34a50 6e 20 74 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 n the BLOB or st
34a60 72 69 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20 ring (exclusive
34a70 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 of the.**
34a80 20 20 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 zero terminat
34a90 6f 72 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 or on the string
34aa0 29 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 ) that was retur
34ab0 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 ned by the.**
34ac0 20 20 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65 most rece
34ad0 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 nt call to [sqli
34ae0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 56 te3_value_blob(V
34af0 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 )] or.**
34b00 20 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 [sqlite3_value
34b10 5f 74 65 78 74 28 56 29 5d 2e 0a 2a 2a 0a 2a 2a _text(V)]..**.**
34b20 20 7b 48 31 35 31 30 39 7d 20 54 68 65 20 5b 73 {H15109} The [s
34b30 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
34b40 65 73 31 36 28 56 29 5d 20 69 6e 74 65 72 66 61 es16(V)] interfa
34b50 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a ce returns the.*
34b60 2a 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 * numbe
34b70 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
34b80 65 20 73 74 72 69 6e 67 20 28 65 78 63 6c 75 73 e string (exclus
34b90 69 76 65 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 ive of the.**
34ba0 20 20 20 20 20 20 20 7a 65 72 6f 20 74 65 72 6d zero term
34bb0 69 6e 61 74 6f 72 20 6f 6e 20 74 68 65 20 73 74 inator on the st
34bc0 72 69 6e 67 29 20 74 68 61 74 20 77 61 73 20 72 ring) that was r
34bd0 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a 2a eturned by the.*
34be0 2a 20 20 20 20 20 20 20 20 20 20 6d 6f 73 74 20 * most
34bf0 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b recent call to [
34c00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
34c10 78 74 31 36 28 56 29 5d 2c 0a 2a 2a 20 20 20 20 xt16(V)],.**
34c20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 [sqlite3_v
34c30 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56 29 alue_text16be(V)
34c40 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 ], or [sqlite3_v
34c50 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 56 29 alue_text16le(V)
34c60 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 31 32 ]..**.** {H15112
34c70 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 } The [sqlite3_v
34c80 61 6c 75 65 5f 64 6f 75 62 6c 65 28 56 29 5d 20 alue_double(V)]
34c90 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 interface conver
34ca0 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 ts the.**
34cb0 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 [protected sq
34cc0 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
34cd0 65 63 74 20 56 20 69 6e 74 6f 20 61 20 66 6c 6f ect V into a flo
34ce0 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
34cf0 65 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 e and.**
34d00 20 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 returns a copy
34d10 20 6f 66 20 74 68 61 74 20 76 61 6c 75 65 2e 0a of that value..
34d20 2a 2a 0a 2a 2a 20 7b 48 31 35 31 31 35 7d 20 54 **.** {H15115} T
34d30 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 he [sqlite3_valu
34d40 65 5f 69 6e 74 28 56 29 5d 20 69 6e 74 65 72 66 e_int(V)] interf
34d50 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 ace converts the
34d60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 .** [pr
34d70 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
34d80 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 value] object V
34d90 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69 into a 64-bit si
34da0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 gned integer and
34db0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 .** ret
34dc0 75 72 6e 73 20 74 68 65 20 6c 6f 77 65 72 20 33 urns the lower 3
34dd0 32 20 62 69 74 73 20 6f 66 20 74 68 61 74 20 69 2 bits of that i
34de0 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 nteger..**.** {H
34df0 31 35 31 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 15118} The [sqli
34e00 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 te3_value_int64(
34e10 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f V)] interface co
34e20 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 nverts the.**
34e30 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 [protecte
34e40 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
34e50 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 object V into a
34e60 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 64-bit signed i
34e70 6e 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20 20 nteger and.**
34e80 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 returns a
34e90 20 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69 6e copy of that in
34ea0 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 teger..**.** {H1
34eb0 35 31 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 5121} The [sqlit
34ec0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 56 29 e3_value_text(V)
34ed0 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 ] interface conv
34ee0 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 erts the.**
34ef0 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 [protected
34f00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
34f10 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 7a bject V into a z
34f20 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 ero-terminated U
34f30 54 46 2d 38 0a 2a 2a 20 20 20 20 20 20 20 20 20 TF-8.**
34f40 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 string and retu
34f50 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
34f60 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a that string..**
34f70 0a 2a 2a 20 7b 48 31 35 31 32 34 7d 20 54 68 65 .** {H15124} The
34f80 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
34f90 74 65 78 74 31 36 28 56 29 5d 20 69 6e 74 65 72 text16(V)] inter
34fa0 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 face converts th
34fb0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 e.** [p
34fc0 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
34fd0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 _value] object V
34fe0 20 69 6e 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 into a zero-ter
34ff0 6d 69 6e 61 74 65 64 20 32 2d 62 79 74 65 0a 2a minated 2-byte.*
35000 2a 20 20 20 20 20 20 20 20 20 20 61 6c 69 67 6e * align
35010 65 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 ed UTF-16 native
35020 20 62 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 byte order.**
35030 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 20 61 string a
35040 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 nd returns a poi
35050 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 72 nter to that str
35060 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 ing..**.** {H151
35070 32 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 27} The [sqlite3
35080 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 _value_text16be(
35090 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f V)] interface co
350a0 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 nverts the.**
350b0 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 [protecte
350c0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
350d0 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 object V into a
350e0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
350f0 20 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 2-byte.**
35100 20 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d aligned UTF-
35110 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a 16 big-endian.**
35120 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 string
35130 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 and returns a p
35140 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73 ointer to that s
35150 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 tring..**.** {H1
35160 35 31 33 30 7d 20 54 68 65 20 5b 73 71 6c 69 74 5130} The [sqlit
35170 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c e3_value_text16l
35180 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 e(V)] interface
35190 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 converts the.**
351a0 20 20 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 [protec
351b0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
351c0 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f e] object V into
351d0 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 a zero-terminat
351e0 65 64 20 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 ed 2-byte.**
351f0 20 20 20 20 20 20 61 6c 69 67 6e 65 64 20 55 54 aligned UT
35200 46 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69 F-16 little-endi
35210 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 an.** s
35220 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e tring and return
35230 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
35240 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a hat string..**.*
35250 2a 20 7b 48 31 35 31 33 33 7d 20 54 68 65 20 5b * {H15133} The [
35260 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
35270 70 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 pe(V)] interface
35280 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 returns.**
35290 20 20 20 20 20 6f 6e 65 20 6f 66 20 5b 53 51 4c one of [SQL
352a0 49 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53 51 4c 49 ITE_NULL], [SQLI
352b0 54 45 5f 49 4e 54 45 47 45 52 5d 2c 20 5b 53 51 TE_INTEGER], [SQ
352c0 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a 2a 2a 20 LITE_FLOAT],.**
352d0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 [SQLITE
352e0 5f 54 45 58 54 5d 2c 20 6f 72 20 5b 53 51 4c 49 _TEXT], or [SQLI
352f0 54 45 5f 42 4c 4f 42 5d 20 61 73 20 61 70 70 72 TE_BLOB] as appr
35300 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a 20 20 opriate for.**
35310 20 20 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c the [sql
35320 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
35330 63 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 ct V..**.** {H15
35340 31 33 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 136} The [sqlite
35350 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 3_value_numeric_
35360 74 79 70 65 28 56 29 5d 20 69 6e 74 65 72 66 61 type(V)] interfa
35370 63 65 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 20 ce converts.**
35380 20 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 6f the [pro
35390 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
353a0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 alue] object V i
353b0 6e 74 6f 20 65 69 74 68 65 72 20 61 6e 20 69 6e nto either an in
353c0 74 65 67 65 72 20 6f 72 0a 2a 2a 20 20 20 20 20 teger or.**
353d0 20 20 20 20 20 61 20 66 6c 6f 61 74 69 6e 67 20 a floating
353e0 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 66 20 69 point value if i
353f0 74 20 63 61 6e 20 64 6f 20 73 6f 20 77 69 74 68 t can do so with
35400 6f 75 74 20 6c 6f 73 73 20 6f 66 0a 2a 2a 20 20 out loss of.**
35410 20 20 20 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 informat
35420 69 6f 6e 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 ion, and returns
35430 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f one of [SQLITE_
35440 4e 55 4c 4c 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 NULL],.**
35450 20 20 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 [SQLITE_INTEG
35460 45 52 5d 2c 20 5b 53 51 4c 49 54 45 5f 46 4c 4f ER], [SQLITE_FLO
35470 41 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 AT], [SQLITE_TEX
35480 54 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 T], or.**
35490 20 20 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d [SQLITE_BLOB]
354a0 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 as appropriate
354b0 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 for the.**
354c0 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 [protected s
354d0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
354e0 6a 65 63 74 20 56 20 61 66 74 65 72 20 74 68 65 ject V after the
354f0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 74 74 65 conversion atte
35500 6d 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 mpt..*/.SQLITE_A
35510 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
35520 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f qlite3_value_blo
35530 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a b(sqlite3_value*
35540 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
35550 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f t sqlite3_value_
35560 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 bytes(sqlite3_va
35570 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
35580 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
35590 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 lue_bytes16(sqli
355a0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
355b0 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 ITE_API double s
355c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 qlite3_value_dou
355d0 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ble(sqlite3_valu
355e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
355f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 int sqlite3_valu
35600 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 e_int(sqlite3_va
35610 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
35620 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 I sqlite3_int64
35630 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e sqlite3_value_in
35640 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 t64(sqlite3_valu
35650 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
35660 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
35670 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c har *sqlite3_val
35680 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f ue_text(sqlite3_
35690 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
356a0 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
356b0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
356c0 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c xt16(sqlite3_val
356d0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
356e0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
356f0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
35700 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 6le(sqlite3_valu
35710 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
35720 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
35730 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
35740 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 be(sqlite3_value
35750 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
35760 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt sqlite3_value
35770 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 _type(sqlite3_va
35780 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
35790 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
357a0 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 lue_numeric_type
357b0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
357c0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
357d0 46 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67 F: Obtain Aggreg
357e0 61 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e ate Function Con
357f0 74 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53 text {H16210} <S
35800 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20200>.**.** The
35810 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
35820 6f 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c of aggregate SQL
35830 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 functions use t
35840 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61 his routine to a
35850 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72 llocate.** a str
35860 75 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69 ucture for stori
35870 6e 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a ng their state..
35880 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
35890 74 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33 time the sqlite3
358a0 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 _aggregate_conte
358b0 78 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 xt() routine is
358c0 63 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20 called for a.**
358d0 70 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 particular aggre
358e0 67 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c gate, SQLite all
358f0 6f 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66 ocates nBytes of
35900 20 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20 memory, zeroes
35910 6f 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f out that.** memo
35920 72 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ry, and returns
35930 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
35940 20 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 On second and s
35950 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 ubsequent calls
35960 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67 to.** sqlite3_ag
35970 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
35980 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61 ) for the same a
35990 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
359a0 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20 n index,.** the
359b0 73 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72 same buffer is r
359c0 65 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70 eturned. The imp
359d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
359e0 68 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e he aggregate can
359f0 20 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 use.** the retu
35a00 72 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61 rned buffer to a
35a10 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a ccumulate data..
35a20 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74 **.** SQLite aut
35a30 6f 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73 omatically frees
35a40 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 the allocated b
35a50 75 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61 uffer when the a
35a60 67 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72 ggregate.** quer
35a70 79 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a y concludes..**.
35a80 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 ** The first par
35a90 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 ameter should be
35aa0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a a copy of the.*
35ab0 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 * [sqlite3_conte
35ac0 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f xt | SQL functio
35ad0 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20 n context] that
35ae0 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72 is the first par
35af0 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ameter.** to the
35b00 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e callback routin
35b10 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 e that implement
35b20 73 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 s the aggregate
35b30 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
35b40 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 This routine mus
35b50 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d t be called from
35b60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
35b70 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 in which.** the
35b80 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 aggregate SQL f
35b90 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 unction is runni
35ba0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 ng..**.** INVARI
35bb0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 ANTS:.**.** {H16
35bc0 32 31 31 7d 20 54 68 65 20 66 69 72 73 74 20 69 211} The first i
35bd0 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 nvocation of [sq
35be0 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
35bf0 63 6f 6e 74 65 78 74 28 43 2c 4e 29 5d 20 66 6f context(C,N)] fo
35c00 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 r.** a
35c10 70 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 particular insta
35c20 6e 63 65 20 6f 66 20 61 6e 20 61 67 67 72 65 67 nce of an aggreg
35c30 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f ate function (fo
35c40 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a r a particular.*
35c50 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 * conte
35c60 78 74 20 43 29 20 63 61 75 73 65 73 20 53 51 4c xt C) causes SQL
35c70 69 74 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ite to allocate
35c80 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 N bytes of memor
35c90 79 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a y,.** z
35ca0 65 72 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2c ero that memory,
35cb0 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f and return a po
35cc0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c inter to the all
35cd0 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a ocated memory..*
35ce0 2a 0a 2a 2a 20 7b 48 31 36 32 31 33 7d 20 49 66 *.** {H16213} If
35cf0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
35d00 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 tion error occur
35d10 73 20 64 75 72 69 6e 67 0a 2a 2a 20 20 20 20 20 s during.**
35d20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 [sqlite3_ag
35d30 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
35d40 43 2c 4e 29 5d 20 74 68 65 6e 20 74 68 65 20 66 C,N)] then the f
35d50 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
35d60 30 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31 35 0..**.** {H16215
35d70 7d 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 } Second and sub
35d80 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 sequent invocati
35d90 6f 6e 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 ons of.**
35da0 20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 [sqlite3_aggr
35db0 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c egate_context(C,
35dc0 4e 29 5d 20 66 6f 72 20 74 68 65 20 73 61 6d 65 N)] for the same
35dd0 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 context pointer
35de0 20 43 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 C.** i
35df0 67 6e 6f 72 65 20 74 68 65 20 4e 20 70 61 72 61 gnore the N para
35e00 6d 65 74 65 72 20 61 6e 64 20 72 65 74 75 72 6e meter and return
35e10 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
35e20 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 e same.**
35e30 20 20 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f block of memo
35e40 72 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ry returned by t
35e50 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74 he first invocat
35e60 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 ion..**.** {H162
35e70 31 37 7d 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 17} The memory a
35e80 6c 6c 6f 63 61 74 65 64 20 62 79 20 5b 73 71 6c llocated by [sql
35e90 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
35ea0 6f 6e 74 65 78 74 28 43 2c 4e 29 5d 20 69 73 0a ontext(C,N)] is.
35eb0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f ** auto
35ec0 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 64 20 matically freed
35ed0 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c on the next call
35ee0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 to [sqlite3_res
35ef0 65 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 et()].**
35f00 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 or [sqlite3_fi
35f10 6e 61 6c 69 7a 65 28 29 5d 20 66 6f 72 20 74 68 nalize()] for th
35f20 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
35f30 65 6d 65 6e 74 5d 20 63 6f 6e 74 61 69 6e 69 6e ement] containin
35f40 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 g.** th
35f50 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 e aggregate func
35f60 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 tion associated
35f70 77 69 74 68 20 63 6f 6e 74 65 78 74 20 43 2e 0a with context C..
35f80 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
35f90 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 id *sqlite3_aggr
35fa0 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 egate_context(sq
35fb0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
35fc0 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a int nBytes);../*
35fd0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 .** CAPI3REF: Us
35fe0 65 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 er Data For Func
35ff0 74 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c tions {H16240} <
36000 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20200>.**.** Th
36010 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 e sqlite3_user_d
36020 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 ata() interface
36030 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f returns a copy o
36040 66 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 f.** the pointer
36050 20 74 68 61 74 20 77 61 73 20 74 68 65 20 70 55 that was the pU
36060 73 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 65 serData paramete
36070 72 20 28 74 68 65 20 35 74 68 20 70 61 72 61 6d r (the 5th param
36080 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 eter).** of the
36090 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
360a0 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 function()].** a
360b0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 nd [sqlite3_crea
360c0 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d te_function16()]
360d0 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f routines that o
360e0 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 riginally.** reg
360f0 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c istered the appl
36100 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 ication defined
36110 66 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a function. {END}.
36120 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
36130 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 ne must be calle
36140 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 d from the same
36150 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a thread in which.
36160 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ** the applicati
36170 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
36180 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a ion is running..
36190 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
361a0 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 34 33 7d :.**.** {H16243}
361b0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 73 The [sqlite3_us
361c0 65 72 5f 64 61 74 61 28 43 29 5d 20 69 6e 74 65 er_data(C)] inte
361d0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 rface returns a
361e0 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 20 copy of the.**
361f0 20 20 20 20 20 20 20 20 50 20 70 6f 69 6e 74 65 P pointe
36200 72 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 r from the [sqli
36210 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
36220 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c ion(D,X,N,E,P,F,
36230 53 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 S,L)].**
36240 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 or [sqlite3_cr
36250 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 eate_function16(
36260 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 D,X,N,E,P,F,S,L)
36270 5d 20 63 61 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 ] call that.**
36280 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 register
36290 65 64 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 ed the SQL funct
362a0 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 ion associated w
362b0 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e ith [sqlite3_con
362c0 74 65 78 74 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 text] C..*/.SQLI
362d0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
362e0 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 ite3_user_data(s
362f0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 qlite3_context*)
36300 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
36310 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e F: Database Conn
36320 65 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 ection For Funct
36330 69 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 ions {H16250} <S
36340 36 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 60600><S20200>.*
36350 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
36360 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
36370 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 le() interface r
36380 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 eturns a copy of
36390 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 .** the pointer
363a0 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 to the [database
363b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 connection] (th
363c0 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 e 1st parameter)
363d0 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 .** of the [sqli
363e0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
363f0 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 ion()].** and [s
36400 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
36410 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 nction16()] rout
36420 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e ines that origin
36430 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 ally.** register
36440 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ed the applicati
36450 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on defined funct
36460 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 ion..**.** INVAR
36470 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 IANTS:.**.** {H1
36480 36 32 35 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 6253} The [sqlit
36490 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
364a0 6e 64 6c 65 28 43 29 5d 20 69 6e 74 65 72 66 61 ndle(C)] interfa
364b0 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 ce returns a cop
364c0 79 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 y of the.**
364d0 20 20 20 20 20 44 20 70 6f 69 6e 74 65 72 20 66 D pointer f
364e0 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33 rom the [sqlite3
364f0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
36500 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c (D,X,N,E,P,F,S,L
36510 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f )].** o
36520 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 r [sqlite3_creat
36530 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44 2c 58 e_function16(D,X
36540 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d 20 63 ,N,E,P,F,S,L)] c
36550 61 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 all that.**
36560 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64 20 registered
36570 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
36580 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
36590 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 [sqlite3_contex
365a0 74 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f t] C..*/.SQLITE_
365b0 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c API sqlite3 *sql
365c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f ite3_context_db_
365d0 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 handle(sqlite3_c
365e0 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a ontext*);../*.**
365f0 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 CAPI3REF: Funct
36600 69 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 ion Auxiliary Da
36610 74 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 ta {H16270} <S20
36620 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 200>.**.** The f
36630 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e ollowing two fun
36640 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 ctions may be us
36650 65 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c ed by scalar SQL
36660 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a functions to.**
36670 20 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 associate metad
36680 61 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e ata with argumen
36690 74 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 t values. If the
366a0 20 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 same value is p
366b0 61 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 assed to.** mult
366c0 69 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 iple invocations
366d0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c of the same SQL
366e0 20 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 function during
366f0 20 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e query execution
36700 2c 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 , under.** some
36710 63 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 circumstances th
36720 65 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 e associated met
36730 61 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 adata may be pre
36740 73 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 served. This may
36750 0a 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 .** be used, for
36760 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 example, to add
36770 20 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 a regular-expre
36780 73 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 ssion matching s
36790 63 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f calar.** functio
367a0 6e 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 n. The compiled
367b0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 version of the r
367c0 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f egular expressio
367d0 6e 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a n is stored as.*
367e0 2a 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 * metadata assoc
367f0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 iated with the S
36800 51 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 QL value passed
36810 61 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 as the regular e
36820 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 xpression.** pat
36830 74 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 tern. The compi
36840 6c 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 led regular expr
36850 65 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 ession can be re
36860 75 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 used on multiple
36870 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 .** invocations
36880 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 of the same func
36890 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 tion so that the
368a0 20 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 original patter
368b0 6e 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 n string.** does
368c0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 not need to be
368d0 72 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 recompiled on ea
368e0 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a ch invocation..*
368f0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
36900 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 _get_auxdata() i
36910 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
36920 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
36930 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 e metadata.** as
36940 73 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 sociated by the
36950 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 sqlite3_set_auxd
36960 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 ata() function w
36970 69 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 ith the Nth argu
36980 6d 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f ment.** value to
36990 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
369a0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
369b0 6e 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 n. If no metadat
369c0 61 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a a has been ever.
369d0 2a 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 ** been set for
369e0 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 the Nth argument
369f0 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
36a00 2c 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 , or if the corr
36a10 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e esponding.** fun
36a20 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 ction parameter
36a30 68 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 has changed sinc
36a40 65 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 e the meta-data
36a50 77 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e was set,.** then
36a60 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 sqlite3_get_aux
36a70 64 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 data() returns a
36a80 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a NULL pointer..*
36a90 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
36aa0 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 _set_auxdata() i
36ab0 6e 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 nterface saves t
36ac0 68 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 he metadata.** p
36ad0 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 ointed to by its
36ae0 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 3rd parameter a
36af0 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 s the metadata f
36b00 6f 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 or the N-th.** a
36b10 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 rgument of the a
36b20 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
36b30 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 ed function. Su
36b40 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c bsequent.** call
36b50 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 s to sqlite3_get
36b60 5f 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 _auxdata() might
36b70 20 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 return this dat
36b80 61 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 a, if it has.**
36b90 6e 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 not been destroy
36ba0 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 ed..** If it is
36bb0 6e 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 not NULL, SQLite
36bc0 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 will invoke the
36bd0 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 destructor.** f
36be0 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 unction given by
36bf0 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 the 4th paramet
36c00 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 er to sqlite3_se
36c10 74 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a t_auxdata() on.*
36c20 2a 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 * the metadata w
36c30 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f hen the correspo
36c40 6e 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 nding function p
36c50 61 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 arameter changes
36c60 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 .** or when the
36c70 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f SQL statement co
36c80 6d 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 mpletes, whichev
36c90 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a er comes first..
36ca0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 **.** SQLite is
36cb0 66 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 free to call the
36cc0 20 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 destructor and
36cd0 64 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e drop metadata on
36ce0 20 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 any.** paramete
36cf0 72 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f r of any functio
36d00 6e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 n at any time.
36d10 54 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 The only guarant
36d20 65 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 ee is that.** th
36d30 65 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c e destructor wil
36d40 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f l be called befo
36d50 72 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 re the metadata
36d60 69 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a is dropped..**.*
36d70 2a 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d * In practice, m
36d80 65 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 etadata is prese
36d90 72 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e rved between fun
36da0 63 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a ction calls for.
36db0 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 ** expressions t
36dc0 68 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 hat are constant
36dd0 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 at compile time
36de0 2e 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 . This includes
36df0 6c 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 literal.** value
36e00 73 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 s and SQL variab
36e10 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 les..**.** These
36e20 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 routines must b
36e30 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 e called from th
36e40 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e e same thread in
36e50 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 which.** the SQ
36e60 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 L function is ru
36e70 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 nning..**.** INV
36e80 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
36e90 48 31 36 32 37 32 7d 20 54 68 65 20 5b 73 71 6c H16272} The [sql
36ea0 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 ite3_get_auxdata
36eb0 28 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 (C,N)] interface
36ec0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
36ed0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 er.** t
36ee0 6f 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 o metadata assoc
36ef0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 4e iated with the N
36f00 74 68 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 th parameter of
36f10 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
36f20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 6f .** who
36f30 73 65 20 63 6f 6e 74 65 78 74 20 69 73 20 43 2c se context is C,
36f40 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 or NULL if ther
36f50 65 20 69 73 20 6e 6f 20 6d 65 74 61 64 61 74 61 e is no metadata
36f60 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 20 associated.**
36f70 20 20 20 20 20 20 20 20 77 69 74 68 20 74 68 61 with tha
36f80 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a t parameter..**.
36f90 2a 2a 20 7b 48 31 36 32 37 34 7d 20 54 68 65 20 ** {H16274} The
36fa0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 [sqlite3_set_aux
36fb0 64 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 69 data(C,N,P,D)] i
36fc0 6e 74 65 72 66 61 63 65 20 61 73 73 69 67 6e 73 nterface assigns
36fd0 20 61 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 20 a metadata.**
36fe0 20 20 20 20 20 20 20 20 70 6f 69 6e 74 65 72 20 pointer
36ff0 50 20 74 6f 20 74 68 65 20 4e 74 68 20 70 61 72 P to the Nth par
37000 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 53 51 ameter of the SQ
37010 4c 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 L function with
37020 63 6f 6e 74 65 78 74 20 43 2e 0a 2a 2a 0a 2a 2a context C..**.**
37030 20 7b 48 31 36 32 37 36 7d 20 53 51 4c 69 74 65 {H16276} SQLite
37040 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 will invoke the
37050 20 64 65 73 74 72 75 63 74 6f 72 20 44 20 77 69 destructor D wi
37060 74 68 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 th a single argu
37070 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 ment.**
37080 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6d 65 which is the me
37090 74 61 64 61 74 61 20 70 6f 69 6e 74 65 72 20 50 tadata pointer P
370a0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c following a cal
370b0 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 l to.**
370c0 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 [sqlite3_set_au
370d0 78 64 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 xdata(C,N,P,D)]
370e0 77 68 65 6e 20 53 51 4c 69 74 65 20 63 65 61 73 when SQLite ceas
370f0 65 73 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 20 20 es to hold.**
37100 20 20 20 20 20 20 20 74 68 65 20 6d 65 74 61 64 the metad
37110 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 ata..**.** {H162
37120 37 37 7d 20 53 51 4c 69 74 65 20 63 65 61 73 65 77} SQLite cease
37130 73 20 74 6f 20 68 6f 6c 64 20 6d 65 74 61 64 61 s to hold metada
37140 74 61 20 66 6f 72 20 61 6e 20 53 51 4c 20 66 75 ta for an SQL fu
37150 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 nction parameter
37160 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 .** whe
37170 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 n the value of t
37180 68 61 74 20 70 61 72 61 6d 65 74 65 72 20 63 68 hat parameter ch
37190 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 anges..**.** {H1
371a0 36 32 37 38 7d 20 57 68 65 6e 20 5b 73 71 6c 69 6278} When [sqli
371b0 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 te3_set_auxdata(
371c0 43 2c 4e 2c 50 2c 44 29 5d 20 69 73 20 69 6e 76 C,N,P,D)] is inv
371d0 6f 6b 65 64 2c 20 74 68 65 20 64 65 73 74 72 75 oked, the destru
371e0 63 74 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ctor.**
371f0 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 is called for a
37200 6e 79 20 70 72 69 6f 72 20 6d 65 74 61 64 61 74 ny prior metadat
37210 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
37220 68 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 h the same funct
37230 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ion.**
37240 63 6f 6e 74 65 78 74 20 43 20 61 6e 64 20 70 61 context C and pa
37250 72 61 6d 65 74 65 72 20 4e 2e 0a 2a 2a 0a 2a 2a rameter N..**.**
37260 20 7b 48 31 36 32 37 39 7d 20 53 51 4c 69 74 65 {H16279} SQLite
37270 20 77 69 6c 6c 20 63 61 6c 6c 20 64 65 73 74 72 will call destr
37280 75 63 74 6f 72 73 20 66 6f 72 20 61 6e 79 20 6d uctors for any m
37290 65 74 61 64 61 74 61 20 69 74 20 69 73 20 68 6f etadata it is ho
372a0 6c 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 lding.**
372b0 20 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 in a particula
372c0 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 r [prepared stat
372d0 65 6d 65 6e 74 5d 20 53 20 77 68 65 6e 20 65 69 ement] S when ei
372e0 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ther.**
372f0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
37300 53 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f S)] or [sqlite3_
37310 66 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 finalize(S)] is
37320 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 called..*/.SQLIT
37330 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
37340 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
37350 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
37360 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 , int N);.SQLITE
37370 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
37380 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73 71 3_set_auxdata(sq
37390 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
373a0 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f int N, void*, vo
373b0 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a id (*)(void*));.
373c0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
373d0 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 69 : Constants Defi
373e0 6e 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 73 ning Special Des
373f0 74 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f 72 tructor Behavior
37400 20 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31 30 {H10280} <S3010
37410 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 0>.**.** These a
37420 72 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 re special value
37430 73 20 66 6f 72 20 74 68 65 20 64 65 73 74 72 75 s for the destru
37440 63 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 73 ctor that is pas
37450 73 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a 2a sed in as the.**
37460 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 final argument
37470 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 to routines like
37480 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
37490 5f 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74 68 _blob()]. If th
374a0 65 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 e destructor.**
374b0 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 argument is SQLI
374c0 54 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d 65 TE_STATIC, it me
374d0 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e ans that the con
374e0 74 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 20 tent pointer is
374f0 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 constant.** and
37500 77 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e 67 will never chang
37510 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 e. It does not
37520 6e 65 65 64 20 74 6f 20 62 65 20 64 65 73 74 72 need to be destr
37530 6f 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 51 oyed. The.** SQ
37540 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 76 LITE_TRANSIENT v
37550 61 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 alue means that
37560 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c the content will
37570 20 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 69 likely change i
37580 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 75 n.** the near fu
37590 74 75 72 65 20 61 6e 64 20 74 68 61 74 20 53 51 ture and that SQ
375a0 4c 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 Lite should make
375b0 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 its own private
375c0 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 copy of.** the
375d0 63 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 72 content before r
375e0 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 eturning..**.**
375f0 54 68 65 20 74 79 70 65 64 65 66 20 69 73 20 6e The typedef is n
37600 65 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72 6b ecessary to work
37610 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d 73 around problems
37620 20 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 43 in certain.** C
37630 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 53 ++ compilers. S
37640 65 65 20 74 69 63 6b 65 74 20 23 32 31 39 31 2e ee ticket #2191.
37650 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64 .*/.typedef void
37660 20 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72 (*sqlite3_destr
37670 75 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64 uctor_type)(void
37680 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 *);.#define SQLI
37690 54 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 28 TE_STATIC (
376a0 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 (sqlite3_destruc
376b0 74 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65 66 tor_type)0).#def
376c0 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 ine SQLITE_TRANS
376d0 49 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33 IENT ((sqlite3
376e0 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 _destructor_type
376f0 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 )-1)../*.** CAPI
37700 33 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54 68 3REF: Setting Th
37710 65 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 53 e Result Of An S
37720 51 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31 36 QL Function {H16
37730 34 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 400} <S20200>.**
37740 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
37750 65 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74 es are used by t
37760 68 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69 6e he xFunc or xFin
37770 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 al callbacks tha
37780 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 53 t.** implement S
37790 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 QL functions and
377a0 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 53 65 aggregates. Se
377b0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 e.** [sqlite3_cr
377c0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d eate_function()]
377d0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 and [sqlite3_cr
377e0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 eate_function16(
377f0 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 )].** for additi
37800 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
37810 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 ..**.** These fu
37820 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65 72 nctions work ver
37830 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20 y much like the
37840 5b 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 [parameter bindi
37850 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a ng] family of.**
37860 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 functions used
37870 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20 74 to bind values t
37880 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 o host parameter
37890 73 20 69 6e 20 70 72 65 70 61 72 65 64 20 73 74 s in prepared st
378a0 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65 66 atements..** Ref
378b0 65 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20 70 er to the [SQL p
378c0 61 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d 65 arameter] docume
378d0 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 ntation for addi
378e0 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
378f0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 on..**.** The sq
37900 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f lite3_result_blo
37910 62 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 b() interface se
37920 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 ts the result fr
37930 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 om.** an applica
37940 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
37950 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 ction to be the
37960 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 65 BLOB whose conte
37970 6e 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a 2a nt is pointed.**
37980 20 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f 6e to by the secon
37990 64 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 d parameter and
379a0 77 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 73 which is N bytes
379b0 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 73 long where N is
379c0 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70 61 the.** third pa
379d0 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 rameter..**.** T
379e0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
379f0 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e 74 t_zeroblob() int
37a00 65 72 66 61 63 65 73 20 73 65 74 20 74 68 65 20 erfaces set the
37a10 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 result of.** the
37a20 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
37a30 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f ined function to
37a40 20 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 61 be a BLOB conta
37a50 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a ining all zero.*
37a60 2a 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62 79 * bytes and N by
37a70 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65 tes in size, whe
37a80 72 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c 75 re N is the valu
37a90 65 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61 72 e of the 2nd par
37aa0 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ameter..**.** Th
37ab0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
37ac0 5f 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 66 _double() interf
37ad0 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 ace sets the res
37ae0 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 ult from.** an a
37af0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
37b00 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 ed function to b
37b10 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 e a floating poi
37b20 6e 74 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 nt value specifi
37b30 65 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e 64 ed.** by its 2nd
37b40 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
37b50 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
37b60 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 ult_error() and
37b70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
37b80 72 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69 6f rror16() functio
37b90 6e 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20 ns.** cause the
37ba0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20 implemented SQL
37bb0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f function to thro
37bc0 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a w an exception..
37bd0 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 ** SQLite uses t
37be0 68 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 he string pointe
37bf0 64 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 32 d to by the.** 2
37c00 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 nd parameter of
37c10 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
37c20 72 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 65 rror() or sqlite
37c30 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
37c40 28 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 78 ().** as the tex
37c50 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 t of an error me
37c60 73 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20 69 ssage. SQLite i
37c70 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 65 72 nterprets the er
37c80 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 73 ror.** message s
37c90 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 tring from sqlit
37ca0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
37cb0 29 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c 69 ) as UTF-8. SQLi
37cc0 74 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74 73 te.** interprets
37cd0 20 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f 6d the string from
37ce0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
37cf0 65 72 72 6f 72 31 36 28 29 20 61 73 20 55 54 46 error16() as UTF
37d00 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a 2a -16 in native.**
37d10 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49 66 byte order. If
37d20 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d the third param
37d30 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f eter to sqlite3_
37d40 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a result_error().*
37d50 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 * or sqlite3_res
37d60 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73 ult_error16() is
37d70 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 negative then S
37d80 51 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20 74 QLite takes as t
37d90 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 he error.** mess
37da0 61 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70 20 age all text up
37db0 74 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 through the firs
37dc0 74 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 t zero character
37dd0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 72 ..** If the thir
37de0 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 d parameter to s
37df0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
37e00 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 ror() or.** sqli
37e10 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
37e20 31 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 16() is non-nega
37e30 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 tive then SQLite
37e40 20 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e 79 takes that many
37e50 0a 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 63 .** bytes (not c
37e60 68 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d 20 haracters) from
37e70 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
37e80 72 20 61 73 20 74 68 65 20 65 72 72 6f 72 20 6d r as the error m
37e90 65 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20 73 essage..** The s
37ea0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
37eb0 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ror() and sqlite
37ec0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
37ed0 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 6d ().** routines m
37ee0 61 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f ake a private co
37ef0 70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 py of the error
37f00 6d 65 73 73 61 67 65 20 74 65 78 74 20 62 65 66 message text bef
37f10 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74 75 ore.** they retu
37f20 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 rn. Hence, the
37f30 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
37f40 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65 20 can deallocate
37f50 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 65 or.** modify the
37f60 20 74 65 78 74 20 61 66 74 65 72 20 74 68 65 79 text after they
37f70 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 return without
37f80 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c harm..** The sql
37f90 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
37fa0 72 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69 6f r_code() functio
37fb0 6e 20 63 68 61 6e 67 65 73 20 74 68 65 20 65 72 n changes the er
37fc0 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75 ror code.** retu
37fd0 72 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 rned by SQLite a
37fe0 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e s a result of an
37ff0 20 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e 63 error in a func
38000 74 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75 6c tion. By defaul
38010 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 t,.** the error
38020 63 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45 code is SQLITE_E
38030 52 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71 75 RROR. A subsequ
38040 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ent call to sqli
38050 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
38060 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 ().** or sqlite3
38070 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
38080 29 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72 ) resets the err
38090 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 or code to SQLIT
380a0 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 E_ERROR..**.** T
380b0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
380c0 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65 72 t_toobig() inter
380d0 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c 69 face causes SQLi
380e0 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 te to throw an e
380f0 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69 rror.** indicati
38100 6e 67 20 74 68 61 74 20 61 20 73 74 72 69 6e 67 ng that a string
38110 20 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20 6c or BLOB is to l
38120 6f 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e 74 ong to represent
38130 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
38140 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d te3_result_nomem
38150 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 () interface cau
38160 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 ses SQLite to th
38170 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 row an error.**
38180 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 indicating that
38190 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
381a0 69 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a ion failed..**.*
381b0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
381c0 73 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72 sult_int() inter
381d0 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 face sets the re
381e0 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 turn value.** of
381f0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
38200 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
38210 6e 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62 n to be the 32-b
38220 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 it signed intege
38230 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e r.** value given
38240 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 in the 2nd argu
38250 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71 6c ment..** The sql
38260 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 ite3_result_int6
38270 34 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 4() interface se
38280 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ts the return va
38290 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 lue.** of the ap
382a0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
382b0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
382c0 20 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e the 64-bit sign
382d0 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 ed integer.** va
382e0 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 lue given in the
382f0 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2nd argument..*
38300 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
38310 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 69 _result_null() i
38320 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 nterface sets th
38330 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
38340 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 * of the applica
38350 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
38360 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c 4c ction to be NULL
38370 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
38380 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
38390 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c ), sqlite3_resul
383a0 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 73 t_text16(),.** s
383b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
383c0 78 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73 71 xt16le(), and sq
383d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
383e0 74 31 36 62 65 28 29 20 69 6e 74 65 72 66 61 63 t16be() interfac
383f0 65 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72 65 es.** set the re
38400 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 turn value of th
38410 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
38420 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
38430 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20 73 o be.** a text s
38440 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 72 tring which is r
38450 65 70 72 65 73 65 6e 74 65 64 20 61 73 20 55 54 epresented as UT
38460 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74 69 F-8, UTF-16 nati
38470 76 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a 2a ve byte order,.*
38480 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 * UTF-16 little
38490 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 endian, or UTF-1
384a0 36 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72 65 6 big endian, re
384b0 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 53 spectively..** S
384c0 51 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65 20 QLite takes the
384d0 74 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f 6d text result from
384e0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
384f0 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e 64 from.** the 2nd
38500 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 parameter of th
38510 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
38520 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 _text* interface
38530 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 s..** If the 3rd
38540 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
38550 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
38560 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 _text* interface
38570 73 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76 65 s.** is negative
38580 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 , then SQLite ta
38590 6b 65 73 20 72 65 73 75 6c 74 20 74 65 78 74 20 kes result text
385a0 66 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 from the 2nd par
385b0 61 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75 67 ameter.** throug
385c0 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f h the first zero
385d0 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 character..** I
385e0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 f the 3rd parame
385f0 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 ter to the sqlit
38600 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 e3_result_text*
38610 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73 interfaces.** is
38620 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 non-negative, t
38630 68 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74 65 hen as many byte
38640 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 72 s (not character
38650 73 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a 2a s) of the text.*
38660 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 * pointed to by
38670 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
38680 72 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20 74 r are taken as t
38690 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
386a0 65 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74 69 efined.** functi
386b0 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 on result..** If
386c0 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 the 4th paramet
386d0 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 er to the sqlite
386e0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 3_result_text* i
386f0 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 nterfaces.** or
38700 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 sqlite3_result_b
38710 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c lob is a non-NUL
38720 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 L pointer, then
38730 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 61 SQLite calls tha
38740 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73 t.** function as
38750 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
38760 6f 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 on the text or B
38770 4c 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e 20 LOB result when
38780 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68 it has.** finish
38790 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65 ed using that re
387a0 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 sult..** If the
387b0 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 4th parameter to
387c0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 the sqlite3_res
387d0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 ult_text* interf
387e0 61 63 65 73 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 aces or.** sqlit
387f0 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 e3_result_blob i
38800 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f s the special co
38810 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 nstant SQLITE_ST
38820 41 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 ATIC, then SQLit
38830 65 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 e.** assumes tha
38840 74 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c t the text or BL
38850 4f 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 OB result is in
38860 63 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 constant space a
38870 6e 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 nd does not.** c
38880 6f 70 79 20 74 68 65 20 69 74 20 6f 72 20 63 61 opy the it or ca
38890 6c 6c 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 ll a destructor
388a0 77 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 when it has fini
388b0 73 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 shed using that
388c0 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 result..** If th
388d0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 4th parameter
388e0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 to the sqlite3_r
388f0 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 esult_text* inte
38900 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c rfaces.** or sql
38910 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
38920 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 is the special
38930 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f constant SQLITE_
38940 54 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 TRANSIENT.** the
38950 6e 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 n SQLite makes a
38960 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 copy of the res
38970 75 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f ult into space o
38980 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
38990 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 from [sqlite3_ma
389a0 6c 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 lloc()] before i
389b0 74 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a t returns..**.**
389c0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
389d0 75 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 ult_value() inte
389e0 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 rface sets the r
389f0 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 esult of.** the
38a00 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
38a10 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 ned function to
38a20 62 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a be a copy the.**
38a30 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 [unprotected sq
38a40 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
38a50 65 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 ect specified by
38a60 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 the 2nd paramet
38a70 65 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 er. The.** sqli
38a80 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
38a90 28 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b () interface mak
38aa0 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 es a copy of the
38ab0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d [sqlite3_value]
38ac0 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 .** so that the
38ad0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 [sqlite3_value]
38ae0 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 specified in the
38af0 20 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63 parameter may c
38b00 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 hange or.** be d
38b10 65 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72 eallocated after
38b20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
38b30 76 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20 value() returns
38b40 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a without harm..**
38b50 20 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 A [protected sq
38b60 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
38b70 65 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 ect may always b
38b80 65 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a e used where an.
38b90 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 ** [unprotected
38ba0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
38bb0 62 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65 bject is require
38bc0 64 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 d, so either.**
38bd0 6b 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 kind of [sqlite3
38be0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 _value] object c
38bf0 61 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 an be used with
38c00 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a this interface..
38c10 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 **.** If these r
38c20 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c outines are call
38c30 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 ed from within t
38c40 68 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 he different thr
38c50 65 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 ead.** than the
38c60 6f 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 one containing t
38c70 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
38c80 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
38c90 74 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a that received.**
38ca0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f the [sqlite3_co
38cb0 6e 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 ntext] pointer,
38cc0 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 the results are
38cd0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
38ce0 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
38cf0 2a 2a 20 7b 48 31 36 34 30 33 7d 20 54 68 65 20 ** {H16403} The
38d00 64 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20 76 default return v
38d10 61 6c 75 65 20 66 72 6f 6d 20 61 6e 79 20 53 51 alue from any SQ
38d20 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 L function is NU
38d30 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 30 LL..**.** {H1640
38d40 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6} The [sqlite3_
38d50 72 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c result_blob(C,V,
38d60 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 N,D)] interface
38d70 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 changes the.**
38d80 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 return v
38d90 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e alue of function
38da0 20 43 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 C to be a BLOB
38db0 74 68 61 74 20 69 73 20 4e 20 62 79 74 65 73 0a that is N bytes.
38dc0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 6c ** in l
38dd0 65 6e 67 74 68 20 61 6e 64 20 77 69 74 68 20 63 ength and with c
38de0 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65 64 20 74 ontent pointed t
38df0 6f 20 62 79 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 o by V..**.** {H
38e00 31 36 34 30 39 7d 20 54 68 65 20 5b 73 71 6c 69 16409} The [sqli
38e10 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c te3_result_doubl
38e20 65 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 61 63 e(C,V)] interfac
38e30 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a e changes the.**
38e40 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
38e50 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 value of functi
38e60 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 66 on C to be the f
38e70 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
38e80 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 lue V..**.** {H1
38e90 36 34 31 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 6412} The [sqlit
38ea0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
38eb0 43 2c 56 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 C,V,N)] interfac
38ec0 65 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 e changes the re
38ed0 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 turn.**
38ee0 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 value of functi
38ef0 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 on C to be an ex
38f00 63 65 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 ception with err
38f10 6f 72 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 or code.**
38f20 20 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f [SQLITE_ERRO
38f30 52 5d 20 61 6e 64 20 61 20 55 54 46 2d 38 20 65 R] and a UTF-8 e
38f40 72 72 6f 72 20 6d 65 73 73 61 67 65 20 63 6f 70 rror message cop
38f50 69 65 64 20 66 72 6f 6d 20 56 20 75 70 20 74 6f ied from V up to
38f60 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
38f70 20 66 69 72 73 74 20 7a 65 72 6f 20 62 79 74 65 first zero byte
38f80 20 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65 or until N byte
38f90 73 20 61 72 65 20 72 65 61 64 20 69 66 20 4e 20 s are read if N
38fa0 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a is positive..**.
38fb0 2a 2a 20 7b 48 31 36 34 31 35 7d 20 54 68 65 20 ** {H16415} The
38fc0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
38fd0 65 72 72 6f 72 31 36 28 43 2c 56 2c 4e 29 5d 20 error16(C,V,N)]
38fe0 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 interface change
38ff0 73 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 s the return.**
39000 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f value o
39010 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 f function C to
39020 62 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 be an exception
39030 77 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65 0a with error code.
39040 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c ** [SQL
39050 49 54 45 5f 45 52 52 4f 52 5d 20 61 6e 64 20 61 ITE_ERROR] and a
39060 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 UTF-16 native b
39070 79 74 65 20 6f 72 64 65 72 20 65 72 72 6f 72 20 yte order error
39080 6d 65 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 message.**
39090 20 20 20 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 copied from
390a0 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 V up to the firs
390b0 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f t zero terminato
390c0 72 20 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 r or until N byt
390d0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 es.** a
390e0 72 65 20 72 65 61 64 20 69 66 20 4e 20 69 73 20 re read if N is
390f0 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 positive..**.**
39100 7b 48 31 36 34 31 38 7d 20 54 68 65 20 5b 73 71 {H16418} The [sq
39110 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
39120 6f 72 5f 74 6f 6f 62 69 67 28 43 29 5d 20 69 6e or_toobig(C)] in
39130 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 terface changes
39140 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 the return.**
39150 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 value of
39160 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 the function C t
39170 6f 20 62 65 20 61 6e 20 65 78 63 65 70 74 69 6f o be an exceptio
39180 6e 20 77 69 74 68 20 65 72 72 6f 72 20 63 6f 64 n with error cod
39190 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 e.** [S
391a0 51 4c 49 54 45 5f 54 4f 4f 42 49 47 5d 20 61 6e QLITE_TOOBIG] an
391b0 64 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 d an appropriate
391c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a error message..
391d0 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32 31 7d 20 54 **.** {H16421} T
391e0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 he [sqlite3_resu
391f0 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 43 lt_error_nomem(C
39200 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 )] interface cha
39210 6e 67 65 73 20 74 68 65 20 72 65 74 75 72 6e 0a nges the return.
39220 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 ** valu
39230 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f e of the functio
39240 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63 n C to be an exc
39250 65 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f eption with erro
39260 72 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20 r code.**
39270 20 20 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d [SQLITE_NOMEM
39280 5d 20 61 6e 64 20 61 6e 20 61 70 70 72 6f 70 72 ] and an appropr
39290 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 iate error messa
392a0 67 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32 ge..**.** {H1642
392b0 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 4} The [sqlite3_
392c0 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 result_error_cod
392d0 65 28 43 2c 45 29 5d 20 69 6e 74 65 72 66 61 63 e(C,E)] interfac
392e0 65 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 e changes the re
392f0 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 turn.**
39300 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 75 value of the fu
39310 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 61 nction C to be a
39320 6e 20 65 78 63 65 70 74 69 6f 6e 20 77 69 74 68 n exception with
39330 20 65 72 72 6f 72 20 63 6f 64 65 20 45 2e 0a 2a error code E..*
39340 2a 20 20 20 20 20 20 20 20 20 20 54 68 65 20 65 * The e
39350 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 rror message tex
39360 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a t is unchanged..
39370 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32 37 7d 20 54 **.** {H16427} T
39380 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 he [sqlite3_resu
39390 6c 74 5f 69 6e 74 28 43 2c 56 29 5d 20 69 6e 74 lt_int(C,V)] int
393a0 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 erface changes t
393b0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 he.** r
393c0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 eturn value of f
393d0 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 unction C to be
393e0 74 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65 67 the 32-bit integ
393f0 65 72 20 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a er value V..**.*
39400 2a 20 7b 48 31 36 34 33 30 7d 20 54 68 65 20 5b * {H16430} The [
39410 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
39420 6e 74 36 34 28 43 2c 56 29 5d 20 69 6e 74 65 72 nt64(C,V)] inter
39430 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 face changes the
39440 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 .** ret
39450 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e urn value of fun
39460 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 ction C to be th
39470 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 e 64-bit integer
39480 20 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 value V..**.**
39490 7b 48 31 36 34 33 33 7d 20 54 68 65 20 5b 73 71 {H16433} The [sq
394a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c lite3_result_nul
394b0 6c 28 43 29 5d 20 69 6e 74 65 72 66 61 63 65 20 l(C)] interface
394c0 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 changes the.**
394d0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 return v
394e0 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e alue of function
394f0 20 43 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a C to be NULL..*
39500 2a 0a 2a 2a 20 7b 48 31 36 34 33 36 7d 20 54 68 *.** {H16436} Th
39510 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c e [sqlite3_resul
39520 74 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d t_text(C,V,N,D)]
39530 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 interface chang
39540 65 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 es the.**
39550 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 return value
39560 6f 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f of function C to
39570 20 62 65 20 74 68 65 20 55 54 46 2d 38 20 73 74 be the UTF-8 st
39580 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 ring.**
39590 20 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 V up to the fir
395a0 73 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20 st zero if N is
395b0 6e 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20 negative.**
395c0 20 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73 or the firs
395d0 74 20 4e 20 62 79 74 65 73 20 6f 66 20 56 20 69 t N bytes of V i
395e0 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 f N is non-negat
395f0 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 ive..**.** {H164
39600 33 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 39} The [sqlite3
39610 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43 _result_text16(C
39620 2c 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 ,V,N,D)] interfa
39630 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a ce changes the.*
39640 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 * retur
39650 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 n value of funct
39660 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 ion C to be the
39670 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 UTF-16 native by
39680 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 20 te order.**
39690 20 20 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 string V up
396a0 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 to the first ze
396b0 72 6f 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 ro if N is negat
396c0 69 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ive.**
396d0 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 or the first N b
396e0 79 74 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 ytes of V if N i
396f0 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a s non-negative..
39700 2a 2a 0a 2a 2a 20 7b 48 31 36 34 34 32 7d 20 54 **.** {H16442} T
39710 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 he [sqlite3_resu
39720 6c 74 5f 74 65 78 74 31 36 62 65 28 43 2c 56 2c lt_text16be(C,V,
39730 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 N,D)] interface
39740 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 changes the.**
39750 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 return v
39760 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e alue of function
39770 20 43 20 74 6f 20 62 65 20 74 68 65 20 55 54 46 C to be the UTF
39780 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a -16 big-endian.*
39790 2a 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e * strin
397a0 67 20 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 g V up to the fi
397b0 72 73 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 rst zero if N is
397c0 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 negative.**
397d0 20 20 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 or the fir
397e0 73 74 20 4e 20 62 79 74 65 73 20 6f 72 20 56 20 st N bytes or V
397f0 69 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 if N is non-nega
39800 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 tive..**.** {H16
39810 34 34 35 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 445} The [sqlite
39820 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 3_result_text16l
39830 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 e(C,V,N,D)] inte
39840 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 rface changes th
39850 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 e.** re
39860 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 turn value of fu
39870 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 nction C to be t
39880 68 65 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 he UTF-16 little
39890 2d 65 6e 64 69 61 6e 0a 2a 2a 20 20 20 20 20 20 -endian.**
398a0 20 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20 string V up
398b0 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 to the first zer
398c0 6f 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69 o if N is negati
398d0 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f ve.** o
398e0 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 r the first N by
398f0 74 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73 tes of V if N is
39900 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a non-negative..*
39910 2a 0a 2a 2a 20 7b 48 31 36 34 34 38 7d 20 54 68 *.** {H16448} Th
39920 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c e [sqlite3_resul
39930 74 5f 76 61 6c 75 65 28 43 2c 56 29 5d 20 69 6e t_value(C,V)] in
39940 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 terface changes
39950 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
39960 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 return value of
39970 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 function C to be
39980 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 the [unprotecte
39990 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
399a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a .** obj
399b0 65 63 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ect V..**.** {H1
399c0 36 34 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 6451} The [sqlit
399d0 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c e3_result_zerobl
399e0 6f 62 28 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 ob(C,N)] interfa
399f0 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a ce changes the.*
39a00 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 * retur
39a10 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 n value of funct
39a20 69 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 4e ion C to be an N
39a30 2d 62 79 74 65 20 42 4c 4f 42 20 6f 66 20 61 6c -byte BLOB of al
39a40 6c 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 7b l zeros..**.** {
39a50 48 31 36 34 35 34 7d 20 54 68 65 20 5b 73 71 6c H16454} The [sql
39a60 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
39a70 72 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 r()] and [sqlite
39a80 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
39a90 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ()].**
39aa0 69 6e 74 65 72 66 61 63 65 73 20 6d 61 6b 65 20 interfaces make
39ab0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 69 72 20 a copy of their
39ac0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 error message st
39ad0 72 69 6e 67 73 20 62 65 66 6f 72 65 0a 2a 2a 20 rings before.**
39ae0 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 69 returni
39af0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 35 ng..**.** {H1645
39b00 37 7d 20 49 66 20 74 68 65 20 44 20 64 65 73 74 7} If the D dest
39b10 72 75 63 74 6f 72 20 70 61 72 61 6d 65 74 65 72 ructor parameter
39b20 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 to [sqlite3_res
39b30 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e 2c 44 ult_blob(C,V,N,D
39b40 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 )],.**
39b50 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
39b60 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 2c 20 text(C,V,N,D)],
39b70 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f [sqlite3_result_
39b80 74 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44 29 5d text16(C,V,N,D)]
39b90 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 ,.** [s
39ba0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
39bb0 78 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44 29 5d xt16be(C,V,N,D)]
39bc0 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 , or.**
39bd0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
39be0 5f 74 65 78 74 31 36 6c 65 28 43 2c 56 2c 4e 2c _text16le(C,V,N,
39bf0 44 29 5d 20 69 73 20 74 68 65 20 63 6f 6e 73 74 D)] is the const
39c00 61 6e 74 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 ant [SQLITE_STAT
39c10 49 43 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 IC].**
39c20 74 68 65 6e 20 6e 6f 20 64 65 73 74 72 75 63 74 then no destruct
39c30 6f 72 20 69 73 20 65 76 65 72 20 63 61 6c 6c 65 or is ever calle
39c40 64 20 6f 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 d on the pointer
39c50 20 56 20 61 6e 64 20 53 51 4c 69 74 65 0a 2a 2a V and SQLite.**
39c60 20 20 20 20 20 20 20 20 20 20 61 73 73 75 6d 65 assume
39c70 73 20 74 68 61 74 20 56 20 69 73 20 69 6d 6d 75 s that V is immu
39c80 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 table..**.** {H1
39c90 36 34 36 30 7d 20 49 66 20 74 68 65 20 44 20 64 6460} If the D d
39ca0 65 73 74 72 75 63 74 6f 72 20 70 61 72 61 6d 65 estructor parame
39cb0 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ter to [sqlite3_
39cc0 72 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c result_blob(C,V,
39cd0 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 N,D)],.**
39ce0 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 [sqlite3_resu
39cf0 6c 74 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 lt_text(C,V,N,D)
39d00 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 ], [sqlite3_resu
39d10 6c 74 5f 74 65 78 74 31 36 28 43 2c 56 2c 4e 2c lt_text16(C,V,N,
39d20 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 D)],.**
39d30 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
39d40 5f 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e 2c _text16be(C,V,N,
39d50 44 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 D)], or.**
39d60 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 [sqlite3_res
39d70 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 43 2c 56 ult_text16le(C,V
39d80 2c 4e 2c 44 29 5d 20 69 73 20 74 68 65 20 63 6f ,N,D)] is the co
39d90 6e 73 74 61 6e 74 0a 2a 2a 20 20 20 20 20 20 20 nstant.**
39da0 20 20 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 [SQLITE_TRANS
39db0 49 45 4e 54 5d 20 74 68 65 6e 20 74 68 65 20 69 IENT] then the i
39dc0 6e 74 65 72 66 61 63 65 73 20 6d 61 6b 65 73 20 nterfaces makes
39dd0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a a copy of the.**
39de0 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e conten
39df0 74 20 6f 66 20 56 20 61 6e 64 20 72 65 74 61 69 t of V and retai
39e00 6e 73 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 0a ns the copy..**.
39e10 2a 2a 20 7b 48 31 36 34 36 33 7d 20 49 66 20 74 ** {H16463} If t
39e20 68 65 20 44 20 64 65 73 74 72 75 63 74 6f 72 20 he D destructor
39e30 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 parameter to [sq
39e40 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f lite3_result_blo
39e50 62 28 43 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 b(C,V,N,D)],.**
39e60 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
39e70 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 43 2c 3_result_text(C,
39e80 56 2c 4e 2c 44 29 5d 2c 20 5b 73 71 6c 69 74 65 V,N,D)], [sqlite
39e90 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 3_result_text16(
39ea0 43 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 C,V,N,D)],.**
39eb0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
39ec0 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 result_text16be(
39ed0 43 2c 56 2c 4e 2c 44 29 5d 2c 20 6f 72 0a 2a 2a C,V,N,D)], or.**
39ee0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
39ef0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 e3_result_text16
39f00 6c 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 73 20 le(C,V,N,D)] is
39f10 73 6f 6d 65 20 76 61 6c 75 65 20 6f 74 68 65 72 some value other
39f20 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 than.**
39f30 20 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 the constants
39f40 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 20 [SQLITE_STATIC]
39f50 61 6e 64 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e and [SQLITE_TRAN
39f60 53 49 45 4e 54 5d 20 74 68 65 6e 0a 2a 2a 20 20 SIENT] then.**
39f70 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 77 SQLite w
39f80 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 ill invoke the d
39f90 65 73 74 72 75 63 74 6f 72 20 44 20 77 69 74 68 estructor D with
39fa0 20 56 20 61 73 20 69 74 73 20 6f 6e 6c 79 20 61 V as its only a
39fb0 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 rgument.**
39fc0 20 20 20 20 77 68 65 6e 20 69 74 20 68 61 73 20 when it has
39fd0 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 74 68 finished with th
39fe0 65 20 56 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 e V value..*/.SQ
39ff0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
3a000 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f lite3_result_blo
3a010 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 b(sqlite3_contex
3a020 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c t*, const void*,
3a030 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f int, void(*)(vo
3a040 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 id*));.SQLITE_AP
3a050 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
3a060 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c esult_double(sql
3a070 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 ite3_context*, d
3a080 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 ouble);.SQLITE_A
3a090 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
3a0a0 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c result_error(sql
3a0b0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 ite3_context*, c
3a0c0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 onst char*, int)
3a0d0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
3a0e0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
3a0f0 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 _error16(sqlite3
3a100 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 _context*, const
3a110 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 void*, int);.SQ
3a120 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
3a130 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
3a140 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 or_toobig(sqlite
3a150 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 3_context*);.SQL
3a160 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
3a170 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
3a180 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f r_nomem(sqlite3_
3a190 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 context*);.SQLIT
3a1a0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
3a1b0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f e3_result_error_
3a1c0 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e code(sqlite3_con
3a1d0 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c text*, int);.SQL
3a1e0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
3a1f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 ite3_result_int(
3a200 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
3a210 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 , int);.SQLITE_A
3a220 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
3a230 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c result_int64(sql
3a240 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 ite3_context*, s
3a250 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 qlite3_int64);.S
3a260 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
3a270 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 qlite3_result_nu
3a280 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ll(sqlite3_conte
3a290 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 xt*);.SQLITE_API
3a2a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
3a2b0 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 sult_text(sqlite
3a2c0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 3_context*, cons
3a2d0 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f t char*, int, vo
3a2e0 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 id(*)(void*));.S
3a2f0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
3a300 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
3a310 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e xt16(sqlite3_con
3a320 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 text*, const voi
3a330 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 d*, int, void(*)
3a340 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 (void*));.SQLITE
3a350 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
3a360 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 3_result_text16l
3a370 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 e(sqlite3_contex
3a380 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c t*, const void*,
3a390 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 int,void(*)(voi
3a3a0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
3a3b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
3a3c0 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 sult_text16be(sq
3a3d0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
3a3e0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 const void*, int
3a3f0 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 ,void(*)(void*))
3a400 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
3a410 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
3a420 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 _value(sqlite3_c
3a430 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 ontext*, sqlite3
3a440 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
3a450 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
3a460 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 3_result_zeroblo
3a470 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 b(sqlite3_contex
3a480 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a t*, int n);../*.
3a490 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 ** CAPI3REF: Def
3a4a0 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e ine New Collatin
3a4b0 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 g Sequences {H16
3a4c0 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 600} <S20300>.**
3a4d0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 .** These functi
3a4e0 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 ons are used to
3a4f0 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f add new collatio
3a500 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 n sequences to t
3a510 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 he.** [database
3a520 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 connection] spec
3a530 69 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 ified as the fir
3a540 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a st argument..**.
3a550 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 ** The name of t
3a560 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e he new collation
3a570 20 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 sequence is spe
3a580 63 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d cified as a UTF-
3a590 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 8 string.** for
3a5a0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
3a5b0 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 ollation() and s
3a5c0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
3a5d0 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 llation_v2().**
3a5e0 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 and a UTF-16 str
3a5f0 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f ing for sqlite3_
3a600 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
3a610 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 16(). In all cas
3a620 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 es.** the name i
3a630 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 s passed as the
3a640 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 second function
3a650 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
3a660 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 The third argume
3a670 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 nt may be one of
3a680 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b the constants [
3a690 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a SQLITE_UTF8],.**
3a6a0 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 [SQLITE_UTF16LE
3a6b0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46 ] or [SQLITE_UTF
3a6c0 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e 16BE], indicatin
3a6d0 67 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d g that the user-
3a6e0 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74 supplied.** rout
3a6f0 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62 ine expects to b
3a700 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72 e passed pointer
3a710 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63 s to strings enc
3a720 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38 oded using UTF-8
3a730 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 ,.** UTF-16 litt
3a740 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 le-endian, or UT
3a750 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c F-16 big-endian,
3a760 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 respectively. T
3a770 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 he.** third argu
3a780 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20 ment might also
3a790 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 be [SQLITE_UTF16
3a7a0 5f 41 4c 49 47 4e 45 44 5d 20 74 6f 20 69 6e 64 _ALIGNED] to ind
3a7b0 69 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 74 68 icate that.** th
3a7c0 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 e routine expect
3a7d0 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 s pointers to 16
3a7e0 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 -bit word aligne
3a7f0 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 d strings.** of
3a800 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 UTF-16 in the na
3a810 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 tive byte order
3a820 6f 66 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 of the host comp
3a830 75 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f uter..**.** A po
3a840 69 6e 74 65 72 20 74 6f 20 74 68 65 20 75 73 65 inter to the use
3a850 72 20 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 r supplied routi
3a860 6e 65 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 ne must be passe
3a870 64 20 61 73 20 74 68 65 20 66 69 66 74 68 0a 2a d as the fifth.*
3a880 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 * argument. If
3a890 69 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 it is NULL, this
3a8a0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
3a8b0 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6c deleting the col
3a8c0 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e lation.** sequen
3a8d0 63 65 20 28 73 6f 20 74 68 61 74 20 53 51 4c 69 ce (so that SQLi
3a8e0 74 65 20 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 te cannot call i
3a8f0 74 20 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 t anymore)..** E
3a900 61 63 68 20 74 69 6d 65 20 74 68 65 20 61 70 70 ach time the app
3a910 6c 69 63 61 74 69 6f 6e 20 73 75 70 70 6c 69 65 lication supplie
3a920 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e d function is in
3a930 76 6f 6b 65 64 2c 20 69 74 20 69 73 20 70 61 73 voked, it is pas
3a940 73 65 64 0a 2a 2a 20 61 73 20 69 74 73 20 66 69 sed.** as its fi
3a950 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 61 20 rst parameter a
3a960 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 copy of the void
3a970 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 * passed as the
3a980 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 0a fourth argument.
3a990 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 ** to sqlite3_cr
3a9a0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 eate_collation()
3a9b0 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 or sqlite3_crea
3a9c0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 te_collation16()
3a9d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 ..**.** The rema
3a9e0 69 6e 69 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 ining arguments
3a9f0 74 6f 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 to the applicati
3aa00 6f 6e 2d 73 75 70 70 6c 69 65 64 20 72 6f 75 74 on-supplied rout
3aa10 69 6e 65 20 61 72 65 20 74 77 6f 20 73 74 72 69 ine are two stri
3aa20 6e 67 73 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 ngs,.** each rep
3aa30 72 65 73 65 6e 74 65 64 20 62 79 20 61 20 28 6c resented by a (l
3aa40 65 6e 67 74 68 2c 20 64 61 74 61 29 20 70 61 69 ength, data) pai
3aa50 72 20 61 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e r and encoded in
3aa60 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a the encoding.**
3aa70 20 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 that was passed
3aa80 20 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72 as the third ar
3aa90 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 gument when the
3aaa0 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
3aab0 63 65 20 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 ce was.** regist
3aac0 65 72 65 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65 ered. {END} The
3aad0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 application def
3aae0 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 ined collation r
3aaf0 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a outine should.**
3ab00 20 72 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 return negative
3ab10 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 , zero or positi
3ab20 76 65 20 69 66 20 74 68 65 20 66 69 72 73 74 20 ve if the first
3ab30 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 string is less t
3ab40 68 61 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f han,.** equal to
3ab50 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 , or greater tha
3ab60 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 73 74 72 n the second str
3ab70 69 6e 67 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e ing. i.e. (STRIN
3ab80 47 31 20 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a G1 - STRING2)..*
3ab90 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
3aba0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
3abb0 6e 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b n_v2() works lik
3abc0 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 e sqlite3_create
3abd0 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 _collation().**
3abe0 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 74 except that it t
3abf0 61 6b 65 73 20 61 6e 20 65 78 74 72 61 20 61 72 akes an extra ar
3ac00 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 gument which is
3ac10 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 a destructor for
3ac20 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f .** the collatio
3ac30 6e 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74 n. The destruct
3ac40 6f 72 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 or is called whe
3ac50 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 n the collation
3ac60 69 73 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 is.** destroyed
3ac70 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 20 and is passed a
3ac80 63 6f 70 79 20 6f 66 20 74 68 65 20 66 6f 75 72 copy of the four
3ac90 74 68 20 70 61 72 61 6d 65 74 65 72 20 76 6f 69 th parameter voi
3aca0 64 2a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 d* pointer.** of
3acb0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 the sqlite3_cre
3acc0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 ate_collation_v2
3acd0 28 29 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e ()..** Collation
3ace0 73 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 s are destroyed
3acf0 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6f 76 when they are ov
3ad00 65 72 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65 erridden by late
3ad10 72 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a r calls to the.*
3ad20 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 * collation crea
3ad30 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f tion functions o
3ad40 72 20 77 68 65 6e 20 74 68 65 20 5b 64 61 74 61 r when the [data
3ad50 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
3ad60 20 69 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 is closed.** us
3ad70 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f ing [sqlite3_clo
3ad80 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 se()]..**.** INV
3ad90 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
3ada0 48 31 36 36 30 33 7d 20 41 20 73 75 63 63 65 73 H16603} A succes
3adb0 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 74 68 65 sful call to the
3adc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
3add0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
3ade0 6c 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c lation_v2(B,X,E,
3adf0 50 2c 46 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 P,F,D)] interfac
3ae00 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 e.** re
3ae10 67 69 73 74 65 72 73 20 66 75 6e 63 74 69 6f 6e gisters function
3ae20 20 46 20 61 73 20 74 68 65 20 63 6f 6d 70 61 72 F as the compar
3ae30 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 ison function us
3ae40 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 ed to.**
3ae50 20 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 6c 6c implement coll
3ae60 61 74 69 6f 6e 20 58 20 6f 6e 20 74 68 65 20 5b ation X on the [
3ae70 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3ae80 69 6f 6e 5d 20 42 20 66 6f 72 0a 2a 2a 20 20 20 ion] B for.**
3ae90 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 73 databases
3aea0 20 68 61 76 69 6e 67 20 65 6e 63 6f 64 69 6e 67 having encoding
3aeb0 20 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30 E..**.** {H1660
3aec0 34 7d 20 53 51 4c 69 74 65 20 75 6e 64 65 72 73 4} SQLite unders
3aed0 74 61 6e 64 73 20 74 68 65 20 58 20 70 61 72 61 tands the X para
3aee0 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 meter to.**
3aef0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 72 [sqlite3_cr
3af00 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 eate_collation_v
3af10 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 20 2(B,X,E,P,F,D)]
3af20 61 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e as a zero-termin
3af30 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 ated.**
3af40 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 6e UTF-8 string in
3af50 20 77 68 69 63 68 20 63 61 73 65 20 69 73 20 69 which case is i
3af60 67 6e 6f 72 65 64 20 66 6f 72 20 41 53 43 49 49 gnored for ASCII
3af70 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a characters and.
3af80 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 73 ** is s
3af90 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 20 6e ignificant for n
3afa0 6f 6e 2d 41 53 43 49 49 20 63 68 61 72 61 63 74 on-ASCII charact
3afb0 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 ers..**.** {H166
3afc0 30 36 7d 20 53 75 63 63 65 73 73 69 76 65 20 63 06} Successive c
3afd0 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
3afe0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
3aff0 6e 5f 76 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 n_v2(B,X,E,P,F,D
3b000 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 )].** w
3b010 69 74 68 20 74 68 65 20 73 61 6d 65 20 76 61 6c ith the same val
3b020 75 65 73 20 66 6f 72 20 42 2c 20 58 2c 20 61 6e ues for B, X, an
3b030 64 20 45 2c 20 6f 76 65 72 72 69 64 65 20 70 72 d E, override pr
3b040 69 6f 72 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 ior values.**
3b050 20 20 20 20 20 20 20 6f 66 20 50 2c 20 46 2c 20 of P, F,
3b060 61 6e 64 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 and D..**.** {H1
3b070 36 36 30 39 7d 20 49 66 20 74 68 65 20 64 65 73 6609} If the des
3b080 74 72 75 63 74 6f 72 20 44 20 69 6e 20 5b 73 71 tructor D in [sq
3b090 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
3b0a0 6c 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c lation_v2(B,X,E,
3b0b0 50 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20 20 20 20 P,F,D)].**
3b0c0 20 20 20 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 is not NULL
3b0d0 74 68 65 6e 20 69 74 20 69 73 20 63 61 6c 6c 65 then it is calle
3b0e0 64 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 d with argument
3b0f0 50 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 P when the.**
3b100 20 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67 collating
3b110 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f function is dro
3b120 70 70 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a pped by SQLite..
3b130 2a 2a 0a 2a 2a 20 7b 48 31 36 36 31 32 7d 20 41 **.** {H16612} A
3b140 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 collating funct
3b150 69 6f 6e 20 69 73 20 64 72 6f 70 70 65 64 20 77 ion is dropped w
3b160 68 65 6e 20 69 74 20 69 73 20 6f 76 65 72 6c 6f hen it is overlo
3b170 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 aded..**.** {H16
3b180 36 31 35 7d 20 41 20 63 6f 6c 6c 61 74 69 6e 67 615} A collating
3b190 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f function is dro
3b1a0 70 70 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 pped when the da
3b1b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3b1c0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 n.** is
3b1d0 20 63 6c 6f 73 65 64 20 75 73 69 6e 67 20 5b 73 closed using [s
3b1e0 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e qlite3_close()].
3b1f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 31 38 7d 20 .**.** {H16618}
3b200 54 68 65 20 70 6f 69 6e 74 65 72 20 50 20 69 6e The pointer P in
3b210 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
3b220 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c _collation_v2(B,
3b230 58 2c 45 2c 50 2c 46 2c 44 29 5d 0a 2a 2a 20 20 X,E,P,F,D)].**
3b240 20 20 20 20 20 20 20 20 69 73 20 70 61 73 73 65 is passe
3b250 64 20 74 68 72 6f 75 67 68 20 61 73 20 74 68 65 d through as the
3b260 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
3b270 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 to the comparis
3b280 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 on.** f
3b290 75 6e 63 74 69 6f 6e 20 46 20 66 6f 72 20 61 6c unction F for al
3b2a0 6c 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 l subsequent inv
3b2b0 6f 63 61 74 69 6f 6e 73 20 6f 66 20 46 2e 0a 2a ocations of F..*
3b2c0 2a 0a 2a 2a 20 7b 48 31 36 36 32 31 7d 20 41 20 *.** {H16621} A
3b2d0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
3b2e0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
3b2f0 6e 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 69 73 n(B,X,E,P,F)] is
3b300 20 65 78 61 63 74 6c 79 0a 2a 2a 20 20 20 20 20 exactly.**
3b310 20 20 20 20 20 74 68 65 20 73 61 6d 65 20 61 73 the same as
3b320 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 a call to [sqli
3b330 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
3b340 74 69 6f 6e 5f 76 32 28 29 5d 20 77 69 74 68 0a tion_v2()] with.
3b350 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
3b360 73 61 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 same parameters
3b370 61 6e 64 20 61 20 4e 55 4c 4c 20 64 65 73 74 72 and a NULL destr
3b380 75 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 uctor..**.** {H1
3b390 36 36 32 34 7d 20 46 6f 6c 6c 6f 77 69 6e 67 20 6624} Following
3b3a0 61 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 a [sqlite3_creat
3b3b0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 e_collation_v2(B
3b3c0 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 2c 0a 2a 2a ,X,E,P,F,D)],.**
3b3d0 20 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 SQLite
3b3e0 20 75 73 65 73 20 74 68 65 20 63 6f 6d 70 61 72 uses the compar
3b3f0 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 46 20 ison function F
3b400 66 6f 72 20 61 6c 6c 20 74 65 78 74 20 63 6f 6d for all text com
3b410 70 61 72 69 73 6f 6e 0a 2a 2a 20 20 20 20 20 20 parison.**
3b420 20 20 20 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f operations o
3b430 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 n the [database
3b440 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 42 20 6f 6e connection] B on
3b450 20 74 65 78 74 20 76 61 6c 75 65 73 20 74 68 61 text values tha
3b460 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73 t.** us
3b470 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 e the collating
3b480 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 58 sequence named X
3b490 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 32 37 7d ..**.** {H16627}
3b4a0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 The [sqlite3_cr
3b4b0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 eate_collation16
3b4c0 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 77 6f 72 (B,X,E,P,F)] wor
3b4d0 6b 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 ks the same.**
3b4e0 20 20 20 20 20 20 20 20 61 73 20 5b 73 71 6c 69 as [sqli
3b4f0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
3b500 74 69 6f 6e 28 42 2c 58 2c 45 2c 50 2c 46 29 5d tion(B,X,E,P,F)]
3b510 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 except that the
3b520 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6c .** col
3b530 6c 61 74 69 6f 6e 20 6e 61 6d 65 20 58 20 69 73 lation name X is
3b540 20 75 6e 64 65 72 73 74 6f 6f 64 20 61 73 20 55 understood as U
3b550 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 20 TF-16 in native
3b560 62 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 byte order.**
3b570 20 20 20 20 20 20 20 69 6e 73 74 65 61 64 20 6f instead o
3b580 66 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b f UTF-8..**.** {
3b590 48 31 36 36 33 30 7d 20 57 68 65 6e 20 6d 75 6c H16630} When mul
3b5a0 74 69 70 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e tiple comparison
3b5b0 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 61 functions are a
3b5c0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 74 68 65 vailable for the
3b5d0 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 same.**
3b5e0 20 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 collating sequ
3b5f0 65 6e 63 65 2c 20 53 51 4c 69 74 65 20 63 68 6f ence, SQLite cho
3b600 6f 73 65 73 20 74 68 65 20 6f 6e 65 20 77 68 6f oses the one who
3b610 73 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 se text encoding
3b620 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 71 .** req
3b630 75 69 72 65 73 20 74 68 65 20 6c 65 61 73 74 20 uires the least
3b640 61 6d 6f 75 6e 74 20 6f 66 20 63 6f 6e 76 65 72 amount of conver
3b650 73 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 64 65 sion from the de
3b660 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 fault.**
3b670 20 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 text encoding
3b680 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e of the database.
3b690 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
3b6a0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
3b6b0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 e_collation(. s
3b6c0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 qlite3*, . cons
3b6d0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a t char *zName, .
3b6e0 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 int eTextRep,
3b6f0 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 . void*,. int(
3b700 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a *xCompare)(void*
3b710 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a ,int,const void*
3b720 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a ,int,const void*
3b730 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ).);.SQLITE_API
3b740 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 int sqlite3_crea
3b750 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 te_collation_v2(
3b760 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 . sqlite3*, .
3b770 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
3b780 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 e, . int eTextR
3b790 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 ep, . void*,.
3b7a0 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 int(*xCompare)(v
3b7b0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 oid*,int,const v
3b7c0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 oid*,int,const v
3b7d0 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 oid*),. void(*x
3b7e0 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a Destroy)(void*).
3b7f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
3b800 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
3b810 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 _collation16(.
3b820 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e sqlite3*, . con
3b830 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a st void *zName,.
3b840 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 int eTextRep,
3b850 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 . void*,. int(
3b860 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a *xCompare)(void*
3b870 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a ,int,const void*
3b880 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a ,int,const void*
3b890 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ).);../*.** CAPI
3b8a0 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 3REF: Collation
3b8b0 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 Needed Callbacks
3b8c0 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 {H16700} <S2030
3b8d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 0>.**.** To avoi
3b8e0 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 d having to regi
3b8f0 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 ster all collati
3b900 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 on sequences bef
3b910 6f 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a ore a database.*
3b920 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 * can be used, a
3b930 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b single callback
3b940 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 function may be
3b950 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 registered with
3b960 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 the.** [databas
3b970 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f e connection] to
3b980 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 be called whene
3b990 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 ver an undefined
3b9a0 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 collation.** se
3b9b0 71 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 quence is requir
3b9c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
3b9d0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 function is reg
3b9e0 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 istered using th
3b9f0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 e sqlite3_collat
3ba00 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 ion_needed() API
3ba10 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 ,.** then it is
3ba20 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 passed the names
3ba30 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f of undefined co
3ba40 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
3ba50 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 s as strings.**
3ba60 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 encoded in UTF-8
3ba70 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 . {H16703} If sq
3ba80 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f lite3_collation_
3ba90 6e 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 needed16() is us
3baa0 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 ed,.** the names
3bab0 20 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 are passed as U
3bac0 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 TF-16 in machine
3bad0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
3bae0 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f er..** A call to
3baf0 20 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e either function
3bb00 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 replaces any ex
3bb10 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e isting callback.
3bb20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
3bb30 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f callback is invo
3bb40 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 ked, the first a
3bb50 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 rgument passed i
3bb60 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 s a copy.** of t
3bb70 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
3bb80 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f nt to sqlite3_co
3bb90 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 llation_needed()
3bba0 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 or.** sqlite3_c
3bbb0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 ollation_needed1
3bbc0 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 6(). The second
3bbd0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
3bbe0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e database.** con
3bbf0 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 nection. The th
3bc00 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ird argument is
3bc10 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 one of [SQLITE_U
3bc20 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 TF8], [SQLITE_UT
3bc30 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 F16BE],.** or [S
3bc40 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 QLITE_UTF16LE],
3bc50 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d indicating the m
3bc60 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f ost desirable fo
3bc70 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 rm of the collat
3bc80 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ion.** sequence
3bc90 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 function require
3bca0 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 d. The fourth p
3bcb0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 arameter is the
3bcc0 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 name of the.** r
3bcd0 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f equired collatio
3bce0 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a n sequence..**.*
3bcf0 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 * The callback f
3bd00 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 unction should r
3bd10 65 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 egister the desi
3bd20 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 red collation us
3bd30 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ing.** [sqlite3_
3bd40 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
3bd50 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 ()], [sqlite3_cr
3bd60 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 eate_collation16
3bd70 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 ()], or.** [sqli
3bd80 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
3bd90 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a tion_v2()]..**.*
3bda0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
3bdb0 0a 2a 2a 20 7b 48 31 36 37 30 32 7d 20 41 20 73 .** {H16702} A s
3bdc0 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
3bdd0 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 o [sqlite3_colla
3bde0 74 69 6f 6e 5f 6e 65 65 64 65 64 28 44 2c 50 2c tion_needed(D,P,
3bdf0 46 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 F)].**
3be00 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c or [sqlite3_coll
3be10 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 44 ation_needed16(D
3be20 2c 50 2c 46 29 5d 20 63 61 75 73 65 73 0a 2a 2a ,P,F)] causes.**
3be30 20 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 64 the [d
3be40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3be50 6f 6e 5d 20 44 20 74 6f 20 69 6e 76 6f 6b 65 20 on] D to invoke
3be60 63 61 6c 6c 62 61 63 6b 20 46 20 77 69 74 68 20 callback F with
3be70 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 first.**
3be80 20 20 70 61 72 61 6d 65 74 65 72 20 50 20 77 68 parameter P wh
3be90 65 6e 65 76 65 72 20 69 74 20 6e 65 65 64 73 20 enever it needs
3bea0 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e a comparison fun
3beb0 63 74 69 6f 6e 20 66 6f 72 20 61 0a 2a 2a 20 20 ction for a.**
3bec0 20 20 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e collatin
3bed0 67 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 g sequence that
3bee0 69 74 20 64 6f 65 73 20 6e 6f 74 20 6b 6e 6f 77 it does not know
3bef0 20 61 62 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 about..**.** {H
3bf00 31 36 37 30 34 7d 20 45 61 63 68 20 73 75 63 63 16704} Each succ
3bf10 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b essful call to [
3bf20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f sqlite3_collatio
3bf30 6e 5f 6e 65 65 64 65 64 28 29 5d 20 6f 72 0a 2a n_needed()] or.*
3bf40 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
3bf50 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 te3_collation_ne
3bf60 65 64 65 64 31 36 28 29 5d 20 6f 76 65 72 72 69 eded16()] overri
3bf70 64 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b des the callback
3bf80 20 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 registered.**
3bf90 20 20 20 20 20 20 20 20 6f 6e 20 74 68 65 20 73 on the s
3bfa0 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f ame [database co
3bfb0 6e 6e 65 63 74 69 6f 6e 5d 20 62 79 20 70 72 69 nnection] by pri
3bfc0 6f 72 20 63 61 6c 6c 73 20 74 6f 20 65 69 74 68 or calls to eith
3bfd0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 er.** i
3bfe0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
3bff0 7b 48 31 36 37 30 36 7d 20 54 68 65 20 6e 61 6d {H16706} The nam
3c000 65 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 e of the request
3c010 65 64 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e ed collating fun
3c020 63 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 20 ction passed in
3c030 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
3c040 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 4th parameter to
3c050 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 the callback is
3c060 20 69 6e 20 55 54 46 2d 38 20 69 66 20 74 68 65 in UTF-8 if the
3c070 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 callback.**
3c080 20 20 20 20 20 20 77 61 73 20 72 65 67 69 73 74 was regist
3c090 65 72 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 ered using [sqli
3c0a0 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 te3_collation_ne
3c0b0 65 64 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 eded()] and.**
3c0c0 20 20 20 20 20 20 20 20 69 73 20 69 6e 20 55 54 is in UT
3c0d0 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 F-16 native byte
3c0e0 20 6f 72 64 65 72 20 69 66 20 74 68 65 20 63 61 order if the ca
3c0f0 6c 6c 62 61 63 6b 20 77 61 73 0a 2a 2a 20 20 20 llback was.**
3c100 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 registere
3c110 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 d using [sqlite3
3c120 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 _collation_neede
3c130 64 31 36 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 d16()]..*/.SQLIT
3c140 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
3c150 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 3_collation_need
3c160 65 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 ed(. sqlite3*,
3c170 0a 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 . void*, . voi
3c180 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 d(*)(void*,sqlit
3c190 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 e3*,int eTextRep
3c1a0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b ,const char*).);
3c1b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
3c1c0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f sqlite3_collatio
3c1d0 6e 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 n_needed16(. sq
3c1e0 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a lite3*, . void*
3c1f0 2c 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 ,. void(*)(void
3c200 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 *,sqlite3*,int e
3c210 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f TextRep,const vo
3c220 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 id*).);../*.** S
3c230 70 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 pecify the key f
3c240 6f 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 or an encrypted
3c250 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 database. This
3c260 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 routine should b
3c270 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 e.** called righ
3c280 74 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f t after sqlite3_
3c290 6f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 open()..**.** Th
3c2a0 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d e code to implem
3c2b0 65 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 ent this API is
3c2c0 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e not available in
3c2d0 20 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 the public rele
3c2e0 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 ase.** of SQLite
3c2f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3c300 69 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 int sqlite3_key(
3c310 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
3c320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3c330 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f /* Database to
3c340 20 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 be rekeyed */.
3c350 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
3c360 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 y, int nKey
3c370 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b /* The key */.);
3c380 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
3c390 68 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 he key on an ope
3c3a0 6e 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 n database. If
3c3b0 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 the current data
3c3c0 62 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 base is not.** e
3c3d0 6e 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72 ncrypted, this r
3c3e0 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 outine will encr
3c3f0 79 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77 ypt it. If pNew
3c400 3d 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 ==0 or nNew==0,
3c410 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
3c420 69 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a is decrypted..**
3c430 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 .** The code to
3c440 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 implement this A
3c450 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 PI is not availa
3c460 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 ble in the publi
3c470 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 c release.** of
3c480 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 SQLite..*/.SQLIT
3c490 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
3c4a0 33 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 3_rekey(. sqlit
3c4b0 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
3c4c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
3c4d0 61 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 abase to be reke
3c4e0 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 yed */. const v
3c4f0 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e oid *pKey, int n
3c500 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e Key /* The n
3c510 65 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a ew key */.);../*
3c520 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 75 .** CAPI3REF: Su
3c530 73 70 65 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 spend Execution
3c540 46 6f 72 20 41 20 53 68 6f 72 74 20 54 69 6d 65 For A Short Time
3c550 20 7b 48 31 30 35 33 30 7d 20 3c 53 34 30 34 31 {H10530} <S4041
3c560 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 0>.**.** The sql
3c570 69 74 65 33 5f 73 6c 65 65 70 28 29 20 66 75 6e ite3_sleep() fun
3c580 63 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 ction causes the
3c590 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 current thread
3c5a0 74 6f 20 73 75 73 70 65 6e 64 20 65 78 65 63 75 to suspend execu
3c5b0 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c tion.** for at l
3c5c0 65 61 73 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 east a number of
3c5d0 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 milliseconds sp
3c5e0 65 63 69 66 69 65 64 20 69 6e 20 69 74 73 20 70 ecified in its p
3c5f0 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 arameter..**.**
3c600 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 If the operating
3c610 20 73 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 system does not
3c620 20 73 75 70 70 6f 72 74 20 73 6c 65 65 70 20 72 support sleep r
3c630 65 71 75 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 equests with.**
3c640 6d 69 6c 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 millisecond time
3c650 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 resolution, the
3c660 6e 20 74 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 n the time will
3c670 62 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f be rounded up to
3c680 0a 2a 2a 20 74 68 65 20 6e 65 61 72 65 73 74 20 .** the nearest
3c690 73 65 63 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 second. The numb
3c6a0 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e er of millisecon
3c6b0 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 ds of sleep actu
3c6c0 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 ally.** requeste
3c6d0 64 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 d from the opera
3c6e0 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 72 ting system is r
3c6f0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 eturned..**.** S
3c700 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 QLite implements
3c710 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 this interface
3c720 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 by calling the x
3c730 53 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f Sleep().** metho
3c740 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 d of the default
3c750 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f [sqlite3_vfs] o
3c760 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 bject..**.** INV
3c770 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
3c780 48 31 30 35 33 33 7d 20 54 68 65 20 5b 73 71 6c H10533} The [sql
3c790 69 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 ite3_sleep(M)] i
3c7a0 6e 74 65 72 66 61 63 65 20 69 6e 76 6f 6b 65 73 nterface invokes
3c7b0 20 74 68 65 20 78 53 6c 65 65 70 0a 2a 2a 20 20 the xSleep.**
3c7c0 20 20 20 20 20 20 20 20 6d 65 74 68 6f 64 20 6f method o
3c7d0 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 f the default [s
3c7e0 71 6c 69 74 65 33 5f 76 66 73 7c 56 46 53 5d 20 qlite3_vfs|VFS]
3c7f0 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 20 in order to.**
3c800 20 20 20 20 20 20 20 20 73 75 73 70 65 6e 64 20 suspend
3c810 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 execution of the
3c820 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 current thread
3c830 66 6f 72 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 for at least.**
3c840 20 20 20 20 20 20 20 20 20 4d 20 6d 69 6c 6c 69 M milli
3c850 73 65 63 6f 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 7b seconds..**.** {
3c860 48 31 30 35 33 36 7d 20 54 68 65 20 5b 73 71 6c H10536} The [sql
3c870 69 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 ite3_sleep(M)] i
3c880 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
3c890 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
3c8a0 2a 20 20 20 20 20 20 20 20 20 20 6d 69 6c 6c 69 * milli
3c8b0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 seconds of sleep
3c8c0 20 61 63 74 75 61 6c 6c 79 20 72 65 71 75 65 73 actually reques
3c8d0 74 65 64 20 6f 66 20 74 68 65 20 6f 70 65 72 61 ted of the opera
3c8e0 74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 ting.**
3c8f0 20 73 79 73 74 65 6d 2c 20 77 68 69 63 68 20 6d system, which m
3c900 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 ight be larger t
3c910 68 61 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 han the paramete
3c920 72 20 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 r M..*/.SQLITE_A
3c930 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
3c940 6c 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a leep(int);../*.*
3c950 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 * CAPI3REF: Name
3c960 20 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 Of The Folder H
3c970 6f 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 olding Temporary
3c980 20 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 Files {H10310}
3c990 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 <S20000>.**.** I
3c9a0 66 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 f this global va
3c9b0 72 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 riable is made t
3c9c0 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 o point to a str
3c9d0 69 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 ing which is.**
3c9e0 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f the name of a fo
3c9f0 6c 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 lder (a.k.a. dir
3ca00 65 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c ectory), then al
3ca10 6c 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 l temporary file
3ca20 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 s.** created by
3ca30 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 SQLite will be p
3ca40 6c 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 laced in that di
3ca50 72 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 rectory. If thi
3ca60 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 s variable.** is
3ca70 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
3ca80 20 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 then SQLite per
3ca90 66 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 forms a search f
3caa0 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 or an appropriat
3cab0 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 e.** temporary f
3cac0 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a ile directory..*
3cad0 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 *.** It is not s
3cae0 61 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 afe to modify th
3caf0 69 73 20 76 61 72 69 61 62 6c 65 20 6f 6e 63 65 is variable once
3cb00 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e a [database con
3cb10 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 68 61 73 20 nection].** has
3cb20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 20 20 49 74 been opened. It
3cb30 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 68 61 is intended tha
3cb40 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 t this variable
3cb50 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a 20 61 be set once.** a
3cb60 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73 s part of proces
3cb70 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e s initialization
3cb80 20 61 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20 and before any
3cb90 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 SQLite interface
3cba0 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 61 76 .** routines hav
3cbb0 65 20 62 65 65 6e 20 63 61 6c 6c 20 61 6e 64 20 e been call and
3cbc0 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 remain unchanged
3cbd0 20 74 68 65 72 65 61 66 74 65 72 2e 0a 2a 2f 0a thereafter..*/.
3cbe0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
3cbf0 2a 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 *sqlite3_temp_di
3cc00 72 65 63 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 rectory;../*.**
3cc10 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 46 CAPI3REF: Test F
3cc20 6f 72 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d or Auto-Commit M
3cc30 6f 64 65 20 7b 48 31 32 39 33 30 7d 20 3c 53 36 ode {H12930} <S6
3cc40 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0200>.** KEYWORD
3cc50 53 3a 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d S: {autocommit m
3cc60 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ode}.**.** The s
3cc70 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 qlite3_get_autoc
3cc80 6f 6d 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 ommit() interfac
3cc90 65 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 e returns non-ze
3cca0 72 6f 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 ro or.** zero if
3ccb0 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 the given datab
3ccc0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 ase connection i
3ccd0 73 20 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 s or is not in a
3cce0 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a utocommit mode,.
3ccf0 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e ** respectively.
3cd00 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 Autocommit mod
3cd10 65 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 e is on by defau
3cd20 6c 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 lt..** Autocommi
3cd30 74 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c t mode is disabl
3cd40 65 64 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20 ed by a [BEGIN]
3cd50 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 statement..** Au
3cd60 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 tocommit mode is
3cd70 20 72 65 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 re-enabled by a
3cd80 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f [COMMIT] or [RO
3cd90 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 LLBACK]..**.** I
3cda0 66 20 63 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 f certain kinds
3cdb0 6f 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 20 of errors occur
3cdc0 6f 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 on a statement w
3cdd0 69 74 68 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 ithin a multi-st
3cde0 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 atement.** trans
3cdf0 61 63 74 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 action (errors i
3ce00 6e 63 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 ncluding [SQLITE
3ce10 5f 46 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f _FULL], [SQLITE_
3ce20 49 4f 45 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 IOERR],.** [SQLI
3ce30 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 TE_NOMEM], [SQLI
3ce40 54 45 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 TE_BUSY], and [S
3ce50 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d QLITE_INTERRUPT]
3ce60 29 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 ) then the.** tr
3ce70 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 ansaction might
3ce80 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 be rolled back a
3ce90 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 utomatically. T
3cea0 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a he only way to.*
3ceb0 2a 20 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68 * find out wheth
3cec0 65 72 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 er SQLite automa
3ced0 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 tically rolled b
3cee0 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 ack the transact
3cef0 69 6f 6e 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 ion after.** an
3cf00 65 72 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20 error is to use
3cf10 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a this function..*
3cf20 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
3cf30 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 33 31 7d 20 .**.** {H12931}
3cf40 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 The [sqlite3_get
3cf50 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 44 29 5d 20 _autocommit(D)]
3cf60 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
3cf70 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a s non-zero or.**
3cf80 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 69 zero i
3cf90 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 f the [database
3cfa0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 69 73 connection] D is
3cfb0 20 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 or is not in au
3cfc0 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 20 20 20 20 tocommit.**
3cfd0 20 20 20 20 20 6d 6f 64 65 2c 20 72 65 73 70 65 mode, respe
3cfe0 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b ctively..**.** {
3cff0 48 31 32 39 33 32 7d 20 41 75 74 6f 63 6f 6d 6d H12932} Autocomm
3d000 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 it mode is on by
3d010 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 default..**.**
3d020 7b 48 31 32 39 33 33 7d 20 41 75 74 6f 63 6f 6d {H12933} Autocom
3d030 6d 69 74 20 6d 6f 64 65 20 69 73 20 64 69 73 61 mit mode is disa
3d040 62 6c 65 64 20 62 79 20 61 20 73 75 63 63 65 73 bled by a succes
3d050 73 66 75 6c 20 5b 42 45 47 49 4e 5d 20 73 74 61 sful [BEGIN] sta
3d060 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 tement..**.** {H
3d070 31 32 39 33 34 7d 20 41 75 74 6f 63 6f 6d 6d 69 12934} Autocommi
3d080 74 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c 65 t mode is enable
3d090 64 20 62 79 20 61 20 73 75 63 63 65 73 73 66 75 d by a successfu
3d0a0 6c 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 l [COMMIT] or [R
3d0b0 4f 4c 4c 42 41 43 4b 5d 0a 2a 2a 20 20 20 20 20 OLLBACK].**
3d0c0 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2e 0a statement..
3d0d0 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e **.** ASSUMPTION
3d0e0 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 39 33 36 S:.**.** {A12936
3d0f0 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 } If another thr
3d100 65 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 ead changes the
3d110 61 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 autocommit statu
3d120 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 s of the databas
3d130 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f e.** co
3d140 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 nnection while t
3d150 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 his routine is r
3d160 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 unning, then the
3d170 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
3d180 20 20 20 20 20 20 20 20 20 20 69 73 20 75 6e 64 is und
3d190 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 efined..*/.SQLIT
3d1a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
3d1b0 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 3_get_autocommit
3d1c0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
3d1d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e ** CAPI3REF: Fin
3d1e0 64 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 d The Database H
3d1f0 61 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 andle Of A Prepa
3d200 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 red Statement {H
3d210 31 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 13120} <S60600>.
3d220 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
3d230 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 3_db_handle inte
3d240 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 rface returns th
3d250 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
3d260 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a ection] handle.*
3d270 2a 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 * to which a [pr
3d280 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
3d290 5d 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 ] belongs. The
3d2a0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
3d2b0 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 returned by.** s
3d2c0 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 qlite3_db_handle
3d2d0 20 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 is the same dat
3d2e0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 abase handle tha
3d2f0 74 20 77 61 73 20 74 68 65 20 66 69 72 73 74 20 t was the first
3d300 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 argument.** to t
3d310 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 he [sqlite3_prep
3d320 61 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 are_v2()] call (
3d330 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 or its variants)
3d340 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 that was used t
3d350 6f 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 20 o.** create the
3d360 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 statement in the
3d370 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a first place..**
3d380 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
3d390 2a 2a 0a 2a 2a 20 7b 48 31 33 31 32 33 7d 20 54 **.** {H13123} T
3d3a0 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 68 he [sqlite3_db_h
3d3b0 61 6e 64 6c 65 28 53 29 5d 20 69 6e 74 65 72 66 andle(S)] interf
3d3c0 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f ace returns a po
3d3d0 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 inter.**
3d3e0 20 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 to the [databa
3d3f0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 se connection] a
3d400 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
3d410 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
3d420 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
3d430 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nt] S..*/.SQLITE
3d440 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 _API sqlite3 *sq
3d450 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 lite3_db_handle(
3d460 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a sqlite3_stmt*);.
3d470 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3d480 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 Find the next p
3d490 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3d4a0 74 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 t {H13140} <S606
3d4b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 00>.**.** This i
3d4c0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
3d4d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
3d4e0 65 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 e next [prepared
3d4f0 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 statement] afte
3d500 72 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 r.** pStmt assoc
3d510 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b iated with the [
3d520 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3d530 69 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 ion] pDb. If pS
3d540 74 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 tmt is NULL.** t
3d550 68 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 hen this interfa
3d560 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
3d570 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 nter to the firs
3d580 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 t prepared state
3d590 6d 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 ment.** associat
3d5a0 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 ed with the data
3d5b0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
3d5c0 70 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 pDb. If no prep
3d5d0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a ared statement.*
3d5e0 2a 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 * satisfies the
3d5f0 63 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 conditions of th
3d600 69 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 is routine, it r
3d610 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a eturns NULL..**.
3d620 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a ** INVARIANTS:.*
3d630 2a 0a 2a 2a 20 7b 48 31 33 31 34 33 7d 20 49 66 *.** {H13143} If
3d640 20 44 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 D is a [databas
3d650 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 e connection] th
3d660 61 74 20 68 6f 6c 64 73 20 6f 6e 65 20 6f 72 20 at holds one or
3d670 6d 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 more.**
3d680 20 75 6e 66 69 6e 61 6c 69 7a 65 64 20 5b 70 72 unfinalized [pr
3d690 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
3d6a0 73 5d 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 s] and S is a NU
3d6b0 4c 4c 20 70 6f 69 6e 74 65 72 2c 0a 2a 2a 20 20 LL pointer,.**
3d6c0 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 then [sq
3d6d0 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 lite3_next_stmt(
3d6e0 44 2c 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 D, S)] routine s
3d6f0 68 61 6c 6c 20 72 65 74 75 72 6e 20 61 20 70 6f hall return a po
3d700 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 inter.**
3d710 20 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 to one of the
3d720 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
3d730 6e 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 nts associated w
3d740 69 74 68 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ith D..**.** {H1
3d750 33 31 34 36 7d 20 49 66 20 44 20 69 73 20 61 20 3146} If D is a
3d760 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
3d770 74 69 6f 6e 5d 20 74 68 61 74 20 68 6f 6c 64 73 tion] that holds
3d780 20 6e 6f 20 75 6e 66 69 6e 61 6c 69 7a 65 64 0a no unfinalized.
3d790 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 ** [pre
3d7a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
3d7b0 5d 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c ] and S is a NUL
3d7c0 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 0a L pointer, then.
3d7d0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
3d7e0 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 ite3_next_stmt(D
3d7f0 2c 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 , S)] routine sh
3d800 61 6c 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c all return a NUL
3d810 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a L pointer..**.**
3d820 20 7b 48 31 33 31 34 39 7d 20 49 66 20 53 20 69 {H13149} If S i
3d830 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 s a [prepared st
3d840 61 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65 20 atement] in the
3d850 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
3d860 74 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20 20 tion] D.**
3d870 20 20 20 20 61 6e 64 20 53 20 69 73 20 6e 6f 74 and S is not
3d880 20 74 68 65 20 6c 61 73 74 20 70 72 65 70 61 72 the last prepar
3d890 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 ed statement in
3d8a0 44 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 D, then.**
3d8b0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 [sqlite3_nex
3d8c0 74 5f 73 74 6d 74 28 44 2c 20 53 29 5d 20 72 6f t_stmt(D, S)] ro
3d8d0 75 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74 75 utine shall retu
3d8e0 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 rn a pointer.**
3d8f0 20 20 20 20 20 20 20 20 20 74 6f 20 74 68 65 20 to the
3d900 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74 next prepared st
3d910 61 74 65 6d 65 6e 74 20 69 6e 20 44 20 61 66 74 atement in D aft
3d920 65 72 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 er S..**.** {H13
3d930 31 35 32 7d 20 49 66 20 53 20 69 73 20 74 68 65 152} If S is the
3d940 20 6c 61 73 74 20 5b 70 72 65 70 61 72 65 64 20 last [prepared
3d950 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 statement] in th
3d960 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 e.** [d
3d970 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3d980 6f 6e 5d 20 44 20 74 68 65 6e 20 74 68 65 20 5b on] D then the [
3d990 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d sqlite3_next_stm
3d9a0 74 28 44 2c 20 53 29 5d 0a 2a 2a 20 20 20 20 20 t(D, S)].**
3d9b0 20 20 20 20 20 72 6f 75 74 69 6e 65 20 73 68 61 routine sha
3d9c0 6c 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c ll return a NULL
3d9d0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
3d9e0 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a ASSUMPTIONS:.**.
3d9f0 2a 2a 20 7b 41 31 33 31 35 34 7d 20 54 68 65 20 ** {A13154} The
3da00 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
3da10 74 69 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 tion] pointer D
3da20 69 6e 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 in a call to.**
3da30 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
3da40 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 3_next_stmt(D,S)
3da50 5d 20 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20 ] must refer to
3da60 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 an open database
3da70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e .** con
3da80 6e 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e 20 70 nection and in p
3da90 61 72 74 69 63 75 6c 61 72 20 6d 75 73 74 20 6e articular must n
3daa0 6f 74 20 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69 ot be a NULL poi
3dab0 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f nter..*/.SQLITE_
3dac0 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 API sqlite3_stmt
3dad0 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 *sqlite3_next_s
3dae0 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 tmt(sqlite3 *pDb
3daf0 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a , sqlite3_stmt *
3db00 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 pStmt);../*.** C
3db10 41 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 API3REF: Commit
3db20 41 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 And Rollback Not
3db30 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 ification Callba
3db40 63 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c 53 36 cks {H12950} <S6
3db50 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0400>.**.** The
3db60 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 sqlite3_commit_h
3db70 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 ook() interface
3db80 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c registers a call
3db90 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e back.** function
3dba0 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 to be invoked w
3dbb0 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 henever a transa
3dbc0 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 ction is committ
3dbd0 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 ed..** Any callb
3dbe0 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 ack set by a pre
3dbf0 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 vious call to sq
3dc00 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f lite3_commit_hoo
3dc10 6b 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 k().** for the s
3dc20 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
3dc30 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 nection is overr
3dc40 69 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 idden..** The sq
3dc50 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 lite3_rollback_h
3dc60 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 ook() interface
3dc70 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c registers a call
3dc80 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e back.** function
3dc90 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 to be invoked w
3dca0 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 henever a transa
3dcb0 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 ction is committ
3dcc0 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 ed..** Any callb
3dcd0 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 ack set by a pre
3dce0 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 vious call to sq
3dcf0 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f lite3_commit_hoo
3dd00 6b 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 k().** for the s
3dd10 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
3dd20 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 nection is overr
3dd30 69 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 idden..** The pA
3dd40 72 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 rg argument is p
3dd50 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f assed through to
3dd60 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a the callback..*
3dd70 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 * If the callbac
3dd80 6b 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f k on a commit ho
3dd90 6f 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 ok function retu
3dda0 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a rns non-zero,.**
3ddb0 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 then the commit
3ddc0 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e is converted in
3ddd0 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a to a rollback..*
3dde0 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 *.** If another
3ddf0 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 function was pre
3de00 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 viously register
3de10 65 64 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 ed, its.** pArg
3de20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 value is returne
3de30 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 d. Otherwise NU
3de40 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a LL is returned..
3de50 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 **.** The callba
3de60 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
3de70 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e n must not do an
3de80 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c ything that will
3de90 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 modify.** the d
3dea0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3deb0 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 on that invoked
3dec0 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 the callback. A
3ded0 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f ny actions.** to
3dee0 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 modify the data
3def0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
3df00 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64 must be deferred
3df10 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 until after the
3df20 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f .** completion o
3df30 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 f the [sqlite3_s
3df40 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 tep()] call that
3df50 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20 63 triggered the c
3df60 6f 6d 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c ommit.** or roll
3df70 62 61 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 back hook in the
3df80 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a first place..**
3df90 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 Note that [sqli
3dfa0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
3dfb0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 ] and [sqlite3_s
3dfc0 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 tep()] both modi
3dfd0 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 fy their.** data
3dfe0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
3dff0 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 for the meaning
3e000 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 of "modify" in
3e010 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a this paragraph..
3e020 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e **.** Registerin
3e030 67 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f g a NULL functio
3e040 6e 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 n disables the c
3e050 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 allback..**.** F
3e060 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 or the purposes
3e070 6f 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 of this API, a t
3e080 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 ransaction is sa
3e090 69 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a id to have been.
3e0a0 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 ** rolled back i
3e0b0 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 f an explicit "R
3e0c0 4f 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 OLLBACK" stateme
3e0d0 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 nt is executed,
3e0e0 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f or.** an error o
3e0f0 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 r constraint cau
3e100 73 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 ses an implicit
3e110 72 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 rollback to occu
3e120 72 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 r..** The rollba
3e130 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e ck callback is n
3e140 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 ot invoked if a
3e150 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a transaction is.*
3e160 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * automatically
3e170 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 rolled back beca
3e180 75 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 use the database
3e190 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 connection is c
3e1a0 6c 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f losed..** The ro
3e1b0 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 llback callback
3e1c0 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 is not invoked i
3e1d0 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
3e1e0 69 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 is.** rolled bac
3e1f0 6b 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d k because a comm
3e200 69 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 it callback retu
3e210 72 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a rned non-zero..*
3e220 2a 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f * <todo> Check o
3e230 6e 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a n this </todo>.*
3e240 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a *.** INVARIANTS:
3e250 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 31 7d 20 .**.** {H12951}
3e260 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d The [sqlite3_com
3e270 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d mit_hook(D,F,P)]
3e280 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 interface regis
3e290 74 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 ters the.**
3e2a0 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66 75 callback fu
3e2b0 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 69 nction F to be i
3e2c0 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67 75 nvoked with argu
3e2d0 6d 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72 0a ment P whenever.
3e2e0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 74 72 ** a tr
3e2f0 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 ansaction commit
3e300 73 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 s on the [databa
3e310 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 se connection] D
3e320 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 32 7d ..**.** {H12952}
3e330 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f The [sqlite3_co
3e340 6d 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 mmit_hook(D,F,P)
3e350 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ] interface retu
3e360 72 6e 73 20 74 68 65 20 50 20 61 72 67 75 6d 65 rns the P argume
3e370 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 nt.** f
3e380 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 rom the previous
3e390 20 63 61 6c 6c 20 77 69 74 68 20 74 68 65 20 73 call with the s
3e3a0 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f ame [database co
3e3b0 6e 6e 65 63 74 69 6f 6e 5d 20 44 2c 0a 2a 2a 20 nnection] D,.**
3e3c0 20 20 20 20 20 20 20 20 20 6f 72 20 4e 55 4c 4c or NULL
3e3d0 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 on the first ca
3e3e0 6c 6c 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 ll for a particu
3e3f0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e lar database con
3e400 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a 2a 2a nection D..**.**
3e410 20 7b 48 31 32 39 35 33 7d 20 45 61 63 68 20 63 {H12953} Each c
3e420 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
3e430 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 6f commit_hook()] o
3e440 76 65 72 77 72 69 74 65 73 20 74 68 65 20 63 61 verwrites the ca
3e450 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 llback.**
3e460 20 20 20 72 65 67 69 73 74 65 72 65 64 20 62 79 registered by
3e470 20 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2a prior calls..**
3e480 0a 2a 2a 20 7b 48 31 32 39 35 34 7d 20 49 66 20 .** {H12954} If
3e490 74 68 65 20 46 20 61 72 67 75 6d 65 6e 74 20 74 the F argument t
3e4a0 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 o [sqlite3_commi
3e4b0 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 69 t_hook(D,F,P)] i
3e4c0 73 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 s NULL.**
3e4d0 20 20 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d then the comm
3e4e0 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b it hook callback
3e4f0 20 69 73 20 63 61 6e 63 65 6c 65 64 20 61 6e 64 is canceled and
3e500 20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 no callback.**
3e510 20 20 20 20 20 20 20 20 20 69 73 20 69 6e 76 6f is invo
3e520 6b 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 ked when a trans
3e530 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 2e 0a action commits..
3e540 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 35 7d 20 49 **.** {H12955} I
3e550 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 61 6c f the commit cal
3e560 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f lback returns no
3e570 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 n-zero then the
3e580 63 6f 6d 6d 69 74 20 69 73 0a 2a 2a 20 20 20 20 commit is.**
3e590 20 20 20 20 20 20 63 6f 6e 76 65 72 74 65 64 20 converted
3e5a0 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e into a rollback.
3e5b0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 31 7d 20 .**.** {H12961}
3e5c0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c The [sqlite3_rol
3e5d0 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46 2c 50 lback_hook(D,F,P
3e5e0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 )] interface reg
3e5f0 69 73 74 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 isters the.**
3e600 20 20 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 callback
3e610 66 75 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 function F to be
3e620 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 72 invoked with ar
3e630 67 75 6d 65 6e 74 20 50 20 77 68 65 6e 65 76 65 gument P wheneve
3e640 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 r.** a
3e650 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c transaction roll
3e660 73 20 62 61 63 6b 20 6f 6e 20 74 68 65 20 5b 64 s back on the [d
3e670 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3e680 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 on] D..**.** {H1
3e690 32 39 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 2962} The [sqlit
3e6a0 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b e3_rollback_hook
3e6b0 28 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 (D,F,P)] interfa
3e6c0 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 50 ce returns the P
3e6d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72 67 .** arg
3e6e0 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 ument from the p
3e6f0 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 77 69 74 revious call wit
3e700 68 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 h the same.**
3e710 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 [database
3e720 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2c 20 connection] D,
3e730 6f 72 20 4e 55 4c 4c 20 6f 6e 20 74 68 65 20 66 or NULL on the f
3e740 69 72 73 74 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 irst call.**
3e750 20 20 20 20 20 20 66 6f 72 20 61 20 70 61 72 74 for a part
3e760 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 icular database
3e770 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a connection D..**
3e780 0a 2a 2a 20 7b 48 31 32 39 36 33 7d 20 45 61 63 .** {H12963} Eac
3e790 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 h call to [sqlit
3e7a0 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b e3_rollback_hook
3e7b0 28 29 5d 20 6f 76 65 72 77 72 69 74 65 73 20 74 ()] overwrites t
3e7c0 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 he callback.**
3e7d0 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 register
3e7e0 65 64 20 62 79 20 70 72 69 6f 72 20 63 61 6c 6c ed by prior call
3e7f0 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 34 s..**.** {H12964
3e800 7d 20 49 66 20 74 68 65 20 46 20 61 72 67 75 6d } If the F argum
3e810 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ent to [sqlite3_
3e820 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c rollback_hook(D,
3e830 46 2c 50 29 5d 20 69 73 20 4e 55 4c 4c 0a 2a 2a F,P)] is NULL.**
3e840 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 74 then t
3e850 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b he rollback hook
3e860 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e callback is can
3e870 63 65 6c 65 64 20 61 6e 64 20 6e 6f 20 63 61 6c celed and no cal
3e880 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 lback.**
3e890 20 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 68 65 is invoked whe
3e8a0 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 n a transaction
3e8b0 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2f 0a 53 rolls back..*/.S
3e8c0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
3e8d0 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 sqlite3_commit_h
3e8e0 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e ook(sqlite3*, in
3e8f0 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 t(*)(void*), voi
3e900 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 d*);.SQLITE_API
3e910 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f void *sqlite3_ro
3e920 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 llback_hook(sqli
3e930 74 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f te3*, void(*)(vo
3e940 69 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a id *), void*);..
3e950 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3e960 44 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74 69 Data Change Noti
3e970 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 fication Callbac
3e980 6b 73 20 7b 48 31 32 39 37 30 7d 20 3c 53 36 30 ks {H12970} <S60
3e990 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 400>.**.** The s
3e9a0 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f qlite3_update_ho
3e9b0 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ok() interface r
3e9c0 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 egisters a callb
3e9d0 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 ack function.**
3e9e0 77 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 with the [databa
3e9f0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 se connection] i
3ea00 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 dentified by the
3ea10 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a first argument.
3ea20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 ** to be invoked
3ea30 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 whenever a row
3ea40 69 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 is updated, inse
3ea50 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e rted or deleted.
3ea60 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b .** Any callback
3ea70 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f set by a previo
3ea80 75 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 us call to this
3ea90 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 function.** for
3eaa0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
3eab0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
3eac0 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a overridden..**.*
3ead0 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 * The second arg
3eae0 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 ument is a point
3eaf0 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 er to the functi
3eb00 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65 on to invoke whe
3eb10 6e 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 n a.** row is up
3eb20 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 dated, inserted
3eb30 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 or deleted..** T
3eb40 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
3eb50 74 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 t to the callbac
3eb60 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 k is a copy of t
3eb70 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
3eb80 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f t.** to sqlite3_
3eb90 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a update_hook()..*
3eba0 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c * The second cal
3ebb0 6c 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 lback argument i
3ebc0 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 s one of [SQLITE
3ebd0 5f 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54 _INSERT], [SQLIT
3ebe0 45 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 E_DELETE],.** or
3ebf0 20 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 5d [SQLITE_UPDATE]
3ec00 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 , depending on t
3ec10 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 he operation tha
3ec20 74 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c t caused the cal
3ec30 6c 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 lback.** to be i
3ec40 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 nvoked..** The t
3ec50 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 hird and fourth
3ec60 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 arguments to the
3ec70 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 callback contai
3ec80 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 n pointers to th
3ec90 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e e.** database an
3eca0 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e d table name con
3ecb0 74 61 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65 taining the affe
3ecc0 63 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 cted row..** The
3ecd0 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 final callback
3ece0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 parameter is the
3ecf0 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f rowid of the ro
3ed00 77 2e 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f w. In the case o
3ed10 66 0a 2a 2a 20 61 6e 20 75 70 64 61 74 65 2c 20 f.** an update,
3ed20 74 68 69 73 20 69 73 20 74 68 65 20 72 6f 77 69 this is the rowi
3ed30 64 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 d after the upda
3ed40 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a te takes place..
3ed50 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 **.** The update
3ed60 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 hook is not inv
3ed70 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e oked when intern
3ed80 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 al system tables
3ed90 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 are.** modified
3eda0 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 (i.e. sqlite_ma
3edb0 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f ster and sqlite_
3edc0 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a sequence)..**.**
3edd0 20 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b The update hook
3ede0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
3edf0 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 must not do anyt
3ee00 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d hing that will m
3ee10 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 odify.** the dat
3ee20 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
3ee30 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 that invoked th
3ee40 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 e update hook.
3ee50 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 Any actions.** t
3ee60 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 o modify the dat
3ee70 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
3ee80 20 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65 must be deferre
3ee90 64 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 d until after th
3eea0 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 e.** completion
3eeb0 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
3eec0 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 step()] call tha
3eed0 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20 t triggered the
3eee0 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 update hook..**
3eef0 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 Note that [sqlit
3ef00 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
3ef10 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 and [sqlite3_st
3ef20 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 ep()] both modif
3ef30 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 y their.** datab
3ef40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ase connections
3ef50 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 for the meaning
3ef60 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 of "modify" in t
3ef70 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a his paragraph..*
3ef80 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 *.** If another
3ef90 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 function was pre
3efa0 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 viously register
3efb0 65 64 2c 20 69 74 73 20 70 41 72 67 20 76 61 6c ed, its pArg val
3efc0 75 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 ue.** is returne
3efd0 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 d. Otherwise NU
3efe0 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a LL is returned..
3eff0 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 **.** INVARIANTS
3f000 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 37 31 7d :.**.** {H12971}
3f010 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 70 The [sqlite3_up
3f020 64 61 74 65 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 date_hook(D,F,P)
3f030 5d 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 ] interface caus
3f040 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a es the callback.
3f050 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 ** func
3f060 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 69 6e 76 tion F to be inv
3f070 6f 6b 65 64 20 77 69 74 68 20 66 69 72 73 74 20 oked with first
3f080 70 61 72 61 6d 65 74 65 72 20 50 20 77 68 65 6e parameter P when
3f090 65 76 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ever.**
3f0a0 20 61 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 a table row is
3f0b0 6d 6f 64 69 66 69 65 64 2c 20 69 6e 73 65 72 74 modified, insert
3f0c0 65 64 2c 20 6f 72 20 64 65 6c 65 74 65 64 20 6f ed, or deleted o
3f0d0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 n.** th
3f0e0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
3f0f0 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a ection] D..**.**
3f100 20 7b 48 31 32 39 37 33 7d 20 54 68 65 20 5b 73 {H12973} The [s
3f110 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f qlite3_update_ho
3f120 6f 6b 28 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 ok(D,F,P)] inter
3f130 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 face returns the
3f140 20 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 value.**
3f150 20 20 20 6f 66 20 50 20 66 6f 72 20 74 68 65 20 of P for the
3f160 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 6f 6e previous call on
3f170 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 the same [datab
3f180 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
3f190 44 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f D,.** o
3f1a0 72 20 4e 55 4c 4c 20 66 6f 72 20 74 68 65 20 66 r NULL for the f
3f1b0 69 72 73 74 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a irst call..**.**
3f1c0 20 7b 48 31 32 39 37 35 7d 20 49 66 20 74 68 65 {H12975} If the
3f1d0 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 63 61 6c update hook cal
3f1e0 6c 62 61 63 6b 20 46 20 69 6e 20 5b 73 71 6c 69 lback F in [sqli
3f1f0 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 te3_update_hook(
3f200 44 2c 46 2c 50 29 5d 0a 2a 2a 20 20 20 20 20 20 D,F,P)].**
3f210 20 20 20 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e is NULL then
3f220 20 74 68 65 20 6e 6f 20 75 70 64 61 74 65 20 63 the no update c
3f230 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 6d 61 64 allbacks are mad
3f240 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 37 37 e..**.** {H12977
3f250 7d 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b } Each call to [
3f260 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 sqlite3_update_h
3f270 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 6f 76 65 72 ook(D,F,P)] over
3f280 72 69 64 65 73 20 70 72 69 6f 72 20 63 61 6c 6c rides prior call
3f290 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f s.** to
3f2a0 20 74 68 65 20 73 61 6d 65 20 69 6e 74 65 72 66 the same interf
3f2b0 61 63 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 ace on the same
3f2c0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
3f2d0 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b tion] D..**.** {
3f2e0 48 31 32 39 37 39 7d 20 54 68 65 20 75 70 64 61 H12979} The upda
3f2f0 74 65 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b te hook callback
3f300 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 is not invoked
3f310 77 68 65 6e 20 69 6e 74 65 72 6e 61 6c 20 73 79 when internal sy
3f320 73 74 65 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 stem.**
3f330 20 74 61 62 6c 65 73 20 73 75 63 68 20 61 73 20 tables such as
3f340 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e sqlite_master an
3f350 64 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 d sqlite_sequenc
3f360 65 20 61 72 65 20 6d 6f 64 69 66 69 65 64 2e 0a e are modified..
3f370 2a 2a 0a 2a 2a 20 7b 48 31 32 39 38 31 7d 20 54 **.** {H12981} T
3f380 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
3f390 74 65 72 20 74 6f 20 74 68 65 20 75 70 64 61 74 ter to the updat
3f3a0 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 e callback.**
3f3b0 20 20 20 20 20 20 20 69 73 20 6f 6e 65 20 6f 66 is one of
3f3c0 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 5d [SQLITE_INSERT]
3f3d0 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 , [SQLITE_DELETE
3f3e0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44 ] or [SQLITE_UPD
3f3f0 41 54 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 ATE],.**
3f400 20 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 depending on t
3f410 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 he operation tha
3f420 74 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c t caused the cal
3f430 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f lback to be invo
3f440 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 ked..**.** {H129
3f450 38 33 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e 83} The third an
3f460 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e d fourth argumen
3f470 74 73 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 ts to the callba
3f480 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 ck contain point
3f490 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ers.**
3f4a0 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 to zero-terminat
3f4b0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 ed UTF-8 strings
3f4c0 20 77 68 69 63 68 20 61 72 65 20 74 68 65 20 6e which are the n
3f4d0 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20 ames of the.**
3f4e0 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 database
3f4f0 20 61 6e 64 20 74 61 62 6c 65 20 74 68 61 74 20 and table that
3f500 69 73 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 is being updated
3f510 2e 0a 0a 2a 2a 20 7b 48 31 32 39 38 35 7d 20 54 ...** {H12985} T
3f520 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 he final callbac
3f530 6b 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 k parameter is t
3f540 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 he rowid of the
3f550 72 6f 77 20 61 66 74 65 72 0a 2a 2a 20 20 20 20 row after.**
3f560 20 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 65 the change
3f570 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 occurs..*/.SQLI
3f580 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
3f590 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b ite3_update_hook
3f5a0 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 (. sqlite3*, .
3f5b0 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 2c void(*)(void *,
3f5c0 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 20 int ,char const
3f5d0 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 *,char const *,s
3f5e0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c 0a 20 qlite3_int64),.
3f5f0 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a void*.);../*.**
3f600 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c CAPI3REF: Enabl
3f610 65 20 4f 72 20 44 69 73 61 62 6c 65 20 53 68 61 e Or Disable Sha
3f620 72 65 64 20 50 61 67 65 72 20 43 61 63 68 65 20 red Pager Cache
3f630 7b 48 31 30 33 33 30 7d 20 3c 53 33 30 39 30 30 {H10330} <S30900
3f640 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
3f650 73 68 61 72 65 64 20 63 61 63 68 65 7d 20 7b 73 shared cache} {s
3f660 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 hared cache mode
3f670 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 }.**.** This rou
3f680 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 tine enables or
3f690 64 69 73 61 62 6c 65 73 20 74 68 65 20 73 68 61 disables the sha
3f6a0 72 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 ring of the data
3f6b0 62 61 73 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e base cache.** an
3f6c0 64 20 73 63 68 65 6d 61 20 64 61 74 61 20 73 74 d schema data st
3f6d0 72 75 63 74 75 72 65 73 20 62 65 74 77 65 65 6e ructures between
3f6e0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
3f6f0 63 74 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 ction | connecti
3f700 6f 6e 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 ons].** to the s
3f710 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20 53 68 ame database. Sh
3f720 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 aring is enabled
3f730 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 if the argument
3f740 20 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 is true.** and
3f750 64 69 73 61 62 6c 65 64 20 69 66 20 74 68 65 20 disabled if the
3f760 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 argument is fals
3f770 65 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73 e..**.** Cache s
3f780 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 haring is enable
3f790 64 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 66 d and disabled f
3f7a0 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72 6f or an entire pro
3f7b0 63 65 73 73 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54 cess. {END}.** T
3f7c0 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20 his is a change
3f7d0 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 as of SQLite ver
3f7e0 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70 sion 3.5.0. In p
3f7f0 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 rior versions of
3f800 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72 SQLite,.** shar
3f810 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20 ing was enabled
3f820 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 or disabled for
3f830 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61 each thread sepa
3f840 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 rately..**.** Th
3f850 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20 e cache sharing
3f860 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73 mode set by this
3f870 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63 interface effec
3f880 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e ts all subsequen
3f890 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73 t.** calls to [s
3f8a0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 qlite3_open()],
3f8b0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 [sqlite3_open_v2
3f8c0 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ()], and [sqlite
3f8d0 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 3_open16()]..**
3f8e0 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 Existing databas
3f8f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f e connections co
3f900 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73 ntinue use the s
3f910 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 haring mode.** t
3f920 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63 hat was in effec
3f930 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68 t at the time th
3f940 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a ey were opened..
3f950 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 **.** Virtual ta
3f960 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75 bles cannot be u
3f970 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65 sed with a share
3f980 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73 d cache. When s
3f990 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 hared.** cache i
3f9a0 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b s enabled, the [
3f9b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
3f9c0 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65 odule()] API use
3f9d0 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a d to register.**
3f9e0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 virtual tables
3f9f0 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 will always retu
3fa00 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a rn an error..**.
3fa10 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
3fa20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
3fa30 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61 OK] if shared ca
3fa40 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20 che was enabled
3fa50 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73 or disabled.** s
3fa60 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e uccessfully. An
3fa70 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 [error code] is
3fa80 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77 returned otherw
3fa90 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 ise..**.** Share
3faa0 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62 d cache is disab
3fab0 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 led by default.
3fac0 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63 But this might c
3fad0 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75 hange in.** futu
3fae0 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 re releases of S
3faf0 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74 QLite. Applicat
3fb00 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61 ions that care a
3fb10 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 bout shared.** c
3fb20 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f ache setting sho
3fb30 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69 uld set it expli
3fb40 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 citly..**.** INV
3fb50 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b ARIANTS:.**.** {
3fb60 48 31 30 33 33 31 7d 20 41 20 73 75 63 63 65 73 H10331} A succes
3fb70 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 sful invocation
3fb80 6f 66 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 of [sqlite3_enab
3fb90 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 le_shared_cache(
3fba0 42 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 B)].**
3fbb0 77 69 6c 6c 20 65 6e 61 62 6c 65 20 6f 72 20 64 will enable or d
3fbc0 69 73 61 62 6c 65 20 73 68 61 72 65 64 20 63 61 isable shared ca
3fbd0 63 68 65 20 6d 6f 64 65 20 66 6f 72 20 61 6e 79 che mode for any
3fbe0 20 73 75 62 73 65 71 75 65 6e 74 6c 79 0a 2a 2a subsequently.**
3fbf0 20 20 20 20 20 20 20 20 20 20 63 72 65 61 74 65 create
3fc00 64 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e d [database conn
3fc10 65 63 74 69 6f 6e 5d 20 69 6e 20 74 68 65 20 73 ection] in the s
3fc20 61 6d 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a ame process..**.
3fc30 2a 2a 20 7b 48 31 30 33 33 36 7d 20 57 68 65 6e ** {H10336} When
3fc40 20 73 68 61 72 65 64 20 63 61 63 68 65 20 69 73 shared cache is
3fc50 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b 73 enabled, the [s
3fc60 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f qlite3_create_mo
3fc70 64 75 6c 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20 dule()].**
3fc80 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 77 69 interface wi
3fc90 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e ll always return
3fca0 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a an error..**.**
3fcb0 20 7b 48 31 30 33 33 37 7d 20 54 68 65 20 5b 73 {H10337} The [s
3fcc0 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 qlite3_enable_sh
3fcd0 61 72 65 64 5f 63 61 63 68 65 28 42 29 5d 20 69 ared_cache(B)] i
3fce0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
3fcf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 .** [SQ
3fd00 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 LITE_OK] if shar
3fd10 65 64 20 63 61 63 68 65 20 77 61 73 20 65 6e 61 ed cache was ena
3fd20 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 bled or disabled
3fd30 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 2a successfully..*
3fd40 2a 0a 2a 2a 20 7b 48 31 30 33 33 39 7d 20 53 68 *.** {H10339} Sh
3fd50 61 72 65 64 20 63 61 63 68 65 20 69 73 20 64 69 ared cache is di
3fd60 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c sabled by defaul
3fd70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
3fd80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 int sqlite3_ena
3fd90 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 ble_shared_cache
3fda0 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 (int);../*.** CA
3fdb0 50 49 33 52 45 46 3a 20 41 74 74 65 6d 70 74 20 PI3REF: Attempt
3fdc0 54 6f 20 46 72 65 65 20 48 65 61 70 20 4d 65 6d To Free Heap Mem
3fdd0 6f 72 79 20 7b 48 31 37 33 34 30 7d 20 3c 53 33 ory {H17340} <S3
3fde0 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0220>.**.** The
3fdf0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
3fe00 6d 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61 memory() interfa
3fe10 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 ce attempts to f
3fe20 72 65 65 20 4e 20 62 79 74 65 73 0a 2a 2a 20 6f ree N bytes.** o
3fe30 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79 f heap memory by
3fe40 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f deallocating no
3fe50 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f n-essential memo
3fe60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a ry allocations.*
3fe70 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 64 61 * held by the da
3fe80 74 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e 20 tabase library.
3fe90 7b 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75 73 {END} Memory us
3fea0 65 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 ed to cache data
3feb0 62 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74 6f base.** pages to
3fec0 20 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 6d improve perform
3fed0 61 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d 70 ance is an examp
3fee0 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 le of non-essent
3fef0 69 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 73 ial memory..** s
3ff00 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
3ff10 65 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 20 emory() returns
3ff20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
3ff30 74 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65 tes actually fre
3ff40 65 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69 67 ed,.** which mig
3ff50 68 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65 ht be more or le
3ff60 73 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f 75 ss than the amou
3ff70 6e 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a 2a nt requested..**
3ff80 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
3ff90 2a 2a 0a 2a 2a 20 7b 48 31 37 33 34 31 7d 20 54 **.** {H17341} T
3ffa0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 he [sqlite3_rele
3ffb0 61 73 65 5f 6d 65 6d 6f 72 79 28 4e 29 5d 20 69 ase_memory(N)] i
3ffc0 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 nterface attempt
3ffd0 73 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 s to.**
3ffe0 20 66 72 65 65 20 4e 20 62 79 74 65 73 20 6f 66 free N bytes of
3fff0 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79 20 heap memory by
40000 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e deallocating non
40010 2d 65 73 73 65 6e 74 69 61 6c 0a 2a 2a 20 20 20 -essential.**
40020 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 61 6c memory al
40030 6c 6f 63 61 74 69 6f 6e 73 20 68 65 6c 64 20 62 locations held b
40040 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c y the database l
40050 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ibrary..**.** {H
40060 31 36 33 34 32 7d 20 54 68 65 20 5b 73 71 6c 69 16342} The [sqli
40070 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f te3_release_memo
40080 72 79 28 4e 29 5d 20 72 65 74 75 72 6e 73 20 74 ry(N)] returns t
40090 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 he number.**
400a0 20 20 20 20 20 20 6f 66 20 62 79 74 65 73 20 61 of bytes a
400b0 63 74 75 61 6c 6c 79 20 66 72 65 65 64 2c 20 77 ctually freed, w
400c0 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f hich might be mo
400d0 72 65 20 6f 72 20 6c 65 73 73 0a 2a 2a 20 20 20 re or less.**
400e0 20 20 20 20 20 20 20 74 68 61 6e 20 74 68 65 20 than the
400f0 61 6d 6f 75 6e 74 20 72 65 71 75 65 73 74 65 64 amount requested
40100 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
40110 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 int sqlite3_rele
40120 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 29 3b ase_memory(int);
40130 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
40140 3a 20 49 6d 70 6f 73 65 20 41 20 4c 69 6d 69 74 : Impose A Limit
40150 20 4f 6e 20 48 65 61 70 20 53 69 7a 65 20 7b 48 On Heap Size {H
40160 31 37 33 35 30 7d 20 3c 53 33 30 32 32 30 3e 0a 17350} <S30220>.
40170 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
40180 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 3_soft_heap_limi
40190 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 70 6c t() interface pl
401a0 61 63 65 73 20 61 20 22 73 6f 66 74 22 20 6c 69 aces a "soft" li
401b0 6d 69 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 61 6d mit.** on the am
401c0 6f 75 6e 74 20 6f 66 20 68 65 61 70 20 6d 65 6d ount of heap mem
401d0 6f 72 79 20 74 68 61 74 20 6d 61 79 20 62 65 20 ory that may be
401e0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51 4c allocated by SQL
401f0 69 74 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e ite..** If an in
40200 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 69 6f ternal allocatio
40210 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 20 74 n is requested t
40220 68 61 74 20 77 6f 75 6c 64 20 65 78 63 65 65 64 hat would exceed
40230 20 74 68 65 0a 2a 2a 20 73 6f 66 74 20 68 65 61 the.** soft hea
40240 70 20 6c 69 6d 69 74 2c 20 5b 73 71 6c 69 74 65 p limit, [sqlite
40250 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
40260 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f ()] is invoked o
40270 6e 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 69 ne or.** more ti
40280 6d 65 73 20 74 6f 20 66 72 65 65 20 75 70 20 73 mes to free up s
40290 6f 6d 65 20 73 70 61 63 65 20 62 65 66 6f 72 65 ome space before
402a0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
402b0 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a is performed..**
402c0 0a 2a 2a 20 54 68 65 20 6c 69 6d 69 74 20 69 73 .** The limit is
402d0 20 63 61 6c 6c 65 64 20 22 73 6f 66 74 22 2c 20 called "soft",
402e0 62 65 63 61 75 73 65 20 69 66 20 5b 73 71 6c 69 because if [sqli
402f0 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f te3_release_memo
40300 72 79 28 29 5d 0a 2a 2a 20 63 61 6e 6e 6f 74 20 ry()].** cannot
40310 66 72 65 65 20 73 75 66 66 69 63 69 65 6e 74 20 free sufficient
40320 6d 65 6d 6f 72 79 20 74 6f 20 70 72 65 76 65 6e memory to preven
40330 74 20 74 68 65 20 6c 69 6d 69 74 20 66 72 6f 6d t the limit from
40340 20 62 65 69 6e 67 20 65 78 63 65 65 64 65 64 2c being exceeded,
40350 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 .** the memory i
40360 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 79 77 s allocated anyw
40370 61 79 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 ay and the curre
40380 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f nt operation pro
40390 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e ceeds..**.** A n
403a0 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 egative or zero
403b0 76 61 6c 75 65 20 66 6f 72 20 4e 20 6d 65 61 6e value for N mean
403c0 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 s that there is
403d0 6e 6f 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d no soft heap lim
403e0 69 74 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 it and.** [sqlit
403f0 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
40400 79 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 y()] will only b
40410 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65 e called when me
40420 6d 6f 72 79 20 69 73 20 65 78 68 61 75 73 74 65 mory is exhauste
40430 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c d..** The defaul
40440 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 t value for the
40450 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 soft heap limit
40460 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 is zero..**.** S
40470 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 62 65 QLite makes a be
40480 73 74 20 65 66 66 6f 72 74 20 74 6f 20 68 6f 6e st effort to hon
40490 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 or the soft heap
404a0 20 6c 69 6d 69 74 2e 0a 2a 2a 20 42 75 74 20 69 limit..** But i
404b0 66 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 f the soft heap
404c0 6c 69 6d 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 limit cannot be
404d0 68 6f 6e 6f 72 65 64 2c 20 65 78 65 63 75 74 69 honored, executi
404e0 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 on will.** conti
404f0 6e 75 65 20 77 69 74 68 6f 75 74 20 65 72 72 6f nue without erro
40500 72 20 6f 72 20 6e 6f 74 69 66 69 63 61 74 69 6f r or notificatio
40510 6e 2e 20 20 54 68 69 73 20 69 73 20 77 68 79 20 n. This is why
40520 74 68 65 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 the limit is.**
40530 63 61 6c 6c 65 64 20 61 20 22 73 6f 66 74 22 20 called a "soft"
40540 6c 69 6d 69 74 2e 20 20 49 74 20 69 73 20 61 64 limit. It is ad
40550 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a visory only..**.
40560 2a 2a 20 50 72 69 6f 72 20 74 6f 20 53 51 4c 69 ** Prior to SQLi
40570 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 te version 3.5.0
40580 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f , this routine o
40590 6e 6c 79 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 nly constrained
405a0 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c the memory.** al
405b0 6c 6f 63 61 74 65 64 20 62 79 20 61 20 73 69 6e located by a sin
405c0 67 6c 65 20 74 68 72 65 61 64 20 2d 20 74 68 65 gle thread - the
405d0 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 same thread in
405e0 77 68 69 63 68 20 74 68 69 73 20 72 6f 75 74 69 which this routi
405f0 6e 65 0a 2a 2a 20 72 75 6e 73 2e 20 20 42 65 67 ne.** runs. Beg
40600 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 69 inning with SQLi
40610 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 te version 3.5.0
40620 2c 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 , the soft heap
40630 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 61 70 70 6c limit is.** appl
40640 69 65 64 20 74 6f 20 61 6c 6c 20 74 68 72 65 61 ied to all threa
40650 64 73 2e 20 54 68 65 20 76 61 6c 75 65 20 73 70 ds. The value sp
40660 65 63 69 66 69 65 64 20 66 6f 72 20 74 68 65 20 ecified for the
40670 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 0a soft heap limit.
40680 2a 2a 20 69 73 20 61 6e 20 75 70 70 65 72 20 62 ** is an upper b
40690 6f 75 6e 64 20 6f 6e 20 74 68 65 20 74 6f 74 61 ound on the tota
406a0 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
406b0 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 ion for all thre
406c0 61 64 73 2e 20 49 6e 0a 2a 2a 20 76 65 72 73 69 ads. In.** versi
406d0 6f 6e 20 33 2e 35 2e 30 20 74 68 65 72 65 20 69 on 3.5.0 there i
406e0 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20 66 s no mechanism f
406f0 6f 72 20 6c 69 6d 69 74 69 6e 67 20 74 68 65 20 or limiting the
40700 68 65 61 70 20 75 73 61 67 65 20 66 6f 72 0a 2a heap usage for.*
40710 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 68 72 * individual thr
40720 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 eads..**.** INVA
40730 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 RIANTS:.**.** {H
40740 31 36 33 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 16351} The [sqli
40750 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 te3_soft_heap_li
40760 6d 69 74 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 mit(N)] interfac
40770 65 20 70 6c 61 63 65 73 20 61 20 73 6f 66 74 20 e places a soft
40780 6c 69 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 limit.**
40790 20 20 6f 66 20 4e 20 62 79 74 65 73 20 6f 6e 20 of N bytes on
407a0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 the amount of he
407b0 61 70 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d ap memory that m
407c0 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a ay be allocated.
407d0 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73 69 6e ** usin
407e0 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f g [sqlite3_mallo
407f0 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 c()] or [sqlite3
40800 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 61 74 20 61 _realloc()] at a
40810 6e 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 ny point.**
40820 20 20 20 20 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a in time..**
40830 0a 2a 2a 20 7b 48 31 36 33 35 32 7d 20 49 66 20 .** {H16352} If
40840 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 a call to [sqlit
40850 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 e3_malloc()] or
40860 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
40870 28 29 5d 20 77 6f 75 6c 64 0a 2a 2a 20 20 20 20 ()] would.**
40880 20 20 20 20 20 20 63 61 75 73 65 20 74 68 65 20 cause the
40890 74 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 total amount of
408a0 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 allocated memory
408b0 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 0a 2a to exceed the.*
408c0 2a 20 20 20 20 20 20 20 20 20 20 73 6f 66 74 20 * soft
408d0 68 65 61 70 20 6c 69 6d 69 74 2c 20 74 68 65 6e heap limit, then
408e0 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 [sqlite3_releas
408f0 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20 69 e_memory()] is i
40900 6e 76 6f 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 nvoked.**
40910 20 20 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 in an attempt
40920 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 6d to reduce the m
40930 65 6d 6f 72 79 20 75 73 61 67 65 20 70 72 69 6f emory usage prio
40940 72 20 74 6f 20 70 72 6f 63 65 65 64 69 6e 67 0a r to proceeding.
40950 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68 ** with
40960 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
40970 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 2e 0a cation attempt..
40980 2a 2a 0a 2a 2a 20 7b 48 31 36 33 35 33 7d 20 43 **.** {H16353} C
40990 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
409a0 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 _malloc()] or [s
409b0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
409c0 5d 20 74 68 61 74 20 74 72 69 67 67 65 72 0a 2a ] that trigger.*
409d0 2a 20 20 20 20 20 20 20 20 20 20 61 74 74 65 6d * attem
409e0 70 74 73 20 74 6f 20 72 65 64 75 63 65 20 6d 65 pts to reduce me
409f0 6d 6f 72 79 20 75 73 61 67 65 20 74 68 72 6f 75 mory usage throu
40a00 67 68 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 gh the soft heap
40a10 20 6c 69 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20 limit.**
40a20 20 20 20 6d 65 63 68 61 6e 69 73 6d 20 63 6f 6e mechanism con
40a30 74 69 6e 75 65 20 65 76 65 6e 20 69 66 20 74 68 tinue even if th
40a40 65 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 64 e attempt to red
40a50 75 63 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 uce memory.**
40a60 20 20 20 20 20 20 20 75 73 61 67 65 20 69 73 20 usage is
40a70 75 6e 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a unsuccessful..**
40a80 0a 2a 2a 20 7b 48 31 36 33 35 34 7d 20 41 20 6e .** {H16354} A n
40a90 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 egative or zero
40aa0 76 61 6c 75 65 20 66 6f 72 20 4e 20 69 6e 20 61 value for N in a
40ab0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 call to.**
40ac0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 6f [sqlite3_so
40ad0 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 4e 29 ft_heap_limit(N)
40ae0 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 ] means that the
40af0 72 65 20 69 73 20 6e 6f 20 73 6f 66 74 0a 2a 2a re is no soft.**
40b00 20 20 20 20 20 20 20 20 20 20 68 65 61 70 20 6c heap l
40b10 69 6d 69 74 20 61 6e 64 20 5b 73 71 6c 69 74 65 imit and [sqlite
40b20 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
40b30 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 ()] will only be
40b40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c .** cal
40b50 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 led when memory
40b60 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 is completely ex
40b70 68 61 75 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b hausted..**.** {
40b80 48 31 36 33 35 35 7d 20 54 68 65 20 64 65 66 61 H16355} The defa
40b90 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 ult value for th
40ba0 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 e soft heap limi
40bb0 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a t is zero..**.**
40bc0 20 7b 48 31 36 33 35 38 7d 20 45 61 63 68 20 63 {H16358} Each c
40bd0 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
40be0 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 soft_heap_limit(
40bf0 4e 29 5d 20 6f 76 65 72 72 69 64 65 73 20 74 68 N)] overrides th
40c00 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 e.** va
40c10 6c 75 65 73 20 73 65 74 20 62 79 20 61 6c 6c 20 lues set by all
40c20 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a prior calls..*/.
40c30 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
40c40 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 sqlite3_soft_hea
40c50 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f p_limit(int);../
40c60 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 *.** CAPI3REF: E
40c70 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20 xtract Metadata
40c80 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f About A Column O
40c90 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35 f A Table {H1285
40ca0 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a 0} <S60300>.**.*
40cb0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
40cc0 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20 eturns metadata
40cd0 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 about a specific
40ce0 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65 column of a spe
40cf0 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73 cific.** databas
40d00 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62 e table accessib
40d10 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61 le using the [da
40d20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
40d30 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73 n] handle.** pas
40d40 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
40d50 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
40d60 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f nt..**.** The co
40d70 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69 lumn is identifi
40d80 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 ed by the second
40d90 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 , third and four
40da0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f th parameters to
40db0 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f .** this functio
40dc0 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 n. The second pa
40dd0 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65 rameter is eithe
40de0 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 r the name of th
40df0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69 e database.** (i
40e00 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d .e. "main", "tem
40e10 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65 p" or an attache
40e20 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74 d database) cont
40e30 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69 aining the speci
40e40 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 fied.** table or
40e50 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 NULL. If it is
40e60 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 NULL, then all a
40e70 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
40e80 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a s are searched.*
40e90 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 * for the table
40ea0 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 using the same a
40eb0 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 lgorithm used by
40ec0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e the database en
40ed0 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c gine to.** resol
40ee0 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 ve unqualified t
40ef0 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e able references.
40f00 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 .**.** The third
40f10 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 and fourth para
40f20 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 meters to this f
40f30 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 unction are the
40f40 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e table and column
40f50 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 .** name of the
40f60 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 desired column,
40f70 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65 respectively. Ne
40f80 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70 ither of these p
40f90 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79 arameters.** may
40fa0 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 be NULL..**.**
40fb0 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75 Metadata is retu
40fc0 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 rned by writing
40fd0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f to the memory lo
40fe0 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61 cations passed a
40ff0 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64 s the 5th.** and
41000 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 subsequent para
41010 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 meters to this f
41020 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 unction. Any of
41030 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20 these arguments
41040 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20 may be.** NULL,
41050 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 in which case th
41060 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
41070 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64 element of metad
41080 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a ata is omitted..
41090 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
410a0 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 e>.** <table bor
410b0 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e der="1">.** <tr>
410c0 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c <th> Parameter <
410d0 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79 th> Output<br>Ty
410e0 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70 pe <th> Descrip
410f0 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c tion.**.** <tr><
41100 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e td> 5th <td> con
41110 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61 st char* <td> Da
41120 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c ta type.** <tr><
41130 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e td> 6th <td> con
41140 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61 st char* <td> Na
41150 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f me of default co
41160 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
41170 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68 .** <tr><td> 7th
41180 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 <td> int
41190 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 <td> True if c
411a0 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20 olumn has a NOT
411b0 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a NULL constraint.
411c0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20 ** <tr><td> 8th
411d0 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 <td> int
411e0 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f <td> True if co
411f0 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 lumn is part of
41200 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a the PRIMARY KEY.
41210 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20 ** <tr><td> 9th
41220 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 <td> int
41230 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f <td> True if co
41240 6c 75 6d 6e 20 69 73 20 41 55 54 4f 49 4e 43 52 lumn is AUTOINCR
41250 45 4d 45 4e 54 0a 2a 2a 20 3c 2f 74 61 62 6c 65 EMENT.** </table
41260 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 >.** </blockquot
41270 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d e>.**.** The mem
41280 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 ory pointed to b
41290 79 20 74 68 65 20 63 68 61 72 61 63 74 65 72 20 y the character
412a0 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 pointers returne
412b0 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 65 63 d for the.** dec
412c0 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e laration type an
412d0 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 d collation sequ
412e0 65 6e 63 65 20 69 73 20 76 61 6c 69 64 20 6f 6e ence is valid on
412f0 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 ly until the nex
41300 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 6e 79 t.** call to any
41310 20 53 51 4c 69 74 65 20 41 50 49 20 66 75 6e 63 SQLite API func
41320 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 tion..**.** If t
41330 68 65 20 73 70 65 63 69 66 69 65 64 20 74 61 62 he specified tab
41340 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 le is actually a
41350 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 72 6f 72 view, an [error
41360 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
41370 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
41380 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d specified colum
41390 6e 20 69 73 20 22 72 6f 77 69 64 22 2c 20 22 6f n is "rowid", "o
413a0 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22 id" or "_rowid_"
413b0 20 61 6e 64 20 61 6e 0a 2a 2a 20 49 4e 54 45 47 and an.** INTEG
413c0 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 ER PRIMARY KEY c
413d0 6f 6c 75 6d 6e 20 68 61 73 20 62 65 65 6e 20 65 olumn has been e
413e0 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 xplicitly declar
413f0 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 74 ed, then the out
41400 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 put.** parameter
41410 73 20 61 72 65 20 73 65 74 20 66 6f 72 20 74 68 s are set for th
41420 65 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 e explicitly dec
41430 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 49 66 lared column. If
41440 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 there is no.**
41450 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 explicitly decla
41460 72 65 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d red INTEGER PRIM
41470 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2c 20 ARY KEY column,
41480 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a then the output.
41490 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 ** parameters ar
414a0 65 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 e set as follows
414b0 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a :.**.** <pre>.**
414c0 20 20 20 20 20 64 61 74 61 20 74 79 70 65 3a 20 data type:
414d0 22 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 20 20 "INTEGER".**
414e0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
414f0 6e 63 65 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a nce: "BINARY".**
41500 20 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30 not null: 0
41510 0a 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 79 20 .** primary
41520 6b 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75 key: 1.** au
41530 74 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a to increment: 0.
41540 2a 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 ** </pre>.**.**
41550 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 This function ma
41560 79 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f y load one or mo
41570 72 65 20 73 63 68 65 6d 61 73 20 66 72 6f 6d 20 re schemas from
41580 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 database files.
41590 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f If an.** error o
415a0 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 69 ccurs during thi
415b0 73 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 s process, or if
415c0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74 the requested t
415d0 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 0a 2a able or column.*
415e0 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e * cannot be foun
415f0 64 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 d, an [error cod
41600 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 e] is returned a
41610 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 nd an error mess
41620 61 67 65 20 6c 65 66 74 0a 2a 2a 20 69 6e 20 74 age left.** in t
41630 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
41640 6e 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62 65 20 nection] (to be
41650 72 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 retrieved using
41660 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
41670 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 )..**.** This AP
41680 49 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 I is only availa
41690 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 ble if the libra
416a0 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 ry was compiled
416b0 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c with the.** [SQL
416c0 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d ITE_ENABLE_COLUM
416d0 4e 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 N_METADATA] C-pr
416e0 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f eprocessor symbo
416f0 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 l defined..*/.SQ
41700 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
41710 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d ite3_table_colum
41720 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71 n_metadata(. sq
41730 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
41740 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e /* Con
41750 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a nection handle *
41760 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
41770 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 zDbName,
41780 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65 /* Database name
41790 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f or NULL */. co
417a0 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 nst char *zTable
417b0 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 Name, /* Tab
417c0 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e le name */. con
417d0 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e st char *zColumn
417e0 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 Name, /* Colu
417f0 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 mn name */. cha
41800 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 r const **pzData
41810 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 Type, /* OUTP
41820 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74 UT: Declared dat
41830 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 a type */. char
41840 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 const **pzCollS
41850 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 eq, /* OUTPU
41860 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 T: Collation seq
41870 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 uence name */.
41880 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 int *pNotNull,
41890 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
418a0 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e UTPUT: True if N
418b0 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 OT NULL constrai
418c0 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 nt exists */. i
418d0 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c nt *pPrimaryKey,
418e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 /* OU
418f0 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f TPUT: True if co
41900 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 lumn part of PK
41910 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 */. int *pAutoi
41920 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nc
41930 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 /* OUTPUT: True
41940 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 if column is au
41950 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a to-increment */.
41960 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
41970 45 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 EF: Load An Exte
41980 6e 73 69 6f 6e 20 7b 48 31 32 36 30 30 7d 20 3c nsion {H12600} <
41990 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S20500>.**.** Th
419a0 69 73 20 69 6e 74 65 72 66 61 63 65 20 6c 6f 61 is interface loa
419b0 64 73 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74 ds an SQLite ext
419c0 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 66 ension library f
419d0 72 6f 6d 20 74 68 65 20 6e 61 6d 65 64 20 66 69 rom the named fi
419e0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 le..**.** {H1260
419f0 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 1} The sqlite3_l
41a00 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 oad_extension()
41a10 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 interface attemp
41a20 74 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a ts to load an.**
41a30 20 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 SQLite
41a40 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 extension libra
41a50 72 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 ry contained in
41a60 74 68 65 20 66 69 6c 65 20 7a 46 69 6c 65 2e 0a the file zFile..
41a70 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 32 7d 20 54 **.** {H12602} T
41a80 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 he entry point i
41a90 73 20 7a 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b s zProc..**.** {
41aa0 48 31 32 36 30 33 7d 20 7a 50 72 6f 63 20 6d 61 H12603} zProc ma
41ab0 79 20 62 65 20 30 2c 20 69 6e 20 77 68 69 63 68 y be 0, in which
41ac0 20 63 61 73 65 20 74 68 65 20 6e 61 6d 65 20 6f case the name o
41ad0 66 20 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e f the entry poin
41ae0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 65 t.** de
41af0 66 61 75 6c 74 73 20 74 6f 20 22 73 71 6c 69 74 faults to "sqlit
41b00 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 e3_extension_ini
41b10 74 22 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 t"..**.** {H1260
41b20 34 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 4} The sqlite3_l
41b30 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 oad_extension()
41b40 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 interface shall
41b50 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 return.**
41b60 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f [SQLITE_OK] o
41b70 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53 n success and [S
41b80 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 QLITE_ERROR] if
41b90 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 something goes w
41ba0 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 rong..**.** {H12
41bb0 36 30 35 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 605} If an error
41bc0 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 occurs and pzEr
41bd0 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 rMsg is not 0, t
41be0 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 hen the.**
41bf0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 [sqlite3_loa
41c00 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 69 d_extension()] i
41c10 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 61 nterface shall a
41c20 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20 ttempt to.**
41c30 20 20 20 20 20 20 66 69 6c 6c 20 2a 70 7a 45 72 fill *pzEr
41c40 72 4d 73 67 20 77 69 74 68 20 65 72 72 6f 72 20 rMsg with error
41c50 6d 65 73 73 61 67 65 20 74 65 78 74 20 73 74 6f message text sto
41c60 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a red in memory.**
41c70 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e obtain
41c80 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
41c90 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 7b 45 4e 44 _malloc()]. {END
41ca0 7d 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 } The calling f
41cb0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 unction.**
41cc0 20 20 20 20 73 68 6f 75 6c 64 20 66 72 65 65 20 should free
41cd0 74 68 69 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 this memory by c
41ce0 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f alling [sqlite3_
41cf0 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b free()]..**.** {
41d00 48 31 32 36 30 36 7d 20 45 78 74 65 6e 73 69 6f H12606} Extensio
41d10 6e 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74 20 62 n loading must b
41d20 65 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 0a e enabled using.
41d30 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
41d40 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 ite3_enable_load
41d50 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 _extension()] pr
41d60 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 ior to calling t
41d70 68 69 73 20 41 50 49 2c 0a 2a 2a 20 20 20 20 20 his API,.**
41d80 20 20 20 20 20 6f 74 68 65 72 77 69 73 65 20 61 otherwise a
41d90 6e 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 n error will be
41da0 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
41db0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
41dc0 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 te3_load_extensi
41dd0 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 on(. sqlite3 *d
41de0 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c b, /* L
41df0 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f oad the extensio
41e00 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 n into this data
41e10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
41e20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
41e30 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 *zFile, /* Na
41e40 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 me of the shared
41e50 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e library contain
41e60 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f ing extension */
41e70 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
41e80 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 Proc, /* Entr
41e90 79 20 70 6f 69 6e 74 2e 20 20 44 65 72 69 76 65 y point. Derive
41ea0 64 20 66 72 6f 6d 20 7a 46 69 6c 65 20 69 66 20 d from zFile if
41eb0 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 0 */. char **pz
41ec0 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 ErrMsg /*
41ed0 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 Put error messag
41ee0 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20 e here if not 0
41ef0 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.);../*.** CAP
41f00 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 I3REF: Enable Or
41f10 20 44 69 73 61 62 6c 65 20 45 78 74 65 6e 73 69 Disable Extensi
41f20 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 on Loading {H126
41f30 32 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 20} <S20500>.**.
41f40 2a 2a 20 53 6f 20 61 73 20 6e 6f 74 20 74 6f 20 ** So as not to
41f50 6f 70 65 6e 20 73 65 63 75 72 69 74 79 20 68 6f open security ho
41f60 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 les in older app
41f70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 61 lications that a
41f80 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61 72 65 64 re.** unprepared
41f90 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 65 78 to deal with ex
41fa0 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 2c tension loading,
41fb0 20 61 6e 64 20 61 73 20 61 20 6d 65 61 6e 73 20 and as a means
41fc0 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a 2a 2a 20 of disabling.**
41fd0 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e extension loadin
41fe0 67 20 77 68 69 6c 65 20 65 76 61 6c 75 61 74 69 g while evaluati
41ff0 6e 67 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 ng user-entered
42000 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 SQL, the followi
42010 6e 67 20 41 50 49 0a 2a 2a 20 69 73 20 70 72 6f ng API.** is pro
42020 76 69 64 65 64 20 74 6f 20 74 75 72 6e 20 74 68 vided to turn th
42030 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f e [sqlite3_load_
42040 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 6d 65 63 extension()] mec
42050 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64 20 6f 66 hanism on and of
42060 66 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 f..**.** Extensi
42070 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 on loading is of
42080 66 20 62 79 20 64 65 66 61 75 6c 74 2e 20 53 65 f by default. Se
42090 65 20 74 69 63 6b 65 74 20 23 31 38 36 33 2e 0a e ticket #1863..
420a0 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 31 7d 20 43 **.** {H12621} C
420b0 61 6c 6c 20 74 68 65 20 73 71 6c 69 74 65 33 5f all the sqlite3_
420c0 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 enable_load_exte
420d0 6e 73 69 6f 6e 28 29 20 72 6f 75 74 69 6e 65 20 nsion() routine
420e0 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 31 0a 2a 2a with onoff==1.**
420f0 20 20 20 20 20 20 20 20 20 20 74 6f 20 74 75 72 to tur
42100 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 n extension load
42110 69 6e 67 20 6f 6e 20 61 6e 64 20 63 61 6c 6c 20 ing on and call
42120 69 74 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 30 it with onoff==0
42130 20 74 6f 20 74 75 72 6e 0a 2a 2a 20 20 20 20 20 to turn.**
42140 20 20 20 20 20 69 74 20 62 61 63 6b 20 6f 66 66 it back off
42150 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 again..**.** {H
42160 31 32 36 32 32 7d 20 45 78 74 65 6e 73 69 6f 6e 12622} Extension
42170 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 66 20 loading is off
42180 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 53 by default..*/.S
42190 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
421a0 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 lite3_enable_loa
421b0 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 d_extension(sqli
421c0 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f te3 *db, int ono
421d0 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ff);../*.** CAPI
421e0 33 52 45 46 3a 20 41 75 74 6f 6d 61 74 69 63 61 3REF: Automatica
421f0 6c 6c 79 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 lly Load An Exte
42200 6e 73 69 6f 6e 73 20 7b 48 31 32 36 34 30 7d 20 nsions {H12640}
42210 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S20500>.**.** T
42220 68 69 73 20 41 50 49 20 63 61 6e 20 62 65 20 69 his API can be i
42230 6e 76 6f 6b 65 64 20 61 74 20 70 72 6f 67 72 61 nvoked at progra
42240 6d 20 73 74 61 72 74 75 70 20 69 6e 20 6f 72 64 m startup in ord
42250 65 72 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a er to register.*
42260 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 74 * one or more st
42270 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 atically linked
42280 65 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 extensions that
42290 77 69 6c 6c 20 62 65 20 61 76 61 69 6c 61 62 6c will be availabl
422a0 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e 65 77 20 e.** to all new
422b0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
422c0 74 69 6f 6e 73 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a tions]. {END}.**
422d0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
422e0 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 stores a pointe
422f0 72 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 r to the extensi
42300 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 20 74 on in an array t
42310 68 61 74 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e hat is.** obtain
42320 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
42330 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 49 66 20 _malloc()]. If
42340 79 6f 75 20 72 75 6e 20 61 20 6d 65 6d 6f 72 79 you run a memory
42350 20 6c 65 61 6b 20 63 68 65 63 6b 65 72 0a 2a 2a leak checker.**
42360 20 6f 6e 20 79 6f 75 72 20 70 72 6f 67 72 61 6d on your program
42370 20 61 6e 64 20 69 74 20 72 65 70 6f 72 74 73 20 and it reports
42380 61 20 6c 65 61 6b 20 62 65 63 61 75 73 65 20 6f a leak because o
42390 66 20 74 68 69 73 20 61 72 72 61 79 2c 20 69 6e f this array, in
423a0 76 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 voke.** [sqlite3
423b0 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 _reset_auto_exte
423c0 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 nsion()] prior t
423d0 6f 20 73 68 75 74 64 6f 77 6e 20 74 6f 20 66 72 o shutdown to fr
423e0 65 65 20 74 68 65 20 6d 65 6d 6f 72 79 2e 0a 2a ee the memory..*
423f0 2a 0a 2a 2a 20 7b 48 31 32 36 34 31 7d 20 54 68 *.** {H12641} Th
42400 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 is function regi
42410 73 74 65 72 73 20 61 6e 20 65 78 74 65 6e 73 69 sters an extensi
42420 6f 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 74 on entry point t
42430 68 61 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 hat is.**
42440 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 automatically
42450 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 invoked wheneve
42460 72 20 61 20 6e 65 77 20 5b 64 61 74 61 62 61 73 r a new [databas
42470 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a e connection].**
42480 20 20 20 20 20 20 20 20 20 20 69 73 20 6f 70 65 is ope
42490 6e 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ned using [sqlit
424a0 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c e3_open()], [sql
424b0 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a ite3_open16()],.
424c0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 5b ** or [
424d0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
424e0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 )]..**.** {H1264
424f0 32 7d 20 44 75 70 6c 69 63 61 74 65 20 65 78 74 2} Duplicate ext
42500 65 6e 73 69 6f 6e 73 20 61 72 65 20 64 65 74 65 ensions are dete
42510 63 74 65 64 20 73 6f 20 63 61 6c 6c 69 6e 67 20 cted so calling
42520 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
42530 20 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c multipl
42540 65 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 e times with the
42550 20 73 61 6d 65 20 65 78 74 65 6e 73 69 6f 6e 20 same extension
42560 69 73 20 68 61 72 6d 6c 65 73 73 2e 0a 2a 2a 0a is harmless..**.
42570 2a 2a 20 7b 48 31 32 36 34 33 7d 20 54 68 69 73 ** {H12643} This
42580 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 routine stores
42590 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
425a0 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e extension in an
425b0 20 61 72 72 61 79 0a 2a 2a 20 20 20 20 20 20 20 array.**
425c0 20 20 20 74 68 61 74 20 69 73 20 6f 62 74 61 69 that is obtai
425d0 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 ned from [sqlite
425e0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 3_malloc()]..**.
425f0 2a 2a 20 7b 48 31 32 36 34 34 7d 20 41 75 74 6f ** {H12644} Auto
42600 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 matic extensions
42610 20 61 70 70 6c 79 20 61 63 72 6f 73 73 20 61 6c apply across al
42620 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 l threads..*/.SQ
42630 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
42640 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 ite3_auto_extens
42650 69 6f 6e 28 76 6f 69 64 20 2a 78 45 6e 74 72 79 ion(void *xEntry
42660 50 6f 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 Point);../*.** C
42670 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 API3REF: Reset A
42680 75 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 utomatic Extensi
42690 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 on Loading {H126
426a0 36 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 60} <S20500>.**.
426b0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
426c0 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 disables all pr
426d0 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 eviously registe
426e0 72 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a red automatic.**
426f0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e extensions. {EN
42700 44 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 D} It undoes th
42710 65 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 e effect of all
42720 70 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 prior.** [sqlite
42730 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 3_auto_extension
42740 28 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a ()] calls..**.**
42750 20 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 {H12661} This f
42760 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 unction disables
42770 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 all previously
42780 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 registered.**
42790 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 automatic
427a0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a extensions..**.
427b0 2a 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 ** {H12662} This
427c0 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c function disabl
427d0 65 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 es automatic ext
427e0 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 ensions in all t
427f0 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 hreads..*/.SQLIT
42800 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
42810 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 e3_reset_auto_ex
42820 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a tension(void);..
42830 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 /*.****** EXPERI
42840 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 MENTAL - subject
42850 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f to change witho
42860 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a ut notice ******
42870 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
42880 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 he interface to
42890 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c the virtual-tabl
428a0 65 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 e mechanism is c
428b0 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 urrently conside
428c0 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 red.** to be exp
428d0 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 erimental. The
428e0 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 interface might
428f0 63 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 change in incomp
42900 61 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 atible ways..**
42910 49 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f If this is a pro
42920 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f blem for you, do
42930 20 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 not use the int
42940 65 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74 erface at this t
42950 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ime..**.** When
42960 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c the virtual-tabl
42970 65 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 e mechanism stab
42980 69 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 ilizes, we will
42990 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 declare the.** i
429a0 6e 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 nterface fixed,
429b0 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 support it indef
429c0 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d initely, and rem
429d0 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 ove this comment
429e0 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 ..*/../*.** Stru
429f0 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 ctures used by t
42a00 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
42a10 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 interface.*/.ty
42a20 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
42a30 69 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 ite3_vtab sqlite
42a40 33 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 3_vtab;.typedef
42a50 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
42a60 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 ndex_info sqlite
42a70 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 3_index_info;.ty
42a80 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
42a90 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
42aa0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 sqlite3_vtab_cu
42ab0 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 rsor;.typedef st
42ac0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 ruct sqlite3_mod
42ad0 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 ule sqlite3_modu
42ae0 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 le;../*.** CAPI3
42af0 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 REF: Virtual Tab
42b00 6c 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 le Object {H1800
42b10 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 0} <S20400>.** K
42b20 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 EYWORDS: sqlite3
42b30 5f 6d 6f 64 75 6c 65 0a 2a 2a 20 45 58 50 45 52 _module.** EXPER
42b40 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 20 IMENTAL.**.** A
42b50 6d 6f 64 75 6c 65 20 69 73 20 61 20 63 6c 61 73 module is a clas
42b60 73 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 s of virtual tab
42b70 6c 65 73 2e 20 20 45 61 63 68 20 6d 6f 64 75 6c les. Each modul
42b80 65 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 e is defined.**
42b90 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f by an instance o
42ba0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
42bb0 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 structure. This
42bc0 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 structure consi
42bd0 73 74 73 0a 2a 2a 20 6d 6f 73 74 6c 79 20 6f 66 sts.** mostly of
42be0 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 methods for the
42bf0 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 module..**.** T
42c00 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 his interface is
42c10 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e experimental an
42c20 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20 d is subject to
42c30 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d change or.** rem
42c40 6f 76 61 6c 20 69 6e 20 66 75 74 75 72 65 20 72 oval in future r
42c50 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 eleases of SQLit
42c60 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c e..*/.struct sql
42c70 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 ite3_module {.
42c80 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 int iVersion;.
42c90 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73 int (*xCreate)(s
42ca0 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 qlite3*, void *p
42cb0 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Aux,.
42cc0 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f int argc, co
42cd0 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a nst char *const*
42ce0 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 argv,.
42cf0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 sqlite3_vta
42d00 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 b **ppVTab, char
42d10 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f **);. int (*xCo
42d20 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c nnect)(sqlite3*,
42d30 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 void *pAux,.
42d40 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
42d50 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 argc, const char
42d60 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 *const*argv,.
42d70 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
42d80 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 ite3_vtab **ppVT
42d90 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 ab, char**);. i
42da0 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 nt (*xBestIndex)
42db0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
42dc0 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e VTab, sqlite3_in
42dd0 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e dex_info*);. in
42de0 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 t (*xDisconnect)
42df0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
42e00 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 VTab);. int (*x
42e10 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 Destroy)(sqlite3
42e20 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 _vtab *pVTab);.
42e30 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 int (*xOpen)(sq
42e40 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 lite3_vtab *pVTa
42e50 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f b, sqlite3_vtab_
42e60 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f cursor **ppCurso
42e70 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f r);. int (*xClo
42e80 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 se)(sqlite3_vtab
42e90 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 _cursor*);. int
42ea0 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69 (*xFilter)(sqli
42eb0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a te3_vtab_cursor*
42ec0 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f , int idxNum, co
42ed0 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 nst char *idxStr
42ee0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
42ef0 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 int argc, sqli
42f00 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
42f10 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 );. int (*xNext
42f20 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 )(sqlite3_vtab_c
42f30 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 ursor*);. int (
42f40 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 *xEof)(sqlite3_v
42f50 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 tab_cursor*);.
42f60 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 int (*xColumn)(s
42f70 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
42f80 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e or*, sqlite3_con
42f90 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 text*, int);. i
42fa0 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c nt (*xRowid)(sql
42fb0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
42fc0 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
42fd0 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 *pRowid);. int
42fe0 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69 (*xUpdate)(sqli
42ff0 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c te3_vtab *, int,
43000 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
43010 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
43020 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 *);. int (*xBe
43030 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 gin)(sqlite3_vta
43040 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 b *pVTab);. int
43050 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65 (*xSync)(sqlite
43060 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 3_vtab *pVTab);.
43070 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 int (*xCommit)
43080 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
43090 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 VTab);. int (*x
430a0 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 Rollback)(sqlite
430b0 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 3_vtab *pVTab);.
430c0 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e int (*xFindFun
430d0 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 ction)(sqlite3_v
430e0 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 tab *pVtab, int
430f0 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 nArg, const char
43100 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 *zName,.
43110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43120 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 void (**pxFunc)(
43130 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
43140 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
43150 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 ue**),.
43160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f vo
43170 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 id **ppArg);. i
43180 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 nt (*xRename)(sq
43190 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
431a0 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
431b0 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 New);.};../*.**
431c0 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 CAPI3REF: Virtua
431d0 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 l Table Indexing
431e0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 Information {H1
431f0 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 8100} <S20400>.*
43200 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 * KEYWORDS: sqli
43210 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a te3_index_info.*
43220 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
43230 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
43240 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 _index_info stru
43250 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 cture and its su
43260 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75 bstructures is u
43270 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 sed to.** pass i
43280 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 nformation into
43290 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20 and receive the
432a0 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 78 reply from the x
432b0 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 BestIndex.** met
432c0 68 6f 64 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 hod of an sqlite
432d0 33 5f 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 66 3_module. The f
432e0 69 65 6c 64 73 20 75 6e 64 65 72 20 2a 2a 49 6e ields under **In
432f0 70 75 74 73 2a 2a 20 61 72 65 20 74 68 65 0a 2a puts** are the.*
43300 2a 20 69 6e 70 75 74 73 20 74 6f 20 78 42 65 73 * inputs to xBes
43310 74 49 6e 64 65 78 20 61 6e 64 20 61 72 65 20 72 tIndex and are r
43320 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 65 73 74 ead-only. xBest
43330 49 6e 64 65 78 20 69 6e 73 65 72 74 73 20 69 74 Index inserts it
43340 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 s.** results int
43350 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75 74 73 2a o the **Outputs*
43360 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 * fields..**.**
43370 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b The aConstraint[
43380 5d 20 61 72 72 61 79 20 72 65 63 6f 72 64 73 20 ] array records
43390 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e WHERE clause con
433a0 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 20 straints of the
433b0 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 form:.**.** <pre
433c0 3e 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 70 72 3c >column OP expr<
433d0 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 /pre>.**.** wher
433e0 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c 74 3b 2c e OP is =, <,
433f0 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c 20 6f 72 <=, >, or
43400 20 26 67 74 3b 3d 2e 20 20 54 68 65 20 70 61 72 >=. The par
43410 74 69 63 75 6c 61 72 20 6f 70 65 72 61 74 6f 72 ticular operator
43420 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e is.** stored in
43430 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 6f aConstraint[].o
43440 70 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 p. The index of
43450 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 73 the column is s
43460 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f 6e tored in.** aCon
43470 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d straint[].iColum
43480 6e 2e 20 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b n. aConstraint[
43490 5d 2e 75 73 61 62 6c 65 20 69 73 20 54 52 55 45 ].usable is TRUE
434a0 20 69 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 20 if the.** expr
434b0 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e on the right-han
434c0 64 20 73 69 64 65 20 63 61 6e 20 62 65 20 65 76 d side can be ev
434d0 61 6c 75 61 74 65 64 20 28 61 6e 64 20 74 68 75 aluated (and thu
434e0 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 s the constraint
434f0 0a 2a 2a 20 69 73 20 75 73 61 62 6c 65 29 20 61 .** is usable) a
43500 6e 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 63 nd false if it c
43510 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 annot..**.** The
43520 20 6f 70 74 69 6d 69 7a 65 72 20 61 75 74 6f 6d optimizer autom
43530 61 74 69 63 61 6c 6c 79 20 69 6e 76 65 72 74 73 atically inverts
43540 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f terms of the fo
43550 72 6d 20 22 65 78 70 72 20 4f 50 20 63 6f 6c 75 rm "expr OP colu
43560 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65 73 mn".** and makes
43570 20 6f 74 68 65 72 20 73 69 6d 70 6c 69 66 69 63 other simplific
43580 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 57 48 ations to the WH
43590 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 61 6e ERE clause in an
435a0 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 67 attempt to.** g
435b0 65 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52 45 et as many WHERE
435c0 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 6e clause terms in
435d0 74 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f 77 to the form show
435e0 6e 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73 69 n above as possi
435f0 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 43 6f 6e ble..** The aCon
43600 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 straint[] array
43610 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48 45 only reports WHE
43620 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 RE clause terms
43630 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a in the correct.*
43640 2a 20 66 6f 72 6d 20 74 68 61 74 20 72 65 66 65 * form that refe
43650 72 20 74 6f 20 74 68 65 20 70 61 72 74 69 63 75 r to the particu
43660 6c 61 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c lar virtual tabl
43670 65 20 62 65 69 6e 67 20 71 75 65 72 69 65 64 2e e being queried.
43680 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 .**.** Informati
43690 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52 44 on about the ORD
436a0 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 ER BY clause is
436b0 73 74 6f 72 65 64 20 69 6e 20 61 4f 72 64 65 72 stored in aOrder
436c0 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 20 74 65 By[]..** Each te
436d0 72 6d 20 6f 66 20 61 4f 72 64 65 72 42 79 20 72 rm of aOrderBy r
436e0 65 63 6f 72 64 73 20 61 20 63 6f 6c 75 6d 6e 20 ecords a column
436f0 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 of the ORDER BY
43700 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 clause..**.** Th
43710 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 e xBestIndex met
43720 68 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 61 43 hod must fill aC
43730 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d onstraintUsage[]
43740 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f with informatio
43750 6e 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74 20 n.** about what
43760 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70 61 parameters to pa
43770 73 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20 20 ss to xFilter.
43780 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20 74 If argvIndex>0 t
43790 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 68 74 hen.** the right
437a0 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 -hand side of th
437b0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
437c0 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69 73 aConstraint[] is
437d0 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 61 6e evaluated.** an
437e0 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 61 72 d becomes the ar
437f0 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 72 79 gvIndex-th entry
43800 20 69 6e 20 61 72 67 76 2e 20 20 49 66 20 61 43 in argv. If aC
43810 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d onstraintUsage[]
43820 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72 75 65 .omit.** is true
43830 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 , then the const
43840 72 61 69 6e 74 20 69 73 20 61 73 73 75 6d 65 64 raint is assumed
43850 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 68 61 6e to be fully han
43860 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 76 dled by the.** v
43870 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 irtual table and
43880 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64 20 is not checked
43890 61 67 61 69 6e 20 62 79 20 53 51 4c 69 74 65 2e again by SQLite.
438a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 78 4e 75 .**.** The idxNu
438b0 6d 20 61 6e 64 20 69 64 78 50 74 72 20 76 61 6c m and idxPtr val
438c0 75 65 73 20 61 72 65 20 72 65 63 6f 72 64 65 64 ues are recorded
438d0 20 61 6e 64 20 70 61 73 73 65 64 20 69 6e 74 6f and passed into
438e0 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 20 73 71 6c xFilter..** sql
438f0 69 74 65 33 5f 66 72 65 65 28 29 20 69 73 20 75 ite3_free() is u
43900 73 65 64 20 74 6f 20 66 72 65 65 20 69 64 78 50 sed to free idxP
43910 74 72 20 69 66 20 6e 65 65 64 54 6f 46 72 65 65 tr if needToFree
43920 49 64 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a IdxPtr is true..
43930 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 **.** The orderB
43940 79 43 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 yConsumed means
43950 74 68 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d that output from
43960 20 78 46 69 6c 74 65 72 20 77 69 6c 6c 20 6f 63 xFilter will oc
43970 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65 20 63 6f cur in.** the co
43980 72 72 65 63 74 20 6f 72 64 65 72 20 74 6f 20 73 rrect order to s
43990 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 atisfy the ORDER
439a0 20 42 59 20 63 6c 61 75 73 65 20 73 6f 20 74 68 BY clause so th
439b0 61 74 20 6e 6f 20 73 65 70 61 72 61 74 65 0a 2a at no separate.*
439c0 2a 20 73 6f 72 74 69 6e 67 20 73 74 65 70 20 69 * sorting step i
439d0 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a s required..**.*
439e0 2a 20 54 68 65 20 65 73 74 69 6d 61 74 65 64 43 * The estimatedC
439f0 6f 73 74 20 76 61 6c 75 65 20 69 73 20 61 6e 20 ost value is an
43a00 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 estimate of the
43a10 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67 20 74 68 cost of doing th
43a20 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 e.** particular
43a30 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75 6c 6c 20 lookup. A full
43a40 73 63 61 6e 20 6f 66 20 61 20 74 61 62 6c 65 20 scan of a table
43a50 77 69 74 68 20 4e 20 65 6e 74 72 69 65 73 20 73 with N entries s
43a60 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 61 20 hould have.** a
43a70 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41 20 62 69 cost of N. A bi
43a80 6e 61 72 79 20 73 65 61 72 63 68 20 6f 66 20 61 nary search of a
43a90 20 74 61 62 6c 65 20 6f 66 20 4e 20 65 6e 74 72 table of N entr
43aa0 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 ies should have
43ab0 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 61 70 70 a.** cost of app
43ac0 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f 67 28 4e roximately log(N
43ad0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e )..**.** This in
43ae0 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 terface is exper
43af0 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 imental and is s
43b00 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 ubject to change
43b10 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 or.** removal i
43b20 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
43b30 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a s of SQLite..*/.
43b40 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
43b50 6e 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a ndex_info {. /*
43b60 20 49 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 Inputs */. int
43b70 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 nConstraint;
43b80 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
43b90 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
43ba0 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 aConstraint */.
43bb0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
43bc0 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 index_constraint
43bd0 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c {. int iCol
43be0 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 umn;
43bf0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c /* Column on l
43c00 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 eft-hand side of
43c10 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 constraint */.
43c20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 unsigned cha
43c30 72 20 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a r op; /*
43c40 20 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 Constraint oper
43c50 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 ator */. uns
43c60 69 67 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c igned char usabl
43c70 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 e; /* True i
43c80 66 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e f this constrain
43c90 74 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 t is usable */.
43ca0 20 20 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 int iTermOff
43cb0 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a set; /*
43cc0 20 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 Used internally
43cd0 20 2d 20 78 42 65 73 74 49 6e 64 65 78 20 73 68 - xBestIndex sh
43ce0 6f 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 ould ignore */.
43cf0 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b } *aConstraint;
43d00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
43d10 61 62 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c able of WHERE cl
43d20 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 ause constraints
43d30 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 */. int nOrder
43d40 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 By;
43d50 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 /* Number of te
43d60 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 rms in the ORDER
43d70 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 BY clause */.
43d80 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
43d90 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 ndex_orderby {.
43da0 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b int iColumn;
43db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
43dc0 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a Column number *
43dd0 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 /. unsigned
43de0 63 68 61 72 20 64 65 73 63 3b 20 20 20 20 20 20 char desc;
43df0 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 /* True for DES
43e00 43 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53 C. False for AS
43e10 43 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 C. */. } *aOrde
43e20 72 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 rBy;
43e30 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 /* The ORDER
43e40 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f BY clause */. /
43e50 2a 20 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 * Outputs */. s
43e60 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
43e70 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 dex_constraint_u
43e80 73 61 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 sage {. int a
43e90 72 67 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20 rgvIndex;
43ea0 20 20 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f /* if >0, co
43eb0 6e 73 74 72 61 69 6e 74 20 69 73 20 70 61 72 74 nstraint is part
43ec0 20 6f 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c of argv to xFil
43ed0 74 65 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 ter */. unsig
43ee0 6e 65 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 ned char omit;
43ef0 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f /* Do not co
43f00 64 65 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 de a test for th
43f10 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f is constraint */
43f20 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e . } *aConstrain
43f30 74 55 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 tUsage;. int id
43f40 78 4e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 xNum;
43f50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 /* Number u
43f60 73 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20 sed to identify
43f70 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 the index */. c
43f80 68 61 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20 har *idxStr;
43f90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 /* Str
43fa0 69 6e 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 ing, possibly ob
43fb0 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
43fc0 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 te3_malloc */.
43fd0 69 6e 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64 int needToFreeId
43fe0 78 53 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 xStr; /* Fr
43ff0 65 65 20 69 64 78 53 74 72 20 75 73 69 6e 67 20 ee idxStr using
44000 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 sqlite3_free() i
44010 66 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 f true */. int
44020 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b orderByConsumed;
44030 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
44040 66 20 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65 f output is alre
44050 61 64 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 ady ordered */.
44060 20 64 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65 double estimate
44070 64 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 dCost; /* E
44080 73 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 stimated cost of
44090 20 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 using this inde
440a0 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 x */.};.#define
440b0 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e SQLITE_INDEX_CON
440c0 53 54 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a STRAINT_EQ 2.
440d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
440e0 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f NDEX_CONSTRAINT_
440f0 47 54 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 GT 4.#define
44100 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e SQLITE_INDEX_CON
44110 53 54 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a STRAINT_LE 8.
44120 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
44130 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f NDEX_CONSTRAINT_
44140 4c 54 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 LT 16.#define
44150 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f SQLITE_INDEX_CO
44160 4e 53 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33 NSTRAINT_GE 3
44170 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
44180 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e _INDEX_CONSTRAIN
44190 54 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a T_MATCH 64../*.*
441a0 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 * CAPI3REF: Regi
441b0 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 ster A Virtual T
441c0 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 able Implementat
441d0 69 6f 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 ion {H18200} <S2
441e0 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0400>.** EXPERIM
441f0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ENTAL.**.** This
44200 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
44210 20 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e to register a n
44220 65 77 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 77 ew module name w
44230 69 74 68 20 61 0a 2a 2a 20 5b 64 61 74 61 62 61 ith a.** [databa
44240 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 se connection].
44250 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 Module names mu
44260 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64 st be registered
44270 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 before.** creat
44280 69 6e 67 20 6e 65 77 20 76 69 72 74 75 61 6c 20 ing new virtual
44290 74 61 62 6c 65 73 20 6f 6e 20 74 68 65 20 6d 6f tables on the mo
442a0 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 65 20 dule, or before
442b0 75 73 69 6e 67 0a 2a 2a 20 70 72 65 65 78 69 73 using.** preexis
442c0 74 69 6e 67 20 76 69 72 74 75 61 6c 20 74 61 62 ting virtual tab
442d0 6c 65 73 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c les of the modul
442e0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e e..**.** This in
442f0 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 terface is exper
44300 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 imental and is s
44310 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 ubject to change
44320 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 or.** removal i
44330 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
44340 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a s of SQLite..*/.
44350 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
44360 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 E_EXPERIMENTAL i
44370 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
44380 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 e_module(. sqli
44390 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
443a0 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 /* SQLite
443b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 connection to r
443c0 65 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 egister module w
443d0 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ith */. const c
443e0 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 har *zName,
443f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
44400 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 he module */. c
44410 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
44420 75 6c 65 20 2a 2c 20 20 20 20 2f 2a 20 4d 65 74 ule *, /* Met
44430 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 hods for the mod
44440 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 20 ule */. void *
44450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44460 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 /* Client da
44470 74 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 ta for xCreate/x
44480 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f Connect */.);../
44490 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 *.** CAPI3REF: R
444a0 65 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61 egister A Virtua
444b0 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e l Table Implemen
444c0 74 61 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 tation {H18210}
444d0 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 <S20400>.** EXPE
444e0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
444f0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 his routine is i
44500 64 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 dentical to the
44510 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
44520 6d 6f 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 module()] method
44530 20 61 62 6f 76 65 2c 0a 2a 2a 20 65 78 63 65 70 above,.** excep
44540 74 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 77 73 t that it allows
44550 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 a destructor fu
44560 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 73 70 65 nction to be spe
44570 63 69 66 69 65 64 2e 20 49 74 20 69 73 0a 2a 2a cified. It is.**
44580 20 65 76 65 6e 20 6d 6f 72 65 20 65 78 70 65 72 even more exper
44590 69 6d 65 6e 74 61 6c 20 74 68 61 6e 20 74 68 65 imental than the
445a0 20 72 65 73 74 20 6f 66 20 74 68 65 20 76 69 72 rest of the vir
445b0 74 75 61 6c 20 74 61 62 6c 65 73 20 41 50 49 2e tual tables API.
445c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
445d0 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
445e0 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 AL int sqlite3_c
445f0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 reate_module_v2(
44600 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
44610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
44620 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69 SQLite connecti
44630 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d on to register m
44640 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20 odule with */.
44650 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
44660 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 e, /* Na
44670 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 me of the module
44680 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 */. const sqli
44690 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c 20 20 20 te3_module *,
446a0 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 /* Methods for
446b0 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 the module */.
446c0 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20 void *,
446d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6c /* Cl
446e0 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20 78 43 ient data for xC
446f0 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a reate/xConnect *
44700 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65 73 74 72 /. void(*xDestr
44710 6f 79 29 28 76 6f 69 64 2a 29 20 20 20 20 20 2f oy)(void*) /
44720 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 * Module destruc
44730 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a tor function */.
44740 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
44750 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c EF: Virtual Tabl
44760 65 20 49 6e 73 74 61 6e 63 65 20 4f 62 6a 65 63 e Instance Objec
44770 74 20 7b 48 31 38 30 31 30 7d 20 3c 53 32 30 34 t {H18010} <S204
44780 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 00>.** KEYWORDS:
44790 20 73 71 6c 69 74 65 33 5f 76 74 61 62 0a 2a 2a sqlite3_vtab.**
447a0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
447b0 0a 2a 2a 20 45 76 65 72 79 20 6d 6f 64 75 6c 65 .** Every module
447c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
447d0 75 73 65 73 20 61 20 73 75 62 63 6c 61 73 73 20 uses a subclass
447e0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
447f0 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 6f structure.** to
44800 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72 74 describe a part
44810 69 63 75 6c 61 72 20 69 6e 73 74 61 6e 63 65 20 icular instance
44820 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 of the module.
44830 45 61 63 68 20 73 75 62 63 6c 61 73 73 20 77 69 Each subclass wi
44840 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65 ll.** be tailore
44850 64 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 d to the specifi
44860 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d c needs of the m
44870 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 odule implementa
44880 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 72 tion..** The pur
44890 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 70 pose of this sup
448a0 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65 erclass is to de
448b0 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65 fine certain fie
448c0 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 lds that are.**
448d0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d 6f common to all mo
448e0 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 dule implementat
448f0 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 ions..**.** Virt
44900 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 6f ual tables metho
44910 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65 72 ds can set an er
44920 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20 61 ror message by a
44930 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 74 ssigning a.** st
44940 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 ring obtained fr
44950 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 om [sqlite3_mpri
44960 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d 73 ntf()] to zErrMs
44970 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20 73 g. The method s
44980 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 61 hould.** take ca
44990 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 69 6f re that any prio
449a0 72 20 73 74 72 69 6e 67 20 69 73 20 66 72 65 65 r string is free
449b0 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b d by a call to [
449c0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 0a sqlite3_free()].
449d0 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69 ** prior to assi
449e0 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69 gning a new stri
449f0 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 ng to zErrMsg.
44a00 41 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 After the error
44a10 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65 message.** is de
44a20 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 68 livered up to th
44a30 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61 e client applica
44a40 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67 tion, the string
44a50 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 will be automat
44a60 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 ically.** freed
44a70 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 by sqlite3_free(
44a80 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73 ) and the zErrMs
44a90 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 g field will be
44aa0 7a 65 72 6f 65 64 2e 20 20 4e 6f 74 65 0a 2a 2a zeroed. Note.**
44ab0 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 6d 70 that sqlite3_mp
44ac0 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69 rintf() and sqli
44ad0 74 65 33 5f 66 72 65 65 28 29 20 61 72 65 20 75 te3_free() are u
44ae0 73 65 64 20 6f 6e 20 74 68 65 20 7a 45 72 72 4d sed on the zErrM
44af0 73 67 20 66 69 65 6c 64 0a 2a 2a 20 73 69 6e 63 sg field.** sinc
44b00 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 e virtual tables
44b10 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 69 6d are commonly im
44b20 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 6c 6f 61 plemented in loa
44b30 64 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 73 dable extensions
44b40 20 77 68 69 63 68 0a 2a 2a 20 64 6f 20 6e 6f 74 which.** do not
44b50 20 68 61 76 65 20 61 63 63 65 73 73 20 74 6f 20 have access to
44b60 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 sqlite3MPrintf()
44b70 20 6f 72 20 73 71 6c 69 74 65 33 46 72 65 65 28 or sqlite3Free(
44b80 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e )..**.** This in
44b90 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 terface is exper
44ba0 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 imental and is s
44bb0 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 ubject to change
44bc0 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 or.** removal i
44bd0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
44be0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a s of SQLite..*/.
44bf0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 struct sqlite3_v
44c00 74 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 tab {. const sq
44c10 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
44c20 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d odule; /* The m
44c30 6f 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76 odule for this v
44c40 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a irtual table */.
44c50 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
44c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
44c70 20 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e /* Used intern
44c80 61 6c 6c 79 20 2a 2f 0a 20 20 63 68 61 72 20 2a ally */. char *
44c90 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 zErrMsg;
44ca0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 /* Err
44cb0 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 or message from
44cc0 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 sqlite3_mprintf(
44cd0 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 ) */. /* Virtua
44ce0 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e l table implemen
44cf0 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 tations will typ
44d00 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 ically add addit
44d10 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a ional fields */.
44d20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 };../*.** CAPI3R
44d30 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c EF: Virtual Tabl
44d40 65 20 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 e Cursor Object
44d50 20 7b 48 31 38 30 32 30 7d 20 3c 53 32 30 34 30 {H18020} <S2040
44d60 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
44d70 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
44d80 73 6f 72 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e sor.** EXPERIMEN
44d90 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 TAL.**.** Every
44da0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 module implement
44db0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 ation uses a sub
44dc0 63 6c 61 73 73 20 6f 66 20 74 68 65 20 66 6f 6c class of the fol
44dd0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
44de0 0a 2a 2a 20 74 6f 20 64 65 73 63 72 69 62 65 20 .** to describe
44df0 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69 cursors that poi
44e00 6e 74 20 69 6e 74 6f 20 74 68 65 20 76 69 72 74 nt into the virt
44e10 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 61 72 ual table and ar
44e20 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 6f e used.** to loo
44e30 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 76 69 p through the vi
44e40 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 43 75 rtual table. Cu
44e50 72 73 6f 72 73 20 61 72 65 20 63 72 65 61 74 65 rsors are create
44e60 64 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 78 d using the.** x
44e70 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 Open method of t
44e80 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 he module. Each
44e90 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
44ea0 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 tation will defi
44eb0 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ne.** the conten
44ec0 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 t of a cursor st
44ed0 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 ructure to suit
44ee0 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a its own needs..*
44ef0 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 *.** This superc
44f00 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f lass exists in o
44f10 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 rder to define f
44f20 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 ields of the cur
44f30 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 sor that.** are
44f40 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d common to all im
44f50 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a plementations..*
44f60 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
44f70 61 63 65 20 69 73 20 65 78 70 65 72 69 6d 65 6e ace is experimen
44f80 74 61 6c 20 61 6e 64 20 69 73 20 73 75 62 6a 65 tal and is subje
44f90 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a ct to change or.
44fa0 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e 20 66 75 ** removal in fu
44fb0 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 ture releases of
44fc0 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 74 72 75 SQLite..*/.stru
44fd0 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f ct sqlite3_vtab_
44fe0 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 cursor {. sqlit
44ff0 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 e3_vtab *pVtab;
45000 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 /* Virtual
45010 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 table of this cu
45020 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 rsor */. /* Vir
45030 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 tual table imple
45040 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 mentations will
45050 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 typically add ad
45060 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 ditional fields
45070 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.};../*.** CAP
45080 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65 20 54 I3REF: Declare T
45090 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41 20 56 he Schema Of A V
450a0 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31 irtual Table {H1
450b0 38 32 38 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 8280} <S20400>.*
450c0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
450d0 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 74 65 *.** The xCreate
450e0 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20 6d 65 and xConnect me
450f0 74 68 6f 64 73 20 6f 66 20 61 20 6d 6f 64 75 6c thods of a modul
45100 65 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 e use the follow
45110 69 6e 67 20 41 50 49 0a 2a 2a 20 74 6f 20 64 65 ing API.** to de
45120 63 6c 61 72 65 20 74 68 65 20 66 6f 72 6d 61 74 clare the format
45130 20 28 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 (the names and
45140 64 61 74 61 74 79 70 65 73 20 6f 66 20 74 68 65 datatypes of the
45150 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a 2a 2a 20 columns) of.**
45160 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c the virtual tabl
45170 65 73 20 74 68 65 79 20 69 6d 70 6c 65 6d 65 6e es they implemen
45180 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e t..**.** This in
45190 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 terface is exper
451a0 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 imental and is s
451b0 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 ubject to change
451c0 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 or.** removal i
451d0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
451e0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a s of SQLite..*/.
451f0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 SQLITE_API SQLIT
45200 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 E_EXPERIMENTAL i
45210 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 nt sqlite3_decla
45220 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 2a re_vtab(sqlite3*
45230 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 , const char *zC
45240 72 65 61 74 65 54 61 62 6c 65 29 3b 0a 0a 2f 2a reateTable);../*
45250 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 76 .** CAPI3REF: Ov
45260 65 72 6c 6f 61 64 20 41 20 46 75 6e 63 74 69 6f erload A Functio
45270 6e 20 46 6f 72 20 41 20 56 69 72 74 75 61 6c 20 n For A Virtual
45280 54 61 62 6c 65 20 7b 48 31 38 33 30 30 7d 20 3c Table {H18300} <
45290 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S20400>.** EXPER
452a0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 56 69 IMENTAL.**.** Vi
452b0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e rtual tables can
452c0 20 70 72 6f 76 69 64 65 20 61 6c 74 65 72 6e 61 provide alterna
452d0 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 tive implementat
452e0 69 6f 6e 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e ions of function
452f0 73 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 78 s.** using the x
45300 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 FindFunction met
45310 68 6f 64 2e 20 20 42 75 74 20 67 6c 6f 62 61 6c hod. But global
45320 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f versions of tho
45330 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 se functions.**
45340 6d 75 73 74 20 65 78 69 73 74 20 69 6e 20 6f 72 must exist in or
45350 64 65 72 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f der to be overlo
45360 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 aded..**.** This
45370 20 41 50 49 20 6d 61 6b 65 73 20 73 75 72 65 20 API makes sure
45380 61 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e a global version
45390 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 of a function w
453a0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 ith a particular
453b0 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d .** name and num
453c0 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72 ber of parameter
453d0 73 20 65 78 69 73 74 73 2e 20 20 49 66 20 6e 6f s exists. If no
453e0 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 such function e
453f0 78 69 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 xists.** before
45400 74 68 69 73 20 41 50 49 20 69 73 20 63 61 6c 6c this API is call
45410 65 64 2c 20 61 20 6e 65 77 20 66 75 6e 63 74 69 ed, a new functi
45420 6f 6e 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 on is created.
45430 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 The implementati
45440 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 on.** of the new
45450 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 function always
45460 20 63 61 75 73 65 73 20 61 6e 20 65 78 63 65 70 causes an excep
45470 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 tion to be throw
45480 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 n. So.** the ne
45490 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f w function is no
454a0 74 20 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 t good for anyth
454b0 69 6e 67 20 62 79 20 69 74 73 65 6c 66 2e 20 20 ing by itself.
454c0 49 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 Its only.** purp
454d0 6f 73 65 20 69 73 20 74 6f 20 62 65 20 61 20 70 ose is to be a p
454e0 6c 61 63 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 laceholder funct
454f0 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 ion that can be
45500 6f 76 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 overloaded.** by
45510 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e virtual tables.
45520 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 .**.** This API
45530 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64 should be consid
45540 65 72 65 64 20 70 61 72 74 20 6f 66 20 74 68 65 ered part of the
45550 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 virtual table i
45560 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 77 68 69 nterface,.** whi
45570 63 68 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 ch is experiment
45580 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 al and subject t
45590 6f 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 53 51 4c o change..*/.SQL
455a0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 ITE_API SQLITE_E
455b0 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 XPERIMENTAL int
455c0 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 sqlite3_overload
455d0 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 _function(sqlite
455e0 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 3*, const char *
455f0 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e zFuncName, int n
45600 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 Arg);../*.** The
45610 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 interface to th
45620 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 e virtual-table
45630 6d 65 63 68 61 6e 69 73 6d 20 64 65 66 69 6e 65 mechanism define
45640 64 20 61 62 6f 76 65 20 28 62 61 63 6b 20 75 70 d above (back up
45650 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d 65 6e 74 .** to a comment
45660 20 72 65 6d 61 72 6b 61 62 6c 79 20 73 69 6d 69 remarkably simi
45670 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 lar to this one)
45680 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f is currently co
45690 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 nsidered.** to b
456a0 65 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 e experimental.
456b0 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d The interface m
456c0 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69 ight change in i
456d0 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73 ncompatible ways
456e0 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 ..** If this is
456f0 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f a problem for yo
45700 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 u, do not use th
45710 65 20 69 6e 74 65 72 66 61 63 65 20 61 74 20 74 e interface at t
45720 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 his time..**.**
45730 57 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c When the virtual
45740 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d -table mechanism
45750 20 73 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 20 stabilizes, we
45760 77 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65 will declare the
45770 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69 .** interface fi
45780 78 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20 xed, support it
45790 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e indefinitely, an
457a0 64 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f d remove this co
457b0 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a mment..**.******
457c0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 EXPERIMENTAL -
457d0 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
457e0 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 e without notice
457f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a **************.
45800 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 */../*.** CAPI3R
45810 45 46 3a 20 41 20 48 61 6e 64 6c 65 20 54 6f 20 EF: A Handle To
45820 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 An Open BLOB {H1
45830 37 38 30 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 7800} <S30230>.*
45840 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 42 4c 4f * KEYWORDS: {BLO
45850 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c 4f 42 20 B handle} {BLOB
45860 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a 2a 20 41 handles}.**.** A
45870 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
45880 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 is object repres
45890 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 42 4c 4f ents an open BLO
458a0 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 5b 73 B on which.** [s
458b0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
458c0 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 | incremental B
458d0 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20 62 65 20 LOB I/O] can be
458e0 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 20 4f 62 performed..** Ob
458f0 6a 65 63 74 73 20 6f 66 20 74 68 69 73 20 74 79 jects of this ty
45900 70 65 20 61 72 65 20 63 72 65 61 74 65 64 20 62 pe are created b
45910 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f y [sqlite3_blob_
45920 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 64 open()].** and d
45930 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c estroyed by [sql
45940 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 ite3_blob_close(
45950 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 )]..** The [sqli
45960 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d te3_blob_read()]
45970 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c and [sqlite3_bl
45980 6f 62 5f 77 72 69 74 65 28 29 5d 20 69 6e 74 65 ob_write()] inte
45990 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e 20 62 65 rfaces.** can be
459a0 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 used to read or
459b0 20 77 72 69 74 65 20 73 6d 61 6c 6c 20 73 75 62 write small sub
459c0 73 65 63 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 sections of the
459d0 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 BLOB..** The [sq
459e0 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 lite3_blob_bytes
459f0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 ()] interface re
45a00 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 6f turns the size o
45a10 66 20 74 68 65 20 42 4c 4f 42 20 69 6e 20 62 79 f the BLOB in by
45a20 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 tes..*/.typedef
45a30 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 struct sqlite3_b
45a40 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 lob sqlite3_blob
45a50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
45a60 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 42 20 46 F: Open A BLOB F
45a70 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 49 or Incremental I
45a80 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c 53 33 30 /O {H17810} <S30
45a90 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 230>.**.** This
45aa0 69 6e 74 65 72 66 61 63 65 73 20 6f 70 65 6e 73 interfaces opens
45ab0 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 20 a [BLOB handle
45ac0 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20 74 68 65 | handle] to the
45ad0 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64 0a 2a 2a BLOB located.**
45ae0 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c 20 63 6f in row iRow, co
45af0 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74 61 lumn zColumn, ta
45b00 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e 20 64 61 ble zTable in da
45b10 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a 2a 20 69 tabase zDb;.** i
45b20 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 n other words, t
45b30 68 65 20 73 61 6d 65 20 42 4c 4f 42 20 74 68 61 he same BLOB tha
45b40 74 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 t would be selec
45b50 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c 70 ted by:.**.** <p
45b60 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 re>.** SELEC
45b70 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a T zColumn FROM z
45b80 44 62 2e 7a 54 61 62 6c 65 20 57 48 45 52 45 20 Db.zTable WHERE
45b90 72 6f 77 69 64 20 3d 20 69 52 6f 77 3b 0a 2a 2a rowid = iRow;.**
45ba0 20 3c 2f 70 72 65 3e 20 7b 45 4e 44 7d 0a 2a 2a </pre> {END}.**
45bb0 0a 2a 2a 20 49 66 20 74 68 65 20 66 6c 61 67 73 .** If the flags
45bc0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
45bd0 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 74 68 65 20 n-zero, the the
45be0 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 BLOB is opened f
45bf0 6f 72 20 72 65 61 64 0a 2a 2a 20 61 6e 64 20 77 or read.** and w
45c00 72 69 74 65 20 61 63 63 65 73 73 2e 20 49 66 20 rite access. If
45c10 69 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20 it is zero, the
45c20 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 BLOB is opened f
45c30 6f 72 20 72 65 61 64 20 61 63 63 65 73 73 2e 0a or read access..
45c40 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
45c50 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
45c60 65 20 69 73 20 6e 6f 74 20 74 68 65 20 66 69 6c e is not the fil
45c70 65 6e 61 6d 65 20 74 68 61 74 20 63 6f 6e 74 61 ename that conta
45c80 69 6e 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 ins.** the datab
45c90 61 73 65 20 62 75 74 20 72 61 74 68 65 72 20 74 ase but rather t
45ca0 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 he symbolic name
45cb0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
45cc0 20 74 68 61 74 0a 2a 2a 20 69 73 20 61 73 73 69 that.** is assi
45cd0 67 6e 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 gned when the da
45ce0 74 61 62 61 73 65 20 69 73 20 63 6f 6e 6e 65 63 tabase is connec
45cf0 74 65 64 20 75 73 69 6e 67 20 5b 41 54 54 41 43 ted using [ATTAC
45d00 48 5d 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6d H]..** For the m
45d10 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
45d20 65 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 e, the database
45d30 6e 61 6d 65 20 69 73 20 22 6d 61 69 6e 22 2e 0a name is "main"..
45d40 2a 2a 20 46 6f 72 20 54 45 4d 50 20 74 61 62 6c ** For TEMP tabl
45d50 65 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 es, the database
45d60 20 6e 61 6d 65 20 69 73 20 22 74 65 6d 70 22 2e name is "temp".
45d70 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 .**.** On succes
45d80 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 s, [SQLITE_OK] i
45d90 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 s returned and t
45da0 68 65 20 6e 65 77 20 5b 42 4c 4f 42 20 68 61 6e he new [BLOB han
45db0 64 6c 65 5d 20 69 73 20 77 72 69 74 74 65 6e 0a dle] is written.
45dc0 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 2e 20 4f ** to *ppBlob. O
45dd0 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 therwise an [err
45de0 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 or code] is retu
45df0 72 6e 65 64 20 61 6e 64 20 61 6e 79 20 76 61 6c rned and any val
45e00 75 65 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f ue written.** to
45e10 20 2a 70 70 42 6c 6f 62 20 73 68 6f 75 6c 64 20 *ppBlob should
45e20 6e 6f 74 20 62 65 20 75 73 65 64 20 62 79 20 74 not be used by t
45e30 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 54 68 he caller..** Th
45e40 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 is function sets
45e50 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
45e60 6f 6e 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 onnection] error
45e70 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 code and messag
45e80 65 0a 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 e.** accessible
45e90 76 69 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 via [sqlite3_err
45ea0 63 6f 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c code()] and [sql
45eb0 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a ite3_errmsg()]..
45ec0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 77 **.** If the row
45ed0 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61 6e that a BLOB han
45ee0 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 dle points to is
45ef0 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a modified by an.
45f00 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44 45 ** [UPDATE], [DE
45f10 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f 4e LETE], or by [ON
45f20 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 2d CONFLICT] side-
45f30 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e 20 effects.** then
45f40 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 the BLOB handle
45f50 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78 is marked as "ex
45f60 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73 20 pired"..** This
45f70 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 63 is true if any c
45f80 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f 77 olumn of the row
45f90 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76 65 is changed, eve
45fa0 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 74 n a column.** ot
45fb0 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 her than the one
45fc0 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 the BLOB handle
45fd0 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a 20 is open on..**
45fe0 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 Calls to [sqlite
45ff0 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 3_blob_read()] a
46000 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 nd [sqlite3_blob
46010 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a 2a _write()] for.**
46020 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42 20 a expired BLOB
46030 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74 68 handle fail with
46040 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65 20 an return code
46050 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 of [SQLITE_ABORT
46060 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77 72 ]..** Changes wr
46070 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c 4f itten into a BLO
46080 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 42 B prior to the B
46090 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72 65 LOB expiring are
460a0 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b not.** rollback
460b0 20 62 79 20 74 68 65 20 65 78 70 69 72 61 74 69 by the expirati
460c0 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e 20 on of the BLOB.
460d0 20 53 75 63 68 20 63 68 61 6e 67 65 73 20 77 69 Such changes wi
460e0 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a ll eventually.**
460f0 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20 74 commit if the t
46100 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74 69 ransaction conti
46110 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 nues to completi
46120 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 on..**.** INVARI
46130 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 ANTS:.**.** {H17
46140 38 31 33 7d 20 41 20 73 75 63 63 65 73 73 66 75 813} A successfu
46150 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 l invocation of
46160 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f the [sqlite3_blo
46170 62 5f 6f 70 65 6e 28 44 2c 42 2c 54 2c 43 2c 52 b_open(D,B,T,C,R
46180 2c 46 2c 50 29 5d 0a 2a 2a 20 20 20 20 20 20 20 ,F,P)].**
46190 20 20 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 interface sha
461a0 6c 6c 20 6f 70 65 6e 20 61 6e 20 5b 73 71 6c 69 ll open an [sqli
461b0 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 te3_blob] object
461c0 20 50 20 6f 6e 20 74 68 65 20 42 4c 4f 42 0a 2a P on the BLOB.*
461d0 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 63 6f * in co
461e0 6c 75 6d 6e 20 43 20 6f 66 20 74 68 65 20 74 61 lumn C of the ta
461f0 62 6c 65 20 54 20 69 6e 20 74 68 65 20 64 61 74 ble T in the dat
46200 61 62 61 73 65 20 42 20 6f 6e 0a 2a 2a 20 20 20 abase B on.**
46210 20 20 20 20 20 20 20 74 68 65 20 5b 64 61 74 61 the [data
46220 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
46230 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 D..**.** {H1781
46240 34 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 4} A successful
46250 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 invocation of [s
46260 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
46270 28 44 2c 2e 2e 2e 29 5d 20 73 68 61 6c 6c 20 73 (D,...)] shall s
46280 74 61 72 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 tart.**
46290 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 a new transacti
462a0 6f 6e 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 on on the [datab
462b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
462c0 44 20 69 66 20 74 68 61 74 0a 2a 2a 20 20 20 20 D if that.**
462d0 20 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e connection
462e0 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
462f0 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e in a transaction
46300 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 36 7d ..**.** {H17816}
46310 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c The [sqlite3_bl
46320 6f 62 5f 6f 70 65 6e 28 44 2c 42 2c 54 2c 43 2c ob_open(D,B,T,C,
46330 52 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 R,F,P)] interfac
46340 65 20 73 68 61 6c 6c 20 6f 70 65 6e 0a 2a 2a 20 e shall open.**
46350 20 20 20 20 20 20 20 20 20 74 68 65 20 42 4c 4f the BLO
46360 42 20 66 6f 72 20 72 65 61 64 20 61 6e 64 20 77 B for read and w
46370 72 69 74 65 20 61 63 63 65 73 73 20 69 66 20 61 rite access if a
46380 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 46 nd only if the F
46390 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 .** par
463a0 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 ameter is non-ze
463b0 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 ro..**.** {H1781
463c0 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 9} The [sqlite3_
463d0 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 69 6e 74 blob_open()] int
463e0 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 erface shall ret
463f0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 urn [SQLITE_OK]
46400 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 on.** s
46410 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 61 70 uccess and an ap
46420 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 propriate [error
46430 20 63 6f 64 65 5d 20 6f 6e 20 66 61 69 6c 75 72 code] on failur
46440 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 32 31 e..**.** {H17821
46450 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 } If an error oc
46460 63 75 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c curs during eval
46470 75 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 uation of [sqlit
46480 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 2e e3_blob_open(D,.
46490 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..)].**
464a0 20 74 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 then subsequent
464b0 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 calls to [sqlit
464c0 65 33 5f 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a e3_errcode(D)],.
464d0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
464e0 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 ite3_extended_er
464f0 72 63 6f 64 65 28 29 5d 2c 20 0a 2a 2a 20 20 20 rcode()], .**
46500 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f [sqlite3_
46510 65 72 72 6d 73 67 28 44 29 5d 2c 20 61 6e 64 20 errmsg(D)], and
46520 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 [sqlite3_errmsg1
46530 36 28 44 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 6(D)] shall retu
46540 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 rn.** i
46550 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f nformation appro
46560 70 72 69 61 74 65 20 66 6f 72 20 74 68 61 74 20 priate for that
46570 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 error..**.** {H1
46580 37 38 32 34 7d 20 49 66 20 61 6e 79 20 63 6f 6c 7824} If any col
46590 75 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77 20 74 umn in the row t
465a0 68 61 74 20 61 20 5b 73 71 6c 69 74 65 33 5f 62 hat a [sqlite3_b
465b0 6c 6f 62 5d 20 68 61 73 20 6f 70 65 6e 20 69 73 lob] has open is
465c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 68 61 .** cha
465d0 6e 67 65 64 20 62 79 20 61 20 73 65 70 61 72 61 nged by a separa
465e0 74 65 20 5b 55 50 44 41 54 45 5d 20 6f 72 20 5b te [UPDATE] or [
465f0 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e DELETE] statemen
46600 74 20 6f 72 20 62 79 0a 2a 2a 20 20 20 20 20 20 t or by.**
46610 20 20 20 20 61 6e 20 5b 4f 4e 20 43 4f 4e 46 4c an [ON CONFL
46620 49 43 54 5d 20 73 69 64 65 20 65 66 66 65 63 74 ICT] side effect
46630 2c 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 , then the [sqli
46640 74 65 33 5f 62 6c 6f 62 5d 20 73 68 61 6c 6c 0a te3_blob] shall.
46650 2a 2a 20 20 20 20 20 20 20 20 20 20 62 65 20 6d ** be m
46660 61 72 6b 65 64 20 61 73 20 69 6e 76 61 6c 69 64 arked as invalid
46670 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
46680 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
46690 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 _open(. sqlite3
466a0 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 *,. const char
466b0 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 *zDb,. const ch
466c0 61 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20 63 6f ar *zTable,. co
466d0 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d nst char *zColum
466e0 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 n,. sqlite3_int
466f0 36 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74 20 66 64 iRow,. int f
46700 6c 61 67 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f lags,. sqlite3_
46710 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a 29 3b blob **ppBlob.);
46720 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
46730 3a 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42 20 48 : Close A BLOB H
46740 61 6e 64 6c 65 20 7b 48 31 37 38 33 30 7d 20 3c andle {H17830} <
46750 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 43 6c S30230>.**.** Cl
46760 6f 73 65 73 20 61 6e 20 6f 70 65 6e 20 5b 42 4c oses an open [BL
46770 4f 42 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2a 0a 2a OB handle]..**.*
46780 2a 20 43 6c 6f 73 69 6e 67 20 61 20 42 4c 4f 42 * Closing a BLOB
46790 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74 68 65 shall cause the
467a0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
467b0 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a tion to commit.*
467c0 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e * if there are n
467d0 6f 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c 20 6e o other BLOBs, n
467e0 6f 20 70 65 6e 64 69 6e 67 20 70 72 65 70 61 72 o pending prepar
467f0 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2c 20 61 ed statements, a
46800 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 nd the.** databa
46810 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 se connection is
46820 20 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69 74 20 in [autocommit
46830 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 66 20 61 6e 79 mode]..** If any
46840 20 77 72 69 74 65 73 20 77 65 72 65 20 6d 61 64 writes were mad
46850 65 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c 20 74 e to the BLOB, t
46860 68 65 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c hey might be hel
46870 64 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20 75 6e d in cache.** un
46880 74 69 6c 20 74 68 65 20 63 6c 6f 73 65 20 6f 70 til the close op
46890 65 72 61 74 69 6f 6e 20 69 66 20 74 68 65 79 20 eration if they
468a0 77 69 6c 6c 20 66 69 74 2e 20 7b 45 4e 44 7d 0a will fit. {END}.
468b0 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68 **.** Closing th
468c0 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72 e BLOB often for
468d0 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a ces the changes.
468e0 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 ** out to disk a
468f0 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f nd so if any I/O
46900 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 errors occur, t
46910 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 hey will likely
46920 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 occur.** at the
46930 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c time when the BL
46940 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 7b OB is closed. {
46950 48 31 37 38 33 33 7d 20 41 6e 79 20 65 72 72 6f H17833} Any erro
46960 72 73 20 74 68 61 74 20 6f 63 63 75 72 20 64 75 rs that occur du
46970 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20 ring.** closing
46980 61 72 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 are reported as
46990 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72 a non-zero retur
469a0 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 n value..**.** T
469b0 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 he BLOB is close
469c0 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c d unconditionall
469d0 79 2e 20 20 45 76 65 6e 20 69 66 20 74 68 69 73 y. Even if this
469e0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
469f0 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 .** an error cod
46a00 65 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 73 e, the BLOB is s
46a10 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a till closed..**.
46a20 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a ** INVARIANTS:.*
46a30 2a 0a 2a 2a 20 7b 48 31 37 38 33 33 7d 20 54 68 *.** {H17833} Th
46a40 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
46a50 63 6c 6f 73 65 28 50 29 5d 20 69 6e 74 65 72 66 close(P)] interf
46a60 61 63 65 20 63 6c 6f 73 65 73 20 61 6e 20 5b 73 ace closes an [s
46a70 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 0a 2a 2a 20 qlite3_blob].**
46a80 20 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 object
46a90 50 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 P previously ope
46aa0 6e 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ned using [sqlit
46ab0 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 2e e3_blob_open()].
46ac0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 33 36 7d 20 .**.** {H17836}
46ad0 43 6c 6f 73 69 6e 67 20 61 6e 20 5b 73 71 6c 69 Closing an [sqli
46ae0 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 te3_blob] object
46af0 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 using.**
46b00 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 [sqlite3_blob
46b10 5f 63 6c 6f 73 65 28 29 5d 20 73 68 61 6c 6c 20 _close()] shall
46b20 63 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e cause the curren
46b30 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f t transaction to
46b40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6d .** com
46b50 6d 69 74 20 69 66 20 74 68 65 72 65 20 61 72 65 mit if there are
46b60 20 6e 6f 20 6f 74 68 65 72 20 6f 70 65 6e 20 5b no other open [
46b70 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 sqlite3_blob] ob
46b80 6a 65 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 jects.**
46b90 20 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 73 or [prepared s
46ba0 74 61 74 65 6d 65 6e 74 73 5d 20 6f 6e 20 74 68 tatements] on th
46bb0 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 e same [database
46bc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 connection] and
46bd0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
46be0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
46bf0 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f tion is in [auto
46c00 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a commit mode]..**
46c10 0a 2a 2a 20 7b 48 31 37 38 33 39 7d 20 54 68 65 .** {H17839} The
46c20 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 [sqlite3_blob_c
46c30 6c 6f 73 65 28 50 29 5d 20 69 6e 74 65 72 66 61 lose(P)] interfa
46c40 63 65 73 20 73 68 61 6c 6c 20 63 6c 6f 73 65 20 ces shall close
46c50 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
46c60 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f [sqlite3_blob] o
46c70 62 6a 65 63 74 20 50 20 75 6e 63 6f 6e 64 69 74 bject P uncondit
46c80 69 6f 6e 61 6c 6c 79 2c 20 65 76 65 6e 20 69 66 ionally, even if
46c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 .** [sq
46ca0 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 lite3_blob_close
46cb0 28 50 29 5d 20 72 65 74 75 72 6e 73 20 73 6f 6d (P)] returns som
46cc0 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 ething other tha
46cd0 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a n [SQLITE_OK]..*
46ce0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
46cf0 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c sqlite3_blob_cl
46d00 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ose(sqlite3_blob
46d10 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 *);../*.** CAPI
46d20 33 52 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 3REF: Return The
46d30 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e Size Of An Open
46d40 20 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d 20 3c BLOB {H17840} <
46d50 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 S30230>.**.** Re
46d60 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 69 turns the size i
46d70 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 n bytes of the B
46d80 4c 4f 42 20 61 63 63 65 73 73 69 62 6c 65 20 76 LOB accessible v
46d90 69 61 20 74 68 65 20 6f 70 65 6e 0a 2a 2a 20 5b ia the open.** [
46da0 5d 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e ]BLOB handle] in
46db0 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75 6d 65 its only argume
46dc0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 nt..**.** INVARI
46dd0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 ANTS:.**.** {H17
46de0 38 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 843} The [sqlite
46df0 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 50 29 5d 3_blob_bytes(P)]
46e00 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
46e10 6e 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20 20 ns the size.**
46e20 20 20 20 20 20 20 20 20 69 6e 20 62 79 74 65 73 in bytes
46e30 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 74 68 61 of the BLOB tha
46e40 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 t the [sqlite3_b
46e50 6c 6f 62 5d 20 6f 62 6a 65 63 74 20 50 0a 2a 2a lob] object P.**
46e60 20 20 20 20 20 20 20 20 20 20 72 65 66 65 72 73 refers
46e70 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 to..*/.SQLITE_A
46e80 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
46e90 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 lob_bytes(sqlite
46ea0 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 3_blob *);../*.*
46eb0 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 61 64 * CAPI3REF: Read
46ec0 20 44 61 74 61 20 46 72 6f 6d 20 41 20 42 4c 4f Data From A BLO
46ed0 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 B Incrementally
46ee0 7b 48 31 37 38 35 30 7d 20 3c 53 33 30 32 33 30 {H17850} <S30230
46ef0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e >.**.** This fun
46f00 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
46f10 20 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 read data from
46f20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 an open [BLOB ha
46f30 6e 64 6c 65 5d 20 69 6e 74 6f 20 61 0a 2a 2a 20 ndle] into a.**
46f40 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 caller-supplied
46f50 62 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 buffer. N bytes
46f60 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 of data are copi
46f70 65 64 20 69 6e 74 6f 20 62 75 66 66 65 72 20 5a ed into buffer Z
46f80 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 .** from the ope
46f90 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 n BLOB, starting
46fa0 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73 at offset iOffs
46fb0 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f 66 66 et..**.** If off
46fc0 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c set iOffset is l
46fd0 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 ess than N bytes
46fe0 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 from the end of
46ff0 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 the BLOB,.** [S
47000 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 QLITE_ERROR] is
47010 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 returned and no
47020 64 61 74 61 20 69 73 20 72 65 61 64 2e 20 20 49 data is read. I
47030 66 20 4e 20 6f 72 20 69 4f 66 66 73 65 74 20 69 f N or iOffset i
47040 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a s.** less than z
47050 65 72 6f 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 ero, [SQLITE_ERR
47060 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 OR] is returned
47070 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 and no data is r
47080 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 ead..**.** An at
47090 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72 tempt to read fr
470a0 6f 6d 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 om an expired [B
470b0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c LOB handle] fail
470c0 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 s with an.** err
470d0 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 or code of [SQLI
470e0 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a TE_ABORT]..**.**
470f0 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c On success, SQL
47100 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
47110 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 ed..** Otherwise
47120 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 , an [error code
47130 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 ] or an [extende
47140 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 d error code] is
47150 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
47160 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a INVARIANTS:.**.
47170 2a 2a 20 7b 48 31 37 38 35 33 7d 20 41 20 73 75 ** {H17853} A su
47180 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 ccessful invocat
47190 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f ion of [sqlite3_
471a0 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c blob_read(P,Z,N,
471b0 58 29 5d 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 X)] .**
471c0 20 73 68 61 6c 6c 20 72 65 61 64 73 20 4e 20 62 shall reads N b
471d0 79 74 65 73 20 6f 66 20 64 61 74 61 20 6f 75 74 ytes of data out
471e0 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 72 65 66 of the BLOB ref
471f0 65 72 65 6e 63 65 64 20 62 79 0a 2a 2a 20 20 20 erenced by.**
47200 20 20 20 20 20 20 20 5b 42 4c 4f 42 20 68 61 6e [BLOB han
47210 64 6c 65 5d 20 50 20 62 65 67 69 6e 6e 69 6e 67 dle] P beginning
47220 20 61 74 20 6f 66 66 73 65 74 20 58 20 61 6e 64 at offset X and
47230 20 73 74 6f 72 65 20 74 68 6f 73 65 20 62 79 74 store those byt
47240 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 es.** i
47250 6e 74 6f 20 62 75 66 66 65 72 20 5a 2e 0a 2a 2a nto buffer Z..**
47260 0a 2a 2a 20 7b 48 31 37 38 35 36 7d 20 49 6e 20 .** {H17856} In
47270 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 [sqlite3_blob_re
47280 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69 66 20 ad(P,Z,N,X)] if
47290 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
472a0 42 4c 4f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 BLOB.**
472b0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 2b is less than N+
472c0 58 20 62 79 74 65 73 2c 20 74 68 65 6e 20 74 68 X bytes, then th
472d0 65 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c e function shall
472e0 20 6c 65 61 76 65 20 74 68 65 0a 2a 2a 20 20 20 leave the.**
472f0 20 20 20 20 20 20 20 5a 20 62 75 66 66 65 72 20 Z buffer
47300 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 72 65 unchanged and re
47310 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 turn [SQLITE_ERR
47320 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 OR]..**.** {H178
47330 35 39 7d 20 49 6e 20 5b 73 71 6c 69 74 65 33 5f 59} In [sqlite3_
47340 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c blob_read(P,Z,N,
47350 58 29 5d 20 69 66 20 58 20 6f 72 20 4e 20 69 73 X)] if X or N is
47360 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 0a less than zero.
47370 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e ** then
47380 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 73 68 the function sh
47390 61 6c 6c 20 6c 65 61 76 65 20 74 68 65 20 5a 20 all leave the Z
473a0 62 75 66 66 65 72 20 75 6e 63 68 61 6e 67 65 64 buffer unchanged
473b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 .** and
473c0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
473d0 45 52 52 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 ERROR]..**.** {H
473e0 31 37 38 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 17862} The [sqli
473f0 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c te3_blob_read(P,
47400 5a 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 Z,N,X)] interfac
47410 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 5b e shall return [
47420 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 20 20 SQLITE_OK].**
47430 20 20 20 20 20 20 20 69 66 20 4e 20 62 79 74 65 if N byte
47440 73 20 61 72 65 20 73 75 63 63 65 73 73 66 75 6c s are successful
47450 6c 79 20 72 65 61 64 20 69 6e 74 6f 20 62 75 66 ly read into buf
47460 66 65 72 20 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 fer Z..**.** {H1
47470 37 38 36 33 7d 20 49 66 20 74 68 65 20 5b 42 4c 7863} If the [BL
47480 4f 42 20 68 61 6e 64 6c 65 5d 20 50 20 69 73 20 OB handle] P is
47490 65 78 70 69 72 65 64 20 61 6e 64 20 58 20 61 6e expired and X an
474a0 64 20 4e 20 61 72 65 20 77 69 74 68 69 6e 20 62 d N are within b
474b0 6f 75 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20 ounds.**
474c0 20 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f then [sqlite3_
474d0 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c blob_read(P,Z,N,
474e0 58 29 5d 20 73 68 61 6c 6c 20 6c 65 61 76 65 20 X)] shall leave
474f0 74 68 65 20 5a 20 62 75 66 66 65 72 0a 2a 2a 20 the Z buffer.**
47500 20 20 20 20 20 20 20 20 20 75 6e 63 68 61 6e 67 unchang
47510 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 ed and return [S
47520 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a QLITE_ABORT]..**
47530 0a 2a 2a 20 7b 48 31 37 38 36 35 7d 20 49 66 20 .** {H17865} If
47540 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65 the requested re
47550 61 64 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ad could not be
47560 63 6f 6d 70 6c 65 74 65 64 2c 0a 2a 2a 20 20 20 completed,.**
47570 20 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c 69 the [sqli
47580 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c te3_blob_read(P,
47590 5a 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 Z,N,X)] interfac
475a0 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 61 e shall return a
475b0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 70 n.** ap
475c0 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 propriate [error
475d0 20 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e code] or [exten
475e0 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 2e ded error code].
475f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 36 38 7d 20 .**.** {H17868}
47600 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
47610 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75 61 rs during evalua
47620 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 tion of [sqlite3
47630 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c 2e 2e 2e _blob_read(P,...
47640 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 )].** t
47650 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 hen subsequent c
47660 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
47670 5f 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a 2a 2a _errcode(D)],.**
47680 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 [sqlit
47690 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 e3_extended_errc
476a0 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 ode()],.**
476b0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 [sqlite3_err
476c0 6d 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b 73 71 msg(D)], and [sq
476d0 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 44 lite3_errmsg16(D
476e0 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a )] shall return.
476f0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 66 6f ** info
47700 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 69 rmation appropri
47710 61 74 65 20 66 6f 72 20 74 68 61 74 20 65 72 72 ate for that err
47720 6f 72 2c 20 77 68 65 72 65 20 44 20 69 73 20 74 or, where D is t
47730 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b he.** [
47740 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
47750 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 20 75 73 ion] that was us
47760 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 5b ed to open the [
47770 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50 2e 0a BLOB handle] P..
47780 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
47790 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 t sqlite3_blob_r
477a0 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ead(sqlite3_blob
477b0 20 2a 2c 20 76 6f 69 64 20 2a 5a 2c 20 69 6e 74 *, void *Z, int
477c0 20 4e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 N, int iOffset)
477d0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
477e0 46 3a 20 57 72 69 74 65 20 44 61 74 61 20 49 6e F: Write Data In
477f0 74 6f 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d to A BLOB Increm
47800 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 37 30 7d entally {H17870}
47810 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 <S30230>.**.**
47820 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
47830 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 64 used to write d
47840 61 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70 65 6e ata into an open
47850 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 [BLOB handle] f
47860 72 6f 6d 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d rom a.** caller-
47870 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e supplied buffer.
47880 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 N bytes of data
47890 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d are copied from
478a0 20 74 68 65 20 62 75 66 66 65 72 20 5a 0a 2a 2a the buffer Z.**
478b0 20 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e 20 42 into the open B
478c0 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20 61 74 LOB, starting at
478d0 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 2e offset iOffset.
478e0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 42 .**.** If the [B
478f0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 70 61 73 73 LOB handle] pass
47900 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
47910 61 72 67 75 6d 65 6e 74 20 77 61 73 20 6e 6f 74 argument was not
47920 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 77 opened for.** w
47930 72 69 74 69 6e 67 20 28 74 68 65 20 66 6c 61 67 riting (the flag
47940 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b s parameter to [
47950 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 sqlite3_blob_ope
47960 6e 28 29 5d 20 77 61 73 20 7a 65 72 6f 29 2c 0a n()] was zero),.
47970 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
47980 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
47990 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a _READONLY]..**.*
479a0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
479b0 6d 61 79 20 6f 6e 6c 79 20 6d 6f 64 69 66 79 20 may only modify
479c0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
479d0 74 68 65 20 42 4c 4f 42 3b 20 69 74 20 69 73 0a the BLOB; it is.
479e0 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 ** not possible
479f0 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 to increase the
47a00 73 69 7a 65 20 6f 66 20 61 20 42 4c 4f 42 20 75 size of a BLOB u
47a10 73 69 6e 67 20 74 68 69 73 20 41 50 49 2e 0a 2a sing this API..*
47a20 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 * If offset iOff
47a30 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e set is less than
47a40 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 N bytes from th
47a50 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f e end of the BLO
47a60 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 B,.** [SQLITE_ER
47a70 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ROR] is returned
47a80 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 and no data is
47a90 77 72 69 74 74 65 6e 2e 20 20 49 66 20 4e 20 69 written. If N i
47aa0 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a s.** less than z
47ab0 65 72 6f 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f ero [SQLITE_ERRO
47ac0 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 R] is returned a
47ad0 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 nd no data is wr
47ae0 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 itten..**.** An
47af0 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 attempt to write
47b00 20 74 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b to an expired [
47b10 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 BLOB handle] fai
47b20 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 ls with an.** er
47b30 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c ror code of [SQL
47b40 49 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 ITE_ABORT]. Wri
47b50 74 65 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 tes to the BLOB
47b60 74 68 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a that occurred.**
47b70 20 62 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f before the [BLO
47b80 42 20 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 B handle] expire
47b90 64 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 d are not rolled
47ba0 20 62 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 back by the.**
47bb0 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 expiration of th
47bc0 65 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 e handle, though
47bd0 20 6f 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 of course those
47be0 20 63 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a changes might.*
47bf0 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 * have been over
47c00 77 72 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 written by the s
47c10 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 tatement that ex
47c20 70 69 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 pired the BLOB h
47c30 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f andle.** or by o
47c40 74 68 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 ther independent
47c50 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a statements..**.
47c60 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 ** On success, S
47c70 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
47c80 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 rned..** Otherwi
47c90 73 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 se, an [error c
47ca0 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 ode] or an [exte
47cb0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d nded error code]
47cc0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
47cd0 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a .** INVARIANTS:.
47ce0 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 33 7d 20 41 **.** {H17873} A
47cf0 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f successful invo
47d00 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 cation of [sqlit
47d10 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c e3_blob_write(P,
47d20 5a 2c 4e 2c 58 29 5d 0a 2a 2a 20 20 20 20 20 20 Z,N,X)].**
47d30 20 20 20 20 73 68 61 6c 6c 20 77 72 69 74 65 20 shall write
47d40 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 N bytes of data
47d50 66 72 6f 6d 20 62 75 66 66 65 72 20 5a 20 69 6e from buffer Z in
47d60 74 6f 20 74 68 65 20 42 4c 4f 42 20 0a 2a 2a 20 to the BLOB .**
47d70 20 20 20 20 20 20 20 20 20 72 65 66 65 72 65 6e referen
47d80 63 65 64 20 62 79 20 5b 42 4c 4f 42 20 68 61 6e ced by [BLOB han
47d90 64 6c 65 5d 20 50 20 62 65 67 69 6e 6e 69 6e 67 dle] P beginning
47da0 20 61 74 20 6f 66 66 73 65 74 20 58 20 69 6e 74 at offset X int
47db0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 o.** th
47dc0 65 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 7b 48 e BLOB..**.** {H
47dd0 31 37 38 37 34 7d 20 49 6e 20 74 68 65 20 61 62 17874} In the ab
47de0 73 65 6e 63 65 20 6f 66 20 6f 74 68 65 72 20 6f sence of other o
47df0 76 65 72 72 69 64 64 69 6e 67 20 63 68 61 6e 67 verridding chang
47e00 65 73 2c 20 74 68 65 20 63 68 61 6e 67 65 73 0a es, the changes.
47e10 2a 2a 20 20 20 20 20 20 20 20 20 20 77 72 69 74 ** writ
47e20 74 65 6e 20 74 6f 20 61 20 42 4c 4f 42 20 62 79 ten to a BLOB by
47e30 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 [sqlite3_blob_w
47e40 72 69 74 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a rite()] shall.**
47e50 20 20 20 20 20 20 20 20 20 20 72 65 6d 61 69 6e remain
47e60 20 69 6e 20 65 66 66 65 63 74 20 61 66 74 65 72 in effect after
47e70 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 the associated
47e80 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78 [BLOB handle] ex
47e90 70 69 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 pires..**.** {H1
47ea0 37 38 37 35 7d 20 49 66 20 74 68 65 20 5b 42 4c 7875} If the [BL
47eb0 4f 42 20 68 61 6e 64 6c 65 5d 20 50 20 77 61 73 OB handle] P was
47ec0 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 opened for read
47ed0 69 6e 67 20 6f 6e 6c 79 20 74 68 65 6e 0a 2a 2a ing only then.**
47ee0 20 20 20 20 20 20 20 20 20 20 61 6e 20 69 6e 76 an inv
47ef0 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 ocation of [sqli
47f00 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 te3_blob_write(P
47f10 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 20 6c ,Z,N,X)] shall l
47f20 65 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 eave.**
47f30 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 the referenced
47f40 42 4c 4f 42 20 75 6e 63 68 61 6e 67 65 64 20 61 BLOB unchanged a
47f50 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 nd return [SQLIT
47f60 45 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a E_READONLY]..**.
47f70 2a 2a 20 7b 48 31 37 38 37 36 7d 20 49 66 20 74 ** {H17876} If t
47f80 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 he size of the B
47f90 4c 4f 42 20 72 65 66 65 72 65 6e 63 65 64 20 62 LOB referenced b
47fa0 79 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 y [BLOB handle]
47fb0 50 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 P is.**
47fc0 20 6c 65 73 73 20 74 68 61 6e 20 4e 2b 58 20 62 less than N+X b
47fd0 79 74 65 73 20 74 68 65 6e 20 5b 73 71 6c 69 74 ytes then [sqlit
47fe0 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c e3_blob_write(P,
47ff0 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 0a 2a 2a Z,N,X)] shall.**
48000 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 20 leave
48010 74 68 65 20 42 4c 4f 42 20 75 6e 63 68 61 6e 67 the BLOB unchang
48020 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 ed and return [S
48030 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a 2a 2a QLITE_ERROR]..**
48040 0a 2a 2a 20 7b 48 31 37 38 37 37 7d 20 49 66 20 .** {H17877} If
48050 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 the [BLOB handle
48060 5d 20 50 20 69 73 20 65 78 70 69 72 65 64 20 61 ] P is expired a
48070 6e 64 20 58 20 61 6e 64 20 4e 20 61 72 65 20 77 nd X and N are w
48080 69 74 68 69 6e 20 62 6f 75 6e 64 73 0a 2a 2a 20 ithin bounds.**
48090 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 then [s
480a0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 qlite3_blob_read
480b0 28 50 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c (P,Z,N,X)] shall
480c0 20 6c 65 61 76 65 20 74 68 65 20 42 4c 4f 42 0a leave the BLOB.
480d0 2a 2a 20 20 20 20 20 20 20 20 20 20 75 6e 63 68 ** unch
480e0 61 6e 67 65 64 20 61 6e 64 20 72 65 74 75 72 6e anged and return
480f0 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e [SQLITE_ABORT].
48100 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 39 7d 20 .**.** {H17879}
48110 49 66 20 58 20 6f 72 20 4e 20 61 72 65 20 6c 65 If X or N are le
48120 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 74 68 65 ss than zero the
48130 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f n [sqlite3_blob_
48140 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 0a write(P,Z,N,X)].
48150 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c ** shal
48160 6c 20 6c 65 61 76 65 20 74 68 65 20 42 4c 4f 42 l leave the BLOB
48170 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 5b referenced by [
48180 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50 20 75 BLOB handle] P u
48190 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 20 20 20 20 nchanged.**
481a0 20 20 20 20 20 61 6e 64 20 72 65 74 75 72 6e 20 and return
481b0 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a [SQLITE_ERROR]..
481c0 2a 2a 0a 2a 2a 20 7b 48 31 37 38 38 32 7d 20 54 **.** {H17882} T
481d0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 he [sqlite3_blob
481e0 5f 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d _write(P,Z,N,X)]
481f0 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c interface shall
48200 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 return.**
48210 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 [SQLITE_OK]
48220 69 66 20 4e 20 62 79 74 65 73 20 77 68 65 72 65 if N bytes where
48230 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 77 72 successfully wr
48240 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 42 itten into the B
48250 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 LOB..**.** {H178
48260 38 35 7d 20 49 66 20 74 68 65 20 72 65 71 75 65 85} If the reque
48270 73 74 65 64 20 77 72 69 74 65 20 63 6f 75 6c 64 sted write could
48280 20 6e 6f 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 not be complete
48290 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 d,.** t
482a0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 he [sqlite3_blob
482b0 5f 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d _write(P,Z,N,X)]
482c0 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c interface shall
482d0 20 72 65 74 75 72 6e 20 61 6e 0a 2a 2a 20 20 20 return an.**
482e0 20 20 20 20 20 20 20 61 70 70 72 6f 70 72 69 61 appropria
482f0 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 te [error code]
48300 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 or [extended err
48310 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 or code]..**.**
48320 7b 48 31 37 38 38 38 7d 20 49 66 20 61 6e 20 65 {H17888} If an e
48330 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
48340 6e 67 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 ng evaluation of
48350 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 [sqlite3_blob_w
48360 72 69 74 65 28 44 2c 2e 2e 2e 29 5d 0a 2a 2a 20 rite(D,...)].**
48370 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 73 75 then su
48380 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 bsequent calls t
48390 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f o [sqlite3_errco
483a0 64 65 28 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 de(D)],.**
483b0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 [sqlite3_ext
483c0 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d ended_errcode()]
483d0 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 ,.** [s
483e0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 44 29 qlite3_errmsg(D)
483f0 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f ], and [sqlite3_
48400 65 72 72 6d 73 67 31 36 28 44 29 5d 20 73 68 61 errmsg16(D)] sha
48410 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 ll return.**
48420 20 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 69 6f informatio
48430 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f n appropriate fo
48440 72 20 74 68 61 74 20 65 72 72 6f 72 2e 0a 2a 2f r that error..*/
48450 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
48460 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 sqlite3_blob_wri
48470 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 te(sqlite3_blob
48480 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a *, const void *z
48490 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 , int n, int iOf
484a0 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 fset);../*.** CA
484b0 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 PI3REF: Virtual
484c0 46 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65 File System Obje
484d0 63 74 73 20 7b 48 31 31 32 30 30 7d 20 3c 53 32 cts {H11200} <S2
484e0 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 0100>.**.** A vi
484f0 72 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d rtual filesystem
48500 20 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71 (VFS) is an [sq
48510 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 lite3_vfs] objec
48520 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65 t.** that SQLite
48530 20 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63 uses to interac
48540 74 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e t.** with the un
48550 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
48560 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74 ng system. Most
48570 20 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63 SQLite builds c
48580 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 69 ome with a.** si
48590 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 53 ngle default VFS
485a0 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 that is appropr
485b0 69 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f 73 iate for the hos
485c0 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e t computer..** N
485d0 65 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 20 ew VFSes can be
485e0 72 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 65 registered and e
485f0 78 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 61 xisting VFSes ca
48600 6e 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 65 n be unregistere
48610 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 d..** The follow
48620 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 61 ing interfaces a
48630 72 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a re provided..**.
48640 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 ** The sqlite3_v
48650 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 72 66 fs_find() interf
48660 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f ace returns a po
48670 69 6e 74 65 72 20 74 6f 20 61 20 56 46 53 20 67 inter to a VFS g
48680 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a iven its name..*
48690 2a 20 4e 61 6d 65 73 20 61 72 65 20 63 61 73 65 * Names are case
486a0 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e sensitive..** N
486b0 61 6d 65 73 20 61 72 65 20 7a 65 72 6f 2d 74 65 ames are zero-te
486c0 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 rminated UTF-8 s
486d0 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68 trings..** If th
486e0 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c ere is no match,
486f0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
48700 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
48710 49 66 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e If zVfsName is N
48720 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66 ULL then the def
48730 61 75 6c 74 20 56 46 53 20 69 73 20 72 65 74 75 ault VFS is retu
48740 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 rned..**.** New
48750 56 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74 VFSes are regist
48760 65 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 ered with sqlite
48770 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 3_vfs_register()
48780 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46 ..** Each new VF
48790 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 S becomes the de
487a0 66 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65 fault VFS if the
487b0 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69 makeDflt flag i
487c0 73 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61 s set..** The sa
487d0 6d 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65 me VFS can be re
487e0 67 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c gistered multipl
487f0 65 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20 e times without
48800 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61 injury..** To ma
48810 6b 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56 ke an existing V
48820 46 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61 FS into the defa
48830 75 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65 ult VFS, registe
48840 72 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69 r it again.** wi
48850 74 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 th the makeDflt
48860 66 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77 flag set. If tw
48870 6f 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65 o different VFSe
48880 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61 s with the.** sa
48890 6d 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69 me name are regi
488a0 73 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61 stered, the beha
488b0 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
488c0 64 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20 d. If a.** VFS
488d0 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 is registered wi
488e0 74 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69 th a name that i
488f0 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 s NULL or an emp
48900 74 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68 ty string,.** th
48910 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 en the behavior
48920 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a is undefined..**
48930 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 .** Unregister a
48940 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71 VFS with the sq
48950 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 lite3_vfs_unregi
48960 73 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 ster() interface
48970 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61 ..** If the defa
48980 75 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67 ult VFS is unreg
48990 69 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 istered, another
489a0 20 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61 VFS is chosen a
489b0 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 s.** the default
489c0 2e 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f . The choice fo
489d0 72 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73 r the new VFS is
489e0 20 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a arbitrary..**.*
489f0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a * INVARIANTS:.**
48a00 0a 2a 2a 20 7b 48 31 31 32 30 33 7d 20 54 68 65 .** {H11203} The
48a10 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 [sqlite3_vfs_fi
48a20 6e 64 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 nd(N)] interface
48a30 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
48a40 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 er to the.**
48a50 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64 registered
48a60 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f [sqlite3_vfs] o
48a70 62 6a 65 63 74 20 77 68 6f 73 65 20 6e 61 6d 65 bject whose name
48a80 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 exactly matches
48a90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 .** the
48aa0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
48ab0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 4e 2c UTF-8 string N,
48ac0 20 6f 72 20 69 74 20 72 65 74 75 72 6e 73 20 4e or it returns N
48ad0 55 4c 4c 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 ULL if.**
48ae0 20 20 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d there is no m
48af0 61 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 atch..**.** {H11
48b00 32 30 36 7d 20 49 66 20 74 68 65 20 4e 20 70 61 206} If the N pa
48b10 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
48b20 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 4e 29 5d te3_vfs_find(N)]
48b30 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a is NULL then.**
48b40 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75 the fu
48b50 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 nction returns a
48b60 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
48b70 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 default [sqlite3
48b80 5f 76 66 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20 _vfs].**
48b90 20 20 6f 62 6a 65 63 74 20 69 66 20 74 68 65 72 object if ther
48ba0 65 20 69 73 20 6f 6e 65 2c 20 6f 72 20 4e 55 4c e is one, or NUL
48bb0 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f L if there is no
48bc0 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 default.**
48bd0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 66 [sqlite3_vf
48be0 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a s] object..**.**
48bf0 20 7b 48 31 31 32 30 39 7d 20 54 68 65 20 5b 73 {H11209} The [s
48c00 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
48c10 74 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 66 ter(P,F)] interf
48c20 61 63 65 20 72 65 67 69 73 74 65 72 73 20 74 68 ace registers th
48c30 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 65 e.** we
48c40 6c 6c 2d 66 6f 72 6d 65 64 20 5b 73 71 6c 69 74 ll-formed [sqlit
48c50 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 50 e3_vfs] object P
48c60 20 75 73 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 using the name
48c70 67 69 76 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 given.**
48c80 20 20 62 79 20 74 68 65 20 7a 4e 61 6d 65 20 66 by the zName f
48c90 69 65 6c 64 20 6f 66 20 74 68 65 20 6f 62 6a 65 ield of the obje
48ca0 63 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 31 ct..**.** {H1121
48cb0 32 7d 20 55 73 69 6e 67 20 74 68 65 20 5b 73 71 2} Using the [sq
48cc0 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
48cd0 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 66 61 er(P,F)] interfa
48ce0 63 65 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a ce to register.*
48cf0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 * the s
48d00 61 6d 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 ame [sqlite3_vfs
48d10 5d 20 6f 62 6a 65 63 74 20 6d 75 6c 74 69 70 6c ] object multipl
48d20 65 20 74 69 6d 65 73 20 69 73 20 61 20 68 61 72 e times is a har
48d30 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a mless no-op..**.
48d40 2a 2a 20 7b 48 31 31 32 31 35 7d 20 54 68 65 20 ** {H11215} The
48d50 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 [sqlite3_vfs_reg
48d60 69 73 74 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 ister(P,F)] inte
48d70 72 66 61 63 65 20 6d 61 6b 65 73 20 74 68 65 20 rface makes the
48d80 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 0a 2a 2a [sqlite3_vfs].**
48d90 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74 object
48da0 20 50 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b P the default [
48db0 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a sqlite3_vfs] obj
48dc0 65 63 74 20 69 66 20 46 20 69 73 20 6e 6f 6e 2d ect if F is non-
48dd0 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 zero..**.** {H11
48de0 32 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 218} The [sqlite
48df0 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 3_vfs_unregister
48e00 28 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 75 (P)] interface u
48e10 6e 72 65 67 69 73 74 65 72 73 20 74 68 65 0a 2a nregisters the.*
48e20 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
48e30 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 te3_vfs] object
48e40 50 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 P so that it is
48e50 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 74 75 72 6e no longer return
48e60 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20 ed by.**
48e70 20 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c subsequent cal
48e80 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 76 ls to [sqlite3_v
48e90 66 73 5f 66 69 6e 64 28 29 5d 2e 0a 2a 2f 0a 53 fs_find()]..*/.S
48ea0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
48eb0 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 3_vfs *sqlite3_v
48ec0 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 fs_find(const ch
48ed0 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 ar *zVfsName);.S
48ee0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
48ef0 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
48f00 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c er(sqlite3_vfs*,
48f10 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a int makeDflt);.
48f20 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
48f30 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 qlite3_vfs_unreg
48f40 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 ister(sqlite3_vf
48f50 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 s*);../*.** CAPI
48f60 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b 48 3REF: Mutexes {H
48f70 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 17000} <S20000>.
48f80 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 **.** The SQLite
48f90 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 65 core uses these
48fa0 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 68 routines for th
48fb0 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e read.** synchron
48fc0 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 ization. Though
48fd0 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 65 they are intende
48fe0 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a d for internal.*
48ff0 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2c * use by SQLite,
49000 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 code that links
49010 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 20 against SQLite
49020 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 is.** permitted
49030 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 to use any of th
49040 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a ese routines..**
49050 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 73 .** The SQLite s
49060 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 ource code conta
49070 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 ins multiple imp
49080 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 lementations.**
49090 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 72 of these mutex r
490a0 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 outines. An app
490b0 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 ropriate impleme
490c0 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 ntation.** is se
490d0 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 63 lected automatic
490e0 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d ally at compile-
490f0 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f time. The follo
49100 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e wing.** implemen
49110 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69 tations are avai
49120 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c lable in the SQL
49130 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 ite core:.**.**
49140 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 <ul>.** <li> S
49150 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a QLITE_MUTEX_OS2.
49160 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 ** <li> SQLITE
49170 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a 2a _MUTEX_PTHREAD.*
49180 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f * <li> SQLITE_
49190 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 MUTEX_W32.** <li
491a0 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 > SQLITE_MUTEX
491b0 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a _NOOP.** </ul>.*
491c0 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
491d0 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 MUTEX_NOOP imple
491e0 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 73 mentation is a s
491f0 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a et of routines.*
49200 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 72 * that does no r
49210 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 eal locking and
49220 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 is appropriate f
49230 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73 or use in.** a s
49240 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 ingle-threaded a
49250 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 pplication. The
49260 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 SQLITE_MUTEX_OS
49270 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 2,.** SQLITE_MUT
49280 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 20 EX_PTHREAD, and
49290 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 SQLITE_MUTEX_W32
492a0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
492b0 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69 .** are appropri
492c0 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f ate for use on O
492d0 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57 S/2, Unix, and W
492e0 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 indows..**.** If
492f0 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
49300 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c led with the SQL
49310 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 ITE_MUTEX_APPDEF
49320 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a preprocessor.**
49330 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 28 macro defined (
49340 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d with "-DSQLITE_M
49350 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 2c UTEX_APPDEF=1"),
49360 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a then no mutex.*
49370 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
49380 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74 is included wit
49390 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 49 h the library. I
493a0 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a n this case the.
493b0 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d ** application m
493c0 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 73 ust supply a cus
493d0 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d tom mutex implem
493e0 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 74 entation using t
493f0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f he.** [SQLITE_CO
49400 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 NFIG_MUTEX] opti
49410 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
49420 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 74 3_config() funct
49430 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 ion.** before ca
49440 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e lling sqlite3_in
49450 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e itialize() or an
49460 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 73 y other public s
49470 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 qlite3_.** funct
49480 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 73 ion that calls s
49490 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
494a0 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 e()..**.** {H170
494b0 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 11} The sqlite3_
494c0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f mutex_alloc() ro
494d0 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 utine allocates
494e0 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 a new.** mutex a
494f0 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 nd returns a poi
49500 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 37 nter to it. {H17
49510 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 72 012} If it retur
49520 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 ns NULL.** that
49530 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 means that a mut
49540 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ex could not be
49550 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 30 allocated. {H170
49560 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 13} SQLite.** wi
49570 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 ll unwind its st
49580 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ack and return a
49590 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 34 n error. {H17014
495a0 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a } The argument.*
495b0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 * to sqlite3_mut
495c0 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e ex_alloc() is on
495d0 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
495e0 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a er constants:.**
495f0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
49600 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 SQLITE_MUTEX_F
49610 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c AST.** <li> SQL
49620 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
49630 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c IVE.** <li> SQL
49640 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
49650 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 _MASTER.** <li>
49660 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
49670 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e ATIC_MEM.** <li>
49680 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
49690 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c TATIC_MEM2.** <l
496a0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
496b0 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 _STATIC_PRNG.**
496c0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
496d0 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a EX_STATIC_LRU.**
496e0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
496f0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a TEX_STATIC_LRU2.
49700 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b ** </ul>.**.** {
49710 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 73 H17015} The firs
49720 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 t two constants
49730 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 cause sqlite3_mu
49740 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 tex_alloc() to c
49750 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d reate.** a new m
49760 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d utex. The new m
49770 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 utex is recursiv
49780 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 e when SQLITE_MU
49790 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
497a0 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 is used but not
497b0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 necessarily so
497c0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 when SQLITE_MUTE
497d0 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 20 X_FAST is used.
497e0 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 {END}.** The mut
497f0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ex implementatio
49800 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
49810 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e to make a distin
49820 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e ction.** between
49830 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
49840 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 CURSIVE and SQLI
49850 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 TE_MUTEX_FAST if
49860 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
49870 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 31 want to. {H1701
49880 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6} But SQLite wi
49890 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 ll only request
498a0 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 a recursive mute
498b0 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 x in.** cases wh
498c0 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 ere it really ne
498d0 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 eds one. {END}
498e0 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d If a faster non-
498f0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a recursive mutex.
49900 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
49910 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f n is available o
49920 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 n the host platf
49930 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 orm, the mutex s
49940 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 ubsystem.** migh
49950 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 t return such a
49960 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 mutex in respons
49970 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 e to SQLITE_MUTE
49980 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 X_FAST..**.** {H
49990 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 72 17017} The other
499a0 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 allowed paramet
499b0 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d ers to sqlite3_m
499c0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 utex_alloc() eac
499d0 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f h return.** a po
499e0 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 inter to a stati
499f0 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 c preexisting mu
49a00 74 65 78 2e 20 7b 45 4e 44 7d 20 20 46 6f 75 72 tex. {END} Four
49a10 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 static mutexes
49a20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 are.** used by t
49a30 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 he current versi
49a40 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 on of SQLite. F
49a50 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
49a60 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 f SQLite.** may
49a70 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 add additional s
49a80 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 tatic mutexes.
49a90 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 Static mutexes a
49aa0 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a re for internal.
49ab0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 ** use by SQLite
49ac0 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 only. Applicat
49ad0 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 ions that use SQ
49ae0 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f Lite mutexes sho
49af0 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 uld.** use only
49b00 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 the dynamic mute
49b10 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 xes returned by
49b20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
49b30 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d T or.** SQLITE_M
49b40 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a UTEX_RECURSIVE..
49b50 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e **.** {H17018} N
49b60 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 ote that if one
49b70 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d of the dynamic m
49b80 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 utex parameters
49b90 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 (SQLITE_MUTEX_FA
49ba0 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f ST.** or SQLITE_
49bb0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 MUTEX_RECURSIVE)
49bc0 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 is used then sq
49bd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
49be0 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 c().** returns a
49bf0 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 different mutex
49c00 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 on every call.
49c10 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f {H17034} But fo
49c20 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 r the static.**
49c30 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
49c40 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
49c50 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
49c60 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
49c70 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
49c80 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 number..**.** {H
49c90 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74 17019} The sqlit
49ca0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20 e3_mutex_free()
49cb0 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 routine dealloca
49cc0 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 tes a previously
49cd0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 .** allocated dy
49ce0 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31 namic mutex. {H1
49cf0 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20 7020} SQLite is
49d00 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c careful to deall
49d10 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64 ocate every.** d
49d20 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61 ynamic mutex tha
49d30 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20 t it allocates.
49d40 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e {A17021} The dyn
49d50 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 amic mutexes mus
49d60 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 t not be in.** u
49d70 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 se when they are
49d80 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 deallocated. {A
49d90 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e 17022} Attemptin
49da0 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 g to deallocate
49db0 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 a static.** mute
49dc0 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 x results in und
49dd0 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e efined behavior.
49de0 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65 {H17023} SQLite
49df0 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 never deallocat
49e00 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d es.** a static m
49e10 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a utex. {END}.**.*
49e20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
49e30 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 tex_enter() and
49e40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
49e50 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 y() routines att
49e60 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 empt.** to enter
49e70 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 a mutex. {H1702
49e80 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 4} If another th
49e90 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
49ea0 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
49eb0 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
49ec0 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
49ed0 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
49ee0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
49ef0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
49f00 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32 ITE_BUSY. {H1702
49f10 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 5} The sqlite3_
49f20 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 mutex_try() inte
49f30 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 rface returns [S
49f40 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f QLITE_OK].** upo
49f50 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 n successful ent
49f60 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75 ry. {H17026} Mu
49f70 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 texes created us
49f80 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 ing.** SQLITE_MU
49f90 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 TEX_RECURSIVE ca
49fa0 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c n be entered mul
49fb0 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 tiple times by t
49fc0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a he same thread..
49fd0 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73 ** {H17027} In s
49fe0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
49ff0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
4a000 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
4a010 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
4a020 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
4a030 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
4a040 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66 er. {A17028} If
4a050 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
4a060 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 tries to enter
4a070 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e any other.** kin
4a080 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20 d of mutex more
4a090 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 than once, the b
4a0a0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
4a0b0 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 ined..** {H17029
4a0c0 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 } SQLite will ne
4a0d0 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 ver exhibit.** s
4a0e0 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20 uch behavior in
4a0f0 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d its own use of m
4a100 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f utexes..**.** So
4a110 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20 me systems (for
4a120 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73 example, Windows
4a130 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70 95) do not supp
4a140 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f ort the operatio
4a150 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 n.** implemented
4a160 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 by sqlite3_mute
4a170 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f x_try(). On tho
4a180 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69 se systems, sqli
4a190 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a te3_mutex_try().
4a1a0 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 ** will always r
4a1b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
4a1c0 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65 Y. {H17030} The
4a1d0 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c SQLite core onl
4a1e0 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73 y ever uses.** s
4a1f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
4a200 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a () as an optimiz
4a210 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73 ation so this is
4a220 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61 acceptable beha
4a230 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 vior..**.** {H17
4a240 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 031} The sqlite3
4a250 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 _mutex_leave() r
4a260 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d outine exits a m
4a270 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a utex that was.**
4a280 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 previously ente
4a290 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 red by the same
4a2a0 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32 thread. {A17032
4a2b0 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a } The behavior.*
4a2c0 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 * is undefined i
4a2d0 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e f the mutex is n
4a2e0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ot currently ent
4a2f0 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 ered by the.** c
4a300 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72 alling thread or
4a310 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
4a320 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48 y allocated. {H
4a330 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69 17033} SQLite wi
4a340 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65 ll.** never do e
4a350 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a ither. {END}.**.
4a360 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 ** If the argume
4a370 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 nt to sqlite3_mu
4a380 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c tex_enter(), sql
4a390 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
4a3a0 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f , or.** sqlite3_
4a3b0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73 mutex_leave() is
4a3c0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
4a3d0 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 then all three
4a3e0 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61 routines.** beha
4a3f0 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a ve as no-ops..**
4a400 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
4a410 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
4a420 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 d()] and [sqlite
4a430 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
4a440 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
4a450 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 I sqlite3_mutex
4a460 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 *sqlite3_mutex_a
4a470 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 lloc(int);.SQLIT
4a480 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
4a490 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 e3_mutex_free(sq
4a4a0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 lite3_mutex*);.S
4a4b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
4a4c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
4a4d0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
4a4e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
4a4f0 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nt sqlite3_mutex
4a500 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 _try(sqlite3_mut
4a510 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ex*);.SQLITE_API
4a520 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 void sqlite3_mu
4a530 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
4a540 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 3_mutex*);../*.*
4a550 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 * CAPI3REF: Mute
4a560 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 x Methods Object
4a570 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33 {H17120} <S2013
4a580 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
4a590 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 AL.**.** An inst
4a5a0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
4a5b0 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 ucture defines t
4a5c0 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 he low-level rou
4a5d0 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f tines.** used to
4a5e0 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 allocate and us
4a5f0 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a e mutexes..**.**
4a600 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 Usually, the de
4a610 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c fault mutex impl
4a620 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 ementations prov
4a630 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 ided by SQLite a
4a640 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 re.** sufficient
4a650 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 , however the us
4a660 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f er has the optio
4a670 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e n of substitutin
4a680 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d g a custom.** im
4a690 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 plementation for
4a6a0 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 specialized dep
4a6b0 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 loyments or syst
4a6c0 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 ems for which SQ
4a6d0 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 Lite.** does not
4a6e0 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 provide a suita
4a6f0 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ble implementati
4a700 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 on. In this case
4a710 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 , the user.** cr
4a720 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 eates and popula
4a730 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 tes an instance
4a740 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
4a750 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 e to pass.** to
4a760 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
4a770 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 along with the
4a780 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
4a790 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a UTEX] option..**
4a7a0 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 Additionally, a
4a7b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
4a7c0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e is structure can
4a7d0 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a be used as an.*
4a7e0 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c * output variabl
4a7f0 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 e when querying
4a800 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 the system for t
4a810 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 he current mutex
4a820 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
4a830 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 on, using the [S
4a840 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 QLITE_CONFIG_GET
4a850 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a MUTEX] option..*
4a860 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49 *.** The xMutexI
4a870 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e nit method defin
4a880 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 ed by this struc
4a890 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 ture is invoked
4a8a0 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 as.** part of sy
4a8b0 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 stem initializat
4a8c0 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 ion by the sqlit
4a8d0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
4a8e0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 function..** {H1
4a8f0 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78 7001} The xMutex
4a900 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61 Init routine sha
4a910 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 ll be called by
4a920 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20 SQLite once for
4a930 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76 each.** effectiv
4a940 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 e call to [sqlit
4a950 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d e3_initialize()]
4a960 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 ..**.** The xMut
4a970 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66 exEnd method def
4a980 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 ined by this str
4a990 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 ucture is invoke
4a9a0 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 d as.** part of
4a9b0 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20 system shutdown
4a9c0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 by the sqlite3_s
4a9d0 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69 hutdown() functi
4a9e0 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 on. The.** imple
4a9f0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 mentation of thi
4aa00 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65 s method is expe
4aa10 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 cted to release
4aa20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a all outstanding.
4aa30 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74 ** resources obt
4aa40 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74 ained by the mut
4aa50 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 ex methods imple
4aa60 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63 mentation, espec
4aa70 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f ially.** those o
4aa80 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78 btained by the x
4aa90 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 MutexInit method
4aaa0 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78 . {H17003} The x
4aab0 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e MutexEnd().** in
4aac0 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 terface shall be
4aad0 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f invoked once fo
4aae0 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b r each call to [
4aaf0 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
4ab00 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ()]..**.** The r
4ab10 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d emaining seven m
4ab20 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62 ethods defined b
4ab30 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 y this structure
4ab40 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a (xMutexAlloc,.*
4ab50 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d * xMutexFree, xM
4ab60 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65 utexEnter, xMute
4ab70 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76 xTry, xMutexLeav
4ab80 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e e, xMutexHeld an
4ab90 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65 d.** xMutexNothe
4aba0 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 ld) implement th
4abb0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 e following inte
4abc0 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69 rfaces (respecti
4abd0 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c vely):.**.** <ul
4abe0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
4abf0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
4ac00 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 c()] </li>.**
4ac10 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
4ac20 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c utex_free()] </l
4ac30 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
4ac40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
4ac50 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 er()] </li>.**
4ac60 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
4ac70 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c mutex_try()] </l
4ac80 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
4ac90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
4aca0 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 ve()] </li>.**
4acb0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
4acc0 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f mutex_held()] </
4acd0 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b li>.** <li> [
4ace0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
4acf0 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a theld()] </li>.*
4ad00 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 * </ul>.**.** Th
4ad10 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 e only differenc
4ad20 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75 e is that the pu
4ad30 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58 blic sqlite3_XXX
4ad40 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65 functions enume
4ad50 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73 rated.** above s
4ad60 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61 ilently ignore a
4ad70 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74 ny invocations t
4ad80 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20 hat pass a NULL
4ad90 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a pointer instead.
4ada0 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 ** of a valid mu
4adb0 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 tex handle. The
4adc0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
4add0 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64 of the methods d
4ade0 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69 efined.** by thi
4adf0 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
4ae00 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
4ae10 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65 handle this case
4ae20 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a , the results.**
4ae30 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55 of passing a NU
4ae40 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 LL pointer inste
4ae50 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 ad of a valid mu
4ae60 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75 tex handle are u
4ae70 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 ndefined.** (i.e
4ae80 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62 . it is acceptab
4ae90 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e le to provide an
4aea0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
4aeb0 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69 that segfaults i
4aec0 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65 f.** it is passe
4aed0 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 d a NULL pointer
4aee0 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 )..*/.typedef st
4aef0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 ruct sqlite3_mut
4af00 65 78 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 ex_methods sqlit
4af10 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
4af20 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 ;.struct sqlite3
4af30 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 7b _mutex_methods {
4af40 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 49 . int (*xMutexI
4af50 6e 69 74 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e nit)(void);. in
4af60 74 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28 76 t (*xMutexEnd)(v
4af70 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f oid);. sqlite3_
4af80 6d 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78 41 mutex *(*xMutexA
4af90 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f lloc)(int);. vo
4afa0 69 64 20 28 2a 78 4d 75 74 65 78 46 72 65 65 29 id (*xMutexFree)
4afb0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
4afc0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 );. void (*xMut
4afd0 65 78 45 6e 74 65 72 29 28 73 71 6c 69 74 65 33 exEnter)(sqlite3
4afe0 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 _mutex *);. int
4aff0 20 28 2a 78 4d 75 74 65 78 54 72 79 29 28 73 71 (*xMutexTry)(sq
4b000 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a lite3_mutex *);.
4b010 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 4c void (*xMutexL
4b020 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 eave)(sqlite3_mu
4b030 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a tex *);. int (*
4b040 78 4d 75 74 65 78 48 65 6c 64 29 28 73 71 6c 69 xMutexHeld)(sqli
4b050 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 te3_mutex *);.
4b060 69 6e 74 20 28 2a 78 4d 75 74 65 78 4e 6f 74 68 int (*xMutexNoth
4b070 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 eld)(sqlite3_mut
4b080 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ex *);.};../*.**
4b090 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 CAPI3REF: Mutex
4b0a0 20 56 65 72 69 66 69 63 61 74 69 6f 6e 20 52 6f Verification Ro
4b0b0 75 74 69 6e 65 73 20 7b 48 31 37 30 38 30 7d 20 utines {H17080}
4b0c0 3c 53 32 30 31 33 30 3e 20 3c 53 33 30 38 30 30 <S20130> <S30800
4b0d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
4b0e0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
4b0f0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
4b100 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 ex_notheld() rou
4b110 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 tines.** are int
4b120 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e ended for use in
4b130 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 side assert() st
4b140 61 74 65 6d 65 6e 74 73 2e 20 7b 48 31 37 30 38 atements. {H1708
4b150 31 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 1} The SQLite co
4b160 72 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 65 73 re.** never uses
4b170 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
4b180 65 78 63 65 70 74 20 69 6e 73 69 64 65 20 61 6e except inside an
4b190 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 61 70 assert() and ap
4b1a0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 plications.** ar
4b1b0 65 20 61 64 76 69 73 65 64 20 74 6f 20 66 6f 6c e advised to fol
4b1c0 6c 6f 77 20 74 68 65 20 6c 65 61 64 20 6f 66 20 low the lead of
4b1d0 74 68 65 20 63 6f 72 65 2e 20 20 7b 48 31 37 30 the core. {H170
4b1e0 38 32 7d 20 54 68 65 20 63 6f 72 65 20 6f 6e 6c 82} The core onl
4b1f0 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69 6d y.** provides im
4b200 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f plementations fo
4b210 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 r these routines
4b220 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70 when it is comp
4b230 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 iled.** with the
4b240 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66 6c SQLITE_DEBUG fl
4b250 61 67 2e 20 20 7b 41 31 37 30 38 37 7d 20 45 78 ag. {A17087} Ex
4b260 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 6d 70 ternal mutex imp
4b270 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 lementations.**
4b280 61 72 65 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 are only require
4b290 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 d to provide the
4b2a0 73 65 20 72 6f 75 74 69 6e 65 73 20 69 66 20 53 se routines if S
4b2b0 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 0a 2a QLITE_DEBUG is.*
4b2c0 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 69 66 * defined and if
4b2d0 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 NDEBUG is not d
4b2e0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 efined..**.** {H
4b2f0 31 37 30 38 33 7d 20 54 68 65 73 65 20 72 6f 75 17083} These rou
4b300 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65 74 tines should ret
4b310 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 urn true if the
4b320 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20 61 mutex in their a
4b330 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68 65 rgument.** is he
4b340 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c 20 ld or not held,
4b350 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62 79 respectively, by
4b360 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 the calling thr
4b370 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 58 31 37 30 ead..**.** {X170
4b380 38 34 7d 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 84} The implemen
4b390 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 tation is not re
4b3a0 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 quired to provid
4b3b0 65 64 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 ed versions of t
4b3c0 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 hese.** routines
4b3d0 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20 77 that actually w
4b3e0 6f 72 6b 2e 20 49 66 20 74 68 65 20 69 6d 70 6c ork. If the impl
4b3f0 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 ementation does
4b400 6e 6f 74 20 70 72 6f 76 69 64 65 20 77 6f 72 6b not provide work
4b410 69 6e 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 ing.** versions
4b420 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 of these routine
4b430 73 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74 20 s, it should at
4b440 6c 65 61 73 74 20 70 72 6f 76 69 64 65 20 73 74 least provide st
4b450 75 62 73 20 74 68 61 74 20 61 6c 77 61 79 73 0a ubs that always.
4b460 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20 73 ** return true s
4b470 6f 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73 20 o that one does
4b480 6e 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75 73 not get spurious
4b490 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 75 assertion failu
4b4a0 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 res..**.** {H170
4b4b0 38 35 7d 20 49 66 20 74 68 65 20 61 72 67 75 6d 85} If the argum
4b4c0 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d ent to sqlite3_m
4b4d0 75 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20 61 utex_held() is a
4b4e0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 NULL pointer th
4b4f0 65 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e en.** the routin
4b500 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 e should return
4b510 31 2e 20 20 7b 45 4e 44 7d 20 54 68 69 73 20 73 1. {END} This s
4b520 65 65 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e 74 eems counter-int
4b530 75 69 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a 20 uitive since.**
4b540 63 6c 65 61 72 6c 79 20 74 68 65 20 6d 75 74 65 clearly the mute
4b550 78 20 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c 64 x cannot be held
4b560 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 if it does not
4b570 65 78 69 73 74 2e 20 20 42 75 74 20 74 68 65 0a exist. But the.
4b580 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68 ** the reason th
4b590 65 20 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f 74 e mutex does not
4b5a0 20 65 78 69 73 74 20 69 73 20 62 65 63 61 75 73 exist is becaus
4b5b0 65 20 74 68 65 20 62 75 69 6c 64 20 69 73 20 6e e the build is n
4b5c0 6f 74 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74 65 ot.** using mute
4b5d0 78 65 73 2e 20 20 41 6e 64 20 77 65 20 64 6f 20 xes. And we do
4b5e0 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 61 73 73 not want the ass
4b5f0 65 72 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67 ert() containing
4b600 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 the.** call to
4b610 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
4b620 6c 64 28 29 20 74 6f 20 66 61 69 6c 2c 20 73 6f ld() to fail, so
4b630 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 a non-zero retu
4b640 72 6e 20 69 73 0a 2a 2a 20 74 68 65 20 61 70 70 rn is.** the app
4b650 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20 74 ropriate thing t
4b660 6f 20 64 6f 2e 20 20 7b 48 31 37 30 38 36 7d 20 o do. {H17086}
4b670 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
4b680 78 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20 69 x_notheld().** i
4b690 6e 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 20 nterface should
4b6a0 61 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 68 also return 1 wh
4b6b0 65 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c 20 en given a NULL
4b6c0 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 pointer..*/.SQLI
4b6d0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
4b6e0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 e3_mutex_held(sq
4b6f0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 lite3_mutex*);.S
4b700 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
4b710 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
4b720 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 eld(sqlite3_mute
4b730 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 x*);../*.** CAPI
4b740 33 52 45 46 3a 20 4d 75 74 65 78 20 54 79 70 65 3REF: Mutex Type
4b750 73 20 7b 48 31 37 30 30 31 7d 20 3c 48 31 37 30 s {H17001} <H170
4b760 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 00>.**.** The [s
4b770 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
4b780 6f 63 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 oc()] interface
4b790 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
4b7a0 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 69 63 68 rgument.** which
4b7b0 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 is one of these
4b7c0 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
4b7d0 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 ts..**.** The se
4b7e0 74 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74 65 t of static mute
4b7f0 78 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20 66 xes may change f
4b800 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20 72 rom one SQLite r
4b810 65 6c 65 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a elease to the.**
4b820 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61 74 next. Applicat
4b830 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 72 69 ions that overri
4b840 64 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 de the built-in
4b850 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d 75 73 74 mutex logic must
4b860 20 62 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 be.** prepared
4b870 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20 61 to accommodate a
4b880 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 dditional static
4b890 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 64 65 mutexes..*/.#de
4b8a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
4b8b0 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20 X_FAST
4b8c0 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0.#define SQL
4b8d0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
4b8e0 49 56 45 20 20 20 20 20 20 20 20 31 0a 23 64 65 IVE 1.#de
4b8f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
4b900 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 X_STATIC_MASTER
4b910 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 2.#define SQL
4b920 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
4b930 5f 4d 45 4d 20 20 20 20 20 20 20 33 20 20 2f 2a _MEM 3 /*
4b940 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
4b950 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ) */.#define SQL
4b960 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
4b970 5f 4d 45 4d 32 20 20 20 20 20 20 34 20 20 2f 2a _MEM2 4 /*
4b980 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
4b990 5f 6d 65 6d 6f 72 79 28 29 20 2a 2f 0a 23 64 65 _memory() */.#de
4b9a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
4b9b0 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 X_STATIC_PRNG
4b9c0 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5 /* sqlite3
4b9d0 5f 72 61 6e 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 _random() */.#de
4b9e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
4b9f0 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 20 20 20 X_STATIC_LRU
4ba00 20 20 20 36 20 20 2f 2a 20 6c 72 75 20 70 61 67 6 /* lru pag
4ba10 65 20 6c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e e list */.#defin
4ba20 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
4ba30 54 41 54 49 43 5f 4c 52 55 32 20 20 20 20 20 20 TATIC_LRU2
4ba40 37 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 7 /* lru page l
4ba50 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 ist */../*.** CA
4ba60 50 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 65 PI3REF: Retrieve
4ba70 20 74 68 65 20 6d 75 74 65 78 20 66 6f 72 20 61 the mutex for a
4ba80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
4ba90 74 69 6f 6e 20 7b 48 31 37 30 30 32 7d 20 3c 48 tion {H17002} <H
4baa0 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 17000>.**.** Thi
4bab0 73 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 s interface retu
4bac0 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 68 rns a pointer th
4bad0 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 e [sqlite3_mutex
4bae0 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 0a 2a ] object that .*
4baf0 2a 20 73 65 72 69 61 6c 69 7a 65 73 20 61 63 63 * serializes acc
4bb00 65 73 73 20 74 6f 20 74 68 65 20 5b 64 61 74 61 ess to the [data
4bb10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
4bb20 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 given in the ar
4bb30 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 74 gument.** when t
4bb40 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f he [threading mo
4bb50 64 65 5d 20 69 73 20 53 65 72 69 61 6c 69 7a 65 de] is Serialize
4bb60 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 74 68 d..** If the [th
4bb70 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 reading mode] is
4bb80 20 53 69 6e 67 6c 65 2d 74 68 72 65 61 64 20 6f Single-thread o
4bb90 72 20 4d 75 6c 74 69 2d 74 68 72 65 61 64 20 74 r Multi-thread t
4bba0 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 hen this.** rout
4bbb0 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 ine returns a NU
4bbc0 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 LL pointer..*/.S
4bbd0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
4bbe0 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 3_mutex *sqlite3
4bbf0 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 _db_mutex(sqlite
4bc00 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 3*);../*.** CAPI
4bc10 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 3REF: Low-Level
4bc20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61 74 61 62 Control Of Datab
4bc30 61 73 65 20 46 69 6c 65 73 20 7b 48 31 31 33 30 ase Files {H1130
4bc40 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 0} <S30800>.**.*
4bc50 2a 20 7b 48 31 31 33 30 31 7d 20 54 68 65 20 5b * {H11301} The [
4bc60 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e sqlite3_file_con
4bc70 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 trol()] interfac
4bc80 65 20 6d 61 6b 65 73 20 61 20 64 69 72 65 63 74 e makes a direct
4bc90 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 call to the.**
4bca0 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 xFileControl met
4bcb0 68 6f 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c hod for the [sql
4bcc0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d ite3_io_methods]
4bcd0 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 object associat
4bce0 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 ed.** with a par
4bcf0 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
4bd00 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 identified by t
4bd10 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
4bd20 6e 74 2e 20 7b 48 31 31 33 30 32 7d 20 54 68 65 nt. {H11302} The
4bd30 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 .** name of the
4bd40 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 database is the
4bd50 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f name assigned to
4bd60 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 79 the database by
4bd70 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d the.** <a href=
4bd80 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e 68 74 6d "lang_attach.htm
4bd90 6c 22 3e 41 54 54 41 43 48 3c 2f 61 3e 20 53 51 l">ATTACH</a> SQ
4bda0 4c 20 63 6f 6d 6d 61 6e 64 20 74 68 61 74 20 6f L command that o
4bdb0 70 65 6e 65 64 20 74 68 65 0a 2a 2a 20 64 61 74 pened the.** dat
4bdc0 61 62 61 73 65 2e 20 7b 48 31 31 33 30 33 7d 20 abase. {H11303}
4bdd0 54 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6d To control the m
4bde0 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
4bdf0 65 2c 20 75 73 65 20 74 68 65 20 6e 61 6d 65 20 e, use the name
4be00 22 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e "main".** or a N
4be10 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 7b 48 31 ULL pointer. {H1
4be20 31 33 30 34 7d 20 54 68 65 20 74 68 69 72 64 20 1304} The third
4be30 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d and fourth param
4be40 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 72 6f eters to this ro
4be50 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20 70 61 73 utine.** are pas
4be60 73 65 64 20 64 69 72 65 63 74 6c 79 20 74 68 72 sed directly thr
4be70 6f 75 67 68 20 74 6f 20 74 68 65 20 73 65 63 6f ough to the seco
4be80 6e 64 20 61 6e 64 20 74 68 69 72 64 20 70 61 72 nd and third par
4be90 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a 20 74 68 ameters of.** th
4bea0 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d e xFileControl m
4beb0 65 74 68 6f 64 2e 20 20 7b 48 31 31 33 30 35 7d ethod. {H11305}
4bec0 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
4bed0 65 20 6f 66 20 74 68 65 20 78 46 69 6c 65 43 6f e of the xFileCo
4bee0 6e 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20 ntrol.** method
4bef0 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 74 75 becomes the retu
4bf00 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 rn value of this
4bf10 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
4bf20 7b 48 31 31 33 30 36 7d 20 49 66 20 74 68 65 20 {H11306} If the
4bf30 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
4bf40 20 28 7a 44 62 4e 61 6d 65 29 20 64 6f 65 73 20 (zDbName) does
4bf50 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e 61 not match the na
4bf60 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 me of any.** ope
4bf70 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c n database file,
4bf80 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 45 52 52 then SQLITE_ERR
4bf90 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 OR is returned.
4bfa0 7b 48 31 31 33 30 37 7d 20 54 68 69 73 20 65 72 {H11307} This er
4bfb0 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 6e ror.** code is n
4bfc0 6f 74 20 72 65 6d 65 6d 62 65 72 65 64 20 61 6e ot remembered an
4bfd0 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 d will not be re
4bfe0 63 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 called by [sqlit
4bff0 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 0a 2a 2a e3_errcode()].**
4c000 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 or [sqlite3_err
4c010 6d 73 67 28 29 5d 2e 20 7b 41 31 31 33 30 38 7d msg()]. {A11308}
4c020 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 The underlying
4c030 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 xFileControl met
4c040 68 6f 64 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 hod might.** als
4c050 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f o return SQLITE_
4c060 45 52 52 4f 52 2e 20 20 7b 41 31 31 33 30 39 7d ERROR. {A11309}
4c070 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 There is no way
4c080 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68 20 to distinguish
4c090 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e between.** an in
4c0a0 63 6f 72 72 65 63 74 20 7a 44 62 4e 61 6d 65 20 correct zDbName
4c0b0 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 and an SQLITE_ER
4c0c0 52 4f 52 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 ROR return from
4c0d0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a the underlying.*
4c0e0 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d * xFileControl m
4c0f0 65 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a ethod. {END}.**.
4c100 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 ** See also: [SQ
4c110 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 LITE_FCNTL_LOCKS
4c120 54 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f TATE].*/.SQLITE_
4c130 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
4c140 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c file_control(sql
4c150 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 ite3*, const cha
4c160 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 r *zDbName, int
4c170 6f 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a op, void*);../*.
4c180 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 73 ** CAPI3REF: Tes
4c190 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20 7b ting Interface {
4c1a0 48 31 31 34 30 30 7d 20 3c 53 33 30 38 30 30 3e H11400} <S30800>
4c1b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
4c1c0 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 e3_test_control(
4c1d0 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 ) interface is u
4c1e0 73 65 64 20 74 6f 20 72 65 61 64 20 6f 75 74 20 sed to read out
4c1f0 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 73 74 61 74 internal.** stat
4c200 65 20 6f 66 20 53 51 4c 69 74 65 20 61 6e 64 20 e of SQLite and
4c210 74 6f 20 69 6e 6a 65 63 74 20 66 61 75 6c 74 73 to inject faults
4c220 20 69 6e 74 6f 20 53 51 4c 69 74 65 20 66 6f 72 into SQLite for
4c230 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 testing.** purp
4c240 6f 73 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 oses. The first
4c250 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e parameter is an
4c260 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 operation code
4c270 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a that determines.
4c280 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 2c 20 6d ** the number, m
4c290 65 61 6e 69 6e 67 2c 20 61 6e 64 20 6f 70 65 72 eaning, and oper
4c2a0 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 75 62 ation of all sub
4c2b0 73 65 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65 sequent paramete
4c2c0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 rs..**.** This i
4c2d0 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 nterface is not
4c2e0 66 6f 72 20 75 73 65 20 62 79 20 61 70 70 6c 69 for use by appli
4c2f0 63 61 74 69 6f 6e 73 2e 20 20 49 74 20 65 78 69 cations. It exi
4c300 73 74 73 20 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f sts solely.** fo
4c310 72 20 76 65 72 69 66 79 69 6e 67 20 74 68 65 20 r verifying the
4c320 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f correct operatio
4c330 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 n of the SQLite
4c340 6c 69 62 72 61 72 79 2e 20 20 44 65 70 65 6e 64 library. Depend
4c350 69 6e 67 0a 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 ing.** on how th
4c360 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
4c370 20 69 73 20 63 6f 6d 70 69 6c 65 64 2c 20 74 68 is compiled, th
4c380 69 73 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 is interface mig
4c390 68 74 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a ht not exist..**
4c3a0 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 .** The details
4c3b0 6f 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e of the operation
4c3c0 20 63 6f 64 65 73 2c 20 74 68 65 69 72 20 6d 65 codes, their me
4c3d0 61 6e 69 6e 67 73 2c 20 74 68 65 20 70 61 72 61 anings, the para
4c3e0 6d 65 74 65 72 73 0a 2a 2a 20 74 68 65 79 20 74 meters.** they t
4c3f0 61 6b 65 2c 20 61 6e 64 20 77 68 61 74 20 74 68 ake, and what th
4c400 65 79 20 64 6f 20 61 72 65 20 61 6c 6c 20 73 75 ey do are all su
4c410 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 bject to change
4c420 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 0a without notice..
4c430 2a 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f ** Unlike most o
4c440 66 20 74 68 65 20 53 51 4c 69 74 65 20 41 50 49 f the SQLite API
4c450 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
4c460 69 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 is not guarantee
4c470 64 20 74 6f 0a 2a 2a 20 6f 70 65 72 61 74 65 20 d to.** operate
4c480 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 66 72 6f consistently fro
4c490 6d 20 6f 6e 65 20 72 65 6c 65 61 73 65 20 74 6f m one release to
4c4a0 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51 the next..*/.SQ
4c4b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
4c4c0 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f ite3_test_contro
4c4d0 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a l(int op, ...);.
4c4e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
4c4f0 20 54 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 Testing Interfa
4c500 63 65 20 4f 70 65 72 61 74 69 6f 6e 20 43 6f 64 ce Operation Cod
4c510 65 73 20 7b 48 31 31 34 31 30 7d 20 3c 48 31 31 es {H11410} <H11
4c520 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 400>.**.** These
4c530 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 constants are t
4c540 68 65 20 76 61 6c 69 64 20 6f 70 65 72 61 74 69 he valid operati
4c550 6f 6e 20 63 6f 64 65 20 70 61 72 61 6d 65 74 65 on code paramete
4c560 72 73 20 75 73 65 64 0a 2a 2a 20 61 73 20 74 68 rs used.** as th
4c570 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
4c580 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 to [sqlite3_tes
4c590 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a t_control()]..**
4c5a0 0a 2a 2a 20 54 68 65 73 65 20 70 61 72 61 6d 65 .** These parame
4c5b0 74 65 72 73 20 61 6e 64 20 74 68 65 69 72 20 6d ters and their m
4c5c0 65 61 6e 69 6e 67 73 20 61 72 65 20 73 75 62 6a eanings are subj
4c5d0 65 63 74 20 74 6f 20 63 68 61 6e 67 65 0a 2a 2a ect to change.**
4c5e0 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e without notice.
4c5f0 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 61 These values a
4c600 72 65 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 re for testing p
4c610 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a urposes only..**
4c620 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 Applications sh
4c630 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 61 6e 79 ould not use any
4c640 20 6f 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 of these parame
4c650 74 65 72 73 20 6f 72 20 74 68 65 0a 2a 2a 20 5b ters or the.** [
4c660 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e sqlite3_test_con
4c670 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 trol()] interfac
4c680 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e..*/.#define SQ
4c690 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 LITE_TESTCTRL_PR
4c6a0 4e 47 5f 53 41 56 45 20 20 20 20 20 20 20 20 20 NG_SAVE
4c6b0 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 5.#define
4c6c0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c SQLITE_TESTCTRL
4c6d0 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45 20 20 20 _PRNG_RESTORE
4c6e0 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 6.#def
4c6f0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 ine SQLITE_TESTC
4c700 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54 20 20 TRL_PRNG_RESET
4c710 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 7.#
4c720 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
4c730 53 54 43 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 STCTRL_BITVEC_TE
4c740 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ST
4c750 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 8.#define SQLITE
4c760 5f 54 45 53 54 43 54 52 4c 5f 46 41 55 4c 54 5f _TESTCTRL_FAULT_
4c770 49 4e 53 54 41 4c 4c 20 20 20 20 20 20 20 20 20 INSTALL
4c780 20 20 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 9.#define SQL
4c790 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e ITE_TESTCTRL_BEN
4c7a0 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 IGN_MALLOC_HOOKS
4c7b0 20 20 20 20 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 10../*.** C
4c7c0 41 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 20 API3REF: SQLite
4c7d0 52 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 7b Runtime Status {
4c7e0 48 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 3e H17200} <S60200>
4c7f0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
4c800 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
4c810 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f rface is used to
4c820 20 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d retrieve runtim
4c830 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 e status informa
4c840 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 68 tion.** about th
4c850 65 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f 66 e preformance of
4c860 20 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 74 SQLite, and opt
4c870 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 74 ionally to reset
4c880 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 68 various.** high
4c890 77 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 68 water marks. Th
4c8a0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
4c8b0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 is an integer c
4c8c0 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 ode for.** the s
4c8d0 70 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 65 pecific paramete
4c8e0 72 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 52 r to measure. R
4c8f0 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 65 ecognized intege
4c900 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 6f r codes.** are o
4c910 66 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c 49 f the form [SQLI
4c920 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 TE_STATUS_MEMORY
4c930 5f 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f 53 _USED | SQLITE_S
4c940 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 54 TATUS_...]..** T
4c950 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 he current value
4c960 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 of the paramete
4c970 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e r is returned in
4c980 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a to *pCurrent..**
4c990 20 54 68 65 20 68 69 67 68 65 73 74 20 72 65 63 The highest rec
4c9a0 6f 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 72 orded value is r
4c9b0 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 67 eturned in *pHig
4c9c0 68 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 0a hwater. If the.
4c9d0 2a 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 20 ** resetFlag is
4c9e0 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 true, then the h
4c9f0 69 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 61 ighest record va
4ca00 6c 75 65 20 69 73 20 72 65 73 65 74 20 61 66 74 lue is reset aft
4ca10 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 er.** *pHighwate
4ca20 72 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 6f r is written. So
4ca30 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 6f me parameters do
4ca40 20 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 20 not record the
4ca50 68 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 65 highest.** value
4ca60 2e 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 72 . For those par
4ca70 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 ameters.** nothi
4ca80 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e ng is written in
4ca90 74 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 to *pHighwater a
4caa0 6e 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 67 nd the resetFlag
4cab0 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 is ignored..**
4cac0 4f 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73 Other parameters
4cad0 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65 record only the
4cae0 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 highwater mark
4caf0 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72 and not the curr
4cb00 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 ent.** value. F
4cb10 6f 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 or these latter
4cb20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69 parameters nothi
4cb30 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e ng is written in
4cb40 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a to *pCurrent..**
4cb50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
4cb60 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
4cb70 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e OK on success an
4cb80 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 d a non-zero.**
4cb90 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 [error code] on
4cba0 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 failure..**.** T
4cbb0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 his routine is t
4cbc0 68 72 65 61 64 73 61 66 65 20 62 75 74 20 69 73 hreadsafe but is
4cbd0 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 68 not atomic. Th
4cbe0 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 0a 2a is routine can.*
4cbf0 2a 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20 6f * called while o
4cc00 74 68 65 72 20 74 68 72 65 61 64 73 20 61 72 65 ther threads are
4cc10 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 73 61 6d running the sam
4cc20 65 20 6f 72 20 64 69 66 66 65 72 65 6e 74 20 53 e or different S
4cc30 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 66 61 QLite.** interfa
4cc40 63 65 73 2e 20 20 48 6f 77 65 76 65 72 20 74 68 ces. However th
4cc50 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 e values returne
4cc60 64 20 69 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 d in *pCurrent a
4cc70 6e 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 nd.** *pHighwate
4cc80 72 20 72 65 66 6c 65 63 74 20 74 68 65 20 73 74 r reflect the st
4cc90 61 74 75 73 20 6f 66 20 53 51 4c 69 74 65 20 61 atus of SQLite a
4cca0 74 20 64 69 66 66 65 72 65 6e 74 20 70 6f 69 6e t different poin
4ccb0 74 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20 61 6e ts in time.** an
4ccc0 64 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 d it is possible
4ccd0 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 that another th
4cce0 72 65 61 64 20 6d 69 67 68 74 20 63 68 61 6e 67 read might chang
4ccf0 65 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a e the parameter.
4cd00 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 ** in between th
4cd10 65 20 74 69 6d 65 73 20 77 68 65 6e 20 2a 70 43 e times when *pC
4cd20 75 72 72 65 6e 74 20 61 6e 64 20 2a 70 48 69 67 urrent and *pHig
4cd30 68 77 61 74 65 72 20 61 72 65 20 77 72 69 74 74 hwater are writt
4cd40 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c en..**.** See al
4cd50 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f so: [sqlite3_db_
4cd60 73 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53 51 4c status()].*/.SQL
4cd70 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 ITE_API SQLITE_E
4cd80 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 XPERIMENTAL int
4cd90 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 sqlite3_status(i
4cda0 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 nt op, int *pCur
4cdb0 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 rent, int *pHigh
4cdc0 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 water, int reset
4cdd0 46 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 Flag);.../*.** C
4cde0 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 API3REF: Status
4cdf0 50 61 72 61 6d 65 74 65 72 73 20 7b 48 31 37 32 Parameters {H172
4ce00 35 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a 2a 20 50} <H17200>.**
4ce10 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
4ce20 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 ** These integer
4ce30 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69 67 constants desig
4ce40 6e 61 74 65 20 76 61 72 69 6f 75 73 20 72 75 6e nate various run
4ce50 2d 74 69 6d 65 20 73 74 61 74 75 73 20 70 61 72 -time status par
4ce60 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 ameters.** that
4ce70 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 can be returned
4ce80 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 by [sqlite3_stat
4ce90 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c us()]..**.** <dl
4cea0 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f >.** <dt>SQLITE_
4ceb0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
4cec0 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ED</dt>.** <dd>T
4ced0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 his parameter is
4cee0 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d 6f the current amo
4cef0 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 68 unt of memory ch
4cf00 65 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73 69 ecked out.** usi
4cf10 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c ng [sqlite3_mall
4cf20 6f 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64 69 oc()], either di
4cf30 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 rectly or indire
4cf40 63 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66 69 ctly. The.** fi
4cf50 67 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63 61 gure includes ca
4cf60 6c 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71 6c lls made to [sql
4cf70 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 ite3_malloc()] b
4cf80 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f y the applicatio
4cf90 6e 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e 61 n.** and interna
4cfa0 6c 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 62 l memory usage b
4cfb0 79 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 y the SQLite lib
4cfc0 72 61 72 79 2e 20 20 53 63 72 61 74 63 68 20 6d rary. Scratch m
4cfd0 65 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c emory.** control
4cfe0 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 led by [SQLITE_C
4cff0 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61 ONFIG_SCRATCH] a
4d000 6e 64 20 61 75 78 69 6c 69 61 72 79 20 70 61 67 nd auxiliary pag
4d010 65 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f 72 e-cache.** memor
4d020 79 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 y controlled by
4d030 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 [SQLITE_CONFIG_P
4d040 41 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f 74 AGECACHE] is not
4d050 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a 20 included in.**
4d060 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e 20 this parameter.
4d070 20 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74 75 The amount retu
4d080 72 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d 20 rned is the sum
4d090 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f of the allocatio
4d0a0 6e 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72 65 n.** sizes as re
4d0b0 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 78 53 ported by the xS
4d0c0 69 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b 73 ize method in [s
4d0d0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
4d0e0 64 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ds].</dd>.**.**
4d0f0 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 <dt>SQLITE_STATU
4d100 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c 2f 64 S_MALLOC_SIZE</d
4d110 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 t>.** <dd>This p
4d120 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 arameter records
4d130 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d the largest mem
4d140 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
4d150 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 equest.** handed
4d160 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c to [sqlite3_mal
4d170 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 loc()] or [sqlit
4d180 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 28 6f e3_realloc()] (o
4d190 72 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74 65 72 r their.** inter
4d1a0 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e 74 73 29 nal equivalents)
4d1b0 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 . Only the valu
4d1c0 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 e returned in th
4d1d0 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 e.** *pHighwater
4d1e0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
4d1f0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d qlite3_status()]
4d200 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e is of interest.
4d210 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 .** The value
4d220 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
4d230 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d *pCurrent param
4d240 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 eter is undefine
4d250 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 d.</dd>.**.** <d
4d260 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f t>SQLITE_STATUS_
4d270 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 3c 2f PAGECACHE_USED</
4d280 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
4d290 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e parameter return
4d2a0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
4d2b0 70 61 67 65 73 20 75 73 65 64 20 6f 75 74 20 6f pages used out o
4d2c0 66 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65 63 61 f the.** [pageca
4d2d0 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 che memory alloc
4d2e0 61 74 6f 72 5d 20 74 68 61 74 20 77 61 73 20 63 ator] that was c
4d2f0 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 20 onfigured using
4d300 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 .** [SQLITE_CONF
4d310 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 20 20 IG_PAGECACHE].
4d320 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72 65 74 The.** value ret
4d330 75 72 6e 65 64 20 69 73 20 69 6e 20 70 61 67 65 urned is in page
4d340 73 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65 73 2e s, not in bytes.
4d350 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
4d360 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
4d370 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 GECACHE_OVERFLOW
4d380 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
4d390 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 s parameter retu
4d3a0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
4d3b0 66 20 62 79 74 65 73 20 6f 66 20 70 61 67 65 20 f bytes of page
4d3c0 63 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 cache.** allocat
4d3d0 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64 20 ion which could
4d3e0 6e 6f 74 20 62 65 20 73 74 61 74 69 73 66 69 65 not be statisfie
4d3f0 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45 d by the [SQLITE
4d400 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
4d410 45 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e 64 E].** buffer and
4d420 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 6f where forced to
4d430 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 71 overflow to [sq
4d440 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e lite3_malloc()].
4d450 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 The.** returne
4d460 64 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 73 d value includes
4d470 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 allocations tha
4d480 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63 t overflowed bec
4d490 61 75 73 65 20 74 68 65 79 0a 2a 2a 20 77 68 65 ause they.** whe
4d4a0 72 65 20 74 6f 6f 20 6c 61 72 67 65 20 28 74 68 re too large (th
4d4b0 65 79 20 77 65 72 65 20 6c 61 72 67 65 72 20 74 ey were larger t
4d4c0 68 61 6e 20 74 68 65 20 22 73 7a 22 20 70 61 72 han the "sz" par
4d4d0 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 53 51 ameter to.** [SQ
4d4e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 LITE_CONFIG_PAGE
4d4f0 43 41 43 48 45 5d 29 20 61 6e 64 20 61 6c 6c 6f CACHE]) and allo
4d500 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 cations that ove
4d510 72 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65 0a rflowed because.
4d520 2a 2a 20 6e 6f 20 73 70 61 63 65 20 77 61 73 20 ** no space was
4d530 6c 65 66 74 20 69 6e 20 74 68 65 20 70 61 67 65 left in the page
4d540 20 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a cache.</dd>.**.
4d550 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
4d560 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 ATUS_PAGECACHE_S
4d570 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e IZE</dt>.** <dd>
4d580 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 This parameter r
4d590 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 ecords the large
4d5a0 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 st memory alloca
4d5b0 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 tion request.**
4d5c0 68 61 6e 64 65 64 20 74 6f 20 5b 70 61 67 65 63 handed to [pagec
4d5d0 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ache memory allo
4d5e0 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 cator]. Only th
4d5f0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
4d600 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 in the.** *pHig
4d610 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 hwater parameter
4d620 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 to [sqlite3_sta
4d630 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 tus()] is of int
4d640 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 erest. .** The
4d650 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e value written in
4d660 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 to the *pCurrent
4d670 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e parameter is un
4d680 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a defined.</dd>.**
4d690 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
4d6a0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 TATUS_SCRATCH_US
4d6b0 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ED</dt>.** <dd>T
4d6c0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
4d6d0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
4d6e0 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 of allocations
4d6f0 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a used out of the.
4d700 2a 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f ** [scratch memo
4d710 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f ry allocator] co
4d720 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a nfigured using.*
4d730 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 * [SQLITE_CONFIG
4d740 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 20 _SCRATCH]. The
4d750 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 value returned i
4d760 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 s in allocations
4d770 2c 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74 65 , not.** in byte
4d780 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e 67 s. Since a sing
4d790 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e le thread may on
4d7a0 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72 61 ly have one scra
4d7b0 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a tch allocation.*
4d7c0 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 * outstanding at
4d7d0 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72 61 time, this para
4d7e0 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f 72 meter also repor
4d7f0 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ts the number of
4d800 20 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69 6e threads.** usin
4d810 67 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 g scratch memory
4d820 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
4d830 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 e.</dd>.**.** <d
4d840 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f t>SQLITE_STATUS_
4d850 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 SCRATCH_OVERFLOW
4d860 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
4d870 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 s parameter retu
4d880 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
4d890 66 20 62 79 74 65 73 20 6f 66 20 73 63 72 61 74 f bytes of scrat
4d8a0 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c ch memory.** all
4d8b0 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f ocation which co
4d8c0 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 69 uld not be stati
4d8d0 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 51 sfied by the [SQ
4d8e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 LITE_CONFIG_SCRA
4d8f0 54 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 TCH].** buffer a
4d900 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 nd where forced
4d910 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b to overflow to [
4d920 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
4d930 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 73 0a 2a ]. The values.*
4d940 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 * returned inclu
4d950 64 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 65 63 de overflows bec
4d960 61 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 ause the request
4d970 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 ed allocation wa
4d980 73 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65 72 20 s too.** larger
4d990 28 74 68 61 74 20 69 73 2c 20 62 65 63 61 75 73 (that is, becaus
4d9a0 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 e the requested
4d9b0 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6c allocation was l
4d9c0 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a 2a arger than the.*
4d9d0 2a 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72 * "sz" parameter
4d9e0 20 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 to [SQLITE_CONF
4d9f0 49 47 5f 53 43 52 41 54 43 48 5d 29 20 61 6e 64 IG_SCRATCH]) and
4da00 20 62 65 63 61 75 73 65 20 6e 6f 20 73 63 72 61 because no scra
4da10 74 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 73 6c tch buffer.** sl
4da20 6f 74 73 20 77 65 72 65 20 61 76 61 69 6c 61 62 ots were availab
4da30 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a le..** </dd>.**.
4da40 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
4da50 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a ATUS_SCRATCH_SIZ
4da60 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 E</dt>.** <dd>Th
4da70 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 is parameter rec
4da80 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 ords the largest
4da90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
4daa0 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 on request.** ha
4dab0 6e 64 65 64 20 74 6f 20 5b 73 63 72 61 74 63 68 nded to [scratch
4dac0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
4dad0 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 r]. Only the va
4dae0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 lue returned in
4daf0 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 the.** *pHighwat
4db00 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 er parameter to
4db10 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 [sqlite3_status(
4db20 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 )] is of interes
4db30 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 t. .** The valu
4db40 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
4db50 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 he *pCurrent par
4db60 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 ameter is undefi
4db70 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ned.</dd>.**.**
4db80 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 <dt>SQLITE_STATU
4db90 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 3c 2f S_PARSER_STACK</
4dba0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
4dbb0 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 parameter record
4dbc0 73 20 74 68 65 20 64 65 65 70 65 73 74 20 70 61 s the deepest pa
4dbd0 72 73 65 72 20 73 74 61 63 6b 2e 20 20 49 74 20 rser stack. It
4dbe0 69 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69 is only.** meani
4dbf0 6e 67 66 75 6c 20 69 66 20 53 51 4c 69 74 65 20 ngful if SQLite
4dc00 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 is compiled with
4dc10 20 5b 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 [YYTRACKMAXSTAC
4dc20 4b 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a 2a 2a KDEPTH].</dd>.**
4dc30 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77 </dl>.**.** New
4dc40 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 status paramete
4dc50 72 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 rs may be added
4dc60 66 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 6d from time to tim
4dc70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e..*/.#define SQ
4dc80 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
4dc90 52 59 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 RY_USED
4dca0 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 0.#define SQLIT
4dcb0 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 E_STATUS_PAGECAC
4dcc0 48 45 5f 55 53 45 44 20 20 20 20 20 20 20 31 0a HE_USED 1.
4dcd0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
4dce0 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
4dcf0 4f 56 45 52 46 4c 4f 57 20 20 20 32 0a 23 64 65 OVERFLOW 2.#de
4dd00 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 fine SQLITE_STAT
4dd10 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 20 US_SCRATCH_USED
4dd20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 3.#defin
4dd30 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f e SQLITE_STATUS_
4dd40 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 SCRATCH_OVERFLOW
4dd50 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 4.#define S
4dd60 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c QLITE_STATUS_MAL
4dd70 4c 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 20 20 LOC_SIZE
4dd80 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 5.#define SQLI
4dd90 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 TE_STATUS_PARSER
4dda0 5f 53 54 41 43 4b 20 20 20 20 20 20 20 20 20 36 _STACK 6
4ddb0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
4ddc0 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
4ddd0 5f 53 49 5a 45 20 20 20 20 20 20 20 37 0a 23 64 _SIZE 7.#d
4dde0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 efine SQLITE_STA
4ddf0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 TUS_SCRATCH_SIZE
4de00 20 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a 0a 2a 8../*.*
4de10 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 * CAPI3REF: Data
4de20 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 base Connection
4de30 53 74 61 74 75 73 20 7b 48 31 37 35 30 30 7d 20 Status {H17500}
4de40 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 <S60200>.** EXPE
4de50 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
4de60 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 his interface is
4de70 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 used to retriev
4de80 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75 73 e runtime status
4de90 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a information .**
4dea0 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20 about a single
4deb0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
4dec0 74 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 72 73 tion]. The firs
4ded0 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 t argument is th
4dee0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f e.** database co
4def0 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 20 nnection object
4df00 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 to be interrogat
4df10 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 ed. The second
4df20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 argument.** is t
4df30 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 he parameter to
4df40 69 6e 74 65 72 72 6f 67 61 74 65 2e 20 20 43 75 interrogate. Cu
4df50 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c rrently, the onl
4df60 79 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 0a y allowed value.
4df70 2a 2a 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e ** for the secon
4df80 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 5b d parameter is [
4df90 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
4dfa0 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 5d 2e LOOKASIDE_USED].
4dfb0 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6f .** Additional o
4dfc0 70 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 ptions will like
4dfd0 6c 79 20 61 70 70 65 61 72 20 69 6e 20 66 75 74 ly appear in fut
4dfe0 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 ure releases of
4dff0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 SQLite..**.** Th
4e000 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
4e010 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 of the requested
4e020 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 77 72 parameter is wr
4e030 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 itten into *pCur
4e040 0a 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 67 68 .** and the high
4e050 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 est instantaneou
4e060 73 20 76 61 6c 75 65 20 69 73 20 77 72 69 74 74 s value is writt
4e070 65 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 72 2e en into *pHiwtr.
4e080 20 20 49 66 0a 2a 2a 20 74 68 65 20 72 65 73 65 If.** the rese
4e090 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 tFlg is true, th
4e0a0 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 20 69 en the highest i
4e0b0 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c nstantaneous val
4e0c0 75 65 20 69 73 0a 2a 2a 20 72 65 73 65 74 20 62 ue is.** reset b
4e0d0 61 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 ack down to the
4e0e0 63 75 72 72 65 6e 74 20 76 61 6c 75 65 2e 0a 2a current value..*
4e0f0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
4e100 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 sqlite3_status()
4e110 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 ] and [sqlite3_s
4e120 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a tmt_status()]..*
4e130 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
4e140 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
4e150 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f int sqlite3_db_
4e160 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 2a 2c status(sqlite3*,
4e170 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 int op, int *pC
4e180 75 72 2c 20 69 6e 74 20 2a 70 48 69 77 74 72 2c ur, int *pHiwtr,
4e190 20 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a int resetFlg);.
4e1a0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
4e1b0 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 Status Paramete
4e1c0 72 73 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 rs for database
4e1d0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48 31 37 connections {H17
4e1e0 35 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a 2a 2a 520} <H17500>.**
4e1f0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
4e200 0a 2a 2a 20 53 74 61 74 75 73 20 76 65 72 62 73 .** Status verbs
4e210 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 64 62 for [sqlite3_db
4e220 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a _status()]..**.*
4e230 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 * <dl>.** <dt>SQ
4e240 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f LITE_DBSTATUS_LO
4e250 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 OKASIDE_USED</dt
4e260 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 >.** <dd>This pa
4e270 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 rameter returns
4e280 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f the number of lo
4e290 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 okaside memory s
4e2a0 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a lots currently.*
4e2b0 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f * checked out.</
4e2c0 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a dd>.** </dl>.*/.
4e2d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
4e2e0 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 BSTATUS_LOOKASID
4e2f0 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a 2f E_USED 0.../
4e300 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 *.** CAPI3REF: P
4e310 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e repared Statemen
4e320 74 20 53 74 61 74 75 73 20 7b 48 31 37 35 35 30 t Status {H17550
4e330 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 } <S60200>.** EX
4e340 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
4e350 20 45 61 63 68 20 70 72 65 70 61 72 65 64 20 73 Each prepared s
4e360 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 69 tatement maintai
4e370 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b 53 ns various.** [S
4e380 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
4e390 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 73 _SORT | counters
4e3a0 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 ] that measure t
4e3b0 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 he number.** of
4e3c0 74 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 times it has per
4e3d0 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 formed specific
4e3e0 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 65 operations. The
4e3f0 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e 0a se counters can.
4e400 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d 6f ** be used to mo
4e410 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f 72 nitor the perfor
4e420 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 72 69 mance characteri
4e430 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 72 65 stics of the pre
4e440 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 pared.** stateme
4e450 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c nts. For exampl
4e460 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 e, if the number
4e470 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73 20 of table steps
4e480 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 73 0a greatly exceeds.
4e490 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
4e4a0 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 73 20 table searches
4e4b0 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c 20 or result rows,
4e4c0 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 20 that would tend
4e4d0 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 to indicate.** t
4e4e0 68 61 74 20 74 68 65 20 70 72 65 70 61 72 65 64 hat the prepared
4e4f0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75 73 statement is us
4e500 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 ing a full table
4e510 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68 61 scan rather tha
4e520 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20 n.** an index.
4e530 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
4e540 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f rface is used to
4e550 20 72 65 74 72 69 65 76 65 20 61 6e 64 20 72 65 retrieve and re
4e560 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 set counter valu
4e570 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 72 es from.** a [pr
4e580 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
4e590 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 ]. The first ar
4e5a0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 72 gument is the pr
4e5b0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
4e5c0 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 .** object to be
4e5d0 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 interrogated.
4e5e0 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
4e5f0 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 ent.** is an int
4e600 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20 eger code for a
4e610 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45 specific [SQLITE
4e620 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 _STMTSTATUS_SORT
4e630 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 | counter].** t
4e640 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 o be interrogate
4e650 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 d. .** The curre
4e660 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 nt value of the
4e670 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 requested counte
4e680 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a r is returned..*
4e690 2a 20 49 66 20 74 68 65 20 72 65 73 65 74 46 6c * If the resetFl
4e6a0 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 g is true, then
4e6b0 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 the counter is r
4e6c0 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 eset to zero aft
4e6d0 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 er this.** inter
4e6e0 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e face call return
4e6f0 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 s..**.** See als
4e700 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 o: [sqlite3_stat
4e710 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 us()] and [sqlit
4e720 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e e3_db_status()].
4e730 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
4e740 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
4e750 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 AL int sqlite3_s
4e760 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 tmt_status(sqlit
4e770 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70 e3_stmt*, int op
4e780 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a ,int resetFlg);.
4e790 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
4e7a0 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 Status Paramete
4e7b0 72 73 20 66 6f 72 20 70 72 65 70 61 72 65 64 20 rs for prepared
4e7c0 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 35 statements {H175
4e7d0 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 20 70} <H17550>.**
4e7e0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
4e7f0 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 ** These preproc
4e800 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 essor macros def
4e810 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 ine integer code
4e820 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e s that name coun
4e830 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 ter.** values as
4e840 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
4e850 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f e [sqlite3_stmt_
4e860 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 status()] interf
4e870 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e ace..** The mean
4e880 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 ings of the vari
4e890 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 ous counters are
4e8a0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
4e8b0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 ** <dl>.** <dt>S
4e8c0 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
4e8d0 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c 2f _FULLSCAN_STEP</
4e8e0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
4e8f0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
4e900 20 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c 69 times that SQLi
4e910 74 65 20 68 61 73 20 73 74 65 70 70 65 64 20 66 te has stepped f
4e920 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 74 orward in.** a t
4e930 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66 20 able as part of
4e940 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 a full table sca
4e950 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 72 n. Large number
4e960 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e 74 s for this count
4e970 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 61 er.** may indica
4e980 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 73 te opportunities
4e990 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 for performance
4e9a0 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 72 improvement thr
4e9b0 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 6c ough .** careful
4e9c0 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e use of indices.
4e9d0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
4e9e0 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 SQLITE_STMTSTATU
4e9f0 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c S_SORT</dt>.** <
4ea00 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e dd>This is the n
4ea10 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70 umber of sort op
4ea20 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61 erations that ha
4ea30 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 ve occurred..**
4ea40 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 A non-zero value
4ea50 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72 in this counter
4ea60 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e may indicate an
4ea70 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a opportunity to.
4ea80 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70 ** improvement p
4ea90 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75 erformance throu
4eaa0 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f gh careful use o
4eab0 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a f indices.</dd>.
4eac0 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 **.** </dl>.*/.#
4ead0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
4eae0 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 MTSTATUS_FULLSCA
4eaf0 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64 65 N_STEP 1.#de
4eb00 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54 fine SQLITE_STMT
4eb10 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20 20 STATUS_SORT
4eb20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2../*.*
4eb30 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75 73 74 * CAPI3REF: Cust
4eb40 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20 4f 62 om Page Cache Ob
4eb50 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45 ject.** EXPERIME
4eb60 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 NTAL.**.** The s
4eb70 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 74 79 qlite3_pcache ty
4eb80 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20 20 49 pe is opaque. I
4eb90 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 t is implemented
4eba0 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67 67 by.** the plugg
4ebb0 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 able module. Th
4ebc0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 68 61 e SQLite core ha
4ebd0 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 6f s no knowledge o
4ebe0 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f 72 f.** its size or
4ebf0 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 internal struct
4ec00 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20 64 65 ure and never de
4ec10 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 als with the.**
4ec20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 6f sqlite3_pcache o
4ec30 62 6a 65 63 74 20 65 78 63 65 70 74 20 62 79 20 bject except by
4ec40 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 73 73 holding and pass
4ec50 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 ing pointers.**
4ec60 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a to the object..*
4ec70 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74 65 *.** See [sqlite
4ec80 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3_pcache_methods
4ec90 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c ] for additional
4eca0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
4ecb0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4ecc0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 73 sqlite3_pcache s
4ecd0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b 0a 0a qlite3_pcache;..
4ece0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
4ecf0 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 69 Application Defi
4ed00 6e 65 64 20 50 61 67 65 20 43 61 63 68 65 2e 0a ned Page Cache..
4ed10 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
4ed20 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 **.** The [sqlit
4ed30 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 e3_config]([SQLI
4ed40 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 TE_CONFIG_PCACHE
4ed50 5d 2c 20 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63 ], ...) interfac
4ed60 65 20 63 61 6e 0a 2a 2a 20 72 65 67 69 73 74 65 e can.** registe
4ed70 72 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 r an alternative
4ed80 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c page cache impl
4ed90 65 6d 65 6e 74 61 74 69 6f 6e 20 62 79 20 70 61 ementation by pa
4eda0 73 73 69 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 ssing in an .**
4edb0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
4edc0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d sqlite3_pcache_m
4edd0 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 ethods structure
4ede0 2e 20 54 68 65 20 6d 61 6a 6f 72 69 74 79 20 6f . The majority o
4edf0 66 20 74 68 65 20 0a 2a 2a 20 68 65 61 70 20 6d f the .** heap m
4ee00 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 73 71 emory used by sq
4ee10 6c 69 74 65 20 69 73 20 75 73 65 64 20 62 79 20 lite is used by
4ee20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 74 the page cache t
4ee30 6f 20 63 61 63 68 65 20 64 61 74 61 20 72 65 61 o cache data rea
4ee40 64 20 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 d .** from, or r
4ee50 65 61 64 79 20 74 6f 20 62 65 20 77 72 69 74 74 eady to be writt
4ee60 65 6e 20 74 6f 2c 20 74 68 65 20 64 61 74 61 62 en to, the datab
4ee70 61 73 65 20 66 69 6c 65 2e 20 42 79 20 69 6d 70 ase file. By imp
4ee80 6c 65 6d 65 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 lementing a .**
4ee90 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 custom page cach
4eea0 65 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 e using this API
4eeb0 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e , an application
4eec0 20 63 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 can control mor
4eed0 65 20 0a 2a 2a 20 70 72 65 63 69 73 65 6c 79 20 e .** precisely
4eee0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 the amount of me
4eef0 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 62 79 mory consumed by
4ef00 20 73 71 6c 69 74 65 2c 20 74 68 65 20 77 61 79 sqlite, the way
4ef10 20 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20 73 61 in which .** sa
4ef20 69 64 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c id memory is all
4ef30 6f 63 61 74 65 64 20 61 6e 64 20 72 65 6c 65 61 ocated and relea
4ef40 73 65 64 2c 20 61 6e 64 20 74 68 65 20 70 6f 6c sed, and the pol
4ef50 69 63 69 65 73 20 75 73 65 64 20 74 6f 20 0a 2a icies used to .*
4ef60 2a 20 64 65 74 65 72 6d 69 6e 65 20 65 78 61 63 * determine exac
4ef70 74 6c 79 20 77 68 69 63 68 20 70 61 72 74 73 20 tly which parts
4ef80 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69 of a database fi
4ef90 6c 65 20 61 72 65 20 63 61 63 68 65 64 20 61 6e le are cached an
4efa0 64 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f d for .** how lo
4efb0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f ng..**.** The co
4efc0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 73 74 ntents of the st
4efd0 72 75 63 74 75 72 65 20 61 72 65 20 63 6f 70 69 ructure are copi
4efe0 65 64 20 74 6f 20 61 6e 20 69 6e 74 65 72 6e 61 ed to an interna
4eff0 6c 20 62 75 66 66 65 72 20 62 79 20 73 71 6c 69 l buffer by sqli
4f000 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 te.** within the
4f010 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
4f020 33 5f 63 6f 6e 66 69 67 5d 2e 0a 2a 2a 0a 2a 2a 3_config]..**.**
4f030 20 54 68 65 20 78 49 6e 69 74 28 29 20 6d 65 74 The xInit() met
4f040 68 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e hod is called on
4f050 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c ce for each call
4f060 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 to [sqlite3_ini
4f070 74 69 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 28 75 tialize()].** (u
4f080 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 65 sually only once
4f090 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 during the life
4f0a0 74 69 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 63 time of the proc
4f0b0 65 73 73 29 2e 20 49 74 20 69 73 20 70 61 73 73 ess). It is pass
4f0c0 65 64 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 ed.** a copy of
4f0d0 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 the sqlite3_pcac
4f0e0 68 65 5f 6d 65 74 68 6f 64 73 2e 70 41 72 67 20 he_methods.pArg
4f0f0 76 61 6c 75 65 2e 20 49 74 20 63 61 6e 20 62 65 value. It can be
4f100 20 75 73 65 64 20 74 6f 20 73 65 74 0a 2a 2a 20 used to set.**
4f110 75 70 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 up global struct
4f120 75 72 65 73 20 61 6e 64 20 6d 75 74 65 78 65 73 ures and mutexes
4f130 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 required by the
4f140 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 custom page cac
4f150 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 he .** implement
4f160 61 74 69 6f 6e 2e 20 54 68 65 20 78 53 68 75 74 ation. The xShut
4f170 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 down() method is
4f180 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
4f190 68 69 6e 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 hin .** [sqlite3
4f1a0 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2c 20 69 66 _shutdown()], if
4f1b0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
4f1c0 20 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 invokes this AP
4f1d0 49 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 I. It can be use
4f1e0 64 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20 75 70 d.** to clean up
4f1f0 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 any outstanding
4f200 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 resources befor
4f210 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f e process shutdo
4f220 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e wn, if required.
4f230 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 .**.** The xCrea
4f240 74 65 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 te() method is u
4f250 73 65 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 sed to construct
4f260 20 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e 73 a new cache ins
4f270 74 61 6e 63 65 2e 20 54 68 65 0a 2a 2a 20 66 69 tance. The.** fi
4f280 72 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20 73 rst parameter, s
4f290 7a 50 61 67 65 2c 20 69 73 20 74 68 65 20 73 69 zPage, is the si
4f2a0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 ze in bytes of t
4f2b0 68 65 20 70 61 67 65 73 20 74 68 61 74 20 6d 75 he pages that mu
4f2c0 73 74 0a 2a 2a 20 62 65 20 61 6c 6c 6f 63 61 74 st.** be allocat
4f2d0 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 2e ed by the cache.
4f2e0 20 73 7a 50 61 67 65 20 77 69 6c 6c 20 6e 6f 74 szPage will not
4f2f0 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 be a power of t
4f300 77 6f 2e 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e wo. The.** secon
4f310 64 20 61 72 67 75 6d 65 6e 74 2c 20 62 50 75 72 d argument, bPur
4f320 67 65 61 62 6c 65 2c 20 69 73 20 74 72 75 65 20 geable, is true
4f330 69 66 20 74 68 65 20 63 61 63 68 65 20 62 65 69 if the cache bei
4f340 6e 67 20 63 72 65 61 74 65 64 20 77 69 6c 6c 0a ng created will.
4f350 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 63 61 ** be used to ca
4f360 63 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 che database pag
4f370 65 73 20 72 65 61 64 20 66 72 6f 6d 20 61 20 66 es read from a f
4f380 69 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 ile stored on di
4f390 73 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 sk, or.** false
4f3a0 69 66 20 69 74 20 69 73 20 75 73 65 64 20 66 6f if it is used fo
4f3b0 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 r an in-memory d
4f3c0 61 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 atabase. The cac
4f3d0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
4f3e0 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 n.** does not ha
4f3f0 76 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e ve to do anythin
4f400 67 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 g special based
4f410 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 on the value of
4f420 62 50 75 72 67 65 61 62 6c 65 2c 0a 2a 2a 20 69 bPurgeable,.** i
4f430 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69 t is purely advi
4f440 73 6f 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 sory. .**.** The
4f450 20 78 43 61 63 68 65 73 69 7a 65 28 29 20 6d 65 xCachesize() me
4f460 74 68 6f 64 20 6d 61 79 20 62 65 20 63 61 6c 6c thod may be call
4f470 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 62 ed at any time b
4f480 79 20 53 51 4c 69 74 65 20 74 6f 20 73 65 74 20 y SQLite to set
4f490 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65 64 the.** suggested
4f4a0 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 2d 73 maximum cache-s
4f4b0 69 7a 65 20 28 6e 75 6d 62 65 72 20 6f 66 20 70 ize (number of p
4f4c0 61 67 65 73 20 73 74 6f 72 65 64 20 62 79 29 20 ages stored by)
4f4d0 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 6e 73 the cache.** ins
4f4e0 74 61 6e 63 65 20 70 61 73 73 65 64 20 61 73 20 tance passed as
4f4f0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
4f500 6e 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 nt. This is the
4f510 76 61 6c 75 65 20 63 6f 6e 66 69 67 75 72 65 64 value configured
4f520 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 using.** the SQ
4f530 4c 69 74 65 20 22 5b 50 52 41 47 4d 41 20 63 61 Lite "[PRAGMA ca
4f540 63 68 65 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d 61 che_size]" comma
4f550 6e 64 2e 20 41 73 20 77 69 74 68 20 74 68 65 20 nd. As with the
4f560 62 50 75 72 67 65 61 62 6c 65 20 70 61 72 61 6d bPurgeable param
4f570 65 74 65 72 2c 0a 2a 2a 20 74 68 65 20 69 6d 70 eter,.** the imp
4f580 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e lementation is n
4f590 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 64 ot required to d
4f5a0 6f 20 61 6e 79 74 68 69 6e 67 20 73 70 65 63 69 o anything speci
4f5b0 61 6c 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 al with this.**
4f5c0 76 61 6c 75 65 2c 20 69 74 20 69 73 20 61 64 76 value, it is adv
4f5d0 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a isory only..**.*
4f5e0 2a 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e 74 * The xPagecount
4f5f0 28 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 () method should
4f600 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 return the numb
4f610 65 72 20 6f 66 20 70 61 67 65 73 20 63 75 72 72 er of pages curr
4f620 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 20 ently.** stored
4f630 69 6e 20 74 68 65 20 63 61 63 68 65 20 73 75 70 in the cache sup
4f640 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 plied as an argu
4f650 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 ment..** .** The
4f660 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64 xFetch() method
4f670 20 69 73 20 75 73 65 64 20 74 6f 20 66 65 74 63 is used to fetc
4f680 68 20 61 20 70 61 67 65 20 61 6e 64 20 72 65 74 h a page and ret
4f690 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
4f6a0 20 69 74 2e 20 0a 2a 2a 20 41 20 27 70 61 67 65 it. .** A 'page
4f6b0 27 2c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74 65 ', in this conte
4f6c0 78 74 2c 20 69 73 20 61 20 62 75 66 66 65 72 20 xt, is a buffer
4f6d0 6f 66 20 73 7a 50 61 67 65 20 62 79 74 65 73 20 of szPage bytes
4f6e0 61 6c 69 67 6e 65 64 20 61 74 20 61 6e 0a 2a 2a aligned at an.**
4f6f0 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 8-byte boundary
4f700 2e 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 . The page to be
4f710 20 66 65 74 63 68 65 64 20 69 73 20 64 65 74 65 fetched is dete
4f720 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b 65 rmined by the ke
4f730 79 2e 20 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d 75 y. The.** mimimu
4f740 6d 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 31 m key value is 1
4f750 2e 20 41 66 74 65 72 20 69 74 20 68 61 73 20 62 . After it has b
4f760 65 65 6e 20 72 65 74 72 69 65 76 65 64 20 75 73 een retrieved us
4f770 69 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65 20 ing xFetch, the
4f780 70 61 67 65 20 0a 2a 2a 20 69 73 20 63 6f 6e 73 page .** is cons
4f790 69 64 65 72 65 64 20 74 6f 20 62 65 20 70 69 6e idered to be pin
4f7a0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ned..**.** If th
4f7b0 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 e requested page
4f7c0 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 is already in t
4f7d0 68 65 20 70 61 67 65 20 63 61 63 68 65 2c 20 74 he page cache, t
4f7e0 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f hen a pointer to
4f7f0 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 64 20 62 .** the cached b
4f800 75 66 66 65 72 20 73 68 6f 75 6c 64 20 62 65 20 uffer should be
4f810 72 65 74 75 72 6e 65 64 20 77 69 74 68 20 69 74 returned with it
4f820 73 20 63 6f 6e 74 65 6e 74 73 20 69 6e 74 61 63 s contents intac
4f830 74 2e 20 49 66 20 74 68 65 0a 2a 2a 20 70 61 67 t. If the.** pag
4f840 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 e is not already
4f850 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 in the cache, t
4f860 68 65 6e 20 74 68 65 20 65 78 70 65 63 74 65 64 hen the expected
4f870 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20 74 68 behaviour of th
4f880 65 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 64 65 e.** cache is de
4f890 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 termined by the
4f8a0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 value of the cre
4f8b0 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65 ateFlag paramete
4f8c0 72 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 78 r passed.** to x
4f8d0 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e 67 Fetch, according
4f8e0 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e to the followin
4f8f0 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c g table:.**.** <
4f900 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20 77 table border=1 w
4f910 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d 63 idth=85% align=c
4f920 65 6e 74 65 72 3e 0a 2a 2a 20 20 20 3c 74 72 3e enter>.** <tr>
4f930 3c 74 68 3e 63 72 65 61 74 65 46 6c 61 67 3c 74 <th>createFlag<t
4f940 68 3e 45 78 70 65 63 74 65 64 20 42 65 68 61 76 h>Expected Behav
4f950 69 6f 75 72 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 iour.** <tr><t
4f960 64 3e 30 3c 74 64 3e 4e 55 4c 4c 20 73 68 6f 75 d>0<td>NULL shou
4f970 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 ld be returned.
4f980 4e 6f 20 6e 65 77 20 63 61 63 68 65 20 65 6e 74 No new cache ent
4f990 72 79 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a ry is created..*
4f9a0 2a 20 20 20 3c 74 72 3e 3c 74 64 3e 31 3c 74 64 * <tr><td>1<td
4f9b0 3e 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 >If createFlag i
4f9c0 73 20 73 65 74 20 74 6f 20 31 2c 20 74 68 69 73 s set to 1, this
4f9d0 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
4f9e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
4f9f0 20 20 20 53 51 4c 69 74 65 20 69 73 20 68 6f 6c SQLite is hol
4fa00 64 69 6e 67 20 70 69 6e 6e 65 64 20 70 61 67 65 ding pinned page
4fa10 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 6e s that can be un
4fa20 70 69 6e 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 pinned.**
4fa30 20 20 20 20 20 20 20 20 20 62 79 20 77 72 69 74 by writ
4fa40 69 6e 67 20 74 68 65 69 72 20 63 6f 6e 74 65 6e ing their conten
4fa50 74 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ts to the databa
4fa60 73 65 20 66 69 6c 65 20 28 61 0a 2a 2a 20 20 20 se file (a.**
4fa70 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c rel
4fa80 61 74 69 76 65 6c 79 20 65 78 70 65 6e 73 69 76 atively expensiv
4fa90 65 20 6f 70 65 72 61 74 69 6f 6e 29 2e 20 49 6e e operation). In
4faa0 20 74 68 69 73 20 73 69 74 75 61 74 69 6f 6e 20 this situation
4fab0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
4fac0 20 20 20 20 20 20 63 61 63 68 65 20 69 6d 70 6c cache impl
4fad0 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 74 ementation has t
4fae0 77 6f 20 63 68 6f 69 63 65 73 3a 20 69 74 20 63 wo choices: it c
4faf0 61 6e 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2c 0a an return NULL,.
4fb00 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
4fb10 20 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 in which case
4fb20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 74 74 65 SQLite will atte
4fb30 6d 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 mpt to unpin one
4fb40 20 6f 72 20 6d 6f 72 65 20 0a 2a 2a 20 20 20 20 or more .**
4fb50 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 page
4fb60 73 20 62 65 66 6f 72 65 20 72 65 2d 72 65 71 75 s before re-requ
4fb70 65 73 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 esting the same
4fb80 70 61 67 65 2c 20 6f 72 20 69 74 20 63 61 6e 0a page, or it can.
4fb90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
4fba0 20 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 allocate a new
4fbb0 20 70 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e page and return
4fbc0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
4fbd0 2e 20 49 66 20 61 20 6e 65 77 0a 2a 2a 20 20 20 . If a new.**
4fbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
4fbf0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 e is allocated,
4fc00 74 68 65 6e 20 69 74 20 6d 75 73 74 20 62 65 20 then it must be
4fc10 63 6f 6d 70 6c 65 74 65 6c 79 20 7a 65 72 6f 65 completely zeroe
4fc20 64 20 62 65 66 6f 72 65 20 0a 2a 2a 20 20 20 20 d before .**
4fc30 20 20 20 20 20 20 20 20 20 20 20 20 69 74 20 69 it i
4fc40 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 20 s returned..**
4fc50 20 3c 74 72 3e 3c 74 64 3e 32 3c 74 64 3e 49 66 <tr><td>2<td>If
4fc60 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 73 createFlag is s
4fc70 65 74 20 74 6f 20 32 2c 20 74 68 65 6e 20 53 51 et to 2, then SQ
4fc80 4c 69 74 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64 Lite is not hold
4fc90 69 6e 67 20 61 6e 79 0a 2a 2a 20 20 20 20 20 20 ing any.**
4fca0 20 20 20 20 20 20 20 20 20 20 70 69 6e 6e 65 64 pinned
4fcb0 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 pages associate
4fcc0 64 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 d with the speci
4fcd0 66 69 63 20 63 61 63 68 65 20 70 61 73 73 65 64 fic cache passed
4fce0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
4fcf0 20 20 20 61 73 20 74 68 65 20 66 69 72 73 74 20 as the first
4fd00 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 46 65 74 argument to xFet
4fd10 63 68 28 29 20 74 68 61 74 20 63 61 6e 20 62 65 ch() that can be
4fd20 20 75 6e 70 69 6e 6e 65 64 2e 20 54 68 65 0a 2a unpinned. The.*
4fd30 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
4fd40 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
4fd50 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 74 74 ation should att
4fd60 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 empt to allocate
4fd70 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 a new.**
4fd80 20 20 20 20 20 20 20 20 20 63 61 63 68 65 20 65 cache e
4fd90 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20 ntry and return
4fda0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
4fdb0 20 41 67 61 69 6e 2c 20 74 68 65 20 6e 65 77 0a Again, the new.
4fdc0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
4fdd0 20 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 page should be
4fde0 20 7a 65 72 6f 65 64 20 62 65 66 6f 72 65 20 69 zeroed before i
4fdf0 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 t is returned. I
4fe00 66 20 74 68 65 20 78 46 65 74 63 68 28 29 0a 2a f the xFetch().*
4fe10 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
4fe20 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 method returns
4fe30 4e 55 4c 4c 20 77 68 65 6e 20 63 72 65 61 74 65 NULL when create
4fe40 46 6c 61 67 3d 3d 32 2c 20 53 51 4c 69 74 65 20 Flag==2, SQLite
4fe50 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 assumes that.**
4fe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 a
4fe70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
4fe80 6f 6e 20 66 61 69 6c 65 64 20 61 6e 64 20 72 65 on failed and re
4fe90 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4e 4f 4d turns SQLITE_NOM
4fea0 45 4d 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 EM to the.**
4feb0 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65 72 user
4fec0 2e 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a ..** </table>.**
4fed0 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20 .** xUnpin() is
4fee0 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 called by SQLite
4fef0 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 with a pointer
4ff00 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70 to a currently p
4ff10 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73 inned page.** as
4ff20 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 its second argu
4ff30 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69 ment. If the thi
4ff40 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69 rd parameter, di
4ff50 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 scard, is non-ze
4ff60 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 ro,.** then the
4ff70 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65 page should be e
4ff80 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 victed from the
4ff90 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 cache. In this c
4ffa0 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61 ase SQLite .** a
4ffb0 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
4ffc0 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61 next time the pa
4ffd0 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20 ge is retrieved
4ffe0 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75 from the cache u
4fff0 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74 sing.** the xFet
50000 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20 ch() method, it
50010 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20 will be zeroed.
50020 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70 If the discard p
50030 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a arameter is.** z
50040 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61 ero, then the pa
50050 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ge is considered
50060 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e to be unpinned.
50070 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 The cache imple
50080 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 mentation.** may
50090 20 63 68 6f 6f 73 65 20 74 6f 20 72 65 63 6c 61 choose to recla
500a0 69 6d 20 28 66 72 65 65 20 6f 72 20 72 65 63 79 im (free or recy
500b0 63 6c 65 29 20 75 6e 70 69 6e 6e 65 64 20 70 61 cle) unpinned pa
500c0 67 65 73 20 61 74 20 61 6e 79 20 74 69 6d 65 2e ges at any time.
500d0 0a 2a 2a 20 53 51 4c 69 74 65 20 61 73 73 75 6d .** SQLite assum
500e0 65 73 20 74 68 61 74 20 6e 65 78 74 20 74 69 6d es that next tim
500f0 65 20 74 68 65 20 70 61 67 65 20 69 73 20 72 65 e the page is re
50100 74 72 69 65 76 65 64 20 66 72 6f 6d 20 74 68 65 trieved from the
50110 20 63 61 63 68 65 0a 2a 2a 20 69 74 20 77 69 6c cache.** it wil
50120 6c 20 65 69 74 68 65 72 20 62 65 20 7a 65 72 6f l either be zero
50130 65 64 2c 20 6f 72 20 63 6f 6e 74 61 69 6e 20 74 ed, or contain t
50140 68 65 20 73 61 6d 65 20 64 61 74 61 20 74 68 61 he same data tha
50150 74 20 69 74 20 64 69 64 20 77 68 65 6e 20 69 74 t it did when it
50160 0a 2a 2a 20 77 61 73 20 75 6e 70 69 6e 6e 65 64 .** was unpinned
50170 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 ..**.** The cach
50180 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 e is not require
50190 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 61 6e 79 d to perform any
501a0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
501b0 69 6e 67 2e 20 41 20 73 69 6e 67 6c 65 20 0a 2a ing. A single .*
501c0 2a 20 63 61 6c 6c 20 74 6f 20 78 55 6e 70 69 6e * call to xUnpin
501d0 28 29 20 75 6e 70 69 6e 73 20 74 68 65 20 70 61 () unpins the pa
501e0 67 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 ge regardless of
501f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
50200 72 69 6f 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 74 rior calls .** t
50210 6f 20 78 46 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a o xFetch()..**.*
50220 2a 20 54 68 65 20 78 52 65 6b 65 79 28 29 20 6d * The xRekey() m
50230 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f ethod is used to
50240 20 63 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 change the key
50250 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64 value associated
50260 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 with the.** pag
50270 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
50280 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
50290 66 72 6f 6d 20 6f 6c 64 4b 65 79 20 74 6f 20 6e from oldKey to n
502a0 65 77 4b 65 79 2e 20 49 66 20 74 68 65 20 63 61 ewKey. If the ca
502b0 63 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c che.** previousl
502c0 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e y contains an en
502d0 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 try associated w
502e0 69 74 68 20 6e 65 77 4b 65 79 2c 20 69 74 20 73 ith newKey, it s
502f0 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 69 73 63 hould be.** disc
50300 61 72 64 65 64 2e 20 41 6e 79 20 70 72 69 6f 72 arded. Any prior
50310 20 63 61 63 68 65 20 65 6e 74 72 79 20 61 73 73 cache entry ass
50320 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 ociated with new
50330 4b 65 79 20 69 73 20 67 75 61 72 61 6e 74 65 65 Key is guarantee
50340 64 20 6e 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 70 d not.** to be p
50350 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 inned..**.** Whe
50360 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 n SQLite calls t
50370 68 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 6d he xTruncate() m
50380 65 74 68 6f 64 2c 20 74 68 65 20 63 61 63 68 65 ethod, the cache
50390 20 6d 75 73 74 20 64 69 73 63 61 72 64 20 61 6c must discard al
503a0 6c 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 63 61 l.** existing ca
503b0 63 68 65 20 65 6e 74 72 69 65 73 20 77 69 74 68 che entries with
503c0 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 28 6b page numbers (k
503d0 65 79 73 29 20 67 72 65 61 74 65 72 20 74 68 61 eys) greater tha
503e0 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 74 6f n or equal.** to
503f0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
50400 65 20 69 4c 69 6d 69 74 20 70 61 72 61 6d 65 74 e iLimit paramet
50410 65 72 20 70 61 73 73 65 64 20 74 6f 20 78 54 72 er passed to xTr
50420 75 6e 63 61 74 65 28 29 2e 20 49 66 20 61 6e 79 uncate(). If any
50430 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 70 61 67 .** of these pag
50440 65 73 20 61 72 65 20 70 69 6e 6e 65 64 2c 20 74 es are pinned, t
50450 68 65 79 20 61 72 65 20 69 6d 70 6c 69 63 69 74 hey are implicit
50460 6c 79 20 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 ly unpinned, mea
50470 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 74 68 65 ning that.** the
50480 79 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 20 y can be safely
50490 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a discarded..**.**
504a0 20 54 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 The xDestroy()
504b0 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 method is used t
504c0 6f 20 64 65 6c 65 74 65 20 61 20 63 61 63 68 65 o delete a cache
504d0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 78 43 allocated by xC
504e0 72 65 61 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c 20 reate()..** All
504f0 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 resources associ
50500 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 70 ated with the sp
50510 65 63 69 66 69 65 64 20 63 61 63 68 65 20 73 68 ecified cache sh
50520 6f 75 6c 64 20 62 65 20 66 72 65 65 64 2e 20 41 ould be freed. A
50530 66 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 fter.** calling
50540 74 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 6d the xDestroy() m
50550 65 74 68 6f 64 2c 20 53 51 4c 69 74 65 20 63 6f ethod, SQLite co
50560 6e 73 69 64 65 72 73 20 74 68 65 20 5b 73 71 6c nsiders the [sql
50570 69 74 65 33 5f 70 63 61 63 68 65 2a 5d 0a 2a 2a ite3_pcache*].**
50580 20 68 61 6e 64 6c 65 20 69 6e 76 61 6c 69 64 2c handle invalid,
50590 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 75 73 and will not us
505a0 65 20 69 74 20 77 69 74 68 20 61 6e 79 20 6f 74 e it with any ot
505b0 68 65 72 20 73 71 6c 69 74 65 33 5f 70 63 61 63 her sqlite3_pcac
505c0 68 65 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 66 75 he_methods.** fu
505d0 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 nctions..*/.type
505e0 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
505f0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
50600 73 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 s sqlite3_pcache
50610 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 _methods;.struct
50620 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f sqlite3_pcache_
50630 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 methods {. void
50640 20 2a 70 41 72 67 3b 0a 20 20 69 6e 74 20 28 2a *pArg;. int (*
50650 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 0a 20 xInit)(void*);.
50660 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77 void (*xShutdow
50670 6e 29 28 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c n)(void*);. sql
50680 69 74 65 33 5f 70 63 61 63 68 65 20 2a 28 2a 78 ite3_pcache *(*x
50690 43 72 65 61 74 65 29 28 69 6e 74 20 73 7a 50 61 Create)(int szPa
506a0 67 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61 62 ge, int bPurgeab
506b0 6c 65 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 43 le);. void (*xC
506c0 61 63 68 65 73 69 7a 65 29 28 73 71 6c 69 74 65 achesize)(sqlite
506d0 33 5f 70 63 61 63 68 65 2a 2c 20 69 6e 74 20 6e 3_pcache*, int n
506e0 43 61 63 68 65 73 69 7a 65 29 3b 0a 20 20 69 6e Cachesize);. in
506f0 74 20 28 2a 78 50 61 67 65 63 6f 75 6e 74 29 28 t (*xPagecount)(
50700 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 sqlite3_pcache*)
50710 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 46 65 74 ;. void *(*xFet
50720 63 68 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 ch)(sqlite3_pcac
50730 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6b 65 he*, unsigned ke
50740 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 y, int createFla
50750 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e g);. void (*xUn
50760 70 69 6e 29 28 73 71 6c 69 74 65 33 5f 70 63 61 pin)(sqlite3_pca
50770 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 che*, void*, int
50780 20 64 69 73 63 61 72 64 29 3b 0a 20 20 76 6f 69 discard);. voi
50790 64 20 28 2a 78 52 65 6b 65 79 29 28 73 71 6c 69 d (*xRekey)(sqli
507a0 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 te3_pcache*, voi
507b0 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6f 6c 64 d*, unsigned old
507c0 4b 65 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e 65 Key, unsigned ne
507d0 77 4b 65 79 29 3b 0a 20 20 76 6f 69 64 20 28 2a wKey);. void (*
507e0 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 xTruncate)(sqlit
507f0 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 e3_pcache*, unsi
50800 67 6e 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20 20 gned iLimit);.
50810 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 void (*xDestroy)
50820 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
50830 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 );.};../*.** Und
50840 6f 20 74 68 65 20 68 61 63 6b 20 74 68 61 74 20 o the hack that
50850 63 6f 6e 76 65 72 74 73 20 66 6c 6f 61 74 69 6e converts floatin
50860 67 20 70 6f 69 6e 74 20 74 79 70 65 73 20 74 6f g point types to
50870 20 69 6e 74 65 67 65 72 20 66 6f 72 0a 2a 2a 20 integer for.**
50880 62 75 69 6c 64 73 20 6f 6e 20 70 72 6f 63 65 73 builds on proces
50890 73 6f 72 73 20 77 69 74 68 6f 75 74 20 66 6c 6f sors without flo
508a0 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 ating point supp
508b0 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ort..*/.#ifdef S
508c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
508d0 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 6e 64 65 ING_POINT.# unde
508e0 66 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a f double.#endif.
508f0 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 45 6e 64 .#if 0.} /* End
50900 20 6f 66 20 74 68 65 20 27 65 78 74 65 72 6e 20 of the 'extern
50910 22 43 22 27 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65 "C"' block */.#e
50920 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a ndif.#endif../**
50930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
50940 20 6f 66 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a of sqlite3.h **
50950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
50980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
50990 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
509a0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c left off in sql
509b0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
509c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
509d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
509e0 6c 75 64 65 20 68 61 73 68 2e 68 20 69 6e 20 74 lude hash.h in t
509f0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
50a00 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
50a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
50a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
50a30 69 6e 20 66 69 6c 65 20 68 61 73 68 2e 68 20 2a in file hash.h *
50a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
50a70 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
50a80 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 22.**.** The a
50a90 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
50aa0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
50ab0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
50ac0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
50ad0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
50ae0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
50af0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
50b00 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
50b10 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
50b20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
50b30 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
50b40 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
50b50 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
50b60 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
50b70 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
50b80 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
50b90 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
50ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
50be0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 ***.** This is t
50bf0 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 he header file f
50c00 6f 72 20 74 68 65 20 67 65 6e 65 72 69 63 20 68 or the generic h
50c10 61 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d ash-table implem
50c20 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 enation.** used
50c30 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a in SQLite..**.**
50c40 20 24 49 64 3a 20 68 61 73 68 2e 68 2c 76 20 31 $Id: hash.h,v 1
50c50 2e 31 32 20 32 30 30 38 2f 31 30 2f 31 30 20 31 .12 2008/10/10 1
50c60 37 3a 34 31 3a 32 39 20 64 72 68 20 45 78 70 20 7:41:29 drh Exp
50c70 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 $.*/.#ifndef _SQ
50c80 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 23 64 65 LITE_HASH_H_.#de
50c90 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41 53 fine _SQLITE_HAS
50ca0 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 H_H_../* Forward
50cb0 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 declarations of
50cc0 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a structures. */.
50cd0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48 typedef struct H
50ce0 61 73 68 20 48 61 73 68 3b 0a 74 79 70 65 64 65 ash Hash;.typede
50cf0 66 20 73 74 72 75 63 74 20 48 61 73 68 45 6c 65 f struct HashEle
50d00 6d 20 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 m HashElem;../*
50d10 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 A complete hash
50d20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 table is an inst
50d30 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
50d40 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
50d50 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c .** The internal
50d60 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 s of this struct
50d70 75 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64 ure are intended
50d80 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d to be opaque --
50d90 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 client.** code
50da0 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d should not attem
50db0 70 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20 pt to access or
50dc0 6d 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c 64 modify the field
50dd0 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 s of this struct
50de0 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e ure.** directly.
50df0 20 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 74 Change this st
50e00 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 ructure only by
50e10 75 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e using the routin
50e20 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 es below..** How
50e30 65 76 65 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68 ever, many of th
50e40 65 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 61 e "procedures" a
50e50 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 nd "functions" f
50e60 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 or modifying and
50e70 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 .** accessing th
50e80 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 is structure are
50e90 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 really macros,
50ea0 73 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c so we can't real
50eb0 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 ly make.** this
50ec0 73 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 65 structure opaque
50ed0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68 ..*/.struct Hash
50ee0 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e {. unsigned in
50ef0 74 20 63 6f 70 79 4b 65 79 3a 20 31 3b 20 20 2f t copyKey: 1; /
50f00 2a 20 54 72 75 65 20 69 66 20 63 6f 70 79 20 6f * True if copy o
50f10 66 20 6b 65 79 20 6d 61 64 65 20 6f 6e 20 69 6e f key made on in
50f20 73 65 72 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e sert */. unsign
50f30 65 64 20 69 6e 74 20 68 74 73 69 7a 65 20 3a 20 ed int htsize :
50f40 33 31 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 31; /* Number of
50f50 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68 65 20 buckets in the
50f60 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 hash table */.
50f70 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 6f 75 unsigned int cou
50f80 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d nt; /* Num
50f90 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
50fa0 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a n this table */.
50fb0 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 HashElem *firs
50fc0 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 t; /* T
50fd0 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 he first element
50fe0 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 2a 2f of the array */
50ff0 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 7b 20 . struct _ht {
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
51010 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a the hash table *
51020 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b /. int count;
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51040 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
51050 74 72 69 65 73 20 77 69 74 68 20 74 68 69 73 20 tries with this
51060 68 61 73 68 20 2a 2f 0a 20 20 20 20 48 61 73 68 hash */. Hash
51070 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 Elem *chain;
51080 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
51090 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79 r to first entry
510a0 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20 with this hash
510b0 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a */. } *ht;.};..
510c0 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 20 /* Each element
510d0 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c in the hash tabl
510e0 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 e is an instance
510f0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
51100 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e g .** structure.
51110 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61 All elements a
51120 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 re stored on a s
51130 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e ingle doubly-lin
51140 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 ked list..**.**
51150 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74 72 75 Again, this stru
51160 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e 64 65 cture is intende
51170 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 2c 20 d to be opaque,
51180 62 75 74 20 69 74 20 63 61 6e 27 74 20 72 65 61 but it can't rea
51190 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65 lly.** be opaque
511a0 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 75 because it is u
511b0 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a sed by macros..*
511c0 2f 0a 73 74 72 75 63 74 20 48 61 73 68 45 6c 65 /.struct HashEle
511d0 6d 20 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a m {. HashElem *
511e0 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 20 20 2f next, *prev; /
511f0 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69 * Next and previ
51200 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 ous elements in
51210 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 the table */. v
51220 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20 oid *data;
51230 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 /* Data
51240 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
51250 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a this element */.
51260 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e void *pKey; in
51270 74 20 6e 4b 65 79 3b 20 20 20 20 2f 2a 20 4b 65 t nKey; /* Ke
51280 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 y associated wit
51290 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a h this element *
512a0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 /.};../*.** Acce
512b0 73 73 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f ss routines. To
512c0 20 64 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 delete, insert
512d0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
512e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
512f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 E void sqlite3Ha
51300 73 68 49 6e 69 74 28 48 61 73 68 2a 2c 20 69 6e shInit(Hash*, in
51310 74 20 63 6f 70 79 4b 65 79 29 3b 0a 53 51 4c 49 t copyKey);.SQLI
51320 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
51330 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 *sqlite3HashInse
51340 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 rt(Hash*, const
51350 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 void *pKey, int
51360 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 74 nKey, void *pDat
51370 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 a);.SQLITE_PRIVA
51380 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
51390 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 HashFind(const H
513a0 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ash*, const void
513b0 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 *pKey, int nKey
513c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
513d0 45 20 48 61 73 68 45 6c 65 6d 20 2a 73 71 6c 69 E HashElem *sqli
513e0 74 65 33 48 61 73 68 46 69 6e 64 45 6c 65 6d 28 te3HashFindElem(
513f0 63 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e const Hash*, con
51400 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 st void *pKey, i
51410 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 nt nKey);.SQLITE
51420 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
51430 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48 lite3HashClear(H
51440 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 ash*);../*.** Ma
51450 63 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 cros for looping
51460 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e over all elemen
51470 74 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 ts of a hash tab
51480 6c 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 le. The idiom i
51490 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a s.** like this:.
514a0 2a 2a 0a 2a 2a 20 20 20 48 61 73 68 20 68 3b 0a **.** Hash h;.
514b0 2a 2a 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 ** HashElem *p
514c0 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 ;.** ....**
514d0 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 for(p=sqliteHash
514e0 46 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d First(&h); p; p=
514f0 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 sqliteHashNext(p
51500 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 )){.** SomeS
51510 74 72 75 63 74 75 72 65 20 2a 70 44 61 74 61 20 tructure *pData
51520 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 = sqliteHashData
51530 28 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 (p);.** // d
51540 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 o something with
51550 20 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f pData.** }.*/
51560 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 .#define sqliteH
51570 61 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48 ashFirst(H) ((H
51580 29 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e )->first).#defin
51590 65 20 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 e sqliteHashNext
515a0 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 (E) ((E)->next
515b0 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 ).#define sqlite
515c0 48 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28 HashData(E) ((
515d0 45 29 2d 3e 64 61 74 61 29 0a 23 64 65 66 69 6e E)->data).#defin
515e0 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 28 e sqliteHashKey(
515f0 45 29 20 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 E) ((E)->pKey
51600 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 ).#define sqlite
51610 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28 HashKeysize(E) (
51620 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a (E)->nKey)../*.*
51630 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
51640 69 65 73 20 69 6e 20 61 20 68 61 73 68 20 74 61 ies in a hash ta
51650 62 6c 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 ble.*/.#define s
51660 71 6c 69 74 65 48 61 73 68 43 6f 75 6e 74 28 48 qliteHashCount(H
51670 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a ) ((H)->count).
51680 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 .#endif /* _SQLI
51690 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f TE_HASH_H_ */../
516a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
516b0 6e 64 20 6f 66 20 68 61 73 68 2e 68 20 2a 2a 2a nd of hash.h ***
516c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
516d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
516e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
516f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
51700 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
51710 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 we left off in s
51720 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
51730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
51740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
51750 6e 63 6c 75 64 65 20 70 61 72 73 65 2e 68 20 69 nclude parse.h i
51760 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
51770 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
51780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
51790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
517a0 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e egin file parse.
517b0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
517c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
517d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 *************/.#
517e0 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d 49 20 20 define TK_SEMI
517f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51800 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 1.#def
51810 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 4e 20 20 ine TK_EXPLAIN
51820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51830 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 2.#define
51840 20 54 4b 5f 51 55 45 52 59 20 20 20 20 20 20 20 TK_QUERY
51850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51860 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 54 4b 3.#define TK
51870 5f 50 4c 41 4e 20 20 20 20 20 20 20 20 20 20 20 _PLAN
51880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51890 20 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 4.#define TK_BE
518a0 47 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 GIN
518b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 5.
518c0 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 41 4e 53 #define TK_TRANS
518d0 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 ACTION
518e0 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 6.#de
518f0 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 45 44 fine TK_DEFERRED
51900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51910 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 7.#defin
51920 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20 20 e TK_IMMEDIATE
51930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51940 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 54 8.#define T
51950 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 K_EXCLUSIVE
51960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51970 20 20 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 9.#define TK_C
51980 4f 4d 4d 49 54 20 20 20 20 20 20 20 20 20 20 20 OMMIT
51990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
519a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 20 .#define TK_END
519b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
519c0 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64 11.#d
519d0 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 43 efine TK_ROLLBAC
519e0 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K
519f0 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 12.#defi
51a00 6e 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20 20 ne TK_CREATE
51a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 13.#define
51a30 54 4b 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 TK_TABLE
51a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a50 20 20 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 14.#define TK_
51a60 49 46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IF
51a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51a80 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 5.#define TK_NOT
51a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51aa0 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23 16.#
51ab0 64 65 66 69 6e 65 20 54 4b 5f 45 58 49 53 54 53 define TK_EXISTS
51ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51ad0 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 66 17.#def
51ae0 69 6e 65 20 54 4b 5f 54 45 4d 50 20 20 20 20 20 ine TK_TEMP
51af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51b00 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e 65 18.#define
51b10 20 54 4b 5f 4c 50 20 20 20 20 20 20 20 20 20 20 TK_LP
51b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51b30 20 20 20 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 19.#define TK
51b40 5f 52 50 20 20 20 20 20 20 20 20 20 20 20 20 20 _RP
51b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51b60 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 20.#define TK_AS
51b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51b80 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31 0a 21.
51b90 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41 #define TK_COMMA
51ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51bb0 20 20 20 20 20 20 20 20 20 20 32 32 0a 23 64 65 22.#de
51bc0 66 69 6e 65 20 54 4b 5f 49 44 20 20 20 20 20 20 fine TK_ID
51bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51be0 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e 23.#defin
51bf0 65 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20 20 e TK_ABORT
51c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51c10 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 54 24.#define T
51c20 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20 20 20 K_AFTER
51c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51c40 20 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 25.#define TK_A
51c50 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20 NALYZE
51c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 36 26
51c70 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 43 20 .#define TK_ASC
51c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51c90 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 27.#d
51ca0 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43 48 20 efine TK_ATTACH
51cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51cc0 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69 28.#defi
51cd0 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20 20 20 ne TK_BEFORE
51ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51cf0 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20 29.#define
51d00 54 4b 5f 43 41 53 43 41 44 45 20 20 20 20 20 20 TK_CASCADE
51d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51d20 20 20 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 30.#define TK_
51d30 43 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20 CAST
51d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
51d50 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 1.#define TK_CON
51d60 46 4c 49 43 54 20 20 20 20 20 20 20 20 20 20 20 FLICT
51d70 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23 32.#
51d80 64 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 42 41 define TK_DATABA
51d90 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SE
51da0 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65 66 33.#def
51db0 69 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 20 20 ine TK_DESC
51dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51dd0 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 34.#define
51de0 20 54 4b 5f 44 45 54 41 43 48 20 20 20 20 20 20 TK_DETACH
51df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51e00 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 35.#define TK
51e10 5f 45 41 43 48 20 20 20 20 20 20 20 20 20 20 20 _EACH
51e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51e30 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 41 36.#define TK_FA
51e40 49 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IL
51e50 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 0a 37.
51e60 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 20 20 #define TK_FOR
51e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51e80 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 65 38.#de
51e90 66 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 20 20 fine TK_IGNORE
51ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51eb0 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69 6e 39.#defin
51ec0 65 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20 20 e TK_INITIALLY
51ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51ee0 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 54 40.#define T
51ef0 4b 5f 49 4e 53 54 45 41 44 20 20 20 20 20 20 20 K_INSTEAD
51f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51f10 20 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 41.#define TK_L
51f20 49 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20 IKE_KW
51f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 32 42
51f40 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 54 43 .#define TK_MATC
51f50 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 H
51f60 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23 64 43.#d
51f70 65 66 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 20 efine TK_KEY
51f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51f90 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 69 44.#defi
51fa0 6e 65 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 20 ne TK_OF
51fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51fc0 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 20 45.#define
51fd0 54 4b 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20 TK_OFFSET
51fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51ff0 20 20 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46.#define TK_
52000 50 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 PRAGMA
52010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
52020 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49 7.#define TK_RAI
52030 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SE
52040 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 23 48.#
52050 64 65 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 43 define TK_REPLAC
52060 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
52070 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 66 49.#def
52080 69 6e 65 20 54 4b 5f 52 45 53 54 52 49 43 54 20 ine TK_RESTRICT
52090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
520a0 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e 65 50.#define
520b0 20 54 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 20 TK_ROW
520c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
520d0 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 51.#define TK
520e0 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 _TRIGGER
520f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52100 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 52.#define TK_VA
52110 43 55 55 4d 20 20 20 20 20 20 20 20 20 20 20 20 CUUM
52120 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33 0a 53.
52130 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 20 #define TK_VIEW
52140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52150 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64 65 54.#de
52160 66 69 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c 20 fine TK_VIRTUAL
52170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52180 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 55.#defin
52190 65 20 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 20 e TK_REINDEX
521a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
521b0 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20 54 56.#define T
521c0 4b 5f 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20 K_RENAME
521d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
521e0 20 35 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 57.#define TK_C
521f0 54 49 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 20 TIME_KW
52200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 38 58
52210 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 20 .#define TK_ANY
52220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52230 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64 59.#d
52240 65 66 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 20 efine TK_OR
52250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52260 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69 60.#defi
52270 6e 65 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 20 ne TK_AND
52280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52290 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 20 61.#define
522a0 54 4b 5f 49 53 20 20 20 20 20 20 20 20 20 20 20 TK_IS
522b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
522c0 20 20 36 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 62.#define TK_
522d0 42 45 54 57 45 45 4e 20 20 20 20 20 20 20 20 20 BETWEEN
522e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
522f0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20 3.#define TK_IN
52300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52310 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23 64.#
52320 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c define TK_ISNULL
52330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52340 20 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 66 65.#def
52350 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 ine TK_NOTNULL
52360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52370 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e 65 66.#define
52380 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 TK_NE
52390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
523a0 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20 54 4b 67.#define TK
523b0 5f 45 51 20 20 20 20 20 20 20 20 20 20 20 20 20 _EQ
523c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
523d0 36 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 54 68.#define TK_GT
523e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
523f0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 39 0a 69.
52400 23 64 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 20 #define TK_LE
52410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52420 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 64 65 70.#de
52430 66 69 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 20 fine TK_LT
52440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52450 20 20 20 20 20 20 20 37 31 0a 23 64 65 66 69 6e 71.#defin
52460 65 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 e TK_GE
52470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52480 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20 54 72.#define T
52490 4b 5f 45 53 43 41 50 45 20 20 20 20 20 20 20 20 K_ESCAPE
524a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
524b0 20 37 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 73.#define TK_B
524c0 49 54 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 ITAND
524d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 34 74
524e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4f .#define TK_BITO
524f0 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R
52500 20 20 20 20 20 20 20 20 20 20 20 37 35 0a 23 64 75.#d
52510 65 66 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 20 efine TK_LSHIFT
52520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52530 20 20 20 20 20 20 20 20 37 36 0a 23 64 65 66 69 76.#defi
52540 6e 65 20 54 4b 5f 52 53 48 49 46 54 20 20 20 20 ne TK_RSHIFT
52550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52560 20 20 20 20 20 37 37 0a 23 64 65 66 69 6e 65 20 77.#define
52570 54 4b 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20 TK_PLUS
52580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52590 20 20 37 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 78.#define TK_
525a0 4d 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 MINUS
525b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
525c0 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41 9.#define TK_STA
525d0 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R
525e0 20 20 20 20 20 20 20 20 20 20 20 20 38 30 0a 23 80.#
525f0 64 65 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 20 define TK_SLASH
52600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52610 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 66 81.#def
52620 69 6e 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 20 ine TK_REM
52630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52640 20 20 20 20 20 20 38 32 0a 23 64 65 66 69 6e 65 82.#define
52650 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 20 TK_CONCAT
52660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52670 20 20 20 38 33 0a 23 64 65 66 69 6e 65 20 54 4b 83.#define TK
52680 5f 43 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 20 _COLLATE
52690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
526a0 38 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4d 84.#define TK_UM
526b0 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20 INUS
526c0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35 0a 85.
526d0 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55 53 #define TK_UPLUS
526e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
526f0 20 20 20 20 20 20 20 20 20 20 38 36 0a 23 64 65 86.#de
52700 66 69 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20 20 fine TK_BITNOT
52710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52720 20 20 20 20 20 20 20 38 37 0a 23 64 65 66 69 6e 87.#defin
52730 65 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 20 20 e TK_STRING
52740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52750 20 20 20 20 38 38 0a 23 64 65 66 69 6e 65 20 54 88.#define T
52760 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20 20 K_JOIN_KW
52770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52780 20 38 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 89.#define TK_C
52790 4f 4e 53 54 52 41 49 4e 54 20 20 20 20 20 20 20 ONSTRAINT
527a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30 90
527b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 41 .#define TK_DEFA
527c0 55 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 ULT
527d0 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23 64 91.#d
527e0 65 66 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 20 efine TK_NULL
527f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52800 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66 69 92.#defi
52810 6e 65 20 54 4b 5f 50 52 49 4d 41 52 59 20 20 20 ne TK_PRIMARY
52820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52830 20 20 20 20 20 39 33 0a 23 64 65 66 69 6e 65 20 93.#define
52840 54 4b 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 TK_UNIQUE
52850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52860 20 20 39 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 94.#define TK_
52870 43 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20 CHECK
52880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
52890 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 46 5.#define TK_REF
528a0 45 52 45 4e 43 45 53 20 20 20 20 20 20 20 20 20 ERENCES
528b0 20 20 20 20 20 20 20 20 20 20 20 20 39 36 0a 23 96.#
528c0 64 65 66 69 6e 65 20 54 4b 5f 41 55 54 4f 49 4e define TK_AUTOIN
528d0 43 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CR
528e0 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66 97.#def
528f0 69 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 ine TK_ON
52900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52910 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 6e 65 98.#define
52920 20 54 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20 TK_DELETE
52930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52940 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 54 4b 99.#define TK
52950 5f 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 _UPDATE
52960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52970 31 30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 100.#define TK_I
52980 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20 NSERT
52990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
529a0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 54 1.#define TK_SET
529b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
529c0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 32 0a 102.
529d0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 #define TK_DEFER
529e0 52 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 RABLE
529f0 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 103.#d
52a00 65 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e efine TK_FOREIGN
52a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52a20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 66 104.#def
52a30 69 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20 20 ine TK_DROP
52a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52a50 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e 105.#defin
52a60 65 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20 e TK_UNION
52a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52a80 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 65 20 106.#define
52a90 54 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20 20 TK_ALL
52aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52ab0 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 54 4b 107.#define TK
52ac0 5f 45 58 43 45 50 54 20 20 20 20 20 20 20 20 20 _EXCEPT
52ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52ae0 31 30 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 108.#define TK_I
52af0 4e 54 45 52 53 45 43 54 20 20 20 20 20 20 20 20 NTERSECT
52b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
52b10 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c 9.#define TK_SEL
52b20 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 ECT
52b30 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 0a 110.
52b40 23 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54 49 #define TK_DISTI
52b50 4e 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 NCT
52b60 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23 64 111.#d
52b70 65 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20 efine TK_DOT
52b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52b90 20 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66 112.#def
52ba0 69 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20 ine TK_FROM
52bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52bc0 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e 113.#defin
52bd0 65 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20 e TK_JOIN
52be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52bf0 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 20 114.#define
52c00 54 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20 TK_INDEXED
52c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52c20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 115.#define TK
52c30 5f 42 59 20 20 20 20 20 20 20 20 20 20 20 20 20 _BY
52c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52c50 31 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 116.#define TK_U
52c60 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 SING
52c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
52c80 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 44 7.#define TK_ORD
52c90 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ER
52ca0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 0a 118.
52cb0 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55 50 #define TK_GROUP
52cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52cd0 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23 64 119.#d
52ce0 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47 20 efine TK_HAVING
52cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52d00 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 65 66 120.#def
52d10 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20 20 ine TK_LIMIT
52d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52d30 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e 121.#defin
52d40 65 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20 20 e TK_WHERE
52d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52d60 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 122.#define
52d70 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20 20 TK_INTO
52d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52d90 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 123.#define TK
52da0 5f 56 41 4c 55 45 53 20 20 20 20 20 20 20 20 20 _VALUES
52db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52dc0 31 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 124.#define TK_I
52dd0 4e 54 45 47 45 52 20 20 20 20 20 20 20 20 20 20 NTEGER
52de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
52df0 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 4f 5.#define TK_FLO
52e00 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AT
52e10 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36 0a 126.
52e20 23 64 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 20 #define TK_BLOB
52e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52e40 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 64 127.#d
52e50 65 66 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 45 efine TK_REGISTE
52e60 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R
52e70 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 128.#def
52e80 69 6e 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 20 ine TK_VARIABLE
52e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52ea0 20 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 129.#defin
52eb0 65 20 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20 e TK_CASE
52ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52ed0 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 65 20 130.#define
52ee0 54 4b 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20 TK_WHEN
52ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52f00 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 131.#define TK
52f10 5f 54 48 45 4e 20 20 20 20 20 20 20 20 20 20 20 _THEN
52f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52f30 31 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 132.#define TK_E
52f40 4c 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 LSE
52f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
52f60 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 3.#define TK_IND
52f70 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EX
52f80 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 0a 134.
52f90 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 52 #define TK_ALTER
52fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52fb0 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64 135.#d
52fc0 65 66 69 6e 65 20 54 4b 5f 54 4f 20 20 20 20 20 efine TK_TO
52fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
52fe0 20 20 20 20 20 20 20 20 31 33 36 0a 23 64 65 66 136.#def
52ff0 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 20 20 20 ine TK_ADD
53000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53010 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 137.#defin
53020 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20 e TK_COLUMNKW
53030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53040 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65 20 138.#define
53050 54 4b 5f 54 4f 5f 54 45 58 54 20 20 20 20 20 20 TK_TO_TEXT
53060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53070 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 139.#define TK
53080 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 _TO_BLOB
53090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
530a0 31 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 140.#define TK_T
530b0 4f 5f 4e 55 4d 45 52 49 43 20 20 20 20 20 20 20 O_NUMERIC
530c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
530d0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 1.#define TK_TO_
530e0 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 INT
530f0 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32 0a 142.
53100 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 #define TK_TO_RE
53110 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AL
53120 20 20 20 20 20 20 20 20 20 20 31 34 33 0a 23 64 143.#d
53130 65 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f efine TK_END_OF_
53140 46 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 FILE
53150 20 20 20 20 20 20 20 20 31 34 34 0a 23 64 65 66 144.#def
53160 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20 ine TK_ILLEGAL
53170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53180 20 20 20 20 20 20 31 34 35 0a 23 64 65 66 69 6e 145.#defin
53190 65 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 20 e TK_SPACE
531a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
531b0 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e 65 20 146.#define
531c0 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49 TK_UNCLOSED_STRI
531d0 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NG
531e0 20 20 31 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 147.#define TK
531f0 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 _FUNCTION
53200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53210 31 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 148.#define TK_C
53220 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 OLUMN
53230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
53240 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 9.#define TK_AGG
53250 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 _FUNCTION
53260 20 20 20 20 20 20 20 20 20 20 20 20 31 35 30 0a 150.
53270 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f 43 #define TK_AGG_C
53280 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 OLUMN
53290 20 20 20 20 20 20 20 20 20 20 31 35 31 0a 23 64 151.#d
532a0 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 efine TK_CONST_F
532b0 55 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20 UNC
532c0 20 20 20 20 20 20 20 20 31 35 32 0a 0a 2f 2a 2a 152../**
532d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
532e0 20 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a of parse.h ****
532f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
53320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
53330 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
53340 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c left off in sql
53350 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
53360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e ***********/.#in
53370 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a clude <stdio.h>.
53380 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 #include <stdlib
53390 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 .h>.#include <st
533a0 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 ring.h>.#include
533b0 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 <assert.h>.#inc
533c0 6c 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e 0a lude <stddef.h>.
533d0 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c ./*.** If compil
533e0 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 73 ing for a proces
533f0 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 66 sor that lacks f
53400 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 loating point su
53410 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 pport,.** substi
53420 74 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f 72 tute integer for
53430 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0a floating-point.
53440 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
53450 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
53460 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 6f OINT.# define do
53470 75 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 uble sqlite_int6
53480 34 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4.# define LONGD
53490 4f 55 42 4c 45 5f 54 59 50 45 20 73 71 6c 69 74 OUBLE_TYPE sqlit
534a0 65 5f 69 6e 74 36 34 0a 23 20 69 66 6e 64 65 66 e_int64.# ifndef
534b0 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a SQLITE_BIG_DBL.
534c0 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 # define SQLIT
534d0 45 5f 42 49 47 5f 44 42 4c 20 28 30 78 37 66 66 E_BIG_DBL (0x7ff
534e0 66 66 66 66 66 66 66 66 66 66 66 66 66 29 0a 23 fffffffffffff).#
534f0 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 endif.# define
53500 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 SQLITE_OMIT_DATE
53510 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20 64 TIME_FUNCS 1.# d
53520 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 efine SQLITE_OMI
53530 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64 65 T_TRACE 1.# unde
53540 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 f SQLITE_MIXED_E
53550 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 NDIAN_64BIT_FLOA
53560 54 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 T.#endif.#ifndef
53570 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a SQLITE_BIG_DBL.
53580 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
53590 42 49 47 5f 44 42 4c 20 28 31 65 39 39 29 0a 23 BIG_DBL (1e99).#
535a0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 endif../*.** OMI
535b0 54 5f 54 45 4d 50 44 42 20 69 73 20 73 65 74 20 T_TEMPDB is set
535c0 74 6f 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f to 1 if SQLITE_O
535d0 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 20 64 65 MIT_TEMPDB is de
535e0 66 69 6e 65 64 2c 20 6f 72 20 30 0a 2a 2a 20 61 fined, or 0.** a
535f0 66 74 65 72 77 61 72 64 2e 20 48 61 76 69 6e 67 fterward. Having
53600 20 74 68 69 73 20 6d 61 63 72 6f 20 61 6c 6c 6f this macro allo
53610 77 73 20 75 73 20 74 6f 20 63 61 75 73 65 20 74 ws us to cause t
53620 68 65 20 43 20 63 6f 6d 70 69 6c 65 72 20 0a 2a he C compiler .*
53630 2a 20 74 6f 20 6f 6d 69 74 20 63 6f 64 65 20 75 * to omit code u
53640 73 65 64 20 62 79 20 54 45 4d 50 20 74 61 62 6c sed by TEMP tabl
53650 65 73 20 77 69 74 68 6f 75 74 20 6d 65 73 73 79 es without messy
53660 20 23 69 66 6e 64 65 66 20 73 74 61 74 65 6d 65 #ifndef stateme
53670 6e 74 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 nts..*/.#ifdef S
53680 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 QLITE_OMIT_TEMPD
53690 42 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 B.#define OMIT_T
536a0 45 4d 50 44 42 20 31 0a 23 65 6c 73 65 0a 23 64 EMPDB 1.#else.#d
536b0 65 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 efine OMIT_TEMPD
536c0 42 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a B 0.#endif../*.*
536d0 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * If the followi
536e0 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20 ng macro is set
536f0 74 6f 20 31 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 to 1, then NULL
53700 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 values are consi
53710 64 65 72 65 64 0a 2a 2a 20 64 69 73 74 69 6e 63 dered.** distinc
53720 74 20 77 68 65 6e 20 64 65 74 65 72 6d 69 6e 69 t when determini
53730 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ng whether or no
53740 74 20 74 77 6f 20 65 6e 74 72 69 65 73 20 61 72 t two entries ar
53750 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 69 6e e the same.** in
53760 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e a UNIQUE index.
53770 20 20 54 68 69 73 20 69 73 20 74 68 65 20 77 61 This is the wa
53780 79 20 50 6f 73 74 67 72 65 53 51 4c 2c 20 4f 72 y PostgreSQL, Or
53790 61 63 6c 65 2c 20 44 42 32 2c 20 4d 79 53 51 4c acle, DB2, MySQL
537a0 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 2c 20 61 6e 64 ,.** OCELOT, and
537b0 20 46 69 72 65 62 69 72 64 20 61 6c 6c 20 77 6f Firebird all wo
537c0 72 6b 2e 20 20 54 68 65 20 53 51 4c 39 32 20 73 rk. The SQL92 s
537d0 70 65 63 20 65 78 70 6c 69 63 69 74 6c 79 20 73 pec explicitly s
537e0 61 79 73 20 74 68 69 73 0a 2a 2a 20 69 73 20 74 ays this.** is t
537f0 68 65 20 77 61 79 20 74 68 69 6e 67 73 20 61 72 he way things ar
53800 65 20 73 75 70 70 6f 73 65 20 74 6f 20 77 6f 72 e suppose to wor
53810 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 k..**.** If the
53820 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 following macro
53830 69 73 20 73 65 74 20 74 6f 20 30 2c 20 74 68 65 is set to 0, the
53840 20 4e 55 4c 4c 73 20 61 72 65 20 69 6e 64 69 73 NULLs are indis
53850 74 69 6e 63 74 20 66 6f 72 0a 2a 2a 20 61 20 55 tinct for.** a U
53860 4e 49 51 55 45 20 69 6e 64 65 78 2e 20 20 49 6e NIQUE index. In
53870 20 74 68 69 73 20 6d 6f 64 65 2c 20 79 6f 75 20 this mode, you
53880 63 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 can only have a
53890 73 69 6e 67 6c 65 20 4e 55 4c 4c 20 65 6e 74 72 single NULL entr
538a0 79 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6c 75 6d y.** for a colum
538b0 6e 20 64 65 63 6c 61 72 65 64 20 55 4e 49 51 55 n declared UNIQU
538c0 45 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 E. This is the
538d0 77 61 79 20 49 6e 66 6f 72 6d 69 78 20 61 6e 64 way Informix and
538e0 20 53 51 4c 20 53 65 72 76 65 72 0a 2a 2a 20 77 SQL Server.** w
538f0 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ork..*/.#define
53900 4e 55 4c 4c 5f 44 49 53 54 49 4e 43 54 5f 46 4f NULL_DISTINCT_FO
53910 52 5f 55 4e 49 51 55 45 20 31 0a 0a 2f 2a 0a 2a R_UNIQUE 1../*.*
53920 2a 20 54 68 65 20 22 66 69 6c 65 20 66 6f 72 6d * The "file form
53930 61 74 22 20 6e 75 6d 62 65 72 20 69 73 20 61 6e at" number is an
53940 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 integer that is
53950 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 incremented whe
53960 6e 65 76 65 72 0a 2a 2a 20 74 68 65 20 56 44 42 never.** the VDB
53970 45 2d 6c 65 76 65 6c 20 66 69 6c 65 20 66 6f 72 E-level file for
53980 6d 61 74 20 63 68 61 6e 67 65 73 2e 20 20 54 68 mat changes. Th
53990 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
539a0 6f 73 20 64 65 66 69 6e 65 20 74 68 65 0a 2a 2a os define the.**
539b0 20 74 68 65 20 64 65 66 61 75 6c 74 20 66 69 6c the default fil
539c0 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 6e 65 77 e format for new
539d0 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20 74 databases and t
539e0 68 65 20 6d 61 78 69 6d 75 6d 20 66 69 6c 65 20 he maximum file
539f0 66 6f 72 6d 61 74 0a 2a 2a 20 74 68 61 74 20 74 format.** that t
53a00 68 65 20 6c 69 62 72 61 72 79 20 63 61 6e 20 72 he library can r
53a10 65 61 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ead..*/.#define
53a20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f SQLITE_MAX_FILE_
53a30 46 4f 52 4d 41 54 20 34 0a 23 69 66 6e 64 65 66 FORMAT 4.#ifndef
53a40 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
53a50 46 49 4c 45 5f 46 4f 52 4d 41 54 0a 23 20 64 65 FILE_FORMAT.# de
53a60 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 fine SQLITE_DEFA
53a70 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 ULT_FILE_FORMAT
53a80 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 1.#endif../*.**
53a90 50 72 6f 76 69 64 65 20 61 20 64 65 66 61 75 6c Provide a defaul
53aa0 74 20 76 61 6c 75 65 20 66 6f 72 20 53 51 4c 49 t value for SQLI
53ab0 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 69 6e TE_TEMP_STORE in
53ac0 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20 case it is not
53ad0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 6f 6e 20 specified.** on
53ae0 74 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 the command-line
53af0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
53b00 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 23 20 TE_TEMP_STORE.#
53b10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
53b20 4d 50 5f 53 54 4f 52 45 20 31 0a 23 65 6e 64 69 MP_STORE 1.#endi
53b30 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43 20 64 6f 65 f../*.** GCC doe
53b40 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 74 68 65 s not define the
53b50 20 6f 66 66 73 65 74 6f 66 28 29 20 6d 61 63 72 offsetof() macr
53b60 6f 20 73 6f 20 77 65 27 6c 6c 20 68 61 76 65 20 o so we'll have
53b70 74 6f 20 64 6f 20 69 74 0a 2a 2a 20 6f 75 72 73 to do it.** ours
53b80 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 elves..*/.#ifnde
53b90 66 20 6f 66 66 73 65 74 6f 66 0a 23 64 65 66 69 f offsetof.#defi
53ba0 6e 65 20 6f 66 66 73 65 74 6f 66 28 53 54 52 55 ne offsetof(STRU
53bb0 43 54 55 52 45 2c 46 49 45 4c 44 29 20 28 28 69 CTURE,FIELD) ((i
53bc0 6e 74 29 28 28 63 68 61 72 2a 29 26 28 28 53 54 nt)((char*)&((ST
53bd0 52 55 43 54 55 52 45 2a 29 30 29 2d 3e 46 49 45 RUCTURE*)0)->FIE
53be0 4c 44 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a LD)).#endif../*.
53bf0 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 ** Check to see
53c00 69 66 20 74 68 69 73 20 6d 61 63 68 69 6e 65 20 if this machine
53c10 75 73 65 73 20 45 42 43 44 49 43 2e 20 20 28 59 uses EBCDIC. (Y
53c20 65 73 2c 20 62 65 6c 69 65 76 65 20 69 74 20 6f es, believe it o
53c30 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68 65 72 65 20 r.** not, there
53c40 61 72 65 20 73 74 69 6c 6c 20 6d 61 63 68 69 6e are still machin
53c50 65 73 20 6f 75 74 20 74 68 65 72 65 20 74 68 61 es out there tha
53c60 74 20 75 73 65 20 45 42 43 44 49 43 2e 29 0a 2a t use EBCDIC.).*
53c70 2f 0a 23 69 66 20 27 41 27 20 3d 3d 20 27 5c 33 /.#if 'A' == '\3
53c80 30 31 27 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 01'.# define SQL
53c90 49 54 45 5f 45 42 43 44 49 43 20 31 0a 23 65 6c ITE_EBCDIC 1.#el
53ca0 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 se.# define SQLI
53cb0 54 45 5f 41 53 43 49 49 20 31 0a 23 65 6e 64 69 TE_ASCII 1.#endi
53cc0 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 67 65 72 f../*.** Integer
53cd0 73 20 6f 66 20 6b 6e 6f 77 6e 20 73 69 7a 65 73 s of known sizes
53ce0 2e 20 20 54 68 65 73 65 20 74 79 70 65 64 65 66 . These typedef
53cf0 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 66 s might change f
53d00 6f 72 20 61 72 63 68 69 74 65 63 74 75 72 65 73 or architectures
53d10 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 73 69 .** where the si
53d20 7a 65 73 20 76 65 72 79 2e 20 20 50 72 65 70 72 zes very. Prepr
53d30 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 61 ocessor macros a
53d40 72 65 20 61 76 61 69 6c 61 62 6c 65 20 73 6f 20 re available so
53d50 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 79 70 65 that the.** type
53d60 73 20 63 61 6e 20 62 65 20 63 6f 6e 76 65 6e 69 s can be conveni
53d70 65 6e 74 6c 79 20 72 65 64 65 66 69 6e 65 64 20 ently redefined
53d80 61 74 20 63 6f 6d 70 69 6c 65 2d 74 79 70 65 2e at compile-type.
53d90 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a Like this:.**.
53da0 2a 2a 20 20 20 20 20 20 20 20 20 63 63 20 27 2d ** cc '-
53db0 44 55 49 4e 54 50 54 52 5f 54 59 50 45 3d 6c 6f DUINTPTR_TYPE=lo
53dc0 6e 67 20 6c 6f 6e 67 20 69 6e 74 27 20 2e 2e 2e ng long int' ...
53dd0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 .*/.#ifndef UINT
53de0 33 32 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 32_TYPE.# ifdef
53df0 48 41 56 45 5f 55 49 4e 54 33 32 5f 54 0a 23 20 HAVE_UINT32_T.#
53e00 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 define UINT32_T
53e10 59 50 45 20 75 69 6e 74 33 32 5f 74 0a 23 20 65 YPE uint32_t.# e
53e20 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 lse.# define UI
53e30 4e 54 33 32 5f 54 59 50 45 20 75 6e 73 69 67 6e NT32_TYPE unsign
53e40 65 64 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 ed int.# endif.#
53e50 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49 endif.#ifndef UI
53e60 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65 NT16_TYPE.# ifde
53e70 66 20 48 41 56 45 5f 55 49 4e 54 31 36 5f 54 0a f HAVE_UINT16_T.
53e80 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 # define UINT16
53e90 5f 54 59 50 45 20 75 69 6e 74 31 36 5f 74 0a 23 _TYPE uint16_t.#
53ea0 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 else.# define
53eb0 55 49 4e 54 31 36 5f 54 59 50 45 20 75 6e 73 69 UINT16_TYPE unsi
53ec0 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 0a 23 gned short int.#
53ed0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 endif.#endif.#i
53ee0 66 6e 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 fndef INT16_TYPE
53ef0 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e .# ifdef HAVE_IN
53f00 54 31 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 T16_T.# define
53f10 49 4e 54 31 36 5f 54 59 50 45 20 69 6e 74 31 36 INT16_TYPE int16
53f20 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 _t.# else.# def
53f30 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 73 ine INT16_TYPE s
53f40 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 hort int.# endif
53f50 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
53f60 55 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 64 UINT8_TYPE.# ifd
53f70 65 66 20 48 41 56 45 5f 55 49 4e 54 38 5f 54 0a ef HAVE_UINT8_T.
53f80 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f # define UINT8_
53f90 54 59 50 45 20 75 69 6e 74 38 5f 74 0a 23 20 65 TYPE uint8_t.# e
53fa0 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 lse.# define UI
53fb0 4e 54 38 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 NT8_TYPE unsigne
53fc0 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23 d char.# endif.#
53fd0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49 4e endif.#ifndef IN
53fe0 54 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 T8_TYPE.# ifdef
53ff0 48 41 56 45 5f 49 4e 54 38 5f 54 0a 23 20 20 64 HAVE_INT8_T.# d
54000 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20 efine INT8_TYPE
54010 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 int8_t.# else.#
54020 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 50 define INT8_TYP
54030 45 20 73 69 67 6e 65 64 20 63 68 61 72 0a 23 20 E signed char.#
54040 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 endif.#endif.#if
54050 6e 64 65 66 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f ndef LONGDOUBLE_
54060 54 59 50 45 0a 23 20 64 65 66 69 6e 65 20 4c 4f TYPE.# define LO
54070 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 6c 6f NGDOUBLE_TYPE lo
54080 6e 67 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66 ng double.#endif
54090 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f .typedef sqlite_
540a0 69 6e 74 36 34 20 69 36 34 3b 20 20 20 20 20 20 int64 i64;
540b0 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 /* 8-byte si
540c0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
540d0 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 typedef sqlite_u
540e0 69 6e 74 36 34 20 75 36 34 3b 20 20 20 20 20 20 int64 u64;
540f0 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 75 6e 73 /* 8-byte uns
54100 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
54110 0a 74 79 70 65 64 65 66 20 55 49 4e 54 33 32 5f .typedef UINT32_
54120 54 59 50 45 20 75 33 32 3b 20 20 20 20 20 20 20 TYPE u32;
54130 20 20 20 20 2f 2a 20 34 2d 62 79 74 65 20 75 6e /* 4-byte un
54140 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
54150 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 31 36 /.typedef UINT16
54160 5f 54 59 50 45 20 75 31 36 3b 20 20 20 20 20 20 _TYPE u16;
54170 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 65 20 75 /* 2-byte u
54180 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 nsigned integer
54190 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e 54 31 36 */.typedef INT16
541a0 5f 54 59 50 45 20 69 31 36 3b 20 20 20 20 20 20 _TYPE i16;
541b0 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 65 20 /* 2-byte
541c0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
541d0 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 38 5f /.typedef UINT8_
541e0 54 59 50 45 20 75 38 3b 20 20 20 20 20 20 20 20 TYPE u8;
541f0 20 20 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 75 /* 1-byte u
54200 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 nsigned integer
54210 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e 54 38 5f */.typedef INT8_
54220 54 59 50 45 20 69 38 3b 20 20 20 20 20 20 20 20 TYPE i8;
54230 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 /* 1-byte
54240 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
54250 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 /../*.** Macros
54260 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 to determine whe
54270 74 68 65 72 20 74 68 65 20 6d 61 63 68 69 6e 65 ther the machine
54280 20 69 73 20 62 69 67 20 6f 72 20 6c 69 74 74 6c is big or littl
54290 65 20 65 6e 64 69 61 6e 2c 0a 2a 2a 20 65 76 61 e endian,.** eva
542a0 6c 75 61 74 65 64 20 61 74 20 72 75 6e 74 69 6d luated at runtim
542b0 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
542c0 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e ITE_AMALGAMATION
542d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
542e0 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 const int sqlite
542f0 33 6f 6e 65 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 3one;.#else.SQLI
54300 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
54310 20 69 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b int sqlite3one;
54320 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 .#endif.#if defi
54330 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 ned(i386) || def
54340 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c ined(__i386__) |
54350 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 | defined(_M_IX8
54360 36 29 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6)\.
54370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54380 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 || defined(__x8
54390 36 5f 36 34 29 20 7c 7c 20 64 65 66 69 6e 65 64 6_64) || defined
543a0 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64 (__x86_64__).# d
543b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 efine SQLITE_BIG
543c0 45 4e 44 49 41 4e 20 20 20 20 30 0a 23 20 64 65 ENDIAN 0.# de
543d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 fine SQLITE_LITT
543e0 4c 45 45 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 LEENDIAN 1.# def
543f0 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 ine SQLITE_UTF16
54400 4e 41 54 49 56 45 20 20 53 51 4c 49 54 45 5f 55 NATIVE SQLITE_U
54410 54 46 31 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 TF16LE.#else.# d
54420 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 efine SQLITE_BIG
54430 45 4e 44 49 41 4e 20 20 20 20 28 2a 28 63 68 61 ENDIAN (*(cha
54440 72 20 2a 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 r *)(&sqlite3one
54450 29 3d 3d 30 29 0a 23 20 64 65 66 69 6e 65 20 53 )==0).# define S
54460 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 QLITE_LITTLEENDI
54470 41 4e 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73 AN (*(char *)(&s
54480 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 qlite3one)==1).#
54490 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 define SQLITE_U
544a0 54 46 31 36 4e 41 54 49 56 45 20 28 53 51 4c 49 TF16NATIVE (SQLI
544b0 54 45 5f 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c TE_BIGENDIAN?SQL
544c0 49 54 45 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 ITE_UTF16BE:SQLI
544d0 54 45 5f 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 TE_UTF16LE).#end
544e0 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 if../*.** Consta
544f0 6e 74 73 20 66 6f 72 20 74 68 65 20 6c 61 72 67 nts for the larg
54500 65 73 74 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74 est and smallest
54510 20 70 6f 73 73 69 62 6c 65 20 36 34 2d 62 69 74 possible 64-bit
54520 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 signed integers
54530 2e 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f ..** These macro
54540 73 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 74 s are designed t
54550 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 o work correctly
54560 20 6f 6e 20 62 6f 74 68 20 33 32 2d 62 69 74 20 on both 32-bit
54570 61 6e 64 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f and 64-bit.** co
54580 6d 70 69 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 mpilers..*/.#def
54590 69 6e 65 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 ine LARGEST_INT6
545a0 34 20 20 28 30 78 66 66 66 66 66 66 66 66 7c 28 4 (0xffffffff|(
545b0 28 28 69 36 34 29 30 78 37 66 66 66 66 66 66 66 ((i64)0x7fffffff
545c0 29 3c 3c 33 32 29 29 0a 23 64 65 66 69 6e 65 20 )<<32)).#define
545d0 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28 SMALLEST_INT64 (
545e0 28 28 69 36 34 29 2d 31 29 20 2d 20 4c 41 52 47 ((i64)-1) - LARG
545f0 45 53 54 5f 49 4e 54 36 34 29 0a 0a 2f 2a 0a 2a EST_INT64)../*.*
54600 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
54610 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
54620 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 tructure is used
54630 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 75 to store the bu
54640 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 sy-handler.** ca
54650 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 67 69 76 llback for a giv
54660 65 6e 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 en sqlite handle
54670 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c . .**.** The sql
54680 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 20 ite.busyHandler
54690 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 73 71 member of the sq
546a0 6c 69 74 65 20 73 74 72 75 63 74 20 63 6f 6e 74 lite struct cont
546b0 61 69 6e 73 20 74 68 65 20 62 75 73 79 0a 2a 2a ains the busy.**
546c0 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 callback for th
546d0 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
546e0 65 2e 20 45 61 63 68 20 70 61 67 65 72 20 6f 70 e. Each pager op
546f0 65 6e 65 64 20 76 69 61 20 74 68 65 20 73 71 6c ened via the sql
54700 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73 ite.** handle is
54710 20 70 61 73 73 65 64 20 61 20 70 6f 69 6e 74 65 passed a pointe
54720 72 20 74 6f 20 73 71 6c 69 74 65 2e 62 75 73 79 r to sqlite.busy
54730 48 61 6e 64 6c 65 72 2e 20 54 68 65 20 62 75 73 Handler. The bus
54740 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c y-handler.** cal
54750 6c 62 61 63 6b 20 69 73 20 63 75 72 72 65 6e 74 lback is current
54760 6c 79 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 ly invoked only
54770 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 65 from within page
54780 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 r.c..*/.typedef
54790 73 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c struct BusyHandl
547a0 65 72 20 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a er BusyHandler;.
547b0 73 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c struct BusyHandl
547c0 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75 er {. int (*xFu
547d0 6e 63 29 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b nc)(void *,int);
547e0 20 20 2f 2a 20 54 68 65 20 62 75 73 79 20 63 61 /* The busy ca
547f0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 llback */. void
54800 20 2a 70 41 72 67 3b 20 20 20 20 20 20 20 20 20 *pArg;
54810 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
54820 61 72 67 20 74 6f 20 62 75 73 79 20 63 61 6c 6c arg to busy call
54830 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 back */. int nB
54840 75 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 usy;
54850 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e /* Incremen
54860 74 65 64 20 77 69 74 68 20 65 61 63 68 20 62 75 ted with each bu
54870 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f sy call */.};../
54880 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 *.** Name of the
54890 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 master database
548a0 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6d 61 73 table. The mas
548b0 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 ter database tab
548c0 6c 65 0a 2a 2a 20 69 73 20 61 20 73 70 65 63 69 le.** is a speci
548d0 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20 68 6f al table that ho
548e0 6c 64 73 20 74 68 65 20 6e 61 6d 65 73 20 61 6e lds the names an
548f0 64 20 61 74 74 72 69 62 75 74 65 73 20 6f 66 20 d attributes of
54900 61 6c 6c 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c all.** user tabl
54910 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a es and indices..
54920 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 */.#define MASTE
54930 52 5f 4e 41 4d 45 20 20 20 20 20 20 20 22 73 71 R_NAME "sq
54940 6c 69 74 65 5f 6d 61 73 74 65 72 22 0a 23 64 65 lite_master".#de
54950 66 69 6e 65 20 54 45 4d 50 5f 4d 41 53 54 45 52 fine TEMP_MASTER
54960 5f 4e 41 4d 45 20 20 22 73 71 6c 69 74 65 5f 74 _NAME "sqlite_t
54970 65 6d 70 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a emp_master"../*.
54980 2a 2a 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 ** The root-page
54990 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 of the master d
549a0 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a atabase table..*
549b0 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52 /.#define MASTER
549c0 5f 52 4f 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f _ROOT 1../
549d0 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 *.** The name of
549e0 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c the schema tabl
549f0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43 e..*/.#define SC
54a00 48 45 4d 41 5f 54 41 42 4c 45 28 78 29 20 20 28 HEMA_TABLE(x) (
54a10 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 29 26 26 (!OMIT_TEMPDB)&&
54a20 28 78 3d 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54 (x==1)?TEMP_MAST
54a30 45 52 5f 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e ER_NAME:MASTER_N
54a40 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f AME)../*.** A co
54a50 6e 76 65 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20 nvenience macro
54a60 74 68 61 74 20 72 65 74 75 72 6e 73 20 74 68 65 that returns the
54a70 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 number of eleme
54a80 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72 nts in.** an arr
54a90 61 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41 ay..*/.#define A
54aa0 72 72 61 79 53 69 7a 65 28 58 29 20 20 20 20 28 rraySize(X) (
54ab0 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 58 29 2f (int)(sizeof(X)/
54ac0 73 69 7a 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a sizeof(X[0])))..
54ad0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
54ae0 69 6e 67 20 76 61 6c 75 65 20 61 73 20 61 20 64 ing value as a d
54af0 65 73 74 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 estructor means
54b00 74 6f 20 75 73 65 20 73 71 6c 69 74 65 33 44 62 to use sqlite3Db
54b10 46 72 65 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 Free()..** This
54b20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 is an internal e
54b30 78 74 65 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 xtension to SQLI
54b40 54 45 5f 53 54 41 54 49 43 20 61 6e 64 20 53 51 TE_STATIC and SQ
54b50 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a LITE_TRANSIENT..
54b60 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
54b70 45 5f 44 59 4e 41 4d 49 43 20 20 20 28 28 73 71 E_DYNAMIC ((sq
54b80 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 lite3_destructor
54b90 5f 74 79 70 65 29 73 71 6c 69 74 65 33 44 62 46 _type)sqlite3DbF
54ba0 72 65 65 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e ree)../*.** When
54bb0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 SQLITE_OMIT_WSD
54bc0 20 69 73 20 64 65 66 69 6e 65 64 2c 20 69 74 20 is defined, it
54bd0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 means that the t
54be0 61 72 67 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 arget platform d
54bf0 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f oes.** not suppo
54c00 72 74 20 57 72 69 74 61 62 6c 65 20 53 74 61 74 rt Writable Stat
54c10 69 63 20 44 61 74 61 20 28 57 53 44 29 20 73 75 ic Data (WSD) su
54c20 63 68 20 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 ch as global and
54c30 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 static variable
54c40 73 2e 0a 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 s..** All variab
54c50 6c 65 73 20 6d 75 73 74 20 65 69 74 68 65 72 20 les must either
54c60 62 65 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 be on the stack
54c70 6f 72 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 or dynamically a
54c80 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a llocated from.**
54c90 20 74 68 65 20 68 65 61 70 2e 20 20 57 68 65 6e the heap. When
54ca0 20 57 53 44 20 69 73 20 75 6e 73 75 70 70 6f 72 WSD is unsuppor
54cb0 74 65 64 2c 20 74 68 65 20 76 61 72 69 61 62 6c ted, the variabl
54cc0 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 e declarations s
54cd0 63 61 74 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f cattered.** thro
54ce0 75 67 68 6f 75 74 20 74 68 65 20 53 51 4c 69 74 ughout the SQLit
54cf0 65 20 63 6f 64 65 20 6d 75 73 74 20 62 65 63 6f e code must beco
54d00 6d 65 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 me constants ins
54d10 74 65 61 64 2e 20 20 54 68 65 20 53 51 4c 49 54 tead. The SQLIT
54d20 45 5f 57 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 E_WSD.** macro i
54d30 73 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 s used for this
54d40 70 75 72 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e purpose. And in
54d50 73 74 65 61 64 20 6f 66 20 72 65 66 65 72 65 6e stead of referen
54d60 63 69 6e 67 20 74 68 65 20 76 61 72 69 61 62 6c cing the variabl
54d70 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 e.** directly, w
54d80 65 20 75 73 65 20 69 74 73 20 63 6f 6e 73 74 61 e use its consta
54d90 6e 74 20 61 73 20 61 20 6b 65 79 20 74 6f 20 6c nt as a key to l
54da0 6f 6f 6b 75 70 20 74 68 65 20 72 75 6e 2d 74 69 ookup the run-ti
54db0 6d 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 me allocated.**
54dc0 62 75 66 66 65 72 20 74 68 61 74 20 68 6f 6c 64 buffer that hold
54dd0 73 20 72 65 61 6c 20 76 61 72 69 61 62 6c 65 2e s real variable.
54de0 20 20 54 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 The constant i
54df0 73 20 61 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 s also the initi
54e00 61 6c 69 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 alizer.** for th
54e10 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 e run-time alloc
54e20 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a ated buffer..**.
54e30 2a 2a 20 49 6e 20 74 68 65 20 75 73 75 61 6c 20 ** In the usual
54e40 63 61 73 65 20 77 68 65 72 65 20 57 53 44 20 69 case where WSD i
54e50 73 20 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65 s supported, the
54e60 20 53 51 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 SQLITE_WSD and
54e70 47 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 GLOBAL.** macros
54e80 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 become no-ops a
54e90 6e 64 20 68 61 76 65 20 7a 65 72 6f 20 70 65 72 nd have zero per
54ea0 66 6f 72 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e formance impact.
54eb0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
54ec0 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 E_OMIT_WSD. #de
54ed0 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20 fine SQLITE_WSD
54ee0 63 6f 6e 73 74 0a 20 20 23 64 65 66 69 6e 65 20 const. #define
54ef0 47 4c 4f 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 GLOBAL(t,v) (*(t
54f00 2a 29 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 *)sqlite3_wsd_fi
54f10 6e 64 28 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 nd((void*)&(v),
54f20 73 69 7a 65 6f 66 28 76 29 29 29 0a 20 20 23 64 sizeof(v))). #d
54f30 65 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f efine sqlite3Glo
54f40 62 61 6c 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c balConfig GLOBAL
54f50 28 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 (struct Sqlite3C
54f60 6f 6e 66 69 67 2c 20 73 71 6c 69 74 65 33 43 6f onfig, sqlite3Co
54f70 6e 66 69 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 nfig).SQLITE_API
54f80 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 int sqlite3_w
54f90 73 64 5f 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 sd_init(int N, i
54fa0 6e 74 20 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 nt J);.SQLITE_AP
54fb0 49 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 I void *sqlite
54fc0 33 5f 77 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 3_wsd_find(void
54fd0 2a 4b 2c 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 *K, int L);.#els
54fe0 65 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 e. #define SQLI
54ff0 54 45 5f 57 53 44 20 0a 20 20 23 64 65 66 69 6e TE_WSD . #defin
55000 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a e GLOBAL(t,v) v.
55010 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
55020 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 3GlobalConfig sq
55030 6c 69 74 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 lite3Config.#end
55040 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f if../*.** The fo
55050 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 61 llowing macros a
55060 72 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72 re used to suppr
55070 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 ess compiler war
55080 6e 69 6e 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20 nings and to.**
55090 6d 61 6b 65 20 69 74 20 63 6c 65 61 72 20 74 6f make it clear to
550a0 20 68 75 6d 61 6e 20 72 65 61 64 65 72 73 20 77 human readers w
550b0 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 hen a function p
550c0 61 72 61 6d 65 74 65 72 20 69 73 20 64 65 6c 69 arameter is deli
550d0 62 65 72 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66 berately .** lef
550e0 74 20 75 6e 75 73 65 64 20 77 69 74 68 69 6e 20 t unused within
550f0 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 the body of a fu
55100 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 75 73 75 nction. This usu
55110 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 ally happens whe
55120 6e 0a 2a 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20 n.** a function
55130 69 73 20 63 61 6c 6c 65 64 20 76 69 61 20 61 20 is called via a
55140 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
55150 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 . For example th
55160 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 e .** implementa
55170 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61 tion of an SQL a
55180 67 67 72 65 67 61 74 65 20 73 74 65 70 20 63 61 ggregate step ca
55190 6c 6c 62 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75 llback may not u
551a0 73 65 20 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65 se the.** parame
551b0 74 65 72 20 69 6e 64 69 63 61 74 69 6e 67 20 74 ter indicating t
551c0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 he number of arg
551d0 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f uments passed to
551e0 20 74 68 65 20 61 67 67 72 65 67 61 74 65 2c 0a the aggregate,.
551f0 2a 2a 20 69 66 20 69 74 20 6b 6e 6f 77 73 20 74 ** if it knows t
55200 68 61 74 20 74 68 69 73 20 69 73 20 65 6e 66 6f hat this is enfo
55210 72 63 65 64 20 65 6c 73 65 77 68 65 72 65 2e 0a rced elsewhere..
55220 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e **.** When a fun
55230 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 ction parameter
55240 69 73 20 6e 6f 74 20 75 73 65 64 20 61 74 20 61 is not used at a
55250 6c 6c 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f ll within the bo
55260 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e dy of a function
55270 2c 0a 2a 2a 20 69 74 20 69 73 20 67 65 6e 65 72 ,.** it is gener
55280 61 6c 6c 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55 ally named "NotU
55290 73 65 64 22 20 6f 72 20 22 4e 6f 74 55 73 65 64 sed" or "NotUsed
552a0 32 22 20 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67 2" to make thing
552b0 73 20 65 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a s even clearer..
552c0 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 73 ** However, thes
552d0 65 20 6d 61 63 72 6f 73 20 6d 61 79 20 61 6c 73 e macros may als
552e0 6f 20 62 65 20 75 73 65 64 20 74 6f 20 73 75 70 o be used to sup
552f0 70 72 65 73 73 20 77 61 72 6e 69 6e 67 73 20 72 press warnings r
55300 65 6c 61 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72 elated to.** par
55310 61 6d 65 74 65 72 73 20 74 68 61 74 20 6d 61 79 ameters that may
55320 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 or may not be u
55330 73 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e sed depending on
55340 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74 compilation opt
55350 69 6f 6e 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 ions..** For exa
55360 6d 70 6c 65 20 74 68 6f 73 65 20 70 61 72 61 6d mple those param
55370 65 74 65 72 73 20 6f 6e 6c 79 20 75 73 65 64 20 eters only used
55380 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 in assert() stat
55390 65 6d 65 6e 74 73 2e 20 49 6e 20 74 68 65 73 65 ements. In these
553a0 0a 2a 2a 20 63 61 73 65 73 20 74 68 65 20 70 61 .** cases the pa
553b0 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 61 6d rameters are nam
553c0 65 64 20 61 73 20 70 65 72 20 74 68 65 20 75 73 ed as per the us
553d0 75 61 6c 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e ual conventions.
553e0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 .*/.#define UNUS
553f0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29 20 ED_PARAMETER(x)
55400 28 76 6f 69 64 29 28 78 29 0a 23 64 65 66 69 6e (void)(x).#defin
55410 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 e UNUSED_PARAMET
55420 45 52 32 28 78 2c 79 29 20 55 4e 55 53 45 44 5f ER2(x,y) UNUSED_
55430 50 41 52 41 4d 45 54 45 52 28 78 29 2c 55 4e 55 PARAMETER(x),UNU
55440 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 29 SED_PARAMETER(y)
55450 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 ../*.** Forward
55460 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 73 74 references to st
55470 72 75 63 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65 ructures.*/.type
55480 64 65 66 20 73 74 72 75 63 74 20 41 67 67 49 6e def struct AggIn
55490 66 6f 20 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65 fo AggInfo;.type
554a0 64 65 66 20 73 74 72 75 63 74 20 41 75 74 68 43 def struct AuthC
554b0 6f 6e 74 65 78 74 20 41 75 74 68 43 6f 6e 74 65 ontext AuthConte
554c0 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 xt;.typedef stru
554d0 63 74 20 42 69 74 76 65 63 20 42 69 74 76 65 63 ct Bitvec Bitvec
554e0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
554f0 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 71 CollSeq CollSeq
55500 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
55510 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a Column Column;.
55520 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44 typedef struct D
55530 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 74 b Db;.typedef st
55540 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 65 ruct Schema Sche
55550 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ma;.typedef stru
55560 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 79 ct Expr Expr;.ty
55570 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70 pedef struct Exp
55580 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b 0a rList ExprList;.
55590 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 typedef struct F
555a0 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 64 65 Key FKey;.typede
555b0 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 f struct FuncDef
555c0 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 64 65 FuncDef;.typede
555d0 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 f struct FuncDef
555e0 48 61 73 68 20 46 75 6e 63 44 65 66 48 61 73 68 Hash FuncDefHash
555f0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
55600 20 49 64 4c 69 73 74 20 49 64 4c 69 73 74 3b 0a IdList IdList;.
55610 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 typedef struct I
55620 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 79 70 65 ndex Index;.type
55630 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 43 6c def struct KeyCl
55640 61 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a 74 79 ass KeyClass;.ty
55650 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 pedef struct Key
55660 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74 79 Info KeyInfo;.ty
55670 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f pedef struct Loo
55680 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69 64 65 kaside Lookaside
55690 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
556a0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 4c LookasideSlot L
556b0 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a 74 79 ookasideSlot;.ty
556c0 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 6f 64 pedef struct Mod
556d0 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79 70 65 ule Module;.type
556e0 64 65 66 20 73 74 72 75 63 74 20 4e 61 6d 65 43 def struct NameC
556f0 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e 74 65 ontext NameConte
55700 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 xt;.typedef stru
55710 63 74 20 50 61 72 73 65 20 50 61 72 73 65 3b 0a ct Parse Parse;.
55720 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
55730 65 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79 elect Select;.ty
55740 70 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63 pedef struct Src
55750 4c 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 List SrcList;.ty
55760 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72 pedef struct Str
55770 41 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a Accum StrAccum;.
55780 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 typedef struct T
55790 61 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 able Table;.type
557a0 64 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 def struct Table
557b0 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a Lock TableLock;.
557c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 typedef struct T
557d0 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 oken Token;.type
557e0 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 def struct Trigg
557f0 65 72 53 74 61 63 6b 20 54 72 69 67 67 65 72 53 erStack TriggerS
55800 74 61 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 74 tack;.typedef st
55810 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 ruct TriggerStep
55820 20 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74 79 TriggerStep;.ty
55830 70 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 pedef struct Tri
55840 67 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74 79 gger Trigger;.ty
55850 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 70 pedef struct Unp
55860 61 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70 61 ackedRecord Unpa
55870 63 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70 65 ckedRecord;.type
55880 64 65 66 20 73 74 72 75 63 74 20 57 61 6c 6b 65 def struct Walke
55890 72 20 57 61 6c 6b 65 72 3b 0a 74 79 70 65 64 65 r Walker;.typede
558a0 66 20 73 74 72 75 63 74 20 57 68 65 72 65 49 6e f struct WhereIn
558b0 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 fo WhereInfo;.ty
558c0 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 pedef struct Whe
558d0 72 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65 76 reLevel WhereLev
558e0 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 el;../*.** Defer
558f0 20 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e 68 sourcing vdbe.h
55900 20 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e 74 and btree.h unt
55910 69 6c 20 61 66 74 65 72 20 74 68 65 20 22 75 38 il after the "u8
55920 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 " and .** "BusyH
55930 61 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66 73 andler" typedefs
55940 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 . vdbe.h also re
55950 71 75 69 72 65 73 20 61 20 66 65 77 20 6f 66 20 quires a few of
55960 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f the opaque.** po
55970 69 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e 65 inter types (i.e
55980 2e 20 46 75 6e 63 44 65 66 29 20 64 65 66 69 6e . FuncDef) defin
55990 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a ed above..*/./**
559a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
559b0 6c 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e 20 lude btree.h in
559c0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 the middle of sq
559d0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
559e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
559f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
55a00 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68 20 in file btree.h
55a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
55a40 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
55a50 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
55a60 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
55a70 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
55a80 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
55a90 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
55aa0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
55ab0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
55ac0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
55ad0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
55ae0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
55af0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
55b00 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
55b10 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
55b20 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
55b30 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
55b40 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
55b50 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
55b60 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
55b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55bb0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 ***.** This head
55bc0 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 er file defines
55bd0 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 the interface th
55be0 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 42 2d at the sqlite B-
55bf0 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 Tree file.** sub
55c00 73 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d system. See com
55c10 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f 75 ments in the sou
55c20 72 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20 64 rce code for a d
55c30 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 74 etailed descript
55c40 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 ion.** of what e
55c50 61 63 68 20 69 6e 74 65 72 66 61 63 65 20 72 6f ach interface ro
55c60 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a utine does..**.*
55c70 2a 20 40 28 23 29 20 24 49 64 3a 20 62 74 72 65 * @(#) $Id: btre
55c80 65 2e 68 2c 76 20 31 2e 31 30 35 20 32 30 30 38 e.h,v 1.105 2008
55c90 2f 31 30 2f 32 37 20 31 33 3a 35 39 3a 33 34 20 /10/27 13:59:34
55ca0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
55cb0 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 54 $.*/.#ifndef _BT
55cc0 52 45 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f REE_H_.#define _
55cd0 42 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 BTREE_H_../* TOD
55ce0 4f 3a 20 54 68 69 73 20 64 65 66 69 6e 69 74 69 O: This definiti
55cf0 6f 6e 20 69 73 20 6a 75 73 74 20 69 6e 63 6c 75 on is just inclu
55d00 64 65 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f 64 ded so other mod
55d10 75 6c 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 74 ules compile. It
55d20 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 .** needs to be
55d30 72 65 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 revisited..*/.#d
55d40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 42 efine SQLITE_N_B
55d50 54 52 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a TREE_META 10../*
55d60 0a 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 .** If defined a
55d70 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f s non-zero, auto
55d80 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c -vacuum is enabl
55d90 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f ed by default. O
55da0 74 68 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 6d therwise.** it m
55db0 75 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e ust be turned on
55dc0 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 for each databa
55dd0 73 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41 se using "PRAGMA
55de0 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31 auto_vacuum = 1
55df0 22 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 "..*/.#ifndef SQ
55e00 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 LITE_DEFAULT_AUT
55e10 4f 56 41 43 55 55 4d 0a 20 20 23 64 65 66 69 6e OVACUUM. #defin
55e20 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
55e30 5f 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 _AUTOVACUUM 0.#e
55e40 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 54 ndif..#define BT
55e50 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e REE_AUTOVACUUM_N
55e60 4f 4e 45 20 30 20 20 20 20 20 20 20 20 2f 2a 20 ONE 0 /*
55e70 44 6f 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d 76 Do not do auto-v
55e80 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 acuum */.#define
55e90 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 BTREE_AUTOVACUU
55ea0 4d 5f 46 55 4c 4c 20 31 20 20 20 20 20 20 20 20 M_FULL 1
55eb0 2f 2a 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f 2d /* Do full auto-
55ec0 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e vacuum */.#defin
55ed0 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 e BTREE_AUTOVACU
55ee0 55 4d 5f 49 4e 43 52 20 32 20 20 20 20 20 20 20 UM_INCR 2
55ef0 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 /* Incremental
55f00 76 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a vacuum */../*.**
55f10 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 Forward declara
55f20 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 tions of structu
55f30 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 re.*/.typedef st
55f40 72 75 63 74 20 42 74 72 65 65 20 42 74 72 65 65 ruct Btree Btree
55f50 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
55f60 20 42 74 43 75 72 73 6f 72 20 42 74 43 75 72 73 BtCursor BtCurs
55f70 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 or;.typedef stru
55f80 63 74 20 42 74 53 68 61 72 65 64 20 42 74 53 68 ct BtShared BtSh
55f90 61 72 65 64 3b 0a 74 79 70 65 64 65 66 20 73 74 ared;.typedef st
55fa0 72 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 ruct BtreeMutexA
55fb0 72 72 61 79 20 42 74 72 65 65 4d 75 74 65 78 41 rray BtreeMutexA
55fc0 72 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rray;../*.** Thi
55fd0 73 20 73 74 72 75 63 74 75 72 65 20 72 65 63 6f s structure reco
55fe0 72 64 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 42 rds all of the B
55ff0 74 72 65 65 73 20 74 68 61 74 20 6e 65 65 64 20 trees that need
56000 74 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 to hold.** a mut
56010 65 78 20 62 65 66 6f 72 65 20 77 65 20 65 6e 74 ex before we ent
56020 65 72 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 er sqlite3VdbeEx
56030 65 63 28 29 2e 20 20 54 68 65 20 42 74 72 65 65 ec(). The Btree
56040 73 20 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c 61 s are.** are pla
56050 63 65 64 20 69 6e 20 61 42 74 72 65 65 5b 5d 20 ced in aBtree[]
56060 69 6e 20 6f 72 64 65 72 20 6f 66 20 61 42 74 72 in order of aBtr
56070 65 65 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 74 ee[]->pBt. That
56080 20 77 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 way,.** we can
56090 61 6c 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 20 always lock and
560a0 75 6e 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c 20 unlock them all
560b0 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 quickly..*/.stru
560c0 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 ct BtreeMutexArr
560d0 61 79 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 65 ay {. int nMute
560e0 78 3b 0a 20 20 42 74 72 65 65 20 2a 61 42 74 72 x;. Btree *aBtr
560f0 65 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 ee[SQLITE_MAX_AT
56100 54 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a TACHED+1];.};...
56110 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
56120 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f nt sqlite3BtreeO
56130 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 pen(. const cha
56140 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 r *zFilename,
56150 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 /* Name of datab
56160 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e ase file to open
56170 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
56180 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f b, /
56190 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 * Associated dat
561a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
561b0 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 2c 20 */. Btree **,
561c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
561d0 2a 20 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 74 * Return open Bt
561e0 72 65 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 69 ree* here */. i
561f0 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
56200 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 /* Flags
56210 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 */. int vfsFla
56220 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20 2f gs /
56230 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 * Flags passed t
56240 68 72 6f 75 67 68 20 74 6f 20 56 46 53 20 6f 70 hrough to VFS op
56250 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 en */.);../* The
56260 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 flags parameter
56270 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
56280 4f 70 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20 Open can be the
56290 62 69 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68 bitwise or of th
562a0 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 e.** following v
562b0 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 alues..**.** NOT
562c0 45 3a 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 E: These values
562d0 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 must match the
562e0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 41 corresponding PA
562f0 47 45 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a 2a GER_ values in.*
56300 2a 20 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 * pager.h..*/.#d
56310 65 66 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 54 efine BTREE_OMIT
56320 5f 4a 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a 20 _JOURNAL 1 /*
56330 44 6f 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 6e Do not use journ
56340 61 6c 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 al. No argument
56350 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
56360 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 E_NO_READLOCK
56370 32 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 2 /* Omit readl
56380 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 ocks on readonly
56390 20 66 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 6e files */.#defin
563a0 65 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 e BTREE_MEMORY
563b0 20 20 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 4 /* In-m
563c0 65 6d 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 72 emory DB. No ar
563d0 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e gument */.#defin
563e0 65 20 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c 59 e BTREE_READONLY
563f0 20 20 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 6e 8 /* Open
56400 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e the database in
56410 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 read-only mode
56420 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 */.#define BTREE
56430 5f 52 45 41 44 57 52 49 54 45 20 20 20 20 31 36 _READWRITE 16
56440 20 20 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6f /* Open for bo
56450 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 th reading and w
56460 72 69 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e riting */.#defin
56470 65 20 42 54 52 45 45 5f 43 52 45 41 54 45 20 20 e BTREE_CREATE
56480 20 20 20 20 20 33 32 20 20 2f 2a 20 43 72 65 61 32 /* Crea
56490 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 te the database
564a0 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 if it does not e
564b0 78 69 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f xist */..SQLITE_
564c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
564d0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 te3BtreeClose(Bt
564e0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
564f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
56500 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 3BtreeSetCacheSi
56510 7a 65 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a ze(Btree*,int);.
56520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
56530 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
56540 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 etSafetyLevel(Bt
56550 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ree*,int,int);.S
56560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
56570 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 t sqlite3BtreeSy
56580 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 ncDisabled(Btree
56590 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
565a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
565b0 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 reeSetPageSize(B
565c0 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a tree*,int,int);.
565d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
565e0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
565f0 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 etPageSize(Btree
56600 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
56610 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
56620 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 reeMaxPageCount(
56630 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c Btree*,int);.SQL
56640 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
56650 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 sqlite3BtreeGetR
56660 65 73 65 72 76 65 28 42 74 72 65 65 2a 29 3b 0a eserve(Btree*);.
56670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
56680 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
56690 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 etAutoVacuum(Btr
566a0 65 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ee *, int);.SQLI
566b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
566c0 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75 qlite3BtreeGetAu
566d0 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a toVacuum(Btree *
566e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
566f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
56700 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 eeBeginTrans(Btr
56710 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ee*,int);.SQLITE
56720 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
56730 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
56740 68 61 73 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20 haseOne(Btree*,
56750 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
56760 74 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ter);.SQLITE_PRI
56770 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
56780 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
56790 54 77 6f 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c Two(Btree*);.SQL
567a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
567b0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
567c0 69 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 it(Btree*);.SQLI
567d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
567e0 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
567f0 61 63 6b 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c ack(Btree*);.SQL
56800 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
56810 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
56820 6e 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53 nStmt(Btree*);.S
56830 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
56840 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
56850 6d 6d 69 74 53 74 6d 74 28 42 74 72 65 65 2a 29 mmitStmt(Btree*)
56860 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
56870 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
56880 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 28 42 74 eRollbackStmt(Bt
56890 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
568a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
568b0 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 3BtreeCreateTabl
568c0 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a 2c 20 e(Btree*, int*,
568d0 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51 4c 49 int flags);.SQLI
568e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
568f0 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 qlite3BtreeIsInT
56900 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51 rans(Btree*);.SQ
56910 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
56920 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
56930 6e 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53 nStmt(Btree*);.S
56940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
56950 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 t sqlite3BtreeIs
56960 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 InReadTrans(Btre
56970 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
56980 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
56990 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 3BtreeSchema(Btr
569a0 65 65 20 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 ee *, int, void(
569b0 2a 29 28 76 6f 69 64 20 2a 29 29 3b 0a 53 51 4c *)(void *));.SQL
569c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
569d0 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 sqlite3BtreeSche
569e0 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a maLocked(Btree *
569f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
56a00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
56a10 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 eeLockTable(Btre
56a20 65 20 2a 2c 20 69 6e 74 2c 20 75 38 29 3b 0a 0a e *, int, u8);..
56a30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
56a40 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
56a50 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 e3BtreeGetFilena
56a60 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c me(Btree *);.SQL
56a70 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
56a80 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 t char *sqlite3B
56a90 74 72 65 65 47 65 74 44 69 72 6e 61 6d 65 28 42 treeGetDirname(B
56aa0 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f tree *);.SQLITE_
56ab0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
56ac0 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 ar *sqlite3Btree
56ad0 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 GetJournalname(B
56ae0 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f tree *);.SQLITE_
56af0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
56b00 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 te3BtreeCopyFile
56b10 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65 65 20 (Btree *, Btree
56b20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);..SQLITE_PRIV
56b30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
56b40 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42 treeIncrVacuum(B
56b50 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54 68 65 tree *);../* The
56b60 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 flags parameter
56b70 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
56b80 43 72 65 61 74 65 54 61 62 6c 65 20 63 61 6e 20 CreateTable can
56b90 62 65 20 74 68 65 20 62 69 74 77 69 73 65 20 4f be the bitwise O
56ba0 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c R.** of the foll
56bb0 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a 2f 0a owing flags:.*/.
56bc0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 49 4e #define BTREE_IN
56bd0 54 4b 45 59 20 20 20 20 20 31 20 20 20 20 2f 2a TKEY 1 /*
56be0 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c 79 20 Table has only
56bf0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
56c00 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 23 64 teger keys */.#d
56c10 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45 52 4f efine BTREE_ZERO
56c20 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a 20 54 DATA 2 /* T
56c30 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20 6f 6e able has keys on
56c40 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a 2f 0a ly - no data */.
56c50 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4c 45 #define BTREE_LE
56c60 41 46 44 41 54 41 20 20 20 34 20 20 20 20 2f 2a AFDATA 4 /*
56c70 20 44 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 Data stored in
56c80 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20 49 6d leaves only. Im
56c90 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a 2f 0a plies INTKEY */.
56ca0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
56cb0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
56cc0 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65 2a DropTable(Btree*
56cd0 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 , int, int*);.SQ
56ce0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
56cf0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
56d00 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 arTable(Btree*,
56d10 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 int, int*);.SQLI
56d20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
56d30 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 qlite3BtreeGetMe
56d40 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 69 ta(Btree*, int i
56d50 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 29 dx, u32 *pValue)
56d60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
56d70 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
56d80 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 eUpdateMeta(Btre
56d90 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 e*, int idx, u32
56da0 20 76 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 5f value);.SQLITE_
56db0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
56dc0 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c ite3BtreeTripAll
56dd0 43 75 72 73 6f 72 73 28 42 74 72 65 65 2a 2c 20 Cursors(Btree*,
56de0 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 int);..SQLITE_PR
56df0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
56e00 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 3BtreeCursor(.
56e10 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20 20 Btree*,
56e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
56e30 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63 6f /* BTree co
56e40 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 74 ntaining table t
56e50 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 o open */. int
56e60 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 iTable,
56e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
56e80 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f 6f /* Index of roo
56e90 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 t page */. int
56ea0 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 wrFlag,
56eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
56ec0 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69 6e /* 1 for writin
56ed0 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d 6f g. 0 for read-o
56ee0 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 nly */. struct
56ef0 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20 20 KeyInfo*,
56f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
56f10 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 First argument
56f20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63 74 to compare funct
56f30 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f ion */. BtCurso
56f40 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20 20 r *pCursor
56f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
56f60 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 Space to write
56f70 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 cursor structure
56f80 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 */.);.SQLITE_PR
56f90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
56fa0 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 3BtreeCursorSize
56fb0 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 45 5f (void);..SQLITE_
56fc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
56fd0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 te3BtreeCloseCur
56fe0 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a sor(BtCursor*);.
56ff0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
57000 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d nt sqlite3BtreeM
57010 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f oveto(. BtCurso
57020 72 2a 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 r*,. const void
57030 20 2a 70 4b 65 79 2c 0a 20 20 69 36 34 20 6e 4b *pKey,. i64 nK
57040 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a ey,. int bias,.
57050 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 int *pRes.);.S
57060 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
57070 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f t sqlite3BtreeMo
57080 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20 vetoUnpacked(.
57090 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e 70 BtCursor*,. Unp
570a0 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e ackedRecord *pUn
570b0 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b 65 Key,. i64 intKe
570c0 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a 20 y,. int bias,.
570d0 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 51 int *pRes.);.SQ
570e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
570f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
57100 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75 sorHasMoved(BtCu
57110 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 rsor*, int*);.SQ
57120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
57130 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c sqlite3BtreeDel
57140 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a ete(BtCursor*);.
57150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
57160 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
57170 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a 2c nsert(BtCursor*,
57180 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
57190 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 20 y, i64 nKey,.
571a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
571b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
571c0 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 onst void *pData
571d0 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 , int nData,.
571e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
571f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
57200 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62 69 nt nZero, int bi
57210 61 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 as);.SQLITE_PRIV
57220 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
57230 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72 73 treeFirst(BtCurs
57240 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b or*, int *pRes);
57250 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
57260 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
57270 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 Last(BtCursor*,
57280 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 int *pRes);.SQLI
57290 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
572a0 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 qlite3BtreeNext(
572b0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
572c0 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pRes);.SQLITE_PR
572d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
572e0 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 3BtreeEof(BtCurs
572f0 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 or*);.SQLITE_PRI
57300 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
57310 42 74 72 65 65 46 6c 61 67 73 28 42 74 43 75 72 BtreeFlags(BtCur
57320 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sor*);.SQLITE_PR
57330 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
57340 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42 3BtreePrevious(B
57350 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 tCursor*, int *p
57360 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Res);.SQLITE_PRI
57370 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
57380 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43 BtreeKeySize(BtC
57390 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69 ursor*, i64 *pSi
573a0 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ze);.SQLITE_PRIV
573b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
573c0 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 treeKey(BtCursor
573d0 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 *, u32 offset, u
573e0 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 32 amt, void*);.
573f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
57400 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42 qlite3 *sqlite3B
57410 74 72 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e treeCursorDb(con
57420 73 74 20 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 st BtCursor*);.S
57430 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
57440 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
57450 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 3BtreeKeyFetch(B
57460 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 tCursor*, int *p
57470 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Amt);.SQLITE_PRI
57480 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 VATE const void
57490 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 *sqlite3BtreeDat
574a0 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a aFetch(BtCursor*
574b0 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 , int *pAmt);.SQ
574c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
574d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
574e0 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c aSize(BtCursor*,
574f0 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53 51 u32 *pSize);.SQ
57500 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
57510 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
57520 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 a(BtCursor*, u32
57530 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 offset, u32 amt
57540 2c 20 76 6f 69 64 2a 29 3b 0a 0a 53 51 4c 49 54 , void*);..SQLIT
57550 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
57560 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 sqlite3BtreeInte
57570 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65 grityCheck(Btree
57580 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 *, int *aRoot, i
57590 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 nt nRoot, int, i
575a0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
575b0 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65 VATE struct Page
575c0 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 r *sqlite3BtreeP
575d0 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53 ager(Btree*);..S
575e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
575f0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 t sqlite3BtreePu
57600 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c tData(BtCursor*,
57610 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 u32 offset, u32
57620 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 amt, void*);.SQ
57630 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
57640 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 d sqlite3BtreeCa
57650 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 cheOverflow(BtCu
57660 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f rsor *);.SQLITE_
57670 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
57680 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 ite3BtreeClearCu
57690 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29 rsor(BtCursor *)
576a0 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
576b0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 _TEST.SQLITE_PRI
576c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
576d0 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 28 BtreeCursorInfo(
576e0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c BtCursor*, int*,
576f0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
57700 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
57710 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 73 e3BtreeCursorLis
57720 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69 t(Btree*);.#endi
57730 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 f../*.** If we a
57740 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 61 re not using sha
57750 72 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e 20 red cache, then
57760 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 there is no need
57770 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78 to.** use mutex
57780 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 es to access the
57790 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 BtShared struct
577a0 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74 ures. So make t
577b0 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 he.** Enter and
577c0 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 73 Leave procedures
577d0 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 20 no-ops..*/.#if
577e0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
577f0 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
57800 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 E) && SQLITE_THR
57810 45 41 44 53 41 46 45 0a 53 51 4c 49 54 45 5f 50 EADSAFE.SQLITE_P
57820 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
57830 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
57840 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
57850 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
57860 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
57870 28 42 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64 65 (Btree*);.#ifnde
57880 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 f NDEBUG. /* Th
57890 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
578a0 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 ed inside assert
578b0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e () statements on
578c0 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly. */.SQLITE_PR
578d0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
578e0 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
578f0 65 78 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 ex(Btree*);.#end
57900 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
57910 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
57920 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 BtreeEnterCursor
57930 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c (BtCursor*);.SQL
57940 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
57950 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c id sqlite3BtreeL
57960 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 72 eaveCursor(BtCur
57970 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sor*);.SQLITE_PR
57980 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
57990 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c ite3BtreeEnterAl
579a0 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c l(sqlite3*);.SQL
579b0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
579c0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c id sqlite3BtreeL
579d0 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a eaveAll(sqlite3*
579e0 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 );.#ifndef NDEBU
579f0 47 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 G. /* This rout
57a00 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 ine is used insi
57a10 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 de assert() stat
57a20 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a ements only. */.
57a30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
57a40 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
57a50 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 eHoldsAllMutexes
57a60 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6e 64 (sqlite3*);.#end
57a70 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
57a80 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
57a90 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 BtreeMutexArrayE
57aa0 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 nter(BtreeMutexA
57ab0 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rray*);.SQLITE_P
57ac0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
57ad0 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
57ae0 72 72 61 79 4c 65 61 76 65 28 42 74 72 65 65 4d rrayLeave(BtreeM
57af0 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c utexArray*);.SQL
57b00 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
57b10 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
57b20 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 utexArrayInsert(
57b30 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a BtreeMutexArray*
57b40 2c 20 42 74 72 65 65 2a 29 3b 0a 23 65 6c 73 65 , Btree*);.#else
57b50 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
57b60 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 0a 23 3BtreeEnter(X).#
57b70 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
57b80 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 69 66 treeLeave(X).#if
57b90 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a ndef NDEBUG. /*
57ba0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
57bb0 20 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 used inside ass
57bc0 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
57bd0 20 6f 6e 6c 79 2e 20 2a 2f 0a 23 20 64 65 66 69 only. */.# defi
57be0 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 ne sqlite3BtreeH
57bf0 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31 0a 23 oldsMutex(X) 1.#
57c00 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 73 endif.# define s
57c10 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
57c20 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 69 Cursor(X).# defi
57c30 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c ne sqlite3BtreeL
57c40 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23 20 eaveCursor(X).#
57c50 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
57c60 72 65 65 45 6e 74 65 72 41 6c 6c 28 58 29 0a 23 reeEnterAll(X).#
57c70 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
57c80 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 0a treeLeaveAll(X).
57c90 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
57ca0 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 /* This routine
57cb0 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 is used inside
57cc0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
57cd0 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 23 20 64 nts only. */.# d
57ce0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 efine sqlite3Btr
57cf0 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 eeHoldsAllMutexe
57d00 73 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 23 20 s(X) 1.#endif.#
57d10 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
57d20 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 reeMutexArrayEnt
57d30 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 er(X).# define s
57d40 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
57d50 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a 23 20 ArrayLeave(X).#
57d60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
57d70 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 reeMutexArrayIns
57d80 65 72 74 28 58 2c 59 29 0a 23 65 6e 64 69 66 0a ert(X,Y).#endif.
57d90 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54 52 ..#endif /* _BTR
57da0 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a EE_H_ */../*****
57db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
57dc0 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a btree.h *******
57dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
57de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
57df0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
57e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
57e10 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
57e20 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
57e30 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
57e40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
57e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
57e60 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65 20 e vdbe.h in the
57e70 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
57e80 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
57e90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
57ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
57eb0 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a 2a file vdbe.h ****
57ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
57ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
57ee0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
57ef0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
57f00 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
57f10 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
57f20 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
57f30 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
57f40 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
57f50 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
57f60 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
57f70 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
57f80 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
57f90 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
57fa0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
57fb0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
57fc0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
57fd0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
57fe0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
57ff0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
58000 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
58010 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
58020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58060 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65 20 .** Header file
58070 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c 20 for the Virtual
58080 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65 20 DataBase Engine
58090 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68 69 (VDBE).**.** Thi
580a0 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65 73 s header defines
580b0 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 the interface t
580c0 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64 61 o the virtual da
580d0 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a 2a tabase engine.**
580e0 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20 56 or VDBE. The V
580f0 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 DBE implements a
58100 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68 69 n abstract machi
58110 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a 2a ne that runs a.*
58120 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61 6d * simple program
58130 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20 6d to access and m
58140 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72 6c odify the underl
58150 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a 2a ying database..*
58160 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 2e 68 *.** $Id: vdbe.h
58170 2c 76 20 31 2e 31 33 39 20 32 30 30 38 2f 31 30 ,v 1.139 2008/10
58180 2f 33 31 20 31 30 3a 35 33 3a 32 33 20 64 61 6e /31 10:53:23 dan
58190 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a ielk1977 Exp $.*
581a0 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 /.#ifndef _SQLIT
581b0 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69 6e E_VDBE_H_.#defin
581c0 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 e _SQLITE_VDBE_H
581d0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c _../*.** A singl
581e0 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70 61 e VDBE is an opa
581f0 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 que structure na
58200 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e 6c med "Vdbe". Onl
58210 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e y routines.** in
58220 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c 65 the source file
58230 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61 72 sqliteVdbe.c ar
58240 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65 e allowed to see
58250 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a 20 the insides.**
58260 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
58270 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 e..*/.typedef st
58280 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b 0a ruct Vdbe Vdbe;.
58290 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 73 ./*.** The names
582a0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
582b0 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65 64 g types declared
582c0 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61 72 in vdbeInt.h ar
582d0 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66 6f e required.** fo
582e0 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65 66 r the VdbeOp def
582f0 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 inition..*/.type
58300 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 46 def struct VdbeF
58310 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74 79 unc VdbeFunc;.ty
58320 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d pedef struct Mem
58330 20 4d 65 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 Mem;../*.** A s
58340 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f ingle instructio
58350 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c n of the virtual
58360 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20 machine has an
58370 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73 opcode.** and as
58380 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f many as three o
58390 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e perands. The in
583a0 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63 struction is rec
583b0 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69 orded.** as an i
583c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
583d0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
583e0 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 re:.*/.struct Vd
583f0 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f beOp {. u8 opco
58400 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 de; /*
58410 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74 What operation t
58420 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 o perform */. s
58430 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79 70 igned char p4typ
58440 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 e; /* One of the
58450 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74 P4_xxx constant
58460 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38 s for p4 */. u8
58470 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20 opflags;
58480 20 20 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 /* Not current
58490 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 20 ly used */. u8
584a0 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p5;
584b0 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65 /* Fifth parame
584c0 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e ter is an unsign
584d0 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a ed character */.
584e0 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 int p1;
584f0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f /* First o
58500 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
58510 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p2;
58520 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 /* Second parame
58530 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a ter (often the j
58540 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 ump destination)
58550 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20 */. int p3;
58560 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
58570 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
58580 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 */. union {
58590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 72 /* for
585a0 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a th parameter */.
585b0 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 int i;
585c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
585d0 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 70 teger value if p
585e0 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 4type==P4_INT32
585f0 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b 20 */. void *p;
58600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
58610 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 Generic pointer
58620 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b */. char *z;
58630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
58640 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 * Pointer to dat
58650 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 68 a for string (ch
58660 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73 20 ar array) types
58670 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 34 */. i64 *pI64
58680 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
58690 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
586a0 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a 2f e is P4_INT64 */
586b0 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 65 . double *pRe
586c0 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 al; /* U
586d0 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 sed when p4type
586e0 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 20 is P4_REAL */.
586f0 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 FuncDef *pFunc
58700 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 ; /* Used
58710 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
58720 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 20 P4_FUNCDEF */.
58730 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 VdbeFunc *pVdb
58740 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 64 eFunc; /* Used
58750 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
58760 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a 20 P4_VDBEFUNC */.
58770 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c CollSeq *pCol
58780 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 l; /* Use
58790 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
587a0 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a 20 P4_COLLSEQ */.
587b0 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 Mem *pMem;
587c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
587d0 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
587e0 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 73 P4_MEM */. s
587f0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
58800 61 62 3b 20 20 20 2f 2a 20 55 73 65 64 20 77 68 ab; /* Used wh
58810 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f en p4type is P4_
58820 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 49 VTAB */. KeyI
58830 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 nfo *pKeyInfo;
58840 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 /* Used when
58850 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 59 p4type is P4_KEY
58860 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 INFO */. int
58870 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 *ai;
58880 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 /* Used when
58890 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54 p4type is P4_INT
588a0 41 52 52 41 59 20 2a 2f 0a 20 20 7d 20 70 34 3b ARRAY */. } p4;
588b0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
588c0 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a 43 6f EBUG. char *zCo
588d0 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 mment;
588e0 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69 6d /* Comment to im
588f0 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c 69 74 prove readabilit
58900 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 y */.#endif.#ifd
58910 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a ef VDBE_PROFILE.
58920 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 int cnt;
58930 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
58940 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 mber of times th
58950 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 is instruction w
58960 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 as executed */.
58970 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 20 20 u64 cycles;
58980 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
58990 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 65 78 al time spent ex
589a0 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e 73 ecuting this ins
589b0 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 truction */.#end
589c0 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 if.};.typedef st
589d0 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 62 65 ruct VdbeOp Vdbe
589e0 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6d 61 Op;../*.** A sma
589f0 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 ller version of
58a00 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f 72 20 VdbeOp used for
58a10 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c 69 73 the VdbeAddOpLis
58a20 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 t() function bec
58a30 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b 65 73 ause.** it takes
58a40 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 2e 0a up less space..
58a50 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70 */.struct VdbeOp
58a60 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 63 6f List {. u8 opco
58a70 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 de; /*
58a80 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74 What operation t
58a90 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 o perform */. s
58aa0 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b 20 20 igned char p1;
58ab0 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65 72 /* First oper
58ac0 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 and */. signed
58ad0 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f 2a 20 char p2; /*
58ae0 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 Second parameter
58af0 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d 70 (often the jump
58b00 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a 2f destination) */
58b10 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 . signed char p
58b20 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72 64 20 3; /* Third
58b30 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d 3b 0a parameter */.};.
58b40 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 typedef struct V
58b50 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 4f 70 dbeOpList VdbeOp
58b60 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c List;../*.** All
58b70 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 56 owed values of V
58b80 64 62 65 4f 70 2e 70 33 74 79 70 65 0a 2a 2f 0a dbeOp.p3type.*/.
58b90 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54 55 53 #define P4_NOTUS
58ba0 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65 ED 0 /* The
58bb0 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69 73 P4 parameter is
58bc0 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 64 65 not used */.#de
58bd0 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49 43 20 fine P4_DYNAMIC
58be0 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e 74 65 (-1) /* Pointe
58bf0 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f 62 r to a string ob
58c00 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
58c10 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 teMalloc() */.#d
58c20 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49 43 20 efine P4_STATIC
58c30 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 6e 74 (-2) /* Point
58c40 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 73 er to a static s
58c50 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 tring */.#define
58c60 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28 2d 34 P4_COLLSEQ (-4
58c70 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f ) /* P4 is a po
58c80 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c 53 inter to a CollS
58c90 65 71 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a eq structure */.
58ca0 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e 43 44 #define P4_FUNCD
58cb0 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 34 20 EF (-5) /* P4
58cc0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
58cd0 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 a FuncDef struct
58ce0 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
58cf0 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 29 20 4_KEYINFO (-6)
58d00 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
58d10 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f ter to a KeyInfo
58d20 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 structure */.#d
58d30 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46 55 4e efine P4_VDBEFUN
58d40 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20 69 73 C (-7) /* P4 is
58d50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
58d60 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 74 75 VdbeFunc structu
58d70 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 re */.#define P4
58d80 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 20 20 _MEM (-8)
58d90 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 /* P4 is a point
58da0 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 20 20 er to a Mem*
58db0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 structure */.#de
58dc0 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49 45 4e fine P4_TRANSIEN
58dd0 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69 73 20 T (-9) /* P4 is
58de0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 74 a pointer to a t
58df0 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e 67 20 ransient string
58e00 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56 54 */.#define P4_VT
58e10 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f 2a 20 AB (-10) /*
58e20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
58e30 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 to an sqlite3_vt
58e40 61 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a ab structure */.
58e50 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52 49 4e #define P4_MPRIN
58e60 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 34 20 TF (-11) /* P4
58e70 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 74 61 is a string obta
58e80 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
58e90 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 3_mprintf() */.#
58ea0 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c 20 20 define P4_REAL
58eb0 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34 20 69 (-12) /* P4 i
58ec0 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 s a 64-bit float
58ed0 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 ing point value
58ee0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e */.#define P4_IN
58ef0 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f 2a 20 T64 (-13) /*
58f00 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20 73 P4 is a 64-bit s
58f10 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
58f20 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 33 .#define P4_INT3
58f30 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 50 34 2 (-14) /* P4
58f40 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 69 67 is a 32-bit sig
58f50 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 ned integer */.#
58f60 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 52 52 define P4_INTARR
58f70 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 20 69 AY (-15) /* P4 i
58f80 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 33 32 s a vector of 32
58f90 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 2a 2f -bit integers */
58fa0 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 ../* When adding
58fb0 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20 75 a P4 argument u
58fc0 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f 2c sing P4_KEYINFO,
58fd0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 4b a copy of the K
58fe0 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
58ff0 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 68 .** is made. Th
59000 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 65 64 at copy is freed
59010 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 when the Vdbe i
59020 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 75 s finalized. Bu
59030 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 t if the.** argu
59040 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49 4e ment is P4_KEYIN
59050 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 20 FO_HANDOFF, the
59060 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 65 passed in pointe
59070 72 20 69 73 20 75 73 65 64 2e 20 20 49 74 20 73 r is used. It s
59080 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 65 till.** gets fre
59090 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 ed when the Vdbe
590a0 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 6f is finalized so
590b0 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c 64 it still should
590c0 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 be obtained.**
590d0 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 71 from a single sq
590e0 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 42 liteMalloc(). B
590f0 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d 61 ut no copy is ma
59100 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 de and the calli
59110 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 ng.** function s
59120 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 20 hould *not* try
59130 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79 49 to free the KeyI
59140 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 nfo..*/.#define
59150 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f P4_KEYINFO_HANDO
59160 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e 65 FF (-16).#define
59170 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 P4_KEYINFO_STAT
59180 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a 2a IC (-17)../*.**
59190 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e 61 The Vdbe.aColNa
591a0 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e me array contain
591b0 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74 75 s 5n Mem structu
591c0 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73 20 res, where n is
591d0 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the .** number o
591e0 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 f columns of dat
591f0 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 a returned by th
59200 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a e statement..*/.
59210 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f #define COLNAME_
59220 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66 69 NAME 0.#defi
59230 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 ne COLNAME_DECLT
59240 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43 4f YPE 1.#define CO
59250 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20 32 LNAME_DATABASE 2
59260 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 .#define COLNAME
59270 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65 66 _TABLE 3.#def
59280 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 ine COLNAME_COLU
59290 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53 51 MN 4.#ifdef SQ
592a0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 LITE_ENABLE_COLU
592b0 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 MN_METADATA.# de
592c0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 fine COLNAME_N
592d0 20 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a 20 5 /*
592e0 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41 4d Number of COLNAM
592f0 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a 2f E_xxx symbols */
59300 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53 .#else.# ifdef S
59310 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 QLITE_OMIT_DECLT
59320 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20 43 YPE.# define C
59330 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31 20 OLNAME_N 1
59340 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f 6e /* Store on
59350 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 23 ly the name */.#
59360 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 else.# define
59370 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 COLNAME_N
59380 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 2 /* Store
59390 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65 63 the name and dec
593a0 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69 66 ltype */.# endif
593b0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
593c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
593d0 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72 65 ro converts a re
593e0 6c 61 74 69 76 65 20 61 64 64 72 65 73 73 20 69 lative address i
593f0 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a 2a n the p2 field.*
59400 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73 74 * of a VdbeOp st
59410 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20 6e ructure into a n
59420 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 73 egative number s
59430 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 74 o that .** sqlit
59440 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 e3VdbeAddOpList(
59450 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 65 ) knows that the
59460 20 61 64 64 72 65 73 73 20 69 73 20 72 65 6c 61 address is rela
59470 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a 2a tive. Calling.*
59480 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61 69 * the macro agai
59490 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 61 n restores the a
594a0 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 69 ddress..*/.#defi
594b0 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d 31 2d ne ADDR(X) (-1-
594c0 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 (X))../*.** The
594d0 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 74 makefile scans t
594e0 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 he vdbe.c source
594f0 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74 65 file and create
59500 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e 68 s the "opcodes.h
59510 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65 ".** header file
59520 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 61 20 that defines a
59530 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68 20 number for each
59540 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79 20 74 opcode used by t
59550 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a 2a he VDBE..*/./***
59560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
59570 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 6e ude opcodes.h in
59580 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 the middle of v
59590 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dbe.h **********
595a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
595b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
595c0 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e 68 n file opcodes.h
595d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
595e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
595f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 **********/./* A
59600 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e utomatically gen
59610 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 erated. Do not
59620 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 74 edit */./* See t
59630 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b he mkopcodeh.awk
59640 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 61 script for deta
59650 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f ils */.#define O
59660 50 5f 56 4e 65 78 74 20 20 20 20 20 20 20 20 20 P_VNext
59670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59680 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
59690 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20 20 OP_Affinity
596a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
596b0 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 2.#defi
596c0 6e 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 20 ne OP_Column
596d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
596e0 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 3.#de
596f0 66 69 6e 65 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 fine OP_SetCooki
59700 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
59710 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 4.#
59720 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 20 20 define OP_Real
59730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59740 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36 126
59750 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
59760 5f 46 4c 4f 41 54 20 20 20 20 2a 2f 0a 23 64 65 _FLOAT */.#de
59770 66 69 6e 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 fine OP_Sequence
59780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59790 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 5.#
597a0 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 47 74 define OP_MoveGt
597b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
597c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
597d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 65 20 20 .#define OP_Ge
597e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
597f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59800 37 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 72 /* same as
59810 54 4b 5f 47 45 20 20 20 20 20 20 20 2a 2f 0a 23 TK_GE */.#
59820 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 4b 65 79 define OP_RowKey
59830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
59850 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 43 6f 70 .#define OP_SCop
59860 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
59870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59880 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 71 8.#define OP_Eq
59890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
598a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
598b0 20 20 36 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 68 /* same a
598c0 73 20 54 4b 5f 45 51 20 20 20 20 20 20 20 2a 2f s TK_EQ */
598d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e .#define OP_Open
598e0 57 72 69 74 65 20 20 20 20 20 20 20 20 20 20 20 Write
598f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59900 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 9.#define OP_No
59910 74 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 tNull
59920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59930 20 20 36 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 66 /* same a
59940 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 2a 2f s TK_NOTNULL */
59950 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 20 20 .#define OP_If
59960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59980 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 10.#define OP_To
59990 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 Int
599a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
599b0 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 142 /* same a
599c0 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 2a 2f s TK_TO_INT */
599d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 .#define OP_Stri
599e0 6e 67 38 20 20 20 20 20 20 20 20 20 20 20 20 20 ng8
599f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59a00 38 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 88 /* same as
59a10 54 4b 5f 53 54 52 49 4e 47 20 20 20 2a 2f 0a 23 TK_STRING */.#
59a20 64 65 66 69 6e 65 20 4f 50 5f 56 52 6f 77 69 64 define OP_VRowid
59a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
59a50 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 6c .#define OP_Coll
59a60 53 65 71 20 20 20 20 20 20 20 20 20 20 20 20 20 Seq
59a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59a80 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 12.#define OP_Op
59a90 65 6e 52 65 61 64 20 20 20 20 20 20 20 20 20 20 enRead
59aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59ab0 20 20 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 13.#define OP_
59ac0 45 78 70 69 72 65 20 20 20 20 20 20 20 20 20 20 Expire
59ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59ae0 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 4f 14.#define O
59af0 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20 20 P_AutoCommit
59b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59b10 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 15.#define
59b20 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20 20 OP_Gt
59b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59b40 20 20 20 20 20 20 20 20 36 39 20 20 20 2f 2a 20 69 /*
59b50 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20 20 same as TK_GT
59b60 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
59b70 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20 20 P_Pagecount
59b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59b90 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 17.#define
59ba0 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 20 OP_IntegrityCk
59bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59bc0 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 18.#defi
59bd0 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20 20 20 20 ne OP_Sort
59be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59bf0 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 64 65 19.#de
59c00 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20 20 fine OP_Copy
59c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59c20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 23 20.#
59c30 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 63 65 20 define OP_Trace
59c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31 21
59c60 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 63 .#define OP_Func
59c70 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 tion
59c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59c90 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 22.#define OP_If
59ca0 4e 65 67 20 20 20 20 20 20 20 20 20 20 20 20 20 Neg
59cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59cc0 20 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 23.#define OP_
59cd0 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 And
59ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59cf0 20 20 20 20 36 31 20 20 20 2f 2a 20 73 61 6d 65 61 /* same
59d00 20 61 73 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 as TK_AND
59d10 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 75 */.#define OP_Su
59d20 62 74 72 61 63 74 20 20 20 20 20 20 20 20 20 20 btract
59d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59d40 20 20 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 79 /* same a
59d50 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 2a 2f s TK_MINUS */
59d60 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70 .#define OP_Noop
59d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59d90 32 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 24.#define OP_Re
59da0 74 75 72 6e 20 20 20 20 20 20 20 20 20 20 20 20 turn
59db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59dc0 20 20 32 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 25.#define OP_
59dd0 52 65 6d 61 69 6e 64 65 72 20 20 20 20 20 20 20 Remainder
59de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59df0 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61 6d 65 82 /* same
59e00 20 61 73 20 54 4b 5f 52 45 4d 20 20 20 20 20 20 as TK_REM
59e10 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 */.#define OP_Ne
59e20 77 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 wRowid
59e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59e40 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 26.#define OP_
59e50 4d 75 6c 74 69 70 6c 79 20 20 20 20 20 20 20 20 Multiply
59e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59e70 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65 80 /* same
59e80 20 61 73 20 54 4b 5f 53 54 41 52 20 20 20 20 20 as TK_STAR
59e90 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 61 */.#define OP_Va
59ea0 72 69 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 riable
59eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59ec0 20 20 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 27.#define OP_
59ed0 53 74 72 69 6e 67 20 20 20 20 20 20 20 20 20 20 String
59ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59ef0 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 4f 28.#define O
59f00 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 20 P_RealAffinity
59f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59f20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 29.#define
59f30 20 4f 50 5f 56 52 65 6e 61 6d 65 20 20 20 20 20 OP_VRename
59f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59f50 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 30.#defi
59f60 6e 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d ne OP_ParseSchem
59f70 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a
59f80 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 65 31.#de
59f90 66 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20 fine OP_VOpen
59fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59fb0 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23 32.#
59fc0 64 65 66 69 6e 65 20 4f 50 5f 43 6c 6f 73 65 20 define OP_Close
59fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33 33
59ff0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 .#define OP_Crea
5a000 74 65 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 teIndex
5a010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a020 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 73 34.#define OP_Is
5a030 55 6e 69 71 75 65 20 20 20 20 20 20 20 20 20 20 Unique
5a040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a050 20 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 35.#define OP_
5a060 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20 20 20 20 NotFound
5a070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a080 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 4f 36.#define O
5a090 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 20 P_Int64
5a0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a0b0 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 37.#define
5a0c0 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 20 20 20 OP_MustBeInt
5a0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a0e0 20 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 69 38.#defi
5a0f0 6e 65 20 4f 50 5f 48 61 6c 74 20 20 20 20 20 20 ne OP_Halt
5a100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a110 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 39.#de
5a120 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20 20 fine OP_Rowid
5a130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a140 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 40.#
5a150 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 4c 54 20 define OP_IdxLT
5a160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31 41
5a180 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 49 .#define OP_AddI
5a190 6d 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 mm
5a1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a1b0 34 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74 42.#define OP_St
5a1c0 61 74 65 6d 65 6e 74 20 20 20 20 20 20 20 20 20 atement
5a1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a1e0 20 20 34 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43.#define OP_
5a1f0 52 6f 77 44 61 74 61 20 20 20 20 20 20 20 20 20 RowData
5a200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a210 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 4f 44.#define O
5a220 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 20 20 20 P_MemMax
5a230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a240 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 45.#define
5a250 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20 20 OP_Or
5a260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a270 20 20 20 20 20 20 20 20 36 30 20 20 20 2f 2a 20 60 /*
5a280 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20 20 same as TK_OR
5a290 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
5a2a0 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 20 P_NotExists
5a2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a2c0 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 65 46.#define
5a2d0 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20 20 OP_Gosub
5a2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a2f0 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 69 47.#defi
5a300 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20 20 ne OP_Divide
5a310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a320 20 20 20 20 20 20 20 20 20 20 38 31 20 20 20 2f 81 /
5a330 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 * same as TK_SLA
5a340 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 SH */.#define
5a350 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20 20 OP_Integer
5a360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a370 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 69 48.#defi
5a380 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 ne OP_ToNumeric
5a390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a3a0 20 20 20 20 20 20 20 20 20 31 34 31 20 20 20 2f 141 /
5a3b0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
5a3c0 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69 6e NUMERIC*/.#defin
5a3d0 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20 20 e OP_Prev
5a3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a3f0 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 66 49.#def
5a400 69 6e 65 20 4f 50 5f 43 6f 6e 63 61 74 20 20 20 ine OP_Concat
5a410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a420 20 20 20 20 20 20 20 20 20 20 20 38 33 20 20 20 83
5a430 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f /* same as TK_CO
5a440 4e 43 41 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e NCAT */.#defin
5a450 65 20 4f 50 5f 42 69 74 41 6e 64 20 20 20 20 20 e OP_BitAnd
5a460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a470 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a 74 /*
5a480 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 41 same as TK_BITA
5a490 4e 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ND */.#define
5a4a0 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20 OP_VColumn
5a4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a4c0 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e 50.#defin
5a4d0 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 e OP_CreateTable
5a4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a4f0 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 66 51.#def
5a500 69 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 ine OP_Last
5a510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a520 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 64 52.#d
5a530 65 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 efine OP_IsNull
5a540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a550 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 20 65
5a560 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
5a570 49 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 ISNULL */.#def
5a580 69 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 ine OP_IncrVacuu
5a590 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m
5a5a0 20 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 53.#d
5a5b0 65 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 efine OP_IdxRowi
5a5c0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
5a5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 54.
5a5e0 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 #define OP_Shift
5a5f0 52 69 67 68 74 20 20 20 20 20 20 20 20 20 20 20 Right
5a600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
5a610 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 7 /* same as T
5a620 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 K_RSHIFT */.#d
5a630 65 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f efine OP_ResetCo
5a640 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 unt
5a650 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 55.
5a660 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f 57 #define OP_FifoW
5a670 72 69 74 65 20 20 20 20 20 20 20 20 20 20 20 20 rite
5a680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
5a690 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 6.#define OP_Con
5a6a0 74 65 78 74 50 75 73 68 20 20 20 20 20 20 20 20 textPush
5a6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a6c0 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59 57.#define OP_Y
5a6d0 69 65 6c 64 20 20 20 20 20 20 20 20 20 20 20 20 ield
5a6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a6f0 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f 50 58.#define OP
5a700 5f 44 72 6f 70 54 72 69 67 67 65 72 20 20 20 20 _DropTrigger
5a710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a720 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 20 59.#define
5a730 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 20 OP_DropIndex
5a740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a750 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 62.#defin
5a760 65 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20 20 e OP_IdxGE
5a770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a780 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 66 63.#def
5a790 69 6e 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 ine OP_IdxDelete
5a7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a7b0 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23 64 64.#d
5a7c0 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20 efine OP_Vacuum
5a7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 33 0a 73.
5a7f0 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 4c #define OP_MoveL
5a800 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
5a810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
5a820 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4e 4.#define OP_IfN
5a830 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ot
5a840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a850 20 38 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 85.#define OP_D
5a860 72 6f 70 54 61 62 6c 65 20 20 20 20 20 20 20 20 ropTable
5a870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a880 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20 4f 50 86.#define OP
5a890 5f 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20 20 _MakeRecord
5a8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a8b0 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 20 89.#define
5a8c0 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 20 OP_ToBlob
5a8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a8e0 20 20 20 20 20 20 31 34 30 20 20 20 2f 2a 20 73 140 /* s
5a8f0 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f ame as TK_TO_BLO
5a900 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 B */.#define OP
5a910 5f 52 65 73 75 6c 74 52 6f 77 20 20 20 20 20 20 _ResultRow
5a920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a930 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20 90.#define
5a940 4f 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20 OP_Delete
5a950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a960 20 20 20 20 20 20 20 39 31 0a 23 64 65 66 69 6e 91.#defin
5a970 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 e OP_AggFinal
5a980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a990 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66 92.#def
5a9a0 69 6e 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 20 ine OP_Compare
5a9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a9c0 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 64 93.#d
5a9d0 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 efine OP_ShiftLe
5a9e0 66 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ft
5a9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 20 76
5aa00 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
5aa10 4c 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 LSHIFT */.#def
5aa20 69 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 ine OP_Goto
5aa30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aa40 20 20 20 20 20 20 20 20 20 20 20 39 34 0a 23 64 94.#d
5aa50 65 66 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f efine OP_TableLo
5aa60 63 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ck
5aa70 20 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a 95.
5aa80 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f 52 #define OP_FifoR
5aa90 65 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 ead
5aaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
5aab0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 65 6.#define OP_Cle
5aac0 61 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ar
5aad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aae0 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 97.#define OP_M
5aaf0 6f 76 65 4c 74 20 20 20 20 20 20 20 20 20 20 20 oveLt
5ab00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ab10 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f 50 98.#define OP
5ab20 5f 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 _Le
5ab30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ab40 20 20 20 20 20 37 30 20 20 20 2f 2a 20 73 61 6d 70 /* sam
5ab50 65 20 61 73 20 54 4b 5f 4c 45 20 20 20 20 20 20 e as TK_LE
5ab60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 */.#define OP_V
5ab70 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20 20 erifyCookie
5ab80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ab90 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 4f 50 99.#define OP
5aba0 5f 41 67 67 53 74 65 70 20 20 20 20 20 20 20 20 _AggStep
5abb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5abc0 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20 100.#define
5abd0 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20 20 OP_ToText
5abe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5abf0 20 20 20 20 20 20 31 33 39 20 20 20 2f 2a 20 73 139 /* s
5ac00 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 ame as TK_TO_TEX
5ac10 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 T */.#define OP
5ac20 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 _Not
5ac30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ac40 20 20 20 20 20 31 36 20 20 20 2f 2a 20 73 61 6d 16 /* sam
5ac50 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20 20 e as TK_NOT
5ac60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 */.#define OP_T
5ac70 6f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 oReal
5ac80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ac90 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20 143 /* same
5aca0 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a as TK_TO_REAL *
5acb0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 /.#define OP_Set
5acc0 4e 75 6d 43 6f 6c 75 6d 6e 73 20 20 20 20 20 20 NumColumns
5acd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ace0 31 30 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 101.#define OP_T
5acf0 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 20 ransaction
5ad00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ad10 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 4f 50 102.#define OP
5ad20 5f 56 46 69 6c 74 65 72 20 20 20 20 20 20 20 20 _VFilter
5ad30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ad40 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 20 103.#define
5ad50 4f 50 5f 4e 65 20 20 20 20 20 20 20 20 20 20 20 OP_Ne
5ad60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ad70 20 20 20 20 20 20 20 36 37 20 20 20 2f 2a 20 73 67 /* s
5ad80 61 6d 65 20 61 73 20 54 4b 5f 4e 45 20 20 20 20 ame as TK_NE
5ad90 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
5ada0 5f 56 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 _VDestroy
5adb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5adc0 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20 104.#define
5add0 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 20 20 20 OP_ContextPop
5ade0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5adf0 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e 105.#defin
5ae00 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20 20 e OP_BitOr
5ae10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ae20 20 20 20 20 20 20 20 20 20 37 35 20 20 20 2f 2a 75 /*
5ae30 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f same as TK_BITO
5ae40 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 R */.#define
5ae50 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 20 20 20 OP_Next
5ae60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ae70 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 106.#defin
5ae80 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 20 20 e OP_IdxInsert
5ae90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aea0 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 107.#def
5aeb0 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20 20 20 ine OP_Lt
5aec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aed0 20 20 20 20 20 20 20 20 20 20 20 37 31 20 20 20 71
5aee0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 /* same as TK_LT
5aef0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
5af00 65 20 4f 50 5f 49 6e 73 65 72 74 20 20 20 20 20 e OP_Insert
5af10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af20 20 20 20 20 20 20 20 20 31 30 38 0a 23 64 65 66 108.#def
5af30 69 6e 65 20 4f 50 5f 44 65 73 74 72 6f 79 20 20 ine OP_Destroy
5af40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af50 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 64 109.#d
5af60 65 66 69 6e 65 20 4f 50 5f 52 65 61 64 43 6f 6f efine OP_ReadCoo
5af70 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 kie
5af80 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 0a 110.
5af90 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 72 63 65 #define OP_Force
5afa0 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 Int
5afb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
5afc0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 6f 61 1.#define OP_Loa
5afd0 64 41 6e 61 6c 79 73 69 73 20 20 20 20 20 20 20 dAnalysis
5afe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aff0 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 112.#define OP_E
5b000 78 70 6c 61 69 6e 20 20 20 20 20 20 20 20 20 20 xplain
5b010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b020 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 113.#define OP
5b030 5f 4f 70 65 6e 50 73 65 75 64 6f 20 20 20 20 20 _OpenPseudo
5b040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b050 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 20 114.#define
5b060 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
5b070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b080 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e 115.#defin
5b090 65 20 4f 50 5f 4e 75 6c 6c 20 20 20 20 20 20 20 e OP_Null
5b0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b0b0 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65 66 116.#def
5b0c0 69 6e 65 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20 ine OP_Move
5b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b0e0 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 64 117.#d
5b0f0 65 66 69 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 20 efine OP_Blob
5b100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b110 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 0a 118.
5b120 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 20 20 #define OP_Add
5b130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
5b150 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 8 /* same as T
5b160 4b 5f 50 4c 55 53 20 20 20 20 20 2a 2f 0a 23 64 K_PLUS */.#d
5b170 65 66 69 6e 65 20 4f 50 5f 52 65 77 69 6e 64 20 efine OP_Rewind
5b180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b190 20 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 119.
5b1a0 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 47 #define OP_MoveG
5b1b0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
5b1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
5b1d0 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 42 65 0.#define OP_VBe
5b1e0 67 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 gin
5b1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b200 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 121.#define OP_V
5b210 55 70 64 61 74 65 20 20 20 20 20 20 20 20 20 20 Update
5b220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b230 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 122.#define OP
5b240 5f 49 66 5a 65 72 6f 20 20 20 20 20 20 20 20 20 _IfZero
5b250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b260 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 123.#define
5b270 4f 50 5f 42 69 74 4e 6f 74 20 20 20 20 20 20 20 OP_BitNot
5b280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b290 20 20 20 20 20 20 20 38 37 20 20 20 2f 2a 20 73 87 /* s
5b2a0 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 ame as TK_BITNOT
5b2b0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
5b2c0 5f 56 43 72 65 61 74 65 20 20 20 20 20 20 20 20 _VCreate
5b2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b2e0 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 20 124.#define
5b2f0 4f 50 5f 46 6f 75 6e 64 20 20 20 20 20 20 20 20 OP_Found
5b300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b310 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e 125.#defin
5b320 65 20 4f 50 5f 49 66 50 6f 73 20 20 20 20 20 20 e OP_IfPos
5b330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b340 20 20 20 20 20 20 20 20 31 32 37 0a 23 64 65 66 127.#def
5b350 69 6e 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 ine OP_NullRow
5b360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b370 20 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 128.#d
5b380 65 66 69 6e 65 20 4f 50 5f 4a 75 6d 70 20 20 20 efine OP_Jump
5b390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b3a0 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 129.
5b3b0 23 64 65 66 69 6e 65 20 4f 50 5f 50 65 72 6d 75 #define OP_Permu
5b3c0 74 61 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 tation
5b3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
5b3e0 30 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 0../* The follow
5b3f0 69 6e 67 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 ing opcode value
5b400 73 20 61 72 65 20 6e 65 76 65 72 20 75 73 65 64 s are never used
5b410 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e */.#define OP_N
5b420 6f 74 55 73 65 64 5f 31 33 31 20 20 20 20 20 20 otUsed_131
5b430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b440 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 131.#define OP
5b450 5f 4e 6f 74 55 73 65 64 5f 31 33 32 20 20 20 20 _NotUsed_132
5b460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b470 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20 132.#define
5b480 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 33 20 20 OP_NotUsed_133
5b490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b4a0 20 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e 133.#defin
5b4b0 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 34 e OP_NotUsed_134
5b4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b4d0 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 66 134.#def
5b4e0 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 ine OP_NotUsed_1
5b4f0 33 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35
5b500 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64 135.#d
5b510 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 efine OP_NotUsed
5b520 5f 31 33 36 20 20 20 20 20 20 20 20 20 20 20 20 _136
5b530 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a 136.
5b540 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 #define OP_NotUs
5b550 65 64 5f 31 33 37 20 20 20 20 20 20 20 20 20 20 ed_137
5b560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
5b570 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 7.#define OP_Not
5b580 55 73 65 64 5f 31 33 38 20 20 20 20 20 20 20 20 Used_138
5b590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b5a0 31 33 38 0a 0a 0a 2f 2a 20 50 72 6f 70 65 72 74 138.../* Propert
5b5b0 69 65 73 20 73 75 63 68 20 61 73 20 22 6f 75 74 ies such as "out
5b5c0 32 22 20 6f 72 20 22 6a 75 6d 70 22 20 74 68 61 2" or "jump" tha
5b5d0 74 20 61 72 65 20 73 70 65 63 69 66 69 65 64 20 t are specified
5b5e0 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 66 in.** comments f
5b5f0 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 ollowing the "ca
5b600 73 65 22 20 66 6f 72 20 65 61 63 68 20 6f 70 63 se" for each opc
5b610 6f 64 65 20 69 6e 20 74 68 65 20 76 64 62 65 2e ode in the vdbe.
5b620 63 0a 2a 2a 20 61 72 65 20 65 6e 63 6f 64 65 64 c.** are encoded
5b630 20 69 6e 74 6f 20 62 69 74 76 65 63 74 6f 72 73 into bitvectors
5b640 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a as follows:.*/.
5b650 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4a 55 #define OPFLG_JU
5b660 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 30 78 MP 0x
5b670 30 30 30 31 20 20 2f 2a 20 6a 75 6d 70 3a 20 20 0001 /* jump:
5b680 50 32 20 68 6f 6c 64 73 20 6a 6d 70 20 74 61 72 P2 holds jmp tar
5b690 67 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f get */.#define O
5b6a0 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c PFLG_OUT2_PREREL
5b6b0 45 41 53 45 20 30 78 30 30 30 32 20 20 2f 2a 20 EASE 0x0002 /*
5b6c0 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 3a out2-prerelease:
5b6d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c */.#define OPFL
5b6e0 47 5f 49 4e 31 20 20 20 20 20 20 20 20 20 20 20 G_IN1
5b6f0 20 20 30 78 30 30 30 34 20 20 2f 2a 20 69 6e 31 0x0004 /* in1
5b700 3a 20 20 20 50 31 20 69 73 20 61 6e 20 69 6e 70 : P1 is an inp
5b710 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 ut */.#define OP
5b720 46 4c 47 5f 49 4e 32 20 20 20 20 20 20 20 20 20 FLG_IN2
5b730 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 69 0x0008 /* i
5b740 6e 32 3a 20 20 20 50 32 20 69 73 20 61 6e 20 69 n2: P2 is an i
5b750 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nput */.#define
5b760 4f 50 46 4c 47 5f 49 4e 33 20 20 20 20 20 20 20 OPFLG_IN3
5b770 20 20 20 20 20 20 30 78 30 30 31 30 20 20 2f 2a 0x0010 /*
5b780 20 69 6e 33 3a 20 20 20 50 33 20 69 73 20 61 6e in3: P3 is an
5b790 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e input */.#defin
5b7a0 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 20 20 20 e OPFLG_OUT3
5b7b0 20 20 20 20 20 20 20 20 30 78 30 30 32 30 20 20 0x0020
5b7c0 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 69 73 20 /* out3: P3 is
5b7d0 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64 65 an output */.#de
5b7e0 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 fine OPFLG_INITI
5b7f0 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20 30 ALIZER {\./* 0
5b800 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 */ 0x00, 0x01,
5b810 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 31 30 0x00, 0x00, 0x10
5b820 2c 20 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78 , 0x02, 0x11, 0x
5b830 30 30 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20 30 00,\./* 8 */ 0
5b840 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35 2c x00, 0x00, 0x05,
5b850 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 0x02, 0x00, 0x0
5b860 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 0, 0x00, 0x00,\.
5b870 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 34 2c 20 /* 16 */ 0x04,
5b880 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 31 0x02, 0x00, 0x01
5b890 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
5b8a0 30 30 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 32 00, 0x05,\./* 2
5b8b0 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 34 2c 4 */ 0x00, 0x04,
5b8c0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
5b8d0 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 2, 0x04, 0x00, 0
5b8e0 78 30 30 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f 20 x00,\./* 32 */
5b8f0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32 0x00, 0x00, 0x02
5b900 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 , 0x11, 0x11, 0x
5b910 30 32 2c 20 30 78 30 35 2c 20 30 78 30 30 2c 5c 02, 0x05, 0x00,\
5b920 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 30 32 2c ./* 40 */ 0x02,
5b930 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78 30 0x11, 0x04, 0x0
5b940 30 2c 20 30 78 30 30 2c 20 30 78 30 63 2c 20 30 0, 0x00, 0x0c, 0
5b950 78 31 31 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 20 x11, 0x01,\./*
5b960 34 38 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 31 48 */ 0x02, 0x01
5b970 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 , 0x00, 0x02, 0x
5b980 30 31 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 01, 0x01, 0x02,
5b990 30 78 30 30 2c 5c 0a 2f 2a 20 20 35 36 20 2a 2f 0x00,\./* 56 */
5b9a0 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30 0x04, 0x00, 0x0
5b9b0 30 2c 20 30 78 30 30 2c 20 30 78 32 63 2c 20 30 0, 0x00, 0x2c, 0
5b9c0 78 32 63 2c 20 30 78 30 30 2c 20 30 78 31 31 2c x2c, 0x00, 0x11,
5b9d0 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 78 30 30 \./* 64 */ 0x00
5b9e0 2c 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 , 0x05, 0x05, 0x
5b9f0 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 15, 0x15, 0x15,
5ba00 30 78 31 35 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 0x15, 0x15,\./*
5ba10 20 37 32 20 2a 2f 20 30 78 31 35 2c 20 30 78 30 72 */ 0x15, 0x0
5ba20 30 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 0, 0x2c, 0x2c, 0
5ba30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c x2c, 0x2c, 0x2c,
5ba40 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 38 30 20 2a 0x2c,\./* 80 *
5ba50 2f 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 / 0x2c, 0x2c, 0x
5ba60 32 63 2c 20 30 78 32 63 2c 20 30 78 31 31 2c 20 2c, 0x2c, 0x11,
5ba70 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 34 0x05, 0x00, 0x04
5ba80 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 30 78 30 ,\./* 88 */ 0x0
5ba90 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 2, 0x00, 0x00, 0
5baa0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
5bab0 20 30 78 30 31 2c 20 30 78 30 30 2c 5c 0a 2f 2a 0x01, 0x00,\./*
5bac0 20 20 39 36 20 2a 2f 20 30 78 30 31 2c 20 30 78 96 */ 0x01, 0x
5bad0 30 30 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20 00, 0x11, 0x00,
5bae0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
5baf0 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 31 30 34 20 , 0x01,\./* 104
5bb00 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 */ 0x00, 0x00, 0
5bb10 78 30 31 2c 20 30 78 30 38 2c 20 30 78 30 30 2c x01, 0x08, 0x00,
5bb20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
5bb30 35 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20 30 78 5,\./* 112 */ 0x
5bb40 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
5bb50 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 30 0x00, 0x02, 0x00
5bb60 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 5c 0a 2f , 0x02, 0x01,\./
5bb70 2a 20 31 32 30 20 2a 2f 20 30 78 31 31 2c 20 30 * 120 */ 0x11, 0
5bb80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35 2c x00, 0x00, 0x05,
5bb90 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30 0x00, 0x11, 0x0
5bba0 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 31 32 38 2, 0x05,\./* 128
5bbb0 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 */ 0x00, 0x01,
5bbc0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
5bbd0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
5bbe0 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20 30 00,\./* 136 */ 0
5bbf0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
5bc00 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78 30 0x04, 0x04, 0x0
5bc10 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 5c 0a 4, 0x04, 0x04,\.
5bc20 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
5bc30 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 ** End of opcode
5bc40 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.h ************
5bc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bc70 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
5bc80 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
5bc90 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
5bca0 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a in vdbe.h ******
5bcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5bcc0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 */../*.** Protot
5bcd0 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 ypes for the VDB
5bce0 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 E interface. Se
5bcf0 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 e comments on th
5bd00 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
5bd10 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 .** for a descri
5bd20 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 ption of what ea
5bd30 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 ch of these rout
5bd40 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 ines does..*/.SQ
5bd50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 LITE_PRIVATE Vdb
5bd60 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 e *sqlite3VdbeCr
5bd70 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a eate(sqlite3*);.
5bd80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5bd90 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
5bda0 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b dOp0(Vdbe*,int);
5bdb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5bdc0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
5bdd0 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c ddOp1(Vdbe*,int,
5bde0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
5bdf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5be00 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a VdbeAddOp2(Vdbe*
5be10 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ,int,int,int);.S
5be20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5be30 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
5be40 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e Op3(Vdbe*,int,in
5be50 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 t,int,int);.SQLI
5be60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5be70 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
5be80 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 (Vdbe*,int,int,i
5be90 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 nt,int,const cha
5bea0 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c r *zP4,int);.SQL
5beb0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
5bec0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
5bed0 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 List(Vdbe*, int
5bee0 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 nOp, VdbeOpList
5bef0 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c const *aOp);.SQL
5bf00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
5bf10 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
5bf20 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 geP1(Vdbe*, int
5bf30 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 addr, int P1);.S
5bf40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
5bf50 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
5bf60 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e angeP2(Vdbe*, in
5bf70 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b t addr, int P2);
5bf80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5bf90 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
5bfa0 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 ChangeP3(Vdbe*,
5bfb0 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33 int addr, int P3
5bfc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5bfd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
5bfe0 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a beChangeP5(Vdbe*
5bff0 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45 , u8 P5);.SQLITE
5c000 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
5c010 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
5c020 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 e(Vdbe*, int add
5c030 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 r);.SQLITE_PRIVA
5c040 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
5c050 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 dbeChangeToNoop(
5c060 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c Vdbe*, int addr,
5c070 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f int N);.SQLITE_
5c080 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
5c090 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 ite3VdbeChangeP4
5c0a0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 (Vdbe*, int addr
5c0b0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 , const char *zP
5c0c0 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 4, int N);.SQLIT
5c0d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
5c0e0 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 qlite3VdbeUsesBt
5c0f0 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b ree(Vdbe*, int);
5c100 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5c110 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 VdbeOp *sqlite3V
5c120 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 dbeGetOp(Vdbe*,
5c130 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
5c140 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5c150 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 VdbeMakeLabel(Vd
5c160 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
5c170 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
5c180 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 3VdbeDelete(Vdbe
5c190 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5c1a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
5c1b0 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62 dbeMakeReady(Vdb
5c1c0 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 e*,int,int,int,i
5c1d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
5c1e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
5c1f0 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 dbeFinalize(Vdbe
5c200 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5c210 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
5c220 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
5c230 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c Vdbe*, int);.SQL
5c240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
5c250 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
5c260 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 ntAddr(Vdbe*);.#
5c270 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
5c280 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
5c290 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
5c2a0 56 64 62 65 54 72 61 63 65 28 56 64 62 65 2a 2c VdbeTrace(Vdbe*,
5c2b0 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 FILE*);.#endif.S
5c2c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
5c2d0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 id sqlite3VdbeRe
5c2e0 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 64 setStepResult(Vd
5c2f0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
5c300 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5c310 56 64 62 65 52 65 73 65 74 28 56 64 62 65 2a 29 VdbeReset(Vdbe*)
5c320 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5c330 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
5c340 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 eSetNumCols(Vdbe
5c350 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
5c360 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5c370 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 e3VdbeSetColName
5c380 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 (Vdbe*, int, int
5c390 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
5c3a0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
5c3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5c3c0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
5c3d0 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62 CountChanges(Vdb
5c3e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
5c3f0 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c ATE sqlite3 *sql
5c400 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65 2a ite3VdbeDb(Vdbe*
5c410 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5c420 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
5c430 62 65 53 65 74 53 71 6c 28 56 64 62 65 2a 2c 20 beSetSql(Vdbe*,
5c440 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
5c450 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt n);.SQLITE_PR
5c460 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
5c470 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 2a e3VdbeSwap(Vdbe*
5c480 2c 56 64 62 65 2a 29 3b 0a 0a 23 69 66 64 65 66 ,Vdbe*);..#ifdef
5c490 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
5c4a0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
5c4b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5c4c0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 int sqlite3VdbeR
5c4d0 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 eleaseMemory(int
5c4e0 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 );.#endif.SQLITE
5c4f0 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 _PRIVATE Unpacke
5c500 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 dRecord *sqlite3
5c510 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b VdbeRecordUnpack
5c520 28 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74 2c 63 6f (KeyInfo*,int,co
5c530 6e 73 74 20 76 6f 69 64 2a 2c 0a 20 20 20 20 20 nst void*,.
5c540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c560 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 UnpackedRecor
5c570 64 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f d*,int);.SQLITE_
5c580 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
5c590 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e ite3VdbeDeleteUn
5c5a0 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70 packedRecord(Unp
5c5b0 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 53 ackedRecord*);.S
5c5c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5c5d0 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 t sqlite3VdbeRec
5c5e0 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 2c 63 ordCompare(int,c
5c5f0 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 61 63 onst void*,Unpac
5c600 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a 0a 23 kedRecord*);...#
5c610 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 ifndef NDEBUG.SQ
5c620 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
5c630 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
5c640 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63 6f omment(Vdbe*, co
5c650 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
5c660 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43 6f .# define VdbeCo
5c670 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 mment(X) sqlite
5c680 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 0a 53 3VdbeComment X.S
5c690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
5c6a0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
5c6b0 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65 NoopComment(Vdbe
5c6c0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
5c6d0 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 ...);.# define V
5c6e0 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58 dbeNoopComment(X
5c6f0 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f ) sqlite3VdbeNo
5c700 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c 73 opComment X.#els
5c710 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43 e.# define VdbeC
5c720 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 66 69 omment(X).# defi
5c730 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 ne VdbeNoopComme
5c740 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 nt(X).#endif..#e
5c750 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
5c760 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
5c770 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
5c780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c7a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
5c7b0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
5c7c0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
5c7d0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
5c7e0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
5c7f0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
5c800 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 ***** Include pa
5c810 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 ger.h in the mid
5c820 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
5c830 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
5c840 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
5c850 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
5c860 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a pager.h *******
5c870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c890 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
5c8a0 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
5c8b0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
5c8c0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
5c8d0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
5c8e0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
5c8f0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
5c900 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
5c910 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
5c920 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
5c930 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
5c940 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
5c950 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
5c960 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
5c970 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
5c980 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
5c990 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
5c9a0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
5c9b0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
5c9c0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
5c9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5c9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5ca00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
5ca10 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 This header file
5ca20 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
5ca30 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 erface that the
5ca40 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 sqlite page cach
5ca50 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 e.** subsystem.
5ca60 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
5ca70 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 73 20 subsystem reads
5ca80 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 69 6c and writes a fil
5ca90 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 20 61 e a page.** at a
5caa0 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 69 64 time and provid
5cab0 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 es a journal for
5cac0 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
5cad0 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 72 @(#) $Id: pager
5cae0 2e 68 2c 76 20 31 2e 38 37 20 32 30 30 38 2f 31 .h,v 1.87 2008/1
5caf0 31 2f 31 39 20 31 30 3a 32 32 3a 33 33 20 64 61 1/19 10:22:33 da
5cb00 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
5cb10 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 */..#ifndef _PAG
5cb20 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 ER_H_.#define _P
5cb30 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 49 AGER_H_../*.** I
5cb40 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f 6e f defined as non
5cb50 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 75 -zero, auto-vacu
5cb60 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 79 um is enabled by
5cb70 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 77 default. Otherw
5cb80 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 62 ise.** it must b
5cb90 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 20 e turned on for
5cba0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75 73 each database us
5cbb0 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 6f ing "PRAGMA auto
5cbc0 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f _vacuum = 1"..*/
5cbd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
5cbe0 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f DEFAULT_JOURNAL_
5cbf0 53 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 SIZE_LIMIT. #de
5cc00 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 fine SQLITE_DEFA
5cc10 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 ULT_JOURNAL_SIZE
5cc20 5f 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 _LIMIT -1.#endif
5cc30 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 ../*.** The type
5cc40 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 used to represe
5cc50 6e 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 nt a page number
5cc60 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67 . The first pag
5cc70 65 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 e in a file.** i
5cc80 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e s called page 1.
5cc90 20 20 30 20 69 73 20 75 73 65 64 20 74 6f 20 72 0 is used to r
5cca0 65 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 epresent "not a
5ccb0 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 page"..*/.typede
5ccc0 66 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a f u32 Pgno;../*.
5ccd0 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c ** Each open fil
5cce0 65 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 e is managed by
5ccf0 61 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 61 a separate insta
5cd00 6e 63 65 20 6f 66 20 74 68 65 20 22 50 61 67 65 nce of the "Page
5cd10 72 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f r" structure..*/
5cd20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
5cd30 50 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a Pager Pager;../*
5cd40 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 .** Handle type
5cd50 66 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 for pages..*/.ty
5cd60 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 pedef struct PgH
5cd70 64 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a dr DbPage;../*.*
5cd80 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 * Allowed values
5cd90 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73 20 70 for the flags p
5cda0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
5cdb0 74 65 33 50 61 67 65 72 4f 70 65 6e 28 29 2e 0a te3PagerOpen()..
5cdc0 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54 68 69 73 **.** NOTE: This
5cdd0 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 values must mat
5cde0 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e ch the correspon
5cdf0 64 69 6e 67 20 42 54 52 45 45 5f 20 76 61 6c 75 ding BTREE_ valu
5ce00 65 73 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a 2a es in btree.h..*
5ce10 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
5ce20 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 30 78 OMIT_JOURNAL 0x
5ce30 30 30 30 31 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 0001 /* Do no
5ce40 74 20 75 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b t use a rollback
5ce50 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 journal */.#def
5ce60 69 6e 65 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 ine PAGER_NO_REA
5ce70 44 4c 4f 43 4b 20 20 20 30 78 30 30 30 32 20 20 DLOCK 0x0002
5ce80 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f /* Omit readlo
5ce90 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 cks on readonly
5cea0 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 files */../*.**
5ceb0 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72 Valid values for
5cec0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
5ced0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50 ment to sqlite3P
5cee0 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 agerLockingMode(
5cef0 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 )..*/.#define PA
5cf00 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
5cf10 51 55 45 52 59 20 20 20 20 20 20 2d 31 0a 23 64 QUERY -1.#d
5cf20 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b efine PAGER_LOCK
5cf30 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 20 INGMODE_NORMAL
5cf40 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 0.#define PA
5cf50 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
5cf60 45 58 43 4c 55 53 49 56 45 20 20 20 31 0a 0a 2f EXCLUSIVE 1../
5cf70 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65 *.** Valid value
5cf80 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 s for the second
5cf90 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
5cfa0 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c ite3PagerJournal
5cfb0 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 Mode()..*/.#defi
5cfc0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
5cfd0 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20 MODE_QUERY
5cfe0 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 -1.#define PAGER
5cff0 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c _JOURNALMODE_DEL
5d000 45 54 45 20 20 20 20 20 20 30 20 20 20 2f 2a 20 ETE 0 /*
5d010 43 6f 6d 6d 69 74 20 62 79 20 64 65 6c 65 74 69 Commit by deleti
5d020 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ng journal file
5d030 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
5d040 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 _JOURNALMODE_PER
5d050 53 49 53 54 20 20 20 20 20 31 20 20 20 2f 2a 20 SIST 1 /*
5d060 43 6f 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69 6e Commit by zeroin
5d070 67 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 g journal header
5d080 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
5d090 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
5d0a0 46 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a F 2 /*
5d0b0 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65 64 Journal omitted
5d0c0 2e 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 . */.#define PA
5d0d0 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
5d0e0 54 52 55 4e 43 41 54 45 20 20 20 20 33 20 20 20 TRUNCATE 3
5d0f0 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 74 72 75 /* Commit by tru
5d100 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 ncating journal
5d110 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
5d120 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
5d130 4f 52 59 20 20 20 20 20 20 34 20 20 20 2f 2a 20 ORY 4 /*
5d140 49 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 In-memory journa
5d150 6c 20 66 69 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a l file */../*.**
5d160 20 53 65 65 20 73 6f 75 72 63 65 20 63 6f 64 65 See source code
5d170 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 61 20 comments for a
5d180 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 detailed descrip
5d190 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 6c 6c tion of the foll
5d1a0 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e 65 owing.** routine
5d1b0 73 3a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s:.*/.SQLITE_PRI
5d1c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5d1d0 50 61 67 65 72 4f 70 65 6e 28 73 71 6c 69 74 65 PagerOpen(sqlite
5d1e0 33 5f 76 66 73 20 2a 2c 20 50 61 67 65 72 20 2a 3_vfs *, Pager *
5d1f0 2a 70 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 *ppPager, const
5d200 63 68 61 72 2a 2c 20 69 6e 74 2c 69 6e 74 2c 69 char*, int,int,i
5d210 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
5d220 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5d230 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 PagerSetBusyhand
5d240 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 ler(Pager*, int(
5d250 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 *)(void *), void
5d260 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
5d270 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5d280 50 61 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 PagerSetReiniter
5d290 28 50 61 67 65 72 2a 2c 20 76 6f 69 64 28 2a 29 (Pager*, void(*)
5d2a0 28 44 62 50 61 67 65 2a 29 29 3b 0a 53 51 4c 49 (DbPage*));.SQLI
5d2b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5d2c0 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 qlite3PagerSetPa
5d2d0 67 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20 75 gesize(Pager*, u
5d2e0 31 36 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 16*);.SQLITE_PRI
5d2f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5d300 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e PagerMaxPageCoun
5d310 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a t(Pager*, int);.
5d320 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5d330 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 nt sqlite3PagerR
5d340 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 61 eadFileheader(Pa
5d350 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e 73 69 67 ger*, int, unsig
5d360 6e 65 64 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 ned char*);.SQLI
5d370 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
5d380 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 sqlite3PagerSetC
5d390 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c achesize(Pager*,
5d3a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
5d3b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5d3c0 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65 3PagerClose(Page
5d3d0 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c 49 r *pPager);.SQLI
5d3e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5d3f0 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 qlite3PagerAcqui
5d400 72 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 re(Pager *pPager
5d410 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 , Pgno pgno, DbP
5d420 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e age **ppPage, in
5d430 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 t clrFlag);.#def
5d440 69 6e 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 ine sqlite3Pager
5d450 47 65 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 Get(A,B,C) sqlit
5d460 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 41 e3PagerAcquire(A
5d470 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 ,B,C,0).SQLITE_P
5d480 52 49 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 RIVATE DbPage *s
5d490 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
5d4a0 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c p(Pager *pPager,
5d4b0 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c Pgno pgno);.SQL
5d4c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
5d4d0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
5d4e0 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 2a Refcount(DbPage*
5d4f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5d500 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
5d510 65 72 52 65 66 28 44 62 50 61 67 65 2a 29 3b 0a erRef(DbPage*);.
5d520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5d530 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 nt sqlite3PagerU
5d540 6e 72 65 66 28 44 62 50 61 67 65 2a 29 3b 0a 53 nref(DbPage*);.S
5d550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5d560 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 t sqlite3PagerWr
5d570 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 ite(DbPage*);.SQ
5d580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5d590 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
5d5a0 65 63 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 ecount(Pager*, i
5d5b0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
5d5c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5d5d0 50 61 67 65 72 54 72 75 6e 63 61 74 65 28 50 61 PagerTruncate(Pa
5d5e0 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a 53 51 4c 49 ger*,Pgno);.SQLI
5d5f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5d600 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e qlite3PagerBegin
5d610 28 44 62 50 61 67 65 2a 2c 20 69 6e 74 20 65 78 (DbPage*, int ex
5d620 46 6c 61 67 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Flag);.SQLITE_PR
5d630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5d640 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
5d650 65 4f 6e 65 28 50 61 67 65 72 2a 2c 63 6f 6e 73 eOne(Pager*,cons
5d660 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c t char *zMaster,
5d670 20 50 67 6e 6f 2c 20 69 6e 74 29 3b 0a 53 51 4c Pgno, int);.SQL
5d680 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
5d690 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
5d6a0 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72 itPhaseTwo(Pager
5d6b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5d6c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
5d6d0 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 gerRollback(Page
5d6e0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
5d6f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
5d700 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 agerIsreadonly(P
5d710 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
5d720 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5d730 65 33 50 61 67 65 72 53 74 6d 74 42 65 67 69 6e e3PagerStmtBegin
5d740 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
5d750 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
5d760 69 74 65 33 50 61 67 65 72 53 74 6d 74 43 6f 6d ite3PagerStmtCom
5d770 6d 69 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c mit(Pager*);.SQL
5d780 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
5d790 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 sqlite3PagerStmt
5d7a0 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 2a 29 Rollback(Pager*)
5d7b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5d7c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
5d7d0 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 44 erDontRollback(D
5d7e0 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f bPage*);.SQLITE_
5d7f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
5d800 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 te3PagerDontWrit
5d810 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 e(DbPage*);.SQLI
5d820 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5d830 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
5d840 75 6e 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c unt(Pager*);.SQL
5d850 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
5d860 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
5d870 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65 SafetyLevel(Page
5d880 72 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c r*,int,int);.SQL
5d890 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
5d8a0 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 t char *sqlite3P
5d8b0 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 agerFilename(Pag
5d8c0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
5d8d0 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 VATE const sqlit
5d8e0 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 e3_vfs *sqlite3P
5d8f0 61 67 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b agerVfs(Pager*);
5d900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5d910 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 sqlite3_file *sq
5d920 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 lite3PagerFile(P
5d930 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
5d940 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
5d950 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 44 r *sqlite3PagerD
5d960 69 72 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a irname(Pager*);.
5d970 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
5d980 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
5d990 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 e3PagerJournalna
5d9a0 6d 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 me(Pager*);.SQLI
5d9b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5d9c0 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e qlite3PagerNosyn
5d9d0 63 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 c(Pager*);.SQLIT
5d9e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
5d9f0 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 lite3PagerMovepa
5da00 67 65 28 50 61 67 65 72 2a 2c 44 62 50 61 67 65 ge(Pager*,DbPage
5da10 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 0a 53 51 4c *,Pgno,int);.SQL
5da20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
5da30 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *sqlite3PagerGe
5da40 74 44 61 74 61 28 44 62 50 61 67 65 20 2a 29 3b tData(DbPage *);
5da50 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 .SQLITE_PRIVATE
5da60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
5da70 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50 61 gerGetExtra(DbPa
5da80 67 65 20 2a 29 3b 20 0a 53 51 4c 49 54 45 5f 50 ge *); .SQLITE_P
5da90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5daa0 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f e3PagerLockingMo
5dab0 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 de(Pager *, int)
5dac0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5dad0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
5dae0 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 rJournalMode(Pag
5daf0 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 er *, int);.SQLI
5db00 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 TE_PRIVATE i64 s
5db10 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
5db20 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 alSizeLimit(Page
5db30 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 r *, i64);.SQLIT
5db40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
5db50 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 sqlite3PagerTemp
5db60 53 70 61 63 65 28 50 61 67 65 72 2a 29 3b 0a 53 Space(Pager*);.S
5db70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5db80 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 t sqlite3PagerSy
5db90 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nc(Pager *pPager
5dba0 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
5dbb0 45 5f 48 41 53 5f 43 4f 44 45 43 0a 53 51 4c 49 E_HAS_CODEC.SQLI
5dbc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
5dbd0 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 d sqlite3PagerSe
5dbe0 74 43 6f 64 65 63 28 50 61 67 65 72 2a 2c 76 6f tCodec(Pager*,vo
5dbf0 69 64 2a 28 2a 29 28 76 6f 69 64 2a 2c 76 6f 69 id*(*)(void*,voi
5dc00 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 76 6f 69 d*,Pgno,int),voi
5dc10 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 d*);.#endif..#if
5dc20 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
5dc30 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
5dc40 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45 ITE_TEST).SQLITE
5dc50 5f 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20 _PRIVATE Pgno
5dc60 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
5dc70 6e 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b number(DbPage*);
5dc80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5dc90 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 int sqlite3Pag
5dca0 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 erIswriteable(Db
5dcb0 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a Page*);.#endif..
5dcc0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
5dcd0 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ST.SQLITE_PRIVAT
5dce0 45 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 E int *sqlite3
5dcf0 50 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72 PagerStats(Pager
5dd00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5dd10 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
5dd20 33 50 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 3PagerRefdump(Pa
5dd30 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
5dd40 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
5dd50 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 te3PagerIsMemdb(
5dd60 50 61 67 65 72 2a 29 3b 0a 23 65 6e 64 69 66 0a Pager*);.#endif.
5dd70 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
5dd80 45 53 54 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 EST.void disable
5dd90 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
5dda0 72 6f 72 73 28 76 6f 69 64 29 3b 0a 76 6f 69 64 rors(void);.void
5ddb0 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 enable_simulate
5ddc0 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 d_io_errors(void
5ddd0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
5dde0 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 e disable_simula
5ddf0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a ted_io_errors().
5de00 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f # define enable_
5de10 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
5de20 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23 65 ors().#endif..#e
5de30 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48 ndif /* _PAGER_H
5de40 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a _ */../*********
5de50 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 ***** End of pag
5de60 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.h ***********
5de70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5de80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5de90 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
5dea0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
5deb0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
5dec0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
5ded0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
5dee0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
5def0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 63 ***** Include pc
5df00 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 ache.h in the mi
5df10 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e ddle of sqliteIn
5df20 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
5df30 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
5df40 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
5df50 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a pcache.h ******
5df60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5df70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5df80 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
5df90 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a August 05.**.**
5dfa0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
5dfb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
5dfc0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
5dfd0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
5dfe0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
5dff0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
5e000 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
5e010 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
5e020 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
5e030 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
5e040 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
5e050 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
5e060 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
5e070 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
5e080 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
5e090 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
5e0a0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
5e0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
5e100 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 s header file de
5e110 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 fines the interf
5e120 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c ace that the sql
5e130 69 74 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a ite page cache.*
5e140 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a 2a * subsystem. .**
5e150 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63 .** @(#) $Id: pc
5e160 61 63 68 65 2e 68 2c 76 20 31 2e 31 36 20 32 30 ache.h,v 1.16 20
5e170 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 3a 34 08/11/19 16:52:4
5e180 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 4 danielk1977 Ex
5e190 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 p $.*/..#ifndef
5e1a0 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65 _PCACHE_H_..type
5e1b0 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 def struct PgHdr
5e1c0 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20 PgHdr;.typedef
5e1d0 73 74 72 75 63 74 20 50 43 61 63 68 65 20 50 43 struct PCache PC
5e1e0 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 ache;../*.** Eve
5e1f0 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 ry page in the c
5e200 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c ache is controll
5e210 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 ed by an instanc
5e220 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
5e230 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ng.** structure.
5e240 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72 .*/.struct PgHdr
5e250 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 {. void *pData
5e260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5e270 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f /* Content o
5e280 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 f this page */.
5e290 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b 20 20 void *pExtra;
5e2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e2b0 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 6e 74 /* Extra content
5e2c0 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 */. PgHdr *pDi
5e2d0 72 74 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 rty;
5e2e0 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 65 6e /* Transien
5e2f0 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 t list of dirty
5e300 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 pages */. Pgno
5e310 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 pgno;
5e320 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
5e330 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 e number for thi
5e340 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 67 65 s page */. Page
5e350 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 r *pPager;
5e360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
5e370 65 20 70 61 67 65 72 20 74 68 69 73 20 70 61 67 e pager this pag
5e380 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a e is part of */.
5e390 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 #ifdef SQLITE_CH
5e3a0 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 32 20 ECK_PAGES. u32
5e3b0 70 61 67 65 48 61 73 68 3b 20 20 20 20 20 20 20 pageHash;
5e3c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 /* Ha
5e3d0 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e 74 65 sh of page conte
5e3e0 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 nt */.#endif. u
5e3f0 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 16 flags;
5e400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5e410 20 50 47 48 44 52 20 66 6c 61 67 73 20 64 65 66 PGHDR flags def
5e420 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20 ined below */..
5e430 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /**************
5e440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e470 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c ********. ** El
5e480 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 72 65 ements above are
5e490 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74 68 public. All th
5e4a0 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70 72 at follows is pr
5e4b0 69 76 61 74 65 20 74 6f 20 70 63 61 63 68 65 2e ivate to pcache.
5e4c0 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75 6c c. ** and shoul
5e4d0 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65 d not be accesse
5e4e0 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c d by other modul
5e4f0 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20 6e es.. */. i16 n
5e500 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
5e510 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
5e520 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 ber of users of
5e530 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 this page */. P
5e540 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 Cache *pCache;
5e550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5e560 20 43 61 63 68 65 20 74 68 61 74 20 6f 77 6e 73 Cache that owns
5e570 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a 20 this page */..
5e580 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 4e 65 PgHdr *pDirtyNe
5e590 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 xt;
5e5a0 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74 20 /* Next element
5e5b0 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 in list of dirty
5e5c0 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64 pages */. PgHd
5e5d0 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b 20 20 r *pDirtyPrev;
5e5e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 /* Pr
5e5f0 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20 69 evious element i
5e600 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 n list of dirty
5e610 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 pages */.};../*
5e620 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72 20 50 Bit values for P
5e630 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23 64 gHdr.flags */.#d
5e640 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54 efine PGHDR_DIRT
5e650 59 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 Y 0x
5e660 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73 002 /* Page has
5e670 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 changed */.#def
5e680 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 ine PGHDR_NEED_S
5e690 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 30 30 YNC 0x00
5e6a0 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20 4 /* Fsync the
5e6b0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
5e6c0 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20 before.
5e6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
5e6f0 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 70 * writing this p
5e700 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 age to the datab
5e710 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ase */.#define P
5e720 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20 GHDR_NEED_READ
5e730 20 20 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a 0x008 /*
5e740 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65 Content is unre
5e750 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 ad */.#define PG
5e760 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45 HDR_REUSE_UNLIKE
5e770 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20 LY 0x010 /*
5e780 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 75 73 A hint that reus
5e790 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f e is unlikely */
5e7a0 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 .#define PGHDR_D
5e7b0 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20 20 ONT_WRITE
5e7c0 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f 0x020 /* Do no
5e7d0 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 t write content
5e7e0 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 to disk */../* I
5e7f0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 nitialize and sh
5e800 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 utdown the page
5e810 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 cache subsystem
5e820 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
5e830 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
5e840 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f cheInitialize(vo
5e850 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
5e860 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5e870 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 PcacheShutdown(v
5e880 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63 oid);../* Page c
5e890 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61 ache buffer mana
5e8a0 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65 gement:.** These
5e8b0 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d routines implem
5e8c0 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ent SQLITE_CONFI
5e8d0 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a G_PAGECACHE..*/.
5e8e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5e8f0 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 oid sqlite3PCach
5e900 65 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69 eBufferSetup(voi
5e910 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 d *, int sz, int
5e920 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20 n);../* Create
5e930 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 63 68 a new pager cach
5e940 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f e..** Under memo
5e950 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b ry stress, invok
5e960 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 72 79 e xStress to try
5e970 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 to make pages c
5e980 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c lean..** Only cl
5e990 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64 ean and unpinned
5e9a0 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 72 65 pages can be re
5e9b0 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 claimed..*/.SQLI
5e9c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
5e9d0 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 sqlite3PcacheOpe
5e9e0 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c n(. int szPage,
5e9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ea00 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 /* Size of e
5ea10 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 very page */. i
5ea20 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20 nt szExtra,
5ea30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5ea40 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 Extra space ass
5ea50 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 ociated with eac
5ea60 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 h page */. int
5ea70 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 bPurgeable,
5ea80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
5ea90 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20 ue if pages are
5eaa0 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 on backing store
5eab0 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 */. int (*xStr
5eac0 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64 ess)(void*, PgHd
5ead0 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 r*), /* Call to
5eae0 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 try to make page
5eaf0 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 s clean */. voi
5eb00 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 d *pStress,
5eb10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5eb20 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 rgument to xStre
5eb30 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a ss */. PCache *
5eb40 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20 pToInit
5eb50 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c /* Preall
5eb60 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 ocated space for
5eb70 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 the PCache */.)
5eb80 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 ;../* Modify the
5eb90 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72 page-size after
5eba0 20 74 68 65 20 63 61 63 68 65 20 68 61 73 20 62 the cache has b
5ebb0 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a een created. */.
5ebc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5ebd0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
5ebe0 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 eSetPageSize(PCa
5ebf0 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a che *, int);../*
5ec00 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 Return the size
5ec10 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 in bytes of a P
5ec20 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55 Cache object. U
5ec30 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61 sed to prealloca
5ec40 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70 te.** storage sp
5ec50 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ace..*/.SQLITE_P
5ec60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5ec70 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 e3PcacheSize(voi
5ec80 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65 d);../* One rele
5ec90 61 73 65 20 70 65 72 20 73 75 63 63 65 73 73 66 ase per successf
5eca0 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65 20 ul fetch. Page
5ecb0 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20 is pinned until
5ecc0 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66 released..** Ref
5ecd0 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20 erence counted.
5ece0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
5ecf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
5ed00 61 63 68 65 46 65 74 63 68 28 50 43 61 63 68 65 acheFetch(PCache
5ed10 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65 *, Pgno, int cre
5ed20 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a ateFlag, PgHdr**
5ed30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5ed40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
5ed50 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 acheRelease(PgHd
5ed60 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 r*);..SQLITE_PRI
5ed70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
5ed80 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 3PcacheDrop(PgHd
5ed90 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 r*); /*
5eda0 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d Remove page from
5edb0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 cache */.SQLITE
5edc0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
5edd0 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
5ede0 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20 irty(PgHdr*);
5edf0 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61 /* Make sure pa
5ee00 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72 ge is marked dir
5ee10 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ty */.SQLITE_PRI
5ee20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
5ee30 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 3PcacheMakeClean
5ee40 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 (PgHdr*); /*
5ee50 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61 Mark a single pa
5ee60 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53 ge as clean */.S
5ee70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
5ee80 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
5ee90 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a CleanAll(PCache*
5eea0 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c ); /* Mark al
5eeb0 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 61 67 l dirty list pag
5eec0 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a es as clean */..
5eed0 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67 65 /* Change a page
5eee0 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62 number. Used b
5eef0 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a y incr-vacuum. *
5ef00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
5ef10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
5ef20 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20 cheMove(PgHdr*,
5ef30 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 Pgno);../* Remov
5ef40 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68 e all pages with
5ef50 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20 pgno>x. Reset
5ef60 74 68 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d the cache if x==
5ef70 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 0 */.SQLITE_PRIV
5ef80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5ef90 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 PcacheTruncate(P
5efa0 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b Cache*, Pgno x);
5efb0 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20 ../* Get a list
5efc0 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 of all dirty pag
5efd0 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c es in the cache,
5efe0 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 sorted by page
5eff0 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45 number */.SQLITE
5f000 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a _PRIVATE PgHdr *
5f010 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 sqlite3PcacheDir
5f020 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b tyList(PCache*);
5f030 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63 ../* Reset and c
5f040 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 6f lose the cache o
5f050 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f bject */.SQLITE_
5f060 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
5f070 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 ite3PcacheClose(
5f080 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c PCache*);../* Cl
5f090 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70 ear flags from p
5f0a0 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 ages of the page
5f0b0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 cache */.SQLITE
5f0c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
5f0d0 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 lite3PcacheClear
5f0e0 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 SyncFlags(PCache
5f0f0 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72 64 *);../* Discard
5f100 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
5f110 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 the cache */.SQ
5f120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5f130 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c sqlite3PcacheCl
5f140 65 61 72 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f ear(PCache*);../
5f150 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 * Return the tot
5f160 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 al number of out
5f170 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 standing page re
5f180 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51 4c 49 ferences */.SQLI
5f190 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5f1a0 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
5f1b0 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a ount(PCache*);..
5f1c0 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 /* Increment the
5f1d0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
5f1e0 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 of an existing
5f1f0 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 page */.SQLITE_P
5f200 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
5f210 74 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48 te3PcacheRef(PgH
5f220 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 dr*);..SQLITE_PR
5f230 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5f240 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 3PcachePageRefco
5f250 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a 2f 2a unt(PgHdr*);../*
5f260 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 Return the tota
5f270 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
5f280 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
5f290 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f cache */.SQLITE_
5f2a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
5f2b0 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 te3PcachePagecou
5f2c0 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 23 69 nt(PCache*);..#i
5f2d0 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 fdef SQLITE_CHEC
5f2e0 4b 5f 50 41 47 45 53 0a 2f 2a 20 49 74 65 72 61 K_PAGES./* Itera
5f2f0 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 64 te through all d
5f300 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 irty pages curre
5f310 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 74 ntly stored in t
5f320 68 65 20 63 61 63 68 65 2e 20 54 68 69 73 0a 2a he cache. This.*
5f330 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f * interface is o
5f340 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 nly available if
5f350 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
5f360 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 77 GES is defined w
5f370 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62 72 hen the .** libr
5f380 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a 2f ary is built..*/
5f390 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5f3a0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
5f3b0 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 heIterateDirty(P
5f3c0 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 Cache *pCache, v
5f3d0 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 oid (*xIter)(PgH
5f3e0 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a 0a dr *));.#endif..
5f3f0 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74 20 74 /* Set and get t
5f400 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 he suggested cac
5f410 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 65 20 he-size for the
5f420 73 70 65 63 69 66 69 65 64 20 70 61 67 65 72 2d specified pager-
5f430 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 cache..**.** If
5f440 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 no global maximu
5f450 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 2c m is configured,
5f460 20 74 68 65 6e 20 74 68 65 20 73 79 73 74 65 6d then the system
5f470 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 69 6d attempts to lim
5f480 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 it.** the total
5f490 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
5f4a0 63 61 63 68 65 64 20 62 79 20 70 75 72 67 65 61 cached by purgea
5f4b0 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68 65 73 ble pager-caches
5f4c0 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f to the sum.** o
5f4d0 66 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 f the suggested
5f4e0 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a cache-sizes..*/.
5f4f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
5f500 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
5f510 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 eSetCachesize(PC
5f520 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69 ache *, int);.#i
5f530 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
5f540 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5f550 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
5f560 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 eGetCachesize(PC
5f570 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66 0a ache *);.#endif.
5f580 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
5f590 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e NABLE_MEMORY_MAN
5f5a0 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74 AGEMENT./* Try t
5f5b0 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20 o return memory
5f5c0 75 73 65 64 20 62 79 20 74 68 65 20 70 63 61 63 used by the pcac
5f5d0 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65 he module to the
5f5e0 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61 main memory hea
5f5f0 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 p */.SQLITE_PRIV
5f600 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
5f610 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f cacheReleaseMemo
5f620 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a ry(int);.#endif.
5f630 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
5f640 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 EST.SQLITE_PRIVA
5f650 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
5f660 63 61 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c cacheStats(int*,
5f670 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b int*,int*,int*);
5f680 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f .#endif..SQLITE_
5f690 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
5f6a0 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 ite3PCacheSetDef
5f6b0 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65 6e ault(void);..#en
5f6c0 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f 48 dif /* _PCACHE_H
5f6d0 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a _ */../*********
5f6e0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 ***** End of pca
5f6f0 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a che.h **********
5f700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f720 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
5f730 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
5f740 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
5f750 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
5f760 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
5f770 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
5f780 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f ****** Include o
5f790 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c s.h in the middl
5f7a0 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 e of sqliteInt.h
5f7b0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
5f7c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
5f7d0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
5f7e0 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a e os.h *********
5f7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f810 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
5f820 31 20 53 65 70 74 65 6d 62 65 72 20 31 36 0a 2a 1 September 16.*
5f830 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
5f840 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
5f850 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
5f860 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
5f870 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
5f880 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
5f890 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
5f8a0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
5f8b0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
5f8c0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
5f8d0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
5f8e0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
5f8f0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
5f900 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
5f910 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
5f920 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
5f930 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
5f940 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
5f950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5f990 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 **.**.** This he
5f9a0 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67 65 74 ader file (toget
5f9b0 68 65 72 20 77 69 74 68 20 69 73 20 63 6f 6d 70 her with is comp
5f9c0 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d 63 anion C source-c
5f9d0 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e ode file.** "os.
5f9e0 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f 20 61 c") attempt to a
5f9f0 62 73 74 72 61 63 74 20 74 68 65 20 75 6e 64 65 bstract the unde
5fa00 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 rlying operating
5fa10 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a system so that.
5fa20 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 ** the SQLite li
5fa30 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b 20 brary will work
5fa40 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 61 6e on both POSIX an
5fa50 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d d windows system
5fa60 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 s..**.** This he
5fa70 61 64 65 72 20 66 69 6c 65 20 69 73 20 23 69 6e ader file is #in
5fa80 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71 6c 69 clude-ed by sqli
5fa90 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 75 73 teInt.h and thus
5faa0 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69 6e ends up.** bein
5fab0 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20 65 76 g included by ev
5fac0 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c 65 2e ery source file.
5fad0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 68 .**.** $Id: os.h
5fae0 2c 76 20 31 2e 31 30 35 20 32 30 30 38 2f 30 36 ,v 1.105 2008/06
5faf0 2f 32 36 20 31 30 3a 34 31 3a 31 39 20 64 61 6e /26 10:41:19 dan
5fb00 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a ielk1977 Exp $.*
5fb10 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 /.#ifndef _SQLIT
5fb20 45 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 20 E_OS_H_.#define
5fb30 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f _SQLITE_OS_H_../
5fb40 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 *.** Figure out
5fb50 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e if we are dealin
5fb60 67 20 77 69 74 68 20 55 6e 69 78 2c 20 57 69 6e g with Unix, Win
5fb70 64 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f 74 dows, or some ot
5fb80 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 her.** operating
5fb90 20 73 79 73 74 65 6d 2e 20 20 41 66 74 65 72 20 system. After
5fba0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c the following bl
5fbb0 6f 63 6b 20 6f 66 20 70 72 65 70 72 6f 63 65 73 ock of preproces
5fbc0 73 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c 6c s macros,.** all
5fbd0 20 6f 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e of SQLITE_OS_UN
5fbe0 49 58 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 IX, SQLITE_OS_WI
5fbf0 4e 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 N, SQLITE_OS_OS2
5fc00 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53 5f , and SQLITE_OS_
5fc10 4f 54 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20 64 OTHER .** will d
5fc20 65 66 69 6e 65 64 20 74 6f 20 65 69 74 68 65 72 efined to either
5fc30 20 31 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f 66 1 or 0. One of
5fc40 20 74 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 62 the four will b
5fc50 65 20 31 2e 20 20 54 68 65 20 6f 74 68 65 72 20 e 1. The other
5fc60 0a 2a 2a 20 74 68 72 65 65 20 77 69 6c 6c 20 62 .** three will b
5fc70 65 20 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 e 0..*/.#if defi
5fc80 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 ned(SQLITE_OS_OT
5fc90 48 45 52 29 0a 23 20 69 66 20 53 51 4c 49 54 45 HER).# if SQLITE
5fca0 5f 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 20 _OS_OTHER==1.#
5fcb0 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 undef SQLITE_OS
5fcc0 5f 55 4e 49 58 0a 23 20 20 20 64 65 66 69 6e 65 _UNIX.# define
5fcd0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 SQLITE_OS_UNIX
5fce0 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 0.# undef SQLI
5fcf0 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64 65 TE_OS_WIN.# de
5fd00 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 fine SQLITE_OS_W
5fd10 49 4e 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 IN 0.# undef S
5fd20 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 QLITE_OS_OS2.#
5fd30 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
5fd40 53 5f 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a 23 S_OS2 0.# else.#
5fd50 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f undef SQLITE_
5fd60 4f 53 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69 66 OS_OTHER.# endif
5fd70 0a 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66 .#endif.#if !def
5fd80 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 55 ined(SQLITE_OS_U
5fd90 4e 49 58 29 20 26 26 20 21 64 65 66 69 6e 65 64 NIX) && !defined
5fda0 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 (SQLITE_OS_OTHER
5fdb0 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ).# define SQLIT
5fdc0 45 5f 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 69 E_OS_OTHER 0.# i
5fdd0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f fndef SQLITE_OS_
5fde0 57 49 4e 0a 23 20 20 20 69 66 20 64 65 66 69 6e WIN.# if defin
5fdf0 65 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 ed(_WIN32) || de
5fe00 66 69 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c 20 fined(WIN32) ||
5fe10 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e defined(__CYGWIN
5fe20 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
5fe30 5f 4d 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64 _MINGW32__) || d
5fe40 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 efined(__BORLAND
5fe50 43 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e C__).# defin
5fe60 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 e SQLITE_OS_WIN
5fe70 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 1.# define S
5fe80 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a QLITE_OS_UNIX 0.
5fe90 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c # define SQL
5fea0 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 ITE_OS_OS2 0.#
5feb0 20 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f elif defined(__
5fec0 45 4d 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 EMX__) || define
5fed0 64 28 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e d(_OS2) || defin
5fee0 65 64 28 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e ed(OS2) || defin
5fef0 65 64 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 66 ed(_OS2_) || def
5ff00 69 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23 20 ined(__OS2__).#
5ff10 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
5ff20 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 E_OS_WIN 0.#
5ff30 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
5ff40 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 S_UNIX 0.# d
5ff50 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
5ff60 4f 53 32 20 31 0a 23 20 20 20 65 6c 73 65 0a 23 OS2 1.# else.#
5ff70 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
5ff80 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 TE_OS_WIN 0.#
5ff90 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
5ffa0 4f 53 5f 55 4e 49 58 20 31 0a 23 20 20 20 20 20 OS_UNIX 1.#
5ffb0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
5ffc0 5f 4f 53 32 20 30 0a 23 20 20 65 6e 64 69 66 0a _OS2 0.# endif.
5ffd0 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 # else.# define
5ffe0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 SQLITE_OS_UNIX
5fff0 30 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 0.# define SQLI
60000 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6e TE_OS_OS2 0.# en
60010 64 69 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e 64 dif.#else.# ifnd
60020 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e ef SQLITE_OS_WIN
60030 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 .# define SQLIT
60040 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e 64 E_OS_WIN 0.# end
60050 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a if.#endif../*.**
60060 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 Determine if we
60070 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 are dealing wit
60080 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 h WindowsCE - wh
60090 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a ich has a much.*
600a0 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a * reduced API..*
600b0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 57 /.#if defined(_W
600c0 49 4e 33 32 5f 57 43 45 29 0a 23 20 64 65 66 69 IN32_WCE).# defi
600d0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e ne SQLITE_OS_WIN
600e0 43 45 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 CE 1.#else.# def
600f0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ine SQLITE_OS_WI
60100 4e 43 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f NCE 0.#endif.../
60110 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 *.** Define the
60120 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 maximum size of
60130 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
60140 6e 61 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 name.*/.#if SQLI
60150 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63 6c TE_OS_WIN.# incl
60160 75 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a ude <windows.h>.
60170 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
60180 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 4d TEMPNAME_SIZE (M
60190 41 58 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c 69 AX_PATH+50).#eli
601a0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a f SQLITE_OS_OS2.
601b0 23 20 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 3e # if (__GNUC__ >
601c0 20 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3 || __GNUC__ =
601d0 3d 20 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d 49 = 3 && __GNUC_MI
601e0 4e 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20 64 NOR__ >= 3) && d
601f0 65 66 69 6e 65 64 28 4f 53 32 5f 48 49 47 48 5f efined(OS2_HIGH_
60200 4d 45 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c 75 MEMORY).# inclu
60210 64 65 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20 2f de <os2safe.h> /
60220 2a 20 68 61 73 20 74 6f 20 62 65 20 69 6e 63 6c * has to be incl
60230 75 64 65 64 20 62 65 66 6f 72 65 20 6f 73 32 2e uded before os2.
60240 68 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 6f h for linking to
60250 20 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 66 work */.# endif
60260 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 .# define INCL_D
60270 4f 53 44 41 54 45 54 49 4d 45 0a 23 20 64 65 66 OSDATETIME.# def
60280 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c 45 ine INCL_DOSFILE
60290 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 MGR.# define INC
602a0 4c 5f 44 4f 53 45 52 52 4f 52 53 0a 23 20 64 65 L_DOSERRORS.# de
602b0 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 53 fine INCL_DOSMIS
602c0 43 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f C.# define INCL_
602d0 44 4f 53 50 52 4f 43 45 53 53 0a 23 20 64 65 66 DOSPROCESS.# def
602e0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 55 ine INCL_DOSMODU
602f0 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 LEMGR.# define I
60300 4e 43 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52 45 NCL_DOSSEMAPHORE
60310 53 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 S.# include <os2
60320 2e 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75 .h>.# include <u
60330 63 6f 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e 65 conv.h>.# define
60340 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 SQLITE_TEMPNAME
60350 5f 53 49 5a 45 20 28 43 43 48 4d 41 58 50 41 54 _SIZE (CCHMAXPAT
60360 48 43 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20 64 HCOMP).#else.# d
60370 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d efine SQLITE_TEM
60380 50 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a 23 PNAME_SIZE 200.#
60390 65 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68 65 endif../* If the
603a0 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61 SET_FULLSYNC ma
603b0 63 72 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e cro is not defin
603c0 65 64 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d ed above, then m
603d0 61 6b 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d 6f ake it.** a no-o
603e0 70 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45 54 p.*/.#ifndef SET
603f0 5f 46 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66 69 _FULLSYNC.# defi
60400 6e 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 28 ne SET_FULLSYNC(
60410 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a x,y).#endif../*.
60420 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 ** The default s
60430 69 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65 ize of a disk se
60440 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ctor.*/.#ifndef
60450 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 SQLITE_DEFAULT_S
60460 45 43 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65 66 ECTOR_SIZE.# def
60470 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ine SQLITE_DEFAU
60480 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 LT_SECTOR_SIZE 5
60490 31 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 12.#endif../*.**
604a0 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 Temporary files
604b0 20 61 72 65 20 6e 61 6d 65 64 20 73 74 61 72 74 are named start
604c0 69 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 72 ing with this pr
604d0 65 66 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79 efix followed by
604e0 20 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 16 random.** al
604f0 70 68 61 6e 75 6d 65 72 69 63 20 63 68 61 72 61 phanumeric chara
60500 63 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69 cters, and no fi
60510 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68 le extension. Th
60520 65 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e ey are stored in
60530 20 74 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74 61 the.** OS's sta
60540 6e 64 61 72 64 20 74 65 6d 70 6f 72 61 72 79 20 ndard temporary
60550 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2c 20 file directory,
60560 61 6e 64 20 61 72 65 20 64 65 6c 65 74 65 64 20 and are deleted
60570 70 72 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a 2a prior to exit..*
60580 2a 20 49 66 20 73 71 6c 69 74 65 20 69 73 20 62 * If sqlite is b
60590 65 69 6e 67 20 65 6d 62 65 64 64 65 64 20 69 6e eing embedded in
605a0 20 61 6e 6f 74 68 65 72 20 70 72 6f 67 72 61 6d another program
605b0 2c 20 79 6f 75 20 6d 61 79 20 77 69 73 68 20 74 , you may wish t
605c0 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 o change the.**
605d0 70 72 65 66 69 78 20 74 6f 20 72 65 66 6c 65 63 prefix to reflec
605e0 74 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 27 73 t your program's
605f0 20 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74 20 69 name, so that i
60600 66 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 65 f your program e
60610 78 69 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75 72 xits.** prematur
60620 65 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 61 ely, old tempora
60630 72 79 20 66 69 6c 65 73 20 63 61 6e 20 62 65 20 ry files can be
60640 65 61 73 69 6c 79 20 69 64 65 6e 74 69 66 69 65 easily identifie
60650 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 64 d. This can be d
60660 6f 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 53 one.** using -DS
60670 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f QLITE_TEMP_FILE_
60680 50 52 45 46 49 58 3d 6d 79 70 72 65 66 69 78 5f PREFIX=myprefix_
60690 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 on the compiler
606a0 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a 2a command line..*
606b0 2a 0a 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31 3a *.** 2006-10-31:
606c0 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 72 The default pr
606d0 65 66 69 78 20 75 73 65 64 20 74 6f 20 62 65 20 efix used to be
606e0 22 73 71 6c 69 74 65 5f 22 2e 20 20 42 75 74 20 "sqlite_". But
606f0 74 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20 73 then.** Mcafee s
60700 74 61 72 74 65 64 20 75 73 69 6e 67 20 53 51 4c tarted using SQL
60710 69 74 65 20 69 6e 20 74 68 65 69 72 20 61 6e 74 ite in their ant
60720 69 2d 76 69 72 75 73 20 70 72 6f 64 75 63 74 20 i-virus product
60730 61 6e 64 20 69 74 0a 2a 2a 20 73 74 61 72 74 65 and it.** starte
60740 64 20 70 75 74 74 69 6e 67 20 66 69 6c 65 73 20 d putting files
60750 77 69 74 68 20 74 68 65 20 22 73 71 6c 69 74 65 with the "sqlite
60760 22 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 63 3a " name in the c:
60770 2f 74 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a 2a /temp folder..**
60780 20 54 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d 61 This annoyed ma
60790 6e 79 20 77 69 6e 64 6f 77 73 20 75 73 65 72 73 ny windows users
607a0 2e 20 20 54 68 6f 73 65 20 75 73 65 72 73 20 77 . Those users w
607b0 6f 75 6c 64 20 74 68 65 6e 20 64 6f 20 61 20 0a ould then do a .
607c0 2a 2a 20 47 6f 6f 67 6c 65 20 73 65 61 72 63 68 ** Google search
607d0 20 66 6f 72 20 22 73 71 6c 69 74 65 22 2c 20 66 for "sqlite", f
607e0 69 6e 64 20 74 68 65 20 74 65 6c 65 70 68 6f 6e ind the telephon
607f0 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 74 68 65 e numbers of the
60800 0a 2a 2a 20 64 65 76 65 6c 6f 70 65 72 73 20 61 .** developers a
60810 6e 64 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65 20 nd call to wake
60820 74 68 65 6d 20 75 70 20 61 74 20 6e 69 67 68 74 them up at night
60830 20 61 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a 2a and complain..*
60840 2a 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f * For this reaso
60850 6e 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e n, the default n
60860 61 6d 65 20 70 72 65 66 69 78 20 69 73 20 63 68 ame prefix is ch
60870 61 6e 67 65 64 20 74 6f 20 62 65 20 22 73 71 6c anged to be "sql
60880 69 74 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65 64 ite" .** spelled
60890 20 62 61 63 6b 77 61 72 64 73 2e 20 20 53 6f 20 backwards. So
608a0 74 68 65 20 74 65 6d 70 20 66 69 6c 65 73 20 61 the temp files a
608b0 72 65 20 73 74 69 6c 6c 20 69 64 65 6e 74 69 66 re still identif
608c0 69 65 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79 62 ied, but.** anyb
608d0 6f 64 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 68 ody smart enough
608e0 20 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 to figure out t
608f0 68 65 20 63 6f 64 65 20 69 73 20 61 6c 73 6f 20 he code is also
60900 6c 69 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a 20 likely smart.**
60910 65 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 74 enough to know t
60920 68 61 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 hat calling the
60930 64 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 6e developer will n
60940 6f 74 20 68 65 6c 70 20 67 65 74 20 72 69 64 0a ot help get rid.
60950 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a ** of the file..
60960 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
60970 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
60980 49 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 IX.# define SQLI
60990 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
609a0 46 49 58 20 22 65 74 69 6c 71 73 5f 22 0a 23 65 FIX "etilqs_".#e
609b0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
609c0 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 following values
609d0 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 20 61 may be passed a
609e0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 s the second arg
609f0 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69 ument to.** sqli
60a00 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68 65 te3OsLock(). The
60a10 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 65 various locks e
60a20 78 68 69 62 69 74 20 74 68 65 20 66 6f 6c 6c 6f xhibit the follo
60a30 77 69 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a 0a wing semantics:.
60a40 2a 2a 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20 20 **.** SHARED:
60a50 20 41 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 70 Any number of p
60a60 72 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c rocesses may hol
60a70 64 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 d a SHARED lock
60a80 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 0a simultaneously..
60a90 2a 2a 20 52 45 53 45 52 56 45 44 3a 20 20 41 20 ** RESERVED: A
60aa0 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d single process m
60ab0 61 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 ay hold a RESERV
60ac0 45 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c ED lock on a fil
60ad0 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 e at.**
60ae0 20 20 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74 68 any time. Oth
60af0 65 72 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 er processes may
60b00 20 68 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69 6e hold and obtain
60b10 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b new SHARED lock
60b20 73 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 20 s..** PENDING:
60b30 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 A single proces
60b40 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 50 45 4e s may hold a PEN
60b50 44 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 DING lock on a f
60b60 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 ile at.**
60b70 20 20 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 6d any one tim
60b80 65 2e 20 45 78 69 73 74 69 6e 67 20 53 48 41 52 e. Existing SHAR
60b90 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 72 ED locks may per
60ba0 73 69 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 77 sist, but no new
60bb0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 53 .** S
60bc0 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 HARED locks may
60bd0 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6f be obtained by o
60be0 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a ther processes..
60bf0 2a 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41 6e ** EXCLUSIVE: An
60c00 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
60c10 70 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 precludes all ot
60c20 68 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a her locks..**.**
60c30 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d 61 PENDING_LOCK ma
60c40 79 20 6e 6f 74 20 62 65 20 70 61 73 73 65 64 20 y not be passed
60c50 64 69 72 65 63 74 6c 79 20 74 6f 20 73 71 6c 69 directly to sqli
60c60 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 73 te3OsLock(). Ins
60c70 74 65 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63 65 tead, a.** proce
60c80 73 73 20 74 68 61 74 20 72 65 71 75 65 73 74 73 ss that requests
60c90 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
60ca0 63 6b 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 ck may actually
60cb0 6f 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 obtain a PENDING
60cc0 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20 63 .** lock. This c
60cd0 61 6e 20 62 65 20 75 70 67 72 61 64 65 64 20 74 an be upgraded t
60ce0 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c o an EXCLUSIVE l
60cf0 6f 63 6b 20 62 79 20 61 20 73 75 62 73 65 71 75 ock by a subsequ
60d00 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 ent call to.** s
60d10 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 0a qlite3OsLock()..
60d20 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c 4f */.#define NO_LO
60d30 43 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 CK 0.#de
60d40 66 69 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b fine SHARED_LOCK
60d50 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 52 1.#define R
60d60 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 32 ESERVED_LOCK 2
60d70 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 .#define PENDING
60d80 5f 4c 4f 43 4b 20 20 20 20 33 0a 23 64 65 66 69 _LOCK 3.#defi
60d90 6e 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 ne EXCLUSIVE_LOC
60da0 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 K 4../*.** File
60db0 20 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a 20 Locking Notes:
60dc0 20 28 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 77 (Mostly about w
60dd0 69 6e 64 6f 77 73 20 62 75 74 20 61 6c 73 6f 20 indows but also
60de0 73 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 6e some info for Un
60df0 69 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6e ix).**.** We can
60e00 6e 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c 65 not use LockFile
60e10 45 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 69 Ex() or UnlockFi
60e20 6c 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 2f leEx() on Win95/
60e30 39 38 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a 2a 98/ME because.**
60e40 20 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 those functions
60e50 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 are not availab
60e60 6c 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20 6f le. So we use o
60e70 6e 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20 61 nly LockFile() a
60e80 6e 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c 65 nd.** UnlockFile
60e90 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 ()..**.** LockFi
60ea0 6c 65 28 29 20 70 72 65 76 65 6e 74 73 20 6e 6f le() prevents no
60eb0 74 20 6a 75 73 74 20 77 72 69 74 69 6e 67 20 62 t just writing b
60ec0 75 74 20 61 6c 73 6f 20 72 65 61 64 69 6e 67 20 ut also reading
60ed0 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 by other process
60ee0 65 73 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44 5f es..** A SHARED_
60ef0 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 LOCK is obtained
60f00 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73 69 by locking a si
60f10 6e 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63 68 ngle randomly-ch
60f20 6f 73 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f 75 osen .** byte ou
60f30 74 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 20 t of a specific
60f40 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 2e 20 range of bytes.
60f50 54 68 65 20 6c 6f 63 6b 20 62 79 74 65 20 69 73 The lock byte is
60f60 20 6f 62 74 61 69 6e 65 64 20 61 74 20 0a 2a 2a obtained at .**
60f70 20 72 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20 73 random so two s
60f80 65 70 61 72 61 74 65 20 72 65 61 64 65 72 73 20 eparate readers
60f90 63 61 6e 20 70 72 6f 62 61 62 6c 79 20 61 63 63 can probably acc
60fa0 65 73 73 20 74 68 65 20 66 69 6c 65 20 61 74 20 ess the file at
60fb0 74 68 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69 6d the .** same tim
60fc0 65 2c 20 75 6e 6c 65 73 73 20 74 68 65 79 20 61 e, unless they a
60fd0 72 65 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20 63 re unlucky and c
60fe0 68 6f 6f 73 65 20 74 68 65 20 73 61 6d 65 20 6c hoose the same l
60ff0 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e 20 ock byte..** An
61000 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 69 EXCLUSIVE_LOCK i
61010 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f s obtained by lo
61020 63 6b 69 6e 67 20 61 6c 6c 20 62 79 74 65 73 20 cking all bytes
61030 69 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2a in the range..**
61040 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 There can only
61050 62 65 20 6f 6e 65 20 77 72 69 74 65 72 2e 20 20 be one writer.
61060 41 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 A RESERVED_LOCK
61070 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c is obtained by l
61080 6f 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e 67 ocking.** a sing
61090 6c 65 20 62 79 74 65 20 6f 66 20 74 68 65 20 66 le byte of the f
610a0 69 6c 65 20 74 68 61 74 20 69 73 20 64 65 73 69 ile that is desi
610b0 67 6e 61 74 65 64 20 61 73 20 74 68 65 20 72 65 gnated as the re
610c0 73 65 72 76 65 64 20 6c 6f 63 6b 20 62 79 74 65 served lock byte
610d0 2e 0a 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f 4c ..** A PENDING_L
610e0 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 OCK is obtained
610f0 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65 73 by locking a des
61100 69 67 6e 61 74 65 64 20 62 79 74 65 20 64 69 66 ignated byte dif
61110 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 ferent from.** t
61120 68 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b he RESERVED_LOCK
61130 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 byte..**.** On
61140 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 74 WinNT/2K/XP syst
61150 65 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 28 ems, LockFileEx(
61160 29 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c 65 ) and UnlockFile
61170 45 78 28 29 20 61 72 65 20 61 76 61 69 6c 61 62 Ex() are availab
61180 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 le,.** which mea
61190 6e 73 20 77 65 20 63 61 6e 20 75 73 65 20 72 65 ns we can use re
611a0 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b ader/writer lock
611b0 73 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72 2f s. When reader/
611c0 77 72 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a 20 writer locks.**
611d0 61 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c 6f are used, the lo
611e0 63 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e 20 ck is placed on
611f0 74 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20 6f the same range o
61200 66 20 62 79 74 65 73 20 74 68 61 74 20 69 73 20 f bytes that is
61210 75 73 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f 62 used.** for prob
61220 61 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69 6e abilistic lockin
61230 67 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 g in Win95/98/ME
61240 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 6c 6f . Hence, the lo
61250 63 6b 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a 20 cking scheme.**
61260 77 69 6c 6c 20 73 75 70 70 6f 72 74 20 74 77 6f will support two
61270 20 6f 72 20 6d 6f 72 65 20 57 69 6e 39 35 20 72 or more Win95 r
61280 65 61 64 65 72 73 20 6f 72 20 74 77 6f 20 6f 72 eaders or two or
61290 20 6d 6f 72 65 20 57 69 6e 4e 54 20 72 65 61 64 more WinNT read
612a0 65 72 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73 69 ers..** But a si
612b0 6e 67 6c 65 20 57 69 6e 39 35 20 72 65 61 64 65 ngle Win95 reade
612c0 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 r will lock out
612d0 61 6c 6c 20 57 69 6e 4e 54 20 72 65 61 64 65 72 all WinNT reader
612e0 73 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a 2a s and a single.*
612f0 2a 20 57 69 6e 4e 54 20 72 65 61 64 65 72 20 77 * WinNT reader w
61300 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c ill lock out all
61310 20 6f 74 68 65 72 20 57 69 6e 39 35 20 72 65 61 other Win95 rea
61320 64 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ders..**.** The
61330 66 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69 6e following #defin
61340 65 73 20 73 70 65 63 69 66 79 20 74 68 65 20 72 es specify the r
61350 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 75 73 ange of bytes us
61360 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 0a ed for locking..
61370 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 ** SHARED_SIZE i
61380 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
61390 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 bytes available
613a0 69 6e 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f 6d in the pool from
613b0 20 77 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e 64 which.** a rand
613c0 6f 6d 20 62 79 74 65 20 69 73 20 73 65 6c 65 63 om byte is selec
613d0 74 65 64 20 66 6f 72 20 61 20 73 68 61 72 65 64 ted for a shared
613e0 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f 6c lock. The pool
613f0 20 6f 66 20 62 79 74 65 73 20 66 6f 72 0a 2a 2a of bytes for.**
61400 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 62 65 shared locks be
61410 67 69 6e 73 20 61 74 20 53 48 41 52 45 44 5f 46 gins at SHARED_F
61420 49 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 IRST. .**.** The
61430 73 65 20 23 64 65 66 69 6e 65 73 20 61 72 65 20 se #defines are
61440 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 73 71 6c available in sql
61450 69 74 65 5f 61 75 78 2e 68 20 73 6f 20 74 68 61 ite_aux.h so tha
61460 74 20 61 64 61 70 74 6f 72 73 20 66 6f 72 0a 2a t adaptors for.*
61470 2a 20 63 6f 6e 6e 65 63 74 69 6e 67 20 53 51 4c * connecting SQL
61480 69 74 65 20 74 6f 20 6f 74 68 65 72 20 6f 70 65 ite to other ope
61490 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 63 rating systems c
614a0 61 6e 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 an use the same
614b0 62 79 74 65 0a 2a 2a 20 72 61 6e 67 65 73 20 66 byte.** ranges f
614c0 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 49 6e 20 or locking. In
614d0 70 61 72 74 69 63 75 6c 61 72 2c 20 74 68 65 20 particular, the
614e0 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 same locking str
614f0 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 79 74 ategy and.** byt
61500 65 20 72 61 6e 67 65 73 20 61 72 65 20 75 73 65 e ranges are use
61510 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 68 69 d for Unix. Thi
61520 73 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 74 68 s leaves open th
61530 65 20 70 6f 73 73 69 62 6c 69 74 79 20 6f 66 20 e possiblity of
61540 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74 having.** client
61550 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e 4e s on win95, winN
61560 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c 20 T, and unix all
61570 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 talking to the s
61580 61 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65 0a ame shared file.
61590 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69 ** and all locki
615a0 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 ng correctly. T
615b0 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72 65 o do so would re
615c0 71 75 69 72 65 20 74 68 61 74 20 73 61 6d 62 61 quire that samba
615d0 20 28 6f 72 20 77 68 61 74 65 76 65 72 0a 2a 2a (or whatever.**
615e0 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20 75 tool is being u
615f0 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68 61 sed for file sha
61600 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74 73 ring) implements
61610 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c 79 locks correctly
61620 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 6e 64 between.** wind
61630 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 20 49 ows and unix. I
61640 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68 61 74 'm guessing that
61650 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 74 6f isn't likely to
61660 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62 79 0a happen, but by.
61670 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d ** using the sam
61680 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 e locking range
61690 77 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 we are at least
616a0 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 6f 73 73 open to the poss
616b0 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c ibility..**.** L
616c0 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 6f 77 ocking in window
616d0 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 2e 20 s is manditory.
616e0 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e For this reason
616f0 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 72 , we cannot stor
61700 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 74 61 e.** actual data
61710 20 69 6e 20 74 68 65 20 62 79 74 65 73 20 75 73 in the bytes us
61720 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 ed for locking.
61730 20 54 68 65 20 70 61 67 65 72 20 6e 65 76 65 72 The pager never
61740 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 74 68 allocates.** th
61750 65 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 e pages involved
61760 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65 72 in locking ther
61770 65 66 6f 72 65 2e 20 20 53 48 41 52 45 44 5f 53 efore. SHARED_S
61780 49 5a 45 20 69 73 20 73 65 6c 65 63 74 65 64 20 IZE is selected
61790 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 6c so.** that all l
617a0 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 6f 6e ocks will fit on
617b0 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 65 a single page e
617c0 76 65 6e 20 61 74 20 74 68 65 20 6d 69 6e 69 6d ven at the minim
617d0 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a um page size..**
617e0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 64 65 PENDING_BYTE de
617f0 66 69 6e 65 73 20 74 68 65 20 62 65 67 69 6e 6e fines the beginn
61800 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73 ing of the locks
61810 2e 20 20 42 79 20 64 65 66 61 75 6c 74 20 50 45 . By default PE
61820 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 69 73 NDING_BYTE.** is
61830 20 73 65 74 20 68 69 67 68 20 73 6f 20 74 68 61 set high so tha
61840 74 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65 20 t we don't have
61850 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20 75 to allocate an u
61860 6e 75 73 65 64 20 70 61 67 65 20 65 78 63 65 70 nused page excep
61870 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 6c 61 t.** for very la
61880 72 67 65 20 64 61 74 61 62 61 73 65 73 2e 20 20 rge databases.
61890 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 20 74 But one should t
618a0 65 73 74 20 74 68 65 20 70 61 67 65 20 73 6b 69 est the page ski
618b0 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a 2a 20 pping logic .**
618c0 62 79 20 73 65 74 74 69 6e 67 20 50 45 4e 44 49 by setting PENDI
618d0 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e 64 20 NG_BYTE low and
618e0 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 69 running the enti
618f0 72 65 20 72 65 67 72 65 73 73 69 6f 6e 20 73 75 re regression su
61900 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 ite..**.** Chang
61910 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ing the value of
61920 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 72 65 PENDING_BYTE re
61930 73 75 6c 74 73 20 69 6e 20 61 20 73 75 62 74 6c sults in a subtl
61940 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0a 2a y incompatible.*
61950 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20 * file format.
61960 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 Depending on how
61970 20 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c 20 it is changed,
61980 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e 6f you might not no
61990 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e 63 6f tice.** the inco
619a0 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69 67 68 mpatibility righ
619b0 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75 6e t away, even run
619c0 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67 72 ning a full regr
619d0 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20 ession test..**
619e0 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 61 The default loca
619f0 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47 5f tion of PENDING_
61a00 42 59 54 45 20 69 73 20 74 68 65 20 66 69 72 73 BYTE is the firs
61a10 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a t byte past the.
61a20 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2e ** 1GB boundary.
61a30 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 .**.*/.#ifndef S
61a40 51 4c 49 54 45 5f 54 45 53 54 0a 23 64 65 66 69 QLITE_TEST.#defi
61a50 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 ne PENDING_BYTE
61a60 20 20 20 20 20 30 78 34 30 30 30 30 30 30 30 20 0x40000000
61a70 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 /* First byte p
61a80 61 73 74 20 74 68 65 20 31 47 42 20 62 6f 75 6e ast the 1GB boun
61a90 64 61 72 79 20 2a 2f 0a 23 65 6c 73 65 0a 53 51 dary */.#else.SQ
61aa0 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 LITE_API extern
61ab0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 71 6c unsigned int sql
61ac0 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 ite3_pending_byt
61ad0 65 3b 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 e;.#define PENDI
61ae0 4e 47 5f 42 59 54 45 20 73 71 6c 69 74 65 33 5f NG_BYTE sqlite3_
61af0 70 65 6e 64 69 6e 67 5f 62 79 74 65 0a 23 65 6e pending_byte.#en
61b00 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 52 45 53 dif..#define RES
61b10 45 52 56 45 44 5f 42 59 54 45 20 20 20 20 20 28 ERVED_BYTE (
61b20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 0a PENDING_BYTE+1).
61b30 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 46 #define SHARED_F
61b40 49 52 53 54 20 20 20 20 20 20 28 50 45 4e 44 49 IRST (PENDI
61b50 4e 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 69 NG_BYTE+2).#defi
61b60 6e 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20 20 ne SHARED_SIZE
61b70 20 20 20 20 20 35 31 30 0a 0a 2f 2a 20 0a 2a 2a 510../* .**
61b80 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 Functions for a
61b90 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 ccessing sqlite3
61ba0 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a 2a _file methods .*
61bb0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
61bc0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c int sqlite3OsCl
61bd0 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
61be0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
61bf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
61c00 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c Read(sqlite3_fil
61c10 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 e*, void*, int a
61c20 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b mt, i64 offset);
61c30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
61c40 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 int sqlite3OsWri
61c50 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a te(sqlite3_file*
61c60 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 , const void*, i
61c70 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 nt amt, i64 offs
61c80 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 et);.SQLITE_PRIV
61c90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
61ca0 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 sTruncate(sqlite
61cb0 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69 7a 3_file*, i64 siz
61cc0 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 e);.SQLITE_PRIVA
61cd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
61ce0 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c Sync(sqlite3_fil
61cf0 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 e*, int);.SQLITE
61d00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
61d10 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 ite3OsFileSize(s
61d20 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 qlite3_file*, i6
61d30 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 4 *pSize);.SQLIT
61d40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
61d50 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 lite3OsLock(sqli
61d60 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b te3_file*, int);
61d70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
61d80 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c int sqlite3OsUnl
61d90 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
61da0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
61db0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
61dc0 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 te3OsCheckReserv
61dd0 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
61de0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 ile *id, int *pR
61df0 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 esOut);.SQLITE_P
61e00 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
61e10 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 e3OsFileControl(
61e20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e sqlite3_file*,in
61e30 74 2c 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 t,void*);.SQLITE
61e40 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
61e50 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 ite3OsSectorSize
61e60 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
61e70 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 d);.SQLITE_PRIVA
61e80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
61e90 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
61ea0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 stics(sqlite3_fi
61eb0 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a le *id);../* .**
61ec0 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 Functions for a
61ed0 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 ccessing sqlite3
61ee0 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f _vfs methods .*/
61ef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
61f00 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 int sqlite3OsOpe
61f10 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c n(sqlite3_vfs *,
61f20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 const char *, s
61f30 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
61f40 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 t, int *);.SQLIT
61f50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
61f60 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 lite3OsDelete(sq
61f70 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
61f80 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b st char *, int);
61f90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
61fa0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 int sqlite3OsAcc
61fb0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ess(sqlite3_vfs
61fc0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
61fd0 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f int, int *pResO
61fe0 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ut);.SQLITE_PRIV
61ff0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
62000 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 sFullPathname(sq
62010 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
62020 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 st char *, int,
62030 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 char *);.#ifndef
62040 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
62050 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 D_EXTENSION.SQLI
62060 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
62070 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e *sqlite3OsDlOpen
62080 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 (sqlite3_vfs *,
62090 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 const char *);.S
620a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
620b0 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 id sqlite3OsDlEr
620c0 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ror(sqlite3_vfs
620d0 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b *, int, char *);
620e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
620f0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 void *sqlite3OsD
62100 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 lSym(sqlite3_vfs
62110 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 *, void *, cons
62120 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 t char *);.SQLIT
62130 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
62140 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 qlite3OsDlClose(
62150 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 sqlite3_vfs *, v
62160 6f 69 64 20 2a 29 3b 0a 23 65 6e 64 69 66 20 2f oid *);.#endif /
62170 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f * SQLITE_OMIT_LO
62180 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a AD_EXTENSION */.
62190 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
621a0 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 nt sqlite3OsRand
621b0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 omness(sqlite3_v
621c0 66 73 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 fs *, int, char
621d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
621e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
621f0 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 Sleep(sqlite3_vf
62200 73 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 s *, int);.SQLIT
62210 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
62220 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 lite3OsCurrentTi
62230 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a me(sqlite3_vfs *
62240 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 0a 2f 2a 0a , double*);../*.
62250 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 66 ** Convenience f
62260 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 6f 70 65 unctions for ope
62270 6e 69 6e 67 20 61 6e 64 20 63 6c 6f 73 69 6e 67 ning and closing
62280 20 66 69 6c 65 73 20 75 73 69 6e 67 20 0a 2a 2a files using .**
62290 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
622a0 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 ) to obtain spac
622b0 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2d 68 e for the file-h
622c0 61 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e andle structure.
622d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
622e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
622f0 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 OpenMalloc(sqlit
62300 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 e3_vfs *, const
62310 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f char *, sqlite3_
62320 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 2c 69 6e 74 file **, int,int
62330 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
62340 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
62350 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 CloseFree(sqlite
62360 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 65 6e 64 3_file *);..#end
62370 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 4f 53 if /* _SQLITE_OS
62380 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a _H_ */../*******
62390 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f ******* End of o
623a0 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.h ************
623b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
623c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
623d0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
623e0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
623f0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
62400 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
62410 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
62420 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
62430 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
62440 6d 75 74 65 78 2e 68 20 69 6e 20 74 68 65 20 6d mutex.h in the m
62450 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 iddle of sqliteI
62460 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
62470 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
62480 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
62490 6c 65 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a le mutex.h *****
624a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
624b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
624c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
624d0 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 07 August 28.**.
624e0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
624f0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
62500 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
62510 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
62520 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
62530 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
62540 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
62550 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
62560 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
62570 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
62580 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
62590 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
625a0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
625b0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
625c0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
625d0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
625e0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
625f0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
62600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
62610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
62620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
62630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
62640 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
62650 61 69 6e 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 ains the common
62660 68 65 61 64 65 72 20 66 6f 72 20 61 6c 6c 20 6d header for all m
62670 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 utex implementat
62680 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c ions..** The sql
62690 69 74 65 49 6e 74 2e 68 20 68 65 61 64 65 72 20 iteInt.h header
626a0 23 69 6e 63 6c 75 64 65 73 20 74 68 69 73 20 66 #includes this f
626b0 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 69 ile so that it i
626c0 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 s available.** t
626d0 6f 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 69 6c o all source fil
626e0 65 73 2e 20 20 57 65 20 62 72 65 61 6b 20 69 74 es. We break it
626f0 20 6f 75 74 20 69 6e 20 61 6e 20 65 66 66 6f 72 out in an effor
62700 74 20 74 6f 20 6b 65 65 70 20 74 68 65 20 63 6f t to keep the co
62710 64 65 0a 2a 2a 20 62 65 74 74 65 72 20 6f 72 67 de.** better org
62720 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f anized..**.** NO
62730 54 45 3a 20 20 73 6f 75 72 63 65 20 66 69 6c 65 TE: source file
62740 73 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 23 s should *not* #
62750 69 6e 63 6c 75 64 65 20 74 68 69 73 20 68 65 61 include this hea
62760 64 65 72 20 66 69 6c 65 20 64 69 72 65 63 74 6c der file directl
62770 79 2e 0a 2a 2a 20 53 6f 75 72 63 65 20 66 69 6c y..** Source fil
62780 65 73 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75 es should #inclu
62790 64 65 20 74 68 65 20 73 71 6c 69 74 65 49 6e 74 de the sqliteInt
627a0 2e 68 20 66 69 6c 65 20 61 6e 64 20 6c 65 74 20 .h file and let
627b0 74 68 61 74 20 66 69 6c 65 0a 2a 2a 20 69 6e 63 that file.** inc
627c0 6c 75 64 65 20 74 68 69 73 20 6f 6e 65 20 69 6e lude this one in
627d0 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 directly..**.**
627e0 24 49 64 3a 20 6d 75 74 65 78 2e 68 2c 76 20 31 $Id: mutex.h,v 1
627f0 2e 39 20 32 30 30 38 2f 31 30 2f 30 37 20 31 35 .9 2008/10/07 15
62800 3a 32 35 3a 34 38 20 64 72 68 20 45 78 70 20 24 :25:48 drh Exp $
62810 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 75 .*/.../*.** Figu
62820 72 65 20 6f 75 74 20 77 68 61 74 20 76 65 72 73 re out what vers
62830 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 64 65 20 ion of the code
62840 74 6f 20 75 73 65 2e 20 20 54 68 65 20 63 68 6f to use. The cho
62850 69 63 65 73 20 61 72 65 0a 2a 2a 0a 2a 2a 20 20 ices are.**.**
62860 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d SQLITE_MUTEX_OM
62870 49 54 20 20 20 20 20 20 20 20 20 4e 6f 20 6d 75 IT No mu
62880 74 65 78 20 6c 6f 67 69 63 2e 20 20 4e 6f 74 20 tex logic. Not
62890 65 76 65 6e 20 73 74 75 62 73 2e 20 20 54 68 65 even stubs. The
628a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
628b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
628c0 6d 75 74 65 78 65 73 20 69 6d 70 6c 65 6d 65 6e mutexes implemen
628d0 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6f tion cannot be o
628e0 76 65 72 72 69 64 64 65 6e 0a 2a 2a 20 20 20 20 verridden.**
628f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62900 20 20 20 20 20 20 20 20 20 61 74 20 73 74 61 72 at star
62910 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 t-time..**.**
62920 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f SQLITE_MUTEX_NOO
62930 50 20 20 20 20 20 20 20 20 20 46 6f 72 20 73 69 P For si
62940 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70 ngle-threaded ap
62950 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 4e 6f 0a plications. No.
62960 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
62970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
62980 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20 utual exclusion
62990 69 73 20 70 72 6f 76 69 64 65 64 2e 20 20 42 75 is provided. Bu
629a0 74 20 74 68 69 73 0a 2a 2a 20 20 20 20 20 20 20 t this.**
629b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
629c0 20 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 61 implementa
629d0 74 69 6f 6e 20 63 61 6e 20 62 65 20 6f 76 65 72 tion can be over
629e0 72 69 64 64 65 6e 20 61 74 0a 2a 2a 20 20 20 20 ridden at.**
629f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62a00 20 20 20 20 20 20 20 20 20 73 74 61 72 74 2d 74 start-t
62a10 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c ime..**.** SQL
62a20 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
62a30 44 53 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 DS For multi
62a40 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 -threaded applic
62a50 61 74 69 6f 6e 73 20 6f 6e 20 55 6e 69 78 2e 0a ations on Unix..
62a60 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
62a70 55 54 45 58 5f 57 33 32 20 20 20 20 20 20 20 20 UTEX_W32
62a80 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 For multi-thre
62a90 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e aded application
62aa0 73 20 6f 6e 20 57 69 6e 33 32 2e 0a 2a 2a 0a 2a s on Win32..**.*
62ab0 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 * SQLITE_MUTEX
62ac0 5f 4f 53 32 20 20 20 20 20 20 20 20 20 20 46 6f _OS2 Fo
62ad0 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 r multi-threaded
62ae0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e applications on
62af0 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 66 20 21 53 OS/2..*/.#if !S
62b00 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
62b10 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
62b20 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 23 65 6e 64 _MUTEX_OMIT.#end
62b30 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 if.#if SQLITE_TH
62b40 52 45 41 44 53 41 46 45 20 26 26 20 21 64 65 66 READSAFE && !def
62b50 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 ined(SQLITE_MUTE
62b60 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 66 20 53 51 X_NOOP).# if SQ
62b70 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20 20 LITE_OS_UNIX.#
62b80 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
62b90 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a 23 MUTEX_PTHREADS.#
62ba0 20 20 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53 elif SQLITE_OS
62bb0 5f 57 49 4e 0a 23 20 20 20 20 64 65 66 69 6e 65 _WIN.# define
62bc0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
62bd0 32 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54 45 2.# elif SQLITE
62be0 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 20 64 65 66 _OS_OS2.# def
62bf0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
62c00 5f 4f 53 32 0a 23 20 20 65 6c 73 65 0a 23 20 20 _OS2.# else.#
62c10 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
62c20 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 20 65 6e MUTEX_NOOP.# en
62c30 64 69 66 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 dif.#endif..#ifd
62c40 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ef SQLITE_MUTEX_
62c50 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 OMIT./*.** If th
62c60 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 6d is is a no-op im
62c70 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 69 6d plementation, im
62c80 70 6c 65 6d 65 6e 74 20 65 76 65 72 79 74 68 69 plement everythi
62c90 6e 67 20 61 73 20 6d 61 63 72 6f 73 2e 0a 2a 2f ng as macros..*/
62ca0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
62cb0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 58 29 20 _mutex_alloc(X)
62cc0 20 20 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 ((sqlite3_mut
62cd0 65 78 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 ex*)8).#define s
62ce0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 qlite3_mutex_fre
62cf0 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c e(X).#define sql
62d00 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
62d10 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 (X).#define sqli
62d20 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 58 29 te3_mutex_try(X)
62d30 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a SQLITE_OK.
62d40 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f #define sqlite3_
62d50 6d 75 74 65 78 5f 6c 65 61 76 65 28 58 29 0a 23 mutex_leave(X).#
62d60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d define sqlite3_m
62d70 75 74 65 78 5f 68 65 6c 64 28 58 29 20 20 20 20 utex_held(X)
62d80 20 31 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 1.#define sqlit
62d90 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
62da0 28 58 29 20 20 31 0a 23 64 65 66 69 6e 65 20 73 (X) 1.#define s
62db0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
62dc0 28 58 29 20 20 20 20 20 20 28 28 73 71 6c 69 74 (X) ((sqlit
62dd0 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 e3_mutex*)8).#de
62de0 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 fine sqlite3Mute
62df0 78 49 6e 69 74 28 29 20 20 20 20 20 20 20 20 53 xInit() S
62e00 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 QLITE_OK.#define
62e10 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 sqlite3MutexEnd
62e20 28 29 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 ().#endif /* def
62e30 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
62e40 5f 4d 55 54 45 58 29 20 2a 2f 0a 0a 2f 2a 2a 2a _MUTEX) */../***
62e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
62e60 6f 66 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a of mutex.h *****
62e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
62e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
62e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
62ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
62eb0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
62ec0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
62ed0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
62ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a **********/.../*
62ef0 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 .** Each databas
62f00 65 20 66 69 6c 65 20 74 6f 20 62 65 20 61 63 63 e file to be acc
62f10 65 73 73 65 64 20 62 79 20 74 68 65 20 73 79 73 essed by the sys
62f20 74 65 6d 20 69 73 20 61 6e 20 69 6e 73 74 61 6e tem is an instan
62f30 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c ce.** of the fol
62f40 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
62f50 2e 20 20 54 68 65 72 65 20 61 72 65 20 6e 6f 72 . There are nor
62f60 6d 61 6c 6c 79 20 74 77 6f 20 6f 66 20 74 68 65 mally two of the
62f70 73 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a se structures.**
62f80 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 in the sqlite.a
62f90 44 62 5b 5d 20 61 72 72 61 79 2e 20 20 61 44 62 Db[] array. aDb
62fa0 5b 30 5d 20 69 73 20 74 68 65 20 6d 61 69 6e 20 [0] is the main
62fb0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
62fc0 64 0a 2a 2a 20 61 44 62 5b 31 5d 20 69 73 20 74 d.** aDb[1] is t
62fd0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
62fe0 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 65 used to hold te
62ff0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 mporary tables.
63000 20 41 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 Additional.** d
63010 61 74 61 62 61 73 65 73 20 6d 61 79 20 62 65 20 atabases may be
63020 61 74 74 61 63 68 65 64 2e 0a 2a 2f 0a 73 74 72 attached..*/.str
63030 75 63 74 20 44 62 20 7b 0a 20 20 63 68 61 72 20 uct Db {. char
63040 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
63050 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 /* Name of this
63060 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 42 74 database */. Bt
63070 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20 ree *pBt;
63080 20 20 20 2f 2a 20 54 68 65 20 42 2a 54 72 65 65 /* The B*Tree
63090 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 structure for t
630a0 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c his database fil
630b0 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e e */. u8 inTran
630c0 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 s; /* 0
630d0 3a 20 6e 6f 74 20 77 72 69 74 61 62 6c 65 2e 20 : not writable.
630e0 20 31 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 2e 1: Transaction.
630f0 20 20 32 3a 20 43 68 65 63 6b 70 6f 69 6e 74 20 2: Checkpoint
63100 2a 2f 0a 20 20 75 38 20 73 61 66 65 74 79 5f 6c */. u8 safety_l
63110 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 48 6f 77 evel; /* How
63120 20 61 67 67 72 65 73 73 69 76 65 20 61 74 20 73 aggressive at s
63130 79 6e 63 68 69 6e 67 20 64 61 74 61 20 74 6f 20 ynching data to
63140 64 69 73 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a disk */. void *
63150 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 pAux;
63160 20 20 20 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 /* Auxiliary
63170 20 64 61 74 61 2e 20 20 55 73 75 61 6c 6c 79 20 data. Usually
63180 4e 55 4c 4c 20 2a 2f 0a 20 20 76 6f 69 64 20 28 NULL */. void (
63190 2a 78 46 72 65 65 41 75 78 29 28 76 6f 69 64 2a *xFreeAux)(void*
631a0 29 3b 20 20 2f 2a 20 52 6f 75 74 69 6e 65 20 74 ); /* Routine t
631b0 6f 20 66 72 65 65 20 70 41 75 78 20 2a 2f 0a 20 o free pAux */.
631c0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
631d0 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 ; /* Pointer
631e0 20 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 to database sch
631f0 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 ema (possibly sh
63200 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ared) */.};../*.
63210 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
63220 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
63230 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 structure stores
63240 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 a database sche
63250 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ma..**.** If the
63260 72 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 re are no virtua
63270 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 l tables configu
63280 72 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 red in this sche
63290 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d ma, the.** Schem
632a0 61 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 a.db variable is
632b0 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 set to NULL. Af
632c0 74 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69 ter the first vi
632d0 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 rtual table.** h
632e0 61 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 as been added, i
632f0 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e t is set to poin
63300 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 t to the databas
63310 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a e connection .**
63320 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 used to create
63330 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 the connection.
63340 4f 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 Once a virtual t
63350 61 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a able has been.**
63360 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 added to the Sc
63370 68 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 hema structure a
63380 6e 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 nd the Schema.db
63390 20 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 variable popula
633a0 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 ted, .** only th
633b0 61 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e at database conn
633c0 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 ection may use t
633d0 68 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 he Schema to pre
633e0 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 pare .** stateme
633f0 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 nts..*/.struct S
63400 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 chema {. int sc
63410 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f hema_cookie; /
63420 2a 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d * Database schem
63430 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 a version number
63440 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a for this file *
63450 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 /. Hash tblHash
63460 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 ; /* All
63470 74 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 tables indexed b
63480 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 y name */. Hash
63490 20 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 idxHash;
634a0 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 /* All (named)
634b0 69 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 indices indexed
634c0 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 by name */. Has
634d0 68 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 h trigHash;
634e0 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 /* All trigger
634f0 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d s indexed by nam
63500 65 20 2a 2f 0a 20 20 48 61 73 68 20 61 46 4b 65 e */. Hash aFKe
63510 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 y; /* F
63520 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 64 65 oreign keys inde
63530 78 65 64 20 62 79 20 74 6f 2d 74 61 62 6c 65 20 xed by to-table
63540 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 65 71 */. Table *pSeq
63550 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 Tab; /* The
63560 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 sqlite_sequence
63570 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 41 table used by A
63580 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a UTOINCREMENT */.
63590 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 6d 61 74 u8 file_format
635a0 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 ; /* Schema
635b0 20 66 6f 72 6d 61 74 20 76 65 72 73 69 6f 6e 20 format version
635c0 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f for this file */
635d0 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20 . u8 enc;
635e0 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 /* Text
635f0 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 encoding used by
63600 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a this database *
63610 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 /. u16 flags;
63620 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
63630 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
63640 68 20 74 68 69 73 20 73 63 68 65 6d 61 20 2a 2f h this schema */
63650 0a 20 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a . int cache_siz
63660 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 e; /* Numbe
63670 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 75 73 r of pages to us
63680 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a e in the cache *
63690 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
636a0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
636b0 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 LE. sqlite3 *db
636c0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 22 4f 77 ; /* "Ow
636d0 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e ner" connection.
636e0 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f See comment abo
636f0 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a ve */.#endif.};.
63700 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 ./*.** These mac
63710 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 ros can be used
63720 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f 72 to test, set, or
63730 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20 74 clear bits in t
63740 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 67 73 20 he .** Db.flags
63750 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e field..*/.#defin
63760 65 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 e DbHasProperty(
63770 44 2c 49 2c 50 29 20 20 20 20 20 28 28 28 44 29 D,I,P) (((D)
63780 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 ->aDb[I].pSchema
63790 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d 28 50 ->flags&(P))==(P
637a0 29 29 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73 )).#define DbHas
637b0 41 6e 79 50 72 6f 70 65 72 74 79 28 44 2c 49 2c AnyProperty(D,I,
637c0 50 29 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 P) (((D)->aDb[I
637d0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 ].pSchema->flags
637e0 26 28 50 29 29 21 3d 30 29 0a 23 64 65 66 69 6e &(P))!=0).#defin
637f0 65 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28 e DbSetProperty(
63800 44 2c 49 2c 50 29 20 20 20 20 20 28 44 29 2d 3e D,I,P) (D)->
63810 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e aDb[I].pSchema->
63820 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 66 69 flags|=(P).#defi
63830 6e 65 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 ne DbClearProper
63840 74 79 28 44 2c 49 2c 50 29 20 20 20 28 44 29 2d ty(D,I,P) (D)-
63850 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d >aDb[I].pSchema-
63860 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a >flags&=~(P)../*
63870 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
63880 65 73 20 66 6f 72 20 74 68 65 20 44 42 2e 66 6c es for the DB.fl
63890 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a ags field..**.**
638a0 20 54 68 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f The DB_SchemaLo
638b0 61 64 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 aded flag is set
638c0 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 62 after the datab
638d0 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 ase schema has b
638e0 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 6e 74 6f een.** read into
638f0 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 internal hash t
63900 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 44 42 5f ables..**.** DB_
63910 55 6e 72 65 73 65 74 56 69 65 77 73 20 6d 65 61 UnresetViews mea
63920 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d ns that one or m
63930 6f 72 65 20 76 69 65 77 73 20 68 61 76 65 20 63 ore views have c
63940 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68 61 74 olumn names that
63950 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 66 69 .** have been fi
63960 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 20 74 68 lled out. If th
63970 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 e schema changes
63980 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 20 6e , these column n
63990 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a 20 63 68 ames might.** ch
639a0 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 74 68 65 anges and so the
639b0 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 65 64 20 view will need
639c0 74 6f 20 62 65 20 72 65 73 65 74 2e 0a 2a 2f 0a to be reset..*/.
639d0 23 64 65 66 69 6e 65 20 44 42 5f 53 63 68 65 6d #define DB_Schem
639e0 61 4c 6f 61 64 65 64 20 20 20 20 30 78 30 30 30 aLoaded 0x000
639f0 31 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 61 1 /* The schema
63a00 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 64 has been loaded
63a10 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 55 */.#define DB_U
63a20 6e 72 65 73 65 74 56 69 65 77 73 20 20 20 20 30 nresetViews 0
63a30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 65 20 76 x0002 /* Some v
63a40 69 65 77 73 20 68 61 76 65 20 64 65 66 69 6e 65 iews have define
63a50 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a d column names *
63a60 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 45 6d 70 /.#define DB_Emp
63a70 74 79 20 20 20 20 20 20 20 20 20 20 20 30 78 30 ty 0x0
63a80 30 30 34 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 004 /* The file
63a90 20 69 73 20 65 6d 70 74 79 20 28 6c 65 6e 67 74 is empty (lengt
63aa0 68 20 30 20 62 79 74 65 73 29 20 2a 2f 0a 0a 2f h 0 bytes) */../
63ab0 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 *.** The number
63ac0 6f 66 20 64 69 66 66 65 72 65 6e 74 20 6b 69 6e of different kin
63ad0 64 73 20 6f 66 20 74 68 69 6e 67 73 20 74 68 61 ds of things tha
63ae0 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 74 65 64 t can be limited
63af0 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71 .** using the sq
63b00 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 20 69 6e lite3_limit() in
63b10 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 terface..*/.#def
63b20 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d ine SQLITE_N_LIM
63b30 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 4d 49 54 IT (SQLITE_LIMIT
63b40 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
63b50 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 61 +1)../*.** Looka
63b60 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20 61 side malloc is a
63b70 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73 69 set of fixed-si
63b80 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74 20 ze buffers that
63b90 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 can be used.** t
63ba0 6f 20 73 61 74 69 73 69 66 79 20 73 6d 61 6c 6c o satisify small
63bb0 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72 transient memor
63bc0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 y allocation req
63bd0 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74 uests for object
63be0 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 s.** associated
63bf0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 with a particula
63c00 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
63c10 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20 ction. The use
63c20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 of.** lookaside
63c30 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20 malloc provides
63c40 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65 a significant pe
63c50 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63 rformance enhanc
63c60 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78 ement.** (approx
63c70 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e 10%) by avoidin
63c80 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f g numerous mallo
63c90 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20 c/free requests
63ca0 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a while parsing.**
63cb0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e SQL statements.
63cc0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61 .**.** The Looka
63cd0 73 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68 side structure h
63ce0 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69 olds configurati
63cf0 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 on information a
63d00 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b bout the.** look
63d10 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62 aside malloc sub
63d20 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76 system. Each av
63d30 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61 ailable memory a
63d40 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20 llocation in.**
63d50 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75 the lookaside su
63d60 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65 bsystem is store
63d70 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 d on a linked li
63d80 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53 st of LookasideS
63d90 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a lot.** objects..
63da0 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 */.struct Lookas
63db0 69 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 ide {. u16 sz;
63dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63dd0 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 /* Size of each
63de0 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 buffer in bytes
63df0 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65 64 */. u8 bEnabled
63e00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
63e10 54 72 75 65 20 69 66 20 75 73 65 20 6c 6f 6f 6b True if use look
63e20 61 73 69 64 65 2e 20 20 46 61 6c 73 65 20 74 6f aside. False to
63e30 20 69 67 6e 6f 72 65 20 69 74 20 2a 2f 0a 20 20 ignore it */.
63e40 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 u8 bMalloced;
63e50 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
63e60 69 66 20 70 53 74 61 72 74 20 6f 62 74 61 69 6e if pStart obtain
63e70 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ed from sqlite3_
63e80 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e malloc() */. in
63e90 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 t nOut;
63ea0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
63eb0 6f 66 20 62 75 66 66 65 72 73 20 63 75 72 72 65 of buffers curre
63ec0 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 ntly checked out
63ed0 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b */. int mxOut;
63ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
63ef0 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 Highwater mark
63f00 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f for nOut */. Lo
63f10 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 okasideSlot *pFr
63f20 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ee; /* List of
63f30 20 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66 65 available buffe
63f40 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 rs */. void *pS
63f50 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 tart;
63f60 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 /* First byte of
63f70 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 available memor
63f80 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 y space */. voi
63f90 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 d *pEnd;
63fa0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 /* First by
63fb0 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61 te past end of a
63fc0 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 2a vailable space *
63fd0 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b /.};.struct Look
63fe0 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f asideSlot {. Lo
63ff0 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 okasideSlot *pNe
64000 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 xt; /* Next b
64010 75 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69 73 uffer in the lis
64020 74 20 6f 66 20 66 72 65 65 20 62 75 66 66 65 72 t of free buffer
64030 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 s */.};../*.** A
64040 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 hash table for
64050 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 function definit
64060 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 ions..**.** Hash
64070 20 65 61 63 68 20 46 75 6e 63 44 65 66 20 73 74 each FuncDef st
64080 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 ructure into one
64090 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 48 of the FuncDefH
640a0 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a ash.a[] slots..*
640b0 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 * Collisions are
640c0 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66 2e on the FuncDef.
640d0 70 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a pHash chain..*/.
640e0 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 struct FuncDefHa
640f0 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a sh {. FuncDef *
64100 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 a[23]; /*
64110 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 Hash table for f
64120 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a unctions */.};..
64130 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 /*.** Each datab
64140 61 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e ase is an instan
64150 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
64160 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
64170 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e *.** The sqlite.
64180 6c 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72 64 lastRowid record
64190 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 s the last inser
641a0 74 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74 65 t rowid generate
641b0 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 d by an.** inser
641c0 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e t statement. In
641d0 73 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20 64 serts on views d
641e0 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74 73 o not affect its
641f0 20 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a value. Each.**
64200 20 74 72 69 67 67 65 72 20 68 61 73 20 69 74 73 trigger has its
64210 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f own context, so
64220 20 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20 that lastRowid
64230 63 61 6e 20 62 65 20 75 70 64 61 74 65 64 20 69 can be updated i
64240 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 nside.** trigger
64250 73 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65 s as usual. The
64260 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
64270 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 will be restored
64280 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 once the trigge
64290 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f r.** exits. Upo
642a0 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66 n entering a bef
642b0 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f ore or instead o
642c0 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52 f trigger, lastR
642d0 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f owid is no.** lo
642e0 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66 74 65 nger (since afte
642f0 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 r version 2.8.12
64300 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a ) reset to -1..*
64310 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e *.** The sqlite.
64320 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 nChange does not
64330 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 77 count changes w
64340 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 61 ithin triggers a
64350 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 nd keeps no.** c
64360 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20 72 ontext. It is r
64370 65 73 65 74 20 61 74 20 73 74 61 72 74 20 6f 66 eset at start of
64380 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a sqlite3_exec..*
64390 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 * The sqlite.lsC
643a0 68 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74 73 hange represents
643b0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
643c0 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 hanges made by t
643d0 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 he last.** inser
643e0 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64 65 t, update, or de
643f0 6c 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 lete statement.
64400 20 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 It remains cons
64410 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 tant throughout
64420 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 the.** length of
64430 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 a statement and
64440 20 69 73 20 74 68 65 6e 20 75 70 64 61 74 65 64 is then updated
64450 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 by OP_SetCounts
64460 2e 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a . It keeps a.**
64470 20 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 6a context stack j
64480 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 ust like lastRow
64490 69 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 id so that the c
644a0 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 0a ount of changes.
644b0 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 ** within a trig
644c0 67 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 ger is not seen
644d0 6f 75 74 73 69 64 65 20 74 68 65 20 74 72 69 67 outside the trig
644e0 67 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74 6f ger. Changes to
644f0 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a views do not.**
64500 20 61 66 66 65 63 74 20 74 68 65 20 76 61 6c 75 affect the valu
64510 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a e of lsChange..*
64520 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73 43 * The sqlite.csC
64530 68 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61 63 hange keeps trac
64540 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 k of the number
64550 6f 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67 of current chang
64560 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 es (since.** the
64570 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 last statement)
64580 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 and is used to
64590 75 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 update sqlite_ls
645a0 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 Change..**.** Th
645b0 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c e member variabl
645c0 65 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 es sqlite.errCod
645d0 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 e, sqlite.zErrMs
645e0 67 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 g and sqlite.zEr
645f0 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 rMsg16.** store
64600 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
64610 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 error code and,
64620 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 if applicable, s
64630 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e tring. The.** in
64640 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 ternal function
64650 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69 sqlite3Error() i
64660 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68 s used to set th
64670 65 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a ese variables.**
64680 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a consistently..*
64690 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
646a0 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 {. sqlite3_vfs
646b0 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 *pVfs;
646c0 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 /* OS Interfa
646d0 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b ce */. int nDb;
646e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
646f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
64700 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72 of backends curr
64710 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a ently in use */.
64720 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20 20 Db *aDb;
64730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64740 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 /* All backends
64750 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 */. int flags;
64760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64770 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 6f /* Miscellano
64780 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 us flags. See be
64790 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 low */. int ope
647a0 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 nFlags;
647b0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
647c0 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
647d0 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 3_vfs.xOpen() */
647e0 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 . int errCode;
647f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64800 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 /* Most recent
64810 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 error code (SQLI
64820 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 TE_*) */. int e
64830 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 rrMask;
64840 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65 /* & re
64850 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20 sult codes with
64860 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 74 75 this before retu
64870 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 rning */. u8 au
64880 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 toCommit;
64890 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
648a0 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 auto-commit flag
648b0 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 . */. u8 temp_s
648c0 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 tore;
648d0 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 /* 1: file
648e0 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 2: memory 0: def
648f0 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c ault */. u8 mal
64900 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20 locFailed;
64910 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
64920 69 66 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 if we have seen
64930 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 a malloc failure
64940 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 */. u8 dfltLoc
64950 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 kMode;
64960 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c /* Default l
64970 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 ocking-mode for
64980 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a attached dbs */.
64990 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c u8 dfltJournal
649a0 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 Mode;
649b0 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e /* Default journ
649c0 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 al mode for atta
649d0 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69 ched dbs */. si
649e0 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 41 75 gned char nextAu
649f0 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41 tovac; /* A
64a00 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61 utovac setting a
64a10 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e fter VACUUM if >
64a20 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 =0 */. int next
64a30 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20 Pagesize;
64a40 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a /* Pagesiz
64a50 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 e after VACUUM i
64a60 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 f >0 */. int nT
64a70 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 able;
64a80 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
64a90 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 r of tables in t
64aa0 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
64ab0 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 CollSeq *pDfltC
64ac0 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f oll; /
64ad0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f * The default co
64ae0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
64af0 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69 (BINARY) */. i
64b00 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 64 lastRowid;
64b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
64b20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65 ROWID of most re
64b30 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65 cent insert (see
64b40 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 69 36 34 above) */. i64
64b50 20 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 3b 20 priorNewRowid;
64b60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
64b70 73 74 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65 st randomly gene
64b80 72 61 74 65 64 20 52 4f 57 49 44 20 2a 2f 0a 20 rated ROWID */.
64b90 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 u32 magic;
64ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
64bb0 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 * Magic number f
64bc0 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72 or detect librar
64bd0 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e y misuse */. in
64be0 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 t nChange;
64bf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
64c00 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
64c10 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
64c20 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 () */. int nTot
64c30 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 alChange;
64c40 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 /* Value r
64c50 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
64c60 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
64c70 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f () */. sqlite3_
64c80 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 mutex *mutex;
64c90 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 /* Connect
64ca0 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 ion mutex */. i
64cb0 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 nt aLimit[SQLITE
64cc0 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 _N_LIMIT]; /*
64cd0 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 Limits */. stru
64ce0 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e ct sqlite3InitIn
64cf0 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 fo { /* Inf
64d00 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75 ormation used du
64d10 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
64d20 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ion */. int i
64d30 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
64d40 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 /* When b
64d50 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69 ack is being ini
64d60 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 tialized */.
64d70 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 int newTnum;
64d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
64d90 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65 ootpage of table
64da0 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a being initializ
64db0 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 ed */. u8 bus
64dc0 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
64dd0 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 /* TRUE if
64de0 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 currently initi
64df0 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 7d 20 69 alizing */. } i
64e00 6e 69 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 nit;. int nExte
64e10 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 nsion;
64e20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
64e30 66 20 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 f loaded extensi
64e40 6f 6e 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a ons */. void **
64e50 61 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 aExtension;
64e60 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 /* Array
64e70 6f 66 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 of shared librar
64e80 61 79 20 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 ay handles */.
64e90 73 74 72 75 63 74 20 56 64 62 65 20 2a 70 56 64 struct Vdbe *pVd
64ea0 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a be; /*
64eb0 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 20 List of active
64ec0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 virtual machines
64ed0 20 2a 2f 0a 20 20 69 6e 74 20 61 63 74 69 76 65 */. int active
64ee0 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20 VdbeCnt;
64ef0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
64f00 20 76 64 62 65 73 20 63 75 72 72 65 6e 74 6c 79 vdbes currently
64f10 20 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 executing */.
64f20 69 6e 74 20 77 72 69 74 65 56 64 62 65 43 6e 74 int writeVdbeCnt
64f30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
64f40 20 4e 75 6d 62 65 72 20 6f 66 20 61 63 74 69 76 Number of activ
64f50 65 20 56 44 42 45 73 20 74 68 61 74 20 61 72 65 e VDBEs that are
64f60 20 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f writing */. vo
64f70 69 64 20 28 2a 78 54 72 61 63 65 29 28 76 6f 69 id (*xTrace)(voi
64f80 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b d*,const char*);
64f90 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 63 65 /* Trace
64fa0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
64fb0 6f 69 64 20 2a 70 54 72 61 63 65 41 72 67 3b 20 oid *pTraceArg;
64fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64fd0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
64fe0 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61 63 ment to the trac
64ff0 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 e function */.
65000 76 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 void (*xProfile)
65010 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 (void*,const cha
65020 72 2a 2c 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f r*,u64); /* Pro
65030 66 69 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 filing function
65040 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 */. void *pProf
65050 69 6c 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 ileArg;
65060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
65070 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 * Argument to pr
65080 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a ofile function *
65090 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 /. void *pCommi
650a0 74 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 tArg;
650b0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
650c0 74 20 74 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c t to xCommitCall
650d0 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 back() */ . i
650e0 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c nt (*xCommitCall
650f0 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 back)(void*);
65100 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 /* Invoked at e
65110 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a very commit. */.
65120 20 20 76 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 void *pRollbac
65130 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 kArg;
65140 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 /* Argument
65150 74 6f 20 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c to xRollbackCall
65160 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 back() */ . v
65170 6f 69 64 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 oid (*xRollbackC
65180 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b allback)(void*);
65190 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 /* Invoked at e
651a0 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a very commit. */.
651b0 20 20 76 6f 69 64 20 2a 70 55 70 64 61 74 65 41 void *pUpdateA
651c0 72 67 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 rg;. void (*xUp
651d0 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f dateCallback)(vo
651e0 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 id*,int, const c
651f0 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
65200 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a ,sqlite_int64);.
65210 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 void(*xCollNee
65220 64 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 ded)(void*,sqlit
65230 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 e3*,int eTextRep
65240 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 ,const char*);.
65250 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 void(*xCollNeed
65260 65 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 ed16)(void*,sqli
65270 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 te3*,int eTextRe
65280 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a p,const void*);.
65290 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 void *pCollNee
652a0 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 dedArg;. sqlite
652b0 33 5f 76 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 3_value *pErr;
652c0 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 /* Most
652d0 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 recent error mes
652e0 73 61 67 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a sage */. char *
652f0 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 zErrMsg;
65300 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 /* Most
65310 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 recent error mes
65320 73 61 67 65 20 28 55 54 46 2d 38 20 65 6e 63 6f sage (UTF-8 enco
65330 64 65 64 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a ded) */. char *
65340 7a 45 72 72 4d 73 67 31 36 3b 20 20 20 20 20 20 zErrMsg16;
65350 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 /* Most
65360 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 recent error mes
65370 73 61 67 65 20 28 55 54 46 2d 31 36 20 65 6e 63 sage (UTF-16 enc
65380 6f 64 65 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e oded) */. union
65390 20 7b 0a 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 {. volatile
653a0 69 6e 74 20 69 73 49 6e 74 65 72 72 75 70 74 65 int isInterrupte
653b0 64 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 73 71 d; /* True if sq
653c0 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 lite3_interrupt
653d0 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 has been called
653e0 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f */. double no
653f0 74 55 73 65 64 31 3b 20 20 20 20 20 20 20 20 20 tUsed1;
65400 20 20 20 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a /* Spacer */.
65410 20 20 7d 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 } u1;. Lookas
65420 69 64 65 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 ide lookaside;
65430 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 /* Looka
65440 73 69 64 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 side malloc conf
65450 69 67 75 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 iguration */.#if
65460 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
65470 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 _AUTHORIZATION.
65480 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f int (*xAuth)(vo
65490 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 id*,int,const ch
654a0 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c ar*,const char*,
654b0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
654c0 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 t char*);.
654d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
654e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63 /* Acc
654f0 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f ess authorizatio
65500 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 n function */.
65510 76 6f 69 64 20 2a 70 41 75 74 68 41 72 67 3b 20 void *pAuthArg;
65520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
65530 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 1st argument to
65540 20 74 68 65 20 61 63 63 65 73 73 20 61 75 74 68 the access auth
65550 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e function */.#en
65560 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
65570 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 TE_OMIT_PROGRESS
65580 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 _CALLBACK. int
65590 28 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69 (*xProgress)(voi
655a0 64 20 2a 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 d *); /* The
655b0 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 progress callba
655c0 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 ck */. void *pP
655d0 72 6f 67 72 65 73 73 41 72 67 3b 20 20 20 20 20 rogressArg;
655e0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
655f0 74 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73 t to the progres
65600 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 s callback */.
65610 69 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 int nProgressOps
65620 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
65630 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 Number of opcod
65640 65 73 20 66 6f 72 20 70 72 6f 67 72 65 73 73 20 es for progress
65650 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 callback */.#end
65660 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 if.#ifndef SQLIT
65670 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
65680 42 4c 45 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 BLE. Hash aModu
65690 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 le;
656a0 20 20 20 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 /* populated
656b0 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72 65 61 by sqlite3_crea
656c0 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 te_module() */.
656d0 20 54 61 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 Table *pVTab;
656e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
656f0 2a 20 76 74 61 62 20 77 69 74 68 20 61 63 74 69 * vtab with acti
65700 76 65 20 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 ve Connect/Creat
65710 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 71 e method */. sq
65720 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 lite3_vtab **aVT
65730 72 61 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 56 rans; /* V
65740 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77 69 irtual tables wi
65750 74 68 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 th open transact
65760 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 ions */. int nV
65770 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 Trans;
65780 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 /* Alloc
65790 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 56 54 ated size of aVT
657a0 72 61 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 rans */.#endif.
657b0 20 46 75 6e 63 44 65 66 48 61 73 68 20 61 46 75 FuncDefHash aFu
657c0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f nc; /
657d0 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 * Hash table of
657e0 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 connection funct
657f0 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20 61 ions */. Hash a
65800 43 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20 20 CollSeq;
65810 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 /* All c
65820 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
65830 65 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 es */. BusyHand
65840 6c 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72 3b ler busyHandler;
65850 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63 61 /* Busy ca
65860 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 llback */. int
65870 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 20 busyTimeout;
65880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 73 /* Bus
65890 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75 y handler timeou
658a0 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 20 t, in msec */.
658b0 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b Db aDbStatic[2];
658c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
658d0 20 53 74 61 74 69 63 20 73 70 61 63 65 20 66 6f Static space fo
658e0 72 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 20 r the 2 default
658f0 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 23 69 66 64 backends */.#ifd
65900 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 ef SQLITE_SSE.
65910 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 sqlite3_stmt *pF
65920 65 74 63 68 3b 20 20 20 20 20 20 20 20 20 2f 2a etch; /*
65930 20 55 73 65 64 20 62 79 20 53 53 45 20 74 6f 20 Used by SSE to
65940 66 65 74 63 68 20 73 74 6f 72 65 64 20 73 74 61 fetch stored sta
65950 74 65 6d 65 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 tements */.#endi
65960 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 f.};../*.** A ma
65970 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20 cro to discover
65980 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 the encoding of
65990 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 a database..*/.#
659a0 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28 define ENC(db) (
659b0 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 (db)->aDb[0].pSc
659c0 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a hema->enc)../*.*
659d0 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 * Possible value
659e0 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 s for the sqlite
659f0 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62 .flags and or Db
65a00 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a .flags fields..*
65a10 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66 *.** On sqlite.f
65a20 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45 lags, the SQLITE
65a30 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d _InTrans value m
65a40 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 61 76 eans that we hav
65a50 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20 e.** executed a
65a60 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c BEGIN. On Db.fl
65a70 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72 ags, SQLITE_InTr
65a80 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74 ans means a stat
65a90 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 ement.** transac
65aa0 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f tion is active o
65ab0 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 6c 61 n that particula
65ac0 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e r database file.
65ad0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
65ae0 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 TE_VdbeTrace
65af0 20 20 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 0x00000001 /*
65b00 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 20 56 True to trace V
65b10 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f DBE execution */
65b20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
65b30 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 20 30 InTrans 0
65b40 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72 x00000008 /* Tr
65b50 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73 ue if in a trans
65b60 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e action */.#defin
65b70 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 e SQLITE_InternC
65b80 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 30 30 hanges 0x000000
65b90 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 10 /* Uncommitt
65ba0 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 63 68 ed Hash table ch
65bb0 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 anges */.#define
65bc0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e SQLITE_FullColN
65bd0 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 30 32 ames 0x0000002
65be0 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 0 /* Show full
65bf0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 column names on
65c00 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e SELECT */.#defin
65c10 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f e SQLITE_ShortCo
65c20 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 30 lNames 0x000000
65c30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 40 /* Show shor
65c40 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 t columns names
65c50 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
65c60 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 E_CountRows
65c70 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20 0x00000080 /*
65c80 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67 Count rows chang
65c90 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f ed by INSERT, */
65ca0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
65cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65cc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 /*
65cd0 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 DELETE, or UPDAT
65ce0 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a E and return */.
65cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65d10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74 /* t
65d20 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61 he count using a
65d30 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 callback. */.#d
65d40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c efine SQLITE_Nul
65d50 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30 lCallback 0x00
65d60 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b 000100 /* Invok
65d70 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f e the callback o
65d80 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20 nce if the */.
65d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65db0 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73 /* res
65dc0 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 ult set is empty
65dd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
65de0 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 TE_SqlTrace
65df0 20 20 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 0x00000200 /*
65e00 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c Debug print SQL
65e10 20 61 73 20 69 74 20 65 78 65 63 75 74 65 73 20 as it executes
65e20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
65e30 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 E_VdbeListing
65e40 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 0x00000400 /*
65e50 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f Debug listings o
65e60 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 f VDBE programs
65e70 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
65e80 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20 20 E_WriteSchema
65e90 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 0x00000800 /*
65ea0 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c OK to update SQL
65eb0 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64 ITE_MASTER */.#d
65ec0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52 efine SQLITE_NoR
65ed0 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30 eadlock 0x00
65ee0 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c 001000 /* Readl
65ef0 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64 ocks are omitted
65f00 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20 when .
65f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
65f30 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65 ** accessing re
65f40 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 ad-only database
65f50 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
65f60 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 ITE_IgnoreChecks
65f70 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 0x00002000 /
65f80 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65 * Do not enforce
65f90 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e check constrain
65fa0 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ts */.#define SQ
65fb0 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 LITE_ReadUncommi
65fc0 74 74 65 64 20 30 78 30 30 30 30 34 30 30 30 20 tted 0x00004000
65fd0 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61 /* For shared-ca
65fe0 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 che mode */.#def
65ff0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 ine SQLITE_Legac
66000 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30 yFileFmt 0x0000
66010 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20 8000 /* Create
66020 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 69 6e new databases in
66030 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65 format 1 */.#de
66040 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c fine SQLITE_Full
66050 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 30 30 FSync 0x000
66060 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75 10000 /* Use fu
66070 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20 ll fsync on the
66080 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69 backend */.#defi
66090 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 ne SQLITE_LoadEx
660a0 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30 tension 0x00020
660b0 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c 000 /* Enable l
660c0 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f oad_extension */
660d0 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ..#define SQLITE
660e0 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20 _RecoveryMode
660f0 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49 0x00040000 /* I
66100 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72 gnore schema err
66110 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ors */.#define S
66120 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 QLITE_SharedCach
66130 65 20 20 20 20 30 78 30 30 30 38 30 30 30 30 20 e 0x00080000
66140 20 2f 2a 20 43 61 63 68 65 20 73 68 61 72 69 6e /* Cache sharin
66150 67 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a g is enabled */.
66160 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 #define SQLITE_V
66170 74 61 62 20 20 20 20 20 20 20 20 20 20 20 30 78 tab 0x
66180 30 30 31 30 30 30 30 30 20 20 2f 2a 20 54 68 65 00100000 /* The
66190 72 65 20 65 78 69 73 74 73 20 61 20 76 69 72 74 re exists a virt
661a0 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 0a 2f 2a ual table */../*
661b0 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c .** Possible val
661c0 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 ues for the sqli
661d0 74 65 2e 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a te.magic field..
661e0 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 73 20 61 ** The numbers a
661f0 72 65 20 6f 62 74 61 69 6e 65 64 20 61 74 20 72 re obtained at r
66200 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e andom and have n
66210 6f 20 73 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e o special meanin
66220 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e g, other.** than
66230 20 62 65 69 6e 67 20 64 69 73 74 69 6e 63 74 20 being distinct
66240 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 from one another
66250 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
66260 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20 ITE_MAGIC_OPEN
66270 20 20 20 30 78 61 30 32 39 61 36 39 37 20 20 2f 0xa029a697 /
66280 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 6f 70 * Database is op
66290 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 en */.#define SQ
662a0 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 LITE_MAGIC_CLOSE
662b0 44 20 20 20 30 78 39 66 33 63 32 64 33 33 20 20 D 0x9f3c2d33
662c0 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 63 /* Database is c
662d0 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 losed */.#define
662e0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 SQLITE_MAGIC_SI
662f0 43 4b 20 20 20 20 20 30 78 34 62 37 37 31 32 39 CK 0x4b77129
66300 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20 0 /* Error and
66310 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a awaiting close *
66320 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
66330 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20 20 20 _MAGIC_BUSY
66340 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a 20 44 0xf03b7906 /* D
66350 61 74 61 62 61 73 65 20 63 75 72 72 65 6e 74 6c atabase currentl
66360 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66 y in use */.#def
66370 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 ine SQLITE_MAGIC
66380 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35 33 35 _ERROR 0xb535
66390 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49 7930 /* An SQLI
663a0 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 20 TE_MISUSE error
663b0 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a occurred */../*.
663c0 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 75 6e 63 ** Each SQL func
663d0 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65 64 20 tion is defined
663e0 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f by an instance o
663f0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a f the following.
66400 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 ** structure. A
66410 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 pointer to this
66420 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 74 structure is st
66430 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 ored in the sqli
66440 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68 te.aFunc.** hash
66450 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75 table. When mu
66460 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73 ltiple functions
66470 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e have the same n
66480 61 6d 65 2c 20 74 68 65 20 68 61 73 68 20 74 61 ame, the hash ta
66490 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f ble.** points to
664a0 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f a linked list o
664b0 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
664c0 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 es..*/.struct Fu
664d0 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 20 6e 41 ncDef {. i16 nA
664e0 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f rg; /
664f0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 * Number of argu
66500 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 ments. -1 means
66510 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20 unlimited */.
66520 75 38 20 69 50 72 65 66 45 6e 63 3b 20 20 20 20 u8 iPrefEnc;
66530 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72 72 65 /* Preferre
66540 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 d text encoding
66550 28 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 31 36 (SQLITE_UTF8, 16
66560 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20 20 75 LE, 16BE) */. u
66570 38 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 8 flags;
66580 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 /* Some comb
66590 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 ination of SQLIT
665a0 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f E_FUNC_* */. vo
665b0 69 64 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20 id *pUserData;
665c0 20 20 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20 /* User data
665d0 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 parameter */. F
665e0 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 uncDef *pNext;
665f0 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 /* Next func
66600 74 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e tion with same n
66610 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a ame */. void (*
66620 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 xFunc)(sqlite3_c
66630 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
66640 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a te3_value**); /*
66650 20 52 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f Regular functio
66660 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 n */. void (*xS
66670 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e tep)(sqlite3_con
66680 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 text*,int,sqlite
66690 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 3_value**); /* A
666a0 67 67 72 65 67 61 74 65 20 73 74 65 70 20 2a 2f ggregate step */
666b0 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c . void (*xFinal
666c0 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e ize)(sqlite3_con
666d0 74 65 78 74 2a 29 3b 20 20 20 20 20 20 20 20 20 text*);
666e0 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 /* Aggreg
666f0 61 74 65 20 66 69 6e 69 61 6c 69 7a 65 72 20 2a ate finializer *
66700 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b /. char *zName;
66710 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 /* SQL
66720 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 name of the func
66730 74 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e 63 44 tion. */. FuncD
66740 65 66 20 2a 70 48 61 73 68 3b 20 20 20 20 20 20 ef *pHash;
66750 2f 2a 20 4e 65 78 74 20 77 69 74 68 20 61 20 64 /* Next with a d
66760 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 20 62 75 ifferent name bu
66770 74 20 74 68 65 20 73 61 6d 65 20 68 61 73 68 20 t the same hash
66780 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 */.};../*.** Pos
66790 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 sible values for
667a0 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 0a 2a FuncDef.flags.*
667b0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
667c0 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20 30 _FUNC_LIKE 0
667d0 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 x01 /* Candidate
667e0 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 for the LIKE op
667f0 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64 timization */.#d
66800 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
66810 43 5f 43 41 53 45 20 20 20 20 20 30 78 30 32 20 C_CASE 0x02
66820 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69 76 /* Case-sensitiv
66830 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e 63 e LIKE-type func
66840 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tion */.#define
66850 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 SQLITE_FUNC_EPHE
66860 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45 70 68 M 0x04 /* Eph
66870 65 72 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65 ermeral. Delete
66880 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 64 with VDBE */.#d
66890 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
668a0 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 20 C_NEEDCOLL 0x08
668b0 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e /* sqlite3GetFun
668c0 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 cCollSeq() might
668d0 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 0a 2f be called */../
668e0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
668f0 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f 73 2c ng three macros,
66900 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c 49 4b FUNCTION(), LIK
66910 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47 47 52 EFUNC() and AGGR
66920 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a 20 75 EGATE() are.** u
66930 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 sed to create th
66940 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 20 66 e initializers f
66950 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66 20 73 or the FuncDef s
66960 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a tructures..**.**
66970 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d FUNCTION(zNam
66980 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 e, nArg, iArg, b
66990 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20 20 20 NC, xFunc).**
669a0 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 Used to create
669b0 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 a scalar functi
669c0 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 on definition of
669d0 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d a function zNam
669e0 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c 65 6d e .** implem
669f0 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e 63 74 ented by C funct
66a00 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74 20 61 ion xFunc that a
66a10 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 ccepts nArg argu
66a20 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20 20 20 ments. The.**
66a30 20 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 value passed a
66a40 73 20 69 41 72 67 20 69 73 20 63 61 73 74 20 74 s iArg is cast t
66a50 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e 64 20 o a (void*) and
66a60 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a made available.*
66a70 2a 20 20 20 20 20 61 73 20 74 68 65 20 75 73 65 * as the use
66a80 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f r-data (sqlite3_
66a90 75 73 65 72 5f 64 61 74 61 28 29 29 20 66 6f 72 user_data()) for
66aa0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 49 the function. I
66ab0 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75 6d 65 f .** argume
66ac0 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65 2c 20 nt bNC is true,
66ad0 74 68 65 6e 20 74 68 65 20 46 75 6e 63 44 65 66 then the FuncDef
66ae0 2e 6e 65 65 64 43 6f 6c 6c 61 74 65 20 66 6c 61 .needCollate fla
66af0 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 g is set..**.**
66b00 20 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 6d AGGREGATE(zNam
66b10 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 e, nArg, iArg, b
66b20 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 NC, xStep, xFina
66b30 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 l).** Used t
66b40 6f 20 63 72 65 61 74 65 20 61 6e 20 61 67 67 72 o create an aggr
66b50 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 64 egate function d
66b60 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 6c 65 6d efinition implem
66b70 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 ented by.**
66b80 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 the C functions
66b90 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c xStep and xFinal
66ba0 2e 20 54 68 65 20 66 69 72 73 74 20 66 6f 75 72 . The first four
66bb0 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 20 parameters.**
66bc0 20 20 20 61 72 65 20 69 6e 74 65 72 70 72 65 74 are interpret
66bd0 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 77 ed in the same w
66be0 61 79 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ay as the first
66bf0 34 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a 4 parameters to.
66c00 2a 2a 20 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ** FUNCTION(
66c10 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46 )..**.** LIKEF
66c20 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c UNC(zName, nArg,
66c30 20 70 41 72 67 2c 20 66 6c 61 67 73 29 0a 2a 2a pArg, flags).**
66c40 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 Used to cre
66c50 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 6e ate a scalar fun
66c60 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e ction definition
66c70 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a of a function z
66c80 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 74 68 61 Name .** tha
66c90 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 61 t accepts nArg a
66ca0 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 69 73 20 rguments and is
66cb0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 61 implemented by a
66cc0 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20 20 call to C .**
66cd0 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 function like
66ce0 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e 74 20 70 Func. Argument p
66cf0 41 72 67 20 69 73 20 63 61 73 74 20 74 6f 20 61 Arg is cast to a
66d00 20 28 76 6f 69 64 20 2a 29 20 61 6e 64 20 6d 61 (void *) and ma
66d10 64 65 0a 2a 2a 20 20 20 20 20 61 76 61 69 6c 61 de.** availa
66d20 62 6c 65 20 61 73 20 74 68 65 20 66 75 6e 63 74 ble as the funct
66d30 69 6f 6e 20 75 73 65 72 2d 64 61 74 61 20 28 73 ion user-data (s
66d40 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
66d50 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 ()). The.**
66d60 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 20 76 61 FuncDef.flags va
66d70 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f riable is set to
66d80 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 the value passe
66d90 64 20 61 73 20 74 68 65 20 66 6c 61 67 73 0a 2a d as the flags.*
66da0 2a 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 2e * parameter.
66db0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 55 4e 43 .*/.#define FUNC
66dc0 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 TION(zName, nArg
66dd0 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 , iArg, bNC, xFu
66de0 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 nc) \. {nArg, S
66df0 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a QLITE_UTF8, bNC*
66e00 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 8, SQLITE_INT_TO
66e10 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78 _PTR(iArg), 0, x
66e20 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 Func, 0, 0, #zNa
66e30 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 53 me, 0}.#define S
66e40 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d TR_FUNCTION(zNam
66e50 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 62 e, nArg, pArg, b
66e60 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b NC, xFunc) \. {
66e70 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 nArg, SQLITE_UTF
66e80 38 2c 20 62 4e 43 2a 38 2c 20 70 41 72 67 2c 20 8, bNC*8, pArg,
66e90 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 0, xFunc, 0, 0,
66ea0 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 #zName, 0}.#defi
66eb0 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d ne LIKEFUNC(zNam
66ec0 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 66 6c e, nArg, arg, fl
66ed0 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 ags) \. {nArg,
66ee0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 66 6c 61 SQLITE_UTF8, fla
66ef0 67 73 2c 20 28 76 6f 69 64 20 2a 29 61 72 67 2c gs, (void *)arg,
66f00 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 0, likeFunc, 0,
66f10 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 0, #zName, 0}.#
66f20 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 45 define AGGREGATE
66f30 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 (zName, nArg, ar
66f40 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 g, nc, xStep, xF
66f50 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 2c inal) \. {nArg,
66f60 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e 63 SQLITE_UTF8, nc
66f70 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 *8, SQLITE_INT_T
66f80 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 30 O_PTR(arg), 0, 0
66f90 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 23 , xStep,xFinal,#
66fa0 7a 4e 61 6d 65 2c 30 7d 0a 0a 0a 2f 2a 0a 2a 2a zName,0}.../*.**
66fb0 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 Each SQLite mod
66fc0 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61 62 ule (virtual tab
66fd0 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 le definition) i
66fe0 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a s defined by an.
66ff0 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ** instance of t
67000 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
67010 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69 ucture, stored i
67020 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d n the sqlite3.aM
67030 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 odule.** hash ta
67040 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d ble..*/.struct M
67050 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 odule {. const
67060 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a sqlite3_module *
67070 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f pModule; /
67080 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 * Callback point
67090 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ers */. const c
670a0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
670b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
670c0 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20 Name passed to
670d0 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 create_module()
670e0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b */. void *pAux;
670f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67100 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75 /* pAu
67110 78 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 x passed to crea
67120 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 te_module() */.
67130 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 void (*xDestroy
67140 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 )(void *);
67150 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 /* Module
67160 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 destructor funct
67170 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ion */.};../*.**
67180 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
67190 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f ut each column o
671a0 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 69 f an SQL table i
671b0 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73 s held in an ins
671c0 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 tance.** of this
671d0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
671e0 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20 truct Column {.
671f0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
67200 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 /* Name of thi
67210 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 s column */. Ex
67220 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f pr *pDflt; /
67230 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 * Default value
67240 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a of this column *
67250 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b /. char *zType;
67260 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 /* Data typ
67270 65 20 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d e for this colum
67280 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f n */. char *zCo
67290 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 ll; /* Colla
672a0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20 ting sequence.
672b0 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65 If NULL, use the
672c0 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 default */. u8
672d0 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f notNull; /
672e0 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 * True if there
672f0 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f is a NOT NULL co
67300 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38 nstraint */. u8
67310 20 69 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f isPrimKey; /
67320 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63 * True if this c
67330 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 olumn is part of
67340 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
67350 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e */. char affin
67360 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 ity; /* One of
67370 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f the SQLITE_AFF_
67380 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69 ... values */.#i
67390 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
673a0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
673b0 20 75 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20 u8 isHidden;
673c0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
673d0 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64 s column is 'hid
673e0 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d den' */.#endif.}
673f0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c ;../*.** A "Coll
67400 61 74 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20 ating Sequence"
67410 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e is defined by an
67420 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
67430 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 following.** st
67440 72 75 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 ructure. Concept
67450 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 ually, a collati
67460 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 ng sequence cons
67470 69 73 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 ists of a name a
67480 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 nd.** a comparis
67490 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 on routine that
674a0 64 65 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 defines the orde
674b0 72 20 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e r of that sequen
674c0 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 ce..**.** There
674d0 6d 61 79 20 74 77 6f 20 73 65 70 65 72 61 74 65 may two seperate
674e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
674f0 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f of the collatio
67500 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a n function, one.
67510 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 73 73 65 ** that processe
67520 73 20 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20 s text in UTF-8
67530 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 encoding (CollSe
67540 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 q.xCmp) and anot
67550 68 65 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 her that.** proc
67560 65 73 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64 esses text encod
67570 65 64 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f ed in UTF-16 (Co
67580 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 llSeq.xCmp16), u
67590 73 69 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65 sing the machine
675a0 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20 .** native byte
675b0 6f 72 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f order. When a co
675c0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
675d0 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c is invoked, SQL
675e0 69 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 ite selects.** t
675f0 68 65 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 he version that
67600 77 69 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65 will require the
67610 20 6c 65 61 73 74 20 65 78 70 65 6e 73 69 76 65 least expensive
67620 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 encoding.** tra
67630 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e nslations, if an
67640 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c y..**.** The Col
67650 6c 53 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 lSeq.pUser membe
67660 72 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e r variable is an
67670 20 65 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 extra parameter
67680 20 74 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a that passed in.
67690 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ** as the first
676a0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
676b0 55 54 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e UTF-8 comparison
676c0 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e function, xCmp.
676d0 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 .** CollSeq.pUse
676e0 72 31 36 20 69 73 20 74 68 65 20 65 71 75 69 76 r16 is the equiv
676f0 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54 alent for the UT
67700 46 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 F-16 comparison
67710 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d function,.** xCm
67720 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f p16..**.** If bo
67730 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 th CollSeq.xCmp
67740 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 and CollSeq.xCmp
67750 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 16 are NULL, it
67760 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a means that the.*
67770 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 * collating sequ
67780 65 6e 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65 ence is undefine
67790 64 2e 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c d. Indices buil
677a0 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 t on an undefine
677b0 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 d.** collating s
677c0 65 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 equence may not
677d0 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 be read or writt
677e0 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f en..*/.struct Co
677f0 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a llSeq {. char *
67800 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 zName;
67810 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 /* Name of the c
67820 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
67830 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 e, UTF-8 encoded
67840 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 */. u8 enc;
67850 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
67860 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e ext encoding han
67870 64 6c 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a dled by xCmp() *
67880 2f 0a 20 20 75 38 20 74 79 70 65 3b 20 20 20 20 /. u8 type;
67890 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
678a0 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 of the SQLITE_C
678b0 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62 OLL_... values b
678c0 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a elow */. void *
678d0 70 55 73 65 72 3b 20 20 20 20 20 20 20 20 20 20 pUser;
678e0 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e /* First argumen
678f0 74 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20 t to xCmp() */.
67900 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 int (*xCmp)(voi
67910 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f d*,int, const vo
67920 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 id*, int, const
67930 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 void*);. void (
67940 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 *xDel)(void*);
67950 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f /* Destructor fo
67960 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f r pUser */.};../
67970 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
67980 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74 ues of CollSeq.t
67990 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ype:.*/.#define
679a0 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 SQLITE_COLL_BINA
679b0 52 59 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65 RY 1 /* The de
679c0 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63 fault memcmp() c
679d0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
679e0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
679f0 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 ITE_COLL_NOCASE
67a00 20 32 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2 /* The built
67a10 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 -in NOCASE colla
67a20 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f ting sequence */
67a30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
67a40 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20 COLL_REVERSE 3
67a50 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 /* The built-in
67a60 52 45 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e REVERSE collatin
67a70 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 g sequence */.#d
67a80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c efine SQLITE_COL
67a90 4c 5f 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20 L_USER 0 /*
67aa0 41 6e 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64 Any other user-d
67ab0 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67 efined collating
67ac0 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a sequence */../*
67ad0 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72 .** A sort order
67ae0 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 41 can be either A
67af0 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23 SC or DESC..*/.#
67b00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f define SQLITE_SO
67b10 5f 41 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a _ASC 0 /*
67b20 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 Sort in ascendi
67b30 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 ng order */.#def
67b40 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 ine SQLITE_SO_DE
67b50 53 43 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f SC 1 /* So
67b60 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 rt in ascending
67b70 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 order */../*.**
67b80 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 Column affinity
67b90 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 types..**.** The
67ba0 73 65 20 75 73 65 64 20 74 6f 20 68 61 76 65 20 se used to have
67bb0 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 mnemonic name li
67bc0 6b 65 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54 ke 'i' for SQLIT
67bd0 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e E_AFF_INTEGER an
67be0 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c d.** 't' for SQL
67bf0 49 54 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 ITE_AFF_TEXT. B
67c00 75 74 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 ut we can save a
67c10 20 6c 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e little space an
67c20 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65 d improve.** the
67c30 20 73 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20 speed a little
67c40 62 79 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 by numbering the
67c50 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 values consecut
67c60 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 ively. .**.** B
67c70 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 ut rather than s
67c80 74 61 72 74 20 77 69 74 68 20 30 20 6f 72 20 31 tart with 0 or 1
67c90 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74 68 20 , we begin with
67ca0 27 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 'a'. That way,.
67cb0 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65 ** when multiple
67cc0 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 20 affinity types
67cd0 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 are concatenated
67ce0 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61 into a string a
67cf0 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68 nd.** used as th
67d00 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68 e P4 operand, th
67d10 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 ey will be more
67d20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 readable..**.**
67d30 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74 Note also that t
67d40 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73 he numeric types
67d50 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 are grouped tog
67d60 65 74 68 65 72 20 73 6f 20 74 68 61 74 20 74 65 ether so that te
67d70 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e sting.** for a n
67d80 75 6d 65 72 69 63 20 74 79 70 65 20 69 73 20 61 umeric type is a
67d90 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73 single comparis
67da0 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 on..*/.#define S
67db0 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 QLITE_AFF_TEXT
67dc0 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53 'a'.#define S
67dd0 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 QLITE_AFF_NONE
67de0 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53 'b'.#define S
67df0 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
67e00 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53 C 'c'.#define S
67e10 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
67e20 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53 R 'd'.#define S
67e30 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 QLITE_AFF_REAL
67e40 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20 'e'..#define
67e50 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 sqlite3IsNumeric
67e60 41 66 66 69 6e 69 74 79 28 58 29 20 20 28 28 58 Affinity(X) ((X
67e70 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 )>=SQLITE_AFF_NU
67e80 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 MERIC)../*.** Th
67e90 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 e SQLITE_AFF_MAS
67ea0 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f K values masks o
67eb0 66 66 20 74 68 65 20 73 69 67 6e 69 66 69 63 61 ff the significa
67ec0 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a nt bits of an.**
67ed0 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e affinity value.
67ee0 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c .*/.#define SQL
67ef0 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20 ITE_AFF_MASK
67f00 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 0x67../*.** Add
67f10 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75 itional bit valu
67f20 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f es that can be O
67f30 52 65 64 20 77 69 74 68 20 61 6e 20 61 66 66 69 Red with an affi
67f40 6e 69 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20 nity without.**
67f50 63 68 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66 changing the aff
67f60 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e inity..*/.#defin
67f70 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e e SQLITE_JUMPIFN
67f80 55 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a ULL 0x08 /* j
67f90 75 6d 70 73 20 69 66 20 65 69 74 68 65 72 20 6f umps if either o
67fa0 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a perand is NULL *
67fb0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
67fc0 5f 53 54 4f 52 45 50 32 20 20 20 20 20 20 30 78 _STOREP2 0x
67fd0 31 30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 10 /* Store res
67fe0 75 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 ult in reg[P2] r
67ff0 61 74 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 ather than jump
68000 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 */../*.** Each S
68010 51 4c 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 QL table is repr
68020 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 esented in memor
68030 79 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 y by an instance
68040 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f of the.** follo
68050 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
68060 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d **.** Table.zNam
68070 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 e is the name of
68080 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 the table. The
68090 20 63 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69 case of the ori
680a0 67 69 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 ginal.** CREATE
680b0 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 TABLE statement
680c0 69 73 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63 is stored, but c
680d0 61 73 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 ase is not signi
680e0 66 69 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f ficant for.** co
680f0 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a mparisons..**.**
68100 20 54 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 Table.nCol is t
68110 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
68120 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 umns in this tab
68130 6c 65 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 le. Table.aCol
68140 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 is a.** pointer
68150 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 to an array of C
68160 6f 6c 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 olumn structures
68170 2c 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 , one for each c
68180 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 olumn..**.** If
68190 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e the table has an
681a0 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
681b0 20 4b 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 KEY, then Table
681c0 2e 69 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e .iPKey is the in
681d0 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f dex of.** the co
681e0 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61 lumn that is tha
681f0 74 20 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69 t key. Otherwi
68200 73 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 se Table.iPKey i
68210 73 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 s negative. Not
68220 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 e.** that the da
68230 74 61 74 79 70 65 20 6f 66 20 74 68 65 20 50 52 tatype of the PR
68240 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 IMARY KEY must b
68250 65 20 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68 e INTEGER for th
68260 69 73 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 is field to.** b
68270 65 20 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 e set. An INTEG
68280 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 ER PRIMARY KEY i
68290 73 20 75 73 65 64 20 61 73 20 74 68 65 20 72 6f s used as the ro
682a0 77 69 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 wid for each row
682b0 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 of.** the table
682c0 2e 20 20 49 66 20 61 20 74 61 62 6c 65 20 68 61 . If a table ha
682d0 73 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 s no INTEGER PRI
682e0 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 MARY KEY, then a
682f0 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a random rowid.**
68300 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f is generated fo
68310 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 r each row of th
68320 65 20 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 e table. TF_Has
68330 50 72 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65 PrimaryKey is se
68340 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c t if.** the tabl
68350 65 20 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52 e has any PRIMAR
68360 59 20 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f Y KEY, INTEGER o
68370 72 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a r otherwise..**.
68380 2a 2a 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 ** Table.tnum is
68390 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
683a0 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54 for the root BT
683b0 72 65 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 ree page of the
683c0 74 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 table in the.**
683d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
683e0 49 66 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20 If Table.iDb is
683f0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
68400 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 database table
68410 62 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 backend.** in sq
68420 6c 69 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 lite.aDb[]. 0 i
68430 73 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 s for the main d
68440 61 74 61 62 61 73 65 20 61 6e 64 20 31 20 69 73 atabase and 1 is
68450 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68 for the file th
68460 61 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 at.** holds temp
68470 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 orary tables and
68480 20 69 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46 indices. If TF
68490 5f 45 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65 _Ephemeral is se
684a0 74 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61 t.** then the ta
684b0 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e ble is stored in
684c0 20 61 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 a file that is
684d0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 automatically de
684e0 6c 65 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 leted.** when th
684f0 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f e VDBE cursor to
68500 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c the table is cl
68510 6f 73 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63 osed. In this c
68520 61 73 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a ase Table.tnum .
68530 2a 2a 20 72 65 66 65 72 73 20 56 44 42 45 20 63 ** refers VDBE c
68540 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 ursor number tha
68550 74 20 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c t holds the tabl
68560 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 e open, not to t
68570 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 he root.** page
68580 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65 number. Transie
68590 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 nt tables are us
685a0 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ed to hold the r
685b0 65 73 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 esults of a.** s
685c0 75 62 2d 71 75 65 72 79 20 74 68 61 74 20 61 70 ub-query that ap
685d0 70 65 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66 pears instead of
685e0 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 a real table na
685f0 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 me in the FROM c
68600 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 lause .** of a S
68610 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
68620 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 .*/.struct Table
68630 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
68640 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 ; /* Ass
68650 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 ociated database
68660 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 4d 69 connection. Mi
68670 67 68 74 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a ght be NULL. */.
68680 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
68690 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
686a0 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 f the table or v
686b0 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 50 4b iew */. int iPK
686c0 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ey; /*
686d0 20 49 66 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 If not negative
686e0 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79 , use aCol[iPKey
686f0 5d 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79 ] as the primary
68700 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 key */. int nC
68710 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ol; /
68720 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
68730 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c mns in this tabl
68740 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 e */. Column *a
68750 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 Col; /* I
68760 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
68770 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a each column */.
68780 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b Index *pIndex;
68790 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f /* List o
687a0 66 20 53 51 4c 20 69 6e 64 65 78 65 73 20 6f 6e f SQL indexes on
687b0 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a this table. */.
687c0 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 int tnum;
687d0 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42 /* Root B
687e0 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 20 74 68 Tree node for th
687f0 69 73 20 74 61 62 6c 65 20 28 73 65 65 20 6e 6f is table (see no
68800 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53 te above) */. S
68810 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 elect *pSelect;
68820 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20 /* NULL for
68830 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e 74 73 20 tables. Points
68840 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 66 to definition if
68850 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 75 31 a view. */. u1
68860 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 6 nRef;
68870 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
68880 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 pointers to this
68890 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 74 Table */. u8 t
688a0 61 62 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 abFlags;
688b0 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 46 5f 2a /* Mask of TF_*
688c0 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 values */. u8
688d0 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 20 20 20 keyConf;
688e0 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 /* What to do
688f0 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 71 75 in case of uniqu
68900 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 6f eness conflict o
68910 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 54 72 69 n iPKey */. Tri
68920 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 gger *pTrigger;
68930 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c /* List of SQL
68940 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69 triggers on thi
68950 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 46 4b 65 s table */. FKe
68960 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20 y *pFKey;
68970 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 /* Linked list
68980 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 of all foreign
68990 6b 65 79 73 20 69 6e 20 74 68 69 73 20 74 61 62 keys in this tab
689a0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 le */. char *zC
689b0 6f 6c 41 66 66 3b 20 20 20 20 20 20 20 2f 2a 20 olAff; /*
689c0 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 String defining
689d0 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 the affinity of
689e0 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 each column */.#
689f0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
68a00 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 70 72 20 IT_CHECK. Expr
68a10 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 *pCheck;
68a20 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20 61 6c /* The AND of al
68a30 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 l CHECK constrai
68a40 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 nts */.#endif.#i
68a50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
68a60 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 69 T_ALTERTABLE. i
68a70 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b nt addColOffset;
68a80 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
68a90 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
68aa0 6d 74 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20 mt to add a new
68ab0 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66 column */.#endif
68ac0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
68ad0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
68ae0 45 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 E. Module *pMod
68af0 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
68b00 74 65 72 20 74 6f 20 74 68 65 20 69 6d 70 6c 65 ter to the imple
68b10 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
68b20 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71 6c module */. sql
68b30 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
68b40 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 ; /* Pointer to
68b50 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e 73 74 61 the module insta
68b60 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f nce */. int nMo
68b70 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 2f 2a duleArg; /*
68b80 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d Number of argum
68b90 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f 64 75 ents to the modu
68ba0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 le */. char **a
68bb0 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20 zModuleArg; /*
68bc0 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75 Text of all modu
68bd0 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69 73 20 le args. [0] is
68be0 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23 module name */.#
68bf0 65 6e 64 69 66 0a 20 20 53 63 68 65 6d 61 20 2a endif. Schema *
68c00 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 pSchema; /*
68c10 53 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e 74 Schema that cont
68c20 61 69 6e 73 20 74 68 69 73 20 74 61 62 6c 65 20 ains this table
68c30 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 78 */. Table *pNex
68c40 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 4e 65 78 tZombie; /* Nex
68c50 74 20 6f 6e 20 74 68 65 20 50 61 72 73 65 2e 70 t on the Parse.p
68c60 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 74 20 2a ZombieTab list *
68c70 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f /.};../*.** Allo
68c80 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 54 wed values for T
68c90 61 62 65 2e 74 61 62 46 6c 61 67 73 2e 0a 2a 2f abe.tabFlags..*/
68ca0 0a 23 64 65 66 69 6e 65 20 54 46 5f 52 65 61 64 .#define TF_Read
68cb0 6f 6e 6c 79 20 20 20 20 20 20 20 20 30 78 30 31 only 0x01
68cc0 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79 /* Read-only
68cd0 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f system table */
68ce0 0a 23 64 65 66 69 6e 65 20 54 46 5f 45 70 68 65 .#define TF_Ephe
68cf0 6d 65 72 61 6c 20 20 20 20 20 20 20 30 78 30 32 meral 0x02
68d00 20 20 20 20 2f 2a 20 41 6e 20 65 6d 70 68 65 72 /* An empher
68d10 6d 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 mal table */.#de
68d20 66 69 6e 65 20 54 46 5f 48 61 73 50 72 69 6d 61 fine TF_HasPrima
68d30 72 79 4b 65 79 20 20 20 30 78 30 34 20 20 20 20 ryKey 0x04
68d40 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 61 20 70 /* Table has a p
68d50 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 23 64 rimary key */.#d
68d60 65 66 69 6e 65 20 54 46 5f 41 75 74 6f 69 6e 63 efine TF_Autoinc
68d70 72 65 6d 65 6e 74 20 20 20 30 78 30 38 20 20 20 rement 0x08
68d80 20 2f 2a 20 49 6e 74 65 67 65 72 20 70 72 69 6d /* Integer prim
68d90 61 72 79 20 6b 65 79 20 69 73 20 61 75 74 6f 69 ary key is autoi
68da0 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 ncrement */.#def
68db0 69 6e 65 20 54 46 5f 56 69 72 74 75 61 6c 20 20 ine TF_Virtual
68dc0 20 20 20 20 20 20 20 30 78 31 30 20 20 20 20 2f 0x10 /
68dd0 2a 20 49 73 20 61 20 76 69 72 74 75 61 6c 20 74 * Is a virtual t
68de0 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 able */.#define
68df0 54 46 5f 4e 65 65 64 4d 65 74 61 64 61 74 61 20 TF_NeedMetadata
68e00 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 61 43 0x20 /* aC
68e10 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e 64 20 61 ol[].zType and a
68e20 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73 Col[].pColl miss
68e30 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 ing */..../*.**
68e40 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65 74 Test to see whet
68e50 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 74 61 62 her or not a tab
68e60 6c 65 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 le is a virtual
68e70 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a table. This is.
68e80 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 6d 61 63 ** done as a mac
68e90 72 6f 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 ro so that it wi
68ea0 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 ll be optimized
68eb0 6f 75 74 20 77 68 65 6e 20 76 69 72 74 75 61 6c out when virtual
68ec0 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70 70 6f 72 .** table suppor
68ed0 74 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f t is omitted fro
68ee0 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a m the build..*/.
68ef0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
68f00 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
68f10 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72 .# define IsVir
68f20 74 75 61 6c 28 58 29 20 20 20 20 20 20 28 28 28 tual(X) (((
68f30 58 29 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 X)->tabFlags & T
68f40 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 29 0a 23 F_Virtual)!=0).#
68f50 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65 define IsHidde
68f60 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28 58 29 2d nColumn(X) ((X)-
68f70 3e 69 73 48 69 64 64 65 6e 29 0a 23 65 6c 73 65 >isHidden).#else
68f80 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72 .# define IsVir
68f90 74 75 61 6c 28 58 29 20 20 20 20 20 20 30 0a 23 tual(X) 0.#
68fa0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65 define IsHidde
68fb0 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a 23 65 6e nColumn(X) 0.#en
68fc0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 dif../*.** Each
68fd0 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
68fe0 74 72 61 69 6e 74 20 69 73 20 61 6e 20 69 6e 73 traint is an ins
68ff0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
69000 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
69010 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72 65 69 67 ..**.** A foreig
69020 6e 20 6b 65 79 20 69 73 20 61 73 73 6f 63 69 61 n key is associa
69030 74 65 64 20 77 69 74 68 20 74 77 6f 20 74 61 62 ted with two tab
69040 6c 65 73 2e 20 20 54 68 65 20 22 66 72 6f 6d 22 les. The "from"
69050 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 table is.** the
69060 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 table that cont
69070 61 69 6e 73 20 74 68 65 20 52 45 46 45 52 45 4e ains the REFEREN
69080 43 45 53 20 63 6c 61 75 73 65 20 74 68 61 74 20 CES clause that
69090 63 72 65 61 74 65 73 20 74 68 65 20 66 6f 72 65 creates the fore
690a0 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20 54 68 65 ign.** key. The
690b0 20 22 74 6f 22 20 74 61 62 6c 65 20 69 73 20 74 "to" table is t
690c0 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 he table that is
690d0 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 52 45 named in the RE
690e0 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 2e FERENCES clause.
690f0 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 69 .** Consider thi
69100 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a s example:.**.**
69110 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c CREATE TABL
69120 45 20 65 78 31 28 0a 2a 2a 20 20 20 20 20 20 20 E ex1(.**
69130 61 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 a INTEGER PRIMAR
69140 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20 20 20 20 Y KEY,.**
69150 62 20 49 4e 54 45 47 45 52 20 43 4f 4e 53 54 52 b INTEGER CONSTR
69160 41 49 4e 54 20 66 6b 31 20 52 45 46 45 52 45 4e AINT fk1 REFEREN
69170 43 45 53 20 65 78 32 28 78 29 0a 2a 2a 20 20 20 CES ex2(x).**
69180 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 );.**.** For f
69190 6f 72 65 69 67 6e 20 6b 65 79 20 22 66 6b 31 22 oreign key "fk1"
691a0 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 , the from-table
691b0 20 69 73 20 22 65 78 31 22 20 61 6e 64 20 74 68 is "ex1" and th
691c0 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 22 65 e to-table is "e
691d0 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 x2"..**.** Each
691e0 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 REFERENCES claus
691f0 65 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 69 e generates an i
69200 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
69210 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
69220 72 65 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61 re.** which is a
69230 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 66 ttached to the f
69240 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54 68 65 20 rom-table. The
69250 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f to-table need no
69260 74 20 65 78 69 73 74 20 77 68 65 6e 0a 2a 2a 20 t exist when.**
69270 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 the from-table i
69280 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 s created. The
69290 65 78 69 73 74 61 6e 63 65 20 6f 66 20 74 68 65 existance of the
692a0 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74 to-table is not
692b0 20 63 68 65 63 6b 65 64 0a 2a 2a 20 75 6e 74 69 checked.** unti
692c0 6c 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 l an attempt is
692d0 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 64 made to insert d
692e0 61 74 61 20 69 6e 74 6f 20 74 68 65 20 66 72 6f ata into the fro
692f0 6d 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 m-table..**.** T
69300 68 65 20 73 71 6c 69 74 65 2e 61 46 4b 65 79 20 he sqlite.aFKey
69310 68 61 73 68 20 74 61 62 6c 65 20 73 74 6f 72 65 hash table store
69320 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 s pointers to th
69330 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 is structure.**
69340 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f given the name o
69350 66 20 61 20 74 6f 2d 74 61 62 6c 65 2e 20 20 46 f a to-table. F
69360 6f 72 20 65 61 63 68 20 74 6f 2d 74 61 62 6c 65 or each to-table
69370 2c 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 , all foreign ke
69380 79 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 ys.** associated
69390 20 77 69 74 68 20 74 68 61 74 20 74 61 62 6c 65 with that table
693a0 20 61 72 65 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 are on a linked
693b0 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 20 list using the
693c0 46 4b 65 79 2e 70 4e 65 78 74 54 6f 0a 2a 2a 20 FKey.pNextTo.**
693d0 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 field..*/.struct
693e0 20 46 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 FKey {. Table
693f0 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 *pFrom; /* T
69400 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f he table that co
69410 6e 73 74 61 69 6e 73 20 74 68 65 20 52 45 46 45 nstains the REFE
69420 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 2a 2f RENCES clause */
69430 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 46 72 . FKey *pNextFr
69440 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72 om; /* Next for
69450 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 72 6f eign key in pFro
69460 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f m */. char *zTo
69470 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ; /* Name
69480 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74 20 74 of table that t
69490 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f he key points to
694a0 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 */. FKey *pNex
694b0 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 tTo; /* Next
694c0 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 68 61 74 foreign key that
694d0 20 70 6f 69 6e 74 73 20 74 6f 20 7a 54 6f 20 2a points to zTo *
694e0 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 /. int nCol;
694f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
69500 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
69510 69 73 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 is key */. stru
69520 63 74 20 73 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a ct sColMap { /*
69530 20 4d 61 70 70 69 6e 67 20 6f 66 20 63 6f 6c 75 Mapping of colu
69540 6d 6e 73 20 69 6e 20 70 46 72 6f 6d 20 74 6f 20 mns in pFrom to
69550 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a columns in zTo *
69560 2f 0a 20 20 20 20 69 6e 74 20 69 46 72 6f 6d 3b /. int iFrom;
69570 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
69580 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 x of column in p
69590 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 From */. char
695a0 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f *zCol; /
695b0 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e * Name of column
695c0 20 69 6e 20 7a 54 6f 2e 20 20 49 66 20 30 20 75 in zTo. If 0 u
695d0 73 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a se PRIMARY KEY *
695e0 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 20 20 20 20 /. } *aCol;
695f0 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 /* One ent
69600 72 79 20 66 6f 72 20 65 61 63 68 20 6f 66 20 6e ry for each of n
69610 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a Col column s */.
69620 20 20 75 38 20 69 73 44 65 66 65 72 72 65 64 3b u8 isDeferred;
69630 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 /* True if c
69640 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 69 onstraint checki
69650 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 20 74 ng is deferred t
69660 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 ill COMMIT */.
69670 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b 20 20 u8 updateConf;
69680 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f /* How to reso
69690 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 lve conflicts th
696a0 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50 44 41 at occur on UPDA
696b0 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c 65 74 TE */. u8 delet
696c0 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 6f 77 eConf; /* How
696d0 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f 6e 66 to resolve conf
696e0 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63 75 72 licts that occur
696f0 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 on DELETE */.
69700 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b 20 20 u8 insertConf;
69710 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f /* How to reso
69720 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 lve conflicts th
69730 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e 53 45 at occur on INSE
69740 52 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 RT */.};../*.**
69750 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74 73 20 SQLite supports
69760 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74 20 77 many different w
69770 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 ays to resolve a
69780 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 constraint.** e
69790 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b 20 rror. ROLLBACK
697a0 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 processing means
697b0 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72 61 69 that a constrai
697c0 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a 20 nt violation.**
697d0 63 61 75 73 65 73 20 74 68 65 20 6f 70 65 72 61 causes the opera
697e0 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 20 tion in process
697f0 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f 72 20 to fail and for
69800 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e the current tran
69810 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 saction.** to be
69820 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 rolled back. A
69830 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 20 BORT processing
69840 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74 means the operat
69850 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a 2a ion in process.*
69860 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e 79 20 * fails and any
69870 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 66 72 prior changes fr
69880 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 65 72 om that one oper
69890 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b 65 64 ation are backed
698a0 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68 65 out,.** but the
698b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
698c0 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e not rolled back.
698d0 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73 69 6e FAIL processin
698e0 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 g means that.**
698f0 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e the operation in
69900 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70 73 20 progress stops
69910 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 and returns an e
69920 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 20 rror code. But
69930 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65 73 prior.** changes
69940 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 6d 65 due to the same
69950 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 6e operation are n
69960 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 61 6e ot backed out an
69970 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a d no rollback.**
69980 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f 52 45 occurs. IGNORE
69990 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
699a0 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 20 74 particular row t
699b0 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63 hat caused the c
699c0 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 onstraint.** err
699d0 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 72 74 or is not insert
699e0 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 ed or updated.
699f0 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 Processing conti
69a00 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f nues and no erro
69a10 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 r.** is returned
69a20 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61 6e 73 . REPLACE means
69a30 20 74 68 61 74 20 70 72 65 65 78 69 73 74 69 6e that preexistin
69a40 67 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 g database rows
69a50 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 61 that caused.** a
69a60 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 UNIQUE constrai
69a70 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72 65 nt violation are
69a80 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 61 74 removed so that
69a90 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72 74 20 the new insert
69aa0 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 61 6e or.** update can
69ab0 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f 63 65 proceed. Proce
69ac0 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 ssing continues
69ad0 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 and no error is
69ae0 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 reported..**.**
69af0 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e 55 4c RESTRICT, SETNUL
69b00 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45 20 61 L, and CASCADE a
69b10 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e 6c ctions apply onl
69b20 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79 y to foreign key
69b30 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 20 69 s..** RESTRICT i
69b40 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 42 s the same as AB
69b50 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 ORT for IMMEDIAT
69b60 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 E foreign keys a
69b70 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 nd the.** same a
69b80 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20 44 s ROLLBACK for D
69b90 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20 20 53 EFERRED keys. S
69ba0 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 ETNULL means tha
69bb0 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a t the foreign.**
69bc0 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f 20 4e key is set to N
69bd0 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 6d 65 ULL. CASCADE me
69be0 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c 45 54 ans that a DELET
69bf0 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66 20 74 E or UPDATE of t
69c00 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 he.** referenced
69c10 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 70 72 table row is pr
69c20 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20 74 68 opagated into th
69c30 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c 64 73 e row that holds
69c40 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e 20 the.** foreign
69c50 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 key..** .** The
69c60 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c following symbol
69c70 69 63 20 76 61 6c 75 65 73 20 61 72 65 20 75 73 ic values are us
69c80 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 ed to record whi
69c90 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61 63 ch type.** of ac
69ca0 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a 2f tion to take..*/
69cb0 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e 65 .#define OE_None
69cc0 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65 72 0 /* Ther
69cd0 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61 69 e is no constrai
69ce0 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 23 nt to check */.#
69cf0 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62 61 define OE_Rollba
69d00 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c 20 74 ck 1 /* Fail t
69d10 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 he operation and
69d20 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 rollback the tr
69d30 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 ansaction */.#de
69d40 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20 20 fine OE_Abort
69d50 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75 74 2 /* Back out
69d60 20 63 68 61 6e 67 65 73 20 62 75 74 20 64 6f 20 changes but do
69d70 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e no rollback tran
69d80 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 saction */.#defi
69d90 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 20 33 ne OE_Fail 3
69da0 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20 6f /* Stop the o
69db0 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c 65 61 peration but lea
69dc0 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 68 61 ve all prior cha
69dd0 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nges */.#define
69de0 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 20 20 OE_Ignore 4
69df0 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 65 72 /* Ignore the er
69e00 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 ror. Do not do t
69e10 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 he INSERT or UPD
69e20 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f ATE */.#define O
69e30 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20 20 2f E_Replace 5 /
69e40 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74 69 6e * Delete existin
69e50 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 64 g record, then d
69e60 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 o INSERT or UPDA
69e70 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f TE */..#define O
69e80 45 5f 52 65 73 74 72 69 63 74 20 36 20 20 20 2f E_Restrict 6 /
69e90 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20 49 * OE_Abort for I
69ea0 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f 6c MMEDIATE, OE_Rol
69eb0 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45 52 52 lback for DEFERR
69ec0 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 ED */.#define OE
69ed0 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f 2a _SetNull 7 /*
69ee0 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e Set the foreign
69ef0 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 4e 55 key value to NU
69f00 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 LL */.#define OE
69f10 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20 2f 2a _SetDflt 8 /*
69f20 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e Set the foreign
69f30 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 69 74 key value to it
69f40 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64 65 s default */.#de
69f50 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 65 20 fine OE_Cascade
69f60 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64 65 20 9 /* Cascade
69f70 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 0a the changes */..
69f80 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 61 75 #define OE_Defau
69f90 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 77 68 lt 99 /* Do wh
69fa0 61 74 65 76 65 72 20 74 68 65 20 64 65 66 61 75 atever the defau
69fb0 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f 0a lt action is */.
69fc0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
69fd0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
69fe0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 wing structure i
69ff0 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 s passed as the
6a000 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e first.** argumen
6a010 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 t to sqlite3Vdbe
6a020 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 20 69 KeyCompare and i
6a030 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f s used to contro
6a040 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61 72 l the .** compar
6a050 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20 ison of the two
6a060 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a 73 index keys..*/.s
6a070 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b 0a truct KeyInfo {.
6a080 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
6a090 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
6a0a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
6a0b0 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 */. u8 enc;
6a0c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 /* Tex
6a0d0 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65 t encoding - one
6a0e0 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55 74 66 of the TEXT_Utf
6a0f0 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 31 * values */. u1
6a100 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 6 nField;
6a110 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
6a120 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c 5b ntries in aColl[
6a130 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 ] */. u8 *aSort
6a140 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 49 66 Order; /* If
6a150 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53 6f 72 defined an aSor
6a160 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 72 75 tOrder[i] is tru
6a170 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a 2f 0a e, sort DESC */.
6a180 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c CollSeq *aColl
6a190 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 [1]; /* Collati
6a1a0 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 ng sequence for
6a1b0 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 each term of the
6a1c0 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a key */.};../*.*
6a1d0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
6a1e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
6a1f0 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 tructure holds i
6a200 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
6a210 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64 a.** single ind
6a220 65 78 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 ex record that h
6a230 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
6a240 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20 parsed out into
6a250 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61 individual.** va
6a260 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 lues..**.** A re
6a270 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63 cord is an objec
6a280 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 t that contains
6a290 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c one or more fiel
6a2a0 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52 ds of data..** R
6a2b0 65 63 6f 72 64 73 20 61 72 65 20 75 73 65 64 20 ecords are used
6a2c0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e to store the con
6a2d0 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20 tent of a table
6a2e0 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65 row and to store
6a2f0 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61 .** the key of a
6a300 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62 n index. A blob
6a310 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 encoding of a r
6a320 65 63 6f 72 64 20 69 73 20 63 72 65 61 74 65 64 ecord is created
6a330 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61 by.** the OP_Ma
6a340 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 keRecord opcode
6a350 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e 64 20 of the VDBE and
6a360 69 73 20 64 69 73 61 73 73 65 6d 62 6c 69 65 64 is disassemblied
6a370 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f by the.** OP_Co
6a380 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a lumn opcode..**.
6a390 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
6a3a0 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 64 e holds a record
6a3b0 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 that has alread
6a3c0 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d 62 y been disassemb
6a3d0 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20 led.** into its
6a3e0 63 6f 6e 73 74 69 74 75 74 65 6e 74 20 66 69 65 constitutent fie
6a3f0 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55 lds..*/.struct U
6a400 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a npackedRecord {.
6a410 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
6a420 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 nfo; /* Collati
6a430 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65 on and sort-orde
6a440 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f r information */
6a450 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 . u16 nField;
6a460 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6a470 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
6a480 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 pMem[] */. u16
6a490 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
6a4a0 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69 /* Boolean setti
6a4b0 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e ngs. UNPACKED_.
6a4c0 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 4d 65 .. below */. Me
6a4d0 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *aMem;
6a4e0 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d /* Values */.}
6a4f0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 ;../*.** Allowed
6a500 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 63 values of Unpac
6a510 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a kedRecord.flags.
6a520 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 */.#define UNPAC
6a530 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 20 KED_NEED_FREE
6a540 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d 0x0001 /* Mem
6a550 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c 69 ory is from sqli
6a560 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 te3Malloc() */.#
6a570 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f define UNPACKED_
6a580 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 78 NEED_DESTROY 0x
6a590 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 0002 /* apMem[]
6a5a0 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 s should all be
6a5b0 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65 destroyed */.#de
6a5c0 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 fine UNPACKED_IG
6a5d0 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 30 NORE_ROWID 0x00
6a5e0 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72 04 /* Ignore tr
6a5f0 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20 ailing rowid on
6a600 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 key1 */.#define
6a610 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
6a620 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 0x0008 /
6a630 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20 * Make this key
6a640 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 an epsilon large
6a650 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 r */.#define UNP
6a660 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 ACKED_PREFIX_MAT
6a670 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 CH 0x0010 /* A
6a680 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 prefix match is
6a690 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a considered OK *
6a6a0 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 /../*.** Each SQ
6a6b0 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72 65 L index is repre
6a6c0 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 sented in memory
6a6d0 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e by an.** instan
6a6e0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
6a6f0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
6a700 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 *.** The columns
6a710 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 of the table th
6a720 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64 at are to be ind
6a730 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 62 exed are describ
6a740 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43 ed.** by the aiC
6a750 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66 olumn[] field of
6a760 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e this structure.
6a770 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 For example, s
6a780 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76 uppose.** we hav
6a790 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 e the following
6a7a0 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a table and index:
6a7b0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 .**.** CREAT
6a7c0 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20 69 E TABLE Ex1(c1 i
6a7d0 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74 nt, c2 int, c3 t
6a7e0 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45 ext);.** CRE
6a7f0 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e ATE INDEX Ex2 ON
6a800 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a Ex1(c3,c1);.**.
6a810 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20 ** In the Table
6a820 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 structure descri
6a830 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d bing Ex1, nCol==
6a840 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65 20 3 because there
6a850 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c are.** three col
6a860 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c umns in the tabl
6a870 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78 e. In the Index
6a880 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 structure descr
6a890 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43 ibing.** Ex2, nC
6a8a0 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32 olumn==2 since 2
6a8b0 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e of the 3 column
6a8c0 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64 s of Ex1 are ind
6a8d0 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c exed..** The val
6a8e0 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69 ue of aiColumn i
6a8f0 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c s {2, 0}. aiCol
6a900 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73 umn[0]==2 becaus
6a910 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 e the .** first
6a920 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 column to be ind
6a930 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61 6e exed (c3) has an
6a940 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45 index of 2 in E
6a950 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68 x1.aCol[]..** Th
6a960 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 e second column
6a970 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 to be indexed (c
6a980 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 1) has an index
6a990 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61 of 0 in.** Ex1.a
6a9a0 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32 Col[], hence Ex2
6a9b0 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e .aiColumn[1]==0.
6a9c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78 .**.** The Index
6a9d0 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64 .onError field d
6a9e0 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 etermines whethe
6a9f0 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 r or not the ind
6aa00 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 exed columns.**
6aa10 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 61 must be unique a
6aa20 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66 nd what to do if
6aa30 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 they are not.
6aa40 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72 When Index.onErr
6aa50 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69 or=OE_None,.** i
6aa60 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 t means this is
6aa70 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64 not a unique ind
6aa80 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 ex. Otherwise i
6aa90 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e t is a unique in
6aaa0 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 dex.** and the v
6aab0 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e alue of Index.on
6aac0 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74 Error indicate t
6aad0 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63 he which conflic
6aae0 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a t resolution .**
6aaf0 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d algorithm to em
6ab00 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e ploy whenever an
6ab10 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
6ab20 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e to insert a non
6ab30 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65 -unique.** eleme
6ab40 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e nt..*/.struct In
6ab50 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e dex {. char *zN
6ab60 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ame; /* Name
6ab70 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a of this index *
6ab80 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b /. int nColumn;
6ab90 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6aba0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
6abb0 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 table used by t
6abc0 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 his index */. i
6abd0 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20 nt *aiColumn;
6abe0 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73 /* Which columns
6abf0 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 69 are used by thi
6ac00 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73 s index. 1st is
6ac10 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 0 */. unsigned
6ac20 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52 *aiRowEst; /* R
6ac30 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45 esult of ANALYZE
6ac40 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65 : Est. rows sele
6ac50 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c cted by each col
6ac60 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a umn */. Table *
6ac70 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65 pTable; /* The
6ac80 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67 SQL table being
6ac90 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e indexed */. in
6aca0 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f t tnum; /
6acb0 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e * Page containin
6acc0 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69 g root of this i
6acd0 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 ndex in database
6ace0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e file */. u8 on
6acf0 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f Error; /* O
6ad00 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f E_Abort, OE_Igno
6ad10 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 re, OE_Replace,
6ad20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 or OE_None */.
6ad30 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20 u8 autoIndex;
6ad40 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 /* True if is a
6ad50 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 utomatically cre
6ad60 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49 ated (ex: by UNI
6ad70 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a QUE) */. char *
6ad80 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74 zColAff; /* St
6ad90 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 ring defining th
6ada0 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 e affinity of ea
6adb0 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 ch column */. I
6adc0 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20 ndex *pNext;
6add0 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65 /* The next inde
6ade0 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 x associated wit
6adf0 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 h the same table
6ae00 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 */. Schema *pS
6ae10 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61 chema; /* Schema
6ae20 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 containing this
6ae30 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a index */. u8 *
6ae40 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20 aSortOrder; /*
6ae50 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e Array of size In
6ae60 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 dex.nColumn. Tru
6ae70 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d e==DESC, False==
6ae80 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a ASC */. char **
6ae90 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 azColl; /* Arr
6aea0 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 ay of collation
6aeb0 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66 sequence names f
6aec0 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a or index */.};..
6aed0 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e /*.** Each token
6aee0 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74 coming out of t
6aef0 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69 he lexer is an i
6af00 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 nstance of.** th
6af10 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 is structure. T
6af20 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75 okens are also u
6af30 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 sed as part of a
6af40 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a n expression..**
6af50 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65 .** Note if Toke
6af60 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65 n.z==0 then Toke
6af70 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e n.dyn and Token.
6af80 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 n are undefined
6af90 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 and.** may conta
6afa0 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73 in random values
6afb0 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61 . Do not make a
6afc0 6e 79 20 61 73 73 75 70 74 69 6f 6e 73 20 61 62 ny assuptions ab
6afd0 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a out Token.dyn.**
6afe0 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65 and Token.n whe
6aff0 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f n Token.z==0..*/
6b000 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a .struct Token {.
6b010 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
6b020 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65 78 char *z; /* Tex
6b030 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20 t of the token.
6b040 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e Not NULL-termin
6b050 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67 ated! */. unsig
6b060 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20 20 20 ned dyn : 1;
6b070 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6d /* True for m
6b080 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c 20 alloced memory,
6b090 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69 63 false for static
6b0a0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6e */. unsigned n
6b0b0 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20 2f 2a : 31; /*
6b0c0 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 Number of chara
6b0d0 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f cters in this to
6b0e0 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ken */.};../*.**
6b0f0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
6b100 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 this structure c
6b110 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 ontains informat
6b120 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 ion needed to ge
6b130 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 nerate.** code f
6b140 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 74 or a SELECT that
6b150 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 contains aggreg
6b160 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a ate functions..*
6b170 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d *.** If Expr.op=
6b180 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f =TK_AGG_COLUMN o
6b190 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f r TK_AGG_FUNCTIO
6b1a0 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67 N then Expr.pAgg
6b1b0 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69 Info is a.** poi
6b1c0 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 nter to this str
6b1d0 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 70 ucture. The Exp
6b1e0 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20 r.iColumn field
6b1f0 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a is the index in.
6b200 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b ** AggInfo.aCol[
6b210 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75 ] or AggInfo.aFu
6b220 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74 nc[] of informat
6b230 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 ion needed to ge
6b240 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 nerate.** code f
6b250 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a or that node..**
6b260 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f .** AggInfo.pGro
6b270 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f upBy and AggInfo
6b280 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69 .aFunc.pExpr poi
6b290 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74 nt to fields wit
6b2a0 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 hin the.** origi
6b2b0 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63 nal Select struc
6b2c0 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 ture that descri
6b2d0 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 73 bes the SELECT s
6b2e0 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65 tatement. These
6b2f0 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f .** fields do no
6b300 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 t need to be fre
6b310 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61 ed when dealloca
6b320 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f ting the AggInfo
6b330 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
6b340 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a truct AggInfo {.
6b350 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b u8 directMode;
6b360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 /* Dir
6b370 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f ect rendering mo
6b380 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 de means take da
6b390 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20 ta directly.
6b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b3b0 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f ** from so
6b3c0 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 68 urce tables rath
6b3d0 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 er than from acc
6b3e0 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75 umulators */. u
6b3f0 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b 8 useSortingIdx;
6b400 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72 /* In dir
6b410 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65 ect mode, refere
6b420 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20 nce the sorting
6b430 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 20 index rather.
6b440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b450 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 ** than t
6b460 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 he source table
6b470 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67 */. int sorting
6b480 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Idx; /*
6b490 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 Cursor number of
6b4a0 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 the sorting ind
6b4b0 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 ex */. ExprList
6b4c0 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 *pGroupBy;
6b4d0 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 /* The group by
6b4e0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 clause */. int
6b4f0 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20 nSortingColumn;
6b500 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6b510 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
6b520 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f sorting index */
6b530 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 . struct AggInf
6b540 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f o_col { /* Fo
6b550 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73 r each column us
6b560 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62 ed in source tab
6b570 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 les */. Table
6b580 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 *pTab;
6b590 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61 /* Source ta
6b5a0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ble */. int i
6b5b0 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 Table;
6b5c0 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 /* Cursor nu
6b5d0 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72 mber of the sour
6b5e0 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 ce table */.
6b5f0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 int iColumn;
6b600 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 /* Colu
6b610 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e mn number within
6b620 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c the source tabl
6b630 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f e */. int iSo
6b640 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 rterColumn;
6b650 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 /* Column numb
6b660 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e er in the sortin
6b670 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 g index */. i
6b680 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 nt iMem;
6b690 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
6b6a0 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 y location that
6b6b0 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61 acts as accumula
6b6c0 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 tor */. Expr
6b6d0 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 *pExpr;
6b6e0 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 /* The origi
6b6f0 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a nal expression *
6b700 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69 /. } *aCol;. i
6b710 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 nt nColumn;
6b720 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6b730 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 73 of used entries
6b740 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 in aCol[] */.
6b750 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 int nColumnAlloc
6b760 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
6b770 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
6b780 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 ated for aCol[]
6b790 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 */. int nAccumu
6b7a0 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 lator; /*
6b7b0 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e Number of column
6b7c0 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f s that show thro
6b7d0 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75 ugh to the outpu
6b7e0 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t..
6b7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
6b800 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d Additional colum
6b810 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 ns are used only
6b820 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 74 as parameters t
6b830 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o.
6b840 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 ** a
6b850 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
6b860 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 ns */. struct A
6b870 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20 ggInfo_func {
6b880 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 72 /* For each aggr
6b890 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a egate function *
6b8a0 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 /. Expr *pExp
6b8b0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f r; /
6b8c0 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63 * Expression enc
6b8d0 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69 oding the functi
6b8e0 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 on */. FuncDe
6b8f0 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 f *pFunc;
6b900 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67 /* The aggreg
6b910 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 ate function imp
6b920 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 lementation */.
6b930 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 int iMem;
6b940 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
6b950 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 emory location t
6b960 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75 hat acts as accu
6b970 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 mulator */. i
6b980 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20 nt iDistinct;
6b990 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 72 /* Epher
6b9a0 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64 meral table used
6b9b0 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54 to enforce DIST
6b9c0 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 INCT */. } *aFu
6b9d0 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b nc;. int nFunc;
6b9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6b9f0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
6ba00 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f es in aFunc[] */
6ba10 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f . int nFuncAllo
6ba20 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 c; /* Nu
6ba30 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c mber of slots al
6ba40 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e located for aFun
6ba50 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a c[] */.};../*.**
6ba60 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e Each node of an
6ba70 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 expression in t
6ba80 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 73 he parse tree is
6ba90 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 an instance.**
6baa0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
6bab0 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 e..**.** Expr.op
6bac0 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 is the opcode.
6bad0 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 The integer par
6bae0 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20 ser token codes
6baf0 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73 are reused.** as
6bb00 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 20 opcodes here.
6bb10 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 For example, the
6bb20 20 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20 parser defines
6bb30 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69 TK_GE to be an i
6bb40 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72 nteger.** code r
6bb50 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 epresenting the
6bb60 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 20 ">=" operator.
6bb70 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65 This same intege
6bb80 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65 64 r code is reused
6bb90 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 .** to represent
6bba0 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68 61 the greater-tha
6bbb0 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70 n-or-equal-to op
6bbc0 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78 erator in the ex
6bbd0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 pression.** tree
6bbe0 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69 ..**.** Expr.pRi
6bbf0 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65 ght and Expr.pLe
6bc00 66 74 20 61 72 65 20 73 75 62 65 78 70 72 65 73 ft are subexpres
6bc10 73 69 6f 6e 73 2e 20 20 45 78 70 72 2e 70 4c 69 sions. Expr.pLi
6bc20 73 74 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20 st is a list.**
6bc30 6f 66 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 of argument if t
6bc40 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
6bc50 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a a function..**.
6bc60 2a 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 20 69 73 ** Expr.token is
6bc70 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f the operator to
6bc80 6b 65 6e 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 ken for this nod
6bc90 65 2e 20 20 46 6f 72 20 73 6f 6d 65 20 65 78 70 e. For some exp
6bca0 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 ressions.** that
6bcb0 20 68 61 76 65 20 73 75 62 65 78 70 72 65 73 73 have subexpress
6bcc0 69 6f 6e 73 2c 20 45 78 70 72 2e 74 6f 6b 65 6e ions, Expr.token
6bcd0 20 63 61 6e 20 62 65 20 74 68 65 20 63 6f 6d 70 can be the comp
6bce0 6c 65 74 65 20 74 65 78 74 20 74 68 61 74 20 67 lete text that g
6bcf0 61 76 65 0a 2a 2a 20 72 69 73 65 20 74 6f 20 74 ave.** rise to t
6bd00 68 65 20 45 78 70 72 2e 20 20 49 6e 20 74 68 65 he Expr. In the
6bd10 20 6c 61 74 74 65 72 20 63 61 73 65 2c 20 74 68 latter case, th
6bd20 65 20 74 6f 6b 65 6e 20 69 73 20 6d 61 72 6b 65 e token is marke
6bd30 64 20 61 73 20 62 65 69 6e 67 0a 2a 2a 20 61 20 d as being.** a
6bd40 63 6f 6d 70 6f 75 6e 64 20 74 6f 6b 65 6e 2e 0a compound token..
6bd50 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73 **.** An express
6bd60 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ion of the form
6bd70 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65 ID or ID.ID refe
6bd80 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 rs to a column i
6bd90 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f n a table..** Fo
6bda0 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f r such expressio
6bdb0 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73 ns, Expr.op is s
6bdc0 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 et to TK_COLUMN
6bdd0 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20 and Expr.iTable
6bde0 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 is.** the intege
6bdf0 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 r cursor number
6be00 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72 of a VDBE cursor
6be10 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61 pointing to tha
6be20 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45 t table and.** E
6be30 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74 xpr.iColumn is t
6be40 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 he column number
6be50 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
6be60 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 c column. If th
6be70 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 e.** expression
6be80 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73 is used as a res
6be90 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67 ult in an aggreg
6bea0 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e ate SELECT, then
6beb0 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73 the.** value is
6bec0 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 also stored in
6bed0 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f the Expr.iAgg co
6bee0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72 lumn in the aggr
6bef0 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a egate so that.**
6bf00 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73 it can be acces
6bf10 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67 sed after all ag
6bf20 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d gregates are com
6bf30 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 puted..**.** If
6bf40 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
6bf50 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 s a function, th
6bf60 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 e Expr.iTable is
6bf70 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 an integer code
6bf80 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 69 6e 67 .** representing
6bf90 20 77 68 69 63 68 20 66 75 6e 63 74 69 6f 6e 2e which function.
6bfa0 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 If the express
6bfb0 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e ion is an unboun
6bfc0 64 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 d variable.** ma
6bfd0 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e rker (a question
6bfe0 20 6d 61 72 6b 20 63 68 61 72 61 63 74 65 72 20 mark character
6bff0 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69 '?' in the origi
6c000 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68 nal SQL) then th
6c010 65 0a 2a 2a 20 45 78 70 72 2e 69 54 61 62 6c 65 e.** Expr.iTable
6c020 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78 holds the index
6c030 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 number for that
6c040 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a variable..**.**
6c050 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 If the expressi
6c060 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79 on is a subquery
6c070 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 then Expr.iColu
6c080 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 mn holds an inte
6c090 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 ger.** register
6c0a0 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e number containin
6c0b0 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 g the result of
6c0c0 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 the subquery. I
6c0d0 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 f the.** subquer
6c0e0 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 y gives a consta
6c0f0 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 nt result, then
6c100 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 iTable is -1. I
6c110 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a f the subquery.*
6c120 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72 * gives a differ
6c130 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 ent answer at di
6c140 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 fferent times du
6c150 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 ring statement p
6c160 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 rocessing.** the
6c170 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 n iTable is the
6c180 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 address of a sub
6c190 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d routine that com
6c1a0 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65 putes the subque
6c1b0 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78 ry..**.** The Ex
6c1c0 70 72 2e 70 53 65 6c 65 63 74 20 66 69 65 6c 64 pr.pSelect field
6c1d0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 53 45 4c points to a SEL
6c1e0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ECT statement.
6c1f0 54 68 65 20 53 45 4c 45 43 54 20 6d 69 67 68 74 The SELECT might
6c200 0a 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74 .** be the right
6c210 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 49 operand of an I
6c220 4e 20 6f 70 65 72 61 74 6f 72 2e 20 20 4f 72 2c N operator. Or,
6c230 20 69 66 20 61 20 73 63 61 6c 61 72 20 53 45 4c if a scalar SEL
6c240 45 43 54 20 61 70 70 65 61 72 73 0a 2a 2a 20 69 ECT appears.** i
6c250 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 n an expression
6c260 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b the opcode is TK
6c270 5f 53 45 4c 45 43 54 20 61 6e 64 20 45 78 70 72 _SELECT and Expr
6c280 2e 70 53 65 6c 65 63 74 20 69 73 20 74 68 65 20 .pSelect is the
6c290 6f 6e 6c 79 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e only.** operand.
6c2a0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 .**.** If the Ex
6c2b0 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50 pr is of type OP
6c2c0 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65 _Column, and the
6c2d0 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c table it is sel
6c2e0 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69 ecting from.** i
6c2f0 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f s a disk table o
6c300 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73 r the "old.*" ps
6c310 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e eudo-table, then
6c320 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20 pTab points to
6c330 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e the.** correspon
6c340 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e ding table defin
6c350 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ition..*/.struct
6c360 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b Expr {. u8 op;
6c370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c380 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65 /* Operation pe
6c390 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20 rformed by this
6c3a0 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 node */. char a
6c3b0 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 ffinity;
6c3c0 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 /* The affinity
6c3d0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f of the column o
6c3e0 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c r 0 if not a col
6c3f0 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 umn */. u16 fla
6c400 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
6c410 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73 /* Various flags
6c420 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a . See below */.
6c430 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
6c440 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
6c450 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f collation type o
6c460 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 f the column or
6c470 30 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 0 */. Expr *pLe
6c480 66 74 2c 20 2a 70 52 69 67 68 74 3b 20 20 2f 2a ft, *pRight; /*
6c490 20 4c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 Left and right
6c4a0 73 75 62 6e 6f 64 65 73 20 2a 2f 0a 20 20 45 78 subnodes */. Ex
6c4b0 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 prList *pList;
6c4c0 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f /* A list o
6c4d0 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 75 73 f expressions us
6c4e0 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 20 61 ed as function a
6c4f0 72 67 75 6d 65 6e 74 73 0a 20 20 20 20 20 20 20 rguments.
6c500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c510 20 20 2a 2a 20 6f 72 20 69 6e 20 22 3c 65 78 70 ** or in "<exp
6c520 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73 r> IN (<expr-lis
6c530 74 29 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 t)" */. Token t
6c540 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 oken;
6c550 2f 2a 20 41 6e 20 6f 70 65 72 61 6e 64 20 74 6f /* An operand to
6c560 6b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 ken */. Token s
6c570 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 pan;
6c580 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 /* Complete text
6c590 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
6c5a0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 on */. int iTab
6c5b0 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f le, iColumn; /
6c5c0 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f * When op==TK_CO
6c5d0 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73 20 LUMN, then this
6c5e0 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20 expr node means
6c5f0 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 the.
6c600 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
6c610 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64 iColumn-th field
6c620 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d 74 of the iTable-t
6c630 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67 h table. */. Ag
6c640 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b gInfo *pAggInfo;
6c650 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 /* Used by
6c660 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e TK_AGG_COLUMN an
6c670 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f d TK_AGG_FUNCTIO
6c680 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b N */. int iAgg;
6c690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6c6a0 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20 Which entry in
6c6b0 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d pAggInfo->aCol[]
6c6c0 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f or ->aFunc[] */
6c6d0 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69 . int iRightJoi
6c6e0 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20 nTable; /* If
6c6f0 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65 EP_FromJoin, the
6c700 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 right table of
6c710 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 53 65 the join */. Se
6c720 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 lect *pSelect;
6c730 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 /* When the
6c740 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
6c750 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 20 41 6c sub-select. Al
6c760 73 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 so the.
6c770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c780 2a 2a 20 72 69 67 68 74 20 73 69 64 65 20 6f 66 ** right side of
6c790 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65 "<expr> IN (<se
6c7a0 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 54 61 62 lect>)" */. Tab
6c7b0 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 le *pTab;
6c7c0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72 /* Table for
6c7d0 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 TK_COLUMN expre
6c7e0 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53 ssions. */.#if S
6c7f0 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
6c800 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65 EPTH>0. int nHe
6c810 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 ight;
6c820 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65 /* Height of the
6c830 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20 tree headed by
6c840 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e this node */.#en
6c850 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 dif.};../*.** Th
6c860 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
6c870 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 the meanings of
6c880 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 bits in the Expr
6c890 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f .flags field..*/
6c8a0 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d .#define EP_From
6c8b0 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f Join 0x0001 /
6c8c0 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 * Originated in
6c8d0 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 ON or USING clau
6c8e0 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a se of a join */.
6c8f0 23 64 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20 #define EP_Agg
6c900 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 0x0002 /*
6c910 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 Contains one or
6c920 20 6d 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 more aggregate
6c930 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 functions */.#de
6c940 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64 fine EP_Resolved
6c950 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44 0x0004 /* ID
6c960 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f s have been reso
6c970 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 lved to COLUMNs
6c980 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72 */.#define EP_Er
6c990 72 6f 72 20 20 20 20 20 20 30 78 30 30 30 38 20 ror 0x0008
6c9a0 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63 /* Expression c
6c9b0 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d ontains one or m
6c9c0 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 ore errors */.#d
6c9d0 65 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63 efine EP_Distinc
6c9e0 74 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 t 0x0010 /* A
6c9f0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
6ca00 6e 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 n with DISTINCT
6ca10 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 keyword */.#defi
6ca20 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20 ne EP_VarSelect
6ca30 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c 0x0020 /* pSel
6ca40 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65 ect is correlate
6ca50 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 d, not constant
6ca60 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 65 */.#define EP_De
6ca70 71 75 6f 74 65 64 20 20 20 30 78 30 30 34 30 20 quoted 0x0040
6ca80 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
6ca90 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20 string has been
6caa0 64 65 71 75 6f 74 65 64 20 2a 2f 0a 23 64 65 66 dequoted */.#def
6cab0 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 ine EP_InfixFunc
6cac0 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 75 0x0080 /* Tru
6cad0 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66 e for an infix f
6cae0 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47 unction: LIKE, G
6caf0 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66 LOB, etc */.#def
6cb00 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 ine EP_ExpCollat
6cb10 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c e 0x0100 /* Col
6cb20 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
6cb30 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 63 specified explic
6cb40 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 itly */.#define
6cb50 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30 78 EP_AnyAff 0x
6cb60 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b 0200 /* Can tak
6cb70 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d e a cached colum
6cb80 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 74 n of any affinit
6cb90 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f y */.#define EP_
6cba0 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34 30 FixedDest 0x040
6cbb0 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65 0 /* Result nee
6cbc0 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66 69 ded in a specifi
6cbd0 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 64 c register */.#d
6cbe0 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75 efine EP_IntValu
6cbf0 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20 49 e 0x0800 /* I
6cc00 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f 6e nteger value con
6cc10 74 61 69 6e 65 64 20 69 6e 20 69 54 61 62 6c 65 tained in iTable
6cc20 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 */./*.** These
6cc30 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 macros can be us
6cc40 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c ed to test, set,
6cc50 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 or clear bits i
6cc60 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66 n the .** Expr.f
6cc70 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 lags field..*/.#
6cc80 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72 define ExprHasPr
6cc90 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 operty(E,P)
6cca0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 (((E)->flags&(P)
6ccb0 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 )==(P)).#define
6ccc0 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
6ccd0 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e ty(E,P) (((E)->
6cce0 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 flags&(P))!=0).#
6ccf0 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72 define ExprSetPr
6cd00 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 operty(E,P)
6cd10 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a (E)->flags|=(P).
6cd20 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61 #define ExprClea
6cd30 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 rProperty(E,P)
6cd40 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 (E)->flags&=~(P
6cd50 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 )../*.** A list
6cd60 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 of expressions.
6cd70 20 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e Each expression
6cd80 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 may optionally
6cd90 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 have a.** name.
6cda0 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f An expr/name co
6cdb0 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 mbination can be
6cdc0 20 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c used in several
6cdd0 20 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 ways, such.** a
6cde0 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65 s the list of "e
6cdf0 78 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64 xpr AS ID" field
6ce00 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 s following a "S
6ce10 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65 ELECT" or in the
6ce20 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20 .** list of "ID
6ce30 3d 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e = expr" items in
6ce40 20 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c an UPDATE. A l
6ce50 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f ist of expressio
6ce60 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 ns can.** also b
6ce70 65 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72 e used as the ar
6ce80 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 gument to a func
6ce90 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 tion, in which c
6cea0 61 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a ase the a.zName.
6ceb0 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 ** field is not
6cec0 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 used..*/.struct
6ced0 45 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 ExprList {. int
6cee0 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 nExpr;
6cef0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6cf00 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 expressions on
6cf10 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e the list */. in
6cf20 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 t nAlloc;
6cf30 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6cf40 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 f entries alloca
6cf50 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 ted below */. i
6cf60 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20 nt iECursor;
6cf70 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 /* VDBE Cu
6cf80 72 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20 rsor associated
6cf90 77 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69 with this ExprLi
6cfa0 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 st */. struct E
6cfb0 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 xprList_item {.
6cfc0 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 Expr *pExpr;
6cfd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
6cfe0 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 list of express
6cff0 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 ions */. char
6d000 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 *zName;
6d010 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f /* Token asso
6d020 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ciated with this
6d030 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
6d040 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b u8 sortOrder;
6d050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 /* 1 f
6d060 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 or DESC or 0 for
6d070 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 ASC */. u8 d
6d080 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 one;
6d090 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 /* A flag to
6d0a0 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 indicate when pr
6d0b0 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 ocessing is fini
6d0c0 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 shed */. u16
6d0d0 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 iCol;
6d0e0 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 /* For ORDER
6d0f0 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 BY, column numbe
6d100 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 r in result set
6d110 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 */. u16 iAlia
6d120 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s; /*
6d130 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 Index into Pars
6d140 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a e.aAlias[] for z
6d150 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 Name */. } *a;
6d160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d170 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f /* One entry fo
6d180 72 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f r each expressio
6d190 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 n */.};../*.** A
6d1a0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
6d1b0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e is structure can
6d1c0 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c hold a simple l
6d1d0 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 ist of identifie
6d1e0 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 rs,.** such as t
6d1f0 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 he list "a,b,c"
6d200 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 in the following
6d210 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a statements:.**.
6d220 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 ** INSERT I
6d230 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c NTO t(a,b,c) VAL
6d240 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 UES ...;.**
6d250 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 CREATE INDEX id
6d260 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a x ON t(a,b,c);.*
6d270 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 * CREATE TR
6d280 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 IGGER trig BEFOR
6d290 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c E UPDATE ON t(a,
6d2a0 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 b,c) ...;.**.**
6d2b0 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 The IdList.a.idx
6d2c0 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 field is used w
6d2d0 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 hen the IdList r
6d2e0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 epresents the li
6d2f0 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 st of.** column
6d300 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 names after a ta
6d310 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 ble name in an I
6d320 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e NSERT statement.
6d330 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 In the stateme
6d340 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 nt.**.** INS
6d350 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 ERT INTO t(a,b,c
6d360 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 ) ....**.** If "
6d370 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 a" is the k-th c
6d380 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 olumn of table "
6d390 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e t", then IdList.
6d3a0 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a a[0].idx==k..*/.
6d3b0 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a struct IdList {.
6d3c0 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f struct IdList_
6d3d0 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 item {. char
6d3e0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 *zName; /*
6d3f0 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e Name of the iden
6d400 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e tifier */. in
6d410 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 t idx;
6d420 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 /* Index in some
6d430 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 Table.aCol[] of
6d440 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 a column named
6d450 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b zName */. } *a;
6d460 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 . int nId;
6d470 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6d480 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 identifiers on
6d490 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e the list */. in
6d4a0 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f t nAlloc; /
6d4b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
6d4c0 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ies allocated fo
6d4d0 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d r a[] below */.}
6d4e0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 ;../*.** The bit
6d4f0 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 mask datatype de
6d500 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 fined below is u
6d510 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 sed for various
6d520 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a optimizations..*
6d530 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 *.** Changing th
6d540 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 is from a 64-bit
6d550 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 to a 32-bit typ
6d560 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d e limits the num
6d570 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 ber of.** tables
6d580 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 in a join to 32
6d590 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 instead of 64.
6d5a0 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 But it also red
6d5b0 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a uces the size.**
6d5c0 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 of the library
6d5d0 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 by 738 bytes on
6d5e0 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ix86..*/.typedef
6d5f0 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f u64 Bitmask;../
6d600 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 *.** The number
6d610 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 of bits in a Bit
6d620 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 mask. "BMS" mea
6d630 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 ns "BitMask Size
6d640 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d "..*/.#define BM
6d650 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 S ((int)(sizeof
6d660 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f (Bitmask)*8))../
6d670 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
6d680 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 ng structure des
6d690 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 cribes the FROM
6d6a0 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 clause of a SELE
6d6b0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a CT statement..**
6d6c0 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 Each table or s
6d6d0 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 ubquery in the F
6d6e0 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 ROM clause is a
6d6f0 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 separate element
6d700 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 of.** the SrcLi
6d710 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a st.a[] array..**
6d720 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 .** With the add
6d730 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c ition of multipl
6d740 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
6d750 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e rt, the followin
6d760 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 g structure.** c
6d770 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 an also be used
6d780 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 to describe a pa
6d790 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 rticular table s
6d7a0 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 uch as the table
6d7b0 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 that.** is modi
6d7c0 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 fied by an INSER
6d7d0 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 T, DELETE, or UP
6d7e0 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 DATE statement.
6d7f0 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c In standard SQL
6d800 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c ,.** such a tabl
6d810 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 e must be a simp
6d820 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 le name: ID. Bu
6d830 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 t in SQLite, the
6d840 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f table can.** no
6d850 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 w be identified
6d860 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 by a database na
6d870 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 me, a dot, then
6d880 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 the table name:
6d890 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ID.ID..**.** The
6d8a0 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 jointype starts
6d8b0 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 out showing the
6d8c0 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 join type betwe
6d8d0 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 en the current t
6d8e0 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 able.** and the
6d8f0 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 next table on th
6d900 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 e list. The par
6d910 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c ser builds the l
6d920 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a ist this way..**
6d930 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c But sqlite3SrcL
6d940 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 istShiftJoinType
6d950 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 () later shifts
6d960 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f the jointypes so
6d970 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f that each.** jo
6d980 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 intype expresses
6d990 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 the join betwee
6d9a0 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 n the table and
6d9b0 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 the previous tab
6d9c0 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 le..*/.struct Sr
6d9d0 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 cList {. i16 nS
6d9e0 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 rc; /* Nu
6d9f0 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f mber of tables o
6da00 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 r subqueries in
6da10 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
6da20 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b */. i16 nAlloc;
6da30 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6da40 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 of entries alloc
6da50 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f ated in a[] belo
6da60 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 w */. struct Sr
6da70 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 cList_item {.
6da80 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 char *zDatabase
6da90 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 ; /* Name of da
6daa0 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 tabase holding t
6dab0 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 his table */.
6dac0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
6dad0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
6dae0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 e table */. c
6daf0 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 har *zAlias;
6db00 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 /* The "B" part
6db10 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 of a "A AS B" p
6db20 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 hrase. zName is
6db30 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 the "A" */.
6db40 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 Table *pTab;
6db50 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c /* An SQL tabl
6db60 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
6db70 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 to zName */.
6db80 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b Select *pSelect;
6db90 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 /* A SELECT st
6dba0 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 atement used in
6dbb0 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 place of a table
6dbc0 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 name */. u8
6dbd0 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f isPopulated; /
6dbe0 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c * Temporary tabl
6dbf0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
6dc00 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 h SELECT is popu
6dc10 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 lated */. u8
6dc20 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f jointype; /
6dc30 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 * Type of join b
6dc40 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 etween this able
6dc50 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 and the previou
6dc60 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75 s */. int iCu
6dc70 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 rsor; /* Th
6dc80 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 e VDBE cursor nu
6dc90 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63 63 mber used to acc
6dca0 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20 2a ess this table *
6dcb0 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e 3b /. Expr *pOn;
6dcc0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f /* The O
6dcd0 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f N clause of a jo
6dce0 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 74 in */. IdList
6dcf0 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 54 *pUsing; /* T
6dd00 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 he USING clause
6dd10 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 of a join */.
6dd20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 64 Bitmask colUsed
6dd30 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c 3c ; /* Bit N (1<<
6dd40 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d 6e N) set if column
6dd50 20 4e 20 6f 72 20 70 54 61 62 20 69 73 20 75 73 N or pTab is us
6dd60 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 ed */. u8 not
6dd70 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 Indexed; /* T
6dd80 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 rue if there is
6dd90 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c a NOT INDEXED cl
6dda0 61 75 73 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 ause */. char
6ddb0 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a *zIndex; /*
6ddc0 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d Identifier from
6ddd0 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 "INDEXED BY <zI
6dde0 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f ndex>" clause */
6ddf0 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 . Index *pInd
6de00 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 ex; /* Index
6de10 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 structure corres
6de20 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 ponding to zInde
6de30 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d x, if any */. }
6de40 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 a[1];
6de50 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 /* One entry
6de60 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 for each identif
6de70 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 ier on the list
6de80 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 */.};../*.** Per
6de90 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 mitted values of
6dea0 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a the SrcList.a.j
6deb0 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f ointype field.*/
6dec0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 .#define JT_INNE
6ded0 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 R 0x0001
6dee0 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 /* Any kind of i
6def0 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f nner or cross jo
6df00 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 in */.#define JT
6df10 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 _CROSS 0x000
6df20 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 2 /* Explicit
6df30 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 use of the CROS
6df40 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 S keyword */.#de
6df50 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 fine JT_NATURAL
6df60 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 0x0004 /* T
6df70 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 rue for a "natur
6df80 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 al" join */.#def
6df90 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 ine JT_LEFT
6dfa0 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 0x0008 /* Le
6dfb0 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f ft outer join */
6dfc0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 .#define JT_RIGH
6dfd0 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 T 0x0010
6dfe0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a /* Right outer j
6dff0 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a oin */.#define J
6e000 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 T_OUTER 0x00
6e010 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 20 /* The "OU
6e020 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 TER" keyword is
6e030 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 present */.#defi
6e040 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 ne JT_ERROR
6e050 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 0x0040 /* unk
6e060 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 nown or unsuppor
6e070 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f ted join type */
6e080 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 ../*.** For each
6e090 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20 nested loop in
6e0a0 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 a WHERE clause i
6e0b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 mplementation, t
6e0c0 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20 he WhereInfo.**
6e0d0 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
6e0e0 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 ns a single inst
6e0f0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
6e100 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 73 74 ucture. This st
6e110 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e ructure.** is in
6e120 74 65 6e 64 65 64 20 74 6f 20 62 65 20 70 72 69 tended to be pri
6e130 76 61 74 65 20 74 68 65 20 74 68 65 20 77 68 65 vate the the whe
6e140 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20 re.c module and
6e150 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a should not be.**
6e160 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 access or modif
6e170 69 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 ied by other mod
6e180 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ules..**.** The
6e190 70 49 64 78 49 6e 66 6f 20 61 6e 64 20 70 42 65 pIdxInfo and pBe
6e1a0 73 74 49 64 78 20 66 69 65 6c 64 73 20 61 72 65 stIdx fields are
6e1b0 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70 69 used to help pi
6e1c0 63 6b 20 74 68 65 20 62 65 73 74 0a 2a 2a 20 69 ck the best.** i
6e1d0 6e 64 65 78 20 6f 6e 20 61 20 76 69 72 74 75 61 ndex on a virtua
6e1e0 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70 49 l table. The pI
6e1f0 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63 dxInfo pointer c
6e200 6f 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e 67 ontains indexing
6e210 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
6e220 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 for the i-th tab
6e230 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 le in the FROM c
6e240 6c 61 75 73 65 20 62 65 66 6f 72 65 20 72 65 6f lause before reo
6e250 72 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 rdering..** All
6e260 74 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 the pIdxInfo poi
6e270 6e 74 65 72 73 20 61 72 65 20 66 72 65 65 64 20 nters are freed
6e280 62 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 by whereInfoFree
6e290 28 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a () in where.c..*
6e2a0 2a 20 54 68 65 20 70 42 65 73 74 49 64 78 20 70 * The pBestIdx p
6e2b0 6f 69 6e 74 65 72 20 69 73 20 61 20 63 6f 70 79 ointer is a copy
6e2c0 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f 72 of pIdxInfo for
6e2d0 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20 the i-th table
6e2e0 61 66 74 65 72 0a 2a 2a 20 46 52 4f 4d 20 63 6c after.** FROM cl
6e2f0 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 20 20 ause ordering.
6e300 54 68 69 73 20 69 73 20 61 20 6c 69 74 74 6c 65 This is a little
6e310 20 63 6f 6e 66 75 73 69 6e 67 20 73 6f 20 49 20 confusing so I
6e320 77 69 6c 6c 20 72 65 70 65 61 74 0a 2a 2a 20 69 will repeat.** i
6e330 74 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77 t in different w
6e340 6f 72 64 73 2e 20 20 57 68 65 72 65 49 6e 66 6f ords. WhereInfo
6e350 2e 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 20 69 .a[i].pIdxInfo i
6e360 73 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 s index informat
6e370 69 6f 6e 20 0a 2a 2a 20 66 6f 72 20 57 68 65 72 ion .** for Wher
6e380 65 49 6e 66 6f 2e 70 54 61 62 4c 69 73 74 2e 61 eInfo.pTabList.a
6e390 5b 69 5d 2e 20 20 57 68 65 72 65 49 6e 66 6f 2e [i]. WhereInfo.
6e3a0 61 5b 69 5d 2e 70 42 65 73 74 49 6e 66 6f 20 69 a[i].pBestInfo i
6e3b0 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 69 s the.** index i
6e3c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 nformation for t
6e3d0 68 65 20 69 2d 74 68 20 6c 6f 6f 70 20 6f 66 20 he i-th loop of
6e3e0 74 68 65 20 6a 6f 69 6e 2e 20 20 70 42 65 73 74 the join. pBest
6e3f0 49 6e 66 6f 20 69 73 20 61 6c 77 61 79 73 0a 2a Info is always.*
6e400 2a 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 * either NULL or
6e410 20 61 20 63 6f 70 79 20 6f 66 20 73 6f 6d 65 20 a copy of some
6e420 70 49 64 78 49 6e 66 6f 2e 20 20 53 6f 20 66 6f pIdxInfo. So fo
6e430 72 20 63 6c 65 61 6e 75 70 20 69 74 20 69 73 20 r cleanup it is
6e440 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 74 .** sufficient t
6e450 6f 20 66 72 65 65 20 61 6c 6c 20 6f 66 20 74 68 o free all of th
6e460 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 e pIdxInfo point
6e470 65 72 73 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 72 75 ers..** .*/.stru
6e480 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a ct WhereLevel {.
6e490 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 int iFrom;
6e4a0 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 /* Which
6e4b0 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46 52 entry in the FR
6e4c0 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 OM clause */. i
6e4d0 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 nt flags;
6e4e0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 /* Flags as
6e4f0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
6e500 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e is level */. in
6e510 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 t iMem;
6e520 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6d 65 6d /* First mem
6e530 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 62 79 ory cell used by
6e540 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 this level */.
6e550 20 69 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 int iLeftJoin;
6e560 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
6e570 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d cell used to im
6e580 70 6c 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54 plement LEFT OUT
6e590 45 52 20 4a 4f 49 4e 20 2a 2f 0a 20 20 49 6e 64 ER JOIN */. Ind
6e5a0 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 ex *pIdx;
6e5b0 20 20 20 2f 2a 20 49 6e 64 65 78 20 75 73 65 64 /* Index used
6e5c0 2e 20 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 69 6e . NULL if no in
6e5d0 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 dex */. int iTa
6e5e0 62 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f bCur; /
6e5f0 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f * The VDBE curso
6e600 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 r used to access
6e610 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
6e620 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 int iIdxCur;
6e630 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 /* The VDB
6e640 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f E cursor used to
6e650 20 61 63 65 73 73 73 20 70 49 64 78 20 2a 2f 0a acesss pIdx */.
6e660 20 20 69 6e 74 20 62 72 6b 3b 20 20 20 20 20 20 int brk;
6e670 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 /* Jump
6e680 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 here to break ou
6e690 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f t of the loop */
6e6a0 0a 20 20 69 6e 74 20 6e 78 74 3b 20 20 20 20 20 . int nxt;
6e6b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 /* Jump
6e6c0 20 68 65 72 65 20 74 6f 20 73 74 61 72 74 20 74 here to start t
6e6d0 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69 he next IN combi
6e6e0 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nation */. int
6e6f0 63 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 cont;
6e700 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
6e710 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 o continue with
6e720 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79 the next loop cy
6e730 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 cle */. int top
6e740 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
6e750 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75 63 74 * First instruct
6e760 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 ion of interior
6e770 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 of the loop */.
6e780 20 69 6e 74 20 6f 70 2c 20 70 31 2c 20 70 32 2c int op, p1, p2,
6e790 20 70 35 3b 20 20 20 2f 2a 20 4f 70 63 6f 64 65 p5; /* Opcode
6e7a0 20 75 73 65 64 20 74 6f 20 74 65 72 6d 69 6e 61 used to termina
6e7b0 74 65 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 te the loop */.
6e7c0 20 69 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 20 int nEq;
6e7d0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6e7e0 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e of == or IN con
6e7f0 73 74 72 61 69 6e 74 73 20 6f 6e 20 74 68 69 73 straints on this
6e800 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e loop */. int n
6e810 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 In;
6e820 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 49 4e /* Number of IN
6e830 20 6f 70 65 72 61 74 6f 72 73 20 63 6f 6e 73 74 operators const
6e840 72 61 69 6e 69 6e 67 20 74 68 69 73 20 6c 6f 6f raining this loo
6e850 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 49 6e p */. struct In
6e860 4c 6f 6f 70 20 7b 0a 20 20 20 20 69 6e 74 20 69 Loop {. int i
6e870 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 Cur;
6e880 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 /* The VDBE cu
6e890 72 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 69 rsor used by thi
6e8a0 73 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f s IN operator */
6e8b0 0a 20 20 20 20 69 6e 74 20 74 6f 70 41 64 64 72 . int topAddr
6e8c0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
6e8d0 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f op of the IN loo
6e8e0 70 20 2a 2f 0a 20 20 7d 20 2a 61 49 6e 4c 6f 6f p */. } *aInLoo
6e8f0 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p; /*
6e900 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
6e910 74 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e t each nested IN
6e920 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 73 operator */. s
6e930 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 qlite3_index_inf
6e940 6f 20 2a 70 42 65 73 74 49 64 78 3b 20 20 2f 2a o *pBestIdx; /*
6e950 20 49 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 Index informati
6e960 6f 6e 20 66 6f 72 20 74 68 69 73 20 6c 65 76 65 on for this leve
6e970 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 l */.. /* The f
6e980 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 69 ollowing field i
6e990 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72 s really not par
6e9a0 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 t of the current
6e9b0 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a level. But. *
6e9c0 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63 * we need a plac
6e9d0 65 20 74 6f 20 63 61 63 68 65 20 69 6e 64 65 78 e to cache index
6e9e0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 information for
6e9f0 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74 each table in t
6ea00 68 65 0a 20 20 2a 2a 20 46 52 4f 4d 20 63 6c 61 he. ** FROM cla
6ea10 75 73 65 20 61 6e 64 20 74 68 65 20 57 68 65 72 use and the Wher
6ea20 65 4c 65 76 65 6c 20 73 74 72 75 63 74 75 72 65 eLevel structure
6ea30 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 is a convenient
6ea40 20 70 6c 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 73 place.. */. s
6ea50 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 qlite3_index_inf
6ea60 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a o *pIdxInfo; /*
6ea70 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 Index info for
6ea80 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c n-th source tabl
6ea90 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 e */.};../*.** F
6eaa0 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 lags appropriate
6eab0 20 66 6f 72 20 74 68 65 20 77 66 6c 61 67 73 20 for the wflags
6eac0 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c parameter of sql
6ead0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 ite3WhereBegin()
6eae0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 ..*/.#define WHE
6eaf0 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 RE_ORDERBY_NORMA
6eb00 4c 20 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 2d L 0 /* No-
6eb10 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 op */.#define WH
6eb20 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 ERE_ORDERBY_MIN
6eb30 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 4f 52 1 /* OR
6eb40 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e DER BY processin
6eb50 67 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63 g for min() func
6eb60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 */.#define WHER
6eb70 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 20 20 E_ORDERBY_MAX
6eb80 20 20 20 20 20 32 20 20 20 2f 2a 20 4f 52 44 45 2 /* ORDE
6eb90 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 R BY processing
6eba0 66 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 20 2a for max() func *
6ebb0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
6ebc0 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 20 ONEPASS_DESIRED
6ebd0 20 20 20 34 20 20 20 2f 2a 20 57 61 6e 74 20 74 4 /* Want t
6ebe0 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50 o do one-pass UP
6ebf0 44 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 0a DATE/DELETE */..
6ec00 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 /*.** The WHERE
6ec10 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e clause processin
6ec20 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 g routine has tw
6ec30 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a o halves. The.*
6ec40 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 * first part doe
6ec50 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 s the start of t
6ec60 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e he WHERE loop an
6ec70 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 d the second.**
6ec80 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 half does the ta
6ec90 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 il of the WHERE
6eca0 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e loop. An instan
6ecb0 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 ce of.** this st
6ecc0 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 ructure is retur
6ecd0 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 ned by the first
6ece0 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 half and passed
6ecf0 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 .** into the sec
6ed00 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 ond half to give
6ed10 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 some continuity
6ed20 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 ..*/.struct Wher
6ed30 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20 eInfo {. Parse
6ed40 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f *pParse; /
6ed50 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
6ed60 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
6ed70 6e 74 65 78 74 20 2a 2f 0a 20 20 75 38 20 6f 6b ntext */. u8 ok
6ed80 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 OnePass;
6ed90 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65 /* Ok to use one
6eda0 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 -pass algorithm
6edb0 66 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 45 for UPDATE or DE
6edc0 4c 45 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73 LETE */. SrcLis
6edd0 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 2f t *pTabList; /
6ede0 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 * List of tables
6edf0 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a in the join */.
6ee00 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20 20 20 int iTop;
6ee10 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 /* The ve
6ee20 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 ry beginning of
6ee30 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a the WHERE loop *
6ee40 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 /. int iContinu
6ee50 65 3b 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 e; /* Jump
6ee60 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 here to continu
6ee70 65 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f e with next reco
6ee80 72 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 rd */. int iBre
6ee90 61 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ak; /*
6eea0 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 Jump here to bre
6eeb0 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f ak out of the lo
6eec0 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 76 op */. int nLev
6eed0 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 el; /*
6eee0 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 Number of nested
6eef0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 71 6c 69 74 loop */. sqlit
6ef00 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a e3_index_info **
6ef10 61 70 49 6e 66 6f 3b 20 20 2f 2a 20 41 72 72 61 apInfo; /* Arra
6ef20 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f y of pointers to
6ef30 20 69 6e 64 65 78 20 69 6e 66 6f 20 73 74 72 75 index info stru
6ef40 63 74 75 72 65 73 20 2a 2f 0a 20 20 57 68 65 72 ctures */. Wher
6ef50 65 4c 65 76 65 6c 20 61 5b 31 5d 3b 20 20 20 20 eLevel a[1];
6ef60 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
6ef70 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 about each nest
6ef80 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 45 52 loop in the WHER
6ef90 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 E */.};../*.** A
6efa0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 66 NameContext def
6efb0 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20 69 ines a context i
6efc0 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f 6c n which to resol
6efd0 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c ve table and col
6efe0 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 umn.** names. T
6eff0 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69 he context consi
6f000 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 sts of a list of
6f010 20 74 61 62 6c 65 73 20 28 74 68 65 20 70 53 72 tables (the pSr
6f020 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e 64 cList) field and
6f030 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 .** a list of na
6f040 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 28 med expression (
6f050 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e 61 pEList). The na
6f060 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 6c med expression l
6f070 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 ist may.** be NU
6f080 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63 6f LL. The pSrc co
6f090 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 rresponds to the
6f0a0 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 FROM clause of
6f0b0 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 a SELECT or.** t
6f0c0 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e o the table bein
6f0d0 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 79 g operated on by
6f0e0 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
6f0f0 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68 65 or DELETE. The
6f100 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 65 .** pEList corre
6f110 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 65 sponds to the re
6f120 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 sult set of a SE
6f130 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c LECT and is NULL
6f140 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74 for.** other st
6f150 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 atements..**.**
6f160 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e NameContexts can
6f170 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65 be nested. Whe
6f180 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 n resolving name
6f190 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 s, the inner-mos
6f1a0 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 t .** context is
6f1b0 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74 2e searched first.
6f1c0 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 If no match is
6f1d0 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 74 found, the next
6f1e0 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 outer.** contex
6f1f0 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 49 t is checked. I
6f200 66 20 74 68 65 72 65 20 69 73 20 73 74 69 6c 6c f there is still
6f210 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 6e no match, the n
6f220 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 ext context.** i
6f230 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69 73 s checked. This
6f240 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75 process continu
6f250 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 es until either
6f260 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 a match is found
6f270 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 .** or all conte
6f280 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20 20 xts are check.
6f290 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73 20 When a match is
6f2a0 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 20 found, the nRef
6f2b0 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65 member of.** the
6f2c0 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e context contain
6f2d0 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69 73 ing the match is
6f2e0 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a incremented. .*
6f2f0 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75 65 *.** Each subque
6f300 72 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e 61 ry gets a new Na
6f310 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 meContext. The
6f320 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69 6e pNext field poin
6f330 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d ts to the.** Nam
6f340 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 eContext in the
6f350 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20 54 parent query. T
6f360 68 75 73 20 74 68 65 20 70 72 6f 63 65 73 73 20 hus the process
6f370 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a of scanning the.
6f380 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c ** NameContext l
6f390 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 ist corresponds
6f3a0 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68 72 to searching thr
6f3b0 6f 75 67 68 20 73 75 63 63 65 73 73 69 76 65 6c ough successivel
6f3c0 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 75 y outer.** subqu
6f3d0 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f eries looking fo
6f3e0 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 r a match..*/.st
6f3f0 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 ruct NameContext
6f400 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 {. Parse *pPar
6f410 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 se; /* The
6f420 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 63 parser */. Src
6f430 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20 List *pSrcList;
6f440 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 /* One or more
6f450 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 tables used to
6f460 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f resolve names */
6f470 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c . ExprList *pEL
6f480 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f ist; /* Optio
6f490 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 nal list of name
6f4a0 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f d expressions */
6f4b0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
6f4c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6f4d0 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c r of names resol
6f4e0 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74 ved by this cont
6f4f0 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 ext */. int nEr
6f500 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r; /*
6f510 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 Number of error
6f520 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 s encountered wh
6f530 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 ile resolving na
6f540 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f mes */. u8 allo
6f550 77 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f 2a wAgg; /*
6f560 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 Aggregate funct
6f570 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 72 ions allowed her
6f580 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 67 e */. u8 hasAgg
6f590 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
6f5a0 72 75 65 20 69 66 20 61 67 67 72 65 67 61 74 65 rue if aggregate
6f5b0 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20 s are seen */.
6f5c0 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20 20 u8 isCheck;
6f5d0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
6f5e0 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 resolving names
6f5f0 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 in a CHECK const
6f600 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e raint */. int n
6f610 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 Depth;
6f620 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62 71 /* Depth of subq
6f630 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e 20 uery recursion.
6f640 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 69 1 for no recursi
6f650 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 on */. AggInfo
6f660 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 *pAggInfo; /*
6f670 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
6f680 74 20 61 67 67 72 65 67 61 74 65 73 20 61 74 20 t aggregates at
6f690 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 this level */.
6f6a0 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 NameContext *pNe
6f6b0 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 xt; /* Next out
6f6c0 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e er name context.
6f6d0 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 NULL for outer
6f6e0 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a most */.};../*.*
6f6f0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
6f700 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
6f710 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
6f720 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f s all informatio
6f730 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 67 n.** needed to g
6f740 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 enerate code for
6f750 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 54 a single SELECT
6f760 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a statement..**.*
6f770 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20 * nLimit is set
6f780 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 to -1 if there i
6f790 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 s no LIMIT claus
6f7a0 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73 e. nOffset is s
6f7b0 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 et to 0..** If t
6f7c0 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20 here is a LIMIT
6f7d0 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 73 clause, the pars
6f7e0 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74 er sets nLimit t
6f7f0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 o the value of t
6f800 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20 he.** limit and
6f810 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 76 nOffset to the v
6f820 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73 alue of the offs
6f830 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 72 et (or 0 if ther
6f840 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 e is not.** offs
6f850 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72 20 et). But later
6f860 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e on, nLimit and n
6f870 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74 68 Offset become th
6f880 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f e memory locatio
6f890 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 42 ns.** in the VDB
6f8a0 45 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 E that record th
6f8b0 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 e limit and offs
6f8c0 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a et counters..**.
6f8d0 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b ** addrOpenEphm[
6f8e0 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 69 ] entries contai
6f8f0 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 n the address of
6f900 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 OP_OpenEphemera
6f910 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 l opcodes..** Th
6f920 65 73 65 20 61 64 64 72 65 73 73 65 73 20 6d 75 ese addresses mu
6f930 73 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f 20 st be stored so
6f940 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20 62 that we can go b
6f950 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a ack and fill in.
6f960 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e 46 ** the P4_KEYINF
6f970 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65 74 O and P2 paramet
6f980 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 74 ers later. Neit
6f990 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 her the KeyInfo
6f9a0 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 nor.** the numbe
6f9b0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
6f9c0 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 74 P2 can be comput
6f9d0 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ed at the same t
6f9e0 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50 ime.** as the OP
6f9f0 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 75 _OpenEphm instru
6fa00 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 62 ction is coded b
6fa10 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e ecause not.** en
6fa20 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ough information
6fa30 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f about the compo
6fa40 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e 6f und query is kno
6fa50 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 wn at that point
6fa60 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f ..** The KeyInfo
6fa70 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 for addrOpenTra
6fa80 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e n[0] and [1] con
6fa90 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20 tains collating
6faa0 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 sequences.** for
6fab0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e the result set.
6fac0 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f The KeyInfo fo
6fad0 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32 r addrOpenTran[2
6fae0 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 ] contains colla
6faf0 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 ting.** sequence
6fb00 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20 s for the ORDER
6fb10 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 BY clause..*/.st
6fb20 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 20 ruct Select {.
6fb30 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
6fb40 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 ; /* The fi
6fb50 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 75 elds of the resu
6fb60 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 lt */. u8 op;
6fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6fb80 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 * One of: TK_UNI
6fb90 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 ON TK_ALL TK_INT
6fba0 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54 ERSECT TK_EXCEPT
6fbb0 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e */. char affin
6fbc0 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ity; /*
6fbd0 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 20 MakeRecord with
6fbe0 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 66 6f this affinity fo
6fbf0 72 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75 r SRT_Set */. u
6fc00 31 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 20 16 selFlags;
6fc10 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 /* Various
6fc20 20 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a SF_* values */.
6fc30 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b SrcList *pSrc;
6fc40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
6fc50 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 FROM clause */.
6fc60 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 Expr *pWhere;
6fc70 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 /* The W
6fc80 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 HERE clause */.
6fc90 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 ExprList *pGrou
6fca0 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 pBy; /* The G
6fcb0 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a ROUP BY clause *
6fcc0 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e /. Expr *pHavin
6fcd0 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 g; /* Th
6fce0 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 e HAVING clause
6fcf0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
6fd00 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54 OrderBy; /* T
6fd10 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
6fd20 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a se */. Select *
6fd30 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20 2f pPrior; /
6fd40 2a 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20 69 * Prior select i
6fd50 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c n a compound sel
6fd60 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f ect statement */
6fd70 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 74 . Select *pNext
6fd80 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 ; /* Nex
6fd90 74 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65 20 t select to the
6fda0 6c 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 left in a compou
6fdb0 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a nd */. Select *
6fdc0 70 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f pRightmost; /
6fdd0 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c * Right-most sel
6fde0 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e ect in a compoun
6fdf0 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 d select stateme
6fe00 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c nt */. Expr *pL
6fe10 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f imit; /
6fe20 2a 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69 * LIMIT expressi
6fe30 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e on. NULL means n
6fe40 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78 ot used. */. Ex
6fe50 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 20 pr *pOffset;
6fe60 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 65 /* OFFSET e
6fe70 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 xpression. NULL
6fe80 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 means not used.
6fe90 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c */. int iLimit,
6fea0 20 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d iOffset; /* M
6feb0 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 emory registers
6fec0 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 holding LIMIT &
6fed0 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73 20 OFFSET counters
6fee0 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65 */. int addrOpe
6fef0 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f nEphm[3]; /* O
6ff00 50 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f P_OpenEphem opco
6ff10 64 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 des related to t
6ff20 68 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b his select */.};
6ff30 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 ../*.** Allowed
6ff40 76 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 63 values for Selec
6ff50 74 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 65 t.selFlags. The
6ff60 20 22 53 46 22 20 70 72 65 66 69 78 20 73 74 61 "SF" prefix sta
6ff70 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 nds for.** "Sele
6ff80 63 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 ct Flag"..*/.#de
6ff90 66 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 74 fine SF_Distinct
6ffa0 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 0x0001
6ffb0 2f 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 /* Output should
6ffc0 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a be DISTINCT */.
6ffd0 23 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c #define SF_Resol
6ffe0 76 65 64 20 20 20 20 20 20 20 20 30 78 30 30 30 ved 0x000
6fff0 32 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 2 /* Identifier
70000 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f s have been reso
70010 6c 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 lved */.#define
70020 53 46 5f 41 67 67 72 65 67 61 74 65 20 20 20 20 SF_Aggregate
70030 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43 6f 0x0004 /* Co
70040 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 ntains aggregate
70050 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 functions */.#d
70060 65 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70 68 efine SF_UsesEph
70070 65 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38 20 emeral 0x0008
70080 20 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70 65 /* Uses the Ope
70090 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 nEphemeral opcod
700a0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f e */.#define SF_
700b0 45 78 70 61 6e 64 65 64 20 20 20 20 20 20 20 20 Expanded
700c0 30 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 74 0x0010 /* sqlit
700d0 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 e3SelectExpand()
700e0 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 called on this
700f0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 61 */.#define SF_Ha
70100 73 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30 78 sTypeInfo 0x
70110 30 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75 0020 /* FROM su
70120 62 71 75 65 72 69 65 73 20 68 61 76 65 20 54 61 bqueries have Ta
70130 62 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a ble metadata */.
70140 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 ../*.** The resu
70150 6c 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20 lts of a select
70160 63 61 6e 20 62 65 20 64 69 73 74 72 69 62 75 74 can be distribut
70170 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 ed in several wa
70180 79 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 54 ys. The.** "SRT
70190 22 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20 22 " prefix means "
701a0 53 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54 79 SELECT Result Ty
701b0 70 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 pe"..*/.#define
701c0 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 SRT_Union
701d0 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 1 /* Store res
701e0 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20 61 ult as keys in a
701f0 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 n index */.#defi
70200 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 ne SRT_Except
70210 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65 2 /* Remove
70220 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 55 result from a U
70230 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 NION index */.#d
70240 65 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74 73 efine SRT_Exists
70250 20 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74 6f 3 /* Sto
70260 72 65 20 31 20 69 66 20 74 68 65 20 72 65 73 75 re 1 if the resu
70270 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 lt is not empty
70280 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 44 */.#define SRT_D
70290 69 73 63 61 72 64 20 20 20 20 20 20 34 20 20 2f iscard 4 /
702a0 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 * Do not save th
702b0 65 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68 65 e results anywhe
702c0 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 re */../* The OR
702d0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 DER BY clause is
702e0 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c ignored for all
702f0 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a 2f of the above */
70300 0a 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 62 .#define Ignorab
70310 6c 65 4f 72 64 65 72 62 79 28 58 29 20 28 28 58 leOrderby(X) ((X
70320 2d 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 69 ->eDest)<=SRT_Di
70330 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 20 scard)..#define
70340 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20 20 SRT_Output
70350 20 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 61 5 /* Output ea
70360 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 ch row of result
70370 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
70380 4d 65 6d 20 20 20 20 20 20 20 20 20 20 36 20 20 Mem 6
70390 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 /* Store result
703a0 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c in a memory cell
703b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
703c0 53 65 74 20 20 20 20 20 20 20 20 20 20 37 20 20 Set 7
703d0 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 /* Store results
703e0 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 as keys in an i
703f0 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ndex */.#define
70400 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 SRT_Table
70410 20 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 8 /* Store res
70420 75 6c 74 20 61 73 20 64 61 74 61 20 77 69 74 68 ult as data with
70430 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f an automatic ro
70440 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 wid */.#define S
70450 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 20 RT_EphemTab
70460 39 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72 61 9 /* Create tra
70470 6e 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20 73 nsient tab and s
70480 74 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 tore like SRT_Ta
70490 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ble */.#define S
704a0 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 31 RT_Coroutine 1
704b0 30 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 0 /* Generate a
704c0 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72 single row of r
704d0 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 esult */../*.**
704e0 41 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64 A structure used
704f0 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 to customize th
70500 65 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20 73 e behaviour of s
70510 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20 qlite3Select().
70520 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 See.** comments
70530 61 62 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c above sqlite3Sel
70540 65 63 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c ect() for detail
70550 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 s..*/.typedef st
70560 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 ruct SelectDest
70570 53 65 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75 SelectDest;.stru
70580 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a ct SelectDest {.
70590 20 20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20 u8 eDest;
705a0 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 /* How to di
705b0 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73 spose of the res
705c0 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66 ults */. u8 aff
705d0 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 inity; /* A
705e0 66 66 69 6e 69 74 79 20 75 73 65 64 20 77 68 65 ffinity used whe
705f0 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 n eDest==SRT_Set
70600 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b */. int iParm;
70610 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 /* A par
70620 61 6d 65 74 65 72 20 75 73 65 64 20 62 79 20 74 ameter used by t
70630 68 65 20 65 44 65 73 74 20 64 69 73 70 6f 73 61 he eDest disposa
70640 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e l method */. in
70650 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 t iMem;
70660 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72 /* Base register
70670 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 where results a
70680 72 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 re written */.
70690 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 int nMem;
706a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
706b0 65 67 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 egisters allocat
706c0 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ed */.};../*.**
706d0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f An SQL parser co
706e0 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f ntext. A copy o
706f0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
70700 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 is passed throu
70710 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72 gh.** the parser
70720 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61 and down into a
70730 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63 ll the parser ac
70740 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20 tion routine in
70750 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72 order to.** carr
70760 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 y around informa
70770 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f tion that is glo
70780 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72 bal to the entir
70790 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 e parse..**.** T
707a0 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 he structure is
707b0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f divided into two
707c0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68 parts. When th
707d0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 e parser and cod
707e0 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61 e.** generate ca
707f0 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65 ll themselves re
70800 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66 cursively, the f
70810 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 irst part of the
70820 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 structure.** is
70830 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68 constant but th
70840 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73 e second part is
70850 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65 reset at the be
70860 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 ginning and end
70870 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72 of.** each recur
70880 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sion..**.** The
70890 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 nTableLock and a
708a0 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62 TableLock variab
708b0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 les are only use
708c0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d d if the shared-
708d0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72 cache .** featur
708e0 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66 e is enabled (if
708f0 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75 sqlite3Tsd()->u
70900 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20 seSharedData is
70910 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a true). They are.
70920 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 ** used to store
70930 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c the set of tabl
70940 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64 e-locks required
70950 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e by the statemen
70960 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 t being.** compi
70970 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71 led. Function sq
70980 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 lite3TableLock()
70990 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 is used to add
709a0 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a entries to the.*
709b0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 * list..*/.struc
709c0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69 t Parse {. sqli
709d0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
709e0 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74 /* The main dat
709f0 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 abase structure
70a00 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
70a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
70a20 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78 urn code from ex
70a30 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 ecution */. cha
70a40 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 r *zErrMsg;
70a50 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 /* An error me
70a60 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20 ssage */. Vdbe
70a70 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20 *pVdbe;
70a80 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72 /* An engine for
70a90 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62 executing datab
70aa0 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a ase bytecode */.
70ab0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74 u8 colNamesSet
70ac0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61 ; /* TRUE a
70ad0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 fter OP_ColumnNa
70ae0 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75 me has been issu
70af0 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20 ed to pVdbe */.
70b00 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20 u8 nameClash;
70b10 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61 /* A perma
70b20 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20 nent table name
70b30 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d clashes with tem
70b40 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a p table name */.
70b50 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61 u8 checkSchema
70b60 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73 ; /* Causes
70b70 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 schema cookie c
70b80 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 heck after an er
70b90 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74 ror */. u8 nest
70ba0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ed; /*
70bb0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
70bc0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70 d calls to the p
70bd0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72 arser/code gener
70be0 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72 ator */. u8 par
70bf0 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f seError; /
70c00 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70 * True after a p
70c10 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54 arsing error. T
70c20 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20 icket #1794 */.
70c30 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20 u8 nTempReg;
70c40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
70c50 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 of temporary reg
70c60 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52 isters in aTempR
70c70 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65 eg[] */. u8 nTe
70c80 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f mpInUse; /
70c90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d * Number of aTem
70ca0 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79 pReg[] currently
70cb0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
70cc0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38 int aTempReg[8
70cd0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e ]; /* Holdin
70ce0 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f g area for tempo
70cf0 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a rary registers *
70d00 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65 /. int nRangeRe
70d10 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 g; /* Size
70d20 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 of the temporar
70d30 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b y register block
70d40 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65 */. int iRange
70d50 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 Reg; /* Fi
70d60 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 rst register in
70d70 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
70d80 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e er block */. in
70d90 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 t nErr;
70da0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
70db0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20 errors seen */.
70dc0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20 int nTab;
70dd0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
70de0 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c of previously al
70df0 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72 located VDBE cur
70e00 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d sors */. int nM
70e10 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
70e20 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
70e30 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f ry cells used so
70e40 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 far */. int nS
70e50 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f et; /
70e60 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 * Number of sets
70e70 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a used so far */.
70e80 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20 int ckBase;
70e90 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 /* Base r
70ea0 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20 egister of data
70eb0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e during check con
70ec0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e straints */. in
70ed0 74 20 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 t disableColCach
70ee0 65 3b 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69 e; /* True to di
70ef0 73 61 62 6c 65 20 61 64 64 69 6e 67 20 74 6f 20 sable adding to
70f00 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a column cache */.
70f10 20 20 69 6e 74 20 6e 43 6f 6c 43 61 63 68 65 3b int nColCache;
70f20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
70f30 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 of entries in t
70f40 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 he column cache
70f50 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 43 61 63 */. int iColCac
70f60 68 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 he; /* Nex
70f70 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 t entry of the c
70f80 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63 65 20 ache to replace
70f90 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c */. struct yCol
70fa0 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e 74 20 Cache {. int
70fb0 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 iTable;
70fc0 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f /* Table curso
70fd0 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 r number */.
70fe0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 int iColumn;
70ff0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 /* Table c
71000 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a olumn number */.
71010 20 20 20 20 63 68 61 72 20 61 66 66 43 68 61 6e char affChan
71020 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ge; /* Tru
71030 65 20 69 66 20 74 68 69 73 20 72 65 67 69 73 74 e if this regist
71040 65 72 20 68 61 73 20 68 61 64 20 61 6e 20 61 66 er has had an af
71050 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 2a 2f finity change */
71060 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 . int iReg;
71070 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
71080 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 76 gister holding v
71090 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c alue of this col
710a0 75 6d 6e 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 umn */. } aColC
710b0 61 63 68 65 5b 31 30 5d 3b 20 20 20 20 20 2f 2a ache[10]; /*
710c0 20 4f 6e 65 20 66 6f 72 20 65 61 63 68 20 76 61 One for each va
710d0 6c 69 64 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 lid column cache
710e0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 20 entry */. u32
710f0 77 72 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 20 writeMask;
71100 20 2f 2a 20 53 74 61 72 74 20 61 20 77 72 69 74 /* Start a writ
71110 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e e transaction on
71120 20 74 68 65 73 65 20 64 61 74 61 62 61 73 65 73 these databases
71130 20 2a 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65 */. u32 cookie
71140 4d 61 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69 Mask; /* Bi
71150 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 20 tmask of schema
71160 76 65 72 69 66 69 65 64 20 64 61 74 61 62 61 73 verified databas
71170 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b es */. int cook
71180 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20 ieGoto; /*
71190 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f Address of OP_Go
711a0 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72 to to cookie ver
711b0 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65 ifier subroutine
711c0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 */. int cookie
711d0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58 Value[SQLITE_MAX
711e0 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f _ATTACHED+2]; /
711f0 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b * Values of cook
71200 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f ies to verify */
71210 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
71220 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
71230 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f E. int nTableLo
71240 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ck; /* Nu
71250 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e mber of locks in
71260 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20 aTableLock */.
71270 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62 TableLock *aTab
71280 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69 leLock; /* Requi
71290 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 red table locks
712a0 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 for shared-cache
712b0 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a mode */.#endif.
712c0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 int regRowid;
712d0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
712e0 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 er holding rowid
712f0 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45 of CREATE TABLE
71300 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 entry */. int
71310 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 regRoot;
71320 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
71330 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e ding root page n
71340 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62 umber for new ob
71350 6a 65 63 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41 jects */.. /* A
71360 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e 74 bove is constant
71370 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73 69 between recursi
71380 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 72 ons. Below is r
71390 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64 20 eset before and
713a0 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 20 after. ** each
713b0 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20 recursion */..
713c0 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20 int nVar;
713d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
713e0 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73 20 f '?' variables
713f0 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c 20 seen in the SQL
71400 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 so far */. int
71410 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20 20 nVarExpr;
71420 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 /* Number of us
71430 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 ed slots in apVa
71440 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74 rExpr[] */. int
71450 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20 nVarExprAlloc;
71460 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
71470 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 69 llocated slots i
71480 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f n apVarExpr[] */
71490 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72 45 . Expr **apVarE
714a0 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 xpr; /* Point
714b0 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 20 ers to :aaa and
714c0 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20 65 $aaaa wildcard e
714d0 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 xpressions */.
714e0 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 int nAlias;
714f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
71500 66 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 f aliased result
71510 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a set columns */.
71520 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20 int *aAlias;
71530 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
71540 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 er used to hold
71550 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 2a aliased result *
71560 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 /. u8 explain;
71570 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
71580 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20 if the EXPLAIN
71590 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e flag is found on
715a0 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 the query */.
715b0 54 6f 6b 65 6e 20 73 45 72 72 54 6f 6b 65 6e 3b Token sErrToken;
715c0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 /* The toke
715d0 6e 20 61 74 20 77 68 69 63 68 20 74 68 65 20 65 n at which the e
715e0 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f rror occurred */
715f0 0a 20 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f . Token sNameTo
71600 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e ken; /* Token
71610 20 77 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65 with unqualifie
71620 64 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 d schema object
71630 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 name */. Token
71640 73 4c 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f sLastToken; /
71650 2a 20 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e * The last token
71660 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e parsed */. con
71670 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 st char *zSql;
71680 20 20 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 /* All SQL tex
71690 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 t */. const cha
716a0 72 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 r *zTail; /* A
716b0 6c 6c 20 53 51 4c 20 74 65 78 74 20 70 61 73 74 ll SQL text past
716c0 20 74 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f the last semico
716d0 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 lon parsed */.
716e0 54 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 Table *pNewTable
716f0 3b 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 ; /* A table
71700 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 being constructe
71710 64 20 62 79 20 43 52 45 41 54 45 20 54 41 42 4c d by CREATE TABL
71720 45 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a E */. Trigger *
71730 70 4e 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 pNewTrigger;
71740 20 2f 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65 /* Trigger unde
71750 72 20 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61 r construct by a
71760 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 CREATE TRIGGER
71770 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 61 63 */. TriggerStac
71780 6b 20 2a 74 72 69 67 53 74 61 63 6b 3b 20 20 2f k *trigStack; /
71790 2a 20 54 72 69 67 67 65 72 20 61 63 74 69 6f 6e * Trigger action
717a0 73 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f s being coded */
717b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
717c0 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20 AuthContext; /*
717d0 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 The 6th paramete
717e0 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63 r to db->xAuth c
717f0 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e allbacks */.#ifn
71800 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
71810 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54 VIRTUALTABLE. T
71820 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20 oken sArg;
71830 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d /* Com
71840 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 20 plete text of a
71850 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 module argument
71860 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 56 */. u8 declareV
71870 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 tab;
71880 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 64 /* True if insid
71890 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 e sqlite3_declar
718a0 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e e_vtab() */. in
718b0 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 t nVtabLock;
718c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
718d0 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 er of virtual ta
718e0 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a bles to lock */.
718f0 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 Table **apVtab
71900 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 Lock; /*
71910 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75 Pointer to virtu
71920 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e al tables needin
71930 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e g locking */.#en
71940 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 dif. int nHeigh
71950 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
71960 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 Expression tree
71970 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72 65 height of curre
71980 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f nt sub-select */
71990 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69 . Table *pZombi
719a0 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69 eTab; /* Li
719b0 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65 st of Table obje
719c0 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61 66 cts to delete af
719d0 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a ter code gen */.
719e0 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 };..#ifdef SQLIT
719f0 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
71a00 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 20 49 4e BLE. #define IN
71a10 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 30 0a _DECLARE_VTAB 0.
71a20 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
71a30 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 IN_DECLARE_VTAB
71a40 28 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 (pParse->declare
71a50 56 74 61 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a Vtab).#endif../*
71a60 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
71a70 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
71a80 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 structure can b
71a90 65 20 64 65 63 6c 61 72 65 64 20 6f 6e 20 61 20 e declared on a
71aa0 73 74 61 63 6b 20 61 6e 64 20 75 73 65 64 0a 2a stack and used.*
71ab0 2a 20 74 6f 20 73 61 76 65 20 74 68 65 20 50 61 * to save the Pa
71ac0 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 rse.zAuthContext
71ad0 20 76 61 6c 75 65 20 73 6f 20 74 68 61 74 20 69 value so that i
71ae0 74 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 t can be restore
71af0 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 72 75 d later..*/.stru
71b00 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 20 7b ct AuthContext {
71b10 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
71b20 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 20 20 2f AuthContext; /
71b30 2a 20 50 75 74 20 73 61 76 65 64 20 50 61 72 73 * Put saved Pars
71b40 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 68 e.zAuthContext h
71b50 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a ere */. Parse *
71b60 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
71b70 20 20 20 20 20 2f 2a 20 54 68 65 20 50 61 72 73 /* The Pars
71b80 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d e structure */.}
71b90 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69 65 6c ;../*.** Bitfiel
71ba0 64 20 66 6c 61 67 73 20 66 6f 72 20 50 32 20 76 d flags for P2 v
71bb0 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 73 65 72 alue in OP_Inser
71bc0 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 74 65 0a t and OP_Delete.
71bd0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 */.#define OPFLA
71be0 47 5f 4e 43 48 41 4e 47 45 20 20 20 31 20 20 20 G_NCHANGE 1
71bf0 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 /* Set to updat
71c00 65 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f e db->nChange */
71c10 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f .#define OPFLAG_
71c20 4c 41 53 54 52 4f 57 49 44 20 32 20 20 20 20 2f LASTROWID 2 /
71c30 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 * Set to update
71c40 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f db->lastRowid */
71c50 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f .#define OPFLAG_
71c60 49 53 55 50 44 41 54 45 20 20 34 20 20 20 20 2f ISUPDATE 4 /
71c70 2a 20 54 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 * This OP_Insert
71c80 20 69 73 20 61 6e 20 73 71 6c 20 55 50 44 41 54 is an sql UPDAT
71c90 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 E */.#define OPF
71ca0 4c 41 47 5f 41 50 50 45 4e 44 20 20 20 20 38 20 LAG_APPEND 8
71cb0 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 /* This is li
71cc0 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 kely to be an ap
71cd0 70 65 6e 64 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 pend */../*. * E
71ce0 61 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73 ach trigger pres
71cf0 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 ent in the datab
71d00 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74 ase schema is st
71d10 6f 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61 ored as an insta
71d20 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 nce of. * struct
71d30 20 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a Trigger. . *. *
71d40 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 Pointers to ins
71d50 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 tances of struct
71d60 20 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f Trigger are sto
71d70 72 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e red in two ways.
71d80 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74 . * 1. In the "t
71d90 72 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61 rigHash" hash ta
71da0 62 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65 ble (part of the
71db0 20 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72 sqlite3* that r
71dc0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20 epresents the .
71dd0 2a 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20 * database).
71de0 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 This allows Trig
71df0 67 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74 ger structures t
71e00 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62 o be retrieved b
71e10 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c y name.. * 2. Al
71e20 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 l triggers assoc
71e30 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e iated with a sin
71e40 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 gle table form a
71e50 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 linked list, us
71e60 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e ing the. * pN
71e70 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 ext member of st
71e80 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20 ruct Trigger. A
71e90 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 pointer to the f
71ea0 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
71eb0 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 the. * linked
71ec0 20 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 list is stored
71ed0 61 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72 as the "pTrigger
71ee0 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 " member of the
71ef0 61 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 associated. *
71f00 20 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 struct Table..
71f10 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c *. * The "step_l
71f20 69 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e ist" member poin
71f30 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 ts to the first
71f40 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e element of a lin
71f50 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 ked list. * cont
71f60 61 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73 aining the SQL s
71f70 74 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66 tatements specif
71f80 69 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67 ied as the trigg
71f90 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a er program.. */.
71fa0 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b struct Trigger {
71fb0 0a 20 20 63 68 61 72 20 2a 6e 61 6d 65 3b 20 20 . char *name;
71fc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
71fd0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 e name of the tr
71fe0 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 igger
71ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a */.
72000 20 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 char *table;
72010 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
72020 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74 table or view t
72030 6f 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67 o which the trig
72040 67 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 ger applies */.
72050 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 u8 op;
72060 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
72070 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b of TK_DELETE, TK
72080 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 _UPDATE, TK_INSE
72090 52 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 RT */.
720a0 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 u8 tr_tm;
720b0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
720c0 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 f TRIGGER_BEFORE
720d0 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 , TRIGGER_AFTER
720e0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e */. Expr *pWhen
720f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
72100 54 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20 The WHEN clause
72110 6f 66 20 74 68 65 20 65 78 70 72 65 73 69 6f 6e of the expresion
72120 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a (may be NULL) *
72130 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c /. IdList *pCol
72140 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49 umns; /* I
72150 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50 44 f this is an UPD
72160 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c ATE OF <column-l
72170 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20 ist> trigger,.
72180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72190 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 3c the <
721a0 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20 column-list> is
721b0 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 stored here */.
721c0 20 54 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e Token nameToken
721d0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 ; /* Toke
721e0 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 7a 4e 61 n containing zNa
721f0 6d 65 2e 20 55 73 65 20 64 75 72 69 6e 67 20 70 me. Use during p
72200 61 72 73 69 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 20 arsing only */.
72210 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
72220 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 ; /* Sche
72230 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ma containing th
72240 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 e trigger */. S
72250 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d chema *pTabSchem
72260 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 a; /* Schema
72270 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
72280 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67 table */. Trigg
72290 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73 erStep *step_lis
722a0 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 t; /* Link list
722b0 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 of trigger progr
722c0 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20 20 am steps
722d0 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65 */. Trigge
722e0 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 r *pNext;
722f0 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 /* Next trigge
72300 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 r associated wit
72310 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d h the table */.}
72320 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67 ;../*.** A trigg
72330 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20 42 er is either a B
72340 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45 EFORE or an AFTE
72350 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 R trigger. The
72360 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 following consta
72370 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 nts.** determine
72380 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49 which. .**.** I
72390 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74 f there are mult
723a0 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79 iple triggers, y
723b0 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65 ou might of some
723c0 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65 BEFORE and some
723d0 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68 AFTER..** In th
723e0 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f at cases, the co
723f0 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61 nstants below ca
72400 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68 n be ORed togeth
72410 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 er..*/.#define T
72420 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31 RIGGER_BEFORE 1
72430 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 .#define TRIGGER
72440 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20 _AFTER 2../*.
72450 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
72460 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 struct TriggerS
72470 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20 73 tep is used to s
72480 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51 tore a single SQ
72490 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74 L statement. * t
724a0 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f 66 hat is a part of
724b0 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 a trigger-progr
724c0 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61 am. . *. * Insta
724d0 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 nces of struct T
724e0 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20 73 riggerStep are s
724f0 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c tored in a singl
72500 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c y linked list (l
72510 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74 inked. * using t
72520 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 he "pNext" membe
72530 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 r) referenced by
72540 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 the "step_list"
72550 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a member of the .
72560 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 * associated st
72570 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e 73 ruct Trigger ins
72580 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74 tance. The first
72590 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
725a0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20 linked list is.
725b0 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70 * the first step
725c0 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2d of the trigger-
725d0 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20 program.. * . *
725e0 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20 The "op" member
725f0 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65 indicates whethe
72600 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45 4c r this is a "DEL
72610 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20 ETE", "INSERT",
72620 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22 "UPDATE" or. * "
72630 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e SELECT" statemen
72640 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 t. The meanings
72650 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d of the other mem
72660 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e bers is determin
72670 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61 ed by the . * va
72680 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66 lue of "op" as f
72690 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f ollows:. *. * (o
726a0 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a p == TK_INSERT).
726b0 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20 * orconf ->
726c0 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f stores the ON CO
726d0 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d NFLICT algorithm
726e0 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e . * pSelect ->
726f0 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 If this is an I
72700 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 NSERT INTO ... S
72710 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d ELECT ... statem
72720 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 ent, then. *
72730 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73 this s
72740 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 tores a pointer
72750 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 to the SELECT st
72760 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69 atement. Otherwi
72770 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67 se NULL.. * targ
72780 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e et -> A token
72790 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d holding the nam
727a0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 e of the table t
727b0 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 o insert into..
727c0 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 * pExprList -> I
727d0 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 f this is an INS
727e0 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c ERT INTO ... VAL
727f0 55 45 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e UES ... statemen
72800 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 t, then. *
72810 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74 6f this sto
72820 72 65 73 20 76 61 6c 75 65 73 20 74 6f 20 62 65 res values to be
72830 20 69 6e 73 65 72 74 65 64 2e 20 4f 74 68 65 72 inserted. Other
72840 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 wise NULL.. * pI
72850 64 4c 69 73 74 20 20 20 2d 3e 20 49 66 20 74 68 dList -> If th
72860 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 is is an INSERT
72870 49 4e 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d INTO ... (<colum
72880 6e 2d 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 n-names>) VALUES
72890 20 2e 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 ... . *
728a0 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c statement,
728b0 20 74 68 65 6e 20 74 68 69 73 20 73 74 6f 72 65 then this store
728c0 73 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d s the column-nam
728d0 65 73 20 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 es to be. *
728e0 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 65 inserte
728f0 64 20 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f d into.. *. * (o
72900 70 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a p == TK_DELETE).
72910 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 * target ->
72920 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 A token holding
72930 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
72940 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 table to delete
72950 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 from.. * pWhere
72960 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 -> The WHERE
72970 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 clause of the DE
72980 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 LETE statement i
72990 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 f one is specifi
729a0 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 ed.. *
729b0 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 Otherwise NU
729c0 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d LL.. * . * (op =
729d0 3d 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 = TK_UPDATE). *
729e0 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 target -> A t
729f0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 oken holding the
72a00 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 name of the tab
72a10 6c 65 20 74 6f 20 75 70 64 61 74 65 20 72 6f 77 le to update row
72a20 73 20 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65 20 s of.. * pWhere
72a30 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 -> The WHERE
72a40 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 clause of the UP
72a50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 DATE statement i
72a60 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 f one is specifi
72a70 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 ed.. *
72a80 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 Otherwise NU
72a90 4c 4c 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74 LL.. * pExprList
72aa0 20 2d 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68 -> A list of th
72ab0 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64 e columns to upd
72ac0 61 74 65 20 61 6e 64 20 74 68 65 20 65 78 70 72 ate and the expr
72ad0 65 73 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74 essions to updat
72ae0 65 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 e. *
72af0 20 20 74 68 65 6d 20 74 6f 2e 20 53 65 65 20 73 them to. See s
72b00 71 6c 69 74 65 33 55 70 64 61 74 65 28 29 20 64 qlite3Update() d
72b10 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 ocumentation of
72b20 22 70 43 68 61 6e 67 65 73 22 0a 20 2a 20 20 20 "pChanges". *
72b30 20 20 20 20 20 20 20 20 20 20 20 61 72 67 75 6d argum
72b40 65 6e 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72 ent.. * . */.str
72b50 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 uct TriggerStep
72b60 7b 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 {. int op;
72b70 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
72b80 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b of TK_DELETE, TK
72b90 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 _UPDATE, TK_INSE
72ba0 52 54 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f RT, TK_SELECT */
72bb0 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 20 . int orconf;
72bc0 20 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52 6f /* OE_Ro
72bd0 6c 6c 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a 20 llback etc. */.
72be0 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b Trigger *pTrig;
72bf0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 69 /* The tri
72c00 67 67 65 72 20 74 68 61 74 20 74 68 69 73 20 73 gger that this s
72c10 74 65 70 20 69 73 20 61 20 70 61 72 74 20 6f 66 tep is a part of
72c20 20 2a 2f 0a 0a 20 20 53 65 6c 65 63 74 20 2a 70 */.. Select *p
72c30 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 56 Select; /* V
72c40 61 6c 69 64 20 66 6f 72 20 53 45 4c 45 43 54 20 alid for SELECT
72c50 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a 20 and sometimes .
72c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72c70 20 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 INSERT
72c80 73 74 65 70 73 20 28 77 68 65 6e 20 70 45 78 70 steps (when pExp
72c90 72 4c 69 73 74 20 3d 3d 20 30 29 20 2a 2f 0a 20 rList == 0) */.
72ca0 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20 Token target;
72cb0 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66 /* Valid f
72cc0 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 41 54 or DELETE, UPDAT
72cd0 45 2c 20 49 4e 53 45 52 54 20 73 74 65 70 73 20 E, INSERT steps
72ce0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 */. Expr *pWher
72cf0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c e; /* Val
72d00 69 64 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 id for DELETE, U
72d10 50 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 PDATE steps */.
72d20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 ExprList *pExpr
72d30 4c 69 73 74 3b 20 2f 2a 20 56 61 6c 69 64 20 66 List; /* Valid f
72d40 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d or UPDATE statem
72d50 65 6e 74 73 20 61 6e 64 20 73 6f 6d 65 74 69 6d ents and sometim
72d60 65 73 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 es .
72d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49 I
72d80 4e 53 45 52 54 20 73 74 65 70 73 20 28 77 68 65 NSERT steps (whe
72d90 6e 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 20 n pSelect == 0)
72da0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 49 64 4c */. IdL
72db0 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20 ist *pIdList;
72dc0 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 49 /* Valid for I
72dd0 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73 NSERT statements
72de0 20 6f 6e 6c 79 20 2a 2f 0a 20 20 54 72 69 67 67 only */. Trigg
72df0 65 72 53 74 65 70 20 2a 70 4e 65 78 74 3b 20 20 erStep *pNext;
72e00 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 65 20 6c /* Next in the l
72e10 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 54 72 ink-list */. Tr
72e20 69 67 67 65 72 53 74 65 70 20 2a 70 4c 61 73 74 iggerStep *pLast
72e30 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c 65 6d 65 ; /* Last eleme
72e40 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e nt in link-list.
72e50 20 56 61 6c 69 64 20 66 6f 72 20 31 73 74 20 65 Valid for 1st e
72e60 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a lem only */.};..
72e70 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*. * An instanc
72e80 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 e of struct Trig
72e90 67 65 72 53 74 61 63 6b 20 73 74 6f 72 65 73 20 gerStack stores
72ea0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 72 65 71 75 information requ
72eb0 69 72 65 64 20 64 75 72 69 6e 67 20 63 6f 64 65 ired during code
72ec0 0a 20 2a 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f . * generation o
72ed0 66 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67 f a single trigg
72ee0 65 72 20 70 72 6f 67 72 61 6d 2e 20 57 68 69 6c er program. Whil
72ef0 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 e the trigger pr
72f00 6f 67 72 61 6d 20 69 73 20 62 65 69 6e 67 0a 20 ogram is being.
72f10 2a 20 63 6f 64 65 64 2c 20 69 74 73 20 61 73 73 * coded, its ass
72f20 6f 63 69 61 74 65 64 20 54 72 69 67 67 65 72 53 ociated TriggerS
72f30 74 61 63 6b 20 69 6e 73 74 61 6e 63 65 20 69 73 tack instance is
72f40 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 pointed to by t
72f50 68 65 0a 20 2a 20 22 70 54 72 69 67 67 65 72 53 he. * "pTriggerS
72f60 74 61 63 6b 22 20 6d 65 6d 62 65 72 20 6f 66 20 tack" member of
72f70 74 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74 the Parse struct
72f80 75 72 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 70 ure.. *. * The p
72f90 54 61 62 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 Tab member point
72fa0 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 74 s to the table t
72fb0 68 61 74 20 74 72 69 67 67 65 72 73 20 61 72 65 hat triggers are
72fc0 20 62 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e 2e being coded on.
72fd0 20 54 68 65 20 0a 20 2a 20 6e 65 77 49 64 78 20 The . * newIdx
72fe0 6d 65 6d 62 65 72 20 63 6f 6e 74 61 69 6e 73 20 member contains
72ff0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
73000 20 76 64 62 65 20 63 75 72 73 6f 72 20 74 68 61 vdbe cursor tha
73010 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 t points at the
73020 74 65 6d 70 0a 20 2a 20 74 61 62 6c 65 20 74 68 temp. * table th
73030 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 6e 65 at stores the ne
73040 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20 w.* references.
73050 49 66 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e If new.* referen
73060 63 65 73 20 61 72 65 20 6e 6f 74 20 76 61 6c 69 ces are not vali
73070 64 0a 20 2a 20 66 6f 72 20 74 68 65 20 74 72 69 d. * for the tri
73080 67 67 65 72 20 62 65 69 6e 67 20 63 6f 64 65 64 gger being coded
73090 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e (for example an
730a0 20 4f 4e 20 44 45 4c 45 54 45 20 74 72 69 67 67 ON DELETE trigg
730b0 65 72 29 2c 20 74 68 65 6e 20 6e 65 77 49 64 78 er), then newIdx
730c0 0a 20 2a 20 69 73 20 73 65 74 20 74 6f 20 2d 31 . * is set to -1
730d0 2e 20 54 68 65 20 6f 6c 64 49 64 78 20 6d 65 6d . The oldIdx mem
730e0 62 65 72 20 69 73 20 61 6e 61 6c 6f 67 6f 75 73 ber is analogous
730f0 20 74 6f 20 6e 65 77 49 64 78 2c 20 66 6f 72 20 to newIdx, for
73100 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 old.* references
73110 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 4f 4e 20 43 .. *. * The ON C
73120 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 ONFLICT policy t
73130 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 o be used for th
73140 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 e trigger progra
73150 6d 20 73 74 65 70 73 20 69 73 20 73 74 6f 72 65 m steps is store
73160 64 20 0a 20 2a 20 61 73 20 74 68 65 20 6f 72 63 d . * as the orc
73170 6f 6e 66 20 6d 65 6d 62 65 72 2e 20 49 66 20 74 onf member. If t
73180 68 69 73 20 69 73 20 4f 45 5f 44 65 66 61 75 6c his is OE_Defaul
73190 74 2c 20 74 68 65 6e 20 74 68 65 20 4f 4e 20 43 t, then the ON C
731a0 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 20 0a ONFLICT clause .
731b0 20 2a 20 73 70 65 63 69 66 69 65 64 20 66 6f 72 * specified for
731c0 20 69 6e 64 69 76 69 64 75 61 6c 20 74 72 69 67 individual trig
731d0 67 65 72 73 20 73 74 65 70 73 20 69 73 20 75 73 gers steps is us
731e0 65 64 2e 0a 20 2a 0a 20 2a 20 73 74 72 75 63 74 ed.. *. * struct
731f0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 68 61 TriggerStack ha
73200 73 20 61 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 s a "pNext" memb
73210 65 72 2c 20 74 6f 20 61 6c 6c 6f 77 20 6c 69 6e er, to allow lin
73220 6b 65 64 20 6c 69 73 74 73 20 74 6f 20 62 65 0a ked lists to be.
73230 20 2a 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 * constructed.
73240 57 68 65 6e 20 63 6f 64 69 6e 67 20 6e 65 73 74 When coding nest
73250 65 64 20 74 72 69 67 67 65 72 73 20 28 74 72 69 ed triggers (tri
73260 67 67 65 72 73 20 66 69 72 65 64 20 62 79 20 6f ggers fired by o
73270 74 68 65 72 20 74 72 69 67 67 65 72 73 29 0a 20 ther triggers).
73280 2a 20 65 61 63 68 20 6e 65 73 74 65 64 20 74 72 * each nested tr
73290 69 67 67 65 72 20 73 74 6f 72 65 73 20 69 74 73 igger stores its
732a0 20 70 61 72 65 6e 74 20 74 72 69 67 67 65 72 27 parent trigger'
732b0 73 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 61 s TriggerStack a
732c0 73 20 74 68 65 20 22 70 4e 65 78 74 22 20 0a 20 s the "pNext" .
732d0 2a 20 70 6f 69 6e 74 65 72 2e 20 4f 6e 63 65 20 * pointer. Once
732e0 74 68 65 20 6e 65 73 74 65 64 20 74 72 69 67 67 the nested trigg
732f0 65 72 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65 er has been code
73300 64 2c 20 74 68 65 20 70 4e 65 78 74 20 76 61 6c d, the pNext val
73310 75 65 20 69 73 20 72 65 73 74 6f 72 65 64 0a 20 ue is restored.
73320 2a 20 74 6f 20 74 68 65 20 70 54 72 69 67 67 65 * to the pTrigge
73330 72 53 74 61 63 6b 20 6d 65 6d 62 65 72 20 6f 66 rStack member of
73340 20 74 68 65 20 50 61 72 73 65 20 73 74 75 63 74 the Parse stuct
73350 75 72 65 20 61 6e 64 20 63 6f 64 69 6e 67 20 6f ure and coding o
73360 66 20 74 68 65 20 70 61 72 65 6e 74 0a 20 2a 20 f the parent. *
73370 74 72 69 67 67 65 72 20 63 6f 6e 74 69 6e 75 65 trigger continue
73380 73 2e 0a 20 2a 0a 20 2a 20 42 65 66 6f 72 65 20 s.. *. * Before
73390 61 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 a nested trigger
733a0 20 69 73 20 63 6f 64 65 64 2c 20 74 68 65 20 6c is coded, the l
733b0 69 6e 6b 65 64 20 6c 69 73 74 20 70 6f 69 6e 74 inked list point
733c0 65 64 20 74 6f 20 62 79 20 74 68 65 20 0a 20 2a ed to by the . *
733d0 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 20 69 pTriggerStack i
733e0 73 20 73 63 61 6e 6e 65 64 20 74 6f 20 65 6e 73 s scanned to ens
733f0 75 72 65 20 74 68 61 74 20 74 68 65 20 74 72 69 ure that the tri
73400 67 67 65 72 20 69 73 20 6e 6f 74 20 61 62 6f 75 gger is not abou
73410 74 20 74 6f 20 62 65 20 63 6f 64 65 64 0a 20 2a t to be coded. *
73420 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20 49 66 recursively. If
73430 20 74 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 this condition
73440 69 73 20 64 65 74 65 63 74 65 64 2c 20 74 68 65 is detected, the
73450 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 20 nested trigger
73460 69 73 20 6e 6f 74 20 63 6f 64 65 64 2e 0a 20 2a is not coded.. *
73470 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72 /.struct Trigger
73480 53 74 61 63 6b 20 7b 0a 20 20 54 61 62 6c 65 20 Stack {. Table
73490 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f *pTab; /
734a0 2a 20 54 61 62 6c 65 20 74 68 61 74 20 74 72 69 * Table that tri
734b0 67 67 65 72 73 20 61 72 65 20 63 75 72 72 65 6e ggers are curren
734c0 74 6c 79 20 62 65 69 6e 67 20 63 6f 64 65 64 20 tly being coded
734d0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 on */. int newI
734e0 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 dx; /*
734f0 49 6e 64 65 78 20 6f 66 20 76 64 62 65 20 63 75 Index of vdbe cu
73500 72 73 6f 72 20 74 6f 20 22 6e 65 77 22 20 74 65 rsor to "new" te
73510 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e mp table */. in
73520 74 20 6f 6c 64 49 64 78 3b 20 20 20 20 20 20 20 t oldIdx;
73530 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76 /* Index of v
73540 64 62 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6f dbe cursor to "o
73550 6c 64 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a ld" temp table *
73560 2f 0a 20 20 75 33 32 20 6e 65 77 43 6f 6c 4d 61 /. u32 newColMa
73570 73 6b 3b 0a 20 20 75 33 32 20 6f 6c 64 43 6f 6c sk;. u32 oldCol
73580 4d 61 73 6b 3b 0a 20 20 69 6e 74 20 6f 72 63 6f Mask;. int orco
73590 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 nf; /*
735a0 43 75 72 72 65 6e 74 20 6f 72 63 6f 6e 66 20 70 Current orconf p
735b0 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 olicy */. int i
735c0 67 6e 6f 72 65 4a 75 6d 70 3b 20 20 20 20 20 20 gnoreJump;
735d0 2f 2a 20 77 68 65 72 65 20 74 6f 20 6a 75 6d 70 /* where to jump
735e0 20 74 6f 20 66 6f 72 20 61 20 52 41 49 53 45 28 to for a RAISE(
735f0 49 47 4e 4f 52 45 29 20 2a 2f 0a 20 20 54 72 69 IGNORE) */. Tri
73600 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 gger *pTrigger;
73610 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 /* The trigger
73620 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 currently being
73630 20 63 6f 64 65 64 20 2a 2f 0a 20 20 54 72 69 67 coded */. Trig
73640 67 65 72 53 74 61 63 6b 20 2a 70 4e 65 78 74 3b gerStack *pNext;
73650 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72 /* Next trigger
73660 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20 74 72 69 down on the tri
73670 67 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b gger stack */.};
73680 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
73690 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
736a0 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 contains informa
736b0 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 tion used by the
736c0 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a sqliteFix....**
736d0 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65 routines as the
736e0 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65 y walk the parse
736f0 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61 tree to make da
73700 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 tabase reference
73710 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20 s.** explicit.
73720 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
73730 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 78 ct DbFixer DbFix
73740 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78 er;.struct DbFix
73750 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 er {. Parse *pP
73760 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 arse; /* Th
73770 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 e parsing contex
73780 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67 t. Error messag
73790 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 es written here
737a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
737b0 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 *zDb; /* Make
737c0 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74 sure all object
737d0 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 s are contained
737e0 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 in this database
737f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
73800 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70 *zType; /* Typ
73810 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e e of the contain
73820 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 er - used for er
73830 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a ror messages */.
73840 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 const Token *p
73850 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 Name; /* Name of
73860 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d the container -
73870 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 used for error
73880 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a messages */.};..
73890 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65 /*.** An objecte
738a0 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75 d used to accumu
738b0 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 late the text of
738c0 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 a string where
738d0 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 we.** do not nec
738e0 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f essarily know ho
738f0 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67 w big the string
73900 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20 will be in the
73910 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 end..*/.struct S
73920 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69 trAccum {. sqli
73930 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
73940 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74 /* Optional dat
73950 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 abase for lookas
73960 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c ide. Can be NUL
73970 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61 L */. char *zBa
73980 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 se; /* A
73990 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e base allocation
739a0 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c . Not from mall
739b0 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a oc. */. char *z
739c0 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Text; /*
739d0 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c The string coll
739e0 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a ected so far */.
739f0 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20 int nChar;
73a00 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
73a10 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73 of the string s
73a20 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 o far */. int
73a30 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 nAlloc;
73a40 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 /* Amount of spa
73a50 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 ce allocated in
73a60 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20 zText */. int
73a70 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 mxAlloc;
73a80 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 /* Maximum allow
73a90 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 ed string length
73aa0 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f */. u8 mallo
73ab0 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65 cFailed; /* Be
73ac0 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e comes true if an
73ad0 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 y memory allocat
73ae0 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75 ion fails */. u
73af0 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 8 useMalloc;
73b00 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a /* True if z
73b10 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 61 62 Text is enlargab
73b20 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 le using realloc
73b30 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42 69 */. u8 tooBi
73b40 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 g; /* Be
73b50 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73 74 comes true if st
73b60 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65 64 ring size exceed
73b70 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a 0a s limits */.};..
73b80 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 /*.** A pointer
73b90 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 to this structur
73ba0 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d e is used to com
73bb0 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d 61 municate informa
73bc0 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c tion.** from sql
73bd0 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f ite3Init and OP_
73be0 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74 6f ParseSchema into
73bf0 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69 74 the sqlite3Init
73c00 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70 Callback..*/.typ
73c10 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20 20 edef struct {.
73c20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
73c30 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
73c40 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61 ase being initia
73c50 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 lized */. int i
73c60 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f Db; /
73c70 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61 74 * 0 for main dat
73c80 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54 45 abase. 1 for TE
73c90 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54 41 MP, 2.. for ATTA
73ca0 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a CHed */. char *
73cb0 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a *pzErrMsg; /*
73cc0 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 Error message s
73cd0 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 tored here */.
73ce0 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
73cf0 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f /* Result co
73d00 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a de stored here *
73d10 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f /.} InitData;../
73d20 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 63 *.** Structure c
73d30 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61 6c ontaining global
73d40 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 64 configuration d
73d50 61 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c 69 ata for the SQLi
73d60 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a te library..**.*
73d70 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 * This structure
73d80 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 73 also contains s
73d90 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72 6d ome state inform
73da0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ation..*/.struct
73db0 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 7b Sqlite3Config {
73dc0 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74 3b . int bMemstat;
73dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73de0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
73df0 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 74 enable memory st
73e00 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62 43 atus */. int bC
73e10 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20 20 oreMutex;
73e20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
73e30 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63 6f rue to enable co
73e40 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 re mutexing */.
73e50 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78 3b int bFullMutex;
73e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73e70 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e /* True to en
73e80 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78 69 able full mutexi
73e90 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 74 ng */. int mxSt
73ea0 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 rlen;
73eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
73ec0 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e 67 imum string leng
73ed0 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c 6f th */. int szLo
73ee0 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 okaside;
73ef0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 /* Def
73f00 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 ault lookaside b
73f10 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20 uffer size */.
73f20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b 20 int nLookaside;
73f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73f40 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f /* Default loo
73f50 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 63 6f kaside buffer co
73f60 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 unt */. sqlite3
73f70 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 20 _mem_methods m;
73f80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
73f90 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
73fa0 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72 66 llocation interf
73fb0 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ace */. sqlite3
73fc0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 6d _mutex_methods m
73fd0 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c 6f utex; /* Lo
73fe0 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69 6e w-level mutex in
73ff0 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c terface */. sql
74000 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
74010 6f 64 73 20 70 63 61 63 68 65 3b 20 20 20 20 2f ods pcache; /
74020 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 67 65 * Low-level page
74030 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65 -cache interface
74040 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 65 61 */. void *pHea
74050 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
74060 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 70 20 /* Heap
74070 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 2a 2f storage space */
74080 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 20 20 . int nHeap;
74090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
740a0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
740b0 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 pHeap[] */. int
740c0 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b 20 20 mnReq, mxReq;
740d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
740e0 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 68 65 * Min and max he
740f0 61 70 20 72 65 71 75 65 73 74 73 20 73 69 7a 65 ap requests size
74100 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 s */. void *pSc
74110 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 ratch;
74120 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 72 61 /* Scra
74130 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 tch memory */.
74140 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 20 int szScratch;
74150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
74160 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 /* Size of eac
74170 68 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 h scratch buffer
74180 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 61 74 */. int nScrat
74190 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ch;
741a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
741b0 72 20 6f 66 20 73 63 72 61 74 63 68 20 62 75 66 r of scratch buf
741c0 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a fers */. void *
741d0 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 pPage;
741e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
741f0 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 age cache memory
74200 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 */. int szPage
74210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
74220 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
74230 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 of each page in
74240 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 pPage[] */. int
74250 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 nPage;
74260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
74270 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
74280 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a s in pPage[] */.
74290 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 int isInit;
742a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
742b0 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 /* True afte
742c0 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e r initialization
742d0 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 2a 2f has finished */
742e0 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 65 73 . int inProgres
742f0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
74300 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 69 /* True whi
74310 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f le initializatio
74320 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f n in progress */
74330 0a 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 . int isMallocI
74340 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 nit;
74350 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 /* True aft
74360 65 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 er malloc is ini
74370 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 73 71 tialized */. sq
74380 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 49 6e lite3_mutex *pIn
74390 69 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 itMutex;
743a0 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 62 79 /* Mutex used by
743b0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
743c0 69 7a 65 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e ize() */. int n
743d0 52 65 66 49 6e 69 74 4d 75 74 65 78 3b 20 20 20 RefInitMutex;
743e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
743f0 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 Number of users
74400 6f 66 20 70 49 6e 69 74 4d 75 74 65 78 20 2a 2f of pInitMutex */
74410 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72 53 . int mxParserS
74420 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 tack;
74430 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d 20 /* maximum
74440 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 depth of the par
74450 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 ser stack */. i
74460 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45 6e nt sharedCacheEn
74470 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 abled;
74480 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61 72 /* true if shar
74490 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 6e ed-cache mode en
744a0 61 62 6c 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a abled */.};../*.
744b0 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 ** Context point
744c0 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e 20 74 er passed down t
744d0 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 65 2d hrough the tree-
744e0 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 walk..*/.struct
744f0 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28 Walker {. int (
74500 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 29 28 *xExprCallback)(
74510 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b Walker*, Expr*);
74520 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b /* Callback
74530 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 for expressions
74540 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c */. int (*xSel
74550 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c ectCallback)(Wal
74560 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20 ker*,Select*);
74570 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 /* Callback for
74580 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 61 72 SELECTs */. Par
74590 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
745a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
745b0 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 /* Parser
745c0 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20 context. */.
745d0 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 union {
745e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
745f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 /* Ext
74600 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c ra data for call
74610 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 back */. Name
74620 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20 Context *pNC;
74630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
74640 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 /* Naming
74650 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 context */.
74660 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
74670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
74680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
74690 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 teger value */.
746a0 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 } u;.};../* For
746b0 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e ward declaration
746c0 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 s */.SQLITE_PRIV
746d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
746e0 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c alkExpr(Walker*,
746f0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
74700 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
74710 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 te3WalkExprList(
74720 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73 Walker*, ExprLis
74730 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
74740 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
74750 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 alkSelect(Walker
74760 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
74770 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
74780 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
74790 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53 tExpr(Walker*, S
747a0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
747b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
747c0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f te3WalkSelectFro
747d0 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 m(Walker*, Selec
747e0 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 t*);../*.** Retu
747f0 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 rn code from the
74800 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b parse-tree walk
74810 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61 ing primitives a
74820 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c nd their.** call
74830 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e backs..*/.#defin
74840 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 e WRC_Continue
74850 20 20 30 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 0.#define WRC_
74860 50 72 75 6e 65 20 20 20 20 20 20 20 31 0a 23 64 Prune 1.#d
74870 65 66 69 6e 65 20 57 52 43 5f 41 62 6f 72 74 20 efine WRC_Abort
74880 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 2../*.** A
74890 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f 69 6e ssuming zIn poin
748a0 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 ts to the first
748b0 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d 38 20 byte of a UTF-8
748c0 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20 61 64 character,.** ad
748d0 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70 6f 69 vance zIn to poi
748e0 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 nt to the first
748f0 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 78 74 byte of the next
74900 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 UTF-8 character
74910 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
74920 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 49 ITE_SKIP_UTF8(zI
74930 6e 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 n) {
74940 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
74950 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29 3e 3d if( (*(zIn++))>=
74960 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 20 20 0xc0 ){
74970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
74980 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c 65 \. while
74990 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d ( (*zIn & 0xc0)=
749a0 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b 3b 20 =0x80 ){ zIn++;
749b0 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a } \.
749c0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 }
749d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
749e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
749f0 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a \.}../*.*
74a00 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43 4f 52 * The SQLITE_COR
74a10 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 72 6f 20 RUPT_BKPT macro
74a20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 can be either a
74a30 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20 70 72 constant (for pr
74a40 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75 69 6c oduction.** buil
74a50 64 73 29 20 6f 72 20 61 20 66 75 6e 63 74 69 6f ds) or a functio
74a60 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 65 62 75 n call (for debu
74a70 67 67 69 6e 67 29 2e 20 20 49 66 20 69 74 20 69 gging). If it i
74a80 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c s a function cal
74a90 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77 73 20 l,.** it allows
74aa0 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 the operator to
74ab0 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74 set a breakpoint
74ac0 20 61 74 20 74 68 65 20 73 70 6f 74 20 77 68 65 at the spot whe
74ad0 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 re database.** c
74ae0 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66 69 72 orruption is fir
74af0 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a 2f 0a st detected..*/.
74b00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
74b10 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
74b20 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
74b30 43 6f 72 72 75 70 74 28 76 6f 69 64 29 3b 0a 23 Corrupt(void);.#
74b40 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 define SQLITE_C
74b50 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 71 6c 69 ORRUPT_BKPT sqli
74b60 74 65 33 43 6f 72 72 75 70 74 28 29 0a 23 65 6c te3Corrupt().#el
74b70 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 se.# define SQLI
74b80 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 TE_CORRUPT_BKPT
74b90 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 23 SQLITE_CORRUPT.#
74ba0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 endif../*.** Int
74bb0 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 70 ernal function p
74bc0 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c rototypes.*/.SQL
74bd0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
74be0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63 sqlite3StrICmp(c
74bf0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e onst char *, con
74c00 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 st char *);.SQLI
74c10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
74c20 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 63 qlite3StrNICmp(c
74c30 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e onst char *, con
74c40 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b st char *, int);
74c50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74c60 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d int sqlite3IsNum
74c70 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c ber(const char*,
74c80 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 int*, u8);.SQLI
74c90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
74ca0 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 73 71 6c qlite3Strlen(sql
74cb0 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 ite3*, const cha
74cc0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 r*);..SQLITE_PRI
74cd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
74ce0 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 MallocInit(void)
74cf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74d00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c void sqlite3Mal
74d10 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 locEnd(void);.SQ
74d20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
74d30 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 d *sqlite3Malloc
74d40 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
74d50 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
74d60 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e te3MallocZero(in
74d70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
74d80 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
74d90 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c DbMallocZero(sql
74da0 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ite3*, int);.SQL
74db0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
74dc0 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *sqlite3DbMallo
74dd0 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 cRaw(sqlite3*, i
74de0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
74df0 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
74e00 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 3DbStrDup(sqlite
74e10 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 3*,const char*);
74e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74e30 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 char *sqlite3DbS
74e40 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c trNDup(sqlite3*,
74e50 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
74e60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74e70 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 E void *sqlite3R
74e80 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e ealloc(void*, in
74e90 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
74ea0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
74eb0 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 DbReallocOrFree(
74ec0 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 sqlite3 *, void
74ed0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
74ee0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
74ef0 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 lite3DbRealloc(s
74f00 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a qlite3 *, void *
74f10 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
74f20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74f30 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 65 te3DbFree(sqlite
74f40 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 3*, void*);.SQLI
74f50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
74f60 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 qlite3MallocSize
74f70 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f (void*);.SQLITE_
74f80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
74f90 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
74fa0 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 sqlite3*, void*)
74fb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74fc0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63 void *sqlite3Sc
74fd0 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 ratchMalloc(int)
74fe0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74ff0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72 void sqlite3Scr
75000 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29 3b atchFree(void*);
75010 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75020 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
75030 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 eMalloc(int);.SQ
75040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
75050 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 d sqlite3PageFre
75060 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 e(void*);.SQLITE
75070 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
75080 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 lite3MemSetDefau
75090 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 lt(void);.SQLITE
750a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
750b0 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f lite3BenignMallo
750c0 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 cHooks(void (*)(
750d0 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 void), void (*)(
750e0 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 void));.SQLITE_P
750f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
75100 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f e3MemoryAlarm(vo
75110 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 id (*)(void*, sq
75120 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 lite3_int64, int
75130 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 ), void*, sqlite
75140 33 5f 69 6e 74 36 34 29 3b 0a 0a 23 69 66 64 65 3_int64);..#ifde
75150 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
75160 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 MEMSYS3.SQLITE_P
75170 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c RIVATE const sql
75180 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
75190 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d *sqlite3MemGetM
751a0 65 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 emsys3(void);.#e
751b0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
751c0 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
751d0 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 5.SQLITE_PRIVATE
751e0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
751f0 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 em_methods *sqli
75200 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 te3MemGetMemsys5
75210 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a (void);.#endif..
75220 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
75230 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 MUTEX_OMIT.SQLIT
75240 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
75250 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
75260 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c s *sqlite3Defaul
75270 74 4d 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 tMutex(void);.SQ
75280 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 LITE_PRIVATE s
75290 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 qlite3_mutex *sq
752a0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
752b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
752c0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
752d0 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 e3MutexInit(void
752e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
752f0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d E int sqlite3M
75300 75 74 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 utexEnd(void);.#
75310 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
75320 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
75330 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74 3StatusValue(int
75340 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75350 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
75360 61 74 75 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 atusAdd(int, int
75370 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75380 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
75390 61 74 75 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 atusSet(int, int
753a0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
753b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 TE int sqlite3Is
753c0 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 NaN(double);..SQ
753d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
753e0 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 d sqlite3VXPrint
753f0 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 f(StrAccum*, int
75400 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 , const char*, v
75410 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f a_list);.SQLITE_
75420 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
75430 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c lite3MPrintf(sql
75440 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 ite3*,const char
75450 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f *, ...);.SQLITE_
75460 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
75470 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71 lite3VMPrintf(sq
75480 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 lite3*,const cha
75490 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 r*, va_list);.SQ
754a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
754b0 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e r *sqlite3MAppen
754c0 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 df(sqlite3*,char
754d0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e *,const char*,..
754e0 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .);.#if defined(
754f0 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 SQLITE_TEST) ||
75500 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
75510 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 EBUG).SQLITE_PRI
75520 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
75530 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 te3DebugPrintf(c
75540 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
75550 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 ;.#endif.#if def
75560 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
75570 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
75580 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 void *sqlite3
75590 54 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f TestTextToPtr(co
755a0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 nst char*);.#end
755b0 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
755c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 E void sqlite3Se
755d0 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c tString(char **,
755e0 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 sqlite3*, const
755f0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 char*, ...);.SQ
75600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
75610 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 d sqlite3ErrorMs
75620 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 g(Parse*, const
75630 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c char*, ...);.SQL
75640 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
75650 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 sqlite3ErrorCle
75660 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 ar(Parse*);.SQLI
75670 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
75680 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 sqlite3Dequote(c
75690 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
756a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
756b0 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 73 71 e3DequoteExpr(sq
756c0 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a lite3*, Expr*);.
756d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
756e0 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 nt sqlite3Keywor
756f0 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 dCode(const unsi
75700 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29 gned char*, int)
75710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75720 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 int sqlite3RunP
75730 61 72 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f arser(Parse*, co
75740 6e 73 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20 nst char*, char
75750 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
75760 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
75770 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72 FinishCoding(Par
75780 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 se*);.SQLITE_PRI
75790 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
757a0 47 65 74 54 65 6d 70 52 65 67 28 50 61 72 73 65 GetTempReg(Parse
757b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
757c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
757d0 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 eleaseTempReg(Pa
757e0 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 rse*,int);.SQLIT
757f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
75800 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 lite3GetTempRang
75810 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 e(Parse*,int);.S
75820 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
75830 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 id sqlite3Releas
75840 65 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 eTempRange(Parse
75850 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 *,int,int);.SQLI
75860 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
75870 2a 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c *sqlite3Expr(sql
75880 69 74 65 33 2a 2c 20 69 6e 74 2c 20 45 78 70 72 ite3*, int, Expr
75890 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 *, Expr*, const
758a0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
758b0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
758c0 6c 69 74 65 33 50 45 78 70 72 28 50 61 72 73 65 lite3PExpr(Parse
758d0 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 *, int, Expr*, E
758e0 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 xpr*, const Toke
758f0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
75900 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
75910 33 52 65 67 69 73 74 65 72 45 78 70 72 28 50 61 3RegisterExpr(Pa
75920 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 rse*,Token*);.SQ
75930 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
75940 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e r *sqlite3ExprAn
75950 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a d(sqlite3*,Expr*
75960 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
75970 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
75980 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 45 78 lite3ExprSpan(Ex
75990 70 72 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e pr*,Token*,Token
759a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
759b0 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
759c0 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 ExprFunction(Par
759d0 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54 se*,ExprList*, T
759e0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
759f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
75a00 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72 te3ExprAssignVar
75a10 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45 Number(Parse*, E
75a20 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
75a30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
75a40 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69 e3ExprClear(sqli
75a50 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 te3*, Expr*);.SQ
75a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
75a70 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c d sqlite3ExprDel
75a80 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 ete(sqlite3*, Ex
75a90 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
75aa0 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 VATE ExprList *s
75ab0 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 qlite3ExprListAp
75ac0 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72 pend(Parse*,Expr
75ad0 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 54 6f 6b 65 List*,Expr*,Toke
75ae0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
75af0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
75b00 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 ExprListDelete(s
75b10 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 qlite3*, ExprLis
75b20 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
75b30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
75b40 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 nit(sqlite3*, ch
75b50 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ar**);.SQLITE_PR
75b60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
75b70 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 3InitCallback(vo
75b80 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a id*, int, char**
75b90 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 , char**);.SQLIT
75ba0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
75bb0 71 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 qlite3Pragma(Par
75bc0 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
75bd0 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 *,Token*,int);.S
75be0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
75bf0 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 id sqlite3ResetI
75c00 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 nternalSchema(sq
75c10 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 lite3*, int);.SQ
75c20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
75c30 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 d sqlite3BeginPa
75c40 72 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b rse(Parse*,int);
75c50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75c60 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d void sqlite3Comm
75c70 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 itInternalChange
75c80 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c s(sqlite3*);.SQL
75c90 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
75ca0 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 e *sqlite3Result
75cb0 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 SetOfSelect(Pars
75cc0 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c e*,Select*);.SQL
75cd0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
75ce0 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 sqlite3OpenMast
75cf0 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c erTable(Parse *,
75d00 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
75d10 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
75d20 65 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 e3StartTable(Par
75d30 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
75d40 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e *,int,int,int,in
75d50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
75d60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
75d70 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c ddColumn(Parse*,
75d80 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
75d90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
75da0 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 ite3AddNotNull(P
75db0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c arse*, int);.SQL
75dc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
75dd0 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 sqlite3AddPrima
75de0 72 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 ryKey(Parse*, Ex
75df0 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e prList*, int, in
75e00 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
75e10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
75e20 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 ite3AddCheckCons
75e30 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 traint(Parse*, E
75e40 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
75e50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
75e60 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 e3AddColumnType(
75e70 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a Parse*,Token*);.
75e80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
75e90 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 oid sqlite3AddDe
75ea0 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 faultValue(Parse
75eb0 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 *,Expr*);.SQLITE
75ec0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
75ed0 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 lite3AddCollateT
75ee0 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 ype(Parse*, Toke
75ef0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
75f00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
75f10 45 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c EndTable(Parse*,
75f20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 Token*,Token*,Se
75f30 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f lect*);..SQLITE_
75f40 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a PRIVATE Bitvec *
75f50 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 sqlite3BitvecCre
75f60 61 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 ate(u32);.SQLITE
75f70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
75f80 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 ite3BitvecTest(B
75f90 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 itvec*, u32);.SQ
75fa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
75fb0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 sqlite3BitvecSe
75fc0 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b t(Bitvec*, u32);
75fd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75fe0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 void sqlite3Bitv
75ff0 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c ecClear(Bitvec*,
76000 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 u32);.SQLITE_PR
76010 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
76020 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
76030 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 Bitvec*);.SQLITE
76040 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
76050 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 ite3BitvecBuilti
76060 6e 54 65 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b nTest(int,int*);
76070 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
76080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 void sqlite3Cre
76090 61 74 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54 ateView(Parse*,T
760a0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b oken*,Token*,Tok
760b0 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c en*,Select*,int,
760c0 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 int);..#if !defi
760d0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
760e0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 VIEW) || !define
760f0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
76100 52 54 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49 RTUALTABLE).SQLI
76110 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
76120 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 sqlite3ViewGetC
76130 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 olumnNames(Parse
76140 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 *,Table*);.#else
76150 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
76160 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 3ViewGetColumnNa
76170 6d 65 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 mes(A,B) 0.#endi
76180 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
76190 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 E void sqlite3Dr
761a0 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 opTable(Parse*,
761b0 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 SrcList*, int, i
761c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
761d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
761e0 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c DeleteTable(Tabl
761f0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
76200 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
76210 49 6e 73 65 72 74 28 50 61 72 73 65 2a 2c 20 53 Insert(Parse*, S
76220 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 rcList*, ExprLis
76230 74 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 49 64 4c t*, Select*, IdL
76240 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ist*, int);.SQLI
76250 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
76260 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c *sqlite3ArrayAll
76270 6f 63 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 76 ocate(sqlite3*,v
76280 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 oid*,int,int,int
76290 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 53 51 *,int*,int*);.SQ
762a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c LITE_PRIVATE IdL
762b0 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 ist *sqlite3IdLi
762c0 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74 65 33 stAppend(sqlite3
762d0 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 54 6f 6b 65 *, IdList*, Toke
762e0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
762f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
76300 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 69 73 dListIndex(IdLis
76310 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b t*,const char*);
76320 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
76330 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 SrcList *sqlite3
76340 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 73 SrcListEnlarge(s
76350 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 qlite3*, SrcList
76360 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
76370 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 LITE_PRIVATE Src
76380 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 List *sqlite3Src
76390 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74 ListAppend(sqlit
763a0 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 e3*, SrcList*, T
763b0 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a oken*, Token*);.
763c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
763d0 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 rcList *sqlite3S
763e0 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d rcListAppendFrom
763f0 54 65 72 6d 28 50 61 72 73 65 2a 2c 20 53 72 63 Term(Parse*, Src
76400 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 List*, Token*, T
76410 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 oken*,.
76420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76430 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b Tok
76440 65 6e 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 en*, Select*, Ex
76450 70 72 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53 pr*, IdList*);.S
76460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
76470 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 id sqlite3SrcLis
76480 74 49 6e 64 65 78 65 64 42 79 28 50 61 72 73 65 tIndexedBy(Parse
76490 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 54 *, SrcList *, T
764a0 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f oken *);.SQLITE_
764b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
764c0 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b te3IndexedByLook
764d0 75 70 28 50 61 72 73 65 20 2a 2c 20 73 74 72 75 up(Parse *, stru
764e0 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
764f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
76500 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
76510 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 rcListShiftJoinT
76520 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b 0a 53 ype(SrcList*);.S
76530 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
76540 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 id sqlite3SrcLis
76550 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50 tAssignCursors(P
76560 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 29 arse*, SrcList*)
76570 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76580 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64 4c void sqlite3IdL
76590 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 istDelete(sqlite
765a0 33 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53 51 3*, IdList*);.SQ
765b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
765c0 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 d sqlite3SrcList
765d0 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c Delete(sqlite3*,
765e0 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 SrcList*);.SQLI
765f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
76600 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 sqlite3CreateInd
76610 65 78 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a ex(Parse*,Token*
76620 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c 69 73 74 2a ,Token*,SrcList*
76630 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 54 ,ExprList*,int,T
76640 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 oken*,.
76650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
76660 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 oken*, int, int)
76670 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76680 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f void sqlite3Dro
76690 70 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 53 pIndex(Parse*, S
766a0 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 rcList*, int);.S
766b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
766c0 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 t sqlite3Select(
766d0 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c Parse*, Select*,
766e0 20 53 65 6c 65 63 74 44 65 73 74 2a 29 3b 0a 53 SelectDest*);.S
766f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 QLITE_PRIVATE Se
76700 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c lect *sqlite3Sel
76710 65 63 74 4e 65 77 28 50 61 72 73 65 2a 2c 45 78 ectNew(Parse*,Ex
76720 70 72 4c 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a prList*,SrcList*
76730 2c 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a ,Expr*,ExprList*
76740 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
76750 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 2a Expr*
76760 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 45 ,ExprList*,int,E
76770 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c xpr*,Expr*);.SQL
76780 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
76790 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
767a0 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 lete(sqlite3*, S
767b0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
767c0 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 PRIVATE Table *s
767d0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f qlite3SrcListLoo
767e0 6b 75 70 28 50 61 72 73 65 2a 2c 20 53 72 63 4c kup(Parse*, SrcL
767f0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
76800 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
76810 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73 3IsReadOnly(Pars
76820 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 e*, Table*, int)
76830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76840 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 void sqlite3Ope
76850 6e 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 69 nTable(Parse*, i
76860 6e 74 20 69 43 75 72 2c 20 69 6e 74 20 69 44 62 nt iCur, int iDb
76870 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a , Table*, int);.
76880 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
76890 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 TE_ENABLE_UPDATE
768a0 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 _DELETE_LIMIT) &
768b0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
768c0 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 E_OMIT_SUBQUERY)
768d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
768e0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 4c 69 6d Expr *sqlite3Lim
768f0 69 74 57 68 65 72 65 28 50 61 72 73 65 20 2a 2c itWhere(Parse *,
76900 20 53 72 63 4c 69 73 74 20 2a 2c 20 45 78 70 72 SrcList *, Expr
76910 20 2a 2c 20 45 78 70 72 4c 69 73 74 20 2a 2c 20 *, ExprList *,
76920 45 78 70 72 20 2a 2c 20 45 78 70 72 20 2a 2c 20 Expr *, Expr *,
76930 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a char *);.#endif.
76940 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
76950 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 oid sqlite3Delet
76960 65 46 72 6f 6d 28 50 61 72 73 65 2a 2c 20 53 72 eFrom(Parse*, Sr
76970 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 29 3b 0a cList*, Expr*);.
76980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
76990 6f 69 64 20 73 71 6c 69 74 65 33 55 70 64 61 74 oid sqlite3Updat
769a0 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 e(Parse*, SrcLis
769b0 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 45 t*, ExprList*, E
769c0 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 xpr*, int);.SQLI
769d0 54 45 5f 50 52 49 56 41 54 45 20 57 68 65 72 65 TE_PRIVATE Where
769e0 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65 Info *sqlite3Whe
769f0 72 65 42 65 67 69 6e 28 50 61 72 73 65 2a 2c 20 reBegin(Parse*,
76a00 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c SrcList*, Expr*,
76a10 20 45 78 70 72 4c 69 73 74 2a 2a 2c 20 75 38 29 ExprList**, u8)
76a20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76a30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 void sqlite3Whe
76a40 72 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 2a reEnd(WhereInfo*
76a50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
76a60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
76a70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 50 rCodeGetColumn(P
76a80 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
76a90 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e nt, int, int, in
76aa0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
76ab0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
76ac0 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 xprCodeMove(Pars
76ad0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e e*, int, int, in
76ae0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
76af0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
76b00 78 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 xprCodeCopy(Pars
76b10 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e e*, int, int, in
76b20 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
76b30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
76b40 78 70 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 xprClearColumnCa
76b50 63 68 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 che(Parse*, int)
76b60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76b70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
76b80 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 rCacheAffinityCh
76b90 61 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 ange(Parse*, int
76ba0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
76bb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
76bc0 65 33 45 78 70 72 57 72 69 74 61 62 6c 65 52 65 e3ExprWritableRe
76bd0 67 69 73 74 65 72 28 50 61 72 73 65 2a 2c 69 6e gister(Parse*,in
76be0 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 t,int);.SQLITE_P
76bf0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
76c00 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 te3ExprHardCopy(
76c10 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b Parse*,int,int);
76c20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
76c30 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
76c40 6f 64 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 ode(Parse*, Expr
76c50 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
76c60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
76c70 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
76c80 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 Parse*, Expr*, i
76c90 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
76ca0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
76cb0 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 ExprCodeTarget(P
76cc0 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e arse*, Expr*, in
76cd0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
76ce0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
76cf0 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 prCodeAndCache(P
76d00 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e arse*, Expr*, in
76d10 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
76d20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
76d30 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 xprCodeConstants
76d40 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b (Parse*, Expr*);
76d50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
76d60 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
76d70 6f 64 65 45 78 70 72 4c 69 73 74 28 50 61 72 73 odeExprList(Pars
76d80 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 e*, ExprList*, i
76d90 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
76da0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
76db0 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 lite3ExprIfTrue(
76dc0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 Parse*, Expr*, i
76dd0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
76de0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
76df0 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 lite3ExprIfFalse
76e00 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 (Parse*, Expr*,
76e10 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
76e20 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 E_PRIVATE Table
76e30 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c *sqlite3FindTabl
76e40 65 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 e(sqlite3*,const
76e50 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 char*, const ch
76e60 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
76e70 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 VATE Table *sqli
76e80 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 te3LocateTable(P
76e90 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 arse*,int isView
76ea0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f ,const char*, co
76eb0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
76ec0 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 TE_PRIVATE Index
76ed0 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 *sqlite3FindInd
76ee0 65 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 ex(sqlite3*,cons
76ef0 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 t char*, const c
76f00 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
76f10 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
76f20 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 e3UnlinkAndDelet
76f30 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c eTable(sqlite3*,
76f40 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 int,const char*)
76f50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
76f60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c void sqlite3Unl
76f70 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 inkAndDeleteInde
76f80 78 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 x(sqlite3*,int,c
76f90 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
76fa0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
76fb0 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 sqlite3Vacuum(P
76fc0 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
76fd0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
76fe0 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 e3RunVacuum(char
76ff0 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 **, sqlite3*);.S
77000 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
77010 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 ar *sqlite3NameF
77020 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 romToken(sqlite3
77030 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
77040 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
77050 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 qlite3ExprCompar
77060 65 28 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b e(Expr*, Expr*);
77070 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
77080 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
77090 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 AnalyzeAggregate
770a0 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 s(NameContext*,
770b0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
770c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
770d0 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 te3ExprAnalyzeAg
770e0 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 gList(NameContex
770f0 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 t*,ExprList*);.S
77100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
77110 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 be *sqlite3GetVd
77120 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 be(Parse*);.SQLI
77130 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
77140 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 *sqlite3CreateId
77150 45 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f Expr(Parse *, co
77160 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
77170 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
77180 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 sqlite3PrngSaveS
77190 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 tate(void);.SQLI
771a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
771b0 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f sqlite3PrngResto
771c0 72 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 reState(void);.S
771d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
771e0 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 id sqlite3PrngRe
771f0 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a setState(void);.
77200 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
77210 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 oid sqlite3Rollb
77220 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 ackAll(sqlite3*)
77230 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
77240 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 void sqlite3Cod
77250 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 eVerifySchema(Pa
77260 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 rse*, int);.SQLI
77270 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
77280 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e sqlite3BeginTran
77290 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 saction(Parse*,
772a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
772b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
772c0 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 3CommitTransacti
772d0 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 on(Parse*);.SQLI
772e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
772f0 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 sqlite3RollbackT
77300 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 ransaction(Parse
77310 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
77320 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
77330 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 prIsConstant(Exp
77340 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
77350 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
77360 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 xprIsConstantNot
77370 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c Join(Expr*);.SQL
77380 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
77390 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e sqlite3ExprIsCon
773a0 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 stantOrFunction(
773b0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
773c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
773d0 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 e3ExprIsInteger(
773e0 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 Expr*, int*);.SQ
773f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
77400 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 sqlite3IsRowid(
77410 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
77420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
77430 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 d sqlite3Generat
77440 65 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65 eRowDelete(Parse
77450 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 *, Table*, int,
77460 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
77470 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
77480 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f qlite3GenerateRo
77490 77 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72 wIndexDelete(Par
774a0 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 se*, Table*, int
774b0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f , int*);.SQLITE_
774c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
774d0 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 te3GenerateIndex
774e0 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65 Key(Parse*, Inde
774f0 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e x*, int, int, in
77500 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
77510 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 TE void sqlite3G
77520 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e enerateConstrain
77530 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54 tChecks(Parse*,T
77540 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20 able*,int,int,.
77550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77570 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 int*,int,int
77580 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ,int,int);.SQLIT
77590 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
775a0 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e qlite3CompleteIn
775b0 73 65 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 sertion(Parse*,
775c0 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Table*, int, int
775d0 2c 20 69 6e 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 , int*, int, int
775e0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
775f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
77600 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e e3OpenTableAndIn
77610 64 69 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61 dices(Parse*, Ta
77620 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b ble*, int, int);
77630 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
77640 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 void sqlite3Begi
77650 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 nWriteOperation(
77660 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Parse*, int, int
77670 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
77680 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
77690 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c xprDup(sqlite3*,
776a0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
776b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
776c0 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 73 71 6c te3TokenCopy(sql
776d0 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 54 6f ite3*,Token*, To
776e0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
776f0 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a IVATE ExprList *
77700 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
77710 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 up(sqlite3*,Expr
77720 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 List*);.SQLITE_P
77730 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a RIVATE SrcList *
77740 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 sqlite3SrcListDu
77750 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69 p(sqlite3*,SrcLi
77760 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
77770 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c VATE IdList *sql
77780 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 ite3IdListDup(sq
77790 6c 69 74 65 33 2a 2c 49 64 4c 69 73 74 2a 29 3b lite3*,IdList*);
777a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
777b0 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 Select *sqlite3S
777c0 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 electDup(sqlite3
777d0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 *,Select*);.SQLI
777e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
777f0 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e sqlite3FuncDefIn
77800 73 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68 sert(FuncDefHash
77810 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 *, FuncDef*);.SQ
77820 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e LITE_PRIVATE Fun
77830 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e cDef *sqlite3Fin
77840 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 dFunction(sqlite
77850 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 3*,const char*,i
77860 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a nt,int,u8,int);.
77870 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
77880 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 oid sqlite3Regis
77890 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 terBuiltinFuncti
778a0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 ons(sqlite3*);.S
778b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
778c0 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 id sqlite3Regist
778d0 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 erDateTimeFuncti
778e0 6f 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ons(void);.SQLIT
778f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
77900 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c qlite3RegisterGl
77910 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f obalFunctions(vo
77920 69 64 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 id);.#ifdef SQLI
77930 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
77940 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
77950 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 lite3SafetyOn(sq
77960 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
77970 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
77980 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 lite3SafetyOff(s
77990 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a qlite3*);.#else.
779a0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
779b0 53 61 66 65 74 79 4f 6e 28 41 29 20 30 0a 23 20 SafetyOn(A) 0.#
779c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 define sqlite3Sa
779d0 66 65 74 79 4f 66 66 28 41 29 20 30 0a 23 65 6e fetyOff(A) 0.#en
779e0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
779f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 TE int sqlite3Sa
77a00 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 fetyCheckOk(sqli
77a10 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
77a20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
77a30 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 3SafetyCheckSick
77a40 4f 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a OrOk(sqlite3*);.
77a50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
77a60 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 oid sqlite3Chang
77a70 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20 eCookie(Parse*,
77a80 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 int);..#if !defi
77a90 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
77aa0 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 VIEW) && !define
77ab0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 d(SQLITE_OMIT_TR
77ac0 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 IGGER).SQLITE_PR
77ad0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
77ae0 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 e3MaterializeVie
77af0 77 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a w(Parse*, Table*
77b00 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 , Expr*, int);.#
77b10 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
77b20 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
77b30 45 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ER.SQLITE_PRIVAT
77b40 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
77b50 42 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72 BeginTrigger(Par
77b60 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 se*, Token*,Toke
77b70 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 n*,int,int,IdLis
77b80 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20 t*,SrcList*,.
77b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77ba0 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e Expr*,in
77bb0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
77bc0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
77bd0 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 qlite3FinishTrig
77be0 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 ger(Parse*, Trig
77bf0 67 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a gerStep*, Token*
77c00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
77c10 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
77c20 44 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 DropTrigger(Pars
77c30 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e e*, SrcList*, in
77c40 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
77c50 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
77c60 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 3DropTriggerPtr(
77c70 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a Parse*, Trigger*
77c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
77c90 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 54 E int sqlite3T
77ca0 72 69 67 67 65 72 73 45 78 69 73 74 28 54 61 62 riggersExist(Tab
77cb0 6c 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 le*, int, ExprLi
77cc0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
77cd0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
77ce0 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 e3CodeRowTrigger
77cf0 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 (Parse*, int, Ex
77d00 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 prList*, int, Ta
77d10 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c ble *, int, int,
77d20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
77d30 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
77d40 2c 20 69 6e 74 2c 20 75 33 32 2a 2c 20 75 33 32 , int, u32*, u32
77d50 2a 29 3b 0a 20 20 76 6f 69 64 20 73 71 6c 69 74 *);. void sqlit
77d60 65 56 69 65 77 54 72 69 67 67 65 72 73 28 50 61 eViewTriggers(Pa
77d70 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 rse*, Table*, Ex
77d80 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 pr*, int, ExprLi
77d90 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
77da0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
77db0 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 te3DeleteTrigger
77dc0 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 20 54 Step(sqlite3*, T
77dd0 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 riggerStep*);.SQ
77de0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 LITE_PRIVATE T
77df0 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 riggerStep *sqli
77e00 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 te3TriggerSelect
77e10 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 Step(sqlite3*,Se
77e20 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 lect*);.SQLITE_P
77e30 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 RIVATE Trigger
77e40 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 Step *sqlite3Tri
77e50 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 73 ggerInsertStep(s
77e60 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 qlite3*,Token*,
77e70 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 IdList*,.
77e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77ea0 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65 63 ExprList*,Selec
77eb0 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
77ec0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 PRIVATE Trigge
77ed0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 rStep *sqlite3Tr
77ee0 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70 28 iggerUpdateStep(
77ef0 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c sqlite3*,Token*,
77f00 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 72 2a ExprList*, Expr*
77f10 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
77f20 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 RIVATE Trigger
77f30 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 Step *sqlite3Tri
77f40 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 73 ggerDeleteStep(s
77f50 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 qlite3*,Token*,
77f60 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
77f70 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
77f80 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 lite3DeleteTrigg
77f90 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 69 er(sqlite3*, Tri
77fa0 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 gger*);.SQLITE_P
77fb0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
77fc0 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 lite3UnlinkAndDe
77fd0 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69 leteTrigger(sqli
77fe0 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 te3*,int,const c
77ff0 68 61 72 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 har*);.#else.# d
78000 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 efine sqlite3Tri
78010 67 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c 44 ggersExist(B,C,D
78020 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e 65 ,E,F) 0.# define
78030 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 sqlite3DeleteTr
78040 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65 66 igger(A,B).# def
78050 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 54 ine sqlite3DropT
78060 72 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a 23 riggerPtr(A,B).#
78070 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55 define sqlite3U
78080 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 nlinkAndDeleteTr
78090 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20 64 igger(A,B,C).# d
780a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 64 efine sqlite3Cod
780b0 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42 2c eRowTrigger(A,B,
780c0 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c 4a 2c C,D,E,F,G,H,I,J,
780d0 4b 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c K) 0.#endif..SQL
780e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
780f0 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 sqlite3JoinType(
78100 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 Parse*, Token*,
78110 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b Token*, Token*);
78120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78130 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 void sqlite3Crea
78140 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 teForeignKey(Par
78150 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 se*, ExprList*,
78160 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 73 74 Token*, ExprList
78170 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
78180 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
78190 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e ite3DeferForeign
781a0 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 Key(Parse*, int)
781b0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
781c0 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
781d0 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ION.SQLITE_PRIVA
781e0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
781f0 33 41 75 74 68 52 65 61 64 28 50 61 72 73 65 2a 3AuthRead(Parse*
78200 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a 2c 53 ,Expr*,Schema*,S
78210 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 rcList*);.SQLITE
78220 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
78230 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
78240 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f 6e 73 Parse*,int, cons
78250 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 t char*, const c
78260 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 har*, const char
78270 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
78280 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
78290 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 3AuthContextPush
782a0 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43 6f 6e (Parse*, AuthCon
782b0 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 text*, const cha
782c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
782d0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
782e0 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 e3AuthContextPop
782f0 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29 3b 0a (AuthContext*);.
78300 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
78310 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 61 qlite3AuthRead(a
78320 2c 62 2c 63 2c 64 29 0a 23 20 64 65 66 69 6e 65 ,b,c,d).# define
78330 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
78340 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 k(a,b,c,d,e)
78350 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65 66 69 SQLITE_OK.# defi
78360 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f ne sqlite3AuthCo
78370 6e 74 65 78 74 50 75 73 68 28 61 2c 62 2c 63 29 ntextPush(a,b,c)
78380 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
78390 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 3AuthContextPop(
783a0 61 29 20 20 28 28 76 6f 69 64 29 28 61 29 29 0a a) ((void)(a)).
783b0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
783c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
783d0 65 33 41 74 74 61 63 68 28 50 61 72 73 65 2a 2c e3Attach(Parse*,
783e0 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 45 Expr*, Expr*, E
783f0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
78400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
78410 65 33 44 65 74 61 63 68 28 50 61 72 73 65 2a 2c e3Detach(Parse*,
78420 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
78430 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
78440 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 te3BtreeFactory(
78450 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 20 2a 64 const sqlite3 *d
78460 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
78470 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 Filename,.
78480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78490 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c int omitJournal
784a0 2c 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 69 6e , int nCache, in
784b0 74 20 66 6c 61 67 73 2c 20 42 74 72 65 65 20 2a t flags, Btree *
784c0 2a 70 70 42 74 72 65 65 29 3b 0a 53 51 4c 49 54 *ppBtree);.SQLIT
784d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
784e0 6c 69 74 65 33 46 69 78 49 6e 69 74 28 44 62 46 lite3FixInit(DbF
784f0 69 78 65 72 2a 2c 20 50 61 72 73 65 2a 2c 20 69 ixer*, Parse*, i
78500 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c nt, const char*,
78510 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a const Token*);.
78520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
78530 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 nt sqlite3FixSrc
78540 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c 20 53 List(DbFixer*, S
78550 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 rcList*);.SQLITE
78560 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
78570 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 44 62 ite3FixSelect(Db
78580 46 69 78 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 Fixer*, Select*)
78590 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
785a0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 int sqlite3FixE
785b0 78 70 72 28 44 62 46 69 78 65 72 2a 2c 20 45 78 xpr(DbFixer*, Ex
785c0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
785d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
785e0 46 69 78 45 78 70 72 4c 69 73 74 28 44 62 46 69 FixExprList(DbFi
785f0 78 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 xer*, ExprList*)
78600 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
78610 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 int sqlite3FixT
78620 72 69 67 67 65 72 53 74 65 70 28 44 62 46 69 78 riggerStep(DbFix
78630 65 72 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 er*, TriggerStep
78640 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
78650 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 TE int sqlite3At
78660 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a oF(const char *z
78670 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c 49 , double*);.SQLI
78680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
78690 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 qlite3GetInt32(c
786a0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
786b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
786c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
786d0 74 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 tsIn64Bits(const
786e0 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 char *, int);.S
786f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
78700 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 t sqlite3Utf16By
78710 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 teLen(const void
78720 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 43 68 *pData, int nCh
78730 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ar);.SQLITE_PRIV
78740 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 ATE int sqlite3U
78750 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 tf8CharLen(const
78760 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e char *pData, in
78770 74 20 6e 42 79 74 65 29 3b 0a 53 51 4c 49 54 45 t nByte);.SQLITE
78780 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
78790 69 74 65 33 55 74 66 38 52 65 61 64 28 63 6f 6e ite3Utf8Read(con
787a0 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38 st u8*, const u8
787b0 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a *, const u8**);.
787c0 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 ./*.** Routines
787d0 74 6f 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 to read and writ
787e0 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 e variable-lengt
787f0 68 20 69 6e 74 65 67 65 72 73 2e 20 20 54 68 65 h integers. The
78800 73 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65 se used to.** be
78810 20 64 65 66 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 defined locally
78820 2c 20 62 75 74 20 6e 6f 77 20 77 65 20 75 73 65 , but now we use
78830 20 74 68 65 20 76 61 72 69 6e 74 20 72 6f 75 74 the varint rout
78840 69 6e 65 73 20 69 6e 20 74 68 65 20 75 74 69 6c ines in the util
78850 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20 20 43 6f 64 .c.** file. Cod
78860 65 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 e should use the
78870 20 4d 41 43 52 4f 20 66 6f 72 6d 73 20 62 65 6c MACRO forms bel
78880 6f 77 2c 20 61 73 20 74 68 65 20 56 61 72 69 6e ow, as the Varin
78890 74 33 32 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 t32 versions.**
788a0 61 72 65 20 63 6f 64 65 64 20 74 6f 20 61 73 73 are coded to ass
788b0 75 6d 65 20 74 68 65 20 73 69 6e 67 6c 65 20 62 ume the single b
788c0 79 74 65 20 63 61 73 65 20 69 73 20 61 6c 72 65 yte case is alre
788d0 61 64 79 20 68 61 6e 64 6c 65 64 20 28 77 68 69 ady handled (whi
788e0 63 68 20 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f ch .** the MACRO
788f0 20 66 6f 72 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a form does)..*/.
78900 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
78910 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 nt sqlite3PutVar
78920 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 int(unsigned cha
78930 72 2a 2c 20 75 36 34 29 3b 0a 53 51 4c 49 54 45 r*, u64);.SQLITE
78940 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
78950 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 ite3PutVarint32(
78960 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 unsigned char*,
78970 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u32);.SQLITE_PRI
78980 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
78990 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 GetVarint(const
789a0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c unsigned char *,
789b0 20 75 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f u64 *);.SQLITE_
789c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
789d0 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 te3GetVarint32(c
789e0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
789f0 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0a 53 51 ar *, u32 *);.SQ
78a00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
78a10 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 sqlite3VarintLe
78a20 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a 2a 2a n(u64 v);../*.**
78a30 20 54 68 65 20 68 65 61 64 65 72 20 6f 66 20 61 The header of a
78a40 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73 74 73 record consists
78a50 20 6f 66 20 61 20 73 65 71 75 65 6e 63 65 20 76 of a sequence v
78a60 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
78a70 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 ntegers..** Thes
78a80 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 61 e integers are a
78a90 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 73 6d 61 lmost always sma
78aa0 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e 63 6f 64 ll and are encod
78ab0 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65 20 62 ed as a single b
78ac0 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c yte..** The foll
78ad0 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 74 61 6b owing macros tak
78ae0 65 20 61 64 76 61 6e 74 61 67 65 20 74 68 69 73 e advantage this
78af0 20 66 61 63 74 20 74 6f 20 70 72 6f 76 69 64 65 fact to provide
78b00 20 61 20 66 61 73 74 20 65 6e 63 6f 64 65 0a 2a a fast encode.*
78b10 2a 20 61 6e 64 20 64 65 63 6f 64 65 20 6f 66 20 * and decode of
78b20 74 68 65 20 69 6e 74 65 67 65 72 73 20 69 6e 20 the integers in
78b30 61 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 2e a record header.
78b40 20 20 49 74 20 69 73 20 66 61 73 74 65 72 20 66 It is faster f
78b50 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a or the common.**
78b60 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 case where the
78b70 69 6e 74 65 67 65 72 20 69 73 20 61 20 73 69 6e integer is a sin
78b80 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20 69 73 gle byte. It is
78b90 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 a little slower
78ba0 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e 74 when the.** int
78bb0 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72 20 6d eger is two or m
78bc0 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75 74 20 ore bytes. But
78bd0 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20 66 61 overall it is fa
78be0 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ster..**.** The
78bf0 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 following expres
78c00 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69 76 61 sions are equiva
78c10 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 lent:.**.**
78c20 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 x = sqlite3GetVa
78c30 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20 29 3b rint32( A, &B );
78c40 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 .** x = sqli
78c50 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 20 te3PutVarint32(
78c60 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20 20 20 A, B );.**.**
78c70 20 20 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33 x = getVarint3
78c80 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 20 20 20 2( A, B );.**
78c90 20 20 78 20 3d 20 70 75 74 56 61 72 69 6e 74 33 x = putVarint3
78ca0 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2f 2( A, B );.**.*/
78cb0 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 72 69 .#define getVari
78cc0 6e 74 33 32 28 41 2c 42 29 20 20 28 28 2a 28 41 nt32(A,B) ((*(A
78cd0 29 3c 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 )<(unsigned char
78ce0 29 30 78 38 30 29 20 3f 20 28 28 42 29 20 3d 20 )0x80) ? ((B) =
78cf0 28 75 33 32 29 2a 28 41 29 29 2c 31 20 3a 20 73 (u32)*(A)),1 : s
78d00 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 qlite3GetVarint3
78d10 32 28 28 41 29 2c 20 28 75 33 32 20 2a 29 26 28 2((A), (u32 *)&(
78d20 42 29 29 29 0a 23 64 65 66 69 6e 65 20 70 75 74 B))).#define put
78d30 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 Varint32(A,B) (
78d40 28 28 75 33 32 29 28 42 29 3c 28 75 33 32 29 30 ((u32)(B)<(u32)0
78d50 78 38 30 29 20 3f 20 28 2a 28 41 29 20 3d 20 28 x80) ? (*(A) = (
78d60 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 42 unsigned char)(B
78d70 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 50 75 )),1 : sqlite3Pu
78d80 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28 tVarint32((A), (
78d90 42 29 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 B))).#define get
78da0 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 Varint sqlite
78db0 33 47 65 74 56 61 72 69 6e 74 0a 23 64 65 66 69 3GetVarint.#defi
78dc0 6e 65 20 70 75 74 56 61 72 69 6e 74 20 20 20 20 ne putVarint
78dd0 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 sqlite3PutVarint
78de0 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ...SQLITE_PRIVAT
78df0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e E void sqlite3In
78e00 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 dexAffinityStr(V
78e10 64 62 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b dbe *, Index *);
78e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78e30 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c void sqlite3Tabl
78e40 65 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 eAffinityStr(Vdb
78e50 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 e *, Table *);.S
78e60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
78e70 61 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 ar sqlite3Compar
78e80 65 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a eAffinity(Expr *
78e90 70 45 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 pExpr, char aff2
78ea0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
78eb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 E int sqlite3Ind
78ec0 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 exAffinityOk(Exp
78ed0 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 r *pExpr, char i
78ee0 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51 dx_affinity);.SQ
78ef0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
78f00 72 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 r sqlite3ExprAff
78f10 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 inity(Expr *pExp
78f20 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 r);.SQLITE_PRIVA
78f30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 TE int sqlite3At
78f40 6f 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a oi64(const char*
78f50 2c 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f , i64*);.SQLITE_
78f60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
78f70 69 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 ite3Error(sqlite
78f80 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 3*, int, const c
78f90 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 har*,...);.SQLIT
78fa0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
78fb0 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 sqlite3HexToBlob
78fc0 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 (sqlite3*, const
78fd0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 char *z, int n)
78fe0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
78ff0 20 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 int sqlite3TwoP
79000 61 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c artName(Parse *,
79010 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 Token *, Token
79020 2a 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 *, Token **);.SQ
79030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
79040 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
79050 45 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c ErrStr(int);.SQL
79060 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
79070 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d sqlite3ReadSchem
79080 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 a(Parse *pParse)
79090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
790a0 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 CollSeq *sqlite
790b0 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 3FindCollSeq(sql
790c0 69 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f ite3*,u8 enc, co
790d0 6e 73 74 20 63 68 61 72 20 2a 2c 69 6e 74 2c 69 nst char *,int,i
790e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
790f0 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c ATE CollSeq *sql
79100 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 ite3LocateCollSe
79110 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c q(Parse *pParse,
79120 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
79130 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 3b 0a me, int nName);.
79140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 SQLITE_PRIVATE C
79150 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 ollSeq *sqlite3E
79160 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 xprCollSeq(Parse
79170 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
79180 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 pExpr);.SQLITE_P
79190 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
791a0 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 ite3ExprSetColl(
791b0 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
791c0 78 70 72 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b xpr *, Token *);
791d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
791e0 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b int sqlite3Check
791f0 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c CollSeq(Parse *,
79200 20 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c CollSeq *);.SQL
79210 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
79220 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 sqlite3CheckObje
79230 63 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 ctName(Parse *,
79240 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 const char *);.S
79250 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
79260 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 id sqlite3VdbeSe
79270 74 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 tChanges(sqlite3
79280 20 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 *, int);..SQLIT
79290 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
792a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c void *sqlite3Val
792b0 75 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 ueText(sqlite3_v
792c0 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 alue*, u8);.SQLI
792d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
792e0 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 qlite3ValueBytes
792f0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c (sqlite3_value*,
79300 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u8);.SQLITE_PRI
79310 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
79320 33 56 61 6c 75 65 53 65 74 53 74 72 28 73 71 6c 3ValueSetStr(sql
79330 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74 ite3_value*, int
79340 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75 , const void *,u
79350 38 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 8, .
79360 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 void
79370 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
79380 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
79390 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 sqlite3ValueFre
793a0 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a e(sqlite3_value*
793b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
793c0 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 E sqlite3_value
793d0 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 *sqlite3ValueNew
793e0 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c (sqlite3 *);.SQL
793f0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
79400 20 2a 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f *sqlite3Utf16to
79410 38 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 8(sqlite3 *, con
79420 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a st void*, int);.
79430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
79440 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 nt sqlite3ValueF
79450 72 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33 20 romExpr(sqlite3
79460 2a 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 75 *, Expr *, u8, u
79470 38 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 8, sqlite3_value
79480 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 **);.SQLITE_PRI
79490 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
794a0 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 3ValueApplyAffin
794b0 69 74 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ity(sqlite3_valu
794c0 65 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 69 e *, u8, u8);.#i
794d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 fndef SQLITE_AMA
794e0 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 LGAMATION.SQLITE
794f0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 _PRIVATE const u
79500 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c nsigned char sql
79510 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 ite3UpperToLower
79520 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 [];.SQLITE_PRIVA
79530 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 TE SQLITE_WSD st
79540 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 ruct Sqlite3Conf
79550 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 ig sqlite3Config
79560 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
79570 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 SQLITE_WSD Func
79580 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 47 DefHash sqlite3G
79590 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a lobalFunctions;.
795a0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
795b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
795c0 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 e3RootPageMoved(
795d0 44 62 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a Db*, int, int);.
795e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
795f0 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 oid sqlite3Reind
79600 65 78 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e ex(Parse*, Token
79610 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
79620 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
79630 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 sqlite3AlterFunc
79640 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b tions(sqlite3*);
79650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
79660 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 void sqlite3Alte
79670 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72 rRenameTable(Par
79680 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 se*, SrcList*, T
79690 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
796a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
796b0 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 e3GetToken(const
796c0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
796d0 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 , int *);.SQLITE
796e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
796f0 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 lite3NestedParse
79700 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 (Parse*, const c
79710 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 har*, ...);.SQLI
79720 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
79730 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 sqlite3ExpirePre
79740 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 paredStatements(
79750 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
79760 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
79770 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c qlite3CodeSubsel
79780 65 63 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70 ect(Parse *, Exp
79790 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a r *, int, int);.
797a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
797b0 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 oid sqlite3Selec
797c0 74 50 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65 tPrep(Parse*, Se
797d0 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 lect*, NameConte
797e0 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 xt*);.SQLITE_PRI
797f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
79800 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
79810 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 (NameContext*, E
79820 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
79830 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
79840 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e e3ResolveSelectN
79850 61 6d 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c ames(Parse*, Sel
79860 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 ect*, NameContex
79870 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
79880 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
79890 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
798a0 42 79 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 By(Parse*, Selec
798b0 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 t*, ExprList*, c
798c0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
798d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
798e0 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 sqlite3ColumnDe
798f0 66 61 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 fault(Vdbe *, Ta
79900 62 6c 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ble *, int);.SQL
79910 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
79920 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e sqlite3AlterFin
79930 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 ishAddColumn(Par
79940 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a se *, Token *);.
79950 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
79960 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 oid sqlite3Alter
79970 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 BeginAddColumn(P
79980 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 arse *, SrcList
79990 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
799a0 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 TE CollSeq *sqli
799b0 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 73 71 te3GetCollSeq(sq
799c0 6c 69 74 65 33 2a 2c 20 43 6f 6c 6c 53 65 71 20 lite3*, CollSeq
799d0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
799e0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
799f0 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 IVATE char sqlit
79a00 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 63 e3AffinityType(c
79a10 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 onst Token*);.SQ
79a20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
79a30 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 d sqlite3Analyze
79a40 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c (Parse*, Token*,
79a50 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
79a60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
79a70 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 ite3InvokeBusyHa
79a80 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65 ndler(BusyHandle
79a90 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
79aa0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
79ab0 69 6e 64 44 62 28 73 71 6c 69 74 65 33 2a 2c 20 indDb(sqlite3*,
79ac0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
79ad0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
79ae0 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 te3AnalysisLoad(
79af0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44 42 sqlite3*,int iDB
79b00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
79b10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 E void sqlite3De
79b20 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 65 faultRowEst(Inde
79b30 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 x*);.SQLITE_PRIV
79b40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
79b50 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 RegisterLikeFunc
79b60 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 2c 20 tions(sqlite3*,
79b70 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
79b80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
79b90 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 IsLikeFunction(s
79ba0 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e qlite3*,Expr*,in
79bb0 74 2a 2c 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 t*,char*);.SQLIT
79bc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
79bd0 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c qlite3MinimumFil
79be0 65 46 6f 72 6d 61 74 28 50 61 72 73 65 2a 2c 20 eFormat(Parse*,
79bf0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
79c00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
79c10 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 qlite3SchemaFree
79c20 28 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 (void *);.SQLITE
79c30 5f 50 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 _PRIVATE Schema
79c40 2a 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 *sqlite3SchemaGe
79c50 74 28 73 71 6c 69 74 65 33 20 2a 2c 20 42 74 72 t(sqlite3 *, Btr
79c60 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ee *);.SQLITE_PR
79c70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
79c80 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 3SchemaToIndex(s
79c90 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65 qlite3 *db, Sche
79ca0 6d 61 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ma *);.SQLITE_PR
79cb0 49 56 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 IVATE KeyInfo *s
79cc0 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e qlite3IndexKeyin
79cd0 66 6f 28 50 61 72 73 65 20 2a 2c 20 49 6e 64 65 fo(Parse *, Inde
79ce0 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 x *);.SQLITE_PRI
79cf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
79d00 43 72 65 61 74 65 46 75 6e 63 28 73 71 6c 69 74 CreateFunc(sqlit
79d10 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 e3 *, const char
79d20 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f *, int, int, vo
79d30 69 64 20 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a id *, . void (*
79d40 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
79d50 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 t*,int,sqlite3_v
79d60 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 alue **),. void
79d70 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e (*)(sqlite3_con
79d80 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 text*,int,sqlite
79d90 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69 3_value **), voi
79da0 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f d (*)(sqlite3_co
79db0 6e 74 65 78 74 2a 29 29 3b 0a 53 51 4c 49 54 45 ntext*));.SQLITE
79dc0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
79dd0 69 74 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 ite3ApiExit(sqli
79de0 74 65 33 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53 te3 *db, int);.S
79df0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
79e00 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d t sqlite3OpenTem
79e10 70 44 61 74 61 62 61 73 65 28 50 61 72 73 65 20 pDatabase(Parse
79e20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);..SQLITE_PRIV
79e30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
79e40 53 74 72 41 63 63 75 6d 49 6e 69 74 28 53 74 72 StrAccumInit(Str
79e50 41 63 63 75 6d 2a 2c 20 63 68 61 72 2a 2c 20 69 Accum*, char*, i
79e60 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
79e70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
79e80 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
79e90 65 6e 64 28 53 74 72 41 63 63 75 6d 2a 2c 63 6f end(StrAccum*,co
79ea0 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a nst char*,int);.
79eb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
79ec0 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 har *sqlite3StrA
79ed0 63 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 ccumFinish(StrAc
79ee0 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 cum*);.SQLITE_PR
79ef0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
79f00 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 e3StrAccumReset(
79f10 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 StrAccum*);.SQLI
79f20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
79f30 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 sqlite3SelectDes
79f40 74 49 6e 69 74 28 53 65 6c 65 63 74 44 65 73 74 tInit(SelectDest
79f50 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a *,int,int);../*.
79f60 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 ** The interface
79f70 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 to the LEMON-ge
79f80 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 0a 2a nerated parser.*
79f90 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
79fa0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
79fb0 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 rserAlloc(void*(
79fc0 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c *)(size_t));.SQL
79fd0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
79fe0 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72 sqlite3ParserFr
79ff0 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a ee(void*, void(*
7a000 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
7a010 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
7a020 71 6c 69 74 65 33 50 61 72 73 65 72 28 76 6f 69 qlite3Parser(voi
7a030 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 d*, int, Token,
7a040 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65 66 20 Parse*);.#ifdef
7a050 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 YYTRACKMAXSTACKD
7a060 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 EPTH.SQLITE_PRIV
7a070 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
7a080 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b 3ParserStackPeak
7a090 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a (void*);.#endif.
7a0a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7a0b0 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 6f 4c int sqlite3AutoL
7a0c0 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 oadExtensions(sq
7a0d0 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 66 lite3*);.#ifndef
7a0e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
7a0f0 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 D_EXTENSION.SQLI
7a100 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
7a110 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 d sqlite3CloseEx
7a120 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 tensions(sqlite3
7a130 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 *);.#else.# defi
7a140 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 ne sqlite3CloseE
7a150 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65 6e xtensions(X).#en
7a160 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
7a170 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
7a180 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 CACHE.SQLITE_PRI
7a190 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
7a1a0 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61 72 te3TableLock(Par
7a1b0 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 se *, int, int,
7a1c0 75 38 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a u8, const char *
7a1d0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 );.#else. #defi
7a1e0 6e 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c ne sqlite3TableL
7a1f0 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 ock(v,w,x,y,z).#
7a200 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
7a210 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
7a220 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
7a230 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75 6e qlite3Utf8To8(un
7a240 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 23 signed char*);.#
7a250 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
7a260 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
7a270 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 LTABLE.# define
7a280 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 sqlite3VtabClea
7a290 72 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 r(X).# define s
7a2a0 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 58 qlite3VtabSync(X
7a2b0 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 ,Y) SQLITE_OK.#
7a2c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
7a2d0 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a 23 tabRollback(X).#
7a2e0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
7a2f0 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 20 VtabCommit(X).#
7a300 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
7a310 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 30 0a tabInSync(db) 0.
7a320 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 #else.SQLITE_PRI
7a330 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c VATE void sql
7a340 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 54 61 ite3VtabClear(Ta
7a350 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ble*);.SQLITE_PR
7a360 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c IVATE int sql
7a370 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c ite3VtabSync(sql
7a380 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a ite3 *db, char *
7a390 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
7a3a0 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 TE int sqlite
7a3b0 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 3VtabRollback(sq
7a3c0 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 lite3 *db);.SQLI
7a3d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e TE_PRIVATE in
7a3e0 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d t sqlite3VtabCom
7a3f0 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 mit(sqlite3 *db)
7a400 3b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 ;.# define sqli
7a410 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 te3VtabInSync(db
7a420 29 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 ) ((db)->nVTrans
7a430 3e 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54 72 >0 && (db)->aVTr
7a440 61 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53 ans==0).#endif.S
7a450 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7a460 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 id sqlite3VtabMa
7a470 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 65 keWritable(Parse
7a480 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 *,Table*);.SQLIT
7a490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
7a4a0 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 73 qlite3VtabLock(s
7a4b0 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53 qlite3_vtab*);.S
7a4c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7a4d0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e id sqlite3VtabUn
7a4e0 6c 6f 63 6b 28 73 71 6c 69 74 65 33 2a 2c 20 73 lock(sqlite3*, s
7a4f0 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53 qlite3_vtab*);.S
7a500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
7a510 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 id sqlite3VtabBe
7a520 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c ginParse(Parse*,
7a530 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c Token*, Token*,
7a540 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
7a550 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
7a560 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 lite3VtabFinishP
7a570 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b arse(Parse*, Tok
7a580 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
7a590 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
7a5a0 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 72 3VtabArgInit(Par
7a5b0 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 se*);.SQLITE_PRI
7a5c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
7a5d0 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28 50 3VtabArgExtend(P
7a5e0 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a arse*, Token*);.
7a5f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
7a600 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 nt sqlite3VtabCa
7a610 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 llCreate(sqlite3
7a620 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 *, int, const ch
7a630 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a ar *, char **);.
7a640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
7a650 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 nt sqlite3VtabCa
7a660 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a llConnect(Parse*
7a670 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 , Table*);.SQLIT
7a680 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7a690 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73 lite3VtabCallDes
7a6a0 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 troy(sqlite3*, i
7a6b0 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a nt, const char *
7a6c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7a6d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 E int sqlite3Vta
7a6e0 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 20 2a bBegin(sqlite3 *
7a6f0 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a , sqlite3_vtab *
7a700 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7a710 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 E FuncDef *sqlit
7a720 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 e3VtabOverloadFu
7a730 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a nction(sqlite3 *
7a740 2c 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74 20 6e ,FuncDef*, int n
7a750 41 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c Arg, Expr*);.SQL
7a760 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
7a770 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 sqlite3InvalidF
7a780 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 5f unction(sqlite3_
7a790 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
7a7a0 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53 ite3_value**);.S
7a7b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
7a7c0 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 t sqlite3Transfe
7a7d0 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 rBindings(sqlite
7a7e0 33 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69 74 65 3_stmt *, sqlite
7a7f0 33 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c 49 54 3_stmt *);.SQLIT
7a800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
7a810 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56 lite3Reprepare(V
7a820 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
7a830 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
7a840 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c e3ExprListCheckL
7a850 65 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45 78 ength(Parse*, Ex
7a860 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 prList*, const c
7a870 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
7a880 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
7a890 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 qlite3BinaryComp
7a8a0 61 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 areCollSeq(Parse
7a8b0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 *, Expr *, Expr
7a8c0 20 2a 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 *);.../*.** Ava
7a8d0 69 6c 61 62 6c 65 20 66 61 75 6c 74 20 69 6e 6a ilable fault inj
7a8e0 65 63 74 6f 72 73 2e 20 20 53 68 6f 75 6c 64 20 ectors. Should
7a8f0 62 65 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69 be numbered begi
7a900 6e 6e 69 6e 67 20 77 69 74 68 20 30 2e 0a 2a 2f nning with 0..*/
7a910 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7a920 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 FAULTINJECTOR_MA
7a930 4c 4c 4f 43 20 20 20 20 20 30 0a 23 64 65 66 69 LLOC 0.#defi
7a940 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 ne SQLITE_FAULTI
7a950 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 20 20 NJECTOR_COUNT
7a960 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 1../*.** The
7a970 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 interface to the
7a980 20 63 6f 64 65 20 69 6e 20 66 61 75 6c 74 2e 63 code in fault.c
7a990 20 75 73 65 64 20 66 6f 72 20 69 64 65 6e 74 69 used for identi
7a9a0 66 79 69 6e 67 20 22 62 65 6e 69 67 6e 22 0a 2a fying "benign".*
7a9b0 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 * malloc failure
7a9c0 73 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 s. This is only
7a9d0 70 72 65 73 65 6e 74 20 69 66 20 53 51 4c 49 54 present if SQLIT
7a9e0 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 E_OMIT_BUILTIN_T
7a9f0 45 53 54 0a 2a 2a 20 69 73 20 6e 6f 74 20 64 65 EST.** is not de
7aa00 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 fined..*/.#ifnde
7aa10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
7aa20 49 4c 54 49 4e 5f 54 45 53 54 0a 53 51 4c 49 54 ILTIN_TEST.SQLIT
7aa30 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
7aa40 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
7aa50 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b ignMalloc(void);
7aa60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7aa70 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e void sqlite3En
7aa80 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f dBenignMalloc(vo
7aa90 69 64 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 id);.#else. #de
7aaa0 66 69 6e 65 20 73 71 6c 69 74 65 33 42 65 67 69 fine sqlite3Begi
7aab0 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a nBenignMalloc().
7aac0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
7aad0 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
7aae0 28 29 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 ().#endif..#defi
7aaf0 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 ne IN_INDEX_ROWI
7ab00 44 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 D 1.#d
7ab10 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 45 efine IN_INDEX_E
7ab20 50 48 20 20 20 20 20 20 20 20 20 20 20 20 20 32 PH 2
7ab30 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 .#define IN_INDE
7ab40 58 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 X_INDEX
7ab50 20 20 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 3.SQLITE_PRIVA
7ab60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
7ab70 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20 ndInIndex(Parse
7ab80 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2a 29 *, Expr *, int*)
7ab90 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
7aba0 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 _ENABLE_ATOMIC_W
7abb0 52 49 54 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 RITE.SQLITE_PRIV
7abc0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
7abd0 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 3JournalOpen(sql
7abe0 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 ite3_vfs *, cons
7abf0 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 t char *, sqlite
7ac00 33 5f 66 69 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 3_file *, int, i
7ac10 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
7ac20 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
7ac30 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 3JournalSize(sql
7ac40 69 74 65 33 5f 76 66 73 20 2a 29 3b 0a 53 51 4c ite3_vfs *);.SQL
7ac50 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
7ac60 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c t sqlite3Journal
7ac70 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 5f 66 Create(sqlite3_f
7ac80 69 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 ile *);.#else.
7ac90 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4a #define sqlite3J
7aca0 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 ournalSize(pVfs)
7acb0 20 28 28 70 56 66 73 29 2d 3e 73 7a 4f 73 46 69 ((pVfs)->szOsFi
7acc0 6c 65 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 le).#endif..SQLI
7acd0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
7ace0 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
7acf0 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 lOpen(sqlite3_fi
7ad00 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 le *);.SQLITE_PR
7ad10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
7ad20 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
7ad30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
7ad40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d E int sqlite3IsM
7ad50 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 emJournal(sqlite
7ad60 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 3_file *);..#if
7ad70 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f SQLITE_MAX_EXPR_
7ad80 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 DEPTH>0.SQLITE_P
7ad90 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
7ada0 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 lite3ExprSetHeig
7adb0 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ht(Parse *pParse
7adc0 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 , Expr *p);.SQLI
7add0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
7ade0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 sqlite3SelectEx
7adf0 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 prHeight(Select
7ae00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
7ae10 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
7ae20 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 ExprCheckHeight(
7ae30 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 Parse*, int);.#e
7ae40 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
7ae50 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 lite3ExprSetHeig
7ae60 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e ht(x,y). #defin
7ae70 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 e sqlite3SelectE
7ae80 78 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20 xprHeight(x) 0.
7ae90 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
7aea0 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 ExprCheckHeight(
7aeb0 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c x,y).#endif..SQL
7aec0 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
7aed0 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 sqlite3Get4byte(
7aee0 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 const u8*);.SQLI
7aef0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
7af00 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 sqlite3Put4byte(
7af10 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 u8*, u32);..#ifd
7af20 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 23 69 ef SQLITE_SSE.#i
7af30 6e 63 6c 75 64 65 20 22 73 73 65 49 6e 74 2e 68 nclude "sseInt.h
7af40 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ".#endif..#ifdef
7af50 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 SQLITE_DEBUG.SQ
7af60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
7af70 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 oid sqlite3Parse
7af80 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 63 68 rTrace(FILE*, ch
7af90 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f ar *);.#endif../
7afa0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 *.** If the SQLI
7afb0 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 41 43 TE_ENABLE IOTRAC
7afc0 45 20 65 78 69 73 74 73 20 74 68 65 6e 20 74 68 E exists then th
7afd0 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c e global variabl
7afe0 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f 54 72 e.** sqlite3IoTr
7aff0 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ace is a pointer
7b000 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c 69 6b to a printf-lik
7b010 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74 e routine used t
7b020 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f 20 74 o.** print I/O t
7b030 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65 73 2e racing messages.
7b040 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 .*/.#ifdef SQLI
7b050 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 TE_ENABLE_IOTRAC
7b060 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41 E.# define IOTRA
7b070 43 45 28 41 29 20 20 69 66 28 20 73 71 6c 69 74 CE(A) if( sqlit
7b080 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73 71 6c e3IoTrace ){ sql
7b090 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b 20 7d ite3IoTrace A; }
7b0a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
7b0b0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
7b0c0 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 beIOTraceSql(Vdb
7b0d0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
7b0e0 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 ATE void (*sqlit
7b0f0 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 e3IoTrace)(const
7b100 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c char*,...);.#el
7b110 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 se.# define IOTR
7b120 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e 65 20 ACE(A).# define
7b130 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 sqlite3VdbeIOTra
7b140 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 66 0a ceSql(X).#endif.
7b150 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
7b160 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
7b170 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
7b180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b1a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
7b1b0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
7b1c0 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a ile global.c ***
7b1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b1f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
7b200 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a 008 June 13.**.*
7b210 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
7b220 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
7b230 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
7b240 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
7b250 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
7b260 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
7b270 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
7b280 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
7b290 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
7b2a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
7b2b0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
7b2c0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
7b2d0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
7b2e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
7b2f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
7b300 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
7b310 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
7b320 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
7b330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
7b370 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
7b380 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 ins definitions
7b390 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 of global variab
7b3a0 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e 74 73 les and contants
7b3b0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 67 6c 6f ..**.** $Id: glo
7b3c0 62 61 6c 2e 63 2c 76 20 31 2e 38 20 32 30 30 38 bal.c,v 1.8 2008
7b3d0 2f 30 39 2f 30 34 20 31 37 3a 31 37 3a 33 39 20 /09/04 17:17:39
7b3e0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
7b3f0 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6e 20 61 72 72 $.*/.../* An arr
7b400 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20 75 70 ay to map all up
7b410 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 74 per-case charact
7b420 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72 20 63 ers into their c
7b430 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 orresponding.**
7b440 6c 6f 77 65 72 2d 63 61 73 65 20 63 68 61 72 61 lower-case chara
7b450 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 51 4c cter. .**.** SQL
7b460 69 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 ite only conside
7b470 72 73 20 55 53 2d 41 53 43 49 49 20 28 6f 72 20 rs US-ASCII (or
7b480 45 42 43 44 49 43 29 20 63 68 61 72 61 63 74 65 EBCDIC) characte
7b490 72 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 0a 2a rs. We do not.*
7b4a0 2a 20 68 61 6e 64 6c 65 20 63 61 73 65 20 63 6f * handle case co
7b4b0 6e 76 65 72 73 69 6f 6e 73 20 66 6f 72 20 74 68 nversions for th
7b4c0 65 20 55 54 46 20 63 68 61 72 61 63 74 65 72 20 e UTF character
7b4d0 73 65 74 20 73 69 6e 63 65 20 74 68 65 20 74 61 set since the ta
7b4e0 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76 65 64 bles.** involved
7b4f0 20 61 72 65 20 6e 65 61 72 6c 79 20 61 73 20 62 are nearly as b
7b500 69 67 20 6f 72 20 62 69 67 67 65 72 20 74 68 61 ig or bigger tha
7b510 6e 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66 2e n SQLite itself.
7b520 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
7b530 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 TE const unsigne
7b540 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70 d char sqlite3Up
7b550 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d 20 7b perToLower[] = {
7b560 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 .#ifdef SQLITE_A
7b570 53 43 49 49 0a 20 20 20 20 20 20 30 2c 20 20 31 SCII. 0, 1
7b580 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 , 2, 3, 4, 5
7b590 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 , 6, 7, 8, 9
7b5a0 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 , 10, 11, 12, 13
7b5b0 2c 20 31 34 2c 20 31 35 2c 20 31 36 2c 20 31 37 , 14, 15, 16, 17
7b5c0 2c 0a 20 20 20 20 20 31 38 2c 20 31 39 2c 20 32 ,. 18, 19, 2
7b5d0 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 0, 21, 22, 23, 2
7b5e0 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 4, 25, 26, 27, 2
7b5f0 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 33 8, 29, 30, 31, 3
7b600 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 0a 20 2, 33, 34, 35,.
7b610 20 20 20 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 36, 37, 38,
7b620 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 39, 40, 41, 42,
7b630 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 43, 44, 45, 46,
7b640 34 37 2c 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 47, 48, 49, 50,
7b650 35 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20 20 20 51, 52, 53,.
7b660 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 54, 55, 56, 57,
7b670 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 58, 59, 60, 61,
7b680 20 36 32 2c 20 36 33 2c 20 36 34 2c 20 39 37 2c 62, 63, 64, 97,
7b690 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 98, 99,100,101,
7b6a0 31 30 32 2c 31 30 33 2c 0a 20 20 20 20 31 30 34 102,103,. 104
7b6b0 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 31 30 38 ,105,106,107,108
7b6c0 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 ,109,110,111,112
7b6d0 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 ,113,114,115,116
7b6e0 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 ,117,118,119,120
7b6f0 2c 31 32 31 2c 0a 20 20 20 20 31 32 32 2c 20 39 ,121,. 122, 9
7b700 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 1, 92, 93, 94, 9
7b710 35 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c 20 39 5, 96, 97, 98, 9
7b720 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 9,100,101,102,10
7b730 33 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 3,104,105,106,10
7b740 37 2c 0a 20 20 20 20 31 30 38 2c 31 30 39 2c 31 7,. 108,109,1
7b750 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 10,111,112,113,1
7b760 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 14,115,116,117,1
7b770 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 31 18,119,120,121,1
7b780 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 0a 22,123,124,125,.
7b790 20 20 20 20 31 32 36 2c 31 32 37 2c 31 32 38 2c 126,127,128,
7b7a0 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 129,130,131,132,
7b7b0 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 133,134,135,136,
7b7c0 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 137,138,139,140,
7b7d0 31 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20 20 20 141,142,143,.
7b7e0 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 144,145,146,147
7b7f0 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 ,148,149,150,151
7b800 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 ,152,153,154,155
7b810 2c 31 35 36 2c 31 35 37 2c 31 35 38 2c 31 35 39 ,156,157,158,159
7b820 2c 31 36 30 2c 31 36 31 2c 0a 20 20 20 20 31 36 ,160,161,. 16
7b830 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 2,163,164,165,16
7b840 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 6,167,168,169,17
7b850 30 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c 31 37 0,171,172,173,17
7b860 34 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c 31 37 4,175,176,177,17
7b870 38 2c 31 37 39 2c 0a 20 20 20 20 31 38 30 2c 31 8,179,. 180,1
7b880 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 81,182,183,184,1
7b890 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 85,186,187,188,1
7b8a0 38 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 2c 31 89,190,191,192,1
7b8b0 39 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 2c 31 93,194,195,196,1
7b8c0 39 37 2c 0a 20 20 20 20 31 39 38 2c 31 39 39 2c 97,. 198,199,
7b8d0 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 33 2c 200,201,202,203,
7b8e0 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 204,205,206,207,
7b8f0 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 31 2c 208,209,210,211,
7b900 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 35 2c 212,213,214,215,
7b910 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32 31 38 . 216,217,218
7b920 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 ,219,220,221,222
7b930 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 32 36 ,223,224,225,226
7b940 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 33 30 ,227,228,229,230
7b950 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a 20 20 ,231,232,233,.
7b960 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c 32 33 234,235,236,23
7b970 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c 32 34 7,238,239,240,24
7b980 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 1,242,243,244,24
7b990 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 5,246,247,248,24
7b9a0 39 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20 20 32 9,250,251,. 2
7b9b0 35 32 2c 32 35 33 2c 32 35 34 2c 32 35 35 0a 23 52,253,254,255.#
7b9c0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
7b9d0 49 54 45 5f 45 42 43 44 49 43 0a 20 20 20 20 20 ITE_EBCDIC.
7b9e0 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 0, 1, 2, 3,
7b9f0 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 4, 5, 6, 7,
7ba00 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 8, 9, 10, 11,
7ba10 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 12, 13, 14, 15,
7ba20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20 31 36 /* 0x */. 16
7ba30 2c 20 31 37 2c 20 31 38 2c 20 31 39 2c 20 32 30 , 17, 18, 19, 20
7ba40 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 , 21, 22, 23, 24
7ba50 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 , 25, 26, 27, 28
7ba60 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 2f 2a , 29, 30, 31, /*
7ba70 20 31 78 20 2a 2f 0a 20 20 20 20 20 33 32 2c 20 1x */. 32,
7ba80 33 33 2c 20 33 34 2c 20 33 35 2c 20 33 36 2c 20 33, 34, 35, 36,
7ba90 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 37, 38, 39, 40,
7baa0 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 41, 42, 43, 44,
7bab0 34 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a 20 32 45, 46, 47, /* 2
7bac0 78 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20 34 39 x */. 48, 49
7bad0 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 , 50, 51, 52, 53
7bae0 2c 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 , 54, 55, 56, 57
7baf0 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 , 58, 59, 60, 61
7bb00 2c 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33 78 20 , 62, 63, /* 3x
7bb10 2a 2f 0a 20 20 20 20 20 36 34 2c 20 36 35 2c 20 */. 64, 65,
7bb20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 66, 67, 68, 69,
7bb30 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 20 70, 71, 72, 73,
7bb40 37 34 2c 20 37 35 2c 20 37 36 2c 20 37 37 2c 20 74, 75, 76, 77,
7bb50 37 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20 2a 2f 78, 79, /* 4x */
7bb60 0a 20 20 20 20 20 38 30 2c 20 38 31 2c 20 38 32 . 80, 81, 82
7bb70 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 , 83, 84, 85, 86
7bb80 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 20 39 30 , 87, 88, 89, 90
7bb90 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 , 91, 92, 93, 94
7bba0 2c 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f 0a 20 , 95, /* 5x */.
7bbb0 20 20 20 20 39 36 2c 20 39 37 2c 20 36 36 2c 20 96, 97, 66,
7bbc0 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 67, 68, 69, 70,
7bbd0 37 31 2c 20 37 32 2c 20 37 33 2c 31 30 36 2c 31 71, 72, 73,106,1
7bbe0 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 07,108,109,110,1
7bbf0 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 11, /* 6x */.
7bc00 20 31 31 32 2c 20 38 31 2c 20 38 32 2c 20 38 33 112, 81, 82, 83
7bc10 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 , 84, 85, 86, 87
7bc20 2c 20 38 38 2c 20 38 39 2c 31 32 32 2c 31 32 33 , 88, 89,122,123
7bc30 2c 31 32 34 2c 31 32 35 2c 31 32 36 2c 31 32 37 ,124,125,126,127
7bc40 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20 31 , /* 7x */. 1
7bc50 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 28,129,130,131,1
7bc60 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 32,133,134,135,1
7bc70 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 36,137,138,139,1
7bc80 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 20 40,141,142,143,
7bc90 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31 34 34 /* 8x */. 144
7bca0 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 ,145,146,147,148
7bcb0 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 ,149,150,151,152
7bcc0 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 ,153,154,155,156
7bcd0 2c 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 2f 2a ,157,156,159, /*
7bce0 20 39 78 20 2a 2f 0a 20 20 20 20 31 36 30 2c 31 9x */. 160,1
7bcf0 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 61,162,163,164,1
7bd00 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 65,166,167,168,1
7bd10 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 2c 31 69,170,171,140,1
7bd20 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a 20 41 41,142,175, /* A
7bd30 78 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31 37 37 x */. 176,177
7bd40 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 38 31 ,178,179,180,181
7bd50 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 ,182,183,184,185
7bd60 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 ,186,187,188,189
7bd70 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 78 20 ,190,191, /* Bx
7bd80 2a 2f 0a 20 20 20 20 31 39 32 2c 31 32 39 2c 31 */. 192,129,1
7bd90 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 30,131,132,133,1
7bda0 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 32 34,135,136,137,2
7bdb0 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 02,203,204,205,2
7bdc0 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20 2a 2f 06,207, /* Cx */
7bdd0 0a 20 20 20 20 32 30 38 2c 31 34 35 2c 31 34 36 . 208,145,146
7bde0 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 ,147,148,149,150
7bdf0 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 32 31 38 ,151,152,153,218
7be00 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 ,219,220,221,222
7be10 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f 0a 20 ,223, /* Dx */.
7be20 20 20 20 32 32 34 2c 32 32 35 2c 31 36 32 2c 31 224,225,162,1
7be30 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 63,164,165,166,1
7be40 36 37 2c 31 36 38 2c 31 36 39 2c 32 33 32 2c 32 67,168,169,232,2
7be50 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 03,204,205,206,2
7be60 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 07, /* Ex */.
7be70 20 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 239,240,241,242
7be80 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 ,243,244,245,246
7be90 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 31 39 ,247,248,249,219
7bea0 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 35 35 ,220,221,222,255
7beb0 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e 64 69 , /* Fx */.#endi
7bec0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 f.};../*.** The
7bed0 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67 6c 65 following single
7bee0 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ton contains the
7bef0 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 global configur
7bf00 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68 65 ation for.** the
7bf10 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e SQLite library.
7bf20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
7bf30 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 TE SQLITE_WSD st
7bf40 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 ruct Sqlite3Conf
7bf50 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 ig sqlite3Config
7bf60 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45 5f 44 = {. SQLITE_D
7bf70 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 EFAULT_MEMSTATUS
7bf80 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74 20 2a , /* bMemstat *
7bf90 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 /. 1,
7bfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7bfb0 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 20 2a 2f /* bCoreMutex */
7bfc0 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 52 45 41 . SQLITE_THREA
7bfd0 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 20 20 2f DSAFE==1, /
7bfe0 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a 2f 0a * bFullMutex */.
7bff0 20 20 20 30 78 37 66 66 66 66 66 66 65 2c 20 20 0x7ffffffe,
7c000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7c010 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 20 20 mxStrlen */.
7c020 31 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 100,
7c030 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a /* sz
7c040 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 Lookaside */.
7c050 35 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 500,
7c060 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 4c /* nL
7c070 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 2f ookaside */. /
7c080 2a 20 4f 74 68 65 72 20 66 69 65 6c 64 73 20 61 * Other fields a
7c090 6c 6c 20 64 65 66 61 75 6c 74 20 74 6f 20 7a 65 ll default to ze
7c0a0 72 6f 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a ro */.};.../*.**
7c0b0 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 Hash table for
7c0c0 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 73 global functions
7c0d0 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6d - functions com
7c0e0 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 64 61 mon to all.** da
7c0f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
7c100 6e 73 2e 20 20 41 66 74 65 72 20 69 6e 69 74 69 ns. After initi
7c110 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 69 73 20 alization, this
7c120 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72 65 61 64 table is.** read
7c130 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 -only..*/.SQLITE
7c140 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f _PRIVATE SQLITE_
7c150 57 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 20 WSD FuncDefHash
7c160 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e sqlite3GlobalFun
7c170 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a ctions;../******
7c180 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
7c190 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a global.c *******
7c1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
7c1d0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
7c1e0 69 6c 65 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a ile status.c ***
7c1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c210 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
7c220 30 30 38 20 4a 75 6e 65 20 31 38 0a 2a 2a 0a 2a 008 June 18.**.*
7c230 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
7c240 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
7c250 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
7c260 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
7c270 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
7c280 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
7c290 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
7c2a0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
7c2b0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
7c2c0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
7c2d0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
7c2e0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
7c2f0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
7c300 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
7c310 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
7c320 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
7c330 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
7c340 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
7c350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7c380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
7c390 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70 This module imp
7c3a0 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69 lements the sqli
7c3b0 74 65 33 5f 73 74 61 74 75 73 28 29 20 69 6e 74 te3_status() int
7c3c0 65 72 66 61 63 65 20 61 6e 64 20 72 65 6c 61 74 erface and relat
7c3d0 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 61 6c ed.** functional
7c3e0 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ity..**.** $Id:
7c3f0 73 74 61 74 75 73 2e 63 2c 76 20 31 2e 39 20 32 status.c,v 1.9 2
7c400 30 30 38 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 008/09/02 00:52:
7c410 35 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 52 drh Exp $.*/.
7c420 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 73 ./*.** Variables
7c430 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 63 in which to rec
7c440 6f 72 64 20 73 74 61 74 75 73 20 69 6e 66 6f 72 ord status infor
7c450 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 mation..*/.typed
7c460 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
7c470 33 53 74 61 74 54 79 70 65 20 73 71 6c 69 74 65 3StatType sqlite
7c480 33 53 74 61 74 54 79 70 65 3b 0a 73 74 61 74 69 3StatType;.stati
7c490 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 c SQLITE_WSD str
7c4a0 75 63 74 20 73 71 6c 69 74 65 33 53 74 61 74 54 uct sqlite3StatT
7c4b0 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e 6f 77 56 ype {. int nowV
7c4c0 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 20 alue[9];
7c4d0 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 61 6c 75 /* Current valu
7c4e0 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 56 61 6c e */. int mxVal
7c4f0 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 20 20 20 ue[9];
7c500 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 6c 75 65 /* Maximum value
7c510 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 53 74 61 */.} sqlite3Sta
7c520 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b 30 2c 7d t = { {0,}, {0,}
7c530 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 22 77 73 };.../* The "ws
7c540 64 53 74 61 74 22 20 6d 61 63 72 6f 20 77 69 6c dStat" macro wil
7c550 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 l resolve to the
7c560 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 status informat
7c570 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 76 65 63 ion.** state vec
7c580 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c tor. If writabl
7c590 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 e static data is
7c5a0 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 unsupported on
7c5b0 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 the target,.** w
7c5c0 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 e have to locate
7c5d0 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f the state vecto
7c5e0 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 r at run-time.
7c5f0 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d In the more comm
7c600 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65 on.** case where
7c610 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 writable static
7c620 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 data is support
7c630 65 64 2c 20 77 73 64 53 74 61 74 20 63 61 6e 20 ed, wsdStat can
7c640 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a 2a refer directly.*
7c650 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 * to the "sqlite
7c660 33 53 74 61 74 22 20 73 74 61 74 65 20 76 65 63 3Stat" state vec
7c670 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f tor declared abo
7c680 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ve..*/.#ifdef SQ
7c690 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 LITE_OMIT_WSD.#
7c6a0 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e define wsdStatIn
7c6b0 69 74 20 20 73 71 6c 69 74 65 33 53 74 61 74 54 it sqlite3StatT
7c6c0 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c ype *x = &GLOBAL
7c6d0 28 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 (sqlite3StatType
7c6e0 2c 73 71 6c 69 74 65 33 53 74 61 74 29 0a 23 20 ,sqlite3Stat).#
7c6f0 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 20 78 define wsdStat x
7c700 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 [0].#else.# defi
7c710 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74 0a 23 ne wsdStatInit.#
7c720 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 20 define wsdStat
7c730 73 71 6c 69 74 65 33 53 74 61 74 0a 23 65 6e 64 sqlite3Stat.#end
7c740 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e if../*.** Return
7c750 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c the current val
7c760 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 70 ue of a status p
7c770 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 53 51 4c arameter..*/.SQL
7c780 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
7c790 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c sqlite3StatusVal
7c7a0 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 20 77 73 ue(int op){. ws
7c7b0 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 dStatInit;. ass
7c7c0 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 ert( op>=0 && op
7c7d0 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 <ArraySize(wsdSt
7c7e0 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a at.nowValue) );.
7c7f0 20 20 72 65 74 75 72 6e 20 77 73 64 53 74 61 74 return wsdStat
7c800 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 7d .nowValue[op];.}
7c810 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e 20 74 6f ../*.** Add N to
7c820 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 the value of a
7c830 73 74 61 74 75 73 20 72 65 63 6f 72 64 2e 20 20 status record.
7c840 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 It is assumed th
7c850 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 at the.** caller
7c860 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 72 69 61 holds appropria
7c870 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 51 4c te locks..*/.SQL
7c880 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
7c890 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
7c8a0 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 4e 29 d(int op, int N)
7c8b0 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b {. wsdStatInit;
7c8c0 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 . assert( op>=0
7c8d0 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 && op<ArraySize
7c8e0 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 (wsdStat.nowValu
7c8f0 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e e) );. wsdStat.
7c900 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 2b 3d 20 nowValue[op] +=
7c910 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61 74 N;. if( wsdStat
7c920 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 .nowValue[op]>ws
7c930 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 dStat.mxValue[op
7c940 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 ] ){. wsdStat
7c950 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 .mxValue[op] = w
7c960 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b sdStat.nowValue[
7c970 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a op];. }.}../*.*
7c980 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 * Set the value
7c990 6f 66 20 61 20 73 74 61 74 75 73 20 74 6f 20 58 of a status to X
7c9a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
7c9b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
7c9c0 53 74 61 74 75 73 53 65 74 28 69 6e 74 20 6f 70 StatusSet(int op
7c9d0 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 73 64 53 , int X){. wsdS
7c9e0 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 tatInit;. asser
7c9f0 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 t( op>=0 && op<A
7ca00 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 rraySize(wsdStat
7ca10 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 .nowValue) );.
7ca20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
7ca30 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 66 28 20 [op] = X;. if(
7ca40 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
7ca50 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 [op]>wsdStat.mxV
7ca60 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 alue[op] ){.
7ca70 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b wsdStat.mxValue[
7ca80 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f op] = wsdStat.no
7ca90 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a wValue[op];. }.
7caa0 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73 }../*.** Query s
7cab0 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f tatus informatio
7cac0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d n..**.** This im
7cad0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 plementation ass
7cae0 75 6d 65 73 20 74 68 61 74 20 72 65 61 64 69 6e umes that readin
7caf0 67 20 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 20 g or writing an
7cb00 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 2d 62 69 aligned.** 32-bi
7cb10 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 6e 20 t integer is an
7cb20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f 6e atomic operation
7cb30 2e 20 20 49 66 20 74 68 61 74 20 61 73 73 75 6d . If that assum
7cb40 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 74 72 75 ption is not tru
7cb50 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 e,.** then this
7cb60 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 74 routine is not t
7cb70 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 51 hreadsafe..*/.SQ
7cb80 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
7cb90 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e 74 20 ite3_status(int
7cba0 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 65 6e op, int *pCurren
7cbb0 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 t, int *pHighwat
7cbc0 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 61 er, int resetFla
7cbd0 67 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 g){. wsdStatIni
7cbe0 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 20 7c 7c t;. if( op<0 ||
7cbf0 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a 65 28 77 op>=ArraySize(w
7cc00 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 sdStat.nowValue)
7cc10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
7cc20 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
7cc30 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 }. *pCurrent =
7cc40 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
7cc50 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67 68 77 61 [op];. *pHighwa
7cc60 74 65 72 20 3d 20 77 73 64 53 74 61 74 2e 6d 78 ter = wsdStat.mx
7cc70 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 69 66 28 Value[op];. if(
7cc80 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 resetFlag ){.
7cc90 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 wsdStat.mxValu
7cca0 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e e[op] = wsdStat.
7ccb0 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 nowValue[op];.
7ccc0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
7ccd0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 E_OK;.}../*.** Q
7cce0 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 6f uery status info
7ccf0 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 73 69 rmation for a si
7cd00 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 63 6f ngle database co
7cd10 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 nnection.*/.SQLI
7cd20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
7cd30 65 33 5f 64 62 5f 73 74 61 74 75 73 28 0a 20 20 e3_db_status(.
7cd40 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
7cd50 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
7cd60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
7cd70 20 77 68 6f 73 65 20 73 74 61 74 75 73 20 69 73 whose status is
7cd80 20 64 65 73 69 72 65 64 20 2a 2f 0a 20 20 69 6e desired */. in
7cd90 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 t op,
7cda0 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 76 65 /* Status ve
7cdb0 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 43 75 rb */. int *pCu
7cdc0 72 72 65 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a rrent, /*
7cdd0 20 57 72 69 74 65 20 63 75 72 72 65 6e 74 20 76 Write current v
7cde0 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 alue here */. i
7cdf0 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20 nt *pHighwater,
7ce00 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 68 69 /* Write hi
7ce10 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 68 65 gh-water mark he
7ce20 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 65 re */. int rese
7ce30 74 46 6c 61 67 20 20 20 20 20 20 20 20 20 2f 2a tFlag /*
7ce40 20 52 65 73 65 74 20 68 69 67 68 2d 77 61 74 65 Reset high-wate
7ce50 72 20 6d 61 72 6b 20 69 66 20 74 72 75 65 20 2a r mark if true *
7ce60 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f /.){. switch( o
7ce70 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 p ){. case SQ
7ce80 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f LITE_DBSTATUS_LO
7ce90 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a 20 7b 0a OKASIDE_USED: {.
7cea0 20 20 20 20 20 20 2a 70 43 75 72 72 65 6e 74 20 *pCurrent
7ceb0 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e = db->lookaside.
7cec0 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a 70 48 69 nOut;. *pHi
7ced0 67 68 77 61 74 65 72 20 3d 20 64 62 2d 3e 6c 6f ghwater = db->lo
7cee0 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 3b 0a 20 okaside.mxOut;.
7cef0 20 20 20 20 20 69 66 28 20 72 65 73 65 74 46 6c if( resetFl
7cf00 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 ag ){. db
7cf10 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 ->lookaside.mxOu
7cf20 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 t = db->lookasid
7cf30 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a e.nOut;. }.
7cf40 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7cf50 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
7cf60 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
7cf70 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
7cf80 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
7cf90 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
7cfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
7cfb0 64 20 6f 66 20 73 74 61 74 75 73 2e 63 20 2a 2a d of status.c **
7cfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7cfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7cfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
7cff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
7d000 67 69 6e 20 66 69 6c 65 20 64 61 74 65 2e 63 20 gin file date.c
7d010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
7d040 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f 62 65 72 .** 2003 October
7d050 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 31.**.** The au
7d060 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
7d070 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
7d080 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
7d090 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
7d0a0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
7d0b0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
7d0c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
7d0d0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
7d0e0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
7d0f0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
7d100 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
7d110 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
7d120 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
7d130 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
7d140 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
7d150 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
7d160 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
7d170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7d1b0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
7d1c0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 contains the C f
7d1d0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d unctions that im
7d1e0 70 6c 65 6d 65 6e 74 20 64 61 74 65 20 61 6e 64 plement date and
7d1f0 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f time.** functio
7d200 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 20 20 ns for SQLite.
7d210 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 .**.** There is
7d220 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74 65 only one exporte
7d230 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73 d symbol in this
7d240 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e 63 file - the func
7d250 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 52 tion.** sqlite3R
7d260 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 egisterDateTimeF
7d270 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64 unctions() found
7d280 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f at the bottom o
7d290 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 f the file..** A
7d2a0 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61 ll other code ha
7d2b0 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a s file scope..**
7d2c0 0a 2a 2a 20 24 49 64 3a 20 64 61 74 65 2e 63 2c .** $Id: date.c,
7d2d0 76 20 31 2e 39 34 20 32 30 30 38 2f 31 31 2f 31 v 1.94 2008/11/1
7d2e0 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65 9 09:05:27 danie
7d2f0 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a 0a lk1977 Exp $.**.
7d300 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73 ** SQLite proces
7d310 73 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e ses all times an
7d320 64 20 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61 d dates as Julia
7d330 6e 20 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 n Day numbers.
7d340 54 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 The.** dates and
7d350 20 74 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65 times are store
7d360 64 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 d as the number
7d370 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f of days since no
7d380 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69 on.** in Greenwi
7d390 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 ch on November 2
7d3a0 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63 4, 4714 B.C. acc
7d3b0 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72 ording to the Gr
7d3c0 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e egorian.** calen
7d3d0 64 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a dar system. .**.
7d3e0 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 30 30 ** 1970-01-01 00
7d3f0 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 34 :00:00 is JD 244
7d400 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 0587.5.** 2000-0
7d410 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 1-01 00:00:00 is
7d420 20 4a 44 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a JD 2451544.5.**
7d430 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 .** This impleme
7d440 6e 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 79 ntion requires y
7d450 65 61 72 73 20 74 6f 20 62 65 20 65 78 70 72 65 ears to be expre
7d460 73 73 65 64 20 61 73 20 61 20 34 2d 64 69 67 69 ssed as a 4-digi
7d470 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63 t number.** whic
7d480 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c h means that onl
7d490 79 20 64 61 74 65 73 20 62 65 74 77 65 65 6e 20 y dates between
7d4a0 30 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 20 39 0000-01-01 and 9
7d4b0 39 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 999-12-31 can.**
7d4c0 20 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 2c be represented,
7d4d0 20 65 76 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c even though jul
7d4e0 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 73 20 ian day numbers
7d4f0 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 69 64 allow a much wid
7d500 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 er.** range of d
7d510 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ates..**.** The
7d520 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 Gregorian calend
7d530 61 72 20 73 79 73 74 65 6d 20 69 73 20 75 73 65 ar system is use
7d540 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 73 20 d for all dates
7d550 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 and times,.** ev
7d560 65 6e 20 74 68 6f 73 65 20 74 68 61 74 20 70 72 en those that pr
7d570 65 64 61 74 65 20 74 68 65 20 47 72 65 67 6f 72 edate the Gregor
7d580 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20 48 ian calendar. H
7d590 69 73 74 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c istorians usuall
7d5a0 79 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a 75 6c y.** use the Jul
7d5b0 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 ian calendar for
7d5c0 20 64 61 74 65 73 20 70 72 69 6f 72 20 74 6f 20 dates prior to
7d5d0 31 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 1582-10-15 and f
7d5e0 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 or some.** dates
7d5f0 20 61 66 74 65 72 77 61 72 64 73 2c 20 64 65 70 afterwards, dep
7d600 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 ending on locale
7d610 2e 20 20 42 65 77 61 72 65 20 6f 66 20 74 68 69 . Beware of thi
7d620 73 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a s difference..**
7d630 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69 .** The conversi
7d640 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 on algorithms ar
7d650 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61 e implemented ba
7d660 73 65 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69 sed on descripti
7d670 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f ons.** in the fo
7d680 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a llowing text:.**
7d690 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d 65 .** Jean Me
7d6a0 65 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 74 72 eus.** Astr
7d6b0 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74 onomical Algorit
7d6c0 68 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e hms, 2nd Edition
7d6d0 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 20 49 , 1998.** I
7d6e0 53 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 31 2d SBM 0-943396-61-
7d6f0 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61 1.** Willma
7d700 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 nn-Bell, Inc.**
7d710 20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56 Richmond, V
7d720 69 72 67 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f irginia (USA).*/
7d730 0a 23 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 65 .#include <ctype
7d740 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 .h>.#include <ti
7d750 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53 me.h>..#ifndef S
7d760 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 QLITE_OMIT_DATET
7d770 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a IME_FUNCS../*.**
7d780 20 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 6f On recent Windo
7d790 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 68 ws platforms, th
7d7a0 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 20 e localtime_s()
7d7b0 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 69 function is avai
7d7c0 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 74 lable.** as part
7d7d0 20 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 20 of the "Secure
7d7e0 43 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 65 CRT". It is esse
7d7f0 6e 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c 65 ntially equivale
7d800 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 nt to .** localt
7d810 69 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 6c ime_r() availabl
7d820 65 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f 53 e under most POS
7d830 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 78 IX platforms, ex
7d840 63 65 70 74 20 74 68 61 74 20 74 68 65 20 0a 2a cept that the .*
7d850 2a 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 70 * order of the p
7d860 61 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 arameters is rev
7d870 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 ersed..**.** See
7d880 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 http://msdn.mic
7d890 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 rosoft.com/en-us
7d8a0 2f 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 79 /library/a442x3y
7d8b0 65 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a 2a e(VS.80).aspx..*
7d8c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 72 *.** If the user
7d8d0 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 74 has not indicat
7d8e0 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c 74 ed to use localt
7d8f0 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 6c ime_r() or local
7d900 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 time_s().** alre
7d910 61 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 ady, check for a
7d920 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e 76 n MSVC build env
7d930 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 72 ironment that pr
7d940 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c ovides .** local
7d950 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 time_s()..*/.#if
7d960 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c !defined(HAVE_L
7d970 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 21 OCALTIME_R) && !
7d980 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 defined(HAVE_LOC
7d990 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a 20 ALTIME_S) && \.
7d9a0 20 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 defined(_MSC
7d9b0 5f 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 64 _VER) && defined
7d9c0 28 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f 44 (_CRT_INSECURE_D
7d9d0 45 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 6e EPRECATE).#defin
7d9e0 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 e HAVE_LOCALTIME
7d9f0 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a _S 1.#endif../*.
7da00 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 66 ** A structure f
7da10 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e or holding a sin
7da20 67 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d gle date and tim
7da30 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 e..*/.typedef st
7da40 72 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 61 ruct DateTime Da
7da50 74 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 44 teTime;.struct D
7da60 61 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69 ateTime {. sqli
7da70 74 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f te3_int64 iJD; /
7da80 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 * The julian day
7da90 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 36 number times 86
7daa0 34 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20 400000 */. int
7dab0 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f Y, M, D; /
7dac0 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 * Year, month, a
7dad0 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20 nd day */. int
7dae0 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 2f h, m; /
7daf0 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74 * Hour and minut
7db00 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 es */. int tz;
7db10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 /* Ti
7db20 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e mezone offset in
7db30 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f minutes */. do
7db40 75 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 20 uble s;
7db50 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 /* Seconds */.
7db60 20 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b 20 char validYMD;
7db70 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 59 /* True if Y
7db80 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a ,M,D are valid *
7db90 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d /. char validHM
7dba0 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 S; /* True i
7dbb0 66 20 68 2c 6d 2c 73 20 61 72 65 20 76 61 6c 69 f h,m,s are vali
7dbc0 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 d */. char vali
7dbd0 64 4a 44 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 dJD; /* Tru
7dbe0 65 20 69 66 20 69 4a 44 20 69 73 20 76 61 6c 69 e if iJD is vali
7dbf0 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 d */. char vali
7dc00 64 54 5a 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 dTZ; /* Tru
7dc10 65 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 64 e if tz is valid
7dc20 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 */.};.../*.** C
7dc30 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e 74 onvert zDate int
7dc40 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e o one or more in
7dc50 74 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 6f tegers. Additio
7dc60 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a nal arguments.**
7dc70 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 20 come in groups
7dc80 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a of 5 as follows:
7dc90 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20 .**.** N
7dca0 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 64 number of d
7dcb0 69 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e 74 igits in the int
7dcc0 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69 eger.** mi
7dcd0 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c n minimum al
7dce0 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74 lowed value of t
7dcf0 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 he integer.**
7dd00 20 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 69 max maxi
7dd10 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 mum allowed valu
7dd20 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 e of the integer
7dd30 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 20 .** nextC
7dd40 20 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 first characte
7dd50 72 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 65 r after the inte
7dd60 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 61 ger.** pVa
7dd70 6c 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 72 l where to wr
7dd80 69 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 73 ite the integers
7dd90 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f value..**.** Co
7dda0 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e nversions contin
7ddb0 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74 ue until one wit
7ddc0 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 6e h nextC==0 is en
7ddd0 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68 countered..** Th
7dde0 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 e function retur
7ddf0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
7de00 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e 76 successful conv
7de10 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 ersions..*/.stat
7de20 69 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 73 ic int getDigits
7de30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 (const char *zDa
7de40 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c te, ...){. va_l
7de50 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 61 ist ap;. int va
7de60 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e l;. int N;. in
7de70 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 t min;. int max
7de80 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20 ;. int nextC;.
7de90 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e int *pVal;. in
7dea0 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f t cnt = 0;. va_
7deb0 73 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 29 start(ap, zDate)
7dec0 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 ;. do{. N =
7ded0 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b va_arg(ap, int);
7dee0 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72 . min = va_ar
7def0 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 g(ap, int);.
7df00 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c max = va_arg(ap,
7df10 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 43 int);. nextC
7df20 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e = va_arg(ap, in
7df30 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 t);. pVal = v
7df40 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b a_arg(ap, int*);
7df50 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20 . val = 0;.
7df60 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a while( N-- ){.
7df70 20 20 20 20 20 20 69 66 28 20 21 69 73 64 69 67 if( !isdig
7df80 69 74 28 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 it(*(u8*)zDate)
7df90 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
7dfa0 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 end_getDigits;.
7dfb0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76 61 6c }. val
7dfc0 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 = val*10 + *zDa
7dfd0 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 te - '0';.
7dfe0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 zDate++;. }.
7dff0 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c if( val<min |
7e000 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 | val>max || (ne
7e010 78 74 43 21 3d 30 20 26 26 20 6e 65 78 74 43 21 xtC!=0 && nextC!
7e020 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 =*zDate) ){.
7e030 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 goto end_getDi
7e040 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 gits;. }.
7e050 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 *pVal = val;.
7e060 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e zDate++;. cn
7e070 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e t++;. }while( n
7e080 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 extC );.end_getD
7e090 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 igits:. va_end(
7e0a0 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e ap);. return cn
7e0b0 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 t;.}../*.** Read
7e0c0 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 61 text from z[] a
7e0d0 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f 20 nd convert into
7e0e0 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 a floating point
7e0f0 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e number. Return
7e100 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
7e110 66 20 64 69 67 69 74 73 20 63 6f 6e 76 65 72 74 f digits convert
7e120 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 ed..*/.#define g
7e130 65 74 56 61 6c 75 65 20 73 71 6c 69 74 65 33 41 etValue sqlite3A
7e140 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 toF../*.** Parse
7e150 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65 a timezone exte
7e160 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64 nsion on the end
7e170 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d 65 2e of a date-time.
7e180 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73 69 6f .** The extensio
7e190 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d n is of the form
7e1a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28 :.**.** (
7e1b0 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 +/-)HH:MM.**.**
7e1c0 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f Or the "zulu" no
7e1d0 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 tation:.**.**
7e1e0 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 Z.**.** If
7e1f0 74 68 65 20 70 61 72 73 65 20 69 73 20 73 75 63 the parse is suc
7e200 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74 cessful, write t
7e210 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e he number of min
7e220 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 utes.** of chang
7e230 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72 e in p->tz and r
7e240 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70 eturn 0. If a p
7e250 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75 arser error occu
7e260 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f rs,.** return no
7e270 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 n-zero..**.** A
7e280 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66 69 65 missing specifie
7e290 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 r is not conside
7e2a0 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f red an error..*/
7e2b0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 .static int pars
7e2c0 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 eTimezone(const
7e2d0 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 char *zDate, Dat
7e2e0 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 eTime *p){. int
7e2f0 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 sgn = 0;. int
7e300 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20 nHr, nMn;. int
7e310 63 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 c;. while( issp
7e320 61 63 65 28 2a 28 75 38 2a 29 7a 44 61 74 65 29 ace(*(u8*)zDate)
7e330 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 ){ zDate++; }.
7e340 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20 p->tz = 0;. c
7e350 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66 28 20 = *zDate;. if(
7e360 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 67 c=='-' ){. sg
7e370 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 n = -1;. }else
7e380 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20 if( c=='+' ){.
7e390 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65 sgn = +1;. }e
7e3a0 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c lse if( c=='Z' |
7e3b0 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20 | c=='z' ){.
7e3c0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74 zDate++;. got
7e3d0 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d o zulu_time;. }
7e3e0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
7e3f0 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61 c!=0;. }. zDa
7e400 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 74 44 te++;. if( getD
7e410 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 igits(zDate, 2,
7e420 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72 0, 14, ':', &nHr
7e430 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 , 2, 0, 59, 0, &
7e440 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 nMn)!=2 ){. r
7e450 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a eturn 1;. }. z
7e460 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e Date += 5;. p->
7e470 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 tz = sgn*(nMn +
7e480 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69 nHr*60);.zulu_ti
7e490 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 69 73 73 me:. while( iss
7e4a0 70 61 63 65 28 2a 28 75 38 2a 29 7a 44 61 74 65 pace(*(u8*)zDate
7e4b0 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a ) ){ zDate++; }.
7e4c0 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74 65 21 return *zDate!
7e4d0 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 =0;.}../*.** Par
7e4e0 73 65 20 74 69 6d 65 73 20 6f 66 20 74 68 65 20 se times of the
7e4f0 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20 48 48 form HH:MM or HH
7e500 3a 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d 4d 3a :MM:SS or HH:MM:
7e510 53 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68 65 20 SS.FFFF..** The
7e520 48 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53 20 6d HH, MM, and SS m
7e530 75 73 74 20 65 61 63 68 20 62 65 20 65 78 61 63 ust each be exac
7e540 74 6c 79 20 32 20 64 69 67 69 74 73 2e 20 20 54 tly 2 digits. T
7e550 68 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c he.** fractional
7e560 20 73 65 63 6f 6e 64 73 20 46 46 46 46 20 63 61 seconds FFFF ca
7e570 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 n be one or more
7e580 20 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a 20 52 digits..**.** R
7e590 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65 eturn 1 if there
7e5a0 20 69 73 20 61 20 70 61 72 73 69 6e 67 20 65 72 is a parsing er
7e5b0 72 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73 75 63 ror and 0 on suc
7e5c0 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 cess..*/.static
7e5d0 69 6e 74 20 70 61 72 73 65 48 68 4d 6d 53 73 28 int parseHhMmSs(
7e5e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 const char *zDat
7e5f0 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b e, DateTime *p){
7e600 0a 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73 3b 0a . int h, m, s;.
7e610 20 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20 30 2e double ms = 0.
7e620 30 3b 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 0;. if( getDigi
7e630 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 ts(zDate, 2, 0,
7e640 32 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32 2c 20 24, ':', &h, 2,
7e650 30 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21 3d 32 0, 59, 0, &m)!=2
7e660 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ){. return 1
7e670 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d ;. }. zDate +=
7e680 20 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61 74 65 5;. if( *zDate
7e690 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a 44 61 ==':' ){. zDa
7e6a0 74 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 67 65 te++;. if( ge
7e6b0 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 tDigits(zDate, 2
7e6c0 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 73 29 21 , 0, 59, 0, &s)!
7e6d0 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 =1 ){. retu
7e6e0 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
7e6f0 7a 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20 20 20 zDate += 2;.
7e700 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e 27 20 if( *zDate=='.'
7e710 26 26 20 69 73 64 69 67 69 74 28 28 75 38 29 7a && isdigit((u8)z
7e720 44 61 74 65 5b 31 5d 29 20 29 7b 0a 20 20 20 20 Date[1]) ){.
7e730 20 20 64 6f 75 62 6c 65 20 72 53 63 61 6c 65 20 double rScale
7e740 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 7a 44 61 = 1.0;. zDa
7e750 74 65 2b 2b 3b 0a 20 20 20 20 20 20 77 68 69 6c te++;. whil
7e760 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a e( isdigit(*(u8*
7e770 29 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 )zDate) ){.
7e780 20 20 20 6d 73 20 3d 20 6d 73 2a 31 30 2e 30 20 ms = ms*10.0
7e790 2b 20 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a + *zDate - '0';.
7e7a0 20 20 20 20 20 20 20 20 72 53 63 61 6c 65 20 2a rScale *
7e7b0 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 = 10.0;.
7e7c0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20 7d zDate++;. }
7e7d0 0a 20 20 20 20 20 20 6d 73 20 2f 3d 20 72 53 63 . ms /= rSc
7e7e0 61 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c ale;. }. }el
7e7f0 73 65 7b 0a 20 20 20 20 73 20 3d 20 30 3b 0a 20 se{. s = 0;.
7e800 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 }. p->validJD
7e810 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 = 0;. p->validH
7e820 4d 53 20 3d 20 31 3b 0a 20 20 70 2d 3e 68 20 3d MS = 1;. p->h =
7e830 20 68 3b 0a 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a h;. p->m = m;.
7e840 20 20 70 2d 3e 73 20 3d 20 73 20 2b 20 6d 73 3b p->s = s + ms;
7e850 0a 20 20 69 66 28 20 70 61 72 73 65 54 69 6d 65 . if( parseTime
7e860 7a 6f 6e 65 28 7a 44 61 74 65 2c 20 70 29 20 29 zone(zDate, p) )
7e870 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 70 2d 3e return 1;. p->
7e880 76 61 6c 69 64 54 5a 20 3d 20 70 2d 3e 74 7a 21 validTZ = p->tz!
7e890 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a =0;. return 0;.
7e8a0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
7e8b0 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 from YYYY-MM-DD
7e8c0 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c HH:MM:SS to jul
7e8d0 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c 77 ian day. We alw
7e8e0 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 ays assume.** th
7e8f0 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 at the YYYY-MM-D
7e900 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 74 D is according t
7e910 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 o the Gregorian
7e920 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 calendar..**.**
7e930 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 Reference: Meeu
7e940 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 s page 61.*/.sta
7e950 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 tic void compute
7e960 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b JD(DateTime *p){
7e970 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 . int Y, M, D,
7e980 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 A, B, X1, X2;..
7e990 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 if( p->validJD
7e9a0 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
7e9b0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 p->validYMD ){.
7e9c0 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 Y = p->Y;.
7e9d0 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 M = p->M;. D
7e9e0 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 = p->D;. }else
7e9f0 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b 20 {. Y = 2000;
7ea00 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 /* If no YMD sp
7ea10 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 20 ecified, assume
7ea20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 2000-Jan-01 */.
7ea30 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 M = 1;. D
7ea40 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d = 1;. }. if( M
7ea50 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a <=2 ){. Y--;.
7ea60 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d M += 12;. }
7ea70 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 . A = Y/100;.
7ea80 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 B = 2 - A + (A/4
7ea90 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 2e 32 35 );. X1 = 365.25
7eaa0 2a 28 59 2b 34 37 31 36 29 3b 0a 20 20 58 32 20 *(Y+4716);. X2
7eab0 3d 20 33 30 2e 36 30 30 31 2a 28 4d 2b 31 29 3b = 30.6001*(M+1);
7eac0 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 58 31 20 . p->iJD = (X1
7ead0 2b 20 58 32 20 2b 20 44 20 2b 20 42 20 2d 20 31 + X2 + D + B - 1
7eae0 35 32 34 2e 35 29 2a 38 36 34 30 30 30 30 30 3b 524.5)*86400000;
7eaf0 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 . p->validJD =
7eb00 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 1;. if( p->vali
7eb10 64 48 4d 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 dHMS ){. p->i
7eb20 4a 44 20 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 JD += p->h*36000
7eb30 30 30 20 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 00 + p->m*60000
7eb40 2b 20 70 2d 3e 73 2a 31 30 30 30 3b 0a 20 20 20 + p->s*1000;.
7eb50 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 if( p->validTZ
7eb60 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 ){. p->iJD
7eb70 2d 3d 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a -= p->tz*60000;.
7eb80 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d p->validYM
7eb90 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e D = 0;. p->
7eba0 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 validHMS = 0;.
7ebb0 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d p->validTZ =
7ebc0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0;. }. }.}.
7ebd0 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 ./*.** Parse dat
7ebe0 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a es of the form.*
7ebf0 2a 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d *.** YYYY-MM
7ec00 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 -DD HH:MM:SS.FFF
7ec10 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d .** YYYY-MM-
7ec20 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 DD HH:MM:SS.**
7ec30 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 YYYY-MM-DD HH
7ec40 3a 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d :MM.** YYYY-
7ec50 4d 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 MM-DD.**.** Writ
7ec60 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 e the result int
7ec70 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 o the DateTime s
7ec80 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 tructure and ret
7ec90 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 urn 0.** on succ
7eca0 65 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 ess and 1 if the
7ecb0 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73 input string is
7ecc0 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d not a well-form
7ecd0 65 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 ed.** date..*/.s
7ece0 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 59 tatic int parseY
7ecf0 79 79 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 yyyMmDd(const ch
7ed00 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 ar *zDate, DateT
7ed10 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 ime *p){. int Y
7ed20 2c 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 , M, D, neg;..
7ed30 69 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d if( zDate[0]=='-
7ed40 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b ' ){. zDate++
7ed50 3b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 ;. neg = 1;.
7ed60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 }else{. neg
7ed70 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 = 0;. }. if( g
7ed80 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34 etDigits(zDate,4
7ed90 2c 30 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 ,0,9999,'-',&Y,2
7eda0 2c 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 ,1,12,'-',&M,2,1
7edb0 2c 33 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a ,31,0,&D)!=3 ){.
7edc0 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
7edd0 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b }. zDate += 10;
7ede0 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 . while( isspac
7edf0 65 28 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 7c e(*(u8*)zDate) |
7ee00 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 | 'T'==*(u8*)zDa
7ee10 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d te ){ zDate++; }
7ee20 0a 20 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d . if( parseHhMm
7ee30 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 Ss(zDate, p)==0
7ee40 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 ){. /* We got
7ee50 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d the time */. }
7ee60 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d else if( *zDate=
7ee70 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c =0 ){. p->val
7ee80 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c idHMS = 0;. }el
7ee90 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 se{. return 1
7eea0 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 ;. }. p->valid
7eeb0 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c JD = 0;. p->val
7eec0 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e idYMD = 1;. p->
7eed0 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 Y = neg ? -Y : Y
7eee0 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 ;. p->M = M;.
7eef0 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 p->D = D;. if(
7ef00 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 p->validTZ ){.
7ef10 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
7ef20 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
7ef30 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
7ef40 20 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 time to the cur
7ef50 72 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 rent time report
7ef60 65 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f ed by the VFS.*/
7ef70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 .static void set
7ef80 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e DateTimeToCurren
7ef90 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 t(sqlite3_contex
7efa0 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65 t *context, Date
7efb0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62 Time *p){. doub
7efc0 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 le r;. sqlite3
7efd0 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f *db = sqlite3_co
7efe0 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
7eff0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 context);. sqli
7f000 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
7f010 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a (db->pVfs, &r);.
7f020 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 p->iJD = (sqli
7f030 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 te3_int64)(r*864
7f040 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 00000.0 + 0.5);.
7f050 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 p->validJD = 1
7f060 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d ;.}../*.** Attem
7f070 70 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 20 pt to parse the
7f080 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74 given string int
7f090 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e o a Julian Day N
7f0a0 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a umber. Return.*
7f0b0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
7f0c0 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 errors..**.** Th
7f0d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
7f0e0 61 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 acceptable forms
7f0f0 20 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 for the input s
7f100 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 tring:.**.**
7f110 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a YYYY-MM-DD HH:
7f120 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48 MM:SS.FFF +/-HH
7f130 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44 :MM.** DDDD
7f140 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 .DD .** now
7f150 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69 .**.** In the fi
7f160 72 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f rst form, the +/
7f170 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73 -HH:MM is always
7f180 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 optional. The
7f190 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 fractional.** se
7f1a0 63 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20 conds extension
7f1b0 28 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 20 (the ".FFF") is
7f1c0 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73 optional. The s
7f1d0 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a econds portion.*
7f1e0 2a 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73 * (":SS.FFF") is
7f1f0 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65 option. The ye
7f200 61 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20 ar and date can
7f210 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f be omitted as lo
7f220 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 ng.** as there i
7f230 73 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e s a time string.
7f240 20 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e The time strin
7f250 67 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 g can be omitted
7f260 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 as long.** as t
7f270 68 65 72 65 20 69 73 20 61 20 79 65 61 72 20 61 here is a year a
7f280 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 nd date..*/.stat
7f290 69 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 65 ic int parseDate
7f2a0 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65 OrTime(. sqlite
7f2b0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
7f2c0 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 xt, . const cha
7f2d0 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74 r *zDate, . Dat
7f2e0 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 66 eTime *p.){. if
7f2f0 28 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 ( parseYyyyMmDd(
7f300 7a 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 zDate,p)==0 ){.
7f310 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
7f320 65 6c 73 65 20 69 66 28 20 70 61 72 73 65 48 68 else if( parseHh
7f330 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d MmSs(zDate, p)==
7f340 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
7f350 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 0;. }else if( s
7f360 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 qlite3StrICmp(zD
7f370 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a ate,"now")==0){.
7f380 20 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 setDateTimeT
7f390 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 oCurrent(context
7f3a0 2c 20 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e , p);. return
7f3b0 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
7f3c0 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 sqlite3IsNumber(
7f3d0 7a 44 61 74 65 2c 20 30 2c 20 53 51 4c 49 54 45 zDate, 0, SQLITE
7f3e0 5f 55 54 46 38 29 20 29 7b 0a 20 20 20 20 64 6f _UTF8) ){. do
7f3f0 75 62 6c 65 20 72 3b 0a 20 20 20 20 67 65 74 56 uble r;. getV
7f400 61 6c 75 65 28 7a 44 61 74 65 2c 20 26 72 29 3b alue(zDate, &r);
7f410 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 . p->iJD = (s
7f420 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a qlite3_int64)(r*
7f430 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 86400000.0 + 0.5
7f440 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a );. p->validJ
7f450 44 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 D = 1;. retur
7f460 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n 0;. }. retur
7f470 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f n 1;.}../*.** Co
7f480 6d 70 75 74 65 20 74 68 65 20 59 65 61 72 2c 20 mpute the Year,
7f490 4d 6f 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66 Month, and Day f
7f4a0 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 rom the julian d
7f4b0 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 ay number..*/.st
7f4c0 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 atic void comput
7f4d0 65 59 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70 eYMD(DateTime *p
7f4e0 29 7b 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 ){. int Z, A, B
7f4f0 2c 20 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 , C, D, E, X1;.
7f500 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 if( p->validYMD
7f510 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
7f520 20 21 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a !p->validJD ){.
7f530 20 20 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b p->Y = 2000;
7f540 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 . p->M = 1;.
7f550 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d p->D = 1;. }
7f560 65 6c 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 70 else{. Z = (p
7f570 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30 ->iJD + 43200000
7f580 29 2f 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20 )/86400000;.
7f590 41 20 3d 20 28 5a 20 2d 20 31 38 36 37 32 31 36 A = (Z - 1867216
7f5a0 2e 32 35 29 2f 33 36 35 32 34 2e 32 35 3b 0a 20 .25)/36524.25;.
7f5b0 20 20 20 41 20 3d 20 5a 20 2b 20 31 20 2b 20 41 A = Z + 1 + A
7f5c0 20 2d 20 28 41 2f 34 29 3b 0a 20 20 20 20 42 20 - (A/4);. B
7f5d0 3d 20 41 20 2b 20 31 35 32 34 3b 0a 20 20 20 20 = A + 1524;.
7f5e0 43 20 3d 20 28 42 20 2d 20 31 32 32 2e 31 29 2f C = (B - 122.1)/
7f5f0 33 36 35 2e 32 35 3b 0a 20 20 20 20 44 20 3d 20 365.25;. D =
7f600 33 36 35 2e 32 35 2a 43 3b 0a 20 20 20 20 45 20 365.25*C;. E
7f610 3d 20 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 3b = (B-D)/30.6001;
7f620 0a 20 20 20 20 58 31 20 3d 20 33 30 2e 36 30 30 . X1 = 30.600
7f630 31 2a 45 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 1*E;. p->D =
7f640 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20 20 20 20 B - D - X1;.
7f650 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d p->M = E<14 ? E-
7f660 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d 1 : E-13;. p-
7f670 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20 >Y = p->M>2 ? C
7f680 2d 20 34 37 31 36 20 3a 20 43 20 2d 20 34 37 31 - 4716 : C - 471
7f690 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 5;. }. p->vali
7f6a0 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a dYMD = 1;.}../*.
7f6b0 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 48 ** Compute the H
7f6c0 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64 our, Minute, and
7f6d0 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68 Seconds from th
7f6e0 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d e julian day num
7f6f0 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ber..*/.static v
7f700 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d 53 28 44 oid computeHMS(D
7f710 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 ateTime *p){. i
7f720 6e 74 20 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76 nt s;. if( p->v
7f730 61 6c 69 64 48 4d 53 20 29 20 72 65 74 75 72 6e alidHMS ) return
7f740 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 ;. computeJD(p)
7f750 3b 0a 20 20 73 20 3d 20 28 70 2d 3e 69 4a 44 20 ;. s = (p->iJD
7f760 2b 20 34 33 32 30 30 30 30 30 29 20 25 20 38 36 + 43200000) % 86
7f770 34 30 30 30 30 30 3b 0a 20 20 70 2d 3e 73 20 3d 400000;. p->s =
7f780 20 73 2f 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d s/1000.0;. s =
7f790 20 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d p->s;. p->s -=
7f7a0 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f 33 s;. p->h = s/3
7f7b0 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 600;. s -= p->h
7f7c0 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 *3600;. p->m =
7f7d0 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 s/60;. p->s +=
7f7e0 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 s - p->m*60;. p
7f7f0 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a ->validHMS = 1;.
7f800 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 }../*.** Compute
7f810 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48 4d both YMD and HM
7f820 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 S.*/.static void
7f830 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 computeYMD_HMS(
7f840 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 DateTime *p){.
7f850 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 computeYMD(p);.
7f860 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b 0a computeHMS(p);.
7f870 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 }../*.** Clear t
7f880 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20 61 he YMD and HMS a
7f890 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 nd the TZ.*/.sta
7f8a0 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59 4d tic void clearYM
7f8b0 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69 6d D_HMS_TZ(DateTim
7f8c0 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 e *p){. p->vali
7f8d0 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 dYMD = 0;. p->v
7f8e0 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 70 alidHMS = 0;. p
7f8f0 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d ->validTZ = 0;.}
7f900 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
7f910 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a _OMIT_LOCALTIME.
7f920 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 /*.** Compute th
7f930 65 20 64 69 66 66 65 72 65 6e 63 65 20 28 69 6e e difference (in
7f940 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a 2a milliseconds).*
7f950 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 74 * between localt
7f960 69 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e 6b ime and UTC (a.k
7f970 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 .a. GMT).** for
7f980 74 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20 70 the time value p
7f990 20 77 68 65 72 65 20 70 20 69 73 20 69 6e 20 55 where p is in U
7f9a0 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e TC..*/.static in
7f9b0 74 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 t localtimeOffse
7f9c0 74 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a t(DateTime *p){.
7f9d0 20 20 44 61 74 65 54 69 6d 65 20 78 2c 20 79 3b DateTime x, y;
7f9e0 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 78 . time_t t;. x
7f9f0 20 3d 20 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65 = *p;. compute
7fa00 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69 YMD_HMS(&x);. i
7fa10 66 28 20 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 f( x.Y<1971 || x
7fa20 2e 59 3e 3d 32 30 33 38 20 29 7b 0a 20 20 20 20 .Y>=2038 ){.
7fa30 78 2e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 x.Y = 2000;.
7fa40 78 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44 x.M = 1;. x.D
7fa50 20 3d 20 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20 = 1;. x.h =
7fa60 30 3b 0a 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 0;. x.m = 0;.
7fa70 20 20 20 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 x.s = 0.0;.
7fa80 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e } else {. in
7fa90 74 20 73 20 3d 20 78 2e 73 20 2b 20 30 2e 35 3b t s = x.s + 0.5;
7faa0 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20 . x.s = s;.
7fab0 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20 }. x.tz = 0;.
7fac0 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 x.validJD = 0;.
7fad0 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a computeJD(&x);.
7fae0 20 20 74 20 3d 20 78 2e 69 4a 44 2f 31 30 30 30 t = x.iJD/1000
7faf0 20 2d 20 32 34 34 30 35 38 37 2e 35 2a 38 36 34 - 2440587.5*864
7fb00 30 30 2e 30 3b 0a 23 69 66 64 65 66 20 48 41 56 00.0;.#ifdef HAV
7fb10 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20 E_LOCALTIME_R.
7fb20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 {. struct tm
7fb30 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 sLocal;. loca
7fb40 6c 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 4c 6f ltime_r(&t, &sLo
7fb50 63 61 6c 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 cal);. y.Y =
7fb60 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b sLocal.tm_year +
7fb70 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 1900;. y.M =
7fb80 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b sLocal.tm_mon +
7fb90 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 1;. y.D = sL
7fba0 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 ocal.tm_mday;.
7fbb0 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.h = sLocal.t
7fbc0 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 m_hour;. y.m
7fbd0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b = sLocal.tm_min;
7fbe0 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 . y.s = sLoca
7fbf0 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 l.tm_sec;. }.#e
7fc00 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 lif defined(HAVE
7fc10 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 0a 20 20 _LOCALTIME_S).
7fc20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 {. struct tm
7fc30 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 sLocal;. loca
7fc40 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c ltime_s(&sLocal,
7fc50 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 &t);. y.Y =
7fc60 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b sLocal.tm_year +
7fc70 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 1900;. y.M =
7fc80 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b sLocal.tm_mon +
7fc90 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 1;. y.D = sL
7fca0 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 ocal.tm_mday;.
7fcb0 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.h = sLocal.t
7fcc0 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 m_hour;. y.m
7fcd0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b = sLocal.tm_min;
7fce0 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 . y.s = sLoca
7fcf0 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 l.tm_sec;. }.#e
7fd00 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 lse. {. stru
7fd10 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 ct tm *pTm;.
7fd20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
7fd30 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ter(sqlite3Mutex
7fd40 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
7fd50 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
7fd60 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f ));. pTm = lo
7fd70 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 caltime(&t);.
7fd80 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79 y.Y = pTm->tm_y
7fd90 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 ear + 1900;.
7fda0 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f y.M = pTm->tm_mo
7fdb0 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d n + 1;. y.D =
7fdc0 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20 pTm->tm_mday;.
7fdd0 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d y.h = pTm->tm
7fde0 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d _hour;. y.m =
7fdf0 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 pTm->tm_min;.
7fe00 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f y.s = pTm->tm_
7fe10 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 sec;. sqlite3
7fe20 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c _mutex_leave(sql
7fe30 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
7fe40 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
7fe50 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d IC_MASTER));. }
7fe60 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69 .#endif. y.vali
7fe70 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61 dYMD = 1;. y.va
7fe80 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e lidHMS = 1;. y.
7fe90 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79 validJD = 0;. y
7fea0 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 .validTZ = 0;.
7feb0 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 computeJD(&y);.
7fec0 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20 return y.iJD -
7fed0 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 x.iJD;.}.#endif
7fee0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c /* SQLITE_OMIT_L
7fef0 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a OCALTIME */../*.
7ff00 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d 6f 64 ** Process a mod
7ff10 69 66 69 65 72 20 74 6f 20 61 20 64 61 74 65 2d ifier to a date-
7ff20 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54 68 65 time stamp. The
7ff30 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65 0a 2a modifiers are.*
7ff40 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a * as follows:.**
7ff50 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 .** NNN days
7ff60 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 .** NNN hour
7ff70 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e s.** NNN min
7ff80 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e utes.** NNN.
7ff90 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20 NNNN seconds.**
7ffa0 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a NNN months.*
7ffb0 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a * NNN years.
7ffc0 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 ** start of
7ffd0 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73 74 61 month.** sta
7ffe0 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20 rt of year.**
7fff0 20 20 73 74 61 72 74 20 6f 66 20 77 65 65 6b 0a start of week.
80000 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 ** start of
80010 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64 day.** weekd
80020 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78 ay N.** unix
80030 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63 epoch.** loc
80040 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74 altime.** ut
80050 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 c.**.** Return 0
80060 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 on success and
80070 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 1 if there is an
80080 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e y kind of error.
80090 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
800a0 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e arseModifier(con
800b0 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44 st char *zMod, D
800c0 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 ateTime *p){. i
800d0 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74 nt rc = 1;. int
800e0 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a n;. double r;.
800f0 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b char *z, zBuf[
80100 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b 30];. z = zBuf;
80110 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72 . for(n=0; n<Ar
80120 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d 31 20 raySize(zBuf)-1
80130 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29 && zMod[n]; n++)
80140 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 74 6f 6c {. z[n] = tol
80150 6f 77 65 72 28 7a 4d 6f 64 5b 6e 5d 29 3b 0a 20 ower(zMod[n]);.
80160 20 7d 0a 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 }. z[n] = 0;.
80170 20 73 77 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b switch( z[0] ){
80180 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
80190 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 OMIT_LOCALTIME.
801a0 20 20 20 63 61 73 65 20 27 6c 27 3a 20 7b 0a 20 case 'l': {.
801b0 20 20 20 20 20 2f 2a 20 20 20 20 6c 6f 63 61 6c /* local
801c0 74 69 6d 65 0a 20 20 20 20 20 20 2a 2a 0a 20 20 time. **.
801d0 20 20 20 20 2a 2a 20 41 73 73 75 6d 69 6e 67 20 ** Assuming
801e0 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 the current time
801f0 20 76 61 6c 75 65 20 69 73 20 55 54 43 20 28 61 value is UTC (a
80200 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20 73 68 69 66 .k.a. GMT), shif
80210 74 20 69 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a t it to. **
80220 20 73 68 6f 77 20 6c 6f 63 61 6c 20 74 69 6d 65 show local time
80230 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
80240 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 if( strcmp(z, "
80250 6c 6f 63 61 6c 74 69 6d 65 22 29 3d 3d 30 20 29 localtime")==0 )
80260 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 {. comput
80270 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 eJD(p);.
80280 70 2d 3e 69 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 p->iJD += localt
80290 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 imeOffset(p);.
802a0 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 clearYMD_H
802b0 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 MS_TZ(p);.
802c0 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 rc = 0;.
802d0 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
802e0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
802f0 63 61 73 65 20 27 75 27 3a 20 7b 0a 20 20 20 20 case 'u': {.
80300 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 /*. **
80310 20 75 6e 69 78 65 70 6f 63 68 0a 20 20 20 20 20 unixepoch.
80320 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 72 65 **. ** Tre
80330 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 at the current v
80340 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4a 44 20 61 alue of p->iJD a
80350 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a s the number of.
80360 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 73 ** seconds
80370 20 73 69 6e 63 65 20 31 39 37 30 2e 20 20 43 6f since 1970. Co
80380 6e 76 65 72 74 20 74 6f 20 61 20 72 65 61 6c 20 nvert to a real
80390 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 julian day numbe
803a0 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 r.. */.
803b0 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 if( strcmp(z,
803c0 22 75 6e 69 78 65 70 6f 63 68 22 29 3d 3d 30 20 "unixepoch")==0
803d0 26 26 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b && p->validJD ){
803e0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
803f0 3d 20 70 2d 3e 69 4a 44 2f 38 36 34 30 30 2e 30 = p->iJD/86400.0
80400 20 2b 20 32 34 34 30 35 38 37 2e 35 2a 38 36 34 + 2440587.5*864
80410 30 30 30 30 30 2e 30 3b 0a 20 20 20 20 20 20 20 00000.0;.
80420 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a clearYMD_HMS_TZ
80430 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 (p);. rc
80440 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 = 0;. }.#if
80450 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
80460 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 _LOCALTIME.
80470 20 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 else if( strcmp
80480 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20 29 7b (z, "utc")==0 ){
80490 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 31 3b . int c1;
804a0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
804b0 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 JD(p);. c
804c0 31 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 1 = localtimeOff
804d0 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 set(p);.
804e0 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 p->iJD -= c1;.
804f0 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 clearYMD_H
80500 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 MS_TZ(p);.
80510 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 63 31 20 2d p->iJD += c1 -
80520 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 localtimeOffset
80530 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 (p);. rc
80540 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e = 0;. }.#en
80550 64 69 66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b dif. break;
80560 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
80570 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 'w': {. /*.
80580 20 20 20 20 20 20 2a 2a 20 20 20 20 77 65 65 6b ** week
80590 64 61 79 20 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 day N. **.
805a0 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 ** Move the
805b0 20 64 61 74 65 20 74 6f 20 74 68 65 20 73 61 6d date to the sam
805c0 65 20 74 69 6d 65 20 6f 6e 20 74 68 65 20 6e 65 e time on the ne
805d0 78 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 xt occurrence of
805e0 0a 20 20 20 20 20 20 2a 2a 20 77 65 65 6b 64 61 . ** weekda
805f0 79 20 4e 20 77 68 65 72 65 20 30 3d 3d 53 75 6e y N where 0==Sun
80600 64 61 79 2c 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 day, 1==Monday,
80610 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 and so forth. I
80620 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 f the. ** d
80630 61 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f ate is already o
80640 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 n the appropriat
80650 65 20 77 65 65 6b 64 61 79 2c 20 74 68 69 73 20 e weekday, this
80660 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 is a no-op..
80670 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 */. if( s
80680 74 72 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64 trncmp(z, "weekd
80690 61 79 20 22 2c 20 38 29 3d 3d 30 20 26 26 20 67 ay ", 8)==0 && g
806a0 65 74 56 61 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 etValue(&z[8],&r
806b0 29 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 )>0.
806c0 20 20 20 20 20 26 26 20 28 6e 3d 72 29 3d 3d 72 && (n=r)==r
806d0 20 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37 20 && n>=0 && r<7
806e0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
806f0 65 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20 20 e3_int64 Z;.
80700 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 computeYMD_H
80710 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 MS(p);. p
80720 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 ->validTZ = 0;.
80730 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a p->validJ
80740 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 D = 0;. c
80750 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 omputeJD(p);.
80760 20 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69 4a Z = ((p->iJ
80770 44 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f 38 D + 129600000)/8
80780 36 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20 20 6400000) % 7;.
80790 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29 20 if( Z>n )
807a0 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20 20 Z -= 7;.
807b0 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a p->iJD += (n - Z
807c0 29 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20 )*86400000;.
807d0 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 clearYMD_HMS
807e0 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 _TZ(p);.
807f0 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a rc = 0;. }.
80800 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
80810 20 7d 0a 20 20 20 20 63 61 73 65 20 27 73 27 3a }. case 's':
80820 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 {. /*.
80830 20 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f 66 ** start of
80840 20 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a 0a TTTTT. **.
80850 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 ** Move th
80860 65 20 64 61 74 65 20 62 61 63 6b 77 61 72 64 73 e date backwards
80870 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e to the beginnin
80880 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 g of the current
80890 20 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20 6f day,. ** o
808a0 72 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72 2e r month or year.
808b0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
808c0 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22 if( strncmp(z, "
808d0 73 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21 3d start of ", 9)!=
808e0 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 0 ) break;.
808f0 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20 63 z += 9;. c
80900 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 omputeYMD(p);.
80910 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 p->validHMS
80920 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68 20 = 1;. p->h
80930 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 = p->m = 0;.
80940 20 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 p->s = 0.0;.
80950 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d p->validTZ =
80960 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 0;. p->val
80970 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 idJD = 0;.
80980 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f if( strcmp(z,"mo
80990 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 nth")==0 ){.
809a0 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 p->D = 1;.
809b0 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 rc = 0;.
809c0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 }else if( st
809d0 72 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d rcmp(z,"year")==
809e0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 0 ){. com
809f0 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 puteYMD(p);.
80a00 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 p->M = 1;.
80a10 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a p->D = 1;.
80a20 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a rc = 0;.
80a30 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
80a40 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d strcmp(z,"day")=
80a50 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 =0 ){. rc
80a60 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
80a70 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
80a80 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a 20 . case '+':.
80a90 20 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20 20 case '-':.
80aa0 20 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20 63 case '0':. c
80ab0 61 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61 73 ase '1':. cas
80ac0 65 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65 20 e '2':. case
80ad0 27 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27 34 '3':. case '4
80ae0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27 3a ':. case '5':
80af0 0a 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a 20 . case '6':.
80b00 20 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20 20 case '7':.
80b10 20 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20 63 case '8':. c
80b20 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20 20 ase '9': {.
80b30 20 6e 20 3d 20 67 65 74 56 61 6c 75 65 28 7a 2c n = getValue(z,
80b40 20 26 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 &r);. asse
80b50 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20 20 20 20 rt( n>=1 );.
80b60 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d 27 3a 27 20 if( z[n]==':'
80b70 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20 ){. /* A
80b80 6d 6f 64 69 66 69 65 72 20 6f 66 20 74 68 65 20 modifier of the
80b90 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 3a 4d 4d 3a form (+|-)HH:MM:
80ba0 53 53 2e 46 46 46 20 61 64 64 73 20 28 6f 72 20 SS.FFF adds (or
80bb0 73 75 62 74 72 61 63 74 73 29 20 74 68 65 0a 20 subtracts) the.
80bc0 20 20 20 20 20 20 20 2a 2a 20 73 70 65 63 69 66 ** specif
80bd0 69 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 68 6f ied number of ho
80be0 75 72 73 2c 20 6d 69 6e 75 74 65 73 2c 20 73 65 urs, minutes, se
80bf0 63 6f 6e 64 73 2c 20 61 6e 64 20 66 72 61 63 74 conds, and fract
80c00 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 0a 20 20 ional seconds.
80c10 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 ** to the
80c20 74 69 6d 65 2e 20 20 54 68 65 20 22 2e 46 46 46 time. The ".FFF
80c30 22 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 " may be omitted
80c40 2e 20 20 54 68 65 20 22 3a 53 53 2e 46 46 46 22 . The ":SS.FFF"
80c50 20 6d 61 79 20 62 65 0a 20 20 20 20 20 20 20 20 may be.
80c60 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a 20 20 20 20 ** omitted..
80c70 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 */. c
80c80 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20 onst char *z2 =
80c90 7a 3b 0a 20 20 20 20 20 20 20 20 44 61 74 65 54 z;. DateT
80ca0 69 6d 65 20 74 78 3b 0a 20 20 20 20 20 20 20 20 ime tx;.
80cb0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 64 61 sqlite3_int64 da
80cc0 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 y;. if( !
80cd0 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 32 isdigit(*(u8*)z2
80ce0 29 20 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 ) ) z2++;.
80cf0 20 20 6d 65 6d 73 65 74 28 26 74 78 2c 20 30 2c memset(&tx, 0,
80d00 20 73 69 7a 65 6f 66 28 74 78 29 29 3b 0a 20 20 sizeof(tx));.
80d10 20 20 20 20 20 20 69 66 28 20 70 61 72 73 65 48 if( parseH
80d20 68 4d 6d 53 73 28 7a 32 2c 20 26 74 78 29 20 29 hMmSs(z2, &tx) )
80d30 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
80d40 63 6f 6d 70 75 74 65 4a 44 28 26 74 78 29 3b 0a computeJD(&tx);.
80d50 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d tx.iJD -
80d60 3d 20 34 33 32 30 30 30 30 30 3b 0a 20 20 20 20 = 43200000;.
80d70 20 20 20 20 64 61 79 20 3d 20 74 78 2e 69 4a 44 day = tx.iJD
80d80 2f 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 /86400000;.
80d90 20 20 20 74 78 2e 69 4a 44 20 2d 3d 20 64 61 79 tx.iJD -= day
80da0 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 *86400000;.
80db0 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 2d 27 if( z[0]=='-'
80dc0 20 29 20 74 78 2e 69 4a 44 20 3d 20 2d 74 78 2e ) tx.iJD = -tx.
80dd0 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d iJD;. com
80de0 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 puteJD(p);.
80df0 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f clearYMD_HMS_
80e00 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 TZ(p);. p
80e10 2d 3e 69 4a 44 20 2b 3d 20 74 78 2e 69 4a 44 3b ->iJD += tx.iJD;
80e20 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b . rc = 0;
80e30 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
80e40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 20 }. z
80e50 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 77 68 69 6c += n;. whil
80e60 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 38 2a e( isspace(*(u8*
80e70 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 )z) ) z++;.
80e80 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a n = strlen(z);.
80e90 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 7c if( n>10 |
80ea0 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a 20 | n<3 ) break;.
80eb0 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d if( z[n-1]=
80ec0 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d ='s' ){ z[n-1] =
80ed0 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 20 0; n--; }.
80ee0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
80ef0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
80f00 20 20 20 69 66 28 20 6e 3d 3d 33 20 26 26 20 73 if( n==3 && s
80f10 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d trcmp(z,"day")==
80f20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 0 ){. p->
80f30 69 4a 44 20 2b 3d 20 72 2a 38 36 34 30 30 30 30 iJD += r*8640000
80f40 30 2e 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20 0.0 + 0.5;.
80f50 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 }else if( n==4
80f60 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 && strcmp(z,"hou
80f70 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 r")==0 ){.
80f80 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 72 2a 28 38 p->iJD += r*(8
80f90 36 34 30 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 6400000.0/24.0)
80fa0 2b 20 30 2e 35 3b 0a 20 20 20 20 20 20 7d 65 6c + 0.5;. }el
80fb0 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 se if( n==6 && s
80fc0 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 22 trcmp(z,"minute"
80fd0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
80fe0 70 2d 3e 69 4a 44 20 2b 3d 20 72 2a 28 38 36 34 p->iJD += r*(864
80ff0 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30 00000.0/(24.0*60
81000 2e 30 29 29 20 2b 20 30 2e 35 3b 0a 20 20 20 20 .0)) + 0.5;.
81010 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 }else if( n==6
81020 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65 && strcmp(z,"se
81030 63 6f 6e 64 22 29 3d 3d 30 20 29 7b 0a 20 20 20 cond")==0 ){.
81040 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 72 p->iJD += r
81050 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 *(86400000.0/(24
81060 2e 30 2a 36 30 2e 30 2a 36 30 2e 30 29 29 20 2b .0*60.0*60.0)) +
81070 20 30 2e 35 3b 0a 20 20 20 20 20 20 7d 65 6c 73 0.5;. }els
81080 65 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73 74 e if( n==5 && st
81090 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d rcmp(z,"month")=
810a0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e =0 ){. in
810b0 74 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20 20 t x, y;.
810c0 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 computeYMD_HMS(p
810d0 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 );. p->M
810e0 2b 3d 20 72 3b 0a 20 20 20 20 20 20 20 20 78 20 += r;. x
810f0 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e 4d = p->M>0 ? (p->M
81100 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d 31 -1)/12 : (p->M-1
81110 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2)/12;. p
81120 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 20 20 ->Y += x;.
81130 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b 0a p->M -= x*12;.
81140 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 p->valid
81150 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 JD = 0;.
81160 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
81170 20 20 20 20 20 20 79 20 3d 20 72 3b 0a 20 20 20 y = r;.
81180 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b if( y!=r ){
81190 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a . p->iJ
811a0 44 20 2b 3d 20 28 72 20 2d 20 79 29 2a 33 30 2e D += (r - y)*30.
811b0 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 0*86400000.0 + 0
811c0 2e 35 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 .5;. }.
811d0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d }else if( n=
811e0 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 =4 && strcmp(z,"
811f0 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 year")==0 ){.
81200 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f computeYMD_
81210 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 HMS(p);.
81220 70 2d 3e 59 20 2b 3d 20 72 3b 0a 20 20 20 20 20 p->Y += r;.
81230 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 p->validJD =
81240 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 0;. compu
81250 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 7d teJD(p);. }
81260 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
81270 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 1;. }.
81280 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 clearYMD_HMS
81290 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62 72 _TZ(p);. br
812a0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
812b0 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
812c0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
812d0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
812e0 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74 ./*.** Process t
812f0 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 ime function arg
81300 75 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30 5d uments. argv[0]
81310 20 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65 20 is a date-time
81320 73 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 stamp..** argv[1
81330 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 ] and following
81340 61 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20 20 are modifiers.
81350 50 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20 61 Parse them all a
81360 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 nd write.** the
81370 72 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20 69 resulting time i
81380 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65 nto the DateTime
81390 20 73 74 72 75 63 74 75 72 65 20 70 2e 20 20 52 structure p. R
813a0 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 eturn 0.** on su
813b0 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74 ccess and 1 if t
813c0 68 65 72 65 20 61 72 65 20 61 6e 79 20 65 72 72 here are any err
813d0 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ors..**.** If th
813e0 65 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61 72 ere are zero par
813f0 61 6d 65 74 65 72 73 20 28 69 66 20 65 76 65 6e ameters (if even
81400 20 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64 65 argv[0] is unde
81410 66 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20 61 fined).** then a
81420 73 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74 20 ssume a default
81430 76 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20 66 value of "now" f
81440 6f 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 or argv[0]..*/.s
81450 74 61 74 69 63 20 69 6e 74 20 69 73 44 61 74 65 tatic int isDate
81460 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
81470 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 ext *context, .
81480 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71 int argc, . sq
81490 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
814a0 67 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 20 gv, . DateTime
814b0 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *p.){. int i;.
814c0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
814d0 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 char *z;. int e
814e0 54 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28 70 Type;. memset(p
814f0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 , 0, sizeof(*p))
81500 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 20 ;. if( argc==0
81510 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 69 ){. setDateTi
81520 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 meToCurrent(cont
81530 65 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65 ext, p);. }else
81540 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 73 71 if( (eType = sq
81550 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
81560 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 (argv[0]))==SQLI
81570 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20 20 TE_FLOAT.
81580 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
81590 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 Type==SQLITE_INT
815a0 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e 69 EGER ){. p->i
815b0 4a 44 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c JD = sqlite3_val
815c0 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 ue_double(argv[0
815d0 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 ])*86400000.0 +
815e0 30 2e 35 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 0.5;. p->vali
815f0 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 dJD = 1;. }else
81600 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 {. z = sqlite
81610 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
81620 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 v[0]);. if( !
81630 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65 4f 72 z || parseDateOr
81640 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 Time(context, (c
81650 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 har*)z, p) ){.
81660 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
81670 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d }. }. for(i=
81680 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 1; i<argc; i++){
81690 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20 73 71 . if( (z = sq
816a0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
816b0 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c (argv[i]))==0 ||
816c0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 28 parseModifier((
816d0 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 char*)z, p) ){.
816e0 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
816f0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
81700 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 n 0;.}.../*.** T
81710 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
81720 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 tines implement
81730 74 68 65 20 76 61 72 69 6f 75 73 20 64 61 74 65 the various date
81740 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63 74 69 and time functi
81750 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 ons.** of SQLite
81760 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a ..*/../*.** j
81770 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45 53 54 ulianday( TIMEST
81780 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 RING, MOD, MOD,
81790 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ...).**.** Retur
817a0 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 n the julian day
817b0 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 64 number of the d
817c0 61 74 65 20 73 70 65 63 69 66 69 65 64 20 69 6e ate specified in
817d0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a the arguments.*
817e0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6a 75 /.static void ju
817f0 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 liandayFunc(. s
81800 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
81810 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
81820 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
81830 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
81840 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 DateTime x;. i
81850 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 f( isDate(contex
81860 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 t, argc, argv, &
81870 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d x)==0 ){. com
81880 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 puteJD(&x);.
81890 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 sqlite3_result_d
818a0 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 ouble(context, x
818b0 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29 .iJD/86400000.0)
818c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 ;. }.}../*.**
818d0 20 20 64 61 74 65 74 69 6d 65 28 20 54 49 4d 45 datetime( TIME
818e0 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 STRING, MOD, MOD
818f0 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 , ...).**.** Ret
81900 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 urn YYYY-MM-DD H
81910 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 H:MM:SS.*/.stati
81920 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d 65 46 c void datetimeF
81930 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
81940 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
81950 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
81960 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
81970 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d rgv.){. DateTim
81980 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 e x;. if( isDat
81990 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c e(context, argc,
819a0 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b argv, &x)==0 ){
819b0 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 . char zBuf[1
819c0 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 00];. compute
819d0 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 YMD_HMS(&x);.
819e0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
819f0 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 f(sizeof(zBuf),
81a00 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 64 zBuf, "%04d-%02d
81a10 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32 64 3a -%02d %02d:%02d:
81a20 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20 20 20 %02d",.
81a30 20 20 20 20 20 20 20 20 20 20 20 20 78 2e 59 2c x.Y,
81a40 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 x.M, x.D, x.h,
81a50 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 x.m, (int)(x.s))
81a60 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
81a70 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
81a80 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c t, zBuf, -1, SQL
81a90 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
81aa0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 }.}../*.**
81ab0 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47 time( TIMESTRING
81ac0 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 , MOD, MOD, ...)
81ad0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 .**.** Return HH
81ae0 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 :MM:SS.*/.static
81af0 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63 28 0a void timeFunc(.
81b00 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
81b10 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
81b20 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
81b30 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
81b40 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a {. DateTime x;.
81b50 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e if( isDate(con
81b60 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 text, argc, argv
81b70 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 , &x)==0 ){.
81b80 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a char zBuf[100];.
81b90 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 computeHMS(&
81ba0 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f x);. sqlite3_
81bb0 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
81bc0 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 zBuf), zBuf, "%0
81bd0 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2d:%02d:%02d", x
81be0 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e .h, x.m, (int)x.
81bf0 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f s);. sqlite3_
81c00 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
81c10 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 ext, zBuf, -1, S
81c20 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
81c30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 ;. }.}../*.**
81c40 20 20 64 61 74 65 28 20 54 49 4d 45 53 54 52 49 date( TIMESTRI
81c50 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e NG, MOD, MOD, ..
81c60 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 .).**.** Return
81c70 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 YYYY-MM-DD.*/.st
81c80 61 74 69 63 20 76 6f 69 64 20 64 61 74 65 46 75 atic void dateFu
81c90 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
81ca0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
81cb0 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
81cc0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
81cd0 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 gv.){. DateTime
81ce0 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 x;. if( isDate
81cf0 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 (context, argc,
81d00 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a argv, &x)==0 ){.
81d10 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 char zBuf[10
81d20 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 0];. computeY
81d30 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 MD(&x);. sqli
81d40 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
81d50 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c eof(zBuf), zBuf,
81d60 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 32 64 "%04d-%02d-%02d
81d70 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 ", x.Y, x.M, x.D
81d80 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 );. sqlite3_r
81d90 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
81da0 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 xt, zBuf, -1, SQ
81db0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b LITE_TRANSIENT);
81dc0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 . }.}../*.**
81dd0 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 strftime( FORMA
81de0 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d T, TIMESTRING, M
81df0 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a OD, MOD, ...).**
81e00 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 74 72 .** Return a str
81e10 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 62 79 ing described by
81e20 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 FORMAT. Conver
81e30 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 sions as follows
81e40 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 :.**.** %d da
81e50 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 y of month.**
81e60 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 %f ** fractiona
81e70 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 l seconds SS.SS
81e80 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75 72 20 S.** %H hour
81e90 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 00-24.** %j d
81ea0 61 79 20 6f 66 20 79 65 61 72 20 30 30 30 2d 33 ay of year 000-3
81eb0 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 66.** %J ** J
81ec0 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 ulian day number
81ed0 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 .** %m month
81ee0 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 01-12.** %M m
81ef0 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 inute 00-59.**
81f00 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73 69 6e %s seconds sin
81f10 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a ce 1970-01-01.**
81f20 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73 20 30 %S seconds 0
81f30 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 0-59.** %w da
81f40 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20 20 73 y of week 0-6 s
81f50 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 unday==0.** %W
81f60 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72 20 30 week of year 0
81f70 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 0-53.** %Y ye
81f80 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 ar 0000-9999.**
81f90 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 %% %.*/.stati
81fa0 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d 65 46 c void strftimeF
81fb0 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
81fc0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
81fd0 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
81fe0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
81ff0 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d rgv.){. DateTim
82000 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 e x;. u64 n;.
82010 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 int i, j;. char
82020 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a *z;. sqlite3 *
82030 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 db;. const char
82040 20 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20 *zFmt = (const
82050 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 char*)sqlite3_va
82060 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d lue_text(argv[0]
82070 29 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 );. char zBuf[1
82080 30 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d 00];. if( zFmt=
82090 3d 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e =0 || isDate(con
820a0 74 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72 text, argc-1, ar
820b0 67 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75 gv+1, &x) ) retu
820c0 72 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 rn;. db = sqlit
820d0 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
820e0 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 ndle(context);.
820f0 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a for(i=0, n=1; z
82100 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b Fmt[i]; i++, n++
82110 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b ){. if( zFmt[
82120 69 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 i]=='%' ){.
82130 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b switch( zFmt[i+
82140 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 1] ){. ca
82150 73 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20 se 'd':.
82160 63 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20 case 'H':.
82170 20 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 case 'm':.
82180 20 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 case 'M':.
82190 20 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a case 'S':.
821a0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 case 'W'
821b0 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b :. n++;
821c0 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 . /* fa
821d0 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 ll thru */.
821e0 20 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20 case 'w':.
821f0 20 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20 case '%':.
82200 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
82210 20 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27 case 'f'
82220 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d :. n +=
82230 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 8;. br
82240 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
82250 65 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 e 'j':.
82260 20 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 n += 3;.
82270 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
82280 20 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20 case 'Y':.
82290 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 n += 8;.
822a0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
822b0 20 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a case 's':
822c0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a . case 'J
822d0 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b ':. n +
822e0 3d 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 50;.
822f0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 break;. d
82300 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 efault:.
82310 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 return; /* ER
82320 52 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e ROR. return a N
82330 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 ULL */. }.
82340 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a i++;. }.
82350 20 20 7d 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 }. if( n<size
82360 6f 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 of(zBuf) ){.
82370 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 z = zBuf;. }els
82380 65 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d e if( n>(u64)db-
82390 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
823a0 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
823b0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
823c0 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 lt_error_toobig(
823d0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 context);. re
823e0 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 turn;. }else{.
823f0 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 z = sqlite3Db
82400 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 MallocRaw(db, n)
82410 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 ;. if( z==0 )
82420 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
82430 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d result_error_nom
82440 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 em(context);.
82450 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
82460 0a 20 20 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 . }. computeJD
82470 28 26 78 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59 (&x);. computeY
82480 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f MD_HMS(&x);. fo
82490 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d r(i=j=0; zFmt[i]
824a0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
824b0 7a 46 6d 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a zFmt[i]!='%' ){.
824c0 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a z[j++] = z
824d0 46 6d 74 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 Fmt[i];. }els
824e0 65 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 e{. i++;.
824f0 20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d 74 switch( zFmt
82500 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 [i] ){. c
82510 61 73 65 20 27 64 27 3a 20 20 73 71 6c 69 74 65 ase 'd': sqlite
82520 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 3_snprintf(3, &z
82530 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b [j],"%02d",x.D);
82540 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 j+=2; break;.
82550 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a 20 case 'f':
82560 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f 75 62 {. doub
82570 6c 65 20 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20 le s = x.s;.
82580 20 20 20 20 20 20 69 66 28 20 73 3e 35 39 2e 39 if( s>59.9
82590 39 39 20 29 20 73 20 3d 20 35 39 2e 39 39 39 3b 99 ) s = 59.999;
825a0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
825b0 65 33 5f 73 6e 70 72 69 6e 74 66 28 37 2c 20 26 e3_snprintf(7, &
825c0 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73 z[j],"%06.3f", s
825d0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b );. j +
825e0 3d 20 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b = strlen(&z[j]);
825f0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
82600 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
82610 20 20 20 20 63 61 73 65 20 27 48 27 3a 20 20 73 case 'H': s
82620 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
82630 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 3, &z[j],"%02d",
82640 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 x.h); j+=2; brea
82650 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
82660 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 'W': /* Fall thr
82670 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 u */. cas
82680 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 e 'j': {.
82690 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 int nDay;
826a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
826b0 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 er of days since
826c0 20 31 73 74 20 64 61 79 20 6f 66 20 79 65 61 72 1st day of year
826d0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 44 61 */. Da
826e0 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 teTime y = x;.
826f0 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69 64 4a y.validJ
82700 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 D = 0;.
82710 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 y.M = 1;.
82720 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 y.D = 1;.
82730 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 computeJD
82740 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (&y);.
82750 6e 44 61 79 20 3d 20 28 78 2e 69 4a 44 20 2d 20 nDay = (x.iJD -
82760 79 2e 69 4a 44 29 2f 38 36 34 30 30 30 30 30 2e y.iJD)/86400000.
82770 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20 20 20 0 + 0.5;.
82780 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d if( zFmt[i]==
82790 27 57 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 'W' ){.
827a0 20 20 20 69 6e 74 20 77 64 3b 20 20 20 2f 2a 20 int wd; /*
827b0 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54 75 65 73 0=Monday, 1=Tues
827c0 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 day, ... 6=Sunda
827d0 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 y */.
827e0 20 77 64 20 3d 20 28 28 78 2e 69 4a 44 2b 34 33 wd = ((x.iJD+43
827f0 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 200000)/86400000
82800 29 20 25 20 37 3b 0a 20 20 20 20 20 20 20 20 20 ) % 7;.
82810 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
82820 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 ntf(3, &z[j],"%0
82830 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 2d",(nDay+7-wd)/
82840 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7);.
82850 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 j += 2;.
82860 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
82870 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
82880 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 rintf(4, &z[j],"
82890 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 %03d",nDay+1);.
828a0 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 j +=
828b0 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 3;. }.
828c0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
828d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
828e0 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 case 'J': {.
828f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
82900 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b snprintf(20, &z[
82910 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44 j],"%.16g",x.iJD
82920 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 /86400000.0);.
82930 20 20 20 20 20 20 20 20 6a 2b 3d 73 74 72 6c 65 j+=strle
82940 6e 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 n(&z[j]);.
82950 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
82960 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 }. cas
82970 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 33 5f e 'm': sqlite3_
82980 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a snprintf(3, &z[j
82990 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b 20 6a ],"%02d",x.M); j
829a0 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 +=2; break;.
829b0 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 20 73 case 'M': s
829c0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
829d0 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 3, &z[j],"%02d",
829e0 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 x.m); j+=2; brea
829f0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
82a00 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 's': {.
82a10 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
82a20 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 64 22 2c f(30,&z[j],"%d",
82a30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
82a40 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 (int
82a50 29 28 78 2e 69 4a 44 2f 31 30 30 30 2e 30 20 2d )(x.iJD/1000.0 -
82a60 20 32 31 30 38 36 36 37 36 30 30 30 30 2e 30 29 210866760000.0)
82a70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b );. j +
82a80 3d 20 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b = strlen(&z[j]);
82a90 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
82aa0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
82ab0 20 20 20 20 63 61 73 65 20 27 53 27 3a 20 20 73 case 'S': s
82ac0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
82ad0 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 3,&z[j],"%02d",(
82ae0 69 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 3b 20 int)x.s); j+=2;
82af0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
82b00 61 73 65 20 27 77 27 3a 20 20 7a 5b 6a 2b 2b 5d ase 'w': z[j++]
82b10 20 3d 20 28 28 28 78 2e 69 4a 44 2b 31 32 39 36 = (((x.iJD+1296
82b20 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 00000)/86400000)
82b30 20 25 20 37 29 20 2b 20 27 30 27 3b 20 62 72 65 % 7) + '0'; bre
82b40 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
82b50 20 27 59 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 'Y': sqlite3_s
82b60 6e 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a 5d 2c nprintf(5,&z[j],
82b70 22 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a 2b 3d "%04d",x.Y); j+=
82b80 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b 62 72 strlen(&z[j]);br
82b90 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 eak;. def
82ba0 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d ault: z[j++] =
82bb0 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20 '%'; break;.
82bc0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
82bd0 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c z[j] = 0;. sql
82be0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
82bf0 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c (context, z, -1,
82c00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
82c10 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f z==zBuf ?
82c20 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
82c30 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d T : SQLITE_DYNAM
82c40 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 IC);.}../*.** cu
82c50 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a rrent_time().**.
82c60 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
82c70 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d returns the sam
82c80 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d 65 28 e value as time(
82c90 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 'now')..*/.stati
82ca0 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75 6e 63 c void ctimeFunc
82cb0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
82cc0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
82cd0 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 int NotUsed,. s
82ce0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e qlite3_value **N
82cf0 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 otUsed2.){. UNU
82d00 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
82d10 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
82d20 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28 63 6f );. timeFunc(co
82d30 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a ntext, 0, 0);.}.
82d40 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64 ./*.** current_d
82d50 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ate().**.** This
82d60 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
82d70 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 s the same value
82d80 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27 29 2e as date('now').
82d90 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
82da0 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c cdateFunc(. sql
82db0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
82dc0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 ntext,. int Not
82dd0 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f Used,. sqlite3_
82de0 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
82df0 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 .){. UNUSED_PAR
82e00 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c AMETER2(NotUsed,
82e10 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 64 61 NotUsed2);. da
82e20 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 teFunc(context,
82e30 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0, 0);.}../*.**
82e40 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d current_timestam
82e50 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 p().**.** This f
82e60 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
82e70 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 the same value a
82e80 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 s datetime('now'
82e90 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
82ea0 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 d ctimestampFunc
82eb0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
82ec0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
82ed0 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 int NotUsed,. s
82ee0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e qlite3_value **N
82ef0 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 otUsed2.){. UNU
82f00 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
82f10 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
82f20 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46 75 6e );. datetimeFun
82f30 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 c(context, 0, 0)
82f40 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 ;.}.#endif /* !d
82f50 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
82f60 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 IT_DATETIME_FUNC
82f70 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 S) */..#ifdef SQ
82f80 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 LITE_OMIT_DATETI
82f90 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 ME_FUNCS./*.** I
82fa0 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 f the library is
82fb0 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69 compiled to omi
82fc0 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65 t the full-scale
82fd0 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a date and time.*
82fe0 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 * handling (to g
82ff0 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e et a smaller bin
83000 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 ary), the follow
83010 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 ing minimal vers
83020 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 ion.** of the fu
83030 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f nctions current_
83040 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e 74 5f time(), current_
83050 64 61 74 65 28 29 20 61 6e 64 20 63 75 72 72 65 date() and curre
83060 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a nt_timestamp().*
83070 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 * are included i
83080 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69 73 20 nstead. This is
83090 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d to support colum
830a0 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 n declarations t
830b0 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22 hat.** include "
830c0 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e 54 5f DEFAULT CURRENT_
830d0 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a TIME" etc..**.**
830e0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 This function u
830f0 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72 61 72 ses the C-librar
83100 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65 y functions time
83110 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20 (), gmtime().**
83120 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29 2e 20 and strftime().
83130 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e The format strin
83140 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73 74 72 g to pass to str
83150 66 74 69 6d 65 28 29 20 69 73 20 73 75 70 70 6c ftime() is suppl
83160 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 75 73 ied.** as the us
83170 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68 65 20 er-data for the
83180 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 function..*/.sta
83190 74 69 63 20 76 6f 69 64 20 63 75 72 72 65 6e 74 tic void current
831a0 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 TimeFunc(. sqli
831b0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
831c0 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
831d0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
831e0 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69 e **argv.){. ti
831f0 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72 20 2a me_t t;. char *
83200 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61 72 20 zFormat = (char
83210 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 *)sqlite3_user_d
83220 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 ata(context);.
83230 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 sqlite3 *db;. d
83240 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68 61 72 ouble rT;. char
83250 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 64 62 zBuf[20];.. db
83260 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 = sqlite3_conte
83270 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e xt_db_handle(con
83280 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 text);. sqlite3
83290 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 OsCurrentTime(db
832a0 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b 0a 20 20 ->pVfs, &rT);.
832b0 74 20 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20 t = 86400.0*(rT
832c0 2d 20 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30 - 2440587.5) + 0
832d0 2e 35 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f .5;.#ifdef HAVE_
832e0 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 GMTIME_R. {.
832f0 20 73 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b struct tm sNow;
83300 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74 . gmtime_r(&t
83310 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74 , &sNow);. st
83320 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c rftime(zBuf, 20,
83330 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29 zFormat, &sNow)
83340 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a ;. }.#else. {.
83350 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 struct tm *p
83360 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f Tm;. sqlite3_
83370 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 mutex_enter(sqli
83380 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
83390 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
833a0 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 C_MASTER));.
833b0 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29 pTm = gmtime(&t)
833c0 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a ;. strftime(z
833d0 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 Buf, 20, zFormat
833e0 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69 , pTm);. sqli
833f0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
83400 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
83410 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
83420 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a TATIC_MASTER));.
83430 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 }.#endif.. sq
83440 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
83450 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c t(context, zBuf,
83460 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e -1, SQLITE_TRAN
83470 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 SIENT);.}.#endif
83480 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
83490 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 ction registered
834a0 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 all of the abov
834b0 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 e C functions as
834c0 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e SQL.** function
834d0 73 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 s. This should
834e0 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 be the only rout
834f0 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ine in this file
83500 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 with.** externa
83510 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 l linkage..*/.SQ
83520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
83530 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 d sqlite3Registe
83540 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f rDateTimeFunctio
83550 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ns(void){. stat
83560 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 ic SQLITE_WSD Fu
83570 6e 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46 ncDef aDateTimeF
83580 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 uncs[] = {.#ifnd
83590 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
835a0 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20 ATETIME_FUNCS.
835b0 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 FUNCTION(julia
835c0 6e 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c nday, -1,
835d0 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79 0, 0, julianday
835e0 46 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43 Func ),. FUNC
835f0 54 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20 TION(date,
83600 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c -1, 0, 0,
83610 20 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29 dateFunc )
83620 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 ,. FUNCTION(t
83630 69 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ime,
83640 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46 -1, 0, 0, timeF
83650 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 unc ),.
83660 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d FUNCTION(datetim
83670 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 e, -1, 0
83680 2c 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e , 0, datetimeFun
83690 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 c ),. FUNCTI
836a0 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20 ON(strftime,
836b0 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73 -1, 0, 0, s
836c0 74 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a trftimeFunc ),.
836d0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 FUNCTION(cur
836e0 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 rent_time,
836f0 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75 0, 0, 0, ctimeFu
83700 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 nc ),. FU
83710 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 NCTION(current_t
83720 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20 imestamp, 0, 0,
83730 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 0, ctimestampFun
83740 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e c),. FUNCTION
83750 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20 (current_date,
83760 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61 0, 0, 0, cda
83770 74 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65 teFunc ),.#e
83780 6c 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 lse. STR_FUNC
83790 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d TION(current_tim
837a0 65 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25 e, 0, "%H:%
837b0 4d 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20 M:%S",
837c0 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 0, currentTimeFu
837d0 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e nc),. STR_FUN
837e0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 CTION(current_ti
837f0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d mestamp, 0, "%Y-
83800 25 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20 %m-%d",
83810 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 0, currentTimeF
83820 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 unc),. STR_FU
83830 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 NCTION(current_d
83840 61 74 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59 ate, 0, "%Y
83850 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22 -%m-%d %H:%M:%S"
83860 2c 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 , 0, currentTime
83870 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20 Func),.#endif.
83880 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 };. int i;. Fu
83890 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 ncDefHash *pHash
838a0 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 = &GLOBAL(FuncD
838b0 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 efHash, sqlite3G
838c0 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b lobalFunctions);
838d0 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e . FuncDef *aFun
838e0 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 c = (FuncDef*)&G
838f0 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 LOBAL(FuncDef, a
83900 44 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a DateTimeFuncs);.
83910 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 . for(i=0; i<Ar
83920 72 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d raySize(aDateTim
83930 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 eFuncs); i++){.
83940 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 sqlite3FuncDe
83950 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 fInsert(pHash, &
83960 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d aFunc[i]);. }.}
83970 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
83980 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20 * End of date.c
83990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
839a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
839b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
839c0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
839d0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e * Begin file os.
839e0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
839f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83a10 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 /./*.** 2005 Nov
83a20 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 ember 29.**.** T
83a30 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
83a40 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
83a50 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
83a60 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
83a70 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
83a80 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
83a90 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
83aa0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
83ab0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
83ac0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
83ad0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
83ae0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
83af0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
83b00 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
83b10 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
83b20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
83b30 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
83b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
83b90 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
83ba0 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61 tains OS interfa
83bb0 63 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ce code that is
83bc0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a common to all.**
83bd0 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a architectures..
83be0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 63 2c **.** $Id: os.c,
83bf0 76 20 31 2e 31 32 34 20 32 30 30 38 2f 31 30 2f v 1.124 2008/10/
83c00 30 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 07 15:25:48 drh
83c10 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66 69 6e 65 Exp $.*/.#define
83c20 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 20 31 _SQLITE_OS_C_ 1
83c30 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f .#undef _SQLITE_
83c40 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 OS_C_../*.** The
83c50 20 64 65 66 61 75 6c 74 20 53 51 4c 69 74 65 20 default SQLite
83c60 73 71 6c 69 74 65 33 5f 76 66 73 20 69 6d 70 6c sqlite3_vfs impl
83c70 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 20 6e ementations do n
83c80 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d ot allocate.** m
83c90 65 6d 6f 72 79 20 28 61 63 74 75 61 6c 6c 79 2c emory (actually,
83ca0 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c 6c 6f 63 os_unix.c alloc
83cb0 61 74 65 73 20 61 20 73 6d 61 6c 6c 20 61 6d 6f ates a small amo
83cc0 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a unt of memory.**
83cd0 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 4f 73 4f from within OsO
83ce0 70 65 6e 28 29 29 2c 20 62 75 74 20 73 6f 6d 65 pen()), but some
83cf0 20 74 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70 third-party imp
83d00 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79 lementations may
83d10 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65 73 74 20 ..** So we test
83d20 74 68 65 20 65 66 66 65 63 74 73 20 6f 66 20 61 the effects of a
83d30 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 69 6e malloc() failin
83d40 67 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 g and the sqlite
83d50 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66 75 6e 63 3OsXXX().** func
83d60 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e 67 20 53 tion returning S
83d70 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 QLITE_IOERR_NOME
83d80 4d 20 75 73 69 6e 67 20 74 68 65 20 44 4f 5f 4f M using the DO_O
83d90 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 6d 61 S_MALLOC_TEST ma
83da0 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 cro..**.** The f
83db0 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f ollowing functio
83dc0 6e 73 20 61 72 65 20 69 6e 73 74 72 75 6d 65 6e ns are instrumen
83dd0 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f 63 28 29 ted for malloc()
83de0 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 74 65 73 failure .** tes
83df0 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ting:.**.**
83e00 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 0a sqlite3OsOpen().
83e10 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
83e20 52 65 61 64 28 29 0a 2a 2a 20 20 20 20 20 73 71 Read().** sq
83e30 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 0a 2a lite3OsWrite().*
83e40 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 * sqlite3OsS
83e50 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c ync().** sql
83e60 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 0a ite3OsLock().**.
83e70 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
83e80 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 28 QLITE_TEST) && (
83e90 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 3d 3d 30 SQLITE_OS_WIN==0
83ea0 29 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f ). #define DO_O
83eb0 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 69 66 S_MALLOC_TEST if
83ec0 20 28 31 29 20 7b 20 20 20 20 20 20 20 20 20 20 (1) {
83ed0 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54 \. void *pT
83ee0 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 stAlloc = sqlite
83ef0 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20 3Malloc(10);
83f00 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70 54 \. if (!pT
83f10 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e 20 stAlloc) return
83f20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d SQLITE_IOERR_NOM
83f30 45 4d 3b 20 20 5c 0a 20 20 20 20 73 71 6c 69 74 EM; \. sqlit
83f40 65 33 5f 66 72 65 65 28 70 54 73 74 41 6c 6c 6f e3_free(pTstAllo
83f50 63 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 c);
83f60 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c \. }.#el
83f70 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f se. #define DO_
83f80 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 0a 23 OS_MALLOC_TEST.#
83f90 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
83fa0 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
83fb0 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 nes are convenie
83fc0 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f nce wrappers aro
83fd0 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f und methods.** o
83fe0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 f the sqlite3_fi
83ff0 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 le object. This
84000 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20 is mostly just
84010 73 79 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e syntactic sugar.
84020 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20 All.** of this
84030 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74 would be complet
84040 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66 ely automatic if
84050 20 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64 SQLite were cod
84060 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20 ed using.** C++
84070 69 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e instead of plain
84080 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 old C..*/.SQLIT
84090 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
840a0 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c lite3OsClose(sql
840b0 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b ite3_file *pId){
840c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
840d0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64 TE_OK;. if( pId
840e0 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 ->pMethods ){.
840f0 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74 rc = pId->pMet
84100 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64 hods->xClose(pId
84110 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74 );. pId->pMet
84120 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 hods = 0;. }.
84130 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c return rc;.}.SQL
84140 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
84150 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 sqlite3OsRead(sq
84160 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
84170 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 void *pBuf, int
84180 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 amt, i64 offset)
84190 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 {. DO_OS_MALLOC
841a0 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 _TEST;. return
841b0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52 id->pMethods->xR
841c0 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 61 6d ead(id, pBuf, am
841d0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 t, offset);.}.SQ
841e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
841f0 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
84200 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
84210 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 , const void *pB
84220 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 uf, int amt, i64
84230 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f offset){. DO_O
84240 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 S_MALLOC_TEST;.
84250 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
84260 68 6f 64 73 2d 3e 78 57 72 69 74 65 28 69 64 2c hods->xWrite(id,
84270 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 pBuf, amt, offs
84280 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 et);.}.SQLITE_PR
84290 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
842a0 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69 3OsTruncate(sqli
842b0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 te3_file *id, i6
842c0 34 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75 72 4 size){. retur
842d0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
842e0 78 54 72 75 6e 63 61 74 65 28 69 64 2c 20 73 69 xTruncate(id, si
842f0 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 ze);.}.SQLITE_PR
84300 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
84310 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 3OsSync(sqlite3_
84320 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c file *id, int fl
84330 61 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 ags){. DO_OS_MA
84340 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 LLOC_TEST;. ret
84350 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 urn id->pMethods
84360 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 67 ->xSync(id, flag
84370 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 s);.}.SQLITE_PRI
84380 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
84390 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 OsFileSize(sqlit
843a0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 e3_file *id, i64
843b0 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f *pSize){. DO_O
843c0 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 S_MALLOC_TEST;.
843d0 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
843e0 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28 hods->xFileSize(
843f0 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51 id, pSize);.}.SQ
84400 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
84410 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 sqlite3OsLock(s
84420 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
84430 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a int lockType){.
84440 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 DO_OS_MALLOC_T
84450 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 EST;. return id
84460 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 4c 6f 63 ->pMethods->xLoc
84470 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b k(id, lockType);
84480 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
84490 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 E int sqlite3OsU
844a0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
844b0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
844c0 54 79 70 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 Type){. return
844d0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 55 id->pMethods->xU
844e0 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 nlock(id, lockTy
844f0 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 pe);.}.SQLITE_PR
84500 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
84510 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 3OsCheckReserved
84520 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
84530 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
84540 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 Out){. DO_OS_MA
84550 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 LLOC_TEST;. ret
84560 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 urn id->pMethods
84570 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 ->xCheckReserved
84580 4c 6f 63 6b 28 69 64 2c 20 70 52 65 73 4f 75 74 Lock(id, pResOut
84590 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
845a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
845b0 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c sFileControl(sql
845c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
845d0 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 nt op, void *pAr
845e0 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d g){. return id-
845f0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 >pMethods->xFile
84600 43 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 Control(id, op,
84610 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f pArg);.}.SQLITE_
84620 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
84630 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 te3OsSectorSize(
84640 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
84650 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74 ){. int (*xSect
84660 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f orSize)(sqlite3_
84670 66 69 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 file*) = id->pMe
84680 74 68 6f 64 73 2d 3e 78 53 65 63 74 6f 72 53 69 thods->xSectorSi
84690 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 78 53 ze;. return (xS
846a0 65 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 63 ectorSize ? xSec
846b0 74 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53 51 torSize(id) : SQ
846c0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 LITE_DEFAULT_SEC
846d0 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 4c TOR_SIZE);.}.SQL
846e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
846f0 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 sqlite3OsDeviceC
84700 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 haracteristics(s
84710 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
84720 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 {. return id->p
84730 4d 65 74 68 6f 64 73 2d 3e 78 44 65 76 69 63 65 Methods->xDevice
84740 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
84750 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 id);.}../*.** Th
84760 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 e next group of
84770 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e routines are con
84780 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 venience wrapper
84790 73 20 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 s around the.**
847a0 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a VFS methods..*/.
847b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
847c0 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e nt sqlite3OsOpen
847d0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
847e0 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 *pVfs, . const
847f0 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 char *zPath, .
84800 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 sqlite3_file *pF
84810 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 ile, . int flag
84820 73 2c 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 s, . int *pFlag
84830 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f sOut.){. DO_OS_
84840 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 MALLOC_TEST;. r
84850 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 4f 70 65 eturn pVfs->xOpe
84860 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 n(pVfs, zPath, p
84870 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70 46 6c File, flags, pFl
84880 61 67 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 agsOut);.}.SQLIT
84890 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
848a0 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 lite3OsDelete(sq
848b0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
848c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
848d0 74 68 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29 th, int dirSync)
848e0 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d {. return pVfs-
848f0 3e 78 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a >xDelete(pVfs, z
84900 50 61 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a Path, dirSync);.
84910 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
84920 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 int sqlite3OsAc
84930 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f cess(. sqlite3_
84940 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f vfs *pVfs, . co
84950 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c nst char *zPath,
84960 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a . int flags, .
84970 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29 int *pResOut.)
84980 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 {. DO_OS_MALLOC
84990 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 _TEST;. return
849a0 70 56 66 73 2d 3e 78 41 63 63 65 73 73 28 70 56 pVfs->xAccess(pV
849b0 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c 61 67 73 fs, zPath, flags
849c0 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 , pResOut);.}.SQ
849d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
849e0 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 sqlite3OsFullPa
849f0 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 thname(. sqlite
84a00 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 3_vfs *pVfs, .
84a10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
84a20 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 74 68 4f h, . int nPathO
84a30 75 74 2c 20 0a 20 20 63 68 61 72 20 2a 7a 50 61 ut, . char *zPa
84a40 74 68 4f 75 74 0a 29 7b 0a 20 20 72 65 74 75 72 thOut.){. retur
84a50 6e 20 70 56 66 73 2d 3e 78 46 75 6c 6c 50 61 74 n pVfs->xFullPat
84a60 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 50 61 74 hname(pVfs, zPat
84a70 68 2c 20 6e 50 61 74 68 4f 75 74 2c 20 7a 50 61 h, nPathOut, zPa
84a80 74 68 4f 75 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 thOut);.}.#ifnde
84a90 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
84aa0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c AD_EXTENSION.SQL
84ab0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
84ac0 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 *sqlite3OsDlOpe
84ad0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 n(sqlite3_vfs *p
84ae0 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Vfs, const char
84af0 2a 7a 50 61 74 68 29 7b 0a 20 20 72 65 74 75 72 *zPath){. retur
84b00 6e 20 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 n pVfs->xDlOpen(
84b10 70 56 66 73 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a pVfs, zPath);.}.
84b20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
84b30 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 oid sqlite3OsDlE
84b40 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
84b50 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 *pVfs, int nByt
84b60 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 e, char *zBufOut
84b70 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 45 72 ){. pVfs->xDlEr
84b80 72 6f 72 28 70 56 66 73 2c 20 6e 42 79 74 65 2c ror(pVfs, nByte,
84b90 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c zBufOut);.}.SQL
84ba0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
84bb0 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d *sqlite3OsDlSym
84bc0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
84bd0 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c fs, void *pHandl
84be0 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
84bf0 53 79 6d 62 6f 6c 29 7b 0a 20 20 72 65 74 75 72 Symbol){. retur
84c00 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 n pVfs->xDlSym(p
84c10 56 66 73 2c 20 70 48 61 6e 64 6c 65 2c 20 7a 53 Vfs, pHandle, zS
84c20 79 6d 62 6f 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 ymbol);.}.SQLITE
84c30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
84c40 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73 lite3OsDlClose(s
84c50 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
84c60 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 , void *pHandle)
84c70 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43 6c 6f {. pVfs->xDlClo
84c80 73 65 28 70 56 66 73 2c 20 70 48 61 6e 64 6c 65 se(pVfs, pHandle
84c90 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
84ca0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
84cb0 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c EXTENSION */.SQL
84cc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
84cd0 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e sqlite3OsRandomn
84ce0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ess(sqlite3_vfs
84cf0 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 *pVfs, int nByte
84d00 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 , char *zBufOut)
84d10 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d {. return pVfs-
84d20 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 28 70 56 66 >xRandomness(pVf
84d30 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75 s, nByte, zBufOu
84d40 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 t);.}.SQLITE_PRI
84d50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
84d60 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f OsSleep(sqlite3_
84d70 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
84d80 4d 69 63 72 6f 29 7b 0a 20 20 72 65 74 75 72 6e Micro){. return
84d90 20 70 56 66 73 2d 3e 78 53 6c 65 65 70 28 70 56 pVfs->xSleep(pV
84da0 66 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d 0a 53 fs, nMicro);.}.S
84db0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
84dc0 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 t sqlite3OsCurre
84dd0 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 ntTime(sqlite3_v
84de0 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 fs *pVfs, double
84df0 20 2a 70 54 69 6d 65 4f 75 74 29 7b 0a 20 20 72 *pTimeOut){. r
84e00 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 43 75 72 eturn pVfs->xCur
84e10 72 65 6e 74 54 69 6d 65 28 70 56 66 73 2c 20 70 rentTime(pVfs, p
84e20 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a 53 51 4c TimeOut);.}..SQL
84e30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
84e40 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c sqlite3OsOpenMal
84e50 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 loc(. sqlite3_v
84e60 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e fs *pVfs, . con
84e70 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 st char *zFile,
84e80 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
84e90 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74 **ppFile, . int
84ea0 20 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20 2a 70 flags,. int *p
84eb0 4f 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20 69 6e OutFlags.){. in
84ec0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f t rc = SQLITE_NO
84ed0 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 MEM;. sqlite3_f
84ee0 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 70 46 ile *pFile;. pF
84ef0 69 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 ile = (sqlite3_f
84f00 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c ile *)sqlite3Mal
84f10 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 loc(pVfs->szOsFi
84f20 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 le);. if( pFile
84f30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
84f40 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c ite3OsOpen(pVfs,
84f50 20 7a 46 69 6c 65 2c 20 70 46 69 6c 65 2c 20 66 zFile, pFile, f
84f60 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 67 73 29 lags, pOutFlags)
84f70 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
84f80 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
84f90 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 sqlite3_free(pF
84fa0 69 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ile);. }else{
84fb0 0a 20 20 20 20 20 20 2a 70 70 46 69 6c 65 20 3d . *ppFile =
84fc0 20 70 46 69 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 pFile;. }.
84fd0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
84fe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
84ff0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f int sqlite3OsClo
85000 73 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 66 seFree(sqlite3_f
85010 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 ile *pFile){. i
85020 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
85030 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 K;. assert( pFi
85040 6c 65 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c le );. rc = sql
85050 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 46 69 6c ite3OsClose(pFil
85060 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 e);. sqlite3_fr
85070 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 72 65 74 ee(pFile);. ret
85080 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
85090 20 54 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c The list of all
850a0 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53 20 registered VFS
850b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e implementations.
850c0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 .*/.static sqlit
850d0 65 33 5f 76 66 73 20 2a 20 53 51 4c 49 54 45 5f e3_vfs * SQLITE_
850e0 57 53 44 20 76 66 73 4c 69 73 74 20 3d 20 30 3b WSD vfsList = 0;
850f0 0a 23 64 65 66 69 6e 65 20 76 66 73 4c 69 73 74 .#define vfsList
85100 20 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 5f GLOBAL(sqlite3_
85110 76 66 73 20 2a 2c 20 76 66 73 4c 69 73 74 29 0a vfs *, vfsList).
85120 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20 ./*.** Locate a
85130 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 20 49 66 VFS by name. If
85140 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 69 76 65 no name is give
85150 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e n, simply return
85160 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 56 46 the.** first VF
85170 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a S on the list..*
85180 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
85190 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 ite3_vfs *sqlite
851a0 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 3_vfs_find(const
851b0 20 63 68 61 72 20 2a 7a 56 66 73 29 7b 0a 20 20 char *zVfs){.
851c0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
851d0 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 s = 0;.#if SQLIT
851e0 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 E_THREADSAFE. s
851f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
85200 74 65 78 3b 0a 23 65 6e 64 69 66 0a 23 69 66 6e tex;.#endif.#ifn
85210 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
85220 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72 AUTOINIT. int r
85230 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 c = sqlite3_init
85240 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 ialize();. if(
85250 72 63 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 rc ) return 0;.#
85260 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 endif.#if SQLITE
85270 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 6d 75 _THREADSAFE. mu
85280 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
85290 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
852a0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
852b0 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 ER);.#endif. sq
852c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
852d0 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 r(mutex);. for(
852e0 70 56 66 73 20 3d 20 76 66 73 4c 69 73 74 3b 20 pVfs = vfsList;
852f0 70 56 66 73 3b 20 70 56 66 73 3d 70 56 66 73 2d pVfs; pVfs=pVfs-
85300 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
85310 20 7a 56 66 73 3d 3d 30 20 29 20 62 72 65 61 6b zVfs==0 ) break
85320 3b 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 ;. if( strcmp
85330 28 7a 56 66 73 2c 20 70 56 66 73 2d 3e 7a 4e 61 (zVfs, pVfs->zNa
85340 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a me)==0 ) break;.
85350 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 }. sqlite3_mu
85360 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 tex_leave(mutex)
85370 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 3b ;. return pVfs;
85380 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b .}../*.** Unlink
85390 20 61 20 56 46 53 20 66 72 6f 6d 20 74 68 65 20 a VFS from the
853a0 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 2a 2f 0a 73 linked list.*/.s
853b0 74 61 74 69 63 20 76 6f 69 64 20 76 66 73 55 6e tatic void vfsUn
853c0 6c 69 6e 6b 28 73 71 6c 69 74 65 33 5f 76 66 73 link(sqlite3_vfs
853d0 20 2a 70 56 66 73 29 7b 0a 20 20 61 73 73 65 72 *pVfs){. asser
853e0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
853f0 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 _held(sqlite3Mut
85400 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
85410 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
85420 45 52 29 29 20 29 3b 0a 20 20 69 66 28 20 70 56 ER)) );. if( pV
85430 66 73 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 fs==0 ){. /*
85440 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d 65 6c 73 65 No-op */. }else
85450 20 69 66 28 20 76 66 73 4c 69 73 74 3d 3d 70 56 if( vfsList==pV
85460 66 73 20 29 7b 0a 20 20 20 20 76 66 73 4c 69 73 fs ){. vfsLis
85470 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74 3b t = pVfs->pNext;
85480 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66 73 . }else if( vfs
85490 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 List ){. sqli
854a0 74 65 33 5f 76 66 73 20 2a 70 20 3d 20 76 66 73 te3_vfs *p = vfs
854b0 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 List;. while(
854c0 20 70 2d 3e 70 4e 65 78 74 20 26 26 20 70 2d 3e p->pNext && p->
854d0 70 4e 65 78 74 21 3d 70 56 66 73 20 29 7b 0a 20 pNext!=pVfs ){.
854e0 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 p = p->pNex
854f0 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 t;. }. if(
85500 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 56 66 73 20 p->pNext==pVfs
85510 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 ){. p->pNex
85520 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74 3b t = pVfs->pNext;
85530 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
85540 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 56 .** Register a V
85550 46 53 20 77 69 74 68 20 74 68 65 20 73 79 73 74 FS with the syst
85560 65 6d 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c em. It is harml
85570 65 73 73 20 74 6f 20 72 65 67 69 73 74 65 72 20 ess to register
85580 74 68 65 20 73 61 6d 65 0a 2a 2a 20 56 46 53 20 the same.** VFS
85590 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 20 multiple times.
855a0 20 54 68 65 20 6e 65 77 20 56 46 53 20 62 65 63 The new VFS bec
855b0 6f 6d 65 73 20 74 68 65 20 64 65 66 61 75 6c 74 omes the default
855c0 20 69 66 20 6d 61 6b 65 44 66 6c 74 20 69 73 0a if makeDflt is.
855d0 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 ** true..*/.SQLI
855e0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
855f0 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 e3_vfs_register(
85600 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
85610 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 s, int makeDflt)
85620 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
85630 78 20 2a 6d 75 74 65 78 20 3d 20 30 3b 0a 23 69 x *mutex = 0;.#i
85640 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
85650 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 T_AUTOINIT. int
85660 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e rc = sqlite3_in
85670 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 itialize();. if
85680 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
85690 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 75 74 65 78 ;.#endif. mutex
856a0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
856b0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
856c0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
856d0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
856e0 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a x_enter(mutex);.
856f0 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73 vfsUnlink(pVfs
85700 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65 44 66 6c );. if( makeDfl
85710 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d 3d 30 20 t || vfsList==0
85720 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 ){. pVfs->pNe
85730 78 74 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 xt = vfsList;.
85740 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66 73 vfsList = pVfs
85750 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
85760 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73 Vfs->pNext = vfs
85770 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 List->pNext;.
85780 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 20 vfsList->pNext
85790 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20 20 61 73 = pVfs;. }. as
857a0 73 65 72 74 28 76 66 73 4c 69 73 74 29 3b 0a 20 sert(vfsList);.
857b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
857c0 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 eave(mutex);. r
857d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
857e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 65 67 69 .}../*.** Unregi
857f0 73 74 65 72 20 61 20 56 46 53 20 73 6f 20 74 68 ster a VFS so th
85800 61 74 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 at it is no long
85810 65 72 20 61 63 63 65 73 73 69 62 6c 65 2e 0a 2a er accessible..*
85820 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
85830 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 sqlite3_vfs_unr
85840 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f egister(sqlite3_
85850 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 69 66 20 vfs *pVfs){.#if
85860 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
85870 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 E. sqlite3_mute
85880 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 x *mutex = sqlit
85890 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
858a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
858b0 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 _MASTER);.#endif
858c0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
858d0 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 _enter(mutex);.
858e0 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73 29 vfsUnlink(pVfs)
858f0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
85900 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a x_leave(mutex);.
85910 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
85920 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a OK;.}../********
85930 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 ****** End of os
85940 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
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 2a 2a 2a ****************
85970 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
85980 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
85990 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a e fault.c ******
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 2a 2a 2a ****************
859c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
859d0 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2a 20 54 8 Jan 22.**.** T
859e0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
859f0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
85a00 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
85a10 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
85a20 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
85a30 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
85a40 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
85a50 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
85a60 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
85a70 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
85a80 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
85a90 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
85aa0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
85ab0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
85ac0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
85ad0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
85ae0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you 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 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 *******.**.** $I
85b40 64 3a 20 66 61 75 6c 74 2e 63 2c 76 20 31 2e 31 d: fault.c,v 1.1
85b50 31 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a 1 2008/09/02 00:
85b60 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a 52:52 drh Exp $.
85b70 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 */../*.** This f
85b80 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
85b90 65 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65 e to support the
85ba0 20 63 6f 6e 63 65 70 74 20 6f 66 20 22 62 65 6e concept of "ben
85bb0 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 ign" .** malloc
85bc0 66 61 69 6c 75 72 65 73 20 28 77 68 65 6e 20 74 failures (when t
85bd0 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 he xMalloc() or
85be0 78 52 65 61 6c 6c 6f 63 28 29 20 6d 65 74 68 6f xRealloc() metho
85bf0 64 20 6f 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 d of the.** sqli
85c00 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
85c10 73 74 72 75 63 74 75 72 65 20 66 61 69 6c 73 20 structure fails
85c20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c to allocate a bl
85c30 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a ock of memory.**
85c40 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30 29 2e and returns 0).
85c50 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6d 61 6c .**.** Most mal
85c60 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 loc failures are
85c70 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 20 41 66 74 non-benign. Aft
85c80 65 72 20 74 68 65 79 20 6f 63 63 75 72 2c 20 53 er they occur, S
85c90 51 4c 69 74 65 0a 2a 2a 20 61 62 61 6e 64 6f 6e QLite.** abandon
85ca0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 s the current op
85cb0 65 72 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 eration and retu
85cc0 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 rns an error cod
85cd0 65 20 28 75 73 75 61 6c 6c 79 0a 2a 2a 20 53 51 e (usually.** SQ
85ce0 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20 74 6f 20 74 LITE_NOMEM) to t
85cf0 68 65 20 75 73 65 72 2e 20 48 6f 77 65 76 65 72 he user. However
85d00 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 20 66 61 , sometimes a fa
85d10 75 6c 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 ult is not neces
85d20 73 61 72 69 6c 79 0a 2a 2a 20 66 61 74 61 6c 2e sarily.** fatal.
85d30 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 For example, if
85d40 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 a malloc fails
85d50 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 61 while resizing a
85d60 20 68 61 73 68 20 74 61 62 6c 65 2c 20 74 68 69 hash table, thi
85d70 73 20 0a 2a 2a 20 69 73 20 63 6f 6d 70 6c 65 74 s .** is complet
85d80 65 6c 79 20 72 65 63 6f 76 65 72 61 62 6c 65 20 ely recoverable
85d90 73 69 6d 70 6c 79 20 62 79 20 6e 6f 74 20 63 61 simply by not ca
85da0 72 72 79 69 6e 67 20 6f 75 74 20 74 68 65 20 72 rrying out the r
85db0 65 73 69 7a 65 2e 20 54 68 65 20 0a 2a 2a 20 68 esize. The .** h
85dc0 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c 20 63 ash table will c
85dd0 6f 6e 74 69 6e 75 65 20 74 6f 20 66 75 6e 63 74 ontinue to funct
85de0 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 53 ion normally. S
85df0 6f 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 o a malloc failu
85e00 72 65 20 0a 2a 2a 20 64 75 72 69 6e 67 20 61 20 re .** during a
85e10 68 61 73 68 20 74 61 62 6c 65 20 72 65 73 69 7a hash table resiz
85e20 65 20 69 73 20 61 20 62 65 6e 69 67 6e 20 66 61 e is a benign fa
85e30 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 ult..*/...#ifnde
85e40 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
85e50 49 4c 54 49 4e 5f 54 45 53 54 0a 0a 2f 2a 0a 2a ILTIN_TEST../*.*
85e60 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c * Global variabl
85e70 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 es..*/.typedef s
85e80 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c truct BenignMall
85e90 6f 63 48 6f 6f 6b 73 20 42 65 6e 69 67 6e 4d 61 ocHooks BenignMa
85ea0 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73 74 61 74 69 llocHooks;.stati
85eb0 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 c SQLITE_WSD str
85ec0 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 uct BenignMalloc
85ed0 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f 69 64 20 28 Hooks {. void (
85ee0 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 *xBenignBegin)(v
85ef0 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 oid);. void (*x
85f00 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 BenignEnd)(void)
85f10 3b 0a 7d 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 ;.} sqlite3Hooks
85f20 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a = { 0, 0 };../*
85f30 20 54 68 65 20 22 77 73 64 48 6f 6f 6b 73 22 20 The "wsdHooks"
85f40 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c macro will resol
85f50 76 65 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 ve to the approp
85f60 72 69 61 74 65 20 42 65 6e 69 67 6e 4d 61 6c 6c riate BenignMall
85f70 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73 74 72 75 63 ocHooks.** struc
85f80 74 75 72 65 2e 20 20 49 66 20 77 72 69 74 61 62 ture. If writab
85f90 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 le static data i
85fa0 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e s unsupported on
85fb0 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 the target,.**
85fc0 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 we have to locat
85fd0 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 e the state vect
85fe0 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 or at run-time.
85ff0 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d In the more com
86000 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 mon.** case wher
86010 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 e writable stati
86020 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72 c data is suppor
86030 74 65 64 2c 20 77 73 64 48 6f 6f 6b 73 20 63 61 ted, wsdHooks ca
86040 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 n refer directly
86050 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 .** to the "sqli
86060 74 65 33 48 6f 6f 6b 73 22 20 73 74 61 74 65 20 te3Hooks" state
86070 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 vector declared
86080 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 above..*/.#ifdef
86090 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 SQLITE_OMIT_WSD
860a0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f .# define wsdHoo
860b0 6b 73 49 6e 69 74 20 5c 0a 20 20 42 65 6e 69 67 ksInit \. Benig
860c0 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20 nMallocHooks *x
860d0 3d 20 26 47 4c 4f 42 41 4c 28 42 65 6e 69 67 6e = &GLOBAL(Benign
860e0 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69 MallocHooks,sqli
860f0 74 65 33 48 6f 6f 6b 73 29 0a 23 20 64 65 66 69 te3Hooks).# defi
86100 6e 65 20 77 73 64 48 6f 6f 6b 73 20 78 5b 30 5d ne wsdHooks x[0]
86110 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
86120 77 73 64 48 6f 6f 6b 73 49 6e 69 74 0a 23 20 64 wsdHooksInit.# d
86130 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20 73 efine wsdHooks s
86140 71 6c 69 74 65 33 48 6f 6f 6b 73 0a 23 65 6e 64 qlite3Hooks.#end
86150 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 if.../*.** Regis
86160 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c ter hooks to cal
86170 6c 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 65 l when sqlite3Be
86180 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
86190 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 ) and.** sqlite3
861a0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
861b0 29 20 61 72 65 20 63 61 6c 6c 65 64 2c 20 72 65 ) are called, re
861c0 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 53 spectively..*/.S
861d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
861e0 69 64 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e id sqlite3Benign
861f0 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 0a 20 20 76 MallocHooks(. v
86200 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 oid (*xBenignBeg
86210 69 6e 29 28 76 6f 69 64 29 2c 0a 20 20 76 6f 69 in)(void),. voi
86220 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 d (*xBenignEnd)(
86230 76 6f 69 64 29 0a 29 7b 0a 20 20 77 73 64 48 6f void).){. wsdHo
86240 6f 6b 73 49 6e 69 74 3b 0a 20 20 77 73 64 48 6f oksInit;. wsdHo
86250 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e oks.xBenignBegin
86260 20 3d 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b = xBenignBegin;
86270 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e . wsdHooks.xBen
86280 69 67 6e 45 6e 64 20 3d 20 78 42 65 6e 69 67 6e ignEnd = xBenign
86290 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 End;.}../*.** Th
862a0 69 73 20 28 73 71 6c 69 74 65 33 45 6e 64 42 65 is (sqlite3EndBe
862b0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 29 20 69 73 nignMalloc()) is
862c0 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 called by SQLit
862d0 65 20 63 6f 64 65 20 74 6f 20 69 6e 64 69 63 61 e code to indica
862e0 74 65 20 74 68 61 74 0a 2a 2a 20 73 75 62 73 65 te that.** subse
862f0 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69 quent malloc fai
86300 6c 75 72 65 73 20 61 72 65 20 62 65 6e 69 67 6e lures are benign
86310 2e 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 . A call to sqli
86320 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
86330 6f 63 28 29 0a 2a 2a 20 69 6e 64 69 63 61 74 65 oc().** indicate
86340 73 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e s that subsequen
86350 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 t malloc failure
86360 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e s are non-benign
86370 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
86380 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
86390 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
863a0 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f c(void){. wsdHo
863b0 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 oksInit;. if( w
863c0 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 sdHooks.xBenignB
863d0 65 67 69 6e 20 29 7b 0a 20 20 20 20 77 73 64 48 egin ){. wsdH
863e0 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 ooks.xBenignBegi
863f0 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 n();. }.}.SQLIT
86400 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
86410 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
86420 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77 alloc(void){. w
86430 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 sdHooksInit;. i
86440 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e f( wsdHooks.xBen
86450 69 67 6e 45 6e 64 20 29 7b 0a 20 20 20 20 77 73 ignEnd ){. ws
86460 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e dHooks.xBenignEn
86470 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 d();. }.}..#end
86480 69 66 20 20 20 2f 2a 20 23 69 66 6e 64 65 66 20 if /* #ifndef
86490 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
864a0 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a TIN_TEST */../**
864b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
864c0 20 6f 66 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a of fault.c ****
864d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
864e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
864f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
86500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
86510 69 6e 20 66 69 6c 65 20 6d 65 6d 30 2e 63 20 2a in file mem0.c *
86520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
86550 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 ** 2008 October
86560 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 28.**.** The aut
86570 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
86580 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
86590 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
865a0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
865b0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
865c0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
865d0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
865e0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
865f0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
86600 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
86610 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
86620 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
86630 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
86640 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
86650 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
86660 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
86670 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
86680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
866a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
866b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
866c0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
866d0 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 6e 6f 2d e contains a no-
866e0 6f 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 op memory alloca
866f0 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72 tion drivers for
86700 20 75 73 65 20 77 68 65 6e 0a 2a 2a 20 53 51 4c use when.** SQL
86710 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 ITE_ZERO_MALLOC
86720 69 73 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65 is defined. The
86730 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 allocation driv
86740 65 72 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a ers implemented.
86750 2a 2a 20 68 65 72 65 20 61 6c 77 61 79 73 20 66 ** here always f
86760 61 69 6c 2e 20 20 53 51 4c 69 74 65 20 77 69 6c ail. SQLite wil
86770 6c 20 6e 6f 74 20 6f 70 65 72 61 74 65 20 77 69 l not operate wi
86780 74 68 20 74 68 65 73 65 20 64 72 69 76 65 72 73 th these drivers
86790 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 . These.** are
867a0 6d 65 72 65 6c 79 20 70 6c 61 63 65 68 6f 6c 64 merely placehold
867b0 65 72 73 2e 20 20 52 65 61 6c 20 64 72 69 76 65 ers. Real drive
867c0 72 73 20 6d 75 73 74 20 62 65 20 73 75 62 73 74 rs must be subst
867d0 69 74 75 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 ituted using.**
867e0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
867f0 20 62 65 66 6f 72 65 20 53 51 4c 69 74 65 20 77 before SQLite w
86800 69 6c 6c 20 6f 70 65 72 61 74 65 2e 0a 2a 2a 0a ill operate..**.
86810 2a 2a 20 24 49 64 3a 20 6d 65 6d 30 2e 63 2c 76 ** $Id: mem0.c,v
86820 20 31 2e 31 20 32 30 30 38 2f 31 30 2f 32 38 20 1.1 2008/10/28
86830 31 38 3a 35 38 3a 32 30 20 64 72 68 20 45 78 70 18:58:20 drh Exp
86840 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 $.*/../*.** Thi
86850 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 s version of the
86860 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
86870 72 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 r is the default
86880 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 . It is.** used
86890 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d when no other m
868a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
868b0 69 73 20 73 70 65 63 69 66 69 65 64 20 75 73 69 is specified usi
868c0 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a ng compile-time.
868d0 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 ** macros..*/.#i
868e0 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 52 4f fdef SQLITE_ZERO
868f0 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e _MALLOC../*.** N
86900 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f 66 o-op versions of
86910 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f all memory allo
86920 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 0a cation routines.
86930 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
86940 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
86950 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74 (int nByte){ ret
86960 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 urn 0; }.static
86970 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 void sqlite3MemF
86980 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
86990 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 ){ return; }.sta
869a0 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 tic void *sqlite
869b0 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 3MemRealloc(void
869c0 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 *pPrior, int nB
869d0 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 yte){ return 0;
869e0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c }.static int sql
869f0 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 ite3MemSize(void
86a00 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72 *pPrior){ retur
86a10 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e n 0; }.static in
86a20 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e t sqlite3MemRoun
86a30 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65 74 75 dup(int n){ retu
86a40 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69 rn n; }.static i
86a50 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 nt sqlite3MemIni
86a60 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
86a70 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
86a80 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 OK; }.static voi
86a90 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 d sqlite3MemShut
86aa0 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 down(void *NotUs
86ab0 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a ed){ return; }..
86ac0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
86ad0 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 ne is the only r
86ae0 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 outine in this f
86af0 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 ile with externa
86b00 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a l linkage..**.**
86b10 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f Populate the lo
86b20 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
86b30 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
86b40 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a on pointers in.*
86b50 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 * sqlite3GlobalC
86b60 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 onfig.m with poi
86b70 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 nters to the rou
86b80 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
86b90 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
86ba0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
86bb0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
86bc0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
86bd0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
86be0 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c m_methods defaul
86bf0 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 tMethods = {.
86c00 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c sqlite3MemMall
86c10 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 oc,. sqlite3
86c20 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 MemFree,. sq
86c30 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c lite3MemRealloc,
86c40 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
86c50 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 Size,. sqlit
86c60 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 e3MemRoundup,.
86c70 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 sqlite3MemIni
86c80 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d t,. sqlite3M
86c90 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 emShutdown,.
86ca0 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 0. };. sqlite
86cb0 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
86cc0 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 CONFIG_MALLOC, &
86cd0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b defaultMethods);
86ce0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
86cf0 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 LITE_ZERO_MALLOC
86d00 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
86d10 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 30 **** End of mem0
86d20 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
86d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86d50 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
86d60 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
86d70 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem1.c *********
86d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86da0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
86db0 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 August 14.**.**
86dc0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
86dd0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
86de0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
86df0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
86e00 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
86e10 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
86e20 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
86e30 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
86e40 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
86e50 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
86e60 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
86e70 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
86e80 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
86e90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
86ea0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
86eb0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
86ec0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
86ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86f10 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
86f20 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
86f30 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f s low-level memo
86f40 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 ry allocation dr
86f50 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a ivers for when.*
86f60 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 * SQLite will us
86f70 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 e the standard C
86f80 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f -library malloc/
86f90 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74 realloc/free int
86fa0 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 erface.** to obt
86fb0 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 ain the memory i
86fc0 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 t needs..**.** T
86fd0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
86fe0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
86ff0 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76 s of the low-lev
87000 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 el memory alloca
87010 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 tion.** routines
87020 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 specified in th
87030 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 e sqlite3_mem_me
87040 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a thods object..**
87050 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 31 2e 63 2c .** $Id: mem1.c,
87060 76 20 31 2e 32 38 20 32 30 30 38 2f 31 31 2f 31 v 1.28 2008/11/1
87070 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65 9 09:05:27 danie
87080 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
87090 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 ./*.** This vers
870a0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
870b0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 y allocator is t
870c0 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20 he default. It
870d0 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 is.** used when
870e0 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 no other memory
870f0 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65 allocator is spe
87100 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d cified using com
87110 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 pile-time.** mac
87120 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ros..*/.#ifdef S
87130 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
87140 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 LOC../*.** Like
87150 6d 61 6c 6c 6f 63 28 29 2c 20 62 75 74 20 72 65 malloc(), but re
87160 6d 65 6d 62 65 72 20 74 68 65 20 73 69 7a 65 20 member the size
87170 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f of the allocatio
87180 6e 0a 2a 2a 20 73 6f 20 74 68 61 74 20 77 65 20 n.** so that we
87190 63 61 6e 20 66 69 6e 64 20 69 74 20 6c 61 74 65 can find it late
871a0 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d r using sqlite3M
871b0 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 emSize()..**.**
871c0 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 For this low-lev
871d0 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 el routine, we a
871e0 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 re guaranteed th
871f0 61 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75 at nByte>0 becau
87200 73 65 0a 2a 2a 20 63 61 73 65 73 20 6f 66 20 6e se.** cases of n
87210 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 62 65 20 Byte<=0 will be
87220 69 6e 74 65 72 63 65 70 74 65 64 20 61 6e 64 20 intercepted and
87230 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 68 69 dealt with by hi
87240 67 68 65 72 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f gher level.** ro
87250 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 utines..*/.stati
87260 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d c void *sqlite3M
87270 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 emMalloc(int nBy
87280 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 te){. sqlite3_i
87290 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72 nt64 *p;. asser
872a0 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 t( nByte>0 );.
872b0 6e 42 79 74 65 20 3d 20 28 6e 42 79 74 65 2b 37 nByte = (nByte+7
872c0 29 26 7e 37 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c )&~7;. p = mall
872d0 6f 63 28 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 oc( nByte+8 );.
872e0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 5b if( p ){. p[
872f0 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 0] = nByte;.
87300 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 p++;. }. retur
87310 6e 20 28 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a n (void *)p;.}..
87320 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 66 72 65 65 28 /*.** Like free(
87330 29 20 62 75 74 20 77 6f 72 6b 73 20 66 6f 72 20 ) but works for
87340 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 62 74 61 allocations obta
87350 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
87360 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 3MemMalloc().**
87370 6f 72 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 or sqlite3MemRea
87380 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f lloc()..**.** Fo
87390 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c r this low-level
873a0 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 6c 72 routine, we alr
873b0 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 70 eady know that p
873c0 50 72 69 6f 72 21 3d 30 20 73 69 6e 63 65 0a 2a Prior!=0 since.*
873d0 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 70 50 * cases where pP
873e0 72 69 6f 72 3d 3d 30 20 77 69 6c 6c 20 68 61 76 rior==0 will hav
873f0 65 20 62 65 65 6e 20 69 6e 74 65 63 65 70 74 65 e been intecepte
87400 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68 d and dealt with
87410 0a 2a 2a 20 62 79 20 68 69 67 68 65 72 2d 6c 65 .** by higher-le
87420 76 65 6c 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f vel routines..*/
87430 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
87440 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 ite3MemFree(void
87450 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c *pPrior){. sql
87460 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 ite3_int64 *p =
87470 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
87480 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 pPrior;. assert
87490 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 ( pPrior!=0 );.
874a0 20 70 2d 2d 3b 0a 20 20 66 72 65 65 28 70 29 3b p--;. free(p);
874b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72 .}../*.** Like r
874c0 65 61 6c 6c 6f 63 28 29 2e 20 20 52 65 73 69 7a ealloc(). Resiz
874d0 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 e an allocation
874e0 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 previously obtai
874f0 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 ned from.** sqli
87500 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a te3MemMalloc()..
87510 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c **.** For this l
87520 6f 77 2d 6c 65 76 65 6c 20 69 6e 74 65 72 66 61 ow-level interfa
87530 63 65 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 ce, we know that
87540 20 70 50 72 69 6f 72 21 3d 30 2e 20 20 43 61 73 pPrior!=0. Cas
87550 65 73 20 77 68 65 72 65 0a 2a 2a 20 70 50 72 69 es where.** pPri
87560 6f 72 3d 3d 30 20 77 68 69 6c 65 20 68 61 76 65 or==0 while have
87570 20 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 been intercepte
87580 64 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 d by higher-leve
87590 6c 20 72 6f 75 74 69 6e 65 20 61 6e 64 0a 2a 2a l routine and.**
875a0 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 redirected to x
875b0 4d 61 6c 6c 6f 63 2e 20 20 53 69 6d 69 6c 61 72 Malloc. Similar
875c0 6c 79 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 ly, we know that
875d0 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75 73 65 nByte>0 because
875e0 73 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 s.** cases where
875f0 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 68 nByte<=0 will h
87600 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 63 65 ave been interce
87610 70 74 65 64 20 62 79 20 68 69 67 68 65 72 2d 6c pted by higher-l
87620 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 evel.** routines
87630 20 61 6e 64 20 72 65 64 69 72 65 63 74 65 64 20 and redirected
87640 74 6f 20 78 46 72 65 65 2e 0a 2a 2f 0a 73 74 61 to xFree..*/.sta
87650 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 tic void *sqlite
87660 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 3MemRealloc(void
87670 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 *pPrior, int nB
87680 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f yte){. sqlite3_
87690 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 int64 *p = (sqli
876a0 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f te3_int64*)pPrio
876b0 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 r;. assert( pPr
876c0 69 6f 72 21 3d 30 20 26 26 20 6e 42 79 74 65 3e ior!=0 && nByte>
876d0 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 28 0 );. nByte = (
876e0 6e 42 79 74 65 2b 37 29 26 7e 37 3b 0a 20 20 70 nByte+7)&~7;. p
876f0 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 = (sqlite3_int6
87700 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 4*)pPrior;. p--
87710 3b 0a 20 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 ;. p = realloc(
87720 70 2c 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 p, nByte+8 );.
87730 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 if( p ){. p[0
87740 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 ] = nByte;. p
87750 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ++;. }. return
87760 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a (void*)p;.}../*
87770 0a 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20 61 .** Report the a
87780 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 llocated size of
87790 20 61 20 70 72 69 6f 72 20 72 65 74 75 72 6e 20 a prior return
877a0 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a from xMalloc().*
877b0 2a 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e * or xRealloc().
877c0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
877d0 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f qlite3MemSize(vo
877e0 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 id *pPrior){. s
877f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b qlite3_int64 *p;
87800 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 . if( pPrior==0
87810 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 ) return 0;. p
87820 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 = (sqlite3_int6
87830 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 4*)pPrior;. p--
87840 3b 0a 20 20 72 65 74 75 72 6e 20 70 5b 30 5d 3b ;. return p[0];
87850 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 .}../*.** Round
87860 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a up a request siz
87870 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 e to the next va
87880 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 lid allocation s
87890 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ize..*/.static i
878a0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 nt sqlite3MemRou
878b0 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 ndup(int n){. r
878c0 65 74 75 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37 eturn (n+7) & ~7
878d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 ;.}../*.** Initi
878e0 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c alize this modul
878f0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
87900 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 sqlite3MemInit(
87910 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
87920 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
87930 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
87940 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
87950 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 .}../*.** Deinit
87960 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 ialize this modu
87970 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
87980 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 id sqlite3MemShu
87990 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
879a0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
879b0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
879c0 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a );. return;.}..
879d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
879e0 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 ne is the only r
879f0 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 outine in this f
87a00 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 ile with externa
87a10 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a l linkage..**.**
87a20 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f Populate the lo
87a30 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
87a40 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
87a50 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a on pointers in.*
87a60 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 * sqlite3GlobalC
87a70 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 onfig.m with poi
87a80 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 nters to the rou
87a90 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
87aa0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
87ab0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
87ac0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
87ad0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
87ae0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
87af0 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c m_methods defaul
87b00 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 tMethods = {.
87b10 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c sqlite3MemMall
87b20 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 oc,. sqlite3
87b30 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 MemFree,. sq
87b40 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c lite3MemRealloc,
87b50 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
87b60 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 Size,. sqlit
87b70 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 e3MemRoundup,.
87b80 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 sqlite3MemIni
87b90 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d t,. sqlite3M
87ba0 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 emShutdown,.
87bb0 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 0. };. sqlite
87bc0 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
87bd0 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 CONFIG_MALLOC, &
87be0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b defaultMethods);
87bf0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
87c00 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c LITE_SYSTEM_MALL
87c10 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a OC */../********
87c20 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
87c30 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m1.c ***********
87c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87c60 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
87c70 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
87c80 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a e mem2.c *******
87c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87cb0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
87cc0 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a 7 August 15.**.*
87cd0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
87ce0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
87cf0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
87d00 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
87d10 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
87d20 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
87d30 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
87d40 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
87d50 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
87d60 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
87d70 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
87d80 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
87d90 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
87da0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
87db0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
87dc0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
87dd0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
87de0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
87df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
87e30 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
87e40 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 ins low-level me
87e50 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
87e60 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e drivers for when
87e70 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 .** SQLite will
87e80 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 use the standard
87e90 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f C-library mallo
87ea0 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 c/realloc/free i
87eb0 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f nterface.** to o
87ec0 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 btain the memory
87ed0 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20 it needs while
87ee0 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61 adding lots of a
87ef0 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67 dditional debugg
87f00 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 ing.** informati
87f10 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63 on to each alloc
87f20 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74 ation in order t
87f30 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e o help detect an
87f40 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20 d fix memory.**
87f50 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79 leaks and memory
87f60 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a usage errors..*
87f70 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
87f80 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e ontains implemen
87f90 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c tations of the l
87fa0 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
87fb0 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f allocation.** ro
87fc0 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 utines specified
87fd0 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f in the sqlite3_
87fe0 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 mem_methods obje
87ff0 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d ct..**.** $Id: m
88000 65 6d 32 2e 63 2c 76 20 31 2e 34 30 20 32 30 30 em2.c,v 1.40 200
88010 38 2f 31 30 2f 32 38 20 31 38 3a 35 38 3a 32 30 8/10/28 18:58:20
88020 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f drh Exp $.*/../
88030 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f *.** This versio
88040 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 n of the memory
88050 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 allocator is use
88060 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a d only if the.**
88070 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 SQLITE_MEMDEBUG
88080 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 macro is define
88090 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 d.*/.#ifdef SQLI
880a0 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a 0a TE_MEMDEBUG../*.
880b0 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63 65 ** The backtrace
880c0 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 functionality i
880d0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
880e0 20 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a 23 with GLIBC.*/.#
880f0 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f 0a ifdef __GLIBC__.
88100 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61 63 extern int bac
88110 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 6e ktrace(void**,in
88120 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f 69 t);. extern voi
88130 64 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 d backtrace_symb
88140 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e 73 ols_fd(void*cons
88150 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 6c t*,int,int);.#el
88160 73 65 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b se.# define back
88170 74 72 61 63 65 28 41 2c 42 29 20 31 0a 23 20 64 trace(A,B) 1.# d
88180 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 5f efine backtrace_
88190 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c 43 symbols_fd(A,B,C
881a0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
881b0 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f Each memory allo
881c0 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c 69 6b cation looks lik
881d0 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 2d e this:.**.** -
881e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
881f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
88200 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
88210 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
88220 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 54 69 -------.** | Ti
88230 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 61 63 65 tle | backtrace
88240 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d 65 6d pointers | Mem
88250 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c 6c 6f BlockHdr | allo
88260 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47 75 61 cation | EndGua
88270 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d rd |.** -------
88280 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
88290 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
882a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
882b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
882c0 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c -.**.** The appl
882d0 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 73 65 65 ication code see
882e0 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72 s only a pointer
882f0 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 to the allocati
88300 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a 2a 20 on. We have.**
88310 74 6f 20 62 61 63 6b 20 75 70 20 66 72 6f 6d 20 to back up from
88320 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 the allocation p
88330 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e 64 20 74 ointer to find t
88340 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 20 he MemBlockHdr.
88350 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b The.** MemBlock
88360 48 64 72 20 74 65 6c 6c 73 20 75 73 20 74 68 65 Hdr tells us the
88370 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c size of the all
88380 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 68 65 20 ocation and the
88390 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 61 63 number of.** bac
883a0 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 2e ktrace pointers.
883b0 20 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 There is also
883c0 61 20 67 75 61 72 64 20 77 6f 72 64 20 61 74 20 a guard word at
883d0 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a the end of the.*
883e0 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0a 2a * MemBlockHdr..*
883f0 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 /.struct MemBloc
88400 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 69 53 69 kHdr {. i64 iSi
88410 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
88420 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
88430 53 69 7a 65 20 6f 66 20 74 68 69 73 20 61 6c 6c Size of this all
88440 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 ocation */. str
88450 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 uct MemBlockHdr
88460 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 *pNext, *pPrev;
88470 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 /* Linked list
88480 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d of all unfreed m
88490 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61 72 20 emory */. char
884a0 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 20 20 20 nBacktrace;
884b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
884c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b * Number of back
884d0 74 72 61 63 65 73 20 6f 6e 20 74 68 69 73 20 61 traces on this a
884e0 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 6e lloc */. char n
884f0 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 20 BacktraceSlots;
88500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
88510 20 41 76 61 69 6c 61 62 6c 65 20 62 61 63 6b 74 Available backt
88520 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a 20 20 race slots */.
88530 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20 20 20 short nTitle;
88540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
88550 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 /* Bytes of
88560 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65 73 20 title; includes
88570 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20 69 46 '\0' */. int iF
88580 6f 72 65 47 75 61 72 64 3b 20 20 20 20 20 20 20 oreGuard;
88590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
885a0 20 47 75 61 72 64 20 77 6f 72 64 20 66 6f 72 20 Guard word for
885b0 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a sanity */.};../*
885c0 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 64 73 0a .** Guard words.
885d0 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52 45 47 */.#define FOREG
885e0 55 41 52 44 20 30 78 38 30 46 35 45 31 35 33 0a UARD 0x80F5E153.
885f0 23 64 65 66 69 6e 65 20 52 45 41 52 47 55 41 52 #define REARGUAR
88600 44 20 30 78 45 34 36 37 36 42 35 33 0a 0a 2f 2a D 0xE4676B53../*
88610 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 .** Number of ma
88620 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72 65 6d lloc size increm
88630 65 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e 0a 2a ents to track..*
88640 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 49 5a 45 /.#define NCSIZE
88650 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6c 1000../*.** Al
88660 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 l of the static
88670 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 variables used b
88680 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 y this module ar
88690 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 e collected.** i
886a0 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 nto a single str
886b0 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 ucture named "me
886c0 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 m". This is to
886d0 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 keep the.** stat
886e0 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 ic variables org
886f0 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 anized and to re
88700 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 duce namespace p
88710 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e ollution.** when
88720 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 this module is
88730 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 combined with ot
88740 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 her in the amalg
88750 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 amation..*/.stat
88760 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20 0a 20 ic struct {. .
88770 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 /*. ** Mutex t
88780 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
88790 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 to the memory a
887a0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
887b0 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 tem.. */. sqli
887c0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
887d0 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 65 61 ;.. /*. ** Hea
887e0 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 20 61 20 d and tail of a
887f0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 linked list of a
88800 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 ll outstanding a
88810 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2f 0a llocations. */.
88820 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
88830 6b 48 64 72 20 2a 70 46 69 72 73 74 3b 0a 20 20 kHdr *pFirst;.
88840 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
88850 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a 20 20 dr *pLast;. .
88860 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75 6d 62 /*. ** The numb
88870 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 er of levels of
88880 62 61 63 6b 74 72 61 63 65 20 74 6f 20 73 61 76 backtrace to sav
88890 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 61 74 e in new allocat
888a0 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 ions.. */. int
888b0 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 76 nBacktrace;. v
888c0 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 63 65 oid (*xBacktrace
888d0 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 )(int, int, void
888e0 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a **);.. /*. **
888f0 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f 20 69 Title text to i
88900 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74 20 6f nsert in front o
88910 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20 20 2a f each block. *
88920 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65 3b 20 /. int nTitle;
88930 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
88940 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 73 61 76 of zTitle to sav
88950 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 27 5c 30 e. Includes '\0
88960 27 20 61 6e 64 20 70 61 64 64 69 6e 67 20 2a 2f ' and padding */
88970 0a 20 20 63 68 61 72 20 7a 54 69 74 6c 65 5b 31 . char zTitle[1
88980 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74 69 74 00]; /* The tit
88990 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20 2f 2a le text */.. /*
889a0 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 . ** sqlite3Ma
889b0 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 20 69 llocDisallow() i
889c0 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20 66 6f ncrements the fo
889d0 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65 72 2e llowing counter.
889e0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c . ** sqlite3Mal
889f0 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63 72 65 locAllow() decre
88a00 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f 0a 20 ments it.. */.
88a10 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b 20 2f int disallow; /
88a20 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 6d * Do not allow m
88a30 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
88a40 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 47 */.. /*. ** G
88a50 61 74 68 65 72 20 73 74 61 74 69 73 74 69 63 73 ather statistics
88a60 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 20 6f 66 on the sizes of
88a70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
88a80 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c 6f 63 ons.. ** nAlloc
88a90 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 [i] is the numbe
88aa0 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 r of allocation
88ab0 61 74 74 65 6d 70 74 73 20 6f 66 20 69 2a 38 0a attempts of i*8.
88ac0 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 69 3d 3d ** bytes. i==
88ad0 4e 43 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 NCSIZE is the nu
88ae0 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 mber of allocati
88af0 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 6f 72 0a on attempts for.
88b00 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 65 20 ** sizes more
88b10 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20 62 79 than NCSIZE*8 by
88b20 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 tes.. */. int
88b30 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d 3b 20 nAlloc[NCSIZE];
88b40 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
88b50 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 mber of allocati
88b60 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 ons */. int nCu
88b70 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 rrent[NCSIZE];
88b80 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d /* Current num
88b90 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f ber of allocatio
88ba0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 75 ns */. int mxCu
88bb0 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20 rrent[NCSIZE];
88bc0 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61 /* Highwater ma
88bd0 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e 74 20 rk for nCurrent
88be0 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f 2a 0a */..} mem;.../*.
88bf0 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f 72 79 ** Adjust memory
88c00 20 75 73 61 67 65 20 73 74 61 74 69 73 74 69 63 usage statistic
88c10 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 s.*/.static void
88c20 20 61 64 6a 75 73 74 53 74 61 74 73 28 69 6e 74 adjustStats(int
88c30 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e 63 72 iSize, int incr
88c40 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 69 20 ement){. int i
88c50 3d 20 28 28 69 53 69 7a 65 2b 37 29 26 7e 37 29 = ((iSize+7)&~7)
88c60 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53 49 /8;. if( i>NCSI
88c70 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d 20 ZE-1 ){. i =
88c80 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d 0a NCSIZE - 1;. }.
88c90 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74 3e if( increment>
88ca0 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 6c 0 ){. mem.nAl
88cb0 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d 65 loc[i]++;. me
88cc0 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b 3b m.nCurrent[i]++;
88cd0 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 75 . if( mem.nCu
88ce0 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 43 rrent[i]>mem.mxC
88cf0 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 20 urrent[i] ){.
88d00 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 mem.mxCurrent
88d10 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 65 [i] = mem.nCurre
88d20 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d nt[i];. }. }
88d30 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e 43 else{. mem.nC
88d40 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 20 urrent[i]--;.
88d50 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43 75 assert( mem.nCu
88d60 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a 20 rrent[i]>=0 );.
88d70 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 }.}../*.** Give
88d80 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c n an allocation,
88d90 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f find the MemBlo
88da0 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20 61 ckHdr for that a
88db0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a llocation..**.**
88dc0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
88dd0 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 73 20 ecks the guards
88de0 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f 66 at either end of
88df0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
88e00 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 and.** if they a
88e10 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74 20 re incorrect it
88e20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 74 asserts..*/.stat
88e30 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f ic struct MemBlo
88e40 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d 65 ckHdr *sqlite3Me
88e50 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 76 6f msysGetHeader(vo
88e60 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e 29 id *pAllocation)
88e70 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
88e80 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e 74 ockHdr *p;. int
88e90 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70 55 *pInt;. u8 *pU
88ea0 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 8;. int nReserv
88eb0 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75 63 e;.. p = (struc
88ec0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 70 t MemBlockHdr*)p
88ed0 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 2d Allocation;. p-
88ee0 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e -;. assert( p->
88ef0 69 46 6f 72 65 47 75 61 72 64 3d 3d 46 4f 52 45 iForeGuard==FORE
88f00 47 55 41 52 44 20 29 3b 0a 20 20 6e 52 65 73 65 GUARD );. nRese
88f10 72 76 65 20 3d 20 28 70 2d 3e 69 53 69 7a 65 2b rve = (p->iSize+
88f20 37 29 26 7e 37 3b 0a 20 20 70 49 6e 74 20 3d 20 7)&~7;. pInt =
88f30 28 69 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f (int*)pAllocatio
88f40 6e 3b 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29 n;. pU8 = (u8*)
88f50 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 pAllocation;. a
88f60 73 73 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 ssert( pInt[nRes
88f70 65 72 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 erve/sizeof(int)
88f80 5d 3d 3d 52 45 41 52 47 55 41 52 44 20 29 3b 0a ]==REARGUARD );.
88f90 20 20 61 73 73 65 72 74 28 20 28 6e 52 65 73 65 assert( (nRese
88fa0 72 76 65 2d 30 29 3c 3d 70 2d 3e 69 53 69 7a 65 rve-0)<=p->iSize
88fb0 20 7c 7c 20 70 55 38 5b 6e 52 65 73 65 72 76 65 || pU8[nReserve
88fc0 2d 31 5d 3d 3d 30 78 36 35 20 29 3b 0a 20 20 61 -1]==0x65 );. a
88fd0 73 73 65 72 74 28 20 28 6e 52 65 73 65 72 76 65 ssert( (nReserve
88fe0 2d 31 29 3c 3d 70 2d 3e 69 53 69 7a 65 20 7c 7c -1)<=p->iSize ||
88ff0 20 70 55 38 5b 6e 52 65 73 65 72 76 65 2d 32 5d pU8[nReserve-2]
89000 3d 3d 30 78 36 35 20 29 3b 0a 20 20 61 73 73 65 ==0x65 );. asse
89010 72 74 28 20 28 6e 52 65 73 65 72 76 65 2d 32 29 rt( (nReserve-2)
89020 3c 3d 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 70 55 <=p->iSize || pU
89030 38 5b 6e 52 65 73 65 72 76 65 2d 33 5d 3d 3d 30 8[nReserve-3]==0
89040 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 x65 );. return
89050 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 p;.}../*.** Retu
89060 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
89070 20 62 79 74 65 73 20 63 75 72 72 65 6e 74 6c 79 bytes currently
89080 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61 64 allocated at ad
89090 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 61 74 dress p..*/.stat
890a0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
890b0 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a mSize(void *p){.
890c0 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
890d0 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 69 66 kHdr *pHdr;. if
890e0 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 ( !p ){. retu
890f0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 64 72 rn 0;. }. pHdr
89100 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 = sqlite3Memsys
89110 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20 20 GetHeader(p);.
89120 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 53 69 return pHdr->iSi
89130 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 ze;.}../*.** Ini
89140 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f tialize the memo
89150 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
89160 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 bsystem..*/.stat
89170 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
89180 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 mInit(void *NotU
89190 73 65 64 29 7b 0a 20 20 69 66 28 20 21 73 71 6c sed){. if( !sql
891a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
891b0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 .bMemstat ){.
891c0 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 /* If memory st
891d0 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c atus is enabled,
891e0 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 then the malloc
891f0 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20 .c wrapper will
89200 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 already. ** h
89210 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d old the STATIC_M
89220 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68 EM mutex when th
89230 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20 e routines here
89240 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a are invoked. */.
89250 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 mem.mutex =
89260 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
89270 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
89280 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a TATIC_MEM);. }.
89290 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
892a0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 OK;.}../*.** Dei
892b0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
892c0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
892d0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
892e0 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
892f0 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 3MemShutdown(voi
89300 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 6d d *NotUsed){. m
89310 65 6d 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a em.mutex = 0;.}.
89320 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 ./*.** Round up
89330 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 a request size t
89340 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 o the next valid
89350 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
89360 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
89370 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 sqlite3MemRoundu
89380 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 p(int n){. retu
89390 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37 3b 0a 7d rn (n+7) & ~7;.}
893a0 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
893b0 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 nByte bytes of
893c0 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 memory..*/.stati
893d0 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d c void *sqlite3M
893e0 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 emMalloc(int nBy
893f0 74 65 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 te){. struct Me
89400 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b mBlockHdr *pHdr;
89410 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 . void **pBt;.
89420 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 char *z;. int
89430 2a 70 49 6e 74 3b 0a 20 20 76 6f 69 64 20 2a 70 *pInt;. void *p
89440 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 6f 74 61 = 0;. int tota
89450 6c 53 69 7a 65 3b 0a 20 20 69 6e 74 20 6e 52 65 lSize;. int nRe
89460 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 74 65 33 serve;. sqlite3
89470 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
89480 2e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 .mutex);. asser
89490 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d t( mem.disallow=
894a0 3d 30 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 =0 );. nReserve
894b0 20 3d 20 28 6e 42 79 74 65 2b 37 29 26 7e 37 3b = (nByte+7)&~7;
894c0 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 3d 20 6e . totalSize = n
894d0 52 65 73 65 72 76 65 20 2b 20 73 69 7a 65 6f 66 Reserve + sizeof
894e0 28 2a 70 48 64 72 29 20 2b 20 73 69 7a 65 6f 66 (*pHdr) + sizeof
894f0 28 69 6e 74 29 20 2b 0a 20 20 20 20 20 20 20 20 (int) +.
89500 20 20 20 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b mem.nBack
89510 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 trace*sizeof(voi
89520 64 2a 29 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 d*) + mem.nTitle
89530 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 ;. p = malloc(t
89540 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 69 66 28 otalSize);. if(
89550 20 70 20 29 7b 0a 20 20 20 20 7a 20 3d 20 70 3b p ){. z = p;
89560 0a 20 20 20 20 70 42 74 20 3d 20 28 76 6f 69 64 . pBt = (void
89570 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 **)&z[mem.nTitle
89580 5d 3b 0a 20 20 20 20 70 48 64 72 20 3d 20 28 73 ];. pHdr = (s
89590 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 truct MemBlockHd
895a0 72 2a 29 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 r*)&pBt[mem.nBac
895b0 6b 74 72 61 63 65 5d 3b 0a 20 20 20 20 70 48 64 ktrace];. pHd
895c0 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 r->pNext = 0;.
895d0 20 20 70 48 64 72 2d 3e 70 50 72 65 76 20 3d 20 pHdr->pPrev =
895e0 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20 20 20 69 mem.pLast;. i
895f0 66 28 20 6d 65 6d 2e 70 4c 61 73 74 20 29 7b 0a f( mem.pLast ){.
89600 20 20 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 2d mem.pLast-
89610 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 3b 0a 20 >pNext = pHdr;.
89620 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
89630 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 70 48 64 mem.pFirst = pHd
89640 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d r;. }. mem
89650 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 3b 0a 20 .pLast = pHdr;.
89660 20 20 20 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 pHdr->iForeGu
89670 61 72 64 20 3d 20 46 4f 52 45 47 55 41 52 44 3b ard = FOREGUARD;
89680 0a 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b . pHdr->nBack
89690 74 72 61 63 65 53 6c 6f 74 73 20 3d 20 6d 65 6d traceSlots = mem
896a0 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 20 .nBacktrace;.
896b0 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 pHdr->nTitle =
896c0 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 mem.nTitle;.
896d0 69 66 28 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 if( mem.nBacktra
896e0 63 65 20 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 ce ){. void
896f0 20 2a 61 41 64 64 72 5b 34 30 5d 3b 0a 20 20 20 *aAddr[40];.
89700 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 pHdr->nBacktr
89710 61 63 65 20 3d 20 62 61 63 6b 74 72 61 63 65 28 ace = backtrace(
89720 61 41 64 64 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b aAddr, mem.nBack
89730 74 72 61 63 65 2b 31 29 2d 31 3b 0a 20 20 20 20 trace+1)-1;.
89740 20 20 6d 65 6d 63 70 79 28 70 42 74 2c 20 26 61 memcpy(pBt, &a
89750 41 64 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e Addr[1], pHdr->n
89760 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 Backtrace*sizeof
89770 28 76 6f 69 64 2a 29 29 3b 0a 20 20 20 20 20 20 (void*));.
89780 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 if( mem.xBacktra
89790 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 ce ){. me
897a0 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e 42 79 m.xBacktrace(nBy
897b0 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 te, pHdr->nBackt
897c0 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72 5b 31 race-1, &aAddr[1
897d0 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ]);. }.
897e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 48 64 }else{. pHd
897f0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 r->nBacktrace =
89800 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 0;. }. if(
89810 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b 0a 20 mem.nTitle ){.
89820 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 6d memcpy(z, m
89830 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e em.zTitle, mem.n
89840 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 Title);. }.
89850 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 3d 20 pHdr->iSize =
89860 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a 75 73 nByte;. adjus
89870 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20 2b 31 tStats(nByte, +1
89880 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20 28 69 );. pInt = (i
89890 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a 20 20 nt*)&pHdr[1];.
898a0 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f pInt[nReserve/
898b0 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d 20 52 sizeof(int)] = R
898c0 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20 6d 65 EARGUARD;. me
898d0 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 36 35 2c mset(pInt, 0x65,
898e0 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20 20 nReserve);.
898f0 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 6e 74 3b p = (void*)pInt;
89900 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
89910 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d utex_leave(mem.m
89920 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
89930 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p; .}../*.** Fre
89940 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 e memory..*/.sta
89950 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
89960 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 MemFree(void *pP
89970 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 63 74 20 rior){. struct
89980 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 MemBlockHdr *pHd
89990 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b r;. void **pBt;
899a0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 61 73 . char *z;. as
899b0 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f sert( sqlite3Glo
899c0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
899d0 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 65 78 21 at || mem.mutex!
899e0 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 3d 20 73 =0 );. pHdr = s
899f0 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 qlite3MemsysGetH
89a00 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a 20 eader(pPrior);.
89a10 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 pBt = (void**)p
89a20 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d 20 70 48 Hdr;. pBt -= pH
89a30 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c dr->nBacktraceSl
89a40 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d ots;. sqlite3_m
89a50 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d utex_enter(mem.m
89a60 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 48 64 utex);. if( pHd
89a70 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 r->pPrev ){.
89a80 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 50 assert( pHdr->pP
89a90 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 48 64 72 rev->pNext==pHdr
89aa0 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 );. pHdr->pP
89ab0 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 rev->pNext = pHd
89ac0 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 r->pNext;. }els
89ad0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d e{. assert( m
89ae0 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 64 72 20 em.pFirst==pHdr
89af0 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 );. mem.pFirs
89b00 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74 3b t = pHdr->pNext;
89b10 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 64 72 2d . }. if( pHdr-
89b20 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73 >pNext ){. as
89b30 73 65 72 74 28 20 70 48 64 72 2d 3e 70 4e 65 78 sert( pHdr->pNex
89b40 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 72 20 29 t->pPrev==pHdr )
89b50 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 ;. pHdr->pNex
89b60 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 64 72 2d t->pPrev = pHdr-
89b70 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b >pPrev;. }else{
89b80 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d . assert( mem
89b90 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 29 3b 0a .pLast==pHdr );.
89ba0 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 mem.pLast =
89bb0 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d pHdr->pPrev;. }
89bc0 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 42 . z = (char*)pB
89bd0 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e t;. z -= pHdr->
89be0 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a 75 73 74 nTitle;. adjust
89bf0 53 74 61 74 73 28 70 48 64 72 2d 3e 69 53 69 7a Stats(pHdr->iSiz
89c00 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d 73 65 74 e, -1);. memset
89c10 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a 65 6f 66 (z, 0x2b, sizeof
89c20 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e 6e 42 (void*)*pHdr->nB
89c30 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 2b 20 acktraceSlots +
89c40 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 0a sizeof(*pHdr) +.
89c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
89c60 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 2b 20 pHdr->iSize +
89c70 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20 70 48 sizeof(int) + pH
89c80 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a 20 20 66 dr->nTitle);. f
89c90 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c 69 74 65 ree(z);. sqlite
89ca0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
89cb0 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f m.mutex); .}../
89cc0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
89cd0 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 size of an exist
89ce0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ing memory alloc
89cf0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 ation..**.** For
89d00 20 74 68 69 73 20 64 65 62 75 67 67 69 6e 67 20 this debugging
89d10 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
89d20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d 61 6b 65 we *always* make
89d30 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a a copy of the.*
89d40 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 * allocation int
89d50 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 20 69 6e o a new place in
89d60 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 74 68 69 memory. In thi
89d70 73 20 77 61 79 2c 20 69 66 20 74 68 65 20 0a 2a s way, if the .*
89d80 2a 20 68 69 67 68 65 72 20 6c 65 76 65 6c 20 63 * higher level c
89d90 6f 64 65 20 69 73 20 75 73 69 6e 67 20 70 6f 69 ode is using poi
89da0 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 6c 64 20 nter to the old
89db0 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 69 allocation, it i
89dc0 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20 s .** much more
89dd0 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 61 6b 20 likely to break
89de0 61 6e 64 20 77 65 20 61 72 65 20 6d 75 63 68 20 and we are much
89df0 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 6f 20 66 more liking to f
89e00 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 ind.** the error
89e10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
89e20 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c *sqlite3MemReal
89e30 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 loc(void *pPrior
89e40 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 , int nByte){.
89e50 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
89e60 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a 20 20 76 dr *pOldHdr;. v
89e70 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 61 73 73 oid *pNew;. ass
89e80 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f ert( mem.disallo
89e90 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c 64 48 64 w==0 );. pOldHd
89ea0 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 r = sqlite3Memsy
89eb0 73 47 65 74 48 65 61 64 65 72 28 70 50 72 69 6f sGetHeader(pPrio
89ec0 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c r);. pNew = sql
89ed0 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 ite3MemMalloc(nB
89ee0 79 74 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 yte);. if( pNew
89ef0 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 ){. memcpy(p
89f00 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79 New, pPrior, nBy
89f10 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a te<pOldHdr->iSiz
89f20 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 4f 6c 64 e ? nByte : pOld
89f30 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 Hdr->iSize);.
89f40 20 69 66 28 20 6e 42 79 74 65 3e 70 4f 6c 64 48 if( nByte>pOldH
89f50 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 dr->iSize ){.
89f60 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 memset(&((cha
89f70 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48 64 72 r*)pNew)[pOldHdr
89f80 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 62 2c 20 ->iSize], 0x2b,
89f90 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 64 72 2d nByte - pOldHdr-
89fa0 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 >iSize);. }.
89fb0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 sqlite3MemFre
89fc0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 e(pPrior);. }.
89fd0 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
89fe0 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 ./*.** Populate
89ff0 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 the low-level me
8a000 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
8a010 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
8a020 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 s in.** sqlite3G
8a030 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 lobalConfig.m wi
8a040 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 th pointers to t
8a050 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 he routines in t
8a060 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c his file..*/.SQL
8a070 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
8a080 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 sqlite3MemSetDe
8a090 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 fault(void){. s
8a0a0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
8a0b0 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
8a0c0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d defaultMethods =
8a0d0 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d {. sqlite3M
8a0e0 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 emMalloc,. s
8a0f0 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 qlite3MemFree,.
8a100 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 sqlite3MemRe
8a110 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 alloc,. sqli
8a120 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 te3MemSize,.
8a130 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 sqlite3MemRound
8a140 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 up,. sqlite3
8a150 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 MemInit,. sq
8a160 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e lite3MemShutdown
8a170 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 ,. 0. };.
8a180 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 sqlite3_config(S
8a190 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c QLITE_CONFIG_MAL
8a1a0 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 LOC, &defaultMet
8a1b0 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 hods);.}../*.**
8a1c0 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f Set the number o
8a1d0 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65 76 65 f backtrace leve
8a1e0 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61 63 68 ls kept for each
8a1f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 allocation..**
8a200 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 20 A value of zero
8a210 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b 74 72 turns off backtr
8a220 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75 6d 62 acing. The numb
8a230 65 72 20 69 73 20 61 6c 77 61 79 73 20 72 6f 75 er is always rou
8a240 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20 61 20 nded.** up to a
8a250 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e 0a 2a multiple of 2..*
8a260 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
8a270 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
8a280 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 28 69 debugBacktrace(i
8a290 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69 66 28 nt depth){. if(
8a2a0 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65 70 74 depth<0 ){ dept
8a2b0 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28 20 64 h = 0; }. if( d
8a2c0 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70 74 68 epth>20 ){ depth
8a2d0 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70 74 68 = 20; }. depth
8a2e0 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30 78 66 = (depth+1)&0xf
8a2f0 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 e;. mem.nBacktr
8a300 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a ace = depth;.}..
8a310 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
8a320 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 oid sqlite3Memde
8a330 62 75 67 42 61 63 6b 74 72 61 63 65 43 61 6c 6c bugBacktraceCall
8a340 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42 61 63 back(void (*xBac
8a350 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 ktrace)(int, int
8a360 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20 20 6d , void **)){. m
8a370 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 3d 20 em.xBacktrace =
8a380 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f xBacktrace;.}../
8a390 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 74 *.** Set the tit
8a3a0 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20 73 75 le string for su
8a3b0 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 bsequent allocat
8a3c0 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
8a3d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
8a3e0 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 74 ite3MemdebugSett
8a3f0 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20 itle(const char
8a400 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 69 6e 74 20 *zTitle){. int
8a410 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 54 69 74 6c n = strlen(zTitl
8a420 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 e) + 1;. sqlite
8a430 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
8a440 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 m.mutex);. if(
8a450 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 n>=sizeof(mem.zT
8a460 69 74 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 itle) ) n = size
8a470 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 of(mem.zTitle)-1
8a480 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a ;. memcpy(mem.z
8a490 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e Title, zTitle, n
8a4a0 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b );. mem.zTitle[
8a4b0 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54 n] = 0;. mem.nT
8a4c0 69 74 6c 65 20 3d 20 28 6e 2b 37 29 26 7e 37 3b itle = (n+7)&~7;
8a4d0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
8a4e0 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 _leave(mem.mutex
8a4f0 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 );.}..SQLITE_PRI
8a500 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
8a510 33 4d 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b 3MemdebugSync(){
8a520 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
8a530 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66 ckHdr *pHdr;. f
8a540 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 or(pHdr=mem.pFir
8a550 73 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 st; pHdr; pHdr=p
8a560 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Hdr->pNext){.
8a570 20 76 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76 void **pBt = (v
8a580 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 oid**)pHdr;.
8a590 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 pBt -= pHdr->nBa
8a5a0 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 cktraceSlots;.
8a5b0 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 mem.xBacktrace
8a5c0 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48 (pHdr->iSize, pH
8a5d0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 dr->nBacktrace-1
8a5e0 2c 20 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a , &pBt[1]);. }.
8a5f0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 }../*.** Open th
8a600 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 e file indicated
8a610 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 and write a log
8a620 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 of all unfreed
8a630 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 memory .** alloc
8a640 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 ations into that
8a650 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f log..*/.SQLITE_
8a660 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
8a670 69 74 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70 ite3MemdebugDump
8a680 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 (const char *zFi
8a690 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 lename){. FILE
8a6a0 2a 6f 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d *out;. struct M
8a6b0 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 emBlockHdr *pHdr
8a6c0 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a ;. void **pBt;.
8a6d0 20 20 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d int i;. out =
8a6e0 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 fopen(zFilename
8a6f0 2c 20 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75 , "w");. if( ou
8a700 74 3d 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69 t==0 ){. fpri
8a710 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 ntf(stderr, "**
8a720 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 Unable to output
8a730 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 memory debug ou
8a740 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c tput log: %s **\
8a750 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
8a760 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d zFilenam
8a770 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a e);. return;.
8a780 20 20 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d }. for(pHdr=m
8a790 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b em.pFirst; pHdr;
8a7a0 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 pHdr=pHdr->pNex
8a7b0 74 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 t){. char *z
8a7c0 3d 20 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20 = (char*)pHdr;.
8a7d0 20 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 z -= pHdr->nB
8a7e0 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69 acktraceSlots*si
8a7f0 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48 zeof(void*) + pH
8a800 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 dr->nTitle;.
8a810 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a fprintf(out, "**
8a820 2a 2a 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74 ** %lld bytes at
8a830 20 25 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a %p from %s ****
8a840 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 \n", .
8a850 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26 pHdr->iSize, &
8a860 70 48 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e pHdr[1], pHdr->n
8a870 54 69 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f Title ? z : "???
8a880 22 29 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72 ");. if( pHdr
8a890 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a ->nBacktrace ){.
8a8a0 20 20 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74 fflush(out
8a8b0 29 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28 );. pBt = (
8a8c0 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 void**)pHdr;.
8a8d0 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e pBt -= pHdr->
8a8e0 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b nBacktraceSlots;
8a8f0 0a 20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65 . backtrace
8a900 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c _symbols_fd(pBt,
8a910 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
8a920 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b e, fileno(out));
8a930 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f . fprintf(o
8a940 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d ut, "\n");. }
8a950 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f . }. fprintf(o
8a960 75 74 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29 ut, "COUNTS:\n")
8a970 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e ;. for(i=0; i<N
8a980 43 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20 CSIZE-1; i++){.
8a990 20 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f if( mem.nAllo
8a9a0 63 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70 c[i] ){. fp
8a9b0 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 rintf(out, " %
8a9c0 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 5d: %10d %10d %1
8a9d0 30 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 0d\n", .
8a9e0 20 20 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c i*8, mem.nAl
8a9f0 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72 loc[i], mem.nCur
8aa00 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43 rent[i], mem.mxC
8aa10 75 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 urrent[i]);.
8aa20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e }. }. if( mem.
8aa30 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d nAlloc[NCSIZE-1]
8aa40 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ){. fprintf(
8aa50 6f 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31 out, " %5d: %1
8aa60 30 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 0d %10d %10d\n",
8aa70 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 43 . NC
8aa80 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41 SIZE*8-8, mem.nA
8aa90 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a lloc[NCSIZE-1],.
8aaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d mem
8aab0 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 .nCurrent[NCSIZE
8aac0 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 -1], mem.mxCurre
8aad0 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20 nt[NCSIZE-1]);.
8aae0 20 7d 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 }. fclose(out)
8aaf0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
8ab00 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
8ab10 74 69 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d times sqlite3Mem
8ab20 4d 61 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65 Malloc() has bee
8ab30 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c n called..*/.SQL
8ab40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
8ab50 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d sqlite3MemdebugM
8ab60 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20 allocCount(){.
8ab70 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f int i;. int nTo
8ab80 74 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 tal = 0;. for(i
8ab90 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b =0; i<NCSIZE; i+
8aba0 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b +){. nTotal +
8abb0 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b = mem.nAlloc[i];
8abc0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54 . }. return nT
8abd0 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 otal;.}...#endif
8abe0 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 /* SQLITE_MEMDE
8abf0 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a BUG */../*******
8ac00 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d ******* End of m
8ac10 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a em2.c **********
8ac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ac40 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
8ac50 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
8ac60 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a le mem3.c ******
8ac70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ac80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ac90 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
8aca0 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 07 October 14.**
8acb0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
8acc0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
8acd0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
8ace0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
8acf0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
8ad00 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
8ad10 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
8ad20 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
8ad30 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
8ad40 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
8ad50 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
8ad60 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
8ad70 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
8ad80 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
8ad90 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
8ada0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
8adb0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
8adc0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
8add0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ade0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8adf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ae00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
8ae10 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
8ae20 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f ns the C functio
8ae30 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e ns that implemen
8ae40 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c t a memory.** al
8ae50 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
8ae60 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 em for use by SQ
8ae70 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 Lite. .**.** Thi
8ae80 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 s version of the
8ae90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
8aea0 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 on subsystem omi
8aeb0 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 ts all.** use of
8aec0 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53 malloc(). The S
8aed0 51 4c 69 74 65 20 75 73 65 72 20 73 75 70 70 6c QLite user suppl
8aee0 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d ies a block of m
8aef0 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 emory.** before
8af00 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f calling sqlite3_
8af10 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f initialize() fro
8af20 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 m which allocati
8af30 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 ons.** are made
8af40 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 and returned by
8af50 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e the xMalloc() an
8af60 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a d xRealloc() .**
8af70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
8af80 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 . Once sqlite3_i
8af90 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 nitialize() has
8afa0 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 been called,.**
8afb0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 the amount of me
8afc0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 mory available t
8afd0 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 o SQLite is fixe
8afe0 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 d and cannot.**
8aff0 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a be changed..**.*
8b000 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
8b010 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
8b020 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
8b030 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a m is included.**
8b040 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e in the build on
8b050 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ly if SQLITE_ENA
8b060 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 BLE_MEMSYS3 is d
8b070 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 efined..**.** $I
8b080 64 3a 20 6d 65 6d 33 2e 63 2c 76 20 31 2e 32 35 d: mem3.c,v 1.25
8b090 20 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 2008/11/19 16:5
8b0a0 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 2:44 danielk1977
8b0b0 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
8b0c0 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
8b0d0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
8b0e0 63 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62 75 cator is only bu
8b0f0 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 ilt into the lib
8b100 72 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 rary.** SQLITE_E
8b110 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 NABLE_MEMSYS3 is
8b120 20 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e 69 defined. Defini
8b130 6e 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 64 ng this symbol d
8b140 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e 20 oes not.** mean
8b150 74 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 that the library
8b160 20 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d 6f will use a memo
8b170 72 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61 75 ry-pool by defau
8b180 6c 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a 2a lt, just that.**
8b190 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 it is available
8b1a0 2e 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61 6c . The mempool al
8b1b0 6c 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69 76 locator is activ
8b1c0 61 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 0a ated by calling.
8b1d0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 ** sqlite3_confi
8b1e0 67 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 g()..*/.#ifdef S
8b1f0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
8b200 53 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 SYS3../*.** Maxi
8b210 6d 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d mum size (in Mem
8b220 33 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73 3Blocks) of a "s
8b230 6d 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f 0a mall" chunk..*/.
8b240 23 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c 4c #define MX_SMALL
8b250 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 10.../*.** Numb
8b260 65 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 68 er of freelist h
8b270 61 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 65 ash slots.*/.#de
8b280 66 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 0a fine N_HASH 61.
8b290 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 ./*.** A memory
8b2a0 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 6f allocation (also
8b2b0 20 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e 6b called a "chunk
8b2c0 22 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 ") consists of t
8b2d0 77 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 62 wo or .** more b
8b2e0 6c 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63 68 locks where each
8b2f0 20 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 65 block is 8 byte
8b300 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 38 20 s. The first 8
8b310 62 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 20 bytes are .** a
8b320 68 65 61 64 65 72 20 74 68 61 74 20 69 73 20 6e header that is n
8b330 6f 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 ot returned to t
8b340 68 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 he user..**.** A
8b350 20 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72 chunk is two or
8b360 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61 more blocks tha
8b370 74 20 69 73 20 65 69 74 68 65 72 20 63 68 65 63 t is either chec
8b380 6b 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 72 ked out or.** fr
8b390 65 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 62 ee. The first b
8b3a0 6c 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20 lock has format
8b3b0 75 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69 u.hdr. u.hdr.si
8b3c0 7a 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 20 ze4x is 4 times
8b3d0 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 the.** size of t
8b3e0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e he allocation in
8b3f0 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 61 blocks if the a
8b400 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 llocation is fre
8b410 65 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 2e e..** The u.hdr.
8b420 73 69 7a 65 34 78 26 31 20 62 69 74 20 69 73 20 size4x&1 bit is
8b430 74 72 75 65 20 69 66 20 74 68 65 20 63 68 75 6e true if the chun
8b440 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 k is checked out
8b450 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 and.** false if
8b460 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f 6e the chunk is on
8b470 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 the freelist.
8b480 54 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 The u.hdr.size4x
8b490 26 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 75 &2 bit.** is tru
8b4a0 65 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 e if the previou
8b4b0 73 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b s chunk is check
8b4c0 65 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 65 ed out and false
8b4d0 20 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 if the.** previ
8b4e0 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72 65 ous chunk is fre
8b4f0 65 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70 72 e. The u.hdr.pr
8b500 65 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73 20 evSize field is
8b510 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 the size of.** t
8b520 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e he previous chun
8b530 6b 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 k in blocks if t
8b540 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e he previous chun
8b550 6b 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 66 k is on the.** f
8b560 72 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65 20 reelist. If the
8b570 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 previous chunk i
8b580 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 s checked out, t
8b590 68 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 65 hen.** u.hdr.pre
8b5a0 76 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61 72 vSize can be par
8b5b0 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f t of the data fo
8b5c0 72 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e 64 r that chunk and
8b5d0 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 62 should.** not b
8b5e0 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 e read or writte
8b5f0 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 65 n..**.** We ofte
8b600 6e 20 69 64 65 6e 74 69 66 79 20 61 20 63 68 75 n identify a chu
8b610 6e 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78 20 nk by its index
8b620 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e in mem3.aPool[].
8b630 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 69 When.** this i
8b640 73 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75 6e s done, the chun
8b650 6b 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 k index refers t
8b660 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f o the second blo
8b670 63 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68 75 ck of.** the chu
8b680 6e 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 nk. In this way
8b690 2c 20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e , the first chun
8b6a0 6b 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f k has an index o
8b6b0 66 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 f 1..** A chunk
8b6c0 69 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e 73 index of 0 means
8b6d0 20 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b 22 "no such chunk"
8b6e0 20 61 6e 64 20 69 73 20 74 68 65 20 65 71 75 69 and is the equi
8b6f0 76 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 4e valent.** of a N
8b700 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a ULL pointer..**.
8b710 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c ** The second bl
8b720 6f 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75 6e ock of free chun
8b730 6b 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 ks is of the for
8b740 6d 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a 2a m u.list. The.*
8b750 2a 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f 72 * two fields for
8b760 6d 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65 m a double-linke
8b770 64 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b 73 d list of chunks
8b780 20 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a 65 of related size
8b790 73 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 s..** Pointers t
8b7a0 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 o the head of th
8b7b0 65 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72 65 e list are store
8b7c0 64 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61 6c d in mem3.aiSmal
8b7d0 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61 6c l[] .** for smal
8b7e0 6c 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20 6d ler chunks and m
8b7f0 65 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f 72 em3.aiHash[] for
8b800 20 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e 0a larger chunks..
8b810 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 **.** The second
8b820 20 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75 6e block of a chun
8b830 6b 20 69 73 20 75 73 65 72 20 64 61 74 61 20 69 k is user data i
8b840 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 63 f the chunk is c
8b850 68 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e 20 hecked .** out.
8b860 20 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20 63 If a chunk is c
8b870 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 20 hecked out, the
8b880 75 73 65 72 20 64 61 74 61 20 6d 61 79 20 65 78 user data may ex
8b890 74 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 tend into.** the
8b8a0 20 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 u.hdr.prevSize
8b8b0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f 6c value of the fol
8b8c0 6c 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a 2f lowing chunk..*/
8b8d0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
8b8e0 4d 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42 6c Mem3Block Mem3Bl
8b8f0 6f 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 33 ock;.struct Mem3
8b900 42 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e 20 Block {. union
8b910 7b 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 {. struct {.
8b920 20 20 20 20 20 75 33 32 20 70 72 65 76 53 69 7a u32 prevSiz
8b930 65 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 e; /* Size of
8b940 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 previous chunk i
8b950 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d n Mem3Block elem
8b960 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75 33 ents */. u3
8b970 32 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 2a 2 size4x; /*
8b980 20 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 4x the size of
8b990 63 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 6e current chunk in
8b9a0 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 Mem3Block eleme
8b9b0 6e 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64 72 nts */. } hdr
8b9c0 3b 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 ;. struct {.
8b9d0 20 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20 20 u32 next;
8b9e0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
8b9f0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 mem3.aPool[] of
8ba00 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b next free chunk
8ba10 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70 72 */. u32 pr
8ba20 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 ev; /* Ind
8ba30 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c ex in mem3.aPool
8ba40 5b 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 [] of previous f
8ba50 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 ree chunk */.
8ba60 20 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b 0a } list;. } u;.
8ba70 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 };../*.** All of
8ba80 20 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69 the static vari
8ba90 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 ables used by th
8baa0 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f is module are co
8bab0 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 llected.** into
8bac0 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 a single structu
8bad0 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22 2e re named "mem3".
8bae0 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65 This is to kee
8baf0 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 p the.** static
8bb00 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 variables organi
8bb10 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 zed and to reduc
8bb20 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c e namespace poll
8bb30 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 ution.** when th
8bb40 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d is module is com
8bb50 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 bined with other
8bb60 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 in the amalgama
8bb70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
8bb80 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 SQLITE_WSD struc
8bb90 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a 20 t Mem3Global {.
8bba0 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 /*. ** Memory
8bbb0 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c available for al
8bbc0 6c 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c 20 location. nPool
8bbd0 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 is the size of t
8bbe0 68 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28 69 he array. ** (i
8bbf0 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70 6f n Mem3Blocks) po
8bc00 69 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f 6f inted to by aPoo
8bc10 6c 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a 20 l less 2.. */.
8bc20 20 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d 65 u32 nPool;. Me
8bc30 6d 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b 0a m3Block *aPool;.
8bc40 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 20 . /*. ** True
8bc50 69 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 61 if we are evalua
8bc60 74 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d ting an out-of-m
8bc70 65 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a emory callback..
8bc80 20 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 6d */. int alarm
8bc90 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 Busy;. . /*.
8bca0 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 ** Mutex to cont
8bcb0 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 rol access to th
8bcc0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
8bcd0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 ion subsystem..
8bce0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
8bcf0 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20 tex *mutex;. .
8bd00 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 6e /*. ** The min
8bd10 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66 imum amount of f
8bd20 72 65 65 20 73 70 61 63 65 20 74 68 61 74 20 77 ree space that w
8bd30 65 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 2a e have seen.. *
8bd40 2f 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 72 /. u32 mnMaster
8bd50 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d 61 ;.. /*. ** iMa
8bd60 73 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 65 ster is the inde
8bd70 78 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 x of the master
8bd80 63 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 77 chunk. Most new
8bd90 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a allocations. *
8bda0 2a 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 74 * occur off of t
8bdb0 68 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d 61 his chunk. szMa
8bdc0 73 74 65 72 20 69 73 20 74 68 65 20 73 69 7a 65 ster is the size
8bdd0 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 (in Mem3Blocks)
8bde0 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 . ** of the cur
8bdf0 72 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 4d rent master. iM
8be00 61 73 74 65 72 20 69 73 20 30 20 69 66 20 74 68 aster is 0 if th
8be10 65 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 65 ere is not maste
8be20 72 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 r chunk.. ** Th
8be30 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 69 e master chunk i
8be40 73 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 20 s not in either
8be50 74 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 20 the aiHash[] or
8be60 61 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a aiSmall[].. */.
8be70 20 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a 20 u32 iMaster;.
8be80 20 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a 0a u32 szMaster;..
8be90 20 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 20 /*. ** Array
8bea0 6f 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65 of lists of free
8beb0 20 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e blocks accordin
8bec0 67 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73 g to the block s
8bed0 69 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 6d ize . ** for sm
8bee0 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72 aller chunks, or
8bef0 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62 a hash on the b
8bf00 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61 lock size for la
8bf10 72 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 rger. ** chunks
8bf20 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 53 .. */. u32 aiS
8bf30 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 5d mall[MX_SMALL-1]
8bf40 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 ; /* For sizes
8bf50 20 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 4d 2 through MX_SM
8bf60 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 2a ALL, inclusive *
8bf70 2f 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b 4e /. u32 aiHash[N
8bf80 5f 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 2f _HASH]; /
8bf90 2a 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f 53 * For sizes MX_S
8bfa0 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 65 MALL+1 and large
8bfb0 72 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b 20 r */.} mem3 = {
8bfc0 39 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64 65 97535575 };..#de
8bfd0 66 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41 4c fine mem3 GLOBAL
8bfe0 28 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 (struct Mem3Glob
8bff0 61 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a al, mem3)../*.**
8c000 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e Unlink the chun
8c010 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b k at mem3.aPool[
8c020 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 i] from list it
8c030 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 is currently.**
8c040 6f 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20 74 on. *pRoot is t
8c050 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 20 69 he list that i i
8c060 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a s a member of..*
8c070 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
8c080 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c msys3UnlinkFromL
8c090 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a ist(u32 i, u32 *
8c0a0 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e 65 pRoot){. u32 ne
8c0b0 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b xt = mem3.aPool[
8c0c0 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a i].u.list.next;.
8c0d0 20 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 6d u32 prev = mem
8c0e0 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 3.aPool[i].u.lis
8c0f0 74 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 t.prev;. assert
8c100 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
8c110 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
8c120 20 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d 3d );. if( prev==
8c130 30 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74 20 0 ){. *pRoot
8c140 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b = next;. }else{
8c150 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
8c160 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 prev].u.list.nex
8c170 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 t = next;. }.
8c180 69 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20 20 if( next ){.
8c190 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74 5d mem3.aPool[next]
8c1a0 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 70 .u.list.prev = p
8c1b0 72 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33 2e rev;. }. mem3.
8c1c0 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
8c1d0 6e 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d 33 next = 0;. mem3
8c1e0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
8c1f0 2e 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a .prev = 0;.}../*
8c200 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 .** Unlink the c
8c210 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 20 hunk at index i
8c220 66 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76 65 from .** whateve
8c230 72 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e r list is curren
8c240 74 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e tly a member of.
8c250 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8c260 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75 33 memsys3Unlink(u3
8c270 32 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2 i){. u32 size
8c280 2c 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 , hash;. assert
8c290 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
8c2a0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
8c2b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d );. assert( (m
8c2c0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 em3.aPool[i-1].u
8c2d0 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 29 .hdr.size4x & 1)
8c2e0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
8c2f0 20 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 i>=1 );. size
8c300 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 = mem3.aPool[i-1
8c310 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 ].u.hdr.size4x/4
8c320 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
8c330 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 ==mem3.aPool[i+s
8c340 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 ize-1].u.hdr.pre
8c350 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 vSize );. asser
8c360 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20 t( size>=2 );.
8c370 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 if( size <= MX_S
8c380 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73 MALL ){. mems
8c390 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 ys3UnlinkFromLis
8c3a0 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 t(i, &mem3.aiSma
8c3b0 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d ll[size-2]);. }
8c3c0 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 3d else{. hash =
8c3d0 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a size % N_HASH;.
8c3e0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e memsys3Unlin
8c3f0 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 kFromList(i, &me
8c400 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 m3.aiHash[hash])
8c410 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c ;. }.}../*.** L
8c420 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 ink the chunk at
8c430 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 73 mem3.aPool[i] s
8c440 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 o that is on the
8c450 20 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a 20 list rooted.**
8c460 61 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 at *pRoot..*/.st
8c470 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
8c480 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 33 3LinkIntoList(u3
8c490 32 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 2 i, u32 *pRoot)
8c4a0 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
8c4b0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
8c4c0 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 em3.mutex) );.
8c4d0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
8c4e0 6c 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52 6f list.next = *pRo
8c4f0 6f 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ot;. mem3.aPool
8c500 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 [i].u.list.prev
8c510 3d 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f 6f = 0;. if( *pRoo
8c520 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 t ){. mem3.aP
8c530 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c 69 ool[*pRoot].u.li
8c540 73 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d st.prev = i;. }
8c550 0a 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a 7d . *pRoot = i;.}
8c560 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 ../*.** Link the
8c570 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 chunk at index
8c580 69 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 68 i into either th
8c590 65 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a e appropriate.**
8c5a0 20 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69 73 small chunk lis
8c5b0 74 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6c t, or into the l
8c5c0 61 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 arge chunk hash
8c5d0 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 table..*/.static
8c5e0 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e void memsys3Lin
8c5f0 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 k(u32 i){. u32
8c600 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 size, hash;. as
8c610 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
8c620 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 tex_held(mem3.mu
8c630 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
8c640 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 ( i>=1 );. asse
8c650 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b rt( (mem3.aPool[
8c660 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
8c670 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 73 x & 1)==0 );. s
8c680 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c ize = mem3.aPool
8c690 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
8c6a0 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 4x/4;. assert(
8c6b0 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c size==mem3.aPool
8c6c0 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 [i+size-1].u.hdr
8c6d0 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 .prevSize );. a
8c6e0 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 ssert( size>=2 )
8c6f0 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 ;. if( size <=
8c700 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 MX_SMALL ){.
8c710 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c memsys3LinkIntoL
8c720 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 ist(i, &mem3.aiS
8c730 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 mall[size-2]);.
8c740 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 }else{. hash
8c750 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 = size % N_HASH
8c760 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e ;. memsys3Lin
8c770 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 kIntoList(i, &me
8c780 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 m3.aiHash[hash])
8c790 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
8c7a0 66 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d f the STATIC_MEM
8c7b0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c mutex is not al
8c7c0 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 ready held, obta
8c7d0 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d in it now. The m
8c7e0 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 utex.** will alr
8c7f0 65 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 eady be held (ob
8c800 74 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 tained by code i
8c810 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a n malloc.c) if.*
8c820 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 * sqlite3GlobalC
8c830 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 onfig.bMemStat i
8c840 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 s true..*/.stati
8c850 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45 6e c void memsys3En
8c860 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 ter(void){. if(
8c870 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
8c880 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 nfig.bMemstat==0
8c890 20 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d 3d && mem3.mutex==
8c8a0 30 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d 75 0 ){. mem3.mu
8c8b0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
8c8c0 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
8c8d0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 UTEX_STATIC_MEM)
8c8e0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
8c8f0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33 mutex_enter(mem3
8c900 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 .mutex);.}.stati
8c910 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 65 c void memsys3Le
8c920 61 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c ave(void){. sql
8c930 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
8c940 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a (mem3.mutex);.}.
8c950 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77 68 ./*.** Called wh
8c960 65 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 en we are unable
8c970 20 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20 61 to satisfy an a
8c980 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42 79 llocation of nBy
8c990 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tes..*/.static v
8c9a0 6f 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 oid memsys3OutOf
8c9b0 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74 65 Memory(int nByte
8c9c0 29 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e 61 ){. if( !mem3.a
8c9d0 6c 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20 20 larmBusy ){.
8c9e0 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d mem3.alarmBusy =
8c9f0 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 1;. assert(
8ca00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
8ca10 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 ld(mem3.mutex) )
8ca20 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
8ca30 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d tex_leave(mem3.m
8ca40 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 utex);. sqlit
8ca50 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
8ca60 79 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 y(nByte);. sq
8ca70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
8ca80 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 r(mem3.mutex);.
8ca90 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 mem3.alarmBus
8caa0 79 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f y = 0;. }.}.../
8cab0 2a 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73 20 *.** Chunk i is
8cac0 61 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68 61 a free chunk tha
8cad0 74 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e t has been unlin
8cae0 6b 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74 73 ked. Adjust its
8caf0 20 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d 65 .** size parame
8cb00 74 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d 6f ters for check-o
8cb10 75 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 ut and return a
8cb20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 0a pointer to the .
8cb30 2a 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e 20 ** user portion
8cb40 6f 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a 2f of the chunk..*/
8cb50 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
8cb60 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75 33 msys3Checkout(u3
8cb70 32 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b 29 2 i, u32 nBlock)
8cb80 7b 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73 73 {. u32 x;. ass
8cb90 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
8cba0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
8cbb0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
8cbc0 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 i>=1 );. asser
8cbd0 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d t( mem3.aPool[i-
8cbe0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
8cbf0 34 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 4==nBlock );. a
8cc00 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f ssert( mem3.aPoo
8cc10 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e l[i+nBlock-1].u.
8cc20 68 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e 42 hdr.prevSize==nB
8cc30 6c 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d 65 lock );. x = me
8cc40 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
8cc50 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d 65 hdr.size4x;. me
8cc60 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
8cc70 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c hdr.size4x = nBl
8cc80 6f 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26 32 ock*4 | 1 | (x&2
8cc90 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b );. mem3.aPool[
8cca0 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 i+nBlock-1].u.hd
8ccb0 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c r.prevSize = nBl
8ccc0 6f 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f ock;. mem3.aPoo
8ccd0 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e l[i+nBlock-1].u.
8cce0 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b hdr.size4x |= 2;
8ccf0 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 2e . return &mem3.
8cd00 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a aPool[i];.}../*.
8cd10 2a 2a 20 43 61 72 76 65 20 61 20 70 69 65 63 65 ** Carve a piece
8cd20 20 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64 20 off of the end
8cd30 6f 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61 73 of the mem3.iMas
8cd40 74 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a ter free chunk..
8cd50 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
8cd60 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 61 ter to the new a
8cd70 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c 20 llocation. Or,
8cd80 69 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 if the master ch
8cd90 75 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 unk.** is not la
8cda0 72 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74 75 rge enough, retu
8cdb0 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rn 0..*/.static
8cdc0 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72 6f void *memsys3Fro
8cdd0 6d 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c 6f mMaster(u32 nBlo
8cde0 63 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 ck){. assert( s
8cdf0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
8ce00 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b d(mem3.mutex) );
8ce10 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e . assert( mem3.
8ce20 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b szMaster>=nBlock
8ce30 20 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b );. if( nBlock
8ce40 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d >=mem3.szMaster-
8ce50 31 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20 1 ){. /* Use
8ce60 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 the entire maste
8ce70 72 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 r */. void *p
8ce80 20 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f = memsys3Checko
8ce90 75 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c ut(mem3.iMaster,
8cea0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b mem3.szMaster);
8ceb0 0a 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 . mem3.iMaste
8cec0 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e r = 0;. mem3.
8ced0 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 szMaster = 0;.
8cee0 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 mem3.mnMaster
8cef0 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 0;. return
8cf00 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 p;. }else{.
8cf10 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61 73 /* Split the mas
8cf20 74 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74 75 ter block. Retu
8cf30 72 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f 0a rn the tail. */.
8cf40 20 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78 3b u32 newi, x;
8cf50 0a 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d 33 . newi = mem3
8cf60 2e 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33 2e .iMaster + mem3.
8cf70 73 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f 63 szMaster - nBloc
8cf80 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e k;. assert( n
8cf90 65 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73 74 ewi > mem3.iMast
8cfa0 65 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d 33 er+1 );. mem3
8cfb0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8cfc0 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 ter+mem3.szMaste
8cfd0 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 r-1].u.hdr.prevS
8cfe0 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 ize = nBlock;.
8cff0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d mem3.aPool[mem
8d000 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 3.iMaster+mem3.s
8d010 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 zMaster-1].u.hdr
8d020 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20 .size4x |= 2;.
8d030 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 mem3.aPool[new
8d040 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
8d050 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 31 x = nBlock*4 + 1
8d060 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 ;. mem3.szMas
8d070 74 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 ter -= nBlock;.
8d080 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 mem3.aPool[ne
8d090 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 wi-1].u.hdr.prev
8d0a0 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 Size = mem3.szMa
8d0b0 73 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d 65 ster;. x = me
8d0c0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
8d0d0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 aster-1].u.hdr.s
8d0e0 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 6d ize4x & 2;. m
8d0f0 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8d100 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8d110 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a size4x = mem3.sz
8d120 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 Master*4 | x;.
8d130 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 if( mem3.szMas
8d140 74 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 73 ter < mem3.mnMas
8d150 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ter ){. mem
8d160 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 3.mnMaster = mem
8d170 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 3.szMaster;.
8d180 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f }. return (vo
8d190 69 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b id*)&mem3.aPool[
8d1a0 6e 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a newi];. }.}../*
8d1b0 0a 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 .** *pRoot is th
8d1c0 65 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73 74 e head of a list
8d1d0 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20 of free chunks
8d1e0 6f 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 of the same size
8d1f0 0a 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a 65 .** or same size
8d200 20 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65 72 hash. In other
8d210 20 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20 69 words, *pRoot i
8d220 73 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65 69 s an entry in ei
8d230 74 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69 53 ther.** mem3.aiS
8d240 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e 61 mall[] or mem3.a
8d250 69 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a iHash[]. .**.**
8d260 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78 This routine ex
8d270 61 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 69 amines all entri
8d280 65 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 es on the given
8d290 6c 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a 2a list and tries.*
8d2a0 2a 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 61 * to coalesce ea
8d2b0 63 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 ch entries with
8d2c0 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 68 adjacent free ch
8d2d0 75 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 unks. .**.** If
8d2e0 20 69 74 20 73 65 65 73 20 61 20 63 68 75 6e 6b it sees a chunk
8d2f0 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 that is larger
8d300 74 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 than mem3.iMaste
8d310 72 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 0a r, it replaces .
8d320 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d ** the current m
8d330 65 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74 68 em3.iMaster with
8d340 20 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20 the new larger
8d350 63 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72 chunk. In order
8d360 20 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 6d for.** this mem
8d370 33 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 63 3.iMaster replac
8d380 65 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74 ement to work, t
8d390 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 he master chunk
8d3a0 6d 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b 65 must be.** linke
8d3b0 64 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 d into the hash
8d3c0 74 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 73 tables. That is
8d3d0 20 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20 not the normal
8d3e0 73 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66 61 state of.** affa
8d3f0 69 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 irs, of course.
8d400 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 The calling rou
8d410 74 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20 74 tine must link t
8d420 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68 75 he master.** chu
8d430 6e 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b 69 nk before invoki
8d440 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c ng this routine,
8d450 20 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69 6e then must unlin
8d460 6b 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79 0a k the (possibly.
8d470 2a 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73 74 ** changed) mast
8d480 65 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74 68 er chunk once th
8d490 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 is routine has f
8d4a0 69 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 74 inished..*/.stat
8d4b0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4d ic void memsys3M
8d4c0 65 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74 29 erge(u32 *pRoot)
8d4d0 7b 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20 70 {. u32 iNext, p
8d4e0 72 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78 3b rev, size, i, x;
8d4f0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
8d500 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
8d510 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 em3.mutex) );.
8d520 66 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69 3e for(i=*pRoot; i>
8d530 30 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20 20 0; i=iNext){.
8d540 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 iNext = mem3.aP
8d550 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 ool[i].u.list.ne
8d560 78 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d xt;. size = m
8d570 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 em3.aPool[i-1].u
8d580 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 .hdr.size4x;.
8d590 20 61 73 73 65 72 74 28 20 28 73 69 7a 65 26 31 assert( (size&1
8d5a0 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 )==0 );. if(
8d5b0 28 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a 20 (size&2)==0 ){.
8d5c0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
8d5d0 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 52 nkFromList(i, pR
8d5e0 6f 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 oot);. asse
8d5f0 72 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 6f rt( i > mem3.aPo
8d600 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 ol[i-1].u.hdr.pr
8d610 65 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 evSize );.
8d620 70 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 2e prev = i - mem3.
8d630 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
8d640 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20 .prevSize;.
8d650 20 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78 74 if( prev==iNext
8d660 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 ){. iNex
8d670 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 t = mem3.aPool[p
8d680 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 rev].u.list.next
8d690 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
8d6a0 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70 72 memsys3Unlink(pr
8d6b0 65 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 ev);. size
8d6c0 3d 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20 70 = i + size/4 - p
8d6d0 72 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d rev;. x = m
8d6e0 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 em3.aPool[prev-1
8d6f0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 ].u.hdr.size4x &
8d700 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 2;. mem3.a
8d710 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 Pool[prev-1].u.h
8d720 64 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a 65 dr.size4x = size
8d730 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 *4 | x;. me
8d740 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73 69 m3.aPool[prev+si
8d750 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 ze-1].u.hdr.prev
8d760 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 Size = size;.
8d770 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 70 memsys3Link(p
8d780 72 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 rev);. i =
8d790 70 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b prev;. }else{
8d7a0 0a 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20 34 . size /= 4
8d7b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
8d7c0 73 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 74 size>mem3.szMast
8d7d0 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 er ){. mem3
8d7e0 2e 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20 20 .iMaster = i;.
8d7f0 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 mem3.szMaste
8d800 72 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a r = size;. }.
8d810 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
8d820 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d urn a block of m
8d830 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 emory of at leas
8d840 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 t nBytes in size
8d850 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c ..** Return NULL
8d860 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a if unable..**.*
8d870 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
8d880 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 assumes that the
8d890 20 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 necessary mutex
8d8a0 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a es, if any, are.
8d8b0 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 ** already held
8d8c0 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 by the caller. H
8d8d0 65 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a ence "Unsafe"..*
8d8e0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d /.static void *m
8d8f0 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 emsys3MallocUnsa
8d900 66 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 fe(int nByte){.
8d910 20 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e 42 u32 i;. u32 nB
8d920 6c 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46 72 lock;. u32 toFr
8d930 65 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ee;.. assert( s
8d940 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
8d950 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b d(mem3.mutex) );
8d960 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f . assert( sizeo
8d970 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 f(Mem3Block)==8
8d980 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d );. if( nByte<=
8d990 31 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 12 ){. nBlock
8d9a0 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 2;. }else{.
8d9b0 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 nBlock = (nBy
8d9c0 74 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d 0a te + 11)/8;. }.
8d9d0 20 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63 6b assert( nBlock
8d9e0 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54 45 >=2 );.. /* STE
8d9f0 50 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20 66 P 1:. ** Look f
8da00 6f 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 or an entry of t
8da10 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 he correct size
8da20 69 6e 20 65 69 74 68 65 72 20 74 68 65 20 73 6d in either the sm
8da30 61 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20 74 all. ** chunk t
8da40 61 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20 6c able or in the l
8da50 61 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 arge chunk hash
8da60 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a table. This is.
8da70 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 ** successful
8da80 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d 65 most of the time
8da90 20 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73 20 (about 9 times
8daa0 6f 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a 2f out of 10).. */
8dab0 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c 3d . if( nBlock <=
8dac0 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 MX_SMALL ){.
8dad0 20 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c i = mem3.aiSmal
8dae0 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20 20 l[nBlock-2];.
8daf0 20 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 if( i>0 ){.
8db00 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 memsys3UnlinkF
8db10 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 romList(i, &mem3
8db20 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d .aiSmall[nBlock-
8db30 32 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 2]);. retur
8db40 6e 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 n memsys3Checkou
8db50 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20 t(i, nBlock);.
8db60 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
8db70 20 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c 6f int hash = nBlo
8db80 63 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 ck % N_HASH;.
8db90 20 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48 61 for(i=mem3.aiHa
8dba0 73 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20 69 sh[hash]; i>0; i
8dbb0 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 =mem3.aPool[i].u
8dbc0 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 .list.next){.
8dbd0 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f 6f if( mem3.aPoo
8dbe0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
8dbf0 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 7b e4x/4==nBlock ){
8dc00 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 33 . memsys3
8dc10 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 UnlinkFromList(i
8dc20 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 , &mem3.aiHash[h
8dc30 61 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20 72 ash]);. r
8dc40 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 eturn memsys3Che
8dc50 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 ckout(i, nBlock)
8dc60 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
8dc70 20 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20 32 }.. /* STEP 2
8dc80 3a 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 61 :. ** Try to sa
8dc90 74 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 tisfy the alloca
8dca0 74 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 tion by carving
8dcb0 61 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 a piece off of t
8dcc0 68 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 he end. ** of t
8dcd0 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e he master chunk.
8dce0 20 20 54 68 69 73 20 73 74 65 70 20 75 73 75 61 This step usua
8dcf0 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74 65 lly works if ste
8dd00 70 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a p 1 fails.. */.
8dd10 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 if( mem3.szMas
8dd20 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 ter>=nBlock ){.
8dd30 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 return memsys
8dd40 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 3FromMaster(nBlo
8dd50 63 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 ck);. }... /*
8dd60 53 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20 4c STEP 3: . ** L
8dd70 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 oop through the
8dd80 65 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70 6f entire memory po
8dd90 6f 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61 64 ol. Coalesce ad
8dda0 6a 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a 2a jacent free. **
8ddb0 20 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d 70 chunks. Recomp
8ddc0 75 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 63 ute the master c
8ddd0 68 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72 67 hunk as the larg
8dde0 65 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a est free chunk..
8ddf0 20 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61 67 ** Then try ag
8de00 61 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20 74 ain to satisfy t
8de10 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 he allocation by
8de20 20 63 61 72 76 69 6e 67 20 61 20 70 69 65 63 65 carving a piece
8de30 20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68 65 off. ** of the
8de40 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 end of the mast
8de50 65 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 er chunk. This
8de60 73 74 65 70 20 68 61 70 70 65 6e 73 20 76 65 72 step happens ver
8de70 79 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28 77 y. ** rarely (w
8de80 65 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20 20 e hope!). */.
8de90 66 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f 63 for(toFree=nBloc
8dea0 6b 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d 65 k*16; toFree<(me
8deb0 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 6f m3.nPool*16); to
8dec0 46 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20 20 Free *= 2){.
8ded0 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f memsys3OutOfMemo
8dee0 72 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20 20 ry(toFree);.
8def0 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 if( mem3.iMaster
8df00 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 ){. memsys
8df10 33 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 3Link(mem3.iMast
8df20 65 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e er);. mem3.
8df30 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 iMaster = 0;.
8df40 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 mem3.szMaster
8df50 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
8df60 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 for(i=0; i<N_HAS
8df70 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d H; i++){. m
8df80 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d emsys3Merge(&mem
8df90 33 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20 20 3.aiHash[i]);.
8dfa0 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b }. for(i=0;
8dfb0 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 i<MX_SMALL-1; i
8dfc0 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 ++){. memsy
8dfd0 73 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69 s3Merge(&mem3.ai
8dfe0 53 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 7d Small[i]);. }
8dff0 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a . if( mem3.sz
8e000 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 Master ){.
8e010 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 memsys3Unlink(me
8e020 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 m3.iMaster);.
8e030 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 if( mem3.szMa
8e040 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a ster>=nBlock ){.
8e050 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d return m
8e060 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 emsys3FromMaster
8e070 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 (nBlock);.
8e080 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f }. }. }.. /
8e090 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 * If none of the
8e0a0 20 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 74 above worked, t
8e0b0 68 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f 0a hen we fail. */.
8e0c0 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
8e0d0 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 *.** Free an out
8e0e0 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 standing memory
8e0f0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a allocation..**.*
8e100 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
8e110 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 assumes that the
8e120 20 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 necessary mutex
8e130 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a es, if any, are.
8e140 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 ** already held
8e150 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 by the caller. H
8e160 65 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a ence "Unsafe"..*
8e170 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 /.void memsys3Fr
8e180 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 eeUnsafe(void *p
8e190 4f 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 Old){. Mem3Bloc
8e1a0 6b 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 k *p = (Mem3Bloc
8e1b0 6b 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20 69 k*)pOld;. int i
8e1c0 3b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78 3b ;. u32 size, x;
8e1d0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
8e1e0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
8e1f0 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 m3.mutex) );. a
8e200 73 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61 50 ssert( p>mem3.aP
8e210 6f 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e 61 ool && p<&mem3.a
8e220 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d Pool[mem3.nPool]
8e230 20 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d 65 );. i = p - me
8e240 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73 65 m3.aPool;. asse
8e250 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b rt( (mem3.aPool[
8e260 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
8e270 78 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69 7a x&1)==1 );. siz
8e280 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 e = mem3.aPool[i
8e290 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8e2a0 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69 2b /4;. assert( i+
8e2b0 73 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c size<=mem3.nPool
8e2c0 2b 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f +1 );. mem3.aPo
8e2d0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
8e2e0 7a 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d 65 ze4x &= ~1;. me
8e2f0 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d m3.aPool[i+size-
8e300 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
8e310 65 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d 33 e = size;. mem3
8e320 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d .aPool[i+size-1]
8e330 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d .u.hdr.size4x &=
8e340 20 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 69 ~2;. memsys3Li
8e350 6e 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 79 nk(i);.. /* Try
8e360 20 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 6d to expand the m
8e370 61 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 20 aster using the
8e380 6e 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 6e newly freed chun
8e390 6b 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e k */. if( mem3.
8e3a0 69 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 77 iMaster ){. w
8e3b0 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f hile( (mem3.aPoo
8e3c0 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 l[mem3.iMaster-1
8e3d0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 ].u.hdr.size4x&2
8e3e0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 69 )==0 ){. si
8e3f0 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
8e400 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
8e410 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a u.hdr.prevSize;.
8e420 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 mem3.iMast
8e430 65 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20 er -= size;.
8e440 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 mem3.szMaster
8e450 2b 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d += size;. m
8e460 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d emsys3Unlink(mem
8e470 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 3.iMaster);.
8e480 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c x = mem3.aPool
8e490 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d [mem3.iMaster-1]
8e4a0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 .u.hdr.size4x &
8e4b0 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 2;. mem3.aP
8e4c0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
8e4d0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8e4e0 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 = mem3.szMaster
8e4f0 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 *4 | x;. me
8e500 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
8e510 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 aster+mem3.szMas
8e520 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 ter-1].u.hdr.pre
8e530 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d vSize = mem3.szM
8e540 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 aster;. }.
8e550 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x = mem3.aPool[
8e560 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
8e570 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 u.hdr.size4x & 2
8e580 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 ;. while( (me
8e590 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
8e5a0 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 aster+mem3.szMas
8e5b0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ter-1].u.hdr.siz
8e5c0 65 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20 20 e4x&1)==0 ){.
8e5d0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
8e5e0 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 (mem3.iMaster+me
8e5f0 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 m3.szMaster);.
8e600 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 mem3.szMaste
8e610 72 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b r += mem3.aPool[
8e620 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d mem3.iMaster+mem
8e630 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 3.szMaster-1].u.
8e640 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 hdr.size4x/4;.
8e650 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d mem3.aPool[m
8e660 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
8e670 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 .hdr.size4x = me
8e680 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 m3.szMaster*4 |
8e690 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 x;. mem3.aP
8e6a0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ool[mem3.iMaster
8e6b0 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 +mem3.szMaster-1
8e6c0 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
8e6d0 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 = mem3.szMaster
8e6e0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
8e6f0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
8e700 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 size of an outst
8e710 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f anding allocatio
8e720 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 n, in bytes. Th
8e730 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e e.** size return
8e740 65 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 ed omits the 8-b
8e750 79 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 yte header overh
8e760 65 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a ead. This only.
8e770 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 ** works for chu
8e780 6e 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72 nks that are cur
8e790 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f rently checked o
8e7a0 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ut..*/.static in
8e7b0 74 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 6f t memsys3Size(vo
8e7c0 69 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42 6c id *p){. Mem3Bl
8e7d0 6f 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69 ock *pBlock;. i
8e7e0 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
8e7f0 20 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20 28 0;. pBlock = (
8e800 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20 Mem3Block*)p;.
8e810 61 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b 5b assert( (pBlock[
8e820 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8e830 26 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 &1)!=0 );. retu
8e840 72 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 rn (pBlock[-1].u
8e850 2e 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29 2a .hdr.size4x&~3)*
8e860 32 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2 - 4;.}../*.**
8e870 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 Round up a reque
8e880 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e st size to the n
8e890 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 ext valid alloca
8e8a0 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 tion size..*/.st
8e8b0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 atic int memsys3
8e8c0 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a Roundup(int n){.
8e8d0 20 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a 20 if( n<=12 ){.
8e8e0 20 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20 20 return 12;.
8e8f0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
8e900 6e 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d 20 n ((n+11)&~7) -
8e910 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4;. }.}../*.**
8e920 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 Allocate nBytes
8e930 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 of memory..*/.st
8e940 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 atic void *memsy
8e950 73 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 s3Malloc(int nBy
8e960 74 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f tes){. sqlite3_
8e970 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 int64 *p;. asse
8e980 72 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b 20 rt( nBytes>0 );
8e990 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c 6c /* mall
8e9a0 6f 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75 74 oc.c filters out
8e9b0 20 30 20 62 79 74 65 20 72 65 71 75 65 73 74 73 0 byte requests
8e9c0 20 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 */. memsys3Ent
8e9d0 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 er();. p = mems
8e9e0 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 ys3MallocUnsafe(
8e9f0 6e 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73 79 nBytes);. memsy
8ea00 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 s3Leave();. ret
8ea10 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d urn (void*)p; .}
8ea20 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d ../*.** Free mem
8ea30 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d ory..*/.void mem
8ea40 73 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a 70 sys3Free(void *p
8ea50 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 Prior){. assert
8ea60 28 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d 65 ( pPrior );. me
8ea70 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 msys3Enter();.
8ea80 6d 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 memsys3FreeUnsaf
8ea90 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d e(pPrior);. mem
8eaa0 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a 0a sys3Leave();.}..
8eab0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
8eac0 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 size of an exis
8ead0 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ting memory allo
8eae0 63 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 2a cation.*/.void *
8eaf0 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28 76 memsys3Realloc(v
8eb00 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 oid *pPrior, int
8eb10 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 nBytes){. int
8eb20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b nOld;. void *p;
8eb30 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 . if( pPrior==0
8eb40 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 ){. return s
8eb50 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 qlite3_malloc(nB
8eb60 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 ytes);. }. if(
8eb70 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 nBytes<=0 ){.
8eb80 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
8eb90 50 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 Prior);. retu
8eba0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 rn 0;. }. nOld
8ebb0 20 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 70 = memsys3Size(p
8ebc0 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e 42 Prior);. if( nB
8ebd0 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e 42 ytes<=nOld && nB
8ebe0 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20 29 ytes>=nOld-128 )
8ebf0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72 {. return pPr
8ec00 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 ior;. }. memsy
8ec10 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d s3Enter();. p =
8ec20 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e memsys3MallocUn
8ec30 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 safe(nBytes);.
8ec40 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 if( p ){. if(
8ec50 20 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b 0a nOld<nBytes ){.
8ec60 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 memcpy(p,
8ec70 70 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 pPrior, nOld);.
8ec80 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
8ec90 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 memcpy(p, pPrior
8eca0 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 7d , nBytes);. }
8ecb0 0a 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 . memsys3Free
8ecc0 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a Unsafe(pPrior);.
8ecd0 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 }. memsys3Lea
8ece0 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ve();. return p
8ecf0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 ;.}../*.** Initi
8ed00 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c alize this modul
8ed10 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
8ed20 20 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f 69 memsys3Init(voi
8ed30 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
8ed40 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
8ed50 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 NotUsed);. if(
8ed60 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f !sqlite3GlobalCo
8ed70 6e 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20 20 nfig.pHeap ){.
8ed80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
8ed90 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a ERROR;. }.. /*
8eda0 20 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 Store a pointer
8edb0 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 to the memory b
8edc0 6c 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 73 lock in global s
8edd0 74 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20 2a tructure mem3. *
8ede0 2f 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 /. assert( size
8edf0 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 of(Mem3Block)==8
8ee00 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c );. mem3.aPool
8ee10 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a 29 = (Mem3Block *)
8ee20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
8ee30 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65 6d fig.pHeap;. mem
8ee40 33 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 74 3.nPool = (sqlit
8ee50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
8ee60 48 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d 65 Heap / sizeof(Me
8ee70 6d 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a 0a m3Block)) - 2;..
8ee80 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 /* Initialize
8ee90 74 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b the master block
8eea0 2e 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d 61 . */. mem3.szMa
8eeb0 73 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f ster = mem3.nPoo
8eec0 6c 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 l;. mem3.mnMast
8eed0 65 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 er = mem3.szMast
8eee0 65 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73 74 er;. mem3.iMast
8eef0 65 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e 61 er = 1;. mem3.a
8ef00 50 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73 69 Pool[0].u.hdr.si
8ef10 7a 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a 4d ze4x = (mem3.szM
8ef20 61 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a 20 aster<<2) + 2;.
8ef30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
8ef40 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70 72 .nPool].u.hdr.pr
8ef50 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e 50 evSize = mem3.nP
8ef60 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f ool;. mem3.aPoo
8ef70 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e l[mem3.nPool].u.
8ef80 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b 0a hdr.size4x = 1;.
8ef90 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
8efa0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 _OK;.}../*.** De
8efb0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 initialize this
8efc0 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 module..*/.stati
8efd0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 68 c void memsys3Sh
8efe0 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 utdown(void *Not
8eff0 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
8f000 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
8f010 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a d);. return;.}.
8f020 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 .../*.** Open th
8f030 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 e file indicated
8f040 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 and write a log
8f050 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 of all unfreed
8f060 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 memory .** alloc
8f070 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 ations into that
8f080 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f log..*/.SQLITE_
8f090 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
8f0a0 69 74 65 33 4d 65 6d 73 79 73 33 44 75 6d 70 28 ite3Memsys3Dump(
8f0b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
8f0c0 65 6e 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 ename){.#ifdef S
8f0d0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 QLITE_DEBUG. FI
8f0e0 4c 45 20 2a 6f 75 74 3b 0a 20 20 75 33 32 20 69 LE *out;. u32 i
8f0f0 2c 20 6a 3b 0a 20 20 75 33 32 20 73 69 7a 65 3b , j;. u32 size;
8f100 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 . if( zFilename
8f110 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 ==0 || zFilename
8f120 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 [0]==0 ){. ou
8f130 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 t = stdout;. }e
8f140 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 lse{. out = f
8f150 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 open(zFilename,
8f160 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 "w");. if( ou
8f170 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 t==0 ){. fp
8f180 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a rintf(stderr, "*
8f190 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 * Unable to outp
8f1a0 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 ut memory debug
8f1b0 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a output log: %s *
8f1c0 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 *\n",.
8f1d0 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c zFil
8f1e0 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 ename);. re
8f1f0 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a turn;. }. }.
8f200 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 memsys3Enter()
8f210 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c ;. fprintf(out,
8f220 20 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20 "CHUNKS:\n");.
8f230 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d for(i=1; i<=mem
8f240 33 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 3.nPool; i+=size
8f250 2f 34 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 /4){. size =
8f260 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
8f270 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 u.hdr.size4x;.
8f280 20 20 69 66 28 20 73 69 7a 65 2f 34 3c 3d 31 20 if( size/4<=1
8f290 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 ){. fprintf
8f2a0 28 6f 75 74 2c 20 22 25 70 20 73 69 7a 65 20 65 (out, "%p size e
8f2b0 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 rror\n", &mem3.a
8f2c0 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 Pool[i]);.
8f2d0 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 assert( 0 );.
8f2e0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
8f2f0 20 20 20 20 69 66 28 20 28 73 69 7a 65 26 31 29 if( (size&1)
8f300 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 61 50 6f 6f ==0 && mem3.aPoo
8f310 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e l[i+size/4-1].u.
8f320 68 64 72 2e 70 72 65 76 53 69 7a 65 21 3d 73 69 hdr.prevSize!=si
8f330 7a 65 2f 34 20 29 7b 0a 20 20 20 20 20 20 66 70 ze/4 ){. fp
8f340 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74 rintf(out, "%p t
8f350 61 69 6c 20 73 69 7a 65 20 64 6f 65 73 20 6e 6f ail size does no
8f360 74 20 6d 61 74 63 68 5c 6e 22 2c 20 26 6d 65 6d t match\n", &mem
8f370 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 3.aPool[i]);.
8f380 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a assert( 0 );.
8f390 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
8f3a0 20 7d 0a 20 20 20 20 69 66 28 20 28 28 6d 65 6d }. if( ((mem
8f3b0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 3.aPool[i+size/4
8f3c0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8f3d0 26 32 29 3e 3e 31 29 21 3d 28 73 69 7a 65 26 31 &2)>>1)!=(size&1
8f3e0 29 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e ) ){. fprin
8f3f0 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c tf(out, "%p tail
8f400 20 63 68 65 63 6b 6f 75 74 20 62 69 74 20 69 73 checkout bit is
8f410 20 69 6e 63 6f 72 72 65 63 74 5c 6e 22 2c 20 26 incorrect\n", &
8f420 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a mem3.aPool[i]);.
8f430 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 assert( 0
8f440 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
8f450 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 }. if( si
8f460 7a 65 26 31 20 29 7b 0a 20 20 20 20 20 20 66 70 ze&1 ){. fp
8f470 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 rintf(out, "%p %
8f480 36 64 20 62 79 74 65 73 20 63 68 65 63 6b 65 64 6d bytes checked
8f490 20 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 out\n", &mem3.a
8f4a0 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 Pool[i], (size/4
8f4b0 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 73 )*8-8);. }els
8f4c0 65 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 e{. fprintf
8f4d0 28 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62 79 (out, "%p %6d by
8f4e0 74 65 73 20 66 72 65 65 25 73 5c 6e 22 2c 20 26 tes free%s\n", &
8f4f0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 mem3.aPool[i], (
8f500 73 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 size/4)*8-8,.
8f510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
8f520 3d 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3f ==mem3.iMaster ?
8f530 20 22 20 2a 2a 6d 61 73 74 65 72 2a 2a 22 20 3a " **master**" :
8f540 20 22 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a "");. }. }.
8f550 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f for(i=0; i<MX_
8f560 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 SMALL-1; i++){.
8f570 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 53 6d if( mem3.aiSm
8f580 61 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 all[i]==0 ) cont
8f590 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 inue;. fprint
8f5a0 66 28 6f 75 74 2c 20 22 73 6d 61 6c 6c 28 25 32 f(out, "small(%2
8f5b0 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f d):", i);. fo
8f5c0 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 r(j = mem3.aiSma
8f5d0 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 ll[i]; j>0; j=me
8f5e0 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 m3.aPool[j].u.li
8f5f0 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 st.next){.
8f600 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 fprintf(out, " %
8f610 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 p(%d)", &mem3.aP
8f620 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 ool[j],.
8f630 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f (mem3.aPoo
8f640 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[j-1].u.hdr.siz
8f650 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 e4x/4)*8-8);.
8f660 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f }. fprintf(o
8f670 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a ut, "\n"); . }.
8f680 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 for(i=0; i<N_H
8f690 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 ASH; i++){. i
8f6a0 66 28 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 f( mem3.aiHash[i
8f6b0 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b ]==0 ) continue;
8f6c0 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 . fprintf(out
8f6d0 2c 20 22 68 61 73 68 28 25 32 64 29 3a 22 2c 20 , "hash(%2d):",
8f6e0 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 i);. for(j =
8f6f0 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3b 20 mem3.aiHash[i];
8f700 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f j>0; j=mem3.aPoo
8f710 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 l[j].u.list.next
8f720 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 ){. fprintf
8f730 28 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c (out, " %p(%d)",
8f740 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c &mem3.aPool[j],
8f750 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 . (
8f760 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e mem3.aPool[j-1].
8f770 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a u.hdr.size4x/4)*
8f780 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 8-8);. }.
8f790 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e fprintf(out, "\n
8f7a0 22 29 3b 20 0a 20 20 7d 0a 20 20 66 70 72 69 6e "); . }. fprin
8f7b0 74 66 28 6f 75 74 2c 20 22 6d 61 73 74 65 72 3d tf(out, "master=
8f7c0 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61 73 %d\n", mem3.iMas
8f7d0 74 65 72 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 ter);. fprintf(
8f7e0 6f 75 74 2c 20 22 6e 6f 77 55 73 65 64 3d 25 64 out, "nowUsed=%d
8f7f0 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a \n", mem3.nPool*
8f800 38 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 8 - mem3.szMaste
8f810 72 2a 38 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 r*8);. fprintf(
8f820 6f 75 74 2c 20 22 6d 78 55 73 65 64 3d 25 64 5c out, "mxUsed=%d\
8f830 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 n", mem3.nPool*8
8f840 20 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 - mem3.mnMaster
8f850 2a 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d *8);. sqlite3_m
8f860 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e utex_leave(mem3.
8f870 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f 75 mutex);. if( ou
8f880 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 20 t==stdout ){.
8f890 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b fflush(stdout);
8f8a0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63 . }else{. fc
8f8b0 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 lose(out);. }.#
8f8c0 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 else. UNUSED_PA
8f8d0 52 41 4d 45 54 45 52 28 7a 46 69 6c 65 6e 61 6d RAMETER(zFilenam
8f8e0 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a e);.#endif.}../*
8f8f0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
8f900 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 is the only rou
8f910 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c tine in this fil
8f920 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 e with external
8f930 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a .** linkage..**.
8f940 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 ** Populate the
8f950 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
8f960 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 allocation func
8f970 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e tion pointers in
8f980 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 .** sqlite3Globa
8f990 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 lConfig.m with p
8f9a0 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 ointers to the r
8f9b0 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 outines in this
8f9c0 66 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67 file. The.** arg
8f9d0 75 6d 65 6e 74 73 20 73 70 65 63 69 66 79 20 74 uments specify t
8f9e0 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f he block of memo
8f9f0 72 79 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a ry to manage..**
8fa00 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
8fa10 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 is only called
8fa20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 by sqlite3_confi
8fa30 67 28 29 2c 20 61 6e 64 20 74 68 65 72 65 66 6f g(), and therefo
8fa40 72 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71 re.** is not req
8fa50 75 69 72 65 64 20 74 6f 20 62 65 20 74 68 72 65 uired to be thre
8fa60 61 64 73 61 66 65 20 28 69 74 20 69 73 20 6e 6f adsafe (it is no
8fa70 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 t)..*/.SQLITE_PR
8fa80 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 IVATE const sqli
8fa90 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
8faa0 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 *sqlite3MemGetMe
8fab0 6d 73 79 73 33 28 76 6f 69 64 29 7b 0a 20 20 73 msys3(void){. s
8fac0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
8fad0 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
8fae0 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d mempoolMethods =
8faf0 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 4d {. memsys3M
8fb00 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 alloc,. mems
8fb10 79 73 33 46 72 65 65 2c 0a 20 20 20 20 20 6d 65 ys3Free,. me
8fb20 6d 73 79 73 33 52 65 61 6c 6c 6f 63 2c 0a 20 20 msys3Realloc,.
8fb30 20 20 20 6d 65 6d 73 79 73 33 53 69 7a 65 2c 0a memsys3Size,.
8fb40 20 20 20 20 20 6d 65 6d 73 79 73 33 52 6f 75 6e memsys3Roun
8fb50 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 dup,. memsys
8fb60 33 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73 3Init,. mems
8fb70 79 73 33 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 ys3Shutdown,.
8fb80 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 0. };. retur
8fb90 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 n &mempoolMethod
8fba0 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 s;.}..#endif /*
8fbb0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
8fbc0 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a MSYS3 */../*****
8fbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
8fbe0 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mem3.c ********
8fbf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fc10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
8fc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
8fc30 66 69 6c 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a file mem5.c ****
8fc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fc60 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
8fc70 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2007 October 14.
8fc80 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
8fc90 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
8fca0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
8fcb0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
8fcc0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
8fcd0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
8fce0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
8fcf0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
8fd00 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
8fd10 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
8fd20 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
8fd30 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
8fd40 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
8fd50 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
8fd60 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
8fd70 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
8fd80 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
8fd90 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
8fda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
8fde0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
8fdf0 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
8fe00 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
8fe10 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ent a memory.**
8fe20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
8fe30 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 stem for use by
8fe40 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 SQLite. .**.** T
8fe50 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
8fe60 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
8fe70 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f tion subsystem o
8fe80 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 mits all.** use
8fe90 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 of malloc(). The
8fea0 20 53 51 4c 69 74 65 20 75 73 65 72 20 73 75 70 SQLite user sup
8feb0 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 plies a block of
8fec0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 memory.** befor
8fed0 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 e calling sqlite
8fee0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 3_initialize() f
8fef0 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 rom which alloca
8ff00 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 tions.** are mad
8ff10 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62 e and returned b
8ff20 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 y the xMalloc()
8ff30 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a and xRealloc() .
8ff40 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
8ff50 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 ns. Once sqlite3
8ff60 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61 _initialize() ha
8ff70 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a s been called,.*
8ff80 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 * the amount of
8ff90 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 memory available
8ffa0 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69 to SQLite is fi
8ffb0 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a xed and cannot.*
8ffc0 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a * be changed..**
8ffd0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
8ffe0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
8fff0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
90000 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a tem is included.
90010 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20 ** in the build
90020 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45 only if SQLITE_E
90030 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73 NABLE_MEMSYS5 is
90040 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 defined..**.**
90050 24 49 64 3a 20 6d 65 6d 35 2e 63 2c 76 20 31 2e $Id: mem5.c,v 1.
90060 31 39 20 32 30 30 38 2f 31 31 2f 31 39 20 31 36 19 2008/11/19 16
90070 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 :52:44 danielk19
90080 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 77 Exp $.*/../*.
90090 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
900a0 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c of the memory al
900b0 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 20 locator is used
900c0 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a 20 53 51 only when .** SQ
900d0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
900e0 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a YS5 is defined..
900f0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
90100 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a _ENABLE_MEMSYS5.
90110 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d ./*.** A minimum
90120 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 allocation is a
90130 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
90140 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
90150 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 cture..** Larger
90160 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 allocations are
90170 20 61 6e 20 61 72 72 61 79 20 6f 66 20 74 68 65 an array of the
90180 73 65 20 73 74 72 75 63 74 75 72 65 73 20 77 68 se structures wh
90190 65 72 65 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 ere the.** size
901a0 6f 66 20 74 68 65 20 61 72 72 61 79 20 69 73 20 of the array is
901b0 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2f a power of 2..*/
901c0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
901d0 4d 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e Mem5Link Mem5Lin
901e0 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 k;.struct Mem5Li
901f0 6e 6b 20 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b nk {. int next;
90200 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
90210 6f 66 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 of next free chu
90220 6e 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 nk */. int prev
90230 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 ; /* Index
90240 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72 65 of previous fre
90250 65 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f e chunk */.};../
90260 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a *.** Maximum siz
90270 65 20 6f 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 e of any allocat
90280 69 6f 6e 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d ion is ((1<<LOGM
90290 41 58 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 29 2e AX)*mem5.nAtom).
902a0 20 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 6e Since.** mem5.n
902b0 41 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61 Atom is always a
902c0 74 20 6c 65 61 73 74 20 38 2c 20 74 68 69 73 20 t least 8, this
902d0 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 61 20 is not really a
902e0 70 72 61 63 74 69 63 61 6c 0a 2a 2a 20 6c 69 6d practical.** lim
902f0 69 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 itation..*/.#def
90300 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f ine LOGMAX 30../
90310 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 65 64 20 *.** Masks used
90320 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d for mem5.aCtrl[]
90330 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 elements..*/.#d
90340 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 53 49 efine CTRL_LOGSI
90350 5a 45 20 20 30 78 31 66 20 20 20 20 2f 2a 20 4c ZE 0x1f /* L
90360 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 68 69 73 og2 Size of this
90370 20 62 6c 6f 63 6b 20 72 65 6c 61 74 69 76 65 20 block relative
90380 74 6f 20 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 23 to POW2_MIN */.#
90390 64 65 66 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 define CTRL_FREE
903a0 20 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 0x20 /*
903b0 54 72 75 65 20 69 66 20 6e 6f 74 20 63 68 65 63 True if not chec
903c0 6b 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a ked out */../*.*
903d0 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 * All of the sta
903e0 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 tic variables us
903f0 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c ed by this modul
90400 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a e are collected.
90410 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 ** into a single
90420 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 structure named
90430 20 22 6d 65 6d 35 22 2e 20 20 54 68 69 73 20 69 "mem5". This i
90440 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a s to keep the.**
90450 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 static variable
90460 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 s organized and
90470 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 to reduce namesp
90480 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a ace pollution.**
90490 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c when this modul
904a0 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 e is combined wi
904b0 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 th other in the
904c0 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f amalgamation..*/
904d0 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 .static SQLITE_W
904e0 53 44 20 73 74 72 75 63 74 20 4d 65 6d 35 47 6c SD struct Mem5Gl
904f0 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a obal {. /*. **
90500 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c Memory availabl
90510 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e e for allocation
90520 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 74 6f . */. int nAto
90530 6d 3b 20 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c m; /* Smal
90540 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 61 6c lest possible al
90550 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 location in byte
90560 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 s */. int nBloc
90570 6b 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 k; /* Numbe
90580 72 20 6f 66 20 6e 41 74 6f 6d 20 73 69 7a 65 64 r of nAtom sized
90590 20 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c blocks in zPool
905a0 20 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b */. u8 *zPool;
905b0 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 . . /*. ** Mu
905c0 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 tex to control a
905d0 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d ccess to the mem
905e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
905f0 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 ubsystem.. */.
90600 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
90610 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a mutex;.. /*. *
90620 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 * Performance st
90630 61 74 69 73 74 69 63 73 0a 20 20 2a 2f 0a 20 20 atistics. */.
90640 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 u64 nAlloc;
90650 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
90660 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f 20 ber of calls to
90670 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 36 34 20 malloc */. u64
90680 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 totalAlloc;
90690 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 /* Total of all
906a0 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 2d 20 69 malloc calls - i
906b0 6e 63 6c 75 64 65 73 20 69 6e 74 65 72 6e 61 6c ncludes internal
906c0 20 66 72 61 67 20 2a 2f 0a 20 20 75 36 34 20 74 frag */. u64 t
906d0 6f 74 61 6c 45 78 63 65 73 73 3b 20 20 20 20 2f otalExcess; /
906e0 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72 6e 61 6c * Total internal
906f0 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a fragmentation *
90700 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 4f /. u32 currentO
90710 75 74 3b 20 20 20 20 20 2f 2a 20 43 75 72 72 65 ut; /* Curre
90720 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20 69 6e 63 nt checkout, inc
90730 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e 61 6c 20 luding internal
90740 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f fragmentation */
90750 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 43 6f . u32 currentCo
90760 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e unt; /* Curren
90770 74 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 73 74 t number of dist
90780 69 6e 63 74 20 63 68 65 63 6b 6f 75 74 73 20 2a inct checkouts *
90790 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75 74 3b 20 /. u32 maxOut;
907a0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
907b0 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 um instantaneous
907c0 20 63 75 72 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 currentOut */.
907d0 20 75 33 32 20 6d 61 78 43 6f 75 6e 74 3b 20 20 u32 maxCount;
907e0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
907f0 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 instantaneous cu
90800 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 rrentCount */.
90810 75 33 32 20 6d 61 78 52 65 71 75 65 73 74 3b 20 u32 maxRequest;
90820 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 61 /* Largest a
90830 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78 63 6c 75 llocation (exclu
90840 73 69 76 65 20 6f 66 20 69 6e 74 65 72 6e 61 6c sive of internal
90850 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a 20 20 2f frag) */. . /
90860 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20 6f 66 20 *. ** Lists of
90870 66 72 65 65 20 62 6c 6f 63 6b 73 20 6f 66 20 76 free blocks of v
90880 61 72 69 6f 75 73 20 73 69 7a 65 73 2e 0a 20 20 arious sizes..
90890 2a 2f 0a 20 20 69 6e 74 20 61 69 46 72 65 65 6c */. int aiFreel
908a0 69 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a ist[LOGMAX+1];..
908b0 20 20 2f 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 /*. ** Space
908c0 66 6f 72 20 74 72 61 63 6b 69 6e 67 20 77 68 69 for tracking whi
908d0 63 68 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 ch blocks are ch
908e0 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 74 68 ecked out and th
908f0 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 e size. ** of e
90900 61 63 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 ach block. One
90910 62 79 74 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a byte per block..
90920 20 20 2a 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c */. u8 *aCtrl
90930 3b 0a 0a 7d 20 6d 65 6d 35 20 3d 20 7b 20 31 39 ;..} mem5 = { 19
90940 38 30 34 31 36 37 20 7d 3b 0a 0a 23 64 65 66 69 804167 };..#defi
90950 6e 65 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 ne mem5 GLOBAL(s
90960 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c truct Mem5Global
90970 2c 20 6d 65 6d 35 29 0a 0a 23 64 65 66 69 6e 65 , mem5)..#define
90980 20 4d 45 4d 35 4c 49 4e 4b 28 69 64 78 29 20 28 MEM5LINK(idx) (
90990 28 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28 26 6d 65 (Mem5Link *)(&me
909a0 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d m5.zPool[(idx)*m
909b0 65 6d 35 2e 6e 41 74 6f 6d 5d 29 29 0a 0a 2f 2a em5.nAtom]))../*
909c0 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 .** Unlink the c
909d0 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f hunk at mem5.aPo
909e0 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 ol[i] from list
909f0 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a it is currently.
90a00 2a 2a 20 6f 6e 2e 20 20 49 74 20 73 68 6f 75 6c ** on. It shoul
90a10 64 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 d be found on me
90a20 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c m5.aiFreelist[iL
90a30 6f 67 73 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 ogsize]..*/.stat
90a40 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 55 ic void memsys5U
90a50 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 nlink(int i, int
90a60 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e iLogsize){. in
90a70 74 20 6e 65 78 74 2c 20 70 72 65 76 3b 0a 20 20 t next, prev;.
90a80 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
90a90 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b i<mem5.nBlock );
90aa0 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 . assert( iLogs
90ab0 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 ize>=0 && iLogsi
90ac0 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 ze<=LOGMAX );.
90ad0 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 assert( (mem5.aC
90ae0 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f trl[i] & CTRL_LO
90af0 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 GSIZE)==iLogsize
90b00 20 29 3b 0a 0a 20 20 6e 65 78 74 20 3d 20 4d 45 );.. next = ME
90b10 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b M5LINK(i)->next;
90b20 0a 20 20 70 72 65 76 20 3d 20 4d 45 4d 35 4c 49 . prev = MEM5LI
90b30 4e 4b 28 69 29 2d 3e 70 72 65 76 3b 0a 20 20 69 NK(i)->prev;. i
90b40 66 28 20 70 72 65 76 3c 30 20 29 7b 0a 20 20 20 f( prev<0 ){.
90b50 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
90b60 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 6e 65 78 [iLogsize] = nex
90b70 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
90b80 4d 45 4d 35 4c 49 4e 4b 28 70 72 65 76 29 2d 3e MEM5LINK(prev)->
90b90 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d next = next;. }
90ba0 0a 20 20 69 66 28 20 6e 65 78 74 3e 3d 30 20 29 . if( next>=0 )
90bb0 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e {. MEM5LINK(n
90bc0 65 78 74 29 2d 3e 70 72 65 76 20 3d 20 70 72 65 ext)->prev = pre
90bd0 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 v;. }.}../*.**
90be0 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 Link the chunk a
90bf0 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 t mem5.aPool[i]
90c00 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 so that is on th
90c10 65 20 69 4c 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 e iLogsize.** fr
90c20 65 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 ee list..*/.stat
90c30 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c ic void memsys5L
90c40 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 ink(int i, int i
90c50 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 Logsize){. int
90c60 78 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c x;. assert( sql
90c70 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
90c80 6d 65 6d 35 2e 6d 75 74 65 78 29 20 29 3b 0a 20 mem5.mutex) );.
90c90 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 assert( i>=0 &&
90ca0 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 i<mem5.nBlock )
90cb0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 ;. assert( iLog
90cc0 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 size>=0 && iLogs
90cd0 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 ize<=LOGMAX );.
90ce0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 assert( (mem5.a
90cf0 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c Ctrl[i] & CTRL_L
90d00 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a OGSIZE)==iLogsiz
90d10 65 20 29 3b 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 e );.. x = MEM5
90d20 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 20 3d 20 LINK(i)->next =
90d30 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
90d40 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d iLogsize];. MEM
90d50 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 20 3d 5LINK(i)->prev =
90d60 20 2d 31 3b 0a 20 20 69 66 28 20 78 3e 3d 30 20 -1;. if( x>=0
90d70 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 78 ){. assert( x
90d80 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a <mem5.nBlock );.
90d90 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d MEM5LINK(x)-
90da0 3e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 >prev = i;. }.
90db0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
90dc0 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 69 3b 0a [iLogsize] = i;.
90dd0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
90de0 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 STATIC_MEM mutex
90df0 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
90e00 68 65 6c 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 held, obtain it
90e10 6e 6f 77 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a now. The mutex.*
90e20 2a 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 * will already b
90e30 65 20 68 65 6c 64 20 28 6f 62 74 61 69 6e 65 64 e held (obtained
90e40 20 62 79 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c by code in mall
90e50 6f 63 2e 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69 oc.c) if.** sqli
90e60 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
90e70 62 4d 65 6d 53 74 61 74 20 69 73 20 74 72 75 65 bMemStat is true
90e80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
90e90 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 76 6f memsys5Enter(vo
90ea0 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 id){. if( sqlit
90eb0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 e3GlobalConfig.b
90ec0 4d 65 6d 73 74 61 74 3d 3d 30 20 26 26 20 6d 65 Memstat==0 && me
90ed0 6d 35 2e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 m5.mutex==0 ){.
90ee0 20 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 mem5.mutex =
90ef0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
90f00 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
90f10 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a TATIC_MEM);. }.
90f20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
90f30 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65 78 enter(mem5.mutex
90f40 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 );.}.static void
90f50 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 76 6f memsys5Leave(vo
90f60 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d id){. sqlite3_m
90f70 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35 2e utex_leave(mem5.
90f80 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a mutex);.}../*.**
90f90 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 Return the size
90fa0 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 of an outstandi
90fb0 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 ng allocation, i
90fc0 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a n bytes. The.**
90fd0 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f size returned o
90fe0 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 mits the 8-byte
90ff0 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e header overhead.
91000 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 This only.** w
91010 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 orks for chunks
91020 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 that are current
91030 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a ly checked out..
91040 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
91050 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64 20 2a msys5Size(void *
91060 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 p){. int iSize
91070 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a = 0;. if( p ){.
91080 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28 75 38 int i = ((u8
91090 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 *)p-mem5.zPool)
910a0 2f 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 20 /mem5.nAtom;.
910b0 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 assert( i>=0 &&
910c0 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 i<mem5.nBlock )
910d0 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d 20 6d 65 ;. iSize = me
910e0 6d 35 2e 6e 41 74 6f 6d 20 2a 20 28 31 20 3c 3c m5.nAtom * (1 <<
910f0 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 (mem5.aCtrl[i]&
91100 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a CTRL_LOGSIZE));.
91110 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 53 69 }. return iSi
91120 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e ze;.}../*.** Fin
91130 64 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 d the first entr
91140 79 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 y on the freelis
91150 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c t iLogsize. Unl
91160 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72 ink that.** entr
91170 79 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 73 y and return its
91180 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 index. .*/.stat
91190 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e ic int memsys5Un
911a0 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74 20 69 4c linkFirst(int iL
911b0 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69 ogsize){. int i
911c0 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 74 3b 0a ;. int iFirst;.
911d0 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 . assert( iLogs
911e0 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 ize>=0 && iLogsi
911f0 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 ze<=LOGMAX );.
91200 69 20 3d 20 69 46 69 72 73 74 20 3d 20 6d 65 6d i = iFirst = mem
91210 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
91220 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74 gsize];. assert
91230 28 20 69 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20 ( iFirst>=0 );.
91240 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 while( i>0 ){.
91250 20 20 20 69 66 28 20 69 3c 69 46 69 72 73 74 20 if( i<iFirst
91260 29 20 69 46 69 72 73 74 20 3d 20 69 3b 0a 20 20 ) iFirst = i;.
91270 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 i = MEM5LINK(i
91280 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d )->next;. }. m
91290 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69 emsys5Unlink(iFi
912a0 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a rst, iLogsize);.
912b0 20 20 72 65 74 75 72 6e 20 69 46 69 72 73 74 3b return iFirst;
912c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
912d0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f a block of memo
912e0 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e ry of at least n
912f0 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a Bytes in size..*
91300 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 * Return NULL if
91310 20 75 6e 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 unable..*/.stat
91320 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 ic void *memsys5
91330 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e 74 MallocUnsafe(int
91340 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 69 nByte){. int i
91350 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 ; /* I
91360 6e 64 65 78 20 6f 66 20 61 20 6d 65 6d 35 2e 61 ndex of a mem5.a
91370 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 2f 0a 20 Pool[] slot */.
91380 20 69 6e 74 20 69 42 69 6e 3b 20 20 20 20 20 20 int iBin;
91390 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 /* Index into
913a0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
913b0 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 46 75 6c 6c ] */. int iFull
913c0 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 Sz; /* Size
913d0 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f of allocation ro
913e0 75 6e 64 65 64 20 75 70 20 74 6f 20 70 6f 77 65 unded up to powe
913f0 72 20 6f 66 20 32 20 2a 2f 0a 20 20 69 6e 74 20 r of 2 */. int
91400 69 4c 6f 67 73 69 7a 65 3b 20 20 20 20 2f 2a 20 iLogsize; /*
91410 4c 6f 67 32 20 6f 66 20 69 46 75 6c 6c 53 7a 2f Log2 of iFullSz/
91420 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a 20 20 2f POW2_MIN */.. /
91430 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 * Keep track of
91440 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f the maximum allo
91450 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e 20 cation request.
91460 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 Even unfulfille
91470 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 20 d. ** requests
91480 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 are counted */.
91490 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 3e if( (u32)nByte>
914a0 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 mem5.maxRequest
914b0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52 ){. mem5.maxR
914c0 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a equest = nByte;.
914d0 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 20 }.. /* Round
914e0 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68 65 20 nByte up to the
914f0 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77 65 72 next valid power
91500 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f 72 of two */. for
91510 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 6e 41 (iFullSz=mem5.nA
91520 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d 30 3b tom, iLogsize=0;
91530 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 iFullSz<nByte;
91540 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c iFullSz *= 2, iL
91550 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f ogsize++){}.. /
91560 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d 65 6d 35 * Make sure mem5
91570 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 .aiFreelist[iLog
91580 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 20 61 size] contains a
91590 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 72 65 65 t least one free
915a0 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 . ** block. If
915b0 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c 69 74 not, then split
915c0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68 65 20 a block of the
915d0 6e 65 78 74 20 6c 61 72 67 65 72 20 70 6f 77 65 next larger powe
915e0 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 69 6e r of. ** two in
915f0 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74 65 order to create
91600 20 61 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 a new free bloc
91610 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67 73 69 k of size iLogsi
91620 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 ze.. */. for(i
91630 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 6d 65 Bin=iLogsize; me
91640 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 42 m5.aiFreelist[iB
91650 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c 3d 4c in]<0 && iBin<=L
91660 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d OGMAX; iBin++){}
91670 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f 47 4d . if( iBin>LOGM
91680 41 58 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 AX ) return 0;.
91690 20 69 20 3d 20 6d 65 6d 73 79 73 35 55 6e 6c 69 i = memsys5Unli
916a0 6e 6b 46 69 72 73 74 28 69 42 69 6e 29 3b 0a 20 nkFirst(iBin);.
916b0 20 77 68 69 6c 65 28 20 69 42 69 6e 3e 69 4c 6f while( iBin>iLo
916c0 67 73 69 7a 65 20 29 7b 0a 20 20 20 20 69 6e 74 gsize ){. int
916d0 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 20 20 69 newSize;.. i
916e0 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77 53 69 Bin--;. newSi
916f0 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69 6e 3b 0a ze = 1 << iBin;.
91700 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 mem5.aCtrl[i
91710 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43 54 52 4c +newSize] = CTRL
91720 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b 0a 20 20 _FREE | iBin;.
91730 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 2b memsys5Link(i+
91740 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e 29 3b 0a newSize, iBin);.
91750 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c }. mem5.aCtrl
91760 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a 65 3b 0a [i] = iLogsize;.
91770 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 61 6c 6c . /* Update all
91780 6f 63 61 74 6f 72 20 70 65 72 66 6f 72 6d 61 6e ocator performan
91790 63 65 20 73 74 61 74 69 73 74 69 63 73 2e 20 2a ce statistics. *
917a0 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 2b /. mem5.nAlloc+
917b0 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 +;. mem5.totalA
917c0 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b lloc += iFullSz;
917d0 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 . mem5.totalExc
917e0 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 7a 20 2d ess += iFullSz -
917f0 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 63 nByte;. mem5.c
91800 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b 0a 20 urrentCount++;.
91810 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 mem5.currentOut
91820 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 += iFullSz;. i
91830 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 f( mem5.maxCount
91840 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 <mem5.currentCou
91850 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 nt ) mem5.maxCou
91860 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e nt = mem5.curren
91870 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28 20 6d 65 tCount;. if( me
91880 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35 2e 63 m5.maxOut<mem5.c
91890 75 72 72 65 6e 74 4f 75 74 20 29 20 6d 65 6d 35 urrentOut ) mem5
918a0 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d 35 2e 63 .maxOut = mem5.c
918b0 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 20 2f 2a urrentOut;.. /*
918c0 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
918d0 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 r to the allocat
918e0 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a 20 20 ed memory. */.
918f0 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 26 6d return (void*)&m
91900 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65 6d 35 em5.zPool[i*mem5
91910 2e 6e 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a .nAtom];.}../*.*
91920 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 73 74 61 * Free an outsta
91930 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c nding memory all
91940 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 ocation..*/.stat
91950 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 ic void memsys5F
91960 72 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a reeUnsafe(void *
91970 70 4f 6c 64 29 7b 0a 20 20 75 33 32 20 73 69 7a pOld){. u32 siz
91980 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 69 e, iLogsize;. i
91990 6e 74 20 69 42 6c 6f 63 6b 3b 20 20 20 20 20 20 nt iBlock;
919a0 20 20 20 20 20 20 20 0a 0a 20 20 2f 2a 20 53 65 .. /* Se
919b0 74 20 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 t iBlock to the
919c0 69 6e 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f index of the blo
919d0 63 6b 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ck pointed to by
919e0 20 70 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74 pOld in . ** t
919f0 68 65 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35 he array of mem5
91a00 2e 6e 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63 .nAtom byte bloc
91a10 6b 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ks pointed to by
91a20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a mem5.zPool.. *
91a30 2f 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75 /. iBlock = ((u
91a40 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 8 *)pOld-mem5.zP
91a50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d 3b ool)/mem5.nAtom;
91a60 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 .. /* Check tha
91a70 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f t the pointer pO
91a80 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 ld points to a v
91a90 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62 alid, non-free b
91aa0 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72 lock. */. asser
91ab0 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20 t( iBlock>=0 &&
91ac0 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f iBlock<mem5.nBlo
91ad0 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ck );. assert(
91ae0 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 ((u8 *)pOld-mem5
91af0 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 6e 41 74 .zPool)%mem5.nAt
91b00 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 om==0 );. asser
91b10 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 t( (mem5.aCtrl[i
91b20 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46 52 Block] & CTRL_FR
91b30 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c 6f EE)==0 );.. iLo
91b40 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43 74 gsize = mem5.aCt
91b50 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 rl[iBlock] & CTR
91b60 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69 7a L_LOGSIZE;. siz
91b70 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65 3b e = 1<<iLogsize;
91b80 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63 . assert( iBloc
91b90 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d 65 k+size-1<(u32)me
91ba0 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20 m5.nBlock );..
91bb0 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 mem5.aCtrl[iBloc
91bc0 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b k] |= CTRL_FREE;
91bd0 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 . mem5.aCtrl[iB
91be0 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d 20 lock+size-1] |=
91bf0 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73 73 CTRL_FREE;. ass
91c00 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e ert( mem5.curren
91c10 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61 73 tCount>0 );. as
91c20 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 sert( mem5.curre
91c30 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65 6d ntOut>=(size*mem
91c40 35 2e 6e 41 74 6f 6d 29 20 29 3b 0a 20 20 6d 65 5.nAtom) );. me
91c50 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2d m5.currentCount-
91c60 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e -;. mem5.curren
91c70 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d 65 6d tOut -= size*mem
91c80 35 2e 6e 41 74 6f 6d 3b 0a 20 20 61 73 73 65 72 5.nAtom;. asser
91c90 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f t( mem5.currentO
91ca0 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 ut>0 || mem5.cur
91cb0 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 29 3b 0a rentCount==0 );.
91cc0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 assert( mem5.c
91cd0 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 7c 7c urrentCount>0 ||
91ce0 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 mem5.currentOut
91cf0 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 ==0 );.. mem5.a
91d00 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 Ctrl[iBlock] = C
91d10 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 TRL_FREE | iLogs
91d20 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28 20 69 4c ize;. while( iL
91d30 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 20 29 7b ogsize<LOGMAX ){
91d40 0a 20 20 20 20 69 6e 74 20 69 42 75 64 64 79 3b . int iBuddy;
91d50 0a 20 20 20 20 69 66 28 20 28 69 42 6c 6f 63 6b . if( (iBlock
91d60 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26 20 31 20 >>iLogsize) & 1
91d70 29 7b 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 ){. iBuddy
91d80 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69 7a 65 3b = iBlock - size;
91d90 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
91da0 20 20 69 42 75 64 64 79 20 3d 20 69 42 6c 6f 63 iBuddy = iBloc
91db0 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a k + size;. }.
91dc0 20 20 20 20 61 73 73 65 72 74 28 20 69 42 75 64 assert( iBud
91dd0 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 dy>=0 );. if(
91de0 20 28 69 42 75 64 64 79 2b 28 31 3c 3c 69 4c 6f (iBuddy+(1<<iLo
91df0 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e 6e 42 6c gsize))>mem5.nBl
91e00 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ock ) break;.
91e10 20 69 66 28 20 6d 65 6d 35 2e 61 43 74 72 6c 5b if( mem5.aCtrl[
91e20 69 42 75 64 64 79 5d 21 3d 28 43 54 52 4c 5f 46 iBuddy]!=(CTRL_F
91e30 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 29 20 REE | iLogsize)
91e40 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 6d 65 6d ) break;. mem
91e50 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42 75 64 64 sys5Unlink(iBudd
91e60 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 y, iLogsize);.
91e70 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b 0a 20 20 iLogsize++;.
91e80 20 20 69 66 28 20 69 42 75 64 64 79 3c 69 42 6c if( iBuddy<iBl
91e90 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ock ){. mem
91ea0 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 5.aCtrl[iBuddy]
91eb0 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c = CTRL_FREE | iL
91ec0 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 ogsize;. me
91ed0 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d m5.aCtrl[iBlock]
91ee0 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 42 6c 6f = 0;. iBlo
91ef0 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a 20 20 20 ck = iBuddy;.
91f00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 }else{. me
91f10 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d m5.aCtrl[iBlock]
91f20 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 = CTRL_FREE | i
91f30 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d Logsize;. m
91f40 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 em5.aCtrl[iBuddy
91f50 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 ] = 0;. }.
91f60 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 20 7d 0a size *= 2;. }.
91f70 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 42 memsys5Link(iB
91f80 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a 65 29 3b lock, iLogsize);
91f90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
91fa0 74 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d te nBytes of mem
91fb0 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ory.*/.static vo
91fc0 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f id *memsys5Mallo
91fd0 63 28 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 c(int nBytes){.
91fe0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a sqlite3_int64 *
91ff0 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 42 79 p = 0;. if( nBy
92000 74 65 73 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d tes>0 ){. mem
92010 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 20 sys5Enter();.
92020 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c p = memsys5Mall
92030 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 ocUnsafe(nBytes)
92040 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 65 61 ;. memsys5Lea
92050 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ve();. }. retu
92060 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a rn (void*)p; .}.
92070 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f ./*.** Free memo
92080 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ry..*/.static vo
92090 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65 28 76 id memsys5Free(v
920a0 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 oid *pPrior){.
920b0 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b if( pPrior==0 ){
920c0 0a 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20 .assert(0);.
920d0 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6d 65 return;. }. me
920e0 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 msys5Enter();.
920f0 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 memsys5FreeUnsaf
92100 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d e(pPrior);. mem
92110 73 79 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d sys5Leave(); .}
92120 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
92130 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 he size of an ex
92140 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c isting memory al
92150 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 location.*/.stat
92160 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 ic void *memsys5
92170 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 Realloc(void *pP
92180 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 rior, int nBytes
92190 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 ){. int nOld;.
921a0 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 void *p;. if(
921b0 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 pPrior==0 ){.
921c0 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 35 4d return memsys5M
921d0 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 alloc(nBytes);.
921e0 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c }. if( nBytes<
921f0 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 =0 ){. memsys
92200 35 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 5Free(pPrior);.
92210 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
92220 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 . nOld = memsys
92230 35 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 5Size(pPrior);.
92240 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c if( nBytes<=nOl
92250 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 d ){. return
92260 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 pPrior;. }. me
92270 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 msys5Enter();.
92280 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f p = memsys5Mallo
92290 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b cUnsafe(nBytes);
922a0 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
922b0 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 memcpy(p, pPrior
922c0 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 6d 65 6d , nOld);. mem
922d0 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 70 sys5FreeUnsafe(p
922e0 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 Prior);. }. me
922f0 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 msys5Leave();.
92300 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
92310 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 ** Round up a re
92320 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 quest size to th
92330 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c e next valid all
92340 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f ocation size..*/
92350 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 .static int mems
92360 79 73 35 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e ys5Roundup(int n
92370 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a ){. int iFullSz
92380 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d ;. for(iFullSz=
92390 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 20 69 46 75 6c mem5.nAtom; iFul
923a0 6c 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20 2a lSz<n; iFullSz *
923b0 3d 20 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 = 2);. return i
923c0 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a 73 74 61 74 69 FullSz;.}..stati
923d0 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 4c 6f 67 c int memsys5Log
923e0 28 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 (int iValue){.
923f0 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 6f 72 28 int iLog;. for(
92400 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 4c 6f 67 iLog=0; (1<<iLog
92410 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f 67 2b 2b )<iValue; iLog++
92420 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 4c 6f 67 );. return iLog
92430 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 ;.}../*.** Initi
92440 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c alize this modul
92450 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
92460 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 76 6f 69 memsys5Init(voi
92470 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 69 d *NotUsed){. i
92480 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 6e 42 79 nt ii;. int nBy
92490 74 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 te = sqlite3Glob
924a0 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 3b 0a alConfig.nHeap;.
924b0 20 20 75 38 20 2a 7a 42 79 74 65 20 3d 20 28 75 u8 *zByte = (u
924c0 38 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 8 *)sqlite3Globa
924d0 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 lConfig.pHeap;.
924e0 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20 20 int nMinLog;
924f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
92500 20 4c 6f 67 20 6f 66 20 6d 69 6e 69 6d 75 6d 20 Log of minimum
92510 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 allocation size
92520 69 6e 20 62 79 74 65 73 2a 2f 0a 20 20 69 6e 74 in bytes*/. int
92530 20 69 4f 66 66 73 65 74 3b 0a 0a 20 20 55 4e 55 iOffset;.. UNU
92540 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
92550 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 21 tUsed);.. if( !
92560 7a 42 79 74 65 20 29 7b 0a 20 20 20 20 72 65 74 zByte ){. ret
92570 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
92580 3b 0a 20 20 7d 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 ;. }.. nMinLog
92590 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 = memsys5Log(sq
925a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
925b0 67 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 g.mnReq);. mem5
925c0 2e 6e 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69 .nAtom = (1<<nMi
925d0 6e 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20 nLog);. while(
925e0 28 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35 (int)sizeof(Mem5
925f0 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 6e 41 74 6f 6d Link)>mem5.nAtom
92600 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6e 41 74 ){. mem5.nAt
92610 6f 6d 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 om = mem5.nAtom
92620 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d << 1;. }.. mem
92630 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 5.nBlock = (nByt
92640 65 20 2f 20 28 6d 65 6d 35 2e 6e 41 74 6f 6d 2b e / (mem5.nAtom+
92650 73 69 7a 65 6f 66 28 75 38 29 29 29 3b 0a 20 20 sizeof(u8)));.
92660 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79 mem5.zPool = zBy
92670 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c te;. mem5.aCtrl
92680 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a = (u8 *)&mem5.z
92690 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b Pool[mem5.nBlock
926a0 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 0a 20 *mem5.nAtom];..
926b0 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c for(ii=0; ii<=L
926c0 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20 OGMAX; ii++){.
926d0 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 mem5.aiFreelis
926e0 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a t[ii] = -1;. }.
926f0 0a 20 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a . iOffset = 0;.
92700 20 20 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b for(ii=LOGMAX;
92710 20 69 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20 ii>=0; ii--){.
92720 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 int nAlloc =
92730 28 31 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28 (1<<ii);. if(
92740 20 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 (iOffset+nAlloc
92750 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 )<=mem5.nBlock )
92760 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 {. mem5.aCt
92770 72 6c 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69 rl[iOffset] = ii
92780 20 7c 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 | CTRL_FREE;.
92790 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 memsys5Link(
927a0 69 4f 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20 iOffset, ii);.
927b0 20 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e iOffset += n
927c0 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 Alloc;. }.
927d0 20 61 73 73 65 72 74 28 28 69 4f 66 66 73 65 74 assert((iOffset
927e0 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42 +nAlloc)>mem5.nB
927f0 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 lock);. }.. re
92800 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
92810 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 }../*.** Deiniti
92820 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c alize this modul
92830 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
92840 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f 77 d memsys5Shutdow
92850 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 n(void *NotUsed)
92860 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
92870 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
92880 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a return;.}../*.*
92890 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 * Open the file
928a0 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 indicated and wr
928b0 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c ite a log of all
928c0 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 unfreed memory
928d0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 .** allocations
928e0 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a into that log..*
928f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
92900 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
92910 73 79 73 35 44 75 6d 70 28 63 6f 6e 73 74 20 63 sys5Dump(const c
92920 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
92930 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
92940 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 EBUG. FILE *out
92950 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b ;. int i, j, n;
92960 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 0a . int nMinLog;.
92970 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 . if( zFilename
92980 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 ==0 || zFilename
92990 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 [0]==0 ){. ou
929a0 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 t = stdout;. }e
929b0 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 lse{. out = f
929c0 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 open(zFilename,
929d0 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 "w");. if( ou
929e0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 t==0 ){. fp
929f0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a rintf(stderr, "*
92a00 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 * Unable to outp
92a10 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 ut memory debug
92a20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a output log: %s *
92a30 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 *\n",.
92a40 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c zFil
92a50 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 ename);. re
92a60 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a turn;. }. }.
92a70 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 memsys5Enter()
92a80 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 ;. nMinLog = me
92a90 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 2e 6e 41 msys5Log(mem5.nA
92aa0 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b tom);. for(i=0;
92ab0 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 20 69 2b i<=LOGMAX && i+
92ac0 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 2b 2b 29 nMinLog<32; i++)
92ad0 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30 2c 20 6a {. for(n=0, j
92ae0 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 =mem5.aiFreelist
92af0 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 3d 20 4d [i]; j>=0; j = M
92b00 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e 65 78 74 EM5LINK(j)->next
92b10 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 66 70 72 , n++){}. fpr
92b20 69 6e 74 66 28 6f 75 74 2c 20 22 66 72 65 65 6c intf(out, "freel
92b30 69 73 74 20 69 74 65 6d 73 20 6f 66 20 73 69 7a ist items of siz
92b40 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20 6d 65 6d e %d: %d\n", mem
92b50 35 2e 6e 41 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29 5.nAtom << i, n)
92b60 3b 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 ;. }. fprintf(
92b70 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f out, "mem5.nAllo
92b80 63 20 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e c = %llu\n
92b90 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b ", mem5.nAlloc);
92ba0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
92bb0 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 "mem5.totalAlloc
92bc0 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 = %llu\n", me
92bd0 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a m5.totalAlloc);.
92be0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
92bf0 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 mem5.totalExcess
92c00 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d = %llu\n", mem
92c10 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 29 3b 0a 5.totalExcess);.
92c20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
92c30 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 mem5.currentOut
92c40 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e = %u\n", mem5.
92c50 63 75 72 72 65 6e 74 4f 75 74 29 3b 0a 20 20 66 currentOut);. f
92c60 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d printf(out, "mem
92c70 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 3d 5.currentCount =
92c80 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 %u\n", mem5.cur
92c90 72 65 6e 74 43 6f 75 6e 74 29 3b 0a 20 20 66 70 rentCount);. fp
92ca0 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 rintf(out, "mem5
92cb0 2e 6d 61 78 4f 75 74 20 20 20 20 20 20 20 3d 20 .maxOut =
92cc0 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f %u\n", mem5.maxO
92cd0 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f ut);. fprintf(o
92ce0 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 43 6f 75 ut, "mem5.maxCou
92cf0 6e 74 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20 nt = %u\n",
92d00 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a mem5.maxCount);.
92d10 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
92d20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 mem5.maxRequest
92d30 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e = %u\n", mem5.
92d40 6d 61 78 52 65 71 75 65 73 74 29 3b 0a 20 20 6d maxRequest);. m
92d50 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 emsys5Leave();.
92d60 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 if( out==stdout
92d70 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 ){. fflush(s
92d80 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b tdout);. }else{
92d90 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 . fclose(out)
92da0 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e ;. }.#else. UN
92db0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a USED_PARAMETER(z
92dc0 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 Filename);.#endi
92dd0 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 f.}../*.** This
92de0 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f routine is the o
92df0 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 nly routine in t
92e00 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 his file with ex
92e10 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 ternal .** linka
92e20 67 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 ge. It returns a
92e30 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 pointer to a st
92e40 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d atic sqlite3_mem
92e50 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 _methods.** stru
92e60 63 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 ct populated wit
92e70 68 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 h the memsys5 me
92e80 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 thods..*/.SQLITE
92e90 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
92ea0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
92eb0 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 ds *sqlite3MemGe
92ec0 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a tMemsys5(void){.
92ed0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
92ee0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
92ef0 64 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 ds memsys5Method
92f00 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 s = {. memsy
92f10 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d s5Malloc,. m
92f20 65 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 emsys5Free,.
92f30 20 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c memsys5Realloc,
92f40 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a . memsys5Siz
92f50 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 e,. memsys5R
92f60 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d oundup,. mem
92f70 73 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d sys5Init,. m
92f80 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a emsys5Shutdown,.
92f90 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 0. };. re
92fa0 74 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 turn &memsys5Met
92fb0 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 hods;.}..#endif
92fc0 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 /* SQLITE_ENABLE
92fd0 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a _MEMSYS5 */../**
92fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
92ff0 20 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a of mem5.c *****
93000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
93030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
93040 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 in file mutex.c
93050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
93080 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 ** 2007 August 1
93090 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 4.**.** The auth
930a0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
930b0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
930c0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
930d0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
930e0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
930f0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
93100 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
93110 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
93120 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
93130 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
93140 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
93150 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
93160 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
93170 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
93180 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
93190 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
931a0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
931b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
931c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
931d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
931e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
931f0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
93200 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e ntains the C fun
93210 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c ctions that impl
93220 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a ement mutexes..*
93230 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
93240 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 ontains code tha
93250 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f t is common acro
93260 73 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 ss all mutex imp
93270 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a lementations...*
93280 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e *.** $Id: mutex.
93290 63 2c 76 20 31 2e 32 39 20 32 30 30 38 2f 31 30 c,v 1.29 2008/10
932a0 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 /07 15:25:48 drh
932b0 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 Exp $.*/..#ifnd
932c0 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ef SQLITE_MUTEX_
932d0 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 OMIT./*.** Initi
932e0 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 alize the mutex
932f0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 system..*/.SQLIT
93300 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
93310 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 lite3MutexInit(v
93320 6f 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 oid){ . int rc
93330 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
93340 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
93350 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 Config.bCoreMute
93360 78 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 x ){. if( !sq
93370 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
93380 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c g.mutex.xMutexAl
93390 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 loc ){. /*
933a0 49 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c If the xMutexAll
933b0 6f 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f oc method has no
933c0 74 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e t been set, then
933d0 20 74 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f the user did no
933e0 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 t. ** insta
933f0 6c 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 ll a mutex imple
93400 6d 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71 mentation via sq
93410 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 lite3_config() p
93420 72 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a rior to . *
93430 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 * sqlite3_initia
93440 6c 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c lize() being cal
93450 6c 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 led. This block
93460 63 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20 copies pointers
93470 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 to. ** the
93480 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e default implemen
93490 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 tation into the
934a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
934b0 66 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20 fig structure..
934c0 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
934d0 20 54 68 65 20 64 61 6e 67 65 72 20 69 73 20 74 The danger is t
934e0 68 61 74 20 61 6c 74 68 6f 75 67 68 20 73 71 6c hat although sql
934f0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 ite3_config() is
93500 20 6e 6f 74 20 61 20 74 68 72 65 61 64 73 61 66 not a threadsaf
93510 65 0a 20 20 20 20 20 20 2a 2a 20 41 50 49 2c 20 e. ** API,
93520 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
93530 7a 65 28 29 20 69 73 2c 20 61 6e 64 20 73 6f 20 ze() is, and so
93540 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 multiple threads
93550 20 6d 61 79 20 62 65 0a 20 20 20 20 20 20 2a 2a may be. **
93560 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 72 attempting to r
93570 75 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e un this function
93580 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e simultaneously.
93590 20 54 6f 20 67 75 61 72 64 20 77 72 69 74 65 0a To guard write.
935a0 20 20 20 20 20 20 2a 2a 20 61 63 63 65 73 73 20 ** access
935b0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 47 6c to the sqlite3Gl
935c0 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74 72 75 63 obalConfig struc
935d0 74 75 72 65 2c 20 74 68 65 20 27 4d 41 53 54 45 ture, the 'MASTE
935e0 52 27 20 73 74 61 74 69 63 20 6d 75 74 65 78 0a R' static mutex.
935f0 20 20 20 20 20 20 2a 2a 20 69 73 20 6f 62 74 61 ** is obta
93600 69 6e 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 ined before modi
93610 66 79 69 6e 67 20 69 74 2e 0a 20 20 20 20 20 20 fying it..
93620 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
93630 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a _mutex_methods *
93640 70 20 3d 20 73 71 6c 69 74 65 33 44 65 66 61 75 p = sqlite3Defau
93650 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 ltMutex();.
93660 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
93670 70 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 0a pMaster = 0;. .
93680 20 20 20 20 20 20 72 63 20 3d 20 70 2d 3e 78 4d rc = p->xM
93690 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 20 20 utexInit();.
936a0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
936b0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 _OK ){. p
936c0 4d 61 73 74 65 72 20 3d 20 70 2d 3e 78 4d 75 74 Master = p->xMut
936d0 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
936e0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
936f0 45 52 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 ER);. ass
93700 65 72 74 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ert(pMaster);.
93710 20 20 20 20 20 20 70 2d 3e 78 4d 75 74 65 78 45 p->xMutexE
93720 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 nter(pMaster);.
93730 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 assert( s
93740 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
93750 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 ig.mutex.xMutexA
93760 6c 6c 6f 63 3d 3d 30 20 0a 20 20 20 20 20 20 20 lloc==0 .
93770 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
93780 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
93790 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 3d 3d ex.xMutexAlloc==
937a0 70 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 0a 20 p->xMutexAlloc.
937b0 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
937c0 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c if( !sqlite3Gl
937d0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
937e0 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b 0a .xMutexAlloc ){.
937f0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
93800 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
93810 74 65 78 20 3d 20 2a 70 3b 0a 20 20 20 20 20 20 tex = *p;.
93820 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 78 }. p->x
93830 4d 75 74 65 78 4c 65 61 76 65 28 70 4d 61 73 74 MutexLeave(pMast
93840 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 er);. }.
93850 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
93860 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
93870 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
93880 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 20 20 7d texInit();. }
93890 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 . }.. return r
938a0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 c;.}../*.** Shut
938b0 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 78 20 73 down the mutex s
938c0 79 73 74 65 6d 2e 20 54 68 69 73 20 63 61 6c 6c ystem. This call
938d0 20 66 72 65 65 73 20 72 65 73 6f 75 72 63 65 73 frees resources
938e0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a allocated by.**
938f0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 sqlite3MutexIni
93900 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 t()..*/.SQLITE_P
93910 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
93920 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 e3MutexEnd(void)
93930 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
93940 49 54 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d 20 73 ITE_OK;. rc = s
93950 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
93960 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 45 ig.mutex.xMutexE
93970 6e 64 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 nd();. return r
93980 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 c;.}../*.** Retr
93990 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 ieve a pointer t
939a0 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 o a static mutex
939b0 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e or allocate a n
939c0 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a ew dynamic one..
939d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
939e0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c lite3_mutex *sql
939f0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
93a00 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65 (int id){.#ifnde
93a10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
93a20 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c TOINIT. if( sql
93a30 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
93a40 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 ) ) return 0;.#e
93a50 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 ndif. return sq
93a60 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
93a70 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c g.mutex.xMutexAl
93a80 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49 loc(id);.}..SQLI
93a90 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
93aa0 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 e3_mutex *sqlite
93ab0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 3MutexAlloc(int
93ac0 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 id){. if( !sqli
93ad0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
93ae0 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 bCoreMutex ){.
93af0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
93b00 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
93b10 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
93b20 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 ex.xMutexAlloc(i
93b30 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 d);.}../*.** Fre
93b40 65 20 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 e a dynamic mute
93b50 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 x..*/.SQLITE_API
93b60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 void sqlite3_mu
93b70 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 tex_free(sqlite3
93b80 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 _mutex *p){. if
93b90 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ( p ){. sqlit
93ba0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
93bb0 75 74 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 utex.xMutexFree(
93bc0 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
93bd0 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 Obtain the mute
93be0 78 20 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 x p. If some oth
93bf0 65 72 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 er thread alread
93c00 79 20 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c y has the mutex,
93c10 20 62 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 block.** until
93c20 69 74 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e it can be obtain
93c30 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ed..*/.SQLITE_AP
93c40 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d I void sqlite3_m
93c50 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 utex_enter(sqlit
93c60 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
93c70 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c if( p ){. sql
93c80 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
93c90 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 .mutex.xMutexEnt
93ca0 65 72 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a er(p);. }.}../*
93cb0 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d .** Obtain the m
93cc0 75 74 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 utex p. If succe
93cd0 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 ssful, return SQ
93ce0 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 LITE_OK. Otherwi
93cf0 73 65 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a se, if another.*
93d00 2a 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 * thread holds t
93d10 68 65 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 he mutex and it
93d20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e cannot be obtain
93d30 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ed, return SQLIT
93d40 45 5f 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 E_BUSY..*/.SQLIT
93d50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
93d60 33 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 3_mutex_try(sqli
93d70 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
93d80 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
93d90 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a _OK;. if( p ){.
93da0 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
93db0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
93dc0 75 74 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 utex.xMutexTry(p
93dd0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
93de0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
93df0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
93e00 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 eave() routine e
93e10 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 xits a mutex tha
93e20 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 t was previously
93e30 0a 2a 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 .** entered by t
93e40 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 he same thread.
93e50 20 54 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 The behavior is
93e60 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 undefined if th
93e70 65 20 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e e mutex .** is n
93e80 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ot currently ent
93e90 65 72 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 ered. If a NULL
93ea0 70 6f 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 pointer is passe
93eb0 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 d as an argument
93ec0 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f .** this functio
93ed0 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f n is a no-op..*/
93ee0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
93ef0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
93f00 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
93f10 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 ex *p){. if( p
93f20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
93f30 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
93f40 2e 78 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b .xMutexLeave(p);
93f50 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 . }.}..#ifndef
93f60 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 NDEBUG./*.** The
93f70 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
93f80 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 eld() and sqlite
93f90 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
93fa0 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a ) routine are.**
93fb0 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 intended for us
93fc0 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 e inside assert(
93fd0 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f ) statements..*/
93fe0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
93ff0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
94000 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
94010 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
94020 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c ==0 || sqlite3Gl
94030 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
94040 2e 78 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a .xMutexHeld(p);.
94050 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
94060 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e sqlite3_mutex_n
94070 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d otheld(sqlite3_m
94080 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
94090 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 rn p==0 || sqlit
940a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
940b0 75 74 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 utex.xMutexNothe
940c0 6c 64 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a ld(p);.}.#endif.
940d0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
940e0 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a E_OMIT_MUTEX */.
940f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
94100 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 End of mutex.c
94110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
94140 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
94150 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 Begin file mute
94160 78 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a x_noop.c *******
94170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
94190 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f ./*.** 2008 Octo
941a0 62 65 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 07.**.** The
941b0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
941c0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
941d0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
941e0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
941f0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
94200 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
94210 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
94220 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
94230 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
94240 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
94250 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
94260 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
94270 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
94280 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
94290 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
942a0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
942b0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
942c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
942d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
942e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
942f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94300 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
94310 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
94320 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
94330 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
94340 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 es..**.** This i
94350 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e mplementation in
94360 20 74 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 this file does
94370 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 not provide any
94380 6d 75 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 mutual.** exclus
94390 69 6f 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 ion and is thus
943a0 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 suitable for use
943b0 20 6f 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 only in applica
943c0 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 tions.** that us
943d0 65 20 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 e SQLite in a si
943e0 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 ngle thread. Th
943f0 65 20 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e e routines defin
94400 65 64 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 ed.** here are p
94410 6c 61 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 lace-holders. A
94420 70 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 pplications can
94430 73 75 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 substitute worki
94440 6e 67 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 ng.** mutex rout
94450 69 6e 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 ines at start-ti
94460 6d 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a me using the.**.
94470 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
94480 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e onfig(SQLITE_CON
94490 46 49 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a FIG_MUTEX,...).*
944a0 2a 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a *.** interface..
944b0 2a 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 **.** If compile
944c0 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 d with SQLITE_DE
944d0 42 55 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 BUG, then additi
944e0 6f 6e 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e onal logic is in
944f0 73 65 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 serted.** that d
94500 6f 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 oes error checki
94510 6e 67 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f ng on mutexes to
94520 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 make sure they
94530 61 72 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c are being.** cal
94540 6c 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a led correctly..*
94550 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f *.** $Id: mutex_
94560 6e 6f 6f 70 2e 63 2c 76 20 31 2e 32 20 32 30 30 noop.c,v 1.2 200
94570 38 2f 31 30 2f 31 35 20 31 39 3a 30 33 3a 30 33 8/10/15 19:03:03
94580 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
94590 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
945a0 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 TE_MUTEX_NOOP) &
945b0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
945c0 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 E_DEBUG)./*.** S
945d0 74 75 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 tub routines for
945e0 20 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f all mutex metho
945f0 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ds..**.** This r
94600 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 outines provide
94610 6e 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 no mutual exclus
94620 69 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 ion or error che
94630 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 cking..*/.static
94640 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 int noopMutexHe
94650 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
94660 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 *p){ return 1;
94670 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f }.static int noo
94680 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 pMutexNotheld(sq
94690 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
946a0 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 return 1; }.sta
946b0 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 tic int noopMute
946c0 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 xInit(void){ ret
946d0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
946e0 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 .static int noop
946f0 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 MutexEnd(void){
94700 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
94710 3b 20 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 ; }.static sqlit
94720 65 33 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 e3_mutex *noopMu
94730 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 texAlloc(int id)
94740 7b 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 { return (sqlite
94750 33 5f 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 3_mutex*)8; }.st
94760 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 atic void noopMu
94770 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f texFree(sqlite3_
94780 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 mutex *p){ retur
94790 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 n; }.static void
947a0 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 noopMutexEnter(
947b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
947c0 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 ){ return; }.sta
947d0 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 tic int noopMute
947e0 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 xTry(sqlite3_mut
947f0 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 ex *p){ return S
94800 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 QLITE_OK; }.stat
94810 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 ic void noopMute
94820 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d xLeave(sqlite3_m
94830 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e utex *p){ return
94840 3b 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ; }..SQLITE_PRIV
94850 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ATE sqlite3_mute
94860 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 x_methods *sqlit
94870 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 e3DefaultMutex(v
94880 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 oid){. static s
94890 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
948a0 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a hods sMutex = {.
948b0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 noopMutexIni
948c0 74 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 t,. noopMutex
948d0 45 6e 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 End,. noopMut
948e0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f exAlloc,. noo
948f0 70 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 pMutexFree,.
94900 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a noopMutexEnter,.
94910 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 noopMutexTry
94920 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c ,. noopMutexL
94930 65 61 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d eave,.. noopM
94940 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f utexHeld,. no
94950 6f 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 opMutexNotheld.
94960 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 };.. return &s
94970 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 Mutex;.}.#endif
94980 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 /* defined(SQLIT
94990 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 E_MUTEX_NOOP) &&
949a0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
949b0 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 _DEBUG) */..#if
949c0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
949d0 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 UTEX_NOOP) && de
949e0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
949f0 55 47 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 UG)./*.** In thi
94a00 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
94a10 2c 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 , error checking
94a20 20 69 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 is provided for
94a30 20 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 testing.** and
94a40 64 65 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 debugging purpos
94a50 65 73 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 es. The mutexes
94a60 20 73 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 still do not pr
94a70 6f 76 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 ovide any.** mut
94a80 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a ual exclusion..*
94a90 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 /../*.** The mut
94aa0 65 78 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 ex object.*/.str
94ab0 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 uct sqlite3_mute
94ac0 78 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 x {. int id;
94ad0 20 20 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 /* The mutex t
94ae0 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 ype */. int cnt
94af0 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f ; /* Number o
94b00 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 f entries withou
94b10 74 20 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 t a matching lea
94b20 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ve */.};../*.**
94b30 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
94b40 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c x_held() and sql
94b50 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 ite3_mutex_nothe
94b60 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 ld() routine are
94b70 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 .** intended for
94b80 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 use inside asse
94b90 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e rt() statements.
94ba0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
94bb0 65 62 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 ebugMutexHeld(sq
94bc0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
94bd0 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c . return p==0 |
94be0 7c 20 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 | p->cnt>0;.}.st
94bf0 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 atic int debugMu
94c00 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 texNotheld(sqlit
94c10 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
94c20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 return p==0 || p
94c30 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a ->cnt==0;.}../*.
94c40 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e ** Initialize an
94c50 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 d deinitialize t
94c60 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 he mutex subsyst
94c70 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e em..*/.static in
94c80 74 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 t debugMutexInit
94c90 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 (void){ return S
94ca0 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 QLITE_OK; }.stat
94cb0 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
94cc0 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 xEnd(void){ retu
94cd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
94ce0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
94cf0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
94d00 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 routine allocat
94d10 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 es a new.** mute
94d20 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 x and returns a
94d30 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 pointer to it.
94d40 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 If it returns NU
94d50 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 LL.** that means
94d60 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f that a mutex co
94d70 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 uld not be alloc
94d80 61 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 ated. .*/.static
94d90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
94da0 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 debugMutexAlloc(
94db0 69 6e 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 int id){. stati
94dc0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
94dd0 61 53 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 aStatic[6];. sq
94de0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 lite3_mutex *pNe
94df0 77 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 w = 0;. switch(
94e00 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 id ){. case
94e10 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
94e20 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 T:. case SQLI
94e30 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
94e40 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 VE: {. pNew
94e50 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
94e60 28 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b (sizeof(*pNew));
94e70 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 . if( pNew
94e80 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d ){. pNew-
94e90 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 >id = id;.
94ea0 20 20 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b pNew->cnt = 0;
94eb0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
94ec0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
94ed0 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
94ee0 20 61 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d assert( id-2 >=
94ef0 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 0 );. asse
94f00 72 74 28 20 69 64 2d 32 20 3c 20 73 69 7a 65 6f rt( id-2 < sizeo
94f10 66 28 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f f(aStatic)/sizeo
94f20 66 28 61 53 74 61 74 69 63 5b 30 5d 29 20 29 3b f(aStatic[0]) );
94f30 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26 61 . pNew = &a
94f40 53 74 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20 20 Static[id-2];.
94f50 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 pNew->id = i
94f60 64 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a d;. break;.
94f70 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
94f80 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pNew;.}../*.*
94f90 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
94fa0 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 eallocates a pre
94fb0 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 viously allocate
94fc0 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 d mutex..*/.stat
94fd0 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 ic void debugMut
94fe0 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d exFree(sqlite3_m
94ff0 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 utex *p){. asse
95000 72 74 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b rt( p->cnt==0 );
95010 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
95020 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 ==SQLITE_MUTEX_F
95030 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 AST || p->id==SQ
95040 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
95050 53 49 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 SIVE );. sqlite
95060 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 3_free(p);.}../*
95070 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
95080 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e mutex_enter() an
95090 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
950a0 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 try() routines a
950b0 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 ttempt.** to ent
950c0 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 er a mutex. If
950d0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 another thread i
950e0 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e s already within
950f0 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 the mutex,.** s
95100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
95110 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 er() will block
95120 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
95130 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 x_try() will ret
95140 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 urn.** SQLITE_BU
95150 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 SY. The sqlite3
95160 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 _mutex_try() int
95170 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 erface returns S
95180 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e QLITE_OK.** upon
95190 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 successful entr
951a0 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 y. Mutexes crea
951b0 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 ted using SQLITE
951c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
951d0 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 can.** be enter
951e0 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 ed multiple time
951f0 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 s by the same th
95200 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 read. In such c
95210 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 ases the,.** mut
95220 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 ex must be exite
95230 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 d an equal numbe
95240 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 r of times befor
95250 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 e another thread
95260 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 .** can enter.
95270 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 If the same thre
95280 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 ad tries to ente
95290 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 r any other kind
952a0 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 of mutex.** mor
952b0 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 e than once, the
952c0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
952d0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 efined..*/.stati
952e0 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 c void debugMute
952f0 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d xEnter(sqlite3_m
95300 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 utex *p){. asse
95310 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
95320 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
95330 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e E || debugMutexN
95340 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 otheld(p) );. p
95350 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69 ->cnt++;.}.stati
95360 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 c int debugMutex
95370 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 Try(sqlite3_mute
95380 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 x *p){. assert(
95390 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
953a0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c UTEX_RECURSIVE |
953b0 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 | debugMutexNoth
953c0 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 eld(p) );. p->c
953d0 6e 74 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 53 nt++;. return S
953e0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
953f0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
95400 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 utex_leave() rou
95410 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 tine exits a mut
95420 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 ex that was.** p
95430 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 reviously entere
95440 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 d by the same th
95450 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 read. The behav
95460 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 ior.** is undefi
95470 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 ned if the mutex
95480 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
95490 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 y entered or.**
954a0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
954b0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c allocated. SQL
954c0 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 ite will never d
954d0 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 o either..*/.sta
954e0 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 tic void debugMu
954f0 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 texLeave(sqlite3
95500 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
95510 73 65 72 74 28 20 64 65 62 75 67 4d 75 74 65 78 sert( debugMutex
95520 48 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e Held(p) );. p->
95530 63 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 cnt--;. assert(
95540 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
95550 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c UTEX_RECURSIVE |
95560 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 | debugMutexNoth
95570 65 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c eld(p) );.}..SQL
95580 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
95590 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
955a0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c s *sqlite3Defaul
955b0 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 tMutex(void){.
955c0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
955d0 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 utex_methods sMu
955e0 74 65 78 20 3d 20 7b 0a 20 20 20 20 64 65 62 75 tex = {. debu
955f0 67 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 gMutexInit,.
95600 64 65 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a 20 debugMutexEnd,.
95610 20 20 20 64 65 62 75 67 4d 75 74 65 78 41 6c 6c debugMutexAll
95620 6f 63 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 oc,. debugMut
95630 65 78 46 72 65 65 2c 0a 20 20 20 20 64 65 62 75 exFree,. debu
95640 67 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 gMutexEnter,.
95650 20 64 65 62 75 67 4d 75 74 65 78 54 72 79 2c 0a debugMutexTry,.
95660 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 4c 65 debugMutexLe
95670 61 76 65 2c 0a 0a 20 20 20 20 64 65 62 75 67 4d ave,.. debugM
95680 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 64 65 utexHeld,. de
95690 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a bugMutexNotheld.
956a0 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 };.. return &
956b0 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 sMutex;.}.#endif
956c0 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 /* defined(SQLI
956d0 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 TE_MUTEX_NOOP) &
956e0 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
956f0 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a _DEBUG) */../***
95700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
95710 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 of mutex_noop.c
95720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
95750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
95760 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73 32 n file mutex_os2
95770 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
95780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
957a0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 * 2007 August 28
957b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
957c0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
957d0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
957e0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
957f0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
95800 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
95810 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
95820 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
95830 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
95840 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
95850 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
95860 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
95870 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
95880 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
95890 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
958a0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
958b0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
958c0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
958d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
958e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
958f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
95910 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
95920 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
95930 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
95940 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 ment mutexes for
95950 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a OS/2.**.** $Id:
95960 20 6d 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20 31 mutex_os2.c,v 1
95970 2e 31 30 20 32 30 30 38 2f 30 36 2f 32 33 20 32 .10 2008/06/23 2
95980 32 3a 31 33 3a 32 38 20 70 77 65 69 6c 62 61 63 2:13:28 pweilbac
95990 68 65 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a her Exp $.*/../*
959a0 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 .** The code in
959b0 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c this file is onl
959c0 79 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 y used if SQLITE
959d0 5f 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 _MUTEX_OS2 is de
959e0 66 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 fined..** See th
959f0 65 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 e mutex.h file f
95a00 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 or details..*/.#
95a10 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 ifdef SQLITE_MUT
95a20 45 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a EX_OS2../*******
95a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
95a40 4f 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65 OS/2 Mutex Imple
95a50 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a mentation ******
95a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95a70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c .**.** This impl
95a80 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 ementation of mu
95a90 74 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 75 texes is built u
95aa0 73 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50 sing the OS/2 AP
95ab0 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 I..*/../*.** The
95ac0 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a mutex object.**
95ad0 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 Each recursive
95ae0 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 mutex is an inst
95af0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
95b00 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
95b10 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 .*/.struct sqlit
95b20 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54 e3_mutex {. HMT
95b30 58 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f X mutex; /
95b40 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c * Mutex controll
95b50 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a ing the lock */.
95b60 20 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20 int id;
95b70 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 /* Mutex typ
95b80 65 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66 e */. int nRef
95b90 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
95ba0 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
95bb0 20 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72 */. TID owner
95bc0 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 ; /* Threa
95bd0 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d d holding this m
95be0 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 utex */.};..#def
95bf0 69 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e ine OS2_MUTEX_IN
95c00 49 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c ITIALIZER 0,0,
95c10 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 0,0../*.** Initi
95c20 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 alize and deinit
95c30 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 ialize the mutex
95c40 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 subsystem..*/.s
95c50 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 tatic int os2Mut
95c60 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 exInit(void){ re
95c70 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
95c80 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 }.static int os2
95c90 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 MutexEnd(void){
95ca0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
95cb0 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ; }../*.** The s
95cc0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
95cd0 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c oc() routine all
95ce0 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 ocates a new.**
95cf0 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e mutex and return
95d00 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 s a pointer to i
95d10 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e t. If it return
95d20 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d s NULL.** that m
95d30 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 eans that a mute
95d40 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 x could not be a
95d50 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51 llocated. .** SQ
95d60 4c 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64 Lite will unwind
95d70 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 its stack and r
95d80 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 eturn an error.
95d90 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a The argument.**
95da0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
95db0 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 x_alloc() is one
95dc0 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 of these intege
95dd0 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a r constants:.**.
95de0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
95df0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
95e00 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ST
95e10 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 0.** <li> SQLI
95e20 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
95e30 56 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a VE 1.**
95e40 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
95e50 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
95e60 52 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e R 2.** <li>
95e70 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
95e80 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 TATIC_MEM
95e90 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 3.** <li> SQL
95ea0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
95eb0 5f 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a _PRNG 4.*
95ec0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 * </ul>.**.** Th
95ed0 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 e first two cons
95ee0 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 tants cause sqli
95ef0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
95f00 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 ) to create.** a
95f10 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 new mutex. The
95f20 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 new mutex is re
95f30 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c cursive when SQL
95f40 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
95f50 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 IVE.** is used b
95f60 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 ut not necessari
95f70 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 ly so when SQLIT
95f80 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 E_MUTEX_FAST is
95f90 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 used..** The mut
95fa0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ex implementatio
95fb0 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
95fc0 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e to make a distin
95fd0 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e ction.** between
95fe0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
95ff0 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 CURSIVE and SQLI
96000 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 TE_MUTEX_FAST if
96010 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
96020 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 want to. But SQ
96030 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 Lite will only r
96040 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 equest a recursi
96050 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 ve mutex in.** c
96060 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 ases where it re
96070 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 ally needs one.
96080 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e If a faster non
96090 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 -recursive mutex
960a0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
960b0 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 on is available
960c0 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 on the host plat
960d0 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 form, the mutex
960e0 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 subsystem.** mig
960f0 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 ht return such a
96100 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e mutex in respon
96110 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 se to SQLITE_MUT
96120 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 EX_FAST..**.** T
96130 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 he other allowed
96140 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 parameters to s
96150 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
96160 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e oc() each return
96170 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f .** a pointer to
96180 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 a static preexi
96190 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 sting mutex. Th
961a0 72 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 ree static mutex
961b0 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 es are.** used b
961c0 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 y the current ve
961d0 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e rsion of SQLite.
961e0 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e Future version
961f0 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d s of SQLite.** m
96200 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 ay add additiona
96210 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 l static mutexes
96220 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 . Static mutexe
96230 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e s are for intern
96240 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c al.** use by SQL
96250 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 ite only. Appli
96260 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 cations that use
96270 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 SQLite mutexes
96280 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e should.** use on
96290 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d ly the dynamic m
962a0 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 utexes returned
962b0 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f by SQLITE_MUTEX_
962c0 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 FAST or.** SQLIT
962d0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
962e0 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 E..**.** Note th
962f0 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 at if one of the
96300 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 dynamic mutex p
96310 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 arameters (SQLIT
96320 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 E_MUTEX_FAST.**
96330 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f or SQLITE_MUTEX_
96340 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 RECURSIVE) is us
96350 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f ed then sqlite3_
96360 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a mutex_alloc().**
96370 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 returns a diffe
96380 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 rent mutex on ev
96390 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 ery call. But f
963a0 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a or the static.**
963b0 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 mutex types, th
963c0 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 e same mutex is
963d0 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 returned on ever
963e0 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a y call that has.
963f0 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 ** the same type
96400 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 number..*/.stat
96410 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
96420 20 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28 *os2MutexAlloc(
96430 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 int iType){. sq
96440 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d lite3_mutex *p =
96450 20 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28 NULL;. switch(
96460 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 iType ){. ca
96470 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f se SQLITE_MUTEX_
96480 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 FAST:. case S
96490 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
964a0 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 RSIVE: {. p
964b0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
964c0 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 Zero( sizeof(*p)
964d0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 );. if( p
964e0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 ){. p->id
964f0 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 = iType;.
96500 20 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d if( DosCreateM
96510 75 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e utexSem( 0, &p->
96520 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 mutex, 0, FALSE
96530 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b ) != NO_ERROR ){
96540 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
96550 65 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20 e3_free( p );.
96560 20 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c p = NULL
96570 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
96580 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
96590 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
965a0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 lt: {. stat
965b0 69 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 ic volatile int
965c0 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 isInit = 0;.
965d0 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
965e0 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 _mutex staticMut
965f0 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 exes[] = {.
96600 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 { OS2_MUTEX_I
96610 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 NITIALIZER, },.
96620 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 { OS2_MUT
96630 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 EX_INITIALIZER,
96640 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 },. { OS2
96650 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
96660 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b ER, },. {
96670 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 OS2_MUTEX_INITI
96680 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 ALIZER, },.
96690 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 { OS2_MUTEX_I
966a0 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 NITIALIZER, },.
966b0 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 { OS2_MUT
966c0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 EX_INITIALIZER,
966d0 7d 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 },. };.
966e0 20 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 if ( !isInit )
966f0 7b 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 54 {. APIRET
96700 20 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49 rc;. PTI
96710 42 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20 B ptib;.
96720 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20 PPIB ppib;.
96730 20 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20 HMTX mutex;.
96740 20 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65 char name
96750 5b 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f [32];. Do
96760 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 sGetInfoBlocks(
96770 26 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a &ptib, &ppib );.
96780 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
96790 73 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66 snprintf( sizeof
967a0 28 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c (name), name, "\
967b0 5c 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30 \SEM32\\SQLITE%0
967c0 34 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 4x",.
967d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
967e0 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29 pib->pib_ulpid )
967f0 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 ;. while(
96800 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 !isInit ){.
96810 20 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b mutex = 0;
96820 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
96830 44 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 DosCreateMutexSe
96840 6d 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c m( name, &mutex,
96850 20 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20 0, FALSE);.
96860 20 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 if( rc ==
96870 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
96880 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 int i;.
96890 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 if( !
968a0 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 isInit ){.
968b0 20 20 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d for( i =
968c0 20 30 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 0; i < sizeof(s
968d0 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 taticMutexes)/si
968e0 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 zeof(staticMutex
968f0 65 73 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 es[0]); i++ ){.
96900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 D
96910 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d osCreateMutexSem
96920 28 20 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 ( 0, &staticMute
96930 78 65 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c xes[i].mutex, 0,
96940 20 46 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 FALSE );.
96950 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
96960 20 20 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d isInit =
96970 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1;.
96980 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 44 6f }. Do
96990 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 sCloseMutexSem(
969a0 6d 75 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 mutex );.
969b0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 }else if( rc
969c0 3d 3d 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 == ERROR_DUPLICA
969d0 54 45 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 TE_NAME ){.
969e0 20 20 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 DosSleep(
969f0 20 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 1 );.
96a00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
96a10 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 return p;.
96a20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
96a30 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
96a40 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 assert( iType-2
96a50 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 >= 0 );. a
96a60 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c ssert( iType-2 <
96a70 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 sizeof(staticMu
96a80 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 texes)/sizeof(st
96a90 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 aticMutexes[0])
96aa0 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 );. p = &st
96ab0 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 aticMutexes[iTyp
96ac0 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 e-2];. p->i
96ad0 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 d = iType;.
96ae0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
96af0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
96b00 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
96b10 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 tine deallocates
96b20 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c a previously al
96b30 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a located mutex..*
96b40 2a 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 * SQLite is care
96b50 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 ful to deallocat
96b60 65 20 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 e every mutex th
96b70 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e at it allocates.
96b80 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
96b90 6f 73 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c os2MutexFree(sql
96ba0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
96bb0 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
96bc0 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 urn;. assert( p
96bd0 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 ->nRef==0 );. a
96be0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
96bf0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
96c00 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 || p->id==SQLITE
96c10 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
96c20 20 29 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 );. DosCloseMu
96c30 74 65 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 texSem( p->mutex
96c40 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 );. sqlite3_fr
96c50 65 65 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ee( p );.}../*.*
96c60 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
96c70 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 tex_enter() and
96c80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
96c90 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 y() routines att
96ca0 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 empt.** to enter
96cb0 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e a mutex. If an
96cc0 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 other thread is
96cd0 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 already within t
96ce0 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c he mutex,.** sql
96cf0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
96d00 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e () will block an
96d10 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
96d20 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 try() will retur
96d30 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 n.** SQLITE_BUSY
96d40 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d . The sqlite3_m
96d50 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 utex_try() inter
96d60 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c face returns SQL
96d70 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 ITE_OK.** upon s
96d80 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e uccessful entry.
96d90 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 Mutexes create
96da0 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d d using SQLITE_M
96db0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 UTEX_RECURSIVE c
96dc0 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 an.** be entered
96dd0 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 multiple times
96de0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
96df0 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 ad. In such cas
96e00 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 es the,.** mutex
96e10 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 must be exited
96e20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 an equal number
96e30 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 of times before
96e40 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a another thread.*
96e50 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 * can enter. If
96e60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
96e70 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 tries to enter
96e80 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f any other kind o
96e90 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 f mutex.** more
96ea0 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 than once, the b
96eb0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
96ec0 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ined..*/.static
96ed0 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 void os2MutexEnt
96ee0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
96ef0 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b *p){. TID tid;
96f00 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a . PID holder1;.
96f10 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b ULONG holder2;
96f20 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
96f30 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 turn;. assert(
96f40 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
96f50 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
96f60 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 os2MutexNotheld
96f70 28 70 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 (p) );. DosRequ
96f80 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d estMutexSem(p->m
96f90 75 74 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 utex, SEM_INDEFI
96fa0 4e 49 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f NITE_WAIT);. Do
96fb0 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 sQueryMutexSem(p
96fc0 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 ->mutex, &holder
96fd0 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 1, &tid, &holder
96fe0 32 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 2);. p->owner =
96ff0 20 74 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b tid;. p->nRef+
97000 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 +;.}.static int
97010 6f 73 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 os2MutexTry(sqli
97020 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
97030 20 69 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 int rc;. TID t
97040 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 id;. PID holder
97050 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 1;. ULONG holde
97060 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 r2;. if( p==0 )
97070 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
97080 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e K;. assert( p->
97090 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
970a0 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 _RECURSIVE || os
970b0 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 2MutexNotheld(p)
970c0 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 );. if( DosReq
970d0 75 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e uestMutexSem(p->
970e0 6d 75 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 mutex, SEM_IMMED
970f0 49 41 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 IATE_RETURN) ==
97100 4e 4f 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 NO_ERROR) {.
97110 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d DosQueryMutexSem
97120 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 (p->mutex, &hold
97130 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 er1, &tid, &hold
97140 65 72 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e er2);. p->own
97150 65 72 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d er = tid;. p-
97160 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 >nRef++;. rc
97170 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
97180 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d else {. rc =
97190 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
971a0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
971b0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
971c0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
971d0 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 () routine exits
971e0 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 a mutex that wa
971f0 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 s.** previously
97200 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 entered by the s
97210 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 ame thread. The
97220 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 behavior.** is
97230 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 undefined if the
97240 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 mutex is not cu
97250 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 rrently entered
97260 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 or.** is not cur
97270 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 rently allocated
97280 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e . SQLite will n
97290 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a ever do either..
972a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f */.static void o
972b0 73 32 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c s2MutexLeave(sql
972c0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
972d0 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 TID tid;. PID
972e0 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e holder1;. ULON
972f0 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 G holder2;. if(
97300 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a p==0 ) return;.
97310 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
97320 66 3e 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 f>0 );. DosQuer
97330 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 yMutexSem(p->mut
97340 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 ex, &holder1, &t
97350 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 id, &holder2);.
97360 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 assert( p->owne
97370 72 3d 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e r==tid );. p->n
97380 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 Ref--;. assert(
97390 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 p->nRef==0 || p
973a0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
973b0 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a EX_RECURSIVE );.
973c0 20 20 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 DosReleaseMute
973d0 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a xSem(p->mutex);.
973e0 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
973f0 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 _DEBUG./*.** The
97400 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
97410 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 eld() and sqlite
97420 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
97430 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a ) routine are.**
97440 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 intended for us
97450 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 e inside assert(
97460 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f ) statements..*/
97470 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d .static int os2M
97480 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 utexHeld(sqlite3
97490 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 _mutex *p){. TI
974a0 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 D tid;. PID pid
974b0 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e ;. ULONG ulCoun
974c0 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a t;. PTIB ptib;.
974d0 20 20 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 if( p!=0 ) {.
974e0 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 DosQueryMutex
974f0 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 Sem(p->mutex, &p
97500 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 id, &tid, &ulCou
97510 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a nt);. } else {.
97520 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c DosGetInfoBl
97530 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c ocks(&ptib, NULL
97540 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 );. tid = pti
97550 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 b->tib_ptib2->ti
97560 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 b2_ultid;. }.
97570 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 return p==0 || (
97580 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d p->nRef!=0 && p-
97590 3e 6f 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a >owner==tid);.}.
975a0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 static int os2Mu
975b0 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 texNotheld(sqlit
975c0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
975d0 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 TID tid;. PID p
975e0 69 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f id;. ULONG ulCo
975f0 75 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 unt;. PTIB ptib
97600 3b 0a 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 ;. if( p!= 0 )
97610 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 {. DosQueryMu
97620 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c texSem(p->mutex,
97630 20 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c &pid, &tid, &ul
97640 43 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 Count);. } else
97650 20 7b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 {. DosGetInf
97660 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e oBlocks(&ptib, N
97670 55 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 ULL);. tid =
97680 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d ptib->tib_ptib2-
97690 3e 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d >tib2_ultid;. }
976a0 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c . return p==0 |
976b0 7c 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 | p->nRef==0 ||
976c0 70 2d 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d p->owner!=tid;.}
976d0 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f .#endif..SQLITE_
976e0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
976f0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 mutex_methods *s
97700 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 qlite3DefaultMut
97710 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ex(void){. stat
97720 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
97730 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 _methods sMutex
97740 3d 20 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 = {. os2Mutex
97750 49 6e 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 Init,. os2Mut
97760 65 78 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 exEnd,. os2Mu
97770 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 texAlloc,. os
97780 32 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 2MutexFree,.
97790 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 os2MutexEnter,.
977a0 20 20 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a os2MutexTry,.
977b0 20 20 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 os2MutexLeav
977c0 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 e,.#ifdef SQLITE
977d0 5f 44 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 _DEBUG. os2Mu
977e0 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 texHeld,. os2
977f0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e MutexNotheld.#en
97800 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 dif. };.. retu
97810 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 rn &sMutex;.}.#e
97820 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d ndif /* SQLITE_M
97830 55 54 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a UTEX_OS2 */../**
97840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
97850 20 6f 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 of mutex_os2.c
97860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
97890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
978a0 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e in file mutex_un
978b0 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ix.c ***********
978c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
978d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
978e0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 ** 2007 August 2
978f0 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 8.**.** The auth
97900 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
97910 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
97920 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
97930 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
97940 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
97950 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
97960 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
97970 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
97980 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
97990 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
979a0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
979b0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
979c0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
979d0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
979e0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
979f0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
97a00 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
97a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97a50 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
97a60 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e ntains the C fun
97a70 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c ctions that impl
97a80 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f ement mutexes fo
97a90 72 20 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a r pthreads.**.**
97aa0 20 24 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 $Id: mutex_unix
97ab0 2e 63 2c 76 20 31 2e 31 35 20 32 30 30 38 2f 31 .c,v 1.15 2008/1
97ac0 31 2f 31 37 20 31 39 3a 31 38 3a 35 35 20 64 61 1/17 19:18:55 da
97ad0 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
97ae0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f */../*.** The co
97af0 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
97b00 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 is only used if
97b10 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 we are compiling
97b20 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 75 threadsafe.** u
97b30 6e 64 65 72 20 75 6e 69 78 20 77 69 74 68 20 70 nder unix with p
97b40 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e threads..**.** N
97b50 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 69 6d ote that this im
97b60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 plementation req
97b70 75 69 72 65 73 20 61 20 76 65 72 73 69 6f 6e 20 uires a version
97b80 6f 66 20 70 74 68 72 65 61 64 73 20 74 68 61 74 of pthreads that
97b90 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72 65 63 .** supports rec
97ba0 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 2e 0a ursive mutexes..
97bb0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
97bc0 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a _MUTEX_PTHREADS.
97bd0 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65 .#include <pthre
97be0 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 ad.h>.../*.** Ea
97bf0 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ch recursive mut
97c00 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ex is an instanc
97c10 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
97c20 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f ng structure..*/
97c30 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
97c40 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 72 65 61 mutex {. pthrea
97c50 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b d_mutex_t mutex;
97c60 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f /* Mutex co
97c70 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f ntrolling the lo
97c80 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 ck */. int id;
97c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
97ca0 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 /* Mutex type
97cb0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 */. int nRef;
97cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
97cd0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
97ce0 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 70 74 68 trances */. pth
97cf0 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b 20 20 20 read_t owner;
97d00 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 /* Threa
97d10 64 20 74 68 61 74 20 69 73 20 77 69 74 68 69 6e d that is within
97d20 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 23 this mutex */.#
97d30 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
97d40 55 47 0a 20 20 69 6e 74 20 74 72 61 63 65 3b 20 UG. int trace;
97d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
97d60 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 /* True to trace
97d70 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65 6e 64 changes */.#end
97d80 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 53 51 4c if.};.#ifdef SQL
97d90 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66 69 6e ITE_DEBUG.#defin
97da0 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f e SQLITE3_MUTEX_
97db0 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 INITIALIZER { PT
97dc0 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 HREAD_MUTEX_INIT
97dd0 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 IALIZER, 0, 0, (
97de0 70 74 68 72 65 61 64 5f 74 29 30 2c 20 30 20 7d pthread_t)0, 0 }
97df0 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 .#else.#define S
97e00 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 QLITE3_MUTEX_INI
97e10 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48 52 45 TIALIZER { PTHRE
97e20 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c AD_MUTEX_INITIAL
97e30 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70 74 68 IZER, 0, 0, (pth
97e40 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 6e 64 69 read_t)0 }.#endi
97e50 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c f../*.** The sql
97e60 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
97e70 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 ) and sqlite3_mu
97e80 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f tex_notheld() ro
97e90 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 utine are.** int
97ea0 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e ended for use on
97eb0 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 ly inside assert
97ec0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 () statements.
97ed0 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d On some platform
97ee0 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 67 68 s,.** there migh
97ef0 74 20 62 65 20 72 61 63 65 20 63 6f 6e 64 69 74 t be race condit
97f00 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 63 61 ions that can ca
97f10 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e use these routin
97f20 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72 es to.** deliver
97f30 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c incorrect resul
97f40 74 73 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c ts. In particul
97f50 61 72 2c 20 69 66 20 70 74 68 72 65 61 64 5f 65 ar, if pthread_e
97f60 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 qual() is.** not
97f70 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 an atomic opera
97f80 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 73 65 tion, then these
97f90 20 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 74 20 routines might
97fa0 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f delivery.** inco
97fb0 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20 rrect results.
97fc0 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d On most platform
97fd0 73 2c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c s, pthread_equal
97fe0 28 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70 () is a .** comp
97ff0 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e arison of two in
98000 74 65 67 65 72 73 20 61 6e 64 20 69 73 20 74 68 tegers and is th
98010 65 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20 erefore atomic.
98020 20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a 20 74 But we are.** t
98030 6f 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73 old that HPUX is
98040 20 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74 not such a plat
98050 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68 form. If so, th
98060 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 en these routine
98070 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c s.** will not al
98080 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63 ways work correc
98090 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a tly on HPUX..**.
980a0 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74 ** On those plat
980b0 66 6f 72 6d 73 20 77 68 65 72 65 20 70 74 68 72 forms where pthr
980c0 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 6e ead_equal() is n
980d0 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74 ot atomic, SQLit
980e0 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 e.** should be c
980f0 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 74 20 ompiled without
98100 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 -DSQLITE_DEBUG a
98110 6e 64 20 77 69 74 68 20 2d 44 4e 44 45 42 55 47 nd with -DNDEBUG
98120 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 to.** make sure
98130 20 6e 6f 20 61 73 73 65 72 74 28 29 20 73 74 61 no assert() sta
98140 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c tements are eval
98150 75 61 74 65 64 20 61 6e 64 20 68 65 6e 63 65 20 uated and hence
98160 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 these.** routine
98170 73 20 61 72 65 20 6e 65 76 65 72 20 63 61 6c 6c s are never call
98180 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 ed..*/.#if !defi
98190 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 ned(NDEBUG) || d
981a0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
981b0 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 BUG).static int
981c0 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 pthreadMutexHeld
981d0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
981e0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d p){. return (p-
981f0 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 74 68 72 >nRef!=0 && pthr
98200 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e ead_equal(p->own
98210 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 er, pthread_self
98220 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 ()));.}.static i
98230 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e nt pthreadMutexN
98240 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d otheld(sqlite3_m
98250 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
98260 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c rn p->nRef==0 ||
98270 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 pthread_equal(p
98280 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64 ->owner, pthread
98290 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23 _self())==0;.}.#
982a0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 endif../*.** Ini
982b0 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
982c0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 itialize the mut
982d0 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f ex subsystem..*/
982e0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 .static int pthr
982f0 65 61 64 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 eadMutexInit(voi
98300 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
98310 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 E_OK; }.static i
98320 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 nt pthreadMutexE
98330 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e nd(void){ return
98340 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f SQLITE_OK; }../
98350 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
98360 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
98370 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
98380 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
98390 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
983a0 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 inter to it. If
983b0 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
983c0 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
983d0 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c hat a mutex coul
983e0 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 d not be allocat
983f0 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 ed. SQLite.** w
98400 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 ill unwind its s
98410 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 tack and return
98420 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 an error. The a
98430 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 rgument.** to sq
98440 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
98450 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 c() is one of th
98460 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 ese integer cons
98470 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c tants:.**.** <ul
98480 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 >.** <li> SQLIT
98490 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 E_MUTEX_FAST.**
984a0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
984b0 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 EX_RECURSIVE.**
984c0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
984d0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
984e0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
984f0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 _MUTEX_STATIC_ME
98500 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 M.** <li> SQLIT
98510 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
98520 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c EM2.** <li> SQL
98530 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
98540 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _PRNG.** <li> S
98550 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
98560 49 43 5f 4c 52 55 0a 2a 2a 20 3c 2f 75 6c 3e 0a IC_LRU.** </ul>.
98570 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
98580 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 two constants ca
98590 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 use sqlite3_mute
985a0 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 x_alloc() to cre
985b0 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 ate.** a new mut
985c0 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 ex. The new mut
985d0 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20 ex is recursive
985e0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 when SQLITE_MUTE
985f0 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 X_RECURSIVE.** i
98600 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e s used but not n
98610 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 ecessarily so wh
98620 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f en SQLITE_MUTEX_
98630 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a FAST is used..**
98640 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 The mutex imple
98650 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e mentation does n
98660 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 ot need to make
98670 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a a distinction.**
98680 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f between SQLITE_
98690 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
986a0 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 and SQLITE_MUTEX
986b0 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 _FAST if it does
986c0 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e .** not want to.
986d0 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c But SQLite wil
986e0 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 l only request a
986f0 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
98700 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 in.** cases whe
98710 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 re it really nee
98720 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 ds one. If a fa
98730 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 ster non-recursi
98740 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c ve mutex.** impl
98750 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 ementation is av
98760 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 ailable on the h
98770 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 ost platform, th
98780 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
98790 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 m.** might retur
987a0 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 n such a mutex i
987b0 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 n response to SQ
987c0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e LITE_MUTEX_FAST.
987d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 .**.** The other
987e0 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 allowed paramet
987f0 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d ers to sqlite3_m
98800 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 utex_alloc() eac
98810 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f h return.** a po
98820 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 inter to a stati
98830 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 c preexisting mu
98840 74 65 78 2e 20 20 54 68 72 65 65 20 73 74 61 74 tex. Three stat
98850 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a ic mutexes are.*
98860 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 * used by the cu
98870 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 rrent version of
98880 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 SQLite. Future
98890 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
988a0 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 ite.** may add a
988b0 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 dditional static
988c0 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 mutexes. Stati
988d0 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f c mutexes are fo
988e0 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 r internal.** us
988f0 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 e by SQLite only
98900 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 . Applications
98910 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 that use SQLite
98920 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a mutexes should.*
98930 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 * use only the d
98940 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 ynamic mutexes r
98950 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 eturned by SQLIT
98960 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a E_MUTEX_FAST or.
98970 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ** SQLITE_MUTEX_
98980 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a RECURSIVE..**.**
98990 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e Note that if on
989a0 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 e of the dynamic
989b0 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 mutex parameter
989c0 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f s (SQLITE_MUTEX_
989d0 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 FAST.** or SQLIT
989e0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
989f0 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 E) is used then
98a00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
98a10 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 loc().** returns
98a20 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 a different mut
98a30 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c ex on every call
98a40 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 . But for the s
98a50 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 tatic .** mutex
98a60 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 types, the same
98a70 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 mutex is returne
98a80 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 d on every call
98a90 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 that has.** the
98aa0 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 same type number
98ab0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 ..*/.static sqli
98ac0 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 72 65 te3_mutex *pthre
98ad0 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 adMutexAlloc(int
98ae0 20 69 54 79 70 65 29 7b 0a 20 20 73 74 61 74 69 iType){. stati
98af0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
98b00 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 staticMutexes[]
98b10 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54 45 33 5f = {. SQLITE3_
98b20 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
98b30 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d R,. SQLITE3_M
98b40 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
98b50 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 ,. SQLITE3_MU
98b60 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
98b70 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 . SQLITE3_MUT
98b80 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a EX_INITIALIZER,.
98b90 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 SQLITE3_MUTE
98ba0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 X_INITIALIZER,.
98bb0 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 SQLITE3_MUTEX
98bc0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d _INITIALIZER. }
98bd0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
98be0 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 68 28 20 x *p;. switch(
98bf0 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 iType ){. cas
98c00 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 e SQLITE_MUTEX_R
98c10 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 ECURSIVE: {.
98c20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
98c30 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 locZero( sizeof(
98c40 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 *p) );. if(
98c50 20 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c p ){.#ifdef SQL
98c60 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 ITE_HOMEGROWN_RE
98c70 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 CURSIVE_MUTEX.
98c80 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 63 75 /* If recu
98c90 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 rsive mutexes ar
98ca0 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c e not available,
98cb0 20 77 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f we will have to
98cc0 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 75 69 6c . ** buil
98cd0 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 65 20 d our own. See
98ce0 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 below. */.
98cf0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
98d00 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 init(&p->mutex,
98d10 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 0);.#else.
98d20 20 20 2f 2a 20 55 73 65 20 61 20 72 65 63 75 72 /* Use a recur
98d30 73 69 76 65 20 6d 75 74 65 78 20 69 66 20 69 74 sive mutex if it
98d40 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f is available */
98d50 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
98d60 5f 6d 75 74 65 78 61 74 74 72 5f 74 20 72 65 63 _mutexattr_t rec
98d70 75 72 73 69 76 65 41 74 74 72 3b 0a 20 20 20 20 ursiveAttr;.
98d80 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
98d90 78 61 74 74 72 5f 69 6e 69 74 28 26 72 65 63 75 xattr_init(&recu
98da0 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20 rsiveAttr);.
98db0 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
98dc0 78 61 74 74 72 5f 73 65 74 74 79 70 65 28 26 72 xattr_settype(&r
98dd0 65 63 75 72 73 69 76 65 41 74 74 72 2c 20 50 54 ecursiveAttr, PT
98de0 48 52 45 41 44 5f 4d 55 54 45 58 5f 52 45 43 55 HREAD_MUTEX_RECU
98df0 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 20 20 RSIVE);.
98e00 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e pthread_mutex_in
98e10 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72 it(&p->mutex, &r
98e20 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20 ecursiveAttr);.
98e30 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
98e40 75 74 65 78 61 74 74 72 5f 64 65 73 74 72 6f 79 utexattr_destroy
98e50 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 (&recursiveAttr)
98e60 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 ;.#endif.
98e70 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
98e80 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
98e90 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
98ea0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
98eb0 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 70 _FAST: {. p
98ec0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
98ed0 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 Zero( sizeof(*p)
98ee0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 );. if( p
98ef0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 ){. p->id
98f00 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 = iType;.
98f10 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
98f20 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 init(&p->mutex,
98f30 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0);. }.
98f40 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
98f50 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
98f60 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 assert( iTyp
98f70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 e-2 >= 0 );.
98f80 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d assert( iType-
98f90 32 20 3c 20 41 72 72 61 79 53 69 7a 65 28 73 74 2 < ArraySize(st
98fa0 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b 0a aticMutexes) );.
98fb0 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69 p = &stati
98fc0 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 cMutexes[iType-2
98fd0 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d ];. p->id =
98fe0 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 iType;. br
98ff0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
99000 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f return p;.}.../
99010 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
99020 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 e deallocates a
99030 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c previously.** al
99040 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 located mutex.
99050 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 SQLite is carefu
99060 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 l to deallocate
99070 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 every.** mutex t
99080 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 hat it allocates
99090 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
990a0 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 pthreadMutexFre
990b0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
990c0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
990d0 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 ->nRef==0 );. a
990e0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
990f0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
99100 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 || p->id==SQLITE
99110 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
99120 20 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 );. pthread_mu
99130 74 65 78 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e tex_destroy(&p->
99140 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 mutex);. sqlite
99150 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 3_free(p);.}../*
99160 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
99170 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e mutex_enter() an
99180 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
99190 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 try() routines a
991a0 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 ttempt.** to ent
991b0 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 er a mutex. If
991c0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 another thread i
991d0 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e s already within
991e0 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 the mutex,.** s
991f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
99200 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 er() will block
99210 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
99220 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 x_try() will ret
99230 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 urn.** SQLITE_BU
99240 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 SY. The sqlite3
99250 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 _mutex_try() int
99260 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 erface returns S
99270 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e QLITE_OK.** upon
99280 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 successful entr
99290 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 y. Mutexes crea
992a0 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 ted using SQLITE
992b0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
992c0 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 can.** be enter
992d0 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 ed multiple time
992e0 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 s by the same th
992f0 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 read. In such c
99300 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 ases the,.** mut
99310 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 ex must be exite
99320 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 d an equal numbe
99330 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 r of times befor
99340 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 e another thread
99350 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 .** can enter.
99360 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 If the same thre
99370 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 ad tries to ente
99380 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 r any other kind
99390 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 of mutex.** mor
993a0 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 e than once, the
993b0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
993c0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 efined..*/.stati
993d0 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 c void pthreadMu
993e0 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 texEnter(sqlite3
993f0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
99400 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
99410 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
99420 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 IVE || pthreadMu
99430 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
99440 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
99450 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 HOMEGROWN_RECURS
99460 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 IVE_MUTEX. /* I
99470 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 f recursive mute
99480 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 xes are not avai
99490 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 lable, then we h
994a0 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a ave to grow. **
994b0 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 our own. This
994c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 implementation a
994d0 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 ssumes that pthr
994e0 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a ead_equal(). **
994f0 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 is atomic - tha
99500 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 t it cannot be d
99510 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 eceived into thi
99520 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 nking self. **
99530 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 and p->owner are
99540 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e equal if p->own
99550 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 er changes betwe
99560 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 en two values.
99570 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 ** that are not
99580 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 equal to self wh
99590 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 ile the comparis
995a0 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 on is taking pla
995b0 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d ce.. ** This im
995c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 plementation als
995d0 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 o assumes a cohe
995e0 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 rent cache - tha
995f0 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 t . ** separate
99600 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f processes canno
99610 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 t read different
99620 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 values from the
99630 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 same. ** addre
99640 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ss at the same t
99650 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 ime. If either
99660 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e of these two con
99670 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 ditions. ** are
99680 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 not met, then t
99690 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 he mutexes will
996a0 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d fail and problem
996b0 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 s will result..
996c0 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 */. {. pthr
996d0 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 ead_t self = pth
996e0 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 read_self();.
996f0 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 if( p->nRef>0 &
99700 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 & pthread_equal(
99710 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 p->owner, self)
99720 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 ){. p->nRef
99730 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ++;. }else{.
99740 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 pthread_mut
99750 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 ex_lock(&p->mute
99760 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 x);. assert
99770 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a ( p->nRef==0 );.
99780 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d p->owner =
99790 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e self;. p->
997a0 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a nRef = 1;. }.
997b0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 }.#else. /* U
997c0 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 se the built-in
997d0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 recursive mutexe
997e0 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61 76 s if they are av
997f0 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 ailable.. */.
99800 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f pthread_mutex_lo
99810 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 ck(&p->mutex);.
99820 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 p->owner = pthr
99830 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d ead_self();. p-
99840 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a >nRef++;.#endif.
99850 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
99860 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 EBUG. if( p->tr
99870 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 ace ){. print
99880 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 25 f("enter mutex %
99890 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 p (%d) with nRef
998a0 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 =%d\n", p, p->tr
998b0 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 ace, p->nRef);.
998c0 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 }.#endif.}.stat
998d0 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 ic int pthreadMu
998e0 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d texTry(sqlite3_m
998f0 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 utex *p){. int
99900 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rc;. assert( p-
99910 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
99920 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 X_RECURSIVE || p
99930 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 threadMutexNothe
99940 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 ld(p) );..#ifdef
99950 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 SQLITE_HOMEGROW
99960 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 N_RECURSIVE_MUTE
99970 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 X. /* If recurs
99980 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 ive mutexes are
99990 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 not available, t
999a0 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 hen we have to g
999b0 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e row. ** our own
999c0 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e . This implemen
999d0 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 tation assumes t
999e0 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 hat pthread_equa
999f0 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d l(). ** is atom
99a00 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e ic - that it can
99a10 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20 not be deceived
99a20 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 into thinking se
99a30 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f lf. ** and p->o
99a40 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 wner are equal i
99a50 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 f p->owner chang
99a60 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 es between two v
99a70 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 alues. ** that
99a80 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f are not equal to
99a90 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 self while the
99aa0 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 comparison is ta
99ab0 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a king place.. **
99ac0 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 This implementa
99ad0 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 tion also assume
99ae0 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 s a coherent cac
99af0 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 he - that . **
99b00 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 separate process
99b10 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 es cannot read d
99b20 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 ifferent values
99b30 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 from the same.
99b40 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68 ** address at th
99b50 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 e same time. If
99b60 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 either of these
99b70 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a two conditions.
99b80 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 ** are not met
99b90 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 , then the mutex
99ba0 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 es will fail and
99bb0 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 problems will r
99bc0 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a esult.. */. {.
99bd0 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 pthread_t se
99be0 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c lf = pthread_sel
99bf0 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e f();. if( p->
99c00 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 nRef>0 && pthrea
99c10 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 d_equal(p->owner
99c20 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 , self) ){.
99c30 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 p->nRef++;.
99c40 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
99c50 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
99c60 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 pthread_mutex_tr
99c70 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 ylock(&p->mutex)
99c80 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ==0 ){. ass
99c90 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 ert( p->nRef==0
99ca0 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 );. p->owne
99cb0 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 r = self;.
99cc0 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 p->nRef = 1;.
99cd0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
99ce0 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 K;. }else{.
99cf0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
99d00 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a BUSY;. }. }.
99d10 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20 74 #else. /* Use t
99d20 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63 75 he built-in recu
99d30 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69 66 rsive mutexes if
99d40 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c 61 they are availa
99d50 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ble.. */. if(
99d60 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 pthread_mutex_tr
99d70 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 ylock(&p->mutex)
99d80 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 ==0 ){. p->ow
99d90 6e 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 ner = pthread_se
99da0 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 lf();. p->nRe
99db0 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 f++;. rc = SQ
99dc0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
99dd0 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
99de0 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 E_BUSY;. }.#end
99df0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
99e00 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 72 63 E_DEBUG. if( rc
99e10 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
99e20 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 ->trace ){. p
99e30 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74 rintf("enter mut
99e40 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 ex %p (%d) with
99e50 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 nRef=%d\n", p, p
99e60 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 ->trace, p->nRef
99e70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
99e80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
99e90 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
99ea0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f mutex_leave() ro
99eb0 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 utine exits a mu
99ec0 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 tex that was.**
99ed0 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 previously enter
99ee0 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 ed by the same t
99ef0 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 hread. The beha
99f00 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 vior.** is undef
99f10 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 ined if the mute
99f20 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 x is not current
99f30 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a ly entered or.**
99f40 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
99f50 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 y allocated. SQ
99f60 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 Lite will never
99f70 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 do either..*/.st
99f80 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 atic void pthrea
99f90 64 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 dMutexLeave(sqli
99fa0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
99fb0 20 61 73 73 65 72 74 28 20 70 74 68 72 65 61 64 assert( pthread
99fc0 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a MutexHeld(p) );.
99fd0 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 p->nRef--;. a
99fe0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
99ff0 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 0 || p->id==SQLI
9a000 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
9a010 56 45 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 VE );..#ifdef SQ
9a020 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 LITE_HOMEGROWN_R
9a030 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 ECURSIVE_MUTEX.
9a040 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 if( p->nRef==0
9a050 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6d ){. pthread_m
9a060 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e utex_unlock(&p->
9a070 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73 mutex);. }.#els
9a080 65 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 e. pthread_mute
9a090 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 x_unlock(&p->mut
9a0a0 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 ex);.#endif..#if
9a0b0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
9a0c0 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 . if( p->trace
9a0d0 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 6c ){. printf("l
9a0e0 65 61 76 65 20 6d 75 74 65 78 20 25 70 20 28 25 eave mutex %p (%
9a0f0 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c d) with nRef=%d\
9a100 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c n", p, p->trace,
9a110 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 p->nRef);. }.#
9a120 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f endif.}..SQLITE_
9a130 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
9a140 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 mutex_methods *s
9a150 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 qlite3DefaultMut
9a160 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ex(void){. stat
9a170 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
9a180 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 _methods sMutex
9a190 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 4d = {. pthreadM
9a1a0 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 70 74 utexInit,. pt
9a1b0 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20 hreadMutexEnd,.
9a1c0 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 41 pthreadMutexA
9a1d0 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72 65 61 lloc,. pthrea
9a1e0 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 dMutexFree,.
9a1f0 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65 pthreadMutexEnte
9a200 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 r,. pthreadMu
9a210 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74 68 72 texTry,. pthr
9a220 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 eadMutexLeave,.#
9a230 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
9a240 55 47 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 UG. pthreadMu
9a250 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70 74 68 texHeld,. pth
9a260 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 readMutexNotheld
9a270 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 .#endif. };..
9a280 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a return &sMutex;.
9a290 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
9a2a0 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
9a2b0 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a D */../*********
9a2c0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 ***** End of mut
9a2d0 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a ex_unix.c ******
9a2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a300 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
9a310 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
9a320 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a mutex_w32.c ***
9a330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a350 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
9a360 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a August 14.**.**
9a370 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
9a380 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
9a390 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
9a3a0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
9a3b0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
9a3c0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
9a3d0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
9a3e0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
9a3f0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
9a400 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
9a410 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
9a420 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
9a430 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
9a440 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
9a450 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
9a460 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
9a470 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
9a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
9a4d0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
9a4e0 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 the C functions
9a4f0 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d that implement m
9a500 75 74 65 78 65 73 20 66 6f 72 20 77 69 6e 33 32 utexes for win32
9a510 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 .**.** $Id: mute
9a520 78 5f 77 33 32 2e 63 2c 76 20 31 2e 31 32 20 32 x_w32.c,v 1.12 2
9a530 30 30 38 2f 31 31 2f 31 30 20 32 30 3a 30 31 3a 008/11/10 20:01:
9a540 34 31 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 41 shane Exp $.*
9a550 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 /../*.** The cod
9a560 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 e in this file i
9a570 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 77 s only used if w
9a580 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 20 e are compiling
9a590 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a multithreaded.**
9a5a0 20 6f 6e 20 61 20 77 69 6e 33 32 20 73 79 73 74 on a win32 syst
9a5b0 65 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 em..*/.#ifdef SQ
9a5c0 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 0a LITE_MUTEX_W32..
9a5d0 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 /*.** Each recur
9a5e0 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e sive mutex is an
9a5f0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
9a600 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
9a610 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ture..*/.struct
9a620 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a sqlite3_mutex {.
9a630 20 20 43 52 49 54 49 43 41 4c 5f 53 45 43 54 49 CRITICAL_SECTI
9a640 4f 4e 20 6d 75 74 65 78 3b 20 20 20 20 2f 2a 20 ON mutex; /*
9a650 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e Mutex controllin
9a660 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 g the lock */.
9a670 69 6e 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 int id;
9a680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 /* Mu
9a690 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e tex type */. in
9a6a0 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
9a6b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
9a6c0 65 72 20 6f 66 20 65 6e 74 65 72 61 6e 63 65 73 er of enterances
9a6d0 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6f 77 6e 65 */. DWORD owne
9a6e0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
9a6f0 20 2f 2a 20 54 68 72 65 61 64 20 68 6f 6c 64 69 /* Thread holdi
9a700 6e 67 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f ng this mutex */
9a710 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 .};../*.** Retur
9a720 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f n true (non-zero
9a730 29 20 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e ) if we are runn
9a740 69 6e 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c ing under WinNT,
9a750 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a Win2K, WinXP,.*
9a760 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 * or WinCE. Ret
9a770 75 72 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 urn false (zero)
9a780 20 66 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 for Win95, Win9
9a790 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 8, or WinME..**.
9a7a0 2a 2a 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e ** Here is an in
9a7b0 74 65 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76 teresting observ
9a7c0 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57 ation: Win95, W
9a7d0 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20 in98, and WinME
9a7e0 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b lack.** the Lock
9a7f0 46 69 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42 FileEx() API. B
9a800 75 74 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 ut we can still
9a810 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 statically link
9a820 61 67 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20 against that.**
9a830 41 50 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 API as long as w
9a840 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 e don't call it
9a850 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39 win running Win9
9a860 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 5/98/ME. A call
9a870 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 to.** this rout
9a880 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 ine is used to d
9a890 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 etermine if the
9a8a0 68 6f 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38 host is Win95/98
9a8b0 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f /ME or.** WinNT/
9a8c0 32 4b 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65 2K/XP so that we
9a8d0 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68 will know wheth
9a8e0 65 72 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e er or not we can
9a8f0 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 safely call.**
9a900 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 the LockFileEx()
9a910 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d 75 74 65 API..**.** mute
9a920 78 49 73 4e 54 28 29 20 69 73 20 6f 6e 6c 79 20 xIsNT() is only
9a930 75 73 65 64 20 66 6f 72 20 74 68 65 20 54 72 79 used for the Try
9a940 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 EnterCriticalSec
9a950 74 69 6f 6e 28 29 20 41 50 49 20 63 61 6c 6c 2c tion() API call,
9a960 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 6f 6e 6c .** which is onl
9a970 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 79 y available if y
9a980 6f 75 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 our application
9a990 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 was compiled wit
9a9a0 68 20 0a 2a 2a 20 5f 57 49 4e 33 32 5f 57 49 4e h .** _WIN32_WIN
9a9b0 4e 54 20 64 65 66 69 6e 65 64 20 74 6f 20 61 20 NT defined to a
9a9c0 76 61 6c 75 65 20 3e 3d 20 30 78 30 34 30 30 2e value >= 0x0400.
9a9d0 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 Currently, the
9a9e0 20 6f 6e 6c 79 0a 2a 2a 20 63 61 6c 6c 20 74 6f only.** call to
9a9f0 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 TryEnterCritica
9aa00 6c 53 65 63 74 69 6f 6e 28 29 20 69 73 20 23 69 lSection() is #i
9aa10 66 64 65 66 27 65 64 20 6f 75 74 2c 20 73 6f 20 fdef'ed out, so
9aa20 23 69 66 64 65 66 20 0a 2a 2a 20 74 68 69 73 20 #ifdef .** this
9aa30 6f 75 74 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a out as well..*/.
9aa40 23 69 66 20 30 0a 23 69 66 20 53 51 4c 49 54 45 #if 0.#if SQLITE
9aa50 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 _OS_WINCE.# defi
9aa60 6e 65 20 6d 75 74 65 78 49 73 4e 54 28 29 20 20 ne mutexIsNT()
9aa70 28 31 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 (1).#else. stat
9aa80 69 63 20 69 6e 74 20 6d 75 74 65 78 49 73 4e 54 ic int mutexIsNT
9aa90 28 76 6f 69 64 29 7b 0a 20 20 20 20 73 74 61 74 (void){. stat
9aaa0 69 63 20 69 6e 74 20 6f 73 54 79 70 65 20 3d 20 ic int osType =
9aab0 30 3b 0a 20 20 20 20 69 66 28 20 6f 73 54 79 70 0;. if( osTyp
9aac0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 e==0 ){. OS
9aad0 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 VERSIONINFO sInf
9aae0 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 o;. sInfo.d
9aaf0 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 wOSVersionInfoSi
9ab00 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 ze = sizeof(sInf
9ab10 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72 o);. GetVer
9ab20 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a sionEx(&sInfo);.
9ab30 20 20 20 20 20 20 6f 73 54 79 70 65 20 3d 20 73 osType = s
9ab40 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49 Info.dwPlatformI
9ab50 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f d==VER_PLATFORM_
9ab60 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31 WIN32_NT ? 2 : 1
9ab70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
9ab80 72 6e 20 6f 73 54 79 70 65 3d 3d 32 3b 0a 20 20 rn osType==2;.
9ab90 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
9aba0 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 23 TE_OS_WINCE */.#
9abb0 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
9abc0 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a LITE_DEBUG./*.**
9abd0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
9abe0 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 ex_held() and sq
9abf0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
9ac00 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 eld() routine ar
9ac10 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f e.** intended fo
9ac20 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 r use only insid
9ac30 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 e assert() state
9ac40 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 ments..*/.static
9ac50 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 48 65 6c int winMutexHel
9ac60 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 d(sqlite3_mutex
9ac70 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
9ac80 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f >nRef!=0 && p->o
9ac90 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 wner==GetCurrent
9aca0 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 73 74 ThreadId();.}.st
9acb0 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 atic int winMute
9acc0 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
9acd0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 _mutex *p){. re
9ace0 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 turn p->nRef==0
9acf0 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d 47 65 74 || p->owner!=Get
9ad00 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 CurrentThreadId(
9ad10 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a );.}.#endif.../*
9ad20 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 .** Initialize a
9ad30 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 nd deinitialize
9ad40 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
9ad50 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 tem..*/.static i
9ad60 6e 74 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 nt winMutexInit(
9ad70 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 void){ return SQ
9ad80 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 LITE_OK; }.stati
9ad90 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 45 6e c int winMutexEn
9ada0 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 d(void){ return
9adb0 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a SQLITE_OK; }../*
9adc0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
9add0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f mutex_alloc() ro
9ade0 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 utine allocates
9adf0 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 a new.** mutex a
9ae00 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 nd returns a poi
9ae10 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 nter to it. If
9ae20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a it returns NULL.
9ae30 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 ** that means th
9ae40 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 at a mutex could
9ae50 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 not be allocate
9ae60 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 d. SQLite.** wi
9ae70 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 ll unwind its st
9ae80 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ack and return a
9ae90 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 n error. The ar
9aea0 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c gument.** to sql
9aeb0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
9aec0 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 () is one of the
9aed0 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 se integer const
9aee0 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e ants:.**.** <ul>
9aef0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
9af00 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 _MUTEX_FAST
9af10 20 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c 0.** <
9af20 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
9af30 58 5f 52 45 43 55 52 53 49 56 45 20 20 20 20 20 X_RECURSIVE
9af40 20 20 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 1.** <li>
9af50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
9af60 54 49 43 5f 4d 41 53 54 45 52 20 20 20 20 20 20 TIC_MASTER
9af70 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 2.** <li> SQLIT
9af80 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
9af90 45 4d 20 20 20 20 20 20 20 20 20 33 0a 2a 2a 20 EM 3.**
9afa0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
9afb0 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 EX_STATIC_PRNG
9afc0 20 20 20 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 4.** </ul>
9afd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
9afe0 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 two constants c
9aff0 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 ause sqlite3_mut
9b000 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 ex_alloc() to cr
9b010 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 eate.** a new mu
9b020 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 tex. The new mu
9b030 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65 tex is recursive
9b040 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 when SQLITE_MUT
9b050 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 EX_RECURSIVE.**
9b060 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 is used but not
9b070 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 necessarily so w
9b080 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 hen SQLITE_MUTEX
9b090 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a _FAST is used..*
9b0a0 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c * The mutex impl
9b0b0 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 ementation does
9b0c0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 not need to make
9b0d0 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a a distinction.*
9b0e0 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 * between SQLITE
9b0f0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
9b100 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 and SQLITE_MUTE
9b110 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 X_FAST if it doe
9b120 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f s.** not want to
9b130 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 . But SQLite wi
9b140 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 ll only request
9b150 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 a recursive mute
9b160 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 x in.** cases wh
9b170 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 ere it really ne
9b180 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 eds one. If a f
9b190 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 aster non-recurs
9b1a0 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 ive mutex.** imp
9b1b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 lementation is a
9b1c0 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 vailable on the
9b1d0 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 host platform, t
9b1e0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 he mutex subsyst
9b1f0 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 em.** might retu
9b200 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 rn such a mutex
9b210 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 in response to S
9b220 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
9b230 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 ..**.** The othe
9b240 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 r allowed parame
9b250 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f ters to sqlite3_
9b260 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 mutex_alloc() ea
9b270 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 ch return.** a p
9b280 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 ointer to a stat
9b290 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d ic preexisting m
9b2a0 75 74 65 78 2e 20 20 54 68 72 65 65 20 73 74 61 utex. Three sta
9b2b0 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a tic mutexes are.
9b2c0 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 ** used by the c
9b2d0 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f urrent version o
9b2e0 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 f SQLite. Futur
9b2f0 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 e versions of SQ
9b300 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 Lite.** may add
9b310 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 additional stati
9b320 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 c mutexes. Stat
9b330 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 ic mutexes are f
9b340 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
9b350 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c se by SQLite onl
9b360 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 y. Applications
9b370 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 that use SQLite
9b380 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a mutexes should.
9b390 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 ** use only the
9b3a0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 dynamic mutexes
9b3b0 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 returned by SQLI
9b3c0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 TE_MUTEX_FAST or
9b3d0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 .** SQLITE_MUTEX
9b3e0 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a _RECURSIVE..**.*
9b3f0 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f * Note that if o
9b400 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 ne of the dynami
9b410 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 c mutex paramete
9b420 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 rs (SQLITE_MUTEX
9b430 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 _FAST.** or SQLI
9b440 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
9b450 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e VE) is used then
9b460 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
9b470 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e lloc().** return
9b480 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 s a different mu
9b490 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c tex on every cal
9b4a0 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 l. But for the
9b4b0 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 static .** mutex
9b4c0 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 types, the same
9b4d0 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e mutex is return
9b4e0 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c ed on every call
9b4f0 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 that has.** the
9b500 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 same type numbe
9b510 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c r..*/.static sql
9b520 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d ite3_mutex *winM
9b530 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 utexAlloc(int iT
9b540 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f ype){. sqlite3_
9b550 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69 mutex *p;.. swi
9b560 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 tch( iType ){.
9b570 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 case SQLITE_MU
9b580 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 TEX_FAST:. ca
9b590 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f se SQLITE_MUTEX_
9b5a0 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 RECURSIVE: {.
9b5b0 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 p = sqlite3Ma
9b5c0 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 llocZero( sizeof
9b5d0 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 (*p) );. if
9b5e0 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ( p ){. p
9b5f0 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 ->id = iType;.
9b600 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 Initialize
9b610 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
9b620 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 &p->mutex);.
9b630 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
9b640 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
9b650 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 lt: {. stat
9b660 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
9b670 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 staticMutexes[6
9b680 5d 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 ];. static
9b690 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a int isInit = 0;.
9b6a0 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 69 73 while( !is
9b6b0 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Init ){.
9b6c0 73 74 61 74 69 63 20 6c 6f 6e 67 20 6c 6f 63 6b static long lock
9b6d0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 = 0;. if
9b6e0 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 49 6e 63 ( InterlockedInc
9b6f0 72 65 6d 65 6e 74 28 26 6c 6f 63 6b 29 3d 3d 31 rement(&lock)==1
9b700 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e ){. in
9b710 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 66 t i;. f
9b720 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 or(i=0; i<sizeof
9b730 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f (staticMutexes)/
9b740 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 sizeof(staticMut
9b750 65 78 65 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a exes[0]); i++){.
9b760 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 69 74 Init
9b770 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 ializeCriticalSe
9b780 63 74 69 6f 6e 28 26 73 74 61 74 69 63 4d 75 74 ction(&staticMut
9b790 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a exes[i].mutex);.
9b7a0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
9b7b0 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31 isInit = 1
9b7c0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
9b7d0 0a 20 20 20 20 20 20 20 20 20 20 53 6c 65 65 70 . Sleep
9b7e0 28 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 (1);. }.
9b7f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
9b800 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 ert( iType-2 >=
9b810 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0 );. asser
9b820 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a t( iType-2 < siz
9b830 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 eof(staticMutexe
9b840 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 s)/sizeof(static
9b850 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 Mutexes[0]) );.
9b860 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 p = &static
9b870 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d Mutexes[iType-2]
9b880 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 ;. p->id =
9b890 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 iType;. bre
9b8a0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
9b8b0 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a return p;.}.../*
9b8c0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
9b8d0 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 deallocates a p
9b8e0 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c reviously.** all
9b8f0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 ocated mutex. S
9b900 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c QLite is careful
9b910 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 to deallocate e
9b920 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 very.** mutex th
9b930 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e at it allocates.
9b940 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
9b950 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c winMutexFree(sql
9b960 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
9b970 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 assert( p );.
9b980 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
9b990 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
9b9a0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
9b9b0 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e UTEX_FAST || p->
9b9c0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
9b9d0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
9b9e0 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65 DeleteCriticalSe
9b9f0 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 ction(&p->mutex)
9ba00 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
9ba10 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 (p);.}../*.** Th
9ba20 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
9ba30 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 enter() and sqli
9ba40 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
9ba50 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 routines attempt
9ba60 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d .** to enter a m
9ba70 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 utex. If anothe
9ba80 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 r thread is alre
9ba90 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d ady within the m
9baa0 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 utex,.** sqlite3
9bab0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 _mutex_enter() w
9bac0 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 ill block and sq
9bad0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
9bae0 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a ) will return.**
9baf0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 SQLITE_BUSY. T
9bb00 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
9bb10 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 _try() interface
9bb20 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
9bb30 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 OK.** upon succe
9bb40 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 ssful entry. Mu
9bb50 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 texes created us
9bb60 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ing SQLITE_MUTEX
9bb70 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a _RECURSIVE can.*
9bb80 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c * be entered mul
9bb90 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 tiple times by t
9bba0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 he same thread.
9bbb0 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 In such cases t
9bbc0 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 he,.** mutex mus
9bbd0 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 t be exited an e
9bbe0 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 qual number of t
9bbf0 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 imes before anot
9bc00 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 her thread.** ca
9bc10 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 n enter. If the
9bc20 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 same thread tri
9bc30 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 es to enter any
9bc40 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 other kind of mu
9bc50 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e tex.** more than
9bc60 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 once, the behav
9bc70 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
9bc80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
9bc90 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73 winMutexEnter(s
9bca0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
9bcb0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 {. assert( p->i
9bcc0 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
9bcd0 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e RECURSIVE || win
9bce0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 MutexNotheld(p)
9bcf0 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63 );. EnterCritic
9bd00 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 alSection(&p->mu
9bd10 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 tex);. p->owner
9bd20 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 = GetCurrentThr
9bd30 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e eadId(); . p->n
9bd40 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 Ref++;.}.static
9bd50 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28 int winMutexTry(
9bd60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
9bd70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
9bd80 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73 LITE_BUSY;. ass
9bd90 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
9bda0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
9bdb0 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f VE || winMutexNo
9bdc0 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a theld(p) );. /*
9bdd0 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 . ** The sqlite
9bde0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
9bdf0 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72 61 utine is very ra
9be00 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77 rely used, and w
9be10 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75 hen it. ** is u
9be20 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79 sed it is merely
9be30 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e an optimization
9be40 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66 . So it is OK f
9be50 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a or it to always.
9be60 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a ** fail. . *
9be70 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e *. ** The TryEn
9be80 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
9be90 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 on() interface i
9bea0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
9beb0 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20 on WinNT.. **
9bec0 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73 And some windows
9bed0 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c compilers compl
9bee0 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74 ain if you try t
9bef0 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74 o use it without
9bf00 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e . ** first doin
9bf10 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20 g some #defines
9bf20 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c that prevent SQL
9bf30 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e ite from buildin
9bf40 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a g on Win98.. **
9bf50 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e For that reason
9bf60 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74 , we will omit t
9bf70 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e his optimization
9bf80 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20 for now. See.
9bf90 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35 ** ticket #2685
9bfa0 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69 .. */.#if 0. i
9bfb0 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26 f( mutexIsNT() &
9bfc0 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 & TryEnterCritic
9bfd0 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 alSection(&p->mu
9bfe0 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f tex) ){. p->o
9bff0 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e wner = GetCurren
9c000 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20 tThreadId();.
9c010 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 p->nRef++;.
9c020 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
9c030 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 }.#endif. ret
9c040 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
9c050 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
9c060 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 ex_leave() routi
9c070 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 ne exits a mutex
9c080 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 that was.** pre
9c090 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 viously entered
9c0a0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
9c0b0 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f ad. The behavio
9c0c0 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 r.** is undefine
9c0d0 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 d if the mutex i
9c0e0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
9c0f0 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 entered or.** is
9c100 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 not currently a
9c110 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
9c120 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 e will never do
9c130 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 either..*/.stati
9c140 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c c void winMutexL
9c150 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
9c160 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ex *p){. assert
9c170 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
9c180 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 assert( p->owne
9c190 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 r==GetCurrentThr
9c1a0 65 61 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e eadId() );. p->
9c1b0 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 nRef--;. assert
9c1c0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 ( p->nRef==0 ||
9c1d0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
9c1e0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b TEX_RECURSIVE );
9c1f0 0a 20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c . LeaveCritical
9c200 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 Section(&p->mute
9c210 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 x);.}..SQLITE_PR
9c220 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 IVATE sqlite3_mu
9c230 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c tex_methods *sql
9c240 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 ite3DefaultMutex
9c250 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
9c260 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
9c270 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 ethods sMutex =
9c280 7b 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e {. winMutexIn
9c290 69 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 it,. winMutex
9c2a0 45 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 End,. winMute
9c2b0 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d xAlloc,. winM
9c2c0 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 utexFree,. wi
9c2d0 6e 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 nMutexEnter,.
9c2e0 20 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 winMutexTry,.
9c2f0 20 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c winMutexLeave,
9c300 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
9c310 45 42 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 EBUG. winMute
9c320 78 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 xHeld,. winMu
9c330 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69 texNotheld.#endi
9c340 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e f. };.. return
9c350 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 &sMutex;.}.#end
9c360 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
9c370 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EX_W32 */../****
9c380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
9c390 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a f mutex_w32.c **
9c3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9c3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9c3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
9c3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
9c3e0 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a file malloc.c *
9c3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9c400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9c410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
9c420 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
9c430 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
9c440 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
9c450 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
9c460 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
9c470 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
9c480 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
9c490 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
9c4a0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
9c4b0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
9c4c0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
9c4d0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
9c4e0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
9c4f0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
9c500 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
9c510 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
9c520 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
9c530 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
9c540 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
9c550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9c560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9c570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9c580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9c590 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61 *.**.** Memory a
9c5a0 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
9c5b0 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 ons used through
9c5c0 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a out sqlite..**.*
9c5d0 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63 2c * $Id: malloc.c,
9c5e0 76 20 31 2e 34 38 20 32 30 30 38 2f 31 31 2f 31 v 1.48 2008/11/1
9c5f0 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65 9 09:05:27 danie
9c600 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
9c610 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
9c620 69 6e 65 20 72 75 6e 73 20 77 68 65 6e 20 74 68 ine runs when th
9c630 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
9c640 6f 72 20 73 65 65 73 20 74 68 61 74 20 74 68 65 or sees that the
9c650 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 .** total memory
9c660 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 allocation is a
9c670 62 6f 75 74 20 74 6f 20 65 78 63 65 65 64 20 74 bout to exceed t
9c680 68 65 20 73 6f 66 74 20 68 65 61 70 0a 2a 2a 20 he soft heap.**
9c690 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 limit..*/.static
9c6a0 20 76 6f 69 64 20 73 6f 66 74 48 65 61 70 4c 69 void softHeapLi
9c6b0 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a 20 20 76 mitEnforcer(. v
9c6c0 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 0a 20 oid *NotUsed, .
9c6d0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 4e sqlite3_int64 N
9c6e0 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e 74 20 61 otUsed2,. int a
9c6f0 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 20 55 4e llocSize.){. UN
9c700 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 USED_PARAMETER2(
9c710 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 NotUsed, NotUsed
9c720 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 2);. sqlite3_re
9c730 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c lease_memory(all
9c740 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ocSize);.}../*.*
9c750 2a 20 53 65 74 20 74 68 65 20 73 6f 66 74 20 68 * Set the soft h
9c760 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20 66 eap-size limit f
9c770 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 or the library.
9c780 50 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20 6f Passing a zero o
9c790 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 76 r .** negative v
9c7a0 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 6e alue indicates n
9c7b0 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 o limit..*/.SQLI
9c7c0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
9c7d0 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 te3_soft_heap_li
9c7e0 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73 71 mit(int n){. sq
9c7f0 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c 69 lite3_uint64 iLi
9c800 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72 61 mit;. int overa
9c810 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b ge;. if( n<0 ){
9c820 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 3b . iLimit = 0;
9c830 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c . }else{. iL
9c840 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 imit = n;. }.
9c850 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
9c860 7a 65 28 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d ze();. if( iLim
9c870 69 74 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 it>0 ){. sqli
9c880 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 te3MemoryAlarm(s
9c890 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f oftHeapLimitEnfo
9c8a0 72 63 65 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29 rcer, 0, iLimit)
9c8b0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
9c8c0 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 qlite3MemoryAlar
9c8d0 6d 28 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a m(0, 0, 0);. }.
9c8e0 20 20 6f 76 65 72 61 67 65 20 3d 20 73 71 6c 69 overage = sqli
9c8f0 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 te3_memory_used(
9c900 29 20 2d 20 6e 3b 0a 20 20 69 66 28 20 6f 76 65 ) - n;. if( ove
9c910 72 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73 71 rage>0 ){. sq
9c920 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
9c930 6d 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a 20 mory(overage);.
9c940 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 }.}../*.** Atte
9c950 6d 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20 75 mpt to release u
9c960 70 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66 20 p to n bytes of
9c970 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 non-essential me
9c980 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a mory currently.*
9c990 2a 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65 * held by SQLite
9c9a0 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 . An example of
9c9b0 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 non-essential me
9c9c0 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20 75 mory is memory u
9c9d0 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20 sed to.** cache
9c9e0 64 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 database pages t
9c9f0 68 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72 72 hat are not curr
9ca00 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f ently in use..*/
9ca10 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
9ca20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
9ca30 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 memory(int n){.#
9ca40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
9ca50 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 BLE_MEMORY_MANAG
9ca60 45 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 EMENT. int nRet
9ca70 20 3d 20 30 3b 0a 23 69 66 20 30 0a 20 20 6e 52 = 0;.#if 0. nR
9ca80 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 et += sqlite3Vdb
9ca90 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e eReleaseMemory(n
9caa0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 52 65 74 );.#endif. nRet
9cab0 20 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 += sqlite3Pcach
9cac0 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e eReleaseMemory(n
9cad0 2d 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72 6e -nRet);. return
9cae0 20 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55 nRet;.#else. U
9caf0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
9cb00 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c n);. return SQL
9cb10 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d ITE_OK;.#endif.}
9cb20 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e ../*.** State in
9cb30 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 formation local
9cb40 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c to the memory al
9cb50 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
9cb60 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 em..*/.static SQ
9cb70 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 LITE_WSD struct
9cb80 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f Mem0Global {. /
9cb90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 * Number of free
9cba0 20 70 61 67 65 73 20 66 6f 72 20 73 63 72 61 74 pages for scrat
9cbb0 63 68 20 61 6e 64 20 70 61 67 65 2d 63 61 63 68 ch and page-cach
9cbc0 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33 e memory */. u3
9cbd0 32 20 6e 53 63 72 61 74 63 68 46 72 65 65 3b 0a 2 nScratchFree;.
9cbe0 20 20 75 33 32 20 6e 50 61 67 65 46 72 65 65 3b u32 nPageFree;
9cbf0 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .. sqlite3_mute
9cc00 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 x *mutex;
9cc10 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 73 65 /* Mutex to se
9cc20 72 69 61 6c 69 7a 65 20 61 63 63 65 73 73 20 2a rialize access *
9cc30 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 /.. /*. ** The
9cc40 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 alarm callback
9cc50 61 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74 and its argument
9cc60 73 2e 20 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 s. The mem0.mut
9cc70 65 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a ex lock will. *
9cc80 2a 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 * be held while
9cc90 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 the callback is
9cca0 72 75 6e 6e 69 6e 67 2e 20 20 52 65 63 75 72 73 running. Recurs
9ccb0 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 ive calls into.
9ccc0 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 ** the memory s
9ccd0 75 62 73 79 73 74 65 6d 20 61 72 65 20 61 6c 6c ubsystem are all
9cce0 6f 77 65 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77 owed, but no new
9ccf0 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 callbacks will
9cd00 62 65 0a 20 20 2a 2a 20 69 73 73 75 65 64 2e 20 be. ** issued.
9cd10 20 54 68 65 20 61 6c 61 72 6d 42 75 73 79 20 76 The alarmBusy v
9cd20 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 ariable is set t
9cd30 6f 20 70 72 65 76 65 6e 74 20 72 65 63 75 72 73 o prevent recurs
9cd40 69 76 65 0a 20 20 2a 2a 20 63 61 6c 6c 62 61 63 ive. ** callbac
9cd50 6b 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ks.. */. sqlit
9cd60 65 33 5f 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 e3_int64 alarmTh
9cd70 72 65 73 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 reshold;. void
9cd80 28 2a 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 (*alarmCallback)
9cd90 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f (void*, sqlite3_
9cda0 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f int64,int);. vo
9cdb0 69 64 20 2a 61 6c 61 72 6d 41 72 67 3b 0a 20 20 id *alarmArg;.
9cdc0 69 6e 74 20 61 6c 61 72 6d 42 75 73 79 3b 0a 0a int alarmBusy;..
9cdd0 20 20 2f 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 /*. ** Pointe
9cde0 72 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 rs to the end of
9cdf0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9ce00 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 61 6e nfig.pScratch an
9ce10 64 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c d. ** sqlite3Gl
9ce20 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 obalConfig.pPage
9ce30 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d to a block of m
9ce40 65 6d 6f 72 79 20 74 68 61 74 20 72 65 63 6f 72 emory that recor
9ce50 64 73 0a 20 20 2a 2a 20 77 68 69 63 68 20 70 61 ds. ** which pa
9ce60 67 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c ges are availabl
9ce70 65 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61 e.. */. u32 *a
9ce80 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 ScratchFree;. u
9ce90 33 32 20 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d 32 *aPageFree;.}
9cea0 20 6d 65 6d 30 20 3d 20 7b 20 36 32 35 36 30 39 mem0 = { 625609
9ceb0 35 35 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 55, 0, 0, 0, 0,
9cec0 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 0, 0, 0, 0 };..#
9ced0 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 define mem0 GLOB
9cee0 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c AL(struct Mem0Gl
9cef0 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a obal, mem0)../*.
9cf00 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 ** Initialize th
9cf10 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
9cf20 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a ion subsystem..*
9cf30 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9cf40 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c int sqlite3Mall
9cf50 6f 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 ocInit(void){.
9cf60 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9cf70 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f lConfig.m.xMallo
9cf80 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 c==0 ){. sqli
9cf90 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 te3MemSetDefault
9cfa0 28 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 ();. }. memset
9cfb0 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f (&mem0, 0, sizeo
9cfc0 66 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20 f(mem0));. if(
9cfd0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9cfe0 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 fig.bCoreMutex )
9cff0 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78 {. mem0.mutex
9d000 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
9d010 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
9d020 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 X_STATIC_MEM);.
9d030 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
9d040 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9d050 72 61 74 63 68 20 26 26 20 73 71 6c 69 74 65 33 ratch && sqlite3
9d060 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 GlobalConfig.szS
9d070 63 72 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20 cratch>=100.
9d080 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 && sqlite3Glob
9d090 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 alConfig.nScratc
9d0a0 68 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 h>=0 ){. int
9d0b0 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c i;. sqlite3Gl
9d0c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9d0d0 61 74 63 68 20 2d 3d 20 34 3b 0a 20 20 20 20 6d atch -= 4;. m
9d0e0 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 em0.aScratchFree
9d0f0 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68 61 72 = (u32*)&((char
9d100 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 *)sqlite3GlobalC
9d110 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 0a onfig.pScratch).
9d120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9d130 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c [sqlite3Global
9d140 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 Config.szScratch
9d150 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f *sqlite3GlobalCo
9d160 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a nfig.nScratch];.
9d170 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 for(i=0; i<s
9d180 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9d190 69 67 2e 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b ig.nScratch; i++
9d1a0 29 7b 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 ){ mem0.aScratch
9d1b0 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 Free[i] = i; }.
9d1c0 20 20 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 mem0.nScratch
9d1d0 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c Free = sqlite3Gl
9d1e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
9d1f0 74 63 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 tch;. }else{.
9d200 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
9d210 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d onfig.pScratch =
9d220 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 0;. sqlite3G
9d230 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 lobalConfig.szSc
9d240 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 ratch = 0;. }.
9d250 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9d260 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 26 alConfig.pPage &
9d270 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 & sqlite3GlobalC
9d280 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31 onfig.szPage>=51
9d290 32 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 2. && sqlit
9d2a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
9d2b0 50 61 67 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69 Page>=1 ){. i
9d2c0 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76 nt i;. int ov
9d2d0 65 72 68 65 61 64 3b 0a 20 20 20 20 69 6e 74 20 erhead;. int
9d2e0 73 7a 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 sz = sqlite3Glob
9d2f0 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3b alConfig.szPage;
9d300 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c . int n = sql
9d310 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9d320 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72 .nPage;. over
9d330 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a head = (4*n + sz
9d340 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71 - 1)/sz;. sq
9d350 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9d360 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68 g.nPage -= overh
9d370 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50 ead;. mem0.aP
9d380 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29 ageFree = (u32*)
9d390 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 &((char*)sqlite3
9d3a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 GlobalConfig.pPa
9d3b0 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 ge).
9d3c0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c [sqlite3Gl
9d3d0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 obalConfig.szPag
9d3e0 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 e*sqlite3GlobalC
9d3f0 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20 onfig.nPage];.
9d400 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c for(i=0; i<sql
9d410 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9d420 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65 .nPage; i++){ me
9d430 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20 m0.aPageFree[i]
9d440 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e = i; }. mem0.
9d450 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69 nPageFree = sqli
9d460 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9d470 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a nPage;. }else{.
9d480 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
9d490 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 lConfig.pPage =
9d4a0 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 0;. sqlite3Gl
9d4b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 obalConfig.szPag
9d4c0 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 e = 0;. }. ret
9d4d0 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 urn sqlite3Globa
9d4e0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28 lConfig.m.xInit(
9d4f0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9d500 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b fig.m.pAppData);
9d510 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 .}../*.** Deinit
9d520 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 ialize the memor
9d530 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
9d540 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 system..*/.SQLIT
9d550 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
9d560 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 qlite3MallocEnd(
9d570 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 void){. sqlite3
9d580 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9d590 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 Shutdown(sqlite3
9d5a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 GlobalConfig.m.p
9d5b0 41 70 70 44 61 74 61 29 3b 0a 20 20 6d 65 6d 73 AppData);. mems
9d5c0 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a et(&mem0, 0, siz
9d5d0 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f eof(mem0));.}../
9d5e0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
9d5f0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
9d600 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b currently check
9d610 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 ed out..*/.SQLIT
9d620 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e E_API sqlite3_in
9d630 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f t64 sqlite3_memo
9d640 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 ry_used(void){.
9d650 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 int n, mx;. sq
9d660 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b lite3_int64 res;
9d670 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 . sqlite3_statu
9d680 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f s(SQLITE_STATUS_
9d690 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c MEMORY_USED, &n,
9d6a0 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20 &mx, 0);. res
9d6b0 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 = (sqlite3_int64
9d6c0 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f )n; /* Work aro
9d6d0 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 und bug in Borla
9d6e0 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 nd C. Ticket #32
9d6f0 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 16 */. return r
9d700 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 es;.}../*.** Ret
9d710 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 urn the maximum
9d720 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
9d730 20 74 68 61 74 20 68 61 73 20 65 76 65 72 20 62 that has ever b
9d740 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f een.** checked o
9d750 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 20 ut since either
9d760 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
9d770 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a this process.**
9d780 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f or since the mo
9d790 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e st recent reset.
9d7a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
9d7b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c qlite3_int64 sql
9d7c0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 ite3_memory_high
9d7d0 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 water(int resetF
9d7e0 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d lag){. int n, m
9d7f0 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 x;. sqlite3_int
9d800 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 64 res;. sqlite
9d810 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f 3_status(SQLITE_
9d820 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
9d830 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73 ED, &n, &mx, res
9d840 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d etFlag);. res =
9d850 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
9d860 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f mx; /* Work aro
9d870 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 und bug in Borla
9d880 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 nd C. Ticket #32
9d890 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 16 */. return r
9d8a0 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 es;.}../*.** Cha
9d8b0 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61 nge the alarm ca
9d8c0 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 llback.*/.SQLITE
9d8d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
9d8e0 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 ite3MemoryAlarm(
9d8f0 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 . void(*xCallba
9d900 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 ck)(void *pArg,
9d910 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 sqlite3_int64 us
9d920 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 ed,int N),. voi
9d930 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 d *pArg,. sqlit
9d940 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 e3_int64 iThresh
9d950 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 old.){. sqlite3
9d960 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
9d970 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 0.mutex);. mem0
9d980 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d .alarmCallback =
9d990 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 xCallback;. me
9d9a0 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 m0.alarmArg = pA
9d9b0 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d rg;. mem0.alarm
9d9c0 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 Threshold = iThr
9d9d0 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 eshold;. sqlite
9d9e0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9d9f0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 m0.mutex);. ret
9da00 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
9da10 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
9da20 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 _OMIT_DEPRECATED
9da30 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 ./*.** Deprecate
9da40 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 d external inter
9da50 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f face. Internal/
9da60 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 core SQLite code
9da70 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 .** should call
9da80 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 sqlite3MemoryAla
9da90 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 rm..*/.SQLITE_AP
9daa0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 I int sqlite3_me
9dab0 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f mory_alarm(. vo
9dac0 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 id(*xCallback)(v
9dad0 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 oid *pArg, sqlit
9dae0 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e e3_int64 used,in
9daf0 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 t N),. void *pA
9db00 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e rg,. sqlite3_in
9db10 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 t64 iThreshold.)
9db20 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
9db30 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43 e3MemoryAlarm(xC
9db40 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69 allback, pArg, i
9db50 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65 Threshold);.}.#e
9db60 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 ndif../*.** Trig
9db70 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a ger the alarm .*
9db80 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 /.static void sq
9db90 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d lite3MallocAlarm
9dba0 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 (int nByte){. v
9dbb0 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 oid (*xCallback)
9dbc0 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 (void*,sqlite3_i
9dbd0 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c nt64,int);. sql
9dbe0 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 ite3_int64 nowUs
9dbf0 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 ed;. void *pArg
9dc00 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 ;. if( mem0.ala
9dc10 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 7c 7c rmCallback==0 ||
9dc20 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 mem0.alarmBusy
9dc30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d ) return;. mem
9dc40 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 31 3b 0.alarmBusy = 1;
9dc50 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d . xCallback = m
9dc60 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 em0.alarmCallbac
9dc70 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 k;. nowUsed = s
9dc80 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 qlite3StatusValu
9dc90 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f e(SQLITE_STATUS_
9dca0 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 MEMORY_USED);.
9dcb0 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 pArg = mem0.alar
9dcc0 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f mArg;. sqlite3_
9dcd0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9dce0 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c .mutex);. xCall
9dcf0 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73 back(pArg, nowUs
9dd00 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71 ed, nByte);. sq
9dd10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
9dd20 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 r(mem0.mutex);.
9dd30 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 mem0.alarmBusy
9dd40 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f = 0;.}../*.** Do
9dd50 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
9dd60 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74 69 73 tion with statis
9dd70 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e tics and alarms.
9dd80 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20 Assume the.**
9dd90 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 lock is already
9dda0 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 held..*/.static
9ddb0 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c int mallocWithAl
9ddc0 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20 arm(int n, void
9ddd0 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75 **pp){. int nFu
9dde0 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 ll;. void *p;.
9ddf0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
9de00 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30 _mutex_held(mem0
9de10 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 46 75 .mutex) );. nFu
9de20 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 ll = sqlite3Glob
9de30 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e alConfig.m.xRoun
9de40 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 dup(n);. sqlite
9de50 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 3StatusSet(SQLIT
9de60 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f E_STATUS_MALLOC_
9de70 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20 SIZE, n);. if(
9de80 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 mem0.alarmCallba
9de90 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 ck!=0 ){. int
9dea0 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33 nUsed = sqlite3
9deb0 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 StatusValue(SQLI
9dec0 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 TE_STATUS_MEMORY
9ded0 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66 28 20 _USED);. if(
9dee0 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d nUsed+nFull >= m
9def0 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f em0.alarmThresho
9df00 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ld ){. sqli
9df10 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e te3MallocAlarm(n
9df20 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Full);. }. }
9df30 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c . p = sqlite3Gl
9df40 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 obalConfig.m.xMa
9df50 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69 lloc(nFull);. i
9df60 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e f( p==0 && mem0.
9df70 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b alarmCallback ){
9df80 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c . sqlite3Mall
9df90 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a ocAlarm(nFull);.
9dfa0 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 p = sqlite3G
9dfb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d lobalConfig.m.xM
9dfc0 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 alloc(nFull);.
9dfd0 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 }. if( p ){.
9dfe0 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 nFull = sqlite3
9dff0 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 MallocSize(p);.
9e000 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9e010 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
9e020 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e S_MEMORY_USED, n
9e030 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 Full);. }. *pp
9e040 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e = p;. return n
9e050 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 Full;.}../*.** A
9e060 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20 llocate memory.
9e070 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
9e080 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61 like sqlite3_ma
9e090 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 20 74 68 lloc() except th
9e0a0 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73 at it.** assumes
9e0b0 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 the memory subs
9e0c0 79 73 74 65 6d 20 68 61 73 20 61 6c 72 65 61 64 ystem has alread
9e0d0 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a y been initializ
9e0e0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
9e0f0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
9e100 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 te3Malloc(int n)
9e110 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 {. void *p;. i
9e120 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 70 f( n<=0 ){. p
9e130 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 0;. }else if
9e140 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9e150 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
9e160 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
9e170 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
9e180 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f utex);. mallo
9e190 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 cWithAlarm(n, &p
9e1a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
9e1b0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9e1c0 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b mutex);. }else{
9e1d0 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
9e1e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9e1f0 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 Malloc(n);. }.
9e200 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
9e210 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
9e220 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
9e230 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 llocation is for
9e240 20 75 73 65 20 62 79 20 74 68 65 20 61 70 70 6c use by the appl
9e250 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 ication..** Firs
9e260 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 t make sure the
9e270 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d memory subsystem
9e280 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c is initialized,
9e290 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 then do the.**
9e2a0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 allocation..*/.S
9e2b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
9e2c0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 sqlite3_malloc(i
9e2d0 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 nt n){.#ifndef S
9e2e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
9e2f0 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 NIT. if( sqlite
9e300 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 3_initialize() )
9e310 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
9e320 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 f. return sqlit
9e330 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a e3Malloc(n);.}..
9e340 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61 /*.** Each threa
9e350 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 d may only have
9e360 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e a single outstan
9e370 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 ding allocation
9e380 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 from.** xScratch
9e390 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 Malloc(). We ve
9e3a0 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 rify this constr
9e3b0 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67 aint in the sing
9e3c0 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 le-threaded.** c
9e3d0 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 20 73 ase by setting s
9e3e0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 cratchAllocOut t
9e3f0 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f o 1 when an allo
9e400 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 cation.** is out
9e410 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e standing clearin
9e420 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c g it when the al
9e430 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 location is free
9e440 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 d..*/.#if SQLITE
9e450 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 _THREADSAFE==0 &
9e460 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 & !defined(NDEBU
9e470 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 G).static int sc
9e480 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 ratchAllocOut =
9e490 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 0;.#endif.../*.*
9e4a0 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 * Allocate memor
9e4b0 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 y that is to be
9e4c0 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 used and release
9e4d0 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a d right away..**
9e4e0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
9e4f0 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f similar to allo
9e500 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 74 20 ca() in that it
9e510 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a is not intended.
9e520 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e ** for situation
9e530 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f s where the memo
9e540 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64 ry might be held
9e550 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 long-term. Thi
9e560 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 s.** routine is
9e570 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20 intended to get
9e580 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 memory to old la
9e590 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61 rge transient da
9e5a0 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 ta.** structures
9e5b0 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 that would not
9e5c0 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 normally fit on
9e5d0 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a the stack of an.
9e5e0 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63 ** embedded proc
9e5f0 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 essor..*/.SQLITE
9e600 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
9e610 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c qlite3ScratchMal
9e620 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f loc(int n){. vo
9e630 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 id *p;. assert(
9e640 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c n>0 );..#if SQL
9e650 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
9e660 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 0 && !defined(ND
9e670 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 EBUG). /* Verif
9e680 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 y that no more t
9e690 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 han one scratch
9e6a0 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 allocation per t
9e6b0 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 hread. ** is ou
9e6c0 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 tstanding at one
9e6d0 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 time. (This is
9e6e0 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e only checked in
9e6f0 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 the. ** single
9e700 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73 -threaded case s
9e710 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e ince checking in
9e720 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 the multi-threa
9e730 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f ded case. ** wo
9e740 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 uld be much more
9e750 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a complicated.) *
9e760 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 72 61 /. assert( scra
9e770 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 tchAllocOut==0 )
9e780 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 ;.#endif.. if(
9e790 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9e7a0 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 fig.szScratch<n
9e7b0 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61 ){. goto scra
9e7c0 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 tch_overflow;.
9e7d0 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c }else{ . sql
9e7e0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9e7f0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9e800 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 if( mem0.nScra
9e810 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 tchFree==0 ){.
9e820 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9e830 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
9e840 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ex);. goto
9e850 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 scratch_overflow
9e860 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
9e870 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
9e880 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 i = mem0.aScratc
9e890 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 hFree[--mem0.nSc
9e8a0 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 ratchFree];.
9e8b0 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c i *= sqlite3Gl
9e8c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9e8d0 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 atch;. sqli
9e8e0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
9e8f0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
9e900 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 CH_USED, 1);.
9e910 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9e920 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Set(SQLITE_STATU
9e930 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 S_SCRATCH_SIZE,
9e940 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 n);. sqlite
9e950 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9e960 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
9e970 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 p = (void*)&((c
9e980 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 har*)sqlite3Glob
9e990 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9e9a0 68 29 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d h)[i];. }. }
9e9b0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
9e9c0 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 ADSAFE==0 && !de
9e9d0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 fined(NDEBUG).
9e9e0 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 scratchAllocOut
9e9f0 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a = p!=0;.#endif..
9ea00 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 return p;..scr
9ea10 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 atch_overflow:.
9ea20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9ea30 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 alConfig.bMemsta
9ea40 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
9ea50 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
9ea60 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 0.mutex);. sq
9ea70 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 lite3StatusSet(S
9ea80 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 QLITE_STATUS_SCR
9ea90 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 ATCH_SIZE, n);.
9eaa0 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 n = mallocWit
9eab0 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 hAlarm(n, &p);.
9eac0 20 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74 if( p ) sqlit
9ead0 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 e3StatusAdd(SQLI
9eae0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
9eaf0 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a H_OVERFLOW, n);.
9eb00 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9eb10 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
9eb20 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ex);. }else{.
9eb30 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f p = sqlite3Glo
9eb40 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c balConfig.m.xMal
9eb50 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 loc(n);. }.#if
9eb60 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
9eb70 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 E==0 && !defined
9eb80 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 (NDEBUG). scrat
9eb90 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d chAllocOut = p!=
9eba0 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 0;.#endif. retu
9ebb0 72 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 rn p; .}.SQLI
9ebc0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
9ebd0 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 sqlite3ScratchFr
9ebe0 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 ee(void *p){. i
9ebf0 66 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c f( p ){..#if SQL
9ec00 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
9ec10 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 0 && !defined(ND
9ec20 45 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 EBUG). /* Ver
9ec30 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 ify that no more
9ec40 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 than one scratc
9ec50 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 h allocation per
9ec60 20 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 thread. ** i
9ec70 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 s outstanding at
9ec80 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 one time. (Thi
9ec90 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 s is only checke
9eca0 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 d in the. **
9ecb0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 single-threaded
9ecc0 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b case since check
9ecd0 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 ing in the multi
9ece0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20 -threaded case.
9ecf0 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d ** would be m
9ed00 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 uch more complic
9ed10 61 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 ated.) */. as
9ed20 73 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c sert( scratchAll
9ed30 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 ocOut==1 );.
9ed40 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 scratchAllocOut
9ed50 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 = 0;.#endif..
9ed60 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9ed70 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9ed80 68 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 h==0.
9ed90 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 || p<sqlite3Glob
9eda0 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9edb0 68 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 h. ||
9edc0 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 p>=(void*)mem0.a
9edd0 53 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a 20 ScratchFree ){.
9ede0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
9edf0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
9ee00 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 20 mstat ){.
9ee10 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c int iSize = sql
9ee20 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
9ee30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
9ee40 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
9ee50 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9ee60 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
9ee70 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
9ee80 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 US_SCRATCH_OVERF
9ee90 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 LOW, -iSize);.
9eea0 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 sqlite3Sta
9eeb0 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
9eec0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
9eed0 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 , -iSize);.
9eee0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
9eef0 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 Config.m.xFree(p
9ef00 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
9ef10 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
9ef20 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9ef30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
9ef40 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9ef50 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b nfig.m.xFree(p);
9ef60 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
9ef70 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b se{. int i;
9ef80 0a 20 20 20 20 20 20 69 20 3d 20 28 75 38 20 2a . i = (u8 *
9ef90 29 70 20 2d 20 28 75 38 20 2a 29 73 71 6c 69 74 )p - (u8 *)sqlit
9efa0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9efb0 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 69 Scratch;. i
9efc0 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 /= sqlite3Globa
9efd0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
9efe0 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 h;. assert(
9eff0 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74 i>=0 && i<sqlit
9f000 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
9f010 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 Scratch );.
9f020 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9f030 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9f040 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
9f050 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 mem0.nScratchFre
9f060 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c e<(u32)sqlite3Gl
9f070 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
9f080 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d tch );. mem
9f090 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d 0.aScratchFree[m
9f0a0 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 em0.nScratchFree
9f0b0 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73 ++] = i;. s
9f0c0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
9f0d0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
9f0e0 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b RATCH_USED, -1);
9f0f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
9f100 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9f110 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 mutex);. }.
9f120 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 }.}../*.** Alloc
9f130 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 62 65 ate memory to be
9f140 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 67 used by the pag
9f150 65 20 63 61 63 68 65 2e 20 20 4d 61 6b 65 20 75 e cache. Make u
9f160 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6d 65 6d se of the.** mem
9f170 6f 72 79 20 62 75 66 66 65 72 20 70 72 6f 76 69 ory buffer provi
9f180 64 65 64 20 62 79 20 53 51 4c 49 54 45 5f 43 4f ded by SQLITE_CO
9f190 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 69 NFIG_PAGECACHE i
9f1a0 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a f there is one.*
9f1b0 2a 20 61 6e 64 20 74 68 61 74 20 6d 65 6d 6f 72 * and that memor
9f1c0 79 20 69 73 20 6f 66 20 74 68 65 20 72 69 67 68 y is of the righ
9f1d0 74 20 73 69 7a 65 20 61 6e 64 20 69 73 20 6e 6f t size and is no
9f1e0 74 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 t completely.**
9f1f0 63 6f 6e 73 75 6d 65 64 2e 20 20 4f 74 68 65 72 consumed. Other
9f200 77 69 73 65 2c 20 66 61 69 6c 6f 76 65 72 20 74 wise, failover t
9f210 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 o sqlite3Malloc(
9f220 29 2e 0a 2a 2f 0a 23 69 66 20 30 0a 53 51 4c 49 )..*/.#if 0.SQLI
9f230 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
9f240 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c *sqlite3PageMall
9f250 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 oc(int n){. voi
9f260 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 d *p;. assert(
9f270 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 n>0 );. assert(
9f280 20 28 6e 20 26 20 28 6e 2d 31 29 29 3d 3d 30 20 (n & (n-1))==0
9f290 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d );. assert( n>=
9f2a0 35 31 32 20 26 26 20 6e 3c 3d 33 32 37 36 38 20 512 && n<=32768
9f2b0 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 );.. if( sqlite
9f2c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 3GlobalConfig.sz
9f2d0 50 61 67 65 3c 6e 20 29 7b 0a 20 20 20 20 67 6f Page<n ){. go
9f2e0 74 6f 20 70 61 67 65 5f 6f 76 65 72 66 6c 6f 77 to page_overflow
9f2f0 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 ;. }else{ .
9f300 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9f310 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9f320 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 30 2e 6e ;. if( mem0.n
9f330 50 61 67 65 46 72 65 65 3d 3d 30 20 29 7b 0a 20 PageFree==0 ){.
9f340 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
9f350 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
9f360 74 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f tex);. goto
9f370 20 70 61 67 65 5f 6f 76 65 72 66 6c 6f 77 3b 0a page_overflow;.
9f380 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9f390 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20 int i;. i
9f3a0 3d 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 = mem0.aPageFree
9f3b0 5b 2d 2d 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65 [--mem0.nPageFre
9f3c0 65 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 e];. sqlite
9f3d0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9f3e0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
9f3f0 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f i *= sqlite3Glo
9f400 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 balConfig.szPage
9f410 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 ;. sqlite3S
9f420 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f tatusSet(SQLITE_
9f430 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
9f440 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 20 _SIZE, n);.
9f450 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
9f460 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
9f470 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 PAGECACHE_USED,
9f480 31 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 28 76 1);. p = (v
9f490 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 73 71 oid*)&((char*)sq
9f4a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9f4b0 67 2e 70 50 61 67 65 29 5b 69 5d 3b 0a 20 20 20 g.pPage)[i];.
9f4c0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
9f4d0 70 3b 0a 0a 70 61 67 65 5f 6f 76 65 72 66 6c 6f p;..page_overflo
9f4e0 77 3a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 w:. if( sqlite3
9f4f0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
9f500 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c mstat ){. sql
9f510 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9f520 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9f530 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 sqlite3StatusS
9f540 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 et(SQLITE_STATUS
9f550 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 2c _PAGECACHE_SIZE,
9f560 20 6e 29 3b 0a 20 20 20 20 6e 20 3d 20 6d 61 6c n);. n = mal
9f570 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 locWithAlarm(n,
9f580 26 70 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 &p);. if( p )
9f590 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
9f5a0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
9f5b0 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c PAGECACHE_OVERFL
9f5c0 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 OW, n);. sqli
9f5d0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9f5e0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d mem0.mutex);. }
9f5f0 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73 71 else{. p = sq
9f600 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9f610 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a g.m.xMalloc(n);.
9f620 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 }. return p;
9f630 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 .}.SQLITE_PRI
9f640 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
9f650 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 2a 3PageFree(void *
9f660 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 p){. if( p ){.
9f670 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c if( sqlite3Gl
9f680 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 obalConfig.pPage
9f690 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c ==0. |
9f6a0 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 | p<sqlite3Globa
9f6b0 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 0a 20 20 lConfig.pPage.
9f6c0 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28 || p>=(
9f6d0 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 50 61 67 65 void*)mem0.aPage
9f6e0 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a Free ){. /*
9f6f0 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 In this case, t
9f700 68 65 20 70 61 67 65 20 61 6c 6c 6f 63 61 74 69 he page allocati
9f710 6f 6e 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 on was obtained
9f720 66 72 6f 6d 20 61 20 72 65 67 75 6c 61 72 20 0a from a regular .
9f730 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f ** call to
9f740 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
9f750 68 6f 64 73 2e 78 4d 61 6c 6c 6f 63 28 29 20 28 hods.xMalloc() (
9f760 61 20 70 61 67 65 2d 63 61 63 68 65 2d 6d 65 6d a page-cache-mem
9f770 6f 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 22 6f ory . ** "o
9f780 76 65 72 66 6c 6f 77 22 29 2e 20 46 72 65 65 20 verflow"). Free
9f790 74 68 65 20 62 6c 6f 63 6b 20 77 69 74 68 20 73 the block with s
9f7a0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
9f7b0 64 73 2e 78 46 72 65 65 28 29 2e 0a 20 20 20 20 ds.xFree()..
9f7c0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 */. if( s
9f7d0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9f7e0 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 ig.bMemstat ){.
9f7f0 20 20 20 20 20 20 20 69 6e 74 20 69 53 69 7a 65 int iSize
9f800 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
9f810 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 Size(p);.
9f820 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9f830 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9f840 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
9f850 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
9f860 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 E_STATUS_PAGECAC
9f870 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53 HE_OVERFLOW, -iS
9f880 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 ize);. sq
9f890 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
9f8a0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d QLITE_STATUS_MEM
9f8b0 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53 69 7a 65 ORY_USED, -iSize
9f8c0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
9f8d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9f8e0 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 .xFree(p);.
9f8f0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9f900 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 _leave(mem0.mute
9f910 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b x);. }else{
9f920 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
9f930 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9f940 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 7d Free(p);. }
9f950 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
9f960 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 61 6c /* The page al
9f970 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 61 6c 6c location was all
9f980 6f 63 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20 ocated from the
9f990 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9f9a0 66 69 67 2e 70 50 61 67 65 0a 20 20 20 20 20 20 fig.pPage.
9f9b0 2a 2a 20 62 75 66 66 65 72 2e 20 49 6e 20 74 68 ** buffer. In th
9f9c0 69 73 20 63 61 73 65 20 61 6c 6c 20 74 68 61 74 is case all that
9f9d0 20 69 73 20 61 64 64 20 74 68 65 20 69 6e 64 65 is add the inde
9f9e0 78 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e x of the page in
9f9f0 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 71 . ** the sq
9fa00 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9fa10 67 2e 70 50 61 67 65 20 61 72 72 61 79 20 74 6f g.pPage array to
9fa20 20 74 68 65 20 73 65 74 20 6f 66 20 66 72 65 65 the set of free
9fa30 20 69 6e 64 65 78 65 73 20 73 74 6f 72 65 64 0a indexes stored.
9fa40 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 ** in the
9fa50 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 5d mem0.aPageFree[]
9fa60 20 61 72 72 61 79 2e 0a 20 20 20 20 20 20 2a 2f array.. */
9fa70 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
9fa80 20 20 20 20 69 20 3d 20 28 75 38 20 2a 29 70 20 i = (u8 *)p
9fa90 2d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 47 - (u8 *)sqlite3G
9faa0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 lobalConfig.pPag
9fab0 65 3b 0a 20 20 20 20 20 20 69 20 2f 3d 20 73 71 e;. i /= sq
9fac0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9fad0 67 2e 73 7a 50 61 67 65 3b 0a 20 20 20 20 20 20 g.szPage;.
9fae0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
9faf0 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 i<sqlite3GlobalC
9fb00 6f 6e 66 69 67 2e 6e 50 61 67 65 20 29 3b 0a 20 onfig.nPage );.
9fb10 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
9fb20 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
9fb30 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 tex);. asse
9fb40 72 74 28 20 6d 65 6d 30 2e 6e 50 61 67 65 46 72 rt( mem0.nPageFr
9fb50 65 65 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ee<sqlite3Global
9fb60 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 29 3b 0a Config.nPage );.
9fb70 20 20 20 20 20 20 6d 65 6d 30 2e 61 50 61 67 65 mem0.aPage
9fb80 46 72 65 65 5b 6d 65 6d 30 2e 6e 50 61 67 65 46 Free[mem0.nPageF
9fb90 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 ree++] = i;.
9fba0 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 sqlite3StatusA
9fbb0 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
9fbc0 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c _PAGECACHE_USED,
9fbd0 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 -1);. sqli
9fbe0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9fbf0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 23 69 66 mem0.mutex);.#if
9fc00 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
9fc10 29 20 26 26 20 30 0a 20 20 20 20 20 20 2f 2a 20 ) && 0. /*
9fc20 41 73 73 65 72 74 20 74 68 61 74 20 61 20 64 75 Assert that a du
9fc30 70 6c 69 63 61 74 65 20 77 61 73 20 6e 6f 74 20 plicate was not
9fc40 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20 69 6e just inserted in
9fc50 74 6f 20 61 50 61 67 65 46 72 65 65 5b 5d 2e 20 to aPageFree[].
9fc60 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 */. for(i=0
9fc70 3b 20 69 3c 6d 65 6d 30 2e 6e 50 61 67 65 46 72 ; i<mem0.nPageFr
9fc80 65 65 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 ee-1; i++){.
9fc90 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 30 assert( mem0
9fca0 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 21 3d 6d .aPageFree[i]!=m
9fcb0 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 6d 65 em0.aPageFree[me
9fcc0 6d 30 2e 6e 50 61 67 65 46 72 65 65 2d 31 5d 20 m0.nPageFree-1]
9fcd0 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 );. }.#endi
9fce0 66 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 f. }. }.}.#e
9fcf0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 ndif../*.** TRUE
9fd00 20 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 if p is a looka
9fd10 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f side memory allo
9fd20 63 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a cation from db.*
9fd30 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
9fd40 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a _OMIT_LOOKASIDE.
9fd50 73 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f static int isLoo
9fd60 6b 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a kaside(sqlite3 *
9fd70 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 db, void *p){.
9fd80 72 65 74 75 72 6e 20 64 62 20 26 26 20 70 20 26 return db && p &
9fd90 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 & p>=db->lookasi
9fda0 64 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c 64 de.pStart && p<d
9fdb0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e b->lookaside.pEn
9fdc0 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 d;.}.#else.#defi
9fdd0 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41 ne isLookaside(A
9fde0 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a ,B) 0.#endif../*
9fdf0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
9fe00 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 ize of a memory
9fe10 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 allocation previ
9fe20 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 ously obtained f
9fe30 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61 rom.** sqlite3Ma
9fe40 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 lloc() or sqlite
9fe50 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 3_malloc()..*/.S
9fe60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
9fe70 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 t sqlite3MallocS
9fe80 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 ize(void *p){.
9fe90 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
9fea0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 obalConfig.m.xSi
9feb0 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f ze(p);.}.SQLITE_
9fec0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
9fed0 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
9fee0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 sqlite3 *db, voi
9fef0 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 69 73 4c d *p){. if( isL
9ff00 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 ookaside(db, p)
9ff10 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 62 ){. return db
9ff20 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 3b 0a ->lookaside.sz;.
9ff30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
9ff40 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 urn sqlite3Globa
9ff50 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 28 lConfig.m.xSize(
9ff60 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
9ff70 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 70 72 65 Free memory pre
9ff80 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 viously obtained
9ff90 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 6c from sqlite3Mal
9ffa0 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 loc()..*/.SQLITE
9ffb0 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
9ffc0 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 3_free(void *p){
9ffd0 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
9ffe0 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 turn;. if( sqli
9fff0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
a0000 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 bMemstat ){.
a0010 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
a0020 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ter(mem0.mutex);
a0030 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 . sqlite3Stat
a0040 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
a0050 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c TUS_MEMORY_USED,
a0060 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 -sqlite3MallocS
a0070 69 7a 65 28 70 29 29 3b 0a 20 20 20 20 73 71 6c ize(p));. sql
a0080 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
a0090 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 .m.xFree(p);.
a00a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
a00b0 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 eave(mem0.mutex)
a00c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
a00d0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
a00e0 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 ig.m.xFree(p);.
a00f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 }.}../*.** Free
a0100 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 69 67 memory that mig
a0110 68 74 20 62 65 20 61 73 73 6f 63 69 61 74 65 64 ht be associated
a0120 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c with a particul
a0130 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 ar database.** c
a0140 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 onnection..*/.SQ
a0150 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a0160 64 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 d sqlite3DbFree(
a0170 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 sqlite3 *db, voi
a0180 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 69 73 4c d *p){. if( isL
a0190 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 ookaside(db, p)
a01a0 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 ){. Lookaside
a01b0 53 6c 6f 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f Slot *pBuf = (Lo
a01c0 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a okasideSlot*)p;.
a01d0 20 20 20 20 70 42 75 66 2d 3e 70 4e 65 78 74 20 pBuf->pNext
a01e0 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e = db->lookaside.
a01f0 70 46 72 65 65 3b 0a 20 20 20 20 64 62 2d 3e 6c pFree;. db->l
a0200 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d ookaside.pFree =
a0210 20 70 42 75 66 3b 0a 20 20 20 20 64 62 2d 3e 6c pBuf;. db->l
a0220 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b ookaside.nOut--;
a0230 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
a0240 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
a0250 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
a0260 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 ge the size of a
a0270 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 n existing memor
a0280 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a y allocation.*/.
a0290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
a02a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c oid *sqlite3Real
a02b0 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 loc(void *pOld,
a02c0 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 int nBytes){. i
a02d0 6e 74 20 6e 4f 6c 64 2c 20 6e 4e 65 77 3b 0a 20 nt nOld, nNew;.
a02e0 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 69 void *pNew;. i
a02f0 66 28 20 70 4f 6c 64 3d 3d 30 20 29 7b 0a 20 20 f( pOld==0 ){.
a0300 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
a0310 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a Malloc(nBytes);.
a0320 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 }. if( nBytes
a0330 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 <=0 ){. sqlit
a0340 65 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20 e3_free(pOld);.
a0350 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
a0360 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c 69 74 65 . nOld = sqlite
a0370 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4f 6c 64 3MallocSize(pOld
a0380 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
a0390 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
a03a0 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c mstat ){. sql
a03b0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
a03c0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
a03d0 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 sqlite3StatusS
a03e0 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 et(SQLITE_STATUS
a03f0 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 _MALLOC_SIZE, nB
a0400 79 74 65 73 29 3b 0a 20 20 20 20 6e 4e 65 77 20 ytes);. nNew
a0410 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
a0420 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 onfig.m.xRoundup
a0430 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66 (nBytes);. if
a0440 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a ( nOld==nNew ){.
a0450 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 4f 6c pNew = pOl
a0460 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 d;. }else{.
a0470 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
a0480 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 tatusValue(SQLIT
a0490 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
a04a0 55 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 USED)+nNew-nOld
a04b0 3e 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 >= .
a04c0 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 mem0.alarmThresh
a04d0 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 old ){. s
a04e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 qlite3MallocAlar
a04f0 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 m(nNew-nOld);.
a0500 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 }. pNew
a0510 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
a0520 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f Config.m.xReallo
a0530 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 c(pOld, nNew);.
a0540 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 if( pNew==0
a0550 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 && mem0.alarmCa
a0560 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 llback ){.
a0570 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 sqlite3MallocA
a0580 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20 20 larm(nBytes);.
a0590 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c pNew = sql
a05a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
a05b0 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 .m.xRealloc(pOld
a05c0 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 7d , nNew);. }
a05d0 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 . if( pNew
a05e0 29 7b 0a 20 20 20 20 20 20 20 20 6e 4e 65 77 20 ){. nNew
a05f0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 = sqlite3MallocS
a0600 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20 ize(pNew);.
a0610 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
a0620 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
a0630 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e S_MEMORY_USED, n
a0640 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 20 New-nOld);.
a0650 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c }. }. sql
a0660 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
a0670 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
a0680 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 }else{. pNew
a0690 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
a06a0 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 onfig.m.xRealloc
a06b0 28 70 4f 6c 64 2c 20 6e 42 79 74 65 73 29 3b 0a (pOld, nBytes);.
a06c0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 }. return pNe
a06d0 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 w;.}../*.** The
a06e0 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 public interface
a06f0 20 74 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c to sqlite3Reall
a0700 6f 63 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 oc. Make sure t
a0710 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a hat the memory.*
a0720 2a 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 * subsystem is i
a0730 6e 69 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72 nitialized prior
a0740 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c to invoking sql
a0750 69 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 iteRealloc..*/.S
a0760 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
a0770 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
a0780 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 void *pOld, int
a0790 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 n){.#ifndef SQLI
a07a0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
a07b0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 . if( sqlite3_i
a07c0 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 nitialize() ) re
a07d0 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 turn 0;.#endif.
a07e0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 return sqlite3R
a07f0 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b ealloc(pOld, n);
a0800 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 .}.../*.** Alloc
a0810 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d ate and zero mem
a0820 6f 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f ory..*/ .SQLITE_
a0830 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
a0840 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 lite3MallocZero(
a0850 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a int n){. void *
a0860 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f p = sqlite3Mallo
a0870 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b c(n);. if( p ){
a0880 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 . memset(p, 0
a0890 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , n);. }. retu
a08a0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 rn p;.}../*.** A
a08b0 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f llocate and zero
a08c0 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 memory. If the
a08d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
a08e0 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d s, make.** the m
a08f0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 allocFailed flag
a0900 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 in the connecti
a0910 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 on pointer..*/.S
a0920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
a0930 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c id *sqlite3DbMal
a0940 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 locZero(sqlite3
a0950 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 *db, int n){. v
a0960 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 oid *p = sqlite3
a0970 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
a0980 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 n);. if( p ){.
a0990 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 memset(p, 0,
a09a0 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e n);. }. return
a09b0 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c p;.}../*.** All
a09c0 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d ocate and zero m
a09d0 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 emory. If the a
a09e0 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
a09f0 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c make.** the mal
a0a00 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 locFailed flag i
a0a10 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e n the connection
a0a20 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
a0a30 49 66 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d If db!=0 and db-
a0a40 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 >mallocFailed is
a0a50 20 74 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e true (indicatin
a0a60 67 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 g a prior malloc
a0a70 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 .** failure on t
a0a80 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
a0a90 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 connection) the
a0aa0 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 n always return
a0ab0 30 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 0..** Hence for
a0ac0 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 a particular dat
a0ad0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
a0ae0 2c 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 , once malloc st
a0af0 61 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c arts.** failing,
a0b00 20 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 it fails consis
a0b10 74 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c tently until mal
a0b20 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 72 65 73 locFailed is res
a0b30 65 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 et..** This is a
a0b40 6e 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75 n important assu
a0b50 6d 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61 mption. There a
a0b60 72 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69 re many places i
a0b70 6e 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68 n the.** code th
a0b80 61 74 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b at do things lik
a0b90 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 e this:.**.**
a0ba0 20 20 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 int *a = (
a0bb0 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 int*)sqlite3DbMa
a0bc0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29 llocRaw(db, 100)
a0bd0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 ;.** int
a0be0 20 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 *b = (int*)sqli
a0bf0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
a0c00 62 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20 b, 200);.**
a0c10 20 20 20 20 69 66 28 20 62 20 29 20 61 5b 31 30 if( b ) a[10
a0c20 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 ] = 9;.**.** In
a0c30 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 other words, if
a0c40 61 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c a subsequent mal
a0c50 6c 6f 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f loc (ex: "b") wo
a0c60 72 6b 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 rked, it is assu
a0c70 6d 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 med.** that all
a0c80 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65 prior mallocs (e
a0c90 78 3a 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74 x: "a") worked t
a0ca0 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 oo..*/.SQLITE_PR
a0cb0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
a0cc0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 te3DbMallocRaw(s
a0cd0 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
a0ce0 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 23 n){. void *p;.#
a0cf0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
a0d00 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 69 IT_LOOKASIDE. i
a0d10 66 28 20 64 62 20 29 7b 0a 20 20 20 20 4c 6f 6f f( db ){. Loo
a0d20 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 kasideSlot *pBuf
a0d30 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 ;. if( db->ma
a0d40 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
a0d50 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
a0d60 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e }. if( db->
a0d70 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c lookaside.bEnabl
a0d80 65 64 20 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f ed && n<=db->loo
a0d90 6b 61 73 69 64 65 2e 73 7a 0a 20 20 20 20 20 20 kaside.sz.
a0da0 20 20 20 26 26 20 28 70 42 75 66 20 3d 20 64 62 && (pBuf = db
a0db0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 ->lookaside.pFre
a0dc0 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 e)!=0 ){. d
a0dd0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 b->lookaside.pFr
a0de0 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78 74 ee = pBuf->pNext
a0df0 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b ;. db->look
a0e00 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 aside.nOut++;.
a0e10 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b if( db->look
a0e20 61 73 69 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e 6c aside.nOut>db->l
a0e30 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 29 ookaside.mxOut )
a0e40 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f {. db->lo
a0e50 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 okaside.mxOut =
a0e60 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f db->lookaside.nO
a0e70 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ut;. }.
a0e80 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
a0e90 70 42 75 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a pBuf;. }. }.
a0ea0 23 65 6c 73 65 0a 20 20 69 66 28 20 64 62 20 26 #else. if( db &
a0eb0 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c & db->mallocFail
a0ec0 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ed ){. return
a0ed0 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 0;. }.#endif.
a0ee0 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c p = sqlite3Mall
a0ef0 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 21 70 20 oc(n);. if( !p
a0f00 26 26 20 64 62 20 29 7b 0a 20 20 20 20 64 62 2d && db ){. db-
a0f10 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
a0f20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
a0f30 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 p;.}../*.** Resi
a0f40 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 ze the block of
a0f50 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 memory pointed t
a0f60 6f 20 62 79 20 70 20 74 6f 20 6e 20 62 79 74 65 o by p to n byte
a0f70 73 2e 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 s. If the.** res
a0f80 69 7a 65 20 66 61 69 6c 73 2c 20 73 65 74 20 74 ize fails, set t
a0f90 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
a0fa0 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e flag in the conn
a0fb0 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a ection object..*
a0fc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
a0fd0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 void *sqlite3Db
a0fe0 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 Realloc(sqlite3
a0ff0 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e *db, void *p, in
a1000 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e t n){. void *pN
a1010 65 77 20 3d 20 30 3b 0a 20 20 69 66 28 20 64 62 ew = 0;. if( db
a1020 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d ->mallocFailed==
a1030 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d 0 ){. if( p==
a1040 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
a1050 6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f n sqlite3DbMallo
a1060 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 cRaw(db, n);.
a1070 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4c 6f 6f }. if( isLoo
a1080 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b kaside(db, p) ){
a1090 0a 20 20 20 20 20 20 69 66 28 20 6e 3c 3d 64 62 . if( n<=db
a10a0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29 ->lookaside.sz )
a10b0 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
a10c0 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p;. }.
a10d0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
a10e0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
a10f0 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e n);. if( pN
a1100 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 ew ){. me
a1110 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 64 62 mcpy(pNew, p, db
a1120 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b ->lookaside.sz);
a1130 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
a1140 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 DbFree(db, p);.
a1150 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
a1160 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 {. pNew = s
a1170 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 qlite3_realloc(p
a1180 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , n);. if(
a1190 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 !pNew ){.
a11a0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
a11b0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 d = 1;. }.
a11c0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
a11d0 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
a11e0 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61 6c Attempt to real
a11f0 6c 6f 63 61 74 65 20 70 2e 20 20 49 66 20 74 68 locate p. If th
a1200 65 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 e reallocation f
a1210 61 69 6c 73 2c 20 74 68 65 6e 20 66 72 65 65 20 ails, then free
a1220 70 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74 68 65 p.** and set the
a1230 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c mallocFailed fl
a1240 61 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ag in the databa
a1250 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a se connection..*
a1260 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
a1270 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 void *sqlite3Db
a1280 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 ReallocOrFree(sq
a1290 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 lite3 *db, void
a12a0 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f *p, int n){. vo
a12b0 69 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77 id *pNew;. pNew
a12c0 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
a12d0 6c 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b 0a 20 loc(db, p, n);.
a12e0 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 if( !pNew ){.
a12f0 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
a1300 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 72 65 db, p);. }. re
a1310 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a turn pNew;.}../*
a1320 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 .** Make a copy
a1330 6f 66 20 61 20 73 74 72 69 6e 67 20 69 6e 20 6d of a string in m
a1340 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
a1350 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 rom sqliteMalloc
a1360 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 66 75 (). These .** fu
a1370 6e 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73 71 6c nctions call sql
a1380 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29 20 ite3MallocRaw()
a1390 64 69 72 65 63 74 6c 79 20 69 6e 73 74 65 61 64 directly instead
a13a0 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 of sqliteMalloc
a13b0 28 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 62 (). This.** is b
a13c0 65 63 61 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f ecause when memo
a13d0 72 79 20 64 65 62 75 67 67 69 6e 67 20 69 73 20 ry debugging is
a13e0 74 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65 turned on, these
a13f0 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 two functions a
a1400 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69 re .** called vi
a1410 61 20 6d 61 63 72 6f 73 20 74 68 61 74 20 72 65 a macros that re
a1420 63 6f 72 64 20 74 68 65 20 63 75 72 72 65 6e 74 cord the current
a1430 20 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e file and line n
a1440 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 umber in the.**
a1450 54 68 72 65 61 64 44 61 74 61 20 73 74 72 75 63 ThreadData struc
a1460 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ture..*/.SQLITE_
a1470 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
a1480 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 lite3DbStrDup(sq
a1490 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 lite3 *db, const
a14a0 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 char *z){. cha
a14b0 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a 65 5f r *zNew;. size_
a14c0 74 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 t n;. if( z==0
a14d0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
a14e0 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 74 72 6c 65 . }. n = strle
a14f0 6e 28 7a 29 2b 31 3b 0a 20 20 61 73 73 65 72 74 n(z)+1;. assert
a1500 28 20 28 6e 26 30 78 37 66 66 66 66 66 66 66 29 ( (n&0x7fffffff)
a1510 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 ==n );. zNew =
a1520 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
a1530 61 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a aw(db, (int)n);.
a1540 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 if( zNew ){.
a1550 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a memcpy(zNew, z
a1560 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , n);. }. retu
a1570 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 rn zNew;.}.SQLIT
a1580 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
a1590 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 sqlite3DbStrNDup
a15a0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f (sqlite3 *db, co
a15b0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
a15c0 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 n){. char *zNe
a15d0 77 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b w;. if( z==0 ){
a15e0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
a15f0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 }. assert( (n&
a1600 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 0x7fffffff)==n )
a1610 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 ;. zNew = sqlit
a1620 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
a1630 2c 20 6e 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e , n+1);. if( zN
a1640 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 ew ){. memcpy
a1650 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 (zNew, z, n);.
a1660 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 zNew[n] = 0;.
a1670 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 }. return zNew
a1680 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 ;.}../*.** Creat
a1690 65 20 61 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 e a string from
a16a0 74 68 65 20 7a 46 72 6f 6d 61 74 20 61 72 67 75 the zFromat argu
a16b0 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 76 61 5f ment and the va_
a16c0 6c 69 73 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 list that follow
a16d0 73 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 s..** Store the
a16e0 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 string in memory
a16f0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
a1700 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e qliteMalloc() an
a1710 64 20 6d 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f d make *pz.** po
a1720 69 6e 74 20 74 6f 20 74 68 61 74 20 73 74 72 69 int to that stri
a1730 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ng..*/.SQLITE_PR
a1740 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a1750 65 33 53 65 74 53 74 72 69 6e 67 28 63 68 61 72 e3SetString(char
a1760 20 2a 2a 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a **pz, sqlite3 *
a1770 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a db, const char *
a1780 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
a1790 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 va_list ap;. c
a17a0 68 61 72 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 har *z;.. va_st
a17b0 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
a17c0 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 ;. z = sqlite3V
a17d0 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 MPrintf(db, zFor
a17e0 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
a17f0 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 nd(ap);. sqlite
a1800 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a 29 3DbFree(db, *pz)
a1810 3b 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a ;. *pz = z;.}..
a1820 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
a1830 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c tion must be cal
a1840 6c 65 64 20 62 65 66 6f 72 65 20 65 78 69 74 69 led before exiti
a1850 6e 67 20 61 6e 79 20 41 50 49 20 66 75 6e 63 74 ng any API funct
a1860 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 ion (i.e. .** re
a1870 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 turning control
a1880 74 6f 20 74 68 65 20 75 73 65 72 29 20 74 68 61 to the user) tha
a1890 74 20 68 61 73 20 63 61 6c 6c 65 64 20 73 71 6c t has called sql
a18a0 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a ite3_malloc or.*
a18b0 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f * sqlite3_reallo
a18c0 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 c..**.** The ret
a18d0 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6e urned value is n
a18e0 6f 72 6d 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f ormally a copy o
a18f0 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
a1900 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a ument to this.**
a1910 20 66 75 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 function. Howev
a1920 65 72 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 er, if a malloc(
a1930 29 20 66 61 69 6c 75 72 65 20 68 61 73 20 6f 63 ) failure has oc
a1940 63 75 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 cured since the
a1950 70 72 65 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f previous.** invo
a1960 63 61 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f cation SQLITE_NO
a1970 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 MEM is returned
a1980 69 6e 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 instead. .**.**
a1990 49 66 20 74 68 65 20 66 69 72 73 74 20 61 72 67 If the first arg
a19a0 75 6d 65 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f ument, db, is no
a19b0 74 20 4e 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c t NULL and a mal
a19c0 6c 6f 63 28 29 20 65 72 72 6f 72 20 68 61 73 20 loc() error has
a19d0 6f 63 63 75 72 65 64 2c 0a 2a 2a 20 74 68 65 6e occured,.** then
a19e0 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 the connection
a19f0 65 72 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 error-code (the
a1a00 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
a1a10 79 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 y sqlite3_errcod
a1a20 65 28 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 e()).** is set t
a1a30 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a o SQLITE_NOMEM..
a1a40 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
a1a50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 E int sqlite3Api
a1a60 45 78 69 74 28 73 71 6c 69 74 65 33 2a 20 64 62 Exit(sqlite3* db
a1a70 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 , int rc){. /*
a1a80 49 66 20 74 68 65 20 64 62 20 68 61 6e 64 6c 65 If the db handle
a1a90 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 is not NULL, th
a1aa0 65 6e 20 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 en we must hold
a1ab0 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 the connection h
a1ac0 61 6e 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 andle. ** mutex
a1ad0 20 68 65 72 65 2e 20 4f 74 68 65 72 77 69 73 65 here. Otherwise
a1ae0 20 74 68 65 20 72 65 61 64 20 28 61 6e 64 20 70 the read (and p
a1af0 6f 73 73 69 62 6c 65 20 77 72 69 74 65 29 20 6f ossible write) o
a1b00 66 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c f db->mallocFail
a1b10 65 64 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 ed . ** is unsa
a1b20 66 65 2c 20 61 73 20 69 73 20 74 68 65 20 63 61 fe, as is the ca
a1b30 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 72 72 ll to sqlite3Err
a1b40 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 or().. */. ass
a1b50 65 72 74 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 ert( !db || sqli
a1b60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
a1b70 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 b->mutex) );. i
a1b80 66 28 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 f( db && (db->ma
a1b90 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 llocFailed || rc
a1ba0 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e ==SQLITE_IOERR_N
a1bb0 4f 4d 45 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c OMEM) ){. sql
a1bc0 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 ite3Error(db, SQ
a1bd0 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a LITE_NOMEM, 0);.
a1be0 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
a1bf0 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 iled = 0;. rc
a1c00 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
a1c10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
a1c20 20 26 20 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 & (db ? db->err
a1c30 4d 61 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a Mask : 0xff);.}.
a1c40 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a1c50 20 45 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 End of malloc.c
a1c60 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
a1c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a1c90 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a1ca0 20 42 65 67 69 6e 20 66 69 6c 65 20 70 72 69 6e Begin file prin
a1cb0 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tf.c ***********
a1cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a1ce0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e ./*.** The "prin
a1cf0 74 66 22 20 63 6f 64 65 20 74 68 61 74 20 66 6f tf" code that fo
a1d00 6c 6c 6f 77 73 20 64 61 74 65 73 20 66 72 6f 6d llows dates from
a1d10 20 74 68 65 20 31 39 38 30 27 73 2e 20 20 49 74 the 1980's. It
a1d20 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 is in.** the pu
a1d30 62 6c 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 blic domain. Th
a1d40 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 e original comme
a1d50 6e 74 73 20 61 72 65 20 69 6e 63 6c 75 64 65 64 nts are included
a1d60 20 68 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d here for.** com
a1d70 70 6c 65 74 65 6e 65 73 73 2e 20 20 54 68 65 79 pleteness. They
a1d80 20 61 72 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 are very out-of
a1d90 2d 64 61 74 65 20 62 75 74 20 6d 69 67 68 74 20 -date but might
a1da0 62 65 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 be useful as.**
a1db0 61 6e 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 an historical re
a1dc0 66 65 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f ference. Most o
a1dd0 66 20 74 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 f the "enhanceme
a1de0 6e 74 73 22 20 68 61 76 65 20 62 65 65 6e 20 62 nts" have been b
a1df0 61 63 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 acked.** out so
a1e00 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f that the functio
a1e10 6e 61 6c 69 74 79 20 69 73 20 6e 6f 77 20 74 68 nality is now th
a1e20 65 20 73 61 6d 65 20 61 73 20 73 74 61 6e 64 61 e same as standa
a1e30 72 64 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a rd printf()..**.
a1e40 2a 2a 20 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 ** $Id: printf.c
a1e50 2c 76 20 31 2e 39 36 20 32 30 30 38 2f 31 31 2f ,v 1.96 2008/11/
a1e60 32 30 20 31 38 3a 32 30 3a 32 38 20 64 72 68 20 20 18:20:28 drh
a1e70 45 78 70 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a Exp $.**.*******
a1e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a1ec0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ***.**.** The fo
a1ed0 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 llowing modules
a1ee0 69 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 is an enhanced r
a1ef0 65 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 eplacement for t
a1f00 68 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72 he "printf" subr
a1f10 6f 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 outines.** found
a1f20 20 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64 in the standard
a1f30 20 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 C library. The
a1f40 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e following enhan
a1f50 63 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 cements are.** s
a1f60 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 upported:.**.**
a1f70 20 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e + Addition
a1f80 61 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 al functions. T
a1f90 68 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20 he standard set
a1fa0 6f 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63 of "printf" func
a1fb0 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 tions.**
a1fc0 20 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66 includes printf
a1fd0 2c 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e , fprintf, sprin
a1fe0 74 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70 tf, vprintf, vfp
a1ff0 72 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 rintf, and.**
a2000 20 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20 vsprintf.
a2010 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 This module add
a2020 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a s the following:
a2030 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
a2040 20 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 * snprintf --
a2050 57 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e Works like sprin
a2060 74 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65 tf, but has an e
a2070 78 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a xtra argument.**
a2080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2090 20 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20 which
a20a0 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 is the size of t
a20b0 68 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65 he buffer writte
a20c0 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 n to..**.**
a20d0 20 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 * mprintf
a20e0 20 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 -- Similar to
a20f0 73 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73 sprintf. Writes
a2100 20 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 output to memor
a2110 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 y.**
a2120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62 ob
a2130 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c tained from mall
a2140 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 oc..**.**
a2150 20 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d * xprintf -
a2160 2d 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 - Calls a funct
a2170 69 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f ion to dispose o
a2180 66 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 f output..**.**
a2190 20 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72 * npr
a21a0 69 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 intf -- No outp
a21b0 75 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20 ut, but returns
a21c0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 the number of ch
a21d0 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 aracters.**
a21e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a21f0 20 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20 that would
a2200 68 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74 have been output
a2210 20 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a by printf..**.*
a2220 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41 * * A
a2230 20 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a v- version (ex:
a2240 20 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 vsnprintf) of e
a2250 76 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 very function is
a2260 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 also.**
a2270 20 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a supplied..
a2280 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 **.** + A
a2290 66 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 few extensions t
a22a0 6f 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 o the formatting
a22b0 20 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75 notation are su
a22c0 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 pported:.**.**
a22d0 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 * The
a22e0 22 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 "=" flag (simila
a22f0 72 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73 r to "-") causes
a2300 20 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62 the output to b
a2310 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 e.**
a2320 20 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e be centered in
a2330 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
a2340 6c 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a ly sized field..
a2350 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **.**
a2360 2a 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20 * The %b field
a2370 6f 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67 outputs an integ
a2380 65 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 er in binary not
a2390 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 ation..**.**
a23a0 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63 * The %c
a23b0 20 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 field now accep
a23c0 74 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 ts a precision.
a23d0 20 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f The character o
a23e0 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 utput.**
a23f0 20 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65 is repeate
a2400 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 d by the number
a2410 6f 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65 of times the pre
a2420 63 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73 cision specifies
a2430 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..**.**
a2440 20 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c * The %' fiel
a2450 64 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c d works like %c,
a2460 20 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74 but takes as it
a2470 73 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a s character the.
a2480 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
a2490 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f next character o
a24a0 66 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 f the format str
a24b0 69 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 ing, instead of
a24c0 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 the next.**
a24d0 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e argumen
a24e0 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c t. For example,
a24f0 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d printf("%.78'-
a2500 22 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69 ") prints 78 mi
a2510 6e 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 nus.**
a2520 20 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73 signs, the s
a2530 61 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22 ame as printf("
a2540 25 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a %.78c",'-')..**.
a2550 2a 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20 ** + When
a2560 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47 compiled using G
a2570 43 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 CC on a SPARC, t
a2580 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 his version of p
a2590 72 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 rintf is.**
a25a0 20 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20 faster than
a25b0 74 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e the library prin
a25c0 74 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e tf for SUN OS 4.
a25d0 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 1..**.** +
a25e0 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 All functions a
a25f0 72 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 re fully reentra
a2600 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a nt..**.*/../*.**
a2610 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 Conversion type
a2620 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 s fall into vari
a2630 6f 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61 ous categories a
a2640 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 s defined by the
a2650 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e .** following en
a2660 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 umeration..*/.#d
a2670 65 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20 efine etRADIX
a2680 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 1 /* Integer
a2690 20 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c types. %d, %x,
a26a0 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 %o, and so fort
a26b0 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 h */.#define etF
a26c0 4c 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20 LOAT 2 /*
a26d0 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 Floating point.
a26e0 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %f */.#define e
a26f0 74 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f tEXP 3 /
a2700 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 * Exponentional
a2710 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 notation. %e and
a2720 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %E */.#define e
a2730 74 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f tGENERIC 4 /
a2740 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 * Floating or ex
a2750 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e ponential, depen
a2760 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 ding on exponent
a2770 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 . %g */.#define
a2780 65 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20 etSIZE 5
a2790 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 /* Return number
a27a0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70 of characters p
a27b0 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e rocessed so far.
a27c0 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %n */.#define e
a27d0 74 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f tSTRING 6 /
a27e0 2a 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f * Strings. %s */
a27f0 0a 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54 .#define etDYNST
a2800 52 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 RING 7 /* Dyna
a2810 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
a2820 64 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f d strings. %z */
a2830 0a 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45 .#define etPERCE
a2840 4e 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63 NT 8 /* Perc
a2850 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a ent symbol. %% *
a2860 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52 /.#define etCHAR
a2870 58 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61 X 9 /* Cha
a2880 72 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f racters. %c */./
a2890 2a 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65 * The rest are e
a28a0 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e xtensions, not n
a28b0 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e ormally found in
a28c0 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 printf() */.#de
a28d0 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 fine etSQLESCAPE
a28e0 20 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 10 /* Strings
a28f0 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 with '\'' double
a2900 64 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e d. %q */.#defin
a2910 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31 e etSQLESCAPE2 1
a2920 31 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 1 /* Strings wit
a2930 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 h '\'' doubled a
a2940 6e 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 nd enclosed in '
a2950 27 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ',.
a2960 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c NUL
a2970 4c 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 L pointers repla
a2980 63 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e ced by SQL NULL.
a2990 20 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 %Q */.#define
a29a0 65 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 etTOKEN 12
a29b0 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 /* a pointer to
a29c0 61 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 a Token structur
a29d0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 e */.#define etS
a29e0 52 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20 RCLIST 13 /*
a29f0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 a pointer to a S
a2a00 72 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e rcList */.#defin
a2a10 65 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31 e etPOINTER 1
a2a20 34 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 4 /* The %p conv
a2a30 65 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e ersion */.#defin
a2a40 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31 e etSQLESCAPE3 1
a2a50 35 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 5 /* %w -> Strin
a2a60 67 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75 gs with '\"' dou
a2a70 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 bled */.#define
a2a80 65 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20 etORDINAL 16
a2a90 2f 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e /* %r -> 1st, 2n
a2aa0 64 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63 d, 3rd, 4th, etc
a2ab0 2e 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 . English only
a2ac0 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 */.../*.** An "e
a2ad0 74 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62 tByte" is an 8-b
a2ae0 69 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 it unsigned valu
a2af0 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e e..*/.typedef un
a2b00 73 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79 signed char etBy
a2b10 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 te;../*.** Each
a2b20 62 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 builtin conversi
a2b30 6f 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78 on character (ex
a2b40 3a 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64 : the 'd' in "%d
a2b50 22 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a ") is described.
a2b60 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 ** by an instanc
a2b70 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
a2b80 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a ng structure.*/.
a2b90 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65 typedef struct e
a2ba0 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e t_info { /* In
a2bb0 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
a2bc0 65 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c each format fiel
a2bd0 64 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 d */. char fmtt
a2be0 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ype;
a2bf0 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69 /* The format fi
a2c00 65 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20 eld code letter
a2c10 2a 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65 */. etByte base
a2c20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
a2c30 20 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61 The base for ra
a2c40 64 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a dix conversion *
a2c50 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73 /. etByte flags
a2c60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a2c70 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 One or more of F
a2c80 4c 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 LAG_ constants b
a2c90 65 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 elow */. etByte
a2ca0 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 type;
a2cb0 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e /* Conversion
a2cc0 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 paradigm */. e
a2cd0 74 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20 tByte charset;
a2ce0 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
a2cf0 74 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d t into aDigits[]
a2d00 20 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73 of the digits s
a2d10 74 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 tring */. etByt
a2d20 65 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 e prefix;
a2d30 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e /* Offset in
a2d40 74 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 to aPrefix[] of
a2d50 74 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e the prefix strin
a2d60 67 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a g */.} et_info;.
a2d70 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
a2d80 61 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 alues for et_inf
a2d90 6f 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 o.flags.*/.#defi
a2da0 6e 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 ne FLAG_SIGNED
a2db0 31 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 1 /* True if
a2dc0 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f the value to co
a2dd0 6e 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20 nvert is signed
a2de0 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f */.#define FLAG_
a2df0 49 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a INTERN 2 /*
a2e00 20 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74 True if for int
a2e10 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a ernal use only *
a2e20 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 /.#define FLAG_S
a2e30 54 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 TRING 4 /*
a2e40 41 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 Allow infinity p
a2e50 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a recision */.../*
a2e60 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
a2e70 67 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63 g table is searc
a2e80 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f hed linearly, so
a2e90 20 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 it is good to p
a2ea0 75 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 ut the.** most f
a2eb0 72 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63 requently used c
a2ec0 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 onversion types
a2ed0 66 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 first..*/.static
a2ee0 20 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67 const char aDig
a2ef0 69 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 its[] = "0123456
a2f00 37 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36 789ABCDEF0123456
a2f10 37 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74 789abcdef";.stat
a2f20 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50 ic const char aP
a2f30 72 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 refix[] = "-x0\0
a2f40 30 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 00X0";.static co
a2f50 6e 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 nst et_info fmti
a2f60 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 nfo[] = {. { '
a2f70 64 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 d', 10, 1, etRAD
a2f80 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d IX, 0, 0 }
a2f90 2c 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 ,. { 's', 0,
a2fa0 34 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20 4, etSTRING,
a2fb0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a2fc0 67 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e g', 0, 1, etGEN
a2fd0 45 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d ERIC, 30, 0 }
a2fe0 2c 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 ,. { 'z', 0,
a2ff0 34 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 4, etDYNSTRING,
a3000 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a3010 71 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c q', 0, 4, etSQL
a3020 45 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d ESCAPE, 0, 0 }
a3030 2c 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 ,. { 'Q', 0,
a3040 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 4, etSQLESCAPE2,
a3050 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a3060 77 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c w', 0, 4, etSQL
a3070 45 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d ESCAPE3, 0, 0 }
a3080 2c 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 ,. { 'c', 0,
a3090 30 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20 0, etCHARX,
a30a0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a30b0 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44 o', 8, 0, etRAD
a30c0 49 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d IX, 0, 2 }
a30d0 2c 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 ,. { 'u', 10,
a30e0 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 0, etRADIX,
a30f0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a3100 78 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 x', 16, 0, etRAD
a3110 49 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d IX, 16, 1 }
a3120 2c 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 ,. { 'X', 16,
a3130 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 0, etRADIX,
a3140 20 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 0, 4 },.#ifnde
a3150 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
a3160 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b OATING_POINT. {
a3170 20 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 'f', 0, 1, et
a3180 46 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20 FLOAT, 0,
a3190 30 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 0 },. { 'e',
a31a0 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 0, 1, etEXP,
a31b0 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 30, 0 },. {
a31c0 20 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 'E', 0, 1, et
a31d0 45 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20 EXP, 14,
a31e0 30 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 0 },. { 'G',
a31f0 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 0, 1, etGENERIC,
a3200 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 14, 0 },.#en
a3210 64 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 dif. { 'i', 10
a3220 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 , 1, etRADIX,
a3230 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 0, 0 },. {
a3240 20 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 'n', 0, 0, etS
a3250 49 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30 IZE, 0, 0
a3260 20 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 },. { '%', 0
a3270 2c 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 , 0, etPERCENT,
a3280 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 0, 0 },. {
a3290 20 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 'p', 16, 0, etP
a32a0 4f 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31 OINTER, 0, 1
a32b0 20 7d 2c 0a 20 20 7b 20 20 27 54 27 2c 20 20 30 },. { 'T', 0
a32c0 2c 20 32 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20 , 2, etTOKEN,
a32d0 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 0, 0 },. {
a32e0 20 27 53 27 2c 20 20 30 2c 20 32 2c 20 65 74 53 'S', 0, 2, etS
a32f0 52 43 4c 49 53 54 2c 20 20 20 20 30 2c 20 20 30 RCLIST, 0, 0
a3300 20 7d 2c 0a 20 20 7b 20 20 27 72 27 2c 20 31 30 },. { 'r', 10
a3310 2c 20 33 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20 , 3, etORDINAL,
a3320 20 20 20 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 0, 0 },.};..
a3330 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f /*.** If SQLITE_
a3340 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
a3350 49 4e 54 20 69 73 20 64 65 66 69 6e 65 64 2c 20 INT is defined,
a3360 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 then none of the
a3370 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a floating point.
a3380 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 ** conversions w
a3390 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 ill work..*/.#if
a33a0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
a33b0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
a33c0 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 /*.** "*val" is
a33d0 61 20 64 6f 75 62 6c 65 20 73 75 63 68 20 74 68 a double such th
a33e0 61 74 20 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c at 0.1 <= *val <
a33f0 20 31 30 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 10.0.** Return
a3400 74 68 65 20 61 73 63 69 69 20 63 6f 64 65 20 66 the ascii code f
a3410 6f 72 20 74 68 65 20 6c 65 61 64 69 6e 67 20 64 or the leading d
a3420 69 67 69 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 igit of *val, th
a3430 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 en.** multiply "
a3440 2a 76 61 6c 22 20 62 79 20 31 30 2e 30 20 74 6f *val" by 10.0 to
a3450 20 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a renormalize..**
a3460 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 .** Example:.**
a3470 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20 2a input: *
a3480 76 61 6c 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a val = 3.14159.**
a3490 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 output:
a34a0 2a 76 61 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 *val = 1.4159
a34b0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
a34c0 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 = '3'.**.** The
a34d0 20 63 6f 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 counter *cnt is
a34e0 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 63 incremented eac
a34f0 68 20 74 69 6d 65 2e 20 20 41 66 74 65 72 20 63 h time. After c
a3500 6f 75 6e 74 65 72 20 65 78 63 65 65 64 73 0a 2a ounter exceeds.*
a3510 2a 20 31 36 20 28 74 68 65 20 6e 75 6d 62 65 72 * 16 (the number
a3520 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 of significant
a3530 64 69 67 69 74 73 20 69 6e 20 61 20 36 34 2d 62 digits in a 64-b
a3540 69 74 20 66 6c 6f 61 74 29 20 27 30 27 20 69 73 it float) '0' is
a3550 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 .** always retur
a3560 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
a3570 6e 74 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c nt et_getdigit(L
a3580 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a ONGDOUBLE_TYPE *
a3590 76 61 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a val, int *cnt){.
a35a0 20 20 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c int digit;. L
a35b0 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 ONGDOUBLE_TYPE d
a35c0 3b 0a 20 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b ;. if( (*cnt)++
a35d0 20 3e 3d 20 31 36 20 29 20 72 65 74 75 72 6e 20 >= 16 ) return
a35e0 27 30 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 '0';. digit = (
a35f0 69 6e 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 int)*val;. d =
a3600 64 69 67 69 74 3b 0a 20 20 64 69 67 69 74 20 2b digit;. digit +
a3610 3d 20 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 = '0';. *val =
a3620 28 2a 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b (*val - d)*10.0;
a3630 0a 20 20 72 65 74 75 72 6e 20 64 69 67 69 74 3b . return digit;
a3640 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
a3650 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
a3660 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a G_POINT */../*.*
a3670 2a 20 41 70 70 65 6e 64 20 4e 20 73 70 61 63 65 * Append N space
a3680 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 characters to t
a3690 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 he given string
a36a0 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 buffer..*/.stati
a36b0 63 20 76 6f 69 64 20 61 70 70 65 6e 64 53 70 61 c void appendSpa
a36c0 63 65 28 53 74 72 41 63 63 75 6d 20 2a 70 41 63 ce(StrAccum *pAc
a36d0 63 75 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 cum, int N){. s
a36e0 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
a36f0 20 7a 53 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 zSpaces[] = "
a3700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3710 20 20 20 20 20 20 20 20 20 20 20 22 3b 0a 20 20 ";.
a3720 77 68 69 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73 while( N>=(int)s
a3730 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 izeof(zSpaces)-1
a3740 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
a3750 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 trAccumAppend(pA
a3760 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73 ccum, zSpaces, s
a3770 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 izeof(zSpaces)-1
a3780 29 3b 0a 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65 );. N -= size
a3790 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20 of(zSpaces)-1;.
a37a0 20 7d 0a 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a }. if( N>0 ){.
a37b0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a37c0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
a37d0 2c 20 7a 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20 , zSpaces, N);.
a37e0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d }.}../*.** On m
a37f0 61 63 68 69 6e 65 73 20 77 69 74 68 20 61 20 73 achines with a s
a3800 6d 61 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c mall stack size,
a3810 20 79 6f 75 20 63 61 6e 20 72 65 64 65 66 69 6e you can redefin
a3820 65 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f e the.** SQLITE_
a3830 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74 PRINT_BUF_SIZE t
a3840 6f 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 33 o be less than 3
a3850 35 30 2e 20 20 42 75 74 20 62 65 77 61 72 65 20 50. But beware
a3860 2d 20 66 6f 72 0a 2a 2a 20 73 6d 61 6c 6c 65 72 - for.** smaller
a3870 20 76 61 6c 75 65 73 20 73 6f 6d 65 20 25 66 20 values some %f
a3880 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6d 61 79 20 conversions may
a3890 67 6f 20 69 6e 74 6f 20 61 6e 20 69 6e 66 69 6e go into an infin
a38a0 69 74 65 20 6c 6f 6f 70 2e 0a 2a 2f 0a 23 69 66 ite loop..*/.#if
a38b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 49 4e ndef SQLITE_PRIN
a38c0 54 5f 42 55 46 5f 53 49 5a 45 0a 23 20 64 65 66 T_BUF_SIZE.# def
a38d0 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 ine SQLITE_PRINT
a38e0 5f 42 55 46 5f 53 49 5a 45 20 33 35 30 0a 23 65 _BUF_SIZE 350.#e
a38f0 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 65 74 42 ndif.#define etB
a3900 55 46 53 49 5a 45 20 53 51 4c 49 54 45 5f 50 52 UFSIZE SQLITE_PR
a3910 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a INT_BUF_SIZE /*
a3920 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 Size of the out
a3930 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f put buffer */../
a3940 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 72 *.** The root pr
a3950 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69 ogram. All vari
a3960 61 74 69 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73 ations call this
a3970 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 core..**.** INP
a3980 55 54 53 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20 UTS:.** func
a3990 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74 This is a point
a39a0 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e er to a function
a39b0 20 74 61 6b 69 6e 67 20 74 68 72 65 65 20 61 72 taking three ar
a39c0 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 guments.**
a39d0 20 20 20 20 20 20 31 2e 20 41 20 70 6f 69 6e 74 1. A point
a39e0 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20 er to anything.
a39f0 20 53 61 6d 65 20 61 73 20 74 68 65 20 22 61 72 Same as the "ar
a3a00 67 22 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a g" parameter..**
a3a10 20 20 20 20 20 20 20 20 20 20 20 20 32 2e 20 41 2. A
a3a20 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
a3a30 6c 69 73 74 20 6f 66 20 63 68 61 72 61 63 74 65 list of characte
a3a40 72 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74 0a rs to be output.
a3a50 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
a3a60 20 28 4e 6f 74 65 2c 20 74 68 69 73 20 6c 69 73 (Note, this lis
a3a70 74 20 69 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 t is NOT null te
a3a80 72 6d 69 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20 rminated.).**
a3a90 20 20 20 20 20 20 20 20 20 33 2e 20 41 6e 20 69 3. An i
a3aa0 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 nteger number of
a3ab0 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 62 characters to b
a3ac0 65 20 6f 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20 e output..**
a3ad0 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65 (Note
a3ae0 3a 20 54 68 69 73 20 6e 75 6d 62 65 72 20 6d 69 : This number mi
a3af0 67 68 74 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a ght be zero.).**
a3b00 0a 2a 2a 20 20 20 61 72 67 20 20 20 20 54 68 69 .** arg Thi
a3b10 73 20 69 73 20 74 68 65 20 70 6f 69 6e 74 65 72 s is the pointer
a3b20 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 77 68 69 to anything whi
a3b30 63 68 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 ch will be passe
a3b40 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 d as the.**
a3b50 20 20 20 20 20 66 69 72 73 74 20 61 72 67 75 6d first argum
a3b60 65 6e 74 20 74 6f 20 22 66 75 6e 63 22 2e 20 20 ent to "func".
a3b70 55 73 65 20 69 74 20 66 6f 72 20 77 68 61 74 65 Use it for whate
a3b80 76 65 72 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a ver you like..**
a3b90 0a 2a 2a 20 20 20 66 6d 74 20 20 20 20 54 68 69 .** fmt Thi
a3ba0 73 20 69 73 20 74 68 65 20 66 6f 72 6d 61 74 20 s is the format
a3bb0 73 74 72 69 6e 67 2c 20 61 73 20 69 6e 20 74 68 string, as in th
a3bc0 65 20 75 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a e usual print..*
a3bd0 2a 0a 2a 2a 20 20 20 61 70 20 20 20 20 20 54 68 *.** ap Th
a3be0 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 is is a pointer
a3bf0 74 6f 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67 to a list of arg
a3c00 75 6d 65 6e 74 73 2e 20 20 53 61 6d 65 20 61 73 uments. Same as
a3c10 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 in.**
a3c20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f vfprint..**.** O
a3c30 55 54 50 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20 UTPUTS:.**
a3c40 20 20 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 The return v
a3c50 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74 61 alue is the tota
a3c60 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 l number of char
a3c70 61 63 74 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a acters sent to.*
a3c80 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66 * the f
a3c90 75 6e 63 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20 unction "func".
a3ca0 20 52 65 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61 Returns -1 on a
a3cb0 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f error..**.** No
a3cc0 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 te that the orde
a3cd0 72 20 69 6e 20 77 68 69 63 68 20 61 75 74 6f 6d r in which autom
a3ce0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 61 atic variables a
a3cf0 72 65 20 64 65 63 6c 61 72 65 64 20 62 65 6c 6f re declared belo
a3d00 77 0a 2a 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61 w.** seems to ma
a3d10 6b 65 20 61 20 62 69 67 20 64 69 66 66 65 72 65 ke a big differe
a3d20 6e 63 65 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 nce in determini
a3d30 6e 67 20 68 6f 77 20 66 61 73 74 20 74 68 69 73 ng how fast this
a3d40 20 62 65 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 beast.** will r
a3d50 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 un..*/.SQLITE_PR
a3d60 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a3d70 65 33 56 58 50 72 69 6e 74 66 28 0a 20 20 53 74 e3VXPrintf(. St
a3d80 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 rAccum *pAccum,
a3d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3da0 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72 /* Accumulate r
a3db0 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 esults here */.
a3dc0 20 69 6e 74 20 75 73 65 45 78 74 65 6e 64 65 64 int useExtended
a3dd0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
a3de0 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 /* Allow ext
a3df0 65 6e 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 ended %-conversi
a3e00 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ons */. const c
a3e10 68 61 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 har *fmt,
a3e20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
a3e30 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a ormat string */.
a3e40 20 20 76 61 5f 6c 69 73 74 20 61 70 20 20 20 20 va_list ap
a3e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3e60 20 20 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 /* argument
a3e70 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b s */.){. int c;
a3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3e90 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 /* Next cha
a3ea0 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 66 6f racter in the fo
a3eb0 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 rmat string */.
a3ec0 20 63 68 61 72 20 2a 62 75 66 70 74 3b 20 20 20 char *bufpt;
a3ed0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
a3ee0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f ointer to the co
a3ef0 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 nversion buffer
a3f00 2a 2f 0a 20 20 69 6e 74 20 70 72 65 63 69 73 69 */. int precisi
a3f10 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 on;
a3f20 2f 2a 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 /* Precision of
a3f30 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c the current fiel
a3f40 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 d */. int lengt
a3f50 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
a3f60 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 /* Length of t
a3f70 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e he field */. in
a3f80 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 t idx;
a3f90 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 67 65 /* A ge
a3fa0 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f neral purpose lo
a3fb0 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
a3fc0 69 6e 74 20 77 69 64 74 68 3b 20 20 20 20 20 20 int width;
a3fd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 /* Wi
a3fe0 64 74 68 20 6f 66 20 74 68 65 20 63 75 72 72 65 dth of the curre
a3ff0 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 nt field */. et
a4000 42 79 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 Byte flag_leftju
a4010 73 74 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 stify; /* True
a4020 20 69 66 20 22 2d 22 20 66 6c 61 67 20 69 73 20 if "-" flag is
a4030 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
a4040 79 74 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 yte flag_plussig
a4050 6e 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 n; /* True
a4060 69 66 20 22 2b 22 20 66 6c 61 67 20 69 73 20 70 if "+" flag is p
a4070 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 resent */. etBy
a4080 74 65 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 te flag_blanksig
a4090 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 n; /* True i
a40a0 66 20 22 20 22 20 66 6c 61 67 20 69 73 20 70 72 f " " flag is pr
a40b0 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 esent */. etByt
a40c0 65 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 e flag_alternate
a40d0 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 form; /* True if
a40e0 20 22 23 22 20 66 6c 61 67 20 69 73 20 70 72 65 "#" flag is pre
a40f0 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
a4100 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 flag_altform2;
a4110 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a4120 22 21 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 "!" flag is pres
a4130 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ent */. etByte
a4140 66 6c 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 flag_zeropad;
a4150 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 /* True if f
a4160 69 65 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74 ield width const
a4170 61 6e 74 20 73 74 61 72 74 73 20 77 69 74 68 20 ant starts with
a4180 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 zero */. etByte
a4190 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 flag_long;
a41a0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a41b0 22 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 "l" flag is pres
a41c0 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ent */. etByte
a41d0 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 flag_longlong;
a41e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
a41f0 68 65 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 he "ll" flag is
a4200 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
a4210 79 74 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 yte done;
a4220 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
a4230 74 65 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 termination flag
a4240 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e */. sqlite_uin
a4250 74 36 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 t64 longvalue;
a4260 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 69 6e /* Value for in
a4270 74 65 67 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 teger types */.
a4280 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 LONGDOUBLE_TYPE
a4290 20 72 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 realvalue; /* V
a42a0 61 6c 75 65 20 66 6f 72 20 72 65 61 6c 20 74 79 alue for real ty
a42b0 70 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 pes */. const e
a42c0 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 t_info *infop;
a42d0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
a42e0 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 o the appropriat
a42f0 65 20 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 e info structure
a4300 20 2a 2f 0a 20 20 63 68 61 72 20 62 75 66 5b 65 */. char buf[e
a4310 74 42 55 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 tBUFSIZE];
a4320 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 /* Conversion b
a4330 75 66 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 uffer */. char
a4340 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 prefix;
a4350 20 20 20 20 20 20 2f 2a 20 50 72 65 66 69 78 20 /* Prefix
a4360 63 68 61 72 61 63 74 65 72 2e 20 20 22 2b 22 20 character. "+"
a4370 6f 72 20 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 or "-" or " " or
a4380 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 '\0'. */. etBy
a4390 74 65 20 78 74 79 70 65 3b 20 20 20 20 20 20 20 te xtype;
a43a0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 /* Conver
a43b0 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f sion paradigm */
a43c0 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b . char *zExtra;
a43d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a43e0 20 45 78 74 72 61 20 6d 65 6d 6f 72 79 20 75 73 Extra memory us
a43f0 65 64 20 66 6f 72 20 65 74 54 43 4c 45 53 43 41 ed for etTCLESCA
a4400 50 45 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a PE conversions *
a4410 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
a4420 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
a4430 4f 49 4e 54 0a 20 20 69 6e 74 20 20 65 78 70 2c OINT. int exp,
a4440 20 65 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 e2;
a4450 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66 /* exponent of
a4460 20 72 65 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f real numbers */
a4470 0a 20 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65 . double rounde
a4480 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r; /*
a4490 20 55 73 65 64 20 66 6f 72 20 72 6f 75 6e 64 69 Used for roundi
a44a0 6e 67 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e ng floating poin
a44b0 74 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74 t values */. et
a44c0 42 79 74 65 20 66 6c 61 67 5f 64 70 3b 20 20 20 Byte flag_dp;
a44d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
a44e0 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e if decimal poin
a44f0 74 20 73 68 6f 75 6c 64 20 62 65 20 73 68 6f 77 t should be show
a4500 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c n */. etByte fl
a4510 61 67 5f 72 74 7a 3b 20 20 20 20 20 20 20 20 20 ag_rtz;
a4520 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 72 61 /* True if tra
a4530 69 6c 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75 iling zeros shou
a4540 6c 64 20 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f ld be removed */
a4550 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 65 . etByte flag_e
a4560 78 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a xp; /*
a4570 20 54 72 75 65 20 74 6f 20 66 6f 72 63 65 20 64 True to force d
a4580 69 73 70 6c 61 79 20 6f 66 20 74 68 65 20 65 78 isplay of the ex
a4590 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 ponent */. int
a45a0 6e 73 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 nsd;
a45b0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
a45c0 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 of significant
a45d0 64 69 67 69 74 73 20 72 65 74 75 72 6e 65 64 20 digits returned
a45e0 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e */.#endif.. len
a45f0 67 74 68 20 3d 20 30 3b 0a 20 20 62 75 66 70 74 gth = 0;. bufpt
a4600 20 3d 20 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63 = 0;. for(; (c
a4610 3d 28 2a 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 =(*fmt))!=0; ++f
a4620 6d 74 29 7b 0a 20 20 20 20 69 66 28 20 63 21 3d mt){. if( c!=
a4630 27 25 27 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 '%' ){. int
a4640 20 61 6d 74 3b 0a 20 20 20 20 20 20 62 75 66 70 amt;. bufp
a4650 74 20 3d 20 28 63 68 61 72 20 2a 29 66 6d 74 3b t = (char *)fmt;
a4660 0a 20 20 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a . amt = 1;.
a4670 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d while( (c=
a4680 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 25 27 20 26 (*++fmt))!='%' &
a4690 26 20 63 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a & c!=0 ) amt++;.
a46a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
a46b0 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
a46c0 75 6d 2c 20 62 75 66 70 74 2c 20 61 6d 74 29 3b um, bufpt, amt);
a46d0 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 . if( c==0
a46e0 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 ) break;. }.
a46f0 20 20 20 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d if( (c=(*++fm
a4700 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 t))==0 ){.
a4710 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
a4720 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 22 25 ppend(pAccum, "%
a4730 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 ", 1);. bre
a4740 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a ak;. }. /*
a4750 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20 66 Find out what f
a4760 6c 61 67 73 20 61 72 65 20 70 72 65 73 65 6e 74 lags are present
a4770 20 2a 2f 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66 */. flag_lef
a4780 74 6a 75 73 74 69 66 79 20 3d 20 66 6c 61 67 5f tjustify = flag_
a4790 70 6c 75 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f plussign = flag_
a47a0 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20 blanksign = .
a47b0 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 flag_alternate
a47c0 66 6f 72 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66 form = flag_altf
a47d0 6f 72 6d 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f orm2 = flag_zero
a47e0 70 61 64 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e pad = 0;. don
a47f0 65 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 e = 0;. do{.
a4800 20 20 20 20 20 73 77 69 74 63 68 28 20 63 20 29 switch( c )
a4810 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 {. case '
a4820 2d 27 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a -': flag_leftj
a4830 75 73 74 69 66 79 20 3d 20 31 3b 20 20 20 20 20 ustify = 1;
a4840 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
a4850 61 73 65 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f ase '+': flag_
a4860 70 6c 75 73 73 69 67 6e 20 3d 20 31 3b 20 20 20 plussign = 1;
a4870 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a4880 20 20 20 20 63 61 73 65 20 27 20 27 3a 20 20 20 case ' ':
a4890 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d flag_blanksign =
a48a0 20 31 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 1; break;
a48b0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 23 . case '#
a48c0 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e ': flag_altern
a48d0 61 74 65 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62 ateform = 1; b
a48e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
a48f0 73 65 20 27 21 27 3a 20 20 20 66 6c 61 67 5f 61 se '!': flag_a
a4900 6c 74 66 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20 ltform2 = 1;
a4910 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
a4920 20 20 20 63 61 73 65 20 27 30 27 3a 20 20 20 66 case '0': f
a4930 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b lag_zeropad = 1;
a4940 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
a4950 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a default:
a4960 20 20 20 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20 done = 1;
a4970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 br
a4980 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
a4990 20 7d 77 68 69 6c 65 28 20 21 64 6f 6e 65 20 26 }while( !done &
a49a0 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d & (c=(*++fmt))!=
a49b0 30 20 29 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20 0 );. /* Get
a49c0 74 68 65 20 66 69 65 6c 64 20 77 69 64 74 68 20 the field width
a49d0 2a 2f 0a 20 20 20 20 77 69 64 74 68 20 3d 20 30 */. width = 0
a49e0 3b 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 ;. if( c=='*'
a49f0 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 ){. width
a4a00 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 = va_arg(ap,int)
a4a10 3b 0a 20 20 20 20 20 20 69 66 28 20 77 69 64 74 ;. if( widt
a4a20 68 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 h<0 ){. f
a4a30 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 lag_leftjustify
a4a40 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 77 69 64 = 1;. wid
a4a50 74 68 20 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20 th = -width;.
a4a60 20 20 20 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a }. c = *
a4a70 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 ++fmt;. }else
a4a80 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 {. while( c
a4a90 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 >='0' && c<='9'
a4aa0 29 7b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 ){. width
a4ab0 20 3d 20 77 69 64 74 68 2a 31 30 20 2b 20 63 20 = width*10 + c
a4ac0 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 63 - '0';. c
a4ad0 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 = *++fmt;.
a4ae0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
a4af0 20 77 69 64 74 68 20 3e 20 65 74 42 55 46 53 49 width > etBUFSI
a4b00 5a 45 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 77 ZE-10 ){. w
a4b10 69 64 74 68 20 3d 20 65 74 42 55 46 53 49 5a 45 idth = etBUFSIZE
a4b20 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f -10;. }. /
a4b30 2a 20 47 65 74 20 74 68 65 20 70 72 65 63 69 73 * Get the precis
a4b40 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 ion */. if( c
a4b50 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 70 =='.' ){. p
a4b60 72 65 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 recision = 0;.
a4b70 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a c = *++fmt;.
a4b80 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 if( c=='*'
a4b90 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 65 63 ){. prec
a4ba0 69 73 69 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61 ision = va_arg(a
a4bb0 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 p,int);.
a4bc0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 if( precision<0
a4bd0 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70 ) precision = -p
a4be0 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 recision;.
a4bf0 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 c = *++fmt;.
a4c00 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a4c10 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 while( c>='0'
a4c20 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 && c<='9' ){.
a4c30 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f precisio
a4c40 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31 30 n = precision*10
a4c50 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 + c - '0';.
a4c60 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 c = *++fmt
a4c70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a4c80 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
a4c90 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d precision =
a4ca0 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f -1;. }. /
a4cb0 2a 20 47 65 74 20 74 68 65 20 63 6f 6e 76 65 72 * Get the conver
a4cc0 73 69 6f 6e 20 74 79 70 65 20 6d 6f 64 69 66 69 sion type modifi
a4cd0 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d er */. if( c=
a4ce0 3d 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 66 6c ='l' ){. fl
a4cf0 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 ag_long = 1;.
a4d00 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 c = *++fmt;.
a4d10 20 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 if( c=='l'
a4d20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f ){. flag_
a4d30 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 longlong = 1;.
a4d40 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 c = *++fmt
a4d50 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
a4d60 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 flag_long
a4d70 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 long = 0;.
a4d80 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
a4d90 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 flag_long = f
a4da0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 lag_longlong = 0
a4db0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 ;. }. /* F
a4dc0 65 74 63 68 20 74 68 65 20 69 6e 66 6f 20 65 6e etch the info en
a4dd0 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 65 6c try for the fiel
a4de0 64 20 2a 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d d */. infop =
a4df0 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 0;. for(idx=
a4e00 30 3b 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65 0; idx<ArraySize
a4e10 28 66 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b (fmtinfo); idx++
a4e20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d ){. if( c==
a4e30 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 fmtinfo[idx].fmt
a4e40 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 type ){.
a4e50 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f infop = &fmtinfo
a4e60 5b 69 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 [idx];. i
a4e70 66 28 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c f( useExtended |
a4e80 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 | (infop->flags
a4e90 26 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d & FLAG_INTERN)==
a4ea0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 0 ){. x
a4eb0 74 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 type = infop->ty
a4ec0 70 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 pe;. }els
a4ed0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 e{. ret
a4ee0 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 urn;. }.
a4ef0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a4f00 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
a4f10 7a 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 20 20 zExtra = 0;.
a4f20 69 66 28 20 69 6e 66 6f 70 3d 3d 30 20 29 7b 0a if( infop==0 ){.
a4f30 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
a4f40 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 20 4c 69 6d }... /* Lim
a4f50 69 74 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e it the precision
a4f60 20 74 6f 20 70 72 65 76 65 6e 74 20 6f 76 65 72 to prevent over
a4f70 66 6c 6f 77 69 6e 67 20 62 75 66 5b 5d 20 64 75 flowing buf[] du
a4f80 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ring conversion
a4f90 2a 2f 0a 20 20 20 20 69 66 28 20 70 72 65 63 69 */. if( preci
a4fa0 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2d 34 sion>etBUFSIZE-4
a4fb0 30 20 26 26 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 0 && (infop->fla
a4fc0 67 73 20 26 20 46 4c 41 47 5f 53 54 52 49 4e 47 gs & FLAG_STRING
a4fd0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 72 )==0 ){. pr
a4fe0 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 ecision = etBUFS
a4ff0 49 5a 45 2d 34 30 3b 0a 20 20 20 20 7d 0a 0a 20 IZE-40;. }..
a5000 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 41 74 20 /*. ** At
a5010 74 68 69 73 20 70 6f 69 6e 74 2c 20 76 61 72 69 this point, vari
a5020 61 62 6c 65 73 20 61 72 65 20 69 6e 69 74 69 61 ables are initia
a5030 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 lized as follows
a5040 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 :. **. **
a5050 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 flag_alternate
a5060 66 6f 72 6d 20 20 20 20 20 20 20 20 20 20 54 52 form TR
a5070 55 45 20 69 66 20 61 20 27 23 27 20 69 73 20 70 UE if a '#' is p
a5080 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 resent.. **
a5090 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 20 flag_altform2
a50a0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 TRU
a50b0 45 20 69 66 20 61 20 27 21 27 20 69 73 20 70 72 E if a '!' is pr
a50c0 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 esent.. **
a50d0 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 20 20 flag_plussign
a50e0 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 TRUE
a50f0 20 69 66 20 61 20 27 2b 27 20 69 73 20 70 72 65 if a '+' is pre
a5100 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 sent.. ** f
a5110 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 lag_leftjustify
a5120 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 TRUE
a5130 69 66 20 61 20 27 2d 27 20 69 73 20 70 72 65 73 if a '-' is pres
a5140 65 6e 74 20 6f 72 20 69 66 20 74 68 65 0a 20 20 ent or if the.
a5150 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 **
a5160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5170 20 20 20 66 69 65 6c 64 20 77 69 64 74 68 20 77 field width w
a5180 61 73 20 6e 65 67 61 74 69 76 65 2e 0a 20 20 20 as negative..
a5190 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 ** flag_zerop
a51a0 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ad
a51b0 20 20 54 52 55 45 20 69 66 20 74 68 65 20 77 69 TRUE if the wi
a51c0 64 74 68 20 62 65 67 61 6e 20 77 69 74 68 20 30 dth began with 0
a51d0 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f .. ** flag_
a51e0 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 long
a51f0 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74 TRUE if t
a5200 68 65 20 6c 65 74 74 65 72 20 27 6c 27 20 28 65 he letter 'l' (e
a5210 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20 20 ll) prefixed.
a5220 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 **
a5230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5240 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e the conversion
a5250 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 character..
a5260 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f ** flag_longlo
a5270 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ng
a5280 20 54 52 55 45 20 69 66 20 74 68 65 20 6c 65 74 TRUE if the let
a5290 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c 20 65 6c ter 'll' (ell el
a52a0 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20 20 20 l) prefixed.
a52b0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
a52c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a52d0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
a52e0 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a character.. *
a52f0 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 * flag_blanksi
a5300 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 gn
a5310 54 52 55 45 20 69 66 20 61 20 27 20 27 20 69 73 TRUE if a ' ' is
a5320 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a present.. **
a5330 20 20 20 77 69 64 74 68 20 20 20 20 20 20 20 20 width
a5340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
a5350 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 65 he specified fie
a5360 6c 64 20 77 69 64 74 68 2e 20 20 54 68 69 73 20 ld width. This
a5370 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 is. **
a5380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5390 20 20 20 20 20 20 20 20 61 6c 77 61 79 73 20 6e always n
a53a0 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 5a 65 on-negative. Ze
a53b0 72 6f 20 69 73 20 74 68 65 20 64 65 66 61 75 6c ro is the defaul
a53c0 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70 72 65 63 t.. ** prec
a53d0 69 73 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 ision
a53e0 20 20 20 20 20 20 20 20 54 68 65 20 73 70 65 63 The spec
a53f0 69 66 69 65 64 20 70 72 65 63 69 73 69 6f 6e 2e ified precision.
a5400 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 20 20 The default.
a5410 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 **
a5420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5430 20 20 20 69 73 20 2d 31 2e 0a 20 20 20 20 2a 2a is -1.. **
a5440 20 20 20 78 74 79 70 65 20 20 20 20 20 20 20 20 xtype
a5450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
a5460 68 65 20 63 6c 61 73 73 20 6f 66 20 74 68 65 20 he class of the
a5470 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20 20 20 20 conversion..
a5480 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20 20 20 20 ** infop
a5490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a54a0 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
a54b0 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66 6f appropriate info
a54c0 20 73 74 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a struct.. */.
a54d0 20 20 20 20 73 77 69 74 63 68 28 20 78 74 79 70 switch( xtyp
a54e0 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 e ){. case
a54f0 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20 20 20 20 etPOINTER:.
a5500 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 flag_longlong
a5510 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 = sizeof(char*)
a5520 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29 3b 0a 20 ==sizeof(i64);.
a5530 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 flag_long
a5540 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 = sizeof(char*)
a5550 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67 20 69 6e ==sizeof(long in
a5560 74 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 t);. /* F
a5570 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f all through into
a5580 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a the next case *
a5590 2f 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 4f /. case etO
a55a0 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20 20 63 61 RDINAL:. ca
a55b0 73 65 20 65 74 52 41 44 49 58 3a 0a 20 20 20 20 se etRADIX:.
a55c0 20 20 20 20 69 66 28 20 69 6e 66 6f 70 2d 3e 66 if( infop->f
a55d0 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 49 47 4e lags & FLAG_SIGN
a55e0 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ED ){.
a55f0 69 36 34 20 76 3b 0a 20 20 20 20 20 20 20 20 20 i64 v;.
a5600 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f if( flag_longlo
a5610 6e 67 20 29 20 20 20 76 20 3d 20 76 61 5f 61 72 ng ) v = va_ar
a5620 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 g(ap,i64);.
a5630 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c else if( fl
a5640 61 67 5f 6c 6f 6e 67 20 29 20 20 76 20 3d 20 76 ag_long ) v = v
a5650 61 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e a_arg(ap,long in
a5660 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c t);. el
a5670 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 se
a5680 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 61 v = va_arg(a
a5690 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 p,int);.
a56a0 20 20 69 66 28 20 76 3c 30 20 29 7b 0a 20 20 20 if( v<0 ){.
a56b0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c longval
a56c0 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 ue = -v;.
a56d0 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d prefix = '-
a56e0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c ';. }el
a56f0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
a5700 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 3b 0a 20 longvalue = v;.
a5710 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 if( f
a5720 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 lag_plussign )
a5730 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 prefix = '
a5740 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 +';.
a5750 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c else if( flag_bl
a5760 61 6e 6b 73 69 67 6e 20 29 20 20 70 72 65 66 69 anksign ) prefi
a5770 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 x = ' ';.
a5780 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 else
a5790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a57a0 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 prefix = 0;.
a57b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a57c0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a57d0 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f if( flag_longlo
a57e0 6e 67 20 29 20 20 20 6c 6f 6e 67 76 61 6c 75 65 ng ) longvalue
a57f0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34 = va_arg(ap,u64
a5800 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 );. els
a5810 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 e if( flag_long
a5820 29 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 ) longvalue = v
a5830 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 a_arg(ap,unsigne
a5840 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 d long int);.
a5850 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 else
a5860 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e lon
a5870 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 gvalue = va_arg(
a5880 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 ap,unsigned int)
a5890 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 66 ;. pref
a58a0 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ix = 0;.
a58b0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f }. if( lo
a58c0 6e 67 76 61 6c 75 65 3d 3d 30 20 29 20 66 6c 61 ngvalue==0 ) fla
a58d0 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 g_alternateform
a58e0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 = 0;. if(
a58f0 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 flag_zeropad &&
a5900 20 70 72 65 63 69 73 69 6f 6e 3c 77 69 64 74 68 precision<width
a5910 2d 28 70 72 65 66 69 78 21 3d 30 29 20 29 7b 0a -(prefix!=0) ){.
a5920 20 20 20 20 20 20 20 20 20 20 70 72 65 63 69 73 precis
a5930 69 6f 6e 20 3d 20 77 69 64 74 68 2d 28 70 72 65 ion = width-(pre
a5940 66 69 78 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 fix!=0);.
a5950 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 }. bufpt
a5960 20 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a = &buf[etBUFSIZ
a5970 45 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 E-1];. if
a5980 28 20 78 74 79 70 65 3d 3d 65 74 4f 52 44 49 4e ( xtype==etORDIN
a5990 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 AL ){.
a59a0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
a59b0 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 74 68 73 74 r zOrd[] = "thst
a59c0 6e 64 72 64 22 3b 0a 20 20 20 20 20 20 20 20 20 ndrd";.
a59d0 20 69 6e 74 20 78 20 3d 20 6c 6f 6e 67 76 61 6c int x = longval
a59e0 75 65 20 25 20 31 30 3b 0a 20 20 20 20 20 20 20 ue % 10;.
a59f0 20 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28 if( x>=4 || (
a5a00 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30 longvalue/10)%10
a5a10 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==1 ){.
a5a20 20 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 x = 0;.
a5a30 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
a5a40 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d buf[etBUFSIZE-3]
a5a50 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 = zOrd[x*2];.
a5a60 20 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 buf[etBU
a5a70 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b FSIZE-2] = zOrd[
a5a80 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 x*2+1];.
a5a90 20 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20 bufpt -= 2;.
a5aa0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a5ab0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 {. regi
a5ac0 73 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20 ster const char
a5ad0 2a 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 *cset; /* U
a5ae0 73 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 se registers for
a5af0 20 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 speed */.
a5b00 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 register int
a5b10 20 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 base;.
a5b20 20 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73 cset = &aDigits
a5b30 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d [infop->charset]
a5b40 3b 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65 ;. base
a5b50 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a = infop->base;.
a5b60 20 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20 do{
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 2f 2a 20 43 6f 6e 76 65 /* Conve
a5ba0 72 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20 rt to ascii */.
a5bb0 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 *(--b
a5bc0 75 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e ufpt) = cset[lon
a5bd0 67 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20 gvalue%base];.
a5be0 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 longva
a5bf0 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f lue = longvalue/
a5c00 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 base;.
a5c10 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 }while( longvalu
a5c20 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d e>0 );. }
a5c30 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
a5c40 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 = &buf[etBUFSIZE
a5c50 2d 31 5d 2d 62 75 66 70 74 3b 0a 20 20 20 20 20 -1]-bufpt;.
a5c60 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 for(idx=preci
a5c70 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69 64 78 sion-length; idx
a5c80 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20 20 20 >0; idx--){.
a5c90 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 *(--bufpt)
a5ca0 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20 20 20 = '0';
a5cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5cc0 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61 64 20 /* Zero pad
a5cd0 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 */. }.
a5ce0 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78 20 if( prefix
a5cf0 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 70 ) *(--bufpt) = p
a5d00 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 refix;
a5d10 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69 67 6e /* Add sign
a5d20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
a5d30 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f flag_alternatefo
a5d40 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70 72 65 rm && infop->pre
a5d50 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a 20 41 fix ){ /* A
a5d60 64 64 20 22 30 22 20 6f 72 20 22 30 78 22 20 2a dd "0" or "0x" *
a5d70 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 /. cons
a5d80 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20 20 20 t char *pre;.
a5d90 20 20 20 20 20 20 20 63 68 61 72 20 78 3b 0a 20 char x;.
a5da0 20 20 20 20 20 20 20 20 20 70 72 65 20 3d 20 26 pre = &
a5db0 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d 3e 70 aPrefix[infop->p
a5dc0 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20 20 20 refix];.
a5dd0 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70 72 65 for(; (x=(*pre
a5de0 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20 2a 28 ))!=0; pre++) *(
a5df0 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a 20 20 --bufpt) = x;.
a5e00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a5e10 6c 65 6e 67 74 68 20 3d 20 26 62 75 66 5b 65 74 length = &buf[et
a5e20 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 BUFSIZE-1]-bufpt
a5e30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
a5e40 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 46 4c . case etFL
a5e50 4f 41 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 OAT:. case
a5e60 65 74 45 58 50 3a 0a 20 20 20 20 20 20 63 61 73 etEXP:. cas
a5e70 65 20 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 e etGENERIC:.
a5e80 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 20 3d realvalue =
a5e90 20 76 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c va_arg(ap,doubl
a5ea0 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 e);.#ifndef SQLI
a5eb0 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
a5ec0 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 20 20 69 _POINT. i
a5ed0 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 f( precision<0 )
a5ee0 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20 precision = 6;
a5ef0 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 64 /* Set d
a5f00 65 66 61 75 6c 74 20 70 72 65 63 69 73 69 6f 6e efault precision
a5f10 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
a5f20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 precision>etBUFS
a5f30 49 5a 45 2f 32 2d 31 30 20 29 20 70 72 65 63 69 IZE/2-10 ) preci
a5f40 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 sion = etBUFSIZE
a5f50 2f 32 2d 31 30 3b 0a 20 20 20 20 20 20 20 20 69 /2-10;. i
a5f60 66 28 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30 f( realvalue<0.0
a5f70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
a5f80 61 6c 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76 alvalue = -realv
a5f90 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 alue;.
a5fa0 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 prefix = '-';.
a5fb0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a5fc0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f if( flag_
a5fd0 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20 20 plussign )
a5fe0 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27 prefix = '+'
a5ff0 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 ;. else
a6000 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 if( flag_blanks
a6010 69 67 6e 20 29 20 20 20 20 70 72 65 66 69 78 20 ign ) prefix
a6020 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20 = ' ';.
a6030 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 else
a6040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 pr
a6050 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 efix = 0;.
a6060 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
a6070 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 xtype==etGENERIC
a6080 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 && precision>0
a6090 29 20 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23 ) precision--;.#
a60a0 69 66 20 30 0a 20 20 20 20 20 20 20 20 2f 2a 20 if 0. /*
a60b0 52 6f 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c Rounding works l
a60c0 69 6b 65 20 42 53 44 20 77 68 65 6e 20 74 68 65 ike BSD when the
a60d0 20 63 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39 39 constant 0.4999
a60e0 20 69 73 20 75 73 65 64 2e 20 20 57 69 65 72 64 is used. Wierd
a60f0 21 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 ! */. for
a6100 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 (idx=precision,
a6110 72 6f 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20 rounder=0.4999;
a6120 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f idx>0; idx--, ro
a6130 75 6e 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c under*=0.1);.#el
a6140 73 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 74 se. /* It
a6150 20 6d 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73 makes more sens
a6160 65 20 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a e to use 0.5 */.
a6170 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d for(idx=
a6180 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 precision, round
a6190 65 72 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69 er=0.5; idx>0; i
a61a0 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 dx--, rounder*=0
a61b0 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20 .1){}.#endif.
a61c0 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d if( xtype==
a61d0 65 74 46 4c 4f 41 54 20 29 20 72 65 61 6c 76 61 etFLOAT ) realva
a61e0 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a lue += rounder;.
a61f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 /* Norma
a6200 6c 69 7a 65 20 72 65 61 6c 76 61 6c 75 65 20 74 lize realvalue t
a6210 6f 20 77 69 74 68 69 6e 20 31 30 2e 30 20 3e 20 o within 10.0 >
a6220 72 65 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30 realvalue >= 1.0
a6230 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 78 70 20 */. exp
a6240 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 = 0;. if(
a6250 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 72 65 sqlite3IsNaN(re
a6260 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 alvalue) ){.
a6270 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 4e bufpt = "N
a6280 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20 6c aN";. l
a6290 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20 20 ength = 3;.
a62a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a62b0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
a62c0 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30 20 ( realvalue>0.0
a62d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 ){. whi
a62e0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 le( realvalue>=1
a62f0 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30 20 e32 && exp<=350
a6300 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 ){ realvalue *=
a6310 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b 20 1e-32; exp+=32;
a6320 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c }. whil
a6330 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 65 e( realvalue>=1e
a6340 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 8 && exp<=350 ){
a6350 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 realvalue *= 1e
a6360 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20 20 -8; exp+=8; }.
a6370 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 while( r
a6380 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 26 ealvalue>=10.0 &
a6390 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 & exp<=350 ){ re
a63a0 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20 alvalue *= 0.1;
a63b0 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 exp++; }.
a63c0 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 while( realva
a63d0 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61 6c lue<1e-8 ){ real
a63e0 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65 78 value *= 1e8; ex
a63f0 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 20 p-=8; }.
a6400 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c while( realval
a6410 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76 61 ue<1.0 ){ realva
a6420 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78 70 lue *= 10.0; exp
a6430 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 --; }.
a6440 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a 20 if( exp>350 ){.
a6450 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 if( p
a6460 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20 20 refix=='-' ){.
a6470 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 bufp
a6480 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20 20 t = "-Inf";.
a6490 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
a64a0 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29 7b ( prefix=='+' ){
a64b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 . b
a64c0 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a 20 ufpt = "+Inf";.
a64d0 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }else
a64e0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
a64f0 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a 20 bufpt = "Inf";.
a6500 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
a6510 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 length
a6520 3d 20 73 74 72 6c 65 6e 28 62 75 66 70 74 29 3b = strlen(bufpt);
a6530 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 . bre
a6540 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ak;. }.
a6550 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a6560 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 bufpt = buf;.
a6570 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 /*.
a6580 20 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c ** If the fiel
a6590 64 20 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 d type is etGENE
a65a0 52 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 RIC, then conver
a65b0 74 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 t to either etEX
a65c0 50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 P. ** or
a65d0 65 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 etFLOAT, as appr
a65e0 6f 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 opriate..
a65f0 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67 */. flag
a6600 5f 65 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74 _exp = xtype==et
a6610 45 58 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 EXP;. if(
a6620 20 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 xtype!=etFLOAT
a6630 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 ){. rea
a6640 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 lvalue += rounde
a6650 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 r;. if(
a6660 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 realvalue>=10.0
a6670 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d ){ realvalue *=
a6680 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 0.1; exp++; }.
a6690 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a66a0 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 if( xtype==etGE
a66b0 4e 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 NERIC ){.
a66c0 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 flag_rtz = !f
a66d0 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 lag_alternatefor
a66e0 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 m;. if(
a66f0 20 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 exp<-4 || exp>p
a6700 72 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 recision ){.
a6710 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 xtype =
a6720 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 etEXP;.
a6730 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a6740 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 precision =
a6750 70 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b precision - exp;
a6760 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 . xty
a6770 70 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 pe = etFLOAT;.
a6780 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a6790 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a67a0 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b flag_rtz = 0;
a67b0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a67c0 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 if( xtype==et
a67d0 45 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 EXP ){.
a67e0 20 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 e2 = 0;.
a67f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a6800 20 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20 e2 = exp;.
a6810 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73 }. ns
a6820 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 d = 0;. f
a6830 6c 61 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73 lag_dp = (precis
a6840 69 6f 6e 3e 30 29 20 7c 20 66 6c 61 67 5f 61 6c ion>0) | flag_al
a6850 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c ternateform | fl
a6860 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 ag_altform2;.
a6870 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e /* The sign
a6880 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65 in front of the
a6890 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 number */.
a68a0 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b if( prefix ){
a68b0 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 . *(buf
a68c0 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a pt++) = prefix;.
a68d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a68e0 20 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f /* Digits prio
a68f0 72 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c r to the decimal
a6900 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 point */.
a6910 20 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20 if( e2<0 ){.
a6920 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
a6930 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 +) = '0';.
a6940 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a6950 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20 for(; e2>=0;
a6960 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 e2--){.
a6970 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
a6980 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 et_getdigit(&rea
a6990 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 lvalue,&nsd);.
a69a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a69b0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 }. /* T
a69c0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 he decimal point
a69d0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
a69e0 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 flag_dp ){.
a69f0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
a6a00 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d = '.';. }
a6a10 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20 . /* "0"
a6a20 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 digits after the
a6a30 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62 decimal point b
a6a40 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 ut before the fi
a6a50 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 rst. ** s
a6a60 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 ignificant digit
a6a70 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a of the number *
a6a80 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32 /. for(e2
a6a90 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73 ++; e2<0; precis
a6aa0 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20 ion--, e2++){.
a6ab0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
a6ac0 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20 precision>0 );.
a6ad0 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
a6ae0 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 ++) = '0';.
a6af0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
a6b00 53 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 Significant digi
a6b10 74 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63 ts after the dec
a6b20 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 imal point */.
a6b30 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72 while( (pr
a6b40 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a ecision--)>0 ){.
a6b50 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a6b60 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 t++) = et_getdig
a6b70 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e it(&realvalue,&n
a6b80 73 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 sd);. }.
a6b90 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 /* Remove
a6ba0 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 trailing zeros
a6bb0 61 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e and the "." if n
a6bc0 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20 o digits follow
a6bd0 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20 the "." */.
a6be0 20 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20 if( flag_rtz
a6bf0 26 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 && flag_dp ){.
a6c00 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62 while( b
a6c10 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 ufpt[-1]=='0' )
a6c20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a *(--bufpt) = 0;.
a6c30 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
a6c40 28 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20 ( bufpt>buf );.
a6c50 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 if( buf
a6c60 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20 pt[-1]=='.' ){.
a6c70 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 if( f
a6c80 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a lag_altform2 ){.
a6c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 *(
a6ca0 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a bufpt++) = '0';.
a6cb0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 }els
a6cc0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e{.
a6cd0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b *(--bufpt) = 0;
a6ce0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
a6cf0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
a6d00 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
a6d10 41 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73 Add the "eNNN" s
a6d20 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20 uffix */.
a6d30 20 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c if( flag_exp ||
a6d40 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b xtype==etEXP ){
a6d50 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 . *(buf
a6d60 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b pt++) = aDigits[
a6d70 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b infop->charset];
a6d80 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 . if( e
a6d90 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 xp<0 ){.
a6da0 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a6db0 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 '-'; exp = -exp
a6dc0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
a6dd0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a e{. *
a6de0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b (bufpt++) = '+';
a6df0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
a6e00 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d if( exp>=
a6e10 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 100 ){.
a6e20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
a6e30 28 65 78 70 2f 31 30 30 29 2b 27 30 27 3b 20 20 (exp/100)+'0';
a6e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a6e50 20 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 100's digit */.
a6e60 20 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 exp
a6e70 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 %= 100;.
a6e80 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 }. *(
a6e90 62 75 66 70 74 2b 2b 29 20 3d 20 65 78 70 2f 31 bufpt++) = exp/1
a6ea0 30 2b 27 30 27 3b 20 20 20 20 20 20 20 20 20 20 0+'0';
a6eb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 /* 10
a6ec0 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 's digit */.
a6ed0 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 *(bufpt++)
a6ee0 20 3d 20 65 78 70 25 31 30 2b 27 30 27 3b 20 20 = exp%10+'0';
a6ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6f00 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 /* 1's digit
a6f10 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 */. }.
a6f20 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b *bufpt = 0;
a6f30 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 .. /* The
a6f40 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 converted numbe
a6f50 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e r is in buf[] an
a6f60 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 d zero terminate
a6f70 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 d. Output it..
a6f80 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 ** Note th
a6f90 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 at the number is
a6fa0 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 in the usual or
a6fb0 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 der, not reverse
a6fc0 64 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 d as with.
a6fd0 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e ** integer con
a6fe0 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 versions. */.
a6ff0 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 62 75 length = bu
a7000 66 70 74 2d 62 75 66 3b 0a 20 20 20 20 20 20 20 fpt-buf;.
a7010 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a 20 bufpt = buf;..
a7020 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 61 /* Specia
a7030 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c 65 61 l case: Add lea
a7040 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20 74 68 ding zeros if th
a7050 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 66 e flag_zeropad f
a7060 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20 20 2a lag is. *
a7070 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61 72 65 * set and we are
a7080 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74 69 66 not left justif
a7090 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 ied */. i
a70a0 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 f( flag_zeropad
a70b0 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 73 && !flag_leftjus
a70c0 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68 20 3c tify && length <
a70d0 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20 20 20 width){.
a70e0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
a70f0 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20 77 int nPad = w
a7100 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a 20 idth - length;.
a7110 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 77 for(i=w
a7120 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20 69 idth; i>=nPad; i
a7130 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 --){.
a7140 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66 70 bufpt[i] = bufp
a7150 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20 20 t[i-nPad];.
a7160 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
a7170 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30 3b 0a i = prefix!=0;.
a7180 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 while(
a7190 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70 74 5b nPad-- ) bufpt[
a71a0 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20 i++] = '0';.
a71b0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 length = w
a71c0 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a idth;. }.
a71d0 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 62 #endif. b
a71e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
a71f0 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20 20 20 etSIZE:.
a7200 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 *(va_arg(ap,int
a7210 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e 43 *)) = pAccum->nC
a7220 68 61 72 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e har;. len
a7230 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b gth = width = 0;
a7240 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
a7250 20 20 20 20 20 20 63 61 73 65 20 65 74 50 45 52 case etPER
a7260 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62 75 CENT:. bu
a7270 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20 20 f[0] = '%';.
a7280 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b bufpt = buf;
a7290 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
a72a0 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65 = 1;. bre
a72b0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 ak;. case e
a72c0 74 43 48 41 52 58 3a 0a 20 20 20 20 20 20 20 20 tCHARX:.
a72d0 63 20 3d 20 62 75 66 5b 30 5d 20 3d 20 76 61 5f c = buf[0] = va_
a72e0 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 arg(ap,int);.
a72f0 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 if( precisi
a7300 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 on>=0 ){.
a7310 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 69 64 for(idx=1; id
a7320 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 64 78 x<precision; idx
a7330 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d 20 63 ++) buf[idx] = c
a7340 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 ;. leng
a7350 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a th = precision;.
a7360 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
a7370 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 length
a7380 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 =1;. }.
a7390 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 bufpt = bu
a73a0 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b f;. break
a73b0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 ;. case etS
a73c0 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 TRING:. cas
a73d0 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 e etDYNSTRING:.
a73e0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 76 bufpt = v
a73f0 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b a_arg(ap,char*);
a7400 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 . if( buf
a7410 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 pt==0 ){.
a7420 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 bufpt = "";.
a7430 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
a7440 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 xtype==etDYNSTR
a7450 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ING ){.
a7460 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74 3b zExtra = bufpt;
a7470 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a7480 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e if( precision
a7490 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 >=0 ){.
a74a0 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c for(length=0; l
a74b0 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 ength<precision
a74c0 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d && bufpt[length]
a74d0 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 ; length++){}.
a74e0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a74f0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a7500 73 74 72 6c 65 6e 28 62 75 66 70 74 29 3b 0a 20 strlen(bufpt);.
a7510 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a7520 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
a7530 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 3a 0a se etSQLESCAPE:.
a7540 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c case etSQL
a7550 45 53 43 41 50 45 32 3a 0a 20 20 20 20 20 20 63 ESCAPE2:. c
a7560 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 ase etSQLESCAPE3
a7570 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 : {. int
a7580 69 2c 20 6a 2c 20 6e 2c 20 63 68 2c 20 69 73 6e i, j, n, ch, isn
a7590 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 ull;. int
a75a0 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 20 20 needQuote;.
a75b0 20 20 20 20 63 68 61 72 20 71 20 3d 20 28 28 78 char q = ((x
a75c0 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 type==etSQLESCAP
a75d0 45 33 29 3f 27 22 27 3a 27 5c 27 27 29 3b 20 20 E3)?'"':'\'');
a75e0 20 2f 2a 20 51 75 6f 74 65 20 63 68 61 72 61 63 /* Quote charac
a75f0 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 ter */. c
a7600 68 61 72 20 2a 65 73 63 61 72 67 20 3d 20 76 61 har *escarg = va
a7610 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a _arg(ap,char*);.
a7620 20 20 20 20 20 20 20 20 69 73 6e 75 6c 6c 20 3d isnull =
a7630 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20 20 20 20 escarg==0;.
a7640 20 20 20 20 69 66 28 20 69 73 6e 75 6c 6c 20 29 if( isnull )
a7650 20 65 73 63 61 72 67 20 3d 20 28 78 74 79 70 65 escarg = (xtype
a7660 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32 20 3f ==etSQLESCAPE2 ?
a7670 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c 4c "NULL" : "(NULL
a7680 29 22 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 )");. for
a7690 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 73 63 61 (i=n=0; (ch=esca
a76a0 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b rg[i])!=0; i++){
a76b0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 . if( c
a76c0 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a 20 20 20 h==q ) n++;.
a76d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e }. n
a76e0 65 65 64 51 75 6f 74 65 20 3d 20 21 69 73 6e 75 eedQuote = !isnu
a76f0 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d 65 74 53 ll && xtype==etS
a7700 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 20 20 20 QLESCAPE2;.
a7710 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 20 2b 20 n += i + 1 +
a7720 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a 20 20 20 needQuote*2;.
a7730 20 20 20 20 20 69 66 28 20 6e 3e 65 74 42 55 46 if( n>etBUF
a7740 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 SIZE ){.
a7750 20 20 62 75 66 70 74 20 3d 20 7a 45 78 74 72 61 bufpt = zExtra
a7760 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
a7770 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ( n );.
a7780 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b if( bufpt==0 ){
a7790 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 41 63 . pAc
a77a0 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 cum->mallocFaile
a77b0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 d = 1;.
a77c0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 return;.
a77d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
a77e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a77f0 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 bufpt = buf;.
a7800 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6a }. j
a7810 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 = 0;. if
a7820 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62 75 ( needQuote ) bu
a7830 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 fpt[j++] = q;.
a7840 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 for(i=0; (
a7850 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 ch=escarg[i])!=0
a7860 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
a7870 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 bufpt[j++] = c
a7880 68 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 h;. if(
a7890 20 63 68 3d 3d 71 20 29 20 62 75 66 70 74 5b 6a ch==q ) bufpt[j
a78a0 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 ++] = ch;.
a78b0 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
a78c0 6e 65 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 needQuote ) bufp
a78d0 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 t[j++] = q;.
a78e0 20 20 20 20 62 75 66 70 74 5b 6a 5d 20 3d 20 30 bufpt[j] = 0
a78f0 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 ;. length
a7900 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 2f 2a = j;. /*
a7910 20 54 68 65 20 70 72 65 63 69 73 69 6f 6e 20 69 The precision i
a7920 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20 25 71 20 s ignored on %q
a7930 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20 20 20 20 and %Q */.
a7940 20 20 2f 2a 20 69 66 28 20 70 72 65 63 69 73 69 /* if( precisi
a7950 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63 69 73 69 on>=0 && precisi
a7960 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c 65 6e 67 on<length ) leng
a7970 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 20 th = precision;
a7980 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b */. break
a7990 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
a79a0 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a 20 7b 0a case etTOKEN: {.
a79b0 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 Token *p
a79c0 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72 67 28 61 Token = va_arg(a
a79d0 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20 20 20 20 p, Token*);.
a79e0 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 if( pToken )
a79f0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
a7a00 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a7a10 64 28 70 41 63 63 75 6d 2c 20 28 63 6f 6e 73 74 d(pAccum, (const
a7a20 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a char*)pToken->z
a7a30 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 , pToken->n);.
a7a40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a7a50 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d length = width =
a7a60 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 0;. brea
a7a70 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
a7a80 20 63 61 73 65 20 65 74 53 52 43 4c 49 53 54 3a case etSRCLIST:
a7a90 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 {. SrcLi
a7aa0 73 74 20 2a 70 53 72 63 20 3d 20 76 61 5f 61 72 st *pSrc = va_ar
a7ab0 67 28 61 70 2c 20 53 72 63 4c 69 73 74 2a 29 3b g(ap, SrcList*);
a7ac0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b 20 3d . int k =
a7ad0 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 va_arg(ap, int)
a7ae0 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 ;. struct
a7af0 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
a7b00 49 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b Item = &pSrc->a[
a7b10 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 k];. asse
a7b20 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b 3c 70 53 rt( k>=0 && k<pS
a7b30 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 rc->nSrc );.
a7b40 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a if( pItem->z
a7b50 44 61 74 61 62 61 73 65 20 29 7b 0a 20 20 20 20 Database ){.
a7b60 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
a7b70 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
a7b80 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 um, pItem->zData
a7b90 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20 20 20 20 base, -1);.
a7ba0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 sqlite3StrA
a7bb0 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 ccumAppend(pAccu
a7bc0 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20 20 20 20 m, ".", 1);.
a7bd0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 }. sq
a7be0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
a7bf0 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 end(pAccum, pIte
a7c00 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 m->zName, -1);.
a7c10 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a7c20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 width = 0;.
a7c30 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a7c40 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20 73 77 }. }/* End sw
a7c50 69 74 63 68 20 6f 76 65 72 20 74 68 65 20 66 6f itch over the fo
a7c60 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20 20 20 rmat type */.
a7c70 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 /*. ** The t
a7c80 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6e 76 65 ext of the conve
a7c90 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 65 64 rsion is pointed
a7ca0 20 74 6f 20 62 79 20 22 62 75 66 70 74 22 20 61 to by "bufpt" a
a7cb0 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 22 6c 65 nd is. ** "le
a7cc0 6e 67 74 68 22 20 63 68 61 72 61 63 74 65 72 73 ngth" characters
a7cd0 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 65 6c long. The fiel
a7ce0 64 20 77 69 64 74 68 20 69 73 20 22 77 69 64 74 d width is "widt
a7cf0 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a 20 74 h". Do. ** t
a7d00 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 2a he output.. *
a7d10 2f 0a 20 20 20 20 69 66 28 20 21 66 6c 61 67 5f /. if( !flag_
a7d20 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 leftjustify ){.
a7d30 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e register in
a7d40 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 t nspace;.
a7d50 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c nspace = width-l
a7d60 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 ength;. if(
a7d70 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 nspace>0 ){.
a7d80 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 appendSpace
a7d90 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 (pAccum, nspace)
a7da0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
a7db0 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e 30 if( length>0
a7dc0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
a7dd0 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
a7de0 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 6c pAccum, bufpt, l
a7df0 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20 20 ength);. }.
a7e00 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74 6a if( flag_leftj
a7e10 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 ustify ){.
a7e20 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 register int nsp
a7e30 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 ace;. nspac
a7e40 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 e = width-length
a7e50 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 ;. if( nspa
a7e60 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ce>0 ){.
a7e70 61 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63 appendSpace(pAcc
a7e80 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20 um, nspace);.
a7e90 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
a7ea0 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20 20 20 f( zExtra ){.
a7eb0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
a7ec0 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20 zExtra);. }.
a7ed0 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f 6f }/* End for loo
a7ee0 70 20 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61 p over the forma
a7ef0 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f 2a t string */.} /*
a7f00 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f 6e End of function
a7f10 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e */../*.** Appen
a7f20 64 20 4e 20 62 79 74 65 73 20 6f 66 20 74 65 78 d N bytes of tex
a7f30 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 20 t from z to the
a7f40 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e StrAccum object.
a7f50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a7f60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
a7f70 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 trAccumAppend(St
a7f80 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74 rAccum *p, const
a7f90 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e 29 char *z, int N)
a7fa0 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 {. if( p->tooBi
a7fb0 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 g | p->mallocFai
a7fc0 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 led ){. retur
a7fd0 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3c 30 n;. }. if( N<0
a7fe0 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 74 72 6c ){. N = strl
a7ff0 65 6e 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 en(z);. }. if(
a8000 20 4e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 N==0 ){. ret
a8010 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 urn;. }. if( p
a8020 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e ->nChar+N >= p->
a8030 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 nAlloc ){. ch
a8040 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 ar *zNew;. if
a8050 28 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 ( !p->useMalloc
a8060 29 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 ){. p->tooB
a8070 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 ig = 1;. N
a8080 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d = p->nAlloc - p-
a8090 3e 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 >nChar - 1;.
a80a0 20 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 if( N<=0 ){.
a80b0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
a80c0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
a80d0 0a 20 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 . i64 szNew
a80e0 20 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 = p->nChar;.
a80f0 20 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 szNew += N +
a8100 31 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 1;. if( szN
a8110 65 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 ew > p->mxAlloc
a8120 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
a8130 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 e3StrAccumReset(
a8140 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 p);. p->t
a8150 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 ooBig = 1;.
a8160 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 return;.
a8170 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a8180 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 7a 4e 65 p->nAlloc = szNe
a8190 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 w;. }.
a81a0 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 zNew = sqlite3D
a81b0 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 bMallocRaw(p->db
a81c0 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 , p->nAlloc );.
a81d0 20 20 20 20 20 69 66 28 20 7a 4e 65 77 20 29 7b if( zNew ){
a81e0 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
a81f0 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c 20 zNew, p->zText,
a8200 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 20 p->nChar);.
a8210 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
a8220 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 umReset(p);.
a8230 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a p->zText = z
a8240 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 New;. }else
a8250 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c {. p->mal
a8260 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
a8270 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
a8280 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a rAccumReset(p);.
a8290 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a return;.
a82a0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
a82b0 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a }. memcpy(&p->z
a82c0 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 2c 20 Text[p->nChar],
a82d0 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 z, N);. p->nCha
a82e0 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a r += N;.}../*.**
a82f0 20 46 69 6e 69 73 68 20 6f 66 66 20 61 20 73 74 Finish off a st
a8300 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e 67 20 73 ring by making s
a8310 75 72 65 20 69 74 20 69 73 20 7a 65 72 6f 2d 74 ure it is zero-t
a8320 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20 52 65 erminated..** Re
a8330 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
a8340 6f 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 o the resulting
a8350 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 string. Return
a8360 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 a NULL.** pointe
a8370 72 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 r if any kind of
a8380 20 65 72 72 6f 72 20 77 61 73 20 65 6e 63 6f 75 error was encou
a8390 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 ntered..*/.SQLIT
a83a0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
a83b0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
a83c0 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d 20 2a inish(StrAccum *
a83d0 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 p){. if( p->zTe
a83e0 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 54 65 xt ){. p->zTe
a83f0 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d 20 30 xt[p->nChar] = 0
a8400 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 65 ;. if( p->use
a8410 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a 54 65 Malloc && p->zTe
a8420 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a xt==p->zBase ){.
a8430 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d p->zText =
a8440 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
a8450 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 43 Raw(p->db, p->nC
a8460 68 61 72 2b 31 20 29 3b 0a 20 20 20 20 20 20 69 har+1 );. i
a8470 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 f( p->zText ){.
a8480 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2d memcpy(p-
a8490 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 61 73 65 >zText, p->zBase
a84a0 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0a 20 , p->nChar+1);.
a84b0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a84c0 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->mallocFai
a84d0 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d led = 1;. }
a84e0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
a84f0 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a 7d 0a urn p->zText;.}.
a8500 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 61 6e 20 ./*.** Reset an
a8510 53 74 72 41 63 63 75 6d 20 73 74 72 69 6e 67 2e StrAccum string.
a8520 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 61 Reclaim all ma
a8530 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a lloced memory..*
a8540 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
a8550 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
a8560 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 AccumReset(StrAc
a8570 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 cum *p){. if( p
a8580 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a 42 61 73 ->zText!=p->zBas
a8590 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
a85a0 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d DbFree(p->db, p-
a85b0 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a 20 20 70 >zText);. }. p
a85c0 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 7d 0a 0a ->zText = 0;.}..
a85d0 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /*.** Initialize
a85e0 20 61 20 73 74 72 69 6e 67 20 61 63 63 75 6d 75 a string accumu
a85f0 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f lator.*/.SQLITE_
a8600 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a8610 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
a8620 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 68 (StrAccum *p, ch
a8630 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e 74 20 6e ar *zBase, int n
a8640 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20 70 2d 3e , int mx){. p->
a8650 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42 61 73 65 zText = p->zBase
a8660 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70 2d 3e 64 = zBase;. p->d
a8670 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43 68 61 b = 0;. p->nCha
a8680 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 6c 6c r = 0;. p->nAll
a8690 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d 78 41 oc = n;. p->mxA
a86a0 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 2d 3e lloc = mx;. p->
a86b0 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 useMalloc = 1;.
a86c0 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 3b 0a p->tooBig = 0;.
a86d0 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 p->mallocFaile
a86e0 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 d = 0;.}../*.**
a86f0 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 Print into memor
a8700 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 y obtained from
a8710 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 sqliteMalloc().
a8720 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 Use the interna
a8730 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f l.** %-conversio
a8740 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f n extensions..*/
a8750 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a8760 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50 char *sqlite3VMP
a8770 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 rintf(sqlite3 *d
a8780 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
a8790 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 Format, va_list
a87a0 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a ap){. char *z;.
a87b0 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c char zBase[SQL
a87c0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 ITE_PRINT_BUF_SI
a87d0 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 ZE];. StrAccum
a87e0 61 63 63 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 acc;. sqlite3St
a87f0 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c rAccumInit(&acc,
a8800 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a zBase, sizeof(z
a8810 42 61 73 65 29 2c 0a 20 20 20 20 20 20 20 20 20 Base),.
a8820 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 20 db
a8830 3f 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ? db->aLimit[SQL
a8840 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
a8850 5d 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c ] : SQLITE_MAX_L
a8860 45 4e 47 54 48 29 3b 0a 20 20 61 63 63 2e 64 62 ENGTH);. acc.db
a8870 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 = db;. sqlite3
a8880 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31 VXPrintf(&acc, 1
a8890 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
a88a0 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 z = sqlite3Str
a88b0 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 AccumFinish(&acc
a88c0 29 3b 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c );. if( acc.mal
a88d0 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 64 62 20 locFailed && db
a88e0 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f ){. db->mallo
a88f0 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d cFailed = 1;. }
a8900 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a . return z;.}..
a8910 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f /*.** Print into
a8920 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
a8930 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c from sqliteMall
a8940 6f 63 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 oc(). Use the i
a8950 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e nternal.** %-con
a8960 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f version extensio
a8970 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ns..*/.SQLITE_PR
a8980 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
a8990 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 te3MPrintf(sqlit
a89a0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
a89b0 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
a89c0 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
a89d0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 . char *z;. va
a89e0 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
a89f0 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 at);. z = sqlit
a8a00 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a e3VMPrintf(db, z
a8a10 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
a8a20 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 a_end(ap);. ret
a8a30 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn z;.}../*.**
a8a40 4c 69 6b 65 20 73 71 6c 69 74 65 33 4d 50 72 69 Like sqlite3MPri
a8a50 6e 74 66 28 29 2c 20 62 75 74 20 63 61 6c 6c 20 ntf(), but call
a8a60 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 sqlite3DbFree()
a8a70 6f 6e 20 7a 53 74 72 20 61 66 74 65 72 20 66 6f on zStr after fo
a8a80 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 rmatting.** the
a8a90 73 74 72 69 6e 67 20 61 6e 64 20 62 65 66 6f 72 string and befor
a8aa0 65 20 72 65 74 75 72 6e 6e 69 6e 67 2e 20 20 54 e returnning. T
a8ab0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 his routine is i
a8ac0 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 ntended to be us
a8ad0 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 ed.** to modify
a8ae0 61 6e 20 65 78 69 73 74 69 6e 67 20 73 74 72 69 an existing stri
a8af0 6e 67 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 ng. For example
a8b00 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20 :.**.** x
a8b10 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 = sqlite3MPrintf
a8b20 28 64 62 2c 20 78 2c 20 22 70 72 65 66 69 78 20 (db, x, "prefix
a8b30 25 73 20 73 75 66 66 69 78 22 2c 20 78 29 3b 0a %s suffix", x);.
a8b40 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 **.*/.SQLITE_PRI
a8b50 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
a8b60 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c 69 74 e3MAppendf(sqlit
a8b70 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 7a 53 e3 *db, char *zS
a8b80 74 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tr, const char *
a8b90 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
a8ba0 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 va_list ap;. c
a8bb0 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 har *z;. va_sta
a8bc0 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b rt(ap, zFormat);
a8bd0 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d . z = sqlite3VM
a8be0 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d Printf(db, zForm
a8bf0 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e at, ap);. va_en
a8c00 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 d(ap);. sqlite3
a8c10 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 72 29 DbFree(db, zStr)
a8c20 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a ;. return z;.}.
a8c30 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 ./*.** Print int
a8c40 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 o memory obtaine
a8c50 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d d from sqlite3_m
a8c60 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69 74 20 74 alloc(). Omit t
a8c70 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 he internal.** %
a8c80 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 -conversion exte
a8c90 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 nsions..*/.SQLIT
a8ca0 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 E_API char *sqli
a8cb0 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e te3_vmprintf(con
a8cc0 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
a8cd0 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 , va_list ap){.
a8ce0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 char *z;. char
a8cf0 20 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 zBase[SQLITE_PR
a8d00 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 INT_BUF_SIZE];.
a8d10 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 23 StrAccum acc;.#
a8d20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
a8d30 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 IT_AUTOINIT. if
a8d40 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 ( sqlite3_initia
a8d50 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 lize() ) return
a8d60 30 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 0;.#endif. sqli
a8d70 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 te3StrAccumInit(
a8d80 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a &acc, zBase, siz
a8d90 65 6f 66 28 7a 42 61 73 65 29 2c 20 53 51 4c 49 eof(zBase), SQLI
a8da0 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b 0a TE_MAX_LENGTH);.
a8db0 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 sqlite3VXPrint
a8dc0 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d f(&acc, 0, zForm
a8dd0 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 at, ap);. z = s
a8de0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 qlite3StrAccumFi
a8df0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 nish(&acc);. re
a8e00 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn z;.}../*.**
a8e10 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f Print into memo
a8e20 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
a8e30 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
a8e40 29 28 29 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 )(). Omit the i
a8e50 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e nternal.** %-con
a8e60 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f version extensio
a8e70 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ns..*/.SQLITE_AP
a8e80 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f I char *sqlite3_
a8e90 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 mprintf(const ch
a8ea0 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
a8eb0 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
a8ec0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 23 69 66 6e . char *z;.#ifn
a8ed0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
a8ee0 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 AUTOINIT. if( s
a8ef0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
a8f00 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a e() ) return 0;.
a8f10 23 65 6e 64 69 66 0a 20 20 76 61 5f 73 74 61 72 #endif. va_star
a8f20 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a t(ap, zFormat);.
a8f30 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d z = sqlite3_vm
a8f40 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20 printf(zFormat,
a8f50 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
a8f60 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d );. return z;.}
a8f70 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ../*.** sqlite3_
a8f80 73 6e 70 72 69 6e 74 66 28 29 20 77 6f 72 6b 73 snprintf() works
a8f90 20 6c 69 6b 65 20 73 6e 70 72 69 6e 74 66 28 29 like snprintf()
a8fa0 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 except that it
a8fb0 69 67 6e 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63 ignores the.** c
a8fc0 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65 urrent locale se
a8fd0 74 74 69 6e 67 73 2e 20 20 54 68 69 73 20 69 73 ttings. This is
a8fe0 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 53 important for S
a8ff0 51 4c 69 74 65 20 62 65 63 61 75 73 65 20 77 65 QLite because we
a9000 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61 62 6c 65 .** are not able
a9010 20 74 6f 20 75 73 65 20 61 20 22 2c 22 20 61 73 to use a "," as
a9020 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 the decimal poi
a9030 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 22 nt in place of "
a9040 2e 22 20 61 73 0a 2a 2a 20 73 70 65 63 69 66 69 ." as.** specifi
a9050 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c ed by some local
a9060 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 es..*/.SQLITE_AP
a9070 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f I char *sqlite3_
a9080 73 6e 70 72 69 6e 74 66 28 69 6e 74 20 6e 2c 20 snprintf(int n,
a9090 63 68 61 72 20 2a 7a 42 75 66 2c 20 63 6f 6e 73 char *zBuf, cons
a90a0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
a90b0 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a ...){. char *z
a90c0 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a ;. va_list ap;.
a90d0 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a StrAccum acc;.
a90e0 0a 20 20 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 . if( n<=0 ){.
a90f0 20 20 20 72 65 74 75 72 6e 20 7a 42 75 66 3b 0a return zBuf;.
a9100 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72 }. sqlite3Str
a9110 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 AccumInit(&acc,
a9120 7a 42 75 66 2c 20 6e 2c 20 30 29 3b 0a 20 20 61 zBuf, n, 0);. a
a9130 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 cc.useMalloc = 0
a9140 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c ;. va_start(ap,
a9150 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 zFormat);. sqli
a9160 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 te3VXPrintf(&acc
a9170 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 , 0, zFormat, ap
a9180 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b );. va_end(ap);
a9190 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 . z = sqlite3St
a91a0 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 rAccumFinish(&ac
a91b0 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a c);. return z;.
a91c0 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 }..#if defined(S
a91d0 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a QLITE_DEBUG)./*.
a91e0 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20 6f 66 20 ** A version of
a91f0 70 72 69 6e 74 66 28 29 20 74 68 61 74 20 75 6e printf() that un
a9200 64 65 72 73 74 61 6e 64 73 20 25 6c 6c 64 2e 20 derstands %lld.
a9210 20 55 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 Used for debugg
a9220 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 70 72 69 6e ing..** The prin
a9230 74 66 28 29 20 62 75 69 6c 74 20 69 6e 74 6f 20 tf() built into
a9240 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 some versions of
a9250 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f windows does no
a9260 74 20 75 6e 64 65 72 73 74 61 6e 64 20 25 6c 6c t understand %ll
a9270 64 0a 2a 2a 20 61 6e 64 20 73 65 67 66 61 75 6c d.** and segfaul
a9280 74 73 20 69 66 20 79 6f 75 20 67 69 76 65 20 69 ts if you give i
a9290 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e t a long long in
a92a0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
a92b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a92c0 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 3DebugPrintf(con
a92d0 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
a92e0 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 , ...){. va_lis
a92f0 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 75 6d t ap;. StrAccum
a9300 20 61 63 63 3b 0a 20 20 63 68 61 72 20 7a 42 75 acc;. char zBu
a9310 66 5b 35 30 30 5d 3b 0a 20 20 73 71 6c 69 74 65 f[500];. sqlite
a9320 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 3StrAccumInit(&a
a9330 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a 65 6f 66 cc, zBuf, sizeof
a9340 28 7a 42 75 66 29 2c 20 30 29 3b 0a 20 20 61 63 (zBuf), 0);. ac
a9350 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b c.useMalloc = 0;
a9360 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a . va_start(ap,z
a9370 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 Format);. sqlit
a9380 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c e3VXPrintf(&acc,
a9390 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 0, zFormat, ap)
a93a0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
a93b0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
a93c0 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 mFinish(&acc);.
a93d0 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 74 2c fprintf(stdout,
a93e0 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 66 "%s", zBuf);. f
a93f0 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 7d flush(stdout);.}
a9400 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
a9410 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
a9420 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a printf.c *******
a9430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9450 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
a9460 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
a9470 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a ile random.c ***
a9480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a94a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
a94b0 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 001 September 15
a94c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
a94d0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
a94e0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
a94f0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
a9500 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
a9510 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
a9520 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
a9530 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
a9540 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
a9550 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
a9560 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
a9570 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
a9580 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
a9590 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
a95a0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
a95b0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
a95c0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
a95d0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
a95e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a95f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
a9620 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
a9630 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 69 6d tains code to im
a9640 70 6c 65 6d 65 6e 74 20 61 20 70 73 65 75 64 6f plement a pseudo
a9650 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a 2a -random number.*
a9660 2a 20 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e * generator (PRN
a9670 47 29 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a G) for SQLite..*
a9680 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 *.** Random numb
a9690 65 72 73 20 61 72 65 20 75 73 65 64 20 62 79 20 ers are used by
a96a0 73 6f 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 some of the data
a96b0 62 61 73 65 20 62 61 63 6b 65 6e 64 73 20 69 6e base backends in
a96c0 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 67 65 6e order.** to gen
a96d0 65 72 61 74 65 20 72 61 6e 64 6f 6d 20 69 6e 74 erate random int
a96e0 65 67 65 72 20 6b 65 79 73 20 66 6f 72 20 74 61 eger keys for ta
a96f0 62 6c 65 73 20 6f 72 20 72 61 6e 64 6f 6d 20 66 bles or random f
a9700 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 ilenames..**.**
a9710 24 49 64 3a 20 72 61 6e 64 6f 6d 2e 63 2c 76 20 $Id: random.c,v
a9720 31 2e 32 37 20 32 30 30 38 2f 31 30 2f 30 37 20 1.27 2008/10/07
a9730 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45 78 70 15:25:48 drh Exp
a9740 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 $.*/.../* All t
a9750 68 72 65 61 64 73 20 73 68 61 72 65 20 61 20 73 hreads share a s
a9760 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d ingle random num
a9770 62 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a ber generator..*
a9780 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 * This structure
a9790 20 69 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 is the current
a97a0 73 74 61 74 65 20 6f 66 20 74 68 65 20 67 65 6e state of the gen
a97b0 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 erator..*/.stati
a97c0 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 c SQLITE_WSD str
a97d0 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 uct sqlite3PrngT
a97e0 79 70 65 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 ype {. unsigned
a97f0 20 63 68 61 72 20 69 73 49 6e 69 74 3b 20 20 20 char isInit;
a9800 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
a9810 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f f initialized */
a9820 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
a9830 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 i, j;
a9840 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 /* State varia
a9850 62 6c 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e bles */. unsign
a9860 65 64 20 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 ed char s[256];
a9870 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 /* Stat
a9880 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d e variables */.}
a9890 20 73 71 6c 69 74 65 33 50 72 6e 67 20 3d 20 7b sqlite3Prng = {
a98a0 20 30 2c 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 0, };../*.** Ge
a98b0 74 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 t a single 8-bit
a98c0 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 random value fr
a98d0 6f 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e om the RC4 PRNG.
a98e0 20 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d The Mutex.** m
a98f0 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c ust be held whil
a9900 65 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 e executing this
a9910 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
a9920 57 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 Why not just use
a9930 20 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f a library rando
a9940 6d 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 m generator like
a9950 20 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 lrand48() for t
a9960 68 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 his?.** Because
a9970 74 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 the OP_NewRowid
a9980 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 opcode in the VD
a9990 42 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 BE depends on ha
a99a0 76 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 ving a very.** g
a99b0 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 ood source of ra
a99c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 ndom numbers. T
a99d0 68 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 he lrand48() lib
a99e0 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 rary function ma
a99f0 79 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f y.** well be goo
a9a00 64 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d d enough. But m
a9a10 61 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 aybe not. Or ma
a9a20 79 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 ybe lrand48() ha
a9a30 73 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 s some.** subtle
a9a40 20 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d problems on som
a9a50 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 e systems that c
a9a60 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c ould cause probl
a9a70 65 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 ems. It is hard
a9a80 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f .** to know. To
a9a90 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 minimize the ri
a9aa0 73 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 sk of problems d
a9ab0 75 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 ue to bad lrand4
a9ac0 38 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 8().** implement
a9ad0 61 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 ations, SQLite u
a9ae0 73 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 ses this random
a9af0 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
a9b00 20 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 based.** on RC4
a9b10 2c 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 , which we know
a9b20 77 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e works very well.
a9b30 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 .**.** (Later):
a9b40 20 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 Actually, OP_Ne
a9b50 77 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 wRowid does not
a9b60 64 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 depend on a good
a9b70 20 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 source of.** ra
a9b80 6e 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 ndomness any mor
a9b90 65 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 e. But we will
a9ba0 6c 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 leave this code
a9bb0 69 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e in all the same.
a9bc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
a9bd0 61 6e 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b andomByte(void){
a9be0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
a9bf0 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 t;... /* The "
a9c00 77 73 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 wsdPrng" macro w
a9c10 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 ill resolve to t
a9c20 68 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d he pseudo-random
a9c30 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f number generato
a9c40 72 0a 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 r. ** state vec
a9c50 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c tor. If writabl
a9c60 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 e static data is
a9c70 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 unsupported on
a9c80 74 68 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a the target,. **
a9c90 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 we have to loca
a9ca0 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 te the state vec
a9cb0 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e tor at run-time.
a9cc0 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f In the more co
a9cd0 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 mmon. ** case w
a9ce0 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 here writable st
a9cf0 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 atic data is sup
a9d00 70 6f 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 ported, wsdPrng
a9d10 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 can refer direct
a9d20 6c 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 ly. ** to the "
a9d30 73 71 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 sqlite3Prng" sta
a9d40 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 te vector declar
a9d50 65 64 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 ed above.. */.#
a9d60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
a9d70 54 5f 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 T_WSD. struct s
a9d80 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a qlite3PrngType *
a9d90 70 20 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 p = &GLOBAL(stru
a9da0 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 ct sqlite3PrngTy
a9db0 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 pe, sqlite3Prng)
a9dc0 3b 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 ;.# define wsdPr
a9dd0 6e 67 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 ng p[0].#else.#
a9de0 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 define wsdPrng s
a9df0 71 6c 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 qlite3Prng.#endi
a9e00 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c f... /* Initial
a9e10 69 7a 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 ize the state of
a9e20 20 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 the random numb
a9e30 65 72 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 er generator onc
a9e40 65 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 e,. ** the firs
a9e50 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 t time this rout
a9e60 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 ine is called.
a9e70 54 68 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 The seed value d
a9e80 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 oes. ** not nee
a9e90 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c d to contain a l
a9ea0 6f 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 ot of randomness
a9eb0 20 73 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f since we are no
a9ec0 74 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f t. ** trying to
a9ed0 20 64 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 do secure encry
a9ee0 70 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e ption or anythin
a9ef0 67 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 g like that....
a9f00 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 **. ** Nothing
a9f10 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 in this file or
a9f20 20 61 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 anywhere else i
a9f30 6e 20 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e n SQLite does an
a9f40 79 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 y kind of. ** e
a9f50 6e 63 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 ncryption. The
a9f60 52 43 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 RC4 algorithm is
a9f70 20 62 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 being used as a
a9f80 20 50 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 PRNG (pseudo-ra
a9f90 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 ndom. ** number
a9fa0 20 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 generator) not
a9fb0 61 73 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e as an encryption
a9fc0 20 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 device.. */.
a9fd0 69 66 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 if( !wsdPrng.isI
a9fe0 6e 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 nit ){. int i
a9ff0 3b 0a 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 ;. char k[256
aa000 5d 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a ];. wsdPrng.j
aa010 20 3d 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e = 0;. wsdPrn
aa020 67 2e 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c g.i = 0;. sql
aa030 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 ite3OsRandomness
aa040 28 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e (sqlite3_vfs_fin
aa050 64 28 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 d(0), 256, k);.
aa060 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 for(i=0; i<25
aa070 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 6; i++){. w
aa080 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 69 3b sdPrng.s[i] = i;
aa090 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
aa0a0 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b =0; i<256; i++){
aa0b0 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 6a . wsdPrng.j
aa0c0 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d += wsdPrng.s[i]
aa0d0 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 74 + k[i];. t
aa0e0 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 = wsdPrng.s[wsd
aa0f0 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 77 Prng.j];. w
aa100 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 sdPrng.s[wsdPrng
aa110 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b .j] = wsdPrng.s[
aa120 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 72 6e i];. wsdPrn
aa130 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 20 g.s[i] = t;.
aa140 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69 73 }. wsdPrng.is
aa150 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 Init = 1;. }..
aa160 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 64 /* Generate and
aa170 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 20 72 return single r
aa180 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a andom byte. */.
aa190 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a 20 wsdPrng.i++;.
aa1a0 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 t = wsdPrng.s[w
aa1b0 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 73 64 sdPrng.i];. wsd
aa1c0 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 77 Prng.j += t;. w
aa1d0 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 sdPrng.s[wsdPrng
aa1e0 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b .i] = wsdPrng.s[
aa1f0 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77 73 wsdPrng.j];. ws
aa200 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e dPrng.s[wsdPrng.
aa210 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 77 j] = t;. t += w
aa220 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 sdPrng.s[wsdPrng
aa230 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 .i];. return ws
aa240 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a 2f dPrng.s[t];.}../
aa250 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 72 61 *.** Return N ra
aa260 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 53 ndom bytes..*/.S
aa270 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
aa280 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
aa290 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 70 s(int N, void *p
aa2a0 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 Buf){. unsigned
aa2b0 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 70 42 char *zBuf = pB
aa2c0 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 uf;.#if SQLITE_T
aa2d0 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 HREADSAFE. sqli
aa2e0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
aa2f0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
aa300 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
aa310 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 3b 0a X_STATIC_PRNG);.
aa320 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 #endif. sqlite3
aa330 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 _mutex_enter(mut
aa340 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d ex);. while( N-
aa350 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 66 2b - ){. *(zBuf+
aa360 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 65 28 +) = randomByte(
aa370 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
aa380 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
aa390 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 ex);.}..#ifndef
aa3a0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
aa3b0 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 46 TIN_TEST./*.** F
aa3c0 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f or testing purpo
aa3d0 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 6d 65 ses, we sometime
aa3e0 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73 65 72 s want to preser
aa3f0 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 0a ve the state of.
aa400 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 73 74 ** PRNG and rest
aa410 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74 6f 20 ore the PRNG to
aa420 69 74 73 20 73 61 76 65 64 20 73 74 61 74 65 20 its saved state
aa430 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2c at a later time,
aa440 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 74 20 or.** to reset
aa450 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 73 20 the PRNG to its
aa460 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 20 20 initial state.
aa470 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
aa480 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 68 6f ccomplish.** tho
aa490 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a 20 se tasks..**.**
aa4a0 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 The sqlite3_test
aa4b0 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 _control() inter
aa4c0 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65 73 65 face calls these
aa4d0 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 routines to.**
aa4e0 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 4e 47 control the PRNG
aa4f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 ..*/.static SQLI
aa500 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 TE_WSD struct sq
aa510 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 73 71 lite3PrngType sq
aa520 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 20 3d lite3SavedPrng =
aa530 20 7b 20 30 2c 20 7d 3b 0a 53 51 4c 49 54 45 5f { 0, };.SQLITE_
aa540 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
aa550 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 ite3PrngSaveStat
aa560 65 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 e(void){. memcp
aa570 79 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 y(. &GLOBAL(s
aa580 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
aa590 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 gType, sqlite3Sa
aa5a0 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 vedPrng),. &G
aa5b0 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c LOBAL(struct sql
aa5c0 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 ite3PrngType, sq
aa5d0 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 lite3Prng),.
aa5e0 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 sizeof(sqlite3Pr
aa5f0 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 ng). );.}.SQLIT
aa600 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
aa610 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 qlite3PrngRestor
aa620 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 eState(void){.
aa630 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f memcpy(. &GLO
aa640 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 BAL(struct sqlit
aa650 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 e3PrngType, sqli
aa660 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 te3Prng),. &G
aa670 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c LOBAL(struct sql
aa680 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 ite3PrngType, sq
aa690 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c lite3SavedPrng),
aa6a0 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 . sizeof(sqli
aa6b0 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a te3Prng). );.}.
aa6c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
aa6d0 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 oid sqlite3PrngR
aa6e0 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b esetState(void){
aa6f0 0a 20 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 . GLOBAL(struct
aa700 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
aa710 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69 , sqlite3Prng).i
aa720 73 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e sInit = 0;.}.#en
aa730 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
aa740 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 IT_BUILTIN_TEST
aa750 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
aa760 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f *** End of rando
aa770 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m.c ************
aa780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aa790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aa7a0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
aa7b0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 *** Begin file u
aa7c0 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tf.c ***********
aa7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aa7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aa7f0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 **/./*.** 2004 A
aa800 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 pril 13.**.** Th
aa810 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
aa820 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
aa830 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
aa840 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
aa850 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
aa860 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
aa870 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
aa880 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
aa890 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
aa8a0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
aa8b0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
aa8c0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
aa8d0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
aa8e0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
aa8f0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
aa900 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
aa910 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
aa920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aa930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aa940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aa950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aa960 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
aa970 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 ile contains rou
aa980 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72 tines used to tr
aa990 61 6e 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20 anslate between
aa9a0 55 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31 UTF-8, .** UTF-1
aa9b0 36 2c 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64 6, UTF-16BE, and
aa9c0 20 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a UTF-16LE..**.**
aa9d0 20 24 49 64 3a 20 75 74 66 2e 63 2c 76 20 31 2e $Id: utf.c,v 1.
aa9e0 36 36 20 32 30 30 38 2f 31 31 2f 30 37 20 30 33 66 2008/11/07 03
aa9f0 3a 32 39 3a 33 34 20 64 72 68 20 45 78 70 20 24 :29:34 drh Exp $
aaa00 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 .**.** Notes on
aaa10 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 UTF-8:.**.** B
aaa20 79 74 65 2d 30 20 20 20 20 42 79 74 65 2d 31 20 yte-0 Byte-1
aaa30 20 20 20 42 79 74 65 2d 32 20 20 20 20 42 79 74 Byte-2 Byt
aaa40 65 2d 33 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 e-3 Value.**
aaa50 20 30 78 78 78 78 78 78 78 20 20 20 20 20 20 20 0xxxxxxx
aaa60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aaa70 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 000000
aaa80 30 30 20 30 30 30 30 30 30 30 30 20 30 78 78 78 00 00000000 0xxx
aaa90 78 78 78 78 0a 2a 2a 20 20 31 31 30 79 79 79 79 xxxx.** 110yyyy
aaaa0 79 20 20 31 30 78 78 78 78 78 78 20 20 20 20 20 y 10xxxxxx
aaab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aaac0 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30 00000000 00000
aaad0 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 yyy yyxxxxxx.**
aaae0 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30 79 79 79 1110zzzz 10yyy
aaaf0 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20 yyy 10xxxxxx
aab00 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 000000
aab10 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 00 zzzzyyyy yyxx
aab20 78 78 78 78 0a 2a 2a 20 20 31 31 31 31 30 75 75 xxxx.** 11110uu
aab30 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20 31 30 79 u 10uuzzzz 10y
aab40 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 yyyyy 10xxxxxx
aab50 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 000uuuuu zzzzy
aab60 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a yyy yyxxxxxx.**.
aab70 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 **.** Notes on U
aab80 54 46 2d 31 36 3a 20 20 28 77 69 74 68 20 77 77 TF-16: (with ww
aab90 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a ww+1==uuuuu).**.
aaba0 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d 30 20 20 ** Word-0
aabb0 20 20 20 20 20 20 20 20 20 20 20 20 20 57 6f 72 Wor
aabc0 64 2d 31 20 20 20 20 20 20 20 20 20 20 56 61 6c d-1 Val
aabd0 75 65 0a 2a 2a 20 20 31 31 30 31 31 30 77 77 20 ue.** 110110ww
aabe0 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31 31 wwzzzzyy 11011
aabf0 31 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20 1yy yyxxxxxx
aac00 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 000uuuuu zzzzyyy
aac10 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 7a y yyxxxxxx.** z
aac20 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 zzzyyyy yyxxxxxx
aac30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aac40 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 00000000
aac50 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 zzzzyyyy yyxxxx
aac60 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 xx.**.**.** BOM
aac70 6f 72 20 42 79 74 65 20 4f 72 64 65 72 20 4d 61 or Byte Order Ma
aac80 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 66 66 20 rk:.** 0xff
aac90 30 78 66 65 20 20 20 6c 69 74 74 6c 65 2d 65 6e 0xfe little-en
aaca0 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c dian utf-16 foll
aacb0 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78 66 65 20 ows.** 0xfe
aacc0 30 78 66 66 20 20 20 62 69 67 2d 65 6e 64 69 61 0xff big-endia
aacd0 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 n utf-16 follows
aace0 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a .**.*/./********
aacf0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76 ****** Include v
aad00 64 62 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 dbeInt.h in the
aad10 6d 69 64 64 6c 65 20 6f 66 20 75 74 66 2e 63 20 middle of utf.c
aad20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aad30 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
aad40 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
aad50 65 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a e vdbeInt.h ****
aad60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aad70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aad80 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
aad90 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 3 September 6.**
aada0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
aadb0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
aadc0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
aadd0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
aade0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
aadf0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
aae00 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
aae10 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
aae20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
aae30 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
aae40 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
aae50 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
aae60 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
aae70 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
aae80 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
aae90 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
aaea0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
aaeb0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
aaec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aaed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aaee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
aaef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
aaf00 54 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 This is the head
aaf10 65 72 20 66 69 6c 65 20 66 6f 72 20 69 6e 66 6f er file for info
aaf20 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 rmation that is
aaf30 70 72 69 76 61 74 65 20 74 6f 20 74 68 65 0a 2a private to the.*
aaf40 2a 20 56 44 42 45 2e 20 20 54 68 69 73 20 69 6e * VDBE. This in
aaf50 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 74 formation used t
aaf60 6f 20 61 6c 6c 20 62 65 20 61 74 20 74 68 65 20 o all be at the
aaf70 74 6f 70 20 6f 66 20 74 68 65 20 73 69 6e 67 6c top of the singl
aaf80 65 0a 2a 2a 20 73 6f 75 72 63 65 20 63 6f 64 65 e.** source code
aaf90 20 66 69 6c 65 20 22 76 64 62 65 2e 63 22 2e 20 file "vdbe.c".
aafa0 20 57 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20 When that file
aafb0 62 65 63 61 6d 65 20 74 6f 6f 20 62 69 67 20 28 became too big (
aafc0 6f 76 65 72 0a 2a 2a 20 36 30 30 30 20 6c 69 6e over.** 6000 lin
aafd0 65 73 20 6c 6f 6e 67 29 20 69 74 20 77 61 73 20 es long) it was
aafe0 73 70 6c 69 74 20 75 70 20 69 6e 74 6f 20 73 65 split up into se
aaff0 76 65 72 61 6c 20 73 6d 61 6c 6c 65 72 20 66 69 veral smaller fi
ab000 6c 65 73 20 61 6e 64 0a 2a 2a 20 74 68 69 73 20 les and.** this
ab010 68 65 61 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 header informati
ab020 6f 6e 20 77 61 73 20 66 61 63 74 6f 72 65 64 20 on was factored
ab030 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 out..**.** $Id:
ab040 76 64 62 65 49 6e 74 2e 68 2c 76 20 31 2e 31 35 vdbeInt.h,v 1.15
ab050 38 20 32 30 30 38 2f 31 31 2f 31 37 20 31 35 3a 8 2008/11/17 15:
ab060 33 31 3a 34 38 20 64 61 6e 69 65 6c 6b 31 39 37 31:48 danielk197
ab070 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 7 Exp $.*/.#ifnd
ab080 65 66 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 23 ef _VDBEINT_H_.#
ab090 64 65 66 69 6e 65 20 5f 56 44 42 45 49 4e 54 5f define _VDBEINT_
ab0a0 48 5f 0a 0a 2f 2a 0a 2a 2a 20 69 6e 74 54 6f 4b H_../*.** intToK
ab0b0 65 79 28 29 20 61 6e 64 20 6b 65 79 54 6f 49 6e ey() and keyToIn
ab0c0 74 28 29 20 75 73 65 64 20 74 6f 20 74 72 61 6e t() used to tran
ab0d0 73 66 6f 72 6d 20 74 68 65 20 72 6f 77 69 64 2e sform the rowid.
ab0e0 20 20 42 75 74 20 77 69 74 68 0a 2a 2a 20 74 68 But with.** th
ab0f0 65 20 6c 61 74 65 73 74 20 76 65 72 73 69 6f 6e e latest version
ab100 73 20 6f 66 20 74 68 65 20 64 65 73 69 67 6e 20 s of the design
ab110 74 68 65 79 20 61 72 65 20 6e 6f 2d 6f 70 73 2e they are no-ops.
ab120 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 6b 65 79 54 .*/.#define keyT
ab130 6f 49 6e 74 28 58 29 20 20 20 28 58 29 0a 23 64 oInt(X) (X).#d
ab140 65 66 69 6e 65 20 69 6e 74 54 6f 4b 65 79 28 58 efine intToKey(X
ab150 29 20 20 20 28 58 29 0a 0a 0a 2f 2a 0a 2a 2a 20 ) (X).../*.**
ab160 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 65 SQL is translate
ab170 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 d into a sequenc
ab180 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e e of instruction
ab190 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 75 s to be.** execu
ab1a0 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61 6c ted by a virtual
ab1b0 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68 20 machine. Each
ab1c0 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 instruction is a
ab1d0 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 n instance.** of
ab1e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
ab1f0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 tructure..*/.typ
ab200 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 edef struct Vdbe
ab210 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f Op Op;../*.** Bo
ab220 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a olean values.*/.
ab230 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 typedef unsigned
ab240 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a char Bool;../*.
ab250 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 ** A cursor is a
ab260 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20 pointer into a
ab270 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74 single BTree wit
ab280 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 hin a database f
ab290 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 ile..** The curs
ab2a0 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61 or can seek to a
ab2b0 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 74 BTree entry wit
ab2c0 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b h a particular k
ab2d0 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f ey, or.** loop o
ab2e0 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 ver all entries
ab2f0 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 59 of the Btree. Y
ab300 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65 ou can also inse
ab310 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20 rt new BTree.**
ab320 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 69 entries or retri
ab330 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64 eve the key or d
ab340 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 ata from the ent
ab350 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 ry that the curs
ab360 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 or.** is current
ab370 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a ly pointing to..
ab380 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 ** .** Every cur
ab390 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 72 sor that the vir
ab3a0 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 tual machine has
ab3b0 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 65 open is represe
ab3c0 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e nted by an.** in
ab3d0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
ab3e0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
ab3f0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
ab400 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72 69 VdbeCursor.isTri
ab410 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73 20 ggerRow flag is
ab420 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68 61 set it means tha
ab430 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 73 t this cursor is
ab440 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69 6e .** really a sin
ab450 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65 70 gle row that rep
ab460 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57 20 resents the NEW
ab470 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 or OLD pseudo-ta
ab480 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 ble of.** a row
ab490 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64 61 trigger. The da
ab4a0 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 69 ta for the row i
ab4b0 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 65 s stored in Vdbe
ab4c0 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e 64 Cursor.pData and
ab4d0 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 73 .** the rowid is
ab4e0 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 69 in VdbeCursor.i
ab4f0 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 Key..*/.struct V
ab500 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 dbeCursor {. Bt
ab510 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b Cursor *pCursor;
ab520 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f /* The curso
ab530 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 r structure of t
ab540 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20 he backend */.
ab550 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
ab560 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
ab570 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61 73 f cursor databas
ab580 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28 e in db->aDb[] (
ab590 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 20 or -1) */. i64
ab5a0 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20 lastRowid;
ab5b0 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 20 /* Last rowid
ab5c0 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 4e from a Next or N
ab5d0 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f 6e extIdx operation
ab5e0 20 2a 2f 0a 20 20 69 36 34 20 6e 65 78 74 52 6f */. i64 nextRo
ab5f0 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e wid; /* N
ab600 65 78 74 20 72 6f 77 69 64 20 72 65 74 75 72 6e ext rowid return
ab610 65 64 20 62 79 20 4f 50 5f 4e 65 77 52 6f 77 69 ed by OP_NewRowi
ab620 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f d */. Bool zero
ab630 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ed; /*
ab640 54 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f True if zeroed o
ab650 75 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72 ut and ready for
ab660 20 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c reuse */. Bool
ab670 20 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20 rowidIsValid;
ab680 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73 /* True if las
ab690 74 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20 tRowid is valid
ab6a0 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73 */. Bool atFirs
ab6b0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 t; /* Tr
ab6c0 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 ue if pointing t
ab6d0 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f o first entry */
ab6e0 0a 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f . Bool useRando
ab6f0 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65 mRowid; /* Gene
ab700 72 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20 rate new record
ab710 6e 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e numbers semi-ran
ab720 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 domly */. Bool
ab730 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20 nullRow;
ab740 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e /* True if poin
ab750 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69 ting to a row wi
ab760 74 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 th no data */.
ab770 42 6f 6f 6c 20 6e 65 78 74 52 6f 77 69 64 56 61 Bool nextRowidVa
ab780 6c 69 64 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 lid; /* True if
ab790 20 74 68 65 20 6e 65 78 74 52 6f 77 69 64 20 66 the nextRowid f
ab7a0 69 65 6c 64 20 69 73 20 76 61 6c 69 64 20 2a 2f ield is valid */
ab7b0 0a 20 20 42 6f 6f 6c 20 70 73 65 75 64 6f 54 61 . Bool pseudoTa
ab7c0 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 68 69 73 ble; /* This
ab7d0 20 69 73 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44 is a NEW or OLD
ab7e0 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 6f pseudo-tables o
ab7f0 66 20 61 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 f a trigger */.
ab800 20 42 6f 6f 6c 20 65 70 68 65 6d 50 73 65 75 64 Bool ephemPseud
ab810 6f 54 61 62 6c 65 3b 0a 20 20 42 6f 6f 6c 20 64 oTable;. Bool d
ab820 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3b 20 20 eferredMoveto;
ab830 2f 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c /* A call to sql
ab840 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 ite3BtreeMoveto(
ab850 29 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 ) is needed */.
ab860 20 42 6f 6f 6c 20 69 73 54 61 62 6c 65 3b 20 20 Bool isTable;
ab870 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
ab880 66 20 61 20 74 61 62 6c 65 20 72 65 71 75 69 72 f a table requir
ab890 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 73 ing integer keys
ab8a0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 49 6e 64 */. Bool isInd
ab8b0 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ex; /* T
ab8c0 72 75 65 20 69 66 20 61 6e 20 69 6e 64 65 78 20 rue if an index
ab8d0 63 6f 6e 74 61 69 6e 69 6e 67 20 6b 65 79 73 20 containing keys
ab8e0 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a only - no data *
ab8f0 2f 0a 20 20 69 36 34 20 6d 6f 76 65 74 6f 54 61 /. i64 movetoTa
ab900 72 67 65 74 3b 20 20 20 20 20 2f 2a 20 41 72 67 rget; /* Arg
ab910 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 65 66 ument to the def
ab920 65 72 72 65 64 20 73 71 6c 69 74 65 33 42 74 72 erred sqlite3Btr
ab930 65 65 4d 6f 76 65 74 6f 28 29 20 2a 2f 0a 20 20 eeMoveto() */.
ab940 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 Btree *pBt;
ab950 20 20 20 20 20 20 2f 2a 20 53 65 70 61 72 61 74 /* Separat
ab960 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 e file holding t
ab970 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 2a emporary table *
ab980 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 /. int nData;
ab990 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ab9a0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
ab9b0 70 44 61 74 61 20 2a 2f 0a 20 20 63 68 61 72 20 pData */. char
ab9c0 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 *pData;
ab9d0 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 61 20 4e /* Data for a N
ab9e0 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f EW or OLD pseudo
ab9f0 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 69 36 34 20 -table */. i64
aba00 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 iKey;
aba10 20 20 2f 2a 20 4b 65 79 20 66 6f 72 20 74 68 65 /* Key for the
aba20 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 NEW or OLD pseu
aba30 64 6f 2d 74 61 62 6c 65 20 72 6f 77 20 2a 2f 0a do-table row */.
aba40 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
aba50 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f 20 nfo; /* Info
aba60 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79 73 about index keys
aba70 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65 78 needed by index
aba80 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e cursors */. in
aba90 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 t nField;
abaa0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
abab0 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 68 fields in the h
abac0 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 73 eader */. i64 s
abad0 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 eqCount;
abae0 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63 6f 75 /* Sequence cou
abaf0 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 nter */. sqlite
abb00 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 3_vtab_cursor *p
abb10 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f 2a 20 VtabCursor; /*
abb20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20 61 The cursor for a
abb30 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a virtual table *
abb40 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 /. const sqlite
abb50 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 3_module *pModul
abb60 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 e; /* Module
abb70 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56 74 61 for cursor pVta
abb80 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 2f 2a bCursor */.. /*
abb90 20 43 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 Cached informat
abba0 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 ion about the he
abbb0 61 64 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 ader for the dat
abbc0 61 20 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 a record that th
abbd0 65 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73 e. ** cursor is
abbe0 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
abbf0 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61 ing to. Only va
abc00 6c 69 64 20 69 66 20 63 61 63 68 65 56 61 6c 69 lid if cacheVali
abc10 64 20 69 73 20 74 72 75 65 2e 0a 20 20 2a 2a 20 d is true.. **
abc20 61 52 6f 77 20 6d 69 67 68 74 20 70 6f 69 6e 74 aRow might point
abc30 20 74 6f 20 28 65 70 68 65 6d 65 72 61 6c 29 20 to (ephemeral)
abc40 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 data for the cur
abc50 72 65 6e 74 20 72 6f 77 2c 20 6f 72 20 69 74 20 rent row, or it
abc60 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55 might. ** be NU
abc70 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 63 LL.. */. int c
abc80 61 63 68 65 53 74 61 74 75 73 3b 20 20 20 20 20 acheStatus;
abc90 20 2f 2a 20 43 61 63 68 65 20 69 73 20 76 61 6c /* Cache is val
abca0 69 64 20 69 66 20 74 68 69 73 20 6d 61 74 63 68 id if this match
abcb0 65 73 20 56 64 62 65 2e 63 61 63 68 65 43 74 72 es Vdbe.cacheCtr
abcc0 20 2a 2f 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 */. int payloa
abcd0 64 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54 dSize; /* T
abce0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 otal number of b
abcf0 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f ytes in the reco
abd00 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 rd */. u32 *aTy
abd10 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a pe; /*
abd20 20 54 79 70 65 20 76 61 6c 75 65 73 20 66 6f 72 Type values for
abd30 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 all entries in
abd40 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
abd50 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 u32 *aOffset;
abd60 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20 /* Cached
abd70 6f 66 66 73 65 74 73 20 74 6f 20 74 68 65 20 73 offsets to the s
abd80 74 61 72 74 20 6f 66 20 65 61 63 68 20 63 6f 6c tart of each col
abd90 75 6d 6e 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 umns data */. u
abda0 38 20 2a 61 52 6f 77 3b 20 20 20 20 20 20 20 20 8 *aRow;
abdb0 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 /* Data for
abdc0 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
abdd0 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20 , if all on one
abde0 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 page */.};.typed
abdf0 65 66 20 73 74 72 75 63 74 20 56 64 62 65 43 75 ef struct VdbeCu
abe00 72 73 6f 72 20 56 64 62 65 43 75 72 73 6f 72 3b rsor VdbeCursor;
abe10 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20 ../*.** A value
abe20 66 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63 for VdbeCursor.c
abe30 61 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d acheValid that m
abe40 65 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69 eans the cache i
abe50 73 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64 s always invalid
abe60 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43 ..*/.#define CAC
abe70 48 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a HE_STALE 0../*.*
abe80 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 * Internally, th
abe90 65 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74 e vdbe manipulat
abea0 65 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51 es nearly all SQ
abeb0 4c 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a L values as Mem.
abec0 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45 ** structures. E
abed0 61 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d ach Mem struct m
abee0 61 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c ay cache multipl
abef0 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e e representation
abf00 73 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e s (string,.** in
abf10 74 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74 teger etc.) of t
abf20 68 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20 he same value.
abf30 41 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65 A value (and the
abf40 72 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63 refore Mem struc
abf50 74 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65 ture).** has the
abf60 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65 following prope
abf70 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63 rties:.**.** Eac
abf80 68 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61 h value has a ma
abf90 6e 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65 nifest type. The
abfa0 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f manifest type o
abfb0 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 f the value stor
abfc0 65 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73 ed.** in a Mem s
abfd0 74 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65 truct is returne
abfe0 64 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65 d by the MemType
abff0 28 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68 (Mem*) macro. Th
ac000 65 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65 e type is.** one
ac010 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c of SQLITE_NULL,
ac020 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c SQLITE_INTEGER,
ac030 20 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51 SQLITE_REAL, SQ
ac040 4c 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20 LITE_TEXT or.**
ac050 53 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a SQLITE_BLOB..*/.
ac060 73 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75 struct Mem {. u
ac070 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69 nion {. i64 i
ac080 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
ac090 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e * Integer value.
ac0a0 20 4f 72 20 46 75 6e 63 44 65 66 2a 20 77 68 65 Or FuncDef* whe
ac0b0 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 67 n flags==MEM_Agg
ac0c0 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 */. FuncDef
ac0d0 2a 70 44 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 *pDef; /* U
ac0e0 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c sed only when fl
ac0f0 61 67 73 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a ags==MEM_Agg */.
ac100 20 20 7d 20 75 3b 0a 20 20 64 6f 75 62 6c 65 20 } u;. double
ac110 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 r; /*
ac120 52 65 61 6c 20 76 61 6c 75 65 20 2a 2f 0a 20 20 Real value */.
ac130 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
ac140 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 /* The assoc
ac150 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 63 iated database c
ac160 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 onnection */. c
ac170 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 har *z;
ac180 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 /* String or
ac190 42 4c 4f 42 20 76 61 6c 75 65 20 2a 2f 0a 20 20 BLOB value */.
ac1a0 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 int n;
ac1b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
ac1c0 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 73 characters in s
ac1d0 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 65 78 63 tring value, exc
ac1e0 6c 75 64 69 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 luding '\0' */.
ac1f0 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 u16 flags;
ac200 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d /* Some com
ac210 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f bination of MEM_
ac220 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d Null, MEM_Str, M
ac230 45 4d 5f 44 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a EM_Dyn, etc. */.
ac240 20 20 75 38 20 20 74 79 70 65 3b 20 20 20 20 20 u8 type;
ac250 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 /* One of
ac260 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c SQLITE_NULL, SQL
ac270 49 54 45 5f 54 45 58 54 2c 20 53 51 4c 49 54 45 ITE_TEXT, SQLITE
ac280 5f 49 4e 54 45 47 45 52 2c 20 65 74 63 20 2a 2f _INTEGER, etc */
ac290 0a 20 20 75 38 20 20 65 6e 63 3b 20 20 20 20 20 . u8 enc;
ac2a0 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 /* SQLITE
ac2b0 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 55 54 _UTF8, SQLITE_UT
ac2c0 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 F16BE, SQLITE_UT
ac2d0 46 31 36 4c 45 20 2a 2f 0a 20 20 76 6f 69 64 20 F16LE */. void
ac2e0 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 3b (*xDel)(void *);
ac2f0 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 75 6c 6c /* If not null
ac300 2c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 , call this func
ac310 74 69 6f 6e 20 74 6f 20 64 65 6c 65 74 65 20 4d tion to delete M
ac320 65 6d 2e 7a 20 2a 2f 0a 20 20 63 68 61 72 20 2a em.z */. char *
ac330 7a 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a zMalloc; /*
ac340 20 44 79 6e 61 6d 69 63 20 62 75 66 66 65 72 20 Dynamic buffer
ac350 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c allocated by sql
ac360 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f ite3_malloc() */
ac370 0a 7d 3b 0a 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d .};../* One or m
ac380 6f 72 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f ore of the follo
ac390 77 69 6e 67 20 66 6c 61 67 73 20 61 72 65 20 73 wing flags are s
ac3a0 65 74 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 et to indicate t
ac3b0 68 65 20 76 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 he validOK.** re
ac3c0 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66 presentations of
ac3d0 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 the value store
ac3e0 64 20 69 6e 20 74 68 65 20 4d 65 6d 20 73 74 72 d in the Mem str
ac3f0 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 uct..**.** If th
ac400 65 20 4d 45 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 e MEM_Null flag
ac410 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 is set, then the
ac420 20 76 61 6c 75 65 20 69 73 20 61 6e 20 53 51 4c value is an SQL
ac430 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 NULL value..**
ac440 4e 6f 20 6f 74 68 65 72 20 66 6c 61 67 73 20 6d No other flags m
ac450 61 79 20 62 65 20 73 65 74 20 69 6e 20 74 68 69 ay be set in thi
ac460 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 s case..**.** If
ac470 20 74 68 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61 the MEM_Str fla
ac480 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 4d 65 g is set then Me
ac490 6d 2e 7a 20 70 6f 69 6e 74 73 20 61 74 20 61 20 m.z points at a
ac4a0 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 string represent
ac4b0 61 74 69 6f 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c ation..** Usuall
ac4c0 79 20 74 68 69 73 20 69 73 20 65 6e 63 6f 64 65 y this is encode
ac4d0 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 75 6e d in the same un
ac4e0 69 63 6f 64 65 20 65 6e 63 6f 64 69 6e 67 20 61 icode encoding a
ac4f0 73 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 s the main.** da
ac500 74 61 62 61 73 65 20 28 73 65 65 20 62 65 6c 6f tabase (see belo
ac510 77 20 66 6f 72 20 65 78 63 65 70 74 69 6f 6e 73 w for exceptions
ac520 29 2e 20 49 66 20 74 68 65 20 4d 45 4d 5f 54 65 ). If the MEM_Te
ac530 72 6d 20 66 6c 61 67 20 69 73 20 61 6c 73 6f 0a rm flag is also.
ac540 2a 2a 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 ** set, then the
ac550 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 20 74 string is nul t
ac560 65 72 6d 69 6e 61 74 65 64 2e 20 54 68 65 20 4d erminated. The M
ac570 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 EM_Int and MEM_R
ac580 65 61 6c 20 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 eal .** flags ma
ac590 79 20 63 6f 65 78 69 73 74 20 77 69 74 68 20 74 y coexist with t
ac5a0 68 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 2e he MEM_Str flag.
ac5b0 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 .**.** Multiple
ac5c0 6f 66 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 of these values
ac5d0 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65 can appear in Me
ac5e0 6d 2e 66 6c 61 67 73 2e 20 20 42 75 74 20 6f 6e m.flags. But on
ac5f0 6c 79 20 6f 6e 65 0a 2a 2a 20 61 74 20 61 20 74 ly one.** at a t
ac600 69 6d 65 20 63 61 6e 20 61 70 70 65 61 72 20 69 ime can appear i
ac610 6e 20 4d 65 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 n Mem.type..*/.#
ac620 64 65 66 69 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 define MEM_Null
ac630 20 20 20 20 20 30 78 30 30 30 31 20 20 20 2f 2a 0x0001 /*
ac640 20 56 61 6c 75 65 20 69 73 20 4e 55 4c 4c 20 2a Value is NULL *
ac650 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 /.#define MEM_St
ac660 72 20 20 20 20 20 20 20 30 78 30 30 30 32 20 20 r 0x0002
ac670 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 20 73 /* Value is a s
ac680 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 tring */.#define
ac690 20 4d 45 4d 5f 49 6e 74 20 20 20 20 20 20 20 30 MEM_Int 0
ac6a0 78 30 30 30 34 20 20 20 2f 2a 20 56 61 6c 75 65 x0004 /* Value
ac6b0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a is an integer *
ac6c0 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 52 65 /.#define MEM_Re
ac6d0 61 6c 20 20 20 20 20 20 30 78 30 30 30 38 20 20 al 0x0008
ac6e0 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 20 72 /* Value is a r
ac6f0 65 61 6c 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 eal number */.#d
ac700 65 66 69 6e 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 efine MEM_Blob
ac710 20 20 20 20 30 78 30 30 31 30 20 20 20 2f 2a 20 0x0010 /*
ac720 56 61 6c 75 65 20 69 73 20 61 20 42 4c 4f 42 20 Value is a BLOB
ac730 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 */..#define MemS
ac740 65 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 etTypeFlag(p, f)
ac750 20 5c 0a 20 20 28 28 70 29 2d 3e 66 6c 61 67 73 \. ((p)->flags
ac760 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e = ((p)->flags&~
ac770 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 (MEM_Int|MEM_Rea
ac780 6c 7c 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 42 l|MEM_Null|MEM_B
ac790 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 7c 66 29 lob|MEM_Str))|f)
ac7a0 0a 0a 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65 ../* Whenever Me
ac7b0 6d 20 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c m contains a val
ac7c0 69 64 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f id string or blo
ac7d0 62 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e b representation
ac7e0 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 , one of.** the
ac7f0 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 following flags
ac800 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 64 must be set to d
ac810 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d etermine the mem
ac820 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a ory management.*
ac830 2a 20 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d * policy for Mem
ac840 2e 7a 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 .z. The MEM_Ter
ac850 6d 20 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 m flag tells us
ac860 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
ac870 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 he.** string is
ac880 5c 30 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 \000 or \u0000 t
ac890 65 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65 erminated.*/.#de
ac8a0 66 69 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20 fine MEM_Term
ac8b0 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a 20 53 0x0020 /* S
ac8c0 74 72 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c tring rep is nul
ac8d0 20 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23 terminated */.#
ac8e0 64 65 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20 define MEM_Dyn
ac8f0 20 20 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 0x0040 /*
ac900 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 Need to call sq
ac910 6c 69 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65 liteFree() on Me
ac920 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d m.z */.#define M
ac930 45 4d 5f 53 74 61 74 69 63 20 20 20 20 30 78 30 EM_Static 0x0
ac940 30 38 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 080 /* Mem.z p
ac950 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69 oints to a stati
ac960 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 c string */.#def
ac970 69 6e 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20 ine MEM_Ephem
ac980 20 20 30 78 30 31 30 30 20 20 20 2f 2a 20 4d 65 0x0100 /* Me
ac990 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e m.z points to an
ac9a0 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e ephemeral strin
ac9b0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d g */.#define MEM
ac9c0 5f 41 67 67 20 20 20 20 20 20 20 30 78 30 34 30 _Agg 0x040
ac9d0 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 0 /* Mem.z poi
ac9e0 6e 74 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75 nts to an agg fu
ac9f0 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a nction context *
aca00 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 /.#define MEM_Ze
aca10 72 6f 20 20 20 20 20 20 30 78 30 38 30 30 20 20 ro 0x0800
aca20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69 /* Mem.i contai
aca30 6e 73 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61 ns count of 0s a
aca40 70 70 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20 ppended to blob
aca50 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
aca60 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
aca70 20 20 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72 #undef MEM_Zer
aca80 6f 0a 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f o. #define MEM_
aca90 5a 65 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64 Zero 0x0000.#end
acaa0 69 66 0a 0a 0a 2f 2a 20 41 20 56 64 62 65 46 75 if.../* A VdbeFu
acab0 6e 63 20 69 73 20 6a 75 73 74 20 61 20 46 75 6e nc is just a Fun
acac0 63 44 65 66 20 28 64 65 66 69 6e 65 64 20 69 6e cDef (defined in
acad0 20 73 71 6c 69 74 65 49 6e 74 2e 68 29 20 74 68 sqliteInt.h) th
acae0 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 at contains.** a
acaf0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
acb00 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 75 78 69 ation about auxi
acb10 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f liary informatio
acb20 6e 20 62 6f 75 6e 64 20 74 6f 20 61 72 67 75 6d n bound to argum
acb30 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 ents.** of the f
acb40 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 unction. This i
acb50 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d s used to implem
acb60 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f ent the sqlite3_
acb70 67 65 74 5f 61 75 78 64 61 74 61 28 29 0a 2a 2a get_auxdata().**
acb80 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 65 74 and sqlite3_set
acb90 5f 61 75 78 64 61 74 61 28 29 20 41 50 49 73 2e _auxdata() APIs.
acba0 20 20 54 68 65 20 22 61 75 78 64 61 74 61 22 20 The "auxdata"
acbb0 69 73 20 73 6f 6d 65 20 61 75 78 69 6c 69 61 72 is some auxiliar
acbc0 79 20 64 61 74 61 0a 2a 2a 20 74 68 61 74 20 63 y data.** that c
acbd0 61 6e 20 62 65 20 61 73 73 6f 63 69 61 74 65 64 an be associated
acbe0 20 77 69 74 68 20 61 20 63 6f 6e 73 74 61 6e 74 with a constant
acbf0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 argument to a f
acc00 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a unction. This.*
acc10 2a 20 61 6c 6c 6f 77 73 20 66 75 6e 63 74 69 6f * allows functio
acc20 6e 73 20 73 75 63 68 20 61 73 20 22 72 65 67 65 ns such as "rege
acc30 78 70 22 20 74 6f 20 63 6f 6d 70 69 6c 65 20 74 xp" to compile t
acc40 68 65 69 72 20 63 6f 6e 73 74 61 6e 74 20 72 65 heir constant re
acc50 67 75 6c 61 72 0a 2a 2a 20 65 78 70 72 65 73 73 gular.** express
acc60 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 6e 63 ion argument onc
acc70 65 20 61 6e 64 20 72 65 75 73 65 64 20 74 68 65 e and reused the
acc80 20 63 6f 6d 70 69 6c 65 64 20 63 6f 64 65 20 66 compiled code f
acc90 6f 72 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 or multiple.** i
acca0 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 nvocations..*/.s
accb0 74 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 7b truct VdbeFunc {
accc0 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e . FuncDef *pFun
accd0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
acce0 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 /* The definiti
accf0 6f 6e 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 on of the functi
acd00 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 75 78 on */. int nAux
acd10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
acd20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
acd30 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 of entries alloc
acd40 61 74 65 64 20 66 6f 72 20 61 70 41 75 78 5b 5d ated for apAux[]
acd50 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 75 78 */. struct Aux
acd60 44 61 74 61 20 7b 0a 20 20 20 20 76 6f 69 64 20 Data {. void
acd70 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 *pAux;
acd80 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 78 20 /* Aux
acd90 64 61 74 61 20 66 6f 72 20 74 68 65 20 69 2d 74 data for the i-t
acda0 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 h argument */.
acdb0 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 void (*xDelete
acdc0 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 )(void *);
acdd0 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f /* Destructor fo
acde0 72 20 74 68 65 20 61 75 78 20 64 61 74 61 20 2a r the aux data *
acdf0 2f 0a 20 20 7d 20 61 70 41 75 78 5b 31 5d 3b 20 /. } apAux[1];
ace00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ace10 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f 74 20 66 6f /* One slot fo
ace20 72 20 65 61 63 68 20 66 75 6e 63 74 69 6f 6e 20 r each function
ace30 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a argument */.};..
ace40 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 6f 6e 74 65 /*.** The "conte
ace50 78 74 22 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 xt" argument for
ace60 20 61 20 69 6e 73 74 61 6c 6c 61 62 6c 65 20 66 a installable f
ace70 75 6e 63 74 69 6f 6e 2e 20 20 41 20 70 6f 69 6e unction. A poin
ace80 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 ter to an.** ins
ace90 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
acea0 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20 66 ructure is the f
aceb0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
acec0 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 75 73 the routines us
aced0 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 ed.** implement
acee0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
acef0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 s..**.** There i
acf00 73 20 61 20 74 79 70 65 64 65 66 20 66 6f 72 20 s a typedef for
acf10 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 this structure i
acf20 6e 20 73 71 6c 69 74 65 2e 68 2e 20 20 53 6f 20 n sqlite.h. So
acf30 61 6c 6c 20 72 6f 75 74 69 6e 65 73 2c 0a 2a 2a all routines,.**
acf40 20 65 76 65 6e 20 74 68 65 20 70 75 62 6c 69 63 even the public
acf50 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 53 51 interface to SQ
acf60 4c 69 74 65 2c 20 63 61 6e 20 75 73 65 20 61 20 Lite, can use a
acf70 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 pointer to this
acf80 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 42 75 structure..** Bu
acf90 74 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74 t this file is t
acfa0 68 65 20 6f 6e 6c 79 20 70 6c 61 63 65 20 77 68 he only place wh
acfb0 65 72 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ere the internal
acfc0 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 69 73 details of this
acfd0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 61 72 .** structure ar
acfe0 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 e known..**.** T
acff0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 his structure is
ad000 20 64 65 66 69 6e 65 64 20 69 6e 73 69 64 65 20 defined inside
ad010 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 62 65 63 of vdbeInt.h bec
ad020 61 75 73 65 20 69 74 20 75 73 65 73 20 73 75 62 ause it uses sub
ad030 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 28 4d structures.** (M
ad040 65 6d 29 20 77 68 69 63 68 20 61 72 65 20 6f 6e em) which are on
ad050 6c 79 20 64 65 66 69 6e 65 64 20 74 68 65 72 65 ly defined there
ad060 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
ad070 74 65 33 5f 63 6f 6e 74 65 78 74 20 7b 0a 20 20 te3_context {.
ad080 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 FuncDef *pFunc;
ad090 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
ad0a0 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 69 6e 66 to function inf
ad0b0 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d 55 53 54 20 ormation. MUST
ad0c0 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 56 64 BE FIRST */. Vd
ad0d0 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e beFunc *pVdbeFun
ad0e0 63 3b 20 20 2f 2a 20 41 75 78 69 6c 61 72 79 20 c; /* Auxilary
ad0f0 64 61 74 61 2c 20 69 66 20 63 72 65 61 74 65 64 data, if created
ad100 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 3b 20 20 20 . */. Mem s;
ad110 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ad120 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
ad130 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 is stored here
ad140 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 */. Mem *pMem;
ad150 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 /* Me
ad160 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 74 mory cell used t
ad170 6f 20 73 74 6f 72 65 20 61 67 67 72 65 67 61 74 o store aggregat
ad180 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 e context */. i
ad190 6e 74 20 69 73 45 72 72 6f 72 3b 20 20 20 20 20 nt isError;
ad1a0 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f /* Error co
ad1b0 64 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 de returned by t
ad1c0 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a he function. */.
ad1d0 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
ad1e0 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 ; /* Colla
ad1f0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f ting sequence */
ad200 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 65 74 .};../*.** A Set
ad210 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 structure is us
ad220 65 64 20 66 6f 72 20 71 75 69 63 6b 20 74 65 73 ed for quick tes
ad230 74 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 61 ting to see if a
ad240 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 61 72 value.** is par
ad250 74 20 6f 66 20 61 20 73 6d 61 6c 6c 20 73 65 74 t of a small set
ad260 2e 20 20 53 65 74 73 20 61 72 65 20 75 73 65 64 . Sets are used
ad270 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f to implement co
ad280 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 68 69 73 3a de like.** this:
ad290 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 .** x
ad2a0 2e 79 20 49 4e 20 28 27 68 69 27 2c 27 68 6f 6f .y IN ('hi','hoo
ad2b0 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74 79 70 65 ','hum').*/.type
ad2c0 64 65 66 20 73 74 72 75 63 74 20 53 65 74 20 53 def struct Set S
ad2d0 65 74 3b 0a 73 74 72 75 63 74 20 53 65 74 20 7b et;.struct Set {
ad2e0 0a 20 20 48 61 73 68 20 68 61 73 68 3b 20 20 20 . Hash hash;
ad2f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 /* A s
ad300 65 74 20 69 73 20 6a 75 73 74 20 61 20 68 61 73 et is just a has
ad310 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 h table */. Has
ad320 68 45 6c 65 6d 20 2a 70 72 65 76 3b 20 20 20 20 hElem *prev;
ad330 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 6c /* Previousl
ad340 79 20 61 63 63 65 73 73 65 64 20 68 61 73 68 20 y accessed hash
ad350 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a elemen */.};../*
ad360 0a 2a 2a 20 41 20 46 69 66 6f 50 61 67 65 20 73 .** A FifoPage s
ad370 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61 tructure holds a
ad380 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 single page of
ad390 76 61 6c 76 65 73 2e 20 20 50 61 67 65 73 20 61 valves. Pages a
ad3a0 72 65 20 61 72 72 61 6e 67 65 64 0a 2a 2a 20 69 re arranged.** i
ad3b0 6e 20 61 20 6c 69 73 74 2e 0a 2a 2f 0a 74 79 70 n a list..*/.typ
ad3c0 65 64 65 66 20 73 74 72 75 63 74 20 46 69 66 6f edef struct Fifo
ad3d0 50 61 67 65 20 46 69 66 6f 50 61 67 65 3b 0a 73 Page FifoPage;.s
ad3e0 74 72 75 63 74 20 46 69 66 6f 50 61 67 65 20 7b truct FifoPage {
ad3f0 0a 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 20 20 . int nSlot;
ad400 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
ad410 6f 66 20 65 6e 74 72 69 65 73 20 61 53 6c 6f 74 of entries aSlot
ad420 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 57 72 69 [] */. int iWri
ad430 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 75 te; /* Pu
ad440 73 68 20 74 68 65 20 6e 65 78 74 20 76 61 6c 75 sh the next valu
ad450 65 20 69 6e 74 6f 20 74 68 69 73 20 65 6e 74 72 e into this entr
ad460 79 20 69 6e 20 61 53 6c 6f 74 5b 5d 20 2a 2f 0a y in aSlot[] */.
ad470 20 20 69 6e 74 20 69 52 65 61 64 3b 20 20 20 20 int iRead;
ad480 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 /* Read the
ad490 20 6e 65 78 74 20 76 61 6c 75 65 20 66 72 6f 6d next value from
ad4a0 20 74 68 69 73 20 65 6e 74 72 79 20 69 6e 20 61 this entry in a
ad4b0 53 6c 6f 74 5b 5d 20 2a 2f 0a 20 20 46 69 66 6f Slot[] */. Fifo
ad4c0 50 61 67 65 20 2a 70 4e 65 78 74 3b 20 20 20 2f Page *pNext; /
ad4d0 2a 20 4e 65 78 74 20 70 61 67 65 20 69 6e 20 74 * Next page in t
ad4e0 68 65 20 66 69 66 6f 20 2a 2f 0a 20 20 69 36 34 he fifo */. i64
ad4f0 20 61 53 6c 6f 74 5b 31 5d 3b 20 20 20 20 20 20 aSlot[1];
ad500 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 /* One or more s
ad510 6c 6f 74 73 20 66 6f 72 20 72 6f 77 69 64 20 76 lots for rowid v
ad520 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a alues */.};../*.
ad530 2a 2a 20 54 68 65 20 46 69 66 6f 20 73 74 72 75 ** The Fifo stru
ad540 63 74 75 72 65 20 69 73 20 74 79 70 65 64 65 66 cture is typedef
ad550 2d 65 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 -ed in vdbeInt.h
ad560 2e 20 20 42 75 74 20 74 68 65 20 69 6d 70 6c 65 . But the imple
ad570 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 mentation.** of
ad580 74 68 61 74 20 73 74 72 75 63 74 75 72 65 20 69 that structure i
ad590 73 20 70 72 69 76 61 74 65 20 74 6f 20 74 68 69 s private to thi
ad5a0 73 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 s file..**.** Th
ad5b0 65 20 46 69 66 6f 20 73 74 72 75 63 74 75 72 65 e Fifo structure
ad5c0 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 describes the e
ad5d0 6e 74 69 72 65 20 66 69 66 6f 2e 20 20 0a 2a 2f ntire fifo. .*/
ad5e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
ad5f0 46 69 66 6f 20 46 69 66 6f 3b 0a 73 74 72 75 63 Fifo Fifo;.struc
ad600 74 20 46 69 66 6f 20 7b 0a 20 20 69 6e 74 20 6e t Fifo {. int n
ad610 45 6e 74 72 79 3b 20 20 20 20 20 20 20 20 20 2f Entry; /
ad620 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
ad630 66 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20 73 f entries */. s
ad640 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
ad650 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 /* The associ
ad660 61 74 65 64 20 64 61 74 61 62 61 73 65 20 63 6f ated database co
ad670 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 46 69 nnection */. Fi
ad680 66 6f 50 61 67 65 20 2a 70 46 69 72 73 74 3b 20 foPage *pFirst;
ad690 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 /* First page
ad6a0 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 on the list */.
ad6b0 20 46 69 66 6f 50 61 67 65 20 2a 70 4c 61 73 74 FifoPage *pLast
ad6c0 3b 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 61 67 ; /* Last pag
ad6d0 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f e on the list */
ad6e0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f 6e .};../*.** A Con
ad6f0 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 20 text stores the
ad700 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69 last insert rowi
ad710 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 74 d, the last stat
ad720 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 ement change cou
ad730 6e 74 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 nt,.** and the c
ad740 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 urrent statement
ad750 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 28 69 change count (i
ad760 2e 65 2e 20 63 68 61 6e 67 65 73 20 73 69 6e 63 .e. changes sinc
ad770 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 e last statement
ad780 29 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e )..** The curren
ad790 74 20 6b 65 79 6c 69 73 74 20 69 73 20 61 6c 73 t keylist is als
ad7a0 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 o stored in the
ad7b0 63 6f 6e 74 65 78 74 2e 0a 2a 2a 20 45 6c 65 6d context..** Elem
ad7c0 65 6e 74 73 20 6f 66 20 43 6f 6e 74 65 78 74 20 ents of Context
ad7d0 73 74 72 75 63 74 75 72 65 20 74 79 70 65 20 6d structure type m
ad7e0 61 6b 65 20 75 70 20 74 68 65 20 43 6f 6e 74 65 ake up the Conte
ad7f0 78 74 53 74 61 63 6b 2c 20 77 68 69 63 68 20 69 xtStack, which i
ad800 73 0a 2a 2a 20 75 70 64 61 74 65 64 20 62 79 20 s.** updated by
ad810 74 68 65 20 43 6f 6e 74 65 78 74 50 75 73 68 20 the ContextPush
ad820 61 6e 64 20 43 6f 6e 74 65 78 74 50 6f 70 20 6f and ContextPop o
ad830 70 63 6f 64 65 73 20 28 75 73 65 64 20 62 79 20 pcodes (used by
ad840 74 72 69 67 67 65 72 73 29 2e 0a 2a 2a 20 54 68 triggers)..** Th
ad850 65 20 63 6f 6e 74 65 78 74 20 69 73 20 70 75 73 e context is pus
ad860 68 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 hed before execu
ad870 74 69 6e 67 20 61 20 74 72 69 67 67 65 72 20 61 ting a trigger a
ad880 20 70 6f 70 70 65 64 20 77 68 65 6e 20 74 68 65 popped when the
ad890 0a 2a 2a 20 74 72 69 67 67 65 72 20 66 69 6e 69 .** trigger fini
ad8a0 73 68 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 shes..*/.typedef
ad8b0 20 73 74 72 75 63 74 20 43 6f 6e 74 65 78 74 20 struct Context
ad8c0 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 Context;.struct
ad8d0 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 36 34 20 Context {. i64
ad8e0 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 2f 2a lastRowid; /*
ad8f0 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 Last insert row
ad900 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 74 id (sqlite3.last
ad910 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20 Rowid) */. int
ad920 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a nChange; /*
ad930 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 Statement chang
ad940 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 65 es (Vdbe.nChange
ad950 73 29 20 20 20 20 20 2a 2f 0a 20 20 46 69 66 6f s) */. Fifo
ad960 20 73 46 69 66 6f 3b 20 20 20 20 20 20 20 2f 2a sFifo; /*
ad970 20 52 65 63 6f 72 64 73 20 74 68 61 74 20 77 69 Records that wi
ad980 6c 6c 20 70 61 72 74 69 63 69 70 61 74 65 20 69 ll participate i
ad990 6e 20 61 20 44 45 4c 45 54 45 20 6f 72 20 55 50 n a DELETE or UP
ad9a0 44 41 54 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a DATE */.};../*.*
ad9b0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
ad9c0 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 the virtual mac
ad9d0 68 69 6e 65 2e 20 20 54 68 69 73 20 73 74 72 75 hine. This stru
ad9e0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 74 cture contains t
ad9f0 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 he complete.** s
ada00 74 61 74 65 20 6f 66 20 74 68 65 20 76 69 72 74 tate of the virt
ada10 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a ual machine..**.
ada20 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 65 33 5f ** The "sqlite3_
ada30 73 74 6d 74 22 20 73 74 72 75 63 74 75 72 65 20 stmt" structure
ada40 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 pointer that is
ada50 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
ada60 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a te3_compile().**
ada70 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 is really a poi
ada80 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
ada90 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 nce of this stru
adaa0 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cture..**.** The
adab0 20 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68 Vdbe.inVtabMeth
adac0 6f 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 od variable is s
adad0 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 et to non-zero f
adae0 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20 or the duration
adaf0 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 61 of.** any virtua
adb00 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69 l table method i
adb10 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 20 nvocations made
adb20 62 79 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 by the vdbe prog
adb30 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 65 ram. It is.** se
adb40 74 20 74 6f 20 32 20 66 6f 72 20 78 44 65 73 74 t to 2 for xDest
adb50 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c 73 roy method calls
adb60 20 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 6f and 1 for all o
adb70 74 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 68 ther methods. Th
adb80 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 is.** variable i
adb90 73 20 75 73 65 64 20 66 6f 72 20 74 77 6f 20 70 s used for two p
adba0 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c 6f urposes: to allo
adbb0 77 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f w xDestroy metho
adbc0 64 73 20 74 6f 20 65 78 65 63 75 74 65 0a 2a 2a ds to execute.**
adbd0 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 73 74 "DROP TABLE" st
adbe0 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f 20 atements and to
adbf0 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 73 prevent some nas
adc00 74 79 20 73 69 64 65 20 65 66 66 65 63 74 73 20 ty side effects
adc10 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 of.** malloc fai
adc20 6c 75 72 65 20 77 68 65 6e 20 53 51 4c 69 74 65 lure when SQLite
adc30 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 75 is invoked recu
adc40 72 73 69 76 65 6c 79 20 62 79 20 61 20 76 69 72 rsively by a vir
adc50 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 6d tual table .** m
adc60 65 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e 0a ethod function..
adc70 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 20 7b */.struct Vdbe {
adc80 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
adc90 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 /* The wh
adca0 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a ole database */.
adcb0 20 20 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 Vdbe *pPrev,*p
adcc0 4e 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b 65 64 20 Next; /* Linked
adcd0 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20 77 69 list of VDBEs wi
adce0 74 68 20 74 68 65 20 73 61 6d 65 20 56 64 62 65 th the same Vdbe
adcf0 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 .db */. int nOp
add00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
add10 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 Number of instru
add20 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72 ctions in the pr
add30 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e ogram */. int n
add40 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f OpAlloc; /
add50 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 * Number of slot
add60 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
add70 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61 aOp[] */. Op *a
add80 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f Op; /
add90 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 * Space to hold
adda0 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 the virtual mach
addb0 69 6e 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f ine's program */
addc0 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 . int nLabel;
addd0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
adde0 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64 20 of labels used
addf0 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 41 */. int nLabelA
ade00 6c 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 lloc; /* Numb
ade10 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f er of slots allo
ade20 63 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b cated in aLabel[
ade30 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62 ] */. int *aLab
ade40 65 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 70 el; /* Sp
ade50 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ace to hold the
ade60 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 labels */. Mem
ade70 2a 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20 **apArg;
ade80 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 /* Arguments to
ade90 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 currently execut
adea0 69 6e 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f ing user functio
adeb0 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c n */. Mem *aCol
adec0 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 43 6f Name; /* Co
aded0 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65 lumn names to re
adee0 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 turn */. int nC
adef0 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a ursor; /*
adf00 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 Number of slots
adf10 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 in apCsr[] */.
adf20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 VdbeCursor **ap
adf30 43 73 72 3b 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d Csr; /* One elem
adf40 65 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72 61 ent of this arra
adf50 79 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 y for each open
adf60 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 cursor */. int
adf70 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 nVar;
adf80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
adf90 72 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a ries in aVar[] *
adfa0 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20 /. Mem *aVar;
adfb0 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
adfc0 73 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 s for the OP_Var
adfd0 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f iable opcode. */
adfe0 0a 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b . char **azVar;
adff0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
ae000 66 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 f variables */.
ae010 20 69 6e 74 20 6f 6b 56 61 72 3b 20 20 20 20 20 int okVar;
ae020 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
ae030 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e azVar[] has been
ae040 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
ae050 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 u32 magic;
ae060 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 /* Mag
ae070 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73 61 ic number for sa
ae080 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f nity checking */
ae090 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 . int nMem;
ae0a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
ae0b0 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 6c mber of memory l
ae0c0 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 ocations current
ae0d0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a ly allocated */.
ae0e0 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 Mem *aMem;
ae0f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
ae100 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e memory location
ae110 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 6c 6c s */. int nCall
ae120 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f back; /
ae130 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c * Number of call
ae140 62 61 63 6b 73 20 69 6e 76 6f 6b 65 64 20 73 6f backs invoked so
ae150 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 61 far */. int ca
ae160 63 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 cheCtr;
ae170 20 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 /* VdbeCursor
ae180 72 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 row cache genera
ae190 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a tion counter */.
ae1a0 20 20 46 69 66 6f 20 73 46 69 66 6f 3b 20 20 20 Fifo sFifo;
ae1b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 6c /* A l
ae1c0 69 73 74 20 6f 66 20 52 4f 57 49 44 73 20 2a 2f ist of ROWIDs */
ae1d0 0a 20 20 69 6e 74 20 63 6f 6e 74 65 78 74 53 74 . int contextSt
ae1e0 61 63 6b 54 6f 70 3b 20 20 20 20 2f 2a 20 49 6e ackTop; /* In
ae1f0 64 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 dex of top eleme
ae200 6e 74 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 nt in the contex
ae210 74 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 t stack */. int
ae220 20 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 contextStackDep
ae230 74 68 3b 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 th; /* The size
ae240 20 6f 66 20 74 68 65 20 22 63 6f 6e 74 65 78 74 of the "context
ae250 22 20 73 74 61 63 6b 20 2a 2f 0a 20 20 43 6f 6e " stack */. Con
ae260 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 53 74 61 text *contextSta
ae270 63 6b 3b 20 20 2f 2a 20 53 74 61 63 6b 20 75 73 ck; /* Stack us
ae280 65 64 20 62 79 20 6f 70 63 6f 64 65 73 20 43 6f ed by opcodes Co
ae290 6e 74 65 78 74 50 75 73 68 20 26 20 43 6f 6e 74 ntextPush & Cont
ae2a0 65 78 74 50 6f 70 2a 2f 0a 20 20 69 6e 74 20 70 extPop*/. int p
ae2b0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
ae2c0 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 /* The progra
ae2d0 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 m counter */. i
ae2e0 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
ae2f0 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 /* Value
ae300 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 75 to return */. u
ae310 6e 73 69 67 6e 65 64 20 75 6e 69 71 75 65 43 6e nsigned uniqueCn
ae320 74 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 t; /* Used b
ae330 79 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 y OP_MakeRecord
ae340 77 68 65 6e 20 50 32 21 3d 30 20 2a 2f 0a 20 20 when P2!=0 */.
ae350 69 6e 74 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b int errorAction;
ae360 20 20 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 /* Recov
ae370 65 72 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f ery action to do
ae380 20 69 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65 in case of an e
ae390 72 72 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 6e rror */. int in
ae3a0 54 65 6d 70 54 72 61 6e 73 3b 20 20 20 20 20 20 TempTrans;
ae3b0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 65 6d /* True if tem
ae3c0 70 20 64 61 74 61 62 61 73 65 20 69 73 20 74 72 p database is tr
ae3d0 61 6e 73 61 63 74 69 6f 6e 65 64 20 2a 2f 0a 20 ansactioned */.
ae3e0 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b int nResColumn;
ae3f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
ae400 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
ae410 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 one row of the
ae420 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 result set */.
ae430 63 68 61 72 20 2a 2a 61 7a 52 65 73 43 6f 6c 75 char **azResColu
ae440 6d 6e 3b 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 mn; /* Value
ae450 73 20 66 6f 72 20 6f 6e 65 20 72 6f 77 20 6f 66 s for one row of
ae460 20 72 65 73 75 6c 74 20 2a 2f 20 0a 20 20 63 68 result */ . ch
ae470 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 ar *zErrMsg;
ae480 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d /* Error m
ae490 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 68 essage written h
ae4a0 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 ere */. Mem *pR
ae4b0 65 73 75 6c 74 53 65 74 3b 20 20 20 20 20 20 20 esultSet;
ae4c0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 /* Pointer to a
ae4d0 6e 20 61 72 72 61 79 20 6f 66 20 72 65 73 75 6c n array of resul
ae4e0 74 73 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 ts */. u8 expla
ae4f0 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 in;
ae500 2f 2a 20 54 72 75 65 20 69 66 20 45 58 50 4c 41 /* True if EXPLA
ae510 49 4e 20 70 72 65 73 65 6e 74 20 6f 6e 20 53 51 IN present on SQ
ae520 4c 20 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 75 L command */. u
ae530 38 20 63 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 20 8 changeCntOn;
ae540 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
ae550 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 o update the cha
ae560 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 nge-counter */.
ae570 20 75 38 20 65 78 70 69 72 65 64 3b 20 20 20 20 u8 expired;
ae580 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
ae590 20 69 66 20 74 68 65 20 56 4d 20 6e 65 65 64 73 if the VM needs
ae5a0 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 to be recompile
ae5b0 64 20 2a 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69 d */. u8 minWri
ae5c0 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f teFileFormat; /
ae5d0 2a 20 4d 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66 * Minimum file f
ae5e0 6f 72 6d 61 74 20 66 6f 72 20 77 72 69 74 61 62 ormat for writab
ae5f0 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 le database file
ae600 73 20 2a 2f 0a 20 20 75 38 20 69 6e 56 74 61 62 s */. u8 inVtab
ae610 4d 65 74 68 6f 64 3b 20 20 20 20 20 20 20 20 2f Method; /
ae620 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 * See comments a
ae630 62 6f 76 65 20 2a 2f 0a 20 20 75 38 20 75 73 65 bove */. u8 use
ae640 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20 20 sStmtJournal;
ae650 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 73 65 /* True if use
ae660 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f s a statement jo
ae670 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 72 65 urnal */. u8 re
ae680 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 adOnly;
ae690 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 72 /* True for r
ae6a0 65 61 64 2d 6f 6e 6c 79 20 73 74 61 74 65 6d 65 ead-only stateme
ae6b0 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 nts */. int nCh
ae6c0 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 ange;
ae6d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 62 /* Number of db
ae6e0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 73 69 changes made si
ae6f0 6e 63 65 20 6c 61 73 74 20 72 65 73 65 74 20 2a nce last reset *
ae700 2f 0a 20 20 69 36 34 20 73 74 61 72 74 54 69 6d /. i64 startTim
ae710 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 e; /* T
ae720 69 6d 65 20 77 68 65 6e 20 71 75 65 72 79 20 73 ime when query s
ae730 74 61 72 74 65 64 20 2d 20 75 73 65 64 20 66 6f tarted - used fo
ae740 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a 2f 0a 20 r profiling */.
ae750 20 69 6e 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 int btreeMask;
ae760 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d /* Bitm
ae770 61 73 6b 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d ask of db->aDb[]
ae780 20 65 6e 74 72 69 65 73 20 72 65 66 65 72 65 6e entries referen
ae790 63 65 64 20 2a 2f 0a 20 20 42 74 72 65 65 4d 75 ced */. BtreeMu
ae7a0 74 65 78 41 72 72 61 79 20 61 4d 75 74 65 78 3b texArray aMutex;
ae7b0 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 /* An array of
ae7c0 42 74 72 65 65 20 75 73 65 64 20 68 65 72 65 20 Btree used here
ae7d0 61 6e 64 20 6e 65 65 64 69 6e 67 20 6c 6f 63 6b and needing lock
ae7e0 73 20 2a 2f 0a 20 20 69 6e 74 20 61 43 6f 75 6e s */. int aCoun
ae7f0 74 65 72 5b 32 5d 3b 20 20 20 20 20 20 20 20 2f ter[2]; /
ae800 2a 20 43 6f 75 6e 74 65 72 73 20 75 73 65 64 20 * Counters used
ae810 62 79 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f by sqlite3_stmt_
ae820 73 74 61 74 75 73 28 29 20 2a 2f 0a 20 20 69 6e status() */. in
ae830 74 20 6e 53 71 6c 3b 20 20 20 20 20 20 20 20 20 t nSql;
ae840 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
ae850 20 62 79 74 65 73 20 69 6e 20 7a 53 71 6c 20 2a bytes in zSql *
ae860 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 /. char *zSql;
ae870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 /* Tex
ae880 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 t of the SQL sta
ae890 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65 tement that gene
ae8a0 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 23 69 rated this */.#i
ae8b0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
ae8c0 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b G. FILE *trace;
ae8d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
ae8e0 74 65 20 61 6e 20 65 78 65 63 75 74 69 6f 6e 20 te an execution
ae8f0 74 72 61 63 65 20 68 65 72 65 2c 20 69 66 20 6e trace here, if n
ae900 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64 69 ot NULL */.#endi
ae910 66 0a 20 20 69 6e 74 20 6f 70 65 6e 65 64 53 74 f. int openedSt
ae920 61 74 65 6d 65 6e 74 3b 20 20 2f 2a 20 54 72 75 atement; /* Tru
ae930 65 20 69 66 20 74 68 69 73 20 56 4d 20 68 61 73 e if this VM has
ae940 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d opened a statem
ae950 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 ent journal */.#
ae960 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 ifdef SQLITE_SSE
ae970 0a 20 20 69 6e 74 20 66 65 74 63 68 49 64 3b 20 . int fetchId;
ae980 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 /* Stat
ae990 65 6d 65 6e 74 20 6e 75 6d 62 65 72 20 75 73 65 ement number use
ae9a0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 65 74 d by sqlite3_fet
ae9b0 63 68 5f 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a ch_statement */.
ae9c0 20 20 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 int lru;
ae9d0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 /* Count
ae9e0 65 72 20 75 73 65 64 20 66 6f 72 20 4c 52 55 20 er used for LRU
ae9f0 63 61 63 68 65 20 72 65 70 6c 61 63 65 6d 65 6e cache replacemen
aea00 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 t */.#endif.#ifd
aea10 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
aea20 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
aea30 4e 54 0a 20 20 56 64 62 65 20 2a 70 4c 72 75 50 NT. Vdbe *pLruP
aea40 72 65 76 3b 0a 20 20 56 64 62 65 20 2a 70 4c 72 rev;. Vdbe *pLr
aea50 75 4e 65 78 74 3b 0a 23 65 6e 64 69 66 0a 7d 3b uNext;.#endif.};
aea60 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
aea70 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c 6f 77 65 owing are allowe
aea80 64 20 76 61 6c 75 65 73 20 66 6f 72 20 56 64 62 d values for Vdb
aea90 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 66 69 e.magic.*/.#defi
aeaa0 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e ne VDBE_MAGIC_IN
aeab0 49 54 20 20 20 20 20 30 78 32 36 62 63 65 61 61 IT 0x26bceaa
aeac0 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 69 6e 67 5 /* Building
aead0 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 a VDBE program
aeae0 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f */.#define VDBE_
aeaf0 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 20 20 30 MAGIC_RUN 0
aeb00 78 62 64 66 32 30 64 61 33 20 20 20 20 2f 2a 20 xbdf20da3 /*
aeb10 56 44 42 45 20 69 73 20 72 65 61 64 79 20 74 6f VDBE is ready to
aeb20 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 64 65 66 execute */.#def
aeb30 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 ine VDBE_MAGIC_H
aeb40 41 4c 54 20 20 20 20 20 30 78 35 31 39 63 32 39 ALT 0x519c29
aeb50 37 33 20 20 20 20 2f 2a 20 56 44 42 45 20 68 61 73 /* VDBE ha
aeb60 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 s completed exec
aeb70 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 ution */.#define
aeb80 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 44 VDBE_MAGIC_DEAD
aeb90 20 20 20 20 20 30 78 62 36 30 36 63 33 63 38 20 0xb606c3c8
aeba0 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 68 /* The VDBE h
aebb0 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 as been dealloca
aebc0 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 75 ted */../*.** Fu
aebd0 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 nction prototype
aebe0 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 s.*/.SQLITE_PRIV
aebf0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
aec00 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 56 VdbeFreeCursor(V
aec10 64 62 65 20 2a 2c 20 56 64 62 65 43 75 72 73 6f dbe *, VdbeCurso
aec20 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74 65 r*);.void sqlite
aec30 56 64 62 65 50 6f 70 53 74 61 63 6b 28 56 64 62 VdbePopStack(Vdb
aec40 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f e*,int);.SQLITE_
aec50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
aec60 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 te3VdbeCursorMov
aec70 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 2a 29 eto(VdbeCursor*)
aec80 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 ;.#if defined(SQ
aec90 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 LITE_DEBUG) || d
aeca0 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 efined(VDBE_PROF
aecb0 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 ILE).SQLITE_PRIV
aecc0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
aecd0 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 VdbePrintOp(FILE
aece0 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a 23 65 *, int, Op*);.#e
aecf0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
aed00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
aed10 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e dbeSerialTypeLen
aed20 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (u32);.SQLITE_PR
aed30 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
aed40 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 3VdbeSerialType(
aed50 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 Mem*, int);.SQLI
aed60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
aed70 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
aed80 50 75 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 Put(unsigned cha
aed90 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c 20 69 r*, int, Mem*, i
aeda0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
aedb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
aedc0 64 62 65 53 65 72 69 61 6c 47 65 74 28 63 6f 6e dbeSerialGet(con
aedd0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
aede0 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 3b 0a 53 *, u32, Mem*);.S
aedf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
aee00 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 id sqlite3VdbeDe
aee10 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 62 65 leteAuxData(Vdbe
aee20 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a 69 6e Func*, int);..in
aee30 74 20 73 71 6c 69 74 65 32 42 74 72 65 65 4b 65 t sqlite2BtreeKe
aee40 79 43 6f 6d 70 61 72 65 28 42 74 43 75 72 73 6f yCompare(BtCurso
aee50 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 r *, const void
aee60 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
aee70 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
aee80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
aee90 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 dbeIdxKeyCompare
aeea0 28 56 64 62 65 43 75 72 73 6f 72 2a 2c 55 6e 70 (VdbeCursor*,Unp
aeeb0 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c 69 6e 74 ackedRecord*,int
aeec0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
aeed0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
aeee0 62 65 49 64 78 52 6f 77 69 64 28 42 74 43 75 72 beIdxRowid(BtCur
aeef0 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 53 sor *, i64 *);.S
aef00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
aef10 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 t sqlite3MemComp
aef20 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 are(const Mem*,
aef30 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 const Mem*, cons
aef40 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c t CollSeq*);.SQL
aef50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
aef60 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 sqlite3VdbeExec(
aef70 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Vdbe*);.SQLITE_P
aef80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
aef90 65 33 56 64 62 65 4c 69 73 74 28 56 64 62 65 2a e3VdbeList(Vdbe*
aefa0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
aefb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
aefc0 65 48 61 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 eHalt(Vdbe*);.SQ
aefd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
aefe0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
aeff0 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a geEncoding(Mem *
af000 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
af010 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
af020 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 e3VdbeMemTooBig(
af030 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
af040 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
af050 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 3VdbeMemCopy(Mem
af060 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 3b 0a *, const Mem*);.
af070 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
af080 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
af090 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 emShallowCopy(Me
af0a0 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 m*, const Mem*,
af0b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
af0c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
af0d0 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 3VdbeMemMove(Mem
af0e0 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 *, Mem*);.SQLITE
af0f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
af100 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
af110 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 3b 0a 53 rminate(Mem*);.S
af120 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
af130 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
af140 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 63 6f 6e SetStr(Mem*, con
af150 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 75 st char*, int, u
af160 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 8, void(*)(void*
af170 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ));.SQLITE_PRIVA
af180 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
af190 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d dbeMemSetInt64(M
af1a0 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 em*, i64);.SQLIT
af1b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
af1c0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
af1d0 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75 Double(Mem*, dou
af1e0 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ble);.SQLITE_PRI
af1f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
af200 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
af210 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
af220 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
af230 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f e3VdbeMemSetZero
af240 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29 3b 0a Blob(Mem*,int);.
af250 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
af260 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
af270 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d mMakeWriteable(M
af280 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
af290 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
af2a0 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 VdbeMemStringify
af2b0 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c (Mem*, int);.SQL
af2c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 ITE_PRIVATE i64
af2d0 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 sqlite3VdbeIntVa
af2e0 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 lue(Mem*);.SQLIT
af2f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
af300 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 lite3VdbeMemInte
af310 67 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 gerify(Mem*);.SQ
af320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 LITE_PRIVATE dou
af330 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 ble sqlite3VdbeR
af340 65 61 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a ealValue(Mem*);.
af350 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
af360 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 oid sqlite3VdbeI
af370 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d ntegerAffinity(M
af380 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
af390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
af3a0 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d VdbeMemRealify(M
af3b0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
af3c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
af3d0 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 VdbeMemNumerify(
af3e0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
af3f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
af400 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 3VdbeMemFromBtre
af410 65 28 42 74 43 75 72 73 6f 72 2a 2c 69 6e 74 2c e(BtCursor*,int,
af420 69 6e 74 2c 69 6e 74 2c 4d 65 6d 2a 29 3b 0a 53 int,int,Mem*);.S
af430 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
af440 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
af450 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 mRelease(Mem *p)
af460 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
af470 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
af480 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 eMemReleaseExter
af490 6e 61 6c 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c nal(Mem *p);.SQL
af4a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
af4b0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 sqlite3VdbeMemFi
af4c0 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 6e nalize(Mem*, Fun
af4d0 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 cDef*);.SQLITE_P
af4e0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
af4f0 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 r *sqlite3Opcode
af500 4e 61 6d 65 28 69 6e 74 29 3b 0a 53 51 4c 49 54 Name(int);.SQLIT
af510 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
af520 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 lite3VdbeOpcodeH
af530 61 73 50 72 6f 70 65 72 74 79 28 69 6e 74 2c 20 asProperty(int,
af540 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
af550 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
af560 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 20 VdbeMemGrow(Mem
af570 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 6e *pMem, int n, in
af580 74 20 70 72 65 73 65 72 76 65 29 3b 0a 23 69 66 t preserve);.#if
af590 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
af5a0 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
af5b0 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ENT.SQLITE_PRIVA
af5c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
af5d0 62 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 beReleaseBuffers
af5e0 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e 64 69 (Vdbe *p);.#endi
af5f0 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 f..#ifndef NDEBU
af600 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
af610 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 void sqlite3V
af620 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65 6d dbeMemSanity(Mem
af630 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 *);.#endif.SQLIT
af640 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
af650 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e lite3VdbeMemTran
af660 73 6c 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b slate(Mem*, u8);
af670 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
af680 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
af690 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
af6a0 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 e3VdbePrintSql(V
af6b0 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
af6c0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
af6d0 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 ite3VdbeMemPrett
af6e0 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d yPrint(Mem *pMem
af6f0 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 , char *zBuf);.#
af700 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
af710 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
af720 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d VdbeMemHandleBom
af730 28 4d 65 6d 20 2a 70 4d 65 6d 29 3b 0a 53 51 4c (Mem *pMem);.SQL
af740 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
af750 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f sqlite3VdbeFifo
af760 49 6e 69 74 28 46 69 66 6f 2a 2c 20 73 71 6c 69 Init(Fifo*, sqli
af770 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
af780 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
af790 33 56 64 62 65 46 69 66 6f 50 75 73 68 28 46 69 3VdbeFifoPush(Fi
af7a0 66 6f 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 fo*, i64);.SQLIT
af7b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
af7c0 6c 69 74 65 33 56 64 62 65 46 69 66 6f 50 6f 70 lite3VdbeFifoPop
af7d0 28 46 69 66 6f 2a 2c 20 69 36 34 2a 29 3b 0a 53 (Fifo*, i64*);.S
af7e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
af7f0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 id sqlite3VdbeFi
af800 66 6f 43 6c 65 61 72 28 46 69 66 6f 2a 29 3b 0a foClear(Fifo*);.
af810 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
af820 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 OMIT_INCRBLOB.SQ
af830 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
af840 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
af850 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 mExpandBlob(Mem
af860 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 *);.#else. #def
af870 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d ine sqlite3VdbeM
af880 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 78 29 20 emExpandBlob(x)
af890 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 SQLITE_OK.#endif
af8a0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 ..#endif /* !def
af8b0 69 6e 65 64 28 5f 56 44 42 45 49 4e 54 5f 48 5f ined(_VDBEINT_H_
af8c0 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
af8d0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
af8e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
af8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af910 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
af920 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
af930 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
af940 66 66 20 69 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a ff in utf.c ****
af950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
af960 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ****/../*.** The
af970 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 following const
af980 61 6e 74 20 76 61 6c 75 65 20 69 73 20 75 73 65 ant value is use
af990 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f d by the SQLITE_
af9a0 42 49 47 45 4e 44 49 41 4e 20 61 6e 64 0a 2a 2a BIGENDIAN and.**
af9b0 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e SQLITE_LITTLEEN
af9c0 44 49 41 4e 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a DIAN macros..*/.
af9d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
af9e0 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33 onst int sqlite3
af9f0 6f 6e 65 20 3d 20 31 3b 0a 0a 2f 2a 0a 2a 2a 20 one = 1;../*.**
afa00 54 68 69 73 20 6c 6f 6f 6b 75 70 20 74 61 62 6c This lookup tabl
afa10 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 65 6c e is used to hel
afa20 70 20 64 65 63 6f 64 65 20 74 68 65 20 66 69 72 p decode the fir
afa30 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 61 20 st byte of.** a
afa40 6d 75 6c 74 69 2d 62 79 74 65 20 55 54 46 38 20 multi-byte UTF8
afa50 63 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 73 74 character..*/.st
afa60 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
afa70 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 ned char sqlite3
afa80 55 74 66 54 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a UtfTrans1[] = {.
afa90 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 0x00, 0x01, 0x
afaa0 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 02, 0x03, 0x04,
afab0 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 0x05, 0x06, 0x07
afac0 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 ,. 0x08, 0x09,
afad0 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 0x0a, 0x0b, 0x0c
afae0 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 , 0x0d, 0x0e, 0x
afaf0 30 66 2c 0a 20 20 30 78 31 30 2c 20 30 78 31 31 0f,. 0x10, 0x11
afb00 2c 20 30 78 31 32 2c 20 30 78 31 33 2c 20 30 78 , 0x12, 0x13, 0x
afb10 31 34 2c 20 30 78 31 35 2c 20 30 78 31 36 2c 20 14, 0x15, 0x16,
afb20 30 78 31 37 2c 0a 20 20 30 78 31 38 2c 20 30 78 0x17,. 0x18, 0x
afb30 31 39 2c 20 30 78 31 61 2c 20 30 78 31 62 2c 20 19, 0x1a, 0x1b,
afb40 30 78 31 63 2c 20 30 78 31 64 2c 20 30 78 31 65 0x1c, 0x1d, 0x1e
afb50 2c 20 30 78 31 66 2c 0a 20 20 30 78 30 30 2c 20 , 0x1f,. 0x00,
afb60 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 0x01, 0x02, 0x03
afb70 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 , 0x04, 0x05, 0x
afb80 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 06, 0x07,. 0x08
afb90 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 , 0x09, 0x0a, 0x
afba0 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 0b, 0x0c, 0x0d,
afbb0 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 0x0e, 0x0f,. 0x
afbc0 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 00, 0x01, 0x02,
afbd0 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 0x03, 0x04, 0x05
afbe0 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 , 0x06, 0x07,.
afbf0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 0x00, 0x01, 0x02
afc00 2c 20 30 78 30 33 2c 20 30 78 30 30 2c 20 30 78 , 0x03, 0x00, 0x
afc10 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 0a 01, 0x00, 0x00,.
afc20 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 };...#define WRI
afc30 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 TE_UTF8(zOut, c)
afc40 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
afc50 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
afc60 69 66 28 20 63 3c 30 78 30 30 30 38 30 20 29 7b if( c<0x00080 ){
afc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afc90 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
afca0 2b 2b 20 3d 20 28 63 26 30 78 46 46 29 3b 20 20 ++ = (c&0xFF);
afcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
afcd0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 }
afce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afd00 20 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 \. else
afd10 69 66 28 20 63 3c 30 78 30 30 38 30 30 20 29 7b if( c<0x00800 ){
afd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afd40 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
afd50 30 78 43 30 20 2b 20 28 28 63 3e 3e 36 29 26 30 0xC0 + ((c>>6)&0
afd60 78 31 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x1F);
afd70 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
afd80 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 zOut++ = 0x80 +
afd90 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 (c & 0x3F);
afda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afdb0 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 \. }
afdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afde0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65 \. e
afdf0 6c 73 65 20 69 66 28 20 63 3c 30 78 31 30 30 30 lse if( c<0x1000
afe00 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
afe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afe20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
afe30 2b 20 3d 20 30 78 45 30 20 2b 20 28 28 63 3e 3e + = 0xE0 + ((c>>
afe40 31 32 29 26 30 78 30 46 29 3b 20 20 20 20 20 20 12)&0x0F);
afe50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
afe60 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
afe70 30 20 2b 20 28 28 63 3e 3e 36 29 20 26 20 30 78 0 + ((c>>6) & 0x
afe80 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3F);
afe90 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
afea0 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 63 20 t++ = 0x80 + (c
afeb0 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 & 0x3F);
afec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
afed0 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 . }else{
afee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
afef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aff00 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
aff10 4f 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b 20 28 Out++ = 0xF0 + (
aff20 28 63 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b (c>>18) & 0x07);
aff30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aff40 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
aff50 20 30 78 38 30 20 2b 20 28 28 63 3e 3e 31 32 29 0x80 + ((c>>12)
aff60 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 & 0x3F);
aff70 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
aff80 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b *zOut++ = 0x80 +
aff90 20 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 ((c>>6) & 0x3F)
affa0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
affb0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
affc0 20 3d 20 30 78 38 30 20 2b 20 28 63 20 26 20 30 = 0x80 + (c & 0
affd0 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x3F);
affe0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
afff0 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
b0000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0020 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e \.}..#defin
b0030 65 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 e WRITE_UTF16LE(
b0040 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 zOut, c) {
b0050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0060 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 \. if
b0070 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 ( c<=0xFFFF ){
b0080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b00a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
b00b0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30 *zOut++ = (c&0
b00c0 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 x00FF);
b00d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b00e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
b00f0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28 *zOut++ = ((
b0100 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 c>>8)&0x00FF);
b0110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0130 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 \. }else{
b0140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0170 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
b0180 3d 20 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30 = (((c>>10)&0x00
b0190 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30 3F) + (((c-0x100
b01a0 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29 00)>>10)&0x00C0)
b01b0 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b ); \. *zOut+
b01c0 2b 20 3d 20 28 30 78 30 30 44 38 20 2b 20 28 28 + = (0x00D8 + ((
b01d0 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 (c-0x10000)>>18)
b01e0 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 &0x03));
b01f0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
b0200 74 2b 2b 20 3d 20 28 63 26 30 78 30 30 46 46 29 t++ = (c&0x00FF)
b0210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b0220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0230 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
b0240 4f 75 74 2b 2b 20 3d 20 28 30 78 30 30 44 43 20 Out++ = (0x00DC
b0250 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 + ((c>>8)&0x03))
b0260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
b0270 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 \. }
b0280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b02a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b02b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a \.}.
b02c0 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 .#define WRITE_U
b02d0 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 20 TF16BE(zOut, c)
b02e0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
b02f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0300 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46 \. if( c<=0xFF
b0310 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 FF ){
b0320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0340 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
b0350 20 3d 20 28 28 63 3e 3e 38 29 26 30 78 30 30 46 = ((c>>8)&0x00F
b0360 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
b0370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0380 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
b0390 2b 2b 20 3d 20 28 63 26 30 78 30 30 46 46 29 3b ++ = (c&0x00FF);
b03a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b03b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b03c0 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 \. }else
b03d0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
b03e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b03f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0400 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
b0410 7a 4f 75 74 2b 2b 20 3d 20 28 30 78 30 30 44 38 zOut++ = (0x00D8
b0420 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 + (((c-0x10000)
b0430 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 >>18)&0x03));
b0440 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
b0450 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28 28 63 3e *zOut++ = (((c>
b0460 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 >10)&0x003F) + (
b0470 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 ((c-0x10000)>>10
b0480 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 )&0x00C0)); \.
b0490 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 30 78 *zOut++ = (0x
b04a0 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 00DC + ((c>>8)&0
b04b0 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 x03));
b04c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
b04d0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
b04e0 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 c&0x00FF);
b04f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0510 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
b0520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0550 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 \.}..#define
b0560 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e READ_UTF16LE(zIn
b0570 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20 , c){
b0580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
b05a0 20 20 63 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 c = (*zIn++);
b05b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b05c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b05d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b05e0 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 \. c += (
b05f0 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 (*zIn++)<<8);
b0600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
b0630 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20 if( c>=0xD800
b0640 26 26 20 63 3c 30 78 45 30 30 30 20 29 7b 20 20 && c<0xE000 ){
b0650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0670 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 \. int c
b0680 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 2 = (*zIn++);
b0690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b06a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b06b0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
b06c0 20 20 20 63 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b c2 += ((*zIn+
b06d0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 +)<<8);
b06e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b06f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0700 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 \. c = (
b0710 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 63 c2&0x03FF) + ((c
b0720 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 &0x003F)<<10) +
b0730 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 30 (((c&0x03C0)+0x0
b0740 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 040)<<10); \.
b0750 20 20 20 69 66 28 20 28 63 20 26 20 30 78 46 46 if( (c & 0xFF
b0760 46 46 30 30 30 30 29 3d 3d 30 20 29 20 63 20 3d FF0000)==0 ) c =
b0770 20 30 78 46 46 46 44 3b 20 20 20 20 20 20 20 20 0xFFFD;
b0780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0790 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 \. }
b07a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b07b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b07c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b07d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
b07e0 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 ..#define READ_U
b07f0 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20 TF16BE(zIn, c){
b0800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0820 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 \. c =
b0830 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 ((*zIn++)<<8);
b0840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0870 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b \. c += (*zIn++
b0880 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
b0890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b08a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b08b0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
b08c0 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 c>=0xD800 && c<0
b08d0 78 45 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 xE000 ){
b08e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b08f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
b0900 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 28 . int c2 = ((
b0910 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 *zIn++)<<8);
b0920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0940 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 \. c2
b0950 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 += (*zIn++);
b0960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
b0990 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 . c = (c2&0x0
b09a0 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 3FF) + ((c&0x003
b09b0 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 F)<<10) + (((c&0
b09c0 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c x03C0)+0x0040)<<
b09d0 31 30 29 3b 20 20 20 5c 0a 20 20 20 20 69 66 28 10); \. if(
b09e0 20 28 63 20 26 20 30 78 46 46 46 46 30 30 30 30 (c & 0xFFFF0000
b09f0 29 3d 3d 30 20 29 20 63 20 3d 20 30 78 46 46 46 )==0 ) c = 0xFFF
b0a00 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D;
b0a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
b0a20 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 . }
b0a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0a60 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a \.}../*.*
b0a70 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69 * Translate a si
b0a80 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61 ngle UTF-8 chara
b0a90 63 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68 cter. Return th
b0aa0 65 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 2e e unicode value.
b0ab0 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74 72 .**.** During tr
b0ac0 61 6e 73 6c 61 74 69 6f 6e 2c 20 61 73 73 75 6d anslation, assum
b0ad0 65 20 74 68 61 74 20 74 68 65 20 62 79 74 65 20 e that the byte
b0ae0 74 68 61 74 20 7a 54 65 72 6d 20 70 6f 69 6e 74 that zTerm point
b0af0 73 0a 2a 2a 20 69 73 20 61 20 30 78 30 30 2e 0a s.** is a 0x00..
b0b00 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 70 6f **.** Write a po
b0b10 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78 inter to the nex
b0b20 74 20 75 6e 72 65 61 64 20 62 79 74 65 20 62 61 t unread byte ba
b0b30 63 6b 20 69 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e ck into *pzNext.
b0b40 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20 .**.** Notes On
b0b50 49 6e 76 61 6c 69 64 20 55 54 46 2d 38 3a 0a 2a Invalid UTF-8:.*
b0b60 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f *.** * This ro
b0b70 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f utine never allo
b0b80 77 73 20 61 20 37 2d 62 69 74 20 63 68 61 72 61 ws a 7-bit chara
b0b90 63 74 65 72 20 28 30 78 30 30 20 74 68 72 6f 75 cter (0x00 throu
b0ba0 67 68 20 30 78 37 66 29 20 74 6f 0a 2a 2a 20 20 gh 0x7f) to.**
b0bb0 20 20 20 62 65 20 65 6e 63 6f 64 65 64 20 61 73 be encoded as
b0bc0 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 a multi-byte ch
b0bd0 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 6d 75 aracter. Any mu
b0be0 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 lti-byte charact
b0bf0 65 72 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 61 er that.** a
b0c00 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 ttempts to encod
b0c10 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 e a value betwee
b0c20 6e 20 30 78 30 30 20 61 6e 64 20 30 78 37 66 20 n 0x00 and 0x7f
b0c30 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 is rendered as 0
b0c40 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 xfffd..**.** *
b0c50 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 This routine ne
b0c60 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 55 54 46 ver allows a UTF
b0c70 31 36 20 73 75 72 72 6f 67 61 74 65 20 76 61 6c 16 surrogate val
b0c80 75 65 20 74 6f 20 62 65 20 65 6e 63 6f 64 65 64 ue to be encoded
b0c90 2e 0a 2a 2a 20 20 20 20 20 49 66 20 61 20 6d 75 ..** If a mu
b0ca0 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 lti-byte charact
b0cb0 65 72 20 61 74 74 65 6d 70 74 73 20 74 6f 20 65 er attempts to e
b0cc0 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 62 65 ncode a value be
b0cd0 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 30 78 64 tween.** 0xd
b0ce0 38 30 30 20 61 6e 64 20 30 78 65 30 30 30 20 74 800 and 0xe000 t
b0cf0 68 65 6e 20 69 74 20 69 73 20 72 65 6e 64 65 72 hen it is render
b0d00 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a ed as 0xfffd..**
b0d10 0a 2a 2a 20 20 2a 20 20 42 79 74 65 73 20 69 6e .** * Bytes in
b0d20 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 30 78 the range of 0x
b0d30 38 30 20 74 68 72 6f 75 67 68 20 30 78 62 66 20 80 through 0xbf
b0d40 77 68 69 63 68 20 6f 63 63 75 72 20 61 73 20 74 which occur as t
b0d50 68 65 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 he first.**
b0d60 62 79 74 65 20 6f 66 20 61 20 63 68 61 72 61 63 byte of a charac
b0d70 74 65 72 20 61 72 65 20 69 6e 74 65 72 70 72 65 ter are interpre
b0d80 74 65 64 20 61 73 20 73 69 6e 67 6c 65 2d 62 79 ted as single-by
b0d90 74 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a te characters.**
b0da0 20 20 20 20 20 61 6e 64 20 72 65 6e 64 65 72 65 and rendere
b0db0 64 20 61 73 20 74 68 65 6d 73 65 6c 76 65 73 20 d as themselves
b0dc0 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79 even though they
b0dd0 20 61 72 65 20 74 65 63 68 6e 69 63 61 6c 6c 79 are technically
b0de0 0a 2a 2a 20 20 20 20 20 69 6e 76 61 6c 69 64 20 .** invalid
b0df0 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a characters..**.*
b0e00 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 * * This routi
b0e10 6e 65 20 61 63 63 65 70 74 73 20 61 6e 20 69 6e ne accepts an in
b0e20 66 69 6e 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 finite number of
b0e30 20 64 69 66 66 65 72 65 6e 74 20 55 54 46 38 20 different UTF8
b0e40 65 6e 63 6f 64 69 6e 67 73 0a 2a 2a 20 20 20 20 encodings.**
b0e50 20 66 6f 72 20 75 6e 69 63 6f 64 65 20 76 61 6c for unicode val
b0e60 75 65 73 20 30 78 38 30 20 61 6e 64 20 67 72 65 ues 0x80 and gre
b0e70 61 74 65 72 2e 20 20 49 74 20 64 6f 20 6e 6f 74 ater. It do not
b0e80 20 63 68 61 6e 67 65 20 6f 76 65 72 2d 6c 65 6e change over-len
b0e90 67 74 68 0a 2a 2a 20 20 20 20 20 65 6e 63 6f 64 gth.** encod
b0ea0 69 6e 67 73 20 74 6f 20 30 78 66 66 66 64 20 61 ings to 0xfffd a
b0eb0 73 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 72 s some systems r
b0ec0 65 63 6f 6d 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65 ecommend..*/.#de
b0ed0 66 69 6e 65 20 52 45 41 44 5f 55 54 46 38 28 7a fine READ_UTF8(z
b0ee0 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 20 20 20 In, zTerm, c)
b0ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0f00 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 \. c =
b0f10 2a 28 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 *(zIn++);
b0f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0f40 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d \. if( c>=
b0f50 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 20 20 0xc0 ){
b0f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b0f80 20 20 5c 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 \. c = sqli
b0f90 74 65 33 55 74 66 54 72 61 6e 73 31 5b 63 2d 30 te3UtfTrans1[c-0
b0fa0 78 63 30 5d 3b 20 20 20 20 20 20 20 20 20 20 20 xc0];
b0fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
b0fc0 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 21 . while( zIn!
b0fd0 3d 7a 54 65 72 6d 20 26 26 20 28 2a 7a 49 6e 20 =zTerm && (*zIn
b0fe0 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b & 0xc0)==0x80 ){
b0ff0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
b1000 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b c = (c<<6) +
b1010 20 28 30 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b (0x3f & *(zIn++
b1020 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ));
b1030 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 7d \. }
b1040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1070 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 28 20 \. if(
b1080 63 3c 30 78 38 30 20 20 20 20 20 20 20 20 20 20 c<0x80
b1090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b10a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b10b0 20 20 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 \. ||
b10c0 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d (c&0xFFFFF800)==
b10d0 30 78 44 38 30 30 20 20 20 20 20 20 20 20 20 20 0xD800
b10e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b10f0 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 \. || (c&
b1100 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 0xFFFFFFFE)==0xF
b1110 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 46 FFE ){ c = 0xFF
b1120 46 44 3b 20 7d 20 20 20 20 20 20 20 20 5c 0a 20 FD; } \.
b1130 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }.SQLITE_PRIVAT
b1140 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 E int sqlite3Utf
b1150 38 52 65 61 64 28 0a 20 20 63 6f 6e 73 74 20 75 8Read(. const u
b1160 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c nsigned char *z,
b1170 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
b1180 74 20 62 79 74 65 20 6f 66 20 55 54 46 2d 38 20 t byte of UTF-8
b1190 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 63 character */. c
b11a0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
b11b0 61 72 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 2f ar *zTerm, /
b11c0 2a 20 50 72 65 74 65 6e 64 20 74 68 69 73 20 62 * Pretend this b
b11d0 79 74 65 20 69 73 20 30 78 30 30 20 2a 2f 0a 20 yte is 0x00 */.
b11e0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
b11f0 63 68 61 72 20 2a 2a 70 7a 4e 65 78 74 20 20 20 char **pzNext
b1200 20 2f 2a 20 57 72 69 74 65 20 66 69 72 73 74 20 /* Write first
b1210 62 79 74 65 20 70 61 73 74 20 55 54 46 2d 38 20 byte past UTF-8
b1220 63 68 61 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a char here */.){.
b1230 20 20 69 6e 74 20 63 3b 0a 20 20 52 45 41 44 5f int c;. READ_
b1240 55 54 46 38 28 7a 2c 20 7a 54 65 72 6d 2c 20 63 UTF8(z, zTerm, c
b1250 29 3b 0a 20 20 2a 70 7a 4e 65 78 74 20 3d 20 7a );. *pzNext = z
b1260 3b 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a ;. return c;.}.
b1270 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 ..../*.** If the
b1280 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 TRANSLATE_TRACE
b1290 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 macro is define
b12a0 64 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 d, the value of
b12b0 65 61 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 each Mem is.** p
b12c0 72 69 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72 rinted on stderr
b12d0 20 6f 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f on the way into
b12e0 20 61 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 and out of sqli
b12f0 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c te3VdbeMemTransl
b1300 61 74 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 ate()..*/ ./* #d
b1310 65 66 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f efine TRANSLATE_
b1320 54 52 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e TRACE 1 */..#ifn
b1330 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
b1340 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 UTF16./*.** This
b1350 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f routine transfo
b1360 72 6d 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c rms the internal
b1370 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 text encoding u
b1380 73 65 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a sed by pMem to.*
b1390 2a 20 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74 * desiredEnc. It
b13a0 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 is an error if
b13b0 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c the string is al
b13c0 72 65 61 64 79 20 6f 66 20 74 68 65 20 64 65 73 ready of the des
b13d0 69 72 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 ired.** encoding
b13e0 2c 20 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f , or if *pMem do
b13f0 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 es not contain a
b1400 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a string value..*
b1410 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b1420 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
b1430 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d MemTranslate(Mem
b1440 20 2a 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72 *pMem, u8 desir
b1450 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 6c 65 edEnc){. int le
b1460 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
b1470 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
b1480 20 6c 65 6e 67 74 68 20 6f 66 20 6f 75 74 70 75 length of outpu
b1490 74 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65 t string in byte
b14a0 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 s */. unsigned
b14b0 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 char *zOut;
b14c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
b14d0 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f Output buffer */
b14e0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
b14f0 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 20 *zIn;
b1500 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 /* Inpu
b1510 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 t iterator */.
b1520 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
b1530 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 Term;
b1540 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 /* End of
b1550 69 6e 70 75 74 20 2a 2f 0a 20 20 75 6e 73 69 67 input */. unsig
b1560 6e 65 64 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 ned char *z;
b1570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b1580 20 2f 2a 20 4f 75 74 70 75 74 20 69 74 65 72 61 /* Output itera
b1590 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 tor */. unsigne
b15a0 64 20 69 6e 74 20 63 3b 0a 0a 20 20 61 73 73 65 d int c;.. asse
b15b0 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
b15c0 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
b15d0 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
b15e0 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
b15f0 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 rt( pMem->flags&
b1600 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 61 73 73 MEM_Str );. ass
b1610 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d ert( pMem->enc!=
b1620 64 65 73 69 72 65 64 45 6e 63 20 29 3b 0a 20 20 desiredEnc );.
b1630 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e assert( pMem->en
b1640 63 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 c!=0 );. assert
b1650 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a ( pMem->n>=0 );.
b1660 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 .#if defined(TRA
b1670 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 NSLATE_TRACE) &&
b1680 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
b1690 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 DEBUG). {. c
b16a0 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 har zBuf[100];.
b16b0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
b16c0 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 mPrettyPrint(pMe
b16d0 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 m, zBuf);. fp
b16e0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 49 rintf(stderr, "I
b16f0 4e 50 55 54 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 NPUT: %s\n", zB
b1700 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a uf);. }.#endif.
b1710 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 61 . /* If the tra
b1720 6e 73 6c 61 74 69 6f 6e 20 69 73 20 62 65 74 77 nslation is betw
b1730 65 65 6e 20 55 54 46 2d 31 36 20 6c 69 74 74 6c een UTF-16 littl
b1740 65 20 61 6e 64 20 62 69 67 20 65 6e 64 69 61 6e e and big endian
b1750 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c , then . ** all
b1760 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 that is require
b1770 64 20 69 73 20 74 6f 20 73 77 61 70 20 74 68 65 d is to swap the
b1780 20 62 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69 byte order. Thi
b1790 73 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 s case is handle
b17a0 64 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 d. ** different
b17b0 6c 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 ly from the othe
b17c0 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 rs.. */. if( p
b17d0 4d 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 Mem->enc!=SQLITE
b17e0 5f 55 54 46 38 20 26 26 20 64 65 73 69 72 65 64 _UTF8 && desired
b17f0 45 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 Enc!=SQLITE_UTF8
b1800 20 29 7b 0a 20 20 20 20 75 38 20 74 65 6d 70 3b ){. u8 temp;
b1810 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 . int rc;.
b1820 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
b1830 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c eMemMakeWriteabl
b1840 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 e(pMem);. if(
b1850 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
b1860 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
b1870 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
b1880 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e );. return
b1890 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
b18a0 20 20 20 7d 0a 20 20 20 20 7a 49 6e 20 3d 20 28 }. zIn = (
b18b0 75 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 u8*)pMem->z;.
b18c0 20 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d zTerm = &zIn[pM
b18d0 65 6d 2d 3e 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 em->n&~1];. w
b18e0 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 hile( zIn<zTerm
b18f0 29 7b 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 ){. temp =
b1900 2a 7a 49 6e 3b 0a 20 20 20 20 20 20 2a 7a 49 6e *zIn;. *zIn
b1910 20 3d 20 2a 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 = *(zIn+1);.
b1920 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 zIn++;.
b1930 2a 7a 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 *zIn++ = temp;.
b1940 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 }. pMem->e
b1950 6e 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b nc = desiredEnc;
b1960 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 6c . goto transl
b1970 61 74 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 ate_out;. }..
b1980 2f 2a 20 53 65 74 20 6c 65 6e 20 74 6f 20 74 68 /* Set len to th
b1990 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
b19a0 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 of bytes requir
b19b0 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 ed in the output
b19c0 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 66 buffer. */. if
b19d0 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 ( desiredEnc==SQ
b19e0 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 LITE_UTF8 ){.
b19f0 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 /* When convert
b1a00 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d 31 36 2c ing from UTF-16,
b1a10 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f the maximum gro
b1a20 77 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d wth results from
b1a30 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 . ** translat
b1a40 69 6e 67 20 61 20 32 2d 62 79 74 65 20 63 68 61 ing a 2-byte cha
b1a50 72 61 63 74 65 72 20 74 6f 20 61 20 34 2d 62 79 racter to a 4-by
b1a60 74 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 te UTF-8 charact
b1a70 65 72 2e 0a 20 20 20 20 2a 2a 20 41 20 73 69 6e er.. ** A sin
b1a80 67 6c 65 20 62 79 74 65 20 69 73 20 72 65 71 75 gle byte is requ
b1a90 69 72 65 64 20 66 6f 72 20 74 68 65 20 6f 75 74 ired for the out
b1aa0 70 75 74 20 73 74 72 69 6e 67 0a 20 20 20 20 2a put string. *
b1ab0 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 * nul-terminator
b1ac0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4d 65 .. */. pMe
b1ad0 6d 2d 3e 6e 20 26 3d 20 7e 31 3b 0a 20 20 20 20 m->n &= ~1;.
b1ae0 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 len = pMem->n *
b1af0 32 20 2b 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 2 + 1;. }else{.
b1b00 20 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 /* When conv
b1b10 65 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d erting from UTF-
b1b20 38 20 74 6f 20 55 54 46 2d 31 36 20 74 68 65 20 8 to UTF-16 the
b1b30 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 69 maximum growth i
b1b40 73 20 63 61 75 73 65 64 0a 20 20 20 20 2a 2a 20 s caused. **
b1b50 77 68 65 6e 20 61 20 31 2d 62 79 74 65 20 55 54 when a 1-byte UT
b1b60 46 2d 38 20 63 68 61 72 61 63 74 65 72 20 69 73 F-8 character is
b1b70 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f translated into
b1b80 20 61 20 32 2d 62 79 74 65 20 55 54 46 2d 31 36 a 2-byte UTF-16
b1b90 0a 20 20 20 20 2a 2a 20 63 68 61 72 61 63 74 65 . ** characte
b1ba0 72 2e 20 54 77 6f 20 62 79 74 65 73 20 61 72 65 r. Two bytes are
b1bb0 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 required in the
b1bc0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 66 output buffer f
b1bd0 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 75 or the. ** nu
b1be0 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 l-terminator..
b1bf0 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70 */. len = p
b1c00 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a Mem->n * 2 + 2;.
b1c10 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 7a 49 }.. /* Set zI
b1c20 6e 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 n to point at th
b1c30 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 69 e start of the i
b1c40 6e 70 75 74 20 62 75 66 66 65 72 20 61 6e 64 20 nput buffer and
b1c50 7a 54 65 72 6d 20 74 6f 20 70 6f 69 6e 74 20 31 zTerm to point 1
b1c60 0a 20 20 2a 2a 20 62 79 74 65 20 70 61 73 74 20 . ** byte past
b1c70 74 68 65 20 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 the end.. **.
b1c80 2a 2a 20 56 61 72 69 61 62 6c 65 20 7a 4f 75 74 ** Variable zOut
b1c90 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 is set to point
b1ca0 20 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 at the output b
b1cb0 75 66 66 65 72 2c 20 73 70 61 63 65 20 6f 62 74 uffer, space obt
b1cc0 61 69 6e 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 ained. ** from
b1cd0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
b1ce0 2e 0a 20 20 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 .. */. zIn = (
b1cf0 75 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a u8*)pMem->z;. z
b1d00 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d Term = &zIn[pMem
b1d10 2d 3e 6e 5d 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 ->n];. zOut = s
b1d20 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
b1d30 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 w(pMem->db, len)
b1d40 3b 0a 20 20 69 66 28 20 21 7a 4f 75 74 20 29 7b ;. if( !zOut ){
b1d50 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
b1d60 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
b1d70 7a 20 3d 20 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 z = zOut;.. if(
b1d80 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 pMem->enc==SQLI
b1d90 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 69 TE_UTF8 ){. i
b1da0 66 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 f( desiredEnc==S
b1db0 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b QLITE_UTF16LE ){
b1dc0 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 . /* UTF-8
b1dd0 2d 3e 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 -> UTF-16 Little
b1de0 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 -endian */.
b1df0 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 while( zIn<zTer
b1e00 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 m ){. /*
b1e10 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 c = sqlite3Utf8R
b1e20 65 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 ead(zIn, zTerm,
b1e30 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e (const u8**)&zIn
b1e40 29 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 ); */. RE
b1e50 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 AD_UTF8(zIn, zTe
b1e60 72 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 rm, c);.
b1e70 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c WRITE_UTF16LE(z,
b1e80 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 c);. }.
b1e90 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
b1ea0 73 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 sert( desiredEnc
b1eb0 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 ==SQLITE_UTF16BE
b1ec0 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 );. /* UTF
b1ed0 2d 38 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67 -8 -> UTF-16 Big
b1ee0 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 -endian */.
b1ef0 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 while( zIn<zTer
b1f00 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 m ){. /*
b1f10 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 c = sqlite3Utf8R
b1f20 65 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 ead(zIn, zTerm,
b1f30 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e (const u8**)&zIn
b1f40 29 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 ); */. RE
b1f50 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 AD_UTF8(zIn, zTe
b1f60 72 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 rm, c);.
b1f70 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c WRITE_UTF16BE(z,
b1f80 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 c);. }.
b1f90 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d }. pMem->n =
b1fa0 20 7a 20 2d 20 7a 4f 75 74 3b 0a 20 20 20 20 2a z - zOut;. *
b1fb0 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 z++ = 0;. }else
b1fc0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65 {. assert( de
b1fd0 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
b1fe0 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28 _UTF8 );. if(
b1ff0 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 pMem->enc==SQLI
b2000 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 TE_UTF16LE ){.
b2010 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 /* UTF-16 Li
b2020 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 ttle-endian -> U
b2030 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 TF-8 */. wh
b2040 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 ile( zIn<zTerm )
b2050 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 {. READ_U
b2060 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b 20 TF16LE(zIn, c);
b2070 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 . WRITE_U
b2080 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 TF8(z, c);.
b2090 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
b20a0 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69 /* UTF-16 Bi
b20b0 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d g-endian -> UTF-
b20c0 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 8 */. while
b20d0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 ( zIn<zTerm ){.
b20e0 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 READ_UTF1
b20f0 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20 6BE(zIn, c); .
b2100 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 WRITE_UTF8
b2110 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a (z, c);. }.
b2120 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e }. pMem->
b2130 6e 20 3d 20 7a 20 2d 20 7a 4f 75 74 3b 0a 20 20 n = z - zOut;.
b2140 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 61 73 }. *z = 0;. as
b2150 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 6e 2b 28 sert( (pMem->n+(
b2160 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 desiredEnc==SQLI
b2170 54 45 5f 55 54 46 38 3f 31 3a 32 29 29 3c 3d 6c TE_UTF8?1:2))<=l
b2180 65 6e 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 en );.. sqlite3
b2190 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
b21a0 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c Mem);. pMem->fl
b21b0 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 53 74 61 ags &= ~(MEM_Sta
b21c0 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f tic|MEM_Dyn|MEM_
b21d0 45 70 68 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e Ephem);. pMem->
b21e0 65 6e 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63 enc = desiredEnc
b21f0 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 ;. pMem->flags
b2200 7c 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c 4d 45 4d |= (MEM_Term|MEM
b2210 5f 44 79 6e 29 3b 0a 20 20 70 4d 65 6d 2d 3e 7a _Dyn);. pMem->z
b2220 20 3d 20 28 63 68 61 72 2a 29 7a 4f 75 74 3b 0a = (char*)zOut;.
b2230 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 pMem->zMalloc
b2240 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 0a 74 72 61 6e = pMem->z;..tran
b2250 73 6c 61 74 65 5f 6f 75 74 3a 0a 23 69 66 20 64 slate_out:.#if d
b2260 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 efined(TRANSLATE
b2270 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e _TRACE) && defin
b2280 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
b2290 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 . {. char zB
b22a0 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c uf[100];. sql
b22b0 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 ite3VdbeMemPrett
b22c0 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 yPrint(pMem, zBu
b22d0 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 f);. fprintf(
b22e0 73 74 64 65 72 72 2c 20 22 4f 55 54 50 55 54 3a stderr, "OUTPUT:
b22f0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 %s\n", zBuf);.
b2300 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
b2310 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
b2320 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
b2330 69 6e 65 20 63 68 65 63 6b 73 20 66 6f 72 20 61 ine checks for a
b2340 20 62 79 74 65 2d 6f 72 64 65 72 20 6d 61 72 6b byte-order mark
b2350 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
b2360 67 20 6f 66 20 74 68 65 20 0a 2a 2a 20 55 54 46 g of the .** UTF
b2370 2d 31 36 20 73 74 72 69 6e 67 20 73 74 6f 72 65 -16 string store
b2380 64 20 69 6e 20 2a 70 4d 65 6d 2e 20 49 66 20 6f d in *pMem. If o
b2390 6e 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20 69 ne is present, i
b23a0 74 20 69 73 20 72 65 6d 6f 76 65 64 20 61 6e 64 t is removed and
b23b0 0a 2a 2a 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 .** the encoding
b23c0 20 6f 66 20 74 68 65 20 4d 65 6d 20 61 64 6a 75 of the Mem adju
b23d0 73 74 65 64 2e 20 54 68 69 73 20 72 6f 75 74 69 sted. This routi
b23e0 6e 65 20 64 6f 65 73 20 6e 6f 74 20 64 6f 20 61 ne does not do a
b23f0 6e 79 0a 2a 2a 20 62 79 74 65 2d 73 77 61 70 70 ny.** byte-swapp
b2400 69 6e 67 2c 20 69 74 20 6a 75 73 74 20 73 65 74 ing, it just set
b2410 73 20 4d 65 6d 2e 65 6e 63 20 61 70 70 72 6f 70 s Mem.enc approp
b2420 72 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 riately..**.** T
b2430 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 73 he allocation (s
b2440 74 61 74 69 63 2c 20 64 79 6e 61 6d 69 63 20 65 tatic, dynamic e
b2450 74 63 2e 29 20 61 6e 64 20 65 6e 63 6f 64 69 6e tc.) and encodin
b2460 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 6d 61 79 g of the Mem may
b2470 20 62 65 0a 2a 2a 20 63 68 61 6e 67 65 64 20 62 be.** changed b
b2480 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e y this function.
b2490 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b24a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
b24b0 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d beMemHandleBom(M
b24c0 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 em *pMem){. int
b24d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
b24e0 0a 20 20 75 38 20 62 6f 6d 20 3d 20 30 3b 0a 0a . u8 bom = 0;..
b24f0 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3c 30 20 if( pMem->n<0
b2500 7c 7c 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b 0a || pMem->n>1 ){.
b2510 20 20 20 20 75 38 20 62 31 20 3d 20 2a 28 75 38 u8 b1 = *(u8
b2520 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 *)pMem->z;.
b2530 75 38 20 62 32 20 3d 20 2a 28 28 28 75 38 20 2a u8 b2 = *(((u8 *
b2540 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b 0a )pMem->z) + 1);.
b2550 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46 45 if( b1==0xFE
b2560 20 26 26 20 62 32 3d 3d 30 78 46 46 20 29 7b 0a && b2==0xFF ){.
b2570 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 bom = SQLI
b2580 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 20 20 TE_UTF16BE;.
b2590 7d 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 }. if( b1==0x
b25a0 46 46 20 26 26 20 62 32 3d 3d 30 78 46 45 20 29 FF && b2==0xFE )
b25b0 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 {. bom = SQ
b25c0 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 LITE_UTF16LE;.
b25d0 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 }. }. . if(
b25e0 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d bom ){. rc =
b25f0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
b2600 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 akeWriteable(pMe
b2610 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d m);. if( rc==
b2620 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
b2630 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32 3b pMem->n -= 2;
b2640 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70 . memmove(p
b2650 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e 7a Mem->z, &pMem->z
b2660 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 [2], pMem->n);.
b2670 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 pMem->z[pMe
b2680 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 m->n] = '\0';.
b2690 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d pMem->z[pMem
b26a0 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 ->n+1] = '\0';.
b26b0 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
b26c0 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 |= MEM_Term;.
b26d0 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 pMem->enc =
b26e0 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 bom;. }. }.
b26f0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 return rc;.}.#e
b2700 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
b2710 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a MIT_UTF16 */../*
b2720 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d .** pZ is a UTF-
b2730 38 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 8 encoded unicod
b2740 65 20 73 74 72 69 6e 67 2e 20 49 66 20 6e 42 79 e string. If nBy
b2750 74 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 te is less than
b2760 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 zero,.** return
b2770 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e the number of un
b2780 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 icode characters
b2790 20 69 6e 20 70 5a 20 75 70 20 74 6f 20 28 62 75 in pZ up to (bu
b27a0 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 t not including)
b27b0 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 30 78 .** the first 0x
b27c0 30 30 20 62 79 74 65 2e 20 49 66 20 6e 42 79 74 00 byte. If nByt
b27d0 65 20 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 e is not less th
b27e0 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75 72 6e 20 an zero, return
b27f0 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 the.** number of
b2800 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 unicode charact
b2810 65 72 73 20 69 6e 20 74 68 65 20 66 69 72 73 74 ers in the first
b2820 20 6e 42 79 74 65 20 6f 66 20 70 5a 20 28 6f 72 nByte of pZ (or
b2830 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20 66 up to .** the f
b2840 69 72 73 74 20 30 78 30 30 2c 20 77 68 69 63 68 irst 0x00, which
b2850 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 ever comes first
b2860 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
b2870 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b2880 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 Utf8CharLen(cons
b2890 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 t char *zIn, int
b28a0 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 72 nByte){. int r
b28b0 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 38 = 0;. const u8
b28c0 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a *z = (const u8*
b28d0 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 38 )zIn;. const u8
b28e0 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e *zTerm;. if( n
b28f0 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20 20 20 7a Byte>=0 ){. z
b2900 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 5d Term = &z[nByte]
b2910 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a ;. }else{. z
b2920 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20 75 38 Term = (const u8
b2930 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 73 *)(-1);. }. as
b2940 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 29 sert( z<=zTerm )
b2950 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 21 3d 30 ;. while( *z!=0
b2960 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 && z<zTerm ){.
b2970 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 SQLITE_SKIP_U
b2980 54 46 38 28 7a 29 3b 0a 20 20 20 20 72 2b 2b 3b TF8(z);. r++;
b2990 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b . }. return r;
b29a0 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74 65 73 74 .}../* This test
b29b0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 function is not
b29c0 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 currently used
b29d0 62 79 20 74 68 65 20 61 75 74 6f 6d 61 74 65 64 by the automated
b29e0 20 74 65 73 74 2d 73 75 69 74 65 2e 20 0a 2a 2a test-suite. .**
b29f0 20 48 65 6e 63 65 20 69 74 20 69 73 20 6f 6e 6c Hence it is onl
b2a00 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 64 y available in d
b2a10 65 62 75 67 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a ebug builds..*/.
b2a20 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
b2a30 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 TE_TEST) && defi
b2a40 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
b2a50 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 )./*.** Translat
b2a60 65 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 38 e UTF-8 to UTF-8
b2a70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 61 73 ..**.** This has
b2a80 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 6d the effect of m
b2a90 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 20 aking sure that
b2aa0 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 77 65 the string is we
b2ab0 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55 54 46 ll-formed.** UTF
b2ac0 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64 20 63 68 -8. Miscoded ch
b2ad0 61 72 61 63 74 65 72 73 20 61 72 65 20 72 65 6d aracters are rem
b2ae0 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 oved..**.** The
b2af0 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 64 translation is d
b2b00 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20 28 73 69 one in-place (si
b2b10 6e 63 65 20 69 74 20 69 73 20 69 6d 70 6f 73 73 nce it is imposs
b2b20 69 62 6c 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 ible for the.**
b2b30 63 6f 72 72 65 63 74 20 55 54 46 2d 38 20 65 6e correct UTF-8 en
b2b40 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 6c 6f 6e coding to be lon
b2b50 67 65 72 20 74 68 61 6e 20 61 20 6d 61 6c 66 6f ger than a malfo
b2b60 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67 29 2e 0a rmed encoding)..
b2b70 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b2b80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 E int sqlite3Utf
b2b90 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 8To8(unsigned ch
b2ba0 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73 69 ar *zIn){. unsi
b2bb0 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 20 gned char *zOut
b2bc0 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 = zIn;. unsigne
b2bd0 64 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d d char *zStart =
b2be0 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 zIn;. unsigned
b2bf0 20 63 68 61 72 20 2a 7a 54 65 72 6d 20 3d 20 26 char *zTerm = &
b2c00 7a 49 6e 5b 73 74 72 6c 65 6e 28 28 63 68 61 72 zIn[strlen((char
b2c10 20 2a 29 7a 49 6e 29 5d 3b 0a 20 20 75 33 32 20 *)zIn)];. u32
b2c20 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e c;.. while( zIn
b2c30 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 [0] ){. c = s
b2c40 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
b2c50 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 In, zTerm, (cons
b2c60 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a 20 20 t u8**)&zIn);.
b2c70 20 20 69 66 28 20 63 21 3d 30 78 66 66 66 64 20 if( c!=0xfffd
b2c80 29 7b 0a 20 20 20 20 20 20 57 52 49 54 45 5f 55 ){. WRITE_U
b2c90 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0a 20 20 TF8(zOut, c);.
b2ca0 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75 74 20 }. }. *zOut
b2cb0 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 7a 4f = 0;. return zO
b2cc0 75 74 20 2d 20 7a 53 74 61 72 74 3b 0a 7d 0a 23 ut - zStart;.}.#
b2cd0 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
b2ce0 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
b2cf0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
b2d00 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 UTF-16 string i
b2d10 6e 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 n the native enc
b2d20 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 oding into a UTF
b2d30 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 -8 string..** Me
b2d40 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 mory to hold the
b2d50 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 UTF-8 string is
b2d60 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
b2d70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e qlite3_malloc an
b2d80 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 d must.** be fre
b2d90 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ed by the callin
b2da0 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a g function..**.*
b2db0 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e * NULL is return
b2dc0 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ed if there is a
b2dd0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 n allocation err
b2de0 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 or..*/.SQLITE_PR
b2df0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
b2e00 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 te3Utf16to8(sqli
b2e10 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 te3 *db, const v
b2e20 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 oid *z, int nByt
b2e30 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d e){. Mem m;. m
b2e40 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a emset(&m, 0, siz
b2e50 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 eof(m));. m.db
b2e60 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 = db;. sqlite3V
b2e70 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c dbeMemSetStr(&m,
b2e80 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 z, nByte, SQLIT
b2e90 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 E_UTF16NATIVE, S
b2ea0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
b2eb0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
b2ec0 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 geEncoding(&m, S
b2ed0 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 QLITE_UTF8);. i
b2ee0 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
b2ef0 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 led ){. sqlit
b2f00 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
b2f10 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 (&m);. m.z =
b2f20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 0;. }. assert(
b2f30 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f (m.flags & MEM_
b2f40 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e Term)!=0 || db->
b2f50 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
b2f60 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 assert( (m.fla
b2f70 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 gs & MEM_Str)!=0
b2f80 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
b2f90 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e iled );. return
b2fa0 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f (m.flags & MEM_
b2fb0 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 Dyn)!=0 ? m.z :
b2fc0 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
b2fd0 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a db, m.z);.}../*.
b2fe0 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d 31 ** pZ is a UTF-1
b2ff0 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 6 encoded unicod
b3000 65 20 73 74 72 69 6e 67 2e 20 49 66 20 6e 43 68 e string. If nCh
b3010 61 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 ar is less than
b3020 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 zero,.** return
b3030 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
b3040 74 65 73 20 75 70 20 74 6f 20 28 62 75 74 20 6e tes up to (but n
b3050 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 2c 20 74 ot including), t
b3060 68 65 20 66 69 72 73 74 20 70 61 69 72 0a 2a 2a he first pair.**
b3070 20 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20 of consecutive
b3080 30 78 30 30 20 62 79 74 65 73 20 69 6e 20 70 5a 0x00 bytes in pZ
b3090 2e 20 49 66 20 6e 43 68 61 72 20 69 73 20 6e 6f . If nChar is no
b30a0 74 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f t less than zero
b30b0 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e ,.** then return
b30c0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
b30d0 79 74 65 73 20 69 6e 20 74 68 65 20 66 69 72 73 ytes in the firs
b30e0 74 20 6e 43 68 61 72 20 75 6e 69 63 6f 64 65 20 t nChar unicode
b30f0 63 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 69 6e characters.** in
b3100 20 70 5a 20 28 6f 72 20 75 70 20 75 6e 74 69 6c pZ (or up until
b3110 20 74 68 65 20 66 69 72 73 74 20 70 61 69 72 20 the first pair
b3120 6f 66 20 30 78 30 30 20 62 79 74 65 73 2c 20 77 of 0x00 bytes, w
b3130 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 hichever comes f
b3140 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 irst)..*/.SQLITE
b3150 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b3160 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e ite3Utf16ByteLen
b3170 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e (const void *zIn
b3180 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 , int nChar){.
b3190 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 20 3d unsigned int c =
b31a0 20 31 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 1;. char const
b31b0 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 *z = zIn;. int
b31c0 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51 n = 0;. if( SQ
b31d0 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 LITE_UTF16NATIVE
b31e0 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 ==SQLITE_UTF16BE
b31f0 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e 67 ){. /* Using
b3200 20 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45 5f an "if (SQLITE_
b3210 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c UTF16NATIVE==SQL
b3220 49 54 45 5f 55 54 46 31 36 42 45 29 22 20 63 6f ITE_UTF16BE)" co
b3230 6e 73 74 72 75 63 74 20 68 65 72 65 0a 20 20 20 nstruct here.
b3240 20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65 72 ** and in other
b3250 20 70 61 72 74 73 20 6f 66 20 74 68 69 73 20 66 parts of this f
b3260 69 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20 61 ile means that a
b3270 74 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69 6c t one branch wil
b3280 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 20 l. ** not be
b3290 63 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65 72 covered by cover
b32a0 61 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20 61 age testing on a
b32b0 6e 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e 20 ny single host.
b32c0 42 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20 20 But coverage.
b32d0 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 ** will be comp
b32e0 6c 65 74 65 20 69 66 20 74 68 65 20 74 65 73 74 lete if the test
b32f0 73 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f 74 s are run on bot
b3300 68 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 h a little-endia
b3310 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62 69 n and . ** bi
b3320 67 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20 42 g-endian host. B
b3330 65 63 61 75 73 65 20 62 6f 74 68 20 74 68 65 20 ecause both the
b3340 55 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64 20 UTF16NATIVE and
b3350 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a 20 SQLITE_UTF16BE.
b3360 20 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72 65 ** macros are
b3370 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d constant at com
b3380 70 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63 6f pile time the co
b3390 6d 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65 72 mpiler can deter
b33a0 6d 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69 63 mine. ** whic
b33b0 68 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62 65 h branch will be
b33c0 20 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69 73 followed. It is
b33d0 20 74 68 65 72 65 66 6f 72 65 20 61 73 73 75 6d therefore assum
b33e0 65 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74 69 ed that no runti
b33f0 6d 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c 74 me. ** penalt
b3400 79 20 69 73 20 70 61 69 64 20 66 6f 72 20 74 68 y is paid for th
b3410 69 73 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e is "if" statemen
b3420 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 t.. */. wh
b3430 69 6c 65 28 20 63 20 26 26 20 28 28 6e 43 68 61 ile( c && ((nCha
b3440 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 29 r<0) || n<nChar)
b3450 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f 55 ){. READ_U
b3460 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 TF16BE(z, c);.
b3470 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 n++;. }.
b3480 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c }else{. whil
b3490 65 28 20 63 20 26 26 20 28 28 6e 43 68 61 72 3c e( c && ((nChar<
b34a0 30 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 29 20 29 0) || n<nChar) )
b34b0 7b 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 {. READ_UTF
b34c0 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 16LE(z, c);.
b34d0 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d n++;. }. }
b34e0 0a 20 20 72 65 74 75 72 6e 20 28 7a 2d 28 63 68 . return (z-(ch
b34f0 61 72 20 63 6f 6e 73 74 20 2a 29 7a 49 6e 29 2d ar const *)zIn)-
b3500 28 28 63 3d 3d 30 29 3f 32 3a 30 29 3b 0a 7d 0a ((c==0)?2:0);.}.
b3510 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
b3520 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 ITE_TEST)./*.**
b3530 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
b3540 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 called from the
b3550 54 43 4c 20 74 65 73 74 20 66 75 6e 63 74 69 6f TCL test functio
b3560 6e 20 22 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c n "translate_sel
b3570 66 74 65 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68 ftest"..** It ch
b3580 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 70 72 ecks that the pr
b3590 69 6d 69 74 69 76 65 73 20 66 6f 72 20 73 65 72 imitives for ser
b35a0 69 61 6c 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 ializing and des
b35b0 65 72 69 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68 erializing.** ch
b35c0 61 72 61 63 74 65 72 73 20 69 6e 20 65 61 63 68 aracters in each
b35d0 20 65 6e 63 6f 64 69 6e 67 20 61 72 65 20 69 6e encoding are in
b35e0 76 65 72 73 65 73 20 6f 66 20 65 61 63 68 20 6f verses of each o
b35f0 74 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ther..*/.SQLITE_
b3600 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
b3610 69 74 65 33 55 74 66 53 65 6c 66 54 65 73 74 28 ite3UtfSelfTest(
b3620 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 void){. unsigne
b3630 64 20 69 6e 74 20 69 2c 20 74 3b 0a 20 20 75 6e d int i, t;. un
b3640 73 69 67 6e 65 64 20 63 68 61 72 20 7a 42 75 66 signed char zBuf
b3650 5b 32 30 5d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 [20];. unsigned
b3660 20 63 68 61 72 20 2a 7a 3b 0a 20 20 75 6e 73 69 char *z;. unsi
b3670 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d gned char *zTerm
b3680 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 ;. int n;. uns
b3690 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20 igned int c;..
b36a0 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 for(i=0; i<0x001
b36b0 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 10000; i++){.
b36c0 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 z = zBuf;. W
b36d0 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b RITE_UTF8(z, i);
b36e0 0a 20 20 20 20 6e 20 3d 20 7a 2d 7a 42 75 66 3b . n = z-zBuf;
b36f0 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 . z[0] = 0;.
b3700 20 20 20 7a 54 65 72 6d 20 3d 20 7a 3b 0a 20 20 zTerm = z;.
b3710 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 z = zBuf;.
b3720 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 c = sqlite3Utf8R
b3730 65 61 64 28 7a 2c 20 7a 54 65 72 6d 2c 20 28 63 ead(z, zTerm, (c
b3740 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 onst u8**)&z);.
b3750 20 20 20 74 20 3d 20 69 3b 0a 20 20 20 20 69 66 t = i;. if
b3760 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 ( i>=0xD800 && i
b3770 3c 3d 30 78 44 46 46 46 20 29 20 74 20 3d 20 30 <=0xDFFF ) t = 0
b3780 78 46 46 46 44 3b 0a 20 20 20 20 69 66 28 20 28 xFFFD;. if( (
b3790 69 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 i&0xFFFFFFFE)==0
b37a0 78 46 46 46 45 20 29 20 74 20 3d 20 30 78 46 46 xFFFE ) t = 0xFF
b37b0 46 44 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 FD;. assert(
b37c0 63 3d 3d 74 20 29 3b 0a 20 20 20 20 61 73 73 65 c==t );. asse
b37d0 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 rt( (z-zBuf)==n
b37e0 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 );. }. for(i=0
b37f0 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 ; i<0x00110000;
b3800 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3e i++){. if( i>
b3810 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 78 45 =0xD800 && i<0xE
b3820 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 000 ) continue;.
b3830 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 z = zBuf;.
b3840 20 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 WRITE_UTF16LE(
b3850 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 7a z, i);. n = z
b3860 2d 7a 42 75 66 3b 0a 20 20 20 20 7a 5b 30 5d 20 -zBuf;. z[0]
b3870 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 = 0;. z = zBu
b3880 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 f;. READ_UTF1
b3890 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 6LE(z, c);. a
b38a0 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 ssert( c==i );.
b38b0 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 assert( (z-zB
b38c0 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 uf)==n );. }.
b38d0 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 for(i=0; i<0x001
b38e0 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 10000; i++){.
b38f0 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 if( i>=0xD800 &
b3900 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e & i<0xE000 ) con
b3910 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a tinue;. z = z
b3920 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 Buf;. WRITE_U
b3930 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 TF16BE(z, i);.
b3940 20 20 6e 20 3d 20 7a 2d 7a 42 75 66 3b 0a 20 20 n = z-zBuf;.
b3950 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 z[0] = 0;.
b3960 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45 z = zBuf;. RE
b3970 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 AD_UTF16BE(z, c)
b3980 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d ;. assert( c=
b3990 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =i );. assert
b39a0 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b ( (z-zBuf)==n );
b39b0 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.}.#endif /*
b39c0 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a SQLITE_TEST */.
b39d0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
b39e0 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a _OMIT_UTF16 */..
b39f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
b3a00 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a End of utf.c ***
b3a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
b3a40 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
b3a50 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 6c 2e Begin file util.
b3a60 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
b3a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
b3a90 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
b3aa0 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
b3ab0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
b3ac0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
b3ad0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
b3ae0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
b3af0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
b3b00 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
b3b10 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
b3b20 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
b3b30 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
b3b40 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
b3b50 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
b3b60 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
b3b70 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
b3b80 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
b3b90 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
b3ba0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
b3bb0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
b3bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3c00 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69 74 ******.** Utilit
b3c10 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 y functions used
b3c20 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69 throughout sqli
b3c30 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 te..**.** This f
b3c40 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e ile contains fun
b3c50 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f 63 ctions for alloc
b3c60 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 6f ating memory, co
b3c70 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69 6e mparing.** strin
b3c80 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c 69 gs, and stuff li
b3c90 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 24 ke that..**.** $
b3ca0 49 64 3a 20 75 74 69 6c 2e 63 2c 76 20 31 2e 32 Id: util.c,v 1.2
b3cb0 34 32 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39 42 2008/11/17 19
b3cc0 3a 31 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39 :18:55 danielk19
b3cd0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 77 Exp $.*/.../*
b3ce0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
b3cf0 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 if the floating
b3d00 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 4e point value is N
b3d10 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e 61 4e ot a Number (NaN
b3d20 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
b3d30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b3d40 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 7b IsNaN(double x){
b3d50 0a 20 20 2f 2a 20 54 68 69 73 20 4e 61 4e 20 74 . /* This NaN t
b3d60 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61 est sometimes fa
b3d70 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 ils if compiled
b3d80 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61 on GCC with -ffa
b3d90 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e st-math.. ** On
b3da0 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
b3db0 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61 the use of -ffa
b3dc0 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69 st-math comes wi
b3dd0 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 th the following
b3de0 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20 . ** warning:.
b3df0 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 **. ** Th
b3e00 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73 is option [-ffas
b3e10 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e t-math] should n
b3e20 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f ever be turned o
b3e30 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20 n by any. **
b3e40 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e -O option sin
b3e50 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74 ce it can result
b3e60 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75 in incorrect ou
b3e70 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d tput for program
b3e80 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63 s. ** whic
b3e90 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65 h depend on an e
b3ea0 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 xact implementat
b3eb0 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49 ion of IEEE or I
b3ec0 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75 SO . ** ru
b3ed0 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f les/specificatio
b3ee0 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63 ns for math func
b3ef0 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
b3f00 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69 Under MSVC, thi
b3f10 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66 s NaN test may f
b3f20 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 ail if compiled
b3f30 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d with a floating-
b3f40 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63 . ** point prec
b3f50 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72 ision mode other
b3f60 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73 than /fp:precis
b3f70 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44 e. From the MSD
b3f80 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 N . ** document
b3f90 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a ation:. **. **
b3fa0 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c The compil
b3fb0 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65 er [with /fp:pre
b3fc0 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65 cise] will prope
b3fd0 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61 rly handle compa
b3fe0 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20 risons . **
b3ff0 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e involving NaN.
b4000 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20 For example, x
b4010 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74 != x evaluates t
b4020 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e o true if x is N
b4030 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e aN . ** ..
b4040 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f .. */.#ifdef __
b4050 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72 FAST_MATH__.# er
b4060 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 ror SQLite will
b4070 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 not work correct
b4080 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61 ly with the -ffa
b4090 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f st-math option o
b40a0 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20 f GCC..#endif.
b40b0 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 volatile double
b40c0 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c y = x;. volatil
b40d0 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a e double z = y;.
b40e0 20 20 72 65 74 75 72 6e 20 79 21 3d 7a 3b 0a 7d return y!=z;.}
b40f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
b4100 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73 he length of a s
b4110 74 72 69 6e 67 2c 20 65 78 63 65 70 74 20 64 6f tring, except do
b4120 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 65 20 73 not allow the s
b4130 74 72 69 6e 67 20 6c 65 6e 67 74 68 0a 2a 2a 20 tring length.**
b4140 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 53 51 to exceed the SQ
b4150 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
b4160 48 20 73 65 74 74 69 6e 67 2e 0a 2a 2f 0a 53 51 H setting..*/.SQ
b4170 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b4180 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 73 sqlite3Strlen(s
b4190 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
b41a0 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f t char *z){. co
b41b0 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a nst char *z2 = z
b41c0 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 ;. int len;. i
b41d0 6e 74 20 78 3b 0a 20 20 77 68 69 6c 65 28 20 2a nt x;. while( *
b41e0 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 z2 ){ z2++; }.
b41f0 78 20 3d 20 7a 32 20 2d 20 7a 3b 0a 20 20 6c 65 x = z2 - z;. le
b4200 6e 20 3d 20 30 78 37 66 66 66 66 66 66 66 20 26 n = 0x7fffffff &
b4210 20 78 3b 0a 20 20 69 66 28 20 6c 65 6e 21 3d 78 x;. if( len!=x
b4220 20 7c 7c 20 6c 65 6e 20 3e 20 64 62 2d 3e 61 4c || len > db->aL
b4230 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
b4240 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
b4250 20 72 65 74 75 72 6e 20 64 62 2d 3e 61 4c 69 6d return db->aLim
b4260 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
b4270 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 65 6c 73 65 LENGTH];. }else
b4280 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 65 6e {. return len
b4290 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ;. }.}../*.** S
b42a0 65 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 et the most rece
b42b0 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e nt error code an
b42c0 64 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 66 d error string f
b42d0 6f 72 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a or the sqlite.**
b42e0 20 68 61 6e 64 6c 65 20 22 64 62 22 2e 20 54 68 handle "db". Th
b42f0 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
b4300 73 65 74 20 74 6f 20 22 65 72 72 5f 63 6f 64 65 set to "err_code
b4310 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 "..**.** If it i
b4320 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69 s not NULL, stri
b4330 6e 67 20 7a 46 6f 72 6d 61 74 20 73 70 65 63 69 ng zFormat speci
b4340 66 69 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 fies the format
b4350 6f 66 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 of the.** error
b4360 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 73 74 string in the st
b4370 79 6c 65 20 6f 66 20 74 68 65 20 70 72 69 6e 74 yle of the print
b4380 66 20 66 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65 f functions: The
b4390 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f following.** fo
b43a0 72 6d 61 74 20 63 68 61 72 61 63 74 65 72 73 20 rmat characters
b43b0 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a are allowed:.**.
b43c0 2a 2a 20 20 20 20 20 20 25 73 20 20 20 20 20 20 ** %s
b43d0 49 6e 73 65 72 74 20 61 20 73 74 72 69 6e 67 0a Insert a string.
b43e0 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 20 20 20 ** %z
b43f0 41 20 73 74 72 69 6e 67 20 74 68 61 74 20 73 68 A string that sh
b4400 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20 61 66 ould be freed af
b4410 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 ter use.**
b4420 25 64 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 %d Insert a
b4430 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 n integer.**
b4440 20 20 25 54 20 20 20 20 20 20 49 6e 73 65 72 74 %T Insert
b4450 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 a token.**
b4460 20 25 53 20 20 20 20 20 20 49 6e 73 65 72 74 20 %S Insert
b4470 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e the first elemen
b4480 74 20 6f 66 20 61 20 53 72 63 4c 69 73 74 0a 2a t of a SrcList.*
b4490 2a 0a 2a 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64 *.** zFormat and
b44a0 20 61 6e 79 20 73 74 72 69 6e 67 20 74 6f 6b 65 any string toke
b44b0 6e 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69 ns that follow i
b44c0 74 20 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f t are assumed to
b44d0 20 62 65 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 be.** encoded i
b44e0 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 n UTF-8..**.** T
b44f0 6f 20 63 6c 65 61 72 20 74 68 65 20 6d 6f 73 74 o clear the most
b4500 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 66 6f recent error fo
b4510 72 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 20 r sqlite handle
b4520 22 64 62 22 2c 20 73 71 6c 69 74 65 33 45 72 72 "db", sqlite3Err
b4530 6f 72 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 or.** should be
b4540 63 61 6c 6c 65 64 20 77 69 74 68 20 65 72 72 5f called with err_
b4550 63 6f 64 65 20 73 65 74 20 74 6f 20 53 51 4c 49 code set to SQLI
b4560 54 45 5f 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 TE_OK and zForma
b4570 74 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c t set.** to NULL
b4580 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b4590 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
b45a0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 20 2a 64 Error(sqlite3 *d
b45b0 62 2c 20 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c b, int err_code,
b45c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
b45d0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 rmat, ...){. if
b45e0 28 20 64 62 20 26 26 20 28 64 62 2d 3e 70 45 72 ( db && (db->pEr
b45f0 72 20 7c 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d r || (db->pErr =
b4600 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 sqlite3ValueNew
b4610 28 64 62 29 29 21 3d 30 29 20 29 7b 0a 20 20 20 (db))!=0) ){.
b4620 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 65 db->errCode = e
b4630 72 72 5f 63 6f 64 65 3b 0a 20 20 20 20 69 66 28 rr_code;. if(
b4640 20 7a 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 zFormat ){.
b4650 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 char *z;.
b4660 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20 va_list ap;.
b4670 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 va_start(ap,
b4680 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 20 20 zFormat);.
b4690 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 z = sqlite3VMPri
b46a0 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c ntf(db, zFormat,
b46b0 20 61 70 29 3b 0a 20 20 20 20 20 20 76 61 5f 65 ap);. va_e
b46c0 6e 64 28 61 70 29 3b 0a 20 20 20 20 20 20 73 71 nd(ap);. sq
b46d0 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 lite3ValueSetStr
b46e0 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 7a (db->pErr, -1, z
b46f0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
b4700 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a QLITE_DYNAMIC);.
b4710 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
b4720 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 sqlite3ValueSet
b4730 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 30 2c Str(db->pErr, 0,
b4740 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 0, SQLITE_UTF8,
b4750 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b SQLITE_STATIC);
b4760 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
b4770 0a 2a 2a 20 41 64 64 20 61 6e 20 65 72 72 6f 72 .** Add an error
b4780 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 message to pPar
b4790 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 se->zErrMsg and
b47a0 69 6e 63 72 65 6d 65 6e 74 20 70 50 61 72 73 65 increment pParse
b47b0 2d 3e 6e 45 72 72 2e 0a 2a 2a 20 54 68 65 20 66 ->nErr..** The f
b47c0 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 61 74 74 ollowing formatt
b47d0 69 6e 67 20 63 68 61 72 61 63 74 65 72 73 20 61 ing characters a
b47e0 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a re allowed:.**.*
b47f0 2a 20 20 20 20 20 20 25 73 20 20 20 20 20 20 49 * %s I
b4800 6e 73 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a nsert a string.*
b4810 2a 20 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 * %z A
b4820 20 73 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f string that sho
b4830 75 6c 64 20 62 65 20 66 72 65 65 64 20 61 66 74 uld be freed aft
b4840 65 72 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 er use.** %
b4850 64 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e d Insert an
b4860 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 integer.**
b4870 20 25 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 %T Insert
b4880 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 a token.**
b4890 25 53 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 %S Insert t
b48a0 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 he first element
b48b0 20 6f 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a of a SrcList.**
b48c0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
b48d0 6e 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 n should be used
b48e0 20 74 6f 20 72 65 70 6f 72 74 20 61 6e 79 20 65 to report any e
b48f0 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 rror that occurs
b4900 20 77 68 69 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 whilst.** compi
b4910 6c 69 6e 67 20 61 6e 20 53 51 4c 20 73 74 61 74 ling an SQL stat
b4920 65 6d 65 6e 74 20 28 69 2e 65 2e 20 77 69 74 68 ement (i.e. with
b4930 69 6e 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 in sqlite3_prepa
b4940 72 65 28 29 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 re()). The.** la
b4950 73 74 20 74 68 69 6e 67 20 74 68 65 20 73 71 6c st thing the sql
b4960 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 66 ite3_prepare() f
b4970 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 69 73 20 unction does is
b4980 63 6f 70 79 20 74 68 65 20 65 72 72 6f 72 0a 2a copy the error.*
b4990 2a 20 73 74 6f 72 65 64 20 62 79 20 74 68 69 73 * stored by this
b49a0 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74 function into t
b49b0 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 he database hand
b49c0 6c 65 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 le using sqlite3
b49d0 45 72 72 6f 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 Error()..** Func
b49e0 74 69 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f tion sqlite3Erro
b49f0 72 28 29 20 73 68 6f 75 6c 64 20 62 65 20 75 73 r() should be us
b4a00 65 64 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d ed during statem
b4a10 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a ent execution.**
b4a20 20 28 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 (sqlite3_step()
b4a30 20 65 74 63 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 etc.)..*/.SQLIT
b4a40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
b4a50 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50 qlite3ErrorMsg(P
b4a60 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f arse *pParse, co
b4a70 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
b4a80 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 t, ...){. va_li
b4a90 73 74 20 61 70 3b 0a 20 20 73 71 6c 69 74 65 33 st ap;. sqlite3
b4aa0 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
b4ab0 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 b;. pParse->nEr
b4ac0 72 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 r++;. sqlite3Db
b4ad0 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d Free(db, pParse-
b4ae0 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 61 5f >zErrMsg);. va_
b4af0 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 start(ap, zForma
b4b00 74 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 t);. pParse->zE
b4b10 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 56 rrMsg = sqlite3V
b4b20 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 MPrintf(db, zFor
b4b30 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
b4b40 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 50 nd(ap);. if( pP
b4b50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 arse->rc==SQLITE
b4b60 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 _OK ){. pPars
b4b70 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 e->rc = SQLITE_E
b4b80 52 52 4f 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a RROR;. }.}../*.
b4b90 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 65 72 72 ** Clear the err
b4ba0 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 or message in pP
b4bb0 61 72 73 65 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a arse, if any.*/.
b4bc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
b4bd0 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 oid sqlite3Error
b4be0 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 Clear(Parse *pPa
b4bf0 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 44 rse){. sqlite3D
b4c00 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 bFree(pParse->db
b4c10 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 , pParse->zErrMs
b4c20 67 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 g);. pParse->zE
b4c30 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 50 61 rrMsg = 0;. pPa
b4c40 72 73 65 2d 3e 6e 45 72 72 20 3d 20 30 3b 0a 7d rse->nErr = 0;.}
b4c50 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
b4c60 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f an SQL-style quo
b4c70 74 65 64 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 ted string into
b4c80 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20 a normal string
b4c90 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 by removing.** t
b4ca0 68 65 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 he quote charact
b4cb0 65 72 73 2e 20 20 54 68 65 20 63 6f 6e 76 65 72 ers. The conver
b4cc0 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d sion is done in-
b4cd0 70 6c 61 63 65 2e 20 20 49 66 20 74 68 65 0a 2a place. If the.*
b4ce0 2a 20 69 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74 * input does not
b4cf0 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 71 75 begin with a qu
b4d00 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c 20 74 ote character, t
b4d10 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
b4d20 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a .** is a no-op..
b4d30 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 **.** 2002-Feb-1
b4d40 34 3a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 4: This routine
b4d50 69 73 20 65 78 74 65 6e 64 65 64 20 74 6f 20 72 is extended to r
b4d60 65 6d 6f 76 65 20 4d 53 2d 41 63 63 65 73 73 20 emove MS-Access
b4d70 73 74 79 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 style.** bracket
b4d80 73 20 66 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 s from around id
b4d90 65 6e 74 69 66 65 72 73 2e 20 20 46 6f 72 20 65 entifers. For e
b4da0 78 61 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 xample: "[a-b-c
b4db0 5d 22 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 ]" becomes.** "a
b4dc0 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 -b-c"..*/.SQLITE
b4dd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
b4de0 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 61 lite3Dequote(cha
b4df0 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 71 75 6f r *z){. int quo
b4e00 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a te;. int i, j;.
b4e10 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 if( z==0 ) ret
b4e20 75 72 6e 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a urn;. quote = z
b4e30 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20 71 [0];. switch( q
b4e40 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73 65 uote ){. case
b4e50 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20 '\'': break;.
b4e60 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 62 case '"': b
b4e70 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 reak;. case '
b4e80 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20 20 `': break;
b4e90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
b4ea0 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69 or MySQL compati
b4eb0 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63 61 bility */. ca
b4ec0 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65 20 se '[': quote
b4ed0 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20 = ']'; break;
b4ee0 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65 72 /* For MS SqlSer
b4ef0 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 ver compatibilit
b4f00 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 y */. default
b4f10 3a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d : return;. }
b4f20 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b . for(i=1, j=0;
b4f30 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 z[i]; i++){.
b4f40 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 if( z[i]==quote
b4f50 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b ){. if( z[
b4f60 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 i+1]==quote ){.
b4f70 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 z[j++] =
b4f80 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 69 quote;. i
b4f90 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ++;. }else{
b4fa0 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 . z[j++]
b4fb0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 = 0;. bre
b4fc0 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
b4fd0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a }else{. z[j
b4fe0 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 ++] = z[i];.
b4ff0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 }. }.}../* Conv
b5000 65 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e enient short-han
b5010 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70 d */.#define Upp
b5020 65 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65 erToLower sqlite
b5030 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f 3UpperToLower../
b5040 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d *.** Some system
b5050 73 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29 s have stricmp()
b5060 2e 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73 . Others have s
b5070 74 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65 trcasecmp(). Be
b5080 63 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69 cause.** there i
b5090 73 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79 s no consistency
b50a0 2c 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65 , we will define
b50b0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c our own..*/.SQL
b50c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b50d0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63 sqlite3StrICmp(c
b50e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 onst char *zLeft
b50f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 , const char *zR
b5100 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65 ight){. registe
b5110 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 r unsigned char
b5120 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 *a, *b;. a = (u
b5130 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a nsigned char *)z
b5140 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 Left;. b = (uns
b5150 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 igned char *)zRi
b5160 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61 ght;. while( *a
b5170 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f !=0 && UpperToLo
b5180 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f wer[*a]==UpperTo
b5190 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b Lower[*b]){ a++;
b51a0 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e b++; }. return
b51b0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 UpperToLower[*a
b51c0 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 ] - UpperToLower
b51d0 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 [*b];.}.SQLITE_P
b51e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b51f0 65 33 53 74 72 4e 49 43 6d 70 28 63 6f 6e 73 74 e3StrNICmp(const
b5200 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f char *zLeft, co
b5210 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 nst char *zRight
b5220 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69 , int N){. regi
b5230 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 63 68 ster unsigned ch
b5240 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d ar *a, *b;. a =
b5250 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
b5260 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 *)zLeft;. b = (
b5270 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
b5280 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 zRight;. while(
b5290 20 4e 2d 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d N-- > 0 && *a!=
b52a0 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 0 && UpperToLowe
b52b0 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f r[*a]==UpperToLo
b52c0 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 wer[*b]){ a++; b
b52d0 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e ++; }. return N
b52e0 3c 30 20 3f 20 30 20 3a 20 55 70 70 65 72 54 6f <0 ? 0 : UpperTo
b52f0 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 Lower[*a] - Uppe
b5300 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a rToLower[*b];.}.
b5310 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
b5320 55 45 20 69 66 20 7a 20 69 73 20 61 20 70 75 72 UE if z is a pur
b5330 65 20 6e 75 6d 65 72 69 63 20 73 74 72 69 6e 67 e numeric string
b5340 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20 . Return FALSE
b5350 69 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 if the.** string
b5360 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 63 68 contains any ch
b5370 61 72 61 63 74 65 72 20 77 68 69 63 68 20 69 73 aracter which is
b5380 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6e not part of a n
b5390 75 6d 62 65 72 2e 20 49 66 0a 2a 2a 20 74 68 65 umber. If.** the
b53a0 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6d 65 72 string is numer
b53b0 69 63 20 61 6e 64 20 63 6f 6e 74 61 69 6e 73 20 ic and contains
b53c0 74 68 65 20 27 2e 27 20 63 68 61 72 61 63 74 65 the '.' characte
b53d0 72 2c 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 0a r, set *realnum.
b53e0 2a 2a 20 74 6f 20 54 52 55 45 20 28 6f 74 68 65 ** to TRUE (othe
b53f0 72 77 69 73 65 20 46 41 4c 53 45 29 2e 0a 2a 2a rwise FALSE)..**
b5400 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 20 73 74 72 .** An empty str
b5410 69 6e 67 20 69 73 20 63 6f 6e 73 69 64 65 72 65 ing is considere
b5420 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a d non-numeric..*
b5430 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b5440 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 int sqlite3IsNu
b5450 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 mber(const char
b5460 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 6c 6e 75 6d *z, int *realnum
b5470 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 , u8 enc){. int
b5480 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51 incr = (enc==SQ
b5490 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a LITE_UTF8?1:2);.
b54a0 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 if( enc==SQLIT
b54b0 45 5f 55 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b E_UTF16BE ) z++;
b54c0 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c . if( *z=='-' |
b54d0 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d | *z=='+' ) z +=
b54e0 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 21 69 73 incr;. if( !is
b54f0 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 digit(*(u8*)z) )
b5500 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
b5510 20 20 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b }. z += incr;
b5520 0a 20 20 69 66 28 20 72 65 61 6c 6e 75 6d 20 29 . if( realnum )
b5530 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20 *realnum = 0;.
b5540 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28 while( isdigit(
b5550 2a 28 75 38 2a 29 7a 29 20 29 7b 20 7a 20 2b 3d *(u8*)z) ){ z +=
b5560 20 69 6e 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a incr; }. if( *
b5570 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20 z=='.' ){. z
b5580 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 += incr;. if(
b5590 20 21 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 !isdigit(*(u8*)
b55a0 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 z) ) return 0;.
b55b0 20 20 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 while( isdigi
b55c0 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 20 7a 20 t(*(u8*)z) ){ z
b55d0 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 69 += incr; }. i
b55e0 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65 f( realnum ) *re
b55f0 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 alnum = 1;. }.
b5600 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 if( *z=='e' ||
b5610 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a *z=='E' ){. z
b5620 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 += incr;. if
b5630 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d ( *z=='+' || *z=
b5640 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 ='-' ) z += incr
b5650 3b 0a 20 20 20 20 69 66 28 20 21 69 73 64 69 67 ;. if( !isdig
b5660 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 20 72 65 it(*(u8*)z) ) re
b5670 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c turn 0;. whil
b5680 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a e( isdigit(*(u8*
b5690 29 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 )z) ){ z += incr
b56a0 3b 20 7d 0a 20 20 20 20 69 66 28 20 72 65 61 6c ; }. if( real
b56b0 6e 75 6d 20 29 20 2a 72 65 61 6c 6e 75 6d 20 3d num ) *realnum =
b56c0 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 1;. }. return
b56d0 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a *z==0;.}../*.**
b56e0 20 54 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20 The string z[]
b56f0 69 73 20 61 6e 20 61 73 63 69 69 20 72 65 70 72 is an ascii repr
b5700 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 esentation of a
b5710 72 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 real number..**
b5720 43 6f 6e 76 65 72 74 20 74 68 69 73 20 73 74 72 Convert this str
b5730 69 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e ing to a double.
b5740 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
b5750 69 6e 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 ine assumes that
b5760 20 7a 5b 5d 20 72 65 61 6c 6c 79 20 69 73 20 61 z[] really is a
b5770 20 76 61 6c 69 64 20 6e 75 6d 62 65 72 2e 20 20 valid number.
b5780 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c If it.** is not,
b5790 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 the result is u
b57a0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
b57b0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
b57c0 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 used instead of
b57d0 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 the library atof
b57e0 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 61 () function beca
b57f0 75 73 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 use.** the libra
b5800 72 79 20 61 74 6f 66 28 29 20 6d 69 67 68 74 20 ry atof() might
b5810 77 61 6e 74 20 74 6f 20 75 73 65 20 22 2c 22 20 want to use ","
b5820 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 as the decimal p
b5830 6f 69 6e 74 20 69 6e 73 74 65 61 64 0a 2a 2a 20 oint instead.**
b5840 6f 66 20 22 2e 22 20 64 65 70 65 6e 64 69 6e 67 of "." depending
b5850 20 6f 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 20 69 on how locale i
b5860 73 20 73 65 74 2e 20 20 42 75 74 20 74 68 61 74 s set. But that
b5870 20 77 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f would cause pro
b5880 62 6c 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 51 4c blems.** for SQL
b5890 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 . So this routi
b58a0 6e 65 20 61 6c 77 61 79 73 20 75 73 65 73 20 22 ne always uses "
b58b0 2e 22 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 ." regardless of
b58c0 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 locale..*/.SQLI
b58d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b58e0 71 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 qlite3AtoF(const
b58f0 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 char *z, double
b5900 20 2a 70 52 65 73 75 6c 74 29 7b 0a 23 69 66 6e *pResult){.#ifn
b5910 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
b5920 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 FLOATING_POINT.
b5930 20 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b 0a 20 int sign = 1;.
b5940 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 65 const char *zBe
b5950 67 69 6e 20 3d 20 7a 3b 0a 20 20 4c 4f 4e 47 44 gin = z;. LONGD
b5960 4f 55 42 4c 45 5f 54 59 50 45 20 76 31 20 3d 20 OUBLE_TYPE v1 =
b5970 30 2e 30 3b 0a 20 20 69 6e 74 20 6e 53 69 67 6e 0.0;. int nSign
b5980 69 66 69 63 61 6e 74 20 3d 20 30 3b 0a 20 20 77 ificant = 0;. w
b5990 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 hile( isspace(*(
b59a0 75 38 2a 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 u8*)z) ) z++;.
b59b0 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 if( *z=='-' ){.
b59c0 20 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 sign = -1;.
b59d0 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 z++;. }else i
b59e0 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 f( *z=='+' ){.
b59f0 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 z++;. }. whi
b5a00 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 7b le( z[0]=='0' ){
b5a10 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 . z++;. }.
b5a20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28 2a while( isdigit(*
b5a30 28 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 76 (u8*)z) ){. v
b5a40 31 20 3d 20 76 31 2a 31 30 2e 30 20 2b 20 28 2a 1 = v1*10.0 + (*
b5a50 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 7a 2b z - '0');. z+
b5a60 2b 3b 0a 20 20 20 20 6e 53 69 67 6e 69 66 69 63 +;. nSignific
b5a70 61 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 ant++;. }. if(
b5a80 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 *z=='.' ){.
b5a90 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 LONGDOUBLE_TYPE
b5aa0 64 69 76 69 73 6f 72 20 3d 20 31 2e 30 3b 0a 20 divisor = 1.0;.
b5ab0 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 69 66 28 20 z++;. if(
b5ac0 6e 53 69 67 6e 69 66 69 63 61 6e 74 3d 3d 30 20 nSignificant==0
b5ad0 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 ){. while(
b5ae0 7a 5b 30 5d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 z[0]=='0' ){.
b5af0 20 20 20 20 20 64 69 76 69 73 6f 72 20 2a 3d 20 divisor *=
b5b00 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 7a 2b 10.0;. z+
b5b10 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d +;. }. }
b5b20 0a 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 69 . while( isdi
b5b30 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a git(*(u8*)z) ){.
b5b40 20 20 20 20 20 20 69 66 28 20 6e 53 69 67 6e 69 if( nSigni
b5b50 66 69 63 61 6e 74 3c 31 38 20 29 7b 0a 20 20 20 ficant<18 ){.
b5b60 20 20 20 20 20 76 31 20 3d 20 76 31 2a 31 30 2e v1 = v1*10.
b5b70 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 0 + (*z - '0');.
b5b80 20 20 20 20 20 20 20 20 64 69 76 69 73 6f 72 20 divisor
b5b90 2a 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 *= 10.0;.
b5ba0 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 2b 2b 3b nSignificant++;
b5bb0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a . }. z
b5bc0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 76 31 ++;. }. v1
b5bd0 20 2f 3d 20 64 69 76 69 73 6f 72 3b 0a 20 20 7d /= divisor;. }
b5be0 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c . if( *z=='e' |
b5bf0 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 | *z=='E' ){.
b5c00 20 69 6e 74 20 65 73 69 67 6e 20 3d 20 31 3b 0a int esign = 1;.
b5c10 20 20 20 20 69 6e 74 20 65 76 61 6c 20 3d 20 30 int eval = 0
b5c20 3b 0a 20 20 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 ;. LONGDOUBLE
b5c30 5f 54 59 50 45 20 73 63 61 6c 65 20 3d 20 31 2e _TYPE scale = 1.
b5c40 30 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 0;. z++;.
b5c50 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 if( *z=='-' ){.
b5c60 20 20 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b esign = -1;
b5c70 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 . z++;.
b5c80 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b }else if( *z=='+
b5c90 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a ' ){. z++;.
b5ca0 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 }. while(
b5cb0 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a isdigit(*(u8*)z
b5cc0 29 20 29 7b 0a 20 20 20 20 20 20 65 76 61 6c 20 ) ){. eval
b5cd0 3d 20 65 76 61 6c 2a 31 30 20 2b 20 2a 7a 20 2d = eval*10 + *z -
b5ce0 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b '0';. z++;
b5cf0 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 . }. while
b5d00 28 20 65 76 61 6c 3e 3d 36 34 20 29 7b 20 73 63 ( eval>=64 ){ sc
b5d10 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 36 34 3b 20 ale *= 1.0e+64;
b5d20 65 76 61 6c 20 2d 3d 20 36 34 3b 20 7d 0a 20 20 eval -= 64; }.
b5d30 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 31 while( eval>=1
b5d40 36 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 6 ){ scale *= 1.
b5d50 30 65 2b 31 36 3b 20 65 76 61 6c 20 2d 3d 20 31 0e+16; eval -= 1
b5d60 36 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 6; }. while(
b5d70 65 76 61 6c 3e 3d 34 20 29 7b 20 73 63 61 6c 65 eval>=4 ){ scale
b5d80 20 2a 3d 20 31 2e 30 65 2b 34 3b 20 65 76 61 6c *= 1.0e+4; eval
b5d90 20 2d 3d 20 34 3b 20 7d 0a 20 20 20 20 77 68 69 -= 4; }. whi
b5da0 6c 65 28 20 65 76 61 6c 3e 3d 31 20 29 7b 20 73 le( eval>=1 ){ s
b5db0 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 cale *= 1.0e+1;
b5dc0 65 76 61 6c 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 eval -= 1; }.
b5dd0 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a if( esign<0 ){.
b5de0 20 20 20 20 20 20 76 31 20 2f 3d 20 73 63 61 6c v1 /= scal
b5df0 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 e;. }else{.
b5e00 20 20 20 20 76 31 20 2a 3d 20 73 63 61 6c 65 3b v1 *= scale;
b5e10 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 . }. }. *pR
b5e20 65 73 75 6c 74 20 3d 20 73 69 67 6e 3c 30 20 3f esult = sign<0 ?
b5e30 20 2d 76 31 20 3a 20 76 31 3b 0a 20 20 72 65 74 -v1 : v1;. ret
b5e40 75 72 6e 20 7a 20 2d 20 7a 42 65 67 69 6e 3b 0a urn z - zBegin;.
b5e50 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73 #else. return s
b5e60 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 qlite3Atoi64(z,
b5e70 70 52 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 pResult);.#endif
b5e80 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
b5e90 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a FLOATING_POINT *
b5ea0 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 /.}../*.** Compa
b5eb0 72 65 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 re the 19-charac
b5ec0 74 65 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 ter string zNum
b5ed0 61 67 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 against the text
b5ee0 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a representation.
b5ef0 2a 2a 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 ** value 2^63:
b5f00 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 9223372036854775
b5f10 38 30 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 808. Return neg
b5f20 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 ative, zero, or
b5f30 70 6f 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a positive.** if z
b5f40 4e 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e Num is less than
b5f50 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 , equal to, or g
b5f60 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
b5f70 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e string..**.** Un
b5f80 6c 69 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 like memcmp() th
b5f90 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75 is routine is gu
b5fa0 61 72 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75 aranteed to retu
b5fb0 72 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 rn the differenc
b5fc0 65 0a 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 e.** in the valu
b5fd0 65 73 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64 es of the last d
b5fe0 69 67 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 igit if the only
b5ff0 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 difference is i
b6000 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 n the.** last di
b6010 67 69 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 git. So, for ex
b6020 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 ample,.**.**
b6030 20 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 compare2pow63(
b6040 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 "922337203685477
b6050 35 38 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 5800").**.** wil
b6060 6c 20 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a l return -8..*/.
b6070 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 static int compa
b6080 72 65 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 re2pow63(const c
b6090 68 61 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e har *zNum){. in
b60a0 74 20 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d t c;. c = memcm
b60b0 70 28 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 p(zNum,"92233720
b60c0 33 36 38 35 34 37 37 35 38 30 22 2c 31 38 29 3b 3685477580",18);
b60d0 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 . if( c==0 ){.
b60e0 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 c = zNum[18]
b60f0 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74 - '8';. }. ret
b6100 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn c;.}.../*.**
b6110 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
b6120 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 74 zNum is a 64-bit
b6130 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
b6140 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 and write.** the
b6150 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e value of the in
b6160 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d teger into *pNum
b6170 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f . If zNum is no
b6180 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 t an integer.**
b6190 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 or is an integer
b61a0 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72 that is too lar
b61b0 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 ge to be express
b61c0 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73 2c ed with 64 bits,
b61d0 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 .** then return
b61e0 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 false..**.** Whe
b61f0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 n this routine w
b6200 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72 as originally wr
b6210 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 77 itten it dealt w
b6220 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 ith only.** 32-b
b6230 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 20 it numbers. At
b6240 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 61 that time, it wa
b6250 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 s much faster th
b6260 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29 an the.** atoi()
b6270 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65 library routine
b6280 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e 0a in RedHat 7.2..
b6290 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b62a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f E int sqlite3Ato
b62b0 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 2a i64(const char *
b62c0 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29 zNum, i64 *pNum)
b62d0 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 {. i64 v = 0;.
b62e0 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20 int neg;. int
b62f0 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 i, c;. const ch
b6300 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68 ar *zStart;. wh
b6310 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 ile( isspace(*(u
b6320 38 2a 29 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b 8*)zNum) ) zNum+
b6330 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d +;. if( *zNum==
b6340 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d '-' ){. neg =
b6350 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 1;. zNum++;.
b6360 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 }else if( *zNu
b6370 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65 m=='+' ){. ne
b6380 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b g = 0;. zNum+
b6390 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 +;. }else{.
b63a0 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a neg = 0;. }. z
b63b0 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20 Start = zNum;.
b63c0 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d while( zNum[0]==
b63d0 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d '0' ){ zNum++; }
b63e0 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 6c 65 /* Skip over le
b63f0 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63 ading zeros. Tic
b6400 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 66 ket #2454 */. f
b6410 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b or(i=0; (c=zNum[
b6420 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 i])>='0' && c<='
b6430 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 9'; i++){. v
b6440 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 = v*10 + c - '0'
b6450 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20 ;. }. *pNum =
b6460 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20 neg ? -v : v;.
b6470 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d if( c!=0 || (i==
b6480 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75 0 && zStart==zNu
b6490 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 m) || i>19 ){.
b64a0 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 /* zNum is emp
b64b0 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e ty or contains n
b64c0 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78 74 20 on-numeric text
b64d0 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 or is longer.
b64e0 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 69 67 69 ** than 19 digi
b64f0 74 73 20 28 74 68 75 73 20 67 75 61 72 61 6e 74 ts (thus guarant
b6500 69 6e 67 20 74 68 61 74 20 69 74 20 69 73 20 74 ing that it is t
b6510 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 oo large) */.
b6520 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c return 0;. }el
b6530 73 65 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 se if( i<19 ){.
b6540 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 /* Less than
b6550 31 39 20 64 69 67 69 74 73 2c 20 73 6f 20 77 65 19 digits, so we
b6560 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 20 66 69 know that it fi
b6570 74 73 20 69 6e 20 36 34 20 62 69 74 73 20 2a 2f ts in 64 bits */
b6580 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
b6590 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 }else{. /* 1
b65a0 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 73 20 9-digit numbers
b65b0 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 must be no large
b65c0 72 20 74 68 61 6e 20 39 32 32 33 33 37 32 30 33 r than 922337203
b65d0 36 38 35 34 37 37 35 38 30 37 20 69 66 20 70 6f 6854775807 if po
b65e0 73 69 74 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 sitive. ** or
b65f0 20 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 922337203685477
b6600 35 38 30 38 20 69 66 20 6e 65 67 61 74 69 76 65 5808 if negative
b6610 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 39 32 32 . Note that 922
b6620 33 33 37 32 30 33 36 38 35 34 36 36 35 38 30 38 3372036854665808
b6630 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e . ** is 2^63.
b6640 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 */. return c
b6650 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 ompare2pow63(zNu
b6660 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f m)<neg;. }.}../
b6670 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 *.** The string
b6680 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 zNum represents
b6690 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65 an integer. The
b66a0 72 65 20 6d 69 67 68 74 20 62 65 20 73 6f 6d 65 re might be some
b66b0 20 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d other.** inform
b66c0 61 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 ation following
b66d0 74 68 65 20 69 6e 74 65 67 65 72 20 74 6f 6f 2c the integer too,
b66e0 20 62 75 74 20 74 68 61 74 20 70 61 72 74 20 69 but that part i
b66f0 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 49 66 s ignored..** If
b6700 20 74 68 65 20 69 6e 74 65 67 65 72 20 74 68 61 the integer tha
b6710 74 20 74 68 65 20 70 72 65 66 69 78 20 6f 66 20 t the prefix of
b6720 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 zNum represents
b6730 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a 2a 2a will fit in a.**
b6740 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 64-bit signed i
b6750 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 54 nteger, return T
b6760 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73 65 20 RUE. Otherwise
b6770 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a return FALSE..**
b6780 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
b6790 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45 20 66 returns FALSE f
b67a0 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 2d 39 or the string -9
b67b0 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 2233720368547758
b67c0 30 38 20 65 76 65 6e 20 74 68 61 74 0a 2a 2a 20 08 even that.**
b67d0 74 68 61 74 20 6e 75 6d 62 65 72 20 77 69 6c 6c that number will
b67e0 2c 20 69 6e 20 74 68 65 6f 72 79 20 66 69 74 20 , in theory fit
b67f0 69 6e 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 in a 64-bit inte
b6800 67 65 72 2e 20 20 50 6f 73 69 74 69 76 65 0a 2a ger. Positive.*
b6810 2a 20 39 32 32 33 33 37 33 30 33 36 38 35 34 37 * 92233730368547
b6820 37 35 38 30 38 20 77 69 6c 6c 20 6e 6f 74 20 66 75808 will not f
b6830 69 74 20 69 6e 20 36 34 20 62 69 74 73 2e 20 20 it in 64 bits.
b6840 53 6f 20 69 74 20 73 65 65 6d 73 20 73 61 66 65 So it seems safe
b6850 72 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 66 r to return.** f
b6860 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f alse..*/.SQLITE_
b6870 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b6880 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 te3FitsIn64Bits(
b6890 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d const char *zNum
b68a0 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 29 7b 0a , int negFlag){.
b68b0 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e int i, c;. in
b68c0 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 28 t neg = 0;. if(
b68d0 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a 20 *zNum=='-' ){.
b68e0 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 neg = 1;.
b68f0 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 zNum++;. }else
b6900 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20 29 if( *zNum=='+' )
b6910 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 {. zNum++;.
b6920 7d 0a 20 20 69 66 28 20 6e 65 67 46 6c 61 67 20 }. if( negFlag
b6930 29 20 6e 65 67 20 3d 20 31 2d 6e 65 67 3b 0a 20 ) neg = 1-neg;.
b6940 20 77 68 69 6c 65 28 20 2a 7a 4e 75 6d 3d 3d 27 while( *zNum=='
b6950 30 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 0' ){. zNum++
b6960 3b 20 20 20 2f 2a 20 53 6b 69 70 20 6c 65 61 64 ; /* Skip lead
b6970 69 6e 67 20 7a 65 72 6f 73 2e 20 20 54 69 63 6b ing zeros. Tick
b6980 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 7d 0a et #2454 */. }.
b6990 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e for(i=0; (c=zN
b69a0 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 um[i])>='0' && c
b69b0 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 7d 0a 20 20 <='9'; i++){}.
b69c0 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 if( i<19 ){.
b69d0 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f /* Guaranteed to
b69e0 20 66 69 74 20 69 66 20 6c 65 73 73 20 74 68 61 fit if less tha
b69f0 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 n 19 digits */.
b6a00 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
b6a10 65 6c 73 65 20 69 66 28 20 69 3e 31 39 20 29 7b else if( i>19 ){
b6a20 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 . /* Guarante
b6a30 65 64 20 74 6f 20 62 65 20 74 6f 6f 20 62 69 67 ed to be too big
b6a40 20 69 66 20 67 72 65 61 74 65 72 20 74 68 61 6e if greater than
b6a50 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 19 digits */.
b6a60 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 return 0;. }e
b6a70 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 lse{. /* Comp
b6a80 61 72 65 20 61 67 61 69 6e 73 74 20 32 5e 36 33 are against 2^63
b6a90 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 . */. return
b6aa0 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e compare2pow63(zN
b6ab0 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a um)<neg;. }.}..
b6ac0 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20 72 65 /*.** If zNum re
b6ad0 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e 74 65 presents an inte
b6ae0 67 65 72 20 74 68 61 74 20 77 69 6c 6c 20 66 69 ger that will fi
b6af0 74 20 69 6e 20 33 32 2d 62 69 74 73 2c 20 74 68 t in 32-bits, th
b6b00 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 61 6c 75 en set.** *pValu
b6b10 65 20 74 6f 20 74 68 61 74 20 69 6e 74 65 67 65 e to that intege
b6b20 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 72 75 r and return tru
b6b30 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 e. Otherwise re
b6b40 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a turn false..**.*
b6b50 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 72 69 * Any non-numeri
b6b60 63 20 63 68 61 72 61 63 74 65 72 73 20 74 68 61 c characters tha
b6b70 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e 75 6d t following zNum
b6b80 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a are ignored..**
b6b90 20 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 This is differe
b6ba0 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 41 nt from sqlite3A
b6bb0 74 6f 69 36 34 28 29 20 77 68 69 63 68 20 72 65 toi64() which re
b6bc0 71 75 69 72 65 73 20 74 68 65 0a 2a 2a 20 69 6e quires the.** in
b6bd0 70 75 74 20 6e 75 6d 62 65 72 20 74 6f 20 62 65 put number to be
b6be0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
b6bf0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b6c00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 ATE int sqlite3G
b6c10 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 etInt32(const ch
b6c20 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 2a 70 ar *zNum, int *p
b6c30 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c 69 74 65 Value){. sqlite
b6c40 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b 0a 20 20 _int64 v = 0;.
b6c50 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e 74 20 int i, c;. int
b6c60 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a neg = 0;. if( z
b6c70 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 Num[0]=='-' ){.
b6c80 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 neg = 1;.
b6c90 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 zNum++;. }else
b6ca0 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2b 27 if( zNum[0]=='+'
b6cb0 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a ){. zNum++;.
b6cc0 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 }. while( zNu
b6cd0 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 4e 75 6d m[0]=='0' ) zNum
b6ce0 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 ++;. for(i=0; i
b6cf0 3c 31 31 20 26 26 20 28 63 20 3d 20 7a 4e 75 6d <11 && (c = zNum
b6d00 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30 20 26 26 [i] - '0')>=0 &&
b6d10 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20 20 20 c<=9; i++){.
b6d20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 3b 0a 20 v = v*10 + c;.
b6d30 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c 6f 6e }.. /* The lon
b6d40 67 65 73 74 20 64 65 63 69 6d 61 6c 20 72 65 70 gest decimal rep
b6d50 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 resentation of a
b6d60 20 33 32 20 62 69 74 20 69 6e 74 65 67 65 72 20 32 bit integer
b6d70 69 73 20 31 30 20 64 69 67 69 74 73 3a 0a 20 20 is 10 digits:.
b6d80 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 **. **
b6d90 20 20 20 20 31 32 33 34 35 36 37 38 39 30 0a 20 1234567890.
b6da0 20 2a 2a 20 20 20 20 20 32 5e 33 31 20 2d 3e 20 ** 2^31 ->
b6db0 32 31 34 37 34 38 33 36 34 38 0a 20 20 2a 2f 0a 2147483648. */.
b6dc0 20 20 69 66 28 20 69 3e 31 30 20 29 7b 0a 20 20 if( i>10 ){.
b6dd0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
b6de0 20 20 69 66 28 20 76 2d 6e 65 67 3e 32 31 34 37 if( v-neg>2147
b6df0 34 38 33 36 34 37 20 29 7b 0a 20 20 20 20 72 65 483647 ){. re
b6e00 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 turn 0;. }. if
b6e10 28 20 6e 65 67 20 29 7b 0a 20 20 20 20 76 20 3d ( neg ){. v =
b6e20 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 56 61 6c -v;. }. *pVal
b6e30 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a 20 20 72 ue = (int)v;. r
b6e40 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 1;.}../*.*
b6e50 2a 20 54 68 65 20 76 61 72 69 61 62 6c 65 2d 6c * The variable-l
b6e60 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 65 6e ength integer en
b6e70 63 6f 64 69 6e 67 20 69 73 20 61 73 20 66 6f 6c coding is as fol
b6e80 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a lows:.**.** KEY:
b6e90 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20 3d 20 .** A =
b6ea0 30 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69 0xxxxxxx 7 bi
b6eb0 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f ts of data and o
b6ec0 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 ne flag bit.**
b6ed0 20 20 20 20 20 20 20 42 20 3d 20 31 78 78 78 78 B = 1xxxx
b6ee0 78 78 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 xxx 7 bits of
b6ef0 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c data and one fl
b6f00 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 ag bit.**
b6f10 20 20 43 20 3d 20 78 78 78 78 78 78 78 78 20 20 C = xxxxxxxx
b6f20 20 20 38 20 62 69 74 73 20 6f 66 20 64 61 74 61 8 bits of data
b6f30 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d .**.** 7 bits -
b6f40 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 A.** 14 bits -
b6f50 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 BA.** 21 bits -
b6f60 42 42 41 0a 2a 2a 20 32 38 20 62 69 74 73 20 2d BBA.** 28 bits -
b6f70 20 42 42 42 41 0a 2a 2a 20 33 35 20 62 69 74 73 BBBA.** 35 bits
b6f80 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34 32 20 62 - BBBBA.** 42 b
b6f90 69 74 73 20 2d 20 42 42 42 42 42 41 0a 2a 2a 20 its - BBBBBA.**
b6fa0 34 39 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 49 bits - BBBBBB
b6fb0 41 0a 2a 2a 20 35 36 20 62 69 74 73 20 2d 20 42 A.** 56 bits - B
b6fc0 42 42 42 42 42 42 41 0a 2a 2a 20 36 34 20 62 69 BBBBBBA.** 64 bi
b6fd0 74 73 20 2d 20 42 42 42 42 42 42 42 42 43 0a 2a ts - BBBBBBBBC.*
b6fe0 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 /../*.** Write a
b6ff0 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 64-bit variable
b7000 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 -length integer
b7010 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 to memory starti
b7020 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 ng at p[0]..** T
b7030 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 he length of dat
b7040 61 20 77 72 69 74 65 20 77 69 6c 6c 20 62 65 20 a write will be
b7050 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 39 20 between 1 and 9
b7060 62 79 74 65 73 2e 20 20 54 68 65 20 6e 75 6d 62 bytes. The numb
b7070 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 77 er.** of bytes w
b7080 72 69 74 74 65 6e 20 69 73 20 72 65 74 75 72 6e ritten is return
b7090 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 ed..**.** A vari
b70a0 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 able-length inte
b70b0 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 ger consists of
b70c0 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 the lower 7 bits
b70d0 20 6f 66 20 65 61 63 68 20 62 79 74 65 0a 2a 2a of each byte.**
b70e0 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 73 20 74 for all bytes t
b70f0 68 61 74 20 68 61 76 65 20 74 68 65 20 38 74 68 hat have the 8th
b7100 20 62 69 74 20 73 65 74 20 61 6e 64 20 6f 6e 65 bit set and one
b7110 20 62 79 74 65 20 77 69 74 68 20 74 68 65 20 38 byte with the 8
b7120 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65 61 72 2e th.** bit clear.
b7130 20 20 45 78 63 65 70 74 2c 20 69 66 20 77 65 20 Except, if we
b7140 67 65 74 20 74 6f 20 74 68 65 20 39 74 68 20 62 get to the 9th b
b7150 79 74 65 2c 20 69 74 20 73 74 6f 72 65 73 20 74 yte, it stores t
b7160 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 he full.** 8 bit
b7170 73 20 61 6e 64 20 69 73 20 74 68 65 20 6c 61 73 s and is the las
b7180 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 t byte..*/.SQLIT
b7190 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b71a0 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 lite3PutVarint(u
b71b0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c nsigned char *p,
b71c0 20 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 u64 v){. int i
b71d0 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 62 75 66 , j, n;. u8 buf
b71e0 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76 20 26 20 [10];. if( v &
b71f0 28 28 28 75 36 34 29 30 78 66 66 30 30 30 30 30 (((u64)0xff00000
b7200 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20 20 20 70 0)<<32) ){. p
b7210 5b 38 5d 20 3d 20 76 3b 0a 20 20 20 20 76 20 3e [8] = v;. v >
b7220 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d >= 8;. for(i=
b7230 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 7; i>=0; i--){.
b7240 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 76 20 26 p[i] = (v &
b7250 20 30 78 37 66 29 20 7c 20 30 78 38 30 3b 0a 20 0x7f) | 0x80;.
b7260 20 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 v >>= 7;.
b7270 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 39 }. return 9
b7280 3b 0a 20 20 7d 20 20 20 20 0a 20 20 6e 20 3d 20 ;. } . n =
b7290 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 62 75 66 0;. do{. buf
b72a0 5b 6e 2b 2b 5d 20 3d 20 28 76 20 26 20 30 78 37 [n++] = (v & 0x7
b72b0 66 29 20 7c 20 30 78 38 30 3b 0a 20 20 20 20 76 f) | 0x80;. v
b72c0 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 >>= 7;. }while
b72d0 28 20 76 21 3d 30 20 29 3b 0a 20 20 62 75 66 5b ( v!=0 );. buf[
b72e0 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20 20 61 73 0] &= 0x7f;. as
b72f0 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 sert( n<=9 );.
b7300 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 for(i=0, j=n-1;
b7310 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b j>=0; j--, i++){
b7320 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b . p[i] = buf[
b7330 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e j];. }. return
b7340 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 n;.}../*.** Thi
b7350 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 66 s routine is a f
b7360 61 73 74 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 aster version of
b7370 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e sqlite3PutVarin
b7380 74 28 29 20 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a t() that only.**
b7390 20 77 6f 72 6b 73 20 66 6f 72 20 33 32 2d 62 69 works for 32-bi
b73a0 74 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 t positive integ
b73b0 65 72 73 20 61 6e 64 20 77 68 69 63 68 20 69 73 ers and which is
b73c0 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 0a 2a optimized for.*
b73d0 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 * the common cas
b73e0 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e 74 65 67 e of small integ
b73f0 65 72 73 2e 20 20 41 20 4d 41 43 52 4f 20 76 65 ers. A MACRO ve
b7400 72 73 69 6f 6e 2c 20 70 75 74 56 61 72 69 6e 74 rsion, putVarint
b7410 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 32,.** is provid
b7420 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73 ed which inlines
b7430 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 the single-byte
b7440 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 case. All code
b7450 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a 2a 20 74 should use.** t
b7460 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e he MACRO version
b7470 20 61 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f as this functio
b7480 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 n assumes the si
b7490 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 ngle-byte case h
b74a0 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 as.** already be
b74b0 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 en handled..*/.S
b74c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b74d0 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 t sqlite3PutVari
b74e0 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 nt32(unsigned ch
b74f0 61 72 20 2a 70 2c 20 75 33 32 20 76 29 7b 0a 23 ar *p, u32 v){.#
b7500 69 66 6e 64 65 66 20 70 75 74 56 61 72 69 6e 74 ifndef putVarint
b7510 33 32 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 32. if( (v & ~0
b7520 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 x7f)==0 ){. p
b7530 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20 72 65 74 [0] = v;. ret
b7540 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 urn 1;. }.#endi
b7550 66 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 f. if( (v & ~0x
b7560 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 3fff)==0 ){.
b7570 70 5b 30 5d 20 3d 20 28 76 3e 3e 37 29 20 7c 20 p[0] = (v>>7) |
b7580 30 78 38 30 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 0x80;. p[1] =
b7590 20 76 20 26 20 30 78 37 66 3b 0a 20 20 20 20 72 v & 0x7f;. r
b75a0 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72 eturn 2;. }. r
b75b0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74 eturn sqlite3Put
b75c0 56 61 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a Varint(p, v);.}.
b75d0 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 ./*.** Read a 64
b75e0 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 -bit variable-le
b75f0 6e 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f ngth integer fro
b7600 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e m memory startin
b7610 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 g at p[0]..** Re
b7620 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
b7630 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 of bytes read.
b7640 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f The value is sto
b7650 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 red in *v..*/.SQ
b7660 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b7670 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
b7680 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 t(const unsigned
b7690 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 char *p, u64 *v
b76a0 29 7b 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a ){. u32 a,b,s;.
b76b0 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 . a = *p;. /*
b76c0 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 a: p0 (unmasked)
b76d0 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
b76e0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 80)). {. *v
b76f0 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = a;. return
b7700 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 1;. }.. p++;.
b7710 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a b = *p;. /* b:
b7720 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p1 (unmasked) *
b7730 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 /. if (!(b&0x80
b7740 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 )). {. a &=
b7750 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 0x7f;. a = a<
b7760 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a <7;. a |= b;.
b7770 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 *v = a;.
b7780 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 return 2;. }..
b7790 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 p++;. a = a<<1
b77a0 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 4;. a |= *p;.
b77b0 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 /* a: p0<<14 | p
b77c0 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 2 (unmasked) */.
b77d0 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 if (!(a&0x80))
b77e0 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 . {. a &= (0
b77f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b7800 0a 20 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a . b &= 0x7f;.
b7810 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 b = b<<7;.
b7820 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 a |= b;. *v
b7830 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e = a;. return
b7840 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 3;. }.. /* CS
b7850 45 31 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f E1 from below */
b7860 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 . a &= (0x7f<<1
b7870 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 4)|(0x7f);. p++
b7880 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 ;. b = b<<14;.
b7890 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 b |= *p;. /* b
b78a0 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 : p1<<14 | p3 (u
b78b0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
b78c0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b (!(b&0x80)). {
b78d0 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c . b &= (0x7f<
b78e0 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 <14)|(0x7f);.
b78f0 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 /* moved CSE1 u
b7900 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d p */. /* a &=
b7910 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
b7920 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 f); */. a = a
b7930 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b <<7;. a |= b;
b7940 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 . *v = a;.
b7950 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a return 4;. }..
b7960 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c /* a: p0<<14 |
b7970 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a p2 (masked) */.
b7980 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c /* b: p1<<14 |
b7990 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p3 (unmasked) *
b79a0 2f 0a 20 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 /. /* 1:save of
b79b0 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 f p0<<21 | p1<<1
b79c0 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 4 | p2<<7 | p3 (
b79d0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 masked) */. /*
b79e0 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f moved CSE1 up */
b79f0 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 . /* a &= (0x7f
b7a00 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f <<14)|(0x7f); */
b7a10 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 . b &= (0x7f<<1
b7a20 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 4)|(0x7f);. s =
b7a30 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c a;. /* s: p0<<
b7a40 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 14 | p2 (masked)
b7a50 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 */.. p++;. a
b7a60 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 = a<<14;. a |=
b7a70 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c *p;. /* a: p0<<
b7a80 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 28 | p2<<14 | p4
b7a90 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b7aa0 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a if (!(a&0x80)).
b7ab0 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 {. /* we ca
b7ac0 6e 20 73 6b 69 70 20 74 68 65 73 65 20 63 61 75 n skip these cau
b7ad0 73 65 20 74 68 65 79 20 77 65 72 65 20 28 65 66 se they were (ef
b7ae0 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 fectively) done
b7af0 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e above in calc'in
b7b00 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 g s */. /* a
b7b10 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 &= (0x7f<<28)|(0
b7b20 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b7b30 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 */. /* b &=
b7b40 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b7b50 29 3b 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c ); */. b = b<
b7b60 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a <7;. a |= b;.
b7b70 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 s = s>>18;.
b7b80 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 *v = ((u64)s)
b7b90 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 <<32 | a;. re
b7ba0 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f turn 5;. }.. /
b7bb0 2a 20 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c * 2:save off p0<
b7bc0 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 <21 | p1<<14 | p
b7bd0 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 2<<7 | p3 (maske
b7be0 64 29 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 d) */. s = s<<7
b7bf0 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a ;. s |= b;. /*
b7c00 20 73 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c s: p0<<21 | p1<
b7c10 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 <14 | p2<<7 | p3
b7c20 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 (masked) */..
b7c30 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 p++;. b = b<<14
b7c40 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. b |= *p;. /
b7c50 2a 20 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 * b: p1<<28 | p3
b7c60 3c 3c 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 <<14 | p5 (unmas
b7c70 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
b7c80 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 b&0x80)). {.
b7c90 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 /* we can skip
b7ca0 74 68 69 73 20 63 61 75 73 65 20 69 74 20 77 61 this cause it wa
b7cb0 73 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 s (effectively)
b7cc0 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 done above in ca
b7cd0 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 lc'ing s */.
b7ce0 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 /* b &= (0x7f<<2
b7cf0 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 8)|(0x7f<<14)|(0
b7d00 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 x7f); */. a &
b7d10 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
b7d20 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 7f);. a = a<<
b7d30 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 7;. a |= b;.
b7d40 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 s = s>>18;.
b7d50 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c *v = ((u64)s)<
b7d60 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 <32 | a;. ret
b7d70 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b urn 6;. }.. p+
b7d80 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a +;. a = a<<14;.
b7d90 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 a |= *p;. /*
b7da0 61 3a 20 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c a: p2<<28 | p4<<
b7db0 31 34 20 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 14 | p6 (unmaske
b7dc0 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 d) */. if (!(a&
b7dd0 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 0x80)). {. a
b7de0 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 &= (0x7f<<28)|(
b7df0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b7e00 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 ;. b &= (0x7f
b7e10 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
b7e20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
b7e30 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 a |= b;. s =
b7e40 73 3e 3e 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 s>>11;. *v =
b7e50 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 ((u64)s)<<32 | a
b7e60 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a ;. return 7;.
b7e70 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 }.. /* CSE2 f
b7e80 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 rom below */. a
b7e90 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
b7ea0 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 0x7f);. p++;.
b7eb0 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c b = b<<14;. b |
b7ec0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 = *p;. /* b: p3
b7ed0 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 <<28 | p5<<14 |
b7ee0 70 37 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p7 (unmasked) */
b7ef0 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 . if (!(b&0x80)
b7f00 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 ). {. b &= (
b7f10 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 0x7f<<28)|(0x7f<
b7f20 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 <14)|(0x7f);.
b7f30 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 /* moved CSE2 u
b7f40 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d p */. /* a &=
b7f50 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
b7f60 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 f); */. a = a
b7f70 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b <<7;. a |= b;
b7f80 0a 20 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 . s = s>>4;.
b7f90 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 *v = ((u64)s)
b7fa0 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 <<32 | a;. re
b7fb0 74 75 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 turn 8;. }.. p
b7fc0 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b ++;. a = a<<15;
b7fd0 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . a |= *p;. /*
b7fe0 20 61 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c a: p4<<29 | p6<
b7ff0 3c 31 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b <15 | p8 (unmask
b8000 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 ed) */.. /* mov
b8010 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 ed CSE2 up */.
b8020 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 /* a &= (0x7f<<2
b8030 39 29 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 9)|(0x7f<<15)|(0
b8040 78 66 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 xff); */. b &=
b8050 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b8060 29 3b 0a 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 );. b = b<<8;.
b8070 20 61 20 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 a |= b;.. s =
b8080 73 3c 3c 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 s<<4;. b = p[-4
b8090 5d 3b 0a 20 20 62 20 26 3d 20 30 78 37 66 3b 0a ];. b &= 0x7f;.
b80a0 20 20 62 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 b = b>>3;. s
b80b0 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 |= b;.. *v = ((
b80c0 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a u64)s)<<32 | a;.
b80d0 0a 20 20 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a . return 9;.}..
b80e0 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d /*.** Read a 32-
b80f0 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e bit variable-len
b8100 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d gth integer from
b8110 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 memory starting
b8120 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 at p[0]..** Ret
b8130 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
b8140 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 f bytes read. T
b8150 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 he value is stor
b8160 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a 20 41 20 4d ed in *v..** A M
b8170 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 67 65 ACRO version, ge
b8180 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20 70 72 tVarint32, is pr
b8190 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c ovided which inl
b81a0 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73 69 6e ines the .** sin
b81b0 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 20 gle-byte case.
b81c0 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 All code should
b81d0 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 76 65 use the MACRO ve
b81e0 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 69 rsion as .** thi
b81f0 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d s function assum
b8200 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 es the single-by
b8210 74 65 20 63 61 73 65 20 68 61 73 20 61 6c 72 65 te case has alre
b8220 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 ady been handled
b8230 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b8240 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 ATE int sqlite3G
b8250 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 etVarint32(const
b8260 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
b8270 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 33 p, u32 *v){. u3
b8280 32 20 61 2c 62 3b 0a 0a 20 20 61 20 3d 20 2a 70 2 a,b;.. a = *p
b8290 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e ;. /* a: p0 (un
b82a0 6d 61 73 6b 65 64 29 20 2a 2f 0a 23 69 66 6e 64 masked) */.#ifnd
b82b0 65 66 20 67 65 74 56 61 72 69 6e 74 33 32 0a 20 ef getVarint32.
b82c0 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a if (!(a&0x80)).
b82d0 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a {. *v = a;.
b82e0 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
b82f0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70 2b 2b 3b }.#endif.. p++;
b8300 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 . b = *p;. /*
b8310 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29 b: p1 (unmasked)
b8320 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 */. if (!(b&0x
b8330 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 80)). {. a &
b8340 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 = 0x7f;. a =
b8350 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 a<<7;. *v = a
b8360 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e | b;. return
b8370 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 2;. }.. p++;.
b8380 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 a = a<<14;. a
b8390 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 |= *p;. /* a:
b83a0 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d p0<<14 | p2 (unm
b83b0 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b83c0 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(a&0x80)). {.
b83d0 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 a &= (0x7f<<1
b83e0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 4)|(0x7f);. b
b83f0 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 &= 0x7f;. b
b8400 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d = b<<7;. *v =
b8410 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 a | b;. retu
b8420 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b rn 3;. }.. p++
b8430 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 ;. b = b<<14;.
b8440 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 b |= *p;. /* b
b8450 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 : p1<<14 | p3 (u
b8460 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
b8470 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b (!(b&0x80)). {
b8480 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c . b &= (0x7f<
b8490 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 <14)|(0x7f);.
b84a0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b84b0 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d |(0x7f);. a =
b84c0 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 a<<7;. *v =
b84d0 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 a | b;. retur
b84e0 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b n 4;. }.. p++;
b84f0 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 . a = a<<14;.
b8500 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a |= *p;. /* a:
b8510 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 p0<<28 | p2<<14
b8520 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 | p4 (unmasked)
b8530 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
b8540 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 80)). {. a &
b8550 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 = (0x7f<<28)|(0x
b8560 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b8570 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c b &= (0x7f<<
b8580 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 28)|(0x7f<<14)|(
b8590 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 62 0x7f);. b = b
b85a0 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 <<7;. *v = a
b85b0 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 | b;. return
b85c0 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 5;. }.. /* We
b85d0 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 68 20 74 can only reach t
b85e0 68 69 73 20 70 6f 69 6e 74 20 77 68 65 6e 20 72 his point when r
b85f0 65 61 64 69 6e 67 20 61 20 63 6f 72 72 75 70 74 eading a corrupt
b8600 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 database. ** f
b8610 69 6c 65 2e 20 20 49 6e 20 74 68 61 74 20 63 61 ile. In that ca
b8620 73 65 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e se we are not in
b8630 20 61 6e 79 20 68 75 72 72 79 2e 20 20 55 73 65 any hurry. Use
b8640 20 74 68 65 20 28 72 65 6c 61 74 69 76 65 6c 79 the (relatively
b8650 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67 65 6e 65 . ** slow) gene
b8660 72 61 6c 2d 70 75 72 70 6f 73 65 20 73 71 6c 69 ral-purpose sqli
b8670 74 65 33 47 65 74 56 61 72 69 6e 74 28 29 20 72 te3GetVarint() r
b8680 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61 63 outine to extrac
b8690 74 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75 65 t the. ** value
b86a0 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 36 34 . */. {. u64
b86b0 20 76 36 34 3b 0a 20 20 20 20 69 6e 74 20 6e 3b v64;. int n;
b86c0 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a 20 20 .. p -= 4;.
b86d0 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 n = sqlite3Get
b86e0 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29 3b Varint(p, &v64);
b86f0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 35 . assert( n>5
b8700 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20 20 && n<=9 );.
b8710 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a 20 *v = (u32)v64;.
b8720 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d return n;. }
b8730 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
b8740 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
b8750 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20 62 ytes that will b
b8760 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74 6f 72 e needed to stor
b8770 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 36 e the given.** 6
b8780 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 4-bit integer..*
b8790 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b87a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72 69 int sqlite3Vari
b87b0 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b 0a 20 20 ntLen(u64 v){.
b87c0 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b int i = 0;. do{
b87d0 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20 . i++;. v
b87e0 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 >>= 7;. }while(
b87f0 20 76 21 3d 30 20 26 26 20 69 3c 39 20 29 3b 0a v!=0 && i<9 );.
b8800 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 0a return i;.}...
b8810 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 /*.** Read or wr
b8820 69 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65 20 ite a four-byte
b8830 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 big-endian integ
b8840 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c er value..*/.SQL
b8850 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
b8860 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 sqlite3Get4byte(
b8870 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20 20 const u8 *p){.
b8880 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32 34 return (p[0]<<24
b8890 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20 7c ) | (p[1]<<16) |
b88a0 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b 33 (p[2]<<8) | p[3
b88b0 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 ];.}.SQLITE_PRIV
b88c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
b88d0 50 75 74 34 62 79 74 65 28 75 6e 73 69 67 6e 65 Put4byte(unsigne
b88e0 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 d char *p, u32 v
b88f0 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 76 3e 3e 32 ){. p[0] = v>>2
b8900 34 3b 0a 20 20 70 5b 31 5d 20 3d 20 76 3e 3e 31 4;. p[1] = v>>1
b8910 36 3b 0a 20 20 70 5b 32 5d 20 3d 20 76 3e 3e 38 6;. p[2] = v>>8
b8920 3b 0a 20 20 70 5b 33 5d 20 3d 20 76 3b 0a 7d 0a ;. p[3] = v;.}.
b8930 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 ...#if !defined(
b8940 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 SQLITE_OMIT_BLOB
b8950 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 _LITERAL) || def
b8960 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f ined(SQLITE_HAS_
b8970 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72 61 CODEC)./*.** Tra
b8980 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 nslate a single
b8990 62 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74 6f byte of Hex into
b89a0 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 an integer..**
b89b0 54 68 69 73 20 72 6f 75 74 69 6e 65 6e 20 6f 6e This routinen on
b89c0 6c 79 20 77 6f 72 6b 73 20 69 66 20 68 20 72 65 ly works if h re
b89d0 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 ally is a valid
b89e0 68 65 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63 hexadecimal.** c
b89f0 68 61 72 61 63 74 65 72 3a 20 20 30 2e 2e 39 61 haracter: 0..9a
b8a00 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74 69 ..fA..F.*/.stati
b8a10 63 20 69 6e 74 20 68 65 78 54 6f 49 6e 74 28 69 c int hexToInt(i
b8a20 6e 74 20 68 29 7b 0a 20 20 61 73 73 65 72 74 28 nt h){. assert(
b8a30 20 28 68 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27 (h>='0' && h<='
b8a40 39 27 29 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20 9') || (h>='a'
b8a50 26 26 20 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28 && h<='f') || (
b8a60 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d 27 46 27 h>='A' && h<='F'
b8a70 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 ) );.#ifdef SQLI
b8a80 54 45 5f 41 53 43 49 49 0a 20 20 68 20 2b 3d 20 TE_ASCII. h +=
b8a90 39 2a 28 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65 9*(1&(h>>6));.#e
b8aa0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
b8ab0 54 45 5f 45 42 43 44 49 43 0a 20 20 68 20 2b 3d TE_EBCDIC. h +=
b8ac0 20 39 2a 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 9*(1&~(h>>4));.
b8ad0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
b8ae0 68 20 26 20 30 78 66 3b 0a 7d 0a 23 65 6e 64 69 h & 0xf;.}.#endi
b8af0 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 f /* !SQLITE_OMI
b8b00 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c T_BLOB_LITERAL |
b8b10 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 | SQLITE_HAS_COD
b8b20 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 EC */..#if !defi
b8b30 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
b8b40 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c BLOB_LITERAL) ||
b8b50 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
b8b60 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a HAS_CODEC)./*.**
b8b70 20 43 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 Convert a BLOB
b8b80 6c 69 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66 literal of the f
b8b90 6f 72 6d 20 22 78 27 68 68 68 68 68 68 27 22 20 orm "x'hhhhhh'"
b8ba0 69 6e 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a into its binary.
b8bb0 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72 ** value. Retur
b8bc0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 n a pointer to i
b8bd0 74 73 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e ts binary value.
b8be0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 Space to hold
b8bf0 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 the.** binary va
b8c00 6c 75 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74 lue has been obt
b8c10 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
b8c20 63 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 c and must be fr
b8c30 65 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 eed by.** the ca
b8c40 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a lling routine..*
b8c50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b8c60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 void *sqlite3He
b8c70 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 xToBlob(sqlite3
b8c80 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *db, const char
b8c90 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 *z, int n){. ch
b8ca0 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 ar *zBlob;. int
b8cb0 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 i;.. zBlob = (
b8cc0 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 char *)sqlite3Db
b8cd0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f MallocRaw(db, n/
b8ce0 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 2 + 1);. n--;.
b8cf0 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 if( zBlob ){.
b8d00 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 for(i=0; i<n;
b8d10 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c i+=2){. zBl
b8d20 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f ob[i/2] = (hexTo
b8d30 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 Int(z[i])<<4) |
b8d40 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 hexToInt(z[i+1])
b8d50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f ;. }. zBlo
b8d60 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a b[i/2] = 0;. }.
b8d70 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a return zBlob;.
b8d80 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
b8d90 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 ITE_OMIT_BLOB_LI
b8da0 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f TERAL || SQLITE_
b8db0 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f HAS_CODEC */.../
b8dc0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
b8dd0 73 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f sqlite.magic fro
b8de0 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f m SQLITE_MAGIC_O
b8df0 50 45 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 PEN to SQLITE_MA
b8e00 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 GIC_BUSY..** Ret
b8e10 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f urn an error (no
b8e20 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d n-zero) if the m
b8e30 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c agic was not SQL
b8e40 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a ITE_MAGIC_OPEN.*
b8e50 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 * when this rout
b8e60 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ine is called..*
b8e70 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
b8e80 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e e is called when
b8e90 20 65 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c entering an SQL
b8ea0 69 74 65 20 41 50 49 2e 20 20 54 68 65 20 53 51 ite API. The SQ
b8eb0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a LITE_MAGIC_OPEN.
b8ec0 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 ** value indicat
b8ed0 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 es that the data
b8ee0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
b8ef0 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 passed into the
b8f00 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 API is.** open a
b8f10 6e 64 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 nd is not being
b8f20 75 73 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 used by another
b8f30 74 68 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e thread. By chan
b8f40 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a ging the value.*
b8f50 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 * to SQLITE_MAGI
b8f60 43 5f 42 55 53 59 20 77 65 20 69 6e 64 69 63 61 C_BUSY we indica
b8f70 74 65 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e te that the conn
b8f80 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 ection is in use
b8f90 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 ..** sqlite3Safe
b8fa0 74 79 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69 tyOff() below wi
b8fb0 6c 6c 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 ll change the va
b8fc0 6c 75 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 lue back to SQLI
b8fd0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a TE_MAGIC_OPEN.**
b8fe0 20 77 68 65 6e 20 74 68 65 20 41 50 49 20 65 78 when the API ex
b8ff0 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 its. .**.** This
b9000 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74 routine is a at
b9010 74 65 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20 tempt to detect
b9020 69 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 75 if two threads u
b9030 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 se the.** same s
b9040 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 qlite* pointer a
b9050 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e t the same time.
b9060 20 20 54 68 65 72 65 20 69 73 20 61 20 72 61 63 There is a rac
b9070 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 e .** condition
b9080 73 6f 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c so it is possibl
b9090 65 20 74 68 61 74 20 74 68 65 20 65 72 72 6f 72 e that the error
b90a0 20 69 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64 is not detected
b90b0 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 ..** But usually
b90c0 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c the problem wil
b90d0 6c 20 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20 l be seen. The
b90e0 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 result will be a
b90f0 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68 n.** error which
b9100 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
b9110 64 65 62 75 67 20 74 68 65 20 61 70 70 6c 69 63 debug the applic
b9120 61 74 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a ation that is.**
b9130 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e using SQLite in
b9140 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a correctly..**.**
b9150 20 54 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49 Ticket #202: I
b9160 66 20 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e f db->magic is n
b9170 6f 74 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 ot a valid open
b9180 76 61 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65 value, take care
b9190 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 not.** to modif
b91a0 79 20 74 68 65 20 64 62 20 73 74 72 75 63 74 75 y the db structu
b91b0 72 65 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 re at all. It c
b91c0 6f 75 6c 64 20 62 65 20 74 68 61 74 20 64 62 20 ould be that db
b91d0 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f is a stale.** po
b91e0 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 inter. In other
b91f0 20 77 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 words, it could
b9200 20 62 65 20 74 68 61 74 20 74 68 65 72 65 20 68 be that there h
b9210 61 73 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a as been a prior.
b9220 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 ** call to sqlit
b9230 65 33 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 e3_close(db) and
b9240 20 64 62 20 68 61 73 20 62 65 65 6e 20 64 65 61 db has been dea
b9250 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77 llocated. And w
b9260 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 e do.** not want
b9270 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64 to write into d
b9280 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 eallocated memor
b9290 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c y..*/.#ifdef SQL
b92a0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 ITE_DEBUG.SQLITE
b92b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b92c0 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c ite3SafetyOn(sql
b92d0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 ite3 *db){. if(
b92e0 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 db->magic==SQLI
b92f0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b TE_MAGIC_OPEN ){
b9300 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d . db->magic =
b9310 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 SQLITE_MAGIC_BU
b9320 53 59 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 SY;. assert(
b9330 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
b9340 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b ld(db->mutex) );
b9350 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b9360 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d }else if( db->m
b9370 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 agic==SQLITE_MAG
b9380 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 IC_BUSY ){. d
b9390 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 b->magic = SQLIT
b93a0 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 E_MAGIC_ERROR;.
b93b0 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 db->u1.isInte
b93c0 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d rrupted = 1;. }
b93d0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 . return 1;.}.#
b93e0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 endif../*.** Cha
b93f0 6e 67 65 20 74 68 65 20 6d 61 67 69 63 20 66 72 nge the magic fr
b9400 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f om SQLITE_MAGIC_
b9410 42 55 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d BUSY to SQLITE_M
b9420 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 AGIC_OPEN..** Re
b9430 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e turn an error (n
b9440 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 on-zero) if the
b9450 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 magic was not SQ
b9460 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a LITE_MAGIC_BUSY.
b9470 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 ** when this rou
b9480 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a tine is called..
b9490 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
b94a0 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 _DEBUG.SQLITE_PR
b94b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b94c0 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 3SafetyOff(sqlit
b94d0 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 e3 *db){. if( d
b94e0 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 b->magic==SQLITE
b94f0 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 _MAGIC_BUSY ){.
b9500 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 db->magic = S
b9510 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b9520 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 ;. assert( sq
b9530 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
b9540 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
b9550 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
b9560 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 else{. db->ma
b9570 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 gic = SQLITE_MAG
b9580 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 IC_ERROR;. db
b9590 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 ->u1.isInterrupt
b95a0 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 ed = 1;. retu
b95b0 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 rn 1;. }.}.#end
b95c0 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 if../*.** Check
b95d0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20 to make sure we
b95e0 68 61 76 65 20 61 20 76 61 6c 69 64 20 64 62 20 have a valid db
b95f0 70 6f 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74 pointer. This t
b9600 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f est is not.** fo
b9610 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64 olproof but it d
b9620 6f 65 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 oes provide some
b9630 20 6d 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74 measure of prot
b9640 65 63 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a ection against.*
b9650 2a 20 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20 * misuse of the
b9660 69 6e 74 65 72 66 61 63 65 20 73 75 63 68 20 61 interface such a
b9670 73 20 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20 s passing in db
b9680 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20 61 72 pointers that ar
b9690 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 e.** NULL or whi
b96a0 63 68 20 68 61 76 65 20 62 65 65 6e 20 70 72 65 ch have been pre
b96b0 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 viously closed.
b96c0 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 If this routine
b96d0 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 returns.** 1 it
b96e0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
b96f0 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 db pointer is va
b9700 6c 69 64 20 61 6e 64 20 30 20 69 66 20 69 74 20 lid and 0 if it
b9710 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a should not be.**
b9720 20 64 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f dereferenced fo
b9730 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 r any reason. T
b9740 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
b9750 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b ion should invok
b9760 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 e.** SQLITE_MISU
b9770 53 45 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a SE immediately..
b9780 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 **.** sqlite3Saf
b9790 65 74 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 etyCheckOk() req
b97a0 75 69 72 65 73 20 74 68 61 74 20 74 68 65 20 64 uires that the d
b97b0 62 20 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c b pointer be val
b97c0 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 id for.** use.
b97d0 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 sqlite3SafetyChe
b97e0 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c ckSickOrOk() all
b97f0 6f 77 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72 ows a db pointer
b9800 20 74 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a that failed to.
b9810 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 ** open properly
b9820 20 61 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20 and is not fit
b9830 66 6f 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20 for general use
b9840 62 75 74 20 77 68 69 63 68 20 63 61 6e 20 62 65 but which can be
b9850 0a 2a 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61 .** used as an a
b9860 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
b9870 65 33 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73 e3_errmsg() or s
b9880 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a qlite3_close()..
b9890 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b98a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 E int sqlite3Saf
b98b0 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 etyCheckOk(sqlit
b98c0 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d e3 *db){. u32 m
b98d0 61 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d agic;. if( db==
b98e0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
b98f0 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 magic = db->magi
b9900 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d c;. if( magic!=
b9910 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
b9920 4e 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 N &&. magic
b9930 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 !=SQLITE_MAGIC_B
b9940 55 53 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a USY ) return 0;.
b9950 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 53 51 return 1;.}.SQ
b9960 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b9970 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 sqlite3SafetyCh
b9980 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 eckSickOrOk(sqli
b9990 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 te3 *db){. u32
b99a0 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d magic;. if( db=
b99b0 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
b99c0 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 magic = db->mag
b99d0 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21 ic;. if( magic!
b99e0 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 =SQLITE_MAGIC_SI
b99f0 43 4b 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 CK &&. magi
b9a00 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f c!=SQLITE_MAGIC_
b9a10 4f 50 45 4e 20 26 26 0a 20 20 20 20 20 20 6d 61 OPEN &&. ma
b9a20 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 gic!=SQLITE_MAGI
b9a30 43 5f 42 55 53 59 20 29 20 72 65 74 75 72 6e 20 C_BUSY ) return
b9a40 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0;. return 1;.}
b9a50 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
b9a60 2a 20 45 6e 64 20 6f 66 20 75 74 69 6c 2e 63 20 * End of util.c
b9a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9aa0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
b9ab0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 * Begin file has
b9ac0 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h.c ************
b9ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9af0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
b9b00 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 tember 22.**.**
b9b10 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
b9b20 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
b9b30 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
b9b40 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
b9b50 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
b9b60 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
b9b70 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
b9b80 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
b9b90 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
b9ba0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
b9bb0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
b9bc0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
b9bd0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
b9be0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
b9bf0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
b9c00 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
b9c10 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
b9c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9c60 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
b9c70 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e is the implemen
b9c80 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69 tation of generi
b9c90 63 20 68 61 73 68 2d 74 61 62 6c 65 73 0a 2a 2a c hash-tables.**
b9ca0 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e used in SQLite.
b9cb0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 .**.** $Id: hash
b9cc0 2e 63 2c 76 20 31 2e 33 31 20 32 30 30 38 2f 31 .c,v 1.31 2008/1
b9cd0 30 2f 31 30 20 31 37 3a 34 31 3a 32 39 20 64 72 0/10 17:41:29 dr
b9ce0 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 54 h Exp $.*/../* T
b9cf0 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 urn bulk memory
b9d00 69 6e 74 6f 20 61 20 68 61 73 68 20 74 61 62 6c into a hash tabl
b9d10 65 20 6f 62 6a 65 63 74 20 62 79 20 69 6e 69 74 e object by init
b9d20 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 ializing the.**
b9d30 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 48 61 fields of the Ha
b9d40 73 68 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a sh structure..**
b9d50 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73 20 61 20 .** "pNew" is a
b9d60 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 68 pointer to the h
b9d70 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 69 ash table that i
b9d80 73 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 s to be initiali
b9d90 7a 65 64 2e 0a 2a 2a 20 22 63 6f 70 79 4b 65 79 zed..** "copyKey
b9da0 22 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65 " is true if the
b9db0 20 68 61 73 68 20 74 61 62 6c 65 20 73 68 6f 75 hash table shou
b9dc0 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 ld make its own
b9dd0 70 72 69 76 61 74 65 0a 2a 2a 20 63 6f 70 79 20 private.** copy
b9de0 6f 66 20 6b 65 79 73 20 61 6e 64 20 66 61 6c 73 of keys and fals
b9df0 65 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6a e if it should j
b9e00 75 73 74 20 75 73 65 20 74 68 65 20 73 75 70 70 ust use the supp
b9e10 6c 69 65 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f lied pointer..*/
b9e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b9e30 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 void sqlite3Hash
b9e40 49 6e 69 74 28 48 61 73 68 20 2a 70 4e 65 77 2c Init(Hash *pNew,
b9e50 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 7b 0a 20 int copyKey){.
b9e60 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 assert( pNew!=0
b9e70 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79 );. pNew->copy
b9e80 4b 65 79 20 3d 20 63 6f 70 79 4b 65 79 21 3d 30 Key = copyKey!=0
b9e90 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20 ;. pNew->first
b9ea0 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 = 0;. pNew->cou
b9eb0 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e nt = 0;. pNew->
b9ec0 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e htsize = 0;. pN
b9ed0 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f ew->ht = 0;.}../
b9ee0 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 * Remove all ent
b9ef0 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68 ries from a hash
b9f00 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d table. Reclaim
b9f10 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 all memory..**
b9f20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e Call this routin
b9f30 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61 e to delete a ha
b9f40 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72 sh table or to r
b9f50 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62 6c eset a hash tabl
b9f60 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74 e.** to the empt
b9f70 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 y state..*/.SQLI
b9f80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
b9f90 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 sqlite3HashClear
b9fa0 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 61 (Hash *pH){. Ha
b9fb0 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 shElem *elem;
b9fc0 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f /* For loo
b9fd0 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c ping over all el
b9fe0 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61 ements of the ta
b9ff0 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ble */.. assert
ba000 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 ( pH!=0 );. ele
ba010 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 m = pH->first;.
ba020 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a pH->first = 0;.
ba030 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
ba040 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 H->ht);. pH->ht
ba050 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 = 0;. pH->htsi
ba060 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 ze = 0;. while(
ba070 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 73 elem ){. Has
ba080 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d hElem *next_elem
ba090 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 = elem->next;.
ba0a0 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b if( pH->copyK
ba0b0 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65 79 ey && elem->pKey
ba0c0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
ba0d0 33 5f 66 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 3_free(elem->pKe
ba0e0 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 y);. }. sq
ba0f0 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d 29 lite3_free(elem)
ba100 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65 78 ;. elem = nex
ba110 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70 48 t_elem;. }. pH
ba120 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a ->count = 0;.}..
ba130 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 /*.** Hash and c
ba140 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 omparison functi
ba150 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 ons when the mod
ba160 65 20 69 73 20 53 51 4c 49 54 45 5f 48 41 53 48 e is SQLITE_HASH
ba170 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 _STRING.*/.stati
ba180 63 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f c int strHash(co
ba190 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
ba1a0 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e int nKey){. con
ba1b0 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f st char *z = (co
ba1c0 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b nst char *)pKey;
ba1d0 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 . int h = 0;.
ba1e0 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b if( nKey<=0 ) nK
ba1f0 65 79 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a ey = strlen(z);.
ba200 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 while( nKey >
ba210 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 0 ){. h = (h
ba220 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 <<3) ^ h ^ sqlit
ba230 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 e3UpperToLower[(
ba240 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a unsigned char)*z
ba250 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b ++];. nKey--;
ba260 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20 . }. return h
ba270 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a & 0x7fffffff;.}.
ba280 73 74 61 74 69 63 20 69 6e 74 20 73 74 72 43 6f static int strCo
ba290 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 mpare(const void
ba2a0 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c *pKey1, int n1,
ba2b0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
ba2c0 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 y2, int n2){. i
ba2d0 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 f( n1!=n2 ) retu
ba2e0 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73 rn 1;. return s
ba2f0 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 qlite3StrNICmp((
ba300 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79 const char*)pKey
ba310 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 1,(const char*)p
ba320 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 0a 2f 2a Key2,n1);.}.../*
ba330 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d 65 6e 74 Link an element
ba340 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 into the hash t
ba350 61 62 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 able.*/.static v
ba360 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e oid insertElemen
ba370 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 t(. Hash *pH,
ba380 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
ba390 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 he complete hash
ba3a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 table */. stru
ba3b0 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 ct _ht *pEntry,
ba3c0 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 /* The entry
ba3d0 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 into which pNew
ba3e0 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 is inserted */.
ba3f0 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 HashElem *pNew
ba400 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 /* The e
ba410 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 lement to be ins
ba420 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 erted */.){. Ha
ba430 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 shElem *pHead;
ba440 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c /* First el
ba450 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e ement already in
ba460 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 70 48 65 pEntry */. pHe
ba470 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 ad = pEntry->cha
ba480 69 6e 3b 0a 20 20 69 66 28 20 70 48 65 61 64 20 in;. if( pHead
ba490 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 ){. pNew->nex
ba4a0 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 t = pHead;. p
ba4b0 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 New->prev = pHea
ba4c0 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 d->prev;. if(
ba4d0 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 pHead->prev ){
ba4e0 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 pHead->prev->nex
ba4f0 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 t = pNew; }.
ba500 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 else
ba510 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 { pH->first = p
ba520 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 New; }. pHead
ba530 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 ->prev = pNew;.
ba540 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 }else{. pNew
ba550 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 ->next = pH->fir
ba560 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e st;. if( pH->
ba570 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 first ){ pH->fir
ba580 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b st->prev = pNew;
ba590 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 }. pNew->pre
ba5a0 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 v = 0;. pH->f
ba5b0 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d irst = pNew;. }
ba5c0 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 . pEntry->count
ba5d0 2b 2b 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 63 68 ++;. pEntry->ch
ba5e0 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 0a ain = pNew;.}...
ba5f0 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68 61 /* Resize the ha
ba600 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74 sh table so that
ba610 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e 65 it cantains "ne
ba620 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73 2e w_size" buckets.
ba630 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a 65 22 20 6d .** "new_size" m
ba640 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f ust be a power o
ba650 66 20 32 2e 20 20 54 68 65 20 68 61 73 68 20 74 f 2. The hash t
ba660 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69 6c 20 able might fail
ba670 0a 2a 2a 20 74 6f 20 72 65 73 69 7a 65 20 69 66 .** to resize if
ba680 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
ba690 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 ) fails..*/.stat
ba6a0 69 63 20 76 6f 69 64 20 72 65 68 61 73 68 28 48 ic void rehash(H
ba6b0 61 73 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65 77 ash *pH, int new
ba6c0 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74 _size){. struct
ba6d0 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 _ht *new_ht;
ba6e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
ba6f0 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20 2a new hash table *
ba700 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c /. HashElem *el
ba710 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20 em, *next_elem;
ba720 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e /* For loopin
ba730 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 g over existing
ba740 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23 69 66 elements */..#if
ba750 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f def SQLITE_MALLO
ba760 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 0a 20 20 69 C_SOFT_LIMIT. i
ba770 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 f( new_size*size
ba780 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3e 53 of(struct _ht)>S
ba790 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 QLITE_MALLOC_SOF
ba7a0 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 6e T_LIMIT ){. n
ba7b0 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54 45 ew_size = SQLITE
ba7c0 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d _MALLOC_SOFT_LIM
ba7d0 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 IT/sizeof(struct
ba7e0 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 _ht);. }. if(
ba7f0 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e 68 new_size==pH->h
ba800 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e 3b 0a tsize ) return;.
ba810 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 #endif.. /* The
ba820 72 65 20 69 73 20 61 20 63 61 6c 6c 20 74 6f 20 re is a call to
ba830 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
ba840 20 69 6e 73 69 64 65 20 72 65 68 61 73 68 28 29 inside rehash()
ba850 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a 20 20 . If there is.
ba860 2a 2a 20 61 6c 72 65 61 64 79 20 61 6e 20 61 6c ** already an al
ba870 6c 6f 63 61 74 69 6f 6e 20 61 74 20 70 48 2d 3e location at pH->
ba880 68 74 2c 20 74 68 65 6e 20 69 66 20 74 68 69 73 ht, then if this
ba890 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 malloc() fails
ba8a0 69 74 0a 20 20 2a 2a 20 69 73 20 62 65 6e 69 67 it. ** is benig
ba8b0 6e 20 28 73 69 6e 63 65 20 66 61 69 6c 69 6e 67 n (since failing
ba8c0 20 74 6f 20 72 65 73 69 7a 65 20 61 20 68 61 73 to resize a has
ba8d0 68 20 74 61 62 6c 65 20 69 73 20 61 20 70 65 72 h table is a per
ba8e0 66 6f 72 6d 61 6e 63 65 0a 20 20 2a 2a 20 68 69 formance. ** hi
ba8f0 74 20 6f 6e 6c 79 2c 20 6e 6f 74 20 61 20 66 61 t only, not a fa
ba900 74 61 6c 20 65 72 72 6f 72 29 2e 0a 20 20 2a 2f tal error).. */
ba910 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a . if( pH->htsiz
ba920 65 3e 30 20 29 20 73 71 6c 69 74 65 33 42 65 67 e>0 ) sqlite3Beg
ba930 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
ba940 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 ;. new_ht = (st
ba950 72 75 63 74 20 5f 68 74 20 2a 29 73 71 6c 69 74 ruct _ht *)sqlit
ba960 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 6e 65 e3MallocZero( ne
ba970 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 w_size*sizeof(st
ba980 72 75 63 74 20 5f 68 74 29 20 29 3b 0a 20 20 69 ruct _ht) );. i
ba990 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 f( pH->htsize>0
ba9a0 29 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 ) sqlite3EndBeni
ba9b0 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 69 gnMalloc();.. i
ba9c0 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20 72 f( new_ht==0 ) r
ba9d0 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 eturn;. sqlite3
ba9e0 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 _free(pH->ht);.
ba9f0 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74 pH->ht = new_ht
baa00 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d ;. pH->htsize =
baa10 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20 66 6f 72 new_size;. for
baa20 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74 2c (elem=pH->first,
baa30 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65 6c pH->first=0; el
baa40 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f em; elem = next_
baa50 65 6c 65 6d 29 7b 0a 20 20 20 20 69 6e 74 20 68 elem){. int h
baa60 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d 2d = strHash(elem-
baa70 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 >pKey, elem->nKe
baa80 79 29 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31 y) & (new_size-1
baa90 29 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d );. next_elem
baaa0 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 = elem->next;.
baab0 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 insertElement
baac0 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c (pH, &new_ht[h],
baad0 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f elem);. }.}../
baae0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
baaf0 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 (for internal us
bab00 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20 e only) locates
bab10 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e an element in an
bab20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 74 .** hash table t
bab30 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 hat matches the
bab40 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 20 given key. The
bab50 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65 hash for this ke
bab60 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 y has.** already
bab70 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 61 been computed a
bab80 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 73 20 nd is passed as
bab90 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 the 4th paramete
baba0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 48 61 73 r..*/.static Has
babb0 68 45 6c 65 6d 20 2a 66 69 6e 64 45 6c 65 6d 65 hElem *findEleme
babc0 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 63 ntGivenHash(. c
babd0 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 20 onst Hash *pH,
babe0 20 20 20 2f 2a 20 54 68 65 20 70 48 20 74 6f 20 /* The pH to
babf0 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 be searched */.
bac00 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
bac10 79 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 y, /* The key
bac20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 we are searching
bac30 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b for */. int nK
bac40 65 79 2c 0a 20 20 69 6e 74 20 68 20 20 20 20 20 ey,. int h
bac50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
bac60 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b hash for this k
bac70 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 ey. */.){. Hash
bac80 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 Elem *elem;
bac90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
baca0 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 ed to loop thru
bacb0 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 the element list
bacc0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b */. int count;
bacd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bace0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
bacf0 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74 20 f elements left
bad00 74 6f 20 74 65 73 74 20 2a 2f 0a 0a 20 20 69 66 to test */.. if
bad10 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 ( pH->ht ){.
bad20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 struct _ht *pEnt
bad30 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b ry = &pH->ht[h];
bad40 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 45 6e 74 . elem = pEnt
bad50 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20 20 63 ry->chain;. c
bad60 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e 63 ount = pEntry->c
bad70 6f 75 6e 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 ount;. while(
bad80 20 63 6f 75 6e 74 2d 2d 20 26 26 20 65 6c 65 6d count-- && elem
bad90 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74 ){. if( st
bada0 72 43 6f 6d 70 61 72 65 28 65 6c 65 6d 2d 3e 70 rCompare(elem->p
badb0 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70 Key,elem->nKey,p
badc0 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 Key,nKey)==0 ){
badd0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
bade0 65 6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 elem;. }.
badf0 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d elem = elem-
bae00 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d >next;. }. }
bae10 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
bae20 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e 67 /* Remove a sing
bae30 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 le entry from th
bae40 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 e hash table giv
bae50 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 en a pointer to
bae60 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 that.** element
bae70 61 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74 68 and a hash on th
bae80 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79 2e e element's key.
bae90 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
baea0 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76 removeElementGiv
baeb0 65 6e 48 61 73 68 28 0a 20 20 48 61 73 68 20 2a enHash(. Hash *
baec0 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 pH, /* T
baed0 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e 67 he pH containing
baee0 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 48 61 73 "elem" */. Has
baef0 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20 2f hElem* elem, /
baf00 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f * The element to
baf10 20 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d be removed from
baf20 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e 74 the pH */. int
baf30 20 68 20 20 20 20 20 20 20 20 20 20 20 20 20 2f h /
baf40 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66 6f 72 * Hash value for
baf50 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a the element */.
baf60 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 ){. struct _ht
baf70 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 65 *pEntry;. if( e
baf80 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20 20 lem->prev ){.
baf90 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78 elem->prev->nex
bafa0 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20 t = elem->next;
bafb0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48 . }else{. pH
bafc0 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d 3e ->first = elem->
bafd0 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 next;. }. if(
bafe0 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 20 elem->next ){.
baff0 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72 elem->next->pr
bb000 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 3b ev = elem->prev;
bb010 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20 . }. pEntry =
bb020 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69 66 &pH->ht[h];. if
bb030 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d ( pEntry->chain=
bb040 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45 6e =elem ){. pEn
bb050 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 try->chain = ele
bb060 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 m->next;. }. p
bb070 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a Entry->count--;.
bb080 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 6f if( pEntry->co
bb090 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70 45 unt<=0 ){. pE
bb0a0 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30 3b ntry->chain = 0;
bb0b0 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 63 . }. if( pH->c
bb0c0 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 opyKey ){. sq
bb0d0 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d 2d lite3_free(elem-
bb0e0 3e 70 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 73 71 >pKey);. }. sq
bb0f0 6c 69 74 65 33 5f 66 72 65 65 28 20 65 6c 65 6d lite3_free( elem
bb100 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d );. pH->count-
bb110 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 -;. if( pH->cou
bb120 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 nt<=0 ){. ass
bb130 65 72 74 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d ert( pH->first==
bb140 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
bb150 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b pH->count==0 );
bb160 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 . sqlite3Hash
bb170 43 6c 65 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d Clear(pH);. }.}
bb180 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 ../* Attempt to
bb190 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e locate an elemen
bb1a0 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 t of the hash ta
bb1b0 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b 65 ble pH with a ke
bb1c0 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65 y.** that matche
bb1d0 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 s pKey,nKey. Re
bb1e0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
bb1f0 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 o the correspond
bb200 69 6e 67 20 0a 2a 2a 20 48 61 73 68 45 6c 65 6d ing .** HashElem
bb210 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 structure for t
bb220 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 his element if i
bb230 74 20 69 73 20 66 6f 75 6e 64 2c 20 6f 72 20 4e t is found, or N
bb240 55 4c 4c 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 ULL.** otherwise
bb250 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
bb260 41 54 45 20 48 61 73 68 45 6c 65 6d 20 2a 73 71 ATE HashElem *sq
bb270 6c 69 74 65 33 48 61 73 68 46 69 6e 64 45 6c 65 lite3HashFindEle
bb280 6d 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 m(const Hash *pH
bb290 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b , const void *pK
bb2a0 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 ey, int nKey){.
bb2b0 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 int h;
bb2c0 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20 6f 6e /* A hash on
bb2d0 20 6b 65 79 20 2a 2f 0a 20 20 48 61 73 68 45 6c key */. HashEl
bb2e0 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 em *elem; /*
bb2f0 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 74 The element that
bb300 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a matches key */.
bb310 0a 20 20 69 66 28 20 70 48 3d 3d 30 20 7c 7c 20 . if( pH==0 ||
bb320 70 48 2d 3e 68 74 3d 3d 30 20 29 20 72 65 74 75 pH->ht==0 ) retu
bb330 72 6e 20 30 3b 0a 20 20 68 20 3d 20 73 74 72 48 rn 0;. h = strH
bb340 61 73 68 28 70 4b 65 79 2c 6e 4b 65 79 29 3b 0a ash(pKey,nKey);.
bb350 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 elem = findEle
bb360 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 mentGivenHash(pH
bb370 2c 70 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 25 20 ,pKey,nKey, h %
bb380 70 48 2d 3e 68 74 73 69 7a 65 29 3b 0a 20 20 72 pH->htsize);. r
bb390 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 7d 0a 0a 2f eturn elem;.}../
bb3a0 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 * Attempt to loc
bb3b0 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f ate an element o
bb3c0 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 f the hash table
bb3d0 20 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a pH with a key.*
bb3e0 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 70 * that matches p
bb3f0 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 Key,nKey. Retur
bb400 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 n the data for t
bb410 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 his element if i
bb420 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f t is.** found, o
bb430 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 r NULL if there
bb440 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a is no match..*/.
bb450 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
bb460 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 oid *sqlite3Hash
bb470 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 20 Find(const Hash
bb480 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 *pH, const void
bb490 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 *pKey, int nKey)
bb4a0 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c {. HashElem *el
bb4b0 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c em; /* The el
bb4c0 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68 ement that match
bb4d0 65 73 20 6b 65 79 20 2a 2f 0a 20 20 65 6c 65 6d es key */. elem
bb4e0 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 = sqlite3HashFi
bb4f0 6e 64 45 6c 65 6d 28 70 48 2c 20 70 4b 65 79 2c ndElem(pH, pKey,
bb500 20 6e 4b 65 79 29 3b 0a 20 20 72 65 74 75 72 6e nKey);. return
bb510 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61 elem ? elem->da
bb520 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e ta : 0;.}../* In
bb530 73 65 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74 20 sert an element
bb540 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 into the hash ta
bb550 62 6c 65 20 70 48 2e 20 20 54 68 65 20 6b 65 79 ble pH. The key
bb560 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a is pKey,nKey.**
bb570 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69 73 and the data is
bb580 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 "data"..**.** I
bb590 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78 69 f no element exi
bb5a0 73 74 73 20 77 69 74 68 20 61 20 6d 61 74 63 68 sts with a match
bb5b0 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e 20 61 20 ing key, then a
bb5c0 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 69 new.** element i
bb5d0 73 20 63 72 65 61 74 65 64 2e 20 20 41 20 63 6f s created. A co
bb5e0 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73 py of the key is
bb5f0 20 6d 61 64 65 20 69 66 20 74 68 65 20 63 6f 70 made if the cop
bb600 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20 69 73 20 yKey.** flag is
bb610 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73 20 72 65 set. NULL is re
bb620 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
bb630 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 another element
bb640 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 already exists
bb650 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 with the same ke
bb660 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e y, then the.** n
bb670 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 ew data replaces
bb680 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e the old data an
bb690 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 d the old data i
bb6a0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 s returned..** T
bb6b0 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f he key is not co
bb6c0 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 pied in this ins
bb6d0 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c tance. If a mal
bb6e0 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a loc fails, then.
bb6f0 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 ** the new data
bb700 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
bb710 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 the hash table i
bb720 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a s unchanged..**.
bb730 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22 ** If the "data"
bb740 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
bb750 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e is function is N
bb760 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a ULL, then the.**
bb770 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 element corresp
bb780 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 onding to "key"
bb790 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 is removed from
bb7a0 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a the hash table..
bb7b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
bb7c0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 E void *sqlite3H
bb7d0 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a ashInsert(Hash *
bb7e0 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a pH, const void *
bb7f0 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 pKey, int nKey,
bb800 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 69 void *data){. i
bb810 6e 74 20 68 72 61 77 3b 20 20 20 20 20 20 20 20 nt hraw;
bb820 20 20 20 20 20 2f 2a 20 52 61 77 20 68 61 73 68 /* Raw hash
bb830 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 value of the ke
bb840 79 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 y */. int h;
bb850 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
bb860 74 68 65 20 68 61 73 68 20 6f 66 20 74 68 65 20 the hash of the
bb870 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 key modulo hash
bb880 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 table size */.
bb890 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 HashElem *elem;
bb8a0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f /* Used to
bb8b0 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 loop thru the e
bb8c0 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 lement list */.
bb8d0 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f 65 HashElem *new_e
bb8e0 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65 6c lem; /* New el
bb8f0 65 6d 65 6e 74 20 61 64 64 65 64 20 74 6f 20 74 ement added to t
bb900 68 65 20 70 48 20 2a 2f 0a 0a 20 20 61 73 73 65 he pH */.. asse
bb910 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 68 rt( pH!=0 );. h
bb920 72 61 77 20 3d 20 73 74 72 48 61 73 68 28 70 4b raw = strHash(pK
bb930 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 69 66 28 ey, nKey);. if(
bb940 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 pH->htsize ){.
bb950 20 20 20 68 20 3d 20 68 72 61 77 20 25 20 70 48 h = hraw % pH
bb960 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 65 6c ->htsize;. el
bb970 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 em = findElement
bb980 47 69 76 65 6e 48 61 73 68 28 70 48 2c 70 4b 65 GivenHash(pH,pKe
bb990 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 20 20 69 y,nKey,h);. i
bb9a0 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20 f( elem ){.
bb9b0 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61 20 void *old_data
bb9c0 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20 20 = elem->data;.
bb9d0 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 if( data==0
bb9e0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6d 6f 76 ){. remov
bb9f0 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 eElementGivenHas
bba00 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 h(pH,elem,h);.
bba10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
bba20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 elem->data =
bba30 64 61 74 61 3b 0a 20 20 20 20 20 20 20 20 69 66 data;. if
bba40 28 20 21 70 48 2d 3e 63 6f 70 79 4b 65 79 20 29 ( !pH->copyKey )
bba50 7b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 65 6d {. elem
bba60 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 20 2a ->pKey = (void *
bba70 29 70 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 7d )pKey;. }
bba80 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
bba90 6e 4b 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 79 nKey==elem->nKey
bbaa0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
bbab0 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61 return old_data
bbac0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
bbad0 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 ( data==0 ) retu
bbae0 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d rn 0;. new_elem
bbaf0 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 = (HashElem*)sq
bbb00 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a lite3Malloc( siz
bbb10 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b eof(HashElem) );
bbb20 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d . if( new_elem=
bbb30 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 =0 ) return data
bbb40 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 ;. if( pH->copy
bbb50 4b 65 79 20 26 26 20 70 4b 65 79 21 3d 30 20 29 Key && pKey!=0 )
bbb60 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e {. new_elem->
bbb70 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 pKey = sqlite3Ma
bbb80 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20 20 lloc( nKey );.
bbb90 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d 3e if( new_elem->
bbba0 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pKey==0 ){.
bbbb0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6e 65 sqlite3_free(ne
bbbc0 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 72 w_elem);. r
bbbd0 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20 eturn data;.
bbbe0 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 28 76 6f }. memcpy((vo
bbbf0 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b id*)new_elem->pK
bbc00 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b ey, pKey, nKey);
bbc10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 . }else{. ne
bbc20 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28 w_elem->pKey = (
bbc30 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d 0a void*)pKey;. }.
bbc40 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 new_elem->nKey
bbc50 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e 63 = nKey;. pH->c
bbc60 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48 ount++;. if( pH
bbc70 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 ->htsize==0 ){.
bbc80 20 20 20 72 65 68 61 73 68 28 70 48 2c 20 31 32 rehash(pH, 12
bbc90 38 2f 73 69 7a 65 6f 66 28 70 48 2d 3e 68 74 5b 8/sizeof(pH->ht[
bbca0 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 70 48 0]));. if( pH
bbcb0 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 ->htsize==0 ){.
bbcc0 20 20 20 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d pH->count =
bbcd0 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 70 48 0;. if( pH
bbce0 2d 3e 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20 ->copyKey ){.
bbcf0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
bbd00 65 28 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 e(new_elem->pKey
bbd10 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
bbd20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6e 65 sqlite3_free(ne
bbd30 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 72 w_elem);. r
bbd40 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20 eturn data;.
bbd50 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e }. }. if( pH->
bbd60 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68 74 73 69 count > pH->htsi
bbd70 7a 65 20 29 7b 0a 20 20 20 20 72 65 68 61 73 68 ze ){. rehash
bbd80 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a 65 2a 32 (pH,pH->htsize*2
bbd90 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
bbda0 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b pH->htsize>0 );
bbdb0 0a 20 20 68 20 3d 20 68 72 61 77 20 25 20 70 48 . h = hraw % pH
bbdc0 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 69 6e 73 65 ->htsize;. inse
bbdd0 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 rtElement(pH, &p
bbde0 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c H->ht[h], new_el
bbdf0 65 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d em);. new_elem-
bbe00 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 >data = data;.
bbe10 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a return 0;.}../**
bbe20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
bbe30 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a of hash.c *****
bbe40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
bbe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
bbe80 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e in file opcodes.
bbe90 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
bbea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 ***********/./*
bbec0 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 Automatically ge
bbed0 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 nerated. Do not
bbee0 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 edit */./* See
bbef0 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 2e 61 77 the mkopcodec.aw
bbf00 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 k script for det
bbf10 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20 21 64 65 ails. */.#if !de
bbf20 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
bbf30 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21 64 T_EXPLAIN) || !d
bbf40 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c efined(NDEBUG) |
bbf50 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 | defined(VDBE_P
bbf60 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e ROFILE) || defin
bbf70 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
bbf80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
bbf90 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
bbfa0 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e te3OpcodeName(in
bbfb0 74 20 69 29 7b 0a 20 73 74 61 74 69 63 20 63 6f t i){. static co
bbfc0 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 nst char *const
bbfd0 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20 22 3f 22 azName[] = { "?"
bbfe0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31 20 2a 2f ,. /* 1 */
bbff0 20 22 56 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f "VNext",. /
bc000 2a 20 20 20 32 20 2a 2f 20 22 41 66 66 69 6e 69 * 2 */ "Affini
bc010 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 33 ty",. /* 3
bc020 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22 2c 0a 20 20 */ "Column",.
bc030 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 53 65 /* 4 */ "Se
bc040 74 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f tCookie",. /
bc050 2a 20 20 20 35 20 2a 2f 20 22 53 65 71 75 65 6e * 5 */ "Sequen
bc060 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36 ce",. /* 6
bc070 20 2a 2f 20 22 4d 6f 76 65 47 74 22 2c 0a 20 20 */ "MoveGt",.
bc080 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20 22 52 6f /* 7 */ "Ro
bc090 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 wKey",. /*
bc0a0 20 38 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 20 8 */ "SCopy",.
bc0b0 20 20 20 20 2f 2a 20 20 20 39 20 2a 2f 20 22 4f /* 9 */ "O
bc0c0 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 penWrite",.
bc0d0 2f 2a 20 20 31 30 20 2a 2f 20 22 49 66 22 2c 0a /* 10 */ "If",.
bc0e0 20 20 20 20 20 2f 2a 20 20 31 31 20 2a 2f 20 22 /* 11 */ "
bc0f0 56 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a VRowid",. /*
bc100 20 20 31 32 20 2a 2f 20 22 43 6f 6c 6c 53 65 71 12 */ "CollSeq
bc110 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 33 20 2a ",. /* 13 *
bc120 2f 20 22 4f 70 65 6e 52 65 61 64 22 2c 0a 20 20 / "OpenRead",.
bc130 20 20 20 2f 2a 20 20 31 34 20 2a 2f 20 22 45 78 /* 14 */ "Ex
bc140 70 69 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 pire",. /*
bc150 31 35 20 2a 2f 20 22 41 75 74 6f 43 6f 6d 6d 69 15 */ "AutoCommi
bc160 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20 t",. /* 16
bc170 2a 2f 20 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f */ "Not",. /
bc180 2a 20 20 31 37 20 2a 2f 20 22 50 61 67 65 63 6f * 17 */ "Pageco
bc190 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 unt",. /* 1
bc1a0 38 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43 8 */ "IntegrityC
bc1b0 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39 20 k",. /* 19
bc1c0 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 20 20 */ "Sort",.
bc1d0 2f 2a 20 20 32 30 20 2a 2f 20 22 43 6f 70 79 22 /* 20 */ "Copy"
bc1e0 2c 0a 20 20 20 20 20 2f 2a 20 20 32 31 20 2a 2f ,. /* 21 */
bc1f0 20 22 54 72 61 63 65 22 2c 0a 20 20 20 20 20 2f "Trace",. /
bc200 2a 20 20 32 32 20 2a 2f 20 22 46 75 6e 63 74 69 * 22 */ "Functi
bc210 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 33 on",. /* 23
bc220 20 2a 2f 20 22 49 66 4e 65 67 22 2c 0a 20 20 20 */ "IfNeg",.
bc230 20 20 2f 2a 20 20 32 34 20 2a 2f 20 22 4e 6f 6f /* 24 */ "Noo
bc240 70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 35 20 p",. /* 25
bc250 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 20 */ "Return",.
bc260 20 20 2f 2a 20 20 32 36 20 2a 2f 20 22 4e 65 77 /* 26 */ "New
bc270 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 Rowid",. /*
bc280 20 32 37 20 2a 2f 20 22 56 61 72 69 61 62 6c 65 27 */ "Variable
bc290 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 38 20 2a ",. /* 28 *
bc2a0 2f 20 22 53 74 72 69 6e 67 22 2c 0a 20 20 20 20 / "String",.
bc2b0 20 2f 2a 20 20 32 39 20 2a 2f 20 22 52 65 61 6c /* 29 */ "Real
bc2c0 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 Affinity",.
bc2d0 2f 2a 20 20 33 30 20 2a 2f 20 22 56 52 65 6e 61 /* 30 */ "VRena
bc2e0 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 31 me",. /* 31
bc2f0 20 2a 2f 20 22 50 61 72 73 65 53 63 68 65 6d 61 */ "ParseSchema
bc300 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 32 20 2a ",. /* 32 *
bc310 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20 / "VOpen",.
bc320 2f 2a 20 20 33 33 20 2a 2f 20 22 43 6c 6f 73 65 /* 33 */ "Close
bc330 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 34 20 2a ",. /* 34 *
bc340 2f 20 22 43 72 65 61 74 65 49 6e 64 65 78 22 2c / "CreateIndex",
bc350 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 2a 2f 20 . /* 35 */
bc360 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20 "IsUnique",.
bc370 20 2f 2a 20 20 33 36 20 2a 2f 20 22 4e 6f 74 46 /* 36 */ "NotF
bc380 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ound",. /*
bc390 33 37 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20 37 */ "Int64",.
bc3a0 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f 20 22 4d /* 38 */ "M
bc3b0 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 20 ustBeInt",.
bc3c0 2f 2a 20 20 33 39 20 2a 2f 20 22 48 61 6c 74 22 /* 39 */ "Halt"
bc3d0 2c 0a 20 20 20 20 20 2f 2a 20 20 34 30 20 2a 2f ,. /* 40 */
bc3e0 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f "Rowid",. /
bc3f0 2a 20 20 34 31 20 2a 2f 20 22 49 64 78 4c 54 22 * 41 */ "IdxLT"
bc400 2c 0a 20 20 20 20 20 2f 2a 20 20 34 32 20 2a 2f ,. /* 42 */
bc410 20 22 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20 20 "AddImm",.
bc420 2f 2a 20 20 34 33 20 2a 2f 20 22 53 74 61 74 65 /* 43 */ "State
bc430 6d 65 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ment",. /*
bc440 34 34 20 2a 2f 20 22 52 6f 77 44 61 74 61 22 2c 44 */ "RowData",
bc450 0a 20 20 20 20 20 2f 2a 20 20 34 35 20 2a 2f 20 . /* 45 */
bc460 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20 20 2f "MemMax",. /
bc470 2a 20 20 34 36 20 2a 2f 20 22 4e 6f 74 45 78 69 * 46 */ "NotExi
bc480 73 74 73 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 sts",. /* 4
bc490 37 20 2a 2f 20 22 47 6f 73 75 62 22 2c 0a 20 20 7 */ "Gosub",.
bc4a0 20 20 20 2f 2a 20 20 34 38 20 2a 2f 20 22 49 6e /* 48 */ "In
bc4b0 74 65 67 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 teger",. /*
bc4c0 20 34 39 20 2a 2f 20 22 50 72 65 76 22 2c 0a 20 49 */ "Prev",.
bc4d0 20 20 20 20 2f 2a 20 20 35 30 20 2a 2f 20 22 56 /* 50 */ "V
bc4e0 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a Column",. /*
bc4f0 20 20 35 31 20 2a 2f 20 22 43 72 65 61 74 65 54 51 */ "CreateT
bc500 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 able",. /*
bc510 35 32 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 52 */ "Last",.
bc520 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22 49 6e /* 53 */ "In
bc530 63 72 56 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 crVacuum",.
bc540 2f 2a 20 20 35 34 20 2a 2f 20 22 49 64 78 52 6f /* 54 */ "IdxRo
bc550 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 wid",. /* 5
bc560 35 20 2a 2f 20 22 52 65 73 65 74 43 6f 75 6e 74 5 */ "ResetCount
bc570 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36 20 2a ",. /* 56 *
bc580 2f 20 22 46 69 66 6f 57 72 69 74 65 22 2c 0a 20 / "FifoWrite",.
bc590 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f 20 22 43 /* 57 */ "C
bc5a0 6f 6e 74 65 78 74 50 75 73 68 22 2c 0a 20 20 20 ontextPush",.
bc5b0 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 59 69 65 /* 58 */ "Yie
bc5c0 6c 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 39 ld",. /* 59
bc5d0 20 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65 72 */ "DropTrigger
bc5e0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 30 20 2a ",. /* 60 *
bc5f0 2f 20 22 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 / "Or",. /*
bc600 20 36 31 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20 61 */ "And",.
bc610 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44 72 /* 62 */ "Dr
bc620 6f 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f opIndex",. /
bc630 2a 20 20 36 33 20 2a 2f 20 22 49 64 78 47 45 22 * 63 */ "IdxGE"
bc640 2c 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f ,. /* 64 */
bc650 20 22 49 64 78 44 65 6c 65 74 65 22 2c 0a 20 20 "IdxDelete",.
bc660 20 20 20 2f 2a 20 20 36 35 20 2a 2f 20 22 49 73 /* 65 */ "Is
bc670 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Null",. /*
bc680 36 36 20 2a 2f 20 22 4e 6f 74 4e 75 6c 6c 22 2c 66 */ "NotNull",
bc690 0a 20 20 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20 . /* 67 */
bc6a0 22 4e 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 "Ne",. /* 6
bc6b0 38 20 2a 2f 20 22 45 71 22 2c 0a 20 20 20 20 20 8 */ "Eq",.
bc6c0 2f 2a 20 20 36 39 20 2a 2f 20 22 47 74 22 2c 0a /* 69 */ "Gt",.
bc6d0 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20 22 /* 70 */ "
bc6e0 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 31 Le",. /* 71
bc6f0 20 2a 2f 20 22 4c 74 22 2c 0a 20 20 20 20 20 2f */ "Lt",. /
bc700 2a 20 20 37 32 20 2a 2f 20 22 47 65 22 2c 0a 20 * 72 */ "Ge",.
bc710 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f 20 22 56 /* 73 */ "V
bc720 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 acuum",. /*
bc730 20 37 34 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c 74 */ "BitAnd",
bc740 0a 20 20 20 20 20 2f 2a 20 20 37 35 20 2a 2f 20 . /* 75 */
bc750 22 42 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a "BitOr",. /*
bc760 20 20 37 36 20 2a 2f 20 22 53 68 69 66 74 4c 65 76 */ "ShiftLe
bc770 66 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37 ft",. /* 77
bc780 20 2a 2f 20 22 53 68 69 66 74 52 69 67 68 74 22 */ "ShiftRight"
bc790 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38 20 2a 2f ,. /* 78 */
bc7a0 20 22 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 "Add",. /*
bc7b0 20 37 39 20 2a 2f 20 22 53 75 62 74 72 61 63 74 79 */ "Subtract
bc7c0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 2a ",. /* 80 *
bc7d0 2f 20 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20 / "Multiply",.
bc7e0 20 20 20 2f 2a 20 20 38 31 20 2a 2f 20 22 44 69 /* 81 */ "Di
bc7f0 76 69 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 vide",. /*
bc800 38 32 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72 82 */ "Remainder
bc810 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a ",. /* 83 *
bc820 2f 20 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20 / "Concat",.
bc830 20 2f 2a 20 20 38 34 20 2a 2f 20 22 4d 6f 76 65 /* 84 */ "Move
bc840 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 35 Le",. /* 85
bc850 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 */ "IfNot",.
bc860 20 20 2f 2a 20 20 38 36 20 2a 2f 20 22 44 72 6f /* 86 */ "Dro
bc870 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a pTable",. /*
bc880 20 20 38 37 20 2a 2f 20 22 42 69 74 4e 6f 74 22 87 */ "BitNot"
bc890 2c 0a 20 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f ,. /* 88 */
bc8a0 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 20 20 20 "String8",.
bc8b0 20 2f 2a 20 20 38 39 20 2a 2f 20 22 4d 61 6b 65 /* 89 */ "Make
bc8c0 52 65 63 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a Record",. /*
bc8d0 20 20 39 30 20 2a 2f 20 22 52 65 73 75 6c 74 52 90 */ "ResultR
bc8e0 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 31 ow",. /* 91
bc8f0 20 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20 */ "Delete",.
bc900 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 22 41 67 /* 92 */ "Ag
bc910 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a gFinal",. /*
bc920 20 20 39 33 20 2a 2f 20 22 43 6f 6d 70 61 72 65 93 */ "Compare
bc930 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 20 2a ",. /* 94 *
bc940 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20 2f / "Goto",. /
bc950 2a 20 20 39 35 20 2a 2f 20 22 54 61 62 6c 65 4c * 95 */ "TableL
bc960 6f 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 ock",. /* 9
bc970 36 20 2a 2f 20 22 46 69 66 6f 52 65 61 64 22 2c 6 */ "FifoRead",
bc980 0a 20 20 20 20 20 2f 2a 20 20 39 37 20 2a 2f 20 . /* 97 */
bc990 22 43 6c 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a "Clear",. /*
bc9a0 20 20 39 38 20 2a 2f 20 22 4d 6f 76 65 4c 74 22 98 */ "MoveLt"
bc9b0 2c 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f ,. /* 99 */
bc9c0 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c "VerifyCookie",
bc9d0 0a 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 . /* 100 */
bc9e0 22 41 67 67 53 74 65 70 22 2c 0a 20 20 20 20 20 "AggStep",.
bc9f0 2f 2a 20 31 30 31 20 2a 2f 20 22 53 65 74 4e 75 /* 101 */ "SetNu
bca00 6d 43 6f 6c 75 6d 6e 73 22 2c 0a 20 20 20 20 20 mColumns",.
bca10 2f 2a 20 31 30 32 20 2a 2f 20 22 54 72 61 6e 73 /* 102 */ "Trans
bca20 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a action",. /*
bca30 20 31 30 33 20 2a 2f 20 22 56 46 69 6c 74 65 72 103 */ "VFilter
bca40 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 34 20 2a ",. /* 104 *
bca50 2f 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 20 / "VDestroy",.
bca60 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 43 6f /* 105 */ "Co
bca70 6e 74 65 78 74 50 6f 70 22 2c 0a 20 20 20 20 20 ntextPop",.
bca80 2f 2a 20 31 30 36 20 2a 2f 20 22 4e 65 78 74 22 /* 106 */ "Next"
bca90 2c 0a 20 20 20 20 20 2f 2a 20 31 30 37 20 2a 2f ,. /* 107 */
bcaa0 20 22 49 64 78 49 6e 73 65 72 74 22 2c 0a 20 20 "IdxInsert",.
bcab0 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20 22 49 6e /* 108 */ "In
bcac0 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 sert",. /* 1
bcad0 30 39 20 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c 09 */ "Destroy",
bcae0 0a 20 20 20 20 20 2f 2a 20 31 31 30 20 2a 2f 20 . /* 110 */
bcaf0 22 52 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 "ReadCookie",.
bcb00 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 46 6f /* 111 */ "Fo
bcb10 72 63 65 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a rceInt",. /*
bcb20 20 31 31 32 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 112 */ "LoadAna
bcb30 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20 lysis",. /*
bcb40 31 31 33 20 2a 2f 20 22 45 78 70 6c 61 69 6e 22 113 */ "Explain"
bcb50 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34 20 2a 2f ,. /* 114 */
bcb60 20 22 4f 70 65 6e 50 73 65 75 64 6f 22 2c 0a 20 "OpenPseudo",.
bcb70 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f 20 22 4f /* 115 */ "O
bcb80 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 2c 0a 20 penEphemeral",.
bcb90 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 22 4e /* 116 */ "N
bcba0 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 ull",. /* 11
bcbb0 37 20 2a 2f 20 22 4d 6f 76 65 22 2c 0a 20 20 20 7 */ "Move",.
bcbc0 20 20 2f 2a 20 31 31 38 20 2a 2f 20 22 42 6c 6f /* 118 */ "Blo
bcbd0 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 39 20 b",. /* 119
bcbe0 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 20 20 */ "Rewind",.
bcbf0 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 4d 6f 76 /* 120 */ "Mov
bcc00 65 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 eGe",. /* 12
bcc10 31 20 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0a 20 1 */ "VBegin",.
bcc20 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 56 /* 122 */ "V
bcc30 55 70 64 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a Update",. /*
bcc40 20 31 32 33 20 2a 2f 20 22 49 66 5a 65 72 6f 22 123 */ "IfZero"
bcc50 2c 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f ,. /* 124 */
bcc60 20 22 56 43 72 65 61 74 65 22 2c 0a 20 20 20 20 "VCreate",.
bcc70 20 2f 2a 20 31 32 35 20 2a 2f 20 22 46 6f 75 6e /* 125 */ "Foun
bcc80 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36 20 d",. /* 126
bcc90 2a 2f 20 22 52 65 61 6c 22 2c 0a 20 20 20 20 20 */ "Real",.
bcca0 2f 2a 20 31 32 37 20 2a 2f 20 22 49 66 50 6f 73 /* 127 */ "IfPos
bccb0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 38 20 2a ",. /* 128 *
bccc0 2f 20 22 4e 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 / "NullRow",.
bccd0 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22 4a 75 6d /* 129 */ "Jum
bcce0 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 30 20 p",. /* 130
bccf0 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 6f 6e 22 */ "Permutation"
bcd00 2c 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a 2f ,. /* 131 */
bcd10 20 22 4e 6f 74 55 73 65 64 5f 31 33 31 22 2c 0a "NotUsed_131",.
bcd20 20 20 20 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 /* 132 */ "
bcd30 4e 6f 74 55 73 65 64 5f 31 33 32 22 2c 0a 20 20 NotUsed_132",.
bcd40 20 20 20 2f 2a 20 31 33 33 20 2a 2f 20 22 4e 6f /* 133 */ "No
bcd50 74 55 73 65 64 5f 31 33 33 22 2c 0a 20 20 20 20 tUsed_133",.
bcd60 20 2f 2a 20 31 33 34 20 2a 2f 20 22 4e 6f 74 55 /* 134 */ "NotU
bcd70 73 65 64 5f 31 33 34 22 2c 0a 20 20 20 20 20 2f sed_134",. /
bcd80 2a 20 31 33 35 20 2a 2f 20 22 4e 6f 74 55 73 65 * 135 */ "NotUse
bcd90 64 5f 31 33 35 22 2c 0a 20 20 20 20 20 2f 2a 20 d_135",. /*
bcda0 31 33 36 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 136 */ "NotUsed_
bcdb0 31 33 36 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 136",. /* 13
bcdc0 37 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 7 */ "NotUsed_13
bcdd0 37 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 38 20 7",. /* 138
bcde0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 38 22 */ "NotUsed_138"
bcdf0 2c 0a 20 20 20 20 20 2f 2a 20 31 33 39 20 2a 2f ,. /* 139 */
bce00 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 20 "ToText",.
bce10 2f 2a 20 31 34 30 20 2a 2f 20 22 54 6f 42 6c 6f /* 140 */ "ToBlo
bce20 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 b",. /* 141
bce30 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a */ "ToNumeric",.
bce40 20 20 20 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 /* 142 */ "
bce50 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 ToInt",. /*
bce60 31 34 33 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c 143 */ "ToReal",
bce70 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61 . };. return a
bce80 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64 zName[i];.}.#end
bce90 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if../***********
bcea0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 *** End of opcod
bceb0 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a es.c ***********
bcec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bced0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcee0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
bcef0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
bcf00 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a s_os2.c ********
bcf10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf30 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46 **/./*.** 2006 F
bcf40 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eb 14.**.** The
bcf50 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
bcf60 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
bcf70 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
bcf80 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
bcf90 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
bcfa0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
bcfb0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
bcfc0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
bcfd0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
bcfe0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
bcff0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
bd000 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
bd010 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
bd020 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
bd030 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
bd040 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
bd050 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
bd060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
bd0b0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
bd0c0 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ns code that is
bd0d0 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f 32 specific to OS/2
bd0e0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f ..**.** $Id: os_
bd0f0 6f 73 32 2e 63 2c 76 20 31 2e 35 39 20 32 30 30 os2.c,v 1.59 200
bd100 38 2f 31 31 2f 31 38 20 32 33 3a 30 33 3a 34 30 8/11/18 23:03:40
bd110 20 70 77 65 69 6c 62 61 63 68 65 72 20 45 78 70 pweilbacher Exp
bd120 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 $.*/...#if SQLI
bd130 54 45 5f 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a TE_OS_OS2../*.**
bd140 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 A Note About Me
bd150 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a mory Allocation:
bd160 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 .**.** This driv
bd170 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 er uses malloc()
bd180 2f 66 72 65 65 28 29 20 64 69 72 65 63 74 6c 79 /free() directly
bd190 20 72 61 74 68 65 72 20 74 68 61 6e 20 67 6f 69 rather than goi
bd1a0 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 ng through.** th
bd1b0 65 20 53 51 4c 69 74 65 2d 77 72 61 70 70 65 72 e SQLite-wrapper
bd1c0 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 s sqlite3_malloc
bd1d0 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 ()/sqlite3_free(
bd1e0 29 2e 20 20 54 68 6f 73 65 20 77 72 61 70 70 65 ). Those wrappe
bd1f0 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 69 67 6e rs.** are design
bd200 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d ed for use on em
bd210 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 20 77 bedded systems w
bd220 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 here memory is s
bd230 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c carce and.** mal
bd240 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 68 61 70 loc failures hap
bd250 70 65 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 pen frequently.
bd260 20 4f 53 2f 32 20 64 6f 65 73 20 6e 6f 74 20 74 OS/2 does not t
bd270 79 70 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a ypically run on.
bd280 2a 2a 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 ** embedded syst
bd290 65 6d 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 ems, and when it
bd2a0 20 64 6f 65 73 20 74 68 65 20 64 65 76 65 6c 6f does the develo
bd2b0 70 65 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 pers normally ha
bd2c0 76 65 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f ve bigger.** pro
bd2d0 62 6c 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 blems to worry a
bd2e0 62 6f 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e bout than runnin
bd2f0 67 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e g out of memory.
bd300 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f So there is no
bd310 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e t.** a compellin
bd320 67 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 g need to use th
bd330 65 20 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a e wrappers..**.*
bd340 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61 * But there is a
bd350 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 good reason to
bd360 6e 6f 74 20 75 73 65 20 74 68 65 20 77 72 61 70 not use the wrap
bd370 70 65 72 73 2e 20 20 49 66 20 77 65 20 75 73 65 pers. If we use
bd380 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 the.** wrappers
bd390 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 then we will ge
bd3a0 74 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c t simulated mall
bd3b0 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20 77 69 oc() failures wi
bd3c0 74 68 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 thin this.** dri
bd3d0 76 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63 ver. And that c
bd3e0 61 75 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 auses all kinds
bd3f0 6f 66 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 of problems for
bd400 6f 75 72 20 74 65 73 74 73 2e 20 20 57 65 0a 2a our tests. We.*
bd410 2a 20 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 * could enhance
bd420 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 77 SQLite to deal w
bd430 69 74 68 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 ith simulated ma
bd440 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 77 69 lloc failures wi
bd450 74 68 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 thin.** the OS d
bd460 72 69 76 65 72 2c 20 62 75 74 20 74 68 65 20 63 river, but the c
bd470 6f 64 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 ode to deal with
bd480 20 74 68 6f 73 65 20 66 61 69 6c 75 72 65 20 77 those failure w
bd490 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 ould not.** be e
bd4a0 78 65 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 xercised on Linu
bd4b0 78 20 28 77 68 69 63 68 20 64 6f 65 73 20 6e 6f x (which does no
bd4c0 74 20 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 t need to malloc
bd4d0 28 29 20 69 6e 20 74 68 65 20 64 72 69 76 65 72 () in the driver
bd4e0 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 ).** and so we w
bd4f0 6f 75 6c 64 20 68 61 76 65 20 64 69 66 66 69 63 ould have diffic
bd500 75 6c 74 79 20 77 72 69 74 69 6e 67 20 63 6f 76 ulty writing cov
bd510 65 72 61 67 65 20 74 65 73 74 73 20 66 6f 72 20 erage tests for
bd520 74 68 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 that.** code. B
bd530 65 74 74 65 72 20 74 6f 20 6c 65 61 76 65 20 74 etter to leave t
bd540 68 65 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 he code out, we
bd550 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 think..**.** The
bd560 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 64 point of this d
bd570 69 73 63 75 73 73 69 6f 6e 20 69 73 20 61 73 20 iscussion is as
bd580 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 follows: When c
bd590 72 65 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a reating a new.**
bd5a0 20 4f 53 20 6c 61 79 65 72 20 66 6f 72 20 61 6e OS layer for an
bd5b0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
bd5c0 2c 20 69 66 20 79 6f 75 20 75 73 65 20 74 68 69 , if you use thi
bd5d0 73 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61 s file as an exa
bd5e0 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 mple,.** avoid t
bd5f0 68 65 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 he use of malloc
bd600 28 29 2f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 ()/free(). Thos
bd610 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 e routines work
bd620 6f 6b 20 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 65 ok on OS/2.** de
bd630 73 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 sktops but not s
bd640 6f 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 o well in embedd
bd650 65 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a ed systems..*/..
bd660 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 /*.** Macros use
bd670 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 d to determine w
bd680 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f hether or not to
bd690 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f use threads..*/
bd6a0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
bd6b0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 20 ITE_THREADSAFE)
bd6c0 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 && SQLITE_THREAD
bd6d0 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SAFE.# define SQ
bd6e0 4c 49 54 45 5f 4f 53 32 5f 54 48 52 45 41 44 53 LITE_OS2_THREADS
bd6f0 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 1.#endif../*.**
bd700 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 Include code th
bd710 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 at is common to
bd720 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 all os_*.c files
bd730 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
bd740 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 *** Include os_c
bd750 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d ommon.h in the m
bd760 69 64 64 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 2e iddle of os_os2.
bd770 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
bd780 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
bd790 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
bd7a0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a s_common.h *****
bd7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd7d0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d **/./*.** 2004 M
bd7e0 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 22.**.** The
bd7f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
bd800 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
bd810 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
bd820 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
bd830 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
bd840 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
bd850 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
bd860 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
bd870 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
bd880 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
bd890 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
bd8a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
bd8b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
bd8c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
bd8d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
bd8e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
bd8f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
bd900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd940 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
bd950 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
bd960 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 ns macros and a
bd970 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f little bit of co
bd980 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f de that is commo
bd990 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 n to.** all of t
bd9a0 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 he platform-spec
bd9b0 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a ific files (os_*
bd9c0 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c .c) and is #incl
bd9d0 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a uded into those.
bd9e0 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 ** files..**.**
bd9f0 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 This file should
bda00 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 be #included by
bda10 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 the os_*.c file
bda20 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e s only. It is n
bda30 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 ot a.** general
bda40 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 purpose header f
bda50 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ile..**.** $Id:
bda60 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e os_common.h,v 1.
bda70 33 37 20 32 30 30 38 2f 30 35 2f 32 39 20 32 30 37 2008/05/29 20
bda80 3a 32 32 3a 33 37 20 73 68 61 6e 65 20 45 78 70 :22:37 shane Exp
bda90 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f $.*/.#ifndef _O
bdaa0 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 S_COMMON_H_.#def
bdab0 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ine _OS_COMMON_H
bdac0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 _../*.** At leas
bdad0 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 t two bugs have
bdae0 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 slipped in becau
bdaf0 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 se we changed th
bdb00 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a e MEMORY_DEBUG.*
bdb10 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 * macro to SQLIT
bdb20 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 E_DEBUG and some
bdb30 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 older makefiles
bdb40 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 have not yet ma
bdb50 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 de the.** switch
bdb60 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
bdb70 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 code should cat
bdb80 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 ch this problem
bdb90 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e at compile-time.
bdba0 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 .*/.#ifdef MEMOR
bdbb0 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 Y_DEBUG.# error
bdbc0 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 "The MEMORY_DEBU
bdbd0 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c G macro is obsol
bdbe0 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 ete. Use SQLITE
bdbf0 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 _DEBUG instead."
bdc00 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20 .#endif.../*. *
bdc10 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 74 68 When testing, th
bdc20 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 is global variab
bdc30 6c 65 20 73 74 6f 72 65 73 20 74 68 65 20 6c 6f le stores the lo
bdc40 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a cation of the. *
bdc50 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e pending-byte in
bdc60 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
bdc70 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53 le.. */.#ifdef S
bdc80 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
bdc90 45 5f 41 50 49 20 75 6e 73 69 67 6e 65 64 20 69 E_API unsigned i
bdca0 6e 74 20 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 nt sqlite3_pendi
bdcb0 6e 67 5f 62 79 74 65 20 3d 20 30 78 34 30 30 30 ng_byte = 0x4000
bdcc0 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 0000;.#endif..#i
bdcd0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
bdce0 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
bdcf0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 int sqlite3OSTr
bdd00 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 ace = 0;.#define
bdd10 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 OSTRACE1(X)
bdd20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
bdd30 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
bdd40 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 3DebugPrintf(X).
bdd50 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 #define OSTRACE2
bdd60 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 (X,Y) if(
bdd70 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
bdd80 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
bdd90 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 ntf(X,Y).#define
bdda0 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 OSTRACE3(X,Y,Z)
bddb0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
bddc0 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
bddd0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
bdde0 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Z).#define OSTR
bddf0 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 ACE4(X,Y,Z,A)
bde00 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
bde10 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
bde20 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 gPrintf(X,Y,Z,A)
bde30 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
bde40 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 5(X,Y,Z,A,B) if(
bde50 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 sqlite3OSTrace
bde60 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
bde70 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a intf(X,Y,Z,A,B).
bde80 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 #define OSTRACE6
bde90 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a (X,Y,Z,A,B,C) \.
bdea0 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 if(sqlite3OS
bdeb0 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 Trace) sqlite3De
bdec0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
bded0 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f A,B,C).#define O
bdee0 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c STRACE7(X,Y,Z,A,
bdef0 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 B,C,D) \. if(
bdf00 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 sqlite3OSTrace)
bdf10 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
bdf20 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 tf(X,Y,Z,A,B,C,D
bdf30 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 ).#else.#define
bdf40 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 OSTRACE1(X).#def
bdf50 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 ine OSTRACE2(X,Y
bdf60 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
bdf70 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e E3(X,Y,Z).#defin
bdf80 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a e OSTRACE4(X,Y,Z
bdf90 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,A).#define OSTR
bdfa0 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a ACE5(X,Y,Z,A,B).
bdfb0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 #define OSTRACE6
bdfc0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 (X,Y,Z,A,B,C).#d
bdfd0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 efine OSTRACE7(X
bdfe0 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 ,Y,Z,A,B,C,D).#e
bdff0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 ndif../*.** Macr
be000 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e os for performan
be010 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 ce tracing. Nor
be020 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 mally turned off
be030 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a . Only works.**
be040 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 on i486 hardwar
be050 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
be060 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f ITE_PERFORMANCE_
be070 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 TRACE../* .** hw
be080 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 time.h contains
be090 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 inline assembler
be0a0 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d code for implem
be0b0 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d enting .** high-
be0c0 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 performance timi
be0d0 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a ng routines..*/.
be0e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
be0f0 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 Include hwtime.h
be100 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
be110 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a f os_common.h **
be120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
be130 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
be140 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d Begin file hwtim
be150 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
be160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
be180 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 /*.** 2008 May 2
be190 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 7.**.** The auth
be1a0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
be1b0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
be1c0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
be1d0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
be1e0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
be1f0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
be200 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
be210 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
be220 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
be230 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
be240 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
be250 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
be260 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
be270 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
be280 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
be290 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
be2a0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
be2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be2f0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
be300 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 file contains i
be310 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 nline asm code f
be320 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 or retrieving "h
be330 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 igh-performance"
be340 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 .** counters for
be350 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e x86 class CPUs.
be360 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 .**.** $Id: hwti
be370 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f me.h,v 1.3 2008/
be380 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 08/01 14:33:15 s
be390 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 hane Exp $.*/.#i
be3a0 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f fndef _HWTIME_H_
be3b0 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 .#define _HWTIME
be3c0 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 _H_../*.** The f
be3d0 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
be3e0 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 only works on p
be3f0 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 entium-class (or
be400 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f newer) processo
be410 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 rs..** It uses t
be420 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 he RDTSC opcode
be430 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c to read the cycl
be440 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 e count value ou
be450 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 t of the.** proc
be460 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e essor and return
be470 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 s that value. T
be480 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 his can be used
be490 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 for high-res.**
be4a0 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 profiling..*/.#i
be4b0 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 f (defined(__GNU
be4c0 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 C__) || defined(
be4d0 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a _MSC_VER)) && \.
be4e0 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 (defined(i
be4f0 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 386) || defined(
be500 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 __i386__) || def
be510 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a ined(_M_IX86))..
be520 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f #if defined(__
be530 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c GNUC__).. __inl
be540 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
be550 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
be560 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e e(void){. un
be570 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 signed int lo, h
be580 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 i;. __asm__
be590 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 __volatile__ ("r
be5a0 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f dtsc" : "=a" (lo
be5b0 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 ), "=d" (hi));.
be5c0 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 return (sqli
be5d0 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 te_uint64)hi <<
be5e0 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 32 | lo;. }..
be5f0 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d #elif defined(_M
be600 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 SC_VER).. __dec
be610 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 lspec(naked) __i
be620 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e nline sqlite_uin
be630 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 t64 __cdecl sqli
be640 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
be650 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 . __asm {.
be660 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 rdtsc.
be670 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 ret ;
be680 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 return value at
be690 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 EDX:EAX. }.
be6a0 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 }.. #endif..#e
be6b0 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 lif (defined(__G
be6c0 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 NUC__) && define
be6d0 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a d(__x86_64__))..
be6e0 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
be6f0 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
be700 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
be710 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c unsigned l
be720 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f ong val;. _
be730 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
be740 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 e__ ("rdtsc" : "
be750 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 =A" (val));.
be760 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 return val;.
be770 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e }. .#elif (defin
be780 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
be790 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 defined(__ppc__)
be7a0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 ).. __inline__
be7b0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
be7c0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
be7d0 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 ){. unsigne
be7e0 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 d long long retv
be7f0 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e al;. unsign
be800 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 ed long junk;.
be810 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f __asm__ __vo
be820 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 latile__ ("\n\.
be830 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 1:
be840 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 mftbu %1\n\.
be850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be860 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a mftb %L0\n\.
be870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be880 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a mftbu %0\n\.
be890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be8a0 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c cmpw %0,%1\
be8b0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
be8c0 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 bne 1b"
be8d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
be8e0 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 : "=r" (retva
be8f0 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 l), "=r" (junk))
be900 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
be910 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 etval;. }..#els
be920 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 e.. #error Need
be930 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
be940 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 of sqlite3Hwtime
be950 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
be960 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a form... /*. **
be970 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 To compile with
be980 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 out implementing
be990 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
be9a0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f for your platfo
be9b0 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e rm,. ** you can
be9c0 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 remove the abov
be9d0 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 e #error and use
be9e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 the following.
be9f0 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f ** stub functio
bea00 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 n. You will los
bea10 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 e timing support
bea20 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f for many. ** o
bea30 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 f the debugging
bea40 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c and testing util
bea50 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 ities, but it sh
bea60 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 ould at. ** lea
bea70 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 st compile and r
bea80 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f un.. */.SQLITE_
bea90 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 PRIVATE sqlite
beaa0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
beab0 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 wtime(void){ ret
beac0 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e urn ((sqlite_uin
bead0 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 t64)0); }..#endi
beae0 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
beaf0 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f fined(_HWTIME_H_
beb00 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
beb10 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 ***** End of hwt
beb20 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ime.h **********
beb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
beb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
beb50 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
beb60 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
beb70 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
beb80 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e ff in os_common.
beb90 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
beba0 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 ****/..static sq
bebb0 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 lite_uint64 g_st
bebc0 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 art;.static sqli
bebd0 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 te_uint64 g_elap
bebe0 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d sed;.#define TIM
bebf0 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 ER_START g
bec00 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 _start=sqlite3Hw
bec10 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 time().#define T
bec20 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 IMER_END
bec30 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 g_elapsed=sqlit
bec40 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 e3Hwtime()-g_sta
bec50 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 rt.#define TIMER
bec60 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 _ELAPSED g_e
bec70 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 lapsed.#else.#de
bec80 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 fine TIMER_START
bec90 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
beca0 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 ND.#define TIMER
becb0 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 _ELAPSED ((s
becc0 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a qlite_uint64)0).
becd0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 #endif../*.** If
bece0 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 we compile with
becf0 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 the SQLITE_TEST
bed00 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e macro set, then
bed10 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 the following b
bed20 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 lock.** of code
bed30 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 will give us the
bed40 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 ability to simu
bed50 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 late a disk I/O
bed60 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 error. This.**
bed70 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 is used for test
bed80 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f ing the I/O reco
bed90 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 very logic..*/.#
beda0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
bedb0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
bedc0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
bedd0 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 r_hit = 0;
bede0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
bedf0 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 umber of I/O Err
bee00 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ors */.SQLITE_AP
bee10 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
bee20 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d _error_hardhit =
bee30 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 0; /* Nu
bee40 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 mber of non-beni
bee50 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c gn errors */.SQL
bee60 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
bee70 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
bee80 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 ding = 0;
bee90 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 /* Count down t
beea0 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f o first I/O erro
beeb0 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 r */.SQLITE_API
beec0 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
beed0 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 rror_persist = 0
beee0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ; /* True
beef0 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 if I/O errors p
bef00 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 ersist */.SQLITE
bef10 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
bef20 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e _io_error_benign
bef30 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
bef40 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 True if errors
bef50 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 are benign */.SQ
bef60 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
bef70 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
bef80 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 nding = 0;.SQLIT
bef90 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
befa0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 3_diskfull = 0;.
befb0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
befc0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 IOErrorBenign(X)
befd0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
befe0 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 r_benign=(X).#de
beff0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
bf000 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 rror(CODE) \.
bf010 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f if( (sqlite3_io_
bf020 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 error_persist &&
bf030 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
bf040 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 r_hit) \.
bf050 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 || sqlite3_io_er
bf060 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d ror_pending-- ==
bf070 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 1 ) \.
bf080 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f { local_io
bf090 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 err(); CODE; }.s
bf0a0 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c tatic void local
bf0b0 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 _ioerr(){. IOTR
bf0c0 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 ACE(("IOERR\n"))
bf0d0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 ;. sqlite3_io_e
bf0e0 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 rror_hit++;. if
bf0f0 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 ( !sqlite3_io_er
bf100 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c ror_benign ) sql
bf110 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 ite3_io_error_ha
bf120 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 rdhit++;.}.#defi
bf130 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 ne SimulateDiskf
bf140 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c ullError(CODE) \
bf150 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f . if( sqlite3_
bf160 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
bf170 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 ){ \. if( s
bf180 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
bf190 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 pending == 1 ){
bf1a0 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 \. local_i
bf1b0 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 oerr(); \.
bf1c0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
bf1d0 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 l = 1; \.
bf1e0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
bf1f0 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 _hit = 1; \.
bf200 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 CODE; \.
bf210 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 }else{ \.
bf220 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
bf230 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 _pending--; \.
bf240 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 } \. }.#els
bf250 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 e.#define Simula
bf260 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
bf270 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c X).#define Simul
bf280 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 ateIOError(A).#d
bf290 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 efine SimulateDi
bf2a0 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 skfullError(A).#
bf2b0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 endif../*.** Whe
bf2c0 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 n testing, keep
bf2d0 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e a count of the n
bf2e0 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 umber of open fi
bf2f0 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 les..*/.#ifdef S
bf300 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
bf310 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
bf320 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 3_open_file_coun
bf330 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f t = 0;.#define O
bf340 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 penCounter(X) s
bf350 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 qlite3_open_file
bf360 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 _count+=(X).#els
bf370 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f e.#define OpenCo
bf380 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a unter(X).#endif.
bf390 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
bf3a0 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ned(_OS_COMMON_H
bf3b0 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a _) */../********
bf3c0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 ****** End of os
bf3d0 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a _common.h ******
bf3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf400 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
bf410 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
bf420 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
bf430 6f 66 66 20 69 6e 20 6f 73 5f 6f 73 32 2e 63 20 off in os_os2.c
bf440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf450 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 *****/../*.** Th
bf460 65 20 6f 73 32 46 69 6c 65 20 73 74 72 75 63 74 e os2File struct
bf470 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 ure is subclass
bf480 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 of sqlite3_file
bf490 73 70 65 63 69 66 69 63 20 66 6f 72 20 74 68 65 specific for the
bf4a0 20 4f 53 2f 32 0a 2a 2a 20 70 72 6f 74 61 62 69 OS/2.** protabi
bf4b0 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 lity layer..*/.t
bf4c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 6f 73 ypedef struct os
bf4d0 32 46 69 6c 65 20 6f 73 32 46 69 6c 65 3b 0a 73 2File os2File;.s
bf4e0 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 7b 0a truct os2File {.
bf4f0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
bf500 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 io_methods *pMet
bf510 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 hod; /* Always
bf520 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 the first entry
bf530 2a 2f 0a 20 20 48 46 49 4c 45 20 68 3b 20 20 20 */. HFILE h;
bf540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
bf550 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 * Handle for acc
bf560 65 73 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 essing the file
bf570 2a 2f 0a 20 20 63 68 61 72 2a 20 70 61 74 68 54 */. char* pathT
bf580 6f 44 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f oDel; /
bf590 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 * Name of file t
bf5a0 6f 20 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 o delete on clos
bf5b0 65 2c 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 2a e, NULL if not *
bf5c0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
bf5d0 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a r locktype; /*
bf5e0 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 Type of lock cu
bf5f0 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 rrently held on
bf600 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a this file */.};.
bf610 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 5f 54 49 .#define LOCK_TI
bf620 4d 45 4f 55 54 20 31 30 4c 20 2f 2a 20 74 68 65 MEOUT 10L /* the
bf630 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 default locking
bf640 20 74 69 6d 65 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a timeout */../**
bf650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
bf6a0 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 he next group of
bf6b0 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d routines implem
bf6c0 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 ent the I/O meth
bf6d0 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a ods specified.**
bf6e0 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f by the sqlite3_
bf6f0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 io_methods objec
bf700 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t..*************
bf710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bf750 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 */../*.** Close
bf760 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
bf770 63 20 69 6e 74 20 6f 73 32 43 6c 6f 73 65 28 20 c int os2Close(
bf780 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
bf790 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 ){. APIRET rc
bf7a0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 = NO_ERROR;. os
bf7b0 32 46 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 2File *pFile;.
bf7c0 69 66 28 20 69 64 20 26 26 20 28 70 46 69 6c 65 if( id && (pFile
bf7d0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 = (os2File*)id)
bf7e0 20 21 3d 20 30 20 29 7b 0a 20 20 20 20 4f 53 54 != 0 ){. OST
bf7f0 52 41 43 45 32 28 20 22 43 4c 4f 53 45 20 25 64 RACE2( "CLOSE %d
bf800 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b \n", pFile->h );
bf810 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 43 6c 6f . rc = DosClo
bf820 73 65 28 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a se( pFile->h );.
bf830 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
bf840 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 ype = NO_LOCK;.
bf850 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 61 if( pFile->pa
bf860 74 68 54 6f 44 65 6c 20 21 3d 20 4e 55 4c 4c 20 thToDel != NULL
bf870 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 44 6f ){. rc = Do
bf880 73 46 6f 72 63 65 44 65 6c 65 74 65 28 20 28 50 sForceDelete( (P
bf890 53 5a 29 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f SZ)pFile->pathTo
bf8a0 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 66 72 65 Del );. fre
bf8b0 65 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f e( pFile->pathTo
bf8c0 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 46 69 Del );. pFi
bf8d0 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 le->pathToDel =
bf8e0 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 NULL;. }.
bf8f0 69 64 20 3d 20 30 3b 0a 20 20 20 20 4f 70 65 6e id = 0;. Open
bf900 43 6f 75 6e 74 65 72 28 20 2d 31 20 29 3b 0a 20 Counter( -1 );.
bf910 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 20 }.. return rc
bf920 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 == NO_ERROR ? SQ
bf930 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
bf940 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _IOERR;.}../*.**
bf950 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 Read data from
bf960 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 a file into a bu
bf970 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 ffer. Return SQ
bf980 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a LITE_OK if all.*
bf990 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 * bytes were rea
bf9a0 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 d successfully a
bf9b0 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 nd SQLITE_IOERR
bf9c0 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 if anything goes
bf9d0 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 .** wrong..*/.st
bf9e0 61 74 69 63 20 69 6e 74 20 6f 73 32 52 65 61 64 atic int os2Read
bf9f0 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
bfa00 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 *id,
bfa10 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72 /* File to r
bfa20 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f ead from */. vo
bfa30 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 id *pBuf,
bfa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bfa50 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 Write content i
bfa60 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 nto this buffer
bfa70 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 */. int amt,
bfa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bfa90 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
bfaa0 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 f bytes to read
bfab0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 */. sqlite3_int
bfac0 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 64 offset
bfad0 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 /* Begin re
bfae0 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 ading at this of
bfaf0 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f fset */.){. ULO
bfb00 4e 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 NG fileLocation
bfb10 3d 20 30 4c 3b 0a 20 20 55 4c 4f 4e 47 20 67 6f = 0L;. ULONG go
bfb20 74 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 t;. os2File *pF
bfb30 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 ile = (os2File*)
bfb40 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 id;. assert( id
bfb50 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 !=0 );. Simulat
bfb60 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e eIOError( return
bfb70 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 SQLITE_IOERR_RE
bfb80 41 44 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 AD );. OSTRACE3
bfb90 28 20 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d ( "READ %d lock=
bfba0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
bfbb0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
bfbc0 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 65 74 );. if( DosSet
bfbd0 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e 68 FilePtr(pFile->h
bfbe0 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f 42 , offset, FILE_B
bfbf0 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74 EGIN, &fileLocat
bfc00 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 ion) != NO_ERROR
bfc10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
bfc20 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d QLITE_IOERR;. }
bfc30 0a 20 20 69 66 28 20 44 6f 73 52 65 61 64 28 20 . if( DosRead(
bfc40 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 pFile->h, pBuf,
bfc50 61 6d 74 2c 20 26 67 6f 74 20 29 20 21 3d 20 4e amt, &got ) != N
bfc60 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 O_ERROR ){. r
bfc70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
bfc80 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 RR_READ;. }. i
bfc90 66 28 20 67 6f 74 20 3d 3d 20 28 55 4c 4f 4e 47 f( got == (ULONG
bfca0 29 61 6d 74 20 29 0a 20 20 20 20 72 65 74 75 72 )amt ). retur
bfcb0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 65 n SQLITE_OK;. e
bfcc0 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 lse {. /* Unr
bfcd0 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 ead portions of
bfce0 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 the input buffer
bfcf0 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 must be zero-fi
bfd00 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 lled */. mems
bfd10 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 et(&((char*)pBuf
bfd20 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 )[got], 0, amt-g
bfd30 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ot);. return
bfd40 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
bfd50 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a RT_READ;. }.}..
bfd60 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 /*.** Write data
bfd70 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 from a buffer i
bfd80 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 nto a file. Ret
bfd90 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e urn SQLITE_OK on
bfda0 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 success.** or s
bfdb0 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 ome other error
bfdc0 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e code on failure.
bfdd0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
bfde0 73 32 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 s2Write(. sqlit
bfdf0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 e3_file *id,
bfe00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
bfe10 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f le to write into
bfe20 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
bfe30 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 *pBuf,
bfe40 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 79 74 /* The byt
bfe50 65 73 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e es to be written
bfe60 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 */. int amt,
bfe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bfe80 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
bfe90 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 of bytes to writ
bfea0 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 e */. sqlite3_i
bfeb0 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 nt64 offset
bfec0 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
bfed0 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 74 into the file t
bfee0 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20 o begin writing
bfef0 61 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 at */.){. ULONG
bff00 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 fileLocation =
bff10 30 4c 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 0L;. APIRET rc
bff20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c = NO_ERROR;. UL
bff30 4f 4e 47 20 77 72 6f 74 65 3b 0a 20 20 6f 73 32 ONG wrote;. os2
bff40 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f File *pFile = (o
bff50 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 s2File*)id;. as
bff60 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
bff70 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
bff80 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
bff90 49 4f 45 52 52 5f 57 52 49 54 45 20 29 3b 0a 20 IOERR_WRITE );.
bffa0 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
bffb0 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 lError( return S
bffc0 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 20 20 QLITE_FULL );.
bffd0 4f 53 54 52 41 43 45 33 28 20 22 57 52 49 54 45 OSTRACE3( "WRITE
bffe0 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 %d lock=%d\n",
bfff0 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d pFile->h, pFile-
c0000 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 >locktype );. i
c0010 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72 f( DosSetFilePtr
c0020 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 (pFile->h, offse
c0030 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 t, FILE_BEGIN, &
c0040 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d fileLocation) !=
c0050 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
c0060 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
c0070 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 61 73 73 65 OERR;. }. asse
c0080 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 rt( amt>0 );. w
c0090 68 69 6c 65 28 20 61 6d 74 20 3e 20 30 20 26 26 hile( amt > 0 &&
c00a0 0a 20 20 20 20 20 20 20 20 20 28 20 72 63 20 3d . ( rc =
c00b0 20 44 6f 73 57 72 69 74 65 28 20 70 46 69 6c 65 DosWrite( pFile
c00c0 2d 3e 68 2c 20 28 50 56 4f 49 44 29 70 42 75 66 ->h, (PVOID)pBuf
c00d0 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 20 29 20 , amt, &wrote )
c00e0 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 26 26 ) == NO_ERROR &&
c00f0 0a 20 20 20 20 20 20 20 20 20 77 72 6f 74 65 20 . wrote
c0100 3e 20 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 > 0. ){. amt
c0110 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 -= wrote;. p
c0120 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 Buf = &((char*)p
c0130 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d Buf)[wrote];. }
c0140 0a 0a 20 20 72 65 74 75 72 6e 20 28 20 72 63 20 .. return ( rc
c0150 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c 20 61 != NO_ERROR || a
c0160 6d 74 20 3e 20 28 69 6e 74 29 77 72 6f 74 65 20 mt > (int)wrote
c0170 29 20 3f 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 ) ? SQLITE_FULL
c0180 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a : SQLITE_OK;.}..
c0190 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 /*.** Truncate a
c01a0 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 n open file to a
c01b0 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 0a specified size.
c01c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c01d0 32 54 72 75 6e 63 61 74 65 28 20 73 71 6c 69 74 2Truncate( sqlit
c01e0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 e3_file *id, i64
c01f0 20 6e 42 79 74 65 20 29 7b 0a 20 20 41 50 49 52 nByte ){. APIR
c0200 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 ET rc = NO_ERROR
c0210 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 ;. os2File *pFi
c0220 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 le = (os2File*)i
c0230 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 d;. OSTRACE3( "
c0240 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 TRUNCATE %d %lld
c0250 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e \n", pFile->h, n
c0260 42 79 74 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 Byte );. Simula
c0270 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
c0280 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 n SQLITE_IOERR_T
c0290 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20 RUNCATE );. rc
c02a0 3d 20 44 6f 73 53 65 74 46 69 6c 65 53 69 7a 65 = DosSetFileSize
c02b0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 ( pFile->h, nByt
c02c0 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 e );. return rc
c02d0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 == NO_ERROR ? S
c02e0 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 QLITE_OK : SQLIT
c02f0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 E_IOERR_TRUNCATE
c0300 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
c0310 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f TE_TEST./*.** Co
c0320 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f unt the number o
c0330 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 f fullsyncs and
c0340 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 normal syncs. T
c0350 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 his is used to t
c0360 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 est.** that sync
c0370 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 s and fullsyncs
c0380 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74 20 are occuring at
c0390 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e the right times.
c03a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
c03b0 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f nt sqlite3_sync_
c03c0 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 count = 0;.SQLIT
c03d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c03e0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 3_fullsync_count
c03f0 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a = 0;.#endif../*
c0400 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c .** Make sure al
c0410 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 61 l writes to a pa
c0420 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72 rticular file ar
c0430 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 e committed to d
c0440 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 isk..*/.static i
c0450 6e 74 20 6f 73 32 53 79 6e 63 28 20 73 71 6c 69 nt os2Sync( sqli
c0460 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
c0470 74 20 66 6c 61 67 73 20 29 7b 0a 20 20 6f 73 32 t flags ){. os2
c0480 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f File *pFile = (o
c0490 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 s2File*)id;. OS
c04a0 54 52 41 43 45 33 28 20 22 53 59 4e 43 20 25 64 TRACE3( "SYNC %d
c04b0 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 lock=%d\n", pFi
c04c0 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f le->h, pFile->lo
c04d0 63 6b 74 79 70 65 20 29 3b 0a 23 69 66 64 65 66 cktype );.#ifdef
c04e0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 SQLITE_TEST. i
c04f0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
c0500 45 5f 53 59 4e 43 5f 46 55 4c 4c 29 7b 0a 20 20 E_SYNC_FULL){.
c0510 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 sqlite3_fullsy
c0520 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a nc_count++;. }.
c0530 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 sqlite3_sync_c
c0540 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 ount++;.#endif.
c0550 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c /* If we compil
c0560 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 ed with the SQLI
c0570 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c TE_NO_SYNC flag,
c0580 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73 then syncing is
c0590 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 a. ** no-op.
c05a0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
c05b0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 65 74 75 72 _NO_SYNC. retur
c05c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c n SQLITE_OK;.#el
c05d0 73 65 0a 20 20 72 65 74 75 72 6e 20 44 6f 73 52 se. return DosR
c05e0 65 73 65 74 42 75 66 66 65 72 28 20 70 46 69 6c esetBuffer( pFil
c05f0 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 e->h ) == NO_ERR
c0600 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a OR ? SQLITE_OK :
c0610 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 23 SQLITE_IOERR;.#
c0620 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 endif.}../*.** D
c0630 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75 72 etermine the cur
c0640 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 66 rent size of a f
c0650 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a ile in bytes.*/.
c0660 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 69 static int os2Fi
c0670 6c 65 53 69 7a 65 28 20 73 71 6c 69 74 65 33 5f leSize( sqlite3_
c0680 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 file *id, sqlite
c0690 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 20 29 3_int64 *pSize )
c06a0 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 {. APIRET rc =
c06b0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49 4c 45 NO_ERROR;. FILE
c06c0 53 54 41 54 55 53 33 20 66 73 74 73 33 46 69 6c STATUS3 fsts3Fil
c06d0 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 73 65 74 28 eInfo;. memset(
c06e0 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 &fsts3FileInfo,
c06f0 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33 46 0, sizeof(fsts3F
c0700 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 20 61 73 73 ileInfo));. ass
c0710 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
c0720 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
c0730 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
c0740 4f 45 52 52 5f 46 53 54 41 54 20 29 3b 0a 20 20 OERR_FSTAT );.
c0750 72 63 20 3d 20 44 6f 73 51 75 65 72 79 46 69 6c rc = DosQueryFil
c0760 65 49 6e 66 6f 28 20 28 28 6f 73 32 46 69 6c 65 eInfo( ((os2File
c0770 2a 29 69 64 29 2d 3e 68 2c 20 46 49 4c 5f 53 54 *)id)->h, FIL_ST
c0780 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33 46 69 ANDARD, &fsts3Fi
c0790 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 leInfo, sizeof(F
c07a0 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b 0a 20 ILESTATUS3) );.
c07b0 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 if( rc == NO_ER
c07c0 52 4f 52 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a ROR ){. *pSiz
c07d0 65 20 3d 20 66 73 74 73 33 46 69 6c 65 49 6e 66 e = fsts3FileInf
c07e0 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 20 20 72 65 o.cbFile;. re
c07f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c0800 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
c0810 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
c0820 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f _FSTAT;. }.}../
c0830 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 72 *.** Acquire a r
c0840 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 eader lock..*/.s
c0850 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65 61 tatic int getRea
c0860 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a dLock( os2File *
c0870 70 46 69 6c 65 20 29 7b 0a 20 20 46 49 4c 45 4c pFile ){. FILEL
c0880 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 OCK LockArea,.
c0890 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 Unloc
c08a0 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45 54 20 kArea;. APIRET
c08b0 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c res;. memset(&L
c08c0 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 ockArea, 0, size
c08d0 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 of(LockArea));.
c08e0 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 memset(&UnlockA
c08f0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 rea, 0, sizeof(U
c0900 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c nlockArea));. L
c0910 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c0920 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a = SHARED_FIRST;.
c0930 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 LockArea.lRang
c0940 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b e = SHARED_SIZE;
c0950 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f . UnlockArea.lO
c0960 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 55 6e ffset = 0L;. Un
c0970 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c0980 3d 20 30 4c 3b 0a 20 20 72 65 73 20 3d 20 44 6f = 0L;. res = Do
c0990 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 sSetFileLocks( p
c09a0 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b File->h, &Unlock
c09b0 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c Area, &LockArea,
c09c0 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 31 LOCK_TIMEOUT, 1
c09d0 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 L );. OSTRACE3(
c09e0 20 22 47 45 54 52 45 41 44 4c 4f 43 4b 20 25 64 "GETREADLOCK %d
c09f0 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c res=%d\n", pFil
c0a00 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 72 e->h, res );. r
c0a10 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a eturn res;.}../*
c0a20 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c .** Undo a readl
c0a30 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ock.*/.static in
c0a40 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b t unlockReadLock
c0a50 28 20 6f 73 32 46 69 6c 65 20 2a 69 64 20 29 7b ( os2File *id ){
c0a60 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 . FILELOCK Loc
c0a70 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 kArea,.
c0a80 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 UnlockArea;.
c0a90 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20 20 6d APIRET res;. m
c0aa0 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c emset(&LockArea,
c0ab0 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 0, sizeof(LockA
c0ac0 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 rea));. memset(
c0ad0 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 &UnlockArea, 0,
c0ae0 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 sizeof(UnlockAre
c0af0 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e a));. LockArea.
c0b00 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 lOffset = 0L;.
c0b10 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
c0b20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 = 0L;. UnlockAr
c0b30 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 ea.lOffset = SHA
c0b40 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 55 6e 6c RED_FIRST;. Unl
c0b50 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c0b60 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 SHARED_SIZE;.
c0b70 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 res = DosSetFile
c0b80 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 2c 20 26 55 Locks( id->h, &U
c0b90 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b nlockArea, &Lock
c0ba0 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f Area, LOCK_TIMEO
c0bb0 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 UT, 1L );. OSTR
c0bc0 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 2d 52 45 ACE3( "UNLOCK-RE
c0bd0 41 44 4c 4f 43 4b 20 66 69 6c 65 20 68 61 6e 64 ADLOCK file hand
c0be0 6c 65 3d 25 64 20 72 65 73 3d 25 64 3f 5c 6e 22 le=%d res=%d?\n"
c0bf0 2c 20 69 64 2d 3e 68 2c 20 72 65 73 20 29 3b 0a , id->h, res );.
c0c00 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a return res;.}.
c0c10 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 ./*.** Lock the
c0c20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f file with the lo
c0c30 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 ck specified by
c0c40 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 parameter lockty
c0c50 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 pe - one.** of t
c0c60 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a he following:.**
c0c70 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 .** (1) SHAR
c0c80 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
c0c90 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 2) RESERVED_LOCK
c0ca0 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 .** (3) PEND
c0cb0 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 ING_LOCK.**
c0cc0 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f (4) EXCLUSIVE_LO
c0cd0 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d CK.**.** Sometim
c0ce0 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 es when requesti
c0cf0 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 ng one lock stat
c0d00 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f e, additional lo
c0d10 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 ck states.** are
c0d20 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 inserted in bet
c0d30 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 ween. The locki
c0d40 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e ng might fail on
c0d50 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 one of the late
c0d60 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 r.** transitions
c0d70 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 leaving the loc
c0d80 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e k state differen
c0d90 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 t from what it s
c0da0 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 tarted but.** st
c0db0 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 ill short of its
c0dc0 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c goal. The foll
c0dd0 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 owing chart show
c0de0 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a s the allowed.**
c0df0 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 transitions and
c0e00 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e the inserted in
c0e10 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 termediate state
c0e20 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f s:.**.** UNLO
c0e30 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a CKED -> SHARED.*
c0e40 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 * SHARED -> R
c0e50 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 ESERVED.** SH
c0e60 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 ARED -> (PENDING
c0e70 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
c0e80 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e * RESERVED ->
c0e90 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 (PENDING) -> EX
c0ea0 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 CLUSIVE.** PE
c0eb0 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 NDING -> EXCLUSI
c0ec0 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f VE.**.** This ro
c0ed0 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 utine will only
c0ee0 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e increase a lock.
c0ef0 20 20 54 68 65 20 6f 73 32 55 6e 6c 6f 63 6b 28 The os2Unlock(
c0f00 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 ) routine.** era
c0f10 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 ses all locks at
c0f20 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e once and return
c0f30 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 s us immediately
c0f40 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 to locking leve
c0f50 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f l 0..** It is no
c0f60 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f t possible to lo
c0f70 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 wer the locking
c0f80 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 level one step a
c0f90 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a t a time. You.*
c0fa0 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69 67 * must go straig
c0fb0 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 ht to locking le
c0fc0 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 vel 0..*/.static
c0fd0 20 69 6e 74 20 6f 73 32 4c 6f 63 6b 28 20 73 71 int os2Lock( sq
c0fe0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
c0ff0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a int locktype ){.
c1000 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
c1010 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 E_OK; /* R
c1020 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
c1030 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 subroutines */.
c1040 20 41 50 49 52 45 54 20 72 65 73 20 3d 20 4e 4f APIRET res = NO
c1050 5f 45 52 52 4f 52 3b 20 20 20 20 2f 2a 20 52 65 _ERROR; /* Re
c1060 73 75 6c 74 20 6f 66 20 61 6e 20 4f 53 2f 32 20 sult of an OS/2
c1070 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 lock call */. i
c1080 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 nt newLocktype;
c1090 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46 69 /* Set pFi
c10a0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20 le->locktype to
c10b0 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f 72 this value befor
c10c0 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69 e exiting */. i
c10d0 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 nt gotPendingLoc
c10e0 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69 66 k = 0;/* True if
c10f0 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20 50 we acquired a P
c1100 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73 ENDING lock this
c1110 20 74 69 6d 65 20 2a 2f 0a 20 20 46 49 4c 45 4c time */. FILEL
c1120 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 OCK LockArea,.
c1130 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 Unloc
c1140 6b 41 72 65 61 3b 0a 20 20 6f 73 32 46 69 6c 65 kArea;. os2File
c1150 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 *pFile = (os2Fi
c1160 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 6d 73 65 74 le*)id;. memset
c1170 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 (&LockArea, 0, s
c1180 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 izeof(LockArea))
c1190 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f ;. memset(&Unlo
c11a0 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f ckArea, 0, sizeo
c11b0 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a f(UnlockArea));.
c11c0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 assert( pFile!
c11d0 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 =0 );. OSTRACE4
c11e0 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 ( "LOCK %d %d wa
c11f0 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s %d\n", pFile->
c1200 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 h, locktype, pFi
c1210 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a le->locktype );.
c1220 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 . /* If there i
c1230 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b s already a lock
c1240 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 of this type or
c1250 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 more restrictiv
c1260 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 73 e on the. ** os
c1270 32 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 2File, do nothin
c1280 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 g. Don't use the
c1290 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 end_lock: exit
c12a0 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71 path, as. ** sq
c12b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
c12c0 72 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 r() hasn't been
c12d0 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f called yet.. */
c12e0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
c12f0 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 cktype>=locktype
c1300 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 ){. OSTRACE3
c1310 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 6f 6b ( "LOCK %d %d ok
c1320 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c (already held)\
c1330 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
c1340 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 72 65 cktype );. re
c1350 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c1360 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
c1370 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ure the locking
c1380 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 sequence is corr
c1390 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ect. */. asser
c13a0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
c13b0 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c pe!=NO_LOCK || l
c13c0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
c13d0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
c13e0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 ( locktype!=PEND
c13f0 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 ING_LOCK );. as
c1400 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d sert( locktype!=
c1410 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c RESERVED_LOCK ||
c1420 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c1430 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
c1440 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 .. /* Lock the
c1450 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 PENDING_LOCK byt
c1460 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 e if we need to
c1470 61 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e acquire a PENDIN
c1480 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 G lock or. ** a
c1490 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 SHARED lock. I
c14a0 66 20 77 65 20 61 72 65 20 61 63 71 75 69 72 69 f we are acquiri
c14b0 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b ng a SHARED lock
c14c0 2c 20 74 68 65 20 61 63 71 75 69 73 69 74 69 6f , the acquisitio
c14d0 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 n of. ** the PE
c14e0 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 NDING_LOCK byte
c14f0 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 is temporary..
c1500 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 */. newLocktype
c1510 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 = pFile->lockty
c1520 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d pe;. if( pFile-
c1530 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f >locktype==NO_LO
c1540 43 4b 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 CK. || (loc
c1550 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
c1560 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e _LOCK && pFile->
c1570 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 locktype==RESERV
c1580 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 ED_LOCK). ){.
c1590 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
c15a0 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 et = PENDING_BYT
c15b0 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e E;. LockArea.
c15c0 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 lRange = 1L;.
c15d0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 UnlockArea.lOff
c15e0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e set = 0L;. Un
c15f0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c1600 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f 2a 20 77 61 = 0L;.. /* wa
c1610 69 74 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 4c it longer than L
c1620 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 68 65 72 65 OCK_TIMEOUT here
c1630 20 6e 6f 74 20 74 6f 20 68 61 76 65 20 74 6f 20 not to have to
c1640 74 72 79 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d try multiple tim
c1650 65 73 20 2a 2f 0a 20 20 20 20 72 65 73 20 3d 20 es */. res =
c1660 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c1670 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c1680 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c1690 61 2c 20 31 30 30 4c 2c 20 30 4c 20 29 3b 0a 20 a, 100L, 0L );.
c16a0 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f if( res == NO
c16b0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 _ERROR ){.
c16c0 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d gotPendingLock =
c16d0 20 31 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 1;. OSTRAC
c16e0 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 70 65 6e E3( "LOCK %d pen
c16f0 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 ding lock boolea
c1700 6e 20 73 65 74 2e 20 20 72 65 73 3d 25 64 5c 6e n set. res=%d\n
c1710 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 ", pFile->h, res
c1720 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 );. }. }..
c1730 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 73 68 /* Acquire a sh
c1740 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 ared lock. */.
c1750 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
c1760 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 HARED_LOCK && re
c1770 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b s == NO_ERROR ){
c1780 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
c1790 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f le->locktype==NO
c17a0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 _LOCK );. res
c17b0 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 = getReadLock(p
c17c0 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72 File);. if( r
c17d0 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
c17e0 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 {. newLockt
c17f0 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 ype = SHARED_LOC
c1800 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 K;. }. OST
c1810 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 RACE3( "LOCK %d
c1820 61 63 71 75 69 72 65 20 73 68 61 72 65 64 20 6c acquire shared l
c1830 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c 20 ock. res=%d\n",
c1840 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
c1850 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
c1860 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f re a RESERVED lo
c1870 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f ck. */. if( lo
c1880 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 cktype==RESERVED
c1890 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 _LOCK && res ==
c18a0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
c18b0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
c18c0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
c18d0 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c 6f 63 6b LOCK );. Lock
c18e0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 Area.lOffset = R
c18f0 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 ESERVED_BYTE;.
c1900 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 LockArea.lRang
c1910 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f e = 1L;. Unlo
c1920 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c1930 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 0L;. UnlockA
c1940 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b rea.lRange = 0L;
c1950 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 . res = DosSe
c1960 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c tFileLocks( pFil
c1970 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 e->h, &UnlockAre
c1980 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f a, &LockArea, LO
c1990 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 CK_TIMEOUT, 0L )
c19a0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d ;. if( res ==
c19b0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
c19c0 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d newLocktype =
c19d0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a RESERVED_LOCK;.
c19e0 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 }. OSTRAC
c19f0 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 E3( "LOCK %d acq
c1a00 75 69 72 65 20 72 65 73 65 72 76 65 64 20 6c 6f uire reserved lo
c1a10 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 ck. res=%d\n", p
c1a20 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a File->h, res );.
c1a30 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
c1a40 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b e a PENDING lock
c1a50 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
c1a60 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f type==EXCLUSIVE_
c1a70 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e LOCK && res == N
c1a80 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 6e O_ERROR ){. n
c1a90 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e ewLocktype = PEN
c1aa0 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 DING_LOCK;. g
c1ab0 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 otPendingLock =
c1ac0 30 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 0;. OSTRACE2(
c1ad0 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 "LOCK %d acquir
c1ae0 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 e pending lock.
c1af0 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f pending lock boo
c1b00 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e 22 2c 20 lean unset.\n",
c1b10 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 7d 0a pFile->h );. }.
c1b20 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e . /* Acquire an
c1b30 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a EXCLUSIVE lock.
c1b40 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
c1b50 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
c1b60 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f OCK && res == NO
c1b70 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 _ERROR ){. as
c1b80 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 sert( pFile->loc
c1b90 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f ktype>=SHARED_LO
c1ba0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 CK );. res =
c1bb0 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 unlockReadLock(p
c1bc0 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 File);. OSTRA
c1bd0 43 45 32 28 20 22 75 6e 72 65 61 64 6c 6f 63 6b CE2( "unreadlock
c1be0 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b = %d\n", res );
c1bf0 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f . LockArea.lO
c1c00 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 ffset = SHARED_F
c1c10 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 IRST;. LockAr
c1c20 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 ea.lRange = SHAR
c1c30 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 55 6e 6c ED_SIZE;. Unl
c1c40 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c1c50 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b = 0L;. Unlock
c1c60 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
c1c70 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 ;. res = DosS
c1c80 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 etFileLocks( pFi
c1c90 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 le->h, &UnlockAr
c1ca0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c ea, &LockArea, L
c1cb0 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 OCK_TIMEOUT, 0L
c1cc0 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d );. if( res =
c1cd0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c1ce0 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 newLocktype
c1cf0 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b = EXCLUSIVE_LOCK
c1d00 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
c1d10 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 53 OSTRACE2( "OS
c1d20 2f 32 20 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 /2 error-code =
c1d30 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a 20 20 %d\n", res );.
c1d40 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 getReadLock(
c1d50 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 pFile);. }.
c1d60 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 OSTRACE3( "LOC
c1d70 4b 20 25 64 20 61 63 71 75 69 72 65 20 65 78 63 K %d acquire exc
c1d80 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 72 65 lusive lock. re
c1d90 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s=%d\n", pFile->
c1da0 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 h, res );. }..
c1db0 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f /* If we are ho
c1dc0 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 lding a PENDING
c1dd0 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20 lock that ought
c1de0 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20 to be released,
c1df0 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 then. ** releas
c1e00 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 e it now.. */.
c1e10 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c if( gotPendingL
c1e20 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d ock && locktype=
c1e30 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a =SHARED_LOCK ){.
c1e40 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20 20 4c int r;. L
c1e50 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c1e60 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 = 0L;. LockAr
c1e70 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a ea.lRange = 0L;.
c1e80 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
c1e90 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 Offset = PENDING
c1ea0 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 _BYTE;. Unloc
c1eb0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 kArea.lRange = 1
c1ec0 4c 3b 0a 20 20 20 20 72 20 3d 20 44 6f 73 53 65 L;. r = DosSe
c1ed0 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c tFileLocks( pFil
c1ee0 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 e->h, &UnlockAre
c1ef0 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f a, &LockArea, LO
c1f00 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 CK_TIMEOUT, 0L )
c1f10 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 ;. OSTRACE3(
c1f20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 6b 69 "LOCK %d unlocki
c1f30 6e 67 20 70 65 6e 64 69 6e 67 2f 69 73 20 73 68 ng pending/is sh
c1f40 61 72 65 64 2e 20 72 3d 25 64 5c 6e 22 2c 20 70 ared. r=%d\n", p
c1f50 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 File->h, r );.
c1f60 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 }.. /* Update t
c1f70 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 he state of the
c1f80 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 69 6e lock has held in
c1f90 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
c1fa0 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72 ptor then. ** r
c1fb0 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70 eturn the approp
c1fc0 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 6f 64 riate result cod
c1fd0 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 e.. */. if( re
c1fe0 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b s == NO_ERROR ){
c1ff0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
c2000 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
c2010 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c 4f 43 OSTRACE4( "LOC
c2020 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 79 69 K FAILED %d tryi
c2030 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 67 6f ng for %d but go
c2040 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e t %d\n", pFile->
c2050 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 h,.
c2060 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f locktype, newLo
c2070 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 72 63 cktype );. rc
c2080 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
c2090 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 }. pFile->loc
c20a0 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 ktype = newLockt
c20b0 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 ype;. OSTRACE3(
c20c0 20 22 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64 "LOCK %d now %d
c20d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 \n", pFile->h, p
c20e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
c20f0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
c2100 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
c2110 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 tine checks if t
c2120 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 here is a RESERV
c2130 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 ED lock held on
c2140 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
c2150 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 file by this or
c2160 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 any other proce
c2170 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f ss. If such a lo
c2180 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 75 ck is held, retu
c2190 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 rn.** non-zero,
c21a0 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a otherwise zero..
c21b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c21c0 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 2CheckReservedLo
c21d0 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck( sqlite3_file
c21e0 20 2a 69 64 2c 20 69 6e 74 20 2a 70 4f 75 74 20 *id, int *pOut
c21f0 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a ){. int r = 0;.
c2200 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 os2File *pFile
c2210 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b = (os2File*)id;
c2220 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
c2230 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 !=0 );. if( pFi
c2240 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 le->locktype>=RE
c2250 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SERVED_LOCK ){.
c2260 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20 4f 53 r = 1;. OS
c2270 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52 TRACE3( "TEST WR
c2280 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f 63 -LOCK %d %d (loc
c2290 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 al)\n", pFile->h
c22a0 2c 20 72 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a , r );. }else{.
c22b0 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f FILELOCK Lo
c22c0 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 ckArea,.
c22d0 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 UnlockArea
c22e0 3b 0a 20 20 20 20 41 50 49 52 45 54 20 72 63 20 ;. APIRET rc
c22f0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 20 20 = NO_ERROR;.
c2300 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 memset(&LockArea
c2310 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b , 0, sizeof(Lock
c2320 41 72 65 61 29 29 3b 0a 20 20 20 20 6d 65 6d 73 Area));. mems
c2330 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 et(&UnlockArea,
c2340 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 0, sizeof(Unlock
c2350 41 72 65 61 29 29 3b 0a 20 20 20 20 4c 6f 63 6b Area));. Lock
c2360 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 Area.lOffset = R
c2370 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 ESERVED_BYTE;.
c2380 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 LockArea.lRang
c2390 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f e = 1L;. Unlo
c23a0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c23b0 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 0L;. UnlockA
c23c0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b rea.lRange = 0L;
c23d0 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 53 65 74 . rc = DosSet
c23e0 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 FileLocks( pFile
c23f0 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 ->h, &UnlockArea
c2400 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 , &LockArea, LOC
c2410 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b K_TIMEOUT, 0L );
c2420 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 . OSTRACE3( "
c2430 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 TEST WR-LOCK %d
c2440 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79 lock reserved by
c2450 74 65 20 72 63 3d 25 64 5c 6e 22 2c 20 70 46 69 te rc=%d\n", pFi
c2460 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20 20 20 le->h, rc );.
c2470 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 if( rc == NO_ER
c2480 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 41 50 49 ROR ){. API
c2490 52 45 54 20 72 63 75 20 3d 20 4e 4f 5f 45 52 52 RET rcu = NO_ERR
c24a0 4f 52 3b 20 2f 2a 20 72 65 74 75 72 6e 20 63 6f OR; /* return co
c24b0 64 65 20 66 6f 72 20 75 6e 6c 6f 63 6b 69 6e 67 de for unlocking
c24c0 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 */. LockAr
c24d0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b ea.lOffset = 0L;
c24e0 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e . LockArea.
c24f0 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 lRange = 0L;.
c2500 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
c2510 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44 ffset = RESERVED
c2520 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 55 6e 6c _BYTE;. Unl
c2530 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c2540 20 31 4c 3b 0a 20 20 20 20 20 20 72 63 75 20 3d 1L;. rcu =
c2550 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
c2560 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
c2570 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
c2580 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
c2590 2c 20 30 4c 20 29 3b 0a 20 20 20 20 20 20 4f 53 , 0L );. OS
c25a0 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52 TRACE3( "TEST WR
c25b0 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 6b 20 -LOCK %d unlock
c25c0 72 65 73 65 72 76 65 64 20 62 79 74 65 20 72 3d reserved byte r=
c25d0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
c25e0 20 72 63 75 20 29 3b 0a 20 20 20 20 7d 0a 20 20 rcu );. }.
c25f0 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20 4e 4f r = !(rc == NO
c2600 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 4f 53 54 _ERROR);. OST
c2610 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52 2d RACE3( "TEST WR-
c2620 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d 6f LOCK %d %d (remo
c2630 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 te)\n", pFile->h
c2640 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f , r );. }. *pO
c2650 75 74 20 3d 20 72 3b 0a 20 20 72 65 74 75 72 6e ut = r;. return
c2660 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
c2670 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c *.** Lower the l
c2680 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 ocking level on
c2690 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
c26a0 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 id to locktype.
c26b0 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 locktype.** mus
c26c0 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c t be either NO_L
c26d0 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f OCK or SHARED_LO
c26e0 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 CK..**.** If the
c26f0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
c2700 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 f the file descr
c2710 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 iptor is already
c2720 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 at or below.**
c2730 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f the requested lo
c2740 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 cking level, thi
c2750 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
c2760 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 o-op..**.** It i
c2770 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 s not possible f
c2780 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 or this routine
c2790 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20 73 to fail if the s
c27a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a econd argument.*
c27b0 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 * is NO_LOCK. I
c27c0 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
c27d0 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44 5f ument is SHARED_
c27e0 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20 72 LOCK then this r
c27f0 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 outine.** might
c2800 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
c2810 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ERR;.*/.static i
c2820 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28 20 73 71 nt os2Unlock( sq
c2830 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
c2840 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a int locktype ){.
c2850 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 6f 73 int type;. os
c2860 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
c2870 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 41 os2File*)id;. A
c2880 50 49 52 45 54 20 72 63 20 3d 20 53 51 4c 49 54 PIRET rc = SQLIT
c2890 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 45 54 20 72 E_OK;. APIRET r
c28a0 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 es = NO_ERROR;.
c28b0 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 FILELOCK LockA
c28c0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rea,.
c28d0 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6d UnlockArea;. m
c28e0 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c emset(&LockArea,
c28f0 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 0, sizeof(LockA
c2900 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 rea));. memset(
c2910 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 &UnlockArea, 0,
c2920 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 sizeof(UnlockAre
c2930 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 a));. assert( p
c2940 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 File!=0 );. ass
c2950 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 ert( locktype<=S
c2960 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
c2970 4f 53 54 52 41 43 45 34 28 20 22 55 4e 4c 4f 43 OSTRACE4( "UNLOC
c2980 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20 25 K %d to %d was %
c2990 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
c29a0 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d locktype, pFile-
c29b0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 74 >locktype );. t
c29c0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 ype = pFile->loc
c29d0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79 70 ktype;. if( typ
c29e0 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e>=EXCLUSIVE_LOC
c29f0 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 K ){. LockAre
c2a00 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
c2a10 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 LockArea.lRa
c2a20 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e nge = 0L;. Un
c2a30 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
c2a40 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
c2a50 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e . UnlockArea.
c2a60 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f lRange = SHARED_
c2a70 53 49 5a 45 3b 0a 20 20 20 20 72 65 73 20 3d 20 SIZE;. res =
c2a80 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c2a90 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c2aa0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c2ab0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c2ac0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
c2ad0 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 CE3( "UNLOCK %d
c2ae0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 exclusive lock r
c2af0 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d es=%d\n", pFile-
c2b00 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 69 >h, res );. i
c2b10 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
c2b20 52 45 44 5f 4c 4f 43 4b 20 26 26 20 67 65 74 52 RED_LOCK && getR
c2b30 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 21 eadLock(pFile) !
c2b40 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c2b50 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 /* This shou
c2b60 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 2e ld never happen.
c2b70 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 We should alwa
c2b80 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 20 ys be able to.
c2b90 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 65 ** reacquire
c2ba0 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a the read lock *
c2bb0 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 /. OSTRACE3
c2bc0 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 ( "UNLOCK %d to
c2bd0 25 64 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 29 %d getReadLock()
c2be0 20 66 61 69 6c 65 64 5c 6e 22 2c 20 70 46 69 6c failed\n", pFil
c2bf0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 e->h, locktype )
c2c00 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
c2c10 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
c2c20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
c2c30 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 ( type>=RESERVED
c2c40 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 _LOCK ){. Loc
c2c50 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
c2c60 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 0L;. LockArea
c2c70 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 .lRange = 0L;.
c2c80 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
c2c90 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f fset = RESERVED_
c2ca0 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b BYTE;. Unlock
c2cb0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c Area.lRange = 1L
c2cc0 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 ;. res = DosS
c2cd0 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 etFileLocks( pFi
c2ce0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 le->h, &UnlockAr
c2cf0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c ea, &LockArea, L
c2d00 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 OCK_TIMEOUT, 0L
c2d10 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 );. OSTRACE3(
c2d20 20 22 55 4e 4c 4f 43 4b 20 25 64 20 72 65 73 65 "UNLOCK %d rese
c2d30 72 76 65 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 rved res=%d\n",
c2d40 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
c2d50 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 . }. if( lockt
c2d60 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 ype==NO_LOCK &&
c2d70 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 type>=SHARED_LOC
c2d80 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 75 K ){. res = u
c2d90 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 nlockReadLock(pF
c2da0 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 ile);. OSTRAC
c2db0 45 35 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 69 E5( "UNLOCK %d i
c2dc0 73 20 25 64 20 77 61 6e 74 20 25 64 20 72 65 73 s %d want %d res
c2dd0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
c2de0 2c 20 74 79 70 65 2c 20 6c 6f 63 6b 74 79 70 65 , type, locktype
c2df0 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 , res );. }. i
c2e00 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 f( type>=PENDING
c2e10 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 _LOCK ){. Loc
c2e20 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
c2e30 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 0L;. LockArea
c2e40 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 .lRange = 0L;.
c2e50 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
c2e60 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 fset = PENDING_B
c2e70 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 YTE;. UnlockA
c2e80 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b rea.lRange = 1L;
c2e90 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 . res = DosSe
c2ea0 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c tFileLocks( pFil
c2eb0 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 e->h, &UnlockAre
c2ec0 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f a, &LockArea, LO
c2ed0 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 CK_TIMEOUT, 0L )
c2ee0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 ;. OSTRACE3(
c2ef0 22 55 4e 4c 4f 43 4b 20 25 64 20 70 65 6e 64 69 "UNLOCK %d pendi
c2f00 6e 67 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 ng res=%d\n", pF
c2f10 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 ile->h, res );.
c2f20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b }. pFile->lock
c2f30 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
c2f40 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e . OSTRACE3( "UN
c2f50 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64 5c 6e LOCK %d now %d\n
c2f60 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 ", pFile->h, pFi
c2f70 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a le->locktype );.
c2f80 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
c2f90 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e /*.** Control an
c2fa0 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 6f d query of the o
c2fb0 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e pen file handle.
c2fc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c2fd0 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 s2FileControl(sq
c2fe0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
c2ff0 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 int op, void *pA
c3000 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f rg){. switch( o
c3010 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 p ){. case SQ
c3020 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 LITE_FCNTL_LOCKS
c3030 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 TATE: {. *(
c3040 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 6f 73 int*)pArg = ((os
c3050 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 2File*)id)->lock
c3060 74 79 70 65 3b 0a 20 20 20 20 20 20 4f 53 54 52 type;. OSTR
c3070 41 43 45 33 28 20 22 46 43 4e 54 4c 5f 4c 4f 43 ACE3( "FCNTL_LOC
c3080 4b 53 54 41 54 45 20 25 64 20 6c 6f 63 6b 3d 25 KSTATE %d lock=%
c3090 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c 65 2a d\n", ((os2File*
c30a0 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32 46 69 )id)->h, ((os2Fi
c30b0 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 le*)id)->locktyp
c30c0 65 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 e );. retur
c30d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
c30e0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
c30f0 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a SQLITE_ERROR;.}.
c3100 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
c3110 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e e sector size in
c3120 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e bytes of the un
c3130 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 derlying block d
c3140 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 evice for.** the
c3150 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e specified file.
c3160 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 This is almost
c3170 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 73 always 512 bytes
c3180 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 , but may be.**
c3190 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 larger for some
c31a0 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 devices..**.** S
c31b0 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75 6d QLite code assum
c31c0 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e es this function
c31d0 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 cannot fail. It
c31e0 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 68 also assumes th
c31f0 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c at.** if two fil
c3200 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 69 es are created i
c3210 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2d n the same file-
c3220 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72 79 system directory
c3230 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 (i.e..** a data
c3240 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 base and its jou
c3250 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20 rnal file) that
c3260 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
c3270 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 will be the.** s
c3280 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f ame for both..*/
c3290 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 .static int os2S
c32a0 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 ectorSize(sqlite
c32b0 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 3_file *id){. r
c32c0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45 46 eturn SQLITE_DEF
c32d0 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 AULT_SECTOR_SIZE
c32e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
c32f0 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20 64 65 n a vector of de
c3300 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 vice characteris
c3310 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tics..*/.static
c3320 69 6e 74 20 6f 73 32 44 65 76 69 63 65 43 68 61 int os2DeviceCha
c3330 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c racteristics(sql
c3340 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
c3350 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a return 0;.}...
c3360 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 72 20 /*.** Character
c3370 73 65 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f set conversion o
c3380 62 6a 65 63 74 73 20 75 73 65 64 20 62 79 20 63 bjects used by c
c3390 6f 6e 76 65 72 73 69 6f 6e 20 72 6f 75 74 69 6e onversion routin
c33a0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 55 63 es..*/.static Uc
c33b0 6f 6e 76 4f 62 6a 65 63 74 20 75 63 55 74 66 38 onvObject ucUtf8
c33c0 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 63 6f 6e 76 = NULL; /* conv
c33d0 65 72 74 20 62 65 74 77 65 65 6e 20 55 54 46 2d ert between UTF-
c33e0 38 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f 0a 73 8 and UCS-2 */.s
c33f0 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 tatic UconvObjec
c3400 74 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 20 t uclCp = NULL;
c3410 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 77 /* convert betw
c3420 65 65 6e 20 6c 6f 63 61 6c 20 63 6f 64 65 70 61 een local codepa
c3430 67 65 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f 0a ge and UCS-2 */.
c3440 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 ./*.** Helper fu
c3450 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 69 74 69 61 nction to initia
c3460 6c 69 7a 65 20 74 68 65 20 63 6f 6e 76 65 72 73 lize the convers
c3470 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d ion objects from
c3480 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a and to UTF-8..*
c3490 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
c34a0 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 itUconvObjects(
c34b0 76 6f 69 64 20 29 7b 0a 20 20 69 66 28 20 55 6e void ){. if( Un
c34c0 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 6a 65 iCreateUconvObje
c34d0 63 74 28 20 55 54 46 5f 38 2c 20 26 75 63 55 74 ct( UTF_8, &ucUt
c34e0 66 38 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 f8 ) != ULS_SUCC
c34f0 45 53 53 20 29 0a 20 20 20 20 75 63 55 74 66 38 ESS ). ucUtf8
c3500 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 66 20 28 20 = NULL;. if (
c3510 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 UniCreateUconvOb
c3520 6a 65 63 74 28 20 28 55 6e 69 43 68 61 72 20 2a ject( (UniChar *
c3530 29 4c 22 40 70 61 74 68 3d 79 65 73 22 2c 20 26 )L"@path=yes", &
c3540 75 63 6c 43 70 20 29 20 21 3d 20 55 4c 53 5f 53 uclCp ) != ULS_S
c3550 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 63 6c UCCESS ). ucl
c3560 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a Cp = NULL;.}../*
c3570 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 .** Helper funct
c3580 69 6f 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20 ion to free the
c3590 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 conversion objec
c35a0 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 ts from and to U
c35b0 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 TF-8..*/.static
c35c0 76 6f 69 64 20 66 72 65 65 55 63 6f 6e 76 4f 62 void freeUconvOb
c35d0 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 jects( void ){.
c35e0 20 69 66 20 28 20 75 63 55 74 66 38 20 29 0a 20 if ( ucUtf8 ).
c35f0 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e 76 4f UniFreeUconvO
c3600 62 6a 65 63 74 28 20 75 63 55 74 66 38 20 29 3b bject( ucUtf8 );
c3610 0a 20 20 69 66 20 28 20 75 63 6c 43 70 20 29 0a . if ( uclCp ).
c3620 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e 76 UniFreeUconv
c3630 4f 62 6a 65 63 74 28 20 75 63 6c 43 70 20 29 3b Object( uclCp );
c3640 0a 20 20 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c . ucUtf8 = NULL
c3650 3b 0a 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c ;. uclCp = NULL
c3660 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 ;.}../*.** Helpe
c3670 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f r function to co
c3680 6e 76 65 72 74 20 55 54 46 2d 38 20 66 69 6c 65 nvert UTF-8 file
c3690 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 61 6c 20 4f names to local O
c36a0 53 2f 32 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a S/2 codepage..**
c36b0 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 72 The two-step pr
c36c0 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f 6e ocess: first con
c36d0 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e vert the incomin
c36e0 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a g UTF-8 string.*
c36f0 2a 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 * into UCS-2 and
c3700 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 then from UCS-2
c3710 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 to the current
c3720 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 68 65 codepage..** The
c3730 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 70 returned char p
c3740 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65 ointer has to be
c3750 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 freed..*/.stati
c3760 63 20 63 68 61 72 20 2a 63 6f 6e 76 65 72 74 55 c char *convertU
c3770 74 66 38 50 61 74 68 54 6f 43 70 28 20 63 6f 6e tf8PathToCp( con
c3780 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 st char *in ){.
c3790 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50 61 74 UniChar tempPat
c37a0 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 h[CCHMAXPATH];.
c37b0 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 char *out = (ch
c37c0 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 ar *)calloc( CCH
c37d0 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 MAXPATH, 1 );..
c37e0 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20 20 20 if( !out ).
c37f0 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 return NULL;..
c3800 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c 20 21 if( !ucUtf8 || !
c3810 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e 69 74 uclCp ). init
c3820 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a UconvObjects();.
c3830 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 . /* determine
c3840 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 63 string for the c
c3850 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 onversion of UTF
c3860 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 31 32 -8 which is CP12
c3870 30 38 20 2a 2f 0a 20 20 69 66 28 20 55 6e 69 53 08 */. if( UniS
c3880 74 72 54 6f 55 63 73 28 20 75 63 55 74 66 38 2c trToUcs( ucUtf8,
c3890 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68 61 72 tempPath, (char
c38a0 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50 41 54 *)in, CCHMAXPAT
c38b0 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 H ) != ULS_SUCCE
c38c0 53 53 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 SS ). return
c38d0 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e 76 65 out; /* if conve
c38e0 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72 65 74 rsion fails, ret
c38f0 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20 73 74 urn the empty st
c3900 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 63 6f ring */.. /* co
c3910 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 72 nversion for cur
c3920 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 68 rent codepage wh
c3930 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 ich can be used
c3940 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 55 for paths */. U
c3950 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20 75 63 niStrFromUcs( uc
c3960 6c 43 70 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 lCp, out, tempPa
c3970 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 th, CCHMAXPATH )
c3980 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b ;.. return out;
c3990 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 .}../*.** Helper
c39a0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e function to con
c39b0 76 65 72 74 20 66 69 6c 65 6e 61 6d 65 73 20 66 vert filenames f
c39c0 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f 64 65 70 61 rom local codepa
c39d0 67 65 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 20 ge to UTF-8..**
c39e0 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 72 6f The two-step pro
c39f0 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f 6e 76 cess: first conv
c3a00 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 ert the incoming
c3a10 20 63 6f 64 65 70 61 67 65 2d 73 70 65 63 69 66 codepage-specif
c3a20 69 63 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 74 ic.** string int
c3a30 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68 65 6e o UCS-2 and then
c3a40 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f 20 74 from UCS-2 to t
c3a50 68 65 20 63 6f 64 65 70 61 67 65 20 6f 66 20 55 he codepage of U
c3a60 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 72 65 74 TF-8..** The ret
c3a70 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 6e 74 urned char point
c3a80 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 72 65 er has to be fre
c3a90 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ed..**.** This f
c3aa0 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 6e 2d 73 unction is non-s
c3ab0 74 61 74 69 63 20 74 6f 20 62 65 20 61 62 6c 65 tatic to be able
c3ac0 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 20 to use this in
c3ad0 73 68 65 6c 6c 2e 63 20 61 6e 64 0a 2a 2a 20 73 shell.c and.** s
c3ae0 69 6d 69 6c 61 72 20 61 70 70 6c 69 63 61 74 69 imilar applicati
c3af0 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 63 6f ons that take co
c3b00 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 72 67 75 6d mmand line argum
c3b10 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 72 20 2a 63 ents..*/.char *c
c3b20 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f 55 74 onvertCpPathToUt
c3b30 66 38 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a f8( const char *
c3b40 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 in ){. UniChar
c3b50 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 tempPath[CCHMAXP
c3b60 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 ATH];. char *ou
c3b70 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c t = (char *)call
c3b80 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 oc( CCHMAXPATH,
c3b90 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 1 );.. if( !out
c3ba0 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 ). return NU
c3bb0 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 LL;.. if( !ucUt
c3bc0 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 f8 || !uclCp ).
c3bd0 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 initUconvObje
c3be0 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 63 6f 6e cts();.. /* con
c3bf0 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 72 72 version for curr
c3c00 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 68 69 ent codepage whi
c3c10 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 ch can be used f
c3c20 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 69 66 or paths */. if
c3c30 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 ( UniStrToUcs( u
c3c40 63 6c 43 70 2c 20 74 65 6d 70 50 61 74 68 2c 20 clCp, tempPath,
c3c50 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d (char *)in, CCHM
c3c60 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f AXPATH ) != ULS_
c3c70 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65 SUCCESS ). re
c3c80 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 turn out; /* if
c3c90 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 conversion fails
c3ca0 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70 , return the emp
c3cb0 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 ty string */..
c3cc0 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 74 72 /* determine str
c3cd0 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6e 76 ing for the conv
c3ce0 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d 38 20 ersion of UTF-8
c3cf0 77 68 69 63 68 20 69 73 20 43 50 31 32 30 38 20 which is CP1208
c3d00 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 */. UniStrFromU
c3d10 63 73 28 20 75 63 55 74 66 38 2c 20 6f 75 74 2c cs( ucUtf8, out,
c3d20 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 tempPath, CCHMA
c3d30 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 XPATH );.. retu
c3d40 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rn out;.}../*.**
c3d50 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 This vector def
c3d60 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 ines all the met
c3d70 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 hods that can op
c3d80 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 erate on an.** s
c3d90 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 qlite3_file for
c3da0 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 os2..*/.static c
c3db0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
c3dc0 6d 65 74 68 6f 64 73 20 6f 73 32 49 6f 4d 65 74 methods os2IoMet
c3dd0 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 hod = {. 1,
c3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3df0 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 /* iVersion
c3e00 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 65 2c 0a 20 */. os2Close,.
c3e10 20 6f 73 32 52 65 61 64 2c 0a 20 20 6f 73 32 57 os2Read,. os2W
c3e20 72 69 74 65 2c 0a 20 20 6f 73 32 54 72 75 6e 63 rite,. os2Trunc
c3e30 61 74 65 2c 0a 20 20 6f 73 32 53 79 6e 63 2c 0a ate,. os2Sync,.
c3e40 20 20 6f 73 32 46 69 6c 65 53 69 7a 65 2c 0a 20 os2FileSize,.
c3e50 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 6f 73 32 55 os2Lock,. os2U
c3e60 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 43 68 65 63 nlock,. os2Chec
c3e70 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 kReservedLock,.
c3e80 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c os2FileControl,
c3e90 0a 20 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 . os2SectorSize
c3ea0 2c 0a 20 20 6f 73 32 44 65 76 69 63 65 43 68 61 ,. os2DeviceCha
c3eb0 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a racteristics.};.
c3ec0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c3ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
c3f10 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 49 Here ends the I
c3f20 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 /O methods that
c3f30 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65 33 form the sqlite3
c3f40 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
c3f50 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 ct..**.** The ne
c3f60 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 xt block of code
c3f70 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
c3f80 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a VFS methods..***
c3f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c3fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
c3fe0 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f * Create a tempo
c3ff0 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 rary file name i
c4000 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 n zBuf. zBuf mu
c4010 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 st be big enough
c4020 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 to.** hold at p
c4030 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 Vfs->mxPathname
c4040 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 characters..*/.s
c4050 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d tatic int getTem
c4060 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 pname(int nBuf,
c4070 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20 char *zBuf ){.
c4080 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
c4090 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72 igned char zChar
c40a0 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 s[] =. "abcde
c40b0 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 fghijklmnopqrstu
c40c0 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 vwxyz". "ABCD
c40d0 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 EFGHIJKLMNOPQRST
c40e0 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 UVWXYZ". "012
c40f0 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e 74 20 3456789";. int
c4100 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65 i, j;. char zTe
c4110 6d 70 50 61 74 68 42 75 66 5b 33 5d 3b 0a 20 20 mpPathBuf[3];.
c4120 50 53 5a 20 7a 54 65 6d 70 50 61 74 68 20 3d 20 PSZ zTempPath =
c4130 28 50 53 5a 29 26 7a 54 65 6d 70 50 61 74 68 42 (PSZ)&zTempPathB
c4140 75 66 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 uf;. if( sqlite
c4150 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3_temp_directory
c4160 20 29 7b 0a 20 20 20 20 7a 54 65 6d 70 50 61 74 ){. zTempPat
c4170 68 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 h = sqlite3_temp
c4180 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 7d 65 _directory;. }e
c4190 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 44 6f 73 lse{. if( Dos
c41a0 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 ScanEnv( (PSZ)"T
c41b0 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 EMP", &zTempPath
c41c0 20 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ) ){. if(
c41d0 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a DosScanEnv( (PSZ
c41e0 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61 )"TMP", &zTempPa
c41f0 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 20 20 th ) ){.
c4200 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 if( DosScanEnv(
c4210 28 50 53 5a 29 22 54 4d 50 44 49 52 22 2c 20 26 (PSZ)"TMPDIR", &
c4220 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 zTempPath ) ){.
c4230 20 20 20 20 20 20 20 20 20 20 55 4c 4f 4e 47 20 ULONG
c4240 75 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30 2c 20 ulDriveNum = 0,
c4250 75 6c 44 72 69 76 65 4d 61 70 20 3d 20 30 3b 0a ulDriveMap = 0;.
c4260 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 51 75 DosQu
c4270 65 72 79 43 75 72 72 65 6e 74 44 69 73 6b 28 20 eryCurrentDisk(
c4280 26 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26 75 6c &ulDriveNum, &ul
c4290 44 72 69 76 65 4d 61 70 20 29 3b 0a 20 20 20 20 DriveMap );.
c42a0 20 20 20 20 20 20 20 73 70 72 69 6e 74 66 28 20 sprintf(
c42b0 28 63 68 61 72 2a 29 7a 54 65 6d 70 50 61 74 68 (char*)zTempPath
c42c0 2c 20 22 25 63 3a 22 2c 20 28 63 68 61 72 29 28 , "%c:", (char)(
c42d0 20 27 41 27 20 2b 20 75 6c 44 72 69 76 65 4e 75 'A' + ulDriveNu
c42e0 6d 20 2d 20 31 20 29 20 29 3b 0a 20 20 20 20 20 m - 1 ) );.
c42f0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
c4300 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 53 74 72 69 }. }. /* Stri
c4310 70 20 6f 66 66 20 61 20 74 72 61 69 6c 69 6e 67 p off a trailing
c4320 20 73 6c 61 73 68 65 73 20 6f 72 20 62 61 63 6b slashes or back
c4330 73 6c 61 73 68 65 73 2c 20 6f 74 68 65 72 77 69 slashes, otherwi
c4340 73 65 20 77 65 20 77 6f 75 6c 64 20 67 65 74 20 se we would get
c4350 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 70 6c 65 20 *. * multiple
c4360 28 62 61 63 6b 29 73 6c 61 73 68 65 73 20 77 68 (back)slashes wh
c4370 69 63 68 20 63 61 75 73 65 73 20 44 6f 73 4f 70 ich causes DosOp
c4380 65 6e 28 29 20 74 6f 20 66 61 69 6c 2e 20 20 20 en() to fail.
c4390 20 20 20 20 20 20 20 20 20 20 20 2a 0a 20 20 20 *.
c43a0 2a 20 54 72 61 69 6c 69 6e 67 20 73 70 61 63 65 * Trailing space
c43b0 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 s are not allowe
c43c0 64 2c 20 65 69 74 68 65 72 2e 20 20 20 20 20 20 d, either.
c43d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c43e0 20 20 20 20 20 20 2a 2f 0a 20 20 6a 20 3d 20 73 */. j = s
c43f0 74 72 6c 65 6e 28 7a 54 65 6d 70 50 61 74 68 29 trlen(zTempPath)
c4400 3b 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 ;. while( j > 0
c4410 20 26 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b && ( zTempPath[
c4420 6a 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 j-1] == '\\' ||
c4430 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d zTempPath[j-1] =
c4440 3d 20 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 = '/'.
c4450 20 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 || zTe
c4460 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 mpPath[j-1] == '
c4470 20 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b ' ) ){. j--;
c4480 0a 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 . }. zTempPath
c4490 5b 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 [j] = '\0';. if
c44a0 28 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f ( !sqlite3_temp_
c44b0 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 directory ){.
c44c0 20 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 char *zTempPath
c44d0 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 UTF = convertCpP
c44e0 61 74 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 athToUtf8( zTemp
c44f0 50 61 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 Path );. sqli
c4500 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 te3_snprintf( nB
c4510 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 uf-30, zBuf,.
c4520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4530 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f "%s\\"SQLITE_
c4540 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 TEMP_FILE_PREFIX
c4550 2c 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 , zTempPathUTF )
c4560 3b 0a 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d ;. free( zTem
c4570 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 pPathUTF );. }e
c4580 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
c4590 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d _snprintf( nBuf-
c45a0 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 30, zBuf,.
c45b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c45c0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d "%s\\"SQLITE_TEM
c45d0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a P_FILE_PREFIX, z
c45e0 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a TempPath );. }.
c45f0 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28 20 7a 42 j = strlen( zB
c4600 75 66 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f uf );. sqlite3_
c4610 72 61 6e 64 6f 6d 6e 65 73 73 28 20 32 30 2c 20 randomness( 20,
c4620 26 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20 20 66 6f &zBuf[j] );. fo
c4630 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 32 30 r( i = 0; i < 20
c4640 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0a 20 20 ; i++, j++ ){.
c4650 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 zBuf[j] = (cha
c4660 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 r)zChars[ ((unsi
c4670 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a gned char)zBuf[j
c4680 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 ])%(sizeof(zChar
c4690 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a s)-1) ];. }. z
c46a0 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 Buf[j] = 0;. OS
c46b0 54 52 41 43 45 32 28 20 22 54 45 4d 50 20 46 49 TRACE2( "TEMP FI
c46c0 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a LENAME: %s\n", z
c46d0 42 75 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 Buf );. return
c46e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
c46f0 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 *.** Turn a rela
c4700 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e tive pathname in
c4710 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 to a full pathna
c4720 6d 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 me. Write the f
c4730 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 ull.** pathname
c4740 69 6e 74 6f 20 7a 46 75 6c 6c 5b 5d 2e 20 20 7a into zFull[]. z
c4750 46 75 6c 6c 5b 5d 20 77 69 6c 6c 20 62 65 20 61 Full[] will be a
c4760 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 t least pVfs->mx
c4770 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 Pathname.** byte
c4780 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 s in size..*/.st
c4790 61 74 69 63 20 69 6e 74 20 6f 73 32 46 75 6c 6c atic int os2Full
c47a0 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 Pathname(. sqli
c47b0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 te3_vfs *pVfs,
c47c0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
c47d0 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 er to vfs object
c47e0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
c47f0 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 *zRelative,
c4800 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 /* Possibly re
c4810 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 lative input pat
c4820 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c 6c h */. int nFull
c4830 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
c4840 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 /* Size of ou
c4850 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 tput buffer in b
c4860 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a ytes */. char *
c4870 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 zFull
c4880 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 /* Output
c4890 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 63 buffer */.){. c
c48a0 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 43 70 har *zRelativeCp
c48b0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 = convertUtf8Pa
c48c0 74 68 54 6f 43 70 28 20 7a 52 65 6c 61 74 69 76 thToCp( zRelativ
c48d0 65 20 29 3b 0a 20 20 63 68 61 72 20 7a 46 75 6c e );. char zFul
c48e0 6c 43 70 5b 43 43 48 4d 41 58 50 41 54 48 5d 20 lCp[CCHMAXPATH]
c48f0 3d 20 22 5c 30 22 3b 0a 20 20 63 68 61 72 20 2a = "\0";. char *
c4900 7a 46 75 6c 6c 55 54 46 3b 0a 20 20 41 50 49 52 zFullUTF;. APIR
c4910 45 54 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 ET rc = DosQuery
c4920 50 61 74 68 49 6e 66 6f 28 20 7a 52 65 6c 61 74 PathInfo( zRelat
c4930 69 76 65 43 70 2c 20 46 49 4c 5f 51 55 45 52 59 iveCp, FIL_QUERY
c4940 46 55 4c 4c 4e 41 4d 45 2c 20 7a 46 75 6c 6c 43 FULLNAME, zFullC
c4950 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 p,.
c4960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4970 20 20 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b CCHMAXPATH );
c4980 0a 20 20 66 72 65 65 28 20 7a 52 65 6c 61 74 69 . free( zRelati
c4990 76 65 43 70 20 29 3b 0a 20 20 7a 46 75 6c 6c 55 veCp );. zFullU
c49a0 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 TF = convertCpPa
c49b0 74 68 54 6f 55 74 66 38 28 20 7a 46 75 6c 6c 43 thToUtf8( zFullC
c49c0 70 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 p );. sqlite3_s
c49d0 6e 70 72 69 6e 74 66 28 20 6e 46 75 6c 6c 2c 20 nprintf( nFull,
c49e0 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55 54 46 20 zFull, zFullUTF
c49f0 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 75 6c 6c );. free( zFull
c4a00 55 54 46 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 UTF );. return
c4a10 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f rc == NO_ERROR ?
c4a20 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c SQLITE_OK : SQL
c4a30 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 0a 2f ITE_IOERR;.}.../
c4a40 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 *.** Open a file
c4a50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c4a60 6f 73 32 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 os2Open(. sqlit
c4a70 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
c4a80 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 /* Not
c4a90 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 used */. const
c4aa0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 char *zName,
c4ab0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
c4ac0 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 of the file */.
c4ad0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 sqlite3_file *i
c4ae0 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f d, /
c4af0 2a 20 57 72 69 74 65 20 74 68 65 20 53 51 4c 69 * Write the SQLi
c4b00 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68 te file handle h
c4b10 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ere */. int fla
c4b20 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
c4b30 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d /* Open m
c4b40 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 ode flags */. i
c4b50 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 nt *pOutFlags
c4b60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c4b70 53 74 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c Status return fl
c4b80 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 46 49 4c ags */.){. HFIL
c4b90 45 20 68 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 46 E h;. ULONG ulF
c4ba0 69 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 46 ileAttribute = F
c4bb0 49 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 55 4c ILE_NORMAL;. UL
c4bc0 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 ONG ulOpenFlags
c4bd0 3d 20 30 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f = 0;. ULONG ulO
c4be0 70 65 6e 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 6f penMode = 0;. o
c4bf0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
c4c00 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
c4c10 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 APIRET rc = NO_E
c4c20 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c RROR;. ULONG ul
c4c30 41 63 74 69 6f 6e 3b 0a 20 20 63 68 61 72 20 2a Action;. char *
c4c40 7a 4e 61 6d 65 43 70 3b 0a 20 20 63 68 61 72 20 zNameCp;. char
c4c50 7a 54 6d 70 6e 61 6d 65 5b 43 43 48 4d 41 58 50 zTmpname[CCHMAXP
c4c60 41 54 48 2b 31 5d 3b 20 20 20 20 2f 2a 20 42 75 ATH+1]; /* Bu
c4c70 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d ffer to hold nam
c4c80 65 20 6f 66 20 74 65 6d 70 20 66 69 6c 65 20 2a e of temp file *
c4c90 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 /.. /* If the s
c4ca0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
c4cb0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 o this function
c4cc0 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 is NULL, generat
c4cd0 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 e a . ** tempor
c4ce0 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f ary file name to
c4cf0 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 use . */. if(
c4d00 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69 !zName ){. i
c4d10 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e nt rc = getTempn
c4d20 61 6d 65 28 43 43 48 4d 41 58 50 41 54 48 2b 31 ame(CCHMAXPATH+1
c4d30 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 , zTmpname);.
c4d40 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
c4d50 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
c4d60 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
c4d70 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d zName = zTmpnam
c4d80 65 3b 0a 20 20 7d 0a 0a 0a 20 20 6d 65 6d 73 65 e;. }... memse
c4d90 74 28 20 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a t( pFile, 0, siz
c4da0 65 6f 66 28 2a 70 46 69 6c 65 29 20 29 3b 0a 0a eof(*pFile) );..
c4db0 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 OSTRACE2( "OPE
c4dc0 4e 20 77 61 6e 74 20 25 64 5c 6e 22 2c 20 66 6c N want %d\n", fl
c4dd0 61 67 73 20 29 3b 0a 0a 20 20 69 66 28 20 66 6c ags );.. if( fl
c4de0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
c4df0 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 N_READWRITE ){.
c4e00 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d ulOpenMode |=
c4e10 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 OPEN_ACCESS_REA
c4e20 44 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 DWRITE;. OSTR
c4e30 41 43 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 ACE1( "OPEN read
c4e40 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d /write\n" );. }
c4e50 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e else{. ulOpen
c4e60 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 Mode |= OPEN_ACC
c4e70 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ESS_READONLY;.
c4e80 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 OSTRACE1( "OPE
c4e90 4e 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 N read only\n" )
c4ea0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 ;. }.. if( fla
c4eb0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
c4ec0 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 75 _CREATE ){. u
c4ed0 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 lOpenFlags |= OP
c4ee0 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 EN_ACTION_OPEN_I
c4ef0 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f F_EXISTS | OPEN_
c4f00 41 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49 46 ACTION_CREATE_IF
c4f10 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 _NEW;. OSTRAC
c4f20 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 6e E1( "OPEN open n
c4f30 65 77 2f 63 72 65 61 74 65 5c 6e 22 20 29 3b 0a ew/create\n" );.
c4f40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f }else{. ulO
c4f50 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e penFlags |= OPEN
c4f60 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f _ACTION_OPEN_IF_
c4f70 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43 EXISTS | OPEN_AC
c4f80 54 49 4f 4e 5f 46 41 49 4c 5f 49 46 5f 4e 45 57 TION_FAIL_IF_NEW
c4f90 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 ;. OSTRACE1(
c4fa0 22 4f 50 45 4e 20 6f 70 65 6e 20 65 78 69 73 74 "OPEN open exist
c4fb0 69 6e 67 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 ing\n" );. }..
c4fc0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
c4fd0 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
c4fe0 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f ){. ulOpenMo
c4ff0 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 de |= OPEN_SHARE
c5000 5f 44 45 4e 59 4e 4f 4e 45 3b 0a 20 20 20 20 4f _DENYNONE;. O
c5010 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 STRACE1( "OPEN s
c5020 68 61 72 65 20 72 65 61 64 2f 77 72 69 74 65 5c hare read/write\
c5030 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 n" );. }else{.
c5040 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d ulOpenMode |=
c5050 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 OPEN_SHARE_DENY
c5060 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41 WRITE;. OSTRA
c5070 43 45 31 28 20 22 4f 50 45 4e 20 73 68 61 72 65 CE1( "OPEN share
c5080 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b read only\n" );
c5090 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 . }.. if( flag
c50a0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
c50b0 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b DELETEONCLOSE ){
c50c0 0a 20 20 20 20 63 68 61 72 20 70 61 74 68 55 74 . char pathUt
c50d0 66 38 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a f8[CCHMAXPATH];.
c50e0 23 69 66 64 65 66 20 4e 44 45 42 55 47 20 2f 2a #ifdef NDEBUG /*
c50f0 20 77 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20 when debugging
c5100 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 we want to make
c5110 73 75 72 65 20 69 74 20 69 73 20 64 65 6c 65 74 sure it is delet
c5120 65 64 20 2a 2f 0a 20 20 20 20 75 6c 46 69 6c 65 ed */. ulFile
c5130 41 74 74 72 69 62 75 74 65 20 3d 20 46 49 4c 45 Attribute = FILE
c5140 5f 48 49 44 44 45 4e 3b 0a 23 65 6e 64 69 66 0a _HIDDEN;.#endif.
c5150 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e os2FullPathn
c5160 61 6d 65 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 ame( pVfs, zName
c5170 2c 20 43 43 48 4d 41 58 50 41 54 48 2c 20 70 61 , CCHMAXPATH, pa
c5180 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20 70 46 thUtf8 );. pF
c5190 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d ile->pathToDel =
c51a0 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 convertUtf8Path
c51b0 54 6f 43 70 28 20 70 61 74 68 55 74 66 38 20 29 ToCp( pathUtf8 )
c51c0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 ;. OSTRACE1(
c51d0 22 4f 50 45 4e 20 68 69 64 64 65 6e 2f 64 65 6c "OPEN hidden/del
c51e0 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 66 69 6c ete on close fil
c51f0 65 20 61 74 74 72 69 62 75 74 65 73 5c 6e 22 20 e attributes\n"
c5200 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
c5210 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c pFile->pathToDel
c5220 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 4f 53 54 = NULL;. OST
c5230 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6e 6f 72 RACE1( "OPEN nor
c5240 6d 61 6c 20 66 69 6c 65 20 61 74 74 72 69 62 75 mal file attribu
c5250 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 te\n" );. }..
c5260 2f 2a 20 61 6c 77 61 79 73 20 6f 70 65 6e 20 69 /* always open i
c5270 6e 20 72 61 6e 64 6f 6d 20 61 63 63 65 73 73 20 n random access
c5280 6d 6f 64 65 20 66 6f 72 20 70 6f 73 73 69 62 6c mode for possibl
c5290 79 20 62 65 74 74 65 72 20 73 70 65 65 64 20 2a y better speed *
c52a0 2f 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c /. ulOpenMode |
c52b0 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 41 4e = OPEN_FLAGS_RAN
c52c0 44 4f 4d 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 DOM;. ulOpenMod
c52d0 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f e |= OPEN_FLAGS_
c52e0 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 3b 0a 20 FAIL_ON_ERROR;.
c52f0 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f ulOpenMode |= O
c5300 50 45 4e 5f 46 4c 41 47 53 5f 4e 4f 49 4e 48 45 PEN_FLAGS_NOINHE
c5310 52 49 54 3b 0a 0a 20 20 7a 4e 61 6d 65 43 70 20 RIT;.. zNameCp
c5320 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 = convertUtf8Pat
c5330 68 54 6f 43 70 28 20 7a 4e 61 6d 65 20 29 3b 0a hToCp( zName );.
c5340 20 20 72 63 20 3d 20 44 6f 73 4f 70 65 6e 28 20 rc = DosOpen(
c5350 28 50 53 5a 29 7a 4e 61 6d 65 43 70 2c 0a 20 20 (PSZ)zNameCp,.
c5360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 68 &h
c5370 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
c5380 20 20 26 75 6c 41 63 74 69 6f 6e 2c 0a 20 20 20 &ulAction,.
c5390 20 20 20 20 20 20 20 20 20 20 20 20 20 30 4c 2c 0L,
c53a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c53b0 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 ulFileAttribute
c53c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
c53d0 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 0a 20 ulOpenFlags,.
c53e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 u
c53f0 6c 4f 70 65 6e 4d 6f 64 65 2c 0a 20 20 20 20 20 lOpenMode,.
c5400 20 20 20 20 20 20 20 20 20 20 20 28 50 45 41 4f (PEAO
c5410 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 20 66 72 65 P2)NULL );. fre
c5420 65 28 20 7a 4e 61 6d 65 43 70 20 29 3b 0a 20 20 e( zNameCp );.
c5430 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 if( rc != NO_ERR
c5440 4f 52 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 OR ){. OSTRAC
c5450 45 37 28 20 22 4f 50 45 4e 20 49 6e 76 61 6c 69 E7( "OPEN Invali
c5460 64 20 68 61 6e 64 6c 65 20 72 63 3d 25 64 3a 20 d handle rc=%d:
c5470 7a 4e 61 6d 65 3d 25 73 2c 20 75 6c 41 63 74 69 zName=%s, ulActi
c5480 6f 6e 3d 25 23 6c 78 2c 20 75 6c 41 74 74 72 3d on=%#lx, ulAttr=
c5490 25 23 6c 78 2c 20 75 6c 46 6c 61 67 73 3d 25 23 %#lx, ulFlags=%#
c54a0 6c 78 2c 20 75 6c 4d 6f 64 65 3d 25 23 6c 78 5c lx, ulMode=%#lx\
c54b0 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
c54c0 20 20 72 63 2c 20 7a 4e 61 6d 65 2c 20 75 6c 41 rc, zName, ulA
c54d0 63 74 69 6f 6e 2c 20 75 6c 46 69 6c 65 41 74 74 ction, ulFileAtt
c54e0 72 69 62 75 74 65 2c 20 75 6c 4f 70 65 6e 46 6c ribute, ulOpenFl
c54f0 61 67 73 2c 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 ags, ulOpenMode
c5500 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 );. if( pFile
c5510 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 0a 20 20 ->pathToDel ).
c5520 20 20 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d free( pFile-
c5530 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 >pathToDel );.
c5540 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 pFile->pathToD
c5550 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 el = NULL;. i
c5560 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
c5570 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
c5580 20 29 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 ){. OSTRAC
c5590 45 32 28 20 22 4f 50 45 4e 20 25 64 20 49 6e 76 E2( "OPEN %d Inv
c55a0 61 6c 69 64 20 68 61 6e 64 6c 65 5c 6e 22 2c 20 alid handle\n",
c55b0 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 ((flags | SQLITE
c55c0 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 _OPEN_READONLY)
c55d0 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & ~SQLITE_OPEN_R
c55e0 45 41 44 57 52 49 54 45 29 20 29 3b 0a 20 20 20 EADWRITE) );.
c55f0 20 20 20 72 65 74 75 72 6e 20 6f 73 32 4f 70 65 return os2Ope
c5600 6e 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 n( pVfs, zName,
c5610 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 id,.
c5620 20 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 ((flag
c5630 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s | SQLITE_OPEN_
c5640 52 45 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c READONLY) & ~SQL
c5650 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
c5660 54 45 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 TE),.
c5670 20 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 46 pOutF
c5680 6c 61 67 73 20 29 3b 0a 20 20 20 20 7d 65 6c 73 lags );. }els
c5690 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
c56a0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
c56b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
c56c0 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 ( pOutFlags ){.
c56d0 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 *pOutFlags =
c56e0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
c56f0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3f 20 PEN_READWRITE ?
c5700 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
c5710 57 52 49 54 45 20 3a 20 53 51 4c 49 54 45 5f 4f WRITE : SQLITE_O
c5720 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 PEN_READONLY;.
c5730 7d 0a 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 }.. pFile->pMet
c5740 68 6f 64 20 3d 20 26 6f 73 32 49 6f 4d 65 74 68 hod = &os2IoMeth
c5750 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d od;. pFile->h =
c5760 20 68 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 h;. OpenCounte
c5770 72 28 2b 31 29 3b 0a 20 20 4f 53 54 52 41 43 45 r(+1);. OSTRACE
c5780 33 28 20 22 4f 50 45 4e 20 25 64 20 70 4f 75 74 3( "OPEN %d pOut
c5790 46 6c 61 67 73 3d 25 64 5c 6e 22 2c 20 70 46 69 Flags=%d\n", pFi
c57a0 6c 65 2d 3e 68 2c 20 70 4f 75 74 46 6c 61 67 73 le->h, pOutFlags
c57b0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c );. return SQL
c57c0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
c57d0 20 44 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65 Delete the name
c57e0 64 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 d file..*/.stati
c57f0 63 20 69 6e 74 20 6f 73 32 44 65 6c 65 74 65 28 c int os2Delete(
c5800 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
c5810 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 pVfs,
c5820 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 /* Not
c5830 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a used on os2 */.
c5840 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
c5850 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 20 20 20 ilename,
c5860 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
c5870 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 of file to dele
c5880 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 te */. int sync
c5890 44 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20 Dir
c58a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c58b0 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 * Not used on os
c58c0 32 20 2a 2f 0a 29 7b 0a 20 20 41 50 49 52 45 54 2 */.){. APIRET
c58d0 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a rc = NO_ERROR;.
c58e0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d char *zFilenam
c58f0 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 eCp = convertUtf
c5900 38 50 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 8PathToCp( zFile
c5910 6e 61 6d 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 name );. Simula
c5920 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
c5930 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 n SQLITE_IOERR_D
c5940 45 4c 45 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 ELETE );. rc =
c5950 44 6f 73 44 65 6c 65 74 65 28 20 28 50 53 5a 29 DosDelete( (PSZ)
c5960 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 zFilenameCp );.
c5970 20 66 72 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 free( zFilename
c5980 43 70 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 Cp );. OSTRACE2
c5990 28 20 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 ( "DELETE \"%s\"
c59a0 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 20 29 \n", zFilename )
c59b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d ;. return rc ==
c59c0 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 NO_ERROR ? SQLI
c59d0 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 TE_OK : SQLITE_I
c59e0 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a OERR_DELETE;.}..
c59f0 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 /*.** Check the
c5a00 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 existance and st
c5a10 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a atus of a file..
c5a20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c5a30 32 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 2Access(. sqlit
c5a40 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
c5a50 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 /* Not used
c5a60 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e on os2 */. con
c5a70 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
c5a80 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f me, /* Name o
c5a90 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 f file to check
c5aa0 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 */. int flags,
c5ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c5ac0 2a 20 54 79 70 65 20 6f 66 20 74 65 73 74 20 74 * Type of test t
c5ad0 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66 o make on this f
c5ae0 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f ile */. int *pO
c5af0 75 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ut
c5b00 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 /* Write resu
c5b10 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 lts here */.){.
c5b20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 FILESTATUS3 fst
c5b30 73 33 43 6f 6e 66 69 67 49 6e 66 6f 3b 0a 20 20 s3ConfigInfo;.
c5b40 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 APIRET rc = NO_E
c5b50 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 RROR;. char *zF
c5b60 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 ilenameCp = conv
c5b70 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 ertUtf8PathToCp(
c5b80 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 0a 20 zFilename );..
c5b90 20 6d 65 6d 73 65 74 28 20 26 66 73 74 73 33 43 memset( &fsts3C
c5ba0 6f 6e 66 69 67 49 6e 66 6f 2c 20 30 2c 20 73 69 onfigInfo, 0, si
c5bb0 7a 65 6f 66 28 66 73 74 73 33 43 6f 6e 66 69 67 zeof(fsts3Config
c5bc0 49 6e 66 6f 29 20 29 3b 0a 20 20 72 63 20 3d 20 Info) );. rc =
c5bd0 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f DosQueryPathInfo
c5be0 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 ( (PSZ)zFilename
c5bf0 43 70 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44 Cp, FIL_STANDARD
c5c00 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
c5c10 20 20 20 20 20 20 20 20 20 20 20 26 66 73 74 73 &fsts
c5c20 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 69 7a 3ConfigInfo, siz
c5c30 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 eof(FILESTATUS3)
c5c40 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c );. free( zFil
c5c50 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 enameCp );. OST
c5c60 52 41 43 45 34 28 20 22 41 43 43 45 53 53 20 66 RACE4( "ACCESS f
c5c70 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 sts3ConfigInfo.a
c5c80 74 74 72 46 69 6c 65 3d 25 64 20 66 6c 61 67 73 ttrFile=%d flags
c5c90 3d 25 64 20 72 63 3d 25 64 5c 6e 22 2c 0a 20 20 =%d rc=%d\n",.
c5ca0 20 20 20 20 20 20 20 20 20 20 66 73 74 73 33 43 fsts3C
c5cb0 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 onfigInfo.attrFi
c5cc0 6c 65 2c 20 66 6c 61 67 73 2c 20 72 63 20 29 3b le, flags, rc );
c5cd0 0a 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 . switch( flags
c5ce0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
c5cf0 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a ITE_ACCESS_READ:
c5d00 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
c5d10 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a _ACCESS_EXISTS:.
c5d20 20 20 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d rc = (rc =
c5d30 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 = NO_ERROR);.
c5d40 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 41 43 OSTRACE3( "AC
c5d50 43 45 53 53 20 25 73 20 61 63 63 65 73 73 20 6f CESS %s access o
c5d60 66 20 72 65 61 64 20 61 6e 64 20 65 78 69 73 74 f read and exist
c5d70 73 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 s rc=%d\n", zFi
c5d80 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 lename, rc );.
c5d90 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 break;. c
c5da0 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ase SQLITE_ACCES
c5db0 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 S_READWRITE:.
c5dc0 20 20 20 72 63 20 3d 20 28 72 63 20 3d 3d 20 4e rc = (rc == N
c5dd0 4f 5f 45 52 52 4f 52 29 20 26 26 20 28 20 28 66 O_ERROR) && ( (f
c5de0 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 sts3ConfigInfo.a
c5df0 74 74 72 46 69 6c 65 20 26 20 46 49 4c 45 5f 52 ttrFile & FILE_R
c5e00 45 41 44 4f 4e 4c 59 29 20 3d 3d 20 30 20 29 3b EADONLY) == 0 );
c5e10 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 . OSTRACE3(
c5e20 20 22 41 43 43 45 53 53 20 25 73 20 61 63 63 65 "ACCESS %s acce
c5e30 73 73 20 6f 66 20 72 65 61 64 2f 77 72 69 74 65 ss of read/write
c5e40 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c rc=%d\n", zFil
c5e50 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20 ename, rc );.
c5e60 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 break;. de
c5e70 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 fault:. ass
c5e80 65 72 74 28 20 21 22 49 6e 76 61 6c 69 64 20 66 ert( !"Invalid f
c5e90 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22 20 29 lags argument" )
c5ea0 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d 20 ;. }. *pOut =
c5eb0 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c rc;. return SQL
c5ec0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e ITE_OK;.}...#ifn
c5ed0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
c5ee0 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f LOAD_EXTENSION./
c5ef0 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 *.** Interfaces
c5f00 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 for opening a sh
c5f10 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 ared library, fi
c5f20 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e nding entry poin
c5f30 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 ts.** within the
c5f40 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
c5f50 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 and closing the
c5f60 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e shared library.
c5f70 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 .*/./*.** Interf
c5f80 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 aces for opening
c5f90 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 a shared librar
c5fa0 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 y, finding entry
c5fb0 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 points.** withi
c5fc0 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 n the shared lib
c5fd0 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e rary, and closin
c5fe0 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 g the shared lib
c5ff0 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rary..*/.static
c6000 76 6f 69 64 20 2a 6f 73 32 44 6c 4f 70 65 6e 28 void *os2DlOpen(
c6010 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
c6020 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a s, const char *z
c6030 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 43 48 Filename){. UCH
c6040 41 52 20 6c 6f 61 64 45 72 72 5b 32 35 36 5d 3b AR loadErr[256];
c6050 0a 20 20 48 4d 4f 44 55 4c 45 20 68 6d 6f 64 3b . HMODULE hmod;
c6060 0a 20 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20 . APIRET rc;.
c6070 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 char *zFilenameC
c6080 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 p = convertUtf8P
c6090 61 74 68 54 6f 43 70 28 7a 46 69 6c 65 6e 61 6d athToCp(zFilenam
c60a0 65 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4c 6f e);. rc = DosLo
c60b0 61 64 4d 6f 64 75 6c 65 28 28 50 53 5a 29 6c 6f adModule((PSZ)lo
c60c0 61 64 45 72 72 2c 20 73 69 7a 65 6f 66 28 6c 6f adErr, sizeof(lo
c60d0 61 64 45 72 72 29 2c 20 7a 46 69 6c 65 6e 61 6d adErr), zFilenam
c60e0 65 43 70 2c 20 26 68 6d 6f 64 29 3b 0a 20 20 66 eCp, &hmod);. f
c60f0 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 43 70 29 ree(zFilenameCp)
c6100 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d ;. return rc !=
c6110 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 NO_ERROR ? 0 :
c6120 28 76 6f 69 64 2a 29 68 6d 6f 64 3b 0a 7d 0a 2f (void*)hmod;.}./
c6130 2a 0a 2a 2a 20 41 20 6e 6f 2d 6f 70 20 73 69 6e *.** A no-op sin
c6140 63 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 ce the error cod
c6150 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e e is returned on
c6160 20 74 68 65 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 the DosLoadModu
c6170 6c 65 20 63 61 6c 6c 2e 0a 2a 2a 20 6f 73 32 44 le call..** os2D
c6180 6c 6f 70 65 6e 20 72 65 74 75 72 6e 73 20 7a 65 lopen returns ze
c6190 72 6f 20 69 66 20 44 6f 73 4c 6f 61 64 4d 6f 64 ro if DosLoadMod
c61a0 75 6c 65 20 69 73 20 6e 6f 74 20 73 75 63 63 65 ule is not succe
c61b0 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 ssful..*/.static
c61c0 20 76 6f 69 64 20 6f 73 32 44 6c 45 72 72 6f 72 void os2DlError
c61d0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
c61e0 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 fs, int nBuf, ch
c61f0 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 2f 2a ar *zBufOut){./*
c6200 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73 74 61 74 no-op */.}.stat
c6210 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 53 79 ic void *os2DlSy
c6220 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 m(sqlite3_vfs *p
c6230 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 Vfs, void *pHand
c6240 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a le, const char *
c6250 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 50 46 4e 20 zSymbol){. PFN
c6260 70 66 6e 3b 0a 20 20 41 50 49 52 45 54 20 72 63 pfn;. APIRET rc
c6270 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 ;. rc = DosQuer
c6280 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 yProcAddr((HMODU
c6290 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 LE)pHandle, 0L,
c62a0 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a zSymbol, &pfn);.
c62b0 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 if( rc != NO_E
c62c0 52 52 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20 69 RROR ){. /* i
c62d0 66 20 74 68 65 20 73 79 6d 62 6f 6c 20 69 74 73 f the symbol its
c62e0 65 6c 66 20 77 61 73 20 6e 6f 74 20 66 6f 75 6e elf was not foun
c62f0 64 2c 20 73 65 61 72 63 68 20 61 67 61 69 6e 20 d, search again
c6300 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 for the same.
c6310 20 20 2a 20 73 79 6d 62 6f 6c 20 77 69 74 68 20 * symbol with
c6320 61 6e 20 65 78 74 72 61 20 75 6e 64 65 72 73 63 an extra undersc
c6330 6f 72 65 2c 20 74 68 61 74 20 6d 69 67 68 74 20 ore, that might
c6340 62 65 20 6e 65 65 64 65 64 20 64 65 70 65 6e 64 be needed depend
c6350 69 6e 67 0a 20 20 20 20 20 2a 20 6f 6e 20 74 68 ing. * on th
c6360 65 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e e calling conven
c6370 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72 tion */. char
c6380 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d 20 3d _zSymbol[256] =
c6390 20 22 5f 22 3b 0a 20 20 20 20 73 74 72 6e 63 61 "_";. strnca
c63a0 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53 79 6d t(_zSymbol, zSym
c63b0 62 6f 6c 2c 20 32 35 35 29 3b 0a 20 20 20 20 72 bol, 255);. r
c63c0 63 20 3d 20 44 6f 73 51 75 65 72 79 50 72 6f 63 c = DosQueryProc
c63d0 41 64 64 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 Addr((HMODULE)pH
c63e0 61 6e 64 6c 65 2c 20 30 4c 2c 20 5f 7a 53 79 6d andle, 0L, _zSym
c63f0 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 7d 0a bol, &pfn);. }.
c6400 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d 20 4e return rc != N
c6410 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 76 O_ERROR ? 0 : (v
c6420 6f 69 64 2a 29 70 66 6e 3b 0a 7d 0a 73 74 61 74 oid*)pfn;.}.stat
c6430 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 43 6c 6f ic void os2DlClo
c6440 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a se(sqlite3_vfs *
c6450 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e pVfs, void *pHan
c6460 64 6c 65 29 7b 0a 20 20 44 6f 73 46 72 65 65 4d dle){. DosFreeM
c6470 6f 64 75 6c 65 28 28 48 4d 4f 44 55 4c 45 29 70 odule((HMODULE)p
c6480 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 Handle);.}.#else
c6490 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d /* if SQLITE_OM
c64a0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
c64b0 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f N is defined: */
c64c0 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c . #define os2Dl
c64d0 4f 70 65 6e 20 30 0a 20 20 23 64 65 66 69 6e 65 Open 0. #define
c64e0 20 6f 73 32 44 6c 45 72 72 6f 72 20 30 0a 20 20 os2DlError 0.
c64f0 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 53 79 6d #define os2DlSym
c6500 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 0. #define os2
c6510 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 DlClose 0.#endif
c6520 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 .../*.** Write u
c6530 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20 p to nBuf bytes
c6540 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e of randomness in
c6550 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 to zBuf..*/.stat
c6560 69 63 20 69 6e 74 20 6f 73 32 52 61 6e 64 6f 6d ic int os2Random
c6570 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 ness(sqlite3_vfs
c6580 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 *pVfs, int nBuf
c6590 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a , char *zBuf ){.
c65a0 20 20 55 4c 4f 4e 47 20 73 69 7a 65 6f 66 55 4c ULONG sizeofUL
c65b0 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 55 4c 4f ong = sizeof(ULO
c65c0 4e 47 29 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 NG);. int n = 0
c65d0 3b 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44 ;. if( sizeof(D
c65e0 41 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 ATETIME) <= nBuf
c65f0 20 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 - n ){. DATE
c6600 54 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 TIME x;. DosG
c6610 65 74 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a etDateTime(&x);.
c6620 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 memcpy(&zBuf
c6630 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 [n], &x, sizeof(
c6640 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 x));. n += si
c6650 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 zeof(x);. }..
c6660 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 if( sizeofULong
c6670 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 <= nBuf - n ){.
c6680 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 PPIB ppib;.
c6690 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 DosGetInfoBloc
c66a0 6b 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b ks(NULL, &ppib);
c66b0 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 . memcpy(&zBu
c66c0 66 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 f[n], &ppib->pib
c66d0 5f 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c _ulpid, sizeofUL
c66e0 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 ong);. n += s
c66f0 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a izeofULong;. }.
c6700 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f . if( sizeofULo
c6710 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 ng <= nBuf - n )
c6720 7b 0a 20 20 20 20 50 54 49 42 20 70 74 69 62 3b {. PTIB ptib;
c6730 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 . DosGetInfoB
c6740 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c locks(&ptib, NUL
c6750 4c 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 L);. memcpy(&
c6760 7a 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e zBuf[n], &ptib->
c6770 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f tib_ptib2->tib2_
c6780 75 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f ultid, sizeofULo
c6790 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 ng);. n += si
c67a0 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a zeofULong;. }..
c67b0 20 20 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c /* if we still
c67c0 20 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 haven't filled
c67d0 74 68 65 20 62 75 66 66 65 72 20 79 65 74 20 74 the buffer yet t
c67e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c he following wil
c67f0 6c 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 l */. /* grab e
c6800 76 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 verything once i
c6810 6e 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 nstead of making
c6820 20 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 several calls f
c6830 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d or a single item
c6840 20 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 */. if( sizeof
c6850 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 ULong <= nBuf -
c6860 6e 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 n ){. ULONG u
c6870 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 lSysInfo[QSV_MAX
c6880 5d 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 ];. DosQueryS
c6890 79 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d ysInfo(1L, QSV_M
c68a0 41 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 AX, ulSysInfo, s
c68b0 69 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 izeofULong * QSV
c68c0 5f 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 _MAX);.. memc
c68d0 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c py(&zBuf[n], &ul
c68e0 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 SysInfo[QSV_MS_C
c68f0 4f 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f OUNT - 1], sizeo
c6900 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b fULong);. n +
c6910 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a = sizeofULong;..
c6920 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c if( sizeofUL
c6930 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 ong <= nBuf - n
c6940 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 ){. memcpy(
c6950 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 &zBuf[n], &ulSys
c6960 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 Info[QSV_TIMER_I
c6970 4e 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 NTERVAL - 1], si
c6980 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 zeofULong);.
c6990 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f n += sizeofULo
c69a0 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ng;. }. if
c69b0 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d ( sizeofULong <=
c69c0 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 nBuf - n ){.
c69d0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
c69e0 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 n], &ulSysInfo[Q
c69f0 53 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d SV_TIME_LOW - 1]
c6a00 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a , sizeofULong);.
c6a10 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f n += sizeo
c6a20 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 fULong;. }.
c6a30 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e if( sizeofULon
c6a40 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b g <= nBuf - n ){
c6a50 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a . memcpy(&z
c6a60 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e Buf[n], &ulSysIn
c6a70 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 fo[QSV_TIME_HIGH
c6a80 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f - 1], sizeofULo
c6a90 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 ng);. n +=
c6aa0 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 sizeofULong;.
c6ab0 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f }. if( sizeo
c6ac0 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d fULong <= nBuf -
c6ad0 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 n ){. memc
c6ae0 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c py(&zBuf[n], &ul
c6af0 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 SysInfo[QSV_TOTA
c6b00 56 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 VAILMEM - 1], si
c6b10 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 zeofULong);.
c6b20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f n += sizeofULo
c6b30 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 ng;. }. }..
c6b40 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a return n;.}../*
c6b50 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 .** Sleep for a
c6b60 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 little while. R
c6b70 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 eturn the amount
c6b80 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a of time slept..
c6b90 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 ** The argument
c6ba0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
c6bb0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 microseconds we
c6bc0 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a want to sleep..
c6bd0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 ** The return va
c6be0 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 lue is the numbe
c6bf0 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 r of microsecond
c6c00 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 s of sleep actua
c6c10 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 lly.** requested
c6c20 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c from the underl
c6c30 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
c6c40 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 ystem, a number
c6c50 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62 which.** might b
c6c60 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f e greater than o
c6c70 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 61 r equal to the a
c6c80 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 rgument, but not
c6c90 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 less.** than th
c6ca0 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 e argument..*/.s
c6cb0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 6c 65 tatic int os2Sle
c6cc0 65 70 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20 ep( sqlite3_vfs
c6cd0 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f *pVfs, int micro
c6ce0 73 65 63 20 29 7b 0a 20 20 44 6f 73 53 6c 65 65 sec ){. DosSlee
c6cf0 70 28 20 28 6d 69 63 72 6f 73 65 63 2f 31 30 30 p( (microsec/100
c6d00 30 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 0) );. return m
c6d10 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a 0a 2a icrosec;.}../*.*
c6d20 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
c6d30 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74 variable, if set
c6d40 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 to a non-zero v
c6d50 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74 68 alue, becomes th
c6d60 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75 e result.** retu
c6d70 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 rned from sqlite
c6d80 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29 3OsCurrentTime()
c6d90 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 . This is used
c6da0 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a for testing..*/.
c6db0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
c6dc0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
c6dd0 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e t sqlite3_curren
c6de0 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 t_time = 0;.#end
c6df0 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 if../*.** Find t
c6e00 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 he current time
c6e10 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f (in Universal Co
c6e20 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e ordinated Time).
c6e30 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 Write the.** c
c6e40 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 urrent time and
c6e50 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e date as a Julian
c6e60 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f Day number into
c6e70 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 *prNow and.** r
c6e80 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e eturn 0. Return
c6e90 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 1 if the time a
c6ea0 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 nd date cannot b
c6eb0 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 e found..*/.int
c6ec0 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 28 20 os2CurrentTime(
c6ed0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
c6ee0 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 s, double *prNow
c6ef0 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6e 6f 77 ){. double now
c6f00 3b 0a 20 20 53 48 4f 52 54 20 6d 69 6e 75 74 65 ;. SHORT minute
c6f10 3b 20 2f 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 ; /* needs to be
c6f20 20 61 62 6c 65 20 74 6f 20 63 6f 70 65 20 77 69 able to cope wi
c6f30 74 68 20 6e 65 67 61 74 69 76 65 20 74 69 6d 65 th negative time
c6f40 7a 6f 6e 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20 zone offset */.
c6f50 20 55 53 48 4f 52 54 20 73 65 63 6f 6e 64 2c 20 USHORT second,
c6f60 68 6f 75 72 2c 0a 20 20 20 20 20 20 20 20 20 64 hour,. d
c6f70 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 65 61 72 3b ay, month, year;
c6f80 0a 20 20 44 41 54 45 54 49 4d 45 20 64 74 3b 0a . DATETIME dt;.
c6f90 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d 65 DosGetDateTime
c6fa0 28 20 26 64 74 20 29 3b 0a 20 20 73 65 63 6f 6e ( &dt );. secon
c6fb0 64 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 73 d = (USHORT)dt.s
c6fc0 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75 74 65 econds;. minute
c6fd0 20 3d 20 28 53 48 4f 52 54 29 64 74 2e 6d 69 6e = (SHORT)dt.min
c6fe0 75 74 65 73 20 2b 20 64 74 2e 74 69 6d 65 7a 6f utes + dt.timezo
c6ff0 6e 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28 55 53 ne;. hour = (US
c7000 48 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b 0a 20 HORT)dt.hours;.
c7010 20 64 61 79 20 3d 20 28 55 53 48 4f 52 54 29 64 day = (USHORT)d
c7020 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68 20 3d t.day;. month =
c7030 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f 6e 74 (USHORT)dt.mont
c7040 68 3b 0a 20 20 79 65 61 72 20 3d 20 28 55 53 48 h;. year = (USH
c7050 4f 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a 20 20 ORT)dt.year;..
c7060 2f 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e 73 20 /* Calculations
c7070 66 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77 77 2e from http://www.
c7080 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 astro.keele.ac.u
c7090 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 k/~rno/Astronomy
c70a0 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20 20 68 /hjd.html. h
c70b0 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 6f 2e ttp://www.astro.
c70c0 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f keele.ac.uk/~rno
c70d0 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2d 30 /Astronomy/hjd-0
c70e0 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43 61 6c .1.c */. /* Cal
c70f0 63 75 6c 61 74 65 20 74 68 65 20 4a 75 6c 69 61 culate the Julia
c7100 6e 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f 77 20 n days */. now
c7110 3d 20 64 61 79 20 2d 20 33 32 30 37 36 20 2b 0a = day - 32076 +.
c7120 20 20 20 20 31 34 36 31 2a 28 79 65 61 72 20 2b 1461*(year +
c7130 20 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 4800 + (month -
c7140 20 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20 20 20 14)/12)/4 +.
c7150 20 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20 32 20 367*(month - 2
c7160 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 - (month - 14)/1
c7170 32 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20 20 33 2*12)/12 -. 3
c7180 2a 28 28 79 65 61 72 20 2b 20 34 39 30 30 20 2b *((year + 4900 +
c7190 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 (month - 14)/12
c71a0 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f 2a 20 )/100)/4;.. /*
c71b0 41 64 64 20 74 68 65 20 66 72 61 63 74 69 6f 6e Add the fraction
c71c0 61 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73 20 61 al hours, mins a
c71d0 6e 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 nd seconds */.
c71e0 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b 20 31 now += (hour + 1
c71f0 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e 6f 77 2.0)/24.0;. now
c7200 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34 30 2e += minute/1440.
c7210 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65 63 6f 0;. now += seco
c7220 6e 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20 2a 70 nd/86400.0;. *p
c7230 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69 66 64 rNow = now;.#ifd
c7240 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
c7250 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72 if( sqlite3_cur
c7260 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 rent_time ){.
c7270 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65 *prNow = sqlite
c7280 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38 3_current_time/8
c7290 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37 6400.0 + 2440587
c72a0 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 .5;. }.#endif.
c72b0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74 return 0;.}..st
c72c0 61 74 69 63 20 69 6e 74 20 6f 73 32 47 65 74 4c atic int os2GetL
c72d0 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 33 astError(sqlite3
c72e0 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
c72f0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
c7300 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d ){. return 0;.}
c7310 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
c7320 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c ze and deinitial
c7330 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e ize the operatin
c7340 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 g system interfa
c7350 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ce..*/.SQLITE_AP
c7360 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 I int sqlite3_os
c7370 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 73 _init(void){. s
c7380 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 tatic sqlite3_vf
c7390 73 20 6f 73 32 56 66 73 20 3d 20 7b 0a 20 20 20 s os2Vfs = {.
c73a0 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
c73b0 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 /* iVersion
c73c0 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 6f 73 */. sizeof(os
c73d0 32 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 2File), /* szO
c73e0 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 43 43 48 sFile */. CCH
c73f0 4d 41 58 50 41 54 48 2c 20 20 20 20 20 20 20 20 MAXPATH,
c7400 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f /* mxPathname */
c7410 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
c7420 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
c7430 20 2a 2f 0a 20 20 20 20 22 6f 73 32 22 2c 20 20 */. "os2",
c7440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e /* zN
c7450 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ame */. 0,
c7460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c7470 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 0a 20 20 pAppData */..
c7480 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20 20 20 20 os2Open,
c7490 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f /* xOpen */
c74a0 0a 20 20 20 20 6f 73 32 44 65 6c 65 74 65 2c 20 . os2Delete,
c74b0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 /* xDele
c74c0 74 65 20 2a 2f 0a 20 20 20 20 6f 73 32 41 63 63 te */. os2Acc
c74d0 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ess, /*
c74e0 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 6f xAccess */. o
c74f0 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 s2FullPathname,
c7500 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 /* xFullPathna
c7510 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 4f me */. os2DlO
c7520 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 pen, /*
c7530 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 6f xDlOpen */. o
c7540 73 32 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 s2DlError,
c7550 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f /* xDlError */
c7560 0a 20 20 20 20 6f 73 32 44 6c 53 79 6d 2c 20 20 . os2DlSym,
c7570 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 /* xDlSy
c7580 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 43 6c m */. os2DlCl
c7590 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 ose, /* x
c75a0 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 6f DlClose */. o
c75b0 73 32 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 s2Randomness,
c75c0 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 /* xRandomness
c75d0 20 2a 2f 0a 20 20 20 20 6f 73 32 53 6c 65 65 70 */. os2Sleep
c75e0 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 , /* xS
c75f0 6c 65 65 70 20 2a 2f 0a 20 20 20 20 6f 73 32 43 leep */. os2C
c7600 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 2f urrentTime, /
c7610 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a * xCurrentTime *
c7620 2f 0a 20 20 20 20 6f 73 32 47 65 74 4c 61 73 74 /. os2GetLast
c7630 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65 74 Error /* xGet
c7640 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20 7d LastError */. }
c7650 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f ;. sqlite3_vfs_
c7660 72 65 67 69 73 74 65 72 28 26 6f 73 32 56 66 73 register(&os2Vfs
c7670 2c 20 31 29 3b 0a 20 20 69 6e 69 74 55 63 6f 6e , 1);. initUcon
c7680 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20 72 65 vObjects();. re
c7690 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c76a0 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
c76b0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
c76c0 76 6f 69 64 29 7b 0a 20 20 66 72 65 65 55 63 6f void){. freeUco
c76d0 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20 72 nvObjects();. r
c76e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
c76f0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
c7700 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 2a 2f 0a 0a LITE_OS_OS2 */..
c7710 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
c7720 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 End of os_os2.c
c7730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
c7760 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
c7770 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 75 6e Begin file os_un
c7780 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ix.c ***********
c7790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c77a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
c77b0 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 /*.** 2004 May 2
c77c0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
c77d0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
c77e0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
c77f0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
c7800 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
c7810 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
c7820 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
c7830 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
c7840 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
c7850 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
c7860 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
c7870 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
c7880 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
c7890 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
c78a0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
c78b0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
c78c0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
c78d0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
c78e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c78f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7920 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
c7930 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
c7940 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63 ode that is spec
c7950 69 66 69 63 20 74 6f 20 55 6e 69 78 20 73 79 73 ific to Unix sys
c7960 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a tems..**.** $Id:
c7970 20 6f 73 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 32 os_unix.c,v 1.2
c7980 31 36 20 32 30 30 38 2f 31 31 2f 31 39 20 31 36 16 2008/11/19 16
c7990 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 :52:44 danielk19
c79a0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 20 77 Exp $.*/.#if
c79b0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20 SQLITE_OS_UNIX
c79c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
c79d0 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 64 his file is used
c79e0 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f on unix only */
c79f0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 ../*.** If SQLIT
c7a00 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
c7a10 5f 53 54 59 4c 45 20 69 73 20 64 65 66 69 6e 65 _STYLE is define
c7a20 64 20 61 6e 64 20 69 73 20 6e 6f 6e 2d 7a 65 72 d and is non-zer
c7a30 6f 2c 20 74 68 65 6e 20 73 65 76 65 72 61 6c 0a o, then several.
c7a40 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c ** alternative l
c7a50 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 ocking implement
c7a60 61 74 69 6f 6e 73 20 61 72 65 20 70 72 6f 76 69 ations are provi
c7a70 64 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 ded:.**.** * P
c7a80 4f 53 49 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68 OSIX locking (th
c7a90 65 20 64 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20 e default),.**
c7aa0 20 2a 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2a * No locking,.*
c7ab0 2a 20 20 20 2a 20 44 6f 74 2d 66 69 6c 65 20 6c * * Dot-file l
c7ac0 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 2a 20 66 ocking,.** * f
c7ad0 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a lock() locking,.
c7ae0 2a 2a 20 20 20 2a 20 41 46 50 20 6c 6f 63 6b 69 ** * AFP locki
c7af0 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a ng (OSX only),.*
c7b00 2a 20 20 20 2a 20 4e 61 6d 65 64 20 50 4f 53 49 * * Named POSI
c7b10 58 20 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 X semaphores (VX
c7b20 57 6f 72 6b 73 20 6f 6e 6c 79 29 2e 0a 2a 2a 0a Works only)..**.
c7b30 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ** SQLITE_ENABLE
c7b40 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 6f _LOCKING_STYLE o
c7b50 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 4d nly works on a M
c7b60 61 63 2e 20 49 74 20 69 73 20 74 75 72 6e 65 64 ac. It is turned
c7b70 20 6f 6e 20 62 79 0a 2a 2a 20 64 65 66 61 75 6c on by.** defaul
c7b80 74 20 6f 6e 20 61 20 4d 61 63 20 61 6e 64 20 64 t on a Mac and d
c7b90 69 73 61 62 6c 65 64 20 6f 6e 20 61 6c 6c 20 6f isabled on all o
c7ba0 74 68 65 72 20 70 6f 73 69 78 20 70 6c 61 74 66 ther posix platf
c7bb0 6f 72 6d 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 orms..*/.#if !de
c7bc0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 fined(SQLITE_ENA
c7bd0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
c7be0 45 29 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 E).# if defined
c7bf0 28 5f 5f 44 41 52 57 49 4e 5f 5f 29 0a 23 20 20 (__DARWIN__).#
c7c00 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
c7c10 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
c7c20 54 59 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23 TYLE 1.# else.#
c7c30 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
c7c40 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
c7c50 5f 53 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 69 _STYLE 0.# endi
c7c60 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 f.#endif../*.**
c7c70 44 65 66 69 6e 65 20 74 68 65 20 49 53 5f 56 58 Define the IS_VX
c7c80 57 4f 52 4b 53 20 70 72 65 2d 70 72 6f 63 65 73 WORKS pre-proces
c7c90 73 6f 72 20 6d 61 63 72 6f 20 74 6f 20 31 20 69 sor macro to 1 i
c7ca0 66 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 0a 2a f building on .*
c7cb0 2a 20 76 78 77 6f 72 6b 73 2c 20 6f 72 20 30 20 * vxworks, or 0
c7cc0 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 23 69 otherwise..*/.#i
c7cd0 66 20 64 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f f defined(__RTP_
c7ce0 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57 _) || defined(_W
c7cf0 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 20 64 65 RS_KERNEL).# de
c7d00 66 69 6e 65 20 49 53 5f 56 58 57 4f 52 4b 53 20 fine IS_VXWORKS
c7d10 31 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 1.#else.# defin
c7d20 65 20 49 53 5f 56 58 57 4f 52 4b 53 20 30 0a 23 e IS_VXWORKS 0.#
c7d30 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
c7d40 73 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 se #defines shou
c7d50 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 ld enable >2GB f
c7d60 69 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 ile support on P
c7d70 6f 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 osix if the.** u
c7d80 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 nderlying operat
c7d90 69 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f ing system suppo
c7da0 72 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 rts it. If the
c7db0 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 OS lacks.** larg
c7dc0 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 e file support,
c7dd0 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20 these should be
c7de0 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 no-ops..**.** La
c7df0 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 rge file support
c7e00 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 can be disabled
c7e10 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c using the -DSQL
c7e20 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 ITE_DISABLE_LFS
c7e30 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 switch.** on the
c7e40 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e compiler comman
c7e50 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 d line. This is
c7e60 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f necessary if yo
c7e70 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a u are compiling.
c7e80 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d ** on a recent m
c7e90 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 achine (ex: RedH
c7ea0 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 at 7.2) but you
c7eb0 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 want your code t
c7ec0 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 o work.** on an
c7ed0 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 older machine (e
c7ee0 78 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 x: RedHat 6.0).
c7ef0 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 If you compile
c7f00 6f 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a on RedHat 7.2.**
c7f10 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 without this op
c7f20 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 tion, LFS is ena
c7f30 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f ble. But LFS do
c7f40 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 es not exist in
c7f50 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e the kernel.** in
c7f60 20 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 RedHat 6.0, so
c7f70 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 the code won't w
c7f80 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 ork. Hence, for
c7f90 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a maximum binary.
c7fa0 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 ** portability y
c7fb0 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c ou should omit L
c7fc0 46 53 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 FS..*/.#ifndef S
c7fd0 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 QLITE_DISABLE_LF
c7fe0 53 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 S.# define _LARG
c7ff0 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 E_FILE 1.#
c8000 20 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 ifndef _FILE_OF
c8010 46 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 FSET_BITS.# de
c8020 66 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 fine _FILE_OFFSE
c8030 54 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 T_BITS 64.# endi
c8040 66 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 f.# define _LARG
c8050 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 EFILE_SOURCE 1.#
c8060 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 endif../*.** sta
c8070 6e 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69 ndard include fi
c8080 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 les..*/.#include
c8090 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 <sys/types.h>.#
c80a0 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 include <sys/sta
c80b0 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 t.h>.#include <f
c80c0 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 cntl.h>.#include
c80d0 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 <unistd.h>.#inc
c80e0 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 lude <sys/time.h
c80f0 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e >.#include <errn
c8100 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 o.h>..#if SQLITE
c8110 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
c8120 53 54 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 STYLE.# include
c8130 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 <sys/ioctl.h>.#
c8140 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 23 20 if IS_VXWORKS.#
c8150 20 64 65 66 69 6e 65 20 6c 73 74 61 74 20 73 74 define lstat st
c8160 61 74 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 at.# include <s
c8170 65 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20 20 69 emaphore.h>.# i
c8180 6e 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73 2e 68 nclude <limits.h
c8190 3e 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e 63 6c >.# else.# incl
c81a0 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 ude <sys/param.h
c81b0 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 >.# include <sy
c81c0 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64 s/mount.h>.# end
c81d0 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c if.#endif /* SQL
c81e0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
c81f0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a NG_STYLE */../*.
c8200 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f 20 ** If we are to
c8210 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c 20 be thread-safe,
c8220 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 72 include the pthr
c8230 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 20 eads header and
c8240 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 define.** the SQ
c8250 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 LITE_UNIX_THREAD
c8260 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 S macro..*/.#if
c8270 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
c8280 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
c8290 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 31 E_UNIX_THREADS 1
c82a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 .#endif../*.** D
c82b0 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f efault permissio
c82c0 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 ns when creating
c82d0 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23 a new file.*/.#
c82e0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
c82f0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
c8300 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 SSIONS.# define
c8310 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
c8320 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20 ILE_PERMISSIONS
c8330 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 0644.#endif../*.
c8340 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70 70 6f ** Maximum suppo
c8350 72 74 65 64 20 70 61 74 68 2d 6c 65 6e 67 74 68 rted path-length
c8360 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 ..*/.#define MAX
c8370 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 0a 0a 0a _PATHNAME 512...
c8380 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e 69 78 46 69 /*.** The unixFi
c8390 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 le structure is
c83a0 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 subclass of sqli
c83b0 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69 te3_file specifi
c83c0 63 20 66 6f 72 20 74 68 65 20 75 6e 69 78 0a 2a c for the unix.*
c83d0 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c 61 * protability la
c83e0 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 yer..*/.typedef
c83f0 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 struct unixFile
c8400 75 6e 69 78 46 69 6c 65 3b 0a 73 74 72 75 63 74 unixFile;.struct
c8410 20 75 6e 69 78 46 69 6c 65 20 7b 0a 20 20 73 71 unixFile {. sq
c8420 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
c8430 20 63 6f 6e 73 74 20 2a 70 4d 65 74 68 6f 64 3b const *pMethod;
c8440 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 /* Always the
c8450 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 23 first entry */.#
c8460 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
c8470 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 6d T. /* In test m
c8480 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68 ode, increase th
c8490 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 73 e size of this s
c84a0 74 72 75 63 74 75 72 65 20 61 20 62 69 74 20 73 tructure a bit s
c84b0 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 20 o that . ** it
c84c0 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
c84d0 68 65 20 73 74 72 75 63 74 20 43 72 61 73 68 46 he struct CrashF
c84e0 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20 74 ile defined in t
c84f0 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63 est6.c.. */. c
c8500 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32 5d har aPadding[32]
c8510 3b 0a 23 65 6e 64 69 66 0a 20 20 73 74 72 75 63 ;.#endif. struc
c8520 74 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e t openCnt *pOpen
c8530 3b 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f ; /* Info abo
c8540 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66 64 27 73 ut all open fd's
c8550 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a on this inode *
c8560 2f 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49 /. struct lockI
c8570 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20 20 2f 2a nfo *pLock; /*
c8580 20 49 6e 66 6f 20 61 62 6f 75 74 20 6c 6f 63 6b Info about lock
c8590 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 s on this inode
c85a0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e */.#if SQLITE_EN
c85b0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
c85c0 4c 45 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 LE. void *locki
c85d0 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 2f ngContext; /
c85e0 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 * Locking style
c85f0 73 70 65 63 69 66 69 63 20 73 74 61 74 65 20 2a specific state *
c8600 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 68 /.#endif. int h
c8610 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c8620 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 /* The file
c8630 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 descriptor */.
c8640 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c unsigned char l
c8650 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 68 ocktype; /* Th
c8660 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 e type of lock h
c8670 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a eld on this fd *
c8680 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 3b 20 20 /. int dirfd;
c8690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c86a0 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 File descriptor
c86b0 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f for the directo
c86c0 72 79 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 ry */.#if SQLITE
c86d0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 70 74 _THREADSAFE. pt
c86e0 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 20 20 hread_t tid;
c86f0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
c8700 68 72 65 61 64 20 74 68 61 74 20 22 6f 77 6e 73 hread that "owns
c8710 22 20 74 68 69 73 20 75 6e 69 78 46 69 6c 65 20 " this unixFile
c8720 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 */.#endif. int
c8730 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 lastErrno;
c8740 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 /* The uni
c8750 78 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 x errno from the
c8760 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 last I/O error
c8770 2a 2f 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b */.#if IS_VXWORK
c8780 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 S. int isDelete
c8790 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
c87a0 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 Delete on close
c87b0 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 63 68 if true */. ch
c87c0 61 72 20 2a 7a 52 65 61 6c 70 61 74 68 3b 0a 23 ar *zRealpath;.#
c87d0 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
c87e0 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 Include code tha
c87f0 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 t is common to a
c8800 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a ll os_*.c files.
c8810 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
c8820 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f ** Include os_co
c8830 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 mmon.h in the mi
c8840 64 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e ddle of os_unix.
c8850 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
c8860 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
c8870 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 ** Begin file os
c8880 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a _common.h ******
c8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c88a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c88b0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 */./*.** 2004 Ma
c88c0 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 22.**.** The a
c88d0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
c88e0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
c88f0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
c8900 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
c8910 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
c8920 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
c8930 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
c8940 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
c8950 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
c8960 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
c8970 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
c8980 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
c8990 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
c89a0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
c89b0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
c89c0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
c89d0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
c89e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c89f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8a20 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
c8a30 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
c8a40 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c s macros and a l
c8a50 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 ittle bit of cod
c8a60 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e e that is common
c8a70 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 to.** all of th
c8a80 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 e platform-speci
c8a90 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e fic files (os_*.
c8aa0 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 c) and is #inclu
c8ab0 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a ded into those.*
c8ac0 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 * files..**.** T
c8ad0 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 his file should
c8ae0 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 be #included by
c8af0 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 the os_*.c files
c8b00 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f only. It is no
c8b10 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 t a.** general p
c8b20 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 urpose header fi
c8b30 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f le..**.** $Id: o
c8b40 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 s_common.h,v 1.3
c8b50 37 20 32 30 30 38 2f 30 35 2f 32 39 20 32 30 3a 7 2008/05/29 20:
c8b60 32 32 3a 33 37 20 73 68 61 6e 65 20 45 78 70 20 22:37 shane Exp
c8b70 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 $.*/.#ifndef _OS
c8b80 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 _COMMON_H_.#defi
c8b90 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f ne _OS_COMMON_H_
c8ba0 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 ../*.** At least
c8bb0 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 two bugs have s
c8bc0 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 lipped in becaus
c8bd0 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 e we changed the
c8be0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a MEMORY_DEBUG.**
c8bf0 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 macro to SQLITE
c8c00 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 _DEBUG and some
c8c10 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 older makefiles
c8c20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 have not yet mad
c8c30 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e e the.** switch.
c8c40 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
c8c50 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 code should catc
c8c60 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 h this problem a
c8c70 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a t compile-time..
c8c80 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 */.#ifdef MEMORY
c8c90 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 _DEBUG.# error "
c8ca0 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 The MEMORY_DEBUG
c8cb0 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 macro is obsole
c8cc0 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f te. Use SQLITE_
c8cd0 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a DEBUG instead.".
c8ce0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20 57 #endif.../*. * W
c8cf0 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 74 68 69 hen testing, thi
c8d00 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c s global variabl
c8d10 65 20 73 74 6f 72 65 73 20 74 68 65 20 6c 6f 63 e stores the loc
c8d20 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a 20 ation of the. *
c8d30 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e 20 pending-byte in
c8d40 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
c8d50 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 e.. */.#ifdef SQ
c8d60 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
c8d70 5f 41 50 49 20 75 6e 73 69 67 6e 65 64 20 69 6e _API unsigned in
c8d80 74 20 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e t sqlite3_pendin
c8d90 67 5f 62 79 74 65 20 3d 20 30 78 34 30 30 30 30 g_byte = 0x40000
c8da0 30 30 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 000;.#endif..#if
c8db0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
c8dc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
c8dd0 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 int sqlite3OSTra
c8de0 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 ce = 0;.#define
c8df0 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 OSTRACE1(X)
c8e00 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
c8e10 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
c8e20 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 DebugPrintf(X).#
c8e30 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 define OSTRACE2(
c8e40 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 X,Y) if( s
c8e50 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
c8e60 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
c8e70 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 tf(X,Y).#define
c8e80 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 OSTRACE3(X,Y,Z)
c8e90 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
c8ea0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
c8eb0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
c8ec0 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Z).#define OSTRA
c8ed0 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 CE4(X,Y,Z,A) i
c8ee0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
c8ef0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
c8f00 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a Printf(X,Y,Z,A).
c8f10 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 #define OSTRACE5
c8f20 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 (X,Y,Z,A,B) if(
c8f30 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
c8f40 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
c8f50 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 ntf(X,Y,Z,A,B).#
c8f60 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
c8f70 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 X,Y,Z,A,B,C) \.
c8f80 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 if(sqlite3OST
c8f90 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 race) sqlite3Deb
c8fa0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
c8fb0 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,B,C).#define OS
c8fc0 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE7(X,Y,Z,A,B
c8fd0 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 ,C,D) \. if(s
c8fe0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 qlite3OSTrace) s
c8ff0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
c9000 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 f(X,Y,Z,A,B,C,D)
c9010 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f .#else.#define O
c9020 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 STRACE1(X).#defi
c9030 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
c9040 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
c9050 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 3(X,Y,Z).#define
c9060 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c OSTRACE4(X,Y,Z,
c9070 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 A).#define OSTRA
c9080 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 CE5(X,Y,Z,A,B).#
c9090 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
c90a0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 X,Y,Z,A,B,C).#de
c90b0 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c fine OSTRACE7(X,
c90c0 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e Y,Z,A,B,C,D).#en
c90d0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f dif../*.** Macro
c90e0 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 s for performanc
c90f0 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d e tracing. Norm
c9100 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e ally turned off.
c9110 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 Only works.**
c9120 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 on i486 hardware
c9130 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
c9140 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 TE_PERFORMANCE_T
c9150 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 RACE../* .** hwt
c9160 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 ime.h contains i
c9170 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 nline assembler
c9180 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 code for impleme
c9190 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 nting .** high-p
c91a0 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e erformance timin
c91b0 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f g routines..*/./
c91c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
c91d0 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 nclude hwtime.h
c91e0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
c91f0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
c9200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
c9210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
c9220 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 egin file hwtime
c9230 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
c9240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
c9260 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 *.** 2008 May 27
c9270 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
c9280 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
c9290 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
c92a0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
c92b0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
c92c0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
c92d0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
c92e0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
c92f0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
c9300 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
c9310 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
c9320 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
c9330 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
c9340 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
c9350 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
c9360 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
c9370 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
c9380 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
c9390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c93a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c93b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c93c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c93d0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
c93e0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e file contains in
c93f0 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f line asm code fo
c9400 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 r retrieving "hi
c9410 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a gh-performance".
c9420 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 ** counters for
c9430 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a x86 class CPUs..
c9440 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d **.** $Id: hwtim
c9450 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 e.h,v 1.3 2008/0
c9460 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 8/01 14:33:15 sh
c9470 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 ane Exp $.*/.#if
c9480 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a ndef _HWTIME_H_.
c9490 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f #define _HWTIME_
c94a0 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f H_../*.** The fo
c94b0 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 llowing routine
c94c0 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 only works on pe
c94d0 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 ntium-class (or
c94e0 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 newer) processor
c94f0 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 s..** It uses th
c9500 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 e RDTSC opcode t
c9510 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 o read the cycle
c9520 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 count value out
c9530 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 of the.** proce
c9540 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 ssor and returns
c9550 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 that value. Th
c9560 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 is can be used f
c9570 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 or high-res.** p
c9580 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 rofiling..*/.#if
c9590 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
c95a0 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
c95b0 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 MSC_VER)) && \.
c95c0 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 (defined(i3
c95d0 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 86) || defined(_
c95e0 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 _i386__) || defi
c95f0 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 ned(_M_IX86))..
c9600 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 #if defined(__G
c9610 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 NUC__).. __inli
c9620 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 ne__ sqlite_uint
c9630 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
c9640 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 (void){. uns
c9650 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 igned int lo, hi
c9660 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f ;. __asm__ _
c9670 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 _volatile__ ("rd
c9680 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 tsc" : "=a" (lo)
c9690 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 , "=d" (hi));.
c96a0 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 return (sqlit
c96b0 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 e_uint64)hi << 3
c96c0 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 2 | lo;. }.. #
c96d0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 elif defined(_MS
c96e0 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c C_VER).. __decl
c96f0 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e spec(naked) __in
c9700 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 line sqlite_uint
c9710 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 64 __cdecl sqlit
c9720 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
c9730 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 __asm {.
c9740 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 rdtsc.
c9750 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 ret ; r
c9760 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 eturn value at E
c9770 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 DX:EAX. }.
c9780 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c }.. #endif..#el
c9790 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
c97a0 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 UC__) && defined
c97b0 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 (__x86_64__))..
c97c0 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
c97d0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
c97e0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
c97f0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
c9800 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f ng val;. __
c9810 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
c9820 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
c9830 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 A" (val));.
c9840 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d return val;. }
c9850 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 . .#elif (define
c9860 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 d(__GNUC__) && d
c9870 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 efined(__ppc__))
c9880 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
c9890 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
c98a0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
c98b0 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
c98c0 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 long long retva
c98d0 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 l;. unsigne
c98e0 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 d long junk;.
c98f0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
c9900 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 atile__ ("\n\.
c9910 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 1:
c9920 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 mftbu %1\n\.
c9930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9940 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 mftb %L0\n\.
c9950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9960 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 mftbu %0\n\.
c9970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9980 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e cmpw %0,%1\n
c9990 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
c99a0 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a bne 1b".
c99b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c99c0 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c : "=r" (retval
c99d0 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b ), "=r" (junk));
c99e0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 . return re
c99f0 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 tval;. }..#else
c9a00 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 .. #error Need
c9a10 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
c9a20 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 f sqlite3Hwtime(
c9a30 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 ) for your platf
c9a40 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 orm... /*. **
c9a50 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f To compile witho
c9a60 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 ut implementing
c9a70 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 sqlite3Hwtime()
c9a80 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 for your platfor
c9a90 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 m,. ** you can
c9aa0 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 remove the above
c9ab0 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 #error and use
c9ac0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
c9ad0 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e ** stub function
c9ae0 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 . You will lose
c9af0 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 timing support
c9b00 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 for many. ** of
c9b10 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 the debugging a
c9b20 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 nd testing utili
c9b30 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f ties, but it sho
c9b40 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 uld at. ** leas
c9b50 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 t compile and ru
c9b60 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 n.. */.SQLITE_P
c9b70 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f RIVATE sqlite_
c9b80 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
c9b90 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 time(void){ retu
c9ba0 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 rn ((sqlite_uint
c9bb0 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 64)0); }..#endif
c9bc0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 ..#endif /* !def
c9bd0 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 ined(_HWTIME_H_)
c9be0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
c9bf0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 **** End of hwti
c9c00 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a me.h ***********
c9c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9c30 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
c9c40 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
c9c50 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
c9c60 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 f in os_common.h
c9c70 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c9c80 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c ***/..static sql
c9c90 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 ite_uint64 g_sta
c9ca0 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 rt;.static sqlit
c9cb0 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 e_uint64 g_elaps
c9cc0 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 ed;.#define TIME
c9cd0 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f R_START g_
c9ce0 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 start=sqlite3Hwt
c9cf0 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 ime().#define TI
c9d00 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 MER_END
c9d10 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 g_elapsed=sqlite
c9d20 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 3Hwtime()-g_star
c9d30 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f t.#define TIMER_
c9d40 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c ELAPSED g_el
c9d50 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 apsed.#else.#def
c9d60 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a ine TIMER_START.
c9d70 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e #define TIMER_EN
c9d80 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f D.#define TIMER_
c9d90 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 ELAPSED ((sq
c9da0 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 lite_uint64)0).#
c9db0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
c9dc0 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 we compile with
c9dd0 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 the SQLITE_TEST
c9de0 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 macro set, then
c9df0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c the following bl
c9e00 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 ock.** of code w
c9e10 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 ill give us the
c9e20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c ability to simul
c9e30 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 ate a disk I/O e
c9e40 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 rror. This.** i
c9e50 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
c9e60 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 ng the I/O recov
c9e70 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 ery logic..*/.#i
c9e80 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
c9e90 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
c9ea0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c9eb0 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 _hit = 0;
c9ec0 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
c9ed0 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f mber of I/O Erro
c9ee0 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 rs */.SQLITE_API
c9ef0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
c9f00 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 error_hardhit =
c9f10 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 0; /* Num
c9f20 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 ber of non-benig
c9f30 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 n errors */.SQLI
c9f40 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c9f50 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
c9f60 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ing = 0;
c9f70 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f /* Count down to
c9f80 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 first I/O error
c9f90 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
c9fa0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
c9fb0 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b ror_persist = 0;
c9fc0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
c9fd0 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 if I/O errors pe
c9fe0 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f rsist */.SQLITE_
c9ff0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
ca000 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 io_error_benign
ca010 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
ca020 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 True if errors a
ca030 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c re benign */.SQL
ca040 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
ca050 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
ca060 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 ding = 0;.SQLITE
ca070 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
ca080 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 _diskfull = 0;.#
ca090 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
ca0a0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 OErrorBenign(X)
ca0b0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
ca0c0 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 _benign=(X).#def
ca0d0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
ca0e0 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 ror(CODE) \. i
ca0f0 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 f( (sqlite3_io_e
ca100 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 rror_persist &&
ca110 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
ca120 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c _hit) \. |
ca130 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 | sqlite3_io_err
ca140 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 or_pending-- ==
ca150 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 1 ) \.
ca160 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 { local_ioe
ca170 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 rr(); CODE; }.st
ca180 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f atic void local_
ca190 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 ioerr(){. IOTRA
ca1a0 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b CE(("IOERR\n"));
ca1b0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 . sqlite3_io_er
ca1c0 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 ror_hit++;. if(
ca1d0 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 !sqlite3_io_err
ca1e0 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 or_benign ) sqli
ca1f0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 te3_io_error_har
ca200 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e dhit++;.}.#defin
ca210 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 e SimulateDiskfu
ca220 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a llError(CODE) \.
ca230 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 if( sqlite3_d
ca240 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 iskfull_pending
ca250 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 ){ \. if( sq
ca260 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
ca270 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c ending == 1 ){ \
ca280 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f . local_io
ca290 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 err(); \.
ca2a0 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
ca2b0 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 = 1; \. s
ca2c0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
ca2d0 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 hit = 1; \.
ca2e0 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d CODE; \. }
ca2f0 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 else{ \. s
ca300 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
ca310 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 pending--; \.
ca320 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 } \. }.#else
ca330 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
ca340 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 eIOErrorBenign(X
ca350 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
ca360 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 teIOError(A).#de
ca370 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 fine SimulateDis
ca380 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 kfullError(A).#e
ca390 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e ndif../*.** When
ca3a0 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 testing, keep a
ca3b0 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 count of the nu
ca3c0 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c mber of open fil
ca3d0 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 es..*/.#ifdef SQ
ca3e0 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
ca3f0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
ca400 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 _open_file_count
ca410 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 = 0;.#define Op
ca420 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 enCounter(X) sq
ca430 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f lite3_open_file_
ca440 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 count+=(X).#else
ca450 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 .#define OpenCou
ca460 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a nter(X).#endif..
ca470 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
ca480 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f ed(_OS_COMMON_H_
ca490 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
ca4a0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f ***** End of os_
ca4b0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
ca4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca4e0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
ca4f0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
ca500 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
ca510 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 ff in os_unix.c
ca520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca530 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 ****/../*.** Def
ca540 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 ine various macr
ca550 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 os that are miss
ca560 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 ing from some sy
ca570 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 stems..*/.#ifnde
ca580 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 f O_LARGEFILE.#
ca590 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 define O_LARGEFI
ca5a0 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 LE 0.#endif.#ifd
ca5b0 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c ef SQLITE_DISABL
ca5c0 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f E_LFS.# undef O_
ca5d0 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 LARGEFILE.# defi
ca5e0 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 ne O_LARGEFILE 0
ca5f0 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
ca600 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 O_NOFOLLOW.# def
ca610 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 ine O_NOFOLLOW 0
ca620 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
ca630 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e O_BINARY.# defin
ca640 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e e O_BINARY 0.#en
ca650 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 dif../*.** The D
ca660 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e JGPP compiler en
ca670 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 vironment looks
ca680 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 mostly like Unix
ca690 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b , but it.** lack
ca6a0 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 s the fcntl() sy
ca6b0 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 stem call. So r
ca6c0 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 edefine fcntl()
ca6d0 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a to be something.
ca6e0 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 ** that always s
ca6f0 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d ucceeds. This m
ca700 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e eans that lockin
ca710 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 g does not occur
ca720 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e under.** DJGPP.
ca730 20 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20 But it is DOS
ca740 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 - what did you e
ca750 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 xpect?.*/.#ifdef
ca760 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 __DJGPP__.# def
ca770 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 ine fcntl(A,B,C)
ca780 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
ca790 20 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 The threadid ma
ca7a0 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 cro resolves to
ca7b0 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 the thread-id or
ca7c0 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 to 0. Used for
ca7d0 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 .** testing and
ca7e0 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a debugging only..
ca7f0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
ca800 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 READSAFE.#define
ca810 20 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61 threadid pthrea
ca820 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 d_self().#else.#
ca830 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 define threadid
ca840 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
ca850 53 65 74 20 6f 72 20 63 68 65 63 6b 20 74 68 65 Set or check the
ca860 20 75 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 69 unixFile.tid fi
ca870 65 6c 64 2e 20 20 54 68 69 73 20 66 69 65 6c 64 eld. This field
ca880 20 69 73 20 73 65 74 20 77 68 65 6e 20 61 6e 20 is set when an
ca890 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66 unixFile.** is f
ca8a0 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c irst opened. Al
ca8b0 6c 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 l subsequent use
ca8c0 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c s of the unixFil
ca8d0 65 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 e verify that th
ca8e0 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 64 e.** same thread
ca8f0 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e is operating on
ca900 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 20 the unixFile.
ca910 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 Some operating s
ca920 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74 ystems do.** not
ca930 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20 allow locks to
ca940 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 be overridden by
ca950 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61 other threads a
ca960 6e 64 20 74 68 61 74 20 72 65 73 74 72 69 63 74 nd that restrict
ca970 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 ion.** means tha
ca980 74 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 t sqlite3* datab
ca990 61 73 65 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e ase handles cann
ca9a0 6f 74 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d ot be moved from
ca9b0 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 one thread.** t
ca9c0 6f 20 61 6e 6f 74 68 65 72 2e 20 20 54 68 69 73 o another. This
ca9d0 20 6c 6f 67 69 63 20 6d 61 6b 65 73 20 73 75 72 logic makes sur
ca9e0 65 20 61 20 75 73 65 72 20 64 6f 65 73 20 6e 6f e a user does no
ca9f0 74 20 74 72 79 20 74 6f 20 64 6f 20 74 68 61 74 t try to do that
caa00 0a 2a 2a 20 62 79 20 6d 69 73 74 61 6b 65 2e 0a .** by mistake..
caa10 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e **.** Version 3.
caa20 33 2e 31 20 28 32 30 30 36 2d 30 31 2d 31 35 29 3.1 (2006-01-15)
caa30 3a 20 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 : unixFile can
caa40 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e be moved from on
caa50 65 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61 e thread to.** a
caa60 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 61 nother as long a
caa70 73 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 s we are running
caa80 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 68 61 on a system tha
caa90 74 20 73 75 70 70 6f 72 74 73 20 74 68 72 65 61 t supports threa
caaa0 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67 ds.** overriding
caab0 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 each others loc
caac0 6b 73 20 28 77 68 69 63 68 20 6e 6f 77 20 74 68 ks (which now th
caad0 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 65 e most common be
caae0 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 66 havior).** or if
caaf0 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 no locks are he
cab00 6c 64 2e 20 20 42 75 74 20 74 68 65 20 75 6e 69 ld. But the uni
cab10 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 6c xFile.pLock fiel
cab20 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a d needs to be.**
cab30 20 72 65 63 6f 6d 70 75 74 65 64 20 62 65 63 61 recomputed beca
cab40 75 73 65 20 69 74 73 20 6b 65 79 20 69 6e 63 6c use its key incl
cab50 75 64 65 73 20 74 68 65 20 74 68 72 65 61 64 2d udes the thread-
cab60 69 64 2e 20 20 53 65 65 20 74 68 65 20 0a 2a 2a id. See the .**
cab70 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 transferOwnersh
cab80 69 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 ip() function be
cab90 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e low for addition
caba0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a al information.*
cabb0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 /.#if SQLITE_THR
cabc0 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 EADSAFE.# define
cabd0 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 58 29 SET_THREADID(X)
cabe0 20 20 20 28 58 29 2d 3e 74 69 64 20 3d 20 70 74 (X)->tid = pt
cabf0 68 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 20 64 hread_self().# d
cac00 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 45 efine CHECK_THRE
cac10 41 44 49 44 28 58 29 20 28 74 68 72 65 61 64 73 ADID(X) (threads
cac20 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
cac30 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 26 20 5c 0a rsLocks==0 && \.
cac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cac50 20 20 20 20 20 20 20 20 20 20 20 20 21 70 74 68 !pth
cac60 72 65 61 64 5f 65 71 75 61 6c 28 28 58 29 2d 3e read_equal((X)->
cac70 74 69 64 2c 20 70 74 68 72 65 61 64 5f 73 65 6c tid, pthread_sel
cac80 66 28 29 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 f())).#else.# de
cac90 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49 fine SET_THREADI
caca0 44 28 58 29 0a 23 20 64 65 66 69 6e 65 20 43 48 D(X).# define CH
cacb0 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29 20 ECK_THREADID(X)
cacc0 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
cacd0 48 65 72 65 20 69 73 20 74 68 65 20 64 69 72 74 Here is the dirt
cace0 20 6f 6e 20 50 4f 53 49 58 20 61 64 76 69 73 6f on POSIX adviso
cacf0 72 79 20 6c 6f 63 6b 73 3a 20 20 41 4e 53 49 20 ry locks: ANSI
cad00 53 54 44 20 31 30 30 33 2e 31 20 28 31 39 39 36 STD 1003.1 (1996
cad10 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35 ).** section 6.5
cad20 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 74 .2.2 lines 483 t
cad30 68 72 6f 75 67 68 20 34 39 30 20 73 70 65 63 69 hrough 490 speci
cad40 66 79 20 74 68 61 74 20 77 68 65 6e 20 61 20 70 fy that when a p
cad50 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f rocess.** sets o
cad60 72 20 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c r clears a lock,
cad70 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 that operation
cad80 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 70 72 overrides any pr
cad90 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a ior locks set.**
cada0 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f by the same pro
cadb0 63 65 73 73 2e 20 20 49 74 20 64 6f 65 73 20 6e cess. It does n
cadc0 6f 74 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 ot explicitly sa
cadd0 79 20 73 6f 2c 20 62 75 74 20 74 68 69 73 20 69 y so, but this i
cade0 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 69 mplies.** that i
cadf0 74 20 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 6b t overrides lock
cae00 73 20 73 65 74 20 62 79 20 74 68 65 20 73 61 6d s set by the sam
cae10 65 20 70 72 6f 63 65 73 73 20 75 73 69 6e 67 20 e process using
cae20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 a different.** f
cae30 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 ile descriptor.
cae40 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74 Consider this t
cae50 65 73 74 20 63 61 73 65 3a 0a 2a 2a 20 20 20 20 est case:.**
cae60 20 20 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 65 int fd2 = ope
cae70 6e 28 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 n("./file2", O_R
cae80 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 DWR|O_CREAT, 064
cae90 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 4);.**.** Suppos
caea0 65 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f e ./file1 and ./
caeb0 66 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c 79 file2 are really
caec0 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 28 the same file (
caed0 62 65 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 because.** one i
caee0 73 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d 62 s a hard or symb
caef0 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65 olic link to the
caf00 20 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66 20 other) then if
caf10 79 6f 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78 you set.** an ex
caf20 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 clusive lock on
caf30 66 64 31 2c 20 74 68 65 6e 20 74 72 79 20 74 6f fd1, then try to
caf40 20 67 65 74 20 61 6e 20 65 78 63 6c 75 73 69 76 get an exclusiv
caf50 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 e lock.** on fd2
caf60 2c 20 69 74 20 77 6f 72 6b 73 2e 20 20 49 20 77 , it works. I w
caf70 6f 75 6c 64 20 68 61 76 65 20 65 78 70 65 63 74 ould have expect
caf80 65 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f ed the second lo
caf90 63 6b 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 ck to.** fail si
cafa0 6e 63 65 20 74 68 65 72 65 20 77 61 73 20 61 6c nce there was al
cafb0 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 ready a lock on
cafc0 74 68 65 20 66 69 6c 65 20 64 75 65 20 74 6f 20 the file due to
cafd0 66 64 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 fd1..** But not
cafe0 73 6f 2e 20 20 53 69 6e 63 65 20 62 6f 74 68 20 so. Since both
caff0 6c 6f 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20 locks came from
cb000 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 the same process
cb010 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 , the.** second
cb020 6f 76 65 72 72 69 64 65 73 20 74 68 65 20 66 69 overrides the fi
cb030 72 73 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 rst, even though
cb040 20 74 68 65 79 20 77 65 72 65 20 6f 6e 20 64 69 they were on di
cb050 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 fferent.** file
cb060 64 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e descriptors open
cb070 65 64 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 20 ed on different
cb080 66 69 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a file names..**.*
cb090 2a 20 42 75 6d 6d 65 72 2e 20 20 49 66 20 79 6f * Bummer. If yo
cb0a0 75 20 61 73 6b 20 6d 65 2c 20 74 68 69 73 20 69 u ask me, this i
cb0b0 73 20 62 72 6f 6b 65 6e 2e 20 20 42 61 64 6c 79 s broken. Badly
cb0c0 20 62 72 6f 6b 65 6e 2e 20 20 49 74 20 6d 65 61 broken. It mea
cb0d0 6e 73 0a 2a 2a 20 74 68 61 74 20 77 65 20 63 61 ns.** that we ca
cb0e0 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c nnot use POSIX l
cb0f0 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e ocks to synchron
cb100 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73 20 ize file access
cb110 61 6d 6f 6e 67 0a 2a 2a 20 63 6f 6d 70 65 74 69 among.** competi
cb120 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20 74 68 ng threads of th
cb130 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 e same process.
cb140 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c POSIX locks wil
cb150 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 l work fine.** t
cb160 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63 o synchronize ac
cb170 63 65 73 73 20 66 6f 72 20 74 68 72 65 61 64 73 cess for threads
cb180 20 69 6e 20 73 65 70 61 72 61 74 65 20 70 72 6f in separate pro
cb190 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a cesses, but not.
cb1a0 2a 2a 20 74 68 72 65 61 64 73 20 77 69 74 68 69 ** threads withi
cb1b0 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 n the same proce
cb1c0 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 ss..**.** To wor
cb1d0 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 72 6f k around the pro
cb1e0 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 73 blem, SQLite has
cb1f0 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 20 to manage file
cb200 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79 locks internally
cb210 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 .** on its own.
cb220 20 57 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 Whenever a new
cb230 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e database is open
cb240 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 ed, we have to f
cb250 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 ind the.** speci
cb260 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 65 fic inode of the
cb270 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 database file (
cb280 74 68 65 20 69 6e 6f 64 65 20 69 73 20 64 65 74 the inode is det
cb290 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 0a 2a ermined by the.*
cb2a0 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 73 74 5f * st_dev and st_
cb2b0 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 ino fields of th
cb2c0 65 20 73 74 61 74 20 73 74 72 75 63 74 75 72 65 e stat structure
cb2d0 20 74 68 61 74 20 66 73 74 61 74 28 29 20 66 69 that fstat() fi
cb2e0 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 lls in).** and c
cb2f0 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61 heck for locks a
cb300 6c 72 65 61 64 79 20 65 78 69 73 74 69 6e 67 20 lready existing
cb310 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 20 on that inode.
cb320 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a When locks are.*
cb330 2a 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 6d * created or rem
cb340 6f 76 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f oved, we have to
cb350 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e look at our own
cb360 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 internal record
cb370 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 of the.** locks
cb380 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68 to see if anoth
cb390 65 72 20 74 68 72 65 61 64 20 68 61 73 20 70 72 er thread has pr
cb3a0 65 76 69 6f 75 73 6c 79 20 73 65 74 20 61 20 6c eviously set a l
cb3b0 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d 65 ock on that same
cb3c0 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a .** inode..**.**
cb3d0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c The sqlite3_fil
cb3e0 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 e structure for
cb3f0 50 4f 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 POSIX is no long
cb400 65 72 20 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 er just an integ
cb410 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 er file.** descr
cb420 69 70 74 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f iptor. It is no
cb430 77 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 w a structure th
cb440 61 74 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 74 at holds the int
cb450 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 eger file.** des
cb460 63 72 69 70 74 6f 72 20 61 6e 64 20 61 20 70 6f criptor and a po
cb470 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 63 inter to a struc
cb480 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 ture that descri
cb490 62 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c bes the internal
cb4a0 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 .** locks on the
cb4b0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 corresponding i
cb4c0 6e 6f 64 65 2e 20 20 54 68 65 72 65 20 69 73 20 node. There is
cb4d0 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 one locking stru
cb4e0 63 74 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f cture.** per ino
cb4f0 64 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73 61 de, so if the sa
cb500 6d 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e me inode is open
cb510 65 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20 75 ed twice, both u
cb520 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 nixFile structur
cb530 65 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 es.** point to t
cb540 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 he same locking
cb550 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 structure. The
cb560 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 locking structur
cb570 65 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65 66 e keeps.** a ref
cb580 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f erence count (so
cb590 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 we will know wh
cb5a0 65 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74 29 en to delete it)
cb5b0 20 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20 and a "cnt".**
cb5c0 66 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c 73 field that tells
cb5d0 20 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61 6c us its internal
cb5e0 20 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20 63 lock status. c
cb5f0 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a nt==0 means the.
cb600 2a 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 ** file is unloc
cb610 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 ked. cnt==-1 me
cb620 61 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61 73 ans the file has
cb630 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
cb640 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 ck..** cnt>0 mea
cb650 6e 73 20 74 68 65 72 65 20 61 72 65 20 63 6e 74 ns there are cnt
cb660 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e shared locks on
cb670 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a the file..**.**
cb680 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 Any attempt to
cb690 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 lock or unlock a
cb6a0 20 66 69 6c 65 20 66 69 72 73 74 20 63 68 65 63 file first chec
cb6b0 6b 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a ks the locking.*
cb6c0 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 * structure. Th
cb6d0 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d e fcntl() system
cb6e0 20 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e call is only in
cb6f0 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20 0a voked to set a .
cb700 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66 ** POSIX lock if
cb710 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f the internal lo
cb720 63 6b 20 73 74 72 75 63 74 75 72 65 20 74 72 61 ck structure tra
cb730 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e nsitions between
cb740 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64 .** a locked and
cb750 20 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61 an unlocked sta
cb760 74 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 34 2d 4a te..**.** 2004-J
cb770 61 6e 2d 31 31 3a 0a 2a 2a 20 4d 6f 72 65 20 72 an-11:.** More r
cb780 65 63 65 6e 74 20 64 69 73 63 6f 76 65 72 69 65 ecent discoverie
cb790 73 20 61 62 6f 75 74 20 50 4f 53 49 58 20 61 64 s about POSIX ad
cb7a0 76 69 73 6f 72 79 20 6c 6f 63 6b 73 2e 20 20 28 visory locks. (
cb7b0 54 68 65 20 6d 6f 72 65 0a 2a 2a 20 49 20 64 69 The more.** I di
cb7c0 73 63 6f 76 65 72 2c 20 74 68 65 20 6d 6f 72 65 scover, the more
cb7d0 20 49 20 72 65 61 6c 69 7a 65 20 74 68 65 20 61 I realize the a
cb7e0 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 POSIX advisory
cb7f0 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 61 6e 20 locks are.** an
cb800 61 62 6f 6d 69 6e 61 74 69 6f 6e 2e 29 0a 2a 2a abomination.).**
cb810 0a 2a 2a 20 49 66 20 79 6f 75 20 63 6c 6f 73 65 .** If you close
cb820 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 a file descript
cb830 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 or that points t
cb840 6f 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 o a file that ha
cb850 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 s locks,.** all
cb860 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74 20 66 69 locks on that fi
cb870 6c 65 20 74 68 61 74 20 61 72 65 20 6f 77 6e 65 le that are owne
cb880 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 d by the current
cb890 20 70 72 6f 63 65 73 73 20 61 72 65 0a 2a 2a 20 process are.**
cb8a0 72 65 6c 65 61 73 65 64 2e 20 20 54 6f 20 77 6f released. To wo
cb8b0 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 rk around this p
cb8c0 72 6f 62 6c 65 6d 2c 20 65 61 63 68 20 75 6e 69 roblem, each uni
cb8d0 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 xFile structure
cb8e0 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f contains.** a po
cb8f0 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 70 65 6e inter to an open
cb900 43 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 20 Cnt structure.
cb910 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6f 70 65 There is one ope
cb920 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 0a 2a nCnt structure.*
cb930 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64 65 * per open inode
cb940 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 , which means th
cb950 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 6e 69 78 at multiple unix
cb960 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e 74 20 74 File can point t
cb970 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6f 70 o a single.** op
cb980 65 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e 20 enCnt. When an
cb990 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
cb9a0 74 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69 78 to close an unix
cb9b0 46 69 6c 65 2c 20 69 66 20 74 68 65 72 65 20 61 File, if there a
cb9c0 72 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69 78 re.** other unix
cb9d0 46 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 File open on the
cb9e0 20 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61 74 same inode that
cb9f0 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f 63 are holding loc
cba00 6b 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a 2a 20 ks, the call.**
cba10 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20 66 to close() the f
cba20 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
cba30 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c s deferred until
cba40 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63 6b all of the lock
cba50 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 65 20 s clear..** The
cba60 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 openCnt structur
cba70 65 20 6b 65 65 70 73 20 61 20 6c 69 73 74 20 6f e keeps a list o
cba80 66 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f f file descripto
cba90 72 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a rs that need to.
cbaa0 2a 2a 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 ** be closed and
cbab0 20 74 68 61 74 20 6c 69 73 74 20 69 73 20 77 61 that list is wa
cbac0 6c 6b 65 64 20 28 61 6e 64 20 63 6c 65 61 72 65 lked (and cleare
cbad0 64 29 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 d) when the last
cbae0 20 6c 6f 63 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e lock.** clears.
cbaf0 0a 2a 2a 0a 2a 2a 20 46 69 72 73 74 2c 20 75 6e .**.** First, un
cbb00 64 65 72 20 4c 69 6e 75 78 20 74 68 72 65 61 64 der Linux thread
cbb10 73 2c 20 62 65 63 61 75 73 65 20 65 61 63 68 20 s, because each
cbb20 74 68 72 65 61 64 20 68 61 73 20 61 20 73 65 70 thread has a sep
cbb30 61 72 61 74 65 0a 2a 2a 20 70 72 6f 63 65 73 73 arate.** process
cbb40 20 49 44 2c 20 6c 6f 63 6b 20 6f 70 65 72 61 74 ID, lock operat
cbb50 69 6f 6e 73 20 69 6e 20 6f 6e 65 20 74 68 72 65 ions in one thre
cbb60 61 64 20 64 6f 20 6e 6f 74 20 6f 76 65 72 72 69 ad do not overri
cbb70 64 65 20 6c 6f 63 6b 73 0a 2a 2a 20 74 6f 20 74 de locks.** to t
cbb80 68 65 20 73 61 6d 65 20 66 69 6c 65 20 69 6e 20 he same file in
cbb90 6f 74 68 65 72 20 74 68 72 65 61 64 73 2e 20 20 other threads.
cbba0 4c 69 6e 75 78 20 74 68 72 65 61 64 73 20 62 65 Linux threads be
cbbb0 68 61 76 65 20 6c 69 6b 65 0a 2a 2a 20 73 65 70 have like.** sep
cbbc0 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20 arate processes
cbbd0 69 6e 20 74 68 69 73 20 72 65 73 70 65 63 74 2e in this respect.
cbbe0 20 20 42 75 74 2c 20 69 66 20 79 6f 75 20 63 6c But, if you cl
cbbf0 6f 73 65 20 61 20 66 69 6c 65 0a 2a 2a 20 64 65 ose a file.** de
cbc00 73 63 72 69 70 74 6f 72 20 69 6e 20 6c 69 6e 75 scriptor in linu
cbc10 78 20 74 68 72 65 61 64 73 2c 20 61 6c 6c 20 6c x threads, all l
cbc20 6f 63 6b 73 20 61 72 65 20 63 6c 65 61 72 65 64 ocks are cleared
cbc30 2c 20 65 76 65 6e 20 6c 6f 63 6b 73 0a 2a 2a 20 , even locks.**
cbc40 6f 6e 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 on other threads
cbc50 20 61 6e 64 20 65 76 65 6e 20 74 68 6f 75 67 68 and even though
cbc60 20 74 68 65 20 6f 74 68 65 72 20 74 68 72 65 61 the other threa
cbc70 64 73 20 68 61 76 65 20 64 69 66 66 65 72 65 6e ds have differen
cbc80 74 0a 2a 2a 20 70 72 6f 63 65 73 73 20 49 44 73 t.** process IDs
cbc90 2e 20 20 4c 69 6e 75 78 20 74 68 72 65 61 64 73 . Linux threads
cbca0 20 69 73 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 is inconsistent
cbcb0 20 69 6e 20 74 68 69 73 20 72 65 73 70 65 63 74 in this respect
cbcc0 2e 0a 2a 2a 20 28 49 27 6d 20 62 65 67 69 6e 6e ..** (I'm beginn
cbcd0 69 6e 67 20 74 6f 20 74 68 69 6e 6b 20 74 68 61 ing to think tha
cbce0 74 20 6c 69 6e 75 78 20 74 68 72 65 61 64 73 20 t linux threads
cbcf0 69 73 20 61 6e 20 61 62 6f 6d 69 6e 61 74 69 6f is an abominatio
cbd00 6e 20 74 6f 6f 2e 29 0a 2a 2a 20 54 68 65 20 63 n too.).** The c
cbd10 6f 6e 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 onsequence of th
cbd20 69 73 20 61 6c 6c 20 69 73 20 74 68 61 74 20 74 is all is that t
cbd30 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f he hash table fo
cbd40 72 20 74 68 65 20 6c 6f 63 6b 49 6e 66 6f 0a 2a r the lockInfo.*
cbd50 2a 20 73 74 72 75 63 74 75 72 65 20 68 61 73 20 * structure has
cbd60 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70 to include the p
cbd70 72 6f 63 65 73 73 20 69 64 20 61 73 20 70 61 72 rocess id as par
cbd80 74 20 6f 66 20 69 74 73 20 6b 65 79 20 62 65 63 t of its key bec
cbd90 61 75 73 65 0a 2a 2a 20 6c 6f 63 6b 73 20 69 6e ause.** locks in
cbda0 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 different threa
cbdb0 64 73 20 61 72 65 20 74 72 65 61 74 65 64 20 61 ds are treated a
cbdc0 73 20 64 69 73 74 69 6e 63 74 2e 20 20 42 75 74 s distinct. But
cbdd0 20 74 68 65 20 0a 2a 2a 20 6f 70 65 6e 43 6e 74 the .** openCnt
cbde0 20 73 74 72 75 63 74 75 72 65 20 73 68 6f 75 6c structure shoul
cbdf0 64 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 d not include th
cbe00 65 20 70 72 6f 63 65 73 73 20 69 64 20 69 6e 20 e process id in
cbe10 69 74 73 0a 2a 2a 20 6b 65 79 20 62 65 63 61 75 its.** key becau
cbe20 73 65 20 63 6c 6f 73 65 28 29 20 63 6c 65 61 72 se close() clear
cbe30 73 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 20 74 68 s lock on all th
cbe40 72 65 61 64 73 2c 20 6e 6f 74 20 6a 75 73 74 20 reads, not just
cbe50 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 the current.** t
cbe60 68 72 65 61 64 2e 20 20 57 65 72 65 20 69 74 20 hread. Were it
cbe70 6e 6f 74 20 66 6f 72 20 74 68 69 73 20 67 6f 6f not for this goo
cbe80 66 69 6e 65 73 73 20 69 6e 20 6c 69 6e 75 78 20 finess in linux
cbe90 74 68 72 65 61 64 73 2c 20 77 65 20 63 6f 75 6c threads, we coul
cbea0 64 0a 2a 2a 20 63 6f 6d 62 69 6e 65 20 74 68 65 d.** combine the
cbeb0 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 6f 70 lockInfo and op
cbec0 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 73 enCnt structures
cbed0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 into a single s
cbee0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
cbef0 32 30 30 34 2d 4a 75 6e 2d 32 38 3a 0a 2a 2a 20 2004-Jun-28:.**
cbf00 4f 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 On some versions
cbf10 20 6f 66 20 6c 69 6e 75 78 2c 20 74 68 72 65 61 of linux, threa
cbf20 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 ds can override
cbf30 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b each others lock
cbf40 73 2e 0a 2a 2a 20 4f 6e 20 6f 74 68 65 72 73 20 s..** On others
cbf50 6e 6f 74 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 not. Sometimes
cbf60 79 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74 you can change t
cbf70 68 65 20 62 65 68 61 76 69 6f 72 20 6f 6e 20 74 he behavior on t
cbf80 68 65 20 73 61 6d 65 0a 2a 2a 20 73 79 73 74 65 he same.** syste
cbf90 6d 20 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 m by setting the
cbfa0 20 4c 44 5f 41 53 53 55 4d 45 5f 4b 45 52 4e 45 LD_ASSUME_KERNE
cbfb0 4c 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 L environment va
cbfc0 72 69 61 62 6c 65 2e 20 20 54 68 65 0a 2a 2a 20 riable. The.**
cbfd0 50 4f 53 49 58 20 73 74 61 6e 64 61 72 64 20 69 POSIX standard i
cbfe0 73 20 73 69 6c 65 6e 74 20 61 73 20 74 6f 20 77 s silent as to w
cbff0 68 69 63 68 20 62 65 68 61 76 69 6f 72 20 69 73 hich behavior is
cc000 20 63 6f 72 72 65 63 74 2c 20 61 73 20 66 61 72 correct, as far
cc010 0a 2a 2a 20 61 73 20 49 20 63 61 6e 20 74 65 6c .** as I can tel
cc020 6c 2c 20 73 6f 20 6f 74 68 65 72 20 76 65 72 73 l, so other vers
cc030 69 6f 6e 73 20 6f 66 20 75 6e 69 78 20 6d 69 67 ions of unix mig
cc040 68 74 20 73 68 6f 77 20 74 68 65 20 73 61 6d 65 ht show the same
cc050 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 .** inconsistenc
cc060 79 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 y. There is no
cc070 6c 69 74 74 6c 65 20 64 6f 75 62 74 20 69 6e 20 little doubt in
cc080 6d 79 20 6d 69 6e 64 20 74 68 61 74 20 70 6f 73 my mind that pos
cc090 69 78 0a 2a 2a 20 61 64 76 69 73 6f 72 79 20 6c ix.** advisory l
cc0a0 6f 63 6b 73 20 61 6e 64 20 6c 69 6e 75 78 20 74 ocks and linux t
cc0b0 68 72 65 61 64 73 20 61 72 65 20 70 72 6f 66 6f hreads are profo
cc0c0 75 6e 64 6c 79 20 62 72 6f 6b 65 6e 2e 0a 2a 2a undly broken..**
cc0d0 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 .** To work arou
cc0e0 6e 64 20 74 68 65 20 69 6e 63 6f 6e 73 69 73 74 nd the inconsist
cc0f0 65 6e 63 69 65 73 2c 20 77 65 20 68 61 76 65 20 encies, we have
cc100 74 6f 20 74 65 73 74 20 61 74 20 72 75 6e 74 69 to test at runti
cc110 6d 65 20 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f me .** whether o
cc120 72 20 6e 6f 74 20 74 68 72 65 61 64 73 20 63 61 r not threads ca
cc130 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 n override each
cc140 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 54 others locks. T
cc150 68 69 73 20 74 65 73 74 0a 2a 2a 20 69 73 20 72 his test.** is r
cc160 75 6e 20 6f 6e 63 65 2c 20 74 68 65 20 66 69 72 un once, the fir
cc170 73 74 20 74 69 6d 65 20 61 6e 79 20 6c 6f 63 6b st time any lock
cc180 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e 20 20 is attempted.
cc190 41 20 73 74 61 74 69 63 20 0a 2a 2a 20 76 61 72 A static .** var
cc1a0 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 iable is set to
cc1b0 72 65 63 6f 72 64 20 74 68 65 20 72 65 73 75 6c record the resul
cc1c0 74 73 20 6f 66 20 74 68 69 73 20 74 65 73 74 20 ts of this test
cc1d0 66 6f 72 20 66 75 74 75 72 65 0a 2a 2a 20 75 73 for future.** us
cc1e0 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 e..*/../*.** An
cc1f0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
cc200 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
cc210 75 72 65 20 73 65 72 76 65 73 20 61 73 20 74 68 ure serves as th
cc220 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f e key used.** to
cc230 20 6c 6f 63 61 74 65 20 61 20 70 61 72 74 69 63 locate a partic
cc240 75 6c 61 72 20 6c 6f 63 6b 49 6e 66 6f 20 73 74 ular lockInfo st
cc250 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 ructure given it
cc260 73 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 s inode..**.** I
cc270 66 20 74 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 f threads cannot
cc280 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f override each o
cc290 74 68 65 72 73 20 6c 6f 63 6b 73 2c 20 74 68 65 thers locks, the
cc2a0 6e 20 77 65 20 73 65 74 20 74 68 65 0a 2a 2a 20 n we set the.**
cc2b0 6c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c lockKey.tid fiel
cc2c0 64 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 20 d to the thread
cc2d0 49 44 2e 20 20 49 66 20 74 68 72 65 61 64 73 20 ID. If threads
cc2e0 63 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20 can override.**
cc2f0 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b each others lock
cc300 73 20 74 68 65 6e 20 74 69 64 20 69 73 20 61 6c s then tid is al
cc310 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f ways set to zero
cc320 2e 20 20 74 69 64 20 69 73 20 6f 6d 69 74 74 65 . tid is omitte
cc330 64 0a 2a 2a 20 69 66 20 77 65 20 63 6f 6d 70 69 d.** if we compi
cc340 6c 65 20 77 69 74 68 6f 75 74 20 74 68 72 65 61 le without threa
cc350 64 69 6e 67 20 73 75 70 70 6f 72 74 2e 0a 2a 2f ding support..*/
cc360 0a 73 74 72 75 63 74 20 6c 6f 63 6b 4b 65 79 20 .struct lockKey
cc370 7b 0a 20 20 64 65 76 5f 74 20 64 65 76 3b 20 20 {. dev_t dev;
cc380 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 6e /* Device n
cc390 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 49 53 5f umber */.#if IS_
cc3a0 56 58 57 4f 52 4b 53 0a 20 20 76 6f 69 64 20 2a VXWORKS. void *
cc3b0 72 6e 61 6d 3b 20 20 20 20 20 20 2f 2a 20 52 65 rnam; /* Re
cc3c0 61 6c 6e 61 6d 65 20 73 69 6e 63 65 20 69 6e 6f alname since ino
cc3d0 64 65 20 75 6e 75 73 61 62 6c 65 20 2a 2f 0a 23 de unusable */.#
cc3e0 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f else. ino_t ino
cc3f0 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 6f 64 65 ; /* Inode
cc400 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 65 6e 64 69 number */.#endi
cc410 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 f.#if SQLITE_THR
cc420 45 41 44 53 41 46 45 0a 20 20 70 74 68 72 65 61 EADSAFE. pthrea
cc430 64 5f 74 20 74 69 64 3b 20 20 20 2f 2a 20 54 68 d_t tid; /* Th
cc440 72 65 61 64 20 49 44 20 6f 72 20 7a 65 72 6f 20 read ID or zero
cc450 69 66 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f if threads can o
cc460 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 verride each oth
cc470 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a er */.#endif.};.
cc480 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
cc490 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
cc4a0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 ing structure is
cc4b0 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 allocated for e
cc4c0 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 ach open.** inod
cc4d0 65 20 6f 6e 20 65 61 63 68 20 74 68 72 65 61 64 e on each thread
cc4e0 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e with a differen
cc4f0 74 20 70 72 6f 63 65 73 73 20 49 44 2e 20 20 28 t process ID. (
cc500 54 68 72 65 61 64 73 20 68 61 76 65 0a 2a 2a 20 Threads have.**
cc510 64 69 66 66 65 72 65 6e 74 20 70 72 6f 63 65 73 different proces
cc520 73 20 49 44 73 20 6f 6e 20 6c 69 6e 75 78 2c 20 s IDs on linux,
cc530 62 75 74 20 6e 6f 74 20 6f 6e 20 6d 6f 73 74 20 but not on most
cc540 6f 74 68 65 72 20 75 6e 69 78 65 73 2e 29 0a 2a other unixes.).*
cc550 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e *.** A single in
cc560 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c ode can have mul
cc570 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72 tiple file descr
cc580 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 iptors, so each
cc590 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 unixFile.** stru
cc5a0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 cture contains a
cc5b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 pointer to an i
cc5c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
cc5d0 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a object and this.
cc5e0 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 ** object keeps
cc5f0 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e a count of the n
cc600 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c umber of unixFil
cc610 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 e pointing to it
cc620 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 6c 6f 63 6b ..*/.struct lock
cc630 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63 74 20 Info {. struct
cc640 6c 6f 63 6b 4b 65 79 20 6b 65 79 3b 20 20 2f 2a lockKey key; /*
cc650 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 The lookup key
cc660 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 */. int cnt;
cc670 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
cc680 62 65 72 20 6f 66 20 53 48 41 52 45 44 20 6c 6f ber of SHARED lo
cc690 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 20 69 6e cks held */. in
cc6a0 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 t locktype;
cc6b0 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 41 /* One of SHA
cc6c0 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56 RED_LOCK, RESERV
cc6d0 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a ED_LOCK etc. */.
cc6e0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
cc6f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
cc700 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
cc710 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
cc720 2f 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49 /. struct lockI
cc730 6e 66 6f 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 nfo *pNext, *pPr
cc740 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ev; /* List of
cc750 20 61 6c 6c 20 6c 6f 63 6b 49 6e 66 6f 20 6f 62 all lockInfo ob
cc760 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a jects */.};../*.
cc770 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
cc780 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
cc790 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 73 structure serves
cc7a0 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 as the key used
cc7b0 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 .** to locate a
cc7c0 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65 6e 43 particular openC
cc7d0 6e 74 20 73 74 72 75 63 74 75 72 65 20 67 69 76 nt structure giv
cc7e0 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 20 20 54 en its inode. T
cc7f0 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 his.** is the sa
cc800 6d 65 20 61 73 20 74 68 65 20 6c 6f 63 6b 4b 65 me as the lockKe
cc810 79 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 y except that th
cc820 65 20 74 68 72 65 61 64 20 49 44 20 69 73 20 6f e thread ID is o
cc830 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 mitted..*/.struc
cc840 74 20 6f 70 65 6e 4b 65 79 20 7b 0a 20 20 64 65 t openKey {. de
cc850 76 5f 74 20 64 65 76 3b 20 20 20 2f 2a 20 44 65 v_t dev; /* De
cc860 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 vice number */.#
cc870 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if IS_VXWORKS.
cc880 76 6f 69 64 20 2a 72 6e 61 6d 3b 20 20 2f 2a 20 void *rnam; /*
cc890 52 65 61 6c 6e 61 6d 65 20 73 69 6e 63 65 20 69 Realname since i
cc8a0 6e 6f 64 65 20 75 6e 75 73 61 62 6c 65 20 2a 2f node unusable */
cc8b0 0a 23 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 .#else. ino_t i
cc8c0 6e 6f 3b 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e no; /* Inode n
cc8d0 75 6d 62 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a umber */.#endif.
cc8e0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 };../*.** An ins
cc8f0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
cc900 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
cc910 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f is allocated fo
cc920 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 r each open.** i
cc930 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74 72 75 node. This stru
cc940 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63 cture keeps trac
cc950 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 k of the number
cc960 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74 of locks on that
cc970 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66 20 61 .** inode. If a
cc980 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65 6d 70 close is attemp
cc990 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e 20 69 ted against an i
cc9a0 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68 6f 6c node that is hol
cc9b0 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c 20 74 ding.** locks, t
cc9c0 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65 66 65 he close is defe
cc9d0 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6c rred until all l
cc9e0 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20 61 64 ocks clear by ad
cc9f0 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 ding the.** file
cca00 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 62 descriptor to b
cca10 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68 65 20 e closed to the
cca20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a 2a 2f pending list..*/
cca30 0a 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 .struct openCnt
cca40 7b 0a 20 20 73 74 72 75 63 74 20 6f 70 65 6e 4b {. struct openK
cca50 65 79 20 6b 65 79 3b 20 20 20 2f 2a 20 54 68 65 ey key; /* The
cca60 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 lookup key */.
cca70 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
cca80 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
cca90 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
ccaa0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
ccab0 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 /. int nLock;
ccac0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ccad0 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 ber of outstandi
ccae0 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 6e ng locks */. in
ccaf0 74 20 6e 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 t nPending;
ccb00 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
ccb10 20 70 65 6e 64 69 6e 67 20 63 6c 6f 73 65 28 29 pending close()
ccb20 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 operations */.
ccb30 20 69 6e 74 20 2a 61 50 65 6e 64 69 6e 67 3b 20 int *aPending;
ccb40 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 /* Malloc
ccb50 65 64 20 73 70 61 63 65 20 68 6f 6c 64 69 6e 67 ed space holding
ccb60 20 66 64 27 73 20 61 77 61 69 74 69 6e 67 20 61 fd's awaiting a
ccb70 20 63 6c 6f 73 65 28 29 20 2a 2f 0a 23 69 66 20 close() */.#if
ccb80 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d IS_VXWORKS. sem
ccb90 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20 _t *pSem;
ccba0 20 20 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 /* Named POSI
ccbb0 58 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 X semaphore */.
ccbc0 20 63 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d char aSemName[M
ccbd0 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 20 AX_PATHNAME+1];
ccbe0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 /* Name of tha
ccbf0 74 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 t semaphore */.#
ccc00 65 6e 64 69 66 0a 20 20 73 74 72 75 63 74 20 6f endif. struct o
ccc10 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a penCnt *pNext, *
ccc20 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 pPrev; /* List
ccc30 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 43 6e 74 20 of all openCnt
ccc40 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f objects */.};../
ccc50 2a 0a 2a 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c *.** List of all
ccc60 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 6f 70 lockInfo and op
ccc70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 enCnt objects.
ccc80 54 68 69 73 20 75 73 65 64 20 74 6f 20 62 65 20 This used to be
ccc90 61 20 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 2e a hash.** table.
ccca0 20 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72 But the number
cccb0 20 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72 of objects is r
cccc0 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 arely more than
cccd0 61 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e a dozen and.** n
ccce0 65 76 65 72 20 65 78 63 65 65 64 73 20 61 20 66 ever exceeds a f
cccf0 65 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e ew thousand. An
ccd00 64 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 d lookup is not
ccd10 6f 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a on a critical.**
ccd20 20 70 61 74 68 20 6f 6f 20 61 20 73 69 6d 70 6c path oo a simpl
ccd30 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 e linked list wi
ccd40 6c 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 ll suffice..*/.s
ccd50 74 61 74 69 63 20 73 74 72 75 63 74 20 6c 6f 63 tatic struct loc
ccd60 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20 kInfo *lockList
ccd70 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75 = 0;.static stru
ccd80 63 74 20 6f 70 65 6e 43 6e 74 20 2a 6f 70 65 6e ct openCnt *open
ccd90 4c 69 73 74 20 3d 20 30 3b 0a 0a 23 69 66 20 49 List = 0;..#if I
ccda0 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 0a 2a 2a 20 S_VXWORKS./*.**
ccdb0 54 68 69 73 20 68 61 73 68 20 74 61 62 6c 65 20 This hash table
ccdc0 69 73 20 75 73 65 64 20 74 6f 20 62 69 6e 64 20 is used to bind
ccdd0 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 the canonical fi
ccde0 6c 65 20 6e 61 6d 65 20 74 6f 20 61 0a 2a 2a 20 le name to a.**
ccdf0 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 unixFile structu
cce00 72 65 20 61 6e 64 20 75 73 65 20 74 68 65 20 68 re and use the h
cce10 61 73 68 20 6b 65 79 20 28 3d 20 63 61 6e 6f 6e ash key (= canon
cce20 69 63 61 6c 20 6e 61 6d 65 29 0a 2a 2a 20 69 6e ical name).** in
cce30 73 74 65 61 64 20 6f 66 20 74 68 65 20 49 6e 6f stead of the Ino
cce40 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 de number of the
cce50 20 66 69 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 file to find th
cce60 65 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 6c 6f e matching.** lo
cce70 63 6b 49 6e 66 6f 20 61 6e 64 20 6f 70 65 6e 43 ckInfo and openC
cce80 6e 74 20 73 74 72 75 63 74 75 72 65 73 2e 20 49 nt structures. I
cce90 74 20 61 6c 73 6f 20 68 65 6c 70 73 20 74 6f 20 t also helps to
ccea0 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 make the.** name
cceb0 20 6f 66 20 74 68 65 20 73 65 6d 61 70 68 6f 72 of the semaphor
ccec0 65 20 77 68 65 6e 20 4c 4f 43 4b 49 4e 47 5f 53 e when LOCKING_S
cced0 54 59 4c 45 5f 4e 41 4d 45 44 53 45 4d 20 69 73 TYLE_NAMEDSEM is
ccee0 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 used.** for the
ccef0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
ccf00 20 48 61 73 68 20 6e 61 6d 65 48 61 73 68 3b 0a Hash nameHash;.
ccf10 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
ccf20 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 73 e locking styles
ccf30 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 are associated
ccf40 77 69 74 68 20 74 68 65 20 64 69 66 66 65 72 65 with the differe
ccf50 6e 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a nt file locking.
ccf60 2a 2a 20 63 61 70 61 62 69 6c 69 74 69 65 73 20 ** capabilities
ccf70 73 75 70 70 6f 72 74 65 64 20 62 79 20 64 69 66 supported by dif
ccf80 66 65 72 65 6e 74 20 66 69 6c 65 20 73 79 73 74 ferent file syst
ccf90 65 6d 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 50 4f 53 ems. .**.** POS
ccfa0 49 58 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 IX locking style
ccfb0 20 66 75 6c 6c 79 20 73 75 70 70 6f 72 74 73 20 fully supports
ccfc0 73 68 61 72 65 64 20 61 6e 64 20 65 78 63 6c 75 shared and exclu
ccfd0 73 69 76 65 20 62 79 74 65 2d 72 61 6e 67 65 20 sive byte-range
ccfe0 6c 6f 63 6b 73 20 0a 2a 2a 20 41 46 50 20 6c 6f locks .** AFP lo
ccff0 63 6b 69 6e 67 20 6f 6e 6c 79 20 73 75 70 70 6f cking only suppo
cd000 72 74 73 20 65 78 63 6c 75 73 69 76 65 20 62 79 rts exclusive by
cd010 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 0a 2a te-range locks.*
cd020 2a 20 46 4c 4f 43 4b 20 6f 6e 6c 79 20 73 75 70 * FLOCK only sup
cd030 70 6f 72 74 73 20 61 20 73 69 6e 67 6c 65 20 66 ports a single f
cd040 69 6c 65 2d 67 6c 6f 62 61 6c 20 65 78 63 6c 75 ile-global exclu
cd050 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 44 4f 54 sive lock.** DOT
cd060 4c 4f 43 4b 20 69 73 6e 27 74 20 61 20 74 72 75 LOCK isn't a tru
cd070 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 2c e locking style,
cd080 20 69 74 20 72 65 66 65 72 73 20 74 6f 20 74 68 it refers to th
cd090 65 20 75 73 65 20 6f 66 20 61 20 73 70 65 63 69 e use of a speci
cd0a0 61 6c 0a 2a 2a 20 20 20 66 69 6c 65 20 6e 61 6d al.** file nam
cd0b0 65 64 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 ed the same as t
cd0c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
cd0d0 20 77 69 74 68 20 61 20 27 2e 6c 6f 63 6b 27 20 with a '.lock'
cd0e0 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69 73 0a extension, this.
cd0f0 2a 2a 20 20 20 63 61 6e 20 62 65 20 75 73 65 64 ** can be used
cd100 20 6f 6e 20 66 69 6c 65 20 73 79 73 74 65 6d 73 on file systems
cd110 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 6f 66 66 that do not off
cd120 65 72 20 61 6e 79 20 72 65 6c 69 61 62 6c 65 20 er any reliable
cd130 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 file locking.**
cd140 4e 4f 20 6c 6f 63 6b 69 6e 67 20 6d 65 61 6e 73 NO locking means
cd150 20 74 68 61 74 20 6e 6f 20 6c 6f 63 6b 69 6e 67 that no locking
cd160 20 77 69 6c 6c 20 62 65 20 61 74 74 65 6d 70 74 will be attempt
cd170 65 64 2c 20 74 68 69 73 20 69 73 20 6f 6e 6c 79 ed, this is only
cd180 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 20 20 72 used for.** r
cd190 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 73 79 ead-only file sy
cd1a0 73 74 65 6d 73 20 63 75 72 72 65 6e 74 6c 79 0a stems currently.
cd1b0 2a 2a 20 4e 41 4d 45 44 53 45 4d 20 69 73 20 73 ** NAMEDSEM is s
cd1c0 69 6d 69 6c 61 72 20 74 6f 20 44 4f 54 4c 4f 43 imilar to DOTLOC
cd1d0 4b 20 62 75 74 20 75 73 65 73 20 61 20 6e 61 6d K but uses a nam
cd1e0 65 64 20 73 65 6d 61 70 68 6f 72 65 20 69 6e 73 ed semaphore ins
cd1f0 74 65 61 64 20 6f 66 20 61 6e 0a 2a 2a 20 20 20 tead of an.**
cd200 69 6e 64 69 63 61 74 6f 72 20 66 69 6c 65 2e 0a indicator file..
cd210 2a 2a 20 55 4e 53 55 50 50 4f 52 54 45 44 20 6d ** UNSUPPORTED m
cd220 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 6c 6f 63 eans that no loc
cd230 6b 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 74 74 king will be att
cd240 65 6d 70 74 65 64 2c 20 74 68 69 73 20 69 73 20 empted, this is
cd250 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 0a 2a 2a only used for.**
cd260 20 20 20 66 69 6c 65 20 73 79 73 74 65 6d 73 20 file systems
cd270 74 68 61 74 20 61 72 65 20 6b 6e 6f 77 6e 20 74 that are known t
cd280 6f 20 62 65 20 75 6e 73 75 70 70 6f 72 74 65 64 o be unsupported
cd290 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b .*/.#define LOCK
cd2a0 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 20 ING_STYLE_POSIX
cd2b0 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
cd2c0 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e LOCKING_STYLE_N
cd2d0 4f 4e 45 20 20 20 20 20 20 20 20 20 32 0a 23 64 ONE 2.#d
cd2e0 65 66 69 6e 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 efine LOCKING_ST
cd2f0 59 4c 45 5f 44 4f 54 46 49 4c 45 20 20 20 20 20 YLE_DOTFILE
cd300 20 33 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 49 3.#define LOCKI
cd310 4e 47 5f 53 54 59 4c 45 5f 46 4c 4f 43 4b 20 20 NG_STYLE_FLOCK
cd320 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4.#define
cd330 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 41 46 LOCKING_STYLE_AF
cd340 50 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 P 5.#de
cd350 66 69 6e 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 fine LOCKING_STY
cd360 4c 45 5f 4e 41 4d 45 44 53 45 4d 20 20 20 20 20 LE_NAMEDSEM
cd370 36 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 6../*.** Only se
cd380 74 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 t the lastErrno
cd390 69 66 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 if the error cod
cd3a0 65 20 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f e is a real erro
cd3b0 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 r and not .** a
cd3c0 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20 normal expected
cd3d0 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 return code of S
cd3e0 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51 QLITE_BUSY or SQ
cd3f0 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 LITE_OK.*/.#defi
cd400 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 ne IS_LOCK_ERROR
cd410 28 78 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49 (x) ((x != SQLI
cd420 54 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 TE_OK) && (x !=
cd430 53 51 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 2f SQLITE_BUSY))../
cd440 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 *.** Helper func
cd450 74 69 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 tions to obtain
cd460 61 6e 64 20 72 65 6c 69 6e 71 75 69 73 68 20 74 and relinquish t
cd470 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e he global mutex.
cd480 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
cd490 65 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29 enterMutex(void)
cd4a0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
cd4b0 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d x_enter(sqlite3M
cd4c0 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
cd4d0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
cd4e0 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63 STER));.}.static
cd4f0 20 76 6f 69 64 20 6c 65 61 76 65 4d 75 74 65 78 void leaveMutex
cd500 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 (void){. sqlite
cd510 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 3_mutex_leave(sq
cd520 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
cd530 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
cd540 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a TIC_MASTER));.}.
cd550 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
cd560 41 44 53 41 46 45 0a 2f 2a 0a 2a 2a 20 54 68 69 ADSAFE./*.** Thi
cd570 73 20 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72 s variable recor
cd580 64 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ds whether or no
cd590 74 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 t threads can ov
cd5a0 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 erride each othe
cd5b0 72 73 0a 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a rs.** locks..**.
cd5c0 2a 2a 20 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54 ** 0: No. T
cd5d0 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 hreads cannot ov
cd5e0 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 erride each othe
cd5f0 72 73 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20 rs locks..**
cd600 31 3a 20 20 59 65 73 2e 20 20 54 68 72 65 61 64 1: Yes. Thread
cd610 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 s can override e
cd620 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 ach others locks
cd630 2e 0a 2a 2a 20 20 20 2d 31 3a 20 20 57 65 20 64 ..** -1: We d
cd640 6f 6e 27 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a on't know yet..*
cd650 2a 0a 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73 *.** On some sys
cd660 74 65 6d 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74 tems, we know at
cd670 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66 compile-time if
cd680 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 threads can ove
cd690 72 72 69 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74 rride each.** ot
cd6a0 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 hers locks. On
cd6b0 74 68 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 74 those systems, t
cd6c0 68 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 he SQLITE_THREAD
cd6d0 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d _OVERRIDE_LOCK m
cd6e0 61 63 72 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 acro.** will be
cd6f0 73 65 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c set appropriatel
cd700 79 2e 20 20 4f 6e 20 6f 74 68 65 72 20 73 79 73 y. On other sys
cd710 74 65 6d 73 2c 20 77 65 20 68 61 76 65 20 74 6f tems, we have to
cd720 20 63 68 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e check at.** run
cd730 74 69 6d 65 2e 20 20 4f 6e 20 74 68 65 73 65 20 time. On these
cd740 6c 61 74 74 65 72 20 73 79 73 74 65 6d 73 2c 20 latter systems,
cd750 53 51 4c 54 49 45 5f 54 48 52 45 41 44 5f 4f 56 SQLTIE_THREAD_OV
cd760 45 52 52 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a ERRIDE_LOCK is.*
cd770 2a 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a * undefined..**.
cd780 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 ** This variable
cd790 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 73 20 66 69 normally has fi
cd7a0 6c 65 20 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 le scope only.
cd7b0 42 75 74 20 64 75 72 69 6e 67 20 74 65 73 74 69 But during testi
cd7c0 6e 67 2c 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 ng, we make.** i
cd7d0 74 20 61 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68 t a global so th
cd7e0 61 74 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 at the test code
cd7f0 20 63 61 6e 20 63 68 61 6e 67 65 20 69 74 73 20 can change its
cd800 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 value in order t
cd810 6f 20 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 o verify.** that
cd820 20 74 68 65 20 72 69 67 68 74 20 73 74 75 66 66 the right stuff
cd830 20 68 61 70 70 65 6e 73 20 69 6e 20 65 69 74 68 happens in eith
cd840 65 72 20 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 6e er case..*/.#ifn
cd850 64 65 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 def SQLITE_THREA
cd860 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 0a D_OVERRIDE_LOCK.
cd870 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
cd880 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f THREAD_OVERRIDE_
cd890 4c 4f 43 4b 20 2d 31 0a 23 65 6e 64 69 66 0a 23 LOCK -1.#endif.#
cd8a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
cd8b0 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 T.int threadsOve
cd8c0 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
cd8d0 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 ocks = SQLITE_TH
cd8e0 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f READ_OVERRIDE_LO
cd8f0 43 4b 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 CK;.#else.static
cd900 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 int threadsOver
cd910 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f rideEachOthersLo
cd920 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 cks = SQLITE_THR
cd930 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 EAD_OVERRIDE_LOC
cd940 4b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a K;.#endif../*.**
cd950 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
cd960 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f holds informatio
cd970 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 69 6e n passed into in
cd980 64 69 76 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a dividual test.**
cd990 20 74 68 72 65 61 64 73 20 62 79 20 74 68 65 20 threads by the
cd9a0 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e testThreadLockin
cd9b0 67 42 65 68 61 76 69 6f 72 28 29 20 72 6f 75 74 gBehavior() rout
cd9c0 69 6e 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 ine..*/.struct t
cd9d0 68 72 65 61 64 54 65 73 74 44 61 74 61 20 7b 0a hreadTestData {.
cd9e0 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 int fd;
cd9f0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
cda00 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f to be locked */
cda10 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 . struct flock
cda20 6c 6f 63 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65 lock; /* The
cda30 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 locking operati
cda40 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75 on */. int resu
cda50 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f lt; /
cda60 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 * Result of the
cda70 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f locking operatio
cda80 6e 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 n */.};..#ifdef
cda90 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 SQLITE_LOCK_TRAC
cdaa0 45 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 E./*.** Print ou
cdab0 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 t information ab
cdac0 6f 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 out all locking
cdad0 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a operations..**.*
cdae0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
cdaf0 73 20 75 73 65 64 20 66 6f 72 20 74 72 6f 75 62 s used for troub
cdb00 6c 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 leshooting locks
cdb10 20 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 on multithreade
cdb20 64 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20 d.** platforms.
cdb30 20 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 69 Enable by compi
cdb40 6c 69 6e 67 20 77 69 74 68 20 74 68 65 20 2d 44 ling with the -D
cdb50 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 SQLITE_LOCK_TRAC
cdb60 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e E.** command-lin
cdb70 65 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20 e option on the
cdb80 63 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 20 compiler. This
cdb90 63 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 code is normally
cdba0 0a 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a .** turned off..
cdbb0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f */.static int lo
cdbc0 63 6b 54 72 61 63 65 28 69 6e 74 20 66 64 2c 20 ckTrace(int fd,
cdbd0 69 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 20 66 int op, struct f
cdbe0 6c 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72 lock *p){. char
cdbf0 20 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 *zOpName, *zTyp
cdc00 65 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e e;. int s;. in
cdc10 74 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 t savedErrno;.
cdc20 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 if( op==F_GETLK
cdc30 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d ){. zOpName =
cdc40 20 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 "GETLK";. }els
cdc50 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c e if( op==F_SETL
cdc60 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 K ){. zOpName
cdc70 20 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 = "SETLK";. }e
cdc80 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e lse{. s = fcn
cdc90 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 tl(fd, op, p);.
cdca0 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 sqlite3DebugP
cdcb0 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b rintf("fcntl unk
cdcc0 6e 6f 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 nown %d %d %d\n"
cdcd0 2c 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 , fd, op, s);.
cdce0 20 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a return s;. }.
cdcf0 20 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d if( p->l_type=
cdd00 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 =F_RDLCK ){.
cdd10 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b zType = "RDLCK";
cdd20 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e . }else if( p->
cdd30 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 l_type==F_WRLCK
cdd40 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 ){. zType = "
cdd50 57 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 WRLCK";. }else
cdd60 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 if( p->l_type==F
cdd70 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 _UNLCK ){. zT
cdd80 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 ype = "UNLCK";.
cdd90 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
cdda0 72 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 rt( 0 );. }. a
cddb0 73 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e ssert( p->l_when
cddc0 63 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a ce==SEEK_SET );.
cddd0 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 s = fcntl(fd,
cdde0 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45 op, p);. savedE
cddf0 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
cde00 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
cde10 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20 tf("fcntl %d %d
cde20 25 73 20 25 73 20 25 64 20 25 64 20 25 64 20 25 %s %s %d %d %d %
cde30 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61 d\n",. threa
cde40 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 did, fd, zOpName
cde50 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d , zType, (int)p-
cde60 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70 >l_start, (int)p
cde70 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 ->l_len,. (i
cde80 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b nt)p->l_pid, s);
cde90 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 . if( s==(-1) &
cdea0 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 & op==F_SETLK &&
cdeb0 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 (p->l_type==F_R
cdec0 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 DLCK || p->l_typ
cded0 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 e==F_WRLCK) ){.
cdee0 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 struct flock
cdef0 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b l2;. l2 = *p;
cdf00 0a 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 . fcntl(fd, F
cdf10 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 _GETLK, &l2);.
cdf20 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d if( l2.l_type=
cdf30 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 =F_RDLCK ){.
cdf40 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b zType = "RDLCK
cdf50 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 ";. }else if(
cdf60 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 l2.l_type==F_WR
cdf70 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 LCK ){. zTy
cdf80 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 pe = "WRLCK";.
cdf90 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c }else if( l2.l
cdfa0 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 _type==F_UNLCK )
cdfb0 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 {. zType =
cdfc0 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c "UNLCK";. }el
cdfd0 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
cdfe0 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ( 0 );. }.
cdff0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
ce000 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75 ntf("fcntl-failu
ce010 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64 re-reason: %s %d
ce020 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 %d %d\n",.
ce030 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32 zType, (int)l2
ce040 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c .l_start, (int)l
ce050 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2.l_len, (int)l2
ce060 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65 .l_pid);. }. e
ce070 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e rrno = savedErrn
ce080 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d o;. return s;.}
ce090 0a 23 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c .#define fcntl l
ce0a0 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20 ockTrace.#endif
ce0b0 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 /* SQLITE_LOCK_T
ce0c0 52 41 43 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20 RACE */..#ifdef
ce0d0 5f 5f 6c 69 6e 75 78 5f 5f 0a 2f 2a 0a 2a 2a 20 __linux__./*.**
ce0e0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
ce0f0 20 75 73 65 64 20 61 73 20 74 68 65 20 6d 61 69 used as the mai
ce100 6e 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 20 n routine for a
ce110 74 68 72 65 61 64 20 6c 61 75 6e 63 68 65 64 20 thread launched
ce120 62 79 0a 2a 2a 20 74 65 73 74 54 68 72 65 61 64 by.** testThread
ce130 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 LockingBehavior(
ce140 29 2e 20 49 74 20 74 65 73 74 73 20 77 68 65 74 ). It tests whet
ce150 68 65 72 20 74 68 65 20 73 68 61 72 65 64 2d 6c her the shared-l
ce160 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 ock obtained.**
ce170 62 79 20 74 68 65 20 6d 61 69 6e 20 74 68 72 65 by the main thre
ce180 61 64 20 69 6e 20 74 65 73 74 54 68 72 65 61 64 ad in testThread
ce190 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 LockingBehavior(
ce1a0 29 20 63 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 ) conflicts with
ce1b0 20 61 0a 2a 2a 20 68 79 70 6f 74 68 65 74 69 63 a.** hypothetic
ce1c0 61 6c 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 62 al write-lock ob
ce1d0 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 74 tained by this t
ce1e0 68 72 65 61 64 20 6f 6e 20 74 68 65 20 73 61 6d hread on the sam
ce1f0 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e file..**.** Th
ce200 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20 e write-lock is
ce210 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 61 63 71 not actually acq
ce220 75 69 72 65 64 2c 20 61 73 20 74 68 69 73 20 69 uired, as this i
ce230 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 69 s not possible i
ce240 66 20 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 69 f .** the file i
ce250 73 20 6f 70 65 6e 20 69 6e 20 72 65 61 64 2d 6f s open in read-o
ce260 6e 6c 79 20 6d 6f 64 65 20 28 73 65 65 20 74 69 nly mode (see ti
ce270 63 6b 65 74 20 23 33 34 37 32 29 2e 0a 2a 2f 20 cket #3472)..*/
ce280 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 74 68 .static void *th
ce290 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 28 readLockingTest(
ce2a0 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 void *pArg){. s
ce2b0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 truct threadTest
ce2c0 44 61 74 61 20 2a 70 44 61 74 61 20 3d 20 28 73 Data *pData = (s
ce2d0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 truct threadTest
ce2e0 44 61 74 61 2a 29 70 41 72 67 3b 0a 20 20 70 44 Data*)pArg;. pD
ce2f0 61 74 61 2d 3e 72 65 73 75 6c 74 20 3d 20 66 63 ata->result = fc
ce300 6e 74 6c 28 70 44 61 74 61 2d 3e 66 64 2c 20 46 ntl(pData->fd, F
ce310 5f 47 45 54 4c 4b 2c 20 26 70 44 61 74 61 2d 3e _GETLK, &pData->
ce320 6c 6f 63 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20 lock);. return
ce330 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 pArg;.}../*.** T
ce340 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 61 74 his procedure at
ce350 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d tempts to determ
ce360 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ine whether or n
ce370 6f 74 20 74 68 72 65 61 64 73 0a 2a 2a 20 63 61 ot threads.** ca
ce380 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 n override each
ce390 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 74 68 65 others locks the
ce3a0 6e 20 73 65 74 73 20 74 68 65 20 0a 2a 2a 20 74 n sets the .** t
ce3b0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 hreadsOverrideEa
ce3c0 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 76 61 chOthersLocks va
ce3d0 72 69 61 62 6c 65 20 61 70 70 72 6f 70 72 69 61 riable appropria
ce3e0 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tely..*/.static
ce3f0 76 6f 69 64 20 74 65 73 74 54 68 72 65 61 64 4c void testThreadL
ce400 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 69 ockingBehavior(i
ce410 6e 74 20 66 64 5f 6f 72 69 67 29 7b 0a 20 20 69 nt fd_orig){. i
ce420 6e 74 20 66 64 3b 0a 20 20 69 6e 74 20 72 63 3b nt fd;. int rc;
ce430 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 61 64 . struct thread
ce440 54 65 73 74 44 61 74 61 20 64 3b 0a 20 20 73 74 TestData d;. st
ce450 72 75 63 74 20 66 6c 6f 63 6b 20 6c 3b 0a 20 20 ruct flock l;.
ce460 70 74 68 72 65 61 64 5f 74 20 74 3b 0a 0a 20 20 pthread_t t;..
ce470 66 64 20 3d 20 64 75 70 28 66 64 5f 6f 72 69 67 fd = dup(fd_orig
ce480 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 20 );. if( fd<0 )
ce490 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 return;. memset
ce4a0 28 26 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c (&l, 0, sizeof(l
ce4b0 29 29 3b 0a 20 20 6c 2e 6c 5f 74 79 70 65 20 3d ));. l.l_type =
ce4c0 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 6c 2e 6c 5f F_RDLCK;. l.l_
ce4d0 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 2e 6c 5f 73 len = 1;. l.l_s
ce4e0 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 2e 6c 5f tart = 0;. l.l_
ce4f0 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 whence = SEEK_SE
ce500 54 3b 0a 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 T;. rc = fcntl(
ce510 66 64 5f 6f 72 69 67 2c 20 46 5f 53 45 54 4c 4b fd_orig, F_SETLK
ce520 2c 20 26 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 , &l);. if( rc!
ce530 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d =0 ) return;. m
ce540 65 6d 73 65 74 28 26 64 2c 20 30 2c 20 73 69 7a emset(&d, 0, siz
ce550 65 6f 66 28 64 29 29 3b 0a 20 20 64 2e 66 64 20 eof(d));. d.fd
ce560 3d 20 66 64 3b 0a 20 20 64 2e 6c 6f 63 6b 20 3d = fd;. d.lock =
ce570 20 6c 3b 0a 20 20 64 2e 6c 6f 63 6b 2e 6c 5f 74 l;. d.lock.l_t
ce580 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 ype = F_WRLCK;.
ce590 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 pthread_create(
ce5a0 26 74 2c 20 30 2c 20 74 68 72 65 61 64 4c 6f 63 &t, 0, threadLoc
ce5b0 6b 69 6e 67 54 65 73 74 2c 20 26 64 29 3b 0a 20 kingTest, &d);.
ce5c0 20 70 74 68 72 65 61 64 5f 6a 6f 69 6e 28 74 2c pthread_join(t,
ce5d0 20 30 29 3b 0a 20 20 63 6c 6f 73 65 28 66 64 29 0);. close(fd)
ce5e0 3b 0a 20 20 69 66 28 20 64 2e 72 65 73 75 6c 74 ;. if( d.result
ce5f0 21 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 !=0 ) return;.
ce600 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 threadsOverrideE
ce610 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d achOthersLocks =
ce620 20 28 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 3d (d.lock.l_type=
ce630 3d 46 5f 55 4e 4c 43 4b 29 3b 0a 7d 0a 23 65 6c =F_UNLCK);.}.#el
ce640 73 65 0a 2f 2a 0a 2a 2a 20 4f 6e 20 61 6e 79 74 se./*.** On anyt
ce650 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 hing other than
ce660 6c 69 6e 75 78 2c 20 61 73 73 75 6d 65 20 74 68 linux, assume th
ce670 72 65 61 64 73 20 6f 76 65 72 72 69 64 65 20 65 reads override e
ce680 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 ach others locks
ce690 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
ce6a0 20 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 testThreadLocki
ce6b0 6e 67 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66 ngBehavior(int f
ce6c0 64 5f 6f 72 69 67 29 7b 0a 20 20 74 68 72 65 61 d_orig){. threa
ce6d0 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
ce6e0 68 65 72 73 4c 6f 63 6b 73 20 3d 20 31 3b 0a 7d hersLocks = 1;.}
ce6f0 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 5f 6c 69 6e .#endif /* __lin
ce700 75 78 5f 5f 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 ux__ */..#endif
ce710 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 /* SQLITE_THREAD
ce720 53 41 46 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 SAFE */../*.** R
ce730 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b 49 6e 66 elease a lockInf
ce740 6f 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 o structure prev
ce750 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 iously allocated
ce760 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f by findLockInfo
ce770 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ()..*/.static vo
ce780 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e id releaseLockIn
ce790 66 6f 28 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e fo(struct lockIn
ce7a0 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a 20 20 69 66 fo *pLock){. if
ce7b0 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 ( pLock ){. p
ce7c0 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 Lock->nRef--;.
ce7d0 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65 if( pLock->nRe
ce7e0 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 f==0 ){. if
ce7f0 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29 ( pLock->pPrev )
ce800 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
ce810 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e ( pLock->pPrev->
ce820 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a pNext==pLock );.
ce830 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 pLock->p
ce840 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c Prev->pNext = pL
ce850 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 ock->pNext;.
ce860 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
ce870 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73 assert( lockLis
ce880 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 t==pLock );.
ce890 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 lockList = p
ce8a0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Lock->pNext;.
ce8b0 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 }. if( p
ce8c0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 Lock->pNext ){.
ce8d0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
ce8e0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 Lock->pNext->pPr
ce8f0 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 ev==pLock );.
ce900 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 pLock->pNex
ce910 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b t->pPrev = pLock
ce920 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d ->pPrev;. }
ce930 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
ce940 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 ree(pLock);.
ce950 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 }. }.}../*.** R
ce960 65 6c 65 61 73 65 20 61 20 6f 70 65 6e 43 6e 74 elease a openCnt
ce970 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69 structure previ
ce980 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 ously allocated
ce990 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 by findLockInfo(
ce9a0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
ce9b0 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 d releaseOpenCnt
ce9c0 28 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 (struct openCnt
ce9d0 2a 70 4f 70 65 6e 29 7b 0a 20 20 69 66 28 20 70 *pOpen){. if( p
ce9e0 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65 Open ){. pOpe
ce9f0 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 n->nRef--;. i
cea00 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d f( pOpen->nRef==
cea10 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
cea20 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20 Open->pPrev ){.
cea30 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
cea40 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 Open->pPrev->pNe
cea50 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 xt==pOpen );.
cea60 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65 pOpen->pPre
cea70 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e v->pNext = pOpen
cea80 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
cea90 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 else{. as
ceaa0 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d sert( openList==
ceab0 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 pOpen );.
ceac0 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 openList = pOpe
cead0 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 n->pNext;.
ceae0 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 }. if( pOpe
ceaf0 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 n->pNext ){.
ceb00 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 assert( pOpe
ceb10 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d n->pNext->pPrev=
ceb20 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 =pOpen );.
ceb30 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e pOpen->pNext->
ceb40 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 pPrev = pOpen->p
ceb50 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Prev;. }.
ceb60 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
ceb70 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 (pOpen->aPending
ceb80 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
ceb90 5f 66 72 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20 _free(pOpen);.
ceba0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 49 }. }.}..#if I
cebb0 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 0a 2a 2a 20 S_VXWORKS./*.**
cebc0 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
cebd0 66 20 61 20 72 65 61 6c 70 61 74 68 28 29 20 6c f a realpath() l
cebe0 69 6b 65 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 ike function for
cebf0 20 76 78 57 6f 72 6b 73 0a 2a 2a 20 74 6f 20 64 vxWorks.** to d
cec00 65 74 65 72 6d 69 6e 65 20 63 61 6e 6f 6e 69 63 etermine canonic
cec10 61 6c 20 70 61 74 68 20 6e 61 6d 65 20 66 72 6f al path name fro
cec20 6d 20 67 69 76 65 6e 20 6e 61 6d 65 2e 20 49 74 m given name. It
cec30 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73 75 70 does.** not sup
cec40 70 6f 72 74 20 73 79 6d 6c 69 6e 6b 73 2e 20 4e port symlinks. N
cec50 65 69 74 68 65 72 20 64 6f 65 73 20 69 74 20 68 either does it h
cec60 61 6e 64 6c 65 20 76 6f 6c 75 6d 65 20 70 72 65 andle volume pre
cec70 66 69 78 65 73 2e 0a 2a 2f 0a 63 68 61 72 20 2a fixes..*/.char *
cec80 0a 76 78 72 65 61 6c 70 61 74 68 28 63 6f 6e 73 .vxrealpath(cons
cec90 74 20 63 68 61 72 20 2a 70 61 74 68 6e 61 6d 65 t char *pathname
ceca0 2c 20 69 6e 74 20 64 6f 73 74 61 74 29 0a 7b 0a , int dostat).{.
cecb0 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 62 struct stat sb
cecc0 75 66 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 uf;. int len;.
cecd0 20 63 68 61 72 20 2a 77 68 65 72 65 2c 20 2a 70 char *where, *p
cece0 74 72 2c 20 2a 6c 61 73 74 3b 0a 20 20 63 68 61 tr, *last;. cha
cecf0 72 20 2a 72 65 73 75 6c 74 2c 20 2a 63 75 72 70 r *result, *curp
ced00 61 74 68 2c 20 2a 77 6f 72 6b 70 61 74 68 2c 20 ath, *workpath,
ced10 2a 6e 61 6d 65 62 75 66 3b 0a 0a 20 20 6c 65 6e *namebuf;.. len
ced20 20 3d 20 70 61 74 68 63 6f 6e 66 28 70 61 74 68 = pathconf(path
ced30 6e 61 6d 65 2c 20 5f 50 43 5f 50 41 54 48 5f 4d name, _PC_PATH_M
ced40 41 58 29 3b 0a 20 20 69 66 28 20 6c 65 6e 3c 30 AX);. if( len<0
ced50 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 50 41 ){. len = PA
ced60 54 48 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 72 65 TH_MAX;. }. re
ced70 73 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d sult = sqlite3_m
ced80 61 6c 6c 6f 63 28 6c 65 6e 20 2a 20 34 29 3b 0a alloc(len * 4);.
ced90 20 20 69 66 28 20 21 72 65 73 75 6c 74 20 29 7b if( !result ){
ceda0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
cedb0 20 7d 0a 20 20 63 75 72 70 61 74 68 20 3d 20 72 }. curpath = r
cedc0 65 73 75 6c 74 20 2b 20 6c 65 6e 3b 0a 20 20 77 esult + len;. w
cedd0 6f 72 6b 70 61 74 68 20 3d 20 63 75 72 70 61 74 orkpath = curpat
cede0 68 20 2b 20 6c 65 6e 3b 0a 20 20 6e 61 6d 65 62 h + len;. nameb
cedf0 75 66 20 3d 20 77 6f 72 6b 70 61 74 68 20 2b 20 uf = workpath +
cee00 6c 65 6e 3b 0a 20 20 73 74 72 63 70 79 28 63 75 len;. strcpy(cu
cee10 72 70 61 74 68 2c 20 70 61 74 68 6e 61 6d 65 29 rpath, pathname)
cee20 3b 0a 20 20 69 66 28 20 2a 70 61 74 68 6e 61 6d ;. if( *pathnam
cee30 65 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 69 66 e!='/' ){. if
cee40 28 20 21 67 65 74 63 77 64 28 77 6f 72 6b 70 61 ( !getcwd(workpa
cee50 74 68 2c 20 6c 65 6e 29 20 29 7b 0a 20 20 20 20 th, len) ){.
cee60 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72 sqlite3_free(r
cee70 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 72 65 esult);. re
cee80 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
cee90 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 77 6f 72 6b }else{. *work
ceea0 70 61 74 68 20 3d 20 27 5c 30 27 3b 0a 20 20 7d path = '\0';. }
ceeb0 0a 20 20 77 68 65 72 65 20 3d 20 63 75 72 70 61 . where = curpa
ceec0 74 68 3b 0a 20 20 77 68 69 6c 65 28 20 2a 77 68 th;. while( *wh
ceed0 65 72 65 20 29 7b 0a 20 20 20 20 69 66 28 20 21 ere ){. if( !
ceee0 73 74 72 63 6d 70 28 77 68 65 72 65 2c 20 22 2e strcmp(where, ".
ceef0 22 29 20 29 7b 0a 20 20 20 20 20 20 77 68 65 72 ") ){. wher
cef00 65 2b 2b 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 e++;. conti
cef10 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 nue;. }. i
cef20 66 28 20 21 73 74 72 6e 63 6d 70 28 77 68 65 72 f( !strncmp(wher
cef30 65 2c 20 22 2e 2f 22 2c 20 32 29 20 29 7b 0a 20 e, "./", 2) ){.
cef40 20 20 20 20 20 77 68 65 72 65 20 2b 3d 20 32 3b where += 2;
cef50 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b . continue;
cef60 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 . }. if( !
cef70 73 74 72 6e 63 6d 70 28 77 68 65 72 65 2c 20 22 strncmp(where, "
cef80 2e 2e 2f 22 2c 20 33 29 20 29 7b 0a 20 20 20 20 ../", 3) ){.
cef90 20 20 77 68 65 72 65 20 2b 3d 20 33 3b 0a 20 20 where += 3;.
cefa0 20 20 20 20 70 74 72 20 3d 20 6c 61 73 74 20 3d ptr = last =
cefb0 20 77 6f 72 6b 70 61 74 68 3b 0a 20 20 20 20 20 workpath;.
cefc0 20 77 68 69 6c 65 28 20 2a 70 74 72 20 29 7b 0a while( *ptr ){.
cefd0 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 74 72 if( *ptr
cefe0 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 =='/' ){.
ceff0 20 20 20 6c 61 73 74 20 3d 20 70 74 72 3b 0a 20 last = ptr;.
cf000 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
cf010 20 70 74 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a ptr++;. }.
cf020 20 20 20 20 20 20 2a 6c 61 73 74 20 3d 20 27 5c *last = '\
cf030 30 27 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 0';. contin
cf040 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 74 ue;. }. pt
cf050 72 20 3d 20 73 74 72 63 68 72 28 77 68 65 72 65 r = strchr(where
cf060 2c 20 27 2f 27 29 3b 0a 20 20 20 20 69 66 28 20 , '/');. if(
cf070 21 70 74 72 20 29 7b 0a 20 20 20 20 20 20 70 74 !ptr ){. pt
cf080 72 20 3d 20 77 68 65 72 65 20 2b 20 73 74 72 6c r = where + strl
cf090 65 6e 28 77 68 65 72 65 29 20 2d 20 31 3b 0a 20 en(where) - 1;.
cf0a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
cf0b0 2a 70 74 72 20 3d 20 27 5c 30 27 3b 0a 20 20 20 *ptr = '\0';.
cf0c0 20 7d 0a 20 20 20 20 73 74 72 63 70 79 28 6e 61 }. strcpy(na
cf0d0 6d 65 62 75 66 2c 20 77 6f 72 6b 70 61 74 68 29 mebuf, workpath)
cf0e0 3b 0a 20 20 20 20 66 6f 72 28 20 6c 61 73 74 20 ;. for( last
cf0f0 3d 20 6e 61 6d 65 62 75 66 3b 20 2a 6c 61 73 74 = namebuf; *last
cf100 3b 20 6c 61 73 74 2b 2b 20 29 7b 0a 20 20 20 20 ; last++ ){.
cf110 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
cf120 7d 0a 20 20 20 20 69 66 28 20 2a 2d 2d 6c 61 73 }. if( *--las
cf130 74 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 t!='/' ){.
cf140 73 74 72 63 61 74 28 6e 61 6d 65 62 75 66 2c 20 strcat(namebuf,
cf150 22 2f 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 "/");. }.
cf160 73 74 72 63 61 74 28 6e 61 6d 65 62 75 66 2c 20 strcat(namebuf,
cf170 77 68 65 72 65 29 3b 0a 20 20 20 20 77 68 65 72 where);. wher
cf180 65 20 3d 20 2b 2b 70 74 72 3b 0a 20 20 20 20 69 e = ++ptr;. i
cf190 66 28 20 64 6f 73 74 61 74 20 29 7b 0a 20 20 20 f( dostat ){.
cf1a0 20 20 20 69 66 28 20 73 74 61 74 28 6e 61 6d 65 if( stat(name
cf1b0 62 75 66 2c 20 26 73 62 75 66 29 3d 3d 2d 31 20 buf, &sbuf)==-1
cf1c0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
cf1d0 65 33 5f 66 72 65 65 28 72 65 73 75 6c 74 29 3b e3_free(result);
cf1e0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
cf1f0 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
cf200 20 69 66 28 20 28 73 62 75 66 2e 73 74 5f 6d 6f if( (sbuf.st_mo
cf210 64 65 20 26 20 53 5f 49 46 44 49 52 29 3d 3d 53 de & S_IFDIR)==S
cf220 5f 49 46 44 49 52 20 29 7b 0a 20 20 20 20 20 20 _IFDIR ){.
cf230 20 20 73 74 72 63 70 79 28 77 6f 72 6b 70 61 74 strcpy(workpat
cf240 68 2c 20 6e 61 6d 65 62 75 66 29 3b 0a 20 20 20 h, namebuf);.
cf250 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
cf260 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
cf270 20 2a 77 68 65 72 65 20 29 7b 0a 20 20 20 20 20 *where ){.
cf280 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
cf290 72 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 result);.
cf2a0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 return 0;.
cf2b0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 74 72 }. }. str
cf2c0 63 70 79 28 77 6f 72 6b 70 61 74 68 2c 20 6e 61 cpy(workpath, na
cf2d0 6d 65 62 75 66 29 3b 0a 20 20 7d 0a 20 20 73 74 mebuf);. }. st
cf2e0 72 63 70 79 28 72 65 73 75 6c 74 2c 20 77 6f 72 rcpy(result, wor
cf2f0 6b 70 61 74 68 29 3b 0a 20 20 72 65 74 75 72 6e kpath);. return
cf300 20 72 65 73 75 6c 74 3b 0a 7d 0a 23 65 6e 64 69 result;.}.#endi
cf310 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e f..#if SQLITE_EN
cf320 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
cf330 4c 45 0a 2f 2a 0a 2a 2a 20 54 65 73 74 73 20 61 LE./*.** Tests a
cf340 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b byte-range lock
cf350 69 6e 67 20 71 75 65 72 79 20 74 6f 20 73 65 65 ing query to see
cf360 20 69 66 20 62 79 74 65 20 72 61 6e 67 65 20 6c if byte range l
cf370 6f 63 6b 73 20 61 72 65 20 0a 2a 2a 20 73 75 70 ocks are .** sup
cf380 70 6f 72 74 65 64 2c 20 69 66 20 6e 6f 74 20 77 ported, if not w
cf390 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 64 e fall back to d
cf3a0 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79 otlockLockingSty
cf3b0 6c 65 2e 0a 2a 2a 20 4f 6e 20 76 78 57 6f 72 6b le..** On vxWork
cf3c0 73 20 77 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 s we fall back t
cf3d0 6f 20 6e 61 6d 65 64 73 65 6d 4c 6f 63 6b 69 6e o namedsemLockin
cf3e0 67 53 74 79 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 gStyle..*/.stati
cf3f0 63 20 69 6e 74 20 74 65 73 74 4c 6f 63 6b 69 6e c int testLockin
cf400 67 53 74 79 6c 65 28 69 6e 74 20 66 64 29 7b 0a gStyle(int fd){.
cf410 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c struct flock l
cf420 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 54 ockInfo;.. /* T
cf430 65 73 74 20 62 79 74 65 2d 72 61 6e 67 65 20 6c est byte-range l
cf440 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 ock using fcntl(
cf450 29 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 20 73 ). If the call s
cf460 75 63 63 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 ucceeds, . ** a
cf470 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 66 ssume that the f
cf480 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f ile-system suppo
cf490 72 74 73 20 50 4f 53 49 58 20 73 74 79 6c 65 20 rts POSIX style
cf4a0 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c locks. . */. l
cf4b0 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 ockInfo.l_len =
cf4c0 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 1;. lockInfo.l_
cf4d0 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 start = 0;. loc
cf4e0 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d kInfo.l_whence =
cf4f0 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 SEEK_SET;. loc
cf500 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 kInfo.l_type = F
cf510 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 _RDLCK;. if( fc
cf520 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c ntl(fd, F_GETLK,
cf530 20 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 &lockInfo)!=-1
cf540 29 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 4c ) {. return L
cf550 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 OCKING_STYLE_POS
cf560 49 58 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 IX;. }. . /*
cf570 54 65 73 74 69 6e 67 20 66 6f 72 20 66 6c 6f 63 Testing for floc
cf580 6b 28 29 20 63 61 6e 20 67 69 76 65 20 66 61 6c k() can give fal
cf590 73 65 20 70 6f 73 69 74 69 76 65 73 2e 20 20 53 se positives. S
cf5a0 6f 20 69 66 20 69 66 20 74 68 65 20 61 62 6f 76 o if if the abov
cf5b0 65 20 0a 20 20 2a 2a 20 74 65 73 74 20 66 61 69 e . ** test fai
cf5c0 6c 73 2c 20 74 68 65 6e 20 77 65 20 66 61 6c 6c ls, then we fall
cf5d0 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 64 back to using d
cf5e0 6f 74 2d 66 69 6c 65 20 73 74 79 6c 65 20 6c 6f ot-file style lo
cf5f0 63 6b 69 6e 67 20 28 6f 72 0a 20 20 2a 2a 20 6e cking (or. ** n
cf600 61 6d 65 64 2d 73 65 6d 61 70 68 6f 72 65 20 6c amed-semaphore l
cf610 6f 63 6b 69 6e 67 20 6f 6e 20 76 78 77 6f 72 6b ocking on vxwork
cf620 73 29 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 s).. */. retur
cf630 6e 20 28 49 53 5f 56 58 57 4f 52 4b 53 20 3f 20 n (IS_VXWORKS ?
cf640 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 41 LOCKING_STYLE_NA
cf650 4d 45 44 53 45 4d 20 3a 20 4c 4f 43 4b 49 4e 47 MEDSEM : LOCKING
cf660 5f 53 54 59 4c 45 5f 44 4f 54 46 49 4c 45 29 3b _STYLE_DOTFILE);
cf670 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a .}.#endif../* .*
cf680 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 * If SQLITE_ENAB
cf690 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
cf6a0 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 69 is defined, thi
cf6b0 73 20 66 75 6e 63 74 69 6f 6e 20 45 78 61 6d 69 s function Exami
cf6c0 6e 65 73 20 74 68 65 20 0a 2a 2a 20 66 5f 66 73 nes the .** f_fs
cf6d0 74 79 70 65 6e 61 6d 65 20 65 6e 74 72 79 20 69 typename entry i
cf6e0 6e 20 74 68 65 20 73 74 61 74 66 73 20 73 74 72 n the statfs str
cf6f0 75 63 74 75 72 65 20 61 73 20 72 65 74 75 72 6e ucture as return
cf700 65 64 20 62 79 20 73 74 61 74 28 29 20 66 6f 72 ed by stat() for
cf710 20 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 73 79 .** the file sy
cf720 73 74 65 6d 20 68 6f 73 74 69 6e 67 20 74 68 65 stem hosting the
cf730 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
cf740 6e 64 20 73 65 6c 65 63 74 73 20 20 74 68 65 20 nd selects the
cf750 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 6c appropriate.** l
cf760 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 62 61 73 ocking style bas
cf770 65 64 20 6f 6e 20 69 74 73 20 76 61 6c 75 65 2e ed on its value.
cf780 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 61 These values a
cf790 6e 64 20 61 73 73 69 67 6e 6d 65 6e 74 73 20 61 nd assignments a
cf7a0 72 65 20 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 re .** based on
cf7b0 44 61 72 77 69 6e 2f 4f 53 58 20 62 65 68 61 76 Darwin/OSX behav
cf7c0 69 6f 72 20 61 6e 64 20 68 61 76 65 20 6e 6f 74 ior and have not
cf7d0 20 62 65 65 6e 20 74 68 6f 72 6f 75 67 68 6c 79 been thoroughly
cf7e0 20 74 65 73 74 65 64 20 6f 6e 20 0a 2a 2a 20 6f tested on .** o
cf7f0 74 68 65 72 20 73 79 73 74 65 6d 73 2e 0a 2a 2a ther systems..**
cf800 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e .** If SQLITE_EN
cf810 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
cf820 4c 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 LE is not define
cf830 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e d, this function
cf840 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 always.** retur
cf850 6e 73 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 ns LOCKING_STYLE
cf860 5f 50 4f 53 49 58 2e 0a 2a 2f 0a 23 69 66 20 53 _POSIX..*/.#if S
cf870 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
cf880 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69 KING_STYLE.stati
cf890 63 20 69 6e 74 20 64 65 74 65 63 74 4c 6f 63 6b c int detectLock
cf8a0 69 6e 67 53 74 79 6c 65 28 0a 20 20 73 71 6c 69 ingStyle(. sqli
cf8b0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20 te3_vfs *pVfs,.
cf8c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c const char *fil
cf8d0 65 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 64 ePath, . int fd
cf8e0 0a 29 7b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 .){.#if IS_VXWOR
cf8f0 4b 53 0a 20 20 69 66 28 20 21 66 69 6c 65 50 61 KS. if( !filePa
cf900 74 68 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e th ){. return
cf910 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e LOCKING_STYLE_N
cf920 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 ONE;. }. if( p
cf930 56 66 73 2d 3e 70 41 70 70 44 61 74 61 20 29 7b Vfs->pAppData ){
cf940 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
cf950 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56 TE_PTR_TO_INT(pV
cf960 66 73 2d 3e 70 41 70 70 44 61 74 61 29 3b 0a 20 fs->pAppData);.
cf970 20 7d 0a 20 20 69 66 20 28 61 63 63 65 73 73 28 }. if (access(
cf980 66 69 6c 65 50 61 74 68 2c 20 30 29 20 21 3d 20 filePath, 0) !=
cf990 2d 31 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 -1){. return
cf9a0 74 65 73 74 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 testLockingStyle
cf9b0 28 66 64 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a (fd);. }.#else.
cf9c0 20 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 struct Mapping
cf9d0 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 {. const cha
cf9e0 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 0a r *zFilesystem;.
cf9f0 20 20 20 20 69 6e 74 20 65 4c 6f 63 6b 69 6e 67 int eLocking
cfa00 53 74 79 6c 65 3b 0a 20 20 7d 20 61 4d 61 70 5b Style;. } aMap[
cfa10 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 73 ] = {. { "hfs
cfa20 22 2c 20 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 ", LOCKING_ST
cfa30 59 4c 45 5f 50 4f 53 49 58 20 7d 2c 0a 20 20 20 YLE_POSIX },.
cfa40 20 7b 20 22 75 66 73 22 2c 20 20 20 20 4c 4f 43 { "ufs", LOC
cfa50 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 KING_STYLE_POSIX
cfa60 20 7d 2c 0a 20 20 20 20 7b 20 22 61 66 70 66 73 },. { "afpfs
cfa70 22 2c 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c ", LOCKING_STYL
cfa80 45 5f 41 46 50 20 7d 2c 0a 23 69 66 64 65 66 20 E_AFP },.#ifdef
cfa90 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46 SQLITE_ENABLE_AF
cfaa0 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 P_LOCKING_SMB.
cfab0 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 4c 4f { "smbfs", LO
cfac0 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 41 46 50 20 CKING_STYLE_AFP
cfad0 7d 2c 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20 22 },.#else. { "
cfae0 73 6d 62 66 73 22 2c 20 20 4c 4f 43 4b 49 4e 47 smbfs", LOCKING
cfaf0 5f 53 54 59 4c 45 5f 46 4c 4f 43 4b 20 7d 2c 0a _STYLE_FLOCK },.
cfb00 23 65 6e 64 69 66 0a 20 20 20 20 7b 20 22 6d 73 #endif. { "ms
cfb10 64 6f 73 22 2c 20 20 4c 4f 43 4b 49 4e 47 5f 53 dos", LOCKING_S
cfb20 54 59 4c 45 5f 44 4f 54 46 49 4c 45 20 7d 2c 0a TYLE_DOTFILE },.
cfb30 20 20 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 { "webdav",
cfb40 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f LOCKING_STYLE_NO
cfb50 4e 45 20 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30 NE },. { 0, 0
cfb60 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b }. };. int i;
cfb70 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 66 73 . struct statfs
cfb80 20 66 73 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 fsInfo;.. if(
cfb90 21 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 !filePath ){.
cfba0 20 72 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f return LOCKING_
cfbb0 53 54 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a STYLE_NONE;. }.
cfbc0 20 20 69 66 28 20 70 56 66 73 2d 3e 70 41 70 70 if( pVfs->pApp
cfbd0 44 61 74 61 20 29 7b 0a 20 20 20 20 72 65 74 75 Data ){. retu
cfbe0 72 6e 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f rn SQLITE_PTR_TO
cfbf0 5f 49 4e 54 28 70 56 66 73 2d 3e 70 41 70 70 44 _INT(pVfs->pAppD
cfc00 61 74 61 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 ata);. }.. if(
cfc10 20 73 74 61 74 66 73 28 66 69 6c 65 50 61 74 68 statfs(filePath
cfc20 2c 20 26 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 , &fsInfo) != -1
cfc30 20 29 7b 0a 20 20 20 20 69 66 28 20 66 73 49 6e ){. if( fsIn
cfc40 66 6f 2e 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54 fo.f_flags & MNT
cfc50 5f 52 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 _RDONLY ){.
cfc60 20 72 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f return LOCKING_
cfc70 53 54 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 20 20 STYLE_NONE;.
cfc80 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 }. for(i=0; a
cfc90 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 Map[i].zFilesyst
cfca0 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 em; i++){.
cfcb0 69 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66 if( strcmp(fsInf
cfcc0 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 o.f_fstypename,
cfcd0 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 aMap[i].zFilesys
cfce0 74 65 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 tem)==0 ){.
cfcf0 20 20 20 72 65 74 75 72 6e 20 61 4d 61 70 5b 69 return aMap[i
cfd00 5d 2e 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b ].eLockingStyle;
cfd10 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
cfd20 20 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 }.. /* Default
cfd30 20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 case. Handles,
cfd40 61 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 amongst others,
cfd50 22 6e 66 73 22 2e 20 2a 2f 0a 20 20 72 65 74 75 "nfs". */. retu
cfd60 72 6e 20 74 65 73 74 4c 6f 63 6b 69 6e 67 53 74 rn testLockingSt
cfd70 79 6c 65 28 66 64 29 3b 20 20 0a 23 65 6e 64 69 yle(fd); .#endi
cfd80 66 20 2f 2a 20 69 66 20 49 53 5f 56 58 57 4f 52 f /* if IS_VXWOR
cfd90 4b 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 4c KS */. return L
cfda0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 OCKING_STYLE_POS
cfdb0 49 58 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 IX;.}.#else. #d
cfdc0 65 66 69 6e 65 20 64 65 74 65 63 74 4c 6f 63 6b efine detectLock
cfdd0 69 6e 67 53 74 79 6c 65 28 78 2c 79 2c 7a 29 20 ingStyle(x,y,z)
cfde0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f LOCKING_STYLE_PO
cfdf0 53 49 58 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 SIX.#endif /* if
cfe00 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
cfe10 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
cfe20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 */../*.** Given
cfe30 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f a file descripto
cfe40 72 2c 20 6c 6f 63 61 74 65 20 6c 6f 63 6b 49 6e r, locate lockIn
cfe50 66 6f 20 61 6e 64 20 6f 70 65 6e 43 6e 74 20 73 fo and openCnt s
cfe60 74 72 75 63 74 75 72 65 73 20 74 68 61 74 0a 2a tructures that.*
cfe70 2a 20 64 65 73 63 72 69 62 65 73 20 74 68 61 74 * describes that
cfe80 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
cfe90 2e 20 20 43 72 65 61 74 65 20 6e 65 77 20 6f 6e . Create new on
cfea0 65 73 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e es if necessary.
cfeb0 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 The.** return
cfec0 76 61 6c 75 65 73 20 6d 69 67 68 74 20 62 65 20 values might be
cfed0 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 uninitialized if
cfee0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
cfef0 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ..**.** Return a
cff00 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 n appropriate er
cff10 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 ror code..*/.sta
cff20 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b tic int findLock
cff30 49 6e 66 6f 28 0a 20 20 69 6e 74 20 66 64 2c 20 Info(. int fd,
cff40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cff50 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 /* The file
cff60 20 64 65 73 63 72 69 70 74 6f 72 20 75 73 65 64 descriptor used
cff70 20 69 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a 23 in the key */.#
cff80 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if IS_VXWORKS.
cff90 76 6f 69 64 20 2a 72 6e 61 6d 2c 20 20 20 20 20 void *rnam,
cffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
cffb0 76 78 57 6f 72 6b 73 20 72 65 61 6c 6e 61 6d 65 vxWorks realname
cffc0 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 73 74 72 */.#endif. str
cffd0 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 uct lockInfo **p
cffe0 70 4c 6f 63 6b 2c 20 20 20 20 2f 2a 20 52 65 74 pLock, /* Ret
cfff0 75 72 6e 20 74 68 65 20 6c 6f 63 6b 49 6e 66 6f urn the lockInfo
d0000 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 structure here
d0010 2a 2f 0a 20 20 73 74 72 75 63 74 20 6f 70 65 6e */. struct open
d0020 43 6e 74 20 2a 2a 70 70 4f 70 65 6e 20 20 20 20 Cnt **ppOpen
d0030 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 /* Return the
d0040 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 openCnt structur
d0050 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 e here */.){. i
d0060 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 nt rc;. struct
d0070 6c 6f 63 6b 4b 65 79 20 6b 65 79 31 3b 0a 20 20 lockKey key1;.
d0080 73 74 72 75 63 74 20 6f 70 65 6e 4b 65 79 20 6b struct openKey k
d0090 65 79 32 3b 0a 20 20 73 74 72 75 63 74 20 73 74 ey2;. struct st
d00a0 61 74 20 73 74 61 74 62 75 66 3b 0a 20 20 73 74 at statbuf;. st
d00b0 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 ruct lockInfo *p
d00c0 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 6f Lock;. struct o
d00d0 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a 20 penCnt *pOpen;.
d00e0 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c 20 rc = fstat(fd,
d00f0 26 73 74 61 74 62 75 66 29 3b 0a 20 20 69 66 28 &statbuf);. if(
d0100 20 72 63 21 3d 30 20 29 7b 0a 23 69 66 64 65 66 rc!=0 ){.#ifdef
d0110 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20 69 EOVERFLOW. i
d0120 66 28 20 65 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 f( errno==EOVERF
d0130 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c LOW ) return SQL
d0140 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 ITE_NOLFS;.#endi
d0150 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c f. return SQL
d0160 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a ITE_IOERR;. }..
d0170 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 /* On OS X on
d0180 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 an msdos filesys
d0190 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e tem, the inode n
d01a0 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 umber is reporte
d01b0 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 d. ** incorrect
d01c0 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 ly for zero-size
d01d0 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 files. See tic
d01e0 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 ket #3260. To w
d01f0 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 ork. ** around
d0200 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 this problem (we
d0210 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 consider it a b
d0220 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 ug in OS X, not
d0230 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 SQLite). ** we
d0240 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 always increase
d0250 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f the file size to
d0260 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 1 by writing a
d0270 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a single byte. **
d0280 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 prior to access
d0290 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 ing the inode nu
d02a0 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 mber. The one b
d02b0 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 yte written is.
d02c0 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 ** an ASCII 'S'
d02d0 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63 68 character which
d02e0 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f also happens to
d02f0 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62 79 be the first by
d0300 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 te. ** in the h
d0310 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 eader of every S
d0320 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 QLite database.
d0330 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 In this way, if
d0340 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 there. ** is a
d0350 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 race condition
d0360 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 such that anothe
d0370 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 r thread has alr
d0380 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 eady populated.
d0390 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 ** the first pa
d03a0 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ge of the databa
d03b0 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 se, no damage is
d03c0 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 done.. */. if
d03d0 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a ( statbuf.st_siz
d03e0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 77 72 69 74 e==0 ){. writ
d03f0 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b 0a 20 e(fd, "S", 1);.
d0400 20 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 rc = fstat(fd
d0410 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 20 , &statbuf);.
d0420 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 if( rc!=0 ){.
d0430 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d0440 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 E_IOERR;. }.
d0450 20 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6b 65 }.. memset(&ke
d0460 79 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 y1, 0, sizeof(ke
d0470 79 31 29 29 3b 0a 20 20 6b 65 79 31 2e 64 65 76 y1));. key1.dev
d0480 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 64 65 = statbuf.st_de
d0490 76 3b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b v;.#if IS_VXWORK
d04a0 53 0a 20 20 6b 65 79 31 2e 72 6e 61 6d 20 3d 20 S. key1.rnam =
d04b0 72 6e 61 6d 3b 0a 23 65 6c 73 65 0a 20 20 6b 65 rnam;.#else. ke
d04c0 79 31 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 y1.ino = statbuf
d04d0 2e 73 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a .st_ino;.#endif.
d04e0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
d04f0 44 53 41 46 45 0a 20 20 69 66 28 20 74 68 72 65 DSAFE. if( thre
d0500 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f adsOverrideEachO
d0510 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b 0a thersLocks<0 ){.
d0520 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c 6f testThreadLo
d0530 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66 64 ckingBehavior(fd
d0540 29 3b 0a 20 20 7d 0a 20 20 6b 65 79 31 2e 74 69 );. }. key1.ti
d0550 64 20 3d 20 74 68 72 65 61 64 73 4f 76 65 72 72 d = threadsOverr
d0560 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 ideEachOthersLoc
d0570 6b 73 20 3f 20 30 20 3a 20 70 74 68 72 65 61 64 ks ? 0 : pthread
d0580 5f 73 65 6c 66 28 29 3b 0a 23 65 6e 64 69 66 0a _self();.#endif.
d0590 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 32 2c 20 memset(&key2,
d05a0 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79 32 29 29 0, sizeof(key2))
d05b0 3b 0a 20 20 6b 65 79 32 2e 64 65 76 20 3d 20 73 ;. key2.dev = s
d05c0 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 tatbuf.st_dev;.#
d05d0 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if IS_VXWORKS.
d05e0 6b 65 79 32 2e 72 6e 61 6d 20 3d 20 72 6e 61 6d key2.rnam = rnam
d05f0 3b 0a 23 65 6c 73 65 0a 20 20 6b 65 79 32 2e 69 ;.#else. key2.i
d0600 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f no = statbuf.st_
d0610 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4c ino;.#endif. pL
d0620 6f 63 6b 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b 0a ock = lockList;.
d0630 20 20 77 68 69 6c 65 28 20 70 4c 6f 63 6b 20 26 while( pLock &
d0640 26 20 6d 65 6d 63 6d 70 28 26 6b 65 79 31 2c 20 & memcmp(&key1,
d0650 26 70 4c 6f 63 6b 2d 3e 6b 65 79 2c 20 73 69 7a &pLock->key, siz
d0660 65 6f 66 28 6b 65 79 31 29 29 20 29 7b 0a 20 20 eof(key1)) ){.
d0670 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 2d pLock = pLock-
d0680 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 >pNext;. }. if
d0690 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 ( pLock==0 ){.
d06a0 20 20 70 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 pLock = sqlite
d06b0 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 3_malloc( sizeof
d06c0 28 2a 70 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 20 (*pLock) );.
d06d0 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a if( pLock==0 ){.
d06e0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
d06f0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 E_NOMEM;. g
d0700 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 oto exit_findloc
d0710 6b 69 6e 66 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 kinfo;. }.
d0720 20 70 4c 6f 63 6b 2d 3e 6b 65 79 20 3d 20 6b 65 pLock->key = ke
d0730 79 31 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e y1;. pLock->n
d0740 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4c 6f Ref = 1;. pLo
d0750 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 ck->cnt = 0;.
d0760 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 pLock->locktype
d0770 20 3d 20 30 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d = 0;. pLock-
d0780 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 73 >pNext = lockLis
d0790 74 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 t;. pLock->pP
d07a0 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 rev = 0;. if(
d07b0 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c 6f 63 6b lockList ) lock
d07c0 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c List->pPrev = pL
d07d0 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 4c 69 73 ock;. lockLis
d07e0 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 65 6c t = pLock;. }el
d07f0 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e se{. pLock->n
d0800 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 70 Ref++;. }. *pp
d0810 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 Lock = pLock;.
d0820 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 7b if( ppOpen!=0 ){
d0830 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70 65 . pOpen = ope
d0840 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 nList;. while
d0850 28 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63 6d ( pOpen && memcm
d0860 70 28 26 6b 65 79 32 2c 20 26 70 4f 70 65 6e 2d p(&key2, &pOpen-
d0870 3e 6b 65 79 2c 20 73 69 7a 65 6f 66 28 6b 65 79 >key, sizeof(key
d0880 32 29 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 2)) ){. pOp
d0890 65 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 en = pOpen->pNex
d08a0 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 t;. }. if(
d08b0 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 pOpen==0 ){.
d08c0 20 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 pOpen = sqlit
d08d0 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f e3_malloc( sizeo
d08e0 66 28 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 f(*pOpen) );.
d08f0 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 if( pOpen==0
d0900 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 ){. relea
d0910 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b seLockInfo(pLock
d0920 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
d0930 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
d0940 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f goto exit_
d0950 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 findlockinfo;.
d0960 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 65 }. pOpe
d0970 6e 2d 3e 6b 65 79 20 3d 20 6b 65 79 32 3b 0a 20 n->key = key2;.
d0980 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 pOpen->nRef
d0990 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65 = 1;. pOpe
d09a0 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 n->nLock = 0;.
d09b0 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 pOpen->nPend
d09c0 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 ing = 0;. p
d09d0 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d Open->aPending =
d09e0 20 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 0;. pOpen-
d09f0 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 73 >pNext = openLis
d0a00 74 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e t;. pOpen->
d0a10 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 pPrev = 0;.
d0a20 20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 if( openList )
d0a30 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 openList->pPrev
d0a40 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f = pOpen;. o
d0a50 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b penList = pOpen;
d0a60 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a .#if IS_VXWORKS.
d0a70 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 53 65 pOpen->pSe
d0a80 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 m = NULL;.
d0a90 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b pOpen->aSemName[
d0aa0 30 5d 20 3d 20 27 5c 30 27 3b 0a 23 65 6e 64 69 0] = '\0';.#endi
d0ab0 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 f. }else{.
d0ac0 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b pOpen->nRef++
d0ad0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f ;. }. *ppO
d0ae0 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d pen = pOpen;. }
d0af0 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 ..exit_findlocki
d0b00 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 nfo:. return rc
d0b10 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
d0b20 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 TE_DEBUG./*.** H
d0b30 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 elper function f
d0b40 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 or printing out
d0b50 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f trace informatio
d0b60 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 n from debugging
d0b70 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 .** binaries. Th
d0b80 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 is returns the s
d0b90 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 tring represetat
d0ba0 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c ion of the suppl
d0bb0 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c ied.** integer l
d0bc0 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 ock-type..*/.sta
d0bd0 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
d0be0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 locktypeName(int
d0bf0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 locktype){. sw
d0c00 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 itch( locktype )
d0c10 7b 0a 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b {. case NO_LOCK
d0c20 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b : return "NONE";
d0c30 0a 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c . case SHARED_L
d0c40 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 OCK: return "SHA
d0c50 52 45 44 22 3b 0a 20 20 63 61 73 65 20 52 45 53 RED";. case RES
d0c60 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 ERVED_LOCK: retu
d0c70 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20 rn "RESERVED";.
d0c80 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f case PENDING_LO
d0c90 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e 44 CK: return "PEND
d0ca0 49 4e 47 22 3b 0a 20 20 63 61 73 65 20 45 58 43 ING";. case EXC
d0cb0 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74 LUSIVE_LOCK: ret
d0cc0 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b urn "EXCLUSIVE";
d0cd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45 . }. return "E
d0ce0 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a RROR";.}.#endif.
d0cf0 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 ./*.** If we are
d0d00 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 currently in a
d0d10 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 different thread
d0d20 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 61 64 than the thread
d0d30 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 6e 69 that the.** uni
d0d40 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 20 62 xFile argument b
d0d50 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 6e 20 elongs to, then
d0d60 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 transfer ownersh
d0d70 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 ip of the unixFi
d0d80 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68 le.** over to th
d0d90 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 e current thread
d0da0 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 46 69 ..**.** A unixFi
d0db0 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e 65 64 le is only owned
d0dc0 20 62 79 20 61 20 74 68 72 65 61 64 20 6f 6e 20 by a thread on
d0dd0 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6f 6e systems where on
d0de0 65 20 74 68 72 65 61 64 20 69 73 0a 2a 2a 20 75 e thread is.** u
d0df0 6e 61 62 6c 65 20 74 6f 20 6f 76 65 72 72 69 64 nable to overrid
d0e00 65 20 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20 e locks created
d0e10 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 by a different t
d0e20 68 72 65 61 64 2e 20 20 52 65 64 48 61 74 39 20 hread. RedHat9
d0e30 69 73 0a 2a 2a 20 61 6e 20 65 78 61 6d 70 6c 65 is.** an example
d0e40 20 6f 66 20 73 75 63 68 20 61 20 73 79 73 74 65 of such a syste
d0e50 6d 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 m..**.** Ownersh
d0e60 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f ip transfer is o
d0e70 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74 nly allowed if t
d0e80 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63 he unixFile is c
d0e90 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 urrently unlocke
d0ea0 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69 d..** If the uni
d0eb0 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 xFile is locked
d0ec0 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70 and an ownership
d0ed0 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20 is wrong, then
d0ee0 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 return.** SQLITE
d0ef0 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45 _MISUSE. SQLITE
d0f00 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 _OK is returned
d0f10 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f if everything wo
d0f20 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 rks..*/.#if SQLI
d0f30 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 73 74 TE_THREADSAFE.st
d0f40 61 74 69 63 20 69 6e 74 20 74 72 61 6e 73 66 65 atic int transfe
d0f50 72 4f 77 6e 65 72 73 68 69 70 28 75 6e 69 78 46 rOwnership(unixF
d0f60 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 ile *pFile){. i
d0f70 6e 74 20 72 63 3b 0a 20 20 70 74 68 72 65 61 64 nt rc;. pthread
d0f80 5f 74 20 68 53 65 6c 66 3b 0a 20 20 69 66 28 20 _t hSelf;. if(
d0f90 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 threadsOverrideE
d0fa0 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 29 achOthersLocks )
d0fb0 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e 65 72 73 68 {. /* Ownersh
d0fc0 69 70 20 74 72 61 6e 73 66 65 72 73 20 6e 6f 74 ip transfers not
d0fd0 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 69 73 20 needed on this
d0fe0 73 79 73 74 65 6d 20 2a 2f 0a 20 20 20 20 72 65 system */. re
d0ff0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d1000 20 20 7d 0a 20 20 68 53 65 6c 66 20 3d 20 70 74 }. hSelf = pt
d1010 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 hread_self();.
d1020 69 66 28 20 70 74 68 72 65 61 64 5f 65 71 75 61 if( pthread_equa
d1030 6c 28 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 l(pFile->tid, hS
d1040 65 6c 66 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57 elf) ){. /* W
d1050 65 20 61 72 65 20 73 74 69 6c 6c 20 69 6e 20 74 e are still in t
d1060 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 2a he same thread *
d1070 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 22 /. OSTRACE1("
d1080 4e 6f 2d 74 72 61 6e 73 66 65 72 2c 20 73 61 6d No-transfer, sam
d1090 65 20 74 68 72 65 61 64 5c 6e 22 29 3b 0a 20 20 e thread\n");.
d10a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d10b0 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 OK;. }. if( pF
d10c0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e ile->locktype!=N
d10d0 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a O_LOCK ){. /*
d10e0 20 57 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 We cannot chang
d10f0 65 20 6f 77 6e 65 72 73 68 69 70 20 77 68 69 6c e ownership whil
d1100 65 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 e we are holding
d1110 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20 20 20 20 a lock! */.
d1120 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
d1130 53 55 53 45 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 SUSE;. }. OSTR
d1140 41 43 45 34 28 22 54 72 61 6e 73 66 65 72 20 6f ACE4("Transfer o
d1150 77 6e 65 72 73 68 69 70 20 6f 66 20 25 64 20 66 wnership of %d f
d1160 72 6f 6d 20 25 64 20 74 6f 20 25 64 5c 6e 22 2c rom %d to %d\n",
d1170 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 46 69 . pFi
d1180 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 74 69 le->h, pFile->ti
d1190 64 2c 20 68 53 65 6c 66 29 3b 0a 20 20 70 46 69 d, hSelf);. pFi
d11a0 6c 65 2d 3e 74 69 64 20 3d 20 68 53 65 6c 66 3b le->tid = hSelf;
d11b0 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 70 4c . if (pFile->pL
d11c0 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20 ock != NULL) {.
d11d0 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e releaseLockIn
d11e0 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 fo(pFile->pLock)
d11f0 3b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 ;.#if IS_VXWORKS
d1200 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f . rc = findLo
d1210 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 68 2c ckInfo(pFile->h,
d1220 20 70 46 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 pFile->zRealpat
d1230 68 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b h, &pFile->pLock
d1240 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 , 0);.#else.
d1250 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 rc = findLockInf
d1260 6f 28 70 46 69 6c 65 2d 3e 68 2c 20 26 70 46 69 o(pFile->h, &pFi
d1270 6c 65 2d 3e 70 4c 6f 63 6b 2c 20 30 29 3b 0a 23 le->pLock, 0);.#
d1280 65 6e 64 69 66 0a 20 20 20 20 4f 53 54 52 41 43 endif. OSTRAC
d1290 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69 E5("LOCK %d i
d12a0 73 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c s now %s(%s,%d)\
d12b0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
d12c0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 locktyp
d12d0 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 eName(pFile->loc
d12e0 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20 ktype),.
d12f0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 locktypeName(
d1300 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f pFile->pLock->lo
d1310 63 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e cktype), pFile->
d1320 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20 pLock->cnt);.
d1330 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 return rc;. }
d1340 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 else {. retur
d1350 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d1360 0a 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 4f 6e .}.#else. /* On
d1370 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 single-threaded
d1380 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68 builds, ownersh
d1390 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 61 ip transfer is a
d13a0 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 no-op */.# defi
d13b0 6e 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 ne transferOwner
d13c0 73 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f ship(X) SQLITE_O
d13d0 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 K.#endif../*.**
d13e0 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 Seek to the offs
d13f0 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 et passed as the
d1400 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
d1410 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 , then read cnt
d1420 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 70 .** bytes into p
d1430 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65 20 Buf. Return the
d1440 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
d1450 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a actually read..*
d1460 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 *.** NB: If you
d1470 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 define USE_PREA
d1480 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36 34 D or USE_PREAD64
d1490 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 , then it might
d14a0 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 also.** be neces
d14b0 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20 5f sary to define _
d14c0 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 XOPEN_SOURCE to
d14d0 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76 61 be 500. This va
d14e0 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 ries from.** one
d14f0 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 system to anoth
d1500 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 74 er. Since SQLit
d1510 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e e does not defin
d1520 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 e USE_PREAD.** a
d1530 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 ny any form by d
d1540 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 efault, we will
d1550 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 not attempt to d
d1560 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 efine _XOPEN_SOU
d1570 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b RCE..** See tick
d1580 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23 32 ets #2741 and #2
d1590 36 38 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 681..*/.static i
d15a0 6e 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 nt seekAndRead(u
d15b0 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c nixFile *id, sql
d15c0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 ite3_int64 offse
d15d0 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 t, void *pBuf, i
d15e0 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 nt cnt){. int g
d15f0 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 ot;. i64 newOff
d1600 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 set;. TIMER_STA
d1610 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 RT;.#if defined(
d1620 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 USE_PREAD). got
d1630 20 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 = pread(id->h,
d1640 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 pBuf, cnt, offse
d1650 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f t);. SimulateIO
d1660 45 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 Error( got = -1
d1670 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 );.#elif defined
d1680 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 (USE_PREAD64).
d1690 67 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 got = pread64(id
d16a0 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 ->h, pBuf, cnt,
d16b0 6f 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c offset);. Simul
d16c0 61 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 ateIOError( got
d16d0 3d 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 = -1 );.#else.
d16e0 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 newOffset = lsee
d16f0 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c k(id->h, offset,
d1700 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 SEEK_SET);. Si
d1710 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e mulateIOError( n
d1720 65 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 ewOffset-- );.
d1730 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f if( newOffset!=o
d1740 66 66 73 65 74 20 29 7b 0a 20 20 20 20 72 65 74 ffset ){. ret
d1750 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f urn -1;. }. go
d1760 74 20 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 t = read(id->h,
d1770 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 pBuf, cnt);.#end
d1780 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a if. TIMER_END;.
d1790 20 20 4f 53 54 52 41 43 45 35 28 22 52 45 41 44 OSTRACE5("READ
d17a0 20 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c %-3d %5d %7l
d17b0 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e ld %llu\n", id->
d17c0 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 h, got, offset,
d17d0 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a TIMER_ELAPSED);.
d17e0 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a return got;.}.
d17f0 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
d1800 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 from a file int
d1810 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 o a buffer. Ret
d1820 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
d1830 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 all.** bytes we
d1840 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66 re read successf
d1850 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f ully and SQLITE_
d1860 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e IOERR if anythin
d1870 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e g goes.** wrong.
d1880 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
d1890 6e 69 78 52 65 61 64 28 0a 20 20 73 71 6c 69 74 nixRead(. sqlit
d18a0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 e3_file *id, .
d18b0 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 20 69 void *pBuf, . i
d18c0 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65 nt amt,. sqlite
d18d0 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 0a 29 3_int64 offset.)
d18e0 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 {. int got;. a
d18f0 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 67 ssert( id );. g
d1900 6f 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 ot = seekAndRead
d1910 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20 ((unixFile*)id,
d1920 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d offset, pBuf, am
d1930 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 t);. if( got==a
d1940 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e mt ){. return
d1950 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
d1960 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b lse if( got<0 ){
d1970 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d1980 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 TE_IOERR_READ;.
d1990 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55 }else{. /* U
d19a0 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 nread parts of t
d19b0 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 he buffer must b
d19c0 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f e zero-filled */
d19d0 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 . memset(&((c
d19e0 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c har*)pBuf)[got],
d19f0 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 0, amt-got);.
d1a00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d1a10 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
d1a20 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ;. }.}../*.** S
d1a30 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 eek to the offse
d1a40 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 t in id->offset
d1a50 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 then read cnt by
d1a60 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a tes into pBuf..*
d1a70 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
d1a80 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 ber of bytes act
d1a90 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 ually read. Upd
d1aa0 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a ate the offset..
d1ab0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 */.static int se
d1ac0 65 6b 41 6e 64 57 72 69 74 65 28 75 6e 69 78 46 ekAndWrite(unixF
d1ad0 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 6f 66 66 ile *id, i64 off
d1ae0 73 65 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 set, const void
d1af0 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b *pBuf, int cnt){
d1b00 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36 . int got;. i6
d1b10 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54 4 newOffset;. T
d1b20 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20 IMER_START;.#if
d1b30 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 defined(USE_PREA
d1b40 44 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 D). got = pwrit
d1b50 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 e(id->h, pBuf, c
d1b60 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c nt, offset);.#el
d1b70 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 if defined(USE_P
d1b80 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 READ64). got =
d1b90 70 77 72 69 74 65 36 34 28 69 64 2d 3e 68 2c 20 pwrite64(id->h,
d1ba0 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 pBuf, cnt, offse
d1bb0 74 29 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f t);.#else. newO
d1bc0 66 66 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 ffset = lseek(id
d1bd0 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 ->h, offset, SEE
d1be0 4b 5f 53 45 54 29 3b 0a 20 20 69 66 28 20 6e 65 K_SET);. if( ne
d1bf0 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 wOffset!=offset
d1c00 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 ){. return -1
d1c10 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 77 72 ;. }. got = wr
d1c20 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c ite(id->h, pBuf,
d1c30 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 cnt);.#endif.
d1c40 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 4f 53 54 TIMER_END;. OST
d1c50 52 41 43 45 35 28 22 57 52 49 54 45 20 20 20 25 RACE5("WRITE %
d1c60 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c -3d %5d %7lld %l
d1c70 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f lu\n", id->h, go
d1c80 74 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 t, offset, TIMER
d1c90 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 _ELAPSED);. ret
d1ca0 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a urn got;.}.../*.
d1cb0 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 ** Write data fr
d1cc0 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f om a buffer into
d1cd0 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e a file. Return
d1ce0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
d1cf0 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 ccess.** or some
d1d00 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 other error cod
d1d10 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f e on failure..*/
d1d20 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
d1d30 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 Write(. sqlite3
d1d40 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f _file *id, . co
d1d50 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 nst void *pBuf,
d1d60 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 . int amt,. sq
d1d70 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
d1d80 65 74 20 0a 29 7b 0a 20 20 69 6e 74 20 77 72 6f et .){. int wro
d1d90 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 te = 0;. assert
d1da0 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65 72 74 ( id );. assert
d1db0 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 ( amt>0 );. whi
d1dc0 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77 72 le( amt>0 && (wr
d1dd0 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 69 ote = seekAndWri
d1de0 74 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 te((unixFile*)id
d1df0 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 , offset, pBuf,
d1e00 61 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 amt))>0 ){. a
d1e10 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 mt -= wrote;.
d1e20 20 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 offset += wrote
d1e30 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 ;. pBuf = &((
d1e40 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 char*)pBuf)[wrot
d1e50 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 e];. }. Simula
d1e60 74 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 teIOError(( wrot
d1e70 65 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 e=(-1), amt=1 ))
d1e80 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b ;. SimulateDisk
d1e90 66 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 fullError(( wrot
d1ea0 65 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 e=0, amt=1 ));.
d1eb0 20 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 if( amt>0 ){.
d1ec0 20 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b if( wrote<0 ){
d1ed0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
d1ee0 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 LITE_IOERR_WRITE
d1ef0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
d1f00 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d1f10 5f 46 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d _FULL;. }. }
d1f20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
d1f30 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 _OK;.}..#ifdef S
d1f40 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a QLITE_TEST./*.**
d1f50 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 Count the numbe
d1f60 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 r of fullsyncs a
d1f70 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e nd normal syncs.
d1f80 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 This is used t
d1f90 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 o test.** that s
d1fa0 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e yncs and fullsyn
d1fb0 63 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 cs are occuring
d1fc0 61 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d at the right tim
d1fd0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 es..*/.SQLITE_AP
d1fe0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 I int sqlite3_sy
d1ff0 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 nc_count = 0;.SQ
d2000 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
d2010 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f ite3_fullsync_co
d2020 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a unt = 0;.#endif.
d2030 0a 2f 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 ./*.** Use the f
d2040 64 61 74 61 73 79 6e 63 28 29 20 41 50 49 20 6f datasync() API o
d2050 6e 6c 79 20 69 66 20 74 68 65 20 48 41 56 45 5f nly if the HAVE_
d2060 46 44 41 54 41 53 59 4e 43 20 6d 61 63 72 6f 20 FDATASYNC macro
d2070 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 4f is defined..** O
d2080 74 68 65 72 77 69 73 65 20 75 73 65 20 66 73 79 therwise use fsy
d2090 6e 63 28 29 20 69 6e 20 69 74 73 20 70 6c 61 63 nc() in its plac
d20a0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 48 41 e..*/.#ifndef HA
d20b0 56 45 5f 46 44 41 54 41 53 59 4e 43 0a 23 20 64 VE_FDATASYNC.# d
d20c0 65 66 69 6e 65 20 66 64 61 74 61 73 79 6e 63 20 efine fdatasync
d20d0 66 73 79 6e 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a fsync.#endif../*
d20e0 0a 2a 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f .** Define HAVE_
d20f0 46 55 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f FULLFSYNC to 0 o
d2100 72 20 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e r 1 depending on
d2110 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a whether or not.
d2120 2a 2a 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59 ** the F_FULLFSY
d2130 4e 43 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 NC macro is defi
d2140 6e 65 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e ned. F_FULLFSYN
d2150 43 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a C is currently.*
d2160 2a 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 * only available
d2170 20 6f 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20 42 on Mac OS X. B
d2180 75 74 20 74 68 61 74 20 63 6f 75 6c 64 20 63 68 ut that could ch
d2190 61 6e 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ange..*/.#ifdef
d21a0 46 5f 46 55 4c 4c 46 53 59 4e 43 0a 23 20 64 65 F_FULLFSYNC.# de
d21b0 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 fine HAVE_FULLFS
d21c0 59 4e 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 YNC 1.#else.# de
d21d0 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 fine HAVE_FULLFS
d21e0 59 4e 43 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f YNC 0.#endif.../
d21f0 2a 0a 2a 2a 20 54 68 65 20 66 73 79 6e 63 28 29 *.** The fsync()
d2200 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 64 6f 65 system call doe
d2210 73 20 6e 6f 74 20 77 6f 72 6b 20 61 73 20 61 64 s not work as ad
d2220 76 65 72 74 69 73 65 64 20 6f 6e 20 6d 61 6e 79 vertised on many
d2230 0a 2a 2a 20 75 6e 69 78 20 73 79 73 74 65 6d 73 .** unix systems
d2240 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
d2250 20 70 72 6f 63 65 64 75 72 65 20 69 73 20 61 6e procedure is an
d2260 20 61 74 74 65 6d 70 74 20 74 6f 20 6d 61 6b 65 attempt to make
d2270 0a 2a 2a 20 69 74 20 77 6f 72 6b 20 62 65 74 74 .** it work bett
d2280 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 er..**.** The SQ
d2290 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 LITE_NO_SYNC mac
d22a0 72 6f 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 ro disables all
d22b0 66 73 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20 fsync()s. This
d22c0 69 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 is useful.** for
d22d0 20 74 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65 testing when we
d22e0 20 77 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72 want to run thr
d22f0 6f 75 67 68 20 74 68 65 20 74 65 73 74 20 73 75 ough the test su
d2300 69 74 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 ite quickly..**
d2310 59 6f 75 20 61 72 65 20 73 74 72 6f 6e 67 6c 79 You are strongly
d2320 20 61 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20 74 advised *not* t
d2330 6f 20 64 65 70 6c 6f 79 20 77 69 74 68 20 53 51 o deploy with SQ
d2340 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 LITE_NO_SYNC.**
d2350 65 6e 61 62 6c 65 64 2c 20 68 6f 77 65 76 65 72 enabled, however
d2360 2c 20 73 69 6e 63 65 20 77 69 74 68 20 53 51 4c , since with SQL
d2370 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 ITE_NO_SYNC enab
d2380 6c 65 64 2c 20 61 6e 20 4f 53 20 63 72 61 73 68 led, an OS crash
d2390 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 .** or power fai
d23a0 6c 75 72 65 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 lure will likely
d23b0 20 63 6f 72 72 75 70 74 20 74 68 65 20 64 61 74 corrupt the dat
d23c0 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 abase file..*/.s
d23d0 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 tatic int full_f
d23e0 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 sync(int fd, int
d23f0 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 fullSync, int d
d2400 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 ataOnly){. int
d2410 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f rc;.. /* The fo
d2420 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65 llowing "ifdef/e
d2430 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b lif/else/" block
d2440 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74 has the same st
d2450 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20 ructure as. **
d2460 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49 the one below. I
d2470 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20 t is replicated
d2480 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61 here solely to a
d2490 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20 void cluttering
d24a0 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61 . ** up the rea
d24b0 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20 l code with the
d24c0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
d24d0 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a () macros.. */.
d24e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f #ifdef SQLITE_NO
d24f0 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 _SYNC. UNUSED_P
d2500 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20 ARAMETER(fd);.
d2510 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
d2520 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e (fullSync);. UN
d2530 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 USED_PARAMETER(d
d2540 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20 ataOnly);.#elif
d2550 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20 HAVE_FULLFSYNC.
d2560 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
d2570 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c R(dataOnly);.#el
d2580 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 se. UNUSED_PARA
d2590 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b METER(fullSync);
d25a0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 .#endif.. /* Re
d25b0 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 cord the number
d25c0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 of times that we
d25d0 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 do a normal fsy
d25e0 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 nc() and . ** F
d25f0 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 ULLSYNC. This i
d2600 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 s used during te
d2610 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 sting to verify
d2620 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 that this proced
d2630 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 ure. ** gets ca
d2640 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f lled with the co
d2650 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e rrect arguments.
d2660 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
d2670 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 ITE_TEST. if( f
d2680 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 ullSync ) sqlite
d2690 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 3_fullsync_count
d26a0 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 ++;. sqlite3_sy
d26b0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 nc_count++;.#end
d26c0 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 if.. /* If we c
d26d0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
d26e0 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 SQLITE_NO_SYNC
d26f0 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 flag, then synci
d2700 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d ng is a. ** no-
d2710 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 op. */.#ifdef S
d2720 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 QLITE_NO_SYNC.
d2730 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d2740 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 #elif HAVE_FULLF
d2750 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 SYNC. if( fullS
d2760 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ync ){. rc =
d2770 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c fcntl(fd, F_FULL
d2780 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c FSYNC, 0);. }el
d2790 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a se{. rc = 1;.
d27a0 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }. /* If the
d27b0 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 FULLFSYNC failed
d27c0 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 , fall back to a
d27d0 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 ttempting an fsy
d27e0 6e 63 28 29 2e 0a 20 20 20 2a 20 49 74 20 73 68 nc().. * It sh
d27f0 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 ouldn't be possi
d2800 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e ble for fullfsyn
d2810 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 c to fail on the
d2820 20 6c 6f 63 61 6c 20 0a 20 20 20 2a 20 66 69 6c local . * fil
d2830 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 e system (on OSX
d2840 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e ), so failure in
d2850 64 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c dicates that FUL
d2860 4c 46 53 59 4e 43 0a 20 20 20 2a 20 69 73 6e 27 LFSYNC. * isn'
d2870 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 t supported for
d2880 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d this file system
d2890 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e . So, attempt an
d28a0 20 66 73 79 6e 63 20 0a 20 20 20 2a 20 61 6e 64 fsync . * and
d28b0 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 (for now) ignor
d28c0 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f e the overhead o
d28d0 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 f a superfluous
d28e0 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 fcntl call. .
d28f0 20 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65 * It'd be bette
d2900 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c r to detect full
d2910 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e fsync support on
d2920 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 ce and avoid .
d2930 20 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c * the fcntl cal
d2940 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e l every time syn
d2950 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20 c is called..
d2960 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 63 */. if( rc ) rc
d2970 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 = fsync(fd);..#
d2980 65 6c 73 65 20 0a 20 20 69 66 28 20 64 61 74 61 else . if( data
d2990 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d Only ){. rc =
d29a0 20 66 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a fdatasync(fd);.
d29b0 20 20 20 20 69 66 28 20 49 53 5f 56 58 57 4f 52 if( IS_VXWOR
d29c0 4b 53 20 26 26 20 72 63 3d 3d 2d 31 20 26 26 20 KS && rc==-1 &&
d29d0 65 72 72 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 errno==ENOTSUP )
d29e0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 73 79 {. rc = fsy
d29f0 6e 63 28 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 nc(fd);. }.
d2a00 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
d2a10 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 7d 0a 23 fsync(fd);. }.#
d2a20 65 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53 endif /* ifdef S
d2a30 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c QLITE_NO_SYNC el
d2a40 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e if HAVE_FULLFSYN
d2a50 43 20 2a 2f 0a 0a 20 20 69 66 28 20 49 53 5f 56 C */.. if( IS_V
d2a60 58 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d XWORKS && rc!= -
d2a70 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 1 ){. rc = 0;
d2a80 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
d2a90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 ;.}../*.** Make
d2aa0 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 sure all writes
d2ab0 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 to a particular
d2ac0 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 file are committ
d2ad0 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a ed to disk..**.*
d2ae0 2a 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 * If dataOnly==0
d2af0 20 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 then both the f
d2b00 69 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 ile itself and i
d2b10 74 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c ts metadata (fil
d2b20 65 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 e.** size, acces
d2b30 73 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 s time, etc) are
d2b40 20 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 synced. If dat
d2b50 61 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e aOnly!=0 then on
d2b60 6c 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 ly the.** file d
d2b70 61 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a ata is synced..*
d2b80 2a 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c *.** Under Unix,
d2b90 20 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 also make sure
d2ba0 74 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f that the directo
d2bb0 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ry entry for the
d2bc0 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 file.** has bee
d2bd0 6e 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79 n created by fsy
d2be0 6e 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 nc-ing the direc
d2bf0 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 tory that contai
d2c00 6e 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 ns the file..**
d2c10 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 If we do not do
d2c20 74 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f this and we enco
d2c30 75 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 unter a power fa
d2c40 69 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 ilure, the direc
d2c50 74 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f tory.** entry fo
d2c60 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 r the journal mi
d2c70 67 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 ght not exist af
d2c80 74 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 ter we reboot.
d2c90 54 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 The next.** SQLi
d2ca0 74 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 te to access the
d2cb0 20 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b file will not k
d2cc0 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 now that the jou
d2cd0 72 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 rnal exists (bec
d2ce0 61 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 ause.** the dire
d2cf0 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 ctory entry for
d2d00 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 the journal was
d2d10 6e 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 never created) a
d2d20 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 nd the transacti
d2d30 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 on.** will not r
d2d40 6f 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 oll back - possi
d2d50 62 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 bly leading to d
d2d60 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 atabase corrupti
d2d70 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e on..*/.static in
d2d80 74 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 t unixSync(sqlit
d2d90 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d2da0 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 flags){. int r
d2db0 63 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 c;. unixFile *p
d2dc0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d2dd0 2a 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 *)id;.. int isD
d2de0 61 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 ataOnly = (flags
d2df0 26 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 &SQLITE_SYNC_DAT
d2e00 41 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 AONLY);. int is
d2e10 46 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 Fullsync = (flag
d2e20 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f s&0x0F)==SQLITE_
d2e30 53 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a SYNC_FULL;.. /*
d2e40 20 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 Check that one
d2e50 6f 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e of SQLITE_SYNC_N
d2e60 4f 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 ORMAL or FULL wa
d2e70 73 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 s passed */. as
d2e80 73 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 sert((flags&0x0F
d2e90 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e )==SQLITE_SYNC_N
d2ea0 4f 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 ORMAL. || (
d2eb0 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c flags&0x0F)==SQL
d2ec0 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 ITE_SYNC_FULL.
d2ed0 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 );.. /* Unix ca
d2ee0 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 nnot, but some s
d2ef0 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 ystems may retur
d2f00 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 n SQLITE_FULL fr
d2f10 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 om here. This.
d2f20 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 ** line is to te
d2f30 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f st that doing so
d2f40 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 does not cause
d2f50 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 any problems..
d2f60 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 */. SimulateDis
d2f70 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 kfullError( retu
d2f80 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 rn SQLITE_FULL )
d2f90 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 ;.. assert( pFi
d2fa0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 le );. OSTRACE2
d2fb0 28 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e ("SYNC %-3d\n
d2fc0 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 ", pFile->h);.
d2fd0 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 rc = full_fsync(
d2fe0 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c pFile->h, isFull
d2ff0 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 sync, isDataOnly
d3000 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
d3010 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 rror( rc=1 );.
d3020 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 if( rc ){. re
d3030 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
d3040 52 5f 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 R_FSYNC;. }. i
d3050 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e f( pFile->dirfd>
d3060 3d 30 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 =0 ){. OSTRAC
d3070 45 34 28 22 44 49 52 53 59 4e 43 20 25 2d 33 64 E4("DIRSYNC %-3d
d3080 20 28 68 61 76 65 5f 66 75 6c 6c 66 73 79 6e 63 (have_fullfsync
d3090 3d 25 64 20 66 75 6c 6c 73 79 6e 63 3d 25 64 29 =%d fullsync=%d)
d30a0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 64 69 72 66 \n", pFile->dirf
d30b0 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 48 d,. H
d30c0 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 69 AVE_FULLFSYNC, i
d30d0 73 46 75 6c 6c 73 79 6e 63 29 3b 0a 23 69 66 6e sFullsync);.#ifn
d30e0 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 def SQLITE_DISAB
d30f0 4c 45 5f 44 49 52 53 59 4e 43 0a 20 20 20 20 2f LE_DIRSYNC. /
d3100 2a 20 54 68 65 20 64 69 72 65 63 74 6f 72 79 20 * The directory
d3110 73 79 6e 63 20 69 73 20 6f 6e 6c 79 20 61 74 74 sync is only att
d3120 65 6d 70 74 65 64 20 69 66 20 66 75 6c 6c 5f 66 empted if full_f
d3130 73 79 6e 63 20 69 73 0a 20 20 20 20 2a 2a 20 74 sync is. ** t
d3140 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 75 6e 61 urned off or una
d3150 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 61 20 vailable. If a
d3160 66 75 6c 6c 5f 66 73 79 6e 63 20 6f 63 63 75 72 full_fsync occur
d3170 72 65 64 20 61 62 6f 76 65 2c 0a 20 20 20 20 2a red above,. *
d3180 2a 20 74 68 65 6e 20 74 68 65 20 64 69 72 65 63 * then the direc
d3190 74 6f 72 79 20 73 79 6e 63 20 69 73 20 73 75 70 tory sync is sup
d31a0 65 72 66 6c 75 6f 75 73 2e 0a 20 20 20 20 2a 2f erfluous.. */
d31b0 0a 20 20 20 20 69 66 28 20 28 21 48 41 56 45 5f . if( (!HAVE_
d31c0 46 55 4c 4c 46 53 59 4e 43 20 7c 7c 20 21 69 73 FULLFSYNC || !is
d31d0 46 75 6c 6c 73 79 6e 63 29 20 26 26 20 66 75 6c Fullsync) && ful
d31e0 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65 2d 3e 64 l_fsync(pFile->d
d31f0 69 72 66 64 2c 30 2c 30 29 20 29 7b 0a 20 20 20 irfd,0,0) ){.
d3200 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 2a 2a /*. **
d3210 20 57 65 20 68 61 76 65 20 72 65 63 65 69 76 65 We have receive
d3220 64 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 6f 72 d multiple repor
d3230 74 73 20 6f 66 20 66 73 79 6e 63 28 29 20 72 65 ts of fsync() re
d3240 74 75 72 6e 69 6e 67 0a 20 20 20 20 20 20 20 2a turning. *
d3250 2a 20 65 72 72 6f 72 73 20 77 68 65 6e 20 61 70 * errors when ap
d3260 70 6c 69 65 64 20 74 6f 20 64 69 72 65 63 74 6f plied to directo
d3270 72 69 65 73 20 6f 6e 20 63 65 72 74 61 69 6e 20 ries on certain
d3280 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 0a 20 20 file systems..
d3290 20 20 20 20 20 2a 2a 20 41 20 66 61 69 6c 65 64 ** A failed
d32a0 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 directory sync
d32b0 69 73 20 6e 6f 74 20 61 20 62 69 67 20 64 65 61 is not a big dea
d32c0 6c 2e 20 20 53 6f 20 69 74 20 73 65 65 6d 73 0a l. So it seems.
d32d0 20 20 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 ** better
d32e0 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 65 to ignore the e
d32f0 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 31 rror. Ticket #1
d3300 36 35 37 0a 20 20 20 20 20 20 20 2a 2f 0a 20 20 657. */.
d3310 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 /* return S
d3320 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a QLITE_IOERR; */.
d3330 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
d3340 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 close(pFile->di
d3350 72 66 64 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 6e rfd); /* Only n
d3360 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e 63 65 eed to sync once
d3370 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65 20 64 , so close the d
d3380 69 72 65 63 74 6f 72 79 20 2a 2f 0a 20 20 20 20 irectory */.
d3390 70 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d pFile->dirfd = -
d33a0 31 3b 20 20 20 20 2f 2a 20 77 68 65 6e 20 77 65 1; /* when we
d33b0 20 61 72 65 20 64 6f 6e 65 2e 20 2a 2f 0a 20 20 are done. */.
d33c0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
d33d0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
d33e0 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 runcate an open
d33f0 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 file to a specif
d3400 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 ied size.*/.stat
d3410 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 ic int unixTrunc
d3420 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
d3430 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29 *id, i64 nByte)
d3440 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 {. int rc;. as
d3450 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69 sert( id );. Si
d3460 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
d3470 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
d3480 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 RR_TRUNCATE );.
d3490 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28 rc = ftruncate(
d34a0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
d34b0 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65 >h, (off_t)nByte
d34c0 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
d34d0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d34e0 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b _IOERR_TRUNCATE;
d34f0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
d3500 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d3510 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 }.}../*.** Det
d3520 65 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 ermine the curre
d3530 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c nt size of a fil
d3540 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 e in bytes.*/.st
d3550 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c atic int unixFil
d3560 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 eSize(sqlite3_fi
d3570 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 le *id, i64 *pSi
d3580 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ze){. int rc;.
d3590 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66 struct stat buf
d35a0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29 ;. assert( id )
d35b0 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 28 ;. rc = fstat((
d35c0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
d35d0 68 2c 20 26 62 75 66 29 3b 0a 20 20 53 69 6d 75 h, &buf);. Simu
d35e0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d lateIOError( rc=
d35f0 31 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 1 );. if( rc!=0
d3600 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
d3610 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 QLITE_IOERR_FSTA
d3620 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 T;. }. *pSize
d3630 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a = buf.st_size;..
d3640 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e /* When openin
d3650 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61 g a zero-size da
d3660 74 61 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64 tabase, the find
d3670 4c 6f 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65 LockInfo() proce
d3680 64 75 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73 dure. ** writes
d3690 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 a single byte i
d36a0 6e 74 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e nto that file in
d36b0 20 6f 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61 order to work a
d36c0 72 6f 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a round a bug. **
d36d0 20 69 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64 in the OS-X msd
d36e0 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 os filesystem.
d36f0 49 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 In order to avoi
d3700 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 d problems with
d3710 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72 upper. ** layer
d3720 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 s, we need to re
d3730 70 6f 72 74 20 74 68 69 73 20 66 69 6c 65 20 73 port this file s
d3740 69 7a 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e ize as zero even
d3750 20 74 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20 though it is.
d3760 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54 ** really 1. T
d3770 69 63 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a icket #3260.. *
d3780 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d /. if( *pSize==
d3790 31 20 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 1 ) *pSize = 0;.
d37a0 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
d37b0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
d37c0 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e his routine tran
d37d0 73 6c 61 74 65 73 20 61 20 73 74 61 6e 64 61 72 slates a standar
d37e0 64 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f d POSIX errno co
d37f0 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e de into somethin
d3800 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 g.** useful to t
d3810 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 he clients of th
d3820 65 20 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69 e sqlite3 functi
d3830 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c ons. Specifical
d3840 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 ly, it is.** int
d3850 65 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 ended to transla
d3860 74 65 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 te a variety of
d3870 22 74 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f "try again" erro
d3880 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 rs into SQLITE_B
d3890 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 USY.** and a var
d38a0 69 65 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20 iety of "please
d38b0 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 close the file d
d38c0 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 escriptor NOW" e
d38d0 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 rrors into .** S
d38e0 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a QLITE_IOERR.** .
d38f0 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67 ** Errors during
d3900 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
d3910 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c of locks, or fil
d3920 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 e system support
d3930 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 for locks,.** s
d3940 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f hould handle ENO
d3950 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f LCK, ENOTSUP, EO
d3960 50 4e 4f 54 53 55 50 50 20 73 65 70 61 72 61 74 PNOTSUPP separat
d3970 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ely..*/.static i
d3980 6e 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 nt sqliteErrorFr
d3990 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 omPosixError(int
d39a0 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 posixError, int
d39b0 20 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a sqliteIOErr) {.
d39c0 20 20 73 77 69 74 63 68 20 28 70 6f 73 69 78 45 switch (posixE
d39d0 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30 rror) {. case 0
d39e0 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 : . return SQ
d39f0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 LITE_OK;. .
d3a00 63 61 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 case EAGAIN:. c
d3a10 61 73 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 ase ETIMEDOUT:.
d3a20 20 63 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63 case EBUSY:. c
d3a30 61 73 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 ase EINTR:. cas
d3a40 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 e ENOLCK: .
d3a50 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 /* random NFS re
d3a60 74 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 try error, unles
d3a70 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 s during file sy
d3a80 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 stem support .
d3a90 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 * introspecti
d3aa0 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 on, in which it
d3ab0 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 actually means w
d3ac0 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 hat it says */.
d3ad0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d3ae0 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 _BUSY;. . ca
d3af0 73 65 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20 se EACCES: .
d3b00 2f 2a 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b /* EACCES is lik
d3b10 65 20 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20 e EAGAIN during
d3b20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f locking operatio
d3b30 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 ns, but not any
d3b40 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 other time*/.
d3b50 20 69 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72 if( (sqliteIOEr
d3b60 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 r == SQLITE_IOER
d3b70 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 R_LOCK) || ..(sq
d3b80 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c liteIOErr == SQL
d3b90 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
d3ba0 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f ) || ..(sqliteIO
d3bb0 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f Err == SQLITE_IO
d3bc0 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 ERR_RDLOCK) ||..
d3bd0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 (sqliteIOErr ==
d3be0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
d3bf0 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 CKRESERVEDLOCK)
d3c00 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
d3c10 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
d3c20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 }. /* else f
d3c30 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 all through */.
d3c40 20 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 case EPERM: .
d3c50 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d3c60 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 PERM;. . cas
d3c70 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 e EDEADLK:. r
d3c80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
d3c90 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 RR_BLOCKED;.
d3ca0 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 .#if EOPNOTSUPP!
d3cb0 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 =ENOTSUP. case
d3cc0 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 EOPNOTSUPP: .
d3cd0 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 /* something we
d3ce0 6e 74 20 74 65 72 72 69 62 6c 79 20 61 77 72 79 nt terribly awry
d3cf0 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 , unless during
d3d00 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 file system supp
d3d10 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 ort . * intr
d3d20 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 ospection, in wh
d3d30 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20 ich it actually
d3d40 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 means what it sa
d3d50 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 ys */.#endif.#if
d3d60 64 65 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 def ENOTSUP. ca
d3d70 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 se ENOTSUP: .
d3d80 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 /* invalid fd,
d3d90 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 unless during fi
d3da0 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 le system suppor
d3db0 74 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c t introspection,
d3dc0 20 69 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20 in which .
d3dd0 2a 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 * it actually me
d3de0 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 ans what it says
d3df0 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 */.#endif. cas
d3e00 65 20 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 e EIO:. case EB
d3e10 41 44 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 ADF:. case EINV
d3e20 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 AL:. case ENOTC
d3e30 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 ONN:. case ENOD
d3e40 45 56 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f EV:. case ENXIO
d3e50 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a :. case ENOENT:
d3e60 0a 20 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a . case ESTALE:.
d3e70 20 20 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 case ENOSYS:.
d3e80 20 20 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 /* these shou
d3e90 6c 64 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69 ld force the cli
d3ea0 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 ent to close the
d3eb0 20 66 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e file and reconn
d3ec0 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 ect */. . de
d3ed0 66 61 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 fault: . retu
d3ee0 72 6e 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a rn sqliteIOErr;.
d3ef0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
d3f00 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
d3f10 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 if there is a R
d3f20 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c ESERVED lock hel
d3f30 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 d on the specifi
d3f40 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 ed.** file by th
d3f50 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 is or any other
d3f60 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 process. If such
d3f70 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c a lock is held,
d3f80 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a set *pResOut.**
d3f90 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 to a non-zero v
d3fa0 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a alue otherwise *
d3fb0 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 pResOut is set t
d3fc0 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 o zero. The ret
d3fd0 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 urn value.** is
d3fe0 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b set to SQLITE_OK
d3ff0 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 unless an I/O e
d4000 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
d4010 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 ng lock checking
d4020 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d4030 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 unixCheckReserve
d4040 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 dLock(sqlite3_fi
d4050 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 le *id, int *pRe
d4060 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 sOut){. int rc
d4070 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
d4080 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b nt reserved = 0;
d4090 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d40a0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d40b0 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 id;.. SimulateI
d40c0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
d40d0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 QLITE_IOERR_CHEC
d40e0 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 KRESERVEDLOCK; )
d40f0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 ;.. assert( pFi
d4100 6c 65 20 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74 le );. enterMut
d4110 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 ex(); /* Because
d4120 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 pFile->pLock is
d4130 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 shared across t
d4140 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20 hreads */.. /*
d4150 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 Check if a threa
d4160 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 d in this proces
d4170 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c s holds such a l
d4180 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 ock */. if( pFi
d4190 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 le->pLock->lockt
d41a0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
d41b0 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 ){. reserved
d41c0 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f = 1;. }.. /* O
d41d0 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 20 therwise see if
d41e0 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 some other proce
d41f0 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a ss holds it.. *
d4200 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 /. if( !reserve
d4210 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 d ){. struct
d4220 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 20 flock lock;.
d4230 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 lock.l_whence =
d4240 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f SEEK_SET;. lo
d4250 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 ck.l_start = RES
d4260 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 ERVED_BYTE;.
d4270 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a lock.l_len = 1;.
d4280 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 lock.l_type
d4290 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 69 = F_WRLCK;. i
d42a0 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 70 f (-1 == fcntl(p
d42b0 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b File->h, F_GETLK
d42c0 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 20 , &lock)) {.
d42d0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
d42e0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d rrno;. rc =
d42f0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d4300 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d4310 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
d4320 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 CHECKRESERVEDLOC
d4330 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d K);. pFile-
d4340 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d4350 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 rno;. } else
d4360 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 21 if( lock.l_type!
d4370 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 =F_UNLCK ){.
d4380 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
d4390 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 6c }. }. . l
d43a0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f eaveMutex();. O
d43b0 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 STRACE4("TEST WR
d43c0 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e -LOCK %d %d %d\n
d43d0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c ", pFile->h, rc,
d43e0 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a reserved);.. *
d43f0 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 pResOut = reserv
d4400 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ed;. return rc;
d4410 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
d4420 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
d4430 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
d4440 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
d4450 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
d4460 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
d4470 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
d4480 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
d4490 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
d44a0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
d44b0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
d44c0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
d44d0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
d44e0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
d44f0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
d4500 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
d4510 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
d4520 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
d4530 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
d4540 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
d4550 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
d4560 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
d4570 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
d4580 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
d4590 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
d45a0 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
d45b0 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
d45c0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
d45d0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
d45e0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
d45f0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
d4600 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
d4610 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
d4620 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
d4630 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
d4640 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
d4650 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
d4660 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
d4670 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
d4680 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
d4690 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d46a0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d46b0 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
d46c0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
d46d0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
d46e0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
d46f0 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c ck. Use the sql
d4700 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a ite3OsUnlock().*
d4710 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 * routine to low
d4720 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 er a locking lev
d4730 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e el..*/.static in
d4740 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 t unixLock(sqlit
d4750 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d4760 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a locktype){. /*
d4770 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 The following d
d4780 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70 escribes the imp
d4790 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
d47a0 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 he various locks
d47b0 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 and. ** lock t
d47c0 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65 ransitions in te
d47d0 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58 rms of the POSIX
d47e0 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64 advisory shared
d47f0 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20 and exclusive.
d4800 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69 ** lock primiti
d4810 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64 ves (called read
d4820 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65 -locks and write
d4830 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f -locks below, to
d4840 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 avoid. ** conf
d4850 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74 usion with SQLit
d4860 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54 e lock names). T
d4870 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 he algorithms ar
d4880 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20 e complicated.
d4890 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f ** slightly in o
d48a0 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61 rder to be compa
d48b0 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f tible with windo
d48c0 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c ws systems simul
d48d0 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 taneously. ** a
d48e0 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d ccessing the sam
d48f0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
d4900 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73 in case that is
d4910 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a ever required..
d4920 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c **. ** Symbol
d4930 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e s defined in os.
d4940 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20 h indentify the
d4950 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61 'pending byte' a
d4960 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64 nd the 'reserved
d4970 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63 . ** byte', eac
d4980 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61 h single bytes a
d4990 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 t well known off
d49a0 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73 sets, and the 's
d49b0 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 hared byte. **
d49c0 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20 range', a range
d49d0 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20 of 510 bytes at
d49e0 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 a well known off
d49f0 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 set.. **. ** T
d4a00 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45 o obtain a SHARE
d4a10 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c D lock, a read-l
d4a20 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ock is obtained
d4a30 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a on the 'pending.
d4a40 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20 ** byte'. If
d4a50 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66 this is successf
d4a60 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 ul, a random byt
d4a70 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72 e from the 'shar
d4a80 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e ed byte. ** ran
d4a90 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b ge' is read-lock
d4aa0 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 ed and the lock
d4ab0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 on the 'pending
d4ac0 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a byte' released..
d4ad0 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 **. ** A proc
d4ae0 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 ess may only obt
d4af0 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c ain a RESERVED l
d4b00 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 ock after it has
d4b10 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a a SHARED lock..
d4b20 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20 ** A RESERVED
d4b30 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e lock is implemen
d4b40 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20 ted by grabbing
d4b50 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
d4b60 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76 the. ** 'reserv
d4b70 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a ed byte'. . **.
d4b80 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d ** A process m
d4b90 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 ay only obtain a
d4ba0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66 PENDING lock af
d4bb0 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69 ter it has obtai
d4bc0 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45 ned a. ** SHARE
d4bd0 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e D lock. A PENDIN
d4be0 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d G lock is implem
d4bf0 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 ented by obtaini
d4c00 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a ng a write-lock.
d4c10 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e ** on the 'pen
d4c20 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73 ding byte'. This
d4c30 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f ensures that no
d4c40 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b new SHARED lock
d4c50 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 s can be. ** ob
d4c60 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73 tained, but exis
d4c70 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b ting SHARED lock
d4c80 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f s are allowed to
d4c90 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63 persist. A proc
d4ca0 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f ess. ** does no
d4cb0 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e t have to obtain
d4cc0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
d4cd0 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61 on the way to a
d4ce0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 PENDING lock..
d4cf0 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74 ** This propert
d4d00 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 y is used by the
d4d10 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72 algorithm for r
d4d20 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f olling back a jo
d4d30 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 urnal file. **
d4d40 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20 after a crash..
d4d50 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c **. ** An EXCL
d4d60 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 USIVE lock, obta
d4d70 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e ined after a PEN
d4d80 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c DING lock is hel
d4d90 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 d, is. ** imple
d4da0 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e mented by obtain
d4db0 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ing a write-lock
d4dc0 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27 on the entire '
d4dd0 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a shared byte. **
d4de0 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61 range'. Since a
d4df0 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72 ll other locks r
d4e00 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f equire a read-lo
d4e10 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 ck on one of the
d4e20 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68 bytes. ** with
d4e30 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74 in this range, t
d4e40 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 his ensures that
d4e50 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 no other locks
d4e60 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a are held on the.
d4e70 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a ** database. .
d4e80 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65 **. ** The re
d4e90 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79 ason a single by
d4ea0 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 te cannot be use
d4eb0 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 d instead of the
d4ec0 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 'shared byte.
d4ed0 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61 ** range' is tha
d4ee0 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 t some versions
d4ef0 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f of windows do no
d4f00 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c t support read-l
d4f10 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f ocks. By. ** lo
d4f20 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62 cking a random b
d4f30 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65 yte from a range
d4f40 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41 , concurrent SHA
d4f50 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78 RED locks may ex
d4f60 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 ist. ** even if
d4f70 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69 the locking pri
d4f80 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61 mitive used is a
d4f90 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f lways a write-lo
d4fa0 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72 ck.. */. int r
d4fb0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
d4fc0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d4fd0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d4fe0 3b 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49 ;. struct lockI
d4ff0 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 70 46 69 nfo *pLock = pFi
d5000 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 le->pLock;. str
d5010 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a uct flock lock;.
d5020 20 20 69 6e 74 20 73 3b 0a 0a 20 20 61 73 73 65 int s;.. asse
d5030 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f rt( pFile );. O
d5040 53 54 52 41 43 45 37 28 22 4c 4f 43 4b 20 20 20 STRACE7("LOCK
d5050 20 25 64 20 25 73 20 77 61 73 20 25 73 28 25 73 %d %s was %s(%s
d5060 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 ,%d) pid=%d\n",
d5070 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 pFile->h,.
d5080 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 locktypeName(loc
d5090 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 ktype), locktype
d50a0 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b Name(pFile->lock
d50b0 74 79 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f 63 type),. loc
d50c0 6b 74 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d ktypeName(pLock-
d50d0 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f 63 >locktype), pLoc
d50e0 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 28 k->cnt , getpid(
d50f0 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 ));.. /* If the
d5100 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 re is already a
d5110 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 lock of this typ
d5120 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 e or more restri
d5130 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a ctive on the. *
d5140 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e * unixFile, do n
d5150 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 othing. Don't us
d5160 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 e the end_lock:
d5170 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 exit path, as.
d5180 2a 2a 20 65 6e 74 65 72 4d 75 74 65 78 28 29 20 ** enterMutex()
d5190 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c hasn't been call
d51a0 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 ed yet.. */. i
d51b0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d51c0 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a pe>=locktype ){.
d51d0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f OSTRACE3("LO
d51e0 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 CK %d %s ok (
d51f0 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 already held)\n"
d5200 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 , pFile->h,.
d5210 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 locktype
d5220 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b Name(locktype));
d5230 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d5240 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a TE_OK;. }.. /*
d5250 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c Make sure the l
d5260 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 ocking sequence
d5270 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a is correct. */.
d5280 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
d5290 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f >locktype!=NO_LO
d52a0 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d CK || locktype==
d52b0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
d52c0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
d52d0 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 e!=PENDING_LOCK
d52e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 );. assert( loc
d52f0 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f ktype!=RESERVED_
d5300 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c LOCK || pFile->l
d5310 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
d5320 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 LOCK );.. /* Th
d5330 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 is mutex is need
d5340 65 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 ed because pFile
d5350 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 ->pLock is share
d5360 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 d across threads
d5370 0a 20 20 2a 2f 0a 20 20 65 6e 74 65 72 4d 75 74 . */. enterMut
d5380 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ex();.. /* Make
d5390 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65 6e sure the curren
d53a0 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 t thread owns th
d53b0 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 e pFile.. */.
d53c0 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e rc = transferOwn
d53d0 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 ership(pFile);.
d53e0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
d53f0 4f 4b 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 4d OK ){. leaveM
d5400 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 utex();. retu
d5410 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f rn rc;. }. pLo
d5420 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 ck = pFile->pLoc
d5430 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 k;.. /* If some
d5440 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 thread using th
d5450 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63 is PID has a loc
d5460 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e k via a differen
d5470 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a t unixFile*. **
d5480 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65 handle that pre
d5490 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65 cludes the reque
d54a0 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72 sted lock, retur
d54b0 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 n BUSY.. */. i
d54c0 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 f( (pFile->lockt
d54d0 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b ype!=pLock->lock
d54e0 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 20 type && .
d54f0 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 (pLock->lockt
d5500 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 ype>=PENDING_LOC
d5510 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48 K || locktype>SH
d5520 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b ARED_LOCK)). ){
d5530 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
d5540 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 _BUSY;. goto
d5550 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 end_lock;. }..
d5560 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 /* If a SHARED
d5570 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 lock is requeste
d5580 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 d, and some thre
d5590 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 ad using this PI
d55a0 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68 D already. ** h
d55b0 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 as a SHARED or R
d55c0 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 ESERVED lock, th
d55d0 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 en increment ref
d55e0 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e erence counts an
d55f0 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 d. ** return SQ
d5600 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 LITE_OK.. */.
d5610 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
d5620 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 ARED_LOCK && .
d5630 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b (pLock->lock
d5640 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
d5650 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b K || pLock->lock
d5660 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c type==RESERVED_L
d5670 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65 OCK) ){. asse
d5680 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 rt( locktype==SH
d5690 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 ARED_LOCK );.
d56a0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
d56b0 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20 locktype==0 );.
d56c0 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
d56d0 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70 ->cnt>0 );. p
d56e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d56f0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
d5700 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a pLock->cnt++;.
d5710 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e pFile->pOpen
d5720 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 ->nLock++;. g
d5730 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 oto end_lock;.
d5740 7d 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 }.. lock.l_len
d5750 3d 20 31 4c 3b 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f = 1L;.. lock.l_
d5760 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 whence = SEEK_SE
d5770 54 3b 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 T;.. /* A PENDI
d5780 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 NG lock is neede
d5790 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 d before acquiri
d57a0 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b ng a SHARED lock
d57b0 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a and before. **
d57c0 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 acquiring an EX
d57d0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 CLUSIVE lock. F
d57e0 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f or the SHARED lo
d57f0 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 ck, the PENDING
d5800 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c will. ** be rel
d5810 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 eased.. */. if
d5820 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
d5830 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c ED_LOCK . |
d5840 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 | (locktype==EXC
d5850 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
d5860 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 File->locktype<P
d5870 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 ENDING_LOCK). )
d5880 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 {. lock.l_typ
d5890 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 e = (locktype==S
d58a0 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c HARED_LOCK?F_RDL
d58b0 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 CK:F_WRLCK);.
d58c0 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 lock.l_start =
d58d0 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 PENDING_BYTE;.
d58e0 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c s = fcntl(pFil
d58f0 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 e->h, F_SETLK, &
d5900 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 lock);. if( s
d5910 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 ==(-1) ){.
d5920 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
d5930 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 no;. rc = s
d5940 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d5950 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d5960 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
d5970 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 CK);. if( I
d5980 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d5990 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
d59a0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d59b0 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 Errno;. }.
d59c0 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f goto end_lo
d59d0 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a ck;. }. }...
d59e0 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 /* If control
d59f0 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 gets to this poi
d5a00 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c nt, then actuall
d5a10 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d y go ahead and m
d5a20 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 ake. ** operati
d5a30 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 ng system calls
d5a40 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 for the specifie
d5a50 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 d lock.. */. i
d5a60 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
d5a70 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
d5a80 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a int tErrno = 0;.
d5a90 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
d5aa0 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 k->cnt==0 );.
d5ab0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
d5ac0 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a locktype==0 );..
d5ad0 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 /* Now get t
d5ae0 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a he read-lock */.
d5af0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
d5b00 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
d5b10 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 . lock.l_len
d5b20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 = SHARED_SIZE;.
d5b30 20 20 20 69 66 28 20 28 73 20 3d 20 66 63 6e 74 if( (s = fcnt
d5b40 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 l(pFile->h, F_SE
d5b50 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 3d 3d 28 2d TLK, &lock))==(-
d5b60 31 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72 1) ){. tErr
d5b70 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d5b80 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 }. /* Drop th
d5b90 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 e temporary PEND
d5ba0 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 ING lock */.
d5bb0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 lock.l_start = P
d5bc0 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
d5bd0 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c lock.l_len = 1L
d5be0 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 ;. lock.l_typ
d5bf0 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 e = F_UNLCK;.
d5c00 20 69 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65 if( fcntl(pFile
d5c10 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c ->h, F_SETLK, &l
d5c20 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ock)!=0 ){.
d5c30 20 69 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a if( s != -1 ){.
d5c40 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
d5c50 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 could happen wit
d5c60 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e h a network moun
d5c70 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72 t */. tEr
d5c80 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 rno = errno; .
d5c90 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
d5ca0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
d5cb0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
d5cc0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
d5cd0 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 ); . if(
d5ce0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
d5cf0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
d5d00 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d5d10 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
d5d20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f }. goto
d5d30 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 end_lock;.
d5d40 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
d5d50 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 s==(-1) ){.
d5d60 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d5d70 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d5d80 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d5d90 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 IOERR_LOCK);.
d5da0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d5db0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
d5dc0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d5dd0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
d5de0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
d5df0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
d5e00 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 ocktype = SHARED
d5e10 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 _LOCK;. pFi
d5e20 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b le->pOpen->nLock
d5e30 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d ++;. pLock-
d5e40 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a >cnt = 1;. }.
d5e50 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b }else if( lock
d5e60 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f type==EXCLUSIVE_
d5e70 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 LOCK && pLock->c
d5e80 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 nt>1 ){. /* W
d5e90 65 20 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 e are trying for
d5ea0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
d5eb0 63 6b 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 ck but another t
d5ec0 68 72 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 hread in this.
d5ed0 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 ** same proces
d5ee0 73 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 s is still holdi
d5ef0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b ng a shared lock
d5f00 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 . */. rc = SQ
d5f10 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c LITE_BUSY;. }el
d5f20 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 se{. /* The r
d5f30 65 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 equest was for a
d5f40 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 RESERVED or EXC
d5f50 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 LUSIVE lock. It
d5f60 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d is. ** assum
d5f70 65 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73 ed that there is
d5f80 20 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 a SHARED or gre
d5f90 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ater lock on the
d5fa0 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 file. ** alr
d5fb0 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 eady.. */.
d5fc0 20 61 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c assert( 0!=pFil
d5fd0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
d5fe0 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d lock.l_type =
d5ff0 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77 F_WRLCK;. sw
d6000 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 itch( locktype )
d6010 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 45 53 {. case RES
d6020 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 ERVED_LOCK:.
d6030 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
d6040 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 = RESERVED_BYTE
d6050 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
d6060 0a 20 20 20 20 20 20 63 61 73 65 20 45 58 43 4c . case EXCL
d6070 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 USIVE_LOCK:.
d6080 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
d6090 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
d60a0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
d60b0 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a len = SHARED_SIZ
d60c0 45 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b E;. break
d60d0 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a ;. default:
d60e0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
d60f0 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 20 0);. }. s
d6100 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 = fcntl(pFile->h
d6110 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_SETLK, &lock
d6120 29 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d );. if( s==(-
d6130 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 1) ){. int
d6140 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d6150 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
d6160 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
d6170 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
d6180 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b ITE_IOERR_LOCK);
d6190 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f . if( IS_LO
d61a0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
d61b0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
d61c0 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d61d0 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d o;. }. }
d61e0 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 . }. . if( rc
d61f0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
d6200 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d6210 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 pe = locktype;.
d6220 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
d6230 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 pe = locktype;.
d6240 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 }else if( lockt
d6250 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
d6260 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 OCK ){. pFile
d6270 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e ->locktype = PEN
d6280 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 DING_LOCK;. p
d6290 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d Lock->locktype =
d62a0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 PENDING_LOCK;.
d62b0 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 }..end_lock:.
d62c0 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 leaveMutex();.
d62d0 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 OSTRACE4("LOCK
d62e0 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 %d %s %s\n", p
d62f0 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
d6300 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c eName(locktype),
d6310 20 0a 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 . rc==SQLI
d6320 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 TE_OK ? "ok" : "
d6330 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 failed");. retu
d6340 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
d6350 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e Lower the lockin
d6360 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 g level on file
d6370 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 descriptor pFile
d6380 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c to locktype. l
d6390 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 ocktype.** must
d63a0 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 be either NO_LOC
d63b0 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b K or SHARED_LOCK
d63c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c ..**.** If the l
d63d0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 ocking level of
d63e0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
d63f0 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 tor is already a
d6400 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 t or below.** th
d6410 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
d6420 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 ing level, this
d6430 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
d6440 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e op..*/.static in
d6450 74 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 71 6c t unixUnlock(sql
d6460 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
d6470 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 nt locktype){.
d6480 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 struct lockInfo
d6490 2a 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 *pLock;. struct
d64a0 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 flock lock;. i
d64b0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
d64c0 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 K;. unixFile *p
d64d0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d64e0 2a 29 69 64 3b 0a 20 20 69 6e 74 20 68 3b 0a 0a *)id;. int h;..
d64f0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
d6500 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55 );. OSTRACE7("U
d6510 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 NLOCK %d %d was
d6520 20 25 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25 %d(%d,%d) pid=%
d6530 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
d6540 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 locktype,.
d6550 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c pFile->locktype,
d6560 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c pFile->pLock->l
d6570 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e ocktype, pFile->
d6580 70 4c 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 pLock->cnt, getp
d6590 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 id());.. assert
d65a0 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
d65b0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 ED_LOCK );. if(
d65c0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d65d0 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 <=locktype ){.
d65e0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d65f0 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 OK;. }. if( CH
d6600 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 ECK_THREADID(pFi
d6610 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 le) ){. retur
d6620 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
d6630 0a 20 20 7d 0a 20 20 65 6e 74 65 72 4d 75 74 65 . }. enterMute
d6640 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65 x();. h = pFile
d6650 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 ->h;. pLock = p
d6660 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 File->pLock;. a
d6670 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e ssert( pLock->cn
d6680 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 t!=0 );. if( pF
d6690 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 ile->locktype>SH
d66a0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d66b0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
d66c0 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d locktype==pFile-
d66d0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 >locktype );.
d66e0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d66f0 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 53 Benign(1);. S
d6700 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
d6710 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 69 6d h=(-1) ). Sim
d6720 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
d6730 67 6e 28 30 29 3b 0a 20 20 20 20 69 66 28 20 6c gn(0);. if( l
d6740 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
d6750 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 6c 6f LOCK ){. lo
d6760 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 ck.l_type = F_RD
d6770 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e LCK;. lock.
d6780 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
d6790 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e SET;. lock.
d67a0 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 l_start = SHARED
d67b0 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 6c 6f _FIRST;. lo
d67c0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 ck.l_len = SHARE
d67d0 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 69 66 D_SIZE;. if
d67e0 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 ( fcntl(h, F_SET
d67f0 4c 4b 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29 LK, &lock)==(-1)
d6800 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
d6810 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d6820 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
d6830 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d6840 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d6850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f QLITE_IOERR_RDLO
d6860 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 CK);. if(
d6870 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 IS_LOCK_ERROR(r
d6880 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 c) ){.
d6890 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d68a0 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 = tErrno;.
d68b0 20 20 20 7d 0a 09 09 09 09 67 6f 74 6f 20 65 6e }.....goto en
d68c0 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 d_unlock;.
d68d0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b }. }. lock
d68e0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 .l_type = F_UNLC
d68f0 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 K;. lock.l_wh
d6900 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b ence = SEEK_SET;
d6910 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 . lock.l_star
d6920 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 t = PENDING_BYTE
d6930 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e ;. lock.l_len
d6940 20 3d 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20 = 2L; assert(
d6950 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d PENDING_BYTE+1==
d6960 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b RESERVED_BYTE );
d6970 0a 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 . if( fcntl(h
d6980 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_SETLK, &lock
d6990 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 )!=(-1) ){.
d69a0 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 pLock->locktype
d69b0 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a = SHARED_LOCK;.
d69c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d69d0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 int tErrno = er
d69e0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rno;. rc =
d69f0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d6a00 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d6a10 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 , SQLITE_IOERR_U
d6a20 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 NLOCK);. if
d6a30 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d6a40 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 rc) ){. p
d6a50 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d6a60 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
d6a70 7d 0a 09 09 09 67 6f 74 6f 20 65 6e 64 5f 75 6e }....goto end_un
d6a80 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a lock;. }. }.
d6a90 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
d6aa0 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73 NO_LOCK ){. s
d6ab0 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a 70 truct openCnt *p
d6ac0 4f 70 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 Open;.. /* De
d6ad0 63 72 65 6d 65 6e 74 20 74 68 65 20 73 68 61 72 crement the shar
d6ae0 65 64 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72 2e ed lock counter.
d6af0 20 20 52 65 6c 65 61 73 65 20 74 68 65 20 6c 6f Release the lo
d6b00 63 6b 20 75 73 69 6e 67 20 61 6e 0a 20 20 20 20 ck using an.
d6b10 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 ** OS call only
d6b20 77 68 65 6e 20 61 6c 6c 20 74 68 72 65 61 64 73 when all threads
d6b30 20 69 6e 20 74 68 69 73 20 73 61 6d 65 20 70 72 in this same pr
d6b40 6f 63 65 73 73 20 68 61 76 65 20 72 65 6c 65 61 ocess have relea
d6b50 73 65 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c sed. ** the l
d6b60 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ock.. */.
d6b70 70 4c 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 pLock->cnt--;.
d6b80 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 if( pLock->cnt
d6b90 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 ==0 ){. loc
d6ba0 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c k.l_type = F_UNL
d6bb0 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c CK;. lock.l
d6bc0 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 _whence = SEEK_S
d6bd0 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c ET;. lock.l
d6be0 5f 73 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f _start = lock.l_
d6bf0 6c 65 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 len = 0L;.
d6c00 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 SimulateIOErrorB
d6c10 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 20 enign(1);.
d6c20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
d6c30 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 20 20 h=(-1) ).
d6c40 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 SimulateIOErrorB
d6c50 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 enign(0);.
d6c60 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 if( fcntl(h, F_S
d6c70 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d ETLK, &lock)!=(-
d6c80 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 1) ){. pL
d6c90 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ock->locktype =
d6ca0 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d NO_LOCK;. }
d6cb0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e else{. in
d6cc0 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f t tErrno = errno
d6cd0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
d6ce0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d6cf0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d6d00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
d6d10 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 LOCK);. i
d6d20 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
d6d30 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 (rc) ){.
d6d40 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d6d50 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d6d60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
d6d70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 09 Lock->cnt = 1;..
d6d80 09 09 09 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f ...goto end_unlo
d6d90 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ck;. }.
d6da0 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d }.. /* Decrem
d6db0 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 ent the count of
d6dc0 20 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 locks against t
d6dd0 68 69 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 his same file.
d6de0 57 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 When the. **
d6df0 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 count reaches ze
d6e00 72 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 ro, close any ot
d6e10 68 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 her file descrip
d6e20 74 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 tors whose close
d6e30 0a 20 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 . ** was defe
d6e40 72 72 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 rred because of
d6e50 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b outstanding lock
d6e60 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
d6e70 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
d6e80 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d ){. pOpen =
d6e90 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 pFile->pOpen;.
d6ea0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 pOpen->nLoc
d6eb0 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 k--;. asser
d6ec0 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e t( pOpen->nLock>
d6ed0 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 =0 );. if(
d6ee0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 pOpen->nLock==0
d6ef0 26 26 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 && pOpen->nPendi
d6f00 6e 67 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ng>0 ){.
d6f10 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 int i;. f
d6f20 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 70 65 6e 2d or(i=0; i<pOpen-
d6f30 3e 6e 50 65 6e 64 69 6e 67 3b 20 69 2b 2b 29 7b >nPending; i++){
d6f40 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 . close
d6f50 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 (pOpen->aPending
d6f60 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a [i]);. }.
d6f70 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
d6f80 66 72 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e free(pOpen->aPen
d6f90 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 70 ding);. p
d6fa0 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d Open->nPending =
d6fb0 20 30 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 0;. pOpe
d6fc0 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b n->aPending = 0;
d6fd0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d6fe0 20 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a }...end_unlock:
d6ff0 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b . leaveMutex();
d7000 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
d7010 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f E_OK ) pFile->lo
d7020 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
d7030 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a e;. return rc;.
d7040 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
d7050 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 nction performs
d7060 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68 65 the parts of the
d7070 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70 "close file" op
d7080 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d eration .** comm
d7090 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e on to all lockin
d70a0 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 63 6c g schemes. It cl
d70b0 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 74 6f oses the directo
d70c0 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68 ry and file.** h
d70d0 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 79 20 andles, if they
d70e0 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 73 are valid, and s
d70f0 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f ets all fields o
d7100 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a f the unixFile.*
d7110 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 30 * structure to 0
d7120 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d7130 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 closeUnixFile(sq
d7140 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
d7150 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d7160 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d7170 69 64 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 id;. if( pFile
d7180 29 7b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 ){. if( pFile
d7190 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 ->dirfd>=0 ){.
d71a0 20 20 20 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d close(pFile-
d71b0 3e 64 69 72 66 64 29 3b 0a 20 20 20 20 7d 0a 20 >dirfd);. }.
d71c0 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e if( pFile->h>
d71d0 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73 =0 ){. clos
d71e0 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 e(pFile->h);.
d71f0 20 7d 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b }.#if IS_VXWORK
d7200 53 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d S. if( pFile-
d7210 3e 69 73 44 65 6c 65 74 65 20 26 26 20 70 46 69 >isDelete && pFi
d7220 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 20 29 7b le->zRealpath ){
d7230 0a 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46 . unlink(pF
d7240 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 29 3b ile->zRealpath);
d7250 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
d7260 46 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 20 File->zRealpath
d7270 29 7b 0a 20 20 20 20 20 20 48 61 73 68 45 6c 65 ){. HashEle
d7280 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 20 20 20 20 m *pElem;.
d7290 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 70 int n = strlen(p
d72a0 46 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 29 File->zRealpath)
d72b0 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 45 6c 65 + 1;. pEle
d72c0 6d 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 m = sqlite3HashF
d72d0 69 6e 64 45 6c 65 6d 28 26 6e 61 6d 65 48 61 73 indElem(&nameHas
d72e0 68 2c 20 70 46 69 6c 65 2d 3e 7a 52 65 61 6c 70 h, pFile->zRealp
d72f0 61 74 68 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 ath, n);. i
d7300 66 28 20 70 45 6c 65 6d 20 29 7b 0a 20 20 20 20 f( pElem ){.
d7310 20 20 20 20 6c 6f 6e 67 20 63 6e 74 20 3d 20 28 long cnt = (
d7320 6c 6f 6e 67 29 70 45 6c 65 6d 2d 3e 64 61 74 61 long)pElem->data
d7330 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 2d 2d 3b ;. cnt--;
d7340 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6e 74 . if( cnt
d7350 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
d7360 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 sqlite3HashInse
d7370 72 74 28 26 6e 61 6d 65 48 61 73 68 2c 20 70 46 rt(&nameHash, pF
d7380 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 2c 20 ile->zRealpath,
d7390 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d n, 0);. }
d73a0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
d73b0 70 45 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 28 76 pElem->data = (v
d73c0 6f 69 64 2a 29 63 6e 74 3b 0a 20 20 20 20 20 20 oid*)cnt;.
d73d0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
d73e0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 4f 53 54 }.#endif. OST
d73f0 52 41 43 45 32 28 22 43 4c 4f 53 45 20 20 20 25 RACE2("CLOSE %
d7400 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 -3d\n", pFile->h
d7410 29 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 );. OpenCount
d7420 65 72 28 2d 31 29 3b 0a 20 20 20 20 6d 65 6d 73 er(-1);. mems
d7430 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a et(pFile, 0, siz
d7440 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a eof(unixFile));.
d7450 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
d7460 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
d7470 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a Close a file..*
d7480 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
d7490 78 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 xClose(sqlite3_f
d74a0 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 66 28 20 ile *id){. if(
d74b0 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 id ){. unixFi
d74c0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
d74d0 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20 20 xFile *)id;.
d74e0 75 6e 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e unixUnlock(id, N
d74f0 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 65 6e 74 O_LOCK);. ent
d7500 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 erMutex();. i
d7510 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 f( pFile->pOpen
d7520 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d && pFile->pOpen-
d7530 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 >nLock ){.
d7540 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 /* If there are
d7550 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b outstanding lock
d7560 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c s, do not actual
d7570 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c ly close the fil
d7580 65 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 e just. **
d7590 79 65 74 20 62 65 63 61 75 73 65 20 74 68 61 74 yet because that
d75a0 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f would clear tho
d75b0 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 se locks. Inste
d75c0 61 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 ad, add the file
d75d0 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 . ** descri
d75e0 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 61 ptor to pOpen->a
d75f0 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c Pending. It wil
d7600 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c l be automatical
d7610 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a 20 ly closed when.
d7620 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 ** the last
d7630 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 lock is cleared
d7640 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
d7650 20 69 6e 74 20 2a 61 4e 65 77 3b 0a 20 20 20 20 int *aNew;.
d7660 20 20 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 struct openCnt
d7670 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d *pOpen = pFile-
d7680 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 61 4e >pOpen;. aN
d7690 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 ew = sqlite3_rea
d76a0 6c 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61 50 65 6e lloc(pOpen->aPen
d76b0 64 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e 6e 50 ding, (pOpen->nP
d76c0 65 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65 6f 66 ending+1)*sizeof
d76d0 28 69 6e 74 29 20 29 3b 0a 20 20 20 20 20 20 69 (int) );. i
d76e0 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 f( aNew==0 ){.
d76f0 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 /* If a ma
d7700 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 6a 75 73 74 lloc fails, just
d7710 20 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 20 64 leak the file d
d7720 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 20 escriptor */.
d7730 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
d7740 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e pOpen->aPendin
d7750 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 g = aNew;.
d7760 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e pOpen->aPendin
d7770 67 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e g[pOpen->nPendin
d7780 67 5d 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 g] = pFile->h;.
d7790 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 pOpen->nP
d77a0 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20 20 20 20 ending++;.
d77b0 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b pFile->h = -1;
d77c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d77d0 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e releaseLockIn
d77e0 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 fo(pFile->pLock)
d77f0 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 ;. releaseOpe
d7800 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 nCnt(pFile->pOpe
d7810 6e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 n);. closeUni
d7820 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20 6c xFile(id);. l
d7830 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
d7840 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
d7850 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 20 53 51 4c _OK;.}...#if SQL
d7860 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
d7870 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66 20 21 49 NG_STYLE..#if !I
d7880 53 5f 56 58 57 4f 52 4b 53 0a 23 70 72 61 67 6d S_VXWORKS.#pragm
d7890 61 20 6d 61 72 6b 20 41 46 50 20 53 75 70 70 6f a mark AFP Suppo
d78a0 72 74 0a 0a 2f 2a 0a 20 2a 2a 20 54 68 65 20 61 rt../*. ** The a
d78b0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
d78c0 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
d78d0 69 6e 73 20 61 6c 6c 20 61 66 70 20 6c 6f 63 6b ins all afp lock
d78e0 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65 0a specific state.
d78f0 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 */.typedef stru
d7900 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e ct afpLockingCon
d7910 74 65 78 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 text afpLockingC
d7920 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 61 ontext;.struct a
d7930 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
d7940 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f {. unsigned lo
d7950 6e 67 20 6c 6f 6e 67 20 73 68 61 72 65 64 4c 6f ng long sharedLo
d7960 63 6b 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20 ckByte;. const
d7970 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 3b 0a char *filePath;.
d7980 7d 3b 0a 0a 73 74 72 75 63 74 20 42 79 74 65 52 };..struct ByteR
d7990 61 6e 67 65 4c 6f 63 6b 50 42 32 0a 7b 0a 20 20 angeLockPB2.{.
d79a0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
d79b0 6e 67 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 ng offset;
d79c0 20 20 2f 2a 20 6f 66 66 73 65 74 20 74 6f 20 66 /* offset to f
d79d0 69 72 73 74 20 62 79 74 65 20 74 6f 20 6c 6f 63 irst byte to loc
d79e0 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 k */. unsigned
d79f0 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 long long length
d7a00 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 62 72 20 ; /* nbr
d7a10 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b of bytes to lock
d7a20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c */. unsigned l
d7a30 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 52 61 6e 67 ong long retRang
d7a40 65 53 74 61 72 74 3b 20 2f 2a 20 6e 62 72 20 6f eStart; /* nbr o
d7a50 66 20 31 73 74 20 62 79 74 65 20 6c 6f 63 6b 65 f 1st byte locke
d7a60 64 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 20 d if successful
d7a70 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
d7a80 61 72 20 75 6e 4c 6f 63 6b 46 6c 61 67 3b 20 20 ar unLockFlag;
d7a90 20 20 20 20 20 20 20 2f 2a 20 31 20 3d 20 75 6e /* 1 = un
d7aa0 6c 6f 63 6b 2c 20 30 20 3d 20 6c 6f 63 6b 20 2a lock, 0 = lock *
d7ab0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
d7ac0 72 20 73 74 61 72 74 45 6e 64 46 6c 61 67 3b 20 r startEndFlag;
d7ad0 20 20 20 20 20 20 2f 2a 20 31 3d 72 65 6c 20 74 /* 1=rel t
d7ae0 6f 20 65 6e 64 20 6f 66 20 66 6f 72 6b 2c 20 30 o end of fork, 0
d7af0 3d 72 65 6c 20 74 6f 20 73 74 61 72 74 20 2a 2f =rel to start */
d7b00 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 . int fd;
d7b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7b20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64 65 73 /* file des
d7b30 63 20 74 6f 20 61 73 73 6f 63 20 74 68 69 73 20 c to assoc this
d7b40 6c 6f 63 6b 20 77 69 74 68 20 2a 2f 0a 7d 3b 0a lock with */.};.
d7b50 0a 23 64 65 66 69 6e 65 20 61 66 70 66 73 42 79 .#define afpfsBy
d7b60 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 teRangeLock2FSCT
d7b70 4c 20 20 20 20 20 20 20 20 5f 49 4f 57 52 28 27 L _IOWR('
d7b80 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 42 z', 23, struct B
d7b90 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 29 yteRangeLockPB2)
d7ba0 0a 0a 2f 2a 20 0a 20 2a 2a 20 52 65 74 75 72 6e ../* . ** Return
d7bb0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
d7bc0 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 ccess, SQLITE_BU
d7bd0 53 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 20 SY on failure..
d7be0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 5f 41 */.static int _A
d7bf0 46 50 46 53 53 65 74 4c 6f 63 6b 28 0a 20 20 63 FPFSSetLock(. c
d7c00 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c onst char *path,
d7c10 20 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 . unixFile *pF
d7c20 69 6c 65 2c 20 0a 20 20 75 6e 73 69 67 6e 65 64 ile, . unsigned
d7c30 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 long long offse
d7c40 74 2c 20 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c t, . unsigned l
d7c50 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c ong long length,
d7c60 20 0a 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46 . int setLockF
d7c70 6c 61 67 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 lag.){. struct
d7c80 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 ByteRangeLockPB2
d7c90 20 20 20 20 20 20 20 70 62 3b 0a 20 20 69 6e 74 pb;. int
d7ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d7cb0 20 20 20 20 20 65 72 72 3b 0a 20 20 0a 20 20 70 err;. . p
d7cc0 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 73 b.unLockFlag = s
d7cd0 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 3a etLockFlag ? 0 :
d7ce0 20 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 6e 1;. pb.startEn
d7cf0 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 2e dFlag = 0;. pb.
d7d00 6f 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74 3b offset = offset;
d7d10 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 6c . pb.length = l
d7d20 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 20 ength; . pb.fd
d7d30 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 4f 53 = pFile->h;. OS
d7d40 54 52 41 43 45 35 28 22 41 46 50 4c 4f 43 4b 20 TRACE5("AFPLOCK
d7d50 73 65 74 74 69 6e 67 20 6c 6f 63 6b 20 25 73 20 setting lock %s
d7d60 66 6f 72 20 25 64 20 69 6e 20 72 61 6e 67 65 20 for %d in range
d7d70 25 6c 6c 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 %llx:%llx\n", .
d7d80 20 20 20 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f (setLockFlag?
d7d90 22 4f 4e 22 3a 22 4f 46 46 22 29 2c 20 70 46 69 "ON":"OFF"), pFi
d7da0 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 6c le->h, offset, l
d7db0 65 6e 67 74 68 29 3b 0a 20 20 65 72 72 20 3d 20 ength);. err =
d7dc0 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66 fsctl(path, afpf
d7dd0 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 sByteRangeLock2F
d7de0 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20 SCTL, &pb, 0);.
d7df0 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 if ( err==-1 )
d7e00 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 {. int rc;.
d7e10 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
d7e20 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43 rrno;. OSTRAC
d7e30 45 34 28 22 41 46 50 4c 4f 43 4b 20 66 61 69 6c E4("AFPLOCK fail
d7e40 65 64 20 74 6f 20 66 73 63 74 6c 28 29 20 27 25 ed to fsctl() '%
d7e50 73 27 20 25 64 20 25 73 5c 6e 22 2c 20 70 61 74 s' %d %s\n", pat
d7e60 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72 h, tErrno, strer
d7e70 72 6f 72 28 74 45 72 72 6e 6f 29 29 3b 0a 20 20 ror(tErrno));.
d7e80 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d7e90 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d7ea0 28 74 45 72 72 6e 6f 2c 20 73 65 74 4c 6f 63 6b (tErrno, setLock
d7eb0 46 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f Flag ? SQLITE_IO
d7ec0 45 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 ERR_LOCK : SQLIT
d7ed0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
d7ee0 20 2f 2a 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 /* error */.
d7ef0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d7f00 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 OR(rc) ){.
d7f10 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d7f20 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d = tErrno;. }
d7f30 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
d7f40 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 } else {. r
d7f50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d7f60 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 46 50 2d 73 . }.}../* AFP-s
d7f70 74 79 6c 65 20 72 65 73 65 72 76 65 64 20 6c 6f tyle reserved lo
d7f80 63 6b 20 63 68 65 63 6b 69 6e 67 20 66 6f 6c 6c ck checking foll
d7f90 6f 77 69 6e 67 20 74 68 65 20 62 65 68 61 76 69 owing the behavi
d7fa0 6f 72 20 6f 66 20 0a 2a 2a 20 75 6e 69 78 43 68 or of .** unixCh
d7fb0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c eckReservedLock,
d7fc0 20 73 65 65 20 74 68 65 20 75 6e 69 78 43 68 65 see the unixChe
d7fd0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 66 ckReservedLock f
d7fe0 75 6e 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 unction comments
d7ff0 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 */.static int a
d8000 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c fpCheckReservedL
d8010 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
d8020 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f *id, int *pResO
d8030 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ut){. int rc =
d8040 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
d8050 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 reserved = 0;.
d8060 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d8070 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d8080 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 ;. . SimulateI
d8090 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
d80a0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 QLITE_IOERR_CHEC
d80b0 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 KRESERVEDLOCK; )
d80c0 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
d80d0 46 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63 File );. afpLoc
d80e0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e kingContext *con
d80f0 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 text = (afpLocki
d8100 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 ngContext *) pFi
d8110 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
d8120 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 xt;. . /* Chec
d8130 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e k if a thread in
d8140 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f this process ho
d8150 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 lds such a lock
d8160 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
d8170 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
d8180 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 LOCK ){. rese
d8190 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 rved = 1;. }.
d81a0 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 . /* Otherwise
d81b0 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 see if some othe
d81c0 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 r process holds
d81d0 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 it.. */. if(
d81e0 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 !reserved ){.
d81f0 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53 /* lock the RES
d8200 45 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20 ERVED byte */.
d8210 20 20 69 6e 74 20 6c 72 63 20 3d 20 5f 41 46 50 int lrc = _AFP
d8220 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 FSSetLock(contex
d8230 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 t->filePath, pFi
d8240 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 le, RESERVED_BYT
d8250 45 2c 20 31 2c 31 29 3b 20 20 0a 20 20 20 20 69 E, 1,1); . i
d8260 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 f( SQLITE_OK==lr
d8270 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 c ){. /* if
d8280 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 6e we succeeded in
d8290 20 74 61 6b 69 6e 67 20 74 68 65 20 72 65 73 65 taking the rese
d82a0 72 76 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 rved lock, unloc
d82b0 6b 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65 0a k it to restore.
d82c0 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 69 ** the ori
d82d0 67 69 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a 20 ginal state */.
d82e0 20 20 20 20 20 6c 72 63 20 3d 20 5f 41 46 50 46 lrc = _AFPF
d82f0 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 SSetLock(context
d8300 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c ->filePath, pFil
d8310 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 e, RESERVED_BYTE
d8320 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 65 , 1, 0);. } e
d8330 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20 69 lse {. /* i
d8340 66 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 67 f we failed to g
d8350 65 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65 6e et the lock then
d8360 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 75 someone else mu
d8370 73 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20 20 st have it */.
d8380 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 reserved = 1
d8390 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
d83a0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 IS_LOCK_ERROR(lr
d83b0 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 3d 6c c) ){. rc=l
d83c0 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rc;. }. }.
d83d0 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 . OSTRACE4("TES
d83e0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
d83f0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
d8400 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a rc, reserved);.
d8410 20 20 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 . *pResOut =
d8420 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 reserved;. retu
d8430 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 46 50 rn rc;.}../* AFP
d8440 2d 73 74 79 6c 65 20 6c 6f 63 6b 69 6e 67 20 66 -style locking f
d8450 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 62 65 68 ollowing the beh
d8460 61 76 69 6f 72 20 6f 66 20 75 6e 69 78 4c 6f 63 avior of unixLoc
d8470 6b 2c 20 73 65 65 20 74 68 65 20 75 6e 69 78 4c k, see the unixL
d8480 6f 63 6b 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ock .** function
d8490 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 64 65 comments for de
d84a0 74 61 69 6c 73 20 6f 66 20 6c 6f 63 6b 20 6d 61 tails of lock ma
d84b0 6e 61 67 65 6d 65 6e 74 2e 20 2a 2f 0a 73 74 61 nagement. */.sta
d84c0 74 69 63 20 69 6e 74 20 61 66 70 4c 6f 63 6b 28 tic int afpLock(
d84d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d84e0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b , int locktype){
d84f0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d8500 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c TE_OK;. unixFil
d8510 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
d8520 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c File*)id;. afpL
d8530 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 ockingContext *c
d8540 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 ontext = (afpLoc
d8550 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 kingContext *) p
d8560 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
d8570 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73 65 72 text;. . asser
d8580 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 t( pFile );. OS
d8590 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 TRACE5("LOCK
d85a0 25 64 20 25 73 20 77 61 73 20 25 73 20 70 69 64 %d %s was %s pid
d85b0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
d85c0 2c 0a 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 ,. lockt
d85d0 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 ypeName(locktype
d85e0 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 ), locktypeName(
d85f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 pFile->locktype)
d8600 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 , getpid());..
d8610 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 /* If there is a
d8620 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 lready a lock of
d8630 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f this type or mo
d8640 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f re restrictive o
d8650 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 n the. ** unixF
d8660 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e ile, do nothing.
d8670 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 61 Don't use the a
d8680 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 fp_end_lock: exi
d8690 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 t path, as. **
d86a0 65 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 enterMutex() has
d86b0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 n't been called
d86c0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 yet.. */. if(
d86d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
d86e0 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
d86f0 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 OSTRACE3("LOCK
d8700 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 %d %s ok (alr
d8710 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 eady held)\n", p
d8720 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
d8730 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 locktypeName
d8740 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 (locktype));.
d8750 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d8760 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b K;. }.. /* Mak
d8770 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 e sure the locki
d8780 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 ng sequence is c
d8790 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 orrect. */. as
d87a0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 sert( pFile->loc
d87b0 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c ktype!=NO_LOCK |
d87c0 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 | locktype==SHAR
d87d0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 ED_LOCK );. ass
d87e0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 ert( locktype!=P
d87f0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 ENDING_LOCK );.
d8800 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
d8810 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e!=RESERVED_LOCK
d8820 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 || pFile->lockt
d8830 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d8840 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 );. . /* This
d8850 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 mutex is needed
d8860 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e because pFile->
d8870 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 pLock is shared
d8880 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 across threads.
d8890 20 2a 2f 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 */. enterMutex
d88a0 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 ();.. /* Make s
d88b0 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ure the current
d88c0 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 thread owns the
d88d0 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 pFile.. */. rc
d88e0 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 = transferOwner
d88f0 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 ship(pFile);. i
d8900 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
d8910 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 4d 75 74 ){. leaveMut
d8920 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ex();. return
d8930 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 rc;. }. .
d8940 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 /* A PENDING loc
d8950 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f k is needed befo
d8960 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53 re acquiring a S
d8970 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 HARED lock and b
d8980 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75 69 efore. ** acqui
d8990 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56 ring an EXCLUSIV
d89a0 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 E lock. For the
d89b0 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 SHARED lock, th
d89c0 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 e PENDING will.
d89d0 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 2e ** be released.
d89e0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
d89f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
d8a00 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 K . || (loc
d8a10 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
d8a20 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e _LOCK && pFile->
d8a30 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47 locktype<PENDING
d8a40 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 _LOCK). ){.
d8a50 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 int failed;.
d8a60 66 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 failed = _AFPFSS
d8a70 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
d8a80 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2c filePath, pFile,
d8a90 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 PENDING_BYTE, 1
d8aa0 2c 20 31 29 3b 0a 20 20 20 20 69 66 20 28 66 61 , 1);. if (fa
d8ab0 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63 iled) {. rc
d8ac0 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 = failed;.
d8ad0 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f goto afp_end_lo
d8ae0 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ck;. }. }.
d8af0 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c . /* If control
d8b00 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f gets to this po
d8b10 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c int, then actual
d8b20 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 ly go ahead and
d8b30 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 make. ** operat
d8b40 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 ing system calls
d8b50 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
d8b60 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 ed lock.. */.
d8b70 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
d8b80 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d8b90 20 69 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c int lk, lrc1, l
d8ba0 72 63 32 2c 20 6c 72 63 31 45 72 72 6e 6f 3b 0a rc2, lrc1Errno;.
d8bb0 20 20 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 . /* Now
d8bc0 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 get the read-loc
d8bd0 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f k SHARED_LOCK */
d8be0 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61 . /* note tha
d8bf0 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66 t the quality of
d8c00 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 the randomness
d8c10 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74 doesn't matter t
d8c20 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20 hat much */.
d8c30 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a lk = random(); .
d8c40 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 context->sha
d8c50 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 6c redLockByte = (l
d8c60 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25 k & 0x7fffffff)%
d8c70 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 (SHARED_SIZE - 1
d8c80 29 3b 0a 20 20 20 20 6c 72 63 31 20 3d 20 5f 41 );. lrc1 = _A
d8c90 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 FPFSSetLock(cont
d8ca0 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 ext->filePath, p
d8cb0 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 File, .
d8cc0 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63 6f SHARED_FIRST+co
d8cd0 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63 ntext->sharedLoc
d8ce0 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 kByte, 1, 1);.
d8cf0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d8d00 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 ROR(lrc1) ){.
d8d10 20 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 lrc1Errno = p
d8d20 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b File->lastErrno;
d8d30 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 . }. /* Dr
d8d40 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 op the temporary
d8d50 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f PENDING lock */
d8d60 0a 20 20 20 20 6c 72 63 32 20 3d 20 5f 41 46 50 . lrc2 = _AFP
d8d70 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 FSSetLock(contex
d8d80 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 t->filePath, pFi
d8d90 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 le, PENDING_BYTE
d8da0 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 0a 20 20 , 1, 0);. .
d8db0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d8dc0 52 4f 52 28 6c 72 63 31 29 20 29 20 7b 0a 20 20 ROR(lrc1) ) {.
d8dd0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d8de0 72 72 6e 6f 20 3d 20 6c 72 63 31 45 72 72 6e 6f rrno = lrc1Errno
d8df0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 ;. rc = lrc
d8e00 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 1;. goto af
d8e10 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 p_end_lock;.
d8e20 7d 20 65 6c 73 65 20 69 66 28 20 49 53 5f 4c 4f } else if( IS_LO
d8e30 43 4b 5f 45 52 52 4f 52 28 6c 72 63 32 29 20 29 CK_ERROR(lrc2) )
d8e40 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 {. rc = lrc
d8e50 32 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 2;. goto af
d8e60 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 p_end_lock;.
d8e70 7d 20 65 6c 73 65 20 69 66 28 20 6c 72 63 31 20 } else if( lrc1
d8e80 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b != SQLITE_OK ) {
d8e90 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 . rc = lrc1
d8ea0 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 ;. } else {.
d8eb0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b pFile->lock
d8ec0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f type = SHARED_LO
d8ed0 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 CK;. }. }els
d8ee0 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 e{. /* The re
d8ef0 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20 quest was for a
d8f00 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c RESERVED or EXCL
d8f10 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 USIVE lock. It
d8f20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 is. ** assume
d8f30 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 d that there is
d8f40 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 a SHARED or grea
d8f50 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ter lock on the
d8f60 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 file. ** alre
d8f70 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ady.. */.
d8f80 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 30 3b 0a int failed = 0;.
d8f90 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70 assert( 0!=p
d8fa0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
d8fb0 3b 0a 20 20 20 20 69 66 20 28 6c 6f 63 6b 74 79 ;. if (lockty
d8fc0 70 65 20 3e 3d 20 52 45 53 45 52 56 45 44 5f 4c pe >= RESERVED_L
d8fd0 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f OCK && pFile->lo
d8fe0 63 6b 74 79 70 65 20 3c 20 52 45 53 45 52 56 45 cktype < RESERVE
d8ff0 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 D_LOCK) {.
d9000 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52 /* Acquire a R
d9010 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a ESERVED lock */.
d9020 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d failed =
d9030 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 _AFPFSSetLock(c
d9040 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 ontext->filePath
d9050 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 , pFile, RESERVE
d9060 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20 D_BYTE, 1,1);.
d9070 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66 61 69 }. if (!fai
d9080 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 20 led && locktype
d9090 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 == EXCLUSIVE_LOC
d90a0 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63 K) {. /* Ac
d90b0 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 quire an EXCLUSI
d90c0 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 VE lock */.
d90d0 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d . /* Rem
d90e0 6f 76 65 20 74 68 65 20 73 68 61 72 65 64 20 6c ove the shared l
d90f0 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79 69 6e ock before tryin
d9100 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20 77 65 g the range. we
d9110 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 20 'll need to .
d9120 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 73 ** reestablis
d9130 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 h the shared loc
d9140 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20 67 65 k if we can't ge
d9150 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f 63 6b t the afpUnlock
d9160 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
d9170 69 66 20 28 21 28 66 61 69 6c 65 64 20 3d 20 5f if (!(failed = _
d9180 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e AFPFSSetLock(con
d9190 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 text->filePath,
d91a0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 pFile, SHARED_FI
d91b0 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20 20 20 RST +.
d91c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
d91d0 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f ontext->sharedLo
d91e0 63 6b 42 79 74 65 2c 20 31 2c 20 30 29 29 29 20 ckByte, 1, 0)))
d91f0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77 {. /* now
d9200 20 61 74 74 65 6d 6d 70 74 20 74 6f 20 67 65 74 attemmpt to get
d9210 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c the exclusive l
d9220 6f 63 6b 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20 ock range */.
d9230 20 20 20 20 20 66 61 69 6c 65 64 20 3d 20 5f 41 failed = _A
d9240 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 FPFSSetLock(cont
d9250 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 ext->filePath, p
d9260 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 File, SHARED_FIR
d9270 53 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 ST, .
d9280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9290 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a 45 2c SHARED_SIZE,
d92a0 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 20 1);. if
d92b0 28 66 61 69 6c 65 64 20 26 26 20 28 66 61 69 6c (failed && (fail
d92c0 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f ed = _AFPFSSetLo
d92d0 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 ck(context->file
d92e0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 Path, pFile, .
d92f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9300 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 SHARED_FIRS
d9310 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 T + context->sha
d9320 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 redLockByte, 1,
d9330 31 29 29 29 20 7b 0a 20 20 20 20 20 20 20 20 20 1))) {.
d9340 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 rc = failed;.
d9350 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 }. }
d9360 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 72 else {. r
d9370 63 20 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20 20 c = failed; .
d9380 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
d9390 66 28 20 66 61 69 6c 65 64 20 29 7b 0a 20 20 20 f( failed ){.
d93a0 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a rc = failed;.
d93b0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 }. }. . i
d93c0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
d93d0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
d93e0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d93f0 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 pe;. }else if(
d9400 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
d9410 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 IVE_LOCK ){.
d9420 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d9430 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a = PENDING_LOCK;.
d9440 20 20 7d 0a 20 20 0a 61 66 70 5f 65 6e 64 5f 6c }. .afp_end_l
d9450 6f 63 6b 3a 0a 20 20 6c 65 61 76 65 4d 75 74 65 ock:. leaveMute
d9460 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 x();. OSTRACE4(
d9470 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 "LOCK %d %s %
d9480 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 s\n", pFile->h,
d9490 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 locktypeName(loc
d94a0 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 20 ktype), .
d94b0 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 rc==SQLITE_OK
d94c0 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 ? "ok" : "failed
d94d0 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ");. return rc;
d94e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 .}../*.** Lower
d94f0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
d9500 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 l on file descri
d9510 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f ptor pFile to lo
d9520 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 cktype. locktyp
d9530 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 e.** must be eit
d9540 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
d9550 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
d9560 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
d9570 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
d9580 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
d9590 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
d95a0 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
d95b0 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
d95c0 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
d95d0 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f e is a no-op..*/
d95e0 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 55 .static int afpU
d95f0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
d9600 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
d9610 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20 72 63 type) {. int rc
d9620 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
d9630 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d9640 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d9650 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e . afpLockingCon
d9660 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 text *context =
d9670 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 (afpLockingConte
d9680 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 xt *) pFile->loc
d9690 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 kingContext;..
d96a0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d96b0 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c . OSTRACE5("UNL
d96c0 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 OCK %d %d was %
d96d0 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 d pid=%d\n", pFi
d96e0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
d96f0 0a 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d . pFile-
d9700 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 >locktype, getpi
d9710 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 d());.. assert(
d9720 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 locktype<=SHARE
d9730 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 D_LOCK );. if(
d9740 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c pFile->locktype<
d9750 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
d9760 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d9770 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45 K;. }. if( CHE
d9780 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c CK_THREADID(pFil
d9790 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e e) ){. return
d97a0 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
d97b0 20 20 7d 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 }. enterMutex
d97c0 28 29 3b 0a 20 20 69 6e 74 20 66 61 69 6c 65 64 ();. int failed
d97d0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
d97e0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
d97f0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
d9800 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 ){. if( lockt
d9810 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d9820 20 29 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 75 6e ){.. /* un
d9830 6c 6f 63 6b 20 74 68 65 20 65 78 63 6c 75 73 69 lock the exclusi
d9840 76 65 20 72 61 6e 67 65 20 2d 20 74 68 65 6e 20 ve range - then
d9850 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 68 65 re-establish the
d9860 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a shared lock */.
d9870 20 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d if (pFile-
d9880 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 >locktype==EXCLU
d9890 53 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 SIVE_LOCK) {.
d98a0 20 20 20 20 20 66 61 69 6c 65 64 20 3d 20 5f 41 failed = _A
d98b0 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 FPFSSetLock(cont
d98c0 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 ext->filePath, p
d98d0 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 File, SHARED_FIR
d98e0 53 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 ST, .
d98f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9900 20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a SHARED_SIZ
d9910 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 E, 0);. i
d9920 66 20 28 21 66 61 69 6c 65 64 29 20 7b 0a 20 20 f (!failed) {.
d9930 20 20 20 20 20 20 20 20 2f 2a 20 73 75 63 63 65 /* succe
d9940 73 73 66 75 6c 6c 79 20 72 65 6d 6f 76 65 64 20 ssfully removed
d9950 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f the exclusive lo
d9960 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ck */.
d9970 69 66 20 28 28 66 61 69 6c 65 64 20 3d 20 5f 41 if ((failed = _A
d9980 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 FPFSSetLock(cont
d9990 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 ext->filePath, p
d99a0 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 File, SHARED_FIR
d99b0 53 54 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ST+.
d99c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d99d0 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c context->sharedL
d99e0 6f 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 29 29 ockByte, 1, 1)))
d99f0 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f {. /
d9a00 2a 20 66 61 69 6c 65 64 20 74 6f 20 72 65 2d 65 * failed to re-e
d9a10 73 74 61 62 6c 69 73 68 20 6f 75 72 20 73 68 61 stablish our sha
d9a20 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 red lock */.
d9a30 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 rc = fai
d9a40 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 20 20 7d led;. }
d9a50 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 . } else
d9a60 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
d9a70 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 failed;.
d9a80 20 7d 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 } . }.
d9a90 7d 0a 20 20 20 20 69 66 20 28 72 63 20 3d 3d 20 }. if (rc ==
d9aa0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 SQLITE_OK && pFi
d9ab0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 le->locktype>=PE
d9ac0 4e 44 49 4e 47 5f 4c 4f 43 4b 29 20 7b 0a 20 20 NDING_LOCK) {.
d9ad0 20 20 20 20 69 66 20 28 28 66 61 69 6c 65 64 20 if ((failed
d9ae0 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 = _AFPFSSetLock(
d9af0 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 context->filePat
d9b00 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 h, pFile, .
d9b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9b20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 45 4e PEN
d9b30 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 DING_BYTE, 1, 0)
d9b40 29 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 )){. /* f
d9b50 61 69 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65 ailed to release
d9b60 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 the pending loc
d9b70 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 k */. rc
d9b80 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20 20 20 20 = failed; .
d9b90 20 7d 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66 }. } . if
d9ba0 20 28 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f (rc == SQLITE_O
d9bb0 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b K && pFile->lock
d9bc0 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c type>=RESERVED_L
d9bd0 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 69 66 20 OCK) {. if
d9be0 28 28 66 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 ((failed = _AFPF
d9bf0 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 SSetLock(context
d9c00 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c ->filePath, pFil
d9c10 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 e, .
d9c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9c30 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f 42 RESERVED_B
d9c40 59 54 45 2c 20 31 2c 20 30 29 29 29 20 7b 0a 20 YTE, 1, 0))) {.
d9c50 20 20 20 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 /* failed
d9c60 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20 to release the
d9c70 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f reserved lock */
d9c80 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 . rc = fa
d9c90 69 6c 65 64 3b 20 20 0a 20 20 20 20 20 20 7d 0a iled; . }.
d9ca0 20 20 20 20 7d 20 0a 20 20 7d 0a 20 20 69 66 28 } . }. if(
d9cb0 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f locktype==NO_LO
d9cc0 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 CK ){. int fa
d9cd0 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 iled = _AFPFSSet
d9ce0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 Lock(context->fi
d9cf0 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a lePath, pFile, .
d9d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 S
d9d20 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 63 6f HARED_FIRST + co
d9d30 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63 ntext->sharedLoc
d9d40 6b 42 79 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20 kByte, 1, 0);.
d9d50 20 20 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a if (failed) {.
d9d60 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 rc = faile
d9d70 64 3b 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 d; . }. }.
d9d80 20 69 66 20 28 72 63 20 3d 3d 20 53 51 4c 49 54 if (rc == SQLIT
d9d90 45 5f 4f 4b 29 0a 20 20 20 20 70 46 69 6c 65 2d E_OK). pFile-
d9da0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
d9db0 74 79 70 65 3b 0a 20 20 6c 65 61 76 65 4d 75 74 type;. leaveMut
d9dc0 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ex();. return r
d9dd0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 c;.}../*.** Clos
d9de0 65 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61 6e e a file & clean
d9df0 75 70 20 41 46 50 20 73 70 65 63 69 66 69 63 20 up AFP specific
d9e00 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 locking context
d9e10 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
d9e20 66 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f fpClose(sqlite3_
d9e30 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 file *id) {. if
d9e40 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 ( id ){. unix
d9e50 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d9e60 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 nixFile*)id;.
d9e70 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e afpUnlock(id, N
d9e80 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c O_LOCK);. sql
d9e90 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d ite3_free(pFile-
d9ea0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 >lockingContext)
d9eb0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 ;. }. return c
d9ec0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 loseUnixFile(id)
d9ed0 3b 0a 7d 0a 0a 0a 23 70 72 61 67 6d 61 20 6d 61 ;.}...#pragma ma
d9ee0 72 6b 20 66 6c 6f 63 6b 28 29 20 73 74 79 6c 65 rk flock() style
d9ef0 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 0a 2a 2a 20 locking../*.**
d9f00 54 68 65 20 66 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 The flockLocking
d9f10 43 6f 6e 74 65 78 74 20 69 73 20 6e 6f 74 20 75 Context is not u
d9f20 73 65 64 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76 sed.*/.typedef v
d9f30 6f 69 64 20 66 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 oid flockLocking
d9f40 43 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 20 66 6c 6f Context;../* flo
d9f50 63 6b 2d 73 74 79 6c 65 20 72 65 73 65 72 76 65 ck-style reserve
d9f60 64 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 20 d lock checking
d9f70 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 62 65 following the be
d9f80 68 61 76 69 6f 72 20 6f 66 20 0a 20 2a 2a 20 75 havior of . ** u
d9f90 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 nixCheckReserved
d9fa0 4c 6f 63 6b 2c 20 73 65 65 20 74 68 65 20 75 6e Lock, see the un
d9fb0 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c ixCheckReservedL
d9fc0 6f 63 6b 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d ock function com
d9fd0 6d 65 6e 74 73 20 2a 2f 0a 73 74 61 74 69 63 20 ments */.static
d9fe0 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 int flockCheckRe
d9ff0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
da000 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
da010 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e *pResOut){. in
da020 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
da030 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 ;. int reserved
da040 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 = 0;. unixFile
da050 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
da060 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 ile*)id;. . Si
da070 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
da080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
da090 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
da0a0 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 LOCK; );. . as
da0b0 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 sert( pFile );.
da0c0 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 . /* Check if
da0d0 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 a thread in this
da0e0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 process holds s
da0f0 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 uch a lock */.
da100 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
da110 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
da120 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 ){. reserved
da130 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a = 1;. }. . /*
da140 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 Otherwise see i
da150 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f f some other pro
da160 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a cess holds it. *
da170 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 /. if( !reserve
da180 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 d ){. /* atte
da190 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 6c mpt to get the l
da1a0 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c ock */. int l
da1b0 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 rc = flock(pFile
da1c0 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c ->h, LOCK_EX | L
da1d0 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 69 66 28 OCK_NB);. if(
da1e0 20 21 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f !lrc ){. /
da1f0 2a 20 67 6f 74 20 74 68 65 20 6c 6f 63 6b 2c 20 * got the lock,
da200 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a 20 20 20 unlock it */.
da210 20 20 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 lrc = flock(p
da220 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e File->h, LOCK_UN
da230 29 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c 72 );. if ( lr
da240 63 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69 6e c ) {. in
da250 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f t tErrno = errno
da260 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 6c ;. /* unl
da270 6f 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68 20 ock failed with
da280 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 an error */.
da290 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 lrc = sqlite
da2a0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
da2b0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
da2c0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 TE_IOERR_UNLOCK)
da2d0 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 ; . if( I
da2e0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 S_LOCK_ERROR(lrc
da2f0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
da300 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
da310 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
da320 20 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 rc = lrc;.
da330 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
da340 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
da350 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
da360 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73 errno;. res
da370 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 erved = 1;.
da380 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 /* someone else
da390 20 6d 69 67 68 74 20 68 61 76 65 20 69 74 20 72 might have it r
da3a0 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20 eserved */.
da3b0 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 lrc = sqliteErr
da3c0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
da3d0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
da3e0 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 IOERR_LOCK); .
da3f0 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
da400 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 ERROR(lrc) ){.
da410 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
da420 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
da430 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 . rc = lr
da440 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
da450 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 . }. OSTRACE4(
da460 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
da470 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 %d %d\n", pFile
da480 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 ->h, rc, reserve
da490 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 d);.. *pResOut
da4a0 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 = reserved;. re
da4b0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 turn rc;.}..stat
da4c0 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b ic int flockLock
da4d0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
da4e0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
da4f0 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 {. int rc = SQ
da500 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 LITE_OK;. unixF
da510 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
da520 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 ixFile*)id;.. a
da530 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
da540 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 . /* if we alre
da550 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c ady have a lock,
da560 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 it is exclusive
da570 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 . . ** Just ad
da580 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 just level and p
da590 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 unt on outta her
da5a0 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c e. */. if (pFil
da5b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f e->locktype > NO
da5c0 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 _LOCK) {. pFi
da5d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
da5e0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 ocktype;. ret
da5f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
da600 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 }. . /* grab
da610 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
da620 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66 6c k */. . if (fl
da630 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f ock(pFile->h, LO
da640 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 CK_EX | LOCK_NB)
da650 29 20 7b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 ) {. int tErr
da660 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
da670 2f 2a 20 64 69 64 6e 27 74 20 67 65 74 2c 20 6d /* didn't get, m
da680 75 73 74 20 62 65 20 62 75 73 79 20 2a 2f 0a 20 ust be busy */.
da690 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
da6a0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
da6b0 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
da6c0 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 _IOERR_LOCK);.
da6d0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
da6e0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
da6f0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
da700 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
da710 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 }. } else {.
da720 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 /* got it, set
da730 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 the type and ret
da740 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 urn ok */. pF
da750 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
da760 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 locktype;. }.
da770 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 OSTRACE4("LOCK
da780 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 %d %s %s\n", p
da790 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
da7a0 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c eName(locktype),
da7b0 20 0a 20 20 20 20 20 20 20 20 20 20 20 72 63 3d . rc=
da7c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b =SQLITE_OK ? "ok
da7d0 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 " : "failed");.
da7e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 return rc;.}..s
da7f0 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 55 tatic int flockU
da800 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
da810 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
da820 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 type) {. unixFi
da830 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
da840 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 xFile*)id;. .
da850 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
da860 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c . OSTRACE5("UNL
da870 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 OCK %d %d was %
da880 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 d pid=%d\n", pFi
da890 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
da8a0 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c . pFil
da8b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 e->locktype, get
da8c0 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 pid());. assert
da8d0 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
da8e0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 ED_LOCK );. .
da8f0 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 /* no-op if poss
da900 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 ible */. if( pF
da910 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c ile->locktype==l
da920 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 ocktype ){. r
da930 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
da940 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 . }. . /* sha
da950 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 red can just be
da960 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 61 set because we a
da970 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 lways have an ex
da980 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 clusive */. if
da990 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 (locktype==SHARE
da9a0 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 D_LOCK) {. pF
da9b0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
da9c0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 locktype;. re
da9d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
da9e0 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 }. . /* no,
da9f0 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20 really, unlock.
daa00 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 6c */. int rc = fl
daa10 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f ock(pFile->h, LO
daa20 43 4b 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72 63 CK_UN);. if (rc
daa30 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20 74 ) {. int r, t
daa40 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
daa50 20 20 20 72 20 3d 20 73 71 6c 69 74 65 45 72 72 r = sqliteErr
daa60 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
daa70 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
daa80 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 IOERR_UNLOCK);.
daa90 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
daaa0 52 52 4f 52 28 72 29 20 29 7b 0a 20 20 20 20 20 RROR(r) ){.
daab0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
daac0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
daad0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 3b 0a }. return r;.
daae0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 } else {. p
daaf0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
dab00 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 NO_LOCK;. re
dab10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
dab20 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f }.}../*.** Clo
dab30 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 se a file..*/.st
dab40 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c atic int flockCl
dab50 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
dab60 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 *id) {. if( id
dab70 20 29 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c ){. flockUnl
dab80 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 ock(id, NO_LOCK)
dab90 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 ;. }. return c
daba0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 loseUnixFile(id)
dabb0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 ;.}..#endif /* !
dabc0 49 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 0a 23 IS_VXWORKS */..#
dabd0 70 72 61 67 6d 61 20 6d 61 72 6b 20 4f 6c 64 2d pragma mark Old-
dabe0 53 63 68 6f 6f 6c 20 2e 6c 6f 63 6b 20 66 69 6c School .lock fil
dabf0 65 20 62 61 73 65 64 20 6c 6f 63 6b 69 6e 67 0a e based locking.
dac00 0a 2f 2a 20 44 6f 74 6c 6f 63 6b 2d 73 74 79 6c ./* Dotlock-styl
dac10 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 e reserved lock
dac20 63 68 65 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69 checking followi
dac30 6e 67 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 ng the behavior
dac40 6f 66 20 0a 2a 2a 20 75 6e 69 78 43 68 65 63 6b of .** unixCheck
dac50 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 20 73 65 ReservedLock, se
dac60 65 20 74 68 65 20 75 6e 69 78 43 68 65 63 6b 52 e the unixCheckR
dac70 65 73 65 72 76 65 64 4c 6f 63 6b 20 66 75 6e 63 eservedLock func
dac80 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f tion comments */
dac90 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c .static int dotl
daca0 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 ockCheckReserved
dacb0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
dacc0 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
dacd0 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 Out) {. int rc
dace0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
dacf0 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b nt reserved = 0;
dad00 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
dad10 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
dad20 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 id;.. SimulateI
dad30 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
dad40 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 QLITE_IOERR_CHEC
dad50 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 KRESERVEDLOCK; )
dad60 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
dad70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 File );.. /* Ch
dad80 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 eck if a thread
dad90 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 in this process
dada0 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 holds such a loc
dadb0 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 k */. if( pFile
dadc0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 ->locktype>SHARE
dadd0 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 D_LOCK ){. re
dade0 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a served = 1;. }.
dadf0 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 . /* Otherwis
dae00 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 e see if some ot
dae10 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 her process hold
dae20 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 s it. */. if( !
dae30 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 reserved ){.
dae40 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 char *zLockFile
dae50 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d = (char *)pFile-
dae60 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
dae70 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 . struct stat
dae80 20 73 74 61 74 42 75 66 3b 0a 20 20 20 20 0a 20 statBuf;. .
dae90 20 20 20 69 66 28 20 6c 73 74 61 74 28 7a 4c 6f if( lstat(zLo
daea0 63 6b 46 69 6c 65 2c 20 26 73 74 61 74 42 75 66 ckFile, &statBuf
daeb0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a )==0 ){. /*
daec0 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 73 6f file exists, so
daed0 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 74 meone else has t
daee0 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 he lock */.
daef0 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 reserved = 1;.
daf00 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
daf10 2f 2a 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 /* file does not
daf20 20 65 78 69 73 74 2c 20 77 65 20 63 6f 75 6c 64 exist, we could
daf30 20 68 61 76 65 20 69 74 20 69 66 20 77 65 20 77 have it if we w
daf40 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 ant it */.
daf50 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
daf60 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 4e no;. if( EN
daf70 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20 29 OENT != tErrno )
daf80 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
daf90 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
dafa0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
dafb0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 SQLITE_IOERR_CH
dafc0 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 ECKRESERVEDLOCK)
dafd0 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d ;. pFile-
dafe0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
daff0 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rno;. }.
db000 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 }. }. OSTRACE
db010 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 4("TEST WR-LOCK
db020 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 %d %d %d\n", pFi
db030 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 le->h, rc, reser
db040 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 ved);.. *pResOu
db050 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 t = reserved;.
db060 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 return rc;.}..st
db070 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b atic int dotlock
db080 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
db090 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
db0a0 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c ype) {. unixFil
db0b0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
db0c0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 File*)id;. int
db0d0 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 fd;. char *zLoc
db0e0 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 kFile = (char *)
db0f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
db100 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 72 63 3d ntext;. int rc=
db110 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a SQLITE_OK;.. /*
db120 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68 if we already h
db130 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 ave a lock, it i
db140 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 s exclusive. .
db150 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20 ** Just adjust
db160 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f level and punt o
db170 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f n outta here. */
db180 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f . if (pFile->lo
db190 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b cktype > NO_LOCK
db1a0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ) {. pFile->l
db1b0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
db1c0 70 65 3b 0a 23 69 66 20 21 49 53 5f 56 58 57 4f pe;.#if !IS_VXWO
db1d0 52 4b 53 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 RKS. /* Alway
db1e0 73 20 75 70 64 61 74 65 20 74 68 65 20 74 69 6d s update the tim
db1f0 65 73 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c estamp on the ol
db200 64 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74 d file */. ut
db210 69 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 imes(zLockFile,
db220 4e 55 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 NULL);.#endif.
db230 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
db240 3b 0a 20 20 20 20 67 6f 74 6f 20 64 6f 74 6c 6f ;. goto dotlo
db250 63 6b 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d ck_end_lock;. }
db260 0a 20 20 0a 20 20 2f 2a 20 63 68 65 63 6b 20 74 . . /* check t
db270 6f 20 73 65 65 20 69 66 20 6c 6f 63 6b 20 66 69 o see if lock fi
db280 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 le already exist
db290 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 74 s */. struct st
db2a0 61 74 20 73 74 61 74 42 75 66 3b 0a 20 20 69 66 at statBuf;. if
db2b0 20 28 6c 73 74 61 74 28 7a 4c 6f 63 6b 46 69 6c (lstat(zLockFil
db2c0 65 2c 26 73 74 61 74 42 75 66 29 20 3d 3d 20 30 e,&statBuf) == 0
db2d0 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
db2e0 54 45 5f 42 55 53 59 3b 20 2f 2a 20 69 74 20 64 TE_BUSY; /* it d
db2f0 6f 65 73 2c 20 62 75 73 79 20 2a 2f 0a 20 20 20 oes, busy */.
db300 20 67 6f 74 6f 20 64 6f 74 6c 6f 63 6b 5f 65 6e goto dotlock_en
db310 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 d_lock;. }. .
db320 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c /* grab an excl
db330 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 usive lock */.
db340 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b 46 fd = open(zLockF
db350 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 43 ile,O_RDONLY|O_C
db360 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30 30 REAT|O_EXCL,0600
db370 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b );. if( fd<0 ){
db380 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 74 . /* failed t
db390 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 74 68 o open/create th
db3a0 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65 20 e file, someone
db3b0 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20 73 74 else may have st
db3c0 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a 2f olen the lock */
db3d0 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 . int tErrno
db3e0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 = errno;. if(
db3f0 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 72 6e EEXIST == tErrn
db400 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 o ){. rc =
db410 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
db420 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
db430 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
db440 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
db450 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
db460 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 ERR_LOCK);.
db470 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
db480 4f 52 28 72 63 29 20 29 7b 0a 09 70 46 69 6c 65 OR(rc) ){..pFile
db490 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
db4a0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rrno;. }.
db4b0 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 64 6f 74 }. goto dot
db4c0 6c 6f 63 6b 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 lock_end_lock;.
db4d0 20 7d 20 0a 20 20 63 6c 6f 73 65 28 66 64 29 3b } . close(fd);
db4e0 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c . . /* got it,
db4f0 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e set the type an
db500 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 d return ok */.
db510 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
db520 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 64 = locktype;.. d
db530 6f 74 6c 6f 63 6b 5f 65 6e 64 5f 6c 6f 63 6b 3a otlock_end_lock:
db540 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
db550 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c .static int dotl
db560 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 ockUnlock(sqlite
db570 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
db580 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e locktype) {. un
db590 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
db5a0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
db5b0 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 char *zLockFile
db5c0 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 = (char *)pFile
db5d0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
db5e0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 ;.. assert( pFi
db5f0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 le );. OSTRACE5
db600 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 ("UNLOCK %d %d
db610 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 was %d pid=%d\n"
db620 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
db630 74 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d type,.. pFile-
db640 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 >locktype, getpi
db650 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 d());. assert(
db660 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 locktype<=SHARED
db670 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a _LOCK );. . /*
db680 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 no-op if possib
db690 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c le */. if( pFil
db6a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 e->locktype==loc
db6b0 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 ktype ){. ret
db6c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
db6d0 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 }. . /* share
db6e0 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65 d can just be se
db6f0 74 20 62 65 63 61 75 73 65 20 77 65 20 61 6c 77 t because we alw
db700 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c ays have an excl
db710 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c usive */. if (l
db720 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
db730 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c LOCK) {. pFil
db740 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f e->locktype = lo
db750 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 cktype;. retu
db760 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
db770 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 }. . /* no, re
db780 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f ally, unlock. */
db790 0a 20 20 69 66 20 28 75 6e 6c 69 6e 6b 28 7a 4c . if (unlink(zL
db7a0 6f 63 6b 46 69 6c 65 29 20 29 20 7b 0a 20 20 20 ockFile) ) {.
db7b0 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 int rc, tErrno
db7c0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 = errno;. if(
db7d0 20 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e ENOENT != tErrn
db7e0 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 o ){. rc =
db7f0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
db800 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
db810 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 , SQLITE_IOERR_U
db820 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 NLOCK);. }.
db830 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
db840 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
db850 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
db860 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
db870 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
db880 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c . }. pFile->l
db890 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 ocktype = NO_LOC
db8a0 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 K;. return SQLI
db8b0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a TE_OK;.}../*. **
db8c0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 20 Close a file..
db8d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f */.static int do
db8e0 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 tlockClose(sqlit
db8f0 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 e3_file *id) {.
db900 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 69 int rc;. if( i
db910 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c d ){. unixFil
db920 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
db930 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f File*)id;. do
db940 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 tlockUnlock(id,
db950 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 NO_LOCK);. sq
db960 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 lite3_free(pFile
db970 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
db980 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 53 5f );. }. if( IS_
db990 56 58 57 4f 52 4b 53 20 29 20 65 6e 74 65 72 4d VXWORKS ) enterM
db9a0 75 74 65 78 28 29 3b 0a 20 20 72 63 20 3d 20 63 utex();. rc = c
db9b0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 loseUnixFile(id)
db9c0 3b 0a 20 20 69 66 28 20 49 53 5f 56 58 57 4f 52 ;. if( IS_VXWOR
db9d0 4b 53 20 29 20 6c 65 61 76 65 4d 75 74 65 78 28 KS ) leaveMutex(
db9e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
db9f0 7d 0a 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b }..#if IS_VXWORK
dba00 53 0a 0a 23 70 72 61 67 6d 61 20 6d 61 72 6b 20 S..#pragma mark
dba10 50 4f 53 49 58 2f 76 78 57 6f 72 6b 73 20 6e 61 POSIX/vxWorks na
dba20 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 62 61 med semaphore ba
dba30 73 65 64 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 20 sed locking../*
dba40 4e 61 6d 65 64 73 65 6d 2d 73 74 79 6c 65 20 72 Namedsem-style r
dba50 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 63 68 65 eserved lock che
dba60 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 20 cking following
dba70 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 the behavior of
dba80 0a 2a 2a 20 75 6e 69 78 43 68 65 63 6b 52 65 73 .** unixCheckRes
dba90 65 72 76 65 64 4c 6f 63 6b 2c 20 73 65 65 20 74 ervedLock, see t
dbaa0 68 65 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 he unixCheckRese
dbab0 72 76 65 64 4c 6f 63 6b 20 66 75 6e 63 74 69 6f rvedLock functio
dbac0 6e 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 73 74 n comments */.st
dbad0 61 74 69 63 20 69 6e 74 20 6e 61 6d 65 64 73 65 atic int namedse
dbae0 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f mCheckReservedLo
dbaf0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
dbb00 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
dbb10 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 t) {. int rc =
dbb20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
dbb30 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 reserved = 0;.
dbb40 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
dbb50 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
dbb60 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 ;.. SimulateIOE
dbb70 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
dbb80 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
dbb90 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a ESERVEDLOCK; );.
dbba0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
dbbb0 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 le );.. /* Chec
dbbc0 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e k if a thread in
dbbd0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f this process ho
dbbe0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 lds such a lock
dbbf0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
dbc00 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
dbc10 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 LOCK ){. rese
dbc20 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 rved = 1;. }.
dbc30 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 . /* Otherwise
dbc40 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 see if some othe
dbc50 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 r process holds
dbc60 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 it. */. if( !re
dbc70 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 65 served ){. se
dbc80 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c m_t *pSem = pFil
dbc90 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a e->pOpen->pSem;.
dbca0 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 struct stat
dbcb0 73 74 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66 statBuf;.. if
dbcc0 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 ( sem_trywait(pS
dbcd0 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 em)==-1 ){.
dbce0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 int tErrno = er
dbcf0 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 rno;. if( E
dbd00 41 47 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 AGAIN != tErrno
dbd10 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
dbd20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
dbd30 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
dbd40 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 , SQLITE_IOERR_C
dbd50 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b HECKRESERVEDLOCK
dbd60 29 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 );. pFile
dbd70 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
dbd80 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c rrno;. } el
dbd90 73 65 20 7b 0a 09 2f 2a 20 73 6f 6d 65 6f 6e 65 se {../* someone
dbda0 20 65 6c 73 65 20 68 61 73 20 74 68 65 20 6c 6f else has the lo
dbdb0 63 6b 20 77 68 65 6e 20 77 65 20 61 72 65 20 69 ck when we are i
dbdc0 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a 09 72 65 n NO_LOCK */..re
dbdd0 73 65 72 76 65 64 20 3d 20 28 70 46 69 6c 65 2d served = (pFile-
dbde0 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 53 48 41 52 >locktype < SHAR
dbdf0 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 ED_LOCK);.
dbe00 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
dbe10 20 20 20 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68 /* we could h
dbe20 61 76 65 20 69 74 20 69 66 20 77 65 20 77 61 6e ave it if we wan
dbe30 74 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 73 65 t it */. se
dbe40 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20 m_post(pSem);.
dbe50 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 }. }. OSTRAC
dbe60 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b E4("TEST WR-LOCK
dbe70 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 %d %d %d\n", pF
dbe80 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 ile->h, rc, rese
dbe90 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f rved);.. *pResO
dbea0 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 ut = reserved;.
dbeb0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 return rc;.}..s
dbec0 74 61 74 69 63 20 69 6e 74 20 6e 61 6d 65 64 73 tatic int nameds
dbed0 65 6d 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 emLock(sqlite3_f
dbee0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
dbef0 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 ktype) {. unixF
dbf00 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
dbf10 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e ixFile*)id;. in
dbf20 74 20 66 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 t fd;. sem_t *p
dbf30 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 Sem = pFile->pOp
dbf40 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 69 6e 74 20 en->pSem;. int
dbf50 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
dbf60 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 . /* if we alre
dbf70 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c ady have a lock,
dbf80 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 it is exclusive
dbf90 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 . . ** Just ad
dbfa0 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 just level and p
dbfb0 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 unt on outta her
dbfc0 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c e. */. if (pFil
dbfd0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f e->locktype > NO
dbfe0 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 _LOCK) {. pFi
dbff0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
dc000 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 63 20 ocktype;. rc
dc010 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
dc020 20 67 6f 74 6f 20 6e 61 6d 65 64 73 65 6d 5f 65 goto namedsem_e
dc030 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a nd_lock;. }. .
dc040 20 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61 70 68 /* lock semaph
dc050 6f 72 65 20 6e 6f 77 20 62 75 74 20 62 61 69 6c ore now but bail
dc060 20 6f 75 74 20 77 68 65 6e 20 61 6c 72 65 61 64 out when alread
dc070 79 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 69 y locked. */. i
dc080 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 f( sem_trywait(p
dc090 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 Sem)==-1 ){.
dc0a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
dc0b0 3b 0a 20 20 20 20 67 6f 74 6f 20 6e 61 6d 65 64 ;. goto named
dc0c0 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 sem_end_lock;.
dc0d0 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 }.. /* got it,
dc0e0 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e 64 set the type and
dc0f0 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 return ok */.
dc100 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
dc110 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 6e 61 = locktype;.. na
dc120 6d 65 64 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a medsem_end_lock:
dc130 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
dc140 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 61 6d 65 .static int name
dc150 64 73 65 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 dsemUnlock(sqlit
dc160 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
dc170 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 locktype) {. u
dc180 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
dc190 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
dc1a0 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 sem_t *pSem =
dc1b0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 pFile->pOpen->pS
dc1c0 65 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 em;.. assert( p
dc1d0 46 69 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 File );. assert
dc1e0 28 20 70 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52 ( pSem );. OSTR
dc1f0 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 ACE5("UNLOCK %d
dc200 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 %d was %d pid=%
dc210 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
dc220 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 locktype,.. pF
dc230 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 ile->locktype, g
dc240 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 etpid());. asse
dc250 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 rt( locktype<=SH
dc260 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a ARED_LOCK );. .
dc270 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f /* no-op if po
dc280 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 ssible */. if(
dc290 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
dc2a0 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
dc2b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
dc2c0 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 K;. }. . /* s
dc2d0 68 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 hared can just b
dc2e0 65 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 e set because we
dc2f0 20 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 always have an
dc300 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 exclusive */. i
dc310 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f (locktype==SHA
dc320 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 RED_LOCK) {.
dc330 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
dc340 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 = locktype;.
dc350 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
dc360 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f ;. }. . /* no
dc370 2c 20 72 65 61 6c 6c 79 20 75 6e 6c 6f 63 6b 2e , really unlock.
dc380 20 2a 2f 0a 20 20 69 66 20 28 20 73 65 6d 5f 70 */. if ( sem_p
dc390 6f 73 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 20 ost(pSem)==-1 )
dc3a0 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 74 45 {. int rc, tE
dc3b0 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
dc3c0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
dc3d0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
dc3e0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
dc3f0 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 IOERR_UNLOCK);.
dc400 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
dc410 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
dc420 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
dc430 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
dc440 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 }. return rc
dc450 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e ; . }. pFile->
dc460 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f locktype = NO_LO
dc470 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c CK;. return SQL
dc480 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a ITE_OK;.}../*. *
dc490 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a * Close a file..
dc4a0 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e */.static int n
dc4b0 61 6d 65 64 73 65 6d 43 6c 6f 73 65 28 73 71 6c amedsemClose(sql
dc4c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
dc4d0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 . if( id ){.
dc4e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
dc4f0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
dc500 3b 0a 20 20 20 20 6e 61 6d 65 64 73 65 6d 55 6e ;. namedsemUn
dc510 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b lock(id, NO_LOCK
dc520 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
dc530 46 69 6c 65 20 29 3b 0a 20 20 20 20 65 6e 74 65 File );. ente
dc540 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 rMutex();. re
dc550 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 leaseLockInfo(pF
dc560 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 ile->pLock);.
dc570 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 releaseOpenCnt(
dc580 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 pFile->pOpen);.
dc590 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 closeUnixFile
dc5a0 28 69 64 29 3b 0a 20 20 20 20 6c 65 61 76 65 4d (id);. leaveM
dc5b0 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 utex();. }. re
dc5c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
dc5d0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 49 53 5f }..#endif /* IS_
dc5e0 56 58 57 4f 52 4b 53 20 2a 2f 0a 0a 23 65 6e 64 VXWORKS */..#end
dc5f0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 if /* SQLITE_ENA
dc600 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
dc610 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 E */../*.** The
dc620 6e 6f 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e nolockLockingCon
dc630 74 65 78 74 20 69 73 20 76 6f 69 64 0a 2a 2f 0a text is void.*/.
dc640 74 79 70 65 64 65 66 20 76 6f 69 64 20 6e 6f 6c typedef void nol
dc650 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 ockLockingContex
dc660 74 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6e t;..static int n
dc670 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 olockCheckReserv
dc680 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
dc690 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e ile *NotUsed, in
dc6a0 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 55 t *pResOut){. U
dc6b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
dc6c0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a 70 52 65 NotUsed);. *pRe
dc6d0 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 sOut = 0;. retu
dc6e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
dc6f0 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f .static int nolo
dc700 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 ckLock(sqlite3_f
dc710 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e ile *NotUsed, in
dc720 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 t NotUsed2){. U
dc730 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
dc740 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
dc750 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 d2);. return SQ
dc760 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 LITE_OK;.}..stat
dc770 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c ic int nolockUnl
dc780 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
dc790 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e *NotUsed, int N
dc7a0 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 otUsed2){. UNUS
dc7b0 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f ED_PARAMETER2(No
dc7c0 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 tUsed, NotUsed2)
dc7d0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
dc7e0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
dc7f0 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a lose a file..*/.
dc800 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 static int noloc
dc810 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 kClose(sqlite3_f
dc820 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 6e 74 ile *id) {. int
dc830 20 72 63 3b 0a 20 20 69 66 28 20 49 53 5f 56 58 rc;. if( IS_VX
dc840 57 4f 52 4b 53 20 29 20 65 6e 74 65 72 4d 75 74 WORKS ) enterMut
dc850 65 78 28 29 3b 0a 20 20 72 63 20 3d 20 63 6c 6f ex();. rc = clo
dc860 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a seUnixFile(id);.
dc870 20 20 69 66 28 20 49 53 5f 56 58 57 4f 52 4b 53 if( IS_VXWORKS
dc880 20 29 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b ) leaveMutex();
dc890 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
dc8a0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 ../*.** Informat
dc8b0 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 ion and control
dc8c0 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 of an open file
dc8d0 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 handle..*/.stati
dc8e0 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 43 6f c int unixFileCo
dc8f0 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 ntrol(sqlite3_fi
dc900 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 le *id, int op,
dc910 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 void *pArg){. s
dc920 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 witch( op ){.
dc930 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e case SQLITE_FCN
dc940 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a TL_LOCKSTATE: {.
dc950 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 *(int*)pAr
dc960 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 g = ((unixFile*)
dc970 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 id)->locktype;.
dc980 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
dc990 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d TE_OK;. }. }
dc9a0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
dc9b0 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _ERROR;.}../*.**
dc9c0 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 Return the sect
dc9d0 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 or size in bytes
dc9e0 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 of the underlyi
dc9f0 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 ng block device
dca00 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 for.** the speci
dca10 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 fied file. This
dca20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 is almost always
dca30 20 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 512 bytes, but
dca40 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 may be.** larger
dca50 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 for some device
dca60 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 s..**.** SQLite
dca70 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 code assumes thi
dca80 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f s function canno
dca90 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 t fail. It also
dcaa0 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 assumes that.**
dcab0 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 if two files are
dcac0 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 created in the
dcad0 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d same file-system
dcae0 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e directory (i.e.
dcaf0 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 .** a database a
dcb00 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 nd its journal f
dcb10 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65 ile) that the se
dcb20 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 ctor size will b
dcb30 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f e the.** same fo
dcb40 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 r both..*/.stati
dcb50 63 20 69 6e 74 20 75 6e 69 78 53 65 63 74 6f 72 c int unixSector
dcb60 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c Size(sqlite3_fil
dcb70 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 e *NotUsed){. U
dcb80 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
dcb90 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 NotUsed);. retu
dcba0 72 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c rn SQLITE_DEFAUL
dcbb0 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d T_SECTOR_SIZE;.}
dcbc0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
dcbd0 68 65 20 64 65 76 69 63 65 20 63 68 61 72 61 63 he device charac
dcbe0 74 65 72 69 73 74 69 63 73 20 66 6f 72 20 74 68 teristics for th
dcbf0 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 e file. This is
dcc00 61 6c 77 61 79 73 20 30 20 66 6f 72 20 75 6e 69 always 0 for uni
dcc10 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 x..*/.static int
dcc20 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 unixDeviceChara
dcc30 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 cteristics(sqlit
dcc40 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 e3_file *NotUsed
dcc50 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
dcc60 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
dcc70 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
dcc80 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
dcc90 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
dcca0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 the unixFile str
dccb0 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 ucture pointed t
dccc0 6f 20 62 79 20 70 49 64 2e 0a 2a 2a 0a 2a 2a 20 o by pId..**.**
dccd0 57 68 65 6e 20 6c 6f 63 6b 69 6e 67 20 65 78 74 When locking ext
dcce0 65 6e 73 69 6f 6e 73 20 61 72 65 20 65 6e 61 62 ensions are enab
dccf0 6c 65 64 2c 20 74 68 65 20 66 69 6c 65 70 61 74 led, the filepat
dcd00 68 20 61 6e 64 20 6c 6f 63 6b 69 6e 67 20 73 74 h and locking st
dcd10 79 6c 65 20 0a 2a 2a 20 61 72 65 20 6e 65 65 64 yle .** are need
dcd20 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
dcd30 74 68 65 20 75 6e 69 78 46 69 6c 65 20 70 4d 65 the unixFile pMe
dcd40 74 68 6f 64 20 74 6f 20 75 73 65 20 66 6f 72 20 thod to use for
dcd50 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f locking operatio
dcd60 6e 73 2e 0a 2a 2a 20 54 68 65 20 6c 6f 63 6b 69 ns..** The locki
dcd70 6e 67 2d 73 74 79 6c 65 20 73 70 65 63 69 66 69 ng-style specifi
dcd80 63 20 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 c lockingContext
dcd90 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 data structure
dcda0 69 73 20 63 72 65 61 74 65 64 20 0a 2a 2a 20 61 is created .** a
dcdb0 6e 64 20 61 73 73 69 67 6e 65 64 20 68 65 72 65 nd assigned here
dcdc0 20 61 6c 73 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 also..*/.static
dcdd0 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e 69 78 46 int fillInUnixF
dcde0 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ile(. sqlite3_v
dcdf0 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 2f fs *pVfs, /
dce00 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 * Pointer to vfs
dce10 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 object */. int
dce20 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 h,
dce30 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c /* Open fil
dce40 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 66 20 e descriptor of
dce50 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 file being opene
dce60 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 d */. int dirfd
dce70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
dce80 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65 * Directory file
dce90 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 descriptor */.
dcea0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
dceb0 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 Id, /* Writ
dcec0 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69 6c e to the unixFil
dced0 65 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 e structure here
dcee0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
dcef0 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a *zFilename, /*
dcf00 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
dcf10 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a e being opened *
dcf20 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 2c 20 /. int noLock,
dcf30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
dcf40 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69 66 20 74 mit locking if t
dcf50 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 rue */. int isD
dcf60 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 elete
dcf70 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c /* Delete on cl
dcf80 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 ose if true */.)
dcf90 7b 0a 20 20 69 6e 74 20 65 4c 6f 63 6b 69 6e 67 {. int eLocking
dcfa0 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c Style;. unixFil
dcfb0 65 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 e *pNew = (unixF
dcfc0 69 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 ile *)pId;. int
dcfd0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
dcfe0 0a 0a 20 20 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 .. /* Macro to
dcff0 64 65 66 69 6e 65 20 74 68 65 20 73 74 61 74 69 define the stati
dd000 63 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6e c contents of an
dd010 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
dd020 6f 64 73 20 0a 20 20 2a 2a 20 73 74 72 75 63 74 ods . ** struct
dd030 75 72 65 20 66 6f 72 20 61 20 75 6e 69 78 20 62 ure for a unix b
dd040 61 63 6b 65 6e 64 20 66 69 6c 65 2e 20 44 69 66 ackend file. Dif
dd050 66 65 72 65 6e 74 20 6c 6f 63 6b 69 6e 67 20 6d ferent locking m
dd060 65 74 68 6f 64 73 0a 20 20 2a 2a 20 72 65 71 75 ethods. ** requ
dd070 69 72 65 20 64 69 66 66 65 72 65 6e 74 20 66 75 ire different fu
dd080 6e 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 nctions for the
dd090 78 43 6c 6f 73 65 2c 20 78 4c 6f 63 6b 2c 20 78 xClose, xLock, x
dd0a0 55 6e 6c 6f 63 6b 20 61 6e 64 0a 20 20 2a 2a 20 Unlock and. **
dd0b0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
dd0c0 63 6b 20 6d 65 74 68 6f 64 73 2e 0a 20 20 2a 2f ck methods.. */
dd0d0 0a 20 20 23 64 65 66 69 6e 65 20 49 4f 4d 45 54 . #define IOMET
dd0e0 48 4f 44 53 28 78 43 6c 6f 73 65 2c 20 78 4c 6f HODS(xClose, xLo
dd0f0 63 6b 2c 20 78 55 6e 6c 6f 63 6b 2c 20 78 43 68 ck, xUnlock, xCh
dd100 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 eckReservedLock)
dd110 20 7b 20 20 20 20 5c 0a 20 20 20 20 31 2c 20 20 { \. 1,
dd120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd130 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 /* iVers
dd140 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ion */
dd150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd160 20 5c 0a 20 20 20 20 78 43 6c 6f 73 65 2c 20 20 \. xClose,
dd170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd180 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 20 /* xClose */
dd190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd1a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
dd1b0 20 20 75 6e 69 78 52 65 61 64 2c 20 20 20 20 20 unixRead,
dd1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
dd1d0 20 78 52 65 61 64 20 2a 2f 20 20 20 20 20 20 20 xRead */
dd1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd1f0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
dd200 78 57 72 69 74 65 2c 20 20 20 20 20 20 20 20 20 xWrite,
dd210 20 20 20 20 20 20 20 20 20 2f 2a 20 78 57 72 69 /* xWri
dd220 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 te */
dd230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd240 20 20 5c 0a 20 20 20 20 75 6e 69 78 54 72 75 6e \. unixTrun
dd250 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 cate,
dd260 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 /* xTruncate
dd270 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
dd280 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
dd290 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 20 20 20 unixSync,
dd2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
dd2b0 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 20 20 20 * xSync */
dd2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd2d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
dd2e0 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20 20 20 ixFileSize,
dd2f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 /* xFi
dd300 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 leSize */
dd310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd320 20 20 20 5c 0a 20 20 20 20 78 4c 6f 63 6b 2c 20 \. xLock,
dd330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd340 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f /* xLock */
dd350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
dd370 20 20 20 20 78 55 6e 6c 6f 63 6b 2c 20 20 20 20 xUnlock,
dd380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd390 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 /* xUnlock */
dd3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dd3b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 78 \. x
dd3c0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
dd3d0 6b 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 k, /* xC
dd3e0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
dd3f0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
dd400 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 46 69 \. unixFi
dd410 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20 20 leControl,
dd420 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f /* xFileCo
dd430 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 20 20 20 20 ntrol */
dd440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
dd450 0a 20 20 20 20 75 6e 69 78 53 65 63 74 6f 72 53 . unixSectorS
dd460 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ize,
dd470 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 /* xSectorSize
dd480 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
dd490 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
dd4a0 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 unixDeviceCharac
dd4b0 74 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 teristics /* x
dd4c0 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 DeviceCapabiliti
dd4d0 65 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 es */
dd4e0 20 20 20 20 20 5c 0a 20 20 7d 0a 20 20 73 74 61 \. }. sta
dd4f0 74 69 63 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d tic sqlite3_io_m
dd500 65 74 68 6f 64 73 20 61 49 6f 4d 65 74 68 6f 64 ethods aIoMethod
dd510 5b 5d 20 3d 20 7b 0a 20 20 20 20 49 4f 4d 45 54 [] = {. IOMET
dd520 48 4f 44 53 28 75 6e 69 78 43 6c 6f 73 65 2c 20 HODS(unixClose,
dd530 75 6e 69 78 4c 6f 63 6b 2c 20 75 6e 69 78 55 6e unixLock, unixUn
dd540 6c 6f 63 6b 2c 20 75 6e 69 78 43 68 65 63 6b 52 lock, unixCheckR
dd550 65 73 65 72 76 65 64 4c 6f 63 6b 29 20 0a 20 20 eservedLock) .
dd560 20 2c 49 4f 4d 45 54 48 4f 44 53 28 6e 6f 6c 6f ,IOMETHODS(nolo
dd570 63 6b 43 6c 6f 73 65 2c 20 6e 6f 6c 6f 63 6b 4c ckClose, nolockL
dd580 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 ock, nolockUnloc
dd590 6b 2c 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 k, nolockCheckRe
dd5a0 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 69 66 20 servedLock).#if
dd5b0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
dd5c0 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 2c CKING_STYLE. ,
dd5d0 49 4f 4d 45 54 48 4f 44 53 28 64 6f 74 6c 6f 63 IOMETHODS(dotloc
dd5e0 6b 43 6c 6f 73 65 2c 20 64 6f 74 6c 6f 63 6b 4c kClose, dotlockL
dd5f0 6f 63 6b 2c 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f ock, dotlockUnlo
dd600 63 6b 2c 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 ck,dotlockCheckR
dd610 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 69 66 eservedLock).#if
dd620 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 2c IS_VXWORKS. ,
dd630 49 4f 4d 45 54 48 4f 44 53 28 6e 6f 6c 6f 63 6b IOMETHODS(nolock
dd640 43 6c 6f 73 65 2c 20 6e 6f 6c 6f 63 6b 4c 6f 63 Close, nolockLoc
dd650 6b 2c 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c k, nolockUnlock,
dd660 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 nolockCheckRese
dd670 72 76 65 64 4c 6f 63 6b 29 0a 20 20 20 2c 49 4f rvedLock). ,IO
dd680 4d 45 54 48 4f 44 53 28 6e 6f 6c 6f 63 6b 43 6c METHODS(nolockCl
dd690 6f 73 65 2c 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c ose, nolockLock,
dd6a0 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 6e nolockUnlock, n
dd6b0 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 olockCheckReserv
dd6c0 65 64 4c 6f 63 6b 29 0a 20 20 20 2c 49 4f 4d 45 edLock). ,IOME
dd6d0 54 48 4f 44 53 28 6e 61 6d 65 64 73 65 6d 43 6c THODS(namedsemCl
dd6e0 6f 73 65 2c 20 6e 61 6d 65 64 73 65 6d 4c 6f 63 ose, namedsemLoc
dd6f0 6b 2c 20 6e 61 6d 65 64 73 65 6d 55 6e 6c 6f 63 k, namedsemUnloc
dd700 6b 2c 20 6e 61 6d 65 64 73 65 6d 43 68 65 63 6b k, namedsemCheck
dd710 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 65 ReservedLock).#e
dd720 6c 73 65 0a 20 20 20 2c 49 4f 4d 45 54 48 4f 44 lse. ,IOMETHOD
dd730 53 28 66 6c 6f 63 6b 43 6c 6f 73 65 2c 20 66 6c S(flockClose, fl
dd740 6f 63 6b 4c 6f 63 6b 2c 20 66 6c 6f 63 6b 55 6e ockLock, flockUn
dd750 6c 6f 63 6b 2c 20 66 6c 6f 63 6b 43 68 65 63 6b lock, flockCheck
dd760 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 20 20 ReservedLock).
dd770 20 2c 49 4f 4d 45 54 48 4f 44 53 28 61 66 70 43 ,IOMETHODS(afpC
dd780 6c 6f 73 65 2c 20 61 66 70 4c 6f 63 6b 2c 20 61 lose, afpLock, a
dd790 66 70 55 6e 6c 6f 63 6b 2c 20 61 66 70 43 68 65 fpUnlock, afpChe
dd7a0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a ckReservedLock).
dd7b0 20 20 20 2c 49 4f 4d 45 54 48 4f 44 53 28 6e 6f ,IOMETHODS(no
dd7c0 6c 6f 63 6b 43 6c 6f 73 65 2c 20 6e 6f 6c 6f 63 lockClose, noloc
dd7d0 6b 4c 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 55 6e 6c kLock, nolockUnl
dd7e0 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b ock, nolockCheck
dd7f0 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 65 ReservedLock).#e
dd800 6e 64 69 66 0a 23 65 6e 64 69 66 0a 20 20 7d 3b ndif.#endif. };
dd810 0a 20 20 2f 2a 20 54 68 65 20 6f 72 64 65 72 20 . /* The order
dd820 6f 66 20 74 68 65 20 49 4f 4d 45 54 48 4f 44 53 of the IOMETHODS
dd830 20 6d 61 63 72 6f 73 20 61 62 6f 76 65 20 69 73 macros above is
dd840 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 49 74 20 important. It
dd850 6d 75 73 74 20 62 65 20 74 68 65 0a 20 20 2a 2a must be the. **
dd860 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20 74 same order as t
dd870 68 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 he LOCKING_STYLE
dd880 20 6e 75 6d 62 65 72 73 0a 20 20 2a 2f 0a 20 20 numbers. */.
dd890 61 73 73 65 72 74 28 4c 4f 43 4b 49 4e 47 5f 53 assert(LOCKING_S
dd8a0 54 59 4c 45 5f 50 4f 53 49 58 3d 3d 31 29 3b 0a TYLE_POSIX==1);.
dd8b0 20 20 61 73 73 65 72 74 28 4c 4f 43 4b 49 4e 47 assert(LOCKING
dd8c0 5f 53 54 59 4c 45 5f 4e 4f 4e 45 3d 3d 32 29 3b _STYLE_NONE==2);
dd8d0 0a 20 20 61 73 73 65 72 74 28 4c 4f 43 4b 49 4e . assert(LOCKIN
dd8e0 47 5f 53 54 59 4c 45 5f 44 4f 54 46 49 4c 45 3d G_STYLE_DOTFILE=
dd8f0 3d 33 29 3b 0a 20 20 61 73 73 65 72 74 28 4c 4f =3);. assert(LO
dd900 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 46 4c 4f 43 CKING_STYLE_FLOC
dd910 4b 3d 3d 34 29 3b 0a 20 20 61 73 73 65 72 74 28 K==4);. assert(
dd920 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 41 46 LOCKING_STYLE_AF
dd930 50 3d 3d 35 29 3b 0a 20 20 61 73 73 65 72 74 28 P==5);. assert(
dd940 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 41 LOCKING_STYLE_NA
dd950 4d 45 44 53 45 4d 3d 3d 36 29 3b 0a 0a 20 20 61 MEDSEM==6);.. a
dd960 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 4c 6f ssert( pNew->pLo
dd970 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 ck==NULL );. as
dd980 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 4f 70 65 sert( pNew->pOpe
dd990 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a n==NULL );.. /*
dd9a0 20 50 61 72 61 6d 65 74 65 72 20 69 73 44 65 6c Parameter isDel
dd9b0 65 74 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 ete is only used
dd9c0 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 50 61 72 on vxworks. Par
dd9d0 61 6d 65 74 65 72 20 70 56 66 73 20 69 73 20 6f ameter pVfs is o
dd9e0 6e 6c 79 0a 20 20 2a 2a 20 75 73 65 64 20 69 66 nly. ** used if
dd9f0 20 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f ENABLE_LOCKING_
dda00 53 54 59 4c 45 20 69 73 20 64 65 66 69 6e 65 64 STYLE is defined
dda10 2e 20 45 78 70 72 65 73 73 20 74 68 69 73 20 65 . Express this e
dda20 78 70 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20 xplicitly . **
dda30 68 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20 here to prevent
dda40 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 compiler warning
dda50 73 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70 s about unused p
dda60 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a arameters.. */.
dda70 20 20 69 66 28 20 21 49 53 5f 56 58 57 4f 52 4b if( !IS_VXWORK
dda80 53 20 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d S ) UNUSED_PARAM
dda90 45 54 45 52 28 69 73 44 65 6c 65 74 65 29 3b 0a ETER(isDelete);.
ddaa0 20 20 69 66 28 20 21 53 51 4c 49 54 45 5f 45 4e if( !SQLITE_EN
ddab0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
ddac0 4c 45 20 29 20 55 4e 55 53 45 44 5f 50 41 52 41 LE ) UNUSED_PARA
ddad0 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69 METER(pVfs);. i
ddae0 66 28 20 21 49 53 5f 56 58 57 4f 52 4b 53 20 26 f( !IS_VXWORKS &
ddaf0 26 20 21 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 & !SQLITE_ENABLE
ddb00 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 29 _LOCKING_STYLE )
ddb10 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
ddb20 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 0a 20 R(zFilename);..
ddb30 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 OSTRACE3("OPEN
ddb40 20 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 %-3d %s\n", h
ddb50 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 , zFilename);
ddb60 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b . pNew->h = h;
ddb70 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d . pNew->dirfd =
ddb80 20 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 dirfd;. SET_TH
ddb90 52 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 0a 23 READID(pNew);..#
ddba0 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if IS_VXWORKS.
ddbb0 7b 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a {. HashElem *
ddbc0 70 45 6c 65 6d 3b 0a 20 20 20 20 63 68 61 72 20 pElem;. char
ddbd0 2a 7a 52 65 61 6c 6e 61 6d 65 20 3d 20 76 78 72 *zRealname = vxr
ddbe0 65 61 6c 70 61 74 68 28 7a 46 69 6c 65 6e 61 6d ealpath(zFilenam
ddbf0 65 2c 20 31 29 3b 0a 20 20 20 20 69 6e 74 20 6e e, 1);. int n
ddc00 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 7a 52 65 61 ;. pNew->zRea
ddc10 6c 70 61 74 68 20 3d 20 30 3b 0a 20 20 20 20 69 lpath = 0;. i
ddc20 66 28 20 21 7a 52 65 61 6c 6e 61 6d 65 20 29 7b f( !zRealname ){
ddc30 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
ddc40 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
ddc50 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 eLockingStyle =
ddc60 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f LOCKING_STYLE_NO
ddc70 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 NE;. }else{.
ddc80 20 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 n = strlen(
ddc90 7a 52 65 61 6c 6e 61 6d 65 29 20 2b 20 31 3b 0a zRealname) + 1;.
ddca0 20 20 20 20 20 20 65 6e 74 65 72 4d 75 74 65 78 enterMutex
ddcb0 28 29 3b 0a 20 20 20 20 20 20 70 45 6c 65 6d 20 ();. pElem
ddcc0 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e = sqlite3HashFin
ddcd0 64 45 6c 65 6d 28 26 6e 61 6d 65 48 61 73 68 2c dElem(&nameHash,
ddce0 20 7a 52 65 61 6c 6e 61 6d 65 2c 20 6e 29 3b 0a zRealname, n);.
ddcf0 20 20 20 20 20 20 69 66 28 20 70 45 6c 65 6d 20 if( pElem
ddd00 29 7b 0a 20 20 20 20 20 20 20 20 6c 6f 6e 67 20 ){. long
ddd10 63 6e 74 20 3d 20 28 6c 6f 6e 67 29 70 45 6c 65 cnt = (long)pEle
ddd20 6d 2d 3e 64 61 74 61 3b 0a 20 20 20 20 20 20 20 m->data;.
ddd30 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 cnt++;.
ddd40 70 4e 65 77 2d 3e 7a 52 65 61 6c 70 61 74 68 20 pNew->zRealpath
ddd50 3d 20 70 45 6c 65 6d 2d 3e 70 4b 65 79 3b 0a 20 = pElem->pKey;.
ddd60 20 20 20 20 20 20 20 70 45 6c 65 6d 2d 3e 64 61 pElem->da
ddd70 74 61 20 3d 20 28 76 6f 69 64 2a 29 63 6e 74 3b ta = (void*)cnt;
ddd80 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
ddd90 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
ddda0 33 48 61 73 68 49 6e 73 65 72 74 28 26 6e 61 6d 3HashInsert(&nam
dddb0 65 48 61 73 68 2c 20 7a 52 65 61 6c 6e 61 6d 65 eHash, zRealname
dddc0 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 31 29 3d 3d , n, (void*)1)==
dddd0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 0 ){. p
ddde0 45 6c 65 6d 20 3d 20 73 71 6c 69 74 65 33 48 61 Elem = sqlite3Ha
dddf0 73 68 46 69 6e 64 45 6c 65 6d 28 26 6e 61 6d 65 shFindElem(&name
dde00 48 61 73 68 2c 20 7a 52 65 61 6c 6e 61 6d 65 2c Hash, zRealname,
dde10 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 n);. i
dde20 66 28 20 70 45 6c 65 6d 20 29 7b 0a 20 20 20 20 f( pElem ){.
dde30 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 7a 52 pNew->zR
dde40 65 61 6c 70 61 74 68 20 3d 20 70 45 6c 65 6d 2d ealpath = pElem-
dde50 3e 70 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 >pKey;.
dde60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
dde70 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 sqlite3HashI
dde80 6e 73 65 72 74 28 26 6e 61 6d 65 48 61 73 68 2c nsert(&nameHash,
dde90 20 7a 52 65 61 6c 6e 61 6d 65 2c 20 6e 2c 20 30 zRealname, n, 0
ddea0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 );. r
ddeb0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
ddec0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 4c ;. eL
dded0 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 4c 4f ockingStyle = LO
ddee0 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e 45 CKING_STYLE_NONE
ddef0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
ddf00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
ddf10 20 20 20 20 20 20 6c 65 61 76 65 4d 75 74 65 78 leaveMutex
ddf20 28 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ();. sqlite
ddf30 33 5f 66 72 65 65 28 7a 52 65 61 6c 6e 61 6d 65 3_free(zRealname
ddf40 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e );. }. }.#en
ddf50 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f 4c 6f 63 dif.. if( noLoc
ddf60 6b 20 29 7b 0a 20 20 20 20 65 4c 6f 63 6b 69 6e k ){. eLockin
ddf70 67 53 74 79 6c 65 20 3d 20 4c 4f 43 4b 49 4e 47 gStyle = LOCKING
ddf80 5f 53 54 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 7d _STYLE_NONE;. }
ddf90 65 6c 73 65 7b 0a 20 20 20 20 65 4c 6f 63 6b 69 else{. eLocki
ddfa0 6e 67 53 74 79 6c 65 20 3d 20 64 65 74 65 63 74 ngStyle = detect
ddfb0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 28 70 56 66 LockingStyle(pVf
ddfc0 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 68 29 s, zFilename, h)
ddfd0 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 ;. }.. switch(
ddfe0 20 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 29 eLockingStyle )
ddff0 7b 0a 0a 20 20 20 20 63 61 73 65 20 4c 4f 43 4b {.. case LOCK
de000 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 3a ING_STYLE_POSIX:
de010 20 7b 0a 20 20 20 20 20 20 65 6e 74 65 72 4d 75 {. enterMu
de020 74 65 78 28 29 3b 0a 23 69 66 20 49 53 5f 56 58 tex();.#if IS_VX
de030 57 4f 52 4b 53 0a 20 20 20 20 20 20 72 63 20 3d WORKS. rc =
de040 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 68 2c findLockInfo(h,
de050 20 70 4e 65 77 2d 3e 7a 52 65 61 6c 70 61 74 68 pNew->zRealpath
de060 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 , &pNew->pLock,
de070 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 23 &pNew->pOpen);.#
de080 65 6c 73 65 0a 20 20 20 20 20 20 72 63 20 3d 20 else. rc =
de090 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 68 2c 20 findLockInfo(h,
de0a0 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 &pNew->pLock, &p
de0b0 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 23 65 6e New->pOpen);.#en
de0c0 64 69 66 0a 20 20 20 20 20 20 6c 65 61 76 65 4d dif. leaveM
de0d0 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 62 72 utex();. br
de0e0 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 eak;. }..#if
de0f0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
de100 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66 CKING_STYLE..#if
de110 20 21 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 !IS_VXWORKS.
de120 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 case LOCKING_ST
de130 59 4c 45 5f 41 46 50 3a 20 7b 0a 20 20 20 20 20 YLE_AFP: {.
de140 20 2f 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 /* AFP locking
de150 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 uses the file pa
de160 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 th so it needs t
de170 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e o be included in
de180 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 61 66 . ** the af
de190 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2e pLockingContext.
de1a0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
de1b0 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
de1c0 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 20 20 70 t *pCtx;. p
de1d0 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 New->lockingCont
de1e0 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c ext = pCtx = sql
de1f0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a ite3_malloc( siz
de200 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 eof(*pCtx) );.
de210 20 20 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 if( pCtx==0
de220 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
de230 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
de240 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
de250 20 20 20 2f 2a 20 4e 42 3a 20 7a 46 69 6c 65 6e /* NB: zFilen
de260 61 6d 65 20 65 78 69 73 74 73 20 61 6e 64 20 72 ame exists and r
de270 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 emains valid unt
de280 69 6c 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 il the file is c
de290 6c 6f 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a losed. **
de2a0 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 72 65 according to re
de2b0 71 75 69 72 65 6d 65 6e 74 20 46 31 31 31 34 31 quirement F11141
de2c0 2e 20 20 53 6f 20 77 65 20 64 6f 20 6e 6f 74 20 . So we do not
de2d0 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 0a 20 need to make a.
de2e0 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f ** copy o
de2f0 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 2e 20 f the filename.
de300 2a 2f 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d */. pCtx-
de310 3e 66 69 6c 65 50 61 74 68 20 3d 20 7a 46 69 6c >filePath = zFil
de320 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 73 ename;. s
de330 72 61 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 randomdev();.
de340 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
de350 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a ;. }.#endif..
de360 20 20 20 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47 case LOCKING
de370 5f 53 54 59 4c 45 5f 44 4f 54 46 49 4c 45 3a 20 _STYLE_DOTFILE:
de380 7b 0a 20 20 20 20 20 20 2f 2a 20 44 6f 74 66 69 {. /* Dotfi
de390 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 le locking uses
de3a0 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f the file path so
de3b0 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 it needs to be
de3c0 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 included in.
de3d0 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f 63 6b ** the dotlock
de3e0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 0a LockingContext .
de3f0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 */. c
de400 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a har *zLockFile;.
de410 20 20 20 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e int nFilen
de420 61 6d 65 3b 0a 20 20 20 20 20 20 6e 46 69 6c 65 ame;. nFile
de430 6e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 46 name = strlen(zF
de440 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 ilename) + 6;.
de450 20 20 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 zLockFile =
de460 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f (char *)sqlite3_
de470 6d 61 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 malloc(nFilename
de480 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4c 6f );. if( zLo
de490 63 6b 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 ckFile==0 ){.
de4a0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
de4b0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 _NOMEM;. }e
de4c0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
de4d0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 ite3_snprintf(nF
de4e0 69 6c 65 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 ilename, zLockFi
de4f0 6c 65 2c 20 22 25 73 2e 6c 6f 63 6b 22 2c 20 7a le, "%s.lock", z
de500 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Filename);.
de510 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c }. pNew->l
de520 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 ockingContext =
de530 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 20 20 20 zLockFile;.
de540 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 break;. }..#
de550 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if IS_VXWORKS.
de560 20 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 case LOCKING_S
de570 54 59 4c 45 5f 4e 41 4d 45 44 53 45 4d 3a 20 7b TYLE_NAMEDSEM: {
de580 0a 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 . /* Named
de590 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e semaphore lockin
de5a0 67 20 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 g uses the file
de5b0 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 path so it needs
de5c0 20 74 6f 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 to be. **
de5d0 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 included in the
de5e0 6e 61 6d 65 64 73 65 6d 4c 6f 63 6b 69 6e 67 43 namedsemLockingC
de5f0 6f 6e 74 65 78 74 0a 20 20 20 20 20 20 2a 2f 0a ontext. */.
de600 20 20 20 20 20 20 65 6e 74 65 72 4d 75 74 65 78 enterMutex
de610 28 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 ();. rc = f
de620 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 68 2c 20 70 indLockInfo(h, p
de630 4e 65 77 2d 3e 7a 52 65 61 6c 70 61 74 68 2c 20 New->zRealpath,
de640 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 &pNew->pLock, &p
de650 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 New->pOpen);.
de660 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 if( (rc==SQLI
de670 54 45 5f 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d TE_OK) && (pNew-
de680 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 >pOpen->pSem==NU
de690 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 LL) ){. c
de6a0 68 61 72 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20 har *zSemName =
de6b0 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 pNew->pOpen->aSe
de6c0 6d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 mName;. i
de6d0 6e 74 20 6e 3b 0a 20 20 20 20 20 20 20 20 73 71 nt n;. sq
de6e0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d lite3_snprintf(M
de6f0 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 53 65 AX_PATHNAME, zSe
de700 6d 4e 61 6d 65 2c 20 22 25 73 2e 73 65 6d 22 2c mName, "%s.sem",
de710 20 70 4e 65 77 2d 3e 7a 52 65 61 6c 70 61 74 68 pNew->zRealpath
de720 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 20 );. for(
de730 6e 3d 30 3b 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d n=0; zSemName[n]
de740 3b 20 6e 2b 2b 20 29 0a 20 20 20 20 20 20 20 20 ; n++ ).
de750 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d 65 5b 6e if( zSemName[n
de760 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d 4e 61 6d ]=='/' ) zSemNam
de770 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20 20 20 20 e[n] = '_';.
de780 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d pNew->pOpen-
de790 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65 6e >pSem = sem_open
de7a0 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52 45 (zSemName, O_CRE
de7b0 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20 20 AT, 0666, 1);.
de7c0 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e if( pNew->
de7d0 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 pOpen->pSem == S
de7e0 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 EM_FAILED ){.
de7f0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
de800 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
de810 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d pNew->pOpen-
de820 3e 61 53 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 >aSemName[0] = '
de830 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 \0';. }.
de840 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6c 65 61 }. lea
de850 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 veMutex();.
de860 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 break;. }.#e
de870 6e 64 69 66 0a 0a 20 20 20 20 63 61 73 65 20 4c ndif.. case L
de880 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 46 4c 4f OCKING_STYLE_FLO
de890 43 4b 3a 20 0a 20 20 20 20 63 61 73 65 20 4c 4f CK: . case LO
de8a0 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e 45 CKING_STYLE_NONE
de8b0 3a 20 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a : . break;.
de8c0 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 0a 20 20 #endif. }. .
de8d0 70 4e 65 77 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 pNew->lastErrno
de8e0 3d 20 30 3b 0a 23 69 66 20 49 53 5f 56 58 57 4f = 0;.#if IS_VXWO
de8f0 52 4b 53 0a 20 20 69 66 28 20 72 63 21 3d 53 51 RKS. if( rc!=SQ
de900 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 LITE_OK ){. u
de910 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 nlink(zFilename)
de920 3b 0a 20 20 20 20 69 73 44 65 6c 65 74 65 20 3d ;. isDelete =
de930 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 0;. }. pNew->
de940 69 73 44 65 6c 65 74 65 20 3d 20 69 73 44 65 6c isDelete = isDel
de950 65 74 65 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 ete;.#endif. if
de960 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
de970 29 7b 0a 20 20 20 20 69 66 28 20 64 69 72 66 64 ){. if( dirfd
de980 3e 3d 30 20 29 20 63 6c 6f 73 65 28 64 69 72 66 >=0 ) close(dirf
de990 64 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28 68 29 d);. close(h)
de9a0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
de9b0 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 New->pMethod = &
de9c0 61 49 6f 4d 65 74 68 6f 64 5b 65 4c 6f 63 6b 69 aIoMethod[eLocki
de9d0 6e 67 53 74 79 6c 65 2d 31 5d 3b 0a 20 20 20 20 ngStyle-1];.
de9e0 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b OpenCounter(+1);
de9f0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
dea00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
dea10 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f a file descripto
dea20 72 20 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f r to the directo
dea30 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 ry containing fi
dea40 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a le zFilename..**
dea50 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
dea60 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 *pFd is set to t
dea70 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 he opened file d
dea80 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a escriptor and.**
dea90 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
deaa0 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 turned. If an er
deab0 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 ror occurs, eith
deac0 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a er SQLITE_NOMEM.
dead0 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e ** or SQLITE_CAN
deae0 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 TOPEN is returne
deaf0 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 d and *pFd is se
deb00 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 t to an undefine
deb10 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a d.** value..**.*
deb20 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 * If SQLITE_OK i
deb30 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 s returned, the
deb40 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e caller is respon
deb50 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e sible for closin
deb60 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65 g.** the file de
deb70 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73 scriptor *pFd us
deb80 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a ing close()..*/.
deb90 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 static int openD
deba0 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 irectory(const c
debb0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
debc0 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 int *pFd){. int
debd0 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 ii;. int fd =
debe0 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e -1;. char zDirn
debf0 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 ame[MAX_PATHNAME
dec00 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f +1];.. sqlite3_
dec10 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
dec20 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c HNAME, zDirname,
dec30 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 "%s", zFilename
dec40 29 3b 0a 20 20 66 6f 72 28 69 69 3d 73 74 72 6c );. for(ii=strl
dec50 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20 69 69 en(zDirname); ii
dec60 3e 3d 30 20 26 26 20 7a 44 69 72 6e 61 6d 65 5b >=0 && zDirname[
dec70 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 3b ii]!='/'; ii--);
dec80 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a 20 . if( ii>0 ){.
dec90 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 20 zDirname[ii]
deca0 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20 3d = '\0';. fd =
decb0 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20 open(zDirname,
decc0 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52 O_RDONLY|O_BINAR
decd0 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 66 Y, 0);. if( f
dece0 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 46 d>=0 ){.#ifdef F
decf0 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20 20 D_CLOEXEC.
ded00 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 fcntl(fd, F_SETF
ded10 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 D, fcntl(fd, F_G
ded20 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c ETFD, 0) | FD_CL
ded30 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a 20 OEXEC);.#endif.
ded40 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4f OSTRACE3("O
ded50 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c 6e PENDIR %-3d %s\n
ded60 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65 29 ", fd, zDirname)
ded70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 ;. }. }. *p
ded80 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75 72 Fd = fd;. retur
ded90 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45 5f n (fd>=0?SQLITE_
deda0 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 OK:SQLITE_CANTOP
dedb0 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 EN);.}../*.** Cr
dedc0 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 eate a temporary
dedd0 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 file name in zB
dede0 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 uf. zBuf must b
dedf0 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 e allocated.** b
dee00 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 y the calling pr
dee10 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20 62 ocess and must b
dee20 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 e big enough to
dee30 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a 2a hold at least.**
dee40 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
dee50 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 e bytes..*/.stat
dee60 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 ic int getTempna
dee70 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 me(int nBuf, cha
dee80 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 r *zBuf){. stat
dee90 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 ic const char *a
deea0 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 zDirs[] = {.
deeb0 20 30 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 74 0,. "/var/t
deec0 6d 70 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f mp",. "/usr/
deed0 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70 tmp",. "/tmp
deee0 22 2c 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d ",. ".",. }
deef0 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 ;. static const
def00 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a unsigned char z
def10 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 Chars[] =. "a
def20 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 bcdefghijklmnopq
def30 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 rstuvwxyz". "
def40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 ABCDEFGHIJKLMNOP
def50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 QRSTUVWXYZ".
def60 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 "0123456789";.
def70 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75 int i, j;. stru
def80 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 63 ct stat buf;. c
def90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 20 onst char *zDir
defa0 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49 74 27 = ".";.. /* It'
defb0 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 s odd to simulat
defc0 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 e an io-error he
defd0 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 re, but really t
defe0 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a his is just. **
deff0 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 using the io-er
df000 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 ror infrastructu
df010 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 re to test that
df020 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 SQLite handles t
df030 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f his. ** functio
df040 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f n failing. . */
df050 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
df060 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
df070 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 61 7a E_IOERR );.. az
df080 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69 74 65 Dirs[0] = sqlite
df090 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3_temp_directory
df0a0 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 ;. for(i=0; i<A
df0b0 72 72 61 79 53 69 7a 65 28 61 7a 44 69 72 73 29 rraySize(azDirs)
df0c0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
df0d0 61 7a 44 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63 azDirs[i]==0 ) c
df0e0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 ontinue;. if(
df0f0 20 73 74 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c stat(azDirs[i],
df100 20 26 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 &buf) ) continu
df110 65 3b 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53 e;. if( !S_IS
df120 44 49 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 DIR(buf.st_mode)
df130 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
df140 20 69 66 28 20 61 63 63 65 73 73 28 61 7a 44 69 if( access(azDi
df150 72 73 5b 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e rs[i], 07) ) con
df160 74 69 6e 75 65 3b 0a 20 20 20 20 7a 44 69 72 20 tinue;. zDir
df170 3d 20 61 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20 = azDirs[i];.
df180 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f break;. }.. /
df190 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
df1a0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 output buffer i
df1b0 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66 s large enough f
df1c0 6f 72 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 or the temporary
df1d0 20 66 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 file . ** name
df1e0 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 . If it is not,
df1f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
df200 52 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ROR.. */. if(
df210 28 73 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 (strlen(zDir) +
df220 73 74 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 strlen(SQLITE_TE
df230 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 MP_FILE_PREFIX)
df240 2b 20 31 37 29 20 3e 3d 20 28 73 69 7a 65 5f 74 + 17) >= (size_t
df250 29 6e 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74 )nBuf ){. ret
df260 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
df270 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 ;. }.. do{.
df280 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
df290 66 28 6e 42 75 66 2d 31 37 2c 20 7a 42 75 66 2c f(nBuf-17, zBuf,
df2a0 20 22 25 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d "%s/"SQLITE_TEM
df2b0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a P_FILE_PREFIX, z
df2c0 44 69 72 29 3b 0a 20 20 20 20 6a 20 3d 20 73 74 Dir);. j = st
df2d0 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 rlen(zBuf);.
df2e0 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
df2f0 73 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 ss(15, &zBuf[j])
df300 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
df310 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a <15; i++, j++){.
df320 20 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 zBuf[j] =
df330 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 (char)zChars[ ((
df340 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 unsigned char)zB
df350 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a uf[j])%(sizeof(z
df360 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 Chars)-1) ];.
df370 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d }. zBuf[j] =
df380 20 30 3b 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 0;. }while( ac
df390 63 65 73 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 cess(zBuf,0)==0
df3a0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
df3b0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a TE_OK;.}.../*.**
df3c0 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 7a Open the file z
df3d0 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65 Path..** .** Pre
df3e0 76 69 6f 75 73 6c 79 2c 20 74 68 65 20 53 51 4c viously, the SQL
df3f0 69 74 65 20 4f 53 20 6c 61 79 65 72 20 75 73 65 ite OS layer use
df400 64 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e d three function
df410 73 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 s in place of th
df420 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a is.** one:.**.**
df430 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 sqlite3OsOp
df440 65 6e 52 65 61 64 57 72 69 74 65 28 29 3b 0a 2a enReadWrite();.*
df450 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f * sqlite3OsO
df460 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a penReadOnly();.*
df470 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f * sqlite3OsO
df480 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 3b 0a penExclusive();.
df490 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c **.** These call
df4a0 73 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 s correspond to
df4b0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f the following co
df4c0 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66 6c mbinations of fl
df4d0 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52 ags:.**.** R
df4e0 65 61 64 57 72 69 74 65 28 29 20 2d 3e 20 20 20 eadWrite() ->
df4f0 20 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 (READWRITE | C
df500 52 45 41 54 45 29 0a 2a 2a 20 20 20 20 20 52 65 REATE).** Re
df510 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20 20 adOnly() ->
df520 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20 (READONLY) .**
df530 20 20 20 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 OpenExclusiv
df540 65 28 29 20 2d 3e 20 28 52 45 41 44 57 52 49 54 e() -> (READWRIT
df550 45 20 7c 20 43 52 45 41 54 45 20 7c 20 45 58 43 E | CREATE | EXC
df560 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68 LUSIVE).**.** Th
df570 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75 73 e old OpenExclus
df580 69 76 65 28 29 20 61 63 63 65 70 74 65 64 20 61 ive() accepted a
df590 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e boolean argumen
df5a0 74 20 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20 49 t - "delFlag". I
df5b0 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 20 66 f.** true, the f
df5c0 69 6c 65 20 77 61 73 20 63 6f 6e 66 69 67 75 72 ile was configur
df5d0 65 64 20 74 6f 20 62 65 20 61 75 74 6f 6d 61 74 ed to be automat
df5e0 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 77 ically deleted w
df5f0 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 hen the.** file
df600 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20 54 handle closed. T
df610 6f 20 61 63 68 69 65 76 65 20 74 68 65 20 73 61 o achieve the sa
df620 6d 65 20 65 66 66 65 63 74 20 75 73 69 6e 67 20 me effect using
df630 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e 74 this new .** int
df640 65 72 66 61 63 65 2c 20 61 64 64 20 74 68 65 20 erface, add the
df650 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66 6c DELETEONCLOSE fl
df660 61 67 20 74 6f 20 74 68 6f 73 65 20 73 70 65 63 ag to those spec
df670 69 66 69 65 64 20 61 62 6f 76 65 20 66 6f 72 20 ified above for
df680 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 .** OpenExclusiv
df690 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 e()..*/.static i
df6a0 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20 73 nt unixOpen(. s
df6b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
df6c0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
df6d0 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c 69 74 *zPath, . sqlit
df6e0 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 0a e3_file *pFile,.
df6f0 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 int flags,. i
df700 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b nt *pOutFlags.){
df710 0a 20 20 69 6e 74 20 66 64 20 3d 20 30 3b 20 20 . int fd = 0;
df720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
df730 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 /* File descri
df740 70 74 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 ptor returned by
df750 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 open() */. int
df760 20 64 69 72 66 64 20 3d 20 2d 31 3b 20 20 20 20 dirfd = -1;
df770 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
df780 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 irectory file de
df790 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e scriptor */. in
df7a0 74 20 6f 66 6c 61 67 73 20 3d 20 30 3b 20 20 20 t oflags = 0;
df7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
df7c0 46 6c 61 67 73 20 74 6f 20 70 61 73 73 20 74 6f Flags to pass to
df7d0 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 open() */. int
df7e0 20 65 54 79 70 65 20 3d 20 66 6c 61 67 73 26 30 eType = flags&0
df7f0 78 46 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 54 xFFFFFF00; /* T
df800 79 70 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f ype of file to o
df810 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c pen */. int noL
df820 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ock;
df830 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
df840 74 6f 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 to omit locking
df850 70 72 69 6d 69 74 69 76 65 73 20 2a 2f 0a 0a 20 primitives */..
df860 20 69 6e 74 20 69 73 45 78 63 6c 75 73 69 76 65 int isExclusive
df870 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c = (flags & SQL
df880 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
df890 56 45 29 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c VE);. int isDel
df8a0 65 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 ete = (flags
df8b0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 & SQLITE_OPEN_D
df8c0 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 ELETEONCLOSE);.
df8d0 20 69 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 int isCreate
df8e0 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c = (flags & SQL
df8f0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 ITE_OPEN_CREATE)
df900 3b 0a 20 20 69 6e 74 20 69 73 52 65 61 64 6f 6e ;. int isReadon
df910 6c 79 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 ly = (flags &
df920 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
df930 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 ONLY);. int isR
df940 65 61 64 57 72 69 74 65 20 20 3d 20 28 66 6c 61 eadWrite = (fla
df950 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
df960 5f 52 45 41 44 57 52 49 54 45 29 3b 0a 0a 20 20 _READWRITE);..
df970 2f 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 /* If creating a
df980 20 6d 61 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d master or main-
df990 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 file journal, th
df9a0 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c is function will
df9b0 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c open. ** a fil
df9c0 65 2d 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 e-descriptor on
df9d0 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f the directory to
df9e0 6f 2e 20 54 68 65 20 66 69 72 73 74 20 74 69 6d o. The first tim
df9f0 65 20 75 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a e unixSync(). *
dfa00 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 * is called the
dfa10 64 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 directory file d
dfa20 65 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 escriptor will b
dfa30 65 20 66 73 79 6e 63 28 29 65 64 20 61 6e 64 20 e fsync()ed and
dfa40 63 6c 6f 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20 close()d.. */.
dfa50 20 69 6e 74 20 69 73 4f 70 65 6e 44 69 72 65 63 int isOpenDirec
dfa60 74 6f 72 79 20 3d 20 28 69 73 43 72 65 61 74 65 tory = (isCreate
dfa70 20 26 26 20 0a 20 20 20 20 20 20 28 65 54 79 70 && . (eTyp
dfa80 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e==SQLITE_OPEN_M
dfa90 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c ASTER_JOURNAL ||
dfaa0 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f eType==SQLITE_O
dfab0 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
dfac0 29 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 ). );.. /* If
dfad0 61 72 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 argument zPath i
dfae0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
dfaf0 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
dfb00 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f is required to o
dfb10 70 65 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f pen. ** a tempo
dfb20 72 61 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 rary file. Use t
dfb30 68 69 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 his buffer to st
dfb40 6f 72 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d ore the file nam
dfb50 65 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 e in.. */. cha
dfb60 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 r zTmpname[MAX_P
dfb70 41 54 48 4e 41 4d 45 2b 31 5d 3b 0a 20 20 63 6f ATHNAME+1];. co
dfb80 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 nst char *zName
dfb90 3d 20 7a 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43 = zPath;.. /* C
dfba0 68 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 heck the followi
dfbb0 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 ng statements ar
dfbc0 65 20 74 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 e true: . **.
dfbd0 2a 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c 79 ** (a) Exactly
dfbe0 20 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41 44 one of the READ
dfbf0 57 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f 4e WRITE and READON
dfc00 4c 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65 LY flags must be
dfc10 20 73 65 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 set, and . **
dfc20 20 20 28 62 29 20 69 66 20 43 52 45 41 54 45 20 (b) if CREATE
dfc30 69 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45 41 is set, then REA
dfc40 44 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73 6f DWRITE must also
dfc50 20 62 65 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a be set, and. *
dfc60 2a 20 20 20 28 63 29 20 69 66 20 45 58 43 4c 55 * (c) if EXCLU
dfc70 53 49 56 45 20 69 73 20 73 65 74 2c 20 74 68 65 SIVE is set, the
dfc80 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c n CREATE must al
dfc90 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 so be set.. **
dfca0 20 20 28 64 29 20 69 66 20 44 45 4c 45 54 45 4f (d) if DELETEO
dfcb0 4e 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20 74 NCLOSE is set, t
dfcc0 68 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 hen CREATE must
dfcd0 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a also be set.. *
dfce0 2f 0a 20 20 61 73 73 65 72 74 28 28 69 73 52 65 /. assert((isRe
dfcf0 61 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 adonly==0 || isR
dfd00 65 61 64 57 72 69 74 65 3d 3d 30 29 20 26 26 20 eadWrite==0) &&
dfd10 28 69 73 52 65 61 64 57 72 69 74 65 20 7c 7c 20 (isReadWrite ||
dfd20 69 73 52 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 isReadonly));.
dfd30 61 73 73 65 72 74 28 69 73 43 72 65 61 74 65 3d assert(isCreate=
dfd40 3d 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 =0 || isReadWrit
dfd50 65 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 45 e);. assert(isE
dfd60 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 xclusive==0 || i
dfd70 73 43 72 65 61 74 65 29 3b 0a 20 20 61 73 73 65 sCreate);. asse
dfd80 72 74 28 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c rt(isDelete==0 |
dfd90 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 | isCreate);..
dfda0 2f 2a 20 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 /* The main DB,
dfdb0 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 61 6e main journal, an
dfdc0 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c d master journal
dfdd0 20 61 72 65 20 6e 65 76 65 72 20 61 75 74 6f 6d are never autom
dfde0 61 74 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 64 65 atically. ** de
dfdf0 6c 65 74 65 64 0a 20 20 2a 2f 0a 20 20 61 73 73 leted. */. ass
dfe00 65 72 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49 ert( eType!=SQLI
dfe10 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 TE_OPEN_MAIN_DB
dfe20 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b 0a || !isDelete );.
dfe30 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 assert( eType!
dfe40 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 =SQLITE_OPEN_MAI
dfe50 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21 69 73 N_JOURNAL || !is
dfe60 44 65 6c 65 74 65 20 29 3b 0a 20 20 61 73 73 65 Delete );. asse
dfe70 72 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49 54 rt( eType!=SQLIT
dfe80 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
dfe90 55 52 4e 41 4c 20 7c 7c 20 21 69 73 44 65 6c 65 URNAL || !isDele
dfea0 74 65 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 te );.. /* Asse
dfeb0 72 74 20 74 68 61 74 20 74 68 65 20 75 70 70 65 rt that the uppe
dfec0 72 20 6c 61 79 65 72 20 68 61 73 20 73 65 74 20 r layer has set
dfed0 6f 6e 65 20 6f 66 20 74 68 65 20 22 66 69 6c 65 one of the "file
dfee0 2d 74 79 70 65 22 20 66 6c 61 67 73 2e 20 2a 2f -type" flags. */
dfef0 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 . assert( eType
dff00 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 ==SQLITE_OPEN_MA
dff10 49 4e 5f 44 42 20 20 20 20 20 20 7c 7c 20 65 54 IN_DB || eT
dff20 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
dff30 5f 54 45 4d 50 5f 44 42 20 0a 20 20 20 20 20 20 _TEMP_DB .
dff40 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 || eType==SQLIT
dff50 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 E_OPEN_MAIN_JOUR
dff60 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 NAL || eType==SQ
dff70 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a LITE_OPEN_TEMP_J
dff80 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c OURNAL . |
dff90 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f | eType==SQLITE_
dffa0 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 OPEN_SUBJOURNAL
dffb0 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 || eType==SQLI
dffc0 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a TE_OPEN_MASTER_J
dffd0 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c OURNAL . |
dffe0 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f | eType==SQLITE_
dfff0 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 OPEN_TRANSIENT_D
e0000 42 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 B. );.. memset
e0010 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f (pFile, 0, sizeo
e0020 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 f(unixFile));..
e0030 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 if( !zName ){.
e0040 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 int rc;. a
e0050 73 73 65 72 74 28 69 73 44 65 6c 65 74 65 20 26 ssert(isDelete &
e0060 26 20 21 69 73 4f 70 65 6e 44 69 72 65 63 74 6f & !isOpenDirecto
e0070 72 79 29 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 ry);. rc = ge
e0080 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 tTempname(MAX_PA
e0090 54 48 4e 41 4d 45 2b 31 2c 20 7a 54 6d 70 6e 61 THNAME+1, zTmpna
e00a0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 me);. if( rc!
e00b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
e00c0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
e00d0 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d }. zName =
e00e0 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a zTmpname;. }..
e00f0 20 20 69 66 28 20 69 73 52 65 61 64 6f 6e 6c 79 if( isReadonly
e0100 20 29 20 20 6f 66 6c 61 67 73 20 7c 3d 20 4f 5f ) oflags |= O_
e0110 52 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 RDONLY;. if( is
e0120 52 65 61 64 57 72 69 74 65 20 29 20 6f 66 6c 61 ReadWrite ) ofla
e0130 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 20 gs |= O_RDWR;.
e0140 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20 20 if( isCreate )
e0150 20 20 6f 66 6c 61 67 73 20 7c 3d 20 4f 5f 43 52 oflags |= O_CR
e0160 45 41 54 3b 0a 20 20 69 66 28 20 69 73 45 78 63 EAT;. if( isExc
e0170 6c 75 73 69 76 65 20 29 20 6f 66 6c 61 67 73 20 lusive ) oflags
e0180 7c 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 |= (O_EXCL|O_NOF
e0190 4f 4c 4c 4f 57 29 3b 0a 20 20 6f 66 6c 61 67 73 OLLOW);. oflags
e01a0 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 49 4c 45 |= (O_LARGEFILE
e01b0 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 20 66 |O_BINARY);.. f
e01c0 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20 d = open(zName,
e01d0 6f 66 6c 61 67 73 2c 20 69 73 44 65 6c 65 74 65 oflags, isDelete
e01e0 3f 30 36 30 30 3a 53 51 4c 49 54 45 5f 44 45 46 ?0600:SQLITE_DEF
e01f0 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 AULT_FILE_PERMIS
e0200 53 49 4f 4e 53 29 3b 0a 20 20 4f 53 54 52 41 43 SIONS);. OSTRAC
e0210 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33 64 E4("OPENX %-3d
e0220 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c 20 %s 0%o\n", fd,
e0230 7a 4e 61 6d 65 2c 20 6f 66 6c 61 67 73 29 3b 0a zName, oflags);.
e0240 20 20 69 66 28 20 66 64 3c 30 20 26 26 20 65 72 if( fd<0 && er
e0250 72 6e 6f 21 3d 45 49 53 44 49 52 20 26 26 20 69 rno!=EISDIR && i
e0260 73 52 65 61 64 57 72 69 74 65 20 26 26 20 21 69 sReadWrite && !i
e0270 73 45 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 sExclusive ){.
e0280 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f /* Failed to o
e0290 70 65 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 pen the file for
e02a0 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 read/write acce
e02b0 73 73 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c ss. Try read-onl
e02c0 79 2e 20 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20 y. */. flags
e02d0 26 3d 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e &= ~(SQLITE_OPEN
e02e0 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 _READWRITE|SQLIT
e02f0 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a E_OPEN_CREATE);.
e0300 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c flags |= SQL
e0310 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
e0320 59 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e Y;. return un
e0330 69 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 ixOpen(pVfs, zPa
e0340 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 th, pFile, flags
e0350 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 , pOutFlags);.
e0360 7d 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a }. if( fd<0 ){.
e0370 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e0380 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a E_CANTOPEN;. }.
e0390 20 20 69 66 28 20 69 73 44 65 6c 65 74 65 20 29 if( isDelete )
e03a0 7b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 {.#if IS_VXWORKS
e03b0 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61 . zPath = zNa
e03c0 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e me;.#else. un
e03d0 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e link(zName);.#en
e03e0 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f dif. }. if( pO
e03f0 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a utFlags ){. *
e0400 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 pOutFlags = flag
e0410 73 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 s;. }.. assert
e0420 28 66 64 21 3d 30 29 3b 0a 20 20 69 66 28 20 69 (fd!=0);. if( i
e0430 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20 29 sOpenDirectory )
e0440 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 6f {. int rc = o
e0450 70 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 penDirectory(zPa
e0460 74 68 2c 20 26 64 69 72 66 64 29 3b 0a 20 20 20 th, &dirfd);.
e0470 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
e0480 4f 4b 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73 OK ){. clos
e0490 65 28 66 64 29 3b 0a 20 20 20 20 20 20 72 65 74 e(fd);. ret
e04a0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
e04b0 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f }..#ifdef FD_CLO
e04c0 45 58 45 43 0a 20 20 66 63 6e 74 6c 28 66 64 2c EXEC. fcntl(fd,
e04d0 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 F_SETFD, fcntl(
e04e0 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 fd, F_GETFD, 0)
e04f0 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 | FD_CLOEXEC);.#
e0500 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63 6b 20 endif.. noLock
e0510 3d 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f = eType!=SQLITE_
e0520 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a 20 20 OPEN_MAIN_DB;.
e0530 72 65 74 75 72 6e 20 66 69 6c 6c 49 6e 55 6e 69 return fillInUni
e0540 78 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 xFile(pVfs, fd,
e0550 64 69 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 dirfd, pFile, zP
e0560 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 ath, noLock, isD
e0570 65 6c 65 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a elete);.}../*.**
e0580 20 44 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 Delete the file
e0590 20 61 74 20 7a 50 61 74 68 2e 20 49 66 20 74 68 at zPath. If th
e05a0 65 20 64 69 72 53 79 6e 63 20 61 72 67 75 6d 65 e dirSync argume
e05b0 6e 74 20 69 73 20 74 72 75 65 2c 20 66 73 79 6e nt is true, fsyn
e05c0 63 28 29 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 c().** the direc
e05d0 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 tory after delet
e05e0 69 6e 67 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f ing the file..*/
e05f0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e0600 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76 Delete(sqlite3_v
e0610 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e fs *NotUsed, con
e0620 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
e0630 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 int dirSync){.
e0640 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
e0650 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 OK;. UNUSED_PAR
e0660 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e0670 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
e0680 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 or(return SQLITE
e0690 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a _IOERR_DELETE);.
e06a0 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b unlink(zPath);
e06b0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
e06c0 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a DISABLE_DIRSYNC.
e06d0 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b if( dirSync ){
e06e0 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20 . int fd;.
e06f0 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 rc = openDirect
e0700 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b ory(zPath, &fd);
e0710 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
e0720 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 49 53 ITE_OK ){.#if IS
e0730 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69 _VXWORKS. i
e0740 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31 f( fsync(fd)==-1
e0750 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69 ).#else. i
e0760 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23 f( fsync(fd) ).#
e0770 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 endif. {.
e0780 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
e0790 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e E_IOERR_DIR_FSYN
e07a0 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 C;. }.
e07b0 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20 20 close(fd);.
e07c0 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 }. }.#endif. r
e07d0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
e07e0 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78 69 73 ** Test the exis
e07f0 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 63 65 tance of or acce
e0800 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f ss permissions o
e0810 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 54 68 f file zPath. Th
e0820 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 6f 72 e.** test perfor
e0830 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 med depends on t
e0840 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c 61 67 he value of flag
e0850 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c s:.**.** SQL
e0860 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 ITE_ACCESS_EXIST
e0870 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 S: Return 1 if t
e0880 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 0a 2a he file exists.*
e0890 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 * SQLITE_ACC
e08a0 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 20 52 ESS_READWRITE: R
e08b0 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 66 eturn 1 if the f
e08c0 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e 64 20 ile is read and
e08d0 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 writable..**
e08e0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 SQLITE_ACCESS_R
e08f0 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72 6e 20 EADONLY: Return
e0900 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 1 if the file is
e0910 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a readable..**.**
e0920 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 Otherwise retur
e0930 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 n 0..*/.static i
e0940 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28 0a 20 nt unixAccess(.
e0950 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f sqlite3_vfs *No
e0960 74 55 73 65 64 2c 20 0a 20 20 63 6f 6e 73 74 20 tUsed, . const
e0970 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 char *zPath, .
e0980 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e int flags, . in
e0990 74 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 t *pResOut.){.
e09a0 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b 0a 20 int amode = 0;.
e09b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e09c0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 R(NotUsed);. Si
e09d0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
e09e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
e09f0 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a 20 20 RR_ACCESS; );.
e0a00 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b switch( flags ){
e0a10 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
e0a20 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a _ACCESS_EXISTS:.
e0a30 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 46 5f amode = F_
e0a40 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b OK;. break;
e0a50 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
e0a60 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
e0a70 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d E:. amode =
e0a80 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 20 W_OK|R_OK;.
e0a90 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
e0aa0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
e0ab0 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d 6f 64 READ:. amod
e0ac0 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 e = R_OK;.
e0ad0 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61 break;.. defa
e0ae0 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72 ult:. asser
e0af0 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 t(!"Invalid flag
e0b00 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 s argument");.
e0b10 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 28 }. *pResOut = (
e0b20 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20 61 6d access(zPath, am
e0b30 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 72 65 74 75 ode)==0);. retu
e0b40 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
e0b50 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 ../*.** Turn a r
e0b60 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 elative pathname
e0b70 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 into a full pat
e0b80 68 6e 61 6d 65 2e 20 54 68 65 20 72 65 6c 61 74 hname. The relat
e0b90 69 76 65 20 70 61 74 68 0a 2a 2a 20 69 73 20 73 ive path.** is s
e0ba0 74 6f 72 65 64 20 61 73 20 61 20 6e 75 6c 2d 74 tored as a nul-t
e0bb0 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 erminated string
e0bc0 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20 70 in the buffer p
e0bd0 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 ointed to by.**
e0be0 7a 50 61 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f zPath. .**.** zO
e0bf0 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 ut points to a b
e0c00 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73 uffer of at leas
e0c10 74 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 t sqlite3_vfs.mx
e0c20 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73 20 0a Pathname bytes .
e0c30 2a 2a 20 28 69 6e 20 74 68 69 73 20 63 61 73 65 ** (in this case
e0c40 2c 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 62 , MAX_PATHNAME b
e0c50 79 74 65 73 29 2e 20 54 68 65 20 66 75 6c 6c 2d ytes). The full-
e0c60 70 61 74 68 20 69 73 20 77 72 69 74 74 65 6e 20 path is written
e0c70 74 6f 0a 2a 2a 20 74 68 69 73 20 62 75 66 66 65 to.** this buffe
e0c80 72 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 r before returni
e0c90 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
e0ca0 74 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 t unixFullPathna
e0cb0 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 me(. sqlite3_vf
e0cc0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
e0cd0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
e0ce0 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a o vfs object */.
e0cf0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
e0d00 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 ath,
e0d10 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 /* Possibly rela
e0d20 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 20 tive input path
e0d30 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 2c 20 20 */. int nOut,
e0d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0d50 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 /* Size of ou
e0d60 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 tput buffer in b
e0d70 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a ytes */. char *
e0d80 7a 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20 zOut
e0d90 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 /* Outpu
e0da0 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a t buffer */.){..
e0db0 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f /* It's odd to
e0dc0 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d simulate an io-
e0dd0 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 error here, but
e0de0 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a really this is j
e0df0 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 ust. ** using t
e0e00 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 he io-error infr
e0e10 61 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 astructure to te
e0e20 73 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 st that SQLite h
e0e30 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a andles this. **
e0e40 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e function failin
e0e50 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e g. This function
e0e60 20 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 2c 20 could fail if,
e0e70 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 for example, the
e0e80 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f . ** current wo
e0e90 72 6b 69 6e 67 20 64 69 72 65 63 74 6c 79 20 68 rking directly h
e0ea0 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 as been unlinked
e0eb0 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 .. */. Simulat
e0ec0 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e eIOError( return
e0ed0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b SQLITE_ERROR );
e0ee0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 56 66 73 .. assert( pVfs
e0ef0 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 ->mxPathname==MA
e0f00 58 5f 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20 20 X_PATHNAME );.
e0f10 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e0f20 28 70 56 66 73 29 3b 0a 0a 23 69 66 20 49 53 5f (pVfs);..#if IS_
e0f30 56 58 57 4f 52 4b 53 0a 20 20 7b 0a 20 20 20 20 VXWORKS. {.
e0f40 63 68 61 72 20 2a 7a 52 65 61 6c 6e 61 6d 65 20 char *zRealname
e0f50 3d 20 76 78 72 65 61 6c 70 61 74 68 28 7a 50 61 = vxrealpath(zPa
e0f60 74 68 2c 20 30 29 3b 0a 20 20 20 20 7a 4f 75 74 th, 0);. zOut
e0f70 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 [0] = '\0';.
e0f80 69 66 28 20 21 7a 52 65 61 6c 6e 61 6d 65 20 29 if( !zRealname )
e0f90 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
e0fa0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
e0fb0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
e0fc0 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 3_snprintf(nOut,
e0fd0 20 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a 52 65 zOut, "%s", zRe
e0fe0 61 6c 6e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c alname);. sql
e0ff0 69 74 65 33 5f 66 72 65 65 28 7a 52 65 61 6c 6e ite3_free(zRealn
e1000 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ame);. return
e1010 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
e1020 23 65 6c 73 65 0a 20 20 7a 4f 75 74 5b 6e 4f 75 #else. zOut[nOu
e1030 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 t-1] = '\0';. i
e1040 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 f( zPath[0]=='/'
e1050 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
e1060 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a snprintf(nOut, z
e1070 4f 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 Out, "%s", zPath
e1080 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
e1090 69 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 int nCwd;. if
e10a0 28 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e ( getcwd(zOut, n
e10b0 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20 Out-1)==0 ){.
e10c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e10d0 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d _CANTOPEN;. }
e10e0 0a 20 20 20 20 6e 43 77 64 20 3d 20 73 74 72 6c . nCwd = strl
e10f0 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73 71 en(zOut);. sq
e1100 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
e1110 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74 5b Out-nCwd, &zOut[
e1120 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a 50 nCwd], "/%s", zP
e1130 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ath);. }. retu
e1140 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 23 rn SQLITE_OK;..#
e1150 69 66 20 30 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 if 0. /*. ** R
e1160 65 6d 6f 76 65 20 22 2f 2e 2f 22 20 70 61 74 68 emove "/./" path
e1170 20 65 6c 65 6d 65 6e 74 73 20 61 6e 64 20 63 6f elements and co
e1180 6e 76 65 72 74 20 22 2f 41 2f 2e 2f 22 20 70 61 nvert "/A/./" pa
e1190 74 68 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a th elements. **
e11a0 20 74 6f 20 6a 75 73 74 20 22 2f 22 2e 0a 20 20 to just "/"..
e11b0 2a 2f 0a 20 20 69 66 28 20 7a 46 75 6c 6c 20 29 */. if( zFull )
e11c0 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a {. int i, j;.
e11d0 20 20 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a for(i=j=0; z
e11e0 46 75 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 Full[i]; i++){.
e11f0 20 20 20 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69 if( zFull[i
e1200 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 ]=='/' ){.
e1210 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69 2b 31 5d if( zFull[i+1]
e1220 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75 65 =='/' ) continue
e1230 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 ;. if( zF
e1240 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 ull[i+1]=='.' &&
e1250 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d 27 2f 27 zFull[i+2]=='/'
e1260 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 20 ){. i
e1270 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 += 1;.
e1280 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
e1290 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
e12a0 7a 46 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27 20 zFull[i+1]=='.'
e12b0 26 26 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d 27 && zFull[i+2]=='
e12c0 2e 27 20 26 26 20 7a 46 75 6c 6c 5b 69 2b 33 5d .' && zFull[i+3]
e12d0 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 =='/' ){.
e12e0 20 20 20 77 68 69 6c 65 28 20 6a 3e 30 20 26 26 while( j>0 &&
e12f0 20 7a 46 75 6c 6c 5b 6a 2d 31 5d 21 3d 27 2f 27 zFull[j-1]!='/'
e1300 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 ){ j--; }.
e1310 20 20 20 20 20 69 20 2b 3d 20 33 3b 0a 20 20 20 i += 3;.
e1320 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b continue;
e1330 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
e1340 20 7d 0a 20 20 20 20 20 20 7a 46 75 6c 6c 5b 6a }. zFull[j
e1350 2b 2b 5d 20 3d 20 7a 46 75 6c 6c 5b 69 5d 3b 0a ++] = zFull[i];.
e1360 20 20 20 20 7d 0a 20 20 20 20 7a 46 75 6c 6c 5b }. zFull[
e1370 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 j] = 0;. }.#end
e1380 69 66 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 23 69 if.#endif.}...#i
e1390 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
e13a0 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
e13b0 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 ./*.** Interface
e13c0 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 s for opening a
e13d0 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 shared library,
e13e0 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f finding entry po
e13f0 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 ints.** within t
e1400 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 he shared librar
e1410 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 y, and closing t
e1420 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 he shared librar
e1430 79 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c y..*/.#include <
e1440 64 6c 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 dlfcn.h>.static
e1450 76 6f 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e void *unixDlOpen
e1460 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f (sqlite3_vfs *No
e1470 74 55 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 tUsed, const cha
e1480 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
e1490 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e14a0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 R(NotUsed);. re
e14b0 74 75 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c turn dlopen(zFil
e14c0 65 6e 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 ename, RTLD_NOW
e14d0 7c 20 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a | RTLD_GLOBAL);.
e14e0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 }../*.** SQLite
e14f0 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 calls this funct
e1500 69 6f 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ion immediately
e1510 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 after a call to
e1520 75 6e 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a unixDlSym() or.*
e1530 2a 20 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 * unixDlOpen() f
e1540 61 69 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20 ails (returns a
e1550 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 null pointer). I
e1560 66 20 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 f a more detaile
e1570 64 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 d error.** messa
e1580 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c ge is available,
e1590 20 69 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 it is written t
e15a0 6f 20 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f o zBufOut. If no
e15b0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a error message.*
e15c0 2a 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 * is available,
e15d0 7a 42 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 zBufOut is left
e15e0 75 6e 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 unmodified and S
e15f0 51 4c 69 74 65 20 75 73 65 73 20 61 20 64 65 66 QLite uses a def
e1600 61 75 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 ault.** error me
e1610 73 73 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 ssage..*/.static
e1620 20 76 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f void unixDlErro
e1630 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e r(sqlite3_vfs *N
e1640 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 otUsed, int nBuf
e1650 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 , char *zBufOut)
e1660 7b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a {. char *zErr;.
e1670 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
e1680 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 65 ER(NotUsed);. e
e1690 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 7a nterMutex();. z
e16a0 45 72 72 20 3d 20 64 6c 65 72 72 6f 72 28 29 3b Err = dlerror();
e16b0 0a 20 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 . if( zErr ){.
e16c0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
e16d0 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 ntf(nBuf, zBufOu
e16e0 74 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a t, "%s", zErr);.
e16f0 20 20 7d 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 }. leaveMutex
e1700 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 ();.}.static voi
e1710 64 20 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c d *unixDlSym(sql
e1720 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 ite3_vfs *NotUse
e1730 64 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 d, void *pHandle
e1740 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 53 79 , const char*zSy
e1750 6d 62 6f 6c 29 7b 0a 20 20 55 4e 55 53 45 44 5f mbol){. UNUSED_
e1760 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e1770 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 6c 73 d);. return dls
e1780 79 6d 28 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d ym(pHandle, zSym
e1790 62 6f 6c 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 bol);.}.static v
e17a0 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f 73 65 28 oid unixDlClose(
e17b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 sqlite3_vfs *Not
e17c0 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 48 61 6e Used, void *pHan
e17d0 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 dle){. UNUSED_P
e17e0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
e17f0 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 70 48 61 );. dlclose(pHa
e1800 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f ndle);.}.#else /
e1810 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 * if SQLITE_OMIT
e1820 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 _LOAD_EXTENSION
e1830 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 is defined: */.
e1840 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 4f #define unixDlO
e1850 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 pen 0. #define
e1860 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 30 0a 20 unixDlError 0.
e1870 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 53 #define unixDlS
e1880 79 6d 20 20 20 30 0a 20 20 23 64 65 66 69 6e 65 ym 0. #define
e1890 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 30 0a 23 unixDlClose 0.#
e18a0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 endif../*.** Wri
e18b0 74 65 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 te nBuf bytes of
e18c0 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 74 6f 20 random data to
e18d0 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66 the supplied buf
e18e0 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 fer zBuf..*/.sta
e18f0 74 69 63 20 69 6e 74 20 75 6e 69 78 52 61 6e 64 tic int unixRand
e1900 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 omness(sqlite3_v
e1910 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 fs *NotUsed, int
e1920 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
e1930 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 f){. UNUSED_PAR
e1940 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e1950 0a 20 20 61 73 73 65 72 74 28 28 73 69 7a 65 5f . assert((size_
e1960 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 6f 66 28 t)nBuf>=(sizeof(
e1970 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f 66 28 69 time_t)+sizeof(i
e1980 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 nt)));.. /* We
e1990 68 61 76 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 have to initiali
e19a0 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 65 76 65 ze zBuf to preve
e19b0 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d nt valgrind from
e19c0 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 reporting. **
e19d0 65 72 72 6f 72 73 2e 20 20 54 68 65 20 72 65 70 errors. The rep
e19e0 6f 72 74 73 20 69 73 73 75 65 64 20 62 79 20 76 orts issued by v
e19f0 61 6c 67 72 69 6e 64 20 61 72 65 20 69 6e 63 6f algrind are inco
e1a00 72 72 65 63 74 20 2d 20 77 65 20 77 6f 75 6c 64 rrect - we would
e1a10 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 74 68 61 . ** prefer tha
e1a20 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 t the randomness
e1a30 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 62 79 be increased by
e1a40 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f 66 20 74 making use of t
e1a50 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 74 69 61 he. ** uninitia
e1a60 6c 69 7a 65 64 20 73 70 61 63 65 20 69 6e 20 7a lized space in z
e1a70 42 75 66 20 2d 20 62 75 74 20 76 61 6c 67 72 69 Buf - but valgri
e1a80 6e 64 20 65 72 72 6f 72 73 20 74 65 6e 64 20 74 nd errors tend t
e1a90 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 73 6f 6d o worry. ** som
e1aa0 65 20 75 73 65 72 73 2e 20 20 52 61 74 68 65 72 e users. Rather
e1ab0 20 74 68 61 6e 20 61 72 67 75 65 2c 20 69 74 20 than argue, it
e1ac0 73 65 65 6d 73 20 65 61 73 69 65 72 20 6a 75 73 seems easier jus
e1ad0 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a t to initialize.
e1ae0 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c 65 20 61 ** the whole a
e1af0 72 72 61 79 20 61 6e 64 20 73 69 6c 65 6e 63 65 rray and silence
e1b00 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 65 6e 20 valgrind, even
e1b10 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 6c 65 if that means le
e1b20 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 0a 20 20 ss randomness.
e1b30 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e 64 6f 6d ** in the random
e1b40 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a seed.. **. **
e1b50 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 69 When testing, i
e1b60 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a 42 75 66 nitializing zBuf
e1b70 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 20 61 6c [] to zero is al
e1b80 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 74 20 6d l we do. That m
e1b90 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 74 20 77 eans. ** that w
e1ba0 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 65 e always use the
e1bb0 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 6e 75 6d same random num
e1bc0 62 65 72 20 73 65 71 75 65 6e 63 65 2e 20 20 54 ber sequence. T
e1bd0 68 69 73 20 6d 61 6b 65 73 20 74 68 65 0a 20 20 his makes the.
e1be0 2a 2a 20 74 65 73 74 73 20 72 65 70 65 61 74 61 ** tests repeata
e1bf0 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 ble.. */. mems
e1c00 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 et(zBuf, 0, nBuf
e1c10 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 );.#if !defined(
e1c20 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 7b SQLITE_TEST). {
e1c30 0a 20 20 20 20 69 6e 74 20 70 69 64 2c 20 66 64 . int pid, fd
e1c40 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 ;. fd = open(
e1c50 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 "/dev/urandom",
e1c60 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 20 20 69 O_RDONLY);. i
e1c70 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 20 f( fd<0 ){.
e1c80 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20 20 time_t t;.
e1c90 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 20 time(&t);.
e1ca0 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 74 memcpy(zBuf, &t
e1cb0 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 , sizeof(t));.
e1cc0 20 20 20 20 70 69 64 20 3d 20 67 65 74 70 69 64 pid = getpid
e1cd0 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 ();. memcpy
e1ce0 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 28 74 29 (&zBuf[sizeof(t)
e1cf0 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 ], &pid, sizeof(
e1d00 70 69 64 29 29 3b 0a 20 20 20 20 20 20 61 73 73 pid));. ass
e1d10 65 72 74 28 20 73 69 7a 65 6f 66 28 74 29 2b 73 ert( sizeof(t)+s
e1d20 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28 73 69 7a izeof(pid)<=(siz
e1d30 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20 20 20 20 e_t)nBuf );.
e1d40 20 20 6e 42 75 66 20 3d 20 73 69 7a 65 6f 66 28 nBuf = sizeof(
e1d50 74 29 20 2b 20 73 69 7a 65 6f 66 28 70 69 64 29 t) + sizeof(pid)
e1d60 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
e1d70 20 20 20 6e 42 75 66 20 3d 20 72 65 61 64 28 66 nBuf = read(f
e1d80 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66 29 3b 0a d, zBuf, nBuf);.
e1d90 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b close(fd);
e1da0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
e1db0 66 0a 20 20 72 65 74 75 72 6e 20 6e 42 75 66 3b f. return nBuf;
e1dc0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 .}.../*.** Sleep
e1dd0 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 for a little wh
e1de0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 ile. Return the
e1df0 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 amount of time
e1e00 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 slept..** The ar
e1e10 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 gument is the nu
e1e20 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 mber of microsec
e1e30 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 onds we want to
e1e40 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 sleep..** The re
e1e50 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 turn value is th
e1e60 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 e number of micr
e1e70 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 oseconds of slee
e1e80 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 p actually.** re
e1e90 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 quested from the
e1ea0 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 underlying oper
e1eb0 61 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 ating system, a
e1ec0 6e 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 number which.**
e1ed0 6d 69 67 68 74 20 62 65 20 67 72 65 61 74 65 72 might be greater
e1ee0 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
e1ef0 6f 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 o the argument,
e1f00 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 but not less.**
e1f10 74 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e than the argumen
e1f20 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
e1f30 20 75 6e 69 78 53 6c 65 65 70 28 73 71 6c 69 74 unixSleep(sqlit
e1f40 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e1f50 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 int microsecond
e1f60 73 29 7b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 s){.#if IS_VXWOR
e1f70 4b 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 KS. struct time
e1f80 73 70 65 63 20 73 70 3b 0a 0a 20 20 73 70 2e 74 spec sp;.. sp.t
e1f90 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f 73 65 63 v_sec = microsec
e1fa0 6f 6e 64 73 20 2f 20 31 30 30 30 30 30 30 3b 0a onds / 1000000;.
e1fb0 20 20 73 70 2e 74 76 5f 6e 73 65 63 20 3d 20 28 sp.tv_nsec = (
e1fc0 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 25 20 31 microseconds % 1
e1fd0 30 30 30 30 30 30 29 20 2a 20 31 30 30 30 3b 0a 000000) * 1000;.
e1fe0 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26 73 70 2c nanosleep(&sp,
e1ff0 20 4e 55 4c 4c 29 3b 0a 20 20 72 65 74 75 72 6e NULL);. return
e2000 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 microseconds;.#
e2010 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 elif defined(HAV
e2020 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56 E_USLEEP) && HAV
e2030 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 65 E_USLEEP. uslee
e2040 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b p(microseconds);
e2050 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 . return micros
e2060 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 econds;.#else.
e2070 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d int seconds = (m
e2080 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 icroseconds+9999
e2090 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 99)/1000000;. s
e20a0 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 leep(seconds);.
e20b0 20 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 73 2a return seconds*
e20c0 31 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 1000000;.#endif.
e20d0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
e20e0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 7d 0a 0a ER(NotUsed);.}..
e20f0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
e2100 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 ing variable, if
e2110 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 set to a non-ze
e2120 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 ro value, become
e2130 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 s the result.**
e2140 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 returned from sq
e2150 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 lite3OsCurrentTi
e2160 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 me(). This is u
e2170 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e sed for testing.
e2180 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
e2190 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
e21a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 I int sqlite3_cu
e21b0 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a rrent_time = 0;.
e21c0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 #endif../*.** Fi
e21d0 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 nd the current t
e21e0 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 ime (in Universa
e21f0 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 l Coordinated Ti
e2200 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a me). Write the.
e2210 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 ** current time
e2220 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 and date as a Ju
e2230 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 lian Day number
e2240 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a into *prNow and.
e2250 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 ** return 0. Re
e2260 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 turn 1 if the ti
e2270 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e me and date cann
e2280 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a ot be found..*/.
e2290 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 static int unixC
e22a0 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 urrentTime(sqlit
e22b0 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e22c0 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b double *prNow){
e22d0 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a .#if IS_VXWORKS.
e22e0 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 struct timespe
e22f0 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f c sNow;. clock_
e2300 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 gettime(CLOCK_RE
e2310 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a ALTIME, &sNow);.
e2320 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 *prNow = 24405
e2330 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 87.5 + sNow.tv_s
e2340 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f ec/86400.0 + sNo
e2350 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 w.tv_nsec/864000
e2360 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 69 00000000.0;.#eli
e2370 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 f defined(NO_GET
e2380 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b TOD). time_t t;
e2390 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a . time(&t);. *
e23a0 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e prNow = t/86400.
e23b0 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 0 + 2440587.5;.#
e23c0 65 6c 73 65 0a 20 20 73 74 72 75 63 74 20 74 69 else. struct ti
e23d0 6d 65 76 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 meval sNow;. ge
e23e0 74 74 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 ttimeofday(&sNow
e23f0 2c 20 30 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d , 0);. *prNow =
e2400 20 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 2440587.5 + sNo
e2410 77 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 w.tv_sec/86400.0
e2420 20 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f + sNow.tv_usec/
e2430 38 36 34 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 86400000000.0;.#
e2440 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
e2450 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 LITE_TEST. if(
e2460 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f sqlite3_current_
e2470 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e time ){. *prN
e2480 6f 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 ow = sqlite3_cur
e2490 72 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e rent_time/86400.
e24a0 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 0 + 2440587.5;.
e24b0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 }.#endif. UNUS
e24c0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e24d0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 Used);. return
e24e0 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0;.}..static int
e24f0 20 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72 6f unixGetLastErro
e2500 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e r(sqlite3_vfs *N
e2510 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 otUsed, int NotU
e2520 73 65 64 32 2c 20 63 68 61 72 20 2a 4e 6f 74 55 sed2, char *NotU
e2530 73 65 64 33 29 7b 0a 20 20 55 4e 55 53 45 44 5f sed3){. UNUSED_
e2540 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e2550 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d);. UNUSED_PAR
e2560 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 32 29 AMETER(NotUsed2)
e2570 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
e2580 45 54 45 52 28 4e 6f 74 55 73 65 64 33 29 3b 0a ETER(NotUsed3);.
e2590 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
e25a0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
e25b0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
e25c0 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a stem interface..
e25d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
e25e0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 t sqlite3_os_ini
e25f0 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 4d t(void){ . /* M
e2600 61 63 72 6f 20 74 6f 20 64 65 66 69 6e 65 20 74 acro to define t
e2610 68 65 20 73 74 61 74 69 63 20 63 6f 6e 74 65 6e he static conten
e2620 74 73 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 ts of an sqlite3
e2630 5f 76 66 73 20 73 74 72 75 63 74 75 72 65 20 66 _vfs structure f
e2640 6f 72 0a 20 20 2a 2a 20 74 68 65 20 75 6e 69 78 or. ** the unix
e2650 20 62 61 63 6b 65 6e 64 2e 20 54 68 65 20 74 77 backend. The tw
e2660 6f 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 o parameters are
e2670 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 75 the values to u
e2680 73 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 se for. ** the
e2690 73 71 6c 69 74 65 33 5f 76 66 73 2e 7a 4e 61 6d sqlite3_vfs.zNam
e26a0 65 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 66 e and sqlite3_vf
e26b0 73 2e 70 41 70 70 44 61 74 61 20 66 69 65 6c 64 s.pAppData field
e26c0 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e s, respectively.
e26d0 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 23 64 . ** . */. #d
e26e0 65 66 69 6e 65 20 55 4e 49 58 56 46 53 28 7a 56 efine UNIXVFS(zV
e26f0 66 73 4e 61 6d 65 2c 20 70 56 66 73 41 70 70 44 fsName, pVfsAppD
e2700 61 74 61 29 20 7b 20 20 20 20 20 20 20 20 20 20 ata) {
e2710 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 31 2c \. 1,
e2720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2730 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 /* iVersion
e2740 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e2750 20 20 20 20 20 20 5c 0a 20 20 20 20 73 69 7a 65 \. size
e2760 6f 66 28 75 6e 69 78 46 69 6c 65 29 2c 20 20 20 of(unixFile),
e2770 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f /* szOsFile */
e2780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2790 20 20 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50 41 \. MAX_PA
e27a0 54 48 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20 THNAME,
e27b0 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f /* mxPathname */
e27c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e27d0 20 20 5c 0a 20 20 20 20 30 2c 20 20 20 20 20 20 \. 0,
e27e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e27f0 20 70 4e 65 78 74 20 2a 2f 20 20 20 20 20 20 20 pNext */
e2800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2810 5c 0a 20 20 20 20 7a 56 66 73 4e 61 6d 65 2c 20 \. zVfsName,
e2820 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a /* z
e2830 4e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 Name */
e2840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e2850 20 20 20 20 28 76 6f 69 64 20 2a 29 70 56 66 73 (void *)pVfs
e2860 41 70 70 44 61 74 61 2c 20 20 2f 2a 20 70 41 70 AppData, /* pAp
e2870 70 44 61 74 61 20 2a 2f 20 20 20 20 20 20 20 20 pData */
e2880 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e2890 20 20 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20 20 unixOpen,
e28a0 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e /* xOpen
e28b0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e28c0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
e28d0 75 6e 69 78 44 65 6c 65 74 65 2c 20 20 20 20 20 unixDelete,
e28e0 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 /* xDelete
e28f0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e2900 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
e2910 69 78 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 ixAccess,
e2920 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a /* xAccess *
e2930 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e2940 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
e2950 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 FullPathname,
e2960 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 /* xFullPathna
e2970 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 me */
e2980 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c \. unixDl
e2990 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 Open,
e29a0 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20 20 /* xDlOpen */
e29b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e29c0 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45 72 \. unixDlEr
e29d0 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ror, /*
e29e0 20 78 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20 20 xDlError */
e29f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2a00 5c 0a 20 20 20 20 75 6e 69 78 44 6c 53 79 6d 2c \. unixDlSym,
e2a10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e2a20 44 6c 53 79 6d 20 2a 2f 20 20 20 20 20 20 20 20 DlSym */
e2a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e2a40 20 20 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65 2c unixDlClose,
e2a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
e2a60 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20 Close */
e2a70 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e2a80 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 unixRandomness
e2a90 2c 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 , /* xRand
e2aa0 6f 6d 6e 65 73 73 20 2a 2f 20 20 20 20 20 20 20 omness */
e2ab0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
e2ac0 75 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20 20 unixSleep,
e2ad0 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 /* xSleep
e2ae0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e2af0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
e2b00 69 78 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 ixCurrentTime,
e2b10 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 /* xCurrentT
e2b20 69 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ime */
e2b30 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
e2b40 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 GetLastError
e2b50 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 /* xGetLastErr
e2b60 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 or */
e2b70 20 20 20 20 5c 0a 20 20 7d 0a 0a 20 20 73 74 61 \. }.. sta
e2b80 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 tic sqlite3_vfs
e2b90 75 6e 69 78 56 66 73 20 3d 20 55 4e 49 58 56 46 unixVfs = UNIXVF
e2ba0 53 28 22 75 6e 69 78 22 2c 20 30 29 3b 0a 23 69 S("unix", 0);.#i
e2bb0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
e2bc0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 LOCKING_STYLE.
e2bd0 69 6e 74 20 69 3b 0a 20 20 73 74 61 74 69 63 20 int i;. static
e2be0 73 71 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 sqlite3_vfs aVfs
e2bf0 5b 5d 20 3d 20 7b 0a 20 20 20 20 55 4e 49 58 56 [] = {. UNIXV
e2c00 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 22 2c FS("unix-posix",
e2c10 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LOCKING_STYLE
e2c20 5f 50 4f 53 49 58 29 2c 20 0a 20 20 20 20 55 4e _POSIX), . UN
e2c30 49 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 IXVFS("unix-afp"
e2c40 2c 20 20 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 , LOCKING_ST
e2c50 59 4c 45 5f 41 46 50 29 2c 20 0a 20 20 20 20 55 YLE_AFP), . U
e2c60 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 66 6c 6f NIXVFS("unix-flo
e2c70 63 6b 22 2c 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 ck", LOCKING_S
e2c80 54 59 4c 45 5f 46 4c 4f 43 4b 29 2c 20 0a 20 20 TYLE_FLOCK), .
e2c90 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d UNIXVFS("unix-
e2ca0 64 6f 74 66 69 6c 65 22 2c 20 4c 4f 43 4b 49 4e dotfile", LOCKIN
e2cb0 47 5f 53 54 59 4c 45 5f 44 4f 54 46 49 4c 45 29 G_STYLE_DOTFILE)
e2cc0 2c 20 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 , . UNIXVFS("
e2cd0 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20 20 20 4c unix-none", L
e2ce0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e OCKING_STYLE_NON
e2cf0 45 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 E),. UNIXVFS(
e2d00 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22 2c "unix-namedsem",
e2d10 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 41 LOCKING_STYLE_NA
e2d20 4d 45 44 53 45 4d 29 2c 0a 20 20 7d 3b 0a 20 20 MEDSEM),. };.
e2d30 66 6f 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65 for(i=0; i<(size
e2d40 6f 66 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28 of(aVfs)/sizeof(
e2d50 73 71 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69 sqlite3_vfs)); i
e2d60 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
e2d70 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 61 _vfs_register(&a
e2d80 56 66 73 5b 69 5d 2c 20 30 29 3b 0a 20 20 7d 0a Vfs[i], 0);. }.
e2d90 23 65 6e 64 69 66 0a 23 69 66 20 49 53 5f 56 58 #endif.#if IS_VX
e2da0 57 4f 52 4b 53 0a 20 20 73 71 6c 69 74 65 33 48 WORKS. sqlite3H
e2db0 61 73 68 49 6e 69 74 28 26 6e 61 6d 65 48 61 73 ashInit(&nameHas
e2dc0 68 2c 20 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20 h, 1);.#endif.
e2dd0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
e2de0 73 74 65 72 28 26 75 6e 69 78 56 66 73 2c 20 31 ster(&unixVfs, 1
e2df0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
e2e00 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a TE_OK; .}../*.**
e2e10 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6f 70 Shutdown the op
e2e20 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
e2e30 6e 74 65 72 66 61 63 65 2e 20 54 68 69 73 20 69 nterface. This i
e2e40 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e s a no-op for un
e2e50 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ix..*/.SQLITE_AP
e2e60 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 I int sqlite3_os
e2e70 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 _end(void){ . r
e2e80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e2e90 20 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 .}. .#endif /*
e2ea0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a SQLITE_OS_UNIX *
e2eb0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
e2ec0 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 ** End of os_uni
e2ed0 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.c ************
e2ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2f00 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
e2f10 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 ** Begin file os
e2f20 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _win.c *********
e2f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e2f50 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 */./*.** 2004 Ma
e2f60 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 22.**.** The a
e2f70 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
e2f80 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
e2f90 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
e2fa0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
e2fb0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
e2fc0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
e2fd0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
e2fe0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
e2ff0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
e3000 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
e3010 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
e3020 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
e3030 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
e3040 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
e3050 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
e3060 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
e3070 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
e3080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e30a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e30b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e30c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
e30d0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
e30e0 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 s code that is s
e30f0 70 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64 6f pecific to windo
e3100 77 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f ws..**.** $Id: o
e3110 73 5f 77 69 6e 2e 63 2c 76 20 31 2e 31 34 30 20 s_win.c,v 1.140
e3120 32 30 30 38 2f 31 31 2f 31 39 20 32 31 3a 33 35 2008/11/19 21:35
e3130 3a 34 37 20 73 68 61 6e 65 20 45 78 70 20 24 0a :47 shane Exp $.
e3140 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
e3150 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 _WIN
e3160 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 /* This file
e3170 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e 64 is used for wind
e3180 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a ows only */.../*
e3190 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 .** A Note About
e31a0 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 Memory Allocati
e31b0 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 on:.**.** This d
e31c0 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f river uses mallo
e31d0 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 63 c()/free() direc
e31e0 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e 20 tly rather than
e31f0 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a going through.**
e3200 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 70 the SQLite-wrap
e3210 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c pers sqlite3_mal
e3220 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 loc()/sqlite3_fr
e3230 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 61 ee(). Those wra
e3240 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 ppers.** are des
e3250 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f 6e igned for use on
e3260 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
e3270 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 s where memory i
e3280 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 s scarce and.**
e3290 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 malloc failures
e32a0 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 6c happen frequentl
e32b0 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 6e y. Win32 does n
e32c0 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 6e ot typically run
e32d0 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 on.** embedded
e32e0 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 65 systems, and whe
e32f0 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 65 n it does the de
e3300 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c velopers normall
e3310 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a 2a y have bigger.**
e3320 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72 problems to wor
e3330 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 75 ry about than ru
e3340 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d nning out of mem
e3350 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 69 ory. So there i
e3360 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 s not.** a compe
e3370 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 73 lling need to us
e3380 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 0a e the wrappers..
e3390 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 20 **.** But there
e33a0 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e is a good reason
e33b0 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20 to not use the
e33c0 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 65 wrappers. If we
e33d0 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 70 use the.** wrap
e33e0 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 6c pers then we wil
e33f0 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 20 l get simulated
e3400 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 malloc() failure
e3410 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a s within this.**
e3420 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 68 driver. And th
e3430 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b 69 at causes all ki
e3440 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 nds of problems
e3450 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 20 for our tests.
e3460 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61 We.** could enha
e3470 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 65 nce SQLite to de
e3480 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 65 al with simulate
e3490 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 d malloc failure
e34a0 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 s within.** the
e34b0 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 74 OS driver, but t
e34c0 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c 20 he code to deal
e34d0 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c 75 with those failu
e34e0 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 re would not.**
e34f0 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e 20 be exercised on
e3500 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f 65 Linux (which doe
e3510 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 s not need to ma
e3520 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 72 lloc() in the dr
e3530 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 iver).** and so
e3540 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 69 we would have di
e3550 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e 67 fficulty writing
e3560 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 20 coverage tests
e3570 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 for that.** code
e3580 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 61 . Better to lea
e3590 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 2c ve the code out,
e35a0 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a we think..**.**
e35b0 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 The point of th
e35c0 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 73 is discussion is
e35d0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 as follows: Wh
e35e0 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 en creating a ne
e35f0 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f w.** OS layer fo
e3600 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 79 r an embedded sy
e3610 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 65 stem, if you use
e3620 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 6e this file as an
e3630 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f example,.** avo
e3640 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d 61 id the use of ma
e3650 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 20 lloc()/free().
e3660 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 77 Those routines w
e3670 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f 77 ork ok on window
e3680 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 s.** desktops bu
e3690 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e t not so well in
e36a0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
e36b0 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 s..*/..#include
e36c0 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 66 <winbase.h>..#if
e36d0 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a 23 def __CYGWIN__.#
e36e0 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 79 include <sys/cy
e36f0 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a gwin.h>.#endif..
e3700 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 /*.** Macros use
e3710 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 d to determine w
e3720 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f hether or not to
e3730 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f use threads..*/
e3740 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52 .#if defined(THR
e3750 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 45 EADSAFE) && THRE
e3760 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 ADSAFE.# define
e3770 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 41 SQLITE_W32_THREA
e3780 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a DS 1.#endif../*.
e3790 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 ** Include code
e37a0 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 that is common t
e37b0 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c o all os_*.c fil
e37c0 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a es.*/./*********
e37d0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 ***** Include os
e37e0 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 _common.h in the
e37f0 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 69 middle of os_wi
e3800 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
e3810 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
e3820 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
e3830 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
e3840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3860 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
e3870 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 May 22.**.** Th
e3880 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
e3890 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
e38a0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
e38b0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
e38c0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
e38d0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
e38e0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
e38f0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
e3900 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
e3910 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
e3920 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
e3930 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
e3940 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
e3950 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
e3960 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
e3970 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
e3980 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
e3990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e39a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e39b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e39c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e39d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
e39e0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
e39f0 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 ains macros and
e3a00 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 a little bit of
e3a10 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d code that is com
e3a20 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 mon to.** all of
e3a30 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 the platform-sp
e3a40 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 ecific files (os
e3a50 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e _*.c) and is #in
e3a60 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 cluded into thos
e3a70 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a e.** files..**.*
e3a80 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 * This file shou
e3a90 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 ld be #included
e3aa0 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 by the os_*.c fi
e3ab0 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 les only. It is
e3ac0 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 not a.** genera
e3ad0 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 l purpose header
e3ae0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 file..**.** $Id
e3af0 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 : os_common.h,v
e3b00 31 2e 33 37 20 32 30 30 38 2f 30 35 2f 32 39 20 1.37 2008/05/29
e3b10 32 30 3a 32 32 3a 33 37 20 73 68 61 6e 65 20 45 20:22:37 shane E
e3b20 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 xp $.*/.#ifndef
e3b30 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 _OS_COMMON_H_.#d
e3b40 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e efine _OS_COMMON
e3b50 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 _H_../*.** At le
e3b60 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 ast two bugs hav
e3b70 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 e slipped in bec
e3b80 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 ause we changed
e3b90 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 the MEMORY_DEBUG
e3ba0 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c .** macro to SQL
e3bb0 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f ITE_DEBUG and so
e3bc0 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c me older makefil
e3bd0 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 es have not yet
e3be0 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 made the.** swit
e3bf0 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ch. The followi
e3c00 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 ng code should c
e3c10 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 atch this proble
e3c20 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d m at compile-tim
e3c30 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d e..*/.#ifdef MEM
e3c40 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f ORY_DEBUG.# erro
e3c50 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 r "The MEMORY_DE
e3c60 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 BUG macro is obs
e3c70 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 olete. Use SQLI
e3c80 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 TE_DEBUG instead
e3c90 2e 22 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 .".#endif.../*.
e3ca0 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 * When testing,
e3cb0 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 this global vari
e3cc0 61 62 6c 65 20 73 74 6f 72 65 73 20 74 68 65 20 able stores the
e3cd0 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a location of the.
e3ce0 20 2a 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 * pending-byte
e3cf0 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
e3d00 66 69 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66 file.. */.#ifdef
e3d10 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
e3d20 49 54 45 5f 41 50 49 20 75 6e 73 69 67 6e 65 64 ITE_API unsigned
e3d30 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 65 6e int sqlite3_pen
e3d40 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78 34 30 ding_byte = 0x40
e3d50 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 0a 000000;.#endif..
e3d60 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
e3d70 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
e3d80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 TE int sqlite3OS
e3d90 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 Trace = 0;.#defi
e3da0 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 ne OSTRACE1(X)
e3db0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
e3dc0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
e3dd0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
e3de0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
e3df0 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 E2(X,Y) if
e3e00 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
e3e10 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
e3e20 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 rintf(X,Y).#defi
e3e30 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c ne OSTRACE3(X,Y,
e3e40 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 Z) if( sqlit
e3e50 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
e3e60 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
e3e70 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Y,Z).#define OS
e3e80 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 TRACE4(X,Y,Z,A)
e3e90 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 if( sqlite3OST
e3ea0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
e3eb0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
e3ec0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 A).#define OSTRA
e3ed0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 CE5(X,Y,Z,A,B) i
e3ee0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
e3ef0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
e3f00 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
e3f10 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
e3f20 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 E6(X,Y,Z,A,B,C)
e3f30 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 \. if(sqlite3
e3f40 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 OSTrace) sqlite3
e3f50 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
e3f60 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
e3f70 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
e3f80 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 A,B,C,D) \. i
e3f90 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
e3fa0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
e3fb0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
e3fc0 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e ,D).#else.#defin
e3fd0 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 e OSTRACE1(X).#d
e3fe0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 efine OSTRACE2(X
e3ff0 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
e4000 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 ACE3(X,Y,Z).#def
e4010 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 ine OSTRACE4(X,Y
e4020 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Z,A).#define OS
e4030 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE5(X,Y,Z,A,B
e4040 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
e4050 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a E6(X,Y,Z,A,B,C).
e4060 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 #define OSTRACE7
e4070 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a (X,Y,Z,A,B,C,D).
e4080 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 #endif../*.** Ma
e4090 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d cros for perform
e40a0 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e ance tracing. N
e40b0 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f ormally turned o
e40c0 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a ff. Only works.
e40d0 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 ** on i486 hardw
e40e0 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 are..*/.#ifdef S
e40f0 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 QLITE_PERFORMANC
e4100 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 E_TRACE../* .**
e4110 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e hwtime.h contain
e4120 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c s inline assembl
e4130 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c er code for impl
e4140 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 ementing .** hig
e4150 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 h-performance ti
e4160 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a ming routines..*
e4170 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
e4180 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 * Include hwtime
e4190 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
e41a0 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 of os_common.h
e41b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e41c0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
e41d0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 * Begin file hwt
e41e0 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ime.h **********
e41f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4210 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 /./*.** 2008 May
e4220 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 27.**.** The au
e4230 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
e4240 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
e4250 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
e4260 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
e4270 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
e4280 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
e4290 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
e42a0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
e42b0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
e42c0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
e42d0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
e42e0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
e42f0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
e4300 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
e4310 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
e4320 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
e4330 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
e4340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4380 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
e4390 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
e43a0 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 inline asm code
e43b0 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 for retrieving
e43c0 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 "high-performanc
e43d0 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 e".** counters f
e43e0 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 or x86 class CPU
e43f0 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 s..**.** $Id: hw
e4400 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 time.h,v 1.3 200
e4410 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 8/08/01 14:33:15
e4420 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a shane Exp $.*/.
e4430 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f #ifndef _HWTIME_
e4440 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 H_.#define _HWTI
e4450 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ME_H_../*.** The
e4460 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
e4470 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ne only works on
e4480 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 pentium-class (
e4490 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 or newer) proces
e44a0 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 sors..** It uses
e44b0 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 the RDTSC opcod
e44c0 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 e to read the cy
e44d0 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 cle count value
e44e0 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 out of the.** pr
e44f0 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 ocessor and retu
e4500 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 rns that value.
e4510 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 This can be use
e4520 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a d for high-res.*
e4530 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a * profiling..*/.
e4540 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 #if (defined(__G
e4550 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 NUC__) || define
e4560 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 d(_MSC_VER)) &&
e4570 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 \. (defined
e4580 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 (i386) || define
e4590 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 d(__i386__) || d
e45a0 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 efined(_M_IX86))
e45b0 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 .. #if defined(
e45c0 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 __GNUC__).. __i
e45d0 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
e45e0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
e45f0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
e4600 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c unsigned int lo,
e4610 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f hi;. __asm_
e4620 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
e4630 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 "rdtsc" : "=a" (
e4640 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b lo), "=d" (hi));
e4650 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 . return (sq
e4660 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c lite_uint64)hi <
e4670 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a < 32 | lo;. }..
e4680 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 #elif defined(
e4690 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 _MSC_VER).. __d
e46a0 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f eclspec(naked) _
e46b0 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 _inline sqlite_u
e46c0 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 int64 __cdecl sq
e46d0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
e46e0 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a ){. __asm {.
e46f0 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 rdtsc.
e4700 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 ret
e4710 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 ; return value a
e4720 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d t EDX:EAX. }
e4730 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a . }.. #endif..
e4740 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f #elif (defined(_
e4750 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 _GNUC__) && defi
e4760 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 ned(__x86_64__))
e4770 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
e4780 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
e4790 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
e47a0 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
e47b0 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 long val;.
e47c0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
e47d0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
e47e0 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 "=A" (val));.
e47f0 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a return val;.
e4800 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 }. .#elif (def
e4810 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 ined(__GNUC__) &
e4820 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f & defined(__ppc_
e4830 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f _)).. __inline_
e4840 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
e4850 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
e4860 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 id){. unsig
e4870 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 ned long long re
e4880 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 tval;. unsi
e4890 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a gned long junk;.
e48a0 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
e48b0 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c volatile__ ("\n\
e48c0 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 . 1:
e48d0 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c mftbu %1\n\
e48e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
e48f0 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e mftb %L0\n
e4900 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
e4910 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e mftbu %0\n
e4920 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
e4930 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 cmpw %0,%
e4940 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
e4950 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 bne 1
e4960 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b".
e4970 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 : "=r" (ret
e4980 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b val), "=r" (junk
e4990 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ));. return
e49a0 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 retval;. }..#e
e49b0 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 lse.. #error Ne
e49c0 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ed implementatio
e49d0 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 n of sqlite3Hwti
e49e0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
e49f0 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 atform... /*.
e4a00 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 ** To compile wi
e4a10 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 thout implementi
e4a20 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 ng sqlite3Hwtime
e4a30 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
e4a40 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 form,. ** you c
e4a50 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 an remove the ab
e4a60 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 ove #error and u
e4a70 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 se the following
e4a80 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 . ** stub funct
e4a90 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c ion. You will l
e4aa0 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f ose timing suppo
e4ab0 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a rt for many. **
e4ac0 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e of the debuggin
e4ad0 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 g and testing ut
e4ae0 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 ilities, but it
e4af0 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c should at. ** l
e4b00 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 east compile and
e4b10 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 run.. */.SQLIT
e4b20 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
e4b30 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
e4b40 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 3Hwtime(void){ r
e4b50 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 eturn ((sqlite_u
e4b60 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e int64)0); }..#en
e4b70 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
e4b80 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f defined(_HWTIME_
e4b90 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a H_) */../*******
e4ba0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 ******* End of h
e4bb0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
e4bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4be0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
e4bf0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
e4c00 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
e4c10 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f off in os_commo
e4c20 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
e4c30 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 ******/..static
e4c40 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f sqlite_uint64 g_
e4c50 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 start;.static sq
e4c60 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c lite_uint64 g_el
e4c70 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 apsed;.#define T
e4c80 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 IMER_START
e4c90 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 g_start=sqlite3
e4ca0 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 Hwtime().#define
e4cb0 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 TIMER_END
e4cc0 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c g_elapsed=sql
e4cd0 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 ite3Hwtime()-g_s
e4ce0 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d tart.#define TIM
e4cf0 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 ER_ELAPSED g
e4d00 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 _elapsed.#else.#
e4d10 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 define TIMER_STA
e4d20 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 RT.#define TIMER
e4d30 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d _END.#define TIM
e4d40 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 ER_ELAPSED (
e4d50 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
e4d60 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
e4d70 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 If we compile wi
e4d80 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 th the SQLITE_TE
e4d90 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 ST macro set, th
e4da0 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 en the following
e4db0 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 block.** of cod
e4dc0 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 e will give us t
e4dd0 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 he ability to si
e4de0 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f mulate a disk I/
e4df0 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a O error. This.*
e4e00 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 * is used for te
e4e10 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 sting the I/O re
e4e20 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f covery logic..*/
e4e30 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
e4e40 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
e4e50 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
e4e60 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 ror_hit = 0;
e4e70 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
e4e80 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 number of I/O E
e4e90 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f rrors */.SQLITE_
e4ea0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
e4eb0 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
e4ec0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
e4ed0 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 Number of non-be
e4ee0 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 nign errors */.S
e4ef0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e4f00 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
e4f10 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 ending = 0;
e4f20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e /* Count down
e4f30 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 to first I/O er
e4f40 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ror */.SQLITE_AP
e4f50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
e4f60 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d _error_persist =
e4f70 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
e4f80 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 ue if I/O errors
e4f90 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 persist */.SQLI
e4fa0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
e4fb0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 e3_io_error_beni
e4fc0 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 gn = 0;
e4fd0 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 /* True if error
e4fe0 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a s are benign */.
e4ff0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
e5000 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
e5010 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c pending = 0;.SQL
e5020 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
e5030 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 te3_diskfull = 0
e5040 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ;.#define Simula
e5050 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
e5060 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 X) sqlite3_io_er
e5070 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 ror_benign=(X).#
e5080 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
e5090 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a OError(CODE) \.
e50a0 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 if( (sqlite3_i
e50b0 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 o_error_persist
e50c0 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 && sqlite3_io_er
e50d0 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 ror_hit) \.
e50e0 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f || sqlite3_io_
e50f0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 error_pending--
e5100 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 == 1 ) \.
e5110 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f { local_
e5120 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d ioerr(); CODE; }
e5130 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 .static void loc
e5140 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f al_ioerr(){. IO
e5150 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 TRACE(("IOERR\n"
e5160 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f ));. sqlite3_io
e5170 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 _error_hit++;.
e5180 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f if( !sqlite3_io_
e5190 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 error_benign ) s
e51a0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
e51b0 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 hardhit++;.}.#de
e51c0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 fine SimulateDis
e51d0 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 kfullError(CODE)
e51e0 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 \. if( sqlite
e51f0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
e5200 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 ng ){ \. if(
e5210 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
e5220 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 l_pending == 1 )
e5230 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c { \. local
e5240 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 _ioerr(); \.
e5250 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 sqlite3_diskf
e5260 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 ull = 1; \.
e5270 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 sqlite3_io_err
e5280 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 or_hit = 1; \.
e5290 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 CODE; \.
e52a0 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 }else{ \.
e52b0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 sqlite3_diskfu
e52c0 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a ll_pending--; \.
e52d0 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 } \. }.#e
e52e0 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 lse.#define Simu
e52f0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
e5300 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d n(X).#define Sim
e5310 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a ulateIOError(A).
e5320 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
e5330 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 DiskfullError(A)
e5340 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
e5350 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 hen testing, kee
e5360 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 p a count of the
e5370 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 number of open
e5380 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 files..*/.#ifdef
e5390 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
e53a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
e53b0 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f te3_open_file_co
e53c0 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 unt = 0;.#define
e53d0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 OpenCounter(X)
e53e0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 sqlite3_open_fi
e53f0 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 le_count+=(X).#e
e5400 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e lse.#define Open
e5410 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 Counter(X).#endi
e5420 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
e5430 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e fined(_OS_COMMON
e5440 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _H_) */../******
e5450 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
e5460 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
e5470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e5480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e5490 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
e54a0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
e54b0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
e54c0 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e t off in os_win.
e54d0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
e54e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
e54f0 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 Some microsoft c
e5500 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 ompilers lack th
e5510 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a is definition..*
e5520 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 /.#ifndef INVALI
e5530 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 D_FILE_ATTRIBUTE
e5540 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c S.# define INVAL
e5550 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 ID_FILE_ATTRIBUT
e5560 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29 20 0a ES ((DWORD)-1) .
e5570 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 #endif../*.** De
e5580 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72 termine if we ar
e5590 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57 e dealing with W
e55a0 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68 indowsCE - which
e55b0 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 has a much.** r
e55c0 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 educed API..*/.#
e55d0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
e55e0 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72 65 46 CE.# define AreF
e55f0 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 31 0a ileApisANSI() 1.
e5600 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 #endif../*.** Wi
e5610 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76 65 nCE lacks native
e5620 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69 6c support for fil
e5630 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65 20 e locking so we
e5640 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74 0a have to fake it.
e5650 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f 64 ** with some cod
e5660 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f e of our own..*/
e5670 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
e5680 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74 72 INCE.typedef str
e5690 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a uct winceLock {.
e56a0 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b 20 int nReaders;
e56b0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
e56c0 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73 20 of reader locks
e56d0 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f obtained */. BO
e56e0 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20 20 OL bPending;
e56f0 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61 /* Indicates a
e5700 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 pending lock ha
e5710 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 s been obtained
e5720 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65 72 */. BOOL bReser
e5730 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64 69 ved; /* Indi
e5740 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65 64 cates a reserved
e5750 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f lock has been o
e5760 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f btained */. BOO
e5770 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20 20 L bExclusive;
e5780 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61 6e /* Indicates an
e5790 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
e57a0 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 has been obtaine
e57b0 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b d */.} winceLock
e57c0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
e57d0 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72 75 The winFile stru
e57e0 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63 6c cture is a subcl
e57f0 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 ass of sqlite3_f
e5800 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74 6f ile* specific to
e5810 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70 6f the win32.** po
e5820 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72 2e rtability layer.
e5830 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
e5840 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46 69 ct winFile winFi
e5850 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46 69 le;.struct winFi
e5860 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c le {. const sql
e5870 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
e5880 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 *pMethod;/* Must
e5890 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 48 be first */. H
e58a0 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20 20 ANDLE h;
e58b0 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 /* Handle
e58c0 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 for accessing t
e58d0 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 he file */. uns
e58e0 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 igned char lockt
e58f0 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66 20 ype; /* Type of
e5900 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 lock currently h
e5910 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 eld on this file
e5920 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61 72 */. short shar
e5930 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f 2a edLockByte; /*
e5940 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e Randomly chosen
e5950 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61 20 byte used as a
e5960 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 23 shared lock */.#
e5970 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
e5980 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c CE. WCHAR *zDel
e5990 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 eteOnClose; /*
e59a0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
e59b0 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73 delete when clos
e59c0 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 ing */. HANDLE
e59d0 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 hMutex;
e59e0 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74 /* Mutex used t
e59f0 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
e5a00 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 to shared lock
e5a10 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53 */ . HANDLE hS
e5a20 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f hared; /
e5a30 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * Shared memory
e5a40 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72 segment used for
e5a50 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 locking */. wi
e5a60 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 nceLock local;
e5a70 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f /* Locks o
e5a80 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 btained by this
e5a90 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46 instance of winF
e5aa0 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f ile */. winceLo
e5ab0 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20 ck *shared;
e5ac0 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65 /* Global share
e5ad0 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f d lock memory fo
e5ae0 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23 r the file */.#
e5af0 65 6e 64 69 66 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a endif.};.../*.**
e5b00 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 The following v
e5b10 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72 6d ariable is (norm
e5b20 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20 61 ally) set once a
e5b30 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65 73 nd never changes
e5b40 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e 20 .** thereafter.
e5b50 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65 74 It records whet
e5b60 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69 6e her the operatin
e5b70 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e 39 g system is Win9
e5b80 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 5.** or WinNT..*
e5b90 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61 74 *.** 0: Operat
e5ba0 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e 6f ing system unkno
e5bb0 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65 72 wn..** 1: Oper
e5bc0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 ating system is
e5bd0 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20 4f Win95..** 2: O
e5be0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
e5bf0 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 is WinNT..**.**
e5c00 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63 69 In order to faci
e5c10 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20 6f litate testing o
e5c20 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65 6d n a WinNT system
e5c30 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74 75 , the test fixtu
e5c40 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61 6c re.** can manual
e5c50 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c 75 ly set this valu
e5c60 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61 74 e to 1 to emulat
e5c70 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f 72 e Win98 behavior
e5c80 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
e5c90 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
e5ca0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
e5cb0 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c 73 s_type = 0;.#els
e5cc0 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c e.static int sql
e5cd0 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 ite3_os_type = 0
e5ce0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
e5cf0 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e Return true (non
e5d00 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 -zero) if we are
e5d10 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 running under W
e5d20 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e inNT, Win2K, Win
e5d30 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e XP,.** or WinCE.
e5d40 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 Return false (
e5d50 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c zero) for Win95,
e5d60 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 Win98, or WinME
e5d70 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 ..**.** Here is
e5d80 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f an interesting o
e5d90 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e bservation: Win
e5da0 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 95, Win98, and W
e5db0 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 inME lack.** the
e5dc0 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 LockFileEx() AP
e5dd0 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 I. But we can s
e5de0 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 till statically
e5df0 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 link against tha
e5e00 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 t.** API as long
e5e10 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c as we don't cal
e5e20 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 l it win running
e5e30 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 Win95/98/ME. A
e5e40 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 call to.** this
e5e50 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
e5e60 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 to determine if
e5e70 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e the host is Win
e5e80 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 95/98/ME or.** W
e5e90 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 inNT/2K/XP so th
e5ea0 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 at we will know
e5eb0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 whether or not w
e5ec0 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c e can safely cal
e5ed0 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c l.** the LockFil
e5ee0 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69 eEx() API..*/.#i
e5ef0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
e5f00 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e 54 28 E.# define isNT(
e5f10 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 73 ) (1).#else. s
e5f20 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54 28 76 tatic int isNT(v
e5f30 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20 73 71 oid){. if( sq
e5f40 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 30 lite3_os_type==0
e5f50 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 53 ){. OSVERS
e5f60 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 IONINFO sInfo;.
e5f70 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 sInfo.dwOSV
e5f80 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d ersionInfoSize =
e5f90 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a sizeof(sInfo);.
e5fa0 20 20 20 20 20 20 47 65 74 56 65 72 73 69 6f 6e GetVersion
e5fb0 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 Ex(&sInfo);.
e5fc0 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 sqlite3_os_typ
e5fd0 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74 e = sInfo.dwPlat
e5fe0 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54 formId==VER_PLAT
e5ff0 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20 FORM_WIN32_NT ?
e6000 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 2 : 1;. }.
e6010 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f return sqlite3_
e6020 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a os_type==2;. }.
e6030 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
e6040 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a _OS_WINCE */../*
e6050 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 .** Convert a UT
e6060 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 6d 69 F-8 string to mi
e6070 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 crosoft unicode
e6080 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a 0a 2a (UTF-16?). .**.*
e6090 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 * Space to hold
e60a0 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 the returned str
e60b0 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ing is obtained
e60c0 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a from malloc..*/.
e60d0 73 74 61 74 69 63 20 57 43 48 41 52 20 2a 75 74 static WCHAR *ut
e60e0 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 f8ToUnicode(cons
e60f0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
e6100 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b e){. int nChar;
e6110 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64 65 46 . WCHAR *zWideF
e6120 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 68 61 ilename;.. nCha
e6130 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 r = MultiByteToW
e6140 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 2c ideChar(CP_UTF8,
e6150 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 0, zFilename, -
e6160 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 20 7a 1, NULL, 0);. z
e6170 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20 6d WideFilename = m
e6180 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73 69 7a alloc( nChar*siz
e6190 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d eof(zWideFilenam
e61a0 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a e[0]) );. if( z
e61b0 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 WideFilename==0
e61c0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
e61d0 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20 4d . }. nChar = M
e61e0 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 ultiByteToWideCh
e61f0 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a ar(CP_UTF8, 0, z
e6200 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 57 Filename, -1, zW
e6210 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e 43 68 ideFilename, nCh
e6220 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68 61 72 ar);. if( nChar
e6230 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 ==0 ){. free(
e6240 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 3b 0a zWideFilename);.
e6250 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d zWideFilenam
e6260 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 e = 0;. }. ret
e6270 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d urn zWideFilenam
e6280 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 e;.}../*.** Conv
e6290 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e ert microsoft un
e62a0 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38 2e 20 icode to UTF-8.
e62b0 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
e62c0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 he returned stri
e62d0 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 ng is.** obtaine
e62e0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e d from malloc().
e62f0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 .*/.static char
e6300 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 63 *unicodeToUtf8(c
e6310 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69 64 onst WCHAR *zWid
e6320 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e eFilename){. in
e6330 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20 t nByte;. char
e6340 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e *zFilename;.. n
e6350 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 Byte = WideCharT
e6360 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55 54 oMultiByte(CP_UT
e6370 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 F8, 0, zWideFile
e6380 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 name, -1, 0, 0,
e6390 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 0, 0);. zFilena
e63a0 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 me = malloc( nBy
e63b0 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c te );. if( zFil
e63c0 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 ename==0 ){.
e63d0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
e63e0 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 nByte = WideChar
e63f0 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55 ToMultiByte(CP_U
e6400 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c TF8, 0, zWideFil
e6410 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 ename, -1, zFile
e6420 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 name, nByte,.
e6430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6440 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 0, 0)
e6450 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d ;. if( nByte ==
e6460 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 0 ){. free(z
e6470 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a Filename);. z
e6480 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 Filename = 0;.
e6490 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 }. return zFile
e64a0 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 name;.}../*.** C
e64b0 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69 20 73 onvert an ansi s
e64c0 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73 6f tring to microso
e64d0 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61 73 65 ft unicode, base
e64e0 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72 d on the.** curr
e64f0 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73 65 74 ent codepage set
e6500 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65 20 61 tings for file a
e6510 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61 63 pis..** .** Spac
e6520 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 e to hold the re
e6530 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 turned string is
e6540 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f obtained.** fro
e6550 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74 61 m malloc..*/.sta
e6560 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63 73 54 tic WCHAR *mbcsT
e6570 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74 20 63 oUnicode(const c
e6580 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
e6590 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 . int nByte;.
e65a0 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69 6c 65 WCHAR *zMbcsFile
e65b0 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65 name;. int code
e65c0 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70 page = AreFileAp
e65d0 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43 isANSI() ? CP_AC
e65e0 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 P : CP_OEMCP;..
e65f0 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 nByte = MultiBy
e6600 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 teToWideChar(cod
e6610 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e epage, 0, zFilen
e6620 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 30 29 ame, -1, NULL,0)
e6630 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29 3b 0a *sizeof(WCHAR);.
e6640 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 zMbcsFilename
e6650 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a = malloc( nByte*
e6660 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 6c 65 sizeof(zMbcsFile
e6670 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 name[0]) );. if
e6680 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3d ( zMbcsFilename=
e6690 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
e66a0 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 0;. }. nByte
e66b0 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 = MultiByteToWid
e66c0 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c 20 eChar(codepage,
e66d0 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 0, zFilename, -1
e66e0 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 2c , zMbcsFilename,
e66f0 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 6e nByte);. if( n
e6700 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 66 Byte==0 ){. f
e6710 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e 61 6d ree(zMbcsFilenam
e6720 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46 69 6c e);. zMbcsFil
e6730 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 ename = 0;. }.
e6740 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46 69 6c return zMbcsFil
e6750 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ename;.}../*.**
e6760 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 Convert microsof
e6770 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d 75 6c t unicode to mul
e6780 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 tibyte character
e6790 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64 20 6f string, based o
e67a0 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27 73 20 n the.** user's
e67b0 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e 0a 2a Ansi codepage..*
e67c0 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f *.** Space to ho
e67d0 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 ld the returned
e67e0 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e string is obtain
e67f0 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f ed from.** mallo
e6800 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 c()..*/.static c
e6810 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 4d 62 har *unicodeToMb
e6820 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52 20 2a cs(const WCHAR *
e6830 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a zWideFilename){.
e6840 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 int nByte;. c
e6850 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a har *zFilename;.
e6860 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20 3d int codepage =
e6870 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 AreFileApisANSI
e6880 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 () ? CP_ACP : CP
e6890 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65 _OEMCP;.. nByte
e68a0 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c = WideCharToMul
e68b0 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 65 2c tiByte(codepage,
e68c0 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 0, zWideFilenam
e68d0 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 e, -1, 0, 0, 0,
e68e0 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 20 0);. zFilename
e68f0 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 = malloc( nByte
e6900 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 );. if( zFilena
e6910 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 me==0 ){. ret
e6920 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 urn 0;. }. nBy
e6930 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d te = WideCharToM
e6940 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 ultiByte(codepag
e6950 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e e, 0, zWideFilen
e6960 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 ame, -1, zFilena
e6970 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 20 20 me, nByte,.
e6980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6990 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 3b 0a 0, 0);.
e69a0 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20 30 if( nByte == 0
e69b0 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 46 69 ){. free(zFi
e69c0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 46 69 lename);. zFi
e69d0 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a lename = 0;. }.
e69e0 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 return zFilena
e69f0 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e me;.}../*.** Con
e6a00 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65 20 63 vert multibyte c
e6a10 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 haracter string
e6a20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63 65 to UTF-8. Space
e6a30 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 to hold the.**
e6a40 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
e6a50 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d is obtained from
e6a60 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 malloc()..*/.SQ
e6a70 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 LITE_API char *s
e6a80 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 qlite3_win32_mbc
e6a90 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 74 20 s_to_utf8(const
e6aa0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
e6ab0 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e {. char *zFilen
e6ac0 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48 41 52 ameUtf8;. WCHAR
e6ad0 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a *zTmpWide;.. z
e6ae0 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 54 6f TmpWide = mbcsTo
e6af0 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d Unicode(zFilenam
e6b00 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 e);. if( zTmpWi
e6b10 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 de==0 ){. ret
e6b20 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 urn 0;. }. zFi
e6b30 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e 69 lenameUtf8 = uni
e6b40 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70 57 codeToUtf8(zTmpW
e6b50 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d ide);. free(zTm
e6b60 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e pWide);. return
e6b70 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a zFilenameUtf8;.
e6b80 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
e6b90 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 69 62 UTF-8 to multib
e6ba0 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 74 yte character st
e6bb0 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 6f 20 ring. Space to
e6bc0 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 65 74 hold the .** ret
e6bd0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 urned string is
e6be0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 obtained from ma
e6bf0 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 lloc()..*/.stati
e6c00 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f 4d 62 c char *utf8ToMb
e6c10 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a cs(const char *z
e6c20 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 Filename){. cha
e6c30 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 r *zFilenameMbcs
e6c40 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 ;. WCHAR *zTmpW
e6c50 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 ide;.. zTmpWide
e6c60 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 = utf8ToUnicode
e6c70 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 (zFilename);. i
e6c80 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 f( zTmpWide==0 )
e6c90 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
e6ca0 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 4d }. zFilenameM
e6cb0 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54 6f 4d bcs = unicodeToM
e6cc0 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 bcs(zTmpWide);.
e6cd0 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29 3b free(zTmpWide);
e6ce0 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e . return zFilen
e6cf0 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69 66 20 ameMbcs;.}..#if
e6d00 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
e6d10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
e6d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e6d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
e6d60 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e 74 61 is section conta
e6d70 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57 69 6e ins code for Win
e6d80 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a 0a 2a CE only..*/./*.*
e6d90 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f 65 73 * WindowsCE does
e6da0 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f 63 61 not have a loca
e6db0 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e ltime() function
e6dc0 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61 0a 2a . So create a.*
e6dd0 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a 2a 2f * substitute..*/
e6de0 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f 63 64 .struct tm *__cd
e6df0 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 63 6f ecl localtime(co
e6e00 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29 0a 7b nst time_t *t).{
e6e10 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63 74 . static struct
e6e20 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54 49 4d tm y;. FILETIM
e6e30 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20 53 59 E uTm, lTm;. SY
e6e40 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a 20 20 STEMTIME pTm;.
e6e50 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 36 sqlite3_int64 t6
e6e60 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b 0a 20 4;. t64 = *t;.
e6e70 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20 31 31 t64 = (t64 + 11
e6e80 36 34 34 34 37 33 36 30 30 29 2a 31 30 30 30 30 644473600)*10000
e6e90 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f 77 000;. uTm.dwLow
e6ea0 44 61 74 65 54 69 6d 65 20 3d 20 74 36 34 20 26 DateTime = t64 &
e6eb0 20 30 78 46 46 46 46 46 46 46 46 3b 0a 20 20 75 0xFFFFFFFF;. u
e6ec0 54 6d 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d Tm.dwHighDateTim
e6ed0 65 3d 20 74 36 34 20 3e 3e 20 33 32 3b 0a 20 20 e= t64 >> 32;.
e6ee0 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 61 6c 46 FileTimeToLocalF
e6ef0 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c 26 6c 54 ileTime(&uTm,&lT
e6f00 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f m);. FileTimeTo
e6f10 53 79 73 74 65 6d 54 69 6d 65 28 26 6c 54 6d 2c SystemTime(&lTm,
e6f20 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d 5f 79 65 &pTm);. y.tm_ye
e6f30 61 72 20 3d 20 70 54 6d 2e 77 59 65 61 72 20 2d ar = pTm.wYear -
e6f40 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d 5f 6d 6f 1900;. y.tm_mo
e6f50 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 68 20 2d n = pTm.wMonth -
e6f60 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64 61 79 20 1;. y.tm_wday
e6f70 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 65 65 6b = pTm.wDayOfWeek
e6f80 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 20 3d 20 ;. y.tm_mday =
e6f90 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79 2e 74 6d pTm.wDay;. y.tm
e6fa0 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 48 6f 75 _hour = pTm.wHou
e6fb0 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e 20 3d 20 r;. y.tm_min =
e6fc0 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a 20 20 79 pTm.wMinute;. y
e6fd0 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d 2e 77 53 .tm_sec = pTm.wS
e6fe0 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75 72 6e 20 econd;. return
e6ff0 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 77 &y;.}../* This w
e7000 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 63 61 6c ill never be cal
e7010 6c 65 64 2c 20 62 75 74 20 64 65 66 69 6e 65 64 led, but defined
e7020 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 63 6f 64 to make the cod
e7030 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a 23 64 65 e compile */.#de
e7040 66 69 6e 65 20 47 65 74 54 65 6d 70 50 61 74 68 fine GetTempPath
e7050 41 28 61 2c 62 29 0a 0a 23 64 65 66 69 6e 65 20 A(a,b)..#define
e7060 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 LockFile(a,b,c,d
e7070 2c 65 29 20 20 20 20 20 20 20 77 69 6e 63 65 4c ,e) winceL
e7080 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 ockFile(&a, b, c
e7090 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 , d, e).#define
e70a0 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63 UnlockFile(a,b,c
e70b0 2c 64 2c 65 29 20 20 20 20 20 77 69 6e 63 65 55 ,d,e) winceU
e70c0 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c nlockFile(&a, b,
e70d0 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 6e c, d, e).#defin
e70e0 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 61 2c 62 e LockFileEx(a,b
e70f0 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77 69 6e 63 ,c,d,e,f) winc
e7100 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26 61 2c 20 eLockFileEx(&a,
e7110 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66 29 0a 0a b, c, d, e, f)..
e7120 23 64 65 66 69 6e 65 20 48 41 4e 44 4c 45 5f 54 #define HANDLE_T
e7130 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20 28 77 69 O_WINFILE(a) (wi
e7140 6e 46 69 6c 65 2a 29 26 28 28 63 68 61 72 2a 29 nFile*)&((char*)
e7150 61 29 5b 2d 6f 66 66 73 65 74 6f 66 28 77 69 6e a)[-offsetof(win
e7160 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 File,h)]../*.**
e7170 41 63 71 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f Acquire a lock o
e7180 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a n the handle h.*
e7190 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 /.static void wi
e71a0 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 nceMutexAcquire(
e71b0 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 HANDLE h){. DW
e71c0 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 20 64 6f ORD dwErr;. do
e71d0 20 7b 0a 20 20 20 20 20 64 77 45 72 72 20 3d 20 {. dwErr =
e71e0 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a WaitForSingleObj
e71f0 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54 45 29 ect(h, INFINITE)
e7200 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28 64 77 ;. } while (dw
e7210 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45 Err != WAIT_OBJE
e7220 43 54 5f 30 20 26 26 20 64 77 45 72 72 20 21 3d CT_0 && dwErr !=
e7230 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29 WAIT_ABANDONED)
e7240 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 ;.}./*.** Releas
e7250 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 72 65 e a lock acquire
e7260 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 78 41 d by winceMutexA
e7270 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66 cquire().*/.#def
e7280 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78 52 65 ine winceMutexRe
e7290 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61 73 65 lease(h) Release
e72a0 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20 Mutex(h)../*.**
e72b0 43 72 65 61 74 65 20 74 68 65 20 6d 75 74 65 78 Create the mutex
e72c0 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 6d 6f and shared memo
e72d0 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b ry used for lock
e72e0 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c 65 0a ing in the file.
e72f0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 ** descriptor pF
e7300 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f ile.*/.static BO
e7310 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65 4c 6f OL winceCreateLo
e7320 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a ck(const char *z
e7330 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c Filename, winFil
e7340 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 57 43 48 e *pFile){. WCH
e7350 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41 AR *zTok;. WCHA
e7360 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54 R *zName = utf8T
e7370 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 oUnicode(zFilena
e7380 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69 me);. BOOL bIni
e7390 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20 t = TRUE;.. /*
e73a0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6c Initialize the l
e73b0 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f ocal lockdata */
e73c0 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70 . ZeroMemory(&p
e73d0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a File->local, siz
e73e0 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c eof(pFile->local
e73f0 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 ));.. /* Replac
e7400 65 20 74 68 65 20 62 61 63 6b 73 6c 61 73 68 65 e the backslashe
e7410 73 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e s from the filen
e7420 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63 61 73 ame and lowercas
e7430 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72 e it. ** to der
e7440 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61 6d 65 ive a mutex name
e7450 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68 . */. zTok = Ch
e7460 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b arLowerW(zName);
e7470 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a . for (;*zTok;z
e7480 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28 Tok++){. if (
e7490 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a *zTok == '\\') *
e74a0 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a zTok = '_';. }.
e74b0 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65 . /* Create/ope
e74c0 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d 75 74 65 n the named mute
e74d0 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d x */. pFile->hM
e74e0 75 74 65 78 20 3d 20 43 72 65 61 74 65 4d 75 74 utex = CreateMut
e74f0 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c exW(NULL, FALSE,
e7500 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 zName);. if (!
e7510 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a pFile->hMutex){.
e7520 20 20 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b free(zName);
e7530 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 . return FALS
e7540 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 E;. }.. /* Acq
e7550 75 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62 uire the mutex b
e7560 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 efore continuing
e7570 20 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 */. winceMutex
e7580 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 Acquire(pFile->h
e7590 4d 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 Mutex);. . /*
e75a0 53 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20 Since the names
e75b0 6f 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 of named mutexes
e75c0 2c 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 , semaphores, fi
e75d0 6c 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20 le mappings etc
e75e0 61 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 are . ** case-s
e75f0 65 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61 ensitive, take a
e7600 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74 dvantage of that
e7610 20 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20 by uppercasing
e7620 74 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 the mutex name.
e7630 20 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68 ** and using th
e7640 61 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64 at as the shared
e7650 20 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d filemapping nam
e7660 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 e.. */. CharUp
e7670 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 perW(zName);. p
e7680 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 File->hShared =
e7690 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e CreateFileMappin
e76a0 67 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c gW(INVALID_HANDL
e76b0 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 E_VALUE, NULL,.
e76c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e76d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e76e0 20 20 20 20 20 20 50 41 47 45 5f 52 45 41 44 57 PAGE_READW
e76f0 52 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 RITE, 0, sizeof(
e7700 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 winceLock),.
e7710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e7730 20 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 zName); ..
e7740 2f 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74 68 /* Set a flag th
e7750 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 65 27 at indicates we'
e7760 72 65 20 74 68 65 20 66 69 72 73 74 20 74 6f 20 re the first to
e7770 63 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 create the memor
e7780 79 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 y so it . ** mu
e7790 73 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 st be zero-initi
e77a0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 alized */. if (
e77b0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 3d GetLastError() =
e77c0 3d 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f = ERROR_ALREADY_
e77d0 45 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49 6e EXISTS){. bIn
e77e0 69 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a it = FALSE;. }.
e77f0 0a 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a . free(zName);.
e7800 0a 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63 . /* If we succ
e7810 65 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 eeded in making
e7820 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 the shared memor
e7830 79 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 y handle, map it
e7840 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 . */. if (pFile
e7850 2d 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20 ->hShared){.
e7860 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 pFile->shared =
e7870 28 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 (winceLock*)MapV
e7880 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d iewOfFile(pFile-
e7890 3e 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 20 >hShared, .
e78a0 20 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 FILE_MAP
e78b0 5f 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 _READ|FILE_MAP_W
e78c0 52 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 RITE, 0, 0, size
e78d0 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a of(winceLock));.
e78e0 20 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e /* If mappin
e78f0 67 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 g failed, close
e7900 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 the shared memor
e7910 79 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 y handle and era
e7920 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 se it */. if
e7930 28 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 (!pFile->shared)
e7940 7b 0a 20 20 20 20 20 20 43 6c 6f 73 65 48 61 6e {. CloseHan
e7950 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 dle(pFile->hShar
e7960 65 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 ed);. pFile
e7970 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c ->hShared = NULL
e7980 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
e7990 2a 20 49 66 20 73 68 61 72 65 64 20 6d 65 6d 6f * If shared memo
e79a0 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ry could not be
e79b0 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 6c created, then cl
e79c0 6f 73 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e ose the mutex an
e79d0 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 d fail */. if (
e79e0 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d pFile->hShared =
e79f0 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e = NULL){. win
e7a00 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 ceMutexRelease(p
e7a10 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 File->hMutex);.
e7a20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 CloseHandle(p
e7a30 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 File->hMutex);.
e7a40 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 pFile->hMutex
e7a50 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 = NULL;. ret
e7a60 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20 urn FALSE;. }.
e7a70 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a . /* Initializ
e7a80 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d e the shared mem
e7a90 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 75 70 ory if we're sup
e7aa0 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66 posed to */. if
e7ab0 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a (bInit) {. Z
e7ac0 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d eroMemory(pFile-
e7ad0 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 6f 66 28 >shared, sizeof(
e7ae0 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d winceLock));. }
e7af0 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 .. winceMutexRe
e7b00 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 lease(pFile->hMu
e7b10 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 tex);. return T
e7b20 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 RUE;.}../*.** De
e7b30 73 74 72 6f 79 20 74 68 65 20 70 61 72 74 20 6f stroy the part o
e7b40 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74 20 64 f winFile that d
e7b50 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63 65 20 eals with wince
e7b60 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 locks.*/.static
e7b70 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74 72 6f void winceDestro
e7b80 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 yLock(winFile *p
e7b90 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70 46 69 File){. if (pFi
e7ba0 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 le->hMutex){.
e7bb0 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 65 20 /* Acquire the
e7bc0 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e mutex */. win
e7bd0 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 ceMutexAcquire(p
e7be0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a File->hMutex);..
e7bf0 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f /* The follo
e7c00 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75 wing blocks shou
e7c10 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 73 65 ld probably asse
e7c20 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f 64 65 rt in debug mode
e7c30 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20 20 20 , but they.
e7c40 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e 75 70 are to cleanup
e7c50 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f 63 in case any loc
e7c60 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e ks remained open
e7c70 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c */. if (pFil
e7c80 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 e->local.nReader
e7c90 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d s){. pFile-
e7ca0 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 >shared->nReader
e7cb0 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 s --;. }.
e7cc0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c if (pFile->local
e7cd0 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20 20 20 .bReserved){.
e7ce0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
e7cf0 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 ->bReserved = FA
e7d00 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 LSE;. }. i
e7d10 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
e7d20 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 bPending){.
e7d30 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
e7d40 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 bPending = FALSE
e7d50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 ;. }. if (
e7d60 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 pFile->local.bEx
e7d70 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20 clusive){.
e7d80 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
e7d90 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 Exclusive = FALS
e7da0 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a E;. }.. /*
e7db0 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20 61 6e De-reference an
e7dc0 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 d close our copy
e7dd0 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6d of the shared m
e7de0 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a emory handle */.
e7df0 20 20 20 20 55 6e 6d 61 70 56 69 65 77 4f 66 46 UnmapViewOfF
e7e00 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 ile(pFile->share
e7e10 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e d);. CloseHan
e7e20 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 dle(pFile->hShar
e7e30 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 6e ed);.. /* Don
e7e40 65 20 77 69 74 68 20 74 68 65 20 6d 75 74 65 78 e with the mutex
e7e50 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 */. winceMut
e7e60 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d exRelease(pFile-
e7e70 3e 68 4d 75 74 65 78 29 3b 20 20 20 20 0a 20 20 >hMutex); .
e7e80 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 CloseHandle(pF
e7e90 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 ile->hMutex);.
e7ea0 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 pFile->hMutex
e7eb0 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f = NULL;. }.}../
e7ec0 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 * .** An impleme
e7ed0 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c ntation of the L
e7ee0 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 20 6f 66 ockFile() API of
e7ef0 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e windows for win
e7f00 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f ce.*/.static BOO
e7f10 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 L winceLockFile(
e7f20 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c . HANDLE *phFil
e7f30 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c e,. DWORD dwFil
e7f40 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 eOffsetLow,. DW
e7f50 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 ORD dwFileOffset
e7f60 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e High,. DWORD nN
e7f70 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f umberOfBytesToLo
e7f80 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e ckLow,. DWORD n
e7f90 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
e7fa0 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e ockHigh.){. win
e7fb0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 File *pFile = HA
e7fc0 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 NDLE_TO_WINFILE(
e7fd0 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 phFile);. BOOL
e7fe0 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b bReturn = FALSE;
e7ff0 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e .. if (!pFile->
e8000 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e 20 54 hMutex) return T
e8010 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 RUE;. winceMute
e8020 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e xAcquire(pFile->
e8030 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 57 hMutex);.. /* W
e8040 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 anting an exclus
e8050 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 69 ive lock? */. i
e8060 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c f (dwFileOffsetL
e8070 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 ow == SHARED_FIR
e8080 53 54 0a 20 20 20 20 20 20 20 26 26 20 6e 4e 75 ST. && nNu
e8090 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 mberOfBytesToLoc
e80a0 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 53 kLow == SHARED_S
e80b0 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28 70 46 IZE){. if (pF
e80c0 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 ile->shared->nRe
e80d0 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20 70 46 aders == 0 && pF
e80e0 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 ile->shared->bEx
e80f0 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20 clusive == 0){.
e8100 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
e8110 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 red->bExclusive
e8120 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 20 70 = TRUE;. p
e8130 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 File->local.bExc
e8140 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 lusive = TRUE;.
e8150 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
e8160 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TRUE;. }. }.
e8170 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 61 . /* Want a rea
e8180 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a 2f 0a d-only lock? */.
e8190 20 20 65 6c 73 65 20 69 66 20 28 28 64 77 46 69 else if ((dwFi
e81a0 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53 leOffsetLow >= S
e81b0 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20 HARED_FIRST &&.
e81c0 20 20 20 20 20 20 20 20 20 20 20 64 77 46 69 6c dwFil
e81d0 65 4f 66 66 73 65 74 4c 6f 77 20 3c 20 53 48 41 eOffsetLow < SHA
e81e0 52 45 44 5f 46 49 52 53 54 20 2b 20 53 48 41 52 RED_FIRST + SHAR
e81f0 45 44 5f 53 49 5a 45 29 20 26 26 0a 20 20 20 20 ED_SIZE) &&.
e8200 20 20 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f nNumberO
e8210 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 fBytesToLockLow
e8220 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 == 1){. if (p
e8230 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 File->shared->bE
e8240 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a xclusive == 0){.
e8250 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
e8260 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a al.nReaders ++;.
e8270 20 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d if (pFile-
e8280 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 >local.nReaders
e8290 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20 20 20 70 == 1){. p
e82a0 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 File->shared->nR
e82b0 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 eaders ++;.
e82c0 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e }. bReturn
e82d0 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
e82e0 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 }.. /* Want a
e82f0 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f 20 2a 2f pending lock? */
e8300 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 . else if (dwFi
e8310 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 50 leOffsetLow == P
e8320 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 6e ENDING_BYTE && n
e8330 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
e8340 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 ockLow == 1){.
e8350 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65 6e 64 69 /* If no pendi
e8360 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e ng lock has been
e8370 20 61 63 71 75 69 72 65 64 2c 20 74 68 65 6e 20 acquired, then
e8380 61 63 71 75 69 72 65 20 69 74 20 2a 2f 0a 20 20 acquire it */.
e8390 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 if (pFile->sha
e83a0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 3d red->bPending ==
e83b0 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 0) {. pFil
e83c0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 e->shared->bPend
e83d0 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 ing = TRUE;.
e83e0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 pFile->local.b
e83f0 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a Pending = TRUE;.
e8400 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
e8410 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TRUE;. }. }.
e8420 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 73 65 /* Want a rese
e8430 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 rved lock? */.
e8440 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f else if (dwFileO
e8450 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52 45 53 45 ffsetLow == RESE
e8460 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e 4e 75 RVED_BYTE && nNu
e8470 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 mberOfBytesToLoc
e8480 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 kLow == 1){.
e8490 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 if (pFile->share
e84a0 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 3d 20 d->bReserved ==
e84b0 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 0) {. pFile
e84c0 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 ->shared->bReser
e84d0 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 ved = TRUE;.
e84e0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 pFile->local.b
e84f0 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b Reserved = TRUE;
e8500 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d . bReturn =
e8510 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d TRUE;. }. }
e8520 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 .. winceMutexRe
e8530 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 lease(pFile->hMu
e8540 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 tex);. return b
e8550 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Return;.}../*.**
e8560 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 An implementati
e8570 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c 6f 63 6b on of the Unlock
e8580 46 69 6c 65 20 41 50 49 20 6f 66 20 77 69 6e 64 File API of wind
e8590 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f ows for wince.*/
e85a0 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e .static BOOL win
e85b0 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 0a 20 20 ceUnlockFile(.
e85c0 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a HANDLE *phFile,.
e85d0 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 DWORD dwFileOf
e85e0 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 fsetLow,. DWORD
e85f0 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67 dwFileOffsetHig
e8600 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 h,. DWORD nNumb
e8610 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 erOfBytesToUnloc
e8620 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e kLow,. DWORD nN
e8630 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e umberOfBytesToUn
e8640 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77 69 lockHigh.){. wi
e8650 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48 nFile *pFile = H
e8660 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 ANDLE_TO_WINFILE
e8670 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c (phFile);. BOOL
e8680 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53 45 bReturn = FALSE
e8690 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d ;.. if (!pFile-
e86a0 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e 20 >hMutex) return
e86b0 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 TRUE;. winceMut
e86c0 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d exAcquire(pFile-
e86d0 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 >hMutex);.. /*
e86e0 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65 61 64 Releasing a read
e86f0 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e 20 65 78 er lock or an ex
e8700 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a clusive lock */.
e8710 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 if (dwFileOffs
e8720 65 74 4c 6f 77 20 3e 3d 20 53 48 41 52 45 44 5f etLow >= SHARED_
e8730 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20 20 FIRST &&.
e8740 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
e8750 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b < SHARED_FIRST +
e8760 20 53 48 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 SHARED_SIZE){.
e8770 20 20 20 2f 2a 20 44 69 64 20 77 65 20 68 61 76 /* Did we hav
e8780 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c e an exclusive l
e8790 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 66 20 28 ock? */. if (
e87a0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 pFile->local.bEx
e87b0 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20 clusive){.
e87c0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 pFile->local.bEx
e87d0 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b clusive = FALSE;
e87e0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 . pFile->sh
e87f0 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 ared->bExclusive
e8800 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 = FALSE;.
e8810 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a bReturn = TRUE;.
e8820 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 }.. /* Di
e8830 64 20 77 65 20 6a 75 73 74 20 68 61 76 65 20 61 d we just have a
e8840 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20 2a 2f reader lock? */
e8850 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 70 46 . else if (pF
e8860 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 ile->local.nRead
e8870 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c ers){. pFil
e8880 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 e->local.nReader
e8890 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69 66 20 28 s --;. if (
e88a0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 pFile->local.nRe
e88b0 61 64 65 72 73 20 3d 3d 20 30 29 0a 20 20 20 20 aders == 0).
e88c0 20 20 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c {. pFil
e88d0 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 e->shared->nRead
e88e0 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a ers --;. }.
e88f0 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
e8900 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TRUE;. }. }.
e8910 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 . /* Releasing
e8920 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 2a a pending lock *
e8930 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 /. else if (dwF
e8940 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 ileOffsetLow ==
e8950 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 PENDING_BYTE &&
e8960 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
e8970 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b UnlockLow == 1){
e8980 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
e8990 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b local.bPending){
e89a0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f . pFile->lo
e89b0 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 46 cal.bPending = F
e89c0 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c ALSE;. pFil
e89d0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 e->shared->bPend
e89e0 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 ing = FALSE;.
e89f0 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 bReturn = TRU
e8a00 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f E;. }. }. /
e8a10 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65 * Releasing a re
e8a20 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 served lock */.
e8a30 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 else if (dwFile
e8a40 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52 45 53 OffsetLow == RES
e8a50 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e 4e ERVED_BYTE && nN
e8a60 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e umberOfBytesToUn
e8a70 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 lockLow == 1){.
e8a80 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f if (pFile->lo
e8a90 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 20 7b cal.bReserved) {
e8aa0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f . pFile->lo
e8ab0 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 cal.bReserved =
e8ac0 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 FALSE;. pFi
e8ad0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 le->shared->bRes
e8ae0 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 erved = FALSE;.
e8af0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 bReturn = T
e8b00 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a RUE;. }. }..
e8b10 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 winceMutexRele
e8b20 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 ase(pFile->hMute
e8b30 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 65 x);. return bRe
e8b40 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 turn;.}../*.** A
e8b50 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
e8b60 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 of the LockFile
e8b70 45 78 28 29 20 41 50 49 20 6f 66 20 77 69 6e 64 Ex() API of wind
e8b80 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f ows for wince.*/
e8b90 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e .static BOOL win
e8ba0 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 0a 20 20 ceLockFileEx(.
e8bb0 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a HANDLE *phFile,.
e8bc0 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 2c DWORD dwFlags,
e8bd0 0a 20 20 44 57 4f 52 44 20 64 77 52 65 73 65 72 . DWORD dwReser
e8be0 76 65 64 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 ved,. DWORD nNu
e8bf0 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 mberOfBytesToLoc
e8c00 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e kLow,. DWORD nN
e8c10 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f umberOfBytesToLo
e8c20 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f 56 45 52 ckHigh,. LPOVER
e8c30 4c 41 50 50 45 44 20 6c 70 4f 76 65 72 6c 61 70 LAPPED lpOverlap
e8c40 70 65 64 0a 29 7b 0a 20 20 2f 2a 20 49 66 20 74 ped.){. /* If t
e8c50 68 65 20 63 61 6c 6c 65 72 20 77 61 6e 74 73 20 he caller wants
e8c60 61 20 73 68 61 72 65 64 20 72 65 61 64 20 6c 6f a shared read lo
e8c70 63 6b 2c 20 66 6f 72 77 61 72 64 20 74 68 69 73 ck, forward this
e8c80 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 77 69 call. ** to wi
e8c90 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a 2f 0a 20 nceLockFile */.
e8ca0 20 69 66 20 28 6c 70 4f 76 65 72 6c 61 70 70 65 if (lpOverlappe
e8cb0 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20 53 48 41 d->Offset == SHA
e8cc0 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 RED_FIRST &&.
e8cd0 20 20 20 64 77 46 6c 61 67 73 20 3d 3d 20 31 20 dwFlags == 1
e8ce0 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 &&. nNumber
e8cf0 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 OfBytesToLockLow
e8d00 20 3d 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 29 == SHARED_SIZE)
e8d10 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e {. return win
e8d20 63 65 4c 6f 63 6b 46 69 6c 65 28 70 68 46 69 6c ceLockFile(phFil
e8d30 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c e, SHARED_FIRST,
e8d40 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 0, 1, 0);. }.
e8d50 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 7d return FALSE;.}
e8d60 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 ./*.** End of th
e8d70 65 20 73 70 65 63 69 61 6c 20 63 6f 64 65 20 66 e special code f
e8d80 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a 2a 2a 2a 2a or wince.*******
e8d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8dd0 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f ******/.#endif /
e8de0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 * SQLITE_OS_WINC
e8df0 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a E */../*********
e8e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8e40 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 ****.** The next
e8e50 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e group of routin
e8e60 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 es implement the
e8e70 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 I/O methods spe
e8e80 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 cified.** by the
e8e90 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e8ea0 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a ods object..****
e8eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e8ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
e8f00 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e ** Close a file.
e8f10 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 72 65 70 .**.** It is rep
e8f20 6f 72 74 65 64 20 74 68 61 74 20 61 6e 20 61 74 orted that an at
e8f30 74 65 6d 70 74 20 74 6f 20 63 6c 6f 73 65 20 61 tempt to close a
e8f40 20 68 61 6e 64 6c 65 20 6d 69 67 68 74 20 73 6f handle might so
e8f50 6d 65 74 69 6d 65 73 0a 2a 2a 20 66 61 69 6c 2e metimes.** fail.
e8f60 20 20 54 68 69 73 20 69 73 20 61 20 76 65 72 79 This is a very
e8f70 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 72 65 unreasonable re
e8f80 73 75 6c 74 2c 20 62 75 74 20 77 69 6e 64 6f 77 sult, but window
e8f90 73 20 69 73 20 6e 6f 74 6f 72 69 6f 75 73 0a 2a s is notorious.*
e8fa0 2a 20 66 6f 72 20 62 65 69 6e 67 20 75 6e 72 65 * for being unre
e8fb0 61 73 6f 6e 61 62 6c 65 20 73 6f 20 49 20 64 6f asonable so I do
e8fc0 20 6e 6f 74 20 64 6f 75 62 74 20 74 68 61 74 20 not doubt that
e8fd0 69 74 20 6d 69 67 68 74 20 68 61 70 70 65 6e 2e it might happen.
e8fe0 20 20 49 66 0a 2a 2a 20 74 68 65 20 63 6c 6f 73 If.** the clos
e8ff0 65 20 66 61 69 6c 73 2c 20 77 65 20 70 61 75 73 e fails, we paus
e9000 65 20 66 6f 72 20 31 30 30 20 6d 69 6c 6c 69 73 e for 100 millis
e9010 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79 20 61 econds and try a
e9020 67 61 69 6e 2e 20 20 41 73 0a 2a 2a 20 6d 61 6e gain. As.** man
e9030 79 20 61 73 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 y as MX_CLOSE_AT
e9040 54 45 4d 50 54 20 61 74 74 65 6d 70 74 73 20 74 TEMPT attempts t
e9050 6f 20 63 6c 6f 73 65 20 74 68 65 20 68 61 6e 64 o close the hand
e9060 6c 65 20 61 72 65 20 6d 61 64 65 20 62 65 66 6f le are made befo
e9070 72 65 0a 2a 2a 20 67 69 76 69 6e 67 20 75 70 20 re.** giving up
e9080 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e and returning an
e9090 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 error..*/.#defi
e90a0 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 ne MX_CLOSE_ATTE
e90b0 4d 50 54 20 33 0a 73 74 61 74 69 63 20 69 6e 74 MPT 3.static int
e90c0 20 77 69 6e 43 6c 6f 73 65 28 73 71 6c 69 74 65 winClose(sqlite
e90d0 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 3_file *id){. i
e90e0 6e 74 20 72 63 2c 20 63 6e 74 20 3d 20 30 3b 0a nt rc, cnt = 0;.
e90f0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
e9100 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b = (winFile*)id;
e9110 0a 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f . OSTRACE2("CLO
e9120 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d SE %d\n", pFile-
e9130 3e 68 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 72 >h);. do{. r
e9140 63 20 3d 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 c = CloseHandle(
e9150 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 68 pFile->h);. }wh
e9160 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 2b 2b ile( rc==0 && ++
e9170 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41 cnt < MX_CLOSE_A
e9180 54 54 45 4d 50 54 20 26 26 20 28 53 6c 65 65 70 TTEMPT && (Sleep
e9190 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 (100), 1) );.#if
e91a0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
e91b0 0a 23 64 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 .#define WINCE_D
e91c0 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 ELETION_ATTEMPTS
e91d0 20 33 0a 20 20 77 69 6e 63 65 44 65 73 74 72 6f 3. winceDestro
e91e0 79 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 yLock(pFile);.
e91f0 69 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 if( pFile->zDele
e9200 74 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 teOnClose ){.
e9210 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 int cnt = 0;.
e9220 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 20 20 while(.
e9230 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28 DeleteFileW(
e9240 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e pFile->zDeleteOn
e9250 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 20 20 20 Close)==0.
e9260 20 20 26 26 20 47 65 74 46 69 6c 65 41 74 74 72 && GetFileAttr
e9270 69 62 75 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a ibutesW(pFile->z
e9280 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d DeleteOnClose)!=
e9290 30 78 66 66 66 66 66 66 66 66 20 0a 20 20 20 20 0xffffffff .
e92a0 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57 && cnt++ < W
e92b0 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 INCE_DELETION_AT
e92c0 54 45 4d 50 54 53 0a 20 20 20 20 29 7b 0a 20 20 TEMPTS. ){.
e92d0 20 20 20 20 20 53 6c 65 65 70 28 31 30 30 29 3b Sleep(100);
e92e0 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 69 74 74 /* Wait a litt
e92f0 6c 65 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67 le before trying
e9300 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20 20 7d 0a again */. }.
e9310 20 20 20 20 66 72 65 65 28 70 46 69 6c 65 2d 3e free(pFile->
e9320 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b zDeleteOnClose);
e9330 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 70 . }.#endif. Op
e9340 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 enCounter(-1);.
e9350 20 72 65 74 75 72 6e 20 72 63 20 3f 20 53 51 4c return rc ? SQL
e9360 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f ITE_OK : SQLITE_
e9370 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 IOERR;.}../*.**
e9380 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 Some microsoft c
e9390 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 ompilers lack th
e93a0 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a is definition..*
e93b0 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 /.#ifndef INVALI
e93c0 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 D_SET_FILE_POINT
e93d0 45 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41 ER.# define INVA
e93e0 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 LID_SET_FILE_POI
e93f0 4e 54 45 52 20 28 28 44 57 4f 52 44 29 2d 31 29 NTER ((DWORD)-1)
e9400 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
e9410 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 ead data from a
e9420 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 file into a buff
e9430 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 er. Return SQLI
e9440 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 TE_OK if all.**
e9450 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20 bytes were read
e9460 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 successfully and
e9470 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 SQLITE_IOERR if
e9480 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a anything goes.*
e9490 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 * wrong..*/.stat
e94a0 69 63 20 69 6e 74 20 77 69 6e 52 65 61 64 28 0a ic int winRead(.
e94b0 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
e94c0 69 64 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 id, /*
e94d0 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f File to read fro
e94e0 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 m */. void *pBu
e94f0 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f,
e9500 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 /* Write conte
e9510 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 nt into this buf
e9520 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 fer */. int amt
e9530 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e9540 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
e9550 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a bytes to read *
e9560 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 /. sqlite3_int6
e9570 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 2f 4 offset /
e9580 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 * Begin reading
e9590 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a at this offset *
e95a0 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 /.){. LONG uppe
e95b0 72 42 69 74 73 20 3d 20 28 6f 66 66 73 65 74 3e rBits = (offset>
e95c0 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 >32) & 0x7ffffff
e95d0 66 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 f;. LONG lowerB
e95e0 69 74 73 20 3d 20 6f 66 66 73 65 74 20 26 20 30 its = offset & 0
e95f0 78 66 66 66 66 66 66 66 66 3b 0a 20 20 44 57 4f xffffffff;. DWO
e9600 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 67 RD rc;. DWORD g
e9610 6f 74 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 ot;. winFile *p
e9620 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
e9630 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 )id;. assert( i
e9640 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 d!=0 );. Simula
e9650 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e teIOError(return
e9660 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 SQLITE_IOERR_RE
e9670 41 44 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 AD);. OSTRACE3(
e9680 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 "READ %d lock=%d
e9690 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 \n", pFile->h, p
e96a0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b File->locktype);
e96b0 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 . rc = SetFileP
e96c0 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c ointer(pFile->h,
e96d0 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 lowerBits, &upp
e96e0 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 erBits, FILE_BEG
e96f0 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 IN);. if( rc==I
e9700 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f NVALID_SET_FILE_
e9710 50 4f 49 4e 54 45 52 20 26 26 20 47 65 74 4c 61 POINTER && GetLa
e9720 73 74 45 72 72 6f 72 28 29 21 3d 4e 4f 5f 45 52 stError()!=NO_ER
e9730 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ROR ){. retur
e9740 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 n SQLITE_FULL;.
e9750 20 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 }. if( !ReadFi
e9760 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 le(pFile->h, pBu
e9770 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 f, amt, &got, 0)
e9780 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
e9790 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
e97a0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 3d ;. }. if( got=
e97b0 3d 28 44 57 4f 52 44 29 61 6d 74 20 29 7b 0a 20 =(DWORD)amt ){.
e97c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e97d0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
e97e0 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 74 /* Unread part
e97f0 73 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 s of the buffer
e9800 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c must be zero-fil
e9810 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 led */. memse
e9820 74 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 t(&((char*)pBuf)
e9830 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f [got], 0, amt-go
e9840 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 t);. return S
e9850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
e9860 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f T_READ;. }.}../
e9870 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 *.** Write data
e9880 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e from a buffer in
e9890 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 to a file. Retu
e98a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 rn SQLITE_OK on
e98b0 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f success.** or so
e98c0 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 me other error c
e98d0 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a ode on failure..
e98e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
e98f0 6e 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 nWrite(. sqlite
e9900 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 3_file *id,
e9910 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 /* File to w
e9920 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 rite into */. c
e9930 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c onst void *pBuf,
e9940 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
e9950 62 79 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 bytes to be writ
e9960 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 ten */. int amt
e9970 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e9980 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
e9990 62 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a bytes to write *
e99a0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 /. sqlite3_int6
e99b0 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 2f 2a 4 offset /*
e99c0 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 Offset into the
e99d0 20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77 file to begin w
e99e0 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a riting at */.){.
e99f0 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 LONG upperBits
e9a00 20 3d 20 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 = (offset>>32)
e9a10 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 & 0x7fffffff;.
e9a20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d LONG lowerBits =
e9a30 20 6f 66 66 73 65 74 20 26 20 30 78 66 66 66 66 offset & 0xffff
e9a40 66 66 66 66 3b 0a 20 20 44 57 4f 52 44 20 72 63 ffff;. DWORD rc
e9a50 3b 0a 20 20 44 57 4f 52 44 20 77 72 6f 74 65 3b ;. DWORD wrote;
e9a60 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
e9a70 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 e = (winFile*)id
e9a80 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d ;. assert( id!=
e9a90 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 0 );. SimulateI
e9aa0 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 OError(return SQ
e9ab0 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 LITE_IOERR_WRITE
e9ac0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 );. SimulateDis
e9ad0 6b 66 75 6c 6c 45 72 72 6f 72 28 72 65 74 75 72 kfullError(retur
e9ae0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 3b 0a n SQLITE_FULL);.
e9af0 20 20 4f 53 54 52 41 43 45 33 28 22 57 52 49 54 OSTRACE3("WRIT
e9b00 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c E %d lock=%d\n",
e9b10 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 pFile->h, pFile
e9b20 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 ->locktype);. r
e9b30 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 c = SetFilePoint
e9b40 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 er(pFile->h, low
e9b50 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 erBits, &upperBi
e9b60 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b ts, FILE_BEGIN);
e9b70 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c . if( rc==INVAL
e9b80 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e ID_SET_FILE_POIN
e9b90 54 45 52 20 26 26 20 47 65 74 4c 61 73 74 45 72 TER && GetLastEr
e9ba0 72 6f 72 28 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 ror()!=NO_ERROR
e9bb0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
e9bc0 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 LITE_FULL;. }.
e9bd0 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 assert( amt>0 )
e9be0 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 ;. while(.
e9bf0 61 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 72 amt>0. && (r
e9c00 63 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 46 c = WriteFile(pF
e9c10 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d ile->h, pBuf, am
e9c20 74 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 3d t, &wrote, 0))!=
e9c30 30 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 3e 0. && wrote>
e9c40 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 0. ){. amt -
e9c50 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 = wrote;. pBu
e9c60 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 f = &((char*)pBu
e9c70 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 f)[wrote];. }.
e9c80 20 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e if( !rc || amt>
e9c90 28 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 20 (int)wrote ){.
e9ca0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e9cb0 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 FULL;. }. retu
e9cc0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
e9cd0 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 ./*.** Truncate
e9ce0 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 an open file to
e9cf0 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 a specified size
e9d00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
e9d10 69 6e 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 inTruncate(sqlit
e9d20 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c e3_file *id, sql
e9d30 69 74 65 33 5f 69 6e 74 36 34 20 6e 42 79 74 65 ite3_int64 nByte
e9d40 29 7b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 ){. DWORD rc;.
e9d50 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 LONG upperBits
e9d60 3d 20 28 6e 42 79 74 65 3e 3e 33 32 29 20 26 20 = (nByte>>32) &
e9d70 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 4c 4f 0x7fffffff;. LO
e9d80 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 6e NG lowerBits = n
e9d90 42 79 74 65 20 26 20 30 78 66 66 66 66 66 66 66 Byte & 0xfffffff
e9da0 66 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 f;. winFile *pF
e9db0 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 ile = (winFile*)
e9dc0 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 id;. OSTRACE3("
e9dd0 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 TRUNCATE %d %lld
e9de0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e \n", pFile->h, n
e9df0 42 79 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74 Byte);. Simulat
e9e00 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 eIOError(return
e9e10 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 SQLITE_IOERR_TRU
e9e20 4e 43 41 54 45 29 3b 0a 20 20 72 63 20 3d 20 53 NCATE);. rc = S
e9e30 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 etFilePointer(pF
e9e40 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 ile->h, lowerBit
e9e50 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 s, &upperBits, F
e9e60 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 ILE_BEGIN);. if
e9e70 28 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 ( INVALID_SET_FI
e9e80 4c 45 5f 50 4f 49 4e 54 45 52 20 21 3d 20 72 63 LE_POINTER != rc
e9e90 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 74 45 6e ){. /* SetEn
e9ea0 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 dOfFile will fai
e9eb0 6c 20 69 66 20 6e 42 79 74 65 20 69 73 20 6e 65 l if nByte is ne
e9ec0 67 61 74 69 76 65 20 2a 2f 0a 20 20 20 20 69 66 gative */. if
e9ed0 28 20 53 65 74 45 6e 64 4f 66 46 69 6c 65 28 70 ( SetEndOfFile(p
e9ee0 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 File->h) ){.
e9ef0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e9f00 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 OK;. }. }.
e9f10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
e9f20 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a ERR_TRUNCATE;.}.
e9f30 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
e9f40 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 EST./*.** Count
e9f50 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 the number of fu
e9f60 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d llsyncs and norm
e9f70 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 al syncs. This
e9f80 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a is used to test.
e9f90 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e ** that syncs an
e9fa0 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 d fullsyncs are
e9fb0 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 occuring at the
e9fc0 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a right times..*/.
e9fd0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
e9fe0 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e qlite3_sync_coun
e9ff0 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 t = 0;.SQLITE_AP
ea000 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 I int sqlite3_fu
ea010 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 llsync_count = 0
ea020 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
ea030 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 Make sure all wr
ea040 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 ites to a partic
ea050 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f ular file are co
ea060 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e mmitted to disk.
ea070 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
ea080 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 inSync(sqlite3_f
ea090 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 ile *id, int fla
ea0a0 67 73 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a gs){. winFile *
ea0b0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
ea0c0 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 *)id;. OSTRACE3
ea0d0 28 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 ("SYNC %d lock=%
ea0e0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
ea0f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 pFile->locktype)
ea100 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
ea110 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67 73 TEST. if( flags
ea120 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 & SQLITE_SYNC_F
ea130 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ULL ){. sqlit
ea140 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e e3_fullsync_coun
ea150 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 t++;. }. sqlit
ea160 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b e3_sync_count++;
ea170 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 .#endif. /* If
ea180 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 we compiled with
ea190 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 the SQLITE_NO_S
ea1a0 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 YNC flag, then s
ea1b0 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a yncing is a. **
ea1c0 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 no-op. */.#ifd
ea1d0 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e ef SQLITE_NO_SYN
ea1e0 43 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c C. return SQL
ea1f0 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 ITE_OK;.#else.
ea200 69 66 28 20 46 6c 75 73 68 46 69 6c 65 42 75 66 if( FlushFileBuf
ea210 66 65 72 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 fers(pFile->h) )
ea220 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
ea230 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
ea240 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
ea250 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 23 65 TE_IOERR;. }.#e
ea260 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 ndif.}../*.** De
ea270 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 termine the curr
ea280 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 ent size of a fi
ea290 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 le in bytes.*/.s
ea2a0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 69 6c tatic int winFil
ea2b0 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 eSize(sqlite3_fi
ea2c0 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f le *id, sqlite3_
ea2d0 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 int64 *pSize){.
ea2e0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
ea2f0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
ea300 20 20 44 57 4f 52 44 20 75 70 70 65 72 42 69 74 DWORD upperBit
ea310 73 2c 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 s, lowerBits;.
ea320 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
ea330 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
ea340 45 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f ERR_FSTAT);. lo
ea350 77 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 6c werBits = GetFil
ea360 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20 eSize(pFile->h,
ea370 26 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 2a &upperBits);. *
ea380 70 53 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74 pSize = (((sqlit
ea390 65 33 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69 e3_int64)upperBi
ea3a0 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72 ts)<<32) + lower
ea3b0 42 69 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 Bits;. return S
ea3c0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
ea3d0 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c ** LOCKFILE_FAIL
ea3e0 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 _IMMEDIATELY is
ea3f0 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d undefined on som
ea400 65 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d e Windows system
ea410 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f s..*/.#ifndef LO
ea420 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 CKFILE_FAIL_IMME
ea430 44 49 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 DIATELY.# define
ea440 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 LOCKFILE_FAIL_I
ea450 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e MMEDIATELY 1.#en
ea460 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 dif../*.** Acqui
ea470 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b re a reader lock
ea480 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41 ..** Different A
ea490 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 PI routines are
ea4a0 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67 called depending
ea4b0 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e on whether or n
ea4c0 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69 ot this.** is Wi
ea4d0 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f n95 or WinNT..*/
ea4e0 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 .static int getR
ea4f0 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 eadLock(winFile
ea500 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 *pFile){. int r
ea510 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 es;. if( isNT()
ea520 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 ){. OVERLAPP
ea530 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c ED ovlp;. ovl
ea540 70 2e 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 p.Offset = SHARE
ea550 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c D_FIRST;. ovl
ea560 70 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30 p.OffsetHigh = 0
ea570 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e ;. ovlp.hEven
ea580 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d t = 0;. res =
ea590 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c LockFileEx(pFil
ea5a0 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 e->h, LOCKFILE_F
ea5b0 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c AIL_IMMEDIATELY,
ea5c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
ea5d0 20 20 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f 0, SHARED_
ea5e0 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b SIZE, 0, &ovlp);
ea5f0 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
ea600 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ea610 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
ea620 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
ea630 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 cuted. .*/.#if S
ea640 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
ea650 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 0. }else{. i
ea660 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 nt lk;. sqlit
ea670 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 e3_randomness(si
ea680 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a zeof(lk), &lk);.
ea690 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
ea6a0 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 6c 6b 20 dLockByte = (lk
ea6b0 26 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53 & 0x7fffffff)%(S
ea6c0 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 3b HARED_SIZE - 1);
ea6d0 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 . res = LockF
ea6e0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 ile(pFile->h, SH
ea6f0 41 52 45 44 5f 46 49 52 53 54 2b 70 46 69 6c 65 ARED_FIRST+pFile
ea700 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 ->sharedLockByte
ea710 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 , 0, 1, 0);.#end
ea720 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 if. }. return
ea730 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e res;.}../*.** Un
ea740 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f do a readlock.*/
ea750 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f .static int unlo
ea760 63 6b 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 ckReadLock(winFi
ea770 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e le *pFile){. in
ea780 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e t res;. if( isN
ea790 54 28 29 20 29 7b 0a 20 20 20 20 72 65 73 20 3d T() ){. res =
ea7a0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c UnlockFile(pFil
ea7b0 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 e->h, SHARED_FIR
ea7c0 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 ST, 0, SHARED_SI
ea7d0 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69 73 4e 54 28 ZE, 0);./* isNT(
ea7e0 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
ea7f0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
ea800 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
ea810 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
ea820 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
ea830 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 WINCE==0. }else
ea840 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f {. res = Unlo
ea850 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
ea860 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 SHARED_FIRST +
ea870 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 pFile->sharedLoc
ea880 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b kByte, 0, 1, 0);
ea890 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 .#endif. }. re
ea8a0 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a turn res;.}../*.
ea8b0 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 ** Lock the file
ea8c0 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 with the lock s
ea8d0 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
ea8e0 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d meter locktype -
ea8f0 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
ea900 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
ea910 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
ea920 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
ea930 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
ea940 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
ea950 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
ea960 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
ea970 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
ea980 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
ea990 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
ea9a0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
ea9b0 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
ea9c0 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
ea9d0 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
ea9e0 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
ea9f0 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
eaa00 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
eaa10 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
eaa20 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
eaa30 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
eaa40 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
eaa50 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
eaa60 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
eaa70 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
eaa80 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
eaa90 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
eaaa0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
eaab0 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
eaac0 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
eaad0 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
eaae0 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
eaaf0 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
eab00 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
eab10 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
eab20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
eab30 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
eab40 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
eab50 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
eab60 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
eab70 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 e will only incr
eab80 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 ease a lock. Th
eab90 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72 6f e winUnlock() ro
eaba0 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 utine.** erases
eabb0 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 all locks at onc
eabc0 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73 e and returns us
eabd0 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 immediately to
eabe0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e locking level 0.
eabf0 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f .** It is not po
eac00 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 ssible to lower
eac10 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
eac20 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 l one step at a
eac30 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 time. You.** mu
eac40 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 st go straight t
eac50 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 o locking level
eac60 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0..*/.static int
eac70 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65 33 winLock(sqlite3
eac80 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
eac90 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 ocktype){. int
eaca0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
eacb0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
eacc0 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e e from subroutin
eacd0 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 es */. int res
eace0 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 2f = 1; /
eacf0 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77 69 * Result of a wi
ead00 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c 20 ndows lock call
ead10 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b */. int newLock
ead20 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 type; /* S
ead30 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 et pFile->lockty
ead40 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 pe to this value
ead50 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 before exiting
ead60 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 */. int gotPend
ead70 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 ingLock = 0;/* T
ead80 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 rue if we acquir
ead90 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 ed a PENDING loc
eada0 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 k this time */.
eadb0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
eadc0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
eadd0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
eade0 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 !=0 );. OSTRACE
eadf0 35 28 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 5("LOCK %d %d wa
eae00 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 s %d(%d)\n",.
eae10 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c pFile->h,
eae20 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 locktype, pFile
eae30 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c ->locktype, pFil
eae40 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 e->sharedLockByt
eae50 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 e);.. /* If the
eae60 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 re is already a
eae70 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 lock of this typ
eae80 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 e or more restri
eae90 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a ctive on the. *
eaea0 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 * OsFile, do not
eaeb0 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 hing. Don't use
eaec0 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 the end_lock: ex
eaed0 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a it path, as. **
eaee0 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d sqlite3OsEnterM
eaef0 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 utex() hasn't be
eaf00 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 en called yet..
eaf10 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
eaf20 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 >locktype>=lockt
eaf30 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ype ){. retur
eaf40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
eaf50 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
eaf60 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 the locking seq
eaf70 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 uence is correct
eaf80 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
eaf90 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 pFile->locktype!
eafa0 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b =NO_LOCK || lock
eafb0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
eafc0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c K );. assert( l
eafd0 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 ocktype!=PENDING
eafe0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
eaff0 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 t( locktype!=RES
eb000 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 ERVED_LOCK || pF
eb010 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 ile->locktype==S
eb020 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 HARED_LOCK );..
eb030 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e /* Lock the PEN
eb040 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 DING_LOCK byte i
eb050 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71 f we need to acq
eb060 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c uire a PENDING l
eb070 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 ock or. ** a SH
eb080 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 ARED lock. If w
eb090 65 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20 e are acquiring
eb0a0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 a SHARED lock, t
eb0b0 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f he acquisition o
eb0c0 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 f. ** the PENDI
eb0d0 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 NG_LOCK byte is
eb0e0 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a temporary.. */.
eb0f0 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 newLocktype =
eb100 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b pFile->locktype;
eb110 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
eb120 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a cktype==NO_LOCK.
eb130 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d || (locktype=
eb140 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
eb150 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 && pFile->lockty
eb160 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe==RESERVED_LOC
eb170 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 K). ){. int
eb180 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 cnt = 3;. whi
eb190 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 le( cnt-->0 && (
eb1a0 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 res = LockFile(p
eb1b0 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 File->h, PENDING
eb1c0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 _BYTE, 0, 1, 0))
eb1d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ==0 ){. /*
eb1e0 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 Try 3 times to g
eb1f0 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c et the pending l
eb200 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e ock. The pendin
eb210 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a g lock might be.
eb220 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 ** held by
eb230 20 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 another reader
eb240 70 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c process who will
eb250 20 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 release it mome
eb260 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a ntarily.. *
eb270 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 /. OSTRACE2
eb280 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 ("could not get
eb290 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 a PENDING lock.
eb2a0 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b cnt=%d\n", cnt);
eb2b0 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b . Sleep(1);
eb2c0 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 . }. gotPe
eb2d0 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b ndingLock = res;
eb2e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
eb2f0 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b re a shared lock
eb300 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
eb310 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
eb320 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 K && res ){.
eb330 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
eb340 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
eb350 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65 );. res = ge
eb360 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 tReadLock(pFile)
eb370 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b ;. if( res ){
eb380 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
eb390 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b pe = SHARED_LOCK
eb3a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
eb3b0 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 * Acquire a RESE
eb3c0 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 RVED lock. */.
eb3d0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 if( locktype==R
eb3e0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 ESERVED_LOCK &&
eb3f0 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72 res ){. asser
eb400 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
eb410 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
eb420 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 );. res = Loc
eb430 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
eb440 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 RESERVED_BYTE, 0
eb450 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 , 1, 0);. if(
eb460 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 res ){. ne
eb470 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45 wLocktype = RESE
eb480 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d RVED_LOCK;. }
eb490 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
eb4a0 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 re a PENDING loc
eb4b0 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 k. */. if( loc
eb4c0 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
eb4d0 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a _LOCK && res ){.
eb4e0 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 newLocktype
eb4f0 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a = PENDING_LOCK;.
eb500 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f gotPendingLo
eb510 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f ck = 0;. }.. /
eb520 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 * Acquire an EXC
eb530 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f LUSIVE lock. */
eb540 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
eb550 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
eb560 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 && res ){. as
eb570 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 sert( pFile->loc
eb580 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f ktype>=SHARED_LO
eb590 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 CK );. res =
eb5a0 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 unlockReadLock(p
eb5b0 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 File);. OSTRA
eb5c0 43 45 32 28 22 75 6e 72 65 61 64 6c 6f 63 6b 20 CE2("unreadlock
eb5d0 3d 20 25 64 5c 6e 22 2c 20 72 65 73 29 3b 0a 20 = %d\n", res);.
eb5e0 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c res = LockFil
eb5f0 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 e(pFile->h, SHAR
eb600 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 ED_FIRST, 0, SHA
eb610 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 RED_SIZE, 0);.
eb620 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
eb630 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d newLocktype =
eb640 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b EXCLUSIVE_LOCK;
eb650 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
eb660 20 20 4f 53 54 52 41 43 45 32 28 22 65 72 72 6f OSTRACE2("erro
eb670 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 r-code = %d\n",
eb680 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 3b GetLastError());
eb690 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f . getReadLo
eb6a0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d ck(pFile);. }
eb6b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 . }.. /* If we
eb6c0 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 are holding a P
eb6d0 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 ENDING lock that
eb6e0 20 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c ought to be rel
eb6f0 65 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a eased, then. **
eb700 20 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e release it now.
eb710 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 . */. if( gotP
eb720 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f endingLock && lo
eb730 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
eb740 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 OCK ){. Unloc
eb750 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
eb760 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c PENDING_BYTE, 0,
eb770 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 1, 0);. }.. /
eb780 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 74 61 * Update the sta
eb790 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 te of the lock h
eb7a0 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 as held in the f
eb7b0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
eb7c0 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 hen. ** return
eb7d0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
eb7e0 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a result code.. *
eb7f0 2f 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 /. if( res ){.
eb800 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
eb810 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
eb820 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46 OSTRACE4("LOCK F
eb830 41 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 AILED %d trying
eb840 66 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 for %d but got %
eb850 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a d\n", pFile->h,.
eb860 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 lockt
eb870 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 ype, newLocktype
eb880 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
eb890 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 TE_BUSY;. }. p
eb8a0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
eb8b0 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 newLocktype;.
eb8c0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
eb8d0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
eb8e0 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
eb8f0 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
eb900 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
eb910 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
eb920 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
eb930 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
eb940 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
eb950 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a s held, return.*
eb960 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 * non-zero, othe
eb970 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 rwise zero..*/.s
eb980 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 68 65 tatic int winChe
eb990 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
eb9a0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
eb9b0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a int *pResOut){.
eb9c0 20 20 69 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46 int rc;. winF
eb9d0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
eb9e0 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 nFile*)id;. ass
eb9f0 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b ert( pFile!=0 );
eba00 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
eba10 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 cktype>=RESERVED
eba20 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 20 _LOCK ){. rc
eba30 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 = 1;. OSTRACE
eba40 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 3("TEST WR-LOCK
eba50 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 %d %d (local)\n"
eba60 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b , pFile->h, rc);
eba70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
eba80 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c = LockFile(pFil
eba90 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 e->h, RESERVED_B
ebaa0 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 YTE, 0, 1, 0);.
ebab0 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
ebac0 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 UnlockFile(pF
ebad0 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 ile->h, RESERVED
ebae0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b _BYTE, 0, 1, 0);
ebaf0 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
ebb00 21 72 63 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 !rc;. OSTRACE
ebb10 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 3("TEST WR-LOCK
ebb20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e %d %d (remote)\n
ebb30 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 ", pFile->h, rc)
ebb40 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 ;. }. *pResOut
ebb50 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 = rc;. return
ebb60 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
ebb70 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
ebb80 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
ebb90 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
ebba0 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 d to locktype.
ebbb0 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 locktype.** must
ebbc0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f be either NO_LO
ebbd0 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 CK or SHARED_LOC
ebbe0 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 K..**.** If the
ebbf0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 locking level of
ebc00 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
ebc10 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 ptor is already
ebc20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 at or below.** t
ebc30 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
ebc40 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 king level, this
ebc50 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
ebc60 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 -op..**.** It is
ebc70 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f not possible fo
ebc80 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 r this routine t
ebc90 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 o fail if the se
ebca0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a cond argument.**
ebcb0 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 is NO_LOCK. If
ebcc0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
ebcd0 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c ment is SHARED_L
ebce0 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f OCK then this ro
ebcf0 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 utine.** might r
ebd00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
ebd10 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e RR;.*/.static in
ebd20 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69 t winUnlock(sqli
ebd30 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
ebd40 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 t locktype){. i
ebd50 6e 74 20 74 79 70 65 3b 0a 20 20 77 69 6e 46 69 nt type;. winFi
ebd60 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
ebd70 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 File*)id;. int
ebd80 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
ebd90 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 assert( pFile!
ebda0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
ebdb0 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 locktype<=SHARED
ebdc0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 _LOCK );. OSTRA
ebdd0 43 45 35 28 22 55 4e 4c 4f 43 4b 20 25 64 20 74 CE5("UNLOCK %d t
ebde0 6f 20 25 64 20 77 61 73 20 25 64 28 25 64 29 5c o %d was %d(%d)\
ebdf0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
ebe00 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 cktype,.
ebe10 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
ebe20 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 e, pFile->shared
ebe30 4c 6f 63 6b 42 79 74 65 29 3b 0a 20 20 74 79 70 LockByte);. typ
ebe40 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 e = pFile->lockt
ebe50 79 70 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e ype;. if( type>
ebe60 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
ebe70 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c ){. UnlockFil
ebe80 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 e(pFile->h, SHAR
ebe90 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 ED_FIRST, 0, SHA
ebea0 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 RED_SIZE, 0);.
ebeb0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
ebec0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 21 SHARED_LOCK && !
ebed0 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c getReadLock(pFil
ebee0 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 e) ){. /* T
ebef0 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 his should never
ebf00 20 68 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f happen. We sho
ebf10 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 uld always be ab
ebf20 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 le to. ** r
ebf30 65 61 63 71 75 69 72 65 20 74 68 65 20 72 65 61 eacquire the rea
ebf40 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 d lock */.
ebf50 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
ebf60 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a R_UNLOCK;. }.
ebf70 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d }. if( type>=
ebf80 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b RESERVED_LOCK ){
ebf90 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 . UnlockFile(
ebfa0 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 pFile->h, RESERV
ebfb0 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 ED_BYTE, 0, 1, 0
ebfc0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 );. }. if( loc
ebfd0 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 ktype==NO_LOCK &
ebfe0 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c & type>=SHARED_L
ebff0 4f 43 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 OCK ){. unloc
ec000 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 kReadLock(pFile)
ec010 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 ;. }. if( type
ec020 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 >=PENDING_LOCK )
ec030 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 {. UnlockFile
ec040 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 (pFile->h, PENDI
ec050 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 NG_BYTE, 0, 1, 0
ec060 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e );. }. pFile->
ec070 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
ec080 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ype;. return rc
ec090 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 ;.}../*.** Contr
ec0a0 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 ol and query of
ec0b0 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 the open file ha
ec0c0 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ndle..*/.static
ec0d0 69 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 int winFileContr
ec0e0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ol(sqlite3_file
ec0f0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 *id, int op, voi
ec100 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 d *pArg){. swit
ec110 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 ch( op ){. ca
ec120 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f se SQLITE_FCNTL_
ec130 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 LOCKSTATE: {.
ec140 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d *(int*)pArg =
ec150 20 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d ((winFile*)id)-
ec160 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 >locktype;.
ec170 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
ec180 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 K;. }. }. r
ec190 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
ec1a0 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 OR;.}../*.** Ret
ec1b0 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 urn the sector s
ec1c0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 ize in bytes of
ec1d0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 the underlying b
ec1e0 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a lock device for.
ec1f0 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ** the specified
ec200 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 file. This is a
ec210 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 lmost always 512
ec220 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 bytes, but may
ec230 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 be.** larger for
ec240 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a some devices..*
ec250 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 *.** SQLite code
ec260 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 assumes this fu
ec270 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 nction cannot fa
ec280 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 il. It also assu
ec290 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 mes that.** if t
ec2a0 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 wo files are cre
ec2b0 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 ated in the same
ec2c0 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 file-system dir
ec2d0 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 ectory (i.e..**
ec2e0 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 a database and i
ec2f0 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 ts journal file)
ec300 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 that the sector
ec310 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 size will be th
ec320 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f e.** same for bo
ec330 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e th..*/.static in
ec340 74 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 t winSectorSize(
ec350 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
ec360 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 ){. return SQLI
ec370 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f TE_DEFAULT_SECTO
ec380 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a R_SIZE;.}../*.**
ec390 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 Return a vector
ec3a0 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 of device chara
ec3b0 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 cteristics..*/.s
ec3c0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 76 tatic int winDev
ec3d0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
ec3e0 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
ec3f0 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 *id){. return 0
ec400 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
ec410 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 vector defines a
ec420 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 ll the methods t
ec430 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 hat can operate
ec440 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 on an.** sqlite3
ec450 5f 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e _file for win32.
ec460 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 .*/.static const
ec470 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
ec480 6f 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20 ods winIoMethod
ec490 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 = {. 1,
ec4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ec4b0 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 /* iVersion */.
ec4c0 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e winClose,. win
ec4d0 52 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 65 Read,. winWrite
ec4e0 2c 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 2c ,. winTruncate,
ec4f0 0a 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69 . winSync,. wi
ec500 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e nFileSize,. win
ec510 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63 Lock,. winUnloc
ec520 6b 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 73 k,. winCheckRes
ec530 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e ervedLock,. win
ec540 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77 FileControl,. w
ec550 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 inSectorSize,.
ec560 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 winDeviceCharact
ec570 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a eristics.};../**
ec580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ec590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ec5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ec5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ec5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 *********.** Her
ec5d0 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d e ends the I/O m
ec5e0 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d ethods that form
ec5f0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f the sqlite3_io_
ec600 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a methods object..
ec610 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 **.** The next b
ec620 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 lock of code imp
ec630 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20 lements the VFS
ec640 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a methods..*******
ec650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ec660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ec670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ec680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ec690 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f *****/../*.** Co
ec6a0 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 69 nvert a UTF-8 fi
ec6b0 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 lename into what
ec6c0 65 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e ever form the un
ec6d0 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72 derlying.** oper
ec6e0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e ating system wan
ec6f0 74 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e ts filenames in.
ec700 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 Space to hold
ec710 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 the result.** is
ec720 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d obtained from m
ec730 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 alloc and must b
ec740 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63 e freed by the c
ec750 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 alling.** functi
ec760 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f on..*/.static vo
ec770 69 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46 id *convertUtf8F
ec780 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68 ilename(const ch
ec790 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a ar *zFilename){.
ec7a0 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 void *zConvert
ec7b0 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73 ed = 0;. if( is
ec7c0 4e 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e NT() ){. zCon
ec7d0 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55 verted = utf8ToU
ec7e0 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 nicode(zFilename
ec7f0 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
ec800 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
ec810 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
ec820 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
ec830 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 xecuted. .*/.#if
ec840 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ec850 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
ec860 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 zConverted = ut
ec870 66 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 f8ToMbcs(zFilena
ec880 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a me);.#endif. }.
ec890 20 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c /* caller will
ec8a0 20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d handle out of m
ec8b0 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 72 emory */. retur
ec8c0 6e 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a n zConverted;.}.
ec8d0 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
ec8e0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e temporary file n
ec8f0 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 ame in zBuf. zB
ec900 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 uf must be big e
ec910 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 nough to.** hold
ec920 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 at pVfs->mxPath
ec930 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e name characters.
ec940 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
ec950 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e etTempname(int n
ec960 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 Buf, char *zBuf)
ec970 7b 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20 {. static char
ec980 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 zChars[] =. "
ec990 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop
ec9a0 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 qrstuvwxyz".
ec9b0 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f "ABCDEFGHIJKLMNO
ec9c0 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 PQRSTUVWXYZ".
ec9d0 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 "0123456789";.
ec9e0 20 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20 size_t i, j;.
ec9f0 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d char zTempPath[M
eca00 41 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66 AX_PATH+1];. if
eca10 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 ( sqlite3_temp_d
eca20 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 irectory ){.
eca30 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
eca40 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 (MAX_PATH-30, zT
eca50 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 73 empPath, "%s", s
eca60 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 qlite3_temp_dire
eca70 63 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20 ctory);. }else
eca80 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 if( isNT() ){.
eca90 20 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a char *zMulti;.
ecaa0 20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 50 WCHAR zWideP
ecab0 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 ath[MAX_PATH];.
ecac0 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 28 GetTempPathW(
ecad0 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 MAX_PATH-30, zWi
ecae0 64 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 dePath);. zMu
ecaf0 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 lti = unicodeToU
ecb00 74 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a tf8(zWidePath);.
ecb10 20 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 if( zMulti )
ecb20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
ecb30 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
ecb40 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c H-30, zTempPath,
ecb50 20 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a "%s", zMulti);.
ecb60 20 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c 74 free(zMult
ecb70 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 i);. }else{.
ecb80 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
ecb90 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
ecba0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
ecbb0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
ecbc0 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
ecbd0 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
ecbe0 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 uted. .** Since
ecbf0 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f the ASCII versio
ecc00 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f n of these Windo
ecc10 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 ws API do not ex
ecc20 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a ist for WINCE,.*
ecc30 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 * it's important
ecc40 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 to not referenc
ecc50 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 e them for WINCE
ecc60 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 builds..*/.#if
ecc70 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
ecc80 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
ecc90 63 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20 char *zUtf8;.
ecca0 20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 5b char zMbcsPath[
eccb0 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 MAX_PATH];. G
eccc0 65 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f etTempPathA(MAX_
eccd0 50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61 PATH-30, zMbcsPa
ecce0 74 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d th);. zUtf8 =
eccf0 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d sqlite3_win32_m
ecd00 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63 bcs_to_utf8(zMbc
ecd10 73 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 sPath);. if(
ecd20 7a 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 73 zUtf8 ){. s
ecd30 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
ecd40 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 MAX_PATH-30, zTe
ecd50 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 55 mpPath, "%s", zU
ecd60 74 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 65 tf8);. free
ecd70 28 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c (zUtf8);. }el
ecd80 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e se{. return
ecd90 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
ecda0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
ecdb0 20 20 66 6f 72 28 69 3d 73 74 72 6c 65 6e 28 7a for(i=strlen(z
ecdc0 54 65 6d 70 50 61 74 68 29 3b 20 69 3e 30 20 26 TempPath); i>0 &
ecdd0 26 20 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d & zTempPath[i-1]
ecde0 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 =='\\'; i--){}.
ecdf0 20 7a 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 zTempPath[i] =
ece00 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 0;. sqlite3_snp
ece10 72 69 6e 74 66 28 6e 42 75 66 2d 33 30 2c 20 7a rintf(nBuf-30, z
ece20 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Buf,.
ece30 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53 51 "%s\\"SQ
ece40 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 LITE_TEMP_FILE_P
ece50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 REFIX, zTempPath
ece60 29 3b 0a 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28 );. j = strlen(
ece70 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 zBuf);. sqlite3
ece80 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30 2c 20 _randomness(20,
ece90 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66 6f 72 &zBuf[j]);. for
ecea0 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b 2b 2c (i=0; i<20; i++,
eceb0 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66 5b j++){. zBuf[
ecec0 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 j] = (char)zChar
eced0 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 s[ ((unsigned ch
ecee0 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a ar)zBuf[j])%(siz
ecef0 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d eof(zChars)-1) ]
ecf00 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 ;. }. zBuf[j]
ecf10 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 = 0;. OSTRACE2(
ecf20 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 "TEMP FILENAME:
ecf30 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 %s\n", zBuf);.
ecf40 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
ecf50 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ; .}../*.** The
ecf60 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 return value of
ecf70 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 0a getLastErrorMsg.
ecf80 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74 68 ** is zero if th
ecf90 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
ecfa0 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66 66 fits in the buff
ecfb0 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a er, or non-zero.
ecfc0 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69 66 ** otherwise (if
ecfd0 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61 73 the message was
ecfe0 20 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f 0a truncated)..*/.
ecff0 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c 61 static int getLa
ed000 73 74 45 72 72 6f 72 4d 73 67 28 69 6e 74 20 6e stErrorMsg(int n
ed010 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 Buf, char *zBuf)
ed020 7b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 {. DWORD error
ed030 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 = GetLastError()
ed040 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 ;..#if SQLITE_OS
ed050 5f 57 49 4e 43 45 0a 20 20 73 71 6c 69 74 65 33 _WINCE. sqlite3
ed060 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 _snprintf(nBuf,
ed070 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30 zBuf, "OsError 0
ed080 78 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72 x%x (%u)", error
ed090 2c 20 65 72 72 6f 72 29 3b 0a 23 65 6c 73 65 0a , error);.#else.
ed0a0 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 73 73 61 /* FormatMessa
ed0b0 67 65 20 72 65 74 75 72 6e 73 20 30 20 6f 6e 20 ge returns 0 on
ed0c0 66 61 69 6c 75 72 65 2e 20 20 4f 74 68 65 72 77 failure. Otherw
ed0d0 69 73 65 20 69 74 0a 20 20 2a 2a 20 72 65 74 75 ise it. ** retu
ed0e0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
ed0f0 66 20 54 43 48 41 52 73 20 77 72 69 74 74 65 6e f TCHARs written
ed100 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 20 to the output.
ed110 20 2a 2a 20 62 75 66 66 65 72 2c 20 65 78 63 6c ** buffer, excl
ed120 75 64 69 6e 67 20 74 68 65 20 74 65 72 6d 69 6e uding the termin
ed130 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68 61 72 2e ating null char.
ed140 0a 20 20 2a 2f 0a 20 20 69 66 20 28 21 46 6f 72 . */. if (!For
ed150 6d 61 74 4d 65 73 73 61 67 65 41 28 46 4f 52 4d matMessageA(FORM
ed160 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f 4d 5f AT_MESSAGE_FROM_
ed170 53 59 53 54 45 4d 2c 0a 20 20 20 20 20 20 20 20 SYSTEM,.
ed180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 NU
ed190 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 LL,.
ed1a0 20 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c error,
ed1b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
ed1c0 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 0,.
ed1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ed1e0 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 zBuf,.
ed1f0 20 20 20 20 20 20 20 20 20 20 20 20 6e 42 75 66 nBuf
ed200 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 -1,.
ed210 20 20 20 20 20 20 20 20 20 20 30 29 29 0a 20 20 0)).
ed220 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e {. sqlite3_sn
ed230 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 printf(nBuf, zBu
ed240 66 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 25 78 f, "OsError 0x%x
ed250 20 28 25 75 29 22 2c 20 65 72 72 6f 72 2c 20 65 (%u)", error, e
ed260 72 72 6f 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 rror);. }.#endi
ed270 66 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d f.. return 0;.}
ed280 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 .../*.** Open a
ed290 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
ed2a0 69 6e 74 20 77 69 6e 4f 70 65 6e 28 0a 20 20 73 int winOpen(. s
ed2b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
ed2c0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 , /* Not
ed2d0 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 used */. const
ed2e0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 char *zName,
ed2f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
ed300 68 65 20 66 69 6c 65 20 28 55 54 46 2d 38 29 20 he file (UTF-8)
ed310 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
ed320 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f e *id, /
ed330 2a 20 57 72 69 74 65 20 74 68 65 20 53 51 4c 69 * Write the SQLi
ed340 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68 te file handle h
ed350 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ere */. int fla
ed360 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
ed370 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 /* Open mode
ed380 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a flags */. int *
ed390 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20 pOutFlags
ed3a0 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 /* Status r
ed3b0 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 eturn flags */.)
ed3c0 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 {. HANDLE h;.
ed3d0 44 57 4f 52 44 20 64 77 44 65 73 69 72 65 64 41 DWORD dwDesiredA
ed3e0 63 63 65 73 73 3b 0a 20 20 44 57 4f 52 44 20 64 ccess;. DWORD d
ed3f0 77 53 68 61 72 65 4d 6f 64 65 3b 0a 20 20 44 57 wShareMode;. DW
ed400 4f 52 44 20 64 77 43 72 65 61 74 69 6f 6e 44 69 ORD dwCreationDi
ed410 73 70 6f 73 69 74 69 6f 6e 3b 0a 20 20 44 57 4f sposition;. DWO
ed420 52 44 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 RD dwFlagsAndAtt
ed430 72 69 62 75 74 65 73 20 3d 20 30 3b 0a 23 69 66 ributes = 0;.#if
ed440 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ed450 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 20 3d 20 . int isTemp =
ed460 30 3b 0a 23 65 6e 64 69 66 0a 20 20 77 69 6e 46 0;.#endif. winF
ed470 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
ed480 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 76 6f 69 nFile*)id;. voi
ed490 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 20 20 d *zConverted;
ed4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ed4b0 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 4f 53 * Filename in OS
ed4c0 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 encoding */. c
ed4d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 74 66 38 onst char *zUtf8
ed4e0 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20 20 20 Name = zName;
ed4f0 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 /* Filename in
ed500 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 2a UTF-8 encoding *
ed510 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d /. char zTmpnam
ed520 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 20 20 e[MAX_PATH+1];
ed530 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 /* Buffer
ed540 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 used to create t
ed550 65 6d 70 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a emp filename */.
ed560 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63 . /* If the sec
ed570 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
ed580 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
ed590 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 NULL, generate
ed5a0 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 a . ** temporar
ed5b0 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 y file name to u
ed5c0 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 se . */. if( !
ed5d0 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20 zUtf8Name ){.
ed5e0 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d int rc = getTem
ed5f0 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b 31 pname(MAX_PATH+1
ed600 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 , zTmpname);.
ed610 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
ed620 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
ed630 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
ed640 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54 6d zUtf8Name = zTm
ed650 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a pname;. }.. /*
ed660 20 43 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c Convert the fil
ed670 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73 ename to the sys
ed680 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f tem encoding. */
ed690 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 . zConverted =
ed6a0 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e convertUtf8Filen
ed6b0 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a ame(zUtf8Name);.
ed6c0 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 if( zConverted
ed6d0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
ed6e0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
ed6f0 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 }.. if( flags
ed700 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & SQLITE_OPEN_R
ed710 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 EADWRITE ){.
ed720 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20 dwDesiredAccess
ed730 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 20 7c = GENERIC_READ |
ed740 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 3b 0a GENERIC_WRITE;.
ed750 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 44 }else{. dwD
ed760 65 73 69 72 65 64 41 63 63 65 73 73 20 3d 20 47 esiredAccess = G
ed770 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a 20 20 7d ENERIC_READ;. }
ed780 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 . if( flags & S
ed790 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
ed7a0 45 20 29 7b 0a 20 20 20 20 64 77 43 72 65 61 74 E ){. dwCreat
ed7b0 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d ionDisposition =
ed7c0 20 4f 50 45 4e 5f 41 4c 57 41 59 53 3b 0a 20 20 OPEN_ALWAYS;.
ed7d0 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 43 72 65 }else{. dwCre
ed7e0 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e ationDisposition
ed7f0 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 49 4e 47 = OPEN_EXISTING
ed800 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 ;. }. if( flag
ed810 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
ed820 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 64 MAIN_DB ){. d
ed830 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c wShareMode = FIL
ed840 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46 E_SHARE_READ | F
ed850 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b ILE_SHARE_WRITE;
ed860 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 . }else{. dw
ed870 53 68 61 72 65 4d 6f 64 65 20 3d 20 30 3b 0a 20 ShareMode = 0;.
ed880 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 }. if( flags &
ed890 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c SQLITE_OPEN_DEL
ed8a0 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 ETEONCLOSE ){.#i
ed8b0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
ed8c0 45 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 E. dwFlagsAnd
ed8d0 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c Attributes = FIL
ed8e0 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 E_ATTRIBUTE_HIDD
ed8f0 45 4e 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d EN;. isTemp =
ed900 20 31 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77 1;.#else. dw
ed910 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 FlagsAndAttribut
ed920 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 es = FILE_ATTRIB
ed930 55 54 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20 UTE_TEMPORARY.
ed940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ed950 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 | F
ed960 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 ILE_ATTRIBUTE_HI
ed970 44 44 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20 DDEN.
ed980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ed990 20 20 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f | FILE_FLAG_
ed9a0 44 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b DELETE_ON_CLOSE;
ed9b0 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b .#endif. }else{
ed9c0 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 . dwFlagsAndA
ed9d0 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 ttributes = FILE
ed9e0 5f 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 _ATTRIBUTE_NORMA
ed9f0 4c 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f L;. }. /* Repo
eda00 72 74 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 rts from the int
eda10 65 72 6e 65 74 20 61 72 65 20 74 68 61 74 20 70 ernet are that p
eda20 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c erformance is al
eda30 77 61 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72 ways. ** better
eda40 20 69 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 if FILE_FLAG_RA
eda50 4e 44 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75 NDOM_ACCESS is u
eda60 73 65 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 sed. Ticket #26
eda70 39 39 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 99. */.#if SQLIT
eda80 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46 E_OS_WINCE. dwF
eda90 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 lagsAndAttribute
edaa0 73 20 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52 s |= FILE_FLAG_R
edab0 41 4e 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65 ANDOM_ACCESS;.#e
edac0 6e 64 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28 ndif. if( isNT(
edad0 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 ) ){. h = Cre
edae0 61 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a ateFileW((WCHAR*
edaf0 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 )zConverted,.
edb00 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 dwDesiredAcc
edb10 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 ess,. dwSh
edb20 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 areMode,.
edb30 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 NULL,. dwC
edb40 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 reationDispositi
edb50 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 on,. dwFla
edb60 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c gsAndAttributes,
edb70 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 . NULL.
edb80 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 );./* isNT() is
edb90 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
edba0 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
edbb0 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
edbc0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 executed. .** Si
edbd0 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 nce the ASCII ve
edbe0 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 rsion of these W
edbf0 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f indows API do no
edc00 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 t exist for WINC
edc10 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 E,.** it's impor
edc20 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 tant to not refe
edc30 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 rence them for W
edc40 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a INCE builds..*/.
edc50 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
edc60 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a NCE==0. }else{.
edc70 20 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 h = CreateFi
edc80 6c 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 leA((char*)zConv
edc90 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 erted,. dw
edca0 44 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 DesiredAccess,.
edcb0 20 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 dwShareMod
edcc0 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a e,. NULL,.
edcd0 20 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f dwCreatio
edce0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 nDisposition,.
edcf0 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 dwFlagsAndA
edd00 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 ttributes,.
edd10 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 NULL. );.#e
edd20 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68 ndif. }. if( h
edd30 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 ==INVALID_HANDLE
edd40 5f 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72 _VALUE ){. fr
edd50 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
edd60 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 if( flags &
edd70 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
edd80 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72 WRITE ){. r
edd90 65 74 75 72 6e 20 77 69 6e 4f 70 65 6e 28 30 2c eturn winOpen(0,
edda0 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20 zName, id, .
eddb0 20 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 ((flag
eddc0 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 s|SQLITE_OPEN_RE
eddd0 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54 45 5f ADONLY)&~SQLITE_
edde0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 2c OPEN_READWRITE),
eddf0 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 pOutFlags);.
ede00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 }else{. re
ede10 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 turn SQLITE_CANT
ede20 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a OPEN;. }. }.
ede30 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 if( pOutFlags
ede40 29 7b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 ){. if( flags
ede50 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & SQLITE_OPEN_R
ede60 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 EADWRITE ){.
ede70 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 *pOutFlags = S
ede80 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
ede90 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b RITE;. }else{
edea0 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 . *pOutFlag
edeb0 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s = SQLITE_OPEN_
edec0 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a READONLY;. }.
eded0 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 }. memset(pFi
edee0 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 le, 0, sizeof(*p
edef0 46 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c 65 2d File));. pFile-
edf00 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77 69 6e 49 >pMethod = &winI
edf10 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 oMethod;. pFile
edf20 2d 3e 68 20 3d 20 68 3b 0a 23 69 66 20 53 51 4c ->h = h;.#if SQL
edf30 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 ITE_OS_WINCE. i
edf40 66 28 20 28 66 6c 61 67 73 20 26 20 28 53 51 4c f( (flags & (SQL
edf50 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
edf60 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d TE|SQLITE_OPEN_M
edf70 41 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20 20 20 AIN_DB)) ==.
edf80 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 (SQLI
edf90 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
edfa0 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 E|SQLITE_OPEN_MA
edfb0 49 4e 5f 44 42 29 0a 20 20 20 20 20 20 20 26 26 IN_DB). &&
edfc0 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 !winceCreateLoc
edfd0 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 29 0a k(zName, pFile).
edfe0 20 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65 48 61 ){. CloseHa
edff0 6e 64 6c 65 28 68 29 3b 0a 20 20 20 20 66 72 65 ndle(h);. fre
ee000 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
ee010 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
ee020 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 _CANTOPEN;. }.
ee030 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a 20 if( isTemp ){.
ee040 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 pFile->zDelet
ee050 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e 76 eOnClose = zConv
ee060 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 0a 23 erted;. }else.#
ee070 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 66 72 endif. {. fr
ee080 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
ee090 20 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 }. OpenCounte
ee0a0 72 28 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 r(+1);. return
ee0b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
ee0c0 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6e .** Delete the n
ee0d0 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a amed file..**.**
ee0e0 20 4e 6f 74 65 20 74 68 61 74 20 77 69 6e 64 6f Note that windo
ee0f0 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f ws does not allo
ee100 77 20 61 20 66 69 6c 65 20 74 6f 20 62 65 20 64 w a file to be d
ee110 65 6c 65 74 65 64 20 69 66 20 73 6f 6d 65 20 6f eleted if some o
ee120 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 ther.** process
ee130 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 20 53 6f has it open. So
ee140 6d 65 74 69 6d 65 73 20 61 20 76 69 72 75 73 20 metimes a virus
ee150 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64 65 78 scanner or index
ee160 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 77 ing program.** w
ee170 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75 72 6e ill open a journ
ee180 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c 79 20 al file shortly
ee190 61 66 74 65 72 20 69 74 20 69 73 20 63 72 65 61 after it is crea
ee1a0 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ted in order to
ee1b0 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72 20 69 do.** whatever i
ee1c0 74 20 64 6f 65 73 2e 20 20 57 68 69 6c 65 20 74 t does. While t
ee1d0 68 69 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 his other proces
ee1e0 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 s is holding the
ee1f0 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c 20 77 .** file open, w
ee200 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 62 6c 65 e will be unable
ee210 20 74 6f 20 64 65 6c 65 74 65 20 69 74 2e 20 20 to delete it.
ee220 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 To work around t
ee230 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c 20 his.** problem,
ee240 77 65 20 64 65 6c 61 79 20 31 30 30 20 6d 69 6c we delay 100 mil
ee250 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 liseconds and tr
ee260 79 20 74 6f 20 64 65 6c 65 74 65 20 61 67 61 69 y to delete agai
ee270 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d 58 5f n. Up.** to MX_
ee280 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 DELETION_ATTEMPT
ee290 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 65 6d s deletion attem
ee2a0 70 74 73 20 61 72 65 20 72 75 6e 20 62 65 66 6f pts are run befo
ee2b0 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75 70 20 re giving.** up
ee2c0 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e and returning an
ee2d0 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 error..*/.#defi
ee2e0 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 ne MX_DELETION_A
ee2f0 54 54 45 4d 50 54 53 20 35 0a 73 74 61 74 69 63 TTEMPTS 5.static
ee300 20 69 6e 74 20 77 69 6e 44 65 6c 65 74 65 28 0a int winDelete(.
ee310 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
ee320 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Vfs, /*
ee330 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e Not used on win
ee340 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 32 */. const ch
ee350 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
ee360 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 /* Name of f
ee370 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f ile to delete */
ee380 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72 20 20 . int syncDir
ee390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ee3a0 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 * Not used on wi
ee3b0 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 n32 */.){. int
ee3c0 63 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 cnt = 0;. DWORD
ee3d0 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 rc;. DWORD err
ee3e0 6f 72 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e or;. void *zCon
ee3f0 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 verted = convert
ee400 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 Utf8Filename(zFi
ee410 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a lename);. if( z
ee420 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a Converted==0 ){.
ee430 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ee440 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 E_NOMEM;. }. S
ee450 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 imulateIOError(r
ee460 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
ee470 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 RR_DELETE);. if
ee480 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
ee490 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 65 do{. Delete
ee4a0 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 FileW(zConverted
ee4b0 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 20 );. }while(
ee4c0 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74 46 ( ((rc = GetF
ee4d0 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 7a ileAttributesW(z
ee4e0 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20 49 Converted)) != I
ee4f0 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 NVALID_FILE_ATTR
ee500 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20 20 IBUTES).
ee510 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72 6f || ((erro
ee520 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 r = GetLastError
ee530 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 ()) == ERROR_ACC
ee540 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 ESS_DENIED)).
ee550 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63 6e && (++cn
ee560 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f t < MX_DELETION_
ee570 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20 20 ATTEMPTS).
ee580 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28 31 && (Sleep(1
ee590 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73 00), 1) );./* is
ee5a0 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c NT() is 1 if SQL
ee5b0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c ITE_OS_WINCE==1,
ee5c0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 so this else is
ee5d0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e never executed.
ee5e0 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 .** Since the A
ee5f0 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 SCII version of
ee600 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 these Windows AP
ee610 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 I do not exist f
ee620 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 or WINCE,.** it'
ee630 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e s important to n
ee640 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 ot reference the
ee650 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c m for WINCE buil
ee660 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ds..*/.#if SQLIT
ee670 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 E_OS_WINCE==0.
ee680 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 }else{. do{.
ee690 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 41 DeleteFileA
ee6a0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
ee6b0 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 20 }while( (
ee6c0 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 74 ((rc = GetFileAt
ee6d0 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e 76 65 tributesA(zConve
ee6e0 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 rted)) != INVALI
ee6f0 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 D_FILE_ATTRIBUTE
ee700 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 S).
ee710 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 || ((error = G
ee720 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 3d etLastError()) =
ee730 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 = ERROR_ACCESS_D
ee740 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 20 ENIED)).
ee750 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d && (++cnt < M
ee760 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d X_DELETION_ATTEM
ee770 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 PTS).
ee780 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 && (Sleep(100),
ee790 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 1) );.#endif. }
ee7a0 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 . free(zConvert
ee7b0 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 ed);. OSTRACE2(
ee7c0 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e "DELETE \"%s\"\n
ee7d0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 ", zFilename);.
ee7e0 20 72 65 74 75 72 6e 20 28 20 20 20 28 72 63 20 return ( (rc
ee7f0 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f == INVALID_FILE_
ee800 41 54 54 52 49 42 55 54 45 53 29 20 0a 20 20 20 ATTRIBUTES) .
ee810 20 20 20 20 20 20 20 26 26 20 28 65 72 72 6f 72 && (error
ee820 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e == ERROR_FILE_N
ee830 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c OT_FOUND)) ? SQL
ee840 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f ITE_OK : SQLITE_
ee850 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a IOERR_DELETE;.}.
ee860 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 ./*.** Check the
ee870 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73 existance and s
ee880 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e tatus of a file.
ee890 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
ee8a0 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 inAccess(. sqli
ee8b0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 te3_vfs *pVfs,
ee8c0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 /* Not us
ee8d0 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 ed on win32 */.
ee8e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
ee8f0 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e lename, /* N
ee900 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 ame of file to c
ee910 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c heck */. int fl
ee920 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
ee930 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 /* Type of
ee940 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 test to make on
ee950 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 this file */. i
ee960 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20 nt *pResOut
ee970 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
ee980 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 : Result */.){.
ee990 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20 69 DWORD attr;. i
ee9a0 6e 74 20 72 63 3b 0a 20 20 76 6f 69 64 20 2a 7a nt rc;. void *z
ee9b0 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 Converted = conv
ee9c0 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 ertUtf8Filename(
ee9d0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 zFilename);. if
ee9e0 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 ( zConverted==0
ee9f0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
eea00 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
eea10 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
eea20 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 attr = GetFi
eea30 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 28 57 leAttributesW((W
eea40 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
eea50 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
eea60 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
eea70 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
eea80 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
eea90 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e xecuted. .** Sin
eeaa0 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 ce the ASCII ver
eeab0 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 sion of these Wi
eeac0 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 ndows API do not
eead0 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 exist for WINCE
eeae0 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 ,.** it's import
eeaf0 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 ant to not refer
eeb00 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 ence them for WI
eeb10 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 NCE builds..*/.#
eeb20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
eeb30 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
eeb40 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c attr = GetFil
eeb50 65 41 74 74 72 69 62 75 74 65 73 41 28 28 63 68 eAttributesA((ch
eeb60 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b ar*)zConverted);
eeb70 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 .#endif. }. fr
eeb80 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
eeb90 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 switch( flags
eeba0 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
eebb0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a TE_ACCESS_READ:.
eebc0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
eebd0 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 ACCESS_EXISTS:.
eebe0 20 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d rc = attr!=
eebf0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
eec00 52 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62 RIBUTES;. b
eec10 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 reak;. case S
eec20 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
eec30 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 DWRITE:. rc
eec40 20 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f = (attr & FILE_
eec50 41 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e ATTRIBUTE_READON
eec60 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72 LY)==0;. br
eec70 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 eak;. default
eec80 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21 :. assert(!
eec90 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 "Invalid flags a
eeca0 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 rgument");. }.
eecb0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a *pResOut = rc;.
eecc0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
eecd0 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 OK;.}.../*.** Tu
eece0 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 rn a relative pa
eecf0 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 thname into a fu
eed00 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 ll pathname. Wr
eed10 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 ite the full.**
eed20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f pathname into zO
eed30 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69 ut[]. zOut[] wi
eed40 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 ll be at least p
eed50 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a Vfs->mxPathname.
eed60 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ** bytes in size
eed70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
eed80 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 winFullPathname(
eed90 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
eeda0 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 pVfs,
eedb0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 /* Pointer to v
eedc0 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 fs object */. c
eedd0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 onst char *zRela
eede0 74 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 tive, /*
eedf0 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 Possibly relativ
eee00 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a e input path */.
eee10 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 int nFull,
eee20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eee30 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 /* Size of outpu
eee40 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 t buffer in byte
eee50 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 s */. char *zFu
eee60 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ll
eee70 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 /* Output b
eee80 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a 23 69 66 uffer */.){..#if
eee90 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 defined(__CYGWI
eeea0 4e 5f 5f 29 0a 20 20 63 79 67 77 69 6e 5f 63 6f N__). cygwin_co
eeeb0 6e 76 5f 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 nv_to_full_win32
eeec0 5f 70 61 74 68 28 7a 52 65 6c 61 74 69 76 65 2c _path(zRelative,
eeed0 20 7a 46 75 6c 6c 29 3b 0a 20 20 72 65 74 75 72 zFull);. retur
eeee0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e n SQLITE_OK;.#en
eeef0 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f dif..#if SQLITE_
eef00 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 57 69 OS_WINCE. /* Wi
eef10 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f 6e 63 65 nCE has no conce
eef20 70 74 20 6f 66 20 61 20 72 65 6c 61 74 69 76 65 pt of a relative
eef30 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 20 73 6f pathname, or so
eef40 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f 0a 20 I am told. */.
eef50 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
eef60 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 f(pVfs->mxPathna
eef70 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c me, zFull, "%s",
eef80 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 72 zRelative);. r
eef90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
eefa0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 53 51 .#endif..#if !SQ
eefb0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26 LITE_OS_WINCE &&
eefc0 20 21 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 !defined(__CYGW
eefd0 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e 42 79 74 IN__). int nByt
eefe0 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 e;. void *zConv
eeff0 65 72 74 65 64 3b 0a 20 20 63 68 61 72 20 2a 7a erted;. char *z
ef000 4f 75 74 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 Out;. zConverte
ef010 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 d = convertUtf8F
ef020 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 ilename(zRelativ
ef030 65 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 e);. if( isNT()
ef040 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20 2a 7a ){. WCHAR *z
ef050 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 Temp;. nByte
ef060 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d = GetFullPathNam
ef070 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 eW((WCHAR*)zConv
ef080 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 erted, 0, 0, 0)
ef090 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d + 3;. zTemp =
ef0a0 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 malloc( nByte*s
ef0b0 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 izeof(zTemp[0])
ef0c0 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 );. if( zTemp
ef0d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 ==0 ){. fre
ef0e0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
ef0f0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
ef100 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
ef110 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e GetFullPathN
ef120 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f ameW((WCHAR*)zCo
ef130 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 nverted, nByte,
ef140 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 zTemp, 0);. f
ef150 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
ef160 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63 . zOut = unic
ef170 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 odeToUtf8(zTemp)
ef180 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70 ;. free(zTemp
ef190 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
ef1a0 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
ef1b0 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
ef1c0 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
ef1d0 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e xecuted. .** Sin
ef1e0 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 ce the ASCII ver
ef1f0 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 sion of these Wi
ef200 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 ndows API do not
ef210 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 exist for WINCE
ef220 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 ,.** it's import
ef230 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 ant to not refer
ef240 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 ence them for WI
ef250 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 NCE builds..*/.#
ef260 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ef270 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
ef280 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a char *zTemp;.
ef290 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46 nByte = GetF
ef2a0 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 ullPathNameA((ch
ef2b0 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 ar*)zConverted,
ef2c0 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 0, 0, 0) + 3;.
ef2d0 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 zTemp = malloc
ef2e0 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a ( nByte*sizeof(z
ef2f0 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 Temp[0]) );.
ef300 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a if( zTemp==0 ){.
ef310 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 free(zConv
ef320 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 erted);. re
ef330 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
ef340 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 M;. }. Get
ef350 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 FullPathNameA((c
ef360 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c har*)zConverted,
ef370 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 nByte, zTemp, 0
ef380 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e );. free(zCon
ef390 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 verted);. zOu
ef3a0 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 t = sqlite3_win3
ef3b0 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 2_mbcs_to_utf8(z
ef3c0 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 Temp);. free(
ef3d0 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20 zTemp);.#endif.
ef3e0 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b }. if( zOut ){
ef3f0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
ef400 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 rintf(pVfs->mxPa
ef410 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 thname, zFull, "
ef420 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 %s", zOut);.
ef430 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 free(zOut);.
ef440 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
ef450 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
ef460 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
ef470 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d EM;. }.#endif.}
ef480 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
ef490 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e _OMIT_LOAD_EXTEN
ef4a0 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 SION./*.** Inter
ef4b0 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e faces for openin
ef4c0 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 g a shared libra
ef4d0 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 ry, finding entr
ef4e0 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 y points.** with
ef4f0 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 in the shared li
ef500 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 brary, and closi
ef510 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 ng the shared li
ef520 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 brary..*/./*.**
ef530 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f Interfaces for o
ef540 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 pening a shared
ef550 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 library, finding
ef560 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a entry points.**
ef570 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 within the shar
ef580 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 ed library, and
ef590 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 closing the shar
ef5a0 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 ed library..*/.s
ef5b0 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 44 tatic void *winD
ef5c0 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 lOpen(sqlite3_vf
ef5d0 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 s *pVfs, const c
ef5e0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
ef5f0 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76 . HANDLE h;. v
ef600 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 oid *zConverted
ef610 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c = convertUtf8Fil
ef620 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 ename(zFilename)
ef630 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 ;. if( zConvert
ef640 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ed==0 ){. ret
ef650 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 urn 0;. }. if(
ef660 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 isNT() ){. h
ef670 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79 57 28 = LoadLibraryW(
ef680 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 (WCHAR*)zConvert
ef690 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 ed);./* isNT() i
ef6a0 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 s 1 if SQLITE_OS
ef6b0 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 _WINCE==1, so th
ef6c0 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 is else is never
ef6d0 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 executed. .** S
ef6e0 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 ince the ASCII v
ef6f0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 ersion of these
ef700 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e Windows API do n
ef710 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e ot exist for WIN
ef720 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f CE,.** it's impo
ef730 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 rtant to not ref
ef740 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 erence them for
ef750 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f WINCE builds..*/
ef760 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
ef770 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b INCE==0. }else{
ef780 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 . h = LoadLib
ef790 72 61 72 79 41 28 28 63 68 61 72 2a 29 7a 43 6f raryA((char*)zCo
ef7a0 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 nverted);.#endif
ef7b0 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e . }. free(zCon
ef7c0 76 65 72 74 65 64 29 3b 0a 20 20 72 65 74 75 72 verted);. retur
ef7d0 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d 0a 73 74 n (void*)h;.}.st
ef7e0 61 74 69 63 20 76 6f 69 64 20 77 69 6e 44 6c 45 atic void winDlE
ef7f0 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
ef800 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 *pVfs, int nBuf
ef810 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 , char *zBufOut)
ef820 7b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f 72 {. getLastError
ef830 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 Msg(nBuf, zBufOu
ef840 74 29 3b 0a 7d 0a 76 6f 69 64 20 2a 77 69 6e 44 t);.}.void *winD
ef850 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 lSym(sqlite3_vfs
ef860 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 *pVfs, void *pH
ef870 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 andle, const cha
ef880 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 23 69 66 r *zSymbol){.#if
ef890 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ef8a0 0a 20 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f . /* The GetPro
ef8b0 63 41 64 64 72 65 73 73 41 28 29 20 72 6f 75 74 cAddressA() rout
ef8c0 69 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 ine is only avai
ef8d0 6c 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20 lable on wince.
ef8e0 2a 2f 0a 20 20 72 65 74 75 72 6e 20 47 65 74 50 */. return GetP
ef8f0 72 6f 63 41 64 64 72 65 73 73 41 28 28 48 41 4e rocAddressA((HAN
ef900 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 DLE)pHandle, zSy
ef910 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f mbol);.#else. /
ef920 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 * All other wind
ef930 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 ows platforms ex
ef940 70 65 63 74 20 47 65 74 50 72 6f 63 41 64 64 72 pect GetProcAddr
ef950 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 ess() to take.
ef960 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 72 69 6e ** an Ansi strin
ef970 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 g regardless of
ef980 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 the _UNICODE set
ef990 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e ting */. return
ef9a0 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 28 GetProcAddress(
ef9b0 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c (HANDLE)pHandle,
ef9c0 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 zSymbol);.#endi
ef9d0 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c f.}.void winDlCl
ef9e0 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ose(sqlite3_vfs
ef9f0 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 *pVfs, void *pHa
efa00 6e 64 6c 65 29 7b 0a 20 20 46 72 65 65 4c 69 62 ndle){. FreeLib
efa10 72 61 72 79 28 28 48 41 4e 44 4c 45 29 70 48 61 rary((HANDLE)pHa
efa20 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f ndle);.}.#else /
efa30 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 * if SQLITE_OMIT
efa40 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 _LOAD_EXTENSION
efa50 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 is defined: */.
efa60 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 4f 70 #define winDlOp
efa70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 en 0. #define
efa80 77 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 winDlError 0. #
efa90 64 65 66 69 6e 65 20 77 69 6e 44 6c 53 79 6d 20 define winDlSym
efaa0 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 0. #define wi
efab0 6e 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 nDlClose 0.#endi
efac0 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 f.../*.** Write
efad0 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 up to nBuf bytes
efae0 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 of randomness i
efaf0 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 nto zBuf..*/.sta
efb00 74 69 63 20 69 6e 74 20 77 69 6e 52 61 6e 64 6f tic int winRando
efb10 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 mness(sqlite3_vf
efb20 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 s *pVfs, int nBu
efb30 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
efb40 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 55 int n = 0;. U
efb50 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
efb60 70 56 66 73 29 3b 0a 23 69 66 20 64 65 66 69 6e pVfs);.#if defin
efb70 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a ed(SQLITE_TEST).
efb80 20 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 n = nBuf;. me
efb90 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 mset(zBuf, 0, nB
efba0 75 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 uf);.#else. if(
efbb0 20 73 69 7a 65 6f 66 28 53 59 53 54 45 4d 54 49 sizeof(SYSTEMTI
efbc0 4d 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 ME)<=nBuf-n ){.
efbd0 20 20 20 53 59 53 54 45 4d 54 49 4d 45 20 78 3b SYSTEMTIME x;
efbe0 0a 20 20 20 20 47 65 74 53 79 73 74 65 6d 54 69 . GetSystemTi
efbf0 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 me(&x);. memc
efc00 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c py(&zBuf[n], &x,
efc10 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 sizeof(x));.
efc20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b n += sizeof(x);
efc30 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f . }. if( sizeo
efc40 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e f(DWORD)<=nBuf-n
efc50 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 70 69 ){. DWORD pi
efc60 64 20 3d 20 47 65 74 43 75 72 72 65 6e 74 50 72 d = GetCurrentPr
efc70 6f 63 65 73 73 49 64 28 29 3b 0a 20 20 20 20 6d ocessId();. m
efc80 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 emcpy(&zBuf[n],
efc90 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 &pid, sizeof(pid
efca0 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a ));. n += siz
efcb0 65 6f 66 28 70 69 64 29 3b 0a 20 20 7d 0a 20 20 eof(pid);. }.
efcc0 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 if( sizeof(DWORD
efcd0 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 )<=nBuf-n ){.
efce0 20 44 57 4f 52 44 20 63 6e 74 20 3d 20 47 65 74 DWORD cnt = Get
efcf0 54 69 63 6b 43 6f 75 6e 74 28 29 3b 0a 20 20 20 TickCount();.
efd00 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
efd10 2c 20 26 63 6e 74 2c 20 73 69 7a 65 6f 66 28 63 , &cnt, sizeof(c
efd20 6e 74 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 nt));. n += s
efd30 69 7a 65 6f 66 28 63 6e 74 29 3b 0a 20 20 7d 0a izeof(cnt);. }.
efd40 20 20 69 66 28 20 73 69 7a 65 6f 66 28 4c 41 52 if( sizeof(LAR
efd50 47 45 5f 49 4e 54 45 47 45 52 29 3c 3d 6e 42 75 GE_INTEGER)<=nBu
efd60 66 2d 6e 20 29 7b 0a 20 20 20 20 4c 41 52 47 45 f-n ){. LARGE
efd70 5f 49 4e 54 45 47 45 52 20 69 3b 0a 20 20 20 20 _INTEGER i;.
efd80 51 75 65 72 79 50 65 72 66 6f 72 6d 61 6e 63 65 QueryPerformance
efd90 43 6f 75 6e 74 65 72 28 26 69 29 3b 0a 20 20 20 Counter(&i);.
efda0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
efdb0 2c 20 26 69 2c 20 73 69 7a 65 6f 66 28 69 29 29 , &i, sizeof(i))
efdc0 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f ;. n += sizeo
efdd0 66 28 69 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 f(i);. }.#endif
efde0 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a . return n;.}..
efdf0 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 ./*.** Sleep for
efe00 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e a little while.
efe10 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f Return the amo
efe20 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 unt of time slep
efe30 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
efe40 20 77 69 6e 53 6c 65 65 70 28 73 71 6c 69 74 65 winSleep(sqlite
efe50 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
efe60 20 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20 53 6c microsec){. Sl
efe70 65 65 70 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 eep((microsec+99
efe80 39 29 2f 31 30 30 30 29 3b 0a 20 20 72 65 74 75 9)/1000);. retu
efe90 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 rn ((microsec+99
efea0 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 7d 9)/1000)*1000;.}
efeb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
efec0 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 owing variable,
efed0 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d if set to a non-
efee0 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f zero value, beco
efef0 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a mes the result.*
eff00 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 * returned from
eff10 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
eff20 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 Time(). This is
eff30 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
eff40 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c g..*/.#ifdef SQL
eff50 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
eff60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
eff70 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 current_time = 0
eff80 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
eff90 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 Find the current
effa0 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 time (in Univer
effb0 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 sal Coordinated
effc0 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 Time). Write th
effd0 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d e.** current tim
effe0 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 e and date as a
efff0 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 Julian Day numbe
f0000 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e r into *prNow an
f0010 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 d.** return 0.
f0020 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
f0030 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 time and date ca
f0040 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a nnot be found..*
f0050 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e 74 /.int winCurrent
f0060 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 Time(sqlite3_vfs
f0070 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a *pVfs, double *
f0080 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54 49 prNow){. FILETI
f0090 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c 45 ME ft;. /* FILE
f00a0 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20 69 TIME structure i
f00b0 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 65 s a 64-bit value
f00c0 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 representing th
f00d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20 20 e number of .
f00e0 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 100-nanosecond
f00f0 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63 65 intervals since
f0100 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30 31 January 1, 1601
f0110 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e 35 (= JD 2305813.5
f0120 29 2e 20 0a 20 20 2a 2f 0a 20 20 64 6f 75 62 6c ). . */. doubl
f0130 65 20 6e 6f 77 3b 0a 23 69 66 20 53 51 4c 49 54 e now;.#if SQLIT
f0140 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59 53 E_OS_WINCE. SYS
f0150 54 45 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20 TEMTIME time;.
f0160 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74 GetSystemTime(&t
f0170 69 6d 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53 79 ime);. /* if Sy
f0180 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 stemTimeToFileTi
f0190 6d 65 28 29 20 66 61 69 6c 73 2c 20 69 74 20 72 me() fails, it r
f01a0 65 74 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f 0a eturns zero. */.
f01b0 20 20 69 66 20 28 21 53 79 73 74 65 6d 54 69 6d if (!SystemTim
f01c0 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 6d eToFileTime(&tim
f01d0 65 2c 26 66 74 29 29 7b 0a 20 20 20 20 72 65 74 e,&ft)){. ret
f01e0 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6c 73 65 urn 1;. }.#else
f01f0 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 . GetSystemTime
f0200 41 73 46 69 6c 65 54 69 6d 65 28 20 26 66 74 20 AsFileTime( &ft
f0210 29 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 6f 77 20 );.#endif. now
f0220 3d 20 28 28 64 6f 75 62 6c 65 29 66 74 2e 64 77 = ((double)ft.dw
f0230 48 69 67 68 44 61 74 65 54 69 6d 65 29 20 2a 20 HighDateTime) *
f0240 34 32 39 34 39 36 37 32 39 36 2e 30 3b 20 0a 20 4294967296.0; .
f0250 20 2a 70 72 4e 6f 77 20 3d 20 28 6e 6f 77 20 2b *prNow = (now +
f0260 20 66 74 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d ft.dwLowDateTim
f0270 65 29 2f 38 36 34 30 30 30 30 30 30 30 30 30 2e e)/864000000000.
f0280 30 20 2b 20 32 33 30 35 38 31 33 2e 35 3b 0a 23 0 + 2305813.5;.#
f0290 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
f02a0 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f T. if( sqlite3_
f02b0 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a current_time ){.
f02c0 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c *prNow = sql
f02d0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d ite3_current_tim
f02e0 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 e/86400.0 + 2440
f02f0 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 587.5;. }.#endi
f0300 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a f. return 0;.}.
f0310 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 ./*.** The idea
f0320 69 73 20 74 68 61 74 20 74 68 69 73 20 66 75 6e is that this fun
f0330 63 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 ction works like
f0340 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f a combination o
f0350 66 0a 2a 2a 20 47 65 74 4c 61 73 74 45 72 72 6f f.** GetLastErro
f0360 72 28 29 20 61 6e 64 20 46 6f 72 6d 61 74 4d 65 r() and FormatMe
f0370 73 73 61 67 65 28 29 20 6f 6e 20 77 69 6e 64 6f ssage() on windo
f0380 77 73 20 28 6f 72 20 65 72 72 6e 6f 20 61 6e 64 ws (or errno and
f0390 0a 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72 28 29 .** strerror_r()
f03a0 20 6f 6e 20 75 6e 69 78 29 2e 20 41 66 74 65 72 on unix). After
f03b0 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 an error is ret
f03c0 75 72 6e 65 64 20 62 79 20 61 6e 20 4f 53 0a 2a urned by an OS.*
f03d0 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c 69 * function, SQLi
f03e0 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75 te calls this fu
f03f0 6e 63 74 69 6f 6e 20 77 69 74 68 20 7a 42 75 66 nction with zBuf
f0400 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 pointing to.**
f0410 61 20 62 75 66 66 65 72 20 6f 66 20 6e 42 75 66 a buffer of nBuf
f0420 20 62 79 74 65 73 2e 20 54 68 65 20 4f 53 20 6c bytes. The OS l
f0430 61 79 65 72 20 73 68 6f 75 6c 64 20 70 6f 70 75 ayer should popu
f0440 6c 61 74 65 20 74 68 65 0a 2a 2a 20 62 75 66 66 late the.** buff
f0450 65 72 20 77 69 74 68 20 61 20 6e 75 6c 2d 74 65 er with a nul-te
f0460 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 65 rminated UTF-8 e
f0470 6e 63 6f 64 65 64 20 65 72 72 6f 72 20 6d 65 73 ncoded error mes
f0480 73 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 62 69 sage.** describi
f0490 6e 67 20 74 68 65 20 6c 61 73 74 20 49 4f 20 65 ng the last IO e
f04a0 72 72 6f 72 20 74 6f 20 68 61 76 65 20 6f 63 63 rror to have occ
f04b0 75 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 ured within the
f04c0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 72 65 61 calling.** threa
f04d0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
f04e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 error message is
f04f0 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20 74 too large for t
f0500 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66 66 he supplied buff
f0510 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f 75 6c 64 er,.** it should
f0520 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e 20 54 be truncated. T
f0530 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
f0540 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 of xGetLastError
f0550 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74 .** is zero if t
f0560 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
f0570 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66 fits in the buf
f0580 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f fer, or non-zero
f0590 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69 .** otherwise (i
f05a0 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61 f the message wa
f05b0 73 20 74 72 75 6e 63 61 74 65 64 29 2e 20 49 66 s truncated). If
f05c0 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65 74 non-zero is ret
f05d0 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 69 urned,.** then i
f05e0 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 t is not necessa
f05f0 72 79 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 ry to include th
f0600 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 e nul-terminator
f0610 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69 6e character.** in
f0620 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 the output buff
f0630 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 73 75 er..**.** Not su
f0640 70 70 6c 79 69 6e 67 20 61 6e 20 65 72 72 6f 72 pplying an error
f0650 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20 68 61 message will ha
f0660 76 65 20 6e 6f 20 61 64 76 65 72 73 65 20 65 66 ve no adverse ef
f0670 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51 4c 69 74 fect.** on SQLit
f0680 65 2e 20 49 74 20 69 73 20 66 69 6e 65 20 74 6f e. It is fine to
f0690 20 68 61 76 65 20 61 6e 20 69 6d 70 6c 65 6d 65 have an impleme
f06a0 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6e 65 76 ntation that nev
f06b0 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 6e er.** returns an
f06c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 3a 0a error message:.
f06d0 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78 47 65 74 **.** int xGet
f06e0 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 LastError(sqlite
f06f0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
f0700 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
f0710 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 f){.** asser
f0720 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c 30 27 29 t(zBuf[0]=='\0')
f0730 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75 72 6e 20 ;.** return
f0740 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 0;.** }.**.**
f0750 48 6f 77 65 76 65 72 20 69 66 20 61 6e 20 65 72 However if an er
f0760 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 73 ror message is s
f0770 75 70 70 6c 69 65 64 2c 20 69 74 20 77 69 6c 6c upplied, it will
f0780 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 74 65 64 be incorporated
f0790 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20 69 6e .** by sqlite in
f07a0 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 to the error mes
f07b0 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65 20 74 sage available t
f07c0 6f 20 74 68 65 20 75 73 65 72 20 75 73 69 6e 67 o the user using
f07d0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d .** sqlite3_errm
f07e0 73 67 28 29 2c 20 70 6f 73 73 69 62 6c 79 20 6d sg(), possibly m
f07f0 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 20 aking IO errors
f0800 65 61 73 69 65 72 20 74 6f 20 64 65 62 75 67 2e easier to debug.
f0810 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
f0820 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 inGetLastError(s
f0830 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
f0840 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
f0850 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 *zBuf){. retur
f0860 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 n getLastErrorMs
f0870 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a 7d g(nBuf, zBuf);.}
f0880 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
f0890 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c ze and deinitial
f08a0 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e ize the operatin
f08b0 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 g system interfa
f08c0 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ce..*/.SQLITE_AP
f08d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 I int sqlite3_os
f08e0 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 73 _init(void){. s
f08f0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 tatic sqlite3_vf
f0900 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 20 s winVfs = {.
f0910 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
f0920 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 /* iVersion
f0930 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 69 */. sizeof(wi
f0940 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f nFile), /* szO
f0950 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58 sFile */. MAX
f0960 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20 20 _PATH,
f0970 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f /* mxPathname */
f0980 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
f0990 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
f09a0 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22 2c */. "win32",
f09b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e /* zN
f09c0 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ame */. 0,
f09d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f09e0 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a 20 pAppData */. .
f09f0 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20 20 winOpen,
f0a00 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a /* xOpen *
f0a10 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65 2c /. winDelete,
f0a20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c /* xDel
f0a30 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63 ete */. winAc
f0a40 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a cess, /*
f0a50 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 xAccess */.
f0a60 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c winFullPathname,
f0a70 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e /* xFullPathn
f0a80 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c ame */. winDl
f0a90 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a Open, /*
f0aa0 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 xDlOpen */.
f0ab0 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 winDlError,
f0ac0 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a /* xDlError *
f0ad0 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20 /. winDlSym,
f0ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 /* xDlS
f0af0 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 43 ym */. winDlC
f0b00 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 lose, /*
f0b10 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 xDlClose */.
f0b20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 winRandomness,
f0b30 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 /* xRandomnes
f0b40 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65 s */. winSlee
f0b50 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 p, /* x
f0b60 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e Sleep */. win
f0b70 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 CurrentTime,
f0b80 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 /* xCurrentTime
f0b90 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 73 */. winGetLas
f0ba0 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65 tError /* xGe
f0bb0 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20 tLastError */.
f0bc0 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 };. sqlite3_vfs
f0bd0 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56 66 _register(&winVf
f0be0 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 s, 1);. return
f0bf0 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 51 SQLITE_OK; .}.SQ
f0c00 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
f0c10 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 ite3_os_end(void
f0c20 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c ){ . return SQL
f0c30 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 ITE_OK;.}..#endi
f0c40 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 f /* SQLITE_OS_W
f0c50 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a IN */../********
f0c60 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 ****** End of os
f0c70 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _win.c *********
f0c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f0c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f0ca0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
f0cb0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
f0cc0 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a e bitvec.c *****
f0cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f0ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f0cf0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
f0d00 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 2a 2a 8 February 16.**
f0d10 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
f0d20 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
f0d30 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
f0d40 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
f0d50 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
f0d60 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
f0d70 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
f0d80 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
f0d90 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
f0da0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
f0db0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
f0dc0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
f0dd0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
f0de0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
f0df0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
f0e00 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
f0e10 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
f0e20 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
f0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f0e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
f0e70 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d This file implem
f0e80 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 74 ents an object t
f0e90 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 hat represents a
f0ea0 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a 2a 2a fixed-length.**
f0eb0 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 20 61 bitmap. Bits a
f0ec0 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 re numbered star
f0ed0 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a 2a 0a ting with 1..**.
f0ee0 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20 75 ** A bitmap is u
f0ef0 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68 sed to record wh
f0f00 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 20 64 ich pages of a d
f0f10 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 76 atabase file hav
f0f20 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 e been.** journa
f0f30 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 74 72 lled during a tr
f0f40 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 77 68 ansaction, or wh
f0f50 69 63 68 20 70 61 67 65 73 20 68 61 76 65 20 74 ich pages have t
f0f60 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 22 0a he "dont-write".
f0f70 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 55 73 ** property. Us
f0f80 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 65 77 ually only a few
f0f90 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 74 20 pages are meet
f0fa0 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e either condition
f0fb0 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 74 6d ..** So the bitm
f0fc0 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 73 70 ap is usually sp
f0fd0 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c 6f 77 arse and has low
f0fe0 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a 2a 2a cardinality..**
f0ff0 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 28 But sometimes (
f1000 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65 6e for example when
f1010 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 20 6f during a DROP o
f1020 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c 65 29 f a large table)
f1030 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c 20 most.** or all
f1040 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 6e 20 of the pages in
f1050 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 67 a database can g
f1060 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 20 et journalled.
f1070 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 In those cases,
f1080 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 20 62 .** the bitmap b
f1090 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 69 74 ecomes dense wit
f10a0 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 6c 69 h high cardinali
f10b0 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 69 74 ty. The algorit
f10c0 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 6f 20 hm needs .** to
f10d0 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 73 65 handle both case
f10e0 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 s well..**.** Th
f10f0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 69 e size of the bi
f1100 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 77 68 tmap is fixed wh
f1110 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 en the object is
f1120 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 created..**.**
f1130 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 6c 65 All bits are cle
f1140 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 74 6d ar when the bitm
f1150 61 70 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 ap is created.
f1160 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 0a Individual bits.
f1170 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 6f 72 ** may be set or
f1180 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 74 20 cleared one at
f1190 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 65 a time..**.** Te
f11a0 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 st operations ar
f11b0 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 6d 65 e about 100 time
f11c0 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68 s more common th
f11d0 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e at set operation
f11e0 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72 s..** Clear oper
f11f0 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 65 65 ations are excee
f1200 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 54 68 dingly rare. Th
f1210 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c 79 20 ere are usually
f1220 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 6e 64 between.** 5 and
f1230 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 74 69 500 set operati
f1240 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 20 6f ons per Bitvec o
f1250 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 74 68 bject, though th
f1260 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 e number of sets
f1270 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 can.** sometime
f1280 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 6e 73 s grow into tens
f1290 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 6f 72 of thousands or
f12a0 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 73 69 larger. The si
f12b0 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 69 74 ze of the.** Bit
f12c0 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 74 68 vec object is th
f12d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
f12e0 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 s in the databas
f12f0 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a e file at the.**
f1300 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 61 6e start of a tran
f1310 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 73 20 saction, and is
f1320 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c 65 73 thus usually les
f1330 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 68 6f s than a few tho
f1340 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 61 usand,.** but ca
f1350 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 61 73 n be as large as
f1360 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 61 2 billion for a
f1370 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 74 61 really big data
f1380 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 base..**.** @(#)
f1390 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63 2c 76 $Id: bitvec.c,v
f13a0 20 31 2e 39 20 32 30 30 38 2f 31 31 2f 31 39 20 1.9 2008/11/19
f13b0 31 38 3a 33 30 3a 33 35 20 73 68 61 6e 65 20 45 18:30:35 shane E
f13c0 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a 65 xp $.*/../* Size
f13d0 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 73 of the Bitvec s
f13e0 74 72 75 63 74 75 72 65 20 69 6e 20 62 79 74 65 tructure in byte
f13f0 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 s. */.#define BI
f1400 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20 35 TVEC_SZ 5
f1410 31 32 0a 0a 2f 2a 20 52 6f 75 6e 64 20 74 68 65 12../* Round the
f1420 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77 6e union size down
f1430 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 to the nearest
f1440 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72 79 pointer boundary
f1450 2c 20 73 69 6e 63 65 20 74 68 61 74 27 73 20 68 , since that's h
f1460 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 62 ow .** it will b
f1470 65 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69 6e e aligned within
f1480 20 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75 the Bitvec stru
f1490 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 ct. */.#define B
f14a0 49 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20 20 ITVEC_USIZE
f14b0 28 28 28 42 49 54 56 45 43 5f 53 5a 2d 28 33 2a (((BITVEC_SZ-(3*
f14c0 73 69 7a 65 6f 66 28 75 33 32 29 29 29 2f 73 69 sizeof(u32)))/si
f14d0 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 2a 73 zeof(Bitvec*))*s
f14e0 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 0a izeof(Bitvec*)).
f14f0 0a 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 ./* Type of the
f1500 61 72 72 61 79 20 22 65 6c 65 6d 65 6e 74 22 20 array "element"
f1510 66 6f 72 20 74 68 65 20 62 69 74 6d 61 70 20 72 for the bitmap r
f1520 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 0a epresentation. .
f1530 2a 2a 20 53 68 6f 75 6c 64 20 62 65 20 61 20 70 ** Should be a p
f1540 6f 77 65 72 20 6f 66 20 32 2c 20 61 6e 64 20 69 ower of 2, and i
f1550 64 65 61 6c 6c 79 2c 20 65 76 65 6e 6c 79 20 64 deally, evenly d
f1560 69 76 69 64 65 20 69 6e 74 6f 20 42 49 54 56 45 ivide into BITVE
f1570 43 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 C_USIZE. .** Set
f1580 74 69 6e 67 20 74 68 69 73 20 74 6f 20 74 68 65 ting this to the
f1590 20 22 6e 61 74 75 72 61 6c 20 77 6f 72 64 22 20 "natural word"
f15a0 73 69 7a 65 20 6f 66 20 79 6f 75 72 20 43 50 55 size of your CPU
f15b0 20 6d 61 79 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 may improve.**
f15c0 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a performance. */.
f15d0 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 54 #define BITVEC_T
f15e0 45 4c 45 4d 20 20 20 20 20 75 38 0a 2f 2a 20 53 ELEM u8./* S
f15f0 69 7a 65 2c 20 69 6e 20 62 69 74 73 2c 20 6f 66 ize, in bits, of
f1600 20 74 68 65 20 62 69 74 6d 61 70 20 65 6c 65 6d the bitmap elem
f1610 65 6e 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ent. */.#define
f1620 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 20 20 20 BITVEC_SZELEM
f1630 20 38 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 8./* Number of
f1640 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 20 62 69 elements in a bi
f1650 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23 tmap array. */.#
f1660 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 45 define BITVEC_NE
f1670 4c 45 4d 20 20 20 20 20 28 42 49 54 56 45 43 5f LEM (BITVEC_
f1680 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 49 54 USIZE/sizeof(BIT
f1690 56 45 43 5f 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e VEC_TELEM))./* N
f16a0 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e umber of bits in
f16b0 20 74 68 65 20 62 69 74 6d 61 70 20 61 72 72 61 the bitmap arra
f16c0 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 y. */.#define BI
f16d0 54 56 45 43 5f 4e 42 49 54 20 20 20 20 20 20 28 TVEC_NBIT (
f16e0 42 49 54 56 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 BITVEC_NELEM*BIT
f16f0 56 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 VEC_SZELEM)../*
f1700 4e 75 6d 62 65 72 20 6f 66 20 75 33 32 20 76 61 Number of u32 va
f1710 6c 75 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 lues in hash tab
f1720 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 le. */.#define B
f1730 49 54 56 45 43 5f 4e 49 4e 54 20 20 20 20 20 20 ITVEC_NINT
f1740 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 (BITVEC_USIZE/si
f1750 7a 65 6f 66 28 75 33 32 29 29 0a 2f 2a 20 4d 61 zeof(u32))./* Ma
f1760 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
f1770 65 6e 74 72 69 65 73 20 69 6e 20 68 61 73 68 20 entries in hash
f1780 74 61 62 6c 65 20 62 65 66 6f 72 65 20 0a 2a 2a table before .**
f1790 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 6e sub-dividing an
f17a0 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f d re-hashing. */
f17b0 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f .#define BITVEC_
f17c0 4d 58 48 41 53 48 20 20 20 20 28 42 49 54 56 45 MXHASH (BITVE
f17d0 43 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 C_NINT/2)./* Has
f17e0 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f hing function fo
f17f0 72 20 74 68 65 20 61 48 61 73 68 20 72 65 70 72 r the aHash repr
f1800 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 esentation..** E
f1810 6d 70 69 72 69 63 61 6c 20 74 65 73 74 69 6e 67 mpirical testing
f1820 20 73 68 6f 77 65 64 20 74 68 61 74 20 74 68 65 showed that the
f1830 20 2a 33 37 20 6d 75 6c 74 69 70 6c 69 65 72 20 *37 multiplier
f1840 0a 2a 2a 20 28 61 6e 20 61 72 62 69 74 72 61 72 .** (an arbitrar
f1850 79 20 70 72 69 6d 65 29 69 6e 20 74 68 65 20 68 y prime)in the h
f1860 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 70 72 6f ash function pro
f1870 76 69 64 65 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 vided .** no few
f1880 65 72 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 er collisions th
f1890 61 6e 20 74 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e an the no-op *1.
f18a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 */.#define BITV
f18b0 45 43 5f 48 41 53 48 28 58 29 20 20 20 28 28 28 EC_HASH(X) (((
f18c0 58 29 2a 31 29 25 42 49 54 56 45 43 5f 4e 49 4e X)*1)%BITVEC_NIN
f18d0 54 29 0a 0a 23 64 65 66 69 6e 65 20 42 49 54 56 T)..#define BITV
f18e0 45 43 5f 4e 50 54 52 20 20 20 20 20 20 28 42 49 EC_NPTR (BI
f18f0 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f TVEC_USIZE/sizeo
f1900 66 28 42 69 74 76 65 63 20 2a 29 29 0a 0a 0a 2f f(Bitvec *)).../
f1910 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 *.** A bitmap is
f1920 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
f1930 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
f1940 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
f1950 68 69 73 20 62 69 74 6d 61 70 20 72 65 63 6f 72 his bitmap recor
f1960 64 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 ds the existance
f1970 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 of zero or more
f1980 20 62 69 74 73 0a 2a 2a 20 77 69 74 68 20 76 61 bits.** with va
f1990 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 lues between 1 a
f19a0 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75 73 nd iSize, inclus
f19b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 ive..**.** There
f19c0 20 61 72 65 20 74 68 72 65 65 20 70 6f 73 73 69 are three possi
f19d0 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 ble representati
f19e0 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61 ons of the bitma
f19f0 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c 3d p..** If iSize<=
f1a00 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68 65 BITVEC_NBIT, the
f1a10 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74 6d n Bitvec.u.aBitm
f1a20 61 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69 67 ap[] is a straig
f1a30 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 54 ht.** bitmap. T
f1a40 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69 he least signifi
f1a50 63 61 6e 74 20 62 69 74 20 69 73 20 62 69 74 20 cant bit is bit
f1a60 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 1..**.** If iSiz
f1a70 65 3e 42 49 54 56 45 43 5f 4e 42 49 54 20 61 6e e>BITVEC_NBIT an
f1a80 64 20 69 44 69 76 69 73 6f 72 3d 3d 30 20 74 68 d iDivisor==0 th
f1a90 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 48 61 73 en Bitvec.u.aHas
f1aa0 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 61 73 68 h[] is.** a hash
f1ab0 20 74 61 62 6c 65 20 74 68 61 74 20 77 69 6c 6c table that will
f1ac0 20 68 6f 6c 64 20 75 70 20 74 6f 20 42 49 54 56 hold up to BITV
f1ad0 45 43 5f 4d 58 48 41 53 48 20 64 69 73 74 69 6e EC_MXHASH distin
f1ae0 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a ct values..**.**
f1af0 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
f1b00 76 61 6c 75 65 20 69 20 69 73 20 72 65 64 69 72 value i is redir
f1b10 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 20 6f ected into one o
f1b20 66 20 42 49 54 56 45 43 5f 4e 50 54 52 0a 2a 2a f BITVEC_NPTR.**
f1b30 20 73 75 62 2d 62 69 74 6d 61 70 73 20 70 6f 69 sub-bitmaps poi
f1b40 6e 74 65 64 20 74 6f 20 62 79 20 42 69 74 76 65 nted to by Bitve
f1b50 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 20 45 61 c.u.apSub[]. Ea
f1b60 63 68 20 73 75 62 62 69 74 6d 61 70 0a 2a 2a 20 ch subbitmap.**
f1b70 68 61 6e 64 6c 65 73 20 75 70 20 74 6f 20 69 44 handles up to iD
f1b80 69 76 69 73 6f 72 20 73 65 70 61 72 61 74 65 20 ivisor separate
f1b90 76 61 6c 75 65 73 20 6f 66 20 69 2e 20 20 61 70 values of i. ap
f1ba0 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 Sub[0] holds.**
f1bb0 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 values between 1
f1bc0 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 20 20 and iDivisor.
f1bd0 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64 73 20 76 apSub[1] holds v
f1be0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a alues between.**
f1bf0 20 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 iDivisor+1 and
f1c00 32 2a 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 2*iDivisor. apS
f1c10 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 61 6c 75 ub[N] holds valu
f1c20 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a es between.** N*
f1c30 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 28 iDivisor+1 and (
f1c40 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72 2e 20 20 N+1)*iDivisor.
f1c50 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 20 69 Each subbitmap i
f1c60 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 s normalized.**
f1c70 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20 77 69 74 to hold deal wit
f1c80 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e h values between
f1c90 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 1 and iDivisor.
f1ca0 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69 74 76 65 .*/.struct Bitve
f1cb0 63 20 7b 0a 20 20 75 33 32 20 69 53 69 7a 65 3b c {. u32 iSize;
f1cc0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
f1cd0 20 62 69 74 20 69 6e 64 65 78 2e 20 20 4d 61 78 bit index. Max
f1ce0 20 69 53 69 7a 65 20 69 73 20 34 2c 32 39 34 2c iSize is 4,294,
f1cf0 39 36 37 2c 32 39 36 2e 20 2a 2f 0a 20 20 75 33 967,296. */. u3
f1d00 32 20 6e 53 65 74 3b 20 20 20 20 20 20 20 2f 2a 2 nSet; /*
f1d10 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 Number of bits
f1d20 74 68 61 74 20 61 72 65 20 73 65 74 20 2d 20 6f that are set - o
f1d30 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 48 nly valid for aH
f1d40 61 73 68 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 ash element */.
f1d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f1d60 20 2f 2a 20 4d 61 78 20 6e 53 65 74 20 69 73 20 /* Max nSet is
f1d70 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f BITVEC_NINT. Fo
f1d80 72 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 r BITVEC_SZ of 5
f1d90 31 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62 12, this would b
f1da0 65 20 31 32 35 2e 20 2a 2f 0a 20 20 75 33 32 20 e 125. */. u32
f1db0 69 44 69 76 69 73 6f 72 3b 20 20 20 2f 2a 20 4e iDivisor; /* N
f1dc0 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 68 61 umber of bits ha
f1dd0 6e 64 6c 65 64 20 62 79 20 65 61 63 68 20 61 70 ndled by each ap
f1de0 53 75 62 5b 5d 20 65 6e 74 72 79 2e 20 2a 2f 0a Sub[] entry. */.
f1df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f1e00 20 20 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d 30 20 /* Should >=0
f1e10 66 6f 72 20 61 70 53 75 62 20 65 6c 65 6d 65 6e for apSub elemen
f1e20 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 t. */.
f1e30 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 69 /* Max i
f1e40 44 69 76 69 73 6f 72 20 69 73 20 6d 61 78 28 75 Divisor is max(u
f1e50 33 32 29 20 2f 20 42 49 54 56 45 43 5f 4e 50 54 32) / BITVEC_NPT
f1e60 52 20 2b 20 31 2e 20 20 2a 2f 0a 20 20 20 20 20 R + 1. */.
f1e70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f1e80 46 6f 72 20 61 20 42 49 54 56 45 43 5f 53 5a 20 For a BITVEC_SZ
f1e90 6f 66 20 35 31 32 2c 20 74 68 69 73 20 77 6f 75 of 512, this wou
f1ea0 6c 64 20 62 65 20 33 34 2c 33 35 39 2c 37 33 39 ld be 34,359,739
f1eb0 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 . */. union {.
f1ec0 20 20 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20 BITVEC_TELEM
f1ed0 61 42 69 74 6d 61 70 5b 42 49 54 56 45 43 5f 4e aBitmap[BITVEC_N
f1ee0 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a 20 42 69 74 ELEM]; /* Bit
f1ef0 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 map representati
f1f00 6f 6e 20 2a 2f 0a 20 20 20 20 75 33 32 20 61 48 on */. u32 aH
f1f10 61 73 68 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d ash[BITVEC_NINT]
f1f20 3b 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 ; /* Hash t
f1f30 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 able representat
f1f40 69 6f 6e 20 2a 2f 0a 20 20 20 20 42 69 74 76 65 ion */. Bitve
f1f50 63 20 2a 61 70 53 75 62 5b 42 49 54 56 45 43 5f c *apSub[BITVEC_
f1f60 4e 50 54 52 5d 3b 20 20 2f 2a 20 52 65 63 75 72 NPTR]; /* Recur
f1f70 73 69 76 65 20 72 65 70 72 65 73 65 6e 74 61 74 sive representat
f1f80 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b ion */. } u;.};
f1f90 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
f1fa0 20 6e 65 77 20 62 69 74 6d 61 70 20 6f 62 6a 65 new bitmap obje
f1fb0 63 74 20 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c ct able to handl
f1fc0 65 20 62 69 74 73 20 62 65 74 77 65 65 6e 20 30 e bits between 0
f1fd0 20 61 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a 20 69 and iSize,.** i
f1fe0 6e 63 6c 75 73 69 76 65 2e 20 20 52 65 74 75 72 nclusive. Retur
f1ff0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
f2000 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 20 he new object.
f2010 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 0a Return NULL if .
f2020 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e ** malloc fails.
f2030 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
f2040 54 45 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74 TE Bitvec *sqlit
f2050 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 75 e3BitvecCreate(u
f2060 33 32 20 69 53 69 7a 65 29 7b 0a 20 20 42 69 74 32 iSize){. Bit
f2070 76 65 63 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 vec *p;. assert
f2080 28 20 73 69 7a 65 6f 66 28 2a 70 29 3d 3d 42 49 ( sizeof(*p)==BI
f2090 54 56 45 43 5f 53 5a 20 29 3b 0a 20 20 70 20 3d TVEC_SZ );. p =
f20a0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 sqlite3MallocZe
f20b0 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 ro( sizeof(*p) )
f20c0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
f20d0 20 70 2d 3e 69 53 69 7a 65 20 3d 20 69 53 69 7a p->iSize = iSiz
f20e0 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
f20f0 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 p;.}../*.** Chec
f2100 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 k to see if the
f2110 69 2d 74 68 20 62 69 74 20 69 73 20 73 65 74 2e i-th bit is set.
f2120 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 6f 72 Return true or
f2130 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 70 20 false..** If p
f2140 69 73 20 4e 55 4c 4c 20 28 69 66 20 74 68 65 20 is NULL (if the
f2150 62 69 74 6d 61 70 20 68 61 73 20 6e 6f 74 20 62 bitmap has not b
f2160 65 65 6e 20 63 72 65 61 74 65 64 29 20 6f 72 20 een created) or
f2170 69 66 0a 2a 2a 20 69 20 69 73 20 6f 75 74 20 6f if.** i is out o
f2180 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 72 65 f range, then re
f2190 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 turn false..*/.S
f21a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
f21b0 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 t sqlite3BitvecT
f21c0 65 73 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75 est(Bitvec *p, u
f21d0 33 32 20 69 29 7b 0a 20 20 69 66 28 20 70 3d 3d 32 i){. if( p==
f21e0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
f21f0 69 66 28 20 69 3e 70 2d 3e 69 53 69 7a 65 20 7c if( i>p->iSize |
f2200 7c 20 69 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 | i==0 ) return
f2210 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 0;. i--;. whil
f2220 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 e( p->iDivisor )
f2230 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 {. u32 bin =
f2240 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 i/p->iDivisor;.
f2250 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 i = i%p->iDiv
f2260 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d isor;. p = p-
f2270 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 >u.apSub[bin];.
f2280 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 if (!p) {.
f2290 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
f22a0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e }. }. if( p->
f22b0 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 iSize<=BITVEC_NB
f22c0 49 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e IT ){. return
f22d0 20 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 (p->u.aBitmap[i
f22e0 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 /BITVEC_SZELEM]
f22f0 26 20 28 31 3c 3c 28 69 26 28 42 49 54 56 45 43 & (1<<(i&(BITVEC
f2300 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 29 21 3d 30 _SZELEM-1))))!=0
f2310 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a 20 20 20 20 ;. } else{.
f2320 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 u32 h = BITVEC_H
f2330 41 53 48 28 69 2b 2b 29 3b 0a 20 20 20 20 77 68 ASH(i++);. wh
f2340 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b ile( p->u.aHash[
f2350 68 5d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 h] ){. if(
f2360 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 p->u.aHash[h]==i
f2370 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 ) return 1;.
f2380 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 69 66 h++;. if
f2390 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 ( h>=BITVEC_NINT
f23a0 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 7d 0a ) h = 0;. }.
f23b0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
f23c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 }.}../*.** Set t
f23d0 68 65 20 69 2d 74 68 20 62 69 74 2e 20 20 52 65 he i-th bit. Re
f23e0 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 turn 0 on succes
f23f0 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 s and an error c
f2400 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 ode if.** anythi
f2410 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a ng goes wrong..*
f2420 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
f2430 65 20 6d 69 67 68 74 20 63 61 75 73 65 20 73 75 e might cause su
f2440 62 2d 62 69 74 6d 61 70 73 20 74 6f 20 62 65 20 b-bitmaps to be
f2450 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 61 69 6c allocated. Fail
f2460 69 6e 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68 ing.** to get th
f2470 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64 20 e memory needed
f2480 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 62 2d to hold the sub-
f2490 62 69 74 6d 61 70 20 69 73 20 74 68 65 20 6f 6e bitmap is the on
f24a0 6c 79 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 67 ly.** that can g
f24b0 6f 20 77 72 6f 6e 67 20 77 69 74 68 20 61 6e 20 o wrong with an
f24c0 69 6e 73 65 72 74 2c 20 61 73 73 75 6d 69 6e 67 insert, assuming
f24d0 20 70 20 61 6e 64 20 69 20 61 72 65 20 76 61 6c p and i are val
f24e0 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 id..**.** The ca
f24f0 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d lling function m
f2500 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 ust ensure that
f2510 70 20 69 73 20 61 20 76 61 6c 69 64 20 42 69 74 p is a valid Bit
f2520 76 65 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e vec object.** an
f2530 64 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 d that the value
f2540 20 66 6f 72 20 22 69 22 20 69 73 20 77 69 74 68 for "i" is with
f2550 69 6e 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20 in range of the
f2560 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a Bitvec object..*
f2570 2a 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 * Otherwise the
f2580 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
f2590 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fined..*/.SQLITE
f25a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
f25b0 69 74 65 33 42 69 74 76 65 63 53 65 74 28 42 69 ite3BitvecSet(Bi
f25c0 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b tvec *p, u32 i){
f25d0 0a 20 20 75 33 32 20 68 3b 0a 20 20 61 73 73 65 . u32 h;. asse
f25e0 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 rt( p!=0 );. as
f25f0 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 61 sert( i>0 );. a
f2600 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53 69 ssert( i<=p->iSi
f2610 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 ze );. i--;. w
f2620 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a 65 20 3e hile((p->iSize >
f2630 20 42 49 54 56 45 43 5f 4e 42 49 54 29 20 26 26 BITVEC_NBIT) &&
f2640 20 70 2d 3e 69 44 69 76 69 73 6f 72 29 20 7b 0a p->iDivisor) {.
f2650 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f u32 bin = i/
f2660 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 p->iDivisor;.
f2670 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 i = i%p->iDivis
f2680 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 or;. if( p->u
f2690 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 .apSub[bin]==0 )
f26a0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 {. sqlite3B
f26b0 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
f26c0 28 29 3b 0a 20 20 20 20 20 20 70 2d 3e 75 2e 61 ();. p->u.a
f26d0 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 pSub[bin] = sqli
f26e0 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 te3BitvecCreate(
f26f0 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b 0a p->iDivisor );.
f2700 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 sqlite3End
f2710 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
f2720 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 if( p->u.a
f2730 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 pSub[bin]==0 ) r
f2740 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
f2750 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 EM;. }. p
f2760 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e = p->u.apSub[bin
f2770 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e ];. }. if( p->
f2780 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 iSize<=BITVEC_NB
f2790 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 IT ){. p->u.a
f27a0 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f Bitmap[i/BITVEC_
f27b0 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20 SZELEM] |= 1 <<
f27c0 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 (i&(BITVEC_SZELE
f27d0 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75 72 M-1));. retur
f27e0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
f27f0 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 . h = BITVEC_HA
f2800 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66 SH(i++);. /* if
f2810 20 74 68 65 72 65 20 77 61 73 6e 27 74 20 61 20 there wasn't a
f2820 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 hash collision,
f2830 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27 74 and this doesn't
f2840 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65 74 */. /* complet
f2850 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61 73 ely fill the has
f2860 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64 64 h, then just add
f2870 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a 20 it without */.
f2880 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f 75 /* worring abou
f2890 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 t sub-dividing a
f28a0 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a nd re-hashing. *
f28b0 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61 48 /. if( !p->u.aH
f28c0 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69 66 ash[h] ){. if
f28d0 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56 45 (p->nSet<(BITVE
f28e0 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20 20 C_NINT-1)) {.
f28f0 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 goto bitvec_s
f2900 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65 6c et_end;. } el
f2910 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 se {. goto
f2920 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 bitvec_set_rehas
f2930 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f h;. }. }. /
f2940 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 63 6f * there was a co
f2950 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20 74 llision, check t
f2960 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 61 6c o see if it's al
f2970 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e ready */. /* in
f2980 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20 74 hash, if not, t
f2990 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70 6f ry to find a spo
f29a0 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64 6f t for it */. do
f29b0 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e {. if( p->u.
f29c0 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 aHash[h]==i ) re
f29d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
f29e0 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66 28 h++;. if(
f29f0 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 h>=BITVEC_NINT
f2a00 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 ) h = 0;. } whi
f2a10 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 le( p->u.aHash[h
f2a20 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69 64 ] );. /* we did
f2a30 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20 74 n't find it in t
f2a40 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69 6e he hash. h poin
f2a50 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 ts to the first
f2a60 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62 6c */. /* availabl
f2a70 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68 65 e free spot. che
f2a80 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 ck to see if thi
f2a90 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a 2f s is going to */
f2aa0 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20 68 . /* make our h
f2ab0 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e 20 ash too "full".
f2ac0 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f 72 */.bitvec_set_r
f2ad0 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d 3e ehash:. if( p->
f2ae0 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58 48 nSet>=BITVEC_MXH
f2af0 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 ASH ){. unsig
f2b00 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 69 ned int j;. i
f2b10 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20 61 nt rc;. u32 a
f2b20 69 56 61 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e iValues[BITVEC_N
f2b30 49 4e 54 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 INT];. memcpy
f2b40 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e (aiValues, p->u.
f2b50 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 61 69 aHash, sizeof(ai
f2b60 56 61 6c 75 65 73 29 29 3b 0a 20 20 20 20 6d 65 Values));. me
f2b70 6d 73 65 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c mset(p->u.apSub,
f2b80 20 30 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 0, sizeof(aiVal
f2b90 75 65 73 29 29 3b 0a 20 20 20 20 70 2d 3e 69 44 ues));. p->iD
f2ba0 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69 ivisor = (p->iSi
f2bb0 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52 ze + BITVEC_NPTR
f2bc0 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54 - 1)/BITVEC_NPT
f2bd0 52 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 R;. rc = sqli
f2be0 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 te3BitvecSet(p,
f2bf0 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b i);. for(j=0;
f2c00 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 j<BITVEC_NINT;
f2c10 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 j++){. if(
f2c20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63 aiValues[j] ) rc
f2c30 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 |= sqlite3Bitve
f2c40 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73 cSet(p, aiValues
f2c50 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 [j]);. }.
f2c60 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 62 return rc;. }.b
f2c70 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 itvec_set_end:.
f2c80 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d p->nSet++;. p-
f2c90 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b >u.aHash[h] = i;
f2ca0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
f2cb0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
f2cc0 65 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 ear the i-th bit
f2cd0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
f2ce0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
f2cf0 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 BitvecClear(Bitv
f2d00 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 ec *p, u32 i){.
f2d10 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b assert( p!=0 );
f2d20 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 . assert( i>0 )
f2d30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 ;. i--;. while
f2d40 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b ( p->iDivisor ){
f2d50 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 . u32 bin = i
f2d60 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 /p->iDivisor;.
f2d70 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 i = i%p->iDivi
f2d80 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e sor;. p = p->
f2d90 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 u.apSub[bin];.
f2da0 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20 if (!p) {.
f2db0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
f2dc0 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 }. if( p->iSi
f2dd0 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 ze<=BITVEC_NBIT
f2de0 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 ){. p->u.aBit
f2df0 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 map[i/BITVEC_SZE
f2e00 4c 45 4d 5d 20 26 3d 20 7e 28 31 20 3c 3c 20 28 LEM] &= ~(1 << (
f2e10 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d i&(BITVEC_SZELEM
f2e20 2d 31 29 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a -1)));. }else{.
f2e30 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
f2e40 20 6a 3b 0a 20 20 20 20 75 33 32 20 61 69 56 61 j;. u32 aiVa
f2e50 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 lues[BITVEC_NINT
f2e60 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 ];. memcpy(ai
f2e70 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 Values, p->u.aHa
f2e80 73 68 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c sh, sizeof(aiVal
f2e90 75 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 ues));. memse
f2ea0 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c t(p->u.aHash, 0,
f2eb0 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 sizeof(aiValues
f2ec0 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20 ));. p->nSet
f2ed0 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 = 0;. for(j=0
f2ee0 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b ; j<BITVEC_NINT;
f2ef0 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 j++){. if(
f2f00 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20 aiValues[j] &&
f2f10 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b aiValues[j]!=(i+
f2f20 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33 1) ){. u3
f2f30 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53 2 h = BITVEC_HAS
f2f40 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d 31 29 H(aiValues[j]-1)
f2f50 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 53 65 ;. p->nSe
f2f60 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 t++;. whi
f2f70 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 le( p->u.aHash[h
f2f80 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 68 ] ){. h
f2f90 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ++;. if
f2fa0 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 ( h>=BITVEC_NINT
f2fb0 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 ) h = 0;.
f2fc0 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 75 }. p->u
f2fd0 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61 69 56 61 .aHash[h] = aiVa
f2fe0 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d lues[j];. }
f2ff0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
f3000 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 62 69 .** Destroy a bi
f3010 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 52 65 tmap object. Re
f3020 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 claim all memory
f3030 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 used..*/.SQLITE
f3040 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
f3050 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
f3060 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 oy(Bitvec *p){.
f3070 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
f3080 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 44 69 rn;. if( p->iDi
f3090 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 6e 73 visor ){. uns
f30a0 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 igned int i;.
f30b0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49 54 56 for(i=0; i<BITV
f30c0 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b 0a 20 EC_NPTR; i++){.
f30d0 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
f30e0 65 63 44 65 73 74 72 6f 79 28 70 2d 3e 75 2e 61 ecDestroy(p->u.a
f30f0 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a pSub[i]);. }.
f3100 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
f3110 65 65 28 70 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 ee(p);.}..#ifnde
f3120 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
f3130 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a ILTIN_TEST./*.**
f3140 20 4c 65 74 20 56 5b 5d 20 62 65 20 61 6e 20 61 Let V[] be an a
f3150 72 72 61 79 20 6f 66 20 75 6e 73 69 67 6e 65 64 rray of unsigned
f3160 20 63 68 61 72 61 63 74 65 72 73 20 73 75 66 66 characters suff
f3170 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c 64 0a 2a icient to hold.*
f3180 2a 20 75 70 20 74 6f 20 4e 20 62 69 74 73 2e 20 * up to N bits.
f3190 20 4c 65 74 20 49 20 62 65 20 61 6e 20 69 6e 74 Let I be an int
f31a0 65 67 65 72 20 62 65 74 77 65 65 6e 20 30 20 61 eger between 0 a
f31b0 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a 2a nd N. 0<=I<N..*
f31c0 2a 20 54 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f * Then the follo
f31d0 77 69 6e 67 20 6d 61 63 72 6f 73 20 63 61 6e 20 wing macros can
f31e0 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 2c 20 be used to set,
f31f0 63 6c 65 61 72 2c 20 6f 72 20 74 65 73 74 0a 2a clear, or test.*
f3200 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 62 69 74 * individual bit
f3210 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a 23 s within V..*/.#
f3220 64 65 66 69 6e 65 20 53 45 54 42 49 54 28 56 2c define SETBIT(V,
f3230 49 29 20 20 20 20 20 20 56 5b 49 3e 3e 33 5d 20 I) V[I>>3]
f3240 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 |= (1<<(I&7)).#d
f3250 65 66 69 6e 65 20 43 4c 45 41 52 42 49 54 28 56 efine CLEARBIT(V
f3260 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33 5d 20 26 ,I) V[I>>3] &
f3270 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 = ~(1<<(I&7)).#d
f3280 65 66 69 6e 65 20 54 45 53 54 42 49 54 28 56 2c efine TESTBIT(V,
f3290 49 29 20 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26 I) (V[I>>3]&
f32a0 28 31 3c 3c 28 49 26 37 29 29 29 21 3d 30 0a 0a (1<<(I&7)))!=0..
f32b0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
f32c0 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 74 65 6e ne runs an exten
f32d0 73 69 76 65 20 74 65 73 74 20 6f 66 20 74 68 65 sive test of the
f32e0 20 42 69 74 76 65 63 20 63 6f 64 65 2e 0a 2a 2a Bitvec code..**
f32f0 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73 .** The input is
f3300 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74 an array of int
f3310 65 67 65 72 73 20 74 68 61 74 20 61 63 74 73 20 egers that acts
f3320 61 73 20 61 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 as a program.**
f3330 74 6f 20 74 65 73 74 20 74 68 65 20 42 69 74 76 to test the Bitv
f3340 65 63 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72 ec. The integer
f3350 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f s are opcodes fo
f3360 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 30 2c 20 llowed.** by 0,
f3370 31 2c 20 6f 72 20 33 20 6f 70 65 72 61 6e 64 73 1, or 3 operands
f3380 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 , depending on t
f3390 68 65 20 6f 70 63 6f 64 65 2e 20 20 41 6e 6f 74 he opcode. Anot
f33a0 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 6f her.** opcode fo
f33b0 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61 74 65 6c llows immediatel
f33c0 79 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 y after the last
f33d0 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 operand..**.**
f33e0 54 68 65 72 65 20 61 72 65 20 36 20 6f 70 63 6f There are 6 opco
f33f0 64 65 73 20 6e 75 6d 62 65 72 65 64 20 66 72 6f des numbered fro
f3400 6d 20 30 20 74 68 72 6f 75 67 68 20 35 2e 20 20 m 0 through 5.
f3410 30 20 69 73 20 74 68 65 0a 2a 2a 20 22 68 61 6c 0 is the.** "hal
f3420 74 22 20 6f 70 63 6f 64 65 20 61 6e 64 20 63 61 t" opcode and ca
f3430 75 73 65 73 20 74 68 65 20 74 65 73 74 20 74 6f uses the test to
f3440 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 end..**.** 0
f3450 20 20 20 20 20 20 20 20 20 20 48 61 6c 74 20 61 Halt a
f3460 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 nd return the nu
f3470 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 0a 2a mber of errors.*
f3480 2a 20 20 20 20 31 20 4e 20 53 20 58 20 20 20 20 * 1 N S X
f3490 53 65 74 20 4e 20 62 69 74 73 20 62 65 67 69 6e Set N bits begin
f34a0 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 ning with S and
f34b0 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 incrementing by
f34c0 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53 20 58 20 X.** 2 N S X
f34d0 20 20 20 43 6c 65 61 72 20 4e 20 62 69 74 73 20 Clear N bits
f34e0 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 beginning with S
f34f0 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e and incrementin
f3500 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 33 20 4e g by X.** 3 N
f3510 20 20 20 20 20 20 20 20 53 65 74 20 4e 20 72 61 Set N ra
f3520 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 ndomly chosen bi
f3530 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20 20 ts.** 4 N
f3540 20 20 20 20 43 6c 65 61 72 20 4e 20 72 61 6e 64 Clear N rand
f3550 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73 omly chosen bits
f3560 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20 58 20 20 .** 5 N S X
f3570 20 20 53 65 74 20 4e 20 62 69 74 73 20 66 72 6f Set N bits fro
f3580 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74 20 58 20 m S increment X
f3590 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e in array only, n
f35a0 6f 74 20 69 6e 20 62 69 74 76 65 63 0a 2a 2a 0a ot in bitvec.**.
f35b0 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 73 20 31 ** The opcodes 1
f35c0 20 74 68 72 6f 75 67 68 20 34 20 70 65 72 66 6f through 4 perfo
f35d0 72 6d 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72 rm set and clear
f35e0 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 operations are
f35f0 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e 20 performed.** on
f3600 62 6f 74 68 20 61 20 42 69 74 76 65 63 20 6f 62 both a Bitvec ob
f3610 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 20 6c 69 ject and on a li
f3620 6e 65 61 72 20 61 72 72 61 79 20 6f 66 20 62 69 near array of bi
f3630 74 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ts obtained from
f3640 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 6f malloc..** Opco
f3650 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e 20 74 68 de 5 works on th
f3660 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f e linear array o
f3670 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 20 nly, not on the
f3680 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f 64 Bitvec..** Opcod
f3690 65 20 35 20 69 73 20 75 73 65 64 20 74 6f 20 64 e 5 is used to d
f36a0 65 6c 69 62 65 72 61 74 65 6c 79 20 69 6e 64 75 eliberately indu
f36b0 63 65 20 61 20 66 61 75 6c 74 20 69 6e 20 6f 72 ce a fault in or
f36c0 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 72 der to.** confir
f36d0 6d 20 74 68 61 74 20 65 72 72 6f 72 20 64 65 74 m that error det
f36e0 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a 2a ection works..**
f36f0 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f 6e 63 6c .** At the concl
f3700 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 73 usion of the tes
f3710 74 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 t the linear arr
f3720 61 79 20 69 73 20 63 6f 6d 70 61 72 65 64 0a 2a ay is compared.*
f3730 2a 20 61 67 61 69 6e 73 74 20 74 68 65 20 42 69 * against the Bi
f3740 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 49 66 tvec object. If
f3750 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 64 there are any d
f3760 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 61 ifferences,.** a
f3770 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
f3780 6e 65 64 2e 20 20 49 66 20 74 68 65 79 20 61 72 ned. If they ar
f3790 65 20 74 68 65 20 73 61 6d 65 2c 20 7a 65 72 6f e the same, zero
f37a0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
f37b0 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 .** If a memory
f37c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
f37d0 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 occurs, return
f37e0 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 -1..*/.SQLITE_PR
f37f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
f3800 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 3BitvecBuiltinTe
f3810 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a st(int sz, int *
f3820 61 4f 70 29 7b 0a 20 20 42 69 74 76 65 63 20 2a aOp){. Bitvec *
f3830 70 42 69 74 76 65 63 20 3d 20 30 3b 0a 20 20 75 pBitvec = 0;. u
f3840 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 56 nsigned char *pV
f3850 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d = 0;. int rc =
f3860 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 78 -1;. int i, nx
f3870 2c 20 70 63 2c 20 6f 70 3b 0a 0a 20 20 2f 2a 20 , pc, op;.. /*
f3880 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 42 69 74 Allocate the Bit
f3890 76 65 63 20 74 6f 20 62 65 20 74 65 73 74 65 64 vec to be tested
f38a0 20 61 6e 64 20 61 20 6c 69 6e 65 61 72 20 61 72 and a linear ar
f38b0 72 61 79 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 ray of. ** bits
f38c0 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 72 to act as the r
f38d0 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42 eference */. pB
f38e0 69 74 76 65 63 20 3d 20 73 71 6c 69 74 65 33 42 itvec = sqlite3B
f38f0 69 74 76 65 63 43 72 65 61 74 65 28 20 73 7a 20 itvecCreate( sz
f3900 29 3b 0a 20 20 70 56 20 3d 20 73 71 6c 69 74 65 );. pV = sqlite
f3910 33 5f 6d 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29 3_malloc( (sz+7)
f3920 2f 38 20 2b 20 31 20 29 3b 0a 20 20 69 66 28 20 /8 + 1 );. if(
f3930 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56 pBitvec==0 || pV
f3940 3d 3d 30 20 29 20 67 6f 74 6f 20 62 69 74 76 65 ==0 ) goto bitve
f3950 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28 c_end;. memset(
f3960 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20 pV, 0, (sz+7)/8
f3970 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 + 1);.. /* Run
f3980 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 the program */.
f3990 20 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 pc = 0;. while
f39a0 28 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 ( (op = aOp[pc])
f39b0 21 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63 !=0 ){. switc
f39c0 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 h( op ){. c
f39d0 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73 ase 1:. cas
f39e0 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 e 2:. case
f39f0 35 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 5: {. nx
f3a00 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20 3d = 4;. i =
f3a10 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a aOp[pc+2] - 1;.
f3a20 20 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 aOp[pc+2
f3a30 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a ] += aOp[pc+3];.
f3a40 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
f3a50 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 }. cas
f3a60 65 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 e 3:. case
f3a70 34 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75 6c 4: . defaul
f3a80 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 t: {. nx
f3a90 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71 6c = 2;. sql
f3aa0 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
f3ab0 73 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a sizeof(i), &i);.
f3ac0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
f3ad0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
f3ae0 20 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 if( (--aOp[pc+1
f3af0 5d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b ]) > 0 ) nx = 0;
f3b00 0a 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 . pc += nx;.
f3b10 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 66 i = (i & 0x7f
f3b20 66 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 ffffff)%sz;.
f3b30 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30 20 if( (op & 1)!=0
f3b40 29 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54 28 ){. SETBIT(
f3b50 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 pV, (i+1));.
f3b60 20 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 if( op!=5 ){.
f3b70 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
f3b80 65 33 42 69 74 76 65 63 53 65 74 28 70 42 69 74 e3BitvecSet(pBit
f3b90 76 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f vec, i+1) ) goto
f3ba0 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 bitvec_end;.
f3bb0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
f3bc0 20 20 20 20 20 20 43 4c 45 41 52 42 49 54 28 70 CLEARBIT(p
f3bd0 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 V, (i+1));.
f3be0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c sqlite3BitvecCl
f3bf0 65 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b 31 ear(pBitvec, i+1
f3c00 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
f3c10 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 20 /* Test to make
f3c20 73 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 20 sure the linear
f3c30 61 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d 61 array exactly ma
f3c40 74 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 42 tches the. ** B
f3c50 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 53 itvec object. S
f3c60 74 61 72 74 20 77 69 74 68 20 74 68 65 20 61 73 tart with the as
f3c70 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 68 sumption that th
f3c80 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 ey do. ** match
f3c90 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67 (rc==0). Chang
f3ca0 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f e rc to non-zero
f3cb0 20 69 66 20 61 20 64 69 73 63 72 65 70 61 6e 63 if a discrepanc
f3cc0 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e y. ** is found.
f3cd0 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c . */. rc = sql
f3ce0 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 30 ite3BitvecTest(0
f3cf0 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 74 ,0) + sqlite3Bit
f3d00 76 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c vecTest(pBitvec,
f3d10 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 20 sz+1).
f3d20 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 + sqlite3Bitvec
f3d30 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 29 Test(pBitvec, 0)
f3d40 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d ;. for(i=1; i<=
f3d50 73 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 sz; i++){. if
f3d60 28 20 20 28 54 45 53 54 42 49 54 28 70 56 2c 69 ( (TESTBIT(pV,i
f3d70 29 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65 ))!=sqlite3Bitve
f3d80 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 29 cTest(pBitvec,i)
f3d90 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 ){. rc = i
f3da0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
f3db0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 }. }.. /* F
f3dc0 72 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 ree allocated st
f3dd0 72 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 65 ructure */.bitve
f3de0 63 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 c_end:. sqlite3
f3df0 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71 6c _free(pV);. sql
f3e00 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f ite3BitvecDestro
f3e10 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72 65 y(pBitvec);. re
f3e20 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 turn rc;.}.#endi
f3e30 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
f3e40 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f _BUILTIN_TEST */
f3e50 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
f3e60 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63 2e * End of bitvec.
f3e70 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
f3e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ea0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
f3eb0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 61 * Begin file pca
f3ec0 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a che.c **********
f3ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ef0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 /./*.** 2008 Aug
f3f00 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 05.**.** The
f3f10 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
f3f20 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
f3f30 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
f3f40 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
f3f50 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
f3f60 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
f3f70 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
f3f80 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
f3f90 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
f3fa0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
f3fb0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
f3fc0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
f3fd0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
f3fe0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
f3ff0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
f4000 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
f4010 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
f4020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4060 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
f4070 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 le implements th
f4080 61 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a 2a at page cache..*
f4090 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 *.** @(#) $Id: p
f40a0 63 61 63 68 65 2e 63 2c 76 20 31 2e 33 38 20 32 cache.c,v 1.38 2
f40b0 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 3a 008/11/19 16:52:
f40c0 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 44 danielk1977 E
f40d0 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 xp $.*/../*.** A
f40e0 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 20 63 complete page c
f40f0 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 61 ache is an insta
f4100 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 nce of this stru
f4110 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 cture..*/.struct
f4120 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 48 64 PCache {. PgHd
f4130 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 69 72 r *pDirty, *pDir
f4140 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 20 20 tyTail;
f4150 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 74 79 /* List of dirty
f4160 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 6f 72 pages in LRU or
f4170 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a der */. PgHdr *
f4180 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 20 20 pSynced;
f4190 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f41a0 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 67 65 Last synced page
f41b0 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 20 6c in dirty page l
f41c0 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 ist */. int nRe
f41d0 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
f41e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f41f0 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 Number of refere
f4200 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 nced pages */.
f4210 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 20 20 int nMax;
f4220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4230 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 /* Configure
f4240 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a d cache size */.
f4250 20 20 69 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20 int nMin;
f4260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4270 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 /* Configu
f4280 72 65 64 20 6d 69 6e 69 6d 75 6d 20 63 61 63 68 red minimum cach
f4290 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 e size */. int
f42a0 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 szPage;
f42b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f42c0 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 /* Size of every
f42d0 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 page in this ca
f42e0 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 che */. int szE
f42f0 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 xtra;
f4300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f4310 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20 73 70 Size of extra sp
f4320 61 63 65 20 66 6f 72 20 65 61 63 68 20 70 61 67 ace for each pag
f4330 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 e */. int bPurg
f4340 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 eable;
f4350 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
f4360 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20 ue if pages are
f4370 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 on backing store
f4380 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 */. int (*xStr
f4390 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 ess)(void*,PgHdr
f43a0 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43 61 6c *); /* Cal
f43b0 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20 61 20 l to try make a
f43c0 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 page clean */.
f43d0 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b 20 20 void *pStress;
f43e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f43f0 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 /* Argument
f4400 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 to xStress */.
f4410 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
f4420 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20 pCache;
f4430 20 20 20 20 2f 2a 20 50 6c 75 67 67 61 62 6c 65 /* Pluggable
f4440 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 2a 2f cache module */
f4450 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 31 . PgHdr *pPage1
f4460 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 ;.};../*.** Some
f4470 20 6f 66 20 74 68 65 20 61 73 73 65 72 74 28 29 of the assert()
f4480 20 6d 61 63 72 6f 73 20 69 6e 20 74 68 69 73 20 macros in this
f4490 63 6f 64 65 20 61 72 65 20 74 6f 6f 20 65 78 70 code are too exp
f44a0 65 6e 73 69 76 65 20 74 6f 20 72 75 6e 0a 2a 2a ensive to run.**
f44b0 20 65 76 65 6e 20 64 75 72 69 6e 67 20 6e 6f 72 even during nor
f44c0 6d 61 6c 20 64 65 62 75 67 67 69 6e 67 2e 20 20 mal debugging.
f44d0 55 73 65 20 74 68 65 6d 20 6f 6e 6c 79 20 72 61 Use them only ra
f44e0 72 65 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75 6e rely on long-run
f44f0 6e 69 6e 67 0a 2a 2a 20 74 65 73 74 73 2e 20 20 ning.** tests.
f4500 45 6e 61 62 6c 65 20 74 68 65 20 65 78 70 65 6e Enable the expen
f4510 73 69 76 65 20 61 73 73 65 72 74 73 20 75 73 69 sive asserts usi
f4520 6e 67 20 74 68 65 0a 2a 2a 20 2d 44 53 51 4c 49 ng the.** -DSQLI
f4530 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 TE_ENABLE_EXPENS
f4540 49 56 45 5f 41 53 53 45 52 54 3d 31 20 63 6f 6d IVE_ASSERT=1 com
f4550 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e pile-time option
f4560 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
f4570 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 TE_ENABLE_EXPENS
f4580 49 56 45 5f 41 53 53 45 52 54 0a 23 20 64 65 66 IVE_ASSERT.# def
f4590 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 ine expensive_as
f45a0 73 65 72 74 28 58 29 20 20 61 73 73 65 72 74 28 sert(X) assert(
f45b0 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e X).#else.# defin
f45c0 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 e expensive_asse
f45d0 72 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a rt(X).#endif../*
f45e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f45f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4600 2a 20 4c 69 6e 6b 65 64 20 4c 69 73 74 20 4d 61 * Linked List Ma
f4610 6e 61 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a 2a nagement *******
f4620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
f4630 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 #if !defined(NDE
f4640 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64 28 BUG) && defined(
f4650 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 SQLITE_ENABLE_EX
f4660 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 29 0a PENSIVE_ASSERT).
f4670 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 /*.** Check that
f4680 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70 53 79 the pCache->pSy
f4690 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69 73 nced variable is
f46a0 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 set correctly.
f46b0 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c If it.** is not,
f46c0 20 65 69 74 68 65 72 20 66 61 69 6c 20 61 6e 20 either fail an
f46d0 61 73 73 65 72 74 20 6f 72 20 72 65 74 75 72 6e assert or return
f46e0 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69 73 65 zero. Otherwise
f46f0 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d , return.** non-
f4700 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20 6f 6e zero. This is on
f4710 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62 75 67 ly used in debug
f4720 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73 20 ging builds, as
f4730 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
f4740 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 expensive_asser
f4750 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 t( pcacheCheckSy
f4760 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b 0a nced(pCache) );.
f4770 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 */.static int pc
f4780 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 acheCheckSynced(
f4790 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
f47a0 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 . PgHdr *p;. f
f47b0 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 or(p=pCache->pDi
f47c0 72 74 79 54 61 69 6c 3b 20 70 21 3d 70 43 61 63 rtyTail; p!=pCac
f47d0 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 70 3d 70 he->pSynced; p=p
f47e0 2d 3e 70 44 69 72 74 79 50 72 65 76 29 7b 0a 20 ->pDirtyPrev){.
f47f0 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 assert( p->nR
f4800 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26 ef || (p->flags&
f4810 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
f4820 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e );. }. return
f4830 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 (p==0 || p->nRe
f4840 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26 50 f || (p->flags&P
f4850 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3d GHDR_NEED_SYNC)=
f4860 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a =0);.}.#endif /*
f4870 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c 49 !NDEBUG && SQLI
f4880 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 TE_ENABLE_EXPENS
f4890 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a 2f IVE_ASSERT */../
f48a0 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 *.** Remove page
f48b0 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65 20 pPage from the
f48c0 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 list of dirty pa
f48d0 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ges..*/.static v
f48e0 6f 69 64 20 70 63 61 63 68 65 52 65 6d 6f 76 65 oid pcacheRemove
f48f0 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 50 67 FromDirtyList(Pg
f4900 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 Hdr *pPage){. P
f4910 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67 65 Cache *p = pPage
f4920 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 ->pCache;.. ass
f4930 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 ert( pPage->pDir
f4940 74 79 4e 65 78 74 20 7c 7c 20 70 50 61 67 65 3d tyNext || pPage=
f4950 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29 =p->pDirtyTail )
f4960 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
f4970 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 7c 7c e->pDirtyPrev ||
f4980 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 pPage==p->pDirt
f4990 79 20 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 y );.. /* Updat
f49a0 65 20 74 68 65 20 50 43 61 63 68 65 31 2e 70 53 e the PCache1.pS
f49b0 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69 ynced variable i
f49c0 66 20 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f 0a f necessary. */.
f49d0 20 20 69 66 28 20 70 2d 3e 70 53 79 6e 63 65 64 if( p->pSynced
f49e0 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 50 ==pPage ){. P
f49f0 67 48 64 72 20 2a 70 53 79 6e 63 65 64 20 3d 20 gHdr *pSynced =
f4a00 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 pPage->pDirtyPre
f4a10 76 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 53 v;. while( pS
f4a20 79 6e 63 65 64 20 26 26 20 28 70 53 79 6e 63 65 ynced && (pSynce
f4a30 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e d->flags&PGHDR_N
f4a40 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 EED_SYNC) ){.
f4a50 20 20 20 70 53 79 6e 63 65 64 20 3d 20 70 53 79 pSynced = pSy
f4a60 6e 63 65 64 2d 3e 70 44 69 72 74 79 50 72 65 76 nced->pDirtyPrev
f4a70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 ;. }. p->p
f4a80 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 64 Synced = pSynced
f4a90 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 ;. }.. if( pPa
f4aa0 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 ge->pDirtyNext )
f4ab0 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 {. pPage->pDi
f4ac0 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 rtyNext->pDirtyP
f4ad0 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 rev = pPage->pDi
f4ae0 72 74 79 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65 rtyPrev;. }else
f4af0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
f4b00 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 age==p->pDirtyTa
f4b10 69 6c 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 il );. p->pDi
f4b20 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 2d rtyTail = pPage-
f4b30 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 7d >pDirtyPrev;. }
f4b40 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 . if( pPage->pD
f4b50 69 72 74 79 50 72 65 76 20 29 7b 0a 20 20 20 20 irtyPrev ){.
f4b60 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 pPage->pDirtyPre
f4b70 76 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 v->pDirtyNext =
f4b80 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 pPage->pDirtyNex
f4b90 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
f4ba0 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70 assert( pPage==p
f4bb0 2d 3e 70 44 69 72 74 79 20 29 3b 0a 20 20 20 20 ->pDirty );.
f4bc0 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 67 p->pDirty = pPag
f4bd0 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 e->pDirtyNext;.
f4be0 20 7d 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 }. pPage->pDir
f4bf0 74 79 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 50 tyNext = 0;. pP
f4c00 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 age->pDirtyPrev
f4c10 3d 20 30 3b 0a 0a 20 20 65 78 70 65 6e 73 69 76 = 0;.. expensiv
f4c20 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 e_assert( pcache
f4c30 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20 29 CheckSynced(p) )
f4c40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 70 ;.}../*.** Add p
f4c50 61 67 65 20 70 50 61 67 65 20 74 6f 20 74 68 65 age pPage to the
f4c60 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69 72 head of the dir
f4c70 74 79 20 6c 69 73 74 20 28 50 43 61 63 68 65 31 ty list (PCache1
f4c80 2e 70 44 69 72 74 79 20 69 73 20 73 65 74 20 74 .pDirty is set t
f4c90 6f 0a 2a 2a 20 70 50 61 67 65 29 2e 0a 2a 2f 0a o.** pPage)..*/.
f4ca0 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
f4cb0 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74 heAddToDirtyList
f4cc0 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a (PgHdr *pPage){.
f4cd0 20 20 50 43 61 63 68 65 20 2a 70 20 3d 20 70 50 PCache *p = pP
f4ce0 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 age->pCache;..
f4cf0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
f4d00 44 69 72 74 79 4e 65 78 74 3d 3d 30 20 26 26 20 DirtyNext==0 &&
f4d10 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 pPage->pDirtyPre
f4d20 76 3d 3d 30 20 26 26 20 70 2d 3e 70 44 69 72 74 v==0 && p->pDirt
f4d30 79 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 20 70 y!=pPage );.. p
f4d40 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
f4d50 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20 20 = p->pDirty;.
f4d60 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 if( pPage->pDirt
f4d70 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73 73 yNext ){. ass
f4d80 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 ert( pPage->pDir
f4d90 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 tyNext->pDirtyPr
f4da0 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 ev==0 );. pPa
f4db0 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e ge->pDirtyNext->
f4dc0 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61 pDirtyPrev = pPa
f4dd0 67 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 44 69 ge;. }. p->pDi
f4de0 72 74 79 20 3d 20 70 50 61 67 65 3b 0a 20 20 69 rty = pPage;. i
f4df0 66 28 20 21 70 2d 3e 70 44 69 72 74 79 54 61 69 f( !p->pDirtyTai
f4e00 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72 l ){. p->pDir
f4e10 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a tyTail = pPage;.
f4e20 20 20 7d 0a 20 20 69 66 28 20 21 70 2d 3e 70 53 }. if( !p->pS
f4e30 79 6e 63 65 64 20 26 26 20 30 3d 3d 28 70 50 61 ynced && 0==(pPa
f4e40 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f ge->flags&PGHDR_
f4e50 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 NEED_SYNC) ){.
f4e60 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 p->pSynced = p
f4e70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 65 78 70 65 Page;. }. expe
f4e80 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 nsive_assert( pc
f4e90 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 acheCheckSynced(
f4ea0 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 p) );.}../*.** W
f4eb0 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 rapper around th
f4ec0 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 e pluggable cach
f4ed0 65 73 20 78 55 6e 70 69 6e 20 6d 65 74 68 6f 64 es xUnpin method
f4ee0 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 20 69 . If the cache i
f4ef0 73 0a 2a 2a 20 62 65 69 6e 67 20 75 73 65 64 20 s.** being used
f4f00 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
f4f10 20 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 20 database, this
f4f20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
f4f30 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 -op..*/.static v
f4f40 6f 69 64 20 70 63 61 63 68 65 55 6e 70 69 6e 28 oid pcacheUnpin(
f4f50 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43 61 PgHdr *p){. PCa
f4f60 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 2d che *pCache = p-
f4f70 3e 70 43 61 63 68 65 3b 0a 20 20 69 66 28 20 70 >pCache;. if( p
f4f80 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c Cache->bPurgeabl
f4f90 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e e ){. if( p->
f4fa0 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 pgno==1 ){.
f4fb0 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 pCache->pPage1
f4fc0 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 = 0;. }. s
f4fd0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
f4fe0 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e ig.pcache.xUnpin
f4ff0 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
f5000 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f p, 0);. }.}../
f5010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5040 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e 74 65 *** General Inte
f5050 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a 2a rfaces ******.**
f5060 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 .** Initialize a
f5070 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65 20 nd shutdown the
f5080 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 79 page cache subsy
f5090 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20 6f 66 stem. Neither of
f50a0 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 these .** funct
f50b0 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61 64 73 ions are threads
f50c0 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 afe..*/.SQLITE_P
f50d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
f50e0 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c 69 e3PcacheInitiali
f50f0 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 ze(void){. if(
f5100 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
f5110 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 fig.pcache.xInit
f5120 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
f5130 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75 e3PCacheSetDefau
f5140 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 lt();. }. retu
f5150 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c rn sqlite3Global
f5160 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49 Config.pcache.xI
f5170 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 nit(sqlite3Globa
f5180 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 70 lConfig.pcache.p
f5190 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 Arg);.}.SQLITE_P
f51a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
f51b0 74 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 te3PcacheShutdow
f51c0 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 n(void){. if( s
f51d0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
f51e0 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75 74 64 ig.pcache.xShutd
f51f0 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 own ){. sqlit
f5200 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
f5210 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 28 cache.xShutdown(
f5220 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
f5230 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67 29 fig.pcache.pArg)
f5240 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
f5250 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 69 eturn the size i
f5260 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43 61 n bytes of a PCa
f5270 63 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 che object..*/.S
f5280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
f5290 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 t sqlite3PcacheS
f52a0 69 7a 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 ize(void){ retur
f52b0 6e 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 29 n sizeof(PCache)
f52c0 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 ; }../*.** Creat
f52d0 65 20 61 20 6e 65 77 20 50 43 61 63 68 65 20 6f e a new PCache o
f52e0 62 6a 65 63 74 2e 20 53 74 6f 72 61 67 65 20 73 bject. Storage s
f52f0 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 pace to hold the
f5300 20 6f 62 6a 65 63 74 0a 2a 2a 20 68 61 73 20 61 object.** has a
f5310 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f lready been allo
f5320 63 61 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 cated and is pas
f5330 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 70 20 sed in as the p
f5340 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 54 68 65 pointer. .** The
f5350 20 63 61 6c 6c 65 72 20 64 69 73 63 6f 76 65 72 caller discover
f5360 73 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 s how much space
f5370 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6c 6c needs to be all
f5380 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 63 61 ocated by .** ca
f5390 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63 61 lling sqlite3Pca
f53a0 63 68 65 53 69 7a 65 28 29 2e 0a 2a 2f 0a 53 51 cheSize()..*/.SQ
f53b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
f53c0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f d sqlite3PcacheO
f53d0 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 pen(. int szPag
f53e0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
f53f0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 /* Size of e
f5400 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 very page */. i
f5410 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20 nt szExtra,
f5420 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
f5430 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f 63 xtra space assoc
f5440 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20 iated with each
f5450 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 page */. int bP
f5460 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20 20 urgeable,
f5470 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
f5480 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62 f pages are on b
f5490 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a acking store */.
f54a0 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29 int (*xStress)
f54b0 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 2c 2f (void*,PgHdr*),/
f54c0 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74 6f * Call to try to
f54d0 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 61 make pages clea
f54e0 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 n */. void *pSt
f54f0 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 ress,
f5500 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 /* Argument
f5510 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 to xStress */.
f5520 50 43 61 63 68 65 20 2a 70 20 20 20 20 20 20 20 PCache *p
f5530 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f5540 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 Preallocated spa
f5550 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 68 ce for the PCach
f5560 65 20 2a 2f 0a 29 7b 0a 20 20 6d 65 6d 73 65 74 e */.){. memset
f5570 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 43 (p, 0, sizeof(PC
f5580 61 63 68 65 29 29 3b 0a 20 20 70 2d 3e 73 7a 50 ache));. p->szP
f5590 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 age = szPage;.
f55a0 70 2d 3e 73 7a 45 78 74 72 61 20 3d 20 73 7a 45 p->szExtra = szE
f55b0 78 74 72 61 3b 0a 20 20 70 2d 3e 62 50 75 72 67 xtra;. p->bPurg
f55c0 65 61 62 6c 65 20 3d 20 62 50 75 72 67 65 61 62 eable = bPurgeab
f55d0 6c 65 3b 0a 20 20 70 2d 3e 78 53 74 72 65 73 73 le;. p->xStress
f55e0 20 3d 20 78 53 74 72 65 73 73 3b 0a 20 20 70 2d = xStress;. p-
f55f0 3e 70 53 74 72 65 73 73 20 3d 20 70 53 74 72 65 >pStress = pStre
f5600 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61 78 20 3d 20 ss;. p->nMax =
f5610 31 30 30 3b 0a 20 20 70 2d 3e 6e 4d 69 6e 20 3d 100;. p->nMin =
f5620 20 31 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 10;.}../*.** Ch
f5630 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73 69 ange the page si
f5640 7a 65 20 66 6f 72 20 50 43 61 63 68 65 20 6f 62 ze for PCache ob
f5650 6a 65 63 74 2e 20 54 68 65 20 63 61 6c 6c 65 72 ject. The caller
f5660 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 must ensure tha
f5670 74 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e t there.** are n
f5680 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 o outstanding pa
f5690 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68 ge references wh
f56a0 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
f56b0 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 is called..*/.S
f56c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
f56d0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
f56e0 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 63 SetPageSize(PCac
f56f0 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 he *pCache, int
f5700 73 7a 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 szPage){. asser
f5710 74 28 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 3d t( pCache->nRef=
f5720 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 44 =0 && pCache->pD
f5730 69 72 74 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 irty==0 );. if(
f5740 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
f5750 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
f5760 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
f5770 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 68 e.xDestroy(pCach
f5780 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 20 20 e->pCache);.
f5790 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 3d pCache->pCache =
f57a0 20 30 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 0;. }. pCache
f57b0 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 ->szPage = szPag
f57c0 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 e;.}../*.** Try
f57d0 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 61 67 65 to obtain a page
f57e0 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e from the cache.
f57f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
f5800 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
f5810 61 63 68 65 46 65 74 63 68 28 0a 20 20 50 43 61 acheFetch(. PCa
f5820 63 68 65 20 2a 70 43 61 63 68 65 2c 20 20 20 20 che *pCache,
f5830 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 /* Obtain the
f5840 20 70 61 67 65 20 66 72 6f 6d 20 74 68 69 73 20 page from this
f5850 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 cache */. Pgno
f5860 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 pgno,
f5870 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
f5880 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0a 20 20 69 to obtain */. i
f5890 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 20 nt createFlag,
f58a0 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c /* If true,
f58b0 20 63 72 65 61 74 65 20 70 61 67 65 20 69 66 20 create page if
f58c0 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 it does not exis
f58d0 74 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 50 t already */. P
f58e0 67 48 64 72 20 2a 2a 70 70 50 61 67 65 20 20 20 gHdr **ppPage
f58f0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 /* Write th
f5900 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a 29 e page here */.)
f5910 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 {. PgHdr *pPage
f5920 20 3d 20 30 3b 0a 20 20 69 6e 74 20 65 43 72 65 = 0;. int eCre
f5930 61 74 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ate;.. assert(
f5940 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 61 pCache!=0 );. a
f5950 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 20 29 3b ssert( pgno>0 );
f5960 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 6c .. /* If the pl
f5970 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 28 73 uggable cache (s
f5980 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 20 qlite3_pcache*)
f5990 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c has not been all
f59a0 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c ocated,. ** all
f59b0 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 0a 20 20 ocate it now..
f59c0 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 63 68 65 */. if( !pCache
f59d0 2d 3e 70 43 61 63 68 65 20 26 26 20 63 72 65 61 ->pCache && crea
f59e0 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 73 71 teFlag ){. sq
f59f0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 3b lite3_pcache *p;
f5a00 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a . int nByte;.
f5a10 20 20 20 20 6e 42 79 74 65 20 3d 20 70 43 61 63 nByte = pCac
f5a20 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70 43 61 he->szPage + pCa
f5a30 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b 20 73 che->szExtra + s
f5a40 69 7a 65 6f 66 28 50 67 48 64 72 29 3b 0a 20 20 izeof(PgHdr);.
f5a50 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f p = sqlite3Glo
f5a60 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
f5a70 2e 78 43 72 65 61 74 65 28 6e 42 79 74 65 2c 20 .xCreate(nByte,
f5a80 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
f5a90 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20 le);. if( !p
f5aa0 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
f5ab0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
f5ac0 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 }. sqlite3G
f5ad0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
f5ae0 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 2c he.xCachesize(p,
f5af0 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a pCache->nMax);.
f5b00 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43 61 63 pCache->pCac
f5b10 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 65 he = p;. }.. e
f5b20 43 72 65 61 74 65 20 3d 20 63 72 65 61 74 65 46 Create = createF
f5b30 6c 61 67 20 3f 20 31 20 3a 20 30 3b 0a 20 20 69 lag ? 1 : 0;. i
f5b40 66 28 20 65 43 72 65 61 74 65 20 26 26 20 28 21 f( eCreate && (!
f5b50 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
f5b60 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70 le || !pCache->p
f5b70 44 69 72 74 79 29 20 29 7b 0a 20 20 20 20 65 43 Dirty) ){. eC
f5b80 72 65 61 74 65 20 3d 20 32 3b 0a 20 20 7d 0a 20 reate = 2;. }.
f5b90 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 if( pCache->pCa
f5ba0 63 68 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 che ){. pPage
f5bb0 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
f5bc0 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 46 Config.pcache.xF
f5bd0 65 74 63 68 28 70 43 61 63 68 65 2d 3e 70 43 61 etch(pCache->pCa
f5be0 63 68 65 2c 20 70 67 6e 6f 2c 20 65 43 72 65 61 che, pgno, eCrea
f5bf0 74 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 te);. }.. if(
f5c00 21 70 50 61 67 65 20 26 26 20 65 43 72 65 61 74 !pPage && eCreat
f5c10 65 3d 3d 31 20 29 7b 0a 20 20 20 20 50 67 48 64 e==1 ){. PgHd
f5c20 72 20 2a 70 50 67 3b 0a 0a 20 20 20 20 2f 2a 20 r *pPg;.. /*
f5c30 46 69 6e 64 20 61 20 64 69 72 74 79 20 70 61 67 Find a dirty pag
f5c40 65 20 74 6f 20 77 72 69 74 65 2d 6f 75 74 20 61 e to write-out a
f5c50 6e 64 20 72 65 63 79 63 6c 65 2e 20 46 69 72 73 nd recycle. Firs
f5c60 74 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20 t try to find a
f5c70 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 74 68 61 . ** page tha
f5c80 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 t does not requi
f5c90 72 65 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e re a journal-syn
f5ca0 63 20 28 6f 6e 65 20 77 69 74 68 20 50 47 48 44 c (one with PGHD
f5cb0 52 5f 4e 45 45 44 5f 53 59 4e 43 0a 20 20 20 20 R_NEED_SYNC.
f5cc0 2a 2a 20 63 6c 65 61 72 65 64 29 2c 20 62 75 74 ** cleared), but
f5cd0 20 69 66 20 74 68 61 74 20 69 73 20 6e 6f 74 20 if that is not
f5ce0 70 6f 73 73 69 62 6c 65 20 73 65 74 74 6c 65 20 possible settle
f5cf0 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 0a 20 for any other .
f5d00 20 20 20 2a 2a 20 75 6e 72 65 66 65 72 65 6e 63 ** unreferenc
f5d10 65 64 20 64 69 72 74 79 20 70 61 67 65 2e 0a 20 ed dirty page..
f5d20 20 20 20 2a 2f 0a 20 20 20 20 65 78 70 65 6e 73 */. expens
f5d30 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 ive_assert( pcac
f5d40 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43 heCheckSynced(pC
f5d50 61 63 68 65 29 20 29 3b 0a 20 20 20 20 66 6f 72 ache) );. for
f5d60 28 70 50 67 3d 70 43 61 63 68 65 2d 3e 70 53 79 (pPg=pCache->pSy
f5d70 6e 63 65 64 3b 20 0a 20 20 20 20 20 20 20 20 70 nced; . p
f5d80 50 67 20 26 26 20 28 70 50 67 2d 3e 6e 52 65 66 Pg && (pPg->nRef
f5d90 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 || (pPg->flags&
f5da0 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
f5db0 29 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 3d ); . pPg=
f5dc0 70 50 67 2d 3e 70 44 69 72 74 79 50 72 65 76 0a pPg->pDirtyPrev.
f5dd0 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 );. if( !
f5de0 70 50 67 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 pPg ){. for
f5df0 28 70 50 67 3d 70 43 61 63 68 65 2d 3e 70 44 69 (pPg=pCache->pDi
f5e00 72 74 79 54 61 69 6c 3b 20 70 50 67 20 26 26 20 rtyTail; pPg &&
f5e10 70 50 67 2d 3e 6e 52 65 66 3b 20 70 50 67 3d 70 pPg->nRef; pPg=p
f5e20 50 67 2d 3e 70 44 69 72 74 79 50 72 65 76 29 3b Pg->pDirtyPrev);
f5e30 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
f5e40 50 67 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 Pg ){. int
f5e50 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 rc;. rc = p
f5e60 43 61 63 68 65 2d 3e 78 53 74 72 65 73 73 28 70 Cache->xStress(p
f5e70 43 61 63 68 65 2d 3e 70 53 74 72 65 73 73 2c 20 Cache->pStress,
f5e80 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 pPg);. if(
f5e90 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc!=SQLITE_OK &&
f5ea0 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc!=SQLITE_BUSY
f5eb0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
f5ec0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
f5ed0 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 20 }.. pPage
f5ee0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
f5ef0 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 46 65 onfig.pcache.xFe
f5f00 74 63 68 28 70 43 61 63 68 65 2d 3e 70 43 61 63 tch(pCache->pCac
f5f10 68 65 2c 20 70 67 6e 6f 2c 20 32 29 3b 0a 20 20 he, pgno, 2);.
f5f20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 }.. if( pPage )
f5f30 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 50 61 {. if( 0==pPa
f5f40 67 65 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 ge->nRef ){.
f5f50 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2b 2b pCache->nRef++
f5f60 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 ;. }. pPag
f5f70 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 70 e->nRef++;. p
f5f80 50 61 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 Page->pData = (v
f5f90 6f 69 64 2a 29 26 70 50 61 67 65 5b 31 5d 3b 0a oid*)&pPage[1];.
f5fa0 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72 pPage->pExtr
f5fb0 61 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 a = (void*)&((ch
f5fc0 61 72 2a 29 70 50 61 67 65 2d 3e 70 44 61 74 61 ar*)pPage->pData
f5fd0 29 5b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 )[pCache->szPage
f5fe0 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 43 ];. pPage->pC
f5ff0 61 63 68 65 20 3d 20 70 43 61 63 68 65 3b 0a 20 ache = pCache;.
f6000 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d pPage->pgno =
f6010 20 70 67 6e 6f 3b 0a 20 20 20 20 69 66 28 20 70 pgno;. if( p
f6020 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 gno==1 ){.
f6030 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d pCache->pPage1 =
f6040 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 pPage;. }.
f6050 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 }. *ppPage = pP
f6060 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 age;. return (p
f6070 50 61 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61 Page==0 && eCrea
f6080 74 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d te) ? SQLITE_NOM
f6090 45 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a EM : SQLITE_OK;.
f60a0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 }../*.** Decreme
f60b0 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 nt the reference
f60c0 20 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 count on a page
f60d0 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 . If the page is
f60e0 20 63 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a clean and the.*
f60f0 2a 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e * reference coun
f6100 74 20 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 t drops to 0, th
f6110 65 6e 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c en it is made el
f6120 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 ible for recycli
f6130 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ng..*/.SQLITE_PR
f6140 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
f6150 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 e3PcacheRelease(
f6160 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 PgHdr *p){. ass
f6170 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 ert( p->nRef>0 )
f6180 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 ;. p->nRef--;.
f6190 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 if( p->nRef==0
f61a0 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70 ){. PCache *p
f61b0 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 Cache = p->pCach
f61c0 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e e;. pCache->n
f61d0 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 Ref--;. if( (
f61e0 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 p->flags&PGHDR_D
f61f0 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 IRTY)==0 ){.
f6200 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 pcacheUnpin(p)
f6210 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
f6220 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 /* Move the p
f6230 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 age to the head
f6240 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 of the dirty lis
f6250 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 t. */. pcac
f6260 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
f6270 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20 yList(p);.
f6280 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 pcacheAddToDirty
f6290 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 List(p);. }.
f62a0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 }.}../*.** Incr
f62b0 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e ease the referen
f62c0 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 ce count of a su
f62d0 70 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 31 pplied page by 1
f62e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
f62f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
f6300 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20 PcacheRef(PgHdr
f6310 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d *p){. assert(p-
f6320 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e >nRef>0);. p->n
f6330 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Ref++;.}../*.**
f6340 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d Drop a page from
f6350 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72 the cache. Ther
f6360 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c e must be exactl
f6370 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 y one reference
f6380 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 to the.** page.
f6390 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 This function de
f63a0 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72 letes that refer
f63b0 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69 ence, so after i
f63c0 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a t returns the.**
f63d0 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f page pointed to
f63e0 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64 by p is invalid
f63f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
f6400 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
f6410 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 PcacheDrop(PgHdr
f6420 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a *p){. PCache *
f6430 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 pCache;. assert
f6440 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a ( p->nRef==1 );.
f6450 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 if( p->flags&P
f6460 47 48 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 GHDR_DIRTY ){.
f6470 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 pcacheRemoveFr
f6480 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a omDirtyList(p);.
f6490 20 20 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70 }. pCache = p
f64a0 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63 ->pCache;. pCac
f64b0 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 he->nRef--;. if
f64c0 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a ( p->pgno==1 ){.
f64d0 20 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 pCache->pPag
f64e0 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 e1 = 0;. }. sq
f64f0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
f6500 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 g.pcache.xUnpin(
f6510 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
f6520 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 p, 1);.}../*.**
f6530 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 Make sure the pa
f6540 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 ge is marked as
f6550 64 69 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e dirty. If it isn
f6560 27 74 20 64 69 72 74 79 20 61 6c 72 65 61 64 79 't dirty already
f6570 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e ,.** make it so.
f6580 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
f6590 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
f65a0 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50 cacheMakeDirty(P
f65b0 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43 61 63 gHdr *p){. PCac
f65c0 68 65 20 2a 70 43 61 63 68 65 3b 0a 20 20 70 2d he *pCache;. p-
f65d0 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 >flags &= ~PGHDR
f65e0 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 20 20 61 _DONT_WRITE;. a
f65f0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 ssert( p->nRef>0
f6600 20 29 3b 0a 20 20 69 66 28 20 30 3d 3d 28 70 2d );. if( 0==(p-
f6610 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 >flags & PGHDR_D
f6620 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70 43 61 IRTY) ){. pCa
f6630 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b che = p->pCache;
f6640 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d . p->flags |=
f6650 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a 20 20 PGHDR_DIRTY;.
f6660 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 pcacheAddToDir
f6670 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20 7d 0a tyList( p);. }.
f6680 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 }../*.** Make su
f6690 72 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6d re the page is m
f66a0 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e 2e 20 arked as clean.
f66b0 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c 65 61 If it isn't clea
f66c0 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 n already,.** ma
f66d0 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c ke it so..*/.SQL
f66e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
f66f0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 sqlite3PcacheMa
f6700 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 keClean(PgHdr *p
f6710 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 ){. if( (p->fla
f6720 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 gs & PGHDR_DIRTY
f6730 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52 ) ){. pcacheR
f6740 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 emoveFromDirtyLi
f6750 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c st(p);. p->fl
f6760 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52 5f 44 ags &= ~(PGHDR_D
f6770 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45 44 5f IRTY|PGHDR_NEED_
f6780 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 20 70 SYNC);. if( p
f6790 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 ->nRef==0 ){.
f67a0 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 pcacheUnpin(p
f67b0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
f67c0 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65 72 79 /*.** Make every
f67d0 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 page in the cac
f67e0 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51 4c he clean..*/.SQL
f67f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
f6800 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c sqlite3PcacheCl
f6810 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20 2a 70 eanAll(PCache *p
f6820 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20 Cache){. PgHdr
f6830 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 20 *p;. while( (p
f6840 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 = pCache->pDirty
f6850 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 )!=0 ){. sqli
f6860 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
f6870 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a an(p);. }.}../*
f6880 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 50 47 .** Clear the PG
f6890 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c HDR_NEED_SYNC fl
f68a0 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69 72 74 ag from all dirt
f68b0 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51 4c 49 y pages..*/.SQLI
f68c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
f68d0 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
f68e0 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 arSyncFlags(PCac
f68f0 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 he *pCache){. P
f6900 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 gHdr *p;. for(p
f6910 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b =pCache->pDirty;
f6920 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e p; p=p->pDirtyN
f6930 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 ext){. p->fla
f6940 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 gs &= ~PGHDR_NEE
f6950 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 70 43 D_SYNC;. }. pC
f6960 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 3d 20 ache->pSynced =
f6970 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 pCache->pDirtyTa
f6980 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 il;.}../*.** Cha
f6990 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e 75 6d nge the page num
f69a0 62 65 72 20 6f 66 20 70 61 67 65 20 70 20 74 6f ber of page p to
f69b0 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a 53 51 newPgno. .*/.SQ
f69c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
f69d0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d d sqlite3PcacheM
f69e0 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20 50 67 ove(PgHdr *p, Pg
f69f0 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 50 no newPgno){. P
f6a00 43 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 Cache *pCache =
f6a10 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61 73 73 p->pCache;. ass
f6a20 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 ert( p->nRef>0 )
f6a30 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65 77 50 ;. assert( newP
f6a40 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c 69 74 gno>0 );. sqlit
f6a50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
f6a60 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70 43 61 cache.xRekey(pCa
f6a70 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 che->pCache, p,
f6a80 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67 6e 6f p->pgno, newPgno
f6a90 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d 20 6e );. p->pgno = n
f6aa0 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20 28 70 ewPgno;. if( (p
f6ab0 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
f6ac0 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c 61 67 RTY) && (p->flag
f6ad0 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
f6ae0 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 C) ){. pcache
f6af0 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c RemoveFromDirtyL
f6b00 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63 61 63 ist(p);. pcac
f6b10 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74 heAddToDirtyList
f6b20 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
f6b30 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63 61 63 * Drop every cac
f6b40 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65 20 70 he entry whose p
f6b50 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 72 age number is gr
f6b60 65 61 74 65 72 20 74 68 61 6e 20 22 70 67 6e 6f eater than "pgno
f6b70 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 ". The.** caller
f6b80 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 must ensure tha
f6b90 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f t there are no o
f6ba0 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
f6bb0 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67 ences to any pag
f6bc0 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e es.** other than
f6bd0 20 70 61 67 65 20 31 20 77 69 74 68 20 61 20 70 page 1 with a p
f6be0 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65 61 74 age number great
f6bf0 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a 2a 2a er than pgno..**
f6c00 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 .** If there is
f6c10 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 a reference to p
f6c20 61 67 65 20 31 20 61 6e 64 20 74 68 65 20 70 67 age 1 and the pg
f6c30 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 no parameter pas
f6c40 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 sed to this.** f
f6c50 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20 74 68 unction is 0, th
f6c60 65 6e 20 74 68 65 20 64 61 74 61 20 61 72 65 61 en the data area
f6c70 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
f6c80 20 70 61 67 65 20 31 20 69 73 20 7a 65 72 6f 65 page 1 is zeroe
f6c90 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20 70 61 d, but.** the pa
f6ca0 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 ge object is not
f6cb0 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 4c dropped..*/.SQL
f6cc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
f6cd0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 sqlite3PcacheTr
f6ce0 75 6e 63 61 74 65 28 50 43 61 63 68 65 20 2a 70 uncate(PCache *p
f6cf0 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67 6e 6f Cache, Pgno pgno
f6d00 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d ){. if( pCache-
f6d10 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 50 >pCache ){. P
f6d20 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 67 48 gHdr *p;. PgH
f6d30 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20 20 66 dr *pNext;. f
f6d40 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 or(p=pCache->pDi
f6d50 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 rty; p; p=pNext)
f6d60 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 {. pNext =
f6d70 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 p->pDirtyNext;.
f6d80 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f if( p->pgno
f6d90 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 >pgno ){.
f6da0 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61 67 assert( p->flag
f6db0 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b s&PGHDR_DIRTY );
f6dc0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
f6dd0 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 PcacheMakeClean(
f6de0 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
f6df0 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d }. if( pgno==
f6e00 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 50 61 0 && pCache->pPa
f6e10 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ge1 ){. mem
f6e20 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50 61 67 set(pCache->pPag
f6e30 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 43 e1->pData, 0, pC
f6e40 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b 0a 20 ache->szPage);.
f6e50 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b 0a 20 pgno = 1;.
f6e60 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
f6e70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
f6e80 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28 70 43 che.xTruncate(pC
f6e90 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 ache->pCache, pg
f6ea0 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a no+1);. }.}../*
f6eb0 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61 63 68 .** Close a cach
f6ec0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
f6ed0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
f6ee0 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50 43 61 3PcacheClose(PCa
f6ef0 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 che *pCache){.
f6f00 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 if( pCache->pCac
f6f10 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 he ){. sqlite
f6f20 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
f6f30 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28 70 43 ache.xDestroy(pC
f6f40 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 ache->pCache);.
f6f50 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 69 73 }.}../* .** Dis
f6f60 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 card the content
f6f70 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 2e 0a s of the cache..
f6f80 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
f6f90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
f6fa0 63 68 65 43 6c 65 61 72 28 50 43 61 63 68 65 20 cheClear(PCache
f6fb0 2a 70 43 61 63 68 65 29 7b 0a 20 20 73 71 6c 69 *pCache){. sqli
f6fc0 74 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 te3PcacheTruncat
f6fd0 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20 e(pCache, 0);.
f6fe0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f6ff0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 ;.}../*.** Merge
f7000 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 two lists of pa
f7010 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 ges connected by
f7020 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 pDirty and in p
f7030 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f gno order..** Do
f7040 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 not both fixing
f7050 20 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20 the pDirtyPrev
f7060 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 pointers..*/.sta
f7070 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 tic PgHdr *pcach
f7080 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 eMergeDirtyList(
f7090 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 PgHdr *pA, PgHdr
f70a0 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 *pB){. PgHdr r
f70b0 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 esult, *pTail;.
f70c0 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 pTail = &result
f70d0 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 ;. while( pA &&
f70e0 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 pB ){. if( p
f70f0 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f A->pgno<pB->pgno
f7100 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d ){. pTail-
f7110 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 >pDirty = pA;.
f7120 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a pTail = pA;.
f7130 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 pA = pA->p
f7140 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 Dirty;. }else
f7150 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 {. pTail->p
f7160 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 Dirty = pB;.
f7170 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 pTail = pB;.
f7180 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 pB = pB->pDi
f7190 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 rty;. }. }.
f71a0 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 if( pA ){. p
f71b0 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 Tail->pDirty = p
f71c0 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 A;. }else if( p
f71d0 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e B ){. pTail->
f71e0 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d pDirty = pB;. }
f71f0 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d else{. pTail-
f7200 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d >pDirty = 0;. }
f7210 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 . return result
f7220 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a .pDirty;.}../*.*
f7230 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 * Sort the list
f7240 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 of pages in acce
f7250 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 nding order by p
f7260 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a gno. Pages are.
f7270 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 ** connected by
f7280 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e pDirty pointers.
f7290 20 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76 The pDirtyPrev
f72a0 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a pointers are.**
f72b0 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 corrupted by th
f72c0 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 65 66 is sort..*/.#def
f72d0 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 ine N_SORT_BUCKE
f72e0 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 65 66 69 T_ALLOC 25.#defi
f72f0 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 ne N_SORT_BUCKET
f7300 20 20 20 20 20 20 20 32 35 0a 23 69 66 64 65 66 25.#ifdef
f7310 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 SQLITE_TEST. i
f7320 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 nt sqlite3_pager
f7330 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 20 3d _n_sort_bucket =
f7340 20 30 3b 0a 20 20 23 75 6e 64 65 66 20 4e 5f 53 0;. #undef N_S
f7350 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20 23 64 65 ORT_BUCKET. #de
f7360 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b fine N_SORT_BUCK
f7370 45 54 20 5c 0a 20 20 20 28 73 71 6c 69 74 65 33 ET \. (sqlite3
f7380 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 _pager_n_sort_bu
f7390 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f 70 61 67 cket?sqlite3_pag
f73a0 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 er_n_sort_bucket
f73b0 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41 :N_SORT_BUCKET_A
f73c0 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a 73 74 61 LLOC).#endif.sta
f73d0 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 tic PgHdr *pcach
f73e0 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28 50 eSortDirtyList(P
f73f0 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50 67 gHdr *pIn){. Pg
f7400 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55 Hdr *a[N_SORT_BU
f7410 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a 70 3b CKET_ALLOC], *p;
f7420 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 . int i;. mems
f7430 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 et(a, 0, sizeof(
f7440 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 49 a));. while( pI
f7450 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e n ){. p = pIn
f7460 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70 ;. pIn = p->p
f7470 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44 Dirty;. p->pD
f7480 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f irty = 0;. fo
f7490 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52 54 5f r(i=0; i<N_SORT_
f74a0 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b 29 7b 0a BUCKET-1; i++){.
f74b0 20 20 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d if( a[i]==
f74c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69 0 ){. a[i
f74d0 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 ] = p;. b
f74e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 reak;. }els
f74f0 65 7b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 e{. p = p
f7500 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c cacheMergeDirtyL
f7510 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 ist(a[i], p);.
f7520 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a a[i] = 0;.
f7530 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
f7540 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f 52 54 5f if( i==N_SORT_
f7550 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 20 20 20 BUCKET-1 ){.
f7560 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a 20 54 /* Coverage: T
f7570 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 72 o get here, ther
f7580 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28 e need to be 2^(
f7590 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 0a N_SORT_BUCKET) .
f75a0 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 ** element
f75b0 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 6c s in the input l
f75c0 69 73 74 2e 20 54 68 69 73 20 69 73 20 70 6f 73 ist. This is pos
f75d0 73 69 62 6c 65 2c 20 62 75 74 20 69 6d 70 72 61 sible, but impra
f75e0 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 20 2a 2a ctical.. **
f75f0 20 54 65 73 74 69 6e 67 20 74 68 69 73 20 6c 69 Testing this li
f7600 6e 65 20 69 73 20 74 68 65 20 70 6f 69 6e 74 20 ne is the point
f7610 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 of global variab
f7620 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 le. ** sqli
f7630 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 te3_pager_n_sort
f7640 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 20 20 2a _bucket.. *
f7650 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 /. a[i] = p
f7660 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c cacheMergeDirtyL
f7670 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 ist(a[i], p);.
f7680 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b }. }. p = a[
f7690 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 0];. for(i=1; i
f76a0 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 <N_SORT_BUCKET;
f76b0 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63 i++){. p = pc
f76c0 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 acheMergeDirtyLi
f76d0 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d st(p, a[i]);. }
f76e0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
f76f0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c /*.** Return a l
f7700 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 ist of all dirty
f7710 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 pages in the ca
f7720 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 che, sorted by p
f7730 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53 age number..*/.S
f7740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 QLITE_PRIVATE Pg
f7750 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63 Hdr *sqlite3Pcac
f7760 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 63 heDirtyList(PCac
f7770 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 he *pCache){. P
f7780 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 gHdr *p;. for(p
f7790 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b =pCache->pDirty;
f77a0 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e p; p=p->pDirtyN
f77b0 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 ext){. p->pDi
f77c0 72 74 79 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e rty = p->pDirtyN
f77d0 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ext;. }. retur
f77e0 6e 20 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 n pcacheSortDirt
f77f0 79 4c 69 73 74 28 70 43 61 63 68 65 2d 3e 70 44 yList(pCache->pD
f7800 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a irty);.}../* .**
f7810 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 Return the tota
f7820 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 l number of refe
f7830 72 65 6e 63 65 64 20 70 61 67 65 73 20 68 65 6c renced pages hel
f7840 64 20 62 79 20 74 68 65 20 63 61 63 68 65 2e 0a d by the cache..
f7850 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
f7860 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
f7870 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 cheRefCount(PCac
f7880 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 he *pCache){. r
f7890 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52 eturn pCache->nR
f78a0 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ef;.}../*.** Ret
f78b0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
f78c0 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 f references to
f78d0 74 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 the page supplie
f78e0 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 d as an argument
f78f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
f7900 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
f7910 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e cachePageRefcoun
f7920 74 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 72 t(PgHdr *p){. r
f7930 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a 7d eturn p->nRef;.}
f7940 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 ../* .** Return
f7950 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
f7960 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
f7970 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 cache..*/.SQLIT
f7980 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
f7990 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 lite3PcachePagec
f79a0 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61 ount(PCache *pCa
f79b0 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 che){. int nPag
f79c0 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 61 e = 0;. if( pCa
f79d0 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 che->pCache ){.
f79e0 20 20 20 6e 50 61 67 65 20 3d 20 73 71 6c 69 74 nPage = sqlit
f79f0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
f7a00 63 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 cache.xPagecount
f7a10 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 (pCache->pCache)
f7a20 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e ;. }. return n
f7a30 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 Page;.}..#ifdef
f7a40 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a SQLITE_TEST./*.*
f7a50 2a 20 47 65 74 20 74 68 65 20 73 75 67 67 65 73 * Get the sugges
f7a60 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 ted cache-size v
f7a70 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f alue..*/.SQLITE_
f7a80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
f7a90 74 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 te3PcacheGetCach
f7aa0 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 esize(PCache *pC
f7ab0 61 63 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 ache){. return
f7ac0 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 7d 0a pCache->nMax;.}.
f7ad0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 #endif../*.** Se
f7ae0 74 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 t the suggested
f7af0 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65 cache-size value
f7b00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
f7b10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
f7b20 50 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69 PcacheSetCachesi
f7b30 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 ze(PCache *pCach
f7b40 65 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a e, int mxPage){.
f7b50 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d pCache->nMax =
f7b60 20 6d 78 50 61 67 65 3b 0a 20 20 69 66 28 20 70 mxPage;. if( p
f7b70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b Cache->pCache ){
f7b80 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
f7b90 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
f7ba0 78 43 61 63 68 65 73 69 7a 65 28 70 43 61 63 68 xCachesize(pCach
f7bb0 65 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50 61 67 e->pCache, mxPag
f7bc0 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 e);. }.}..#ifde
f7bd0 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
f7be0 41 47 45 53 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 AGES./*.** For a
f7bf0 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 63 ll dirty pages c
f7c00 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 urrently in the
f7c10 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68 cache, invoke th
f7c20 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 63 e specified.** c
f7c30 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 20 69 73 allback. This is
f7c40 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 only used if th
f7c50 65 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 e SQLITE_CHECK_P
f7c60 41 47 45 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a AGES macro is.**
f7c70 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c defined..*/.SQL
f7c80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
f7c90 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 sqlite3PcacheIt
f7ca0 65 72 61 74 65 44 69 72 74 79 28 50 43 61 63 68 erateDirty(PCach
f7cb0 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 e *pCache, void
f7cc0 28 2a 78 49 74 65 72 29 28 50 67 48 64 72 20 2a (*xIter)(PgHdr *
f7cd0 29 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69 )){. PgHdr *pDi
f7ce0 72 74 79 3b 0a 20 20 66 6f 72 28 70 44 69 72 74 rty;. for(pDirt
f7cf0 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 y=pCache->pDirty
f7d00 3b 20 70 44 69 72 74 79 3b 20 70 44 69 72 74 79 ; pDirty; pDirty
f7d10 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 74 79 4e =pDirty->pDirtyN
f7d20 65 78 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28 ext){. xIter(
f7d30 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 pDirty);. }.}.#
f7d40 65 6e 64 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a endif.../*******
f7d50 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 ******* End of p
f7d60 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a cache.c ********
f7d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7d90 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
f7da0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
f7db0 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a le pcache1.c ***
f7dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7de0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
f7df0 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35 0a 2a 08 November 05.*
f7e00 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
f7e10 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
f7e20 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
f7e30 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
f7e40 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
f7e50 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
f7e60 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
f7e70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
f7e80 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
f7e90 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
f7ea0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
f7eb0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
f7ec0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
f7ed0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
f7ee0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
f7ef0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
f7f00 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
f7f10 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
f7f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
f7f60 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d .** This file im
f7f70 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 65 66 plements the def
f7f80 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20 ault page cache
f7f90 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 implementation (
f7fa0 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 the.** sqlite3_p
f7fb0 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65 29 cache interface)
f7fc0 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 . It also contai
f7fd0 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 ns part of the i
f7fe0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a mplementation.**
f7ff0 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 of the SQLITE_C
f8000 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 ONFIG_PAGECACHE
f8010 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 and sqlite3_rele
f8020 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66 65 61 ase_memory() fea
f8030 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 tures..** If the
f8040 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 default page ca
f8050 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 che implementati
f8060 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65 6e 2c on is overriden,
f8070 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 6f 66 then neither of
f8080 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20 66 65 .** these two fe
f8090 61 74 75 72 65 73 20 61 72 65 20 61 76 61 69 6c atures are avail
f80a0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 able..**.** @(#)
f80b0 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e 63 2c $Id: pcache1.c,
f80c0 76 20 31 2e 33 20 32 30 30 38 2f 31 31 2f 31 39 v 1.3 2008/11/19
f80d0 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65 6c 09:05:27 daniel
f80e0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
f80f0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
f8100 50 43 61 63 68 65 31 20 50 43 61 63 68 65 31 3b PCache1 PCache1;
f8110 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
f8120 50 67 48 64 72 31 20 50 67 48 64 72 31 3b 0a 74 PgHdr1 PgHdr1;.t
f8130 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 ypedef struct Pg
f8140 46 72 65 65 73 6c 6f 74 20 50 67 46 72 65 65 73 Freeslot PgFrees
f8150 6c 6f 74 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 lot;../* Pointer
f8160 73 20 74 6f 20 73 74 72 75 63 74 75 72 65 73 20 s to structures
f8170 6f 66 20 74 68 69 73 20 74 79 70 65 20 61 72 65 of this type are
f8180 20 63 61 73 74 20 61 6e 64 20 72 65 74 75 72 6e cast and return
f8190 65 64 20 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 ed as .** opaque
f81a0 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a sqlite3_pcache*
f81b0 20 68 61 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 handles.*/.stru
f81c0 63 74 20 50 43 61 63 68 65 31 20 7b 0a 20 20 2f ct PCache1 {. /
f81d0 2a 20 43 61 63 68 65 20 63 6f 6e 66 69 67 75 72 * Cache configur
f81e0 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 ation parameters
f81f0 2e 20 50 61 67 65 20 73 69 7a 65 20 28 73 7a 50 . Page size (szP
f8200 61 67 65 29 20 61 6e 64 20 74 68 65 20 70 75 72 age) and the pur
f8210 67 65 61 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 geable. ** flag
f8220 20 28 62 50 75 72 67 65 61 62 6c 65 29 20 61 72 (bPurgeable) ar
f8230 65 20 73 65 74 20 77 68 65 6e 20 74 68 65 20 63 e set when the c
f8240 61 63 68 65 20 69 73 20 63 72 65 61 74 65 64 2e ache is created.
f8250 20 6e 4d 61 78 20 6d 61 79 20 62 65 20 0a 20 20 nMax may be .
f8260 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 74 20 61 ** modified at a
f8270 6e 79 20 74 69 6d 65 20 62 79 20 61 20 63 61 6c ny time by a cal
f8280 6c 20 74 6f 20 74 68 65 20 70 63 61 63 68 65 31 l to the pcache1
f8290 43 61 63 68 65 53 69 7a 65 28 29 20 6d 65 74 68 CacheSize() meth
f82a0 6f 64 2e 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f od.. ** The glo
f82b0 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 bal mutex must b
f82c0 65 20 68 65 6c 64 20 77 68 65 6e 20 61 63 63 65 e held when acce
f82d0 73 73 69 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f ssing nMax.. */
f82e0 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 . int szPage;
f82f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8300 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
f8310 66 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 f allocated page
f8320 73 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 s in bytes */.
f8330 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 int bPurgeable;
f8340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8350 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 /* True if c
f8360 61 63 68 65 20 69 73 20 70 75 72 67 65 61 62 6c ache is purgeabl
f8370 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 e */. unsigned
f8380 69 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 int nMin;
f8390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 /* Mi
f83a0 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 nimum number of
f83b0 70 61 67 65 73 20 72 65 73 65 72 76 65 64 20 2a pages reserved *
f83c0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
f83d0 20 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 nMax;
f83e0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 /* Confi
f83f0 67 75 72 65 64 20 22 63 61 63 68 65 5f 73 69 7a gured "cache_siz
f8400 65 22 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f e" value */.. /
f8410 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 * Hash table of
f8420 61 6c 6c 20 70 61 67 65 73 2e 20 54 68 65 20 66 all pages. The f
f8430 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c ollowing variabl
f8440 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 es may only be a
f8450 63 63 65 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 ccessed. ** whe
f8460 6e 20 74 68 65 20 61 63 63 65 73 73 6f 72 20 69 n the accessor i
f8470 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c s holding the gl
f8480 6f 62 61 6c 20 6d 75 74 65 78 20 28 73 65 65 20 obal mutex (see
f8490 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
f84a0 78 28 29 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 x() . ** and pc
f84b0 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 ache1LeaveMutex(
f84c0 29 29 2e 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 )).. */. unsig
f84d0 6e 65 64 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 ned int nRecycla
f84e0 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f ble; /
f84f0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
f8500 73 20 69 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 s in the LRU lis
f8510 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 t */. unsigned
f8520 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 int nPage;
f8530 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f /* To
f8540 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 tal number of pa
f8550 67 65 73 20 69 6e 20 61 70 48 61 73 68 20 2a 2f ges in apHash */
f8560 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
f8570 6e 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 nHash;
f8580 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
f8590 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 of slots in apH
f85a0 61 73 68 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 ash[] */. PgHdr
f85b0 31 20 2a 2a 61 70 48 61 73 68 3b 20 20 20 20 20 1 **apHash;
f85c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f85d0 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 * Hash table for
f85e0 20 66 61 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 fast lookup by
f85f0 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a key */.};../*.**
f8600 20 45 61 63 68 20 63 61 63 68 65 20 65 6e 74 72 Each cache entr
f8610 79 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 y is represented
f8620 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 by an instance
f8630 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
f8640 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 .** structure.
f8650 41 20 62 75 66 66 65 72 20 6f 66 20 50 67 48 64 A buffer of PgHd
f8660 72 31 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 r1.pCache->szPag
f8670 65 20 62 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 e bytes is alloc
f8680 61 74 65 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c ated .** directl
f8690 79 20 61 66 74 65 72 20 74 68 65 20 73 74 72 75 y after the stru
f86a0 63 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 cture in memory
f86b0 28 73 65 65 20 74 68 65 20 50 47 48 44 52 31 5f (see the PGHDR1_
f86c0 54 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 TO_PAGE() .** ma
f86d0 63 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 cro below)..*/.s
f86e0 74 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20 truct PgHdr1 {.
f86f0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b unsigned int iK
f8700 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ey;
f8710 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61 /* Key value (pa
f8720 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 ge number) */.
f8730 50 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 PgHdr1 *pNext;
f8740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f8750 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74 * Next in hash t
f8760 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 able chain */.
f8770 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b PCache1 *pCache;
f8780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f8790 2a 20 43 61 63 68 65 20 74 68 61 74 20 63 75 72 * Cache that cur
f87a0 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 rently owns this
f87b0 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 page */. PgHdr
f87c0 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 1 *pLruNext;
f87d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 /* Nex
f87e0 74 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 t in LRU list of
f87f0 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 unpinned pages
f8800 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 */. PgHdr1 *pLr
f8810 75 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 uPrev;
f8820 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 /* Previous
f8830 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 in LRU list of u
f8840 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f npinned pages */
f8850 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 .};../*.** Free
f8860 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c slots in the all
f8870 6f 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64 ocator used to d
f8880 69 76 69 64 65 20 75 70 20 74 68 65 20 62 75 66 ivide up the buf
f8890 66 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 69 fer provided usi
f88a0 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 ng.** the SQLITE
f88b0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
f88c0 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a E mechanism..*/.
f88d0 73 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f struct PgFreeslo
f88e0 74 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 t {. PgFreeslot
f88f0 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 *pNext; /* Nex
f8900 74 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d t free slot */.}
f8910 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 ;../*.** Global
f8920 64 61 74 61 20 75 73 65 64 20 62 79 20 74 68 69 data used by thi
f8930 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 s cache..*/.stat
f8940 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 ic SQLITE_WSD st
f8950 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 ruct PCacheGloba
f8960 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 l {. sqlite3_mu
f8970 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 tex *mutex;
f8980 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 /* sta
f8990 74 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f tic mutex MUTEX_
f89a0 53 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 STATIC_LRU */..
f89b0 20 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 int nMaxPage;
f89c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f89d0 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e /* Sum of n
f89e0 4d 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 67 MaxPage for purg
f89f0 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a eable caches */.
f8a00 20 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 int nMinPage;
f8a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8a20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 /* Sum of
f8a30 6e 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72 nMinPage for pur
f8a40 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f geable caches */
f8a50 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 . int nCurrentP
f8a60 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
f8a70 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
f8a80 20 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 of purgeable pa
f8a90 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f ges allocated */
f8aa0 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 . PgHdr1 *pLruH
f8ab0 65 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 ead, *pLruTail;
f8ac0 20 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 /* LRU li
f8ad0 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 st of unpinned p
f8ae0 61 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 ages */.. /* Va
f8af0 72 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20 riables related
f8b00 74 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 to SQLITE_CONFIG
f8b10 5f 50 41 47 45 43 41 43 48 45 20 73 65 74 74 69 _PAGECACHE setti
f8b20 6e 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a ngs. */. int sz
f8b30 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 Slot;
f8b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f8b50 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 Size of each fr
f8b60 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 ee slot */. voi
f8b70 64 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 d *pStart, *pEnd
f8b80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
f8b90 20 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 /* Bounds of pa
f8ba0 67 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 gecache malloc r
f8bb0 61 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 ange */. PgFree
f8bc0 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 slot *pFree;
f8bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f8be0 20 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b Free page block
f8bf0 73 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 s */.} pcache1_g
f8c00 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 ;../*.** All cod
f8c10 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 e in this file s
f8c20 68 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 hould access the
f8c30 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 global structur
f8c40 65 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a e above via the.
f8c50 2a 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 ** alias "pcache
f8c60 31 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 1". This ensures
f8c70 20 74 68 61 74 20 74 68 65 20 57 53 44 20 65 6d that the WSD em
f8c80 75 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 ulation is used
f8c90 77 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e when.** compilin
f8ca0 67 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68 g for systems th
f8cb0 61 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 at do not suppor
f8cc0 74 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 t real WSD..*/.#
f8cd0 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 28 define pcache1 (
f8ce0 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43 GLOBAL(struct PC
f8cf0 61 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 acheGlobal, pcac
f8d00 68 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 he1_g))../*.** W
f8d10 68 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 72 hen a PgHdr1 str
f8d20 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 ucture is alloca
f8d30 74 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61 ted, the associa
f8d40 74 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61 ted PCache1.szPa
f8d50 67 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 ge.** bytes of d
f8d60 61 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20 ata are located
f8d70 64 69 72 65 63 74 6c 79 20 61 66 74 65 72 20 69 directly after i
f8d80 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 t in memory (i.e
f8d90 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 . the total.** s
f8da0 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 ize of the alloc
f8db0 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 ation is sizeof(
f8dc0 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e PgHdr1)+PCache1.
f8dd0 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 szPage byte). Th
f8de0 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 e.** PGHDR1_TO_P
f8df0 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 AGE() macro take
f8e00 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
f8e10 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75 72 PgHdr1 structur
f8e20 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d e as.** an argum
f8e30 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ent and returns
f8e40 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
f8e50 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 associated bloc
f8e60 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 k of szPage.** b
f8e70 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 ytes. The PAGE_T
f8e80 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f O_PGHDR1() macro
f8e90 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 does the opposi
f8ea0 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 te: its argument
f8eb0 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 is.** a pointer
f8ec0 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 to a block of s
f8ed0 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 zPage bytes of d
f8ee0 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 ata and the retu
f8ef0 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 rn value is.** a
f8f00 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
f8f10 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 associated PgHdr
f8f20 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 1 structure..**.
f8f30 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47 48 ** assert( PGH
f8f40 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 DR1_TO_PAGE(PAGE
f8f50 5f 54 4f 5f 50 47 48 44 52 31 28 58 29 29 3d 3d _TO_PGHDR1(X))==
f8f60 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 X );.*/.#define
f8f70 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 PGHDR1_TO_PAGE(p
f8f80 29 20 28 76 6f 69 64 20 2a 29 28 26 28 28 75 6e ) (void *)(&((un
f8f90 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 signed char *)p)
f8fa0 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 5d [sizeof(PgHdr1)]
f8fb0 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 5f 54 ).#define PAGE_T
f8fc0 4f 5f 50 47 48 44 52 31 28 70 29 20 28 50 67 48 O_PGHDR1(p) (PgH
f8fd0 64 72 31 20 2a 29 28 26 28 28 75 6e 73 69 67 6e dr1 *)(&((unsign
f8fe0 65 64 20 63 68 61 72 20 2a 29 70 29 5b 2d 31 2a ed char *)p)[-1*
f8ff0 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 5d 29 sizeof(PgHdr1)])
f9000 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 ../*.** Macros t
f9010 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 o enter and leav
f9020 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 e the global LRU
f9030 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 65 66 69 mutex..*/.#defi
f9040 6e 65 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d ne pcache1EnterM
f9050 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d utex() sqlite3_m
f9060 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63 68 utex_enter(pcach
f9070 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 66 69 6e e1.mutex).#defin
f9080 65 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 e pcache1LeaveMu
f9090 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 tex() sqlite3_mu
f90a0 74 65 78 5f 6c 65 61 76 65 28 70 63 61 63 68 65 tex_leave(pcache
f90b0 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a 2a 1.mutex)../*****
f90c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f90d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f90e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f90f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
f9110 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c 6f 63 61 **** Page Alloca
f9120 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e 46 tion/SQLITE_CONF
f9130 49 47 5f 50 43 41 43 48 45 20 52 65 6c 61 74 65 IG_PCACHE Relate
f9140 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a d Functions ****
f9150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
f9160 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
f9170 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69 6e is called durin
f9180 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e g initialization
f9190 20 69 66 20 61 20 73 74 61 74 69 63 20 62 75 66 if a static buf
f91a0 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 70 70 6c fer is .** suppl
f91b0 69 65 64 20 74 6f 20 75 73 65 20 66 6f 72 20 74 ied to use for t
f91c0 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 62 79 he page-cache by
f91d0 20 70 61 73 73 69 6e 67 20 74 68 65 20 53 51 4c passing the SQL
f91e0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
f91f0 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 74 6f 20 ACHE.** verb to
f9200 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
f9210 2e 20 50 61 72 61 6d 65 74 65 72 20 70 42 75 66 . Parameter pBuf
f9220 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c points to an al
f9230 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65 0a 2a location large.*
f9240 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e 74 * enough to cont
f9250 61 69 6e 20 27 6e 27 20 62 75 66 66 65 72 73 20 ain 'n' buffers
f9260 6f 66 20 27 73 7a 27 20 62 79 74 65 73 20 65 61 of 'sz' bytes ea
f9270 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ch..*/.SQLITE_PR
f9280 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
f9290 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65 e3PCacheBufferSe
f92a0 74 75 70 28 76 6f 69 64 20 2a 70 42 75 66 2c 20 tup(void *pBuf,
f92b0 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a int sz, int n){.
f92c0 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b PgFreeslot *p;
f92d0 0a 20 20 73 7a 20 26 3d 20 7e 37 3b 0a 20 20 70 . sz &= ~7;. p
f92e0 63 61 63 68 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 cache1.szSlot =
f92f0 73 7a 3b 0a 20 20 70 63 61 63 68 65 31 2e 70 53 sz;. pcache1.pS
f9300 74 61 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 70 tart = pBuf;. p
f9310 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 cache1.pFree = 0
f9320 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29 ;. while( n-- )
f9330 7b 0a 20 20 20 20 70 20 3d 20 28 50 67 46 72 65 {. p = (PgFre
f9340 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a 20 20 20 eslot*)pBuf;.
f9350 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 p->pNext = pcac
f9360 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 he1.pFree;. p
f9370 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 cache1.pFree = p
f9380 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 28 76 6f ;. pBuf = (vo
f9390 69 64 2a 29 26 28 28 63 68 61 72 2a 29 70 42 75 id*)&((char*)pBu
f93a0 66 29 5b 73 7a 5d 3b 0a 20 20 7d 0a 20 20 70 63 f)[sz];. }. pc
f93b0 61 63 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 ache1.pEnd = pBu
f93c0 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c f;.}../*.** Mall
f93d0 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 oc function used
f93e0 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c within this fil
f93f0 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 e to allocate sp
f9400 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66 ace from the buf
f9410 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65 fer.** configure
f9420 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f d using sqlite3_
f9430 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f config(SQLITE_CO
f9440 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20 NFIG_PAGECACHE)
f9450 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a option. If no .*
f9460 2a 20 73 75 63 68 20 62 75 66 66 65 72 20 65 78 * such buffer ex
f9470 69 73 74 73 20 6f 72 20 74 68 65 72 65 20 69 73 ists or there is
f9480 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74 20 69 no space left i
f9490 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e 63 74 n it, this funct
f94a0 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 62 61 ion falls .** ba
f94b0 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c ck to sqlite3Mal
f94c0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
f94d0 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 41 6c void *pcache1Al
f94e0 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a loc(int nByte){.
f94f0 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 void *p;. ass
f9500 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
f9510 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e ex_held(pcache1.
f9520 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
f9530 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31 2e 73 nByte<=pcache1.s
f9540 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68 65 31 zSlot && pcache1
f9550 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20 70 20 .pFree ){. p
f9560 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 = (PgHdr1 *)pcac
f9570 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 he1.pFree;. p
f9580 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 cache1.pFree = p
f9590 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e cache1.pFree->pN
f95a0 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ext;. sqlite3
f95b0 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 StatusSet(SQLITE
f95c0 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
f95d0 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a E_SIZE, nByte);.
f95e0 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
f95f0 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
f9600 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 US_PAGECACHE_USE
f9610 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a D, 1);. }else{.
f9620 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 . /* Allocate
f9630 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 a new buffer us
f9640 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f ing sqlite3Mallo
f9650 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 c. Before doing
f9660 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 so, exit the.
f9670 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 ** global pcach
f9680 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f e mutex and unlo
f9690 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 ck the pager-cac
f96a0 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 he object pCache
f96b0 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a . This is . *
f96c0 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 * so that if the
f96d0 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f attempt to allo
f96e0 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 cate a new buffe
f96f0 72 20 63 61 75 73 65 73 20 74 68 65 20 74 68 65 r causes the the
f9700 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 . ** configu
f9710 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 red soft-heap-li
f9720 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 mit to be breach
f9730 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 ed, it will be p
f9740 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a ossible to. *
f9750 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 * reclaim memory
f9760 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 from this pager
f9770 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 -cache.. */.
f9780 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d pcache1LeaveM
f9790 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 utex();. p =
f97a0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 sqlite3Malloc(nB
f97b0 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 yte);. pcache
f97c0 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
f97d0 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 if( p ){.
f97e0 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 int sz = sqlit
f97f0 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b e3MallocSize(p);
f9800 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 . sqlite3St
f9810 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
f9820 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
f9830 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 OVERFLOW, sz);.
f9840 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
f9850 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 n p;.}../*.** Fr
f9860 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 ee an allocated
f9870 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 buffer obtained
f9880 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f from pcache1Allo
f9890 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 c()..*/.static v
f98a0 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 28 oid pcache1Free(
f98b0 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 void *p){. asse
f98c0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
f98d0 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d x_held(pcache1.m
f98e0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
f98f0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
f9900 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 if( p>=pcache1.p
f9910 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 Start && p<pcach
f9920 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 e1.pEnd ){. P
f9930 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 gFreeslot *pSlot
f9940 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
f9950 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
f9960 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
f9970 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 SED, -1);. pS
f9980 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f lot = (PgFreeslo
f9990 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d t*)p;. pSlot-
f99a0 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 >pNext = pcache1
f99b0 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 .pFree;. pcac
f99c0 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f he1.pFree = pSlo
f99d0 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
f99e0 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 int iSize = sqli
f99f0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 te3MallocSize(p)
f9a00 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
f9a10 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
f9a20 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f ATUS_PAGECACHE_O
f9a30 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 VERFLOW, -iSize)
f9a40 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
f9a50 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ee(p);. }.}../*
f9a60 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
f9a70 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 ew page object i
f9a80 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 nitially associa
f9a90 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70 ted with cache p
f9aa0 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 Cache..*/.static
f9ab0 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 PgHdr1 *pcache1
f9ac0 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 AllocPage(PCache
f9ad0 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 1 *pCache){. in
f9ae0 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 t nByte = sizeof
f9af0 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 (PgHdr1) + pCach
f9b00 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 50 67 48 e->szPage;. PgH
f9b10 64 72 31 20 2a 70 20 3d 20 28 50 67 48 64 72 31 dr1 *p = (PgHdr1
f9b20 20 2a 29 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 *)pcache1Alloc(
f9b30 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 20 nByte);. if( p
f9b40 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c ){. memset(p,
f9b50 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 0, nByte);.
f9b60 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 if( pCache->bPur
f9b70 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 geable ){.
f9b80 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 pcache1.nCurrent
f9b90 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 Page++;. }.
f9ba0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
f9bb0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 ./*.** Free a pa
f9bc0 67 65 20 6f 62 6a 65 63 74 20 61 6c 6c 6f 63 61 ge object alloca
f9bd0 74 65 64 20 62 79 20 70 63 61 63 68 65 31 41 6c ted by pcache1Al
f9be0 6c 6f 63 50 61 67 65 28 29 2e 0a 2a 2f 0a 73 74 locPage()..*/.st
f9bf0 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
f9c00 31 46 72 65 65 50 61 67 65 28 50 67 48 64 72 31 1FreePage(PgHdr1
f9c10 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
f9c20 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63 . if( p->pCac
f9c30 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 he->bPurgeable )
f9c40 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e {. pcache1.
f9c50 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a nCurrentPage--;.
f9c60 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
f9c70 31 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 1Free(p);. }.}.
f9c80 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 ./*.** Malloc fu
f9c90 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53 nction used by S
f9ca0 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 QLite to obtain
f9cb0 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 space from the b
f9cc0 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64 uffer configured
f9cd0 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 .** using sqlite
f9ce0 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
f9cf0 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
f9d00 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 ) option. If no
f9d10 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 such buffer.** e
f9d20 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63 xists, this func
f9d30 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 tion falls back
f9d40 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 to sqlite3Malloc
f9d50 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
f9d60 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
f9d70 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e te3PageMalloc(in
f9d80 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 t sz){. void *p
f9d90 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
f9da0 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70 Mutex();. p = p
f9db0 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b cache1Alloc(sz);
f9dc0 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
f9dd0 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e utex();. return
f9de0 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p;.}../*.** Fre
f9df0 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 e an allocated b
f9e00 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 uffer obtained f
f9e10 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d rom sqlite3PageM
f9e20 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 alloc()..*/.SQLI
f9e30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
f9e40 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
f9e50 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63 void *p){. pcac
f9e60 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
f9e70 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70 . pcache1Free(p
f9e80 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 );. pcache1Leav
f9e90 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a eMutex();.}../**
f9ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
f9ef0 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 ******* General
f9f00 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46 Implementation F
f9f10 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a unctions *******
f9f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
f9f40 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
f9f50 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 ion is used to r
f9f60 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 esize the hash t
f9f70 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 able used by the
f9f80 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a cache passed.**
f9f90 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
f9fa0 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 gument..**.** Th
f9fb0 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d e global mutex m
f9fc0 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e ust be held when
f9fd0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
f9fe0 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
f9ff0 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52 tic int pcache1R
fa000 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65 esizeHash(PCache
fa010 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20 1 *p){. PgHdr1
fa020 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67 **apNew;. unsig
fa030 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 ned int nNew;.
fa040 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a unsigned int i;.
fa050 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
fa060 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
fa070 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
fa080 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61 . nNew = p->nHa
fa090 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77 sh*2;. if( nNew
fa0a0 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77 <256 ){. nNew
fa0b0 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70 = 256;. }.. p
fa0c0 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
fa0d0 28 29 3b 0a 20 20 61 70 4e 65 77 20 3d 20 28 50 ();. apNew = (P
fa0e0 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33 gHdr1 **)sqlite3
fa0f0 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 _malloc(sizeof(P
fa100 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a gHdr1 *)*nNew);.
fa110 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
fa120 74 65 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e tex();. if( apN
fa130 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 ew ){. memset
fa140 28 61 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f (apNew, 0, sizeo
fa150 66 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 f(PgHdr1 *)*nNew
fa160 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 );. for(i=0;
fa170 69 3c 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 i<p->nHash; i++)
fa180 7b 0a 20 20 20 20 20 20 50 67 48 64 72 31 20 2a {. PgHdr1 *
fa190 70 50 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 pPage;. PgH
fa1a0 64 72 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e dr1 *pNext = p->
fa1b0 61 70 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 apHash[i];.
fa1c0 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d while( (pPage =
fa1d0 20 70 4e 65 78 74 29 20 29 7b 0a 20 20 20 20 20 pNext) ){.
fa1e0 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
fa1f0 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 h = pPage->iKey
fa200 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 % nNew;.
fa210 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 pNext = pPage->p
fa220 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 50 Next;. pP
fa230 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 4e age->pNext = apN
fa240 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 61 ew[h];. a
fa250 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 3b pNew[h] = pPage;
fa260 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
fa270 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
fa280 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 20 p->apHash);.
fa290 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e 65 p->apHash = apNe
fa2a0 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 20 w;. p->nHash
fa2b0 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 72 = nNew;. }.. r
fa2c0 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 68 eturn (p->apHash
fa2d0 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 ? SQLITE_OK : S
fa2e0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a QLITE_NOMEM);.}.
fa2f0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
fa300 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 74 tion is used int
fa310 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f 76 ernally to remov
fa320 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67 65 e the page pPage
fa330 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 6c from the .** gl
fa340 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 69 obal LRU list, i
fa350 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 2e f is part of it.
fa360 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 If pPage is not
fa370 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f part of the glo
fa380 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 2c bal.** LRU list,
fa390 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
fa3a0 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ion is a no-op..
fa3b0 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c **.** The global
fa3c0 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 mutex must be h
fa3d0 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 eld when this fu
fa3e0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
fa3f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
fa400 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 pcache1PinPage(
fa410 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a PgHdr1 *pPage){.
fa420 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
fa430 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 3_mutex_held(pca
fa440 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 che1.mutex) );.
fa450 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28 70 if( pPage && (p
fa460 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 7c Page->pLruNext |
fa470 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 31 | pPage==pcache1
fa480 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20 .pLruTail) ){.
fa490 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 if( pPage->pLr
fa4a0 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 uPrev ){. p
fa4b0 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d 3e Page->pLruPrev->
fa4c0 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 65 pLruNext = pPage
fa4d0 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 ->pLruNext;.
fa4e0 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d }. if( pPage-
fa4f0 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20 >pLruNext ){.
fa500 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 pPage->pLruNe
fa510 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 xt->pLruPrev = p
fa520 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a Page->pLruPrev;.
fa530 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 }. if( pc
fa540 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d 3d ache1.pLruHead==
fa550 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 pPage ){. p
fa560 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 cache1.pLruHead
fa570 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 = pPage->pLruNex
fa580 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 t;. }. if(
fa590 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 pcache1.pLruTai
fa5a0 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 l==pPage ){.
fa5b0 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 pcache1.pLruTa
fa5c0 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 il = pPage->pLru
fa5d0 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Prev;. }.
fa5e0 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
fa5f0 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e = 0;. pPage->
fa600 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 pLruPrev = 0;.
fa610 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d pPage->pCache-
fa620 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b 0a >nRecyclable--;.
fa630 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 }.}.../*.** Re
fa640 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 75 move the page su
fa650 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 pplied as an arg
fa660 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 68 ument from the h
fa670 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 50 ash table .** (P
fa680 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 74 Cache1.apHash st
fa690 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69 74 ructure) that it
fa6a0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 is currently st
fa6b0 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 ored in..**.** T
fa6c0 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 he global mutex
fa6d0 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 must be held whe
fa6e0 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
fa6f0 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 is called..*/.st
fa700 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
fa710 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 1RemoveFromHash(
fa720 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a PgHdr1 *pPage){.
fa730 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
fa740 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 ;. PCache1 *pCa
fa750 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 che = pPage->pCa
fa760 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a che;. PgHdr1 **
fa770 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 65 pp;.. h = pPage
fa780 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d ->iKey % pCache-
fa790 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 70 >nHash;. for(pp
fa7a0 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 =&pCache->apHash
fa7b0 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 67 [h]; (*pp)!=pPag
fa7c0 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 4e e; pp=&(*pp)->pN
fa7d0 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 2a ext);. *pp = (*
fa7e0 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 70 pp)->pNext;.. p
fa7f0 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a Cache->nPage--;.
fa800 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 }../*.** If ther
fa810 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 e are currently
fa820 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 65 more than pcache
fa830 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20 .nMaxPage pages
fa840 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a allocated, try.*
fa850 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67 * to recycle pag
fa860 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 es to reduce the
fa870 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65 number allocate
fa880 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 78 d to pcache.nMax
fa890 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Page..*/.static
fa8a0 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 6f void pcache1Enfo
fa8b0 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 29 rceMaxPage(void)
fa8c0 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
fa8d0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
fa8e0 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b cache1.mutex) );
fa8f0 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 65 . while( pcache
fa900 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70 1.nCurrentPage>p
fa910 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 cache1.nMaxPage
fa920 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 && pcache1.pLruT
fa930 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 72 ail ){. PgHdr
fa940 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e 70 1 *p = pcache1.p
fa950 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 61 LruTail;. pca
fa960 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a che1PinPage(p);.
fa970 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 pcache1Remov
fa980 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20 eFromHash(p);.
fa990 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 pcache1FreePag
fa9a0 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a e(p);. }.}../*.
fa9b0 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 70 ** Discard all p
fa9c0 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 20 ages from cache
fa9d0 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70 61 pCache with a pa
fa9e0 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 76 ge number (key v
fa9f0 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 65 alue) .** greate
faa00 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 r than or equal
faa10 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 to iLimit. Any p
faa20 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 74 inned pages that
faa30 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 63 meet this .** c
faa40 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 69 riteria are unpi
faa50 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 79 nned before they
faa60 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e 0a are discarded..
faa70 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c **.** The global
faa80 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 mutex must be h
faa90 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 eld when this fu
faaa0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
faab0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
faac0 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 pcache1Truncate
faad0 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 65 Unsafe(. PCache
faae0 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 6e 1 *pCache, . un
faaf0 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 signed int iLimi
fab00 74 20 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 t .){. unsigned
fab10 20 69 6e 74 20 68 3b 0a 20 20 61 73 73 65 72 74 int h;. assert
fab20 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
fab30 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 held(pcache1.mut
fab40 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 68 3d 30 ex) );. for(h=0
fab50 3b 20 68 3c 70 43 61 63 68 65 2d 3e 6e 48 61 73 ; h<pCache->nHas
fab60 68 3b 20 68 2b 2b 29 7b 0a 20 20 20 20 50 67 48 h; h++){. PgH
fab70 64 72 31 20 2a 2a 70 70 20 3d 20 26 70 43 61 63 dr1 **pp = &pCac
fab80 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 0a he->apHash[h]; .
fab90 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 PgHdr1 *pPag
faba0 65 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 70 e;. while( (p
fabb0 50 61 67 65 20 3d 20 2a 70 70 29 20 29 7b 0a 20 Page = *pp) ){.
fabc0 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e if( pPage->
fabd0 69 4b 65 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a iKey>=iLimit ){.
fabe0 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31 50 pcache1P
fabf0 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 inPage(pPage);.
fac00 20 20 20 20 20 20 20 2a 70 70 20 3d 20 70 50 61 *pp = pPa
fac10 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 ge->pNext;.
fac20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 pcache1FreePa
fac30 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ge(pPage);.
fac40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
fac50 70 70 20 3d 20 26 70 50 61 67 65 2d 3e 70 4e 65 pp = &pPage->pNe
fac60 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 xt;. }.
fac70 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a }. }.}../******
fac80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fac90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
faca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
facb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
facc0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
facd0 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 *** sqlite3_pcac
face0 68 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a he Methods *****
facf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fad00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fad10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
fad20 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
fad30 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
fad40 70 63 61 63 68 65 2e 78 49 6e 69 74 20 6d 65 74 pcache.xInit met
fad50 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 hod..*/.static i
fad60 6e 74 20 70 63 61 63 68 65 31 49 6e 69 74 28 76 nt pcache1Init(v
fad70 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 oid *NotUsed){.
fad80 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
fad90 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 R(NotUsed);. me
fada0 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 mset(&pcache1, 0
fadb0 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 , sizeof(pcache1
fadc0 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 ));. if( sqlite
fadd0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 3GlobalConfig.bC
fade0 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 oreMutex ){.
fadf0 70 63 61 63 68 65 31 2e 6d 75 74 65 78 20 3d 20 pcache1.mutex =
fae00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
fae10 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
fae20 5f 53 54 41 54 49 43 5f 4c 52 55 29 3b 0a 20 20 _STATIC_LRU);.
fae30 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
fae40 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 E_OK;.}../*.** I
fae50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
fae60 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
fae70 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20 6d 65 che.xShutdown me
fae80 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 thod..*/.static
fae90 76 6f 69 64 20 70 63 61 63 68 65 31 53 68 75 74 void pcache1Shut
faea0 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 down(void *NotUs
faeb0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ed){. UNUSED_PA
faec0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
faed0 3b 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a ;. /* no-op */.
faee0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
faef0 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
faf00 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 43 qlite3_pcache.xC
faf10 72 65 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2a reate method..**
faf20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
faf30 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 ew cache..*/.sta
faf40 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61 63 tic sqlite3_pcac
faf50 68 65 20 2a 70 63 61 63 68 65 31 43 72 65 61 74 he *pcache1Creat
faf60 65 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e e(int szPage, in
faf70 74 20 62 50 75 72 67 65 61 62 6c 65 29 7b 0a 20 t bPurgeable){.
faf80 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 PCache1 *pCache
faf90 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d 20 28 50 ;.. pCache = (P
fafa0 43 61 63 68 65 31 20 2a 29 73 71 6c 69 74 65 33 Cache1 *)sqlite3
fafb0 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 _malloc(sizeof(P
fafc0 43 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 Cache1));. if(
fafd0 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6d 65 pCache ){. me
fafe0 6d 73 65 74 28 70 43 61 63 68 65 2c 20 30 2c 20 mset(pCache, 0,
faff0 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31 29 29 sizeof(PCache1))
fb000 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 73 7a ;. pCache->sz
fb010 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 Page = szPage;.
fb020 20 20 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 pCache->bPurg
fb030 65 61 62 6c 65 20 3d 20 28 62 50 75 72 67 65 61 eable = (bPurgea
fb040 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 ble ? 1 : 0);.
fb050 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c 65 if( bPurgeable
fb060 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 ){. pCache
fb070 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20 20 ->nMin = 10;.
fb080 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d pcache1EnterM
fb090 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 70 63 utex();. pc
fb0a0 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2b ache1.nMinPage +
fb0b0 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a = pCache->nMin;.
fb0c0 20 20 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 pcache1Lea
fb0d0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 7d veMutex();. }
fb0e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 73 . }. return (s
fb0f0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 29 qlite3_pcache *)
fb100 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pCache;.}../*.**
fb110 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
fb120 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 of the sqlite3_p
fb130 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 cache.xCachesize
fb140 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 method. .**.**
fb150 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 61 Configure the ca
fb160 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20 66 che_size limit f
fb170 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 or a cache..*/.s
fb180 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
fb190 65 31 43 61 63 68 65 73 69 7a 65 28 73 71 6c 69 e1Cachesize(sqli
fb1a0 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 69 te3_pcache *p, i
fb1b0 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61 63 nt nMax){. PCac
fb1c0 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 he1 *pCache = (P
fb1d0 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 69 66 Cache1 *)p;. if
fb1e0 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 ( pCache->bPurge
fb1f0 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61 63 able ){. pcac
fb200 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
fb210 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6e 4d 61 . pcache1.nMa
fb220 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20 2d xPage += (nMax -
fb230 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a pCache->nMax);.
fb240 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 pCache->nMax
fb250 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63 61 = nMax;. pca
fb260 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 che1EnforceMaxPa
fb270 67 65 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 ge();. pcache
fb280 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
fb290 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c }.}../*.** Impl
fb2a0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
fb2b0 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
fb2c0 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d 65 74 68 .xPagecount meth
fb2d0 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 od. .*/.static i
fb2e0 6e 74 20 70 63 61 63 68 65 31 50 61 67 65 63 6f nt pcache1Pageco
fb2f0 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 63 61 63 unt(sqlite3_pcac
fb300 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b he *p){. int n;
fb310 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d . pcache1EnterM
fb320 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28 28 utex();. n = ((
fb330 50 43 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e 50 PCache1 *)p)->nP
fb340 61 67 65 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 age;. pcache1Le
fb350 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 aveMutex();. re
fb360 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn n;.}../*.**
fb370 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
fb380 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 of the sqlite3_p
fb390 63 61 63 68 65 2e 78 46 65 74 63 68 20 6d 65 74 cache.xFetch met
fb3a0 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74 63 hod. .**.** Fetc
fb3b0 68 20 61 20 70 61 67 65 20 62 79 20 6b 65 79 20 h a page by key
fb3c0 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 value..**.** Whe
fb3d0 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e 65 ther or not a ne
fb3e0 77 20 70 61 67 65 20 6d 61 79 20 62 65 20 61 6c w page may be al
fb3f0 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 located by this
fb400 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64 73 function depends
fb410 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 on.** the value
fb420 20 6f 66 20 74 68 65 20 63 72 65 61 74 65 46 6c of the createFl
fb430 61 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a ag argument..**.
fb440 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 ** There are thr
fb450 65 65 20 64 69 66 66 65 72 65 6e 74 20 61 70 70 ee different app
fb460 72 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 61 69 roaches to obtai
fb470 6e 69 6e 67 20 73 70 61 63 65 20 66 6f 72 20 61 ning space for a
fb480 20 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e 64 page,.** depend
fb490 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 ing on the value
fb4a0 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20 63 72 of parameter cr
fb4b0 65 61 74 65 46 6c 61 67 20 28 77 68 69 63 68 20 eateFlag (which
fb4c0 6d 61 79 20 62 65 20 30 2c 20 31 20 6f 72 20 32 may be 0, 1 or 2
fb4d0 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52 65 )..**.** 1. Re
fb4e0 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 gardless of the
fb4f0 76 61 6c 75 65 20 6f 66 20 63 72 65 61 74 65 46 value of createF
fb500 6c 61 67 2c 20 74 68 65 20 63 61 63 68 65 20 69 lag, the cache i
fb510 73 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 61 s searched for a
fb520 20 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79 20 6f .** copy o
fb530 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 f the requested
fb540 70 61 67 65 2e 20 49 66 20 6f 6e 65 20 69 73 20 page. If one is
fb550 66 6f 75 6e 64 2c 20 69 74 20 69 73 20 72 65 74 found, it is ret
fb560 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 urned..**.** 2
fb570 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 3d . If createFlag=
fb580 3d 30 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 =0 and the page
fb590 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 is not already i
fb5a0 6e 20 74 68 65 20 63 61 63 68 65 2c 20 4e 55 4c n the cache, NUL
fb5b0 4c 20 69 73 0a 2a 2a 20 20 20 20 20 20 72 65 74 L is.** ret
fb5c0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 urned..**.** 3
fb5d0 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 . If createFlag
fb5e0 69 73 20 31 2c 20 74 68 65 20 63 61 63 68 65 20 is 1, the cache
fb5f0 69 73 20 6d 61 72 6b 65 64 20 61 73 20 70 75 72 is marked as pur
fb600 67 65 61 62 6c 65 20 61 6e 64 20 74 68 65 20 70 geable and the p
fb610 61 67 65 20 69 73 20 0a 2a 2a 20 20 20 20 20 20 age is .**
fb620 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 not already in t
fb630 68 65 20 63 61 63 68 65 2c 20 61 6e 64 20 69 66 he cache, and if
fb640 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 either of the f
fb650 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 ollowing are tru
fb660 65 2c 20 0a 2a 2a 20 20 20 20 20 20 72 65 74 75 e, .** retu
fb670 72 6e 20 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 rn NULL:.**.**
fb680 20 20 20 20 20 28 61 29 20 74 68 65 20 6e 75 6d (a) the num
fb690 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e ber of pages pin
fb6a0 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 ned by the cache
fb6b0 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
fb6c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 .** PC
fb6d0 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a ache1.nMax, or.*
fb6e0 2a 20 20 20 20 20 20 20 28 62 29 20 74 68 65 20 * (b) the
fb6f0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
fb700 70 69 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61 pinned by the ca
fb710 63 68 65 20 69 73 20 67 72 65 61 74 65 72 20 74 che is greater t
fb720 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 han.**
fb730 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 the sum of nMax
fb740 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 61 62 for all purgeab
fb750 6c 65 20 63 61 63 68 65 73 2c 20 6c 65 73 73 20 le caches, less
fb760 74 68 65 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 the sum of .**
fb770 20 20 20 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f nMin fo
fb780 72 20 61 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 r all other purg
fb790 65 61 62 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a eable caches. .*
fb7a0 2a 0a 2a 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e *.** 4. If non
fb7b0 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 e of the first t
fb7c0 68 72 65 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 hree conditions
fb7d0 61 70 70 6c 79 20 61 6e 64 20 74 68 65 20 63 61 apply and the ca
fb7e0 63 68 65 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a che is marked.**
fb7f0 20 20 20 20 20 20 61 73 20 70 75 72 67 65 61 62 as purgeab
fb800 6c 65 2c 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f le, and if one o
fb810 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
fb820 69 73 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 is true:.**.**
fb830 20 20 20 20 20 28 61 29 20 54 68 65 20 6e 75 6d (a) The num
fb840 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c ber of pages all
fb850 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 63 ocated for the c
fb860 61 63 68 65 20 69 73 20 61 6c 72 65 61 64 79 20 ache is already
fb870 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 .** PC
fb880 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a ache1.nMax, or.*
fb890 2a 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20 54 *.** (b) T
fb8a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
fb8b0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 es allocated for
fb8c0 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 all purgeable c
fb8d0 61 63 68 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 aches is.**
fb8e0 20 20 20 20 20 20 61 6c 72 65 61 64 79 20 65 71 already eq
fb8f0 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 65 ual to or greate
fb900 72 20 74 68 61 6e 20 74 68 65 20 73 75 6d 20 6f r than the sum o
fb910 66 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a f nMax for all.*
fb920 2a 20 20 20 20 20 20 20 20 20 20 20 70 75 72 67 * purg
fb930 65 61 62 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a eable caches,.**
fb940 0a 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 61 74 .** then at
fb950 74 65 6d 70 74 20 74 6f 20 72 65 63 79 63 6c 65 tempt to recycle
fb960 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 a page from the
fb970 20 4c 52 55 20 6c 69 73 74 2e 20 49 66 20 69 74 LRU list. If it
fb980 20 69 73 20 74 68 65 20 72 69 67 68 74 0a 2a 2a is the right.**
fb990 20 20 20 20 20 20 73 69 7a 65 2c 20 72 65 74 75 size, retu
fb9a0 72 6e 20 74 68 65 20 72 65 63 79 63 6c 65 64 20 rn the recycled
fb9b0 62 75 66 66 65 72 2e 20 4f 74 68 65 72 77 69 73 buffer. Otherwis
fb9c0 65 2c 20 66 72 65 65 20 74 68 65 20 62 75 66 66 e, free the buff
fb9d0 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 er and.** p
fb9e0 72 6f 63 65 65 64 20 74 6f 20 73 74 65 70 20 35 roceed to step 5
fb9f0 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 . .**.** 5. Ot
fba00 68 65 72 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 herwise, allocat
fba10 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 6e e and return a n
fba20 65 77 20 70 61 67 65 20 62 75 66 66 65 72 2e 0a ew page buffer..
fba30 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
fba40 70 63 61 63 68 65 31 46 65 74 63 68 28 73 71 6c pcache1Fetch(sql
fba50 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 ite3_pcache *p,
fba60 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 unsigned int iKe
fba70 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 y, int createFla
fba80 67 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 g){. unsigned i
fba90 6e 74 20 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 nt nPinned;. PC
fbaa0 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 ache1 *pCache =
fbab0 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 (PCache1 *)p;.
fbac0 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 PgHdr1 *pPage =
fbad0 30 3b 0a 0a 20 20 70 63 61 63 68 65 31 45 6e 74 0;.. pcache1Ent
fbae0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 erMutex();. if(
fbaf0 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 createFlag==1 )
fbb00 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
fbb10 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 ignMalloc();..
fbb20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 68 61 /* Search the ha
fbb30 73 68 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 sh table for an
fbb40 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 existing entry.
fbb50 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d */. if( pCache-
fbb60 3e 6e 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 >nHash>0 ){.
fbb70 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d unsigned int h =
fbb80 20 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e iKey % pCache->
fbb90 6e 48 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 nHash;. for(p
fbba0 50 61 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 Page=pCache->apH
fbbb0 61 73 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 ash[h]; pPage&&p
fbbc0 50 61 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 Page->iKey!=iKey
fbbd0 3b 20 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 ; pPage=pPage->p
fbbe0 4e 65 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 Next);. }.. if
fbbf0 28 20 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 ( pPage || creat
fbc00 65 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 eFlag==0 ){.
fbc10 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 pcache1PinPage(p
fbc20 50 61 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Page);. goto
fbc30 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a fetch_out;. }..
fbc40 20 20 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 /* Step 3 of h
fbc50 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a eader comment. *
fbc60 2f 0a 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 /. nPinned = pC
fbc70 61 63 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 ache->nPage - pC
fbc80 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c ache->nRecyclabl
fbc90 65 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 e;. if( createF
fbca0 6c 61 67 3d 3d 31 20 26 26 20 70 43 61 63 68 65 lag==1 && pCache
fbcb0 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 26 20 ->bPurgeable &&
fbcc0 28 0a 20 20 20 20 20 20 20 20 6e 50 69 6e 6e 65 (. nPinne
fbcd0 64 3e 3d 28 70 63 61 63 68 65 31 2e 6e 4d 61 78 d>=(pcache1.nMax
fbce0 50 61 67 65 2b 70 43 61 63 68 65 2d 3e 6e 4d 69 Page+pCache->nMi
fbcf0 6e 2d 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 n-pcache1.nMinPa
fbd00 67 65 29 0a 20 20 20 20 20 7c 7c 20 6e 50 69 6e ge). || nPin
fbd10 6e 65 64 3e 3d 28 70 43 61 63 68 65 2d 3e 6e 4d ned>=(pCache->nM
fbd20 61 78 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f ax). )){. go
fbd30 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 to fetch_out;.
fbd40 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d }.. if( pCache-
fbd50 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e >nPage>=pCache->
fbd60 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65 31 nHash && pcache1
fbd70 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 68 ResizeHash(pCach
fbd80 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 e) ){. goto f
fbd90 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 etch_out;. }..
fbda0 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 20 /* Step 4. Try
fbdb0 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67 to recycle a pag
fbdc0 65 20 62 75 66 66 65 72 20 69 66 20 61 70 70 72 e buffer if appr
fbdd0 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69 66 opriate. */. if
fbde0 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 ( pCache->bPurge
fbdf0 61 62 6c 65 20 26 26 20 70 63 61 63 68 65 31 2e able && pcache1.
fbe00 70 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20 20 pLruTail && (.
fbe10 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 pCache->nPag
fbe20 65 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78 2d e>=pCache->nMax-
fbe30 31 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75 1 || pcache1.nCu
fbe40 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61 63 68 rrentPage>=pcach
fbe50 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20 29 29 e1.nMaxPage. ))
fbe60 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 63 {. pPage = pc
fbe70 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a ache1.pLruTail;.
fbe80 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 pcache1Remov
fbe90 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29 eFromHash(pPage)
fbea0 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69 6e ;. pcache1Pin
fbeb0 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
fbec0 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43 61 63 if( pPage->pCac
fbed0 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43 61 63 he->szPage!=pCac
fbee0 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a 20 20 he->szPage ){.
fbef0 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 pcache1FreeP
fbf00 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
fbf10 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 pPage = 0;.
fbf20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 }else{. pc
fbf30 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 ache1.nCurrentPa
fbf40 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e 70 43 ge -= (pPage->pC
fbf50 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
fbf60 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 - pCache->bPurg
fbf70 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 eable);. }.
fbf80 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20 }.. /* Step 5.
fbf90 49 66 20 61 20 75 73 61 62 6c 65 20 70 61 67 65 If a usable page
fbfa0 20 62 75 66 66 65 72 20 68 61 73 20 73 74 69 6c buffer has stil
fbfb0 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64 l not been found
fbfc0 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20 , . ** attempt
fbfd0 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 to allocate a ne
fbfe0 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69 w one. . */. i
fbff0 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20 f( !pPage ){.
fc000 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31 pPage = pcache1
fc010 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 68 65 AllocPage(pCache
fc020 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 );. }.. if( pP
fc030 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 age ){. unsig
fc040 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 ned int h = iKey
fc050 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 % pCache->nHash
fc060 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 ;. memset(pPa
fc070 67 65 2c 20 30 2c 20 70 43 61 63 68 65 2d 3e 73 ge, 0, pCache->s
fc080 7a 50 61 67 65 20 2b 20 73 69 7a 65 6f 66 28 50 zPage + sizeof(P
fc090 67 48 64 72 31 29 29 3b 0a 20 20 20 20 70 43 61 gHdr1));. pCa
fc0a0 63 68 65 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 che->nPage++;.
fc0b0 20 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 pPage->iKey =
fc0c0 69 4b 65 79 3b 0a 20 20 20 20 70 50 61 67 65 2d iKey;. pPage-
fc0d0 3e 70 4e 65 78 74 20 3d 20 70 43 61 63 68 65 2d >pNext = pCache-
fc0e0 3e 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 20 20 >apHash[h];.
fc0f0 70 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 pPage->pCache =
fc100 70 43 61 63 68 65 3b 0a 20 20 20 20 70 43 61 63 pCache;. pCac
fc110 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 he->apHash[h] =
fc120 70 50 61 67 65 3b 0a 20 20 7d 0a 0a 66 65 74 63 pPage;. }..fetc
fc130 68 5f 6f 75 74 3a 0a 20 20 69 66 28 20 63 72 65 h_out:. if( cre
fc140 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c ateFlag==1 ) sql
fc150 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
fc160 6c 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 31 loc();. pcache1
fc170 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
fc180 72 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f 20 return (pPage ?
fc190 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 PGHDR1_TO_PAGE(p
fc1a0 50 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a Page) : 0);.}...
fc1b0 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
fc1c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
fc1d0 74 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 te3_pcache.xUnpi
fc1e0 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 n method..**.**
fc1f0 4d 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 75 Mark a page as u
fc200 6e 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c npinned (eligibl
fc210 65 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f e for asynchrono
fc220 75 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a us recycling)..*
fc230 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
fc240 61 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 ache1Unpin(sqlit
fc250 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 6f e3_pcache *p, vo
fc260 69 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 id *pPg, int reu
fc270 73 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 seUnlikely){. P
fc280 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d Cache1 *pCache =
fc290 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 (PCache1 *)p;.
fc2a0 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d PgHdr1 *pPage =
fc2b0 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 PAGE_TO_PGHDR1(
fc2c0 70 50 67 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 pPg);.. pcache1
fc2d0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 EnterMutex();..
fc2e0 20 2f 2a 20 49 74 20 69 73 20 61 6e 20 65 72 72 /* It is an err
fc2f0 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 or to call this
fc300 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20 function if the
fc310 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 page is already
fc320 0a 20 20 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 . ** part of th
fc330 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 e global LRU lis
fc340 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 t.. */. assert
fc350 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 ( pPage->pLruPre
fc360 76 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 v==0 && pPage->p
fc370 4c 72 75 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 LruNext==0 );.
fc380 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e assert( pcache1.
fc390 70 4c 72 75 48 65 61 64 21 3d 70 50 61 67 65 20 pLruHead!=pPage
fc3a0 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 && pcache1.pLruT
fc3b0 61 69 6c 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 ail!=pPage );..
fc3c0 20 69 66 28 20 72 65 75 73 65 55 6e 6c 69 6b 65 if( reuseUnlike
fc3d0 6c 79 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 ly || pcache1.nC
fc3e0 75 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63 68 urrentPage>pcach
fc3f0 65 31 2e 6e 4d 61 78 50 61 67 65 20 29 7b 0a 20 e1.nMaxPage ){.
fc400 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 pcache1Remove
fc410 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29 3b FromHash(pPage);
fc420 0a 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 . pcache1Free
fc430 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d Page(pPage);. }
fc440 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41 64 64 else{. /* Add
fc450 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 the page to the
fc460 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 global LRU list
fc470 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 65 20 . Normally, the
fc480 70 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f page is added to
fc490 0a 20 20 20 20 2a 2a 20 74 68 65 20 68 65 61 64 . ** the head
fc4a0 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28 6c 61 of the list (la
fc4b0 73 74 20 70 61 67 65 20 74 6f 20 62 65 20 72 65 st page to be re
fc4c0 63 79 63 6c 65 64 29 2e 20 48 6f 77 65 76 65 72 cycled). However
fc4d0 2c 20 69 66 20 74 68 65 20 0a 20 20 20 20 2a 2a , if the . **
fc4e0 20 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 66 reuseUnlikely f
fc4f0 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 74 68 lag passed to th
fc500 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 is function is t
fc510 72 75 65 2c 20 74 68 65 20 70 61 67 65 20 69 73 rue, the page is
fc520 20 61 64 64 65 64 0a 20 20 20 20 2a 2a 20 74 6f added. ** to
fc530 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65 the tail of the
fc540 20 6c 69 73 74 20 28 66 69 72 73 74 20 70 61 67 list (first pag
fc550 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 64 e to be recycled
fc560 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 ).. */. if
fc570 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 ( pcache1.pLruHe
fc580 61 64 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 ad ){. pcac
fc590 68 65 31 2e 70 4c 72 75 48 65 61 64 2d 3e 70 4c he1.pLruHead->pL
fc5a0 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 3b 0a ruPrev = pPage;.
fc5b0 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 pPage->pLr
fc5c0 75 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 2e uNext = pcache1.
fc5d0 70 4c 72 75 48 65 61 64 3b 0a 20 20 20 20 20 20 pLruHead;.
fc5e0 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
fc5f0 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 65 = pPage;. }e
fc600 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 61 63 68 lse{. pcach
fc610 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 e1.pLruTail = pP
fc620 61 67 65 3b 0a 20 20 20 20 20 20 70 63 61 63 68 age;. pcach
fc630 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 e1.pLruHead = pP
fc640 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 age;. }. p
fc650 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 Cache->nRecyclab
fc660 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 le++;. }.. pca
fc670 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
fc680 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
fc690 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
fc6a0 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e sqlite3_pcache.
fc6b0 78 52 65 6b 65 79 20 6d 65 74 68 6f 64 2e 20 0a xRekey method. .
fc6c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
fc6d0 63 61 63 68 65 31 52 65 6b 65 79 28 0a 20 20 73 cache1Rekey(. s
fc6e0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 qlite3_pcache *p
fc6f0 2c 0a 20 20 76 6f 69 64 20 2a 70 50 67 2c 0a 20 ,. void *pPg,.
fc700 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4f unsigned int iO
fc710 6c 64 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 ld,. unsigned i
fc720 6e 74 20 69 4e 65 77 0a 29 7b 0a 20 20 50 43 61 nt iNew.){. PCa
fc730 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 che1 *pCache = (
fc740 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 PCache1 *)p;. P
fc750 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 50 gHdr1 *pPage = P
fc760 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 50 AGE_TO_PGHDR1(pP
fc770 67 29 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a 70 g);. PgHdr1 **p
fc780 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e p;. unsigned in
fc790 74 20 68 3b 20 0a 20 20 61 73 73 65 72 74 28 20 t h; . assert(
fc7a0 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f 6c pPage->iKey==iOl
fc7b0 64 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 45 d );.. pcache1E
fc7c0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 nterMutex();..
fc7d0 68 20 3d 20 69 4f 6c 64 25 70 43 61 63 68 65 2d h = iOld%pCache-
fc7e0 3e 6e 48 61 73 68 3b 0a 20 20 70 70 20 3d 20 26 >nHash;. pp = &
fc7f0 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 pCache->apHash[h
fc800 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 2a 70 70 ];. while( (*pp
fc810 29 21 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 )!=pPage ){.
fc820 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 pp = &(*pp)->pNe
fc830 78 74 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 xt;. }. *pp =
fc840 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a 20 pPage->pNext;..
fc850 20 68 20 3d 20 69 4e 65 77 25 70 43 61 63 68 65 h = iNew%pCache
fc860 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 50 61 67 65 ->nHash;. pPage
fc870 2d 3e 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 ->iKey = iNew;.
fc880 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 pPage->pNext =
fc890 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 pCache->apHash[h
fc8a0 5d 3b 0a 20 20 70 43 61 63 68 65 2d 3e 61 70 48 ];. pCache->apH
fc8b0 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a ash[h] = pPage;.
fc8c0 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
fc8d0 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a utex();.}../*.**
fc8e0 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
fc8f0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 of the sqlite3_p
fc900 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 20 cache.xTruncate
fc910 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 method. .**.** D
fc920 69 73 63 61 72 64 20 61 6c 6c 20 75 6e 70 69 6e iscard all unpin
fc930 6e 65 64 20 70 61 67 65 73 20 69 6e 20 74 68 65 ned pages in the
fc940 20 63 61 63 68 65 20 77 69 74 68 20 61 20 70 61 cache with a pa
fc950 67 65 20 6e 75 6d 62 65 72 20 65 71 75 61 6c 20 ge number equal
fc960 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 to.** or greater
fc970 20 74 68 61 6e 20 70 61 72 61 6d 65 74 65 72 20 than parameter
fc980 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e iLimit. Any pinn
fc990 65 64 20 70 61 67 65 73 20 77 69 74 68 20 61 20 ed pages with a
fc9a0 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 65 page number.** e
fc9b0 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 qual to or great
fc9c0 65 72 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 61 er than iLimit a
fc9d0 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e re implicitly un
fc9e0 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 pinned..*/.stati
fc9f0 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 54 72 c void pcache1Tr
fca00 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 70 uncate(sqlite3_p
fca10 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e cache *p, unsign
fca20 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 29 7b 0a ed int iLimit){.
fca30 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
fca40 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 e = (PCache1 *)p
fca50 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
fca60 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68 Mutex();. pcach
fca70 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65 e1TruncateUnsafe
fca80 28 70 43 61 63 68 65 2c 20 69 4c 69 6d 69 74 29 (pCache, iLimit)
fca90 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 ;. pcache1Leave
fcaa0 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a Mutex();.}../*.*
fcab0 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
fcac0 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
fcad0 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20 pcache.xDestroy
fcae0 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 method. .**.** D
fcaf0 65 73 74 72 6f 79 20 61 20 63 61 63 68 65 20 61 estroy a cache a
fcb00 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70 llocated using p
fcb10 63 61 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a cache1Create()..
fcb20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
fcb30 63 61 63 68 65 31 44 65 73 74 72 6f 79 28 73 71 cache1Destroy(sq
fcb40 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29 lite3_pcache *p)
fcb50 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 {. PCache1 *pCa
fcb60 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a che = (PCache1 *
fcb70 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 )p;. pcache1Ent
fcb80 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 erMutex();. pca
fcb90 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 che1TruncateUnsa
fcba0 66 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 fe(pCache, 0);.
fcbb0 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 pcache1.nMaxPag
fcbc0 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 e -= pCache->nMa
fcbd0 78 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 x;. pcache1.nMi
fcbe0 6e 50 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d nPage -= pCache-
fcbf0 3e 6e 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31 >nMin;. pcache1
fcc00 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 EnforceMaxPage()
fcc10 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 ;. pcache1Leave
fcc20 4d 75 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74 Mutex();. sqlit
fcc30 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e e3_free(pCache->
fcc40 61 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 apHash);. sqlit
fcc50 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b e3_free(pCache);
fcc60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
fcc70 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
fcc80 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c d during initial
fcc90 69 7a 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 ization (sqlite3
fcca0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74 _initialize()) t
fccb0 6f 0a 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65 o.** install the
fccc0 20 64 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62 default pluggab
fccd0 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c le cache module,
fcce0 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73 assuming the us
fccf0 65 72 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c er has not.** al
fcd00 72 65 61 64 79 20 70 72 6f 76 69 64 65 64 20 61 ready provided a
fcd10 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a n alternative..*
fcd20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
fcd30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 void sqlite3PCa
fcd40 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f cheSetDefault(vo
fcd50 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 id){. static sq
fcd60 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
fcd70 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 hods defaultMeth
fcd80 6f 64 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 ods = {. 0,
fcd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fcda0 20 20 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a /* pArg */.
fcdb0 20 20 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c pcache1Init,
fcdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fcdd0 78 49 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61 xInit */. pca
fcde0 63 68 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20 che1Shutdown,
fcdf0 20 20 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f /* xShutdo
fce00 77 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 wn */. pcache
fce10 31 43 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 1Create,
fce20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f /* xCreate */
fce30 0a 20 20 20 20 70 63 61 63 68 65 31 43 61 63 68 . pcache1Cach
fce40 65 73 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a esize, /*
fce50 20 78 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20 xCachesize */.
fce60 20 20 20 70 63 61 63 68 65 31 50 61 67 65 63 6f pcache1Pageco
fce70 75 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 unt, /* x
fce80 50 61 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 Pagecount */.
fce90 20 70 63 61 63 68 65 31 46 65 74 63 68 2c 20 20 pcache1Fetch,
fcea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65 /* xFe
fceb0 74 63 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 tch */. pcach
fcec0 65 31 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20 e1Unpin,
fced0 20 20 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f /* xUnpin */
fcee0 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6b 65 . pcache1Reke
fcef0 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a y, /*
fcf00 20 78 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70 xRekey */. p
fcf10 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20 cache1Truncate,
fcf20 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e /* xTrun
fcf30 63 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 cate */. pcac
fcf40 68 65 31 44 65 73 74 72 6f 79 20 20 20 20 20 20 he1Destroy
fcf50 20 20 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 /* xDestroy
fcf60 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 */. };. sqlit
fcf70 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 e3_config(SQLITE
fcf80 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20 _CONFIG_PCACHE,
fcf90 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 &defaultMethods)
fcfa0 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
fcfb0 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 TE_ENABLE_MEMORY
fcfc0 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a _MANAGEMENT./*.*
fcfd0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
fcfe0 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 is called to fre
fcff0 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 e superfluous dy
fd000 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 namically alloca
fd010 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 ted memory.** he
fd020 6c 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20 ld by the pager
fd030 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 system. Memory i
fd040 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c n use by any SQL
fd050 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 ite pager alloca
fd060 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 ted.** by the cu
fd070 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 rrent thread may
fd080 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 be sqlite3_free
fd090 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 ()ed..**.** nReq
fd0a0 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
fd0b0 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 f bytes of memor
fd0c0 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 y required. Once
fd0d0 20 74 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a this much has.*
fd0e0 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c * been released,
fd0f0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 the function re
fd100 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 turns. The retur
fd110 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 n value is the t
fd120 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 otal number .**
fd130 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f of bytes of memo
fd140 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a ry released..*/.
fd150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
fd160 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
fd170 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e ReleaseMemory(in
fd180 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e t nReq){. int n
fd190 46 72 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 Free = 0;. if(
fd1a0 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d pcache1.pStart==
fd1b0 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 0 ){. PgHdr1
fd1c0 2a 70 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 *p;. pcache1E
fd1d0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
fd1e0 20 77 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 while( (nReq<0
fd1f0 7c 7c 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 || nFree<nReq) &
fd200 26 20 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 & (p=pcache1.pLr
fd210 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20 uTail) ){.
fd220 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 nFree += sqlite3
fd230 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 MallocSize(p);.
fd240 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 pcache1PinP
fd250 61 67 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 age(p);. pc
fd260 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 ache1RemoveFromH
fd270 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63 ash(p);. pc
fd280 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29 ache1FreePage(p)
fd290 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 ;. }. pcac
fd2a0 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b he1LeaveMutex();
fd2b0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 . }. return nF
fd2c0 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a ree;.}.#endif /*
fd2d0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
fd2e0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
fd2f0 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 */..#ifdef SQLI
fd300 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 TE_TEST./*.** Th
fd310 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
fd320 73 65 64 20 62 79 20 74 65 73 74 20 70 72 6f 63 sed by test proc
fd330 65 64 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63 edures to inspec
fd340 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 t the internal s
fd350 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67 tate.** of the g
fd360 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a lobal cache..*/.
fd370 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
fd380 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
fd390 65 53 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70 eStats(. int *p
fd3a0 6e 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f nCurrent, /
fd3b0 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d * OUT: Total num
fd3c0 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 ber of pages cac
fd3d0 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e hed */. int *pn
fd3e0 4d 61 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Max, /*
fd3f0 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78 OUT: Global max
fd400 69 6d 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20 imum cache size
fd410 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c */. int *pnMin,
fd420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
fd430 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31 : Sum of PCache1
fd440 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61 .nMin for purgea
fd450 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 ble caches */.
fd460 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c int *pnRecyclabl
fd470 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 e /* OUT: Tot
fd480 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
fd490 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 es available for
fd4a0 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b recycling */.){
fd4b0 0a 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 . PgHdr1 *p;.
fd4c0 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20 int nRecyclable
fd4d0 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61 = 0;. for(p=pca
fd4e0 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70 che1.pLruHead; p
fd4f0 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 ; p=p->pLruNext)
fd500 7b 0a 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c {. nRecyclabl
fd510 65 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75 e++;. }. *pnCu
fd520 72 72 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e rrent = pcache1.
fd530 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20 nCurrentPage;.
fd540 2a 70 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31 *pnMax = pcache1
fd550 2e 6e 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e .nMaxPage;. *pn
fd560 4d 69 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d Min = pcache1.nM
fd570 69 6e 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 inPage;. *pnRec
fd580 79 63 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 yclable = nRecyc
fd590 6c 61 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a lable;.}.#endif.
fd5a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
fd5b0 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e End of pcache1.
fd5c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
fd5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
fd5f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
fd600 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67 65 Begin file page
fd610 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a r.c ************
fd620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
fd640 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
fd650 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
fd660 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
fd670 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
fd680 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
fd690 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
fd6a0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
fd6b0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
fd6c0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
fd6d0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
fd6e0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
fd6f0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
fd700 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
fd710 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
fd720 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
fd730 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
fd740 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
fd750 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
fd760 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
fd770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd7b0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
fd7c0 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 is the implement
fd7d0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 ation of the pag
fd7e0 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 e cache subsyste
fd7f0 6d 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a m or "pager"..**
fd800 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69 .** The pager i
fd810 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 s used to access
fd820 20 61 20 64 61 74 61 62 61 73 65 20 64 69 73 6b a database disk
fd830 20 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 file. It imple
fd840 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 ments.** atomic
fd850 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 commit and rollb
fd860 61 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 ack through the
fd870 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c use of a journal
fd880 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 file that.** is
fd890 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 separate from t
fd8a0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
fd8b0 2e 20 20 54 68 65 20 70 61 67 65 72 20 61 6c 73 . The pager als
fd8c0 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c o implements fil
fd8d0 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 e.** locking to
fd8e0 70 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 prevent two proc
fd8f0 65 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74 69 esses from writi
fd900 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 ng the same data
fd910 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d base.** file sim
fd920 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 ultaneously, or
fd930 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f 6d one process from
fd940 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 reading the dat
fd950 61 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 abase while.** a
fd960 6e 6f 74 68 65 72 20 69 73 20 77 72 69 74 69 6e nother is writin
fd970 67 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 g..**.** @(#) $I
fd980 64 3a 20 70 61 67 65 72 2e 63 2c 76 20 31 2e 35 d: pager.c,v 1.5
fd990 30 36 20 32 30 30 38 2f 31 31 2f 31 39 20 31 38 06 2008/11/19 18
fd9a0 3a 33 30 3a 32 39 20 64 72 68 20 45 78 70 20 24 :30:29 drh Exp $
fd9b0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
fd9c0 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a TE_OMIT_DISKIO..
fd9d0 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 /*.** Macros for
fd9e0 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 troubleshooting
fd9f0 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e . Normally turn
fda00 65 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a ed off.*/.#if 0.
fda10 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 #define sqlite3D
fda20 65 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 ebugPrintf print
fda30 66 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 f.#define PAGERT
fda40 52 41 43 45 31 28 58 29 20 20 20 20 20 20 20 73 RACE1(X) s
fda50 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
fda60 66 28 58 29 0a 23 64 65 66 69 6e 65 20 50 41 47 f(X).#define PAG
fda70 45 52 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 ERTRACE2(X,Y)
fda80 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 sqlite3DebugPr
fda90 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e intf(X,Y).#defin
fdaa0 65 20 50 41 47 45 52 54 52 41 43 45 33 28 58 2c e PAGERTRACE3(X,
fdab0 59 2c 5a 29 20 20 20 73 71 6c 69 74 65 33 44 65 Y,Z) sqlite3De
fdac0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 bugPrintf(X,Y,Z)
fdad0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 .#define PAGERTR
fdae0 41 43 45 34 28 58 2c 59 2c 5a 2c 57 29 20 73 71 ACE4(X,Y,Z,W) sq
fdaf0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
fdb00 28 58 2c 59 2c 5a 2c 57 29 0a 23 64 65 66 69 6e (X,Y,Z,W).#defin
fdb10 65 20 50 41 47 45 52 54 52 41 43 45 35 28 58 2c e PAGERTRACE5(X,
fdb20 59 2c 5a 2c 57 2c 56 29 20 73 71 6c 69 74 65 33 Y,Z,W,V) sqlite3
fdb30 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
fdb40 5a 2c 57 2c 56 29 0a 23 65 6c 73 65 0a 23 64 65 Z,W,V).#else.#de
fdb50 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 31 fine PAGERTRACE1
fdb60 28 58 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 (X).#define PAGE
fdb70 52 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 RTRACE2(X,Y).#de
fdb80 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 33 fine PAGERTRACE3
fdb90 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 (X,Y,Z).#define
fdba0 50 41 47 45 52 54 52 41 43 45 34 28 58 2c 59 2c PAGERTRACE4(X,Y,
fdbb0 5a 2c 57 29 0a 23 64 65 66 69 6e 65 20 50 41 47 Z,W).#define PAG
fdbc0 45 52 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 57 ERTRACE5(X,Y,Z,W
fdbd0 2c 56 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ,V).#endif../*.*
fdbe0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
fdbf0 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 65 20 75 two macros are u
fdc00 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 50 sed within the P
fdc10 41 47 45 52 54 52 41 43 45 58 28 29 20 6d 61 63 AGERTRACEX() mac
fdc20 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 ros above.** to
fdc30 70 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 2d 64 print out file-d
fdc40 65 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a 2a 0a escriptors. .**.
fdc50 2a 2a 20 50 41 47 45 52 49 44 28 29 20 74 61 6b ** PAGERID() tak
fdc60 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 es a pointer to
fdc70 61 20 50 61 67 65 72 20 73 74 72 75 63 74 20 61 a Pager struct a
fdc80 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 20 s its argument.
fdc90 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 The.** associate
fdca0 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f d file-descripto
fdcb0 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 46 r is returned. F
fdcc0 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 74 61 ILEHANDLEID() ta
fdcd0 6b 65 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 66 kes an sqlite3_f
fdce0 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20 61 73 ile.** struct as
fdcf0 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 0a 2a its argument..*
fdd00 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 49 /.#define PAGERI
fdd10 44 28 70 29 20 28 28 69 6e 74 29 28 70 2d 3e 66 D(p) ((int)(p->f
fdd20 64 29 29 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 d)).#define FILE
fdd30 48 41 4e 44 4c 45 49 44 28 66 64 29 20 28 28 69 HANDLEID(fd) ((i
fdd40 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 nt)fd)../*.** Th
fdd50 65 20 70 61 67 65 20 63 61 63 68 65 20 61 73 20 e page cache as
fdd60 61 20 77 68 6f 6c 65 20 69 73 20 61 6c 77 61 79 a whole is alway
fdd70 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 s in one of the
fdd80 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 61 following.** sta
fdd90 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 tes:.**.** PAG
fdda0 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20 ER_UNLOCK
fddb0 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
fddc0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
fddd0 20 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a 2a 20 reading or .**
fdde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fddf0 20 20 20 20 20 20 77 72 69 74 69 6e 67 20 74 68 writing th
fde00 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
fde10 20 20 54 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a There is no.**
fde20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fde30 20 20 20 20 20 20 20 64 61 74 61 20 68 65 6c 64 data held
fde40 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 in memory. Thi
fde50 73 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 6c s is the initial
fde60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
fde70 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 2e state.
fde80 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 .**.** PAGER_S
fde90 48 41 52 45 44 20 20 20 20 20 20 20 20 54 68 65 HARED The
fdea0 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 72 page cache is r
fdeb0 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 eading the datab
fdec0 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 ase..**
fded0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57 72 Wr
fdee0 69 74 69 6e 67 20 69 73 20 6e 6f 74 20 70 65 72 iting is not per
fdef0 6d 69 74 74 65 64 2e 20 20 54 68 65 72 65 20 63 mitted. There c
fdf00 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 an be.**
fdf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
fdf20 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 20 ultiple readers
fdf30 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 accessing the sa
fdf40 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 me database.**
fdf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fdf60 20 20 20 20 20 66 69 6c 65 20 61 74 20 74 68 65 file at the
fdf70 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a same time..**.*
fdf80 2a 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 56 * PAGER_RESERV
fdf90 45 44 20 20 20 20 20 20 54 68 69 73 20 70 72 6f ED This pro
fdfa0 63 65 73 73 20 68 61 73 20 72 65 73 65 72 76 65 cess has reserve
fdfb0 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 d the database f
fdfc0 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 20 20 or writing.**
fdfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fdfe0 20 20 20 20 62 75 74 20 68 61 73 20 6e 6f 74 20 but has not
fdff0 79 65 74 20 6d 61 64 65 20 61 6e 79 20 63 68 61 yet made any cha
fe000 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e 65 20 nges. Only one
fe010 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20 20 20 process.**
fe020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe030 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 72 at a time can r
fe040 65 73 65 72 76 65 20 74 68 65 20 64 61 74 61 62 eserve the datab
fe050 61 73 65 2e 20 20 54 68 65 20 6f 72 69 67 69 6e ase. The origin
fe060 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 al.**
fe070 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 data
fe080 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e 6f base file has no
fe090 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 t been modified
fe0a0 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 so other.**
fe0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe0c0 20 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 processes may
fe0d0 73 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 still be reading
fe0e0 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a 2a 20 the on-disk.**
fe0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe100 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 database f
fe110 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 ile..**.** PAG
fe120 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 ER_EXCLUSIVE
fe130 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 The page cache
fe140 69 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 is writing the d
fe150 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 atabase..**
fe160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe170 20 20 41 63 63 65 73 73 20 69 73 20 65 78 63 6c Access is excl
fe180 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68 65 72 usive. No other
fe190 20 70 72 6f 63 65 73 73 65 73 20 6f 72 0a 2a 2a processes or.**
fe1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe1b0 20 20 20 20 20 20 20 74 68 72 65 61 64 73 20 63 threads c
fe1c0 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 6f 72 an be reading or
fe1d0 20 77 72 69 74 69 6e 67 20 77 68 69 6c 65 20 6f writing while o
fe1e0 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ne.**
fe1f0 20 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 proc
fe200 65 73 73 20 69 73 20 77 72 69 74 69 6e 67 2e 0a ess is writing..
fe210 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 59 **.** PAGER_SY
fe220 4e 43 45 44 20 20 20 20 20 20 20 20 54 68 65 20 NCED The
fe230 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 74 pager moves to t
fe240 68 69 73 20 73 74 61 74 65 20 66 72 6f 6d 20 50 his state from P
fe250 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 0a 2a AGER_EXCLUSIVE.*
fe260 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
fe270 20 20 20 20 20 20 20 20 61 66 74 65 72 20 61 6c after al
fe280 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 68 61 l dirty pages ha
fe290 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 ve been written
fe2a0 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 to the.**
fe2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe2c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
fe2d0 64 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 62 d the file has b
fe2e0 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a 2a 2a een synced to.**
fe2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe300 20 20 20 20 20 20 20 64 69 73 6b 2e 20 41 6c 6c disk. All
fe310 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 6f that remains to
fe320 20 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f 76 65 do is to remove
fe330 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 or.**
fe340 20 20 20 20 20 20 20 20 20 20 20 20 20 74 72 75 tru
fe350 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 ncate the journa
fe360 6c 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 74 l file and the t
fe370 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 20 ransaction .**
fe380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe390 20 20 20 20 20 77 69 6c 6c 20 62 65 20 63 6f 6d will be com
fe3a0 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 mitted..**.** Th
fe3b0 65 20 70 61 67 65 20 63 61 63 68 65 20 63 6f 6d e page cache com
fe3c0 65 73 20 75 70 20 69 6e 20 50 41 47 45 52 5f 55 es up in PAGER_U
fe3d0 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 NLOCK. The firs
fe3e0 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71 6c 69 t time a.** sqli
fe3f0 74 65 33 50 61 67 65 72 47 65 74 28 29 20 6f 63 te3PagerGet() oc
fe400 63 75 72 73 2c 20 74 68 65 20 73 74 61 74 65 20 curs, the state
fe410 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 transitions to P
fe420 41 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a 2a 20 AGER_SHARED..**
fe430 41 66 74 65 72 20 61 6c 6c 20 70 61 67 65 73 20 After all pages
fe440 68 61 76 65 20 62 65 65 6e 20 72 65 6c 65 61 73 have been releas
fe450 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 5f ed using sqlite_
fe460 70 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a 2a 2a page_unref(),.**
fe470 20 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 the state trans
fe480 69 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f 20 50 itions back to P
fe490 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 AGER_UNLOCK. Th
fe4a0 65 20 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 e first time.**
fe4b0 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 that sqlite3Page
fe4c0 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c 6c rWrite() is call
fe4d0 65 64 2c 20 74 68 65 20 73 74 61 74 65 20 74 72 ed, the state tr
fe4e0 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 ansitions to.**
fe4f0 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20 PAGER_RESERVED.
fe500 20 28 4e 6f 74 65 20 74 68 61 74 20 73 71 6c 69 (Note that sqli
fe510 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
fe520 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 63 can only be.** c
fe530 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75 74 73 alled on an outs
fe540 74 61 6e 64 69 6e 67 20 70 61 67 65 20 77 68 69 tanding page whi
fe550 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 ch means that th
fe560 65 20 70 61 67 65 72 20 6d 75 73 74 0a 2a 2a 20 e pager must.**
fe570 62 65 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 be in PAGER_SHAR
fe580 45 44 20 62 65 66 6f 72 65 20 69 74 20 74 72 61 ED before it tra
fe590 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 nsitions to PAGE
fe5a0 52 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a 2a 20 R_RESERVED.).**
fe5b0 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 6d PAGER_RESERVED m
fe5c0 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20 eans that there
fe5d0 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c 6c 62 is an open rollb
fe5e0 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 ack journal..**
fe5f0 54 68 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74 The transition t
fe600 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 o PAGER_EXCLUSIV
fe610 45 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 20 E occurs before
fe620 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 any changes.** a
fe630 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 64 re made to the d
fe640 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
fe650 6f 75 67 68 20 77 72 69 74 65 73 20 74 6f 20 74 ough writes to t
fe660 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a he rollback.** j
fe670 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73 20 77 69 ournal occurs wi
fe680 74 68 20 6a 75 73 74 20 50 41 47 45 52 5f 52 45 th just PAGER_RE
fe690 53 45 52 56 45 44 2e 20 20 41 66 74 65 72 20 61 SERVED. After a
fe6a0 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f n sqlite3PagerRo
fe6b0 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72 20 73 llback().** or s
fe6c0 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 qlite3PagerCommi
fe6d0 74 50 68 61 73 65 54 77 6f 28 29 2c 20 74 68 65 tPhaseTwo(), the
fe6e0 20 73 74 61 74 65 20 63 61 6e 20 67 6f 20 62 61 state can go ba
fe6f0 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 ck to PAGER_SHAR
fe700 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63 61 6e ED,.** or it can
fe710 20 73 74 61 79 20 61 74 20 50 41 47 45 52 5f 45 stay at PAGER_E
fe720 58 43 4c 55 53 49 56 45 20 69 66 20 77 65 20 61 XCLUSIVE if we a
fe730 72 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 re in exclusive
fe740 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a 2f 0a access mode..*/.
fe750 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 55 4e #define PAGER_UN
fe760 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23 64 65 66 LOCK 0.#def
fe770 69 6e 65 20 50 41 47 45 52 5f 53 48 41 52 45 44 ine PAGER_SHARED
fe780 20 20 20 20 20 20 31 20 20 20 2f 2a 20 73 61 6d 1 /* sam
fe790 65 20 61 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b e as SHARED_LOCK
fe7a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
fe7b0 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 32 20 R_RESERVED 2
fe7c0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 52 45 53 /* same as RES
fe7d0 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 ERVED_LOCK */.#d
fe7e0 65 66 69 6e 65 20 50 41 47 45 52 5f 45 58 43 4c efine PAGER_EXCL
fe7f0 55 53 49 56 45 20 20 20 34 20 20 20 2f 2a 20 73 USIVE 4 /* s
fe800 61 6d 65 20 61 73 20 45 58 43 4c 55 53 49 56 45 ame as EXCLUSIVE
fe810 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 _LOCK */.#define
fe820 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 PAGER_SYNCED
fe830 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 5../*.** If t
fe840 68 65 20 53 51 4c 49 54 45 5f 42 55 53 59 5f 52 he SQLITE_BUSY_R
fe850 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 6d 61 63 ESERVED_LOCK mac
fe860 72 6f 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 ro is set to tru
fe870 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d e at compile-tim
fe880 65 2c 0a 2a 2a 20 74 68 65 6e 20 66 61 69 6c 65 e,.** then faile
fe890 64 20 61 74 74 65 6d 70 74 73 20 74 6f 20 67 65 d attempts to ge
fe8a0 74 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 t a reserved loc
fe8b0 6b 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 k will invoke th
fe8c0 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 2e e busy callback.
fe8d0 0a 2a 2a 20 54 68 69 73 20 69 73 20 6f 66 66 20 .** This is off
fe8e0 62 79 20 64 65 66 61 75 6c 74 2e 20 20 54 6f 20 by default. To
fe8f0 73 65 65 20 77 68 79 2c 20 63 6f 6e 73 69 64 65 see why, conside
fe900 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 r the following
fe910 73 63 65 6e 61 72 69 6f 3a 0a 2a 2a 20 0a 2a 2a scenario:.** .**
fe920 20 53 75 70 70 6f 73 65 20 74 68 72 65 61 64 20 Suppose thread
fe930 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 A already has a
fe940 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20 shared lock and
fe950 77 61 6e 74 73 20 61 20 72 65 73 65 72 76 65 64 wants a reserved
fe960 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 72 65 61 64 lock..** Thread
fe970 20 42 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 B already has a
fe980 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 61 reserved lock a
fe990 6e 64 20 77 61 6e 74 73 20 61 6e 20 65 78 63 6c nd wants an excl
fe9a0 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 66 0a usive lock. If.
fe9b0 2a 2a 20 62 6f 74 68 20 74 68 72 65 61 64 73 20 ** both threads
fe9c0 61 72 65 20 75 73 69 6e 67 20 74 68 65 69 72 20 are using their
fe9d0 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 73 2c 20 busy callbacks,
fe9e0 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 6c 6f it might be a lo
fe9f0 6e 67 20 74 69 6d 65 0a 2a 2a 20 62 65 20 66 6f ng time.** be fo
fea00 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 68 72 r one of the thr
fea10 65 61 64 73 20 67 69 76 65 20 75 70 20 61 6e 64 eads give up and
fea20 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 74 68 65 allows the othe
fea30 72 20 74 6f 20 70 72 6f 63 65 65 64 2e 0a 2a 2a r to proceed..**
fea40 20 42 75 74 20 69 66 20 74 68 65 20 74 68 72 65 But if the thre
fea50 61 64 20 74 72 79 69 6e 67 20 74 6f 20 67 65 74 ad trying to get
fea60 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f the reserved lo
fea70 63 6b 20 67 69 76 65 73 20 75 70 20 71 75 69 63 ck gives up quic
fea80 6b 6c 79 0a 2a 2a 20 28 69 66 20 69 74 20 6e 65 kly.** (if it ne
fea90 76 65 72 20 69 6e 76 6f 6b 65 73 20 69 74 73 20 ver invokes its
feaa0 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 29 20 74 busy callback) t
feab0 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 69 hen the contenti
feac0 6f 6e 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 72 65 on will be.** re
fead0 73 6f 6c 76 65 64 20 71 75 69 63 6b 6c 79 2e 0a solved quickly..
feae0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
feaf0 45 5f 42 55 53 59 5f 52 45 53 45 52 56 45 44 5f E_BUSY_RESERVED_
feb00 4c 4f 43 4b 0a 23 20 64 65 66 69 6e 65 20 53 51 LOCK.# define SQ
feb10 4c 49 54 45 5f 42 55 53 59 5f 52 45 53 45 52 56 LITE_BUSY_RESERV
feb20 45 44 5f 4c 4f 43 4b 20 30 0a 23 65 6e 64 69 66 ED_LOCK 0.#endif
feb30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 ../*.** This mac
feb40 72 6f 20 72 6f 75 6e 64 73 20 76 61 6c 75 65 73 ro rounds values
feb50 20 75 70 20 73 6f 20 74 68 61 74 20 69 66 20 74 up so that if t
feb60 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20 61 he value is an a
feb70 64 64 72 65 73 73 20 69 74 0a 2a 2a 20 69 73 20 ddress it.** is
feb80 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 guaranteed to be
feb90 20 61 6e 20 61 64 64 72 65 73 73 20 74 68 61 74 an address that
feba0 20 69 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 is aligned to a
febb0 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 n 8-byte boundar
febc0 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f y..*/.#define FO
febd0 52 43 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 29 RCE_ALIGNMENT(X)
febe0 20 20 20 28 28 28 58 29 2b 37 29 26 7e 37 29 0a (((X)+7)&~7).
febf0 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 ./*.** A macro u
fec00 73 65 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 sed for invoking
fec10 20 74 68 65 20 63 6f 64 65 63 20 69 66 20 74 68 the codec if th
fec20 65 72 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 ere is one.*/.#i
fec30 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f fdef SQLITE_HAS_
fec40 43 4f 44 45 43 0a 23 20 64 65 66 69 6e 65 20 43 CODEC.# define C
fec50 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 29 20 69 ODEC1(P,D,N,X) i
fec60 66 28 20 50 2d 3e 78 43 6f 64 65 63 21 3d 30 20 f( P->xCodec!=0
fec70 29 7b 20 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e ){ P->xCodec(P->
fec80 70 43 6f 64 65 63 41 72 67 2c 44 2c 4e 2c 58 29 pCodecArg,D,N,X)
fec90 3b 20 7d 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 ; }.# define COD
feca0 45 43 32 28 50 2c 44 2c 4e 2c 58 29 20 28 28 63 EC2(P,D,N,X) ((c
fecb0 68 61 72 2a 29 28 50 2d 3e 78 43 6f 64 65 63 21 har*)(P->xCodec!
fecc0 3d 30 3f 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e =0?P->xCodec(P->
fecd0 70 43 6f 64 65 63 41 72 67 2c 44 2c 4e 2c 58 29 pCodecArg,D,N,X)
fece0 3a 44 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 :D)).#else.# def
fecf0 69 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e ine CODEC1(P,D,N
fed00 2c 58 29 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a ,X) /* NO-OP */.
fed10 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 # define CODEC2(
fed20 50 2c 44 2c 4e 2c 58 29 20 28 28 63 68 61 72 2a P,D,N,X) ((char*
fed30 29 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a )D).#endif../*.*
fed40 2a 20 41 20 6f 70 65 6e 20 70 61 67 65 20 63 61 * A open page ca
fed50 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e che is an instan
fed60 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
fed70 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
fed80 2a 0a 2a 2a 20 50 61 67 65 72 2e 65 72 72 43 6f *.** Pager.errCo
fed90 64 65 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f de may be set to
feda0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 SQLITE_IOERR, S
fedb0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f QLITE_CORRUPT, o
fedc0 72 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 46 r.** or SQLITE_F
fedd0 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 ULL. Once one of
fede0 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 the first three
fedf0 20 65 72 72 6f 72 73 20 6f 63 63 75 72 73 2c 20 errors occurs,
fee00 69 74 20 70 65 72 73 69 73 74 73 0a 2a 2a 20 61 it persists.** a
fee10 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 nd is returned a
fee20 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 s the result of
fee30 65 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 67 65 every major page
fee40 72 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 68 65 r API call. The
fee50 0a 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 .** SQLITE_FULL
fee60 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73 20 73 return code is s
fee70 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e lightly differen
fee80 74 2e 20 49 74 20 70 65 72 73 69 73 74 73 20 6f t. It persists o
fee90 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a nly until the.**
feea0 20 6e 65 78 74 20 73 75 63 63 65 73 73 66 75 6c next successful
feeb0 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65 72 rollback is per
feec0 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70 61 formed on the pa
feed0 67 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c ger cache. Also,
feee0 0a 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 .** SQLITE_FULL
feef0 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74 20 does not affect
fef00 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 the sqlite3Pager
fef10 47 65 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 Get() and sqlite
fef20 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 0a 2a 3PagerLookup().*
fef30 2a 20 41 50 49 73 2c 20 74 68 65 79 20 6d 61 79 * APIs, they may
fef40 20 73 74 69 6c 6c 20 62 65 20 75 73 65 64 20 73 still be used s
fef50 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 2a 2f 0a uccessfully..*/.
fef60 73 74 72 75 63 74 20 50 61 67 65 72 20 7b 0a 20 struct Pager {.
fef70 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
fef80 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 fs; /*
fef90 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 OS functions to
fefa0 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a 20 20 use for IO */.
fefb0 75 38 20 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3b 20 u8 journalOpen;
fefc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
fefd0 72 75 65 20 69 66 20 6a 6f 75 72 6e 61 6c 20 66 rue if journal f
fefe0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
feff0 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 75 38 is valid */. u8
ff000 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 3b journalStarted;
ff010 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
ff020 65 20 69 66 20 68 65 61 64 65 72 20 6f 66 20 6a e if header of j
ff030 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 ournal is synced
ff040 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f 75 72 */. u8 useJour
ff050 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 nal;
ff060 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c 6c 62 /* Use a rollb
ff070 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20 74 ack journal on t
ff080 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 his file */. u8
ff090 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20 20 20 noReadlock;
ff0a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 /* Do
ff0b0 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 6f 62 not bother to ob
ff0c0 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73 20 2a tain readlocks *
ff0d0 2f 0a 20 20 75 38 20 73 74 6d 74 4f 70 65 6e 3b /. u8 stmtOpen;
ff0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff0f0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 73 /* True if the s
ff100 74 61 74 65 6d 65 6e 74 20 73 75 62 6a 6f 75 72 tatement subjour
ff110 6e 61 6c 20 69 73 20 6f 70 65 6e 20 2a 2f 0a 20 nal is open */.
ff120 20 75 38 20 73 74 6d 74 49 6e 55 73 65 3b 20 20 u8 stmtInUse;
ff130 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ff140 54 72 75 65 20 77 65 20 61 72 65 20 69 6e 20 61 True we are in a
ff150 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 statement subtr
ff160 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 ansaction */. u
ff170 38 20 73 74 6d 74 41 75 74 6f 6f 70 65 6e 3b 20 8 stmtAutoopen;
ff180 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
ff190 65 6e 20 73 74 6d 74 20 6a 6f 75 72 6e 61 6c 20 en stmt journal
ff1a0 77 68 65 6e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 when main journa
ff1b0 6c 20 69 73 20 6f 70 65 6e 65 64 2a 2f 0a 20 20 l is opened*/.
ff1c0 75 38 20 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20 u8 noSync;
ff1d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
ff1e0 6f 20 6e 6f 74 20 73 79 6e 63 20 74 68 65 20 6a o not sync the j
ff1f0 6f 75 72 6e 61 6c 20 69 66 20 74 72 75 65 20 2a ournal if true *
ff200 2f 0a 20 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b /. u8 fullSync;
ff210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff220 2f 2a 20 44 6f 20 65 78 74 72 61 20 73 79 6e 63 /* Do extra sync
ff230 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c s of the journal
ff240 20 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 20 for robustness
ff250 2a 2f 0a 20 20 75 38 20 73 79 6e 63 5f 66 6c 61 */. u8 sync_fla
ff260 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
ff270 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f /* One of SYNC_
ff280 4e 4f 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 NORMAL or SYNC_F
ff290 55 4c 4c 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 ULL */. u8 stat
ff2a0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
ff2b0 20 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e /* PAGER_UN
ff2c0 4c 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f LOCK, _SHARED, _
ff2d0 52 45 53 45 52 56 45 44 2c 20 65 74 63 2e 20 2a RESERVED, etc. *
ff2e0 2f 0a 20 20 75 38 20 74 65 6d 70 46 69 6c 65 3b /. u8 tempFile;
ff2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff300 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 /* zFilename is
ff310 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
ff320 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c */. u8 readOnl
ff330 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
ff340 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
ff350 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 read-only databa
ff360 73 65 20 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 se */. u8 needS
ff370 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 ync;
ff380 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
ff390 6e 20 66 73 79 6e 63 28 29 20 69 73 20 6e 65 65 n fsync() is nee
ff3a0 64 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e ded on the journ
ff3b0 61 6c 20 2a 2f 0a 20 20 75 38 20 64 69 72 74 79 al */. u8 dirty
ff3c0 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 Cache;
ff3d0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 /* True if c
ff3e0 61 63 68 65 64 20 70 61 67 65 73 20 68 61 76 65 ached pages have
ff3f0 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 38 changed */. u8
ff400 20 61 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 3b alwaysRollback;
ff410 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73 /* Dis
ff420 61 62 6c 65 20 44 6f 6e 74 52 6f 6c 6c 62 61 63 able DontRollbac
ff430 6b 28 29 20 66 6f 72 20 61 6c 6c 20 70 61 67 65 k() for all page
ff440 73 20 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 3b s */. u8 memDb;
ff450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff460 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e /* True to in
ff470 68 69 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 49 hibit all file I
ff480 2f 4f 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 /O */. u8 setMa
ff490 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 ster;
ff4a0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
ff4b0 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 m-j name has be
ff4c0 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 en written to jr
ff4d0 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 nl */. u8 doNot
ff4e0 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 Sync;
ff4f0 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 /* Boolean.
ff500 57 68 69 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e While true, do n
ff510 6f 74 20 73 70 69 6c 6c 20 74 68 65 20 63 61 63 ot spill the cac
ff520 68 65 20 2a 2f 0a 20 20 75 38 20 65 78 63 6c 75 he */. u8 exclu
ff530 73 69 76 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 siveMode;
ff540 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 /* Boolean.
ff550 54 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e 67 5f True if locking_
ff560 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56 45 20 mode==EXCLUSIVE
ff570 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4d */. u8 journalM
ff580 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ode;
ff590 20 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 20 50 41 /* On of the PA
ff5a0 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
ff5b0 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 * values */. u8
ff5c0 20 64 62 4d 6f 64 69 66 69 65 64 3b 20 20 20 20 dbModified;
ff5d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
ff5e0 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 e if there are a
ff5f0 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 ny changes to th
ff600 65 20 44 62 20 2a 2f 0a 20 20 75 38 20 63 68 61 e Db */. u8 cha
ff610 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 ngeCountDone;
ff620 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74 /* Set aft
ff630 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 er incrementing
ff640 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
ff650 65 72 20 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a er */. u8 dbSiz
ff660 65 56 61 6c 69 64 3b 20 20 20 20 20 20 20 20 20 eValid;
ff670 20 20 20 20 2f 2a 20 53 65 74 20 77 68 65 6e 20 /* Set when
ff680 64 62 53 69 7a 65 20 69 73 20 63 6f 72 72 65 63 dbSize is correc
ff690 74 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 46 6c t */. u32 vfsFl
ff6a0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
ff6b0 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 20 /* Flags for
ff6c0 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 sqlite3_vfs.xOpe
ff6d0 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 n() */. int err
ff6e0 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 Code;
ff6f0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 /* One of s
ff700 65 76 65 72 61 6c 20 6b 69 6e 64 73 20 6f 66 20 everal kinds of
ff710 65 72 72 6f 72 73 20 2a 2f 0a 20 20 50 67 6e 6f errors */. Pgno
ff720 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 dbSize;
ff730 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ff740 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
ff750 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f e file */. Pgno
ff760 20 6f 72 69 67 44 62 53 69 7a 65 3b 20 20 20 20 origDbSize;
ff770 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a /* dbSiz
ff780 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 75 72 e before the cur
ff790 72 65 6e 74 20 63 68 61 6e 67 65 20 2a 2f 0a 20 rent change */.
ff7a0 20 50 67 6e 6f 20 73 74 6d 74 53 69 7a 65 3b 20 Pgno stmtSize;
ff7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ff7c0 53 69 7a 65 20 6f 66 20 64 61 74 61 62 61 73 65 Size of database
ff7d0 20 28 69 6e 20 70 61 67 65 73 29 20 61 74 20 73 (in pages) at s
ff7e0 74 6d 74 5f 62 65 67 69 6e 28 29 20 2a 2f 0a 20 tmt_begin() */.
ff7f0 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20 20 int nRec;
ff800 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ff810 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
ff820 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
ff830 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 ournal */. u32
ff840 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 cksumInit;
ff850 20 20 20 20 20 20 20 20 2f 2a 20 51 75 61 73 69 /* Quasi
ff860 2d 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64 -random value ad
ff870 64 65 64 20 74 6f 20 65 76 65 72 79 20 63 68 65 ded to every che
ff880 63 6b 73 75 6d 20 2a 2f 0a 20 20 69 6e 74 20 73 cksum */. int s
ff890 74 6d 74 4e 52 65 63 3b 20 20 20 20 20 20 20 20 tmtNRec;
ff8a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
ff8b0 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e 20 73 of records in s
ff8c0 74 6d 74 20 73 75 62 6a 6f 75 72 6e 61 6c 20 2a tmt subjournal *
ff8d0 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 3b 20 /. int nExtra;
ff8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff8f0 2f 2a 20 41 64 64 20 74 68 69 73 20 6d 61 6e 79 /* Add this many
ff900 20 62 79 74 65 73 20 74 6f 20 65 61 63 68 20 69 bytes to each i
ff910 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f n-memory page */
ff920 0a 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 3b . int pageSize;
ff930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ff940 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
ff950 73 20 69 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 s in a page */.
ff960 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 int nPage;
ff970 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ff980 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Total number of
ff990 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 in-memory pages
ff9a0 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 67 65 3b */. int mxPage;
ff9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff9c0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 /* Maximum numb
ff9d0 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 68 er of pages to h
ff9e0 6f 6c 64 20 69 6e 20 63 61 63 68 65 20 2a 2f 0a old in cache */.
ff9f0 20 20 50 67 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20 Pgno mxPgno;
ffa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ffa10 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 Maximum allowed
ffa20 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
ffa30 61 62 61 73 65 20 2a 2f 0a 20 20 42 69 74 76 65 abase */. Bitve
ffa40 63 20 2a 70 49 6e 4a 6f 75 72 6e 61 6c 3b 20 20 c *pInJournal;
ffa50 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69 /* One bi
ffa60 74 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 t for each page
ffa70 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
ffa80 66 69 6c 65 20 2a 2f 0a 20 20 42 69 74 76 65 63 file */. Bitvec
ffa90 20 2a 70 49 6e 53 74 6d 74 3b 20 20 20 20 20 20 *pInStmt;
ffaa0 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69 74 /* One bit
ffab0 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 for each page i
ffac0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a n the database *
ffad0 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 41 6c 77 /. Bitvec *pAlw
ffae0 61 79 73 52 6f 6c 6c 62 61 63 6b 3b 20 20 20 20 aysRollback;
ffaf0 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 /* One bit for e
ffb00 61 63 68 20 70 61 67 65 20 6d 61 72 6b 65 64 20 ach page marked
ffb10 61 6c 77 61 79 73 2d 72 6f 6c 6c 62 61 63 6b 20 always-rollback
ffb20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 */. char *zFile
ffb30 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 name;
ffb40 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
ffb50 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
ffb60 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 . char *zJourna
ffb70 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f l; /
ffb80 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f * Name of the jo
ffb90 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 urnal file */.
ffba0 63 68 61 72 20 2a 7a 44 69 72 65 63 74 6f 72 79 char *zDirectory
ffbb0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 ; /* D
ffbc0 69 72 65 63 74 6f 72 79 20 68 6f 6c 64 20 64 61 irectory hold da
ffbd0 74 61 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e tabase and journ
ffbe0 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 73 71 al files */. sq
ffbf0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 lite3_file *fd,
ffc00 2a 6a 66 64 3b 20 20 20 20 20 2f 2a 20 46 69 6c *jfd; /* Fil
ffc10 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 66 6f e descriptors fo
ffc20 72 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 6a r database and j
ffc30 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 ournal */. sqli
ffc40 74 65 33 5f 66 69 6c 65 20 2a 73 74 66 64 3b 20 te3_file *stfd;
ffc50 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 /* File
ffc60 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 descriptor for t
ffc70 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 he statement sub
ffc80 6a 6f 75 72 6e 61 6c 2a 2f 0a 20 20 69 6e 74 20 journal*/. int
ffc90 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28 (*xBusyHandler)(
ffca0 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 74 void*); /* Funct
ffcb0 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e ion to call when
ffcc0 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20 busy */. void
ffcd0 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 *pBusyHandlerArg
ffce0 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 ; /* Contex
ffcf0 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 78 t argument for x
ffd00 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 20 BusyHandler */.
ffd10 20 69 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b i64 journalOff;
ffd20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ffd30 43 75 72 72 65 6e 74 20 62 79 74 65 20 6f 66 66 Current byte off
ffd40 73 65 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e set in the journ
ffd50 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 al file */. i64
ffd60 20 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20 journalHdr;
ffd70 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
ffd80 20 6f 66 66 73 65 74 20 74 6f 20 70 72 65 76 69 offset to previ
ffd90 6f 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 ous journal head
ffda0 65 72 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74 er */. i64 stmt
ffdb0 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 HdrOff;
ffdc0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6a 6f 75 /* First jou
ffdd0 72 6e 61 6c 20 68 65 61 64 65 72 20 77 72 69 74 rnal header writ
ffde0 74 65 6e 20 74 68 69 73 20 73 74 61 74 65 6d 65 ten this stateme
ffdf0 6e 74 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74 nt */. i64 stmt
ffe00 43 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 Cksum;
ffe10 20 20 20 20 2f 2a 20 63 6b 73 75 6d 49 6e 69 74 /* cksumInit
ffe20 20 77 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 20 when statement
ffe30 77 61 73 20 73 74 61 72 74 65 64 20 2a 2f 0a 20 was started */.
ffe40 20 69 36 34 20 73 74 6d 74 4a 53 69 7a 65 3b 20 i64 stmtJSize;
ffe50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ffe60 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 Size of journal
ffe70 61 74 20 73 74 6d 74 5f 62 65 67 69 6e 28 29 20 at stmt_begin()
ffe80 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53 */. u32 sectorS
ffe90 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
ffea0 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 /* Assumed sect
ffeb0 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 or size during r
ffec0 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 23 69 66 64 65 ollback */.#ifde
ffed0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
ffee0 69 6e 74 20 6e 48 69 74 2c 20 6e 4d 69 73 73 3b int nHit, nMiss;
ffef0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
fff00 61 63 68 65 20 68 69 74 73 20 61 6e 64 20 6d 69 ache hits and mi
fff10 73 73 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e ssing */. int n
fff20 52 65 61 64 2c 20 6e 57 72 69 74 65 3b 20 20 20 Read, nWrite;
fff30 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
fff40 73 65 20 70 61 67 65 73 20 72 65 61 64 2f 77 72 se pages read/wr
fff50 69 74 74 65 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a itten */.#endif.
fff60 20 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 void (*xReinit
fff70 65 72 29 28 44 62 50 61 67 65 2a 29 3b 20 2f 2a er)(DbPage*); /*
fff80 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 Call this routi
fff90 6e 65 20 77 68 65 6e 20 72 65 6c 6f 61 64 69 6e ne when reloadin
fffa0 67 20 70 61 67 65 73 20 2a 2f 0a 23 69 66 64 65 g pages */.#ifde
fffb0 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 f SQLITE_HAS_COD
fffc0 45 43 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f EC. void *(*xCo
fffd0 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a dec)(void*,void*
fffe0 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 2f 2a 20 52 ,Pgno,int); /* R
ffff0 6f 75 74 69 6e 65 20 66 6f 72 20 65 6e 2f 64 65 outine for en/de
10000 63 6f 64 69 6e 67 20 64 61 74 61 20 2a 2f 0a 20 coding data */.
10001 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 41 72 67 void *pCodecArg
10002 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
10003 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 First argument t
10004 6f 20 78 43 6f 64 65 63 28 29 20 2a 2f 0a 23 65 o xCodec() */.#e
10005 6e 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54 6d ndif. char *pTm
10006 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 pSpace;
10007 20 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67 65 /* Pager.page
10008 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 Size bytes of sp
10009 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 ace for tmp use
1000a 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69 6c 65 */. char dbFile
1000b 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20 20 20 Vers[16];
1000c 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68 65 6e /* Changes when
1000d 65 76 65 72 20 64 61 74 61 62 61 73 65 20 66 69 ever database fi
1000e 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 le changes */.
1000f 69 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c i64 journalSizeL
10010 69 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 imit; /* S
10011 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 ize limit for pe
10012 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c rsistent journal
10013 20 66 69 6c 65 73 20 2a 2f 0a 20 20 50 43 61 63 files */. PCac
10014 68 65 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20 he *pPCache;
10015 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
10016 65 72 20 74 6f 20 70 61 67 65 20 63 61 63 68 65 er to page cache
10017 20 6f 62 6a 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f object */.};../
10018 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
10019 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 ng global variab
1001a 6c 65 73 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72 les hold counter
1001b 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 s used for.** te
1001c 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f sting purposes o
1001d 6e 6c 79 2e 20 20 54 68 65 73 65 20 76 61 72 69 nly. These vari
1001e 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 65 78 69 ables do not exi
1001f 73 74 20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 st in.** a non-t
10020 65 73 74 69 6e 67 20 62 75 69 6c 64 2e 20 20 54 esting build. T
10021 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 61 hese variables a
10022 72 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 re not thread-sa
10023 66 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 fe..*/.#ifdef SQ
10024 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
10025 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
10026 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63 6f _pager_readdb_co
10027 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e unt = 0; /* N
10028 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 umber of full pa
10029 67 65 73 20 72 65 61 64 20 66 72 6f 6d 20 44 42 ges read from DB
1002a 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
1002b 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 nt sqlite3_pager
1002c 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 20 3d _writedb_count =
1002d 20 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 0; /* Number
1002e 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 77 72 of full pages wr
1002f 69 74 74 65 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 itten to DB */.S
10030 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
10031 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 lite3_pager_writ
10032 65 6a 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 ej_count = 0;
10033 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
10034 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6a ges written to j
10035 6f 75 72 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 69 ournal */.# defi
10036 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 ne PAGER_INCR(v)
10037 20 20 76 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 65 v++.#else.# de
10038 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28 fine PAGER_INCR(
10039 76 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a v).#endif..../*.
1003a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 ** Journal files
1003b 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 begin with the
1003c 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 67 69 63 20 following magic
1003d 73 74 72 69 6e 67 2e 20 20 54 68 65 20 64 61 74 string. The dat
1003e 61 0a 2a 2a 20 77 61 73 20 6f 62 74 61 69 6e 65 a.** was obtaine
1003f 64 20 66 72 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 d from /dev/rand
10040 6f 6d 2e 20 20 49 74 20 69 73 20 75 73 65 64 20 om. It is used
10041 6f 6e 6c 79 20 61 73 20 61 20 73 61 6e 69 74 79 only as a sanity
10042 20 63 68 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 check..**.** Si
10043 6e 63 65 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e nce version 2.8.
10044 30 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 0, the journal f
10045 6f 72 6d 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 ormat contains a
10046 64 64 69 74 69 6f 6e 61 6c 20 73 61 6e 69 74 79 dditional sanity
10047 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 .** checking inf
10048 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 ormation. If th
10049 65 20 70 6f 77 65 72 20 66 61 69 6c 73 20 77 68 e power fails wh
1004a 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ile the journal
1004b 69 73 20 62 65 67 69 6e 0a 2a 2a 20 77 72 69 74 is begin.** writ
1004c 74 65 6e 2c 20 73 65 6d 69 2d 72 61 6e 64 6f 6d ten, semi-random
1004d 20 67 61 72 62 61 67 65 20 64 61 74 61 20 6d 69 garbage data mi
1004e 67 68 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 ght appear in th
1004f 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c e journal.** fil
10050 65 20 61 66 74 65 72 20 70 6f 77 65 72 20 69 73 e after power is
10051 20 72 65 73 74 6f 72 65 64 2e 20 20 49 66 20 61 restored. If a
10052 6e 20 61 74 74 65 6d 70 74 20 69 73 20 74 68 65 n attempt is the
10053 6e 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c n made.** to rol
10054 6c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61 l the journal ba
10055 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 ck, the database
10056 20 63 6f 75 6c 64 20 62 65 20 63 6f 72 72 75 70 could be corrup
10057 74 65 64 2e 20 20 54 68 65 20 61 64 64 69 74 69 ted. The additi
10058 6f 6e 61 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 63 onal.** sanity c
10059 68 65 63 6b 69 6e 67 20 64 61 74 61 20 69 73 20 hecking data is
1005a 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 64 69 an attempt to di
1005b 73 63 6f 76 65 72 20 74 68 65 20 67 61 72 62 61 scover the garba
1005c 67 65 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 ge in the.** jou
1005d 72 6e 61 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20 rnal and ignore
1005e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 it..**.** The sa
1005f 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 69 6e nity checking in
10060 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 formation for th
10061 65 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 66 6f e new journal fo
10062 72 6d 61 74 20 63 6f 6e 73 69 73 74 73 0a 2a 2a rmat consists.**
10063 20 6f 66 20 61 20 33 32 2d 62 69 74 20 63 68 65 of a 32-bit che
10064 63 6b 73 75 6d 20 6f 6e 20 65 61 63 68 20 70 61 cksum on each pa
10065 67 65 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65 ge of data. The
10066 20 63 68 65 63 6b 73 75 6d 20 63 6f 76 65 72 73 checksum covers
10067 20 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 67 both.** the pag
10068 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 e number and the
10069 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
1006a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 e bytes of data
1006b 66 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a for the page..**
1006c 20 54 68 69 73 20 63 6b 73 75 6d 20 69 73 20 69 This cksum is i
1006d 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 nitialized to a
1006e 33 32 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 32-bit random va
1006f 6c 75 65 20 74 68 61 74 20 61 70 70 65 61 72 73 lue that appears
10070 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e in the.** journ
10071 61 6c 20 66 69 6c 65 20 72 69 67 68 74 20 61 66 al file right af
10072 74 65 72 20 74 68 65 20 68 65 61 64 65 72 2e 20 ter the header.
10073 20 54 68 65 20 72 61 6e 64 6f 6d 20 69 6e 69 74 The random init
10074 69 61 6c 69 7a 65 72 20 69 73 20 69 6d 70 6f 72 ializer is impor
10075 74 61 6e 74 2c 0a 2a 2a 20 62 65 63 61 75 73 65 tant,.** because
10076 20 67 61 72 62 61 67 65 20 64 61 74 61 20 74 68 garbage data th
10077 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 at appears at th
10078 65 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e e end of a journ
10079 61 6c 20 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 al is likely.**
1007a 64 61 74 61 20 74 68 61 74 20 77 61 73 20 6f 6e data that was on
1007b 63 65 20 69 6e 20 6f 74 68 65 72 20 66 69 6c 65 ce in other file
1007c 73 20 74 68 61 74 20 68 61 76 65 20 6e 6f 77 20 s that have now
1007d 62 65 65 6e 20 64 65 6c 65 74 65 64 2e 20 20 49 been deleted. I
1007e 66 20 74 68 65 0a 2a 2a 20 67 61 72 62 61 67 65 f the.** garbage
1007f 20 64 61 74 61 20 63 61 6d 65 20 66 72 6f 6d 20 data came from
10080 61 6e 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 an obsolete jour
10081 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 63 68 nal file, the ch
10082 65 63 6b 73 75 6d 73 20 6d 69 67 68 74 0a 2a 2a ecksums might.**
10083 20 62 65 20 63 6f 72 72 65 63 74 2e 20 20 42 75 be correct. Bu
10084 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e t by initializin
10085 67 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 74 g the checksum t
10086 6f 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 77 o random value w
10087 68 69 63 68 0a 2a 2a 20 69 73 20 64 69 66 66 65 hich.** is diffe
10088 72 65 6e 74 20 66 6f 72 20 65 76 65 72 79 20 6a rent for every j
10089 6f 75 72 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 6d ournal, we minim
1008a 69 7a 65 20 74 68 61 74 20 72 69 73 6b 2e 0a 2a ize that risk..*
1008b 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 /.static const u
1008c 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4a 6f nsigned char aJo
1008d 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b urnalMagic[] = {
1008e 0a 20 20 30 78 64 39 2c 20 30 78 64 35 2c 20 30 . 0xd9, 0xd5, 0
1008f 78 30 35 2c 20 30 78 66 39 2c 20 30 78 32 30 2c x05, 0xf9, 0x20,
10090 20 30 78 61 31 2c 20 30 78 36 33 2c 20 30 78 64 0xa1, 0x63, 0xd
10091 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 7,.};../*.** The
10092 20 73 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 size of the hea
10093 64 65 72 20 61 6e 64 20 6f 66 20 65 61 63 68 20 der and of each
10094 70 61 67 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 page in the jour
10095 6e 61 6c 20 69 73 20 64 65 74 65 72 6d 69 6e 65 nal is determine
10096 64 0a 2a 2a 20 62 79 20 74 68 65 20 66 6f 6c 6c d.** by the foll
10097 6f 77 69 6e 67 20 6d 61 63 72 6f 73 2e 0a 2a 2f owing macros..*/
10098 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c .#define JOURNAL
10099 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 20 20 _PG_SZ(pPager)
1009a 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 ((pPager->pageSi
1009b 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 ze) + 8)../*.**
1009c 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 The journal head
1009d 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 er size for this
1009e 20 70 61 67 65 72 2e 20 49 6e 20 74 68 65 20 66 pager. In the f
1009f 75 74 75 72 65 2c 20 74 68 69 73 20 63 6f 75 6c uture, this coul
100a0 64 20 62 65 0a 2a 2a 20 73 65 74 20 74 6f 20 73 d be.** set to s
100a1 6f 6d 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 ome value read f
100a2 72 6f 6d 20 74 68 65 20 64 69 73 6b 20 63 6f 6e rom the disk con
100a3 74 72 6f 6c 6c 65 72 2e 20 54 68 65 20 69 6d 70 troller. The imp
100a4 6f 72 74 61 6e 74 0a 2a 2a 20 63 68 61 72 61 63 ortant.** charac
100a5 74 65 72 69 73 74 69 63 20 69 73 20 74 68 61 74 teristic is that
100a6 20 69 74 20 69 73 20 74 68 65 20 73 61 6d 65 20 it is the same
100a7 73 69 7a 65 20 61 73 20 61 20 64 69 73 6b 20 73 size as a disk s
100a8 65 63 74 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e ector..*/.#defin
100a9 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a e JOURNAL_HDR_SZ
100aa 28 70 50 61 67 65 72 29 20 28 70 50 61 67 65 72 (pPager) (pPager
100ab 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a 0a 2f ->sectorSize)../
100ac 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 4d *.** The macro M
100ad 45 4d 44 42 20 69 73 20 74 72 75 65 20 69 66 20 EMDB is true if
100ae 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 we are dealing w
100af 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 ith an in-memory
100b0 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 57 65 database..** We
100b1 20 64 6f 20 74 68 69 73 20 61 73 20 61 20 6d 61 do this as a ma
100b2 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66 20 74 cro so that if t
100b3 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d he SQLITE_OMIT_M
100b4 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20 69 73 EMORYDB macro is
100b5 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76 61 6c set,.** the val
100b6 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69 6c 6c ue of MEMDB will
100b7 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74 20 61 be a constant a
100b8 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 nd the compiler
100b9 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a 2a 2a will optimize.**
100ba 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74 20 77 out code that w
100bb 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65 63 75 ould never execu
100bc 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 te..*/.#ifdef SQ
100bd 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 LITE_OMIT_MEMORY
100be 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 DB.# define MEMD
100bf 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 B 0.#else.# defi
100c0 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 72 2d ne MEMDB pPager-
100c1 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a 0a 2f >memDb.#endif../
100c2 2a 0a 2a 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 *.** Page number
100c3 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 20 69 PAGER_MJ_PGNO i
100c4 73 20 6e 65 76 65 72 20 75 73 65 64 20 69 6e 20 s never used in
100c5 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 an SQLite databa
100c6 73 65 20 28 69 74 20 69 73 0a 2a 2a 20 72 65 73 se (it is.** res
100c7 65 72 76 65 64 20 66 6f 72 20 77 6f 72 6b 69 6e erved for workin
100c8 67 20 61 72 6f 75 6e 64 20 61 20 77 69 6e 64 6f g around a windo
100c9 77 73 2f 70 6f 73 69 78 20 69 6e 63 6f 6d 70 61 ws/posix incompa
100ca 74 69 62 69 6c 69 74 79 29 2e 20 49 74 20 69 73 tibility). It is
100cb 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74 68 65 20 .** used in the
100cc 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 69 67 6e 69 journal to signi
100cd 66 79 20 74 68 61 74 20 74 68 65 20 72 65 6d 61 fy that the rema
100ce 69 6e 64 65 72 20 6f 66 20 74 68 65 20 6a 6f 75 inder of the jou
100cf 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 69 73 rnal file .** is
100d0 20 64 65 76 6f 74 65 64 20 74 6f 20 73 74 6f 72 devoted to stor
100d1 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 ing a master jou
100d2 72 6e 61 6c 20 6e 61 6d 65 20 2d 20 74 68 65 72 rnal name - ther
100d3 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61 e are no more pa
100d4 67 65 73 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 ges to.** roll b
100d5 61 63 6b 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 ack. See comment
100d6 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 77 s for function w
100d7 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 riteMasterJourna
100d8 6c 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e l() for details.
100d9 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 50 .*/./* #define P
100da 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 78 29 20 AGER_MJ_PGNO(x)
100db 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 (PENDING_BYTE/((
100dc 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 20 2a x)->pageSize)) *
100dd 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
100de 4d 4a 5f 50 47 4e 4f 28 78 29 20 28 28 50 67 6e MJ_PGNO(x) ((Pgn
100df 6f 29 28 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 o)((PENDING_BYTE
100e0 2f 28 28 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 /((x)->pageSize)
100e1 29 2b 31 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 )+1))../*.** The
100e2 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 maximum legal p
100e3 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32 age number is (2
100e4 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 ^31 - 1)..*/.#de
100e5 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 fine PAGER_MAX_P
100e6 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a GNO 2147483647..
100e7 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
100e8 65 20 69 66 20 70 61 67 65 20 2a 70 50 67 20 68 e if page *pPg h
100e9 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
100ea 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 written to the s
100eb 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6a 6f 75 72 tatement.** jour
100ec 6e 61 6c 20 28 6f 72 20 73 74 61 74 65 6d 65 6e nal (or statemen
100ed 74 20 73 6e 61 70 73 68 6f 74 20 68 61 73 20 62 t snapshot has b
100ee 65 65 6e 20 63 72 65 61 74 65 64 2c 20 69 66 20 een created, if
100ef 2a 70 50 67 20 69 73 20 70 61 72 74 0a 2a 2a 20 *pPg is part.**
100f0 6f 66 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 of an in-memory
100f1 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a 73 74 database)..*/.st
100f2 61 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 53 atic int pageInS
100f3 74 61 74 65 6d 65 6e 74 28 50 67 48 64 72 20 2a tatement(PgHdr *
100f4 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 pPg){. Pager *p
100f5 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
100f6 67 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 ger;. return sq
100f7 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
100f8 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c pPager->pInStmt,
100f9 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a pPg->pgno);.}..
100fa 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 49 static int pageI
100fb 6e 4a 6f 75 72 6e 61 6c 28 50 67 48 64 72 20 2a nJournal(PgHdr *
100fc 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 pPg){. return s
100fd 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
100fe 28 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 70 49 (pPg->pPager->pI
100ff 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 nJournal, pPg->p
10100 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 gno);.}../*.** R
10101 65 61 64 20 61 20 33 32 2d 62 69 74 20 69 6e 74 ead a 32-bit int
10102 65 67 65 72 20 66 72 6f 6d 20 74 68 65 20 67 69 eger from the gi
10103 76 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 ven file descrip
10104 74 6f 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20 tor. Store the
10105 69 6e 74 65 67 65 72 0a 2a 2a 20 74 68 61 74 20 integer.** that
10106 69 73 20 72 65 61 64 20 69 6e 20 2a 70 52 65 73 is read in *pRes
10107 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
10108 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e _OK if everythin
10109 67 20 77 6f 72 6b 65 64 2c 20 6f 72 20 61 6e 0a g worked, or an.
1010a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 ** error code is
1010b 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 something goes
1010c 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c wrong..**.** All
1010d 20 76 61 6c 75 65 73 20 61 72 65 20 73 74 6f 72 values are stor
1010e 65 64 20 6f 6e 20 64 69 73 6b 20 61 73 20 62 69 ed on disk as bi
1010f 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 g-endian..*/.sta
10110 74 69 63 20 69 6e 74 20 72 65 61 64 33 32 62 69 tic int read32bi
10111 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ts(sqlite3_file
10112 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c *fd, i64 offset,
10113 20 75 33 32 20 2a 70 52 65 73 29 7b 0a 20 20 75 u32 *pRes){. u
10114 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 63 5b nsigned char ac[
10115 34 5d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 4];. int rc = s
10116 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66 64 2c qlite3OsRead(fd,
10117 20 61 63 2c 20 73 69 7a 65 6f 66 28 61 63 29 2c ac, sizeof(ac),
10118 20 6f 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20 offset);. if(
10119 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1011a 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 73 71 6c . *pRes = sql
1011b 69 74 65 33 47 65 74 34 62 79 74 65 28 61 63 29 ite3Get4byte(ac)
1011c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1011d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 c;.}../*.** Writ
1011e 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 e a 32-bit integ
1011f 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 er into a string
10120 20 62 75 66 66 65 72 20 69 6e 20 62 69 67 2d 65 buffer in big-e
10121 6e 64 69 61 6e 20 62 79 74 65 20 6f 72 64 65 72 ndian byte order
10122 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70 75 74 ..*/.#define put
10123 33 32 62 69 74 73 28 41 2c 42 29 20 20 73 71 6c 32bits(A,B) sql
10124 69 74 65 33 50 75 74 34 62 79 74 65 28 28 75 38 ite3Put4byte((u8
10125 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 *)A,B)../*.** Wr
10126 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 ite a 32-bit int
10127 65 67 65 72 20 69 6e 74 6f 20 74 68 65 20 67 69 eger into the gi
10128 76 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 ven file descrip
10129 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c tor. Return SQL
1012a 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 ITE_OK.** on suc
1012b 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 cess or an error
1012c 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 74 68 69 code is somethi
1012d 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a ng goes wrong..*
1012e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 /.static int wri
1012f 74 65 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 te32bits(sqlite3
10130 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f _file *fd, i64 o
10131 66 66 73 65 74 2c 20 75 33 32 20 76 61 6c 29 7b ffset, u32 val){
10132 0a 20 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 . char ac[4];.
10133 20 70 75 74 33 32 62 69 74 73 28 61 63 2c 20 76 put32bits(ac, v
10134 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 al);. return sq
10135 6c 69 74 65 33 4f 73 57 72 69 74 65 28 66 64 2c lite3OsWrite(fd,
10136 20 61 63 2c 20 34 2c 20 6f 66 66 73 65 74 29 3b ac, 4, offset);
10137 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69 6c .}../*.** If fil
10138 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c 20 63 e pFd is open, c
10139 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c all sqlite3OsUnl
1013a 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f 0a ock() on it..*/.
1013b 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55 6e 6c static int osUnl
1013c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
1013d 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f 63 6b *pFd, int eLock
1013e 29 7b 0a 20 20 69 66 28 20 21 70 46 64 2d 3e 70 ){. if( !pFd->p
1013f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 Methods ){. r
10140 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
10141 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 . }. return sq
10142 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 lite3OsUnlock(pF
10143 64 2c 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a d, eLock);.}../*
10144 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
10145 6e 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 n determines whe
10146 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
10147 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 atomic-write opt
10148 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e imization.** can
10149 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68 be used with th
1014a 69 73 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70 is pager. The op
1014b 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 timization can b
1014c 65 20 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a e used if:.**.**
1014d 20 20 28 61 29 20 74 68 65 20 76 61 6c 75 65 20 (a) the value
1014e 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65 returned by OsDe
1014f 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
10150 69 63 73 28 29 20 69 6e 64 69 63 61 74 65 73 20 ics() indicates
10151 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 that.** a d
10152 61 74 61 62 61 73 65 20 70 61 67 65 20 6d 61 79 atabase page may
10153 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d be written atom
10154 69 63 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 ically, and.**
10155 28 62 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 (b) the value re
10156 74 75 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74 turned by OsSect
10157 6f 72 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73 orSize() is less
10158 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a than or equal.*
10159 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 * to the pa
1015a 67 65 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 ge size..**.** I
1015b 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 f the optimizati
1015c 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 on cannot be use
1015d 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 64 d, 0 is returned
1015e 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65 20 75 . If it can be u
1015f 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 sed,.** then the
10160 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
10161 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 is the size of t
10162 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
10163 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74 61 when it.** conta
10164 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64 61 74 ins rollback dat
10165 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20 6f 6e a for exactly on
10166 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 e page..*/.#ifde
10167 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
10168 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 73 74 61 ATOMIC_WRITE.sta
10169 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 75 66 66 tic int jrnlBuff
1016a 65 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 erSize(Pager *pP
1016b 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 64 63 3b ager){. int dc;
1016c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
1016d 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 vice characteris
1016e 74 69 63 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 tics */. int nS
1016f 65 63 74 6f 72 3b 20 20 20 20 20 20 2f 2a 20 53 ector; /* S
10170 65 63 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 ector size */.
10171 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 int szPage;
10172 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 /* Page size
10173 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
10174 65 20 2a 66 64 20 3d 20 70 50 61 67 65 72 2d 3e e *fd = pPager->
10175 66 64 3b 0a 0a 20 20 69 66 28 20 66 64 2d 3e 70 fd;.. if( fd->p
10176 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 64 Methods ){. d
10177 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 c = sqlite3OsDev
10178 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
10179 63 73 28 66 64 29 3b 0a 20 20 20 20 6e 53 65 63 cs(fd);. nSec
1017a 74 6f 72 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 tor = sqlite3OsS
1017b 65 63 74 6f 72 53 69 7a 65 28 66 64 29 3b 0a 20 ectorSize(fd);.
1017c 20 20 20 73 7a 50 61 67 65 20 3d 20 70 50 61 67 szPage = pPag
1017d 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 er->pageSize;.
1017e 7d 0a 0a 20 20 61 73 73 65 72 74 28 53 51 4c 49 }.. assert(SQLI
1017f 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 TE_IOCAP_ATOMIC5
10180 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 12==(512>>8));.
10181 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 assert(SQLITE_I
10182 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d OCAP_ATOMIC64K==
10183 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a 0a 20 20 (65536>>8));..
10184 69 66 28 20 21 66 64 2d 3e 70 4d 65 74 68 6f 64 if( !fd->pMethod
10185 73 20 7c 7c 20 0a 20 20 20 20 20 20 20 28 64 63 s || . (dc
10186 20 26 20 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 & (SQLITE_IOCAP
10187 5f 41 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e _ATOMIC|(szPage>
10188 3e 38 29 29 20 26 26 20 6e 53 65 63 74 6f 72 3c >8)) && nSector<
10189 3d 73 7a 50 61 67 65 29 20 29 7b 0a 20 20 20 20 =szPage) ){.
1018a 72 65 74 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 return JOURNAL_H
1018b 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 DR_SZ(pPager) +
1018c 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
1018d 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ager);. }. ret
1018e 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a urn 0;.}.#endif.
1018f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
10190 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63 tion should be c
10191 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 65 72 alled when an er
10192 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 ror occurs withi
10193 6e 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 63 n the pager.** c
10194 6f 64 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 ode. The first a
10195 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 rgument is a poi
10196 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 nter to the page
10197 72 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 r structure, the
10198 0a 2a 2a 20 73 65 63 6f 6e 64 20 74 68 65 20 65 .** second the e
10199 72 72 6f 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 rror-code about
1019a 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 to be returned b
1019b 79 20 61 20 70 61 67 65 72 20 41 50 49 20 66 75 y a pager API fu
1019c 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 20 54 68 65 20 nction. .** The
1019d 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 value returned i
1019e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
1019f 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
101a0 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
101a1 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 . .**.** If the
101a2 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
101a3 69 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c is SQLITE_IOERR,
101a4 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c SQLITE_CORRUPT,
101a5 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a or SQLITE_FULL.
101a6 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65 63 ** the error bec
101a7 6f 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74 2e omes persistent.
101a8 20 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73 69 Until the persi
101a9 73 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63 6c sten error is cl
101aa 65 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65 71 eared,.** subseq
101ab 75 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 6f uent API calls o
101ac 6e 20 74 68 69 73 20 50 61 67 65 72 20 77 69 6c n this Pager wil
101ad 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 65 l immediately re
101ae 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a 2a turn the same .*
101af 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a * error code..**
101b0 0a 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e 74 .** A persistent
101b1 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65 73 error indicates
101b2 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e that the conten
101b3 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d ts of the pager-
101b4 63 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f 74 cache .** cannot
101b5 20 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 69 be trusted. Thi
101b6 73 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 63 s state can be c
101b7 6c 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c 65 leared by comple
101b8 74 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 20 tely discarding
101b9 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 .** the contents
101ba 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 of the pager-ca
101bb 63 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73 61 che. If a transa
101bc 63 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76 65 ction was active
101bd 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65 72 when.** the per
101be 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f 63 sistent error oc
101bf 63 75 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 cured, then the
101c0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
101c1 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 may need.** to
101c2 62 65 20 72 65 70 6c 61 79 65 64 2e 0a 2a 2f 0a be replayed..*/.
101c3 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 static void page
101c4 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a r_unlock(Pager *
101c5 70 50 61 67 65 72 29 3b 0a 73 74 61 74 69 63 20 pPager);.static
101c6 69 6e 74 20 70 61 67 65 72 5f 65 72 72 6f 72 28 int pager_error(
101c7 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
101c8 6e 74 20 72 63 29 7b 0a 20 20 69 6e 74 20 72 63 nt rc){. int rc
101c9 32 20 3d 20 72 63 20 26 20 30 78 66 66 3b 0a 20 2 = rc & 0xff;.
101ca 20 61 73 73 65 72 74 28 0a 20 20 20 20 20 20 20 assert(.
101cb 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d pPager->errCode=
101cc 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a =SQLITE_FULL ||.
101cd 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 pPager->e
101ce 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f rrCode==SQLITE_O
101cf 4b 20 7c 7c 0a 20 20 20 20 20 20 20 28 70 50 61 K ||. (pPa
101d0 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 20 30 ger->errCode & 0
101d1 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 xff)==SQLITE_IOE
101d2 52 52 0a 20 20 29 3b 0a 20 20 69 66 28 0a 20 20 RR. );. if(.
101d3 20 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 46 55 rc2==SQLITE_FU
101d4 4c 4c 20 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53 LL ||. rc2==S
101d5 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 7c 0a 20 QLITE_IOERR ||.
101d6 20 20 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 43 rc2==SQLITE_C
101d7 4f 52 52 55 50 54 0a 20 20 29 7b 0a 20 20 20 20 ORRUPT. ){.
101d8 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
101d9 3d 20 72 63 3b 0a 20 20 20 20 69 66 28 20 70 50 = rc;. if( pP
101da 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
101db 45 52 5f 55 4e 4c 4f 43 4b 20 0a 20 20 20 20 20 ER_UNLOCK .
101dc 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 && sqlite3Pcache
101dd 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
101de 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 >pPCache)==0 .
101df 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 ){. /* If
101e0 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 6c the pager is al
101e1 72 65 61 64 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 ready unlocked,
101e2 63 61 6c 6c 20 70 61 67 65 72 5f 75 6e 6c 6f 63 call pager_unloc
101e3 6b 28 29 20 6e 6f 77 20 74 6f 0a 20 20 20 20 20 k() now to.
101e4 20 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 65 72 ** clear the er
101e5 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20 65 6e ror state and en
101e6 73 75 72 65 20 74 68 61 74 20 74 68 65 20 70 61 sure that the pa
101e7 67 65 72 2d 63 61 63 68 65 20 69 73 20 0a 20 20 ger-cache is .
101e8 20 20 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 6c ** completel
101e9 79 20 65 6d 70 74 79 2e 0a 20 20 20 20 20 20 2a y empty.. *
101ea 2f 0a 20 20 20 20 20 20 70 61 67 65 72 5f 75 6e /. pager_un
101eb 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 lock(pPager);.
101ec 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
101ed 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 rc;.}../*.** If
101ee 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
101ef 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 GES is defined t
101f0 68 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 hen we do some s
101f1 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a anity checking.*
101f2 2a 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 * on the cache u
101f3 73 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 sing a hash func
101f4 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 tion. This is u
101f5 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a sed for testing.
101f6 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 ** and debugging
101f7 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 only..*/.#ifdef
101f8 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
101f9 47 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e GES./*.** Return
101fa 20 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f a 32-bit hash o
101fb 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 f the page data
101fc 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 for pPage..*/.st
101fd 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 atic u32 pager_d
101fe 61 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 atahash(int nByt
101ff 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 e, unsigned char
10200 20 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 *pData){. u32
10201 68 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 hash = 0;. int
10202 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
10203 6e 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 nByte; i++){.
10204 20 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 hash = (hash*10
10205 33 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 39) + pData[i];.
10206 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 }. return has
10207 68 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 h;.}.static u32
10208 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 pager_pagehash(P
10209 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 gHdr *pPage){.
1020a 72 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 return pager_dat
1020b 61 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 ahash(pPage->pPa
1020c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 ger->pageSize, (
1020d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
1020e 70 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d pPage->pData);.}
1020f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 .static void pag
10210 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 er_set_pagehash(
10211 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 PgHdr *pPage){.
10212 20 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68 pPage->pageHash
10213 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
10214 68 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a h(pPage);.}../*.
10215 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 ** The CHECK_PAG
10216 45 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 E macro takes a
10217 50 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 PgHdr* as an arg
10218 75 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 ument. If SQLITE
10219 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 _CHECK_PAGES.**
1021a 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 is defined, and
1021b 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 NDEBUG is not de
1021c 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 fined, an assert
1021d 28 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 () statement che
1021e 63 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 cks.** that the
1021f 70 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64 page is either d
10220 69 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 irty or still ma
10221 74 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c tches the calcul
10222 61 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a ated page-hash..
10223 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b */.#define CHECK
10224 5f 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 _PAGE(x) checkPa
10225 67 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 ge(x).static voi
10226 64 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 d checkPage(PgHd
10227 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 r *pPg){. Pager
10228 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e *pPager = pPg->
10229 70 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 pPager;. assert
1022a 28 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68 ( !pPg->pageHash
1022b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 || pPager->errC
1022c 6f 64 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 ode. || (pP
1022d 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
1022e 49 52 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 IRTY) || pPg->pa
1022f 67 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 geHash==pager_pa
10230 67 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d gehash(pPg) );.}
10231 0a 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 ..#else.#define
10232 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 pager_datahash(X
10233 2c 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 ,Y) 0.#define p
10234 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 ager_pagehash(X)
10235 20 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43 0.#define CHEC
10236 4b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66 K_PAGE(x).#endif
10237 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 /* SQLITE_CHEC
10238 4b 5f 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a K_PAGES */../*.*
10239 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 * When this is c
1023a 61 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 alled the journa
1023b 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 l file for pager
1023c 20 70 50 61 67 65 72 20 6d 75 73 74 20 62 65 20 pPager must be
1023d 6f 70 65 6e 2e 0a 2a 2a 20 54 68 65 20 6d 61 73 open..** The mas
1023e 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
1023f 20 6e 61 6d 65 20 69 73 20 72 65 61 64 20 66 72 name is read fr
10240 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 om the end of th
10241 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 77 e file and .** w
10242 72 69 74 74 65 6e 20 69 6e 74 6f 20 6d 65 6d 6f ritten into memo
10243 72 79 20 73 75 70 70 6c 69 65 64 20 62 79 20 74 ry supplied by t
10244 68 65 20 63 61 6c 6c 65 72 2e 20 0a 2a 2a 0a 2a he caller. .**.*
10245 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 20 70 * zMaster must p
10246 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 oint to a buffer
10247 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 4d 61 of at least nMa
10248 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c 6f 63 ster bytes alloc
10249 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 ated by.** the c
1024a 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68 6f 75 aller. This shou
1024b 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f 76 66 ld be sqlite3_vf
1024c 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20 28 s.mxPathname+1 (
1024d 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72 65 20 to ensure there
1024e 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73 70 61 is.** enough spa
1024f 63 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 ce to write the
10250 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
10251 61 6d 65 29 2e 20 49 66 20 74 68 65 20 6d 61 73 ame). If the mas
10252 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 6e ter journal.** n
10253 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ame in the journ
10254 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74 68 61 al is longer tha
10255 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73 20 n nMaster bytes
10256 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a 2a 20 (including a.**
10257 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 2c nul-terminator),
10258 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 68 61 then this is ha
10259 6e 64 6c 65 64 20 61 73 20 69 66 20 6e 6f 20 6d ndled as if no m
1025a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
1025b 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 65 73 65 me.** were prese
1025c 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 nt in the journa
1025d 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 6d l..**.** If no m
1025e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1025f 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65 le name is prese
10260 6e 74 20 7a 4d 61 73 74 65 72 5b 30 5d 20 69 73 nt zMaster[0] is
10261 20 73 65 74 20 74 6f 20 30 20 61 6e 64 0a 2a 2a set to 0 and.**
10262 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 SQLITE_OK retur
10263 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
10264 6e 74 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 nt readMasterJou
10265 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c rnal(sqlite3_fil
10266 65 20 2a 70 4a 72 6e 6c 2c 20 63 68 61 72 20 2a e *pJrnl, char *
10267 7a 4d 61 73 74 65 72 2c 20 75 33 32 20 6e 4d 61 zMaster, u32 nMa
10268 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b ster){. int rc;
10269 0a 20 20 75 33 32 20 6c 65 6e 3b 0a 20 20 69 36 . u32 len;. i6
1026a 34 20 73 7a 4a 3b 0a 20 20 75 33 32 20 63 6b 73 4 szJ;. u32 cks
1026b 75 6d 3b 0a 20 20 75 33 32 20 75 3b 20 20 20 20 um;. u32 u;
1026c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1026d 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 * Unsigned loop
1026e 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 counter */. uns
1026f 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 igned char aMagi
10270 63 5b 38 5d 3b 20 2f 2a 20 41 20 62 75 66 66 65 c[8]; /* A buffe
10271 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 r to hold the ma
10272 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 0a 20 gic header */..
10273 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c zMaster[0] = '\
10274 30 27 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 0';.. rc = sqli
10275 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a te3OsFileSize(pJ
10276 72 6e 6c 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66 rnl, &szJ);. if
10277 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
10278 7c 7c 20 73 7a 4a 3c 31 36 20 29 20 72 65 74 75 || szJ<16 ) retu
10279 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72 rn rc;.. rc = r
1027a 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c ead32bits(pJrnl,
1027b 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 3b 0a szJ-16, &len);.
1027c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1027d 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b _OK ) return rc;
1027e 0a 0a 20 20 69 66 28 20 6c 65 6e 3e 3d 6e 4d 61 .. if( len>=nMa
1027f 73 74 65 72 20 29 7b 0a 20 20 20 20 72 65 74 75 ster ){. retu
10280 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
10281 7d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 }.. rc = read32
10282 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d bits(pJrnl, szJ-
10283 31 32 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 69 12, &cksum);. i
10284 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
10285 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 ) return rc;..
10286 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
10287 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 ead(pJrnl, aMagi
10288 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 3b 0a 20 20 c, 8, szJ-8);.
10289 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1028a 4b 20 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 K || memcmp(aMag
1028b 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 ic, aJournalMagi
1028c 63 2c 20 38 29 20 29 20 72 65 74 75 72 6e 20 72 c, 8) ) return r
1028d 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 c;.. rc = sqlit
1028e 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 e3OsRead(pJrnl,
1028f 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a zMaster, len, sz
10290 4a 2d 31 36 2d 6c 65 6e 29 3b 0a 20 20 69 66 28 J-16-len);. if(
10291 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
10292 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
10293 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 5b 6c . }. zMaster[l
10294 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 2f en] = '\0';.. /
10295 2a 20 53 65 65 20 69 66 20 74 68 65 20 63 68 65 * See if the che
10296 63 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74 68 cksum matches th
10297 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
10298 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 75 name */. for(u
10299 3d 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 7b =0; u<len; u++){
1029a 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a 4d . cksum -= zM
1029b 61 73 74 65 72 5b 75 5d 3b 0a 20 20 20 7d 0a 20 aster[u];. }.
1029c 20 69 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20 if( cksum ){.
1029d 20 20 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63 /* If the chec
1029e 6b 73 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64 ksum doesn't add
1029f 20 75 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72 up, then one or
102a0 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73 more of the dis
102a1 6b 20 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a k sectors. **
102a2 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
102a3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
102a4 69 6c 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75 ilename is corru
102a5 70 74 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73 pted. This means
102a6 0a 20 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65 . ** definite
102a7 6c 79 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f ly roll back, so
102a8 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c just return SQL
102a9 49 54 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72 ITE_OK and repor
102aa 74 20 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a t a (nul). **
102ab 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 master-journal
102ac 66 69 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f filename.. */
102ad 0a 20 20 20 20 7a 4d 61 73 74 65 72 5b 30 5d 20 . zMaster[0]
102ae 3d 20 27 5c 30 27 3b 0a 20 20 7d 0a 20 20 20 0a = '\0';. }. .
102af 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
102b0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 OK;.}../*.** See
102b1 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 k the journal fi
102b2 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f le descriptor to
102b3 20 74 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 the next sector
102b4 20 62 6f 75 6e 64 61 72 79 20 77 68 65 72 65 20 boundary where
102b5 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 a.** journal hea
102b6 64 65 72 20 6d 61 79 20 62 65 20 72 65 61 64 20 der may be read
102b7 6f 72 20 77 72 69 74 74 65 6e 2e 20 50 61 67 65 or written. Page
102b8 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69 73 20 r.journalOff is
102b9 75 70 64 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 updated with.**
102ba 74 68 65 20 6e 65 77 20 73 65 65 6b 20 6f 66 66 the new seek off
102bb 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 set..**.** i.e f
102bc 6f 72 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65 or a sector size
102bd 20 6f 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 49 of 512:.**.** I
102be 6e 70 75 74 20 4f 66 66 73 65 74 20 20 20 20 20 nput Offset
102bf 20 20 20 20 20 20 20 20 20 4f 75 74 70 75 74 20 Output
102c0 4f 66 66 73 65 74 0a 2a 2a 20 2d 2d 2d 2d 2d 2d Offset.** ------
102c1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
102c2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
102c3 2d 0a 2a 2a 20 30 20 20 20 20 20 20 20 20 20 20 -.** 0
102c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
102c5 0a 2a 2a 20 35 31 32 20 20 20 20 20 20 20 20 20 .** 512
102c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 51
102c7 32 0a 2a 2a 20 31 30 30 20 20 20 20 20 20 20 20 2.** 100
102c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
102c9 31 32 0a 2a 2a 20 32 30 30 30 20 20 20 20 20 20 12.** 2000
102ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
102cb 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74 2048.** .*/.stat
102cc 69 63 20 76 6f 69 64 20 73 65 65 6b 4a 6f 75 72 ic void seekJour
102cd 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 nalHdr(Pager *pP
102ce 61 67 65 72 29 7b 0a 20 20 69 36 34 20 6f 66 66 ager){. i64 off
102cf 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 34 20 63 set = 0;. i64 c
102d0 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e = pPager->journ
102d1 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63 20 29 alOff;. if( c )
102d2 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 28 {. offset = (
102d3 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f 48 44 (c-1)/JOURNAL_HD
102d4 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 31 R_SZ(pPager) + 1
102d5 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ) * JOURNAL_HDR_
102d6 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a SZ(pPager);. }.
102d7 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 assert( offset
102d8 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 %JOURNAL_HDR_SZ(
102d9 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a 20 20 pPager)==0 );.
102da 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e 3d assert( offset>=
102db 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 c );. assert( (
102dc 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 4e 41 offset-c)<JOURNA
102dd 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
102de 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f );. pPager->jo
102df 75 72 6e 61 6c 4f 66 66 20 3d 20 6f 66 66 73 65 urnalOff = offse
102e0 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 t;.}../*.** Writ
102e1 65 20 7a 65 72 6f 73 20 6f 76 65 72 20 74 68 65 e zeros over the
102e2 20 68 65 61 64 65 72 20 6f 66 20 74 68 65 20 6a header of the j
102e3 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 54 68 ournal file. Th
102e4 69 73 20 68 61 73 20 74 68 65 0a 2a 2a 20 65 66 is has the.** ef
102e5 66 65 63 74 20 6f 66 20 69 6e 76 61 6c 69 64 61 fect of invalida
102e6 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c ting the journal
102e7 20 66 69 6c 65 20 61 6e 64 20 63 6f 6d 6d 69 74 file and commit
102e8 74 69 6e 67 20 74 68 65 0a 2a 2a 20 74 72 61 6e ting the.** tran
102e9 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 saction..*/.stat
102ea 69 63 20 69 6e 74 20 7a 65 72 6f 4a 6f 75 72 6e ic int zeroJourn
102eb 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 61 alHdr(Pager *pPa
102ec 67 65 72 2c 20 69 6e 74 20 64 6f 54 72 75 6e 63 ger, int doTrunc
102ed 61 74 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ate){. int rc =
102ee 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 74 SQLITE_OK;. st
102ef 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
102f0 7a 65 72 6f 48 64 72 5b 32 38 5d 20 3d 20 7b 30 zeroHdr[28] = {0
102f1 7d 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 };.. if( pPager
102f2 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a ->journalOff ){.
102f3 20 20 20 20 69 36 34 20 69 4c 69 6d 69 74 20 3d i64 iLimit =
102f4 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
102f5 53 69 7a 65 4c 69 6d 69 74 3b 0a 0a 20 20 20 20 SizeLimit;..
102f6 49 4f 54 52 41 43 45 28 28 22 4a 5a 45 52 4f 48 IOTRACE(("JZEROH
102f7 44 52 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 DR %p\n", pPager
102f8 29 29 0a 20 20 20 20 69 66 28 20 64 6f 54 72 75 )). if( doTru
102f9 6e 63 61 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d ncate || iLimit=
102fa 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d =0 ){. rc =
102fb 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
102fc 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
102fd 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0);. }else{.
102fe 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
102ff 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
10300 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73 >jfd, zeroHdr, s
10301 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20 izeof(zeroHdr),
10302 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0);. }. if
10303 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
10304 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 && !pPager->noSy
10305 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d nc ){. rc =
10306 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
10307 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 Pager->jfd, SQLI
10308 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
10309 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c |pPager->sync_fl
1030a 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ags);. }..
1030b 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e /* At this poin
1030c 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
1030d 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 n is committed b
1030e 75 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 ut the write loc
1030f 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 k . ** is sti
10310 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 ll held on the f
10311 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 ile. If there is
10312 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f a size limit co
10313 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 20 nfigured for .
10314 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 ** the persist
10315 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 ent journal and
10316 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
10317 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75 currently consu
10318 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 mes more. **
10319 73 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 20 space than that
1031a 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72 limit allows for
1031b 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e 6f , truncate it no
1031c 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e w. There is no n
1031d 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79 eed. ** to sy
1031e 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c nc the file foll
1031f 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 owing this opera
10320 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tion.. */.
10321 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
10322 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 29 OK && iLimit>0 )
10323 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b 0a {. i64 sz;.
10324 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
10325 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 e3OsFileSize(pPa
10326 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a ger->jfd, &sz);.
10327 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
10328 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c LITE_OK && sz>iL
10329 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 imit ){.
1032a 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 rc = sqlite3OsTr
1032b 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a uncate(pPager->j
1032c 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 fd, iLimit);.
1032d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1032e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1032f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c *.** The journal
10330 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 file must be op
10331 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 en when this rou
10332 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 tine is called.
10333 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 A journal.** hea
10334 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 der (JOURNAL_HDR
10335 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 72 _SZ bytes) is wr
10336 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
10337 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 74 ournal file at t
10338 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f he.** current lo
10339 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 cation..**.** Th
1033a 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 65 e format for the
1033b 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
1033c 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
1033d 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 67 * - 8 bytes: Mag
1033e 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 6a ic identifying j
1033f 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a ournal format..*
10340 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 6d * - 4 bytes: Num
10341 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69 ber of records i
10342 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 n journal, or -1
10343 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73 no-sync mode is
10344 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 on..** - 4 byte
10345 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 s: Random number
10346 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 68 used for page h
10347 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 ash..** - 4 byte
10348 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 62 s: Initial datab
10349 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a ase page count..
1034a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 65 ** - 4 bytes: Se
1034b 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 62 ctor size used b
1034c 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 y the process th
1034d 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a 6f at wrote this jo
1034e 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 urnal..** - 4 by
1034f 74 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 61 tes: Database pa
10350 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 ge size..** .**
10351 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 Followed by (JOU
10352 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 RNAL_HDR_SZ - 28
10353 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65 ) bytes of unuse
10354 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 d space..*/.stat
10355 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 ic int writeJour
10356 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 nalHdr(Pager *pP
10357 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 ager){. int rc
10358 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 = SQLITE_OK;. c
10359 68 61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20 70 har *zHeader = p
1035a 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
1035b 3b 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72 20 ;. u32 nHeader
1035c 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
1035d 7a 65 3b 0a 20 20 75 33 32 20 6e 57 72 69 74 65 ze;. u32 nWrite
1035e 3b 0a 0a 20 20 69 66 28 20 6e 48 65 61 64 65 72 ;.. if( nHeader
1035f 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 >JOURNAL_HDR_SZ(
10360 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 6e pPager) ){. n
10361 48 65 61 64 65 72 20 3d 20 4a 4f 55 52 4e 41 4c Header = JOURNAL
10362 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b _HDR_SZ(pPager);
10363 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 . }.. if( pPag
10364 65 72 2d 3e 73 74 6d 74 48 64 72 4f 66 66 3d 3d er->stmtHdrOff==
10365 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 0 ){. pPager-
10366 3e 73 74 6d 74 48 64 72 4f 66 66 20 3d 20 70 50 >stmtHdrOff = pP
10367 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
10368 3b 0a 20 20 7d 0a 0a 20 20 73 65 65 6b 4a 6f 75 ;. }.. seekJou
10369 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b rnalHdr(pPager);
1036a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e . pPager->journ
1036b 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d 3e alHdr = pPager->
1036c 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 6d journalOff;.. m
1036d 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 emcpy(zHeader, a
1036e 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 JournalMagic, si
1036f 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
10370 69 63 29 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a ic));.. /* . *
10371 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65 63 * Write the nRec
10372 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e 75 6d Field - the num
10373 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 63 6f ber of page reco
10374 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 rds that follow
10375 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 this. ** journa
10376 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d 61 6c l header. Normal
10377 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72 69 74 ly, zero is writ
10378 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61 6c 75 ten to this valu
10379 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a e at this time..
1037a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65 20 72 ** After the r
1037b 65 63 6f 72 64 73 20 61 72 65 20 61 64 64 65 64 ecords are added
1037c 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
1037d 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c (and the journal
1037e 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a 20 69 synced, . ** i
1037f 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d f in full-sync m
10380 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f 20 69 ode), the zero i
10381 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 s overwritten wi
10382 74 68 20 74 68 65 20 74 72 75 65 20 6e 75 6d 62 th the true numb
10383 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63 6f 72 er. ** of recor
10384 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f 75 72 ds (see syncJour
10385 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 20 2a nal()).. **. *
10386 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74 65 72 * A faster alter
10387 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77 72 69 native is to wri
10388 74 65 20 30 78 46 46 46 46 46 46 46 46 20 74 6f te 0xFFFFFFFF to
10389 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 2e the nRec field.
1038a 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61 64 69 When. ** readi
1038b 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 ng the journal t
1038c 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c 73 20 his value tells
1038d 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75 6d 65 SQLite to assume
1038e 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 72 that the. ** r
1038f 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e est of the journ
10390 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 al file contains
10391 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f valid page reco
10392 72 64 73 2e 20 54 68 69 73 20 61 73 73 75 6d 70 rds. This assump
10393 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 61 6e tion. ** is dan
10394 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20 61 20 gerous, as if a
10395 66 61 69 6c 75 72 65 20 6f 63 63 75 72 65 64 20 failure occured
10396 77 68 69 6c 73 74 20 77 72 69 74 69 6e 67 20 74 whilst writing t
10397 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 o the journal.
10398 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61 79 20 63 ** file it may c
10399 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67 61 72 62 ontain some garb
1039a 61 67 65 20 64 61 74 61 2e 20 54 68 65 72 65 20 age data. There
1039b 61 72 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f are two scenario
1039c 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 74 68 69 s. ** where thi
1039d 73 20 72 69 73 6b 20 63 61 6e 20 62 65 20 69 67 s risk can be ig
1039e 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a nored:. **. **
1039f 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20 70 61 * When the pa
103a0 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e ger is in no-syn
103a1 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75 70 74 69 c mode. Corrupti
103a2 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 20 61 0a on can follow a.
103a3 20 20 2a 2a 20 20 20 20 20 70 6f 77 65 72 20 66 ** power f
103a4 61 69 6c 75 72 65 20 69 6e 20 74 68 69 73 20 63 ailure in this c
103a5 61 73 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2a ase anyway.. **
103a6 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 . ** * When t
103a7 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f he SQLITE_IOCAP_
103a8 53 41 46 45 5f 41 50 50 45 4e 44 20 66 6c 61 67 SAFE_APPEND flag
103a9 20 69 73 20 73 65 74 2e 20 54 68 69 73 20 67 75 is set. This gu
103aa 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a 20 20 20 arantees. **
103ab 20 20 74 68 61 74 20 67 61 72 62 61 67 65 20 64 that garbage d
103ac 61 74 61 20 69 73 20 6e 65 76 65 72 20 61 70 70 ata is never app
103ad 65 6e 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 ended to the jou
103ae 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a rnal file.. */.
103af 20 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d assert(pPager-
103b0 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 >fd->pMethods||p
103b1 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 3b 0a Pager->noSync);.
103b2 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6e if( (pPager->n
103b3 6f 53 79 6e 63 29 20 7c 7c 20 28 70 50 61 67 65 oSync) || (pPage
103b4 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
103b5 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
103b6 45 5f 4d 45 4d 4f 52 59 29 0a 20 20 20 7c 7c 20 E_MEMORY). ||
103b7 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 (sqlite3OsDevice
103b8 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
103b9 70 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 pPager->fd)&SQLI
103ba 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 TE_IOCAP_SAFE_AP
103bb 50 45 4e 44 29 20 0a 20 20 29 7b 0a 20 20 20 20 PEND) . ){.
103bc 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
103bd 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
103be 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66 66 alMagic)], 0xfff
103bf 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65 7b fffff);. }else{
103c0 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26 . put32bits(&
103c1 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
103c2 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 JournalMagic)],
103c3 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 0);. }.. /* Th
103c4 65 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 e random check-h
103c5 61 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20 ash initialiser
103c6 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 */ . sqlite3_ra
103c7 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 ndomness(sizeof(
103c8 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
103c9 74 29 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 t), &pPager->cks
103ca 75 6d 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 umInit);. put32
103cb 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 bits(&zHeader[si
103cc 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
103cd 69 63 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e ic)+4], pPager->
103ce 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a cksumInit);. /*
103cf 20 54 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 The initial dat
103d0 61 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 abase size */.
103d1 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
103d2 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
103d3 61 6c 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 alMagic)+8], pPa
103d4 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 ger->dbSize);.
103d5 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 64 20 73 /* The assumed s
103d6 65 63 74 6f 72 20 73 69 7a 65 20 66 6f 72 20 74 ector size for t
103d7 68 69 73 20 70 72 6f 63 65 73 73 20 2a 2f 0a 20 his process */.
103d8 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 put32bits(&zHea
103d9 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 der[sizeof(aJour
103da 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d 2c 20 70 nalMagic)+12], p
103db 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
103dc 65 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 e);. if( pPager
103dd 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 3d 3d 30 20 ->journalHdr==0
103de 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 ){. /* The pa
103df 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70 ge size */. p
103e0 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 ut32bits(&zHeade
103e1 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
103e2 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 61 lMagic)+16], pPa
103e3 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a ger->pageSize);.
103e4 20 20 7d 0a 0a 20 20 66 6f 72 28 6e 57 72 69 74 }.. for(nWrit
103e5 65 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f e=0; rc==SQLITE_
103e6 4f 4b 26 26 6e 57 72 69 74 65 3c 4a 4f 55 52 4e OK&&nWrite<JOURN
103e7 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
103e8 29 3b 20 6e 57 72 69 74 65 2b 3d 6e 48 65 61 64 ); nWrite+=nHead
103e9 65 72 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 er){. IOTRACE
103ea 28 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 (("JHDR %p %lld
103eb 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
103ec 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
103ed 72 2c 20 6e 48 65 61 64 65 72 29 29 0a 20 20 20 r, nHeader)).
103ee 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 rc = sqlite3OsW
103ef 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 rite(pPager->jfd
103f0 2c 20 7a 48 65 61 64 65 72 2c 20 6e 48 65 61 64 , zHeader, nHead
103f1 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 er, pPager->jour
103f2 6e 61 6c 4f 66 66 29 3b 0a 20 20 20 20 70 50 61 nalOff);. pPa
103f3 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
103f4 2b 3d 20 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a += nHeader;. }.
103f5 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
103f6 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e ./*.** The journ
103f7 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 al file must be
103f8 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 69 open when this i
103f9 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 s called. A jour
103fa 6e 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 0a nal header file.
103fb 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ** (JOURNAL_HDR_
103fc 53 5a 20 62 79 74 65 73 29 20 69 73 20 72 65 61 SZ bytes) is rea
103fd 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 d from the curre
103fe 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74 nt location in t
103ff 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 he journal.** fi
10400 6c 65 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 le. See comments
10401 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 above function
10402 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
10403 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 ) for a descript
10404 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 6a 6f ion of.** the jo
10405 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f 72 urnal header for
10406 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 mat..**.** If th
10407 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61 64 e header is read
10408 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 2a successfully, *
10409 6e 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 74 nRec is set to t
1040a 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
1040b 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 6f 6c page records fol
1040c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 61 64 lowing this head
1040d 65 72 20 61 6e 64 20 2a 64 62 53 69 7a 65 20 69 er and *dbSize i
1040e 73 20 73 65 74 20 74 6f 20 74 68 65 20 73 69 7a s set to the siz
1040f 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 e of the.** data
10410 62 61 73 65 20 62 65 66 6f 72 65 20 74 68 65 20 base before the
10411 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 67 61 transaction bega
10412 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41 6c 73 n, in pages. Als
10413 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d o, pPager->cksum
10414 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74 20 74 Init.** is set t
10415 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 o the value read
10416 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
10417 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49 54 45 l header. SQLITE
10418 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0a _OK is returned.
10419 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ** in this case.
1041a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f .**.** If the jo
1041b 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69 6c urnal header fil
1041c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 e appears to be
1041d 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54 corrupted, SQLIT
1041e 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72 65 74 E_DONE is.** ret
1041f 75 72 6e 65 64 20 61 6e 64 20 2a 6e 52 65 63 20 urned and *nRec
10420 61 6e 64 20 2a 64 62 53 69 7a 65 20 61 72 65 20 and *dbSize are
10421 6e 6f 74 20 73 65 74 2e 20 20 49 66 20 4a 4f 55 not set. If JOU
10422 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 RNAL_HDR_SZ byte
10423 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 s.** cannot be r
10424 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 ead from the jou
10425 72 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 rnal file an err
10426 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
10427 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
10428 6e 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 nt readJournalHd
10429 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 r(. Pager *pPag
1042a 65 72 2c 20 0a 20 20 69 36 34 20 6a 6f 75 72 6e er, . i64 journ
1042b 61 6c 53 69 7a 65 2c 0a 20 20 75 33 32 20 2a 70 alSize,. u32 *p
1042c 4e 52 65 63 2c 20 0a 20 20 75 33 32 20 2a 70 44 NRec, . u32 *pD
1042d 62 53 69 7a 65 0a 29 7b 0a 20 20 69 6e 74 20 72 bSize.){. int r
1042e 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 c;. unsigned ch
1042f 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 2f 2a ar aMagic[8]; /*
10430 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c A buffer to hol
10431 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 d the magic head
10432 65 72 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e 6c er */. i64 jrnl
10433 4f 66 66 3b 0a 20 20 69 6e 74 20 69 50 61 67 65 Off;. int iPage
10434 53 69 7a 65 3b 0a 0a 20 20 73 65 65 6b 4a 6f 75 Size;.. seekJou
10435 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b rnalHdr(pPager);
10436 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a . if( pPager->j
10437 6f 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 ournalOff+JOURNA
10438 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
10439 20 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29 > journalSize )
1043a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1043b 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 ITE_DONE;. }.
1043c 6a 72 6e 6c 4f 66 66 20 3d 20 70 50 61 67 65 72 jrnlOff = pPager
1043d 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 ->journalOff;..
1043e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
1043f 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ead(pPager->jfd,
10440 20 61 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 aMagic, sizeof(
10441 61 4d 61 67 69 63 29 2c 20 6a 72 6e 6c 4f 66 66 aMagic), jrnlOff
10442 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
10443 74 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c 4f turn rc;. jrnlO
10444 66 66 20 2b 3d 20 73 69 7a 65 6f 66 28 61 4d 61 ff += sizeof(aMa
10445 67 69 63 29 3b 0a 0a 20 20 69 66 28 20 6d 65 6d gic);.. if( mem
10446 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 cmp(aMagic, aJou
10447 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f rnalMagic, sizeo
10448 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 29 7b f(aMagic))!=0 ){
10449 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1044a 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 0a 20 20 TE_DONE;. }..
1044b 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
1044c 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e pPager->jfd, jrn
1044d 6c 4f 66 66 2c 20 70 4e 52 65 63 29 3b 0a 20 20 lOff, pNRec);.
1044e 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
1044f 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 rc;.. rc = read
10450 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
10451 66 64 2c 20 6a 72 6e 6c 4f 66 66 2b 34 2c 20 26 fd, jrnlOff+4, &
10452 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
10453 74 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 t);. if( rc ) r
10454 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 eturn rc;.. rc
10455 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 = read32bits(pPa
10456 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 ger->jfd, jrnlOf
10457 66 2b 38 2c 20 70 44 62 53 69 7a 65 29 3b 0a 20 f+8, pDbSize);.
10458 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
10459 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61 rc;.. rc = rea
1045a 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e d32bits(pPager->
1045b 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 2b 31 36 2c jfd, jrnlOff+16,
1045c 20 28 75 33 32 20 2a 29 26 69 50 61 67 65 53 69 (u32 *)&iPageSi
1045d 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ze);. if( rc==S
1045e 51 4c 49 54 45 5f 4f 4b 20 0a 20 20 20 26 26 20 QLITE_OK . &&
1045f 69 50 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 0a iPageSize>=512 .
10460 20 20 20 26 26 20 69 50 61 67 65 53 69 7a 65 3c && iPageSize<
10461 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 =SQLITE_MAX_PAGE
10462 5f 53 49 5a 45 20 0a 20 20 20 26 26 20 28 28 69 _SIZE . && ((i
10463 50 61 67 65 53 69 7a 65 2d 31 29 26 69 50 61 67 PageSize-1)&iPag
10464 65 53 69 7a 65 29 3d 3d 30 20 0a 20 20 29 7b 0a eSize)==0 . ){.
10465 20 20 20 20 75 31 36 20 70 61 67 65 73 69 7a 65 u16 pagesize
10466 20 3d 20 69 50 61 67 65 53 69 7a 65 3b 0a 20 20 = iPageSize;.
10467 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
10468 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 gerSetPagesize(p
10469 50 61 67 65 72 2c 20 26 70 61 67 65 73 69 7a 65 Pager, &pagesize
1046a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 );. }. if( rc
1046b 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 ) return rc;..
1046c 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 73 /* Update the as
1046d 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 7a sumed sector-siz
1046e 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 e to match the v
1046f 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 20 alue used by .
10470 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 ** the process t
10471 68 61 74 20 63 72 65 61 74 65 64 20 74 68 69 73 hat created this
10472 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 69 journal. If thi
10473 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 s journal was.
10474 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 20 ** created by a
10475 70 72 6f 63 65 73 73 20 6f 74 68 65 72 20 74 68 process other th
10476 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 an this one, the
10477 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 20 n this routine.
10478 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61 6c ** is being cal
10479 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 led from within
1047a 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 pager_playback()
1047b 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 . The local valu
1047c 65 0a 20 20 2a 2a 20 6f 66 20 50 61 67 65 72 2e e. ** of Pager.
1047d 73 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72 65 sectorSize is re
1047e 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 6e stored at the en
1047f 64 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69 6e d of that routin
10480 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 72 e.. */. rc = r
10481 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 ead32bits(pPager
10482 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 2b 31 ->jfd, jrnlOff+1
10483 32 2c 20 26 70 50 61 67 65 72 2d 3e 73 65 63 74 2, &pPager->sect
10484 6f 72 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 orSize);. if( r
10485 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
10486 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 73 65 if( (pPager->se
10487 63 74 6f 72 53 69 7a 65 20 26 20 28 70 50 61 67 ctorSize & (pPag
10488 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 2d 31 er->sectorSize-1
10489 29 29 21 3d 30 0a 20 20 20 20 20 20 20 20 20 7c ))!=0. |
1048a 7c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 | pPager->sector
1048b 53 69 7a 65 3e 30 78 31 30 30 30 30 30 30 20 29 Size>0x1000000 )
1048c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1048d 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 0a 20 ITE_DONE;. }..
1048e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1048f 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 Off += JOURNAL_H
10490 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 DR_SZ(pPager);.
10491 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
10492 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 K;.}.../*.** Wri
10493 74 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 te the supplied
10494 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
10495 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ame into the jou
10496 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 rnal file for pa
10497 67 65 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 ger.** pPager at
10498 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 the current loc
10499 61 74 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 ation. The maste
1049a 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d r journal name m
1049b 75 73 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a ust be the last.
1049c 2a 2a 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e ** thing written
1049d 20 74 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 to a journal fi
1049e 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 le. If the pager
1049f 20 69 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 is in full-sync
104a0 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f mode, the.** jo
104a1 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 urnal file descr
104a2 69 70 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 iptor is advance
104a3 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 d to the next se
104a4 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 ctor boundary be
104a5 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 fore.** anything
104a6 20 69 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 is written. The
104a7 20 66 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a format is:.**.*
104a8 2a 20 2b 20 34 20 62 79 74 65 73 3a 20 50 41 47 * + 4 bytes: PAG
104a9 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 2b ER_MJ_PGNO..** +
104aa 20 4e 20 62 79 74 65 73 3a 20 6c 65 6e 67 74 68 N bytes: length
104ab 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e of master journ
104ac 61 6c 20 6e 61 6d 65 2e 0a 2a 2a 20 2b 20 34 20 al name..** + 4
104ad 62 79 74 65 73 3a 20 4e 0a 2a 2a 20 2b 20 34 20 bytes: N.** + 4
104ae 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f bytes: Master jo
104af 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b urnal name check
104b0 73 75 6d 2e 0a 2a 2a 20 2b 20 38 20 62 79 74 65 sum..** + 8 byte
104b1 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 s: aJournalMagic
104b2 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 []..**.** The ma
104b3 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 ster journal pag
104b4 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68 e checksum is th
104b5 65 20 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74 e sum of the byt
104b6 65 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 es in the master
104b7 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 .** journal name
104b8 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 ..**.** If zMast
104b9 65 72 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 er is a NULL poi
104ba 6e 74 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72 nter (occurs for
104bb 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 a single databa
104bc 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c se transaction),
104bd 20 0a 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 .** this call i
104be 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 s a no-op..*/.st
104bf 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 4d 61 atic int writeMa
104c0 73 74 65 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 sterJournal(Page
104c1 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 r *pPager, const
104c2 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b char *zMaster){
104c3 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 . int rc;. int
104c4 20 6c 65 6e 3b 20 0a 20 20 69 6e 74 20 69 3b 20 len; . int i;
104c5 0a 20 20 69 36 34 20 6a 72 6e 6c 4f 66 66 3b 0a . i64 jrnlOff;.
104c6 20 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 0a i64 jrnlSize;.
104c7 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b u32 cksum = 0;
104c8 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 73 69 7a . char zBuf[siz
104c9 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
104ca 63 29 2b 32 2a 34 5d 3b 0a 0a 20 20 69 66 28 20 c)+2*4];.. if(
104cb 21 7a 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 67 !zMaster || pPag
104cc 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 20 er->setMaster )
104cd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
104ce 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
104cf 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
104d0 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
104d1 45 4d 4f 52 59 20 29 20 72 65 74 75 72 6e 20 53 EMORY ) return S
104d2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 QLITE_OK;. pPag
104d3 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 er->setMaster =
104d4 31 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 1;.. len = strl
104d5 65 6e 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20 66 en(zMaster);. f
104d6 6f 72 28 69 3d 30 3b 20 69 3c 6c 65 6e 3b 20 69 or(i=0; i<len; i
104d7 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b ++){. cksum +
104d8 3d 20 7a 4d 61 73 74 65 72 5b 69 5d 3b 0a 20 20 = zMaster[i];.
104d9 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 75 }.. /* If in fu
104da 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64 ll-sync mode, ad
104db 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 vance to the nex
104dc 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20 62 65 t disk sector be
104dd 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 20 2a fore writing. *
104de 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 * the master jou
104df 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20 rnal name. This
104e0 69 73 20 69 6e 20 63 61 73 65 20 74 68 65 20 70 is in case the p
104e1 72 65 76 69 6f 75 73 20 70 61 67 65 20 77 72 69 revious page wri
104e2 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65 tten to. ** the
104e3 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c 72 journal has alr
104e4 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 eady been synced
104e5 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 .. */. if( pPa
104e6 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b ger->fullSync ){
104e7 0a 20 20 20 20 73 65 65 6b 4a 6f 75 72 6e 61 6c . seekJournal
104e8 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 7d Hdr(pPager);. }
104e9 0a 20 20 6a 72 6e 6c 4f 66 66 20 3d 20 70 50 61 . jrnlOff = pPa
104ea 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
104eb 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e . pPager->journ
104ec 61 6c 4f 66 66 20 2b 3d 20 28 6c 65 6e 2b 32 30 alOff += (len+20
104ed 29 3b 0a 0a 20 20 72 63 20 3d 20 77 72 69 74 65 );.. rc = write
104ee 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
104ef 66 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 50 41 47 fd, jrnlOff, PAG
104f0 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
104f1 72 29 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 r));. if( rc!=S
104f2 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
104f3 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c 4f 66 66 20 n rc;. jrnlOff
104f4 2b 3d 20 34 3b 0a 0a 20 20 72 63 20 3d 20 73 71 += 4;.. rc = sq
104f5 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
104f6 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 ger->jfd, zMaste
104f7 72 2c 20 6c 65 6e 2c 20 6a 72 6e 6c 4f 66 66 29 r, len, jrnlOff)
104f8 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
104f9 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
104fa 63 3b 0a 20 20 6a 72 6e 6c 4f 66 66 20 2b 3d 20 c;. jrnlOff +=
104fb 6c 65 6e 3b 0a 0a 20 20 70 75 74 33 32 62 69 74 len;.. put32bit
104fc 73 28 7a 42 75 66 2c 20 6c 65 6e 29 3b 0a 20 20 s(zBuf, len);.
104fd 70 75 74 33 32 62 69 74 73 28 26 7a 42 75 66 5b put32bits(&zBuf[
104fe 34 5d 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 6d 65 4], cksum);. me
104ff 6d 63 70 79 28 26 7a 42 75 66 5b 38 5d 2c 20 61 mcpy(&zBuf[8], a
10500 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 JournalMagic, si
10501 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
10502 69 63 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c ic));. rc = sql
10503 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
10504 65 72 2d 3e 6a 66 64 2c 20 7a 42 75 66 2c 20 38 er->jfd, zBuf, 8
10505 2b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c +sizeof(aJournal
10506 4d 61 67 69 63 29 2c 20 6a 72 6e 6c 4f 66 66 29 Magic), jrnlOff)
10507 3b 0a 20 20 6a 72 6e 6c 4f 66 66 20 2b 3d 20 38 ;. jrnlOff += 8
10508 2b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c +sizeof(aJournal
10509 4d 61 67 69 63 29 3b 0a 20 20 70 50 61 67 65 72 Magic);. pPager
1050a 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50 ->needSync = !pP
1050b 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 0a 20 ager->noSync;..
1050c 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 /* If the pager
1050d 20 69 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74 is in peristent
1050e 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74 -journal mode, t
1050f 68 65 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c hen the physical
10510 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66 . ** journal-f
10511 69 6c 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70 ile may extend p
10512 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 ast the end of t
10513 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 he master-journa
10514 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 l name. ** and
10515 38 20 62 79 74 65 73 20 6f 66 20 6d 61 67 69 63 8 bytes of magic
10516 20 64 61 74 61 20 6a 75 73 74 20 77 72 69 74 74 data just writt
10517 65 6e 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 en to the file.
10518 54 68 69 73 20 69 73 20 0a 20 20 2a 2a 20 64 61 This is . ** da
10519 6e 67 65 72 6f 75 73 20 62 65 63 61 75 73 65 20 ngerous because
1051a 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f 6c 6c the code to roll
1051b 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e back a hot-journ
1051c 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 77 69 6c al file. ** wil
1051d 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f l not be able to
1051e 20 66 69 6e 64 20 74 68 65 20 6d 61 73 74 65 72 find the master
1051f 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f -journal name to
10520 20 64 65 74 65 72 6d 69 6e 65 20 0a 20 20 2a 2a determine . **
10521 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
10522 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 the journal is h
10523 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 ot. . **. ** E
10524 61 73 69 65 73 74 20 74 68 69 6e 67 20 74 6f 20 asiest thing to
10525 64 6f 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61 do in this scena
10526 72 69 6f 20 69 73 20 74 6f 20 74 72 75 6e 63 61 rio is to trunca
10527 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a te the journal .
10528 20 20 2a 2a 20 66 69 6c 65 20 74 6f 20 74 68 65 ** file to the
10529 20 72 65 71 75 69 72 65 64 20 73 69 7a 65 2e 0a required size..
1052a 20 20 2a 2f 20 0a 20 20 69 66 28 20 28 72 63 3d */ . if( (rc=
1052b 3d 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 26 =SQLITE_OK). &
1052c 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f & (rc = sqlite3O
1052d 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 sFileSize(pPager
1052e 2d 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 ->jfd, &jrnlSize
1052f 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 ))==SQLITE_OK.
10530 20 26 26 20 6a 72 6e 6c 53 69 7a 65 3e 6a 72 6e && jrnlSize>jrn
10531 6c 4f 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 lOff. ){. rc
10532 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e = sqlite3OsTrun
10533 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 cate(pPager->jfd
10534 2c 20 6a 72 6e 6c 4f 66 66 29 3b 0a 20 20 7d 0a , jrnlOff);. }.
10535 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
10536 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67 /*.** Find a pag
10537 65 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 e in the hash ta
10538 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20 70 61 ble given its pa
10539 67 65 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 ge number. Retu
1053a 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 rn.** a pointer
1053b 74 6f 20 74 68 65 20 70 61 67 65 20 6f 72 20 4e to the page or N
1053c 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 ULL if not found
1053d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 ..*/.static PgHd
1053e 72 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 r *pager_lookup(
1053f 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
10540 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 gno pgno){. PgH
10541 64 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 dr *p;. sqlite3
10542 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 PcacheFetch(pPag
10543 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e er->pPCache, pgn
10544 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72 65 74 o, 0, &p);. ret
10545 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
10546 43 6c 65 61 72 20 74 68 65 20 69 6e 2d 6d 65 6d Clear the in-mem
10547 6f 72 79 20 63 61 63 68 65 2e 20 20 54 68 69 73 ory cache. This
10548 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 65 74 73 routine.** sets
10549 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 the state of th
1054a 65 20 70 61 67 65 72 20 62 61 63 6b 20 74 6f 20 e pager back to
1054b 77 68 61 74 20 69 74 20 77 61 73 20 77 68 65 6e what it was when
1054c 20 69 74 20 77 61 73 20 66 69 72 73 74 0a 2a 2a it was first.**
1054d 20 6f 70 65 6e 65 64 2e 20 20 41 6e 79 20 6f 75 opened. Any ou
1054e 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 tstanding pages
1054f 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 are invalidated
10550 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 61 and subsequent a
10551 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 61 63 ttempts.** to ac
10552 63 65 73 73 20 74 68 6f 73 65 20 70 61 67 65 73 cess those pages
10553 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 73 will likely res
10554 75 6c 74 20 69 6e 20 61 20 63 6f 72 65 64 75 6d ult in a coredum
10555 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 p..*/.static voi
10556 64 20 70 61 67 65 72 5f 72 65 73 65 74 28 50 61 d pager_reset(Pa
10557 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
10558 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 if( pPager->errC
10559 6f 64 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ode ) return;.
1055a 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
1055b 61 72 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ar(pPager->pPCac
1055c 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e he);.}../*.** Un
1055d 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 lock the databas
1055e 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 e file. .**.** I
1055f 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63 f the pager is c
10560 75 72 72 65 6e 74 6c 79 20 69 6e 20 65 72 72 6f urrently in erro
10561 72 20 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 r state, discard
10562 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
10563 20 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 61 .** the cache a
10564 6e 64 20 72 65 73 65 74 20 74 68 65 20 50 61 67 nd reset the Pag
10565 65 72 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 er structure int
10566 65 72 6e 61 6c 20 73 74 61 74 65 2e 20 49 66 20 ernal state. If
10567 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 6f there is.** an o
10568 70 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 pen journal-file
10569 2c 20 74 68 65 6e 20 74 68 65 20 6e 65 78 74 20 , then the next
1056a 74 69 6d 65 20 61 20 73 68 61 72 65 64 2d 6c 6f time a shared-lo
1056b 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a ck is obtained.*
1056c 2a 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 66 * on the pager f
1056d 69 6c 65 20 28 62 79 20 74 68 69 73 20 6f 72 20 ile (by this or
1056e 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 any other proces
1056f 73 29 2c 20 69 74 20 77 69 6c 6c 20 62 65 0a 2a s), it will be.*
10570 2a 20 74 72 65 61 74 65 64 20 61 73 20 61 20 68 * treated as a h
10571 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 72 ot-journal and r
10572 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73 olled back..*/.s
10573 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 tatic void pager
10574 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 _unlock(Pager *p
10575 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 21 70 Pager){. if( !p
10576 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
10577 4d 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 Mode ){. int
10578 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 rc = osUnlock(pP
10579 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 ager->fd, NO_LOC
1057a 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 K);. if( rc )
1057b 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
1057c 20 3d 20 72 63 3b 0a 20 20 20 20 70 50 61 67 65 = rc;. pPage
1057d 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d r->dbSizeValid =
1057e 20 30 3b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 0;. IOTRACE(
1057f 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 ("UNLOCK %p\n",
10580 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f 2a pPager)).. /*
10581 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74 68 Always close th
10582 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 e journal file w
10583 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68 65 hen dropping the
10584 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e 0a database lock..
10585 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 ** Otherwise
10586 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 , another connec
10587 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e 61 tion with journa
10588 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d 69 l_mode=delete mi
10589 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 ght. ** delet
1058a 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20 66 e the file out f
1058b 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20 20 rom under us..
1058c 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 */. if( pPa
1058d 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e ger->journalOpen
1058e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1058f 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
10590 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 50 61 >jfd);. pPa
10591 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e ger->journalOpen
10592 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 = 0;. sqli
10593 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
10594 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
10595 6e 61 6c 29 3b 0a 20 20 20 20 20 20 70 50 61 67 nal);. pPag
10596 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d er->pInJournal =
10597 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 0;. sqlite
10598 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
10599 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 6f Pager->pAlwaysRo
1059a 6c 6c 62 61 63 6b 29 3b 0a 20 20 20 20 20 20 70 llback);. p
1059b 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 6f Pager->pAlwaysRo
1059c 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 20 20 llback = 0;.
1059d 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 50 61 67 }.. /* If Pag
1059e 65 72 2e 65 72 72 43 6f 64 65 20 69 73 20 73 65 er.errCode is se
1059f 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 t, the contents
105a0 6f 66 20 74 68 65 20 70 61 67 65 72 20 63 61 63 of the pager cac
105a1 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a 20 20 20 he cannot be.
105a2 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 4e 6f 77 ** trusted. Now
105a3 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 that the pager
105a4 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 file is unlocked
105a5 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f , the contents o
105a6 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 63 f the. ** cac
105a7 68 65 20 63 61 6e 20 62 65 20 64 69 73 63 61 72 he can be discar
105a8 64 65 64 20 61 6e 64 20 74 68 65 20 65 72 72 6f ded and the erro
105a9 72 20 63 6f 64 65 20 73 61 66 65 6c 79 20 63 6c r code safely cl
105aa 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 eared.. */.
105ab 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 if( pPager->er
105ac 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 69 rCode ){. i
105ad 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
105ae 20 29 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f ) pPager->errCo
105af 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a de = SQLITE_OK;.
105b0 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 pager_rese
105b1 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 t(pPager);.
105b2 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 6d if( pPager->stm
105b3 74 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 tOpen ){.
105b4 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
105b5 70 50 61 67 65 72 2d 3e 73 74 66 64 29 3b 0a 20 pPager->stfd);.
105b6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 sqlite3Bi
105b7 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 tvecDestroy(pPag
105b8 65 72 2d 3e 70 49 6e 53 74 6d 74 29 3b 0a 20 20 er->pInStmt);.
105b9 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 pPager->pI
105ba 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 nStmt = 0;.
105bb 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d }. pPager-
105bc 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 0a 20 >stmtOpen = 0;.
105bd 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d pPager->stm
105be 74 49 6e 55 73 65 20 3d 20 30 3b 0a 20 20 20 20 tInUse = 0;.
105bf 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
105c0 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 lOff = 0;.
105c1 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 pPager->journalS
105c2 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 tarted = 0;.
105c3 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 75 pPager->stmtAu
105c4 74 6f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 20 toopen = 0;.
105c5 20 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 pPager->origDb
105c6 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a Size = 0;. }.
105c7 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 . pPager->sta
105c8 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 te = PAGER_UNLOC
105c9 4b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 K;. pPager->c
105ca 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d hangeCountDone =
105cb 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0;. }.}../*.**
105cc 20 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c 62 Execute a rollb
105cd 61 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 63 ack if a transac
105ce 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 tion is active a
105cf 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a nd unlock the .*
105d0 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e * database file.
105d1 20 49 66 20 74 68 65 20 70 61 67 65 72 20 68 61 If the pager ha
105d2 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 s already entere
105d3 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 d the error stat
105d4 65 2c 20 0a 2a 2a 20 64 6f 20 6e 6f 74 20 61 74 e, .** do not at
105d5 74 65 6d 70 74 20 74 68 65 20 72 6f 6c 6c 62 61 tempt the rollba
105d6 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ck..*/.static vo
105d7 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e id pagerUnlockAn
105d8 64 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 dRollback(Pager
105d9 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 65 72 *p){. if( p->er
105da 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b rCode==SQLITE_OK
105db 20 26 26 20 70 2d 3e 73 74 61 74 65 3e 3d 50 41 && p->state>=PA
105dc 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a GER_RESERVED ){.
105dd 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e sqlite3Begin
105de 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
105df 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
105e0 52 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 20 Rollback(p);.
105e1 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
105e2 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20 nMalloc();. }.
105e3 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 29 pager_unlock(p)
105e4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
105e5 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 routine ends a t
105e6 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 41 20 74 ransaction. A t
105e7 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 65 6e ransaction is en
105e8 64 65 64 20 62 79 20 65 69 74 68 65 72 0a 2a 2a ded by either.**
105e9 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 61 20 52 a COMMIT or a R
105ea 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 20 57 OLLBACK..**.** W
105eb 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
105ec 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 is called, the
105ed 70 61 67 65 72 20 68 61 73 20 74 68 65 20 6a 6f pager has the jo
105ee 75 72 6e 61 6c 20 66 69 6c 65 20 6f 70 65 6e 20 urnal file open
105ef 61 6e 64 0a 2a 2a 20 61 20 52 45 53 45 52 56 45 and.** a RESERVE
105f0 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c D or EXCLUSIVE l
105f1 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
105f2 61 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ase. This routi
105f3 6e 65 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 0a ne will release.
105f4 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
105f5 6c 6f 63 6b 20 61 6e 64 20 61 63 71 75 69 72 65 lock and acquire
105f6 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 s a SHARED lock
105f7 69 6e 20 69 74 73 20 70 6c 61 63 65 20 69 66 20 in its place if
105f8 74 68 61 74 20 69 73 0a 2a 2a 20 74 68 65 20 61 that is.** the a
105f9 70 70 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67 ppropriate thing
105fa 20 74 6f 20 64 6f 2e 20 20 52 65 6c 65 61 73 65 to do. Release
105fb 20 6c 6f 63 6b 73 20 75 73 75 61 6c 6c 79 20 69 locks usually i
105fc 73 20 61 70 70 72 6f 70 72 69 61 74 65 2c 0a 2a s appropriate,.*
105fd 2a 20 75 6e 6c 65 73 73 20 77 65 20 61 72 65 20 * unless we are
105fe 69 6e 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 in exclusive acc
105ff 65 73 73 20 6d 6f 64 65 20 6f 72 20 75 6e 6c 65 ess mode or unle
10600 73 73 20 74 68 69 73 20 69 73 20 61 20 0a 2a 2a ss this is a .**
10601 20 43 4f 4d 4d 49 54 20 41 4e 44 20 42 45 47 49 COMMIT AND BEGI
10602 4e 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 20 41 4e N or ROLLBACK AN
10603 44 20 42 45 47 49 4e 20 6f 70 65 72 61 74 69 6f D BEGIN operatio
10604 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 n..**.** The jou
10605 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 65 69 74 rnal file is eit
10606 68 65 72 20 64 65 6c 65 74 65 64 20 6f 72 20 74 her deleted or t
10607 72 75 6e 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 runcated..**.**
10608 54 4f 44 4f 3a 20 43 6f 6e 73 69 64 65 72 20 6b TODO: Consider k
10609 65 65 70 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e eeping the journ
1060a 61 6c 20 66 69 6c 65 20 6f 70 65 6e 20 66 6f 72 al file open for
1060b 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 temporary datab
1060c 61 73 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6d 69 ases..** This mi
1060d 67 68 74 20 67 69 76 65 20 61 20 70 65 72 66 6f ght give a perfo
1060e 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 rmance improveme
1060f 6e 74 20 6f 6e 20 77 69 6e 64 6f 77 73 20 77 68 nt on windows wh
10610 65 72 65 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 61 ere opening.** a
10611 20 66 69 6c 65 20 69 73 20 61 6e 20 65 78 70 65 file is an expe
10612 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e 2e nsive operation.
10613 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
10614 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 ager_end_transac
10615 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 67 tion(Pager *pPag
10616 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 74 65 er, int hasMaste
10617 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
10618 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
10619 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc2 = SQLITE_OK;
1061a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
1061b 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 tate<PAGER_RESER
1061c 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 VED ){. retur
1061d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
1061e 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 . sqlite3PagerS
1061f 74 6d 74 43 6f 6d 6d 69 74 28 70 50 61 67 65 72 tmtCommit(pPager
10620 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d );. if( pPager-
10621 3e 73 74 6d 74 4f 70 65 6e 20 26 26 20 21 70 50 >stmtOpen && !pP
10622 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
10623 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ode ){. sqlit
10624 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 e3OsClose(pPager
10625 2d 3e 73 74 66 64 29 3b 0a 20 20 20 20 70 50 61 ->stfd);. pPa
10626 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 ger->stmtOpen =
10627 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 0;. }. if( pPa
10628 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e ger->journalOpen
10629 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ){. if( pPag
1062a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
1062b 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
1062c 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 DE_MEMORY ){.
1062d 20 20 20 69 6e 74 20 69 73 4d 65 6d 6f 72 79 4a int isMemoryJ
1062e 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 ournal = sqlite3
1062f 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 IsMemJournal(pPa
10630 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 ger->jfd);.
10631 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
10632 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 pPager->jfd);.
10633 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
10634 6e 61 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 nalOpen = 0;.
10635 20 20 20 69 66 28 20 21 69 73 4d 65 6d 6f 72 79 if( !isMemory
10636 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 Journal ){.
10637 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
10638 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e sDelete(pPager->
10639 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a pVfs, pPager->zJ
1063a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 ournal, 0);.
1063b 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1063c 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
1063d 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
1063e 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 RNALMODE_TRUNCAT
1063f 45 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 72 E. && (r
10640 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 c = sqlite3OsTru
10641 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 ncate(pPager->jf
10642 64 2c 20 30 29 29 3d 3d 53 51 4c 49 54 45 5f 4f d, 0))==SQLITE_O
10643 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 K ){. pPage
10644 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
10645 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 0;. pPager-
10646 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 >journalStarted
10647 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 = 0;. }else i
10648 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 f( pPager->exclu
10649 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c siveMode . |
1064a 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 | pPager->journa
1064b 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
1064c 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 RNALMODE_PERSIST
1064d 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 . ){. rc
1064e 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 = zeroJournalHd
1064f 72 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 r(pPager, hasMas
10650 74 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67 65 ter);. page
10651 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 r_error(pPager,
10652 72 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 rc);. pPage
10653 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
10654 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 0;. pPager-
10655 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 >journalStarted
10656 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 0;. }else{.
10657 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
10658 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
10659 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
1065a 4d 4f 44 45 5f 44 45 4c 45 54 45 20 7c 7c 20 72 MODE_DELETE || r
1065b 63 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 c );. sqlit
1065c 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 e3OsClose(pPager
1065d 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 50 ->jfd);. pP
1065e 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
1065f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 n = 0;. if(
10660 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
10661 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 & !pPager->tempF
10662 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ile ){. r
10663 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c c = sqlite3OsDel
10664 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 ete(pPager->pVfs
10665 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e , pPager->zJourn
10666 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a al, 0);. }.
10667 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
10668 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
10669 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
1066a 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e l);. pPager->
1066b 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a pInJournal = 0;.
1066c 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 sqlite3Bitve
1066d 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d cDestroy(pPager-
1066e 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b >pAlwaysRollback
1066f 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 );. pPager->p
10670 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d AlwaysRollback =
10671 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 0;.#ifdef SQLIT
10672 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
10673 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 sqlite3PcacheI
10674 74 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67 terateDirty(pPag
10675 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 er->pPCache, pag
10676 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 er_set_pagehash)
10677 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c ;.#endif. sql
10678 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 ite3PcacheCleanA
10679 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ll(pPager->pPCac
1067a 68 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d he);. pPager-
1067b 3e 64 69 72 74 79 43 61 63 68 65 20 3d 20 30 3b >dirtyCache = 0;
1067c 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 . pPager->nRe
1067d 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a c = 0;. }else{.
1067e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
1067f 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d er->pInJournal==
10680 30 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 0 );. }.. if(
10681 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 !pPager->exclusi
10682 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 veMode ){. rc
10683 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 2 = osUnlock(pPa
10684 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f ger->fd, SHARED_
10685 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 LOCK);. pPage
10686 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 r->state = PAGER
10687 5f 53 48 41 52 45 44 3b 0a 20 20 7d 65 6c 73 65 _SHARED;. }else
10688 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
10689 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 te==PAGER_SYNCED
1068a 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
1068b 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 state = PAGER_EX
1068c 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70 CLUSIVE;. }. p
1068d 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a Pager->origDbSiz
1068e 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d e = 0;. pPager-
1068f 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a >setMaster = 0;.
10690 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 pPager->needSy
10691 6e 63 20 3d 20 30 3b 0a 20 20 2f 2a 20 6c 72 75 nc = 0;. /* lru
10692 4c 69 73 74 53 65 74 46 69 72 73 74 53 79 6e 63 ListSetFirstSync
10693 65 64 28 70 50 61 67 65 72 29 3b 20 2a 2f 0a 20 ed(pPager); */.
10694 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 if( !MEMDB ){.
10695 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a pPager->dbSiz
10696 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a eValid = 0;. }.
10697 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 pPager->dbModi
10698 66 69 65 64 20 3d 20 30 3b 0a 0a 20 20 72 65 74 fied = 0;.. ret
10699 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f urn (rc==SQLITE_
1069a 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a 2f OK?rc2:rc);.}../
1069b 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 6e 64 *.** Compute and
1069c 20 72 65 74 75 72 6e 20 61 20 63 68 65 63 6b 73 return a checks
1069d 75 6d 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 um for the page
1069e 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 of data..**.** T
1069f 68 69 73 20 69 73 20 6e 6f 74 20 61 20 72 65 61 his is not a rea
106a0 6c 20 63 68 65 63 6b 73 75 6d 2e 20 20 49 74 20 l checksum. It
106a1 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 is really just t
106a2 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 0a 2a he sum of the .*
106a3 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c * random initial
106a4 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20 70 value and the p
106a5 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 57 65 20 age number. We
106a6 65 78 70 65 72 69 6d 65 6e 74 65 64 20 77 69 74 experimented wit
106a7 68 0a 2a 2a 20 61 20 63 68 65 63 6b 73 75 6d 20 h.** a checksum
106a8 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 64 61 of the entire da
106a9 74 61 2c 20 62 75 74 20 74 68 61 74 20 77 61 73 ta, but that was
106aa 20 66 6f 75 6e 64 20 74 6f 20 62 65 20 74 6f 6f found to be too
106ab 20 73 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 slow..**.** Not
106ac 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
106ad 6e 75 6d 62 65 72 20 69 73 20 73 74 6f 72 65 64 number is stored
106ae 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
106af 67 20 6f 66 20 64 61 74 61 20 61 6e 64 0a 2a 2a g of data and.**
106b0 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 69 73 the checksum is
106b1 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 stored at the e
106b2 6e 64 2e 20 20 54 68 69 73 20 69 73 20 69 6d 70 nd. This is imp
106b3 6f 72 74 61 6e 74 2e 20 20 49 66 20 6a 6f 75 72 ortant. If jour
106b4 6e 61 6c 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f nal.** corruptio
106b5 6e 20 6f 63 63 75 72 73 20 64 75 65 20 74 6f 20 n occurs due to
106b6 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c a power failure,
106b7 20 74 68 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 the most likely
106b8 20 73 63 65 6e 61 72 69 6f 0a 2a 2a 20 69 73 20 scenario.** is
106b9 74 68 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 that one end or
106ba 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 the other of the
106bb 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 record will be
106bc 63 68 61 6e 67 65 64 2e 20 20 49 74 20 69 73 0a changed. It is.
106bd 2a 2a 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b ** much less lik
106be 65 6c 79 20 74 68 61 74 20 74 68 65 20 74 77 6f ely that the two
106bf 20 65 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 ends of the jou
106c0 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c rnal record will
106c1 20 62 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 be.** correct a
106c2 6e 64 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 nd the middle be
106c3 20 63 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c corrupt. Thus,
106c4 20 74 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 this "checksum"
106c5 20 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 scheme,.** thou
106c6 67 68 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 gh fast and simp
106c7 6c 65 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 le, catches the
106c8 6d 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 mostly likely ki
106c9 6e 64 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e nd of corruption
106ca 2e 0a 2a 2a 0a 2a 2a 20 46 49 58 20 4d 45 3a 20 ..**.** FIX ME:
106cb 20 43 6f 6e 73 69 64 65 72 20 61 64 64 69 6e 67 Consider adding
106cc 20 65 76 65 72 79 20 32 30 30 74 68 20 28 6f 72 every 200th (or
106cd 20 73 6f 29 20 62 79 74 65 20 6f 66 20 74 68 65 so) byte of the
106ce 20 64 61 74 61 20 74 6f 20 74 68 65 0a 2a 2a 20 data to the.**
106cf 63 68 65 63 6b 73 75 6d 2e 20 20 54 68 61 74 20 checksum. That
106d0 77 61 79 20 69 66 20 61 20 73 69 6e 67 6c 65 20 way if a single
106d1 70 61 67 65 20 73 70 61 6e 73 20 33 20 6f 72 20 page spans 3 or
106d2 6d 6f 72 65 20 64 69 73 6b 20 73 65 63 74 6f 72 more disk sector
106d3 73 20 61 6e 64 0a 2a 2a 20 6f 6e 6c 79 20 74 68 s and.** only th
106d4 65 20 6d 69 64 64 6c 65 20 73 65 63 74 6f 72 20 e middle sector
106d5 69 73 20 63 6f 72 72 75 70 74 2c 20 77 65 20 77 is corrupt, we w
106d6 69 6c 6c 20 73 74 69 6c 6c 20 68 61 76 65 20 61 ill still have a
106d7 20 72 65 61 73 6f 6e 61 62 6c 65 0a 2a 2a 20 63 reasonable.** c
106d8 68 61 6e 63 65 20 6f 66 20 66 61 69 6c 69 6e 67 hance of failing
106d9 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 61 6e the checksum an
106da 64 20 74 68 75 73 20 64 65 74 65 63 74 69 6e 67 d thus detecting
106db 20 74 68 65 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f the problem..*/
106dc 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 67 65 .static u32 page
106dd 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20 2a 70 r_cksum(Pager *p
106de 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75 38 20 Pager, const u8
106df 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32 20 63 *aData){. u32 c
106e0 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e 63 ksum = pPager->c
106e1 6b 73 75 6d 49 6e 69 74 3b 0a 20 20 69 6e 74 20 ksumInit;. int
106e2 69 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 i = pPager->page
106e3 53 69 7a 65 2d 32 30 30 3b 0a 20 20 77 68 69 6c Size-200;. whil
106e4 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b e( i>0 ){. ck
106e5 73 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b sum += aData[i];
106e6 0a 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20 . i -= 200;.
106e7 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73 75 }. return cksu
106e8 6d 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 m;.}../* Forward
106e9 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a declaration */.
106ea 73 74 61 74 69 63 20 76 6f 69 64 20 6d 61 6b 65 static void make
106eb 43 6c 65 61 6e 28 50 67 48 64 72 2a 29 3b 0a 0a Clean(PgHdr*);..
106ec 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e /*.** Read a sin
106ed 67 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 gle page from th
106ee 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f e journal file o
106ef 70 65 6e 65 64 20 6f 6e 20 66 69 6c 65 20 64 65 pened on file de
106f0 73 63 72 69 70 74 6f 72 0a 2a 2a 20 6a 66 64 2e scriptor.** jfd.
106f1 20 20 50 6c 61 79 62 61 63 6b 20 74 68 69 73 20 Playback this
106f2 6f 6e 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 one page..**.**
106f3 54 68 65 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 66 The isMainJrnl f
106f4 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 20 74 lag is true if t
106f5 68 69 73 20 69 73 20 74 68 65 20 6d 61 69 6e 20 his is the main
106f6 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
106f7 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 66 6f and.** false fo
106f8 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 r the statement
106f9 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 journal. The ma
106fa 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 in rollback jour
106fb 6e 61 6c 20 75 73 65 73 0a 2a 2a 20 63 68 65 63 nal uses.** chec
106fc 6b 73 75 6d 73 20 2d 20 74 68 65 20 73 74 61 74 ksums - the stat
106fd 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 64 6f ement journal do
106fe 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 es not..*/.stati
106ff 63 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 c int pager_play
10700 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a 20 back_one_page(.
10701 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 Pager *pPager,
10702 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
10703 65 72 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20 er being played
10704 62 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 back */. sqlite
10705 33 5f 66 69 6c 65 20 2a 6a 66 64 2c 20 20 20 2f 3_file *jfd, /
10706 2a 20 54 68 65 20 66 69 6c 65 20 74 68 61 74 20 * The file that
10707 69 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 is the journal b
10708 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b eing rolled back
10709 20 2a 2f 0a 20 20 69 36 34 20 6f 66 66 73 65 74 */. i64 offset
1070a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 , /* Of
1070b 66 73 65 74 20 6f 66 20 74 68 65 20 70 61 67 65 fset of the page
1070c 20 77 69 74 68 69 6e 20 74 68 65 20 6a 6f 75 72 within the jour
1070d 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d nal */. int isM
1070e 61 69 6e 4a 72 6e 6c 20 20 20 20 20 20 20 2f 2a ainJrnl /*
1070f 20 54 72 75 65 20 66 6f 72 20 6d 61 69 6e 20 72 True for main r
10710 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
10711 20 46 61 6c 73 65 20 66 6f 72 20 53 74 6d 74 20 False for Stmt
10712 6a 72 6e 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 jrnl */.){. int
10713 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 rc;. PgHdr *pP
10714 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
10715 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 74 /* An exist
10716 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 20 ing page in the
10717 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 cache */. Pgno
10718 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 pgno;
10719 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1071a 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 page number of a
1071b 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c page in journal
1071c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b */. u32 cksum;
1071d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1071e 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 /* Checksum
1071f 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 20 used for sanity
10720 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 38 checking */. u8
10721 20 2a 61 44 61 74 61 20 3d 20 28 75 38 20 2a 29 *aData = (u8 *)
10722 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
10723 65 3b 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f e; /* Temp sto
10724 72 61 67 65 20 66 6f 72 20 61 20 70 61 67 65 20 rage for a page
10725 2a 2f 0a 0a 20 20 2f 2a 20 69 73 4d 61 69 6e 4a */.. /* isMainJ
10726 72 6e 6c 20 73 68 6f 75 6c 64 20 62 65 20 74 72 rnl should be tr
10727 75 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 ue for the main
10728 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 66 61 6c 73 journal and fals
10729 65 20 66 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65 e for. ** state
1072a 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2e 20 20 ment journals.
1072b 56 65 72 69 66 79 20 74 68 61 74 20 74 68 69 73 Verify that this
1072c 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 63 is always the c
1072d 61 73 65 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ase. */. asser
1072e 74 28 20 6a 66 64 20 3d 3d 20 28 69 73 4d 61 69 t( jfd == (isMai
1072f 6e 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e nJrnl ? pPager->
10730 6a 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 74 jfd : pPager->st
10731 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 fd) );. assert(
10732 20 61 44 61 74 61 20 29 3b 0a 0a 20 20 72 63 20 aData );.. rc
10733 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 64 = read32bits(jfd
10734 2c 20 6f 66 66 73 65 74 2c 20 26 70 67 6e 6f 29 , offset, &pgno)
10735 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
10736 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
10737 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 c;. rc = sqlite
10738 33 4f 73 52 65 61 64 28 6a 66 64 2c 20 61 44 61 3OsRead(jfd, aDa
10739 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 ta, pPager->page
1073a 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b Size, offset+4);
1073b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1073c 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
1073d 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 ;. pPager->jour
1073e 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 61 67 65 72 nalOff += pPager
1073f 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34 3b 0a ->pageSize + 4;.
10740 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 . /* Sanity che
10741 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61 67 cking on the pag
10742 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72 65 e. This is more
10743 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 important that
10744 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 2a I originally. *
10745 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20 61 * thought. If a
10746 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f power failure o
10747 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65 20 ccurs while the
10748 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e 67 journal is being
10749 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20 69 written,. ** i
1074a 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69 6e t could cause in
1074b 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62 65 valid data to be
1074c 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
1074d 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20 6e e journal. We n
1074e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74 65 eed to. ** dete
1074f 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64 20 ct this invalid
10750 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68 20 data (with high
10751 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e 64 probability) and
10752 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a 2f ignore it.. */
10753 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 7c . if( pgno==0 |
10754 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a | pgno==PAGER_MJ
10755 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b _PGNO(pPager) ){
10756 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
10757 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 TE_DONE;. }. i
10758 66 28 20 70 67 6e 6f 3e 28 75 6e 73 69 67 6e 65 f( pgno>(unsigne
10759 64 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 d)pPager->dbSize
1075a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1075b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
1075c 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 if( isMainJrnl )
1075d 7b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 33 {. rc = read3
1075e 32 62 69 74 73 28 6a 66 64 2c 20 6f 66 66 73 65 2bits(jfd, offse
1075f 74 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 t+pPager->pageSi
10760 7a 65 2b 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 ze+4, &cksum);.
10761 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
10762 72 6e 20 72 63 3b 0a 20 20 20 20 70 50 61 67 65 rn rc;. pPage
10763 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d r->journalOff +=
10764 20 34 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 4;. if( page
10765 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 r_cksum(pPager,
10766 61 44 61 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b aData)!=cksum ){
10767 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
10768 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d LITE_DONE;. }
10769 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
1076a 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
1076b 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 7c 7c AGER_RESERVED ||
1076c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
1076d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
1076e 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
1076f 70 61 67 65 72 20 69 73 20 69 6e 20 52 45 53 45 pager is in RESE
10770 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e RVED state, then
10771 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 there must be a
10772 20 63 6f 70 79 20 6f 66 20 74 68 69 73 0a 20 20 copy of this.
10773 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 70 ** page in the p
10774 61 67 65 72 20 63 61 63 68 65 2e 20 49 6e 20 74 ager cache. In t
10775 68 69 73 20 63 61 73 65 20 6a 75 73 74 20 75 70 his case just up
10776 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20 63 date the pager c
10777 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74 ache,. ** not t
10778 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
10779 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 6c 65 . The page is le
1077a 66 74 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 ft marked dirty
1077b 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 in this case..
1077c 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 63 65 70 **. ** An excep
1077d 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 62 6f 76 tion to the abov
1077e 65 20 72 75 6c 65 3a 20 49 66 20 74 68 65 20 64 e rule: If the d
1077f 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 6e 6f atabase is in no
10780 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a 20 -sync mode. **
10781 61 6e 64 20 61 20 70 61 67 65 20 69 73 20 6d 6f and a page is mo
10782 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20 69 6e ved during an in
10783 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
10784 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d then the page m
10785 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 69 ay. ** not be i
10786 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 n the pager cach
10787 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61 20 6d e. Later: if a m
10788 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65 72 alloc() or IO er
10789 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20 ror occurs. **
1078a 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70 61 67 during a Movepag
1078b 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e 20 74 e() call, then t
1078c 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 he page may not
1078d 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 0a be in the cache.
1078e 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 53 6f 20 ** either. So
1078f 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 the condition de
10790 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 61 scribed in the a
10791 62 6f 76 65 20 70 61 72 61 67 72 61 70 68 20 69 bove paragraph i
10792 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 65 72 s not. ** asser
10793 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 t()able.. **.
10794 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c 55 53 49 ** If in EXCLUSI
10795 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e 20 77 VE state, then w
10796 65 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 e update the pag
10797 65 72 20 63 61 63 68 65 20 69 66 20 69 74 20 65 er cache if it e
10798 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e 64 20 74 xists. ** and t
10799 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68 he main file. Th
1079a 65 20 70 61 67 65 20 69 73 20 74 68 65 6e 20 6d e page is then m
1079b 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 74 79 2e arked not dirty.
1079c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b 65 . **. ** Ticke
1079d 74 20 23 31 31 37 31 3a 20 20 54 68 65 20 73 74 t #1171: The st
1079e 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 atement journal
1079f 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 70 61 might contain pa
107a0 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61 74 20 ge content that
107a1 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e is. ** differen
107a2 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 t from the page
107a3 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65 20 73 content at the s
107a4 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e tart of the tran
107a5 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 saction.. ** Th
107a6 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 61 is occurs when a
107a7 20 70 61 67 65 20 69 73 20 63 68 61 6e 67 65 64 page is changed
107a8 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 prior to the st
107a9 61 72 74 20 6f 66 20 61 20 73 74 61 74 65 6d 65 art of a stateme
107aa 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68 61 nt. ** then cha
107ab 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74 68 69 nged again withi
107ac 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e n the statement.
107ad 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 When rolling b
107ae 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 ack such a. **
107af 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d 75 73 statement we mus
107b0 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 t not write to t
107b1 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 he original data
107b2 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65 20 6b base unless we k
107b3 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65 72 now. ** for cer
107b4 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67 69 6e tain that origin
107b5 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 al page contents
107b6 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e 74 6f are synced into
107b7 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 the main rollba
107b8 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e ck. ** journal.
107b9 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70 Otherwise, a p
107ba 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68 74 20 ower loss might
107bb 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64 20 64 leave modified d
107bc 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 ata in the. **
107bd 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 database file wi
107be 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79 20 69 thout an entry i
107bf 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a n the rollback j
107c0 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61 6e 0a ournal that can.
107c1 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74 68 65 ** restore the
107c2 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 database to its
107c3 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 original form.
107c4 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20 Two conditions
107c5 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65 74 must be. ** met
107c6 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 before writing
107c7 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
107c8 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65 20 64 files. (1) the d
107c9 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 0a atabase must be.
107ca 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32 ** locked. (2
107cb 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 ) we know that t
107cc 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 he original page
107cd 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c content is full
107ce 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69 6e y synced. ** in
107cf 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
107d0 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 l either because
107d1 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
107d2 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 6c 73 in cache or els
107d3 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 e. ** the page
107d4 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e 65 65 is marked as nee
107d5 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20 dSync==0.. **.
107d6 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a 20 ** 2008-04-14:
107d7 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 When attempting
107d8 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63 6f 72 to vacuum a cor
107d9 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
107da 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 le, it. ** is p
107db 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69 6c 20 ossible to fail
107dc 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 61 a statement on a
107dd 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 64 database that d
107de 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78 69 73 oes not yet exis
107df 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 61 t.. ** Do not a
107e0 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 ttempt to write
107e1 69 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 if database file
107e2 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e 20 has never been
107e3 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 opened.. */. p
107e4 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 Pg = pager_looku
107e5 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b p(pPager, pgno);
107e6 0a 20 20 50 41 47 45 52 54 52 41 43 45 34 28 22 . PAGERTRACE4("
107e7 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 PLAYBACK %d page
107e8 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e %d hash(%08x)\n
107e9 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
107ea 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
107eb 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 64 ), pgno, pager_d
107ec 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d 3e atahash(pPager->
107ed 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61 29 pageSize, aData)
107ee 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 );. if( (pPager
107ef 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 ->state>=PAGER_E
107f0 58 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 XCLUSIVE). &&
107f1 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 (pPg==0 || 0==(p
107f2 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
107f3 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 NEED_SYNC)). &
107f4 26 20 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 & (pPager->fd->p
107f5 4d 65 74 68 6f 64 73 29 0a 20 20 29 7b 0a 20 20 Methods). ){.
107f6 20 20 69 36 34 20 6f 66 73 74 20 3d 20 28 70 67 i64 ofst = (pg
107f7 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 no-1)*(i64)pPage
107f8 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 r->pageSize;.
107f9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 rc = sqlite3OsW
107fa 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c rite(pPager->fd,
107fb 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e aData, pPager->
107fc 70 61 67 65 53 69 7a 65 2c 20 6f 66 73 74 29 3b pageSize, ofst);
107fd 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 67 20 29 . }. if( pPg )
107fe 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 70 61 67 65 {. /* No page
107ff 20 73 68 6f 75 6c 64 20 65 76 65 72 20 62 65 20 should ever be
10800 65 78 70 6c 69 63 69 74 6c 79 20 72 6f 6c 6c 65 explicitly rolle
10801 64 20 62 61 63 6b 20 74 68 61 74 20 69 73 20 69 d back that is i
10802 6e 20 75 73 65 2c 20 65 78 63 65 70 74 0a 20 20 n use, except.
10803 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 20 31 20 ** for page 1
10804 77 68 69 63 68 20 69 73 20 68 65 6c 64 20 69 6e which is held in
10805 20 75 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f use in order to
10806 20 6b 65 65 70 20 74 68 65 20 6c 6f 63 6b 20 6f keep the lock o
10807 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 n the. ** dat
10808 61 62 61 73 65 20 61 63 74 69 76 65 2e 20 48 6f abase active. Ho
10809 77 65 76 65 72 20 73 75 63 68 20 61 20 70 61 67 wever such a pag
1080a 65 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 20 e may be rolled
1080b 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 back as a result
1080c 0a 20 20 20 20 2a 2a 20 6f 66 20 61 6e 20 69 6e . ** of an in
1080d 74 65 72 6e 61 6c 20 65 72 72 6f 72 20 72 65 73 ternal error res
1080e 75 6c 74 69 6e 67 20 69 6e 20 61 6e 20 61 75 74 ulting in an aut
1080f 6f 6d 61 74 69 63 20 63 61 6c 6c 20 74 6f 0a 20 omatic call to.
10810 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 ** sqlite3Pag
10811 65 72 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 20 20 erRollback()..
10812 20 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 */. void *p
10813 44 61 74 61 3b 0a 20 20 20 20 70 44 61 74 61 20 Data;. pData
10814 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 = pPg->pData;.
10815 20 20 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20 memcpy(pData,
10816 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 aData, pPager->p
10817 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 ageSize);. if
10818 28 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 ( pPager->xReini
10819 74 65 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61 ter ){. pPa
1081a 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 ger->xReiniter(p
1081b 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 Pg);. }. i
1081c 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 20 f( isMainJrnl )
1081d 6d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a makeClean(pPg);.
1081e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 #ifdef SQLITE_CH
1081f 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 ECK_PAGES. pP
10820 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 g->pageHash = pa
10821 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 ger_pagehash(pPg
10822 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a );.#endif. /*
10823 20 49 66 20 74 68 69 73 20 77 61 73 20 70 61 67 If this was pag
10824 65 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 e 1, then restor
10825 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 e the value of P
10826 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 2e ager.dbFileVers.
10827 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 . ** Do this
10828 62 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f 64 before any decod
10829 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ing. */. if(
1082a 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 pgno==1 ){.
1082b 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d memcpy(&pPager-
1082c 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 >dbFileVers, &((
1082d 75 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c 73 u8*)pData)[24],s
1082e 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 izeof(pPager->db
1082f 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 FileVers));.
10830 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 65 }.. /* Decode
10831 20 74 68 65 20 70 61 67 65 20 6a 75 73 74 20 72 the page just r
10832 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a 2f ead from disk */
10833 0a 20 20 20 20 43 4f 44 45 43 31 28 70 50 61 67 . CODEC1(pPag
10834 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e er, pData, pPg->
10835 70 67 6e 6f 2c 20 33 29 3b 0a 20 20 20 20 73 71 pgno, 3);. sq
10836 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 lite3PcacheRelea
10837 73 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 se(pPg);. }. r
10838 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
10839 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4d 61 ** Parameter zMa
1083a 73 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 ster is the name
1083b 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 of a master jou
1083c 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73 69 6e rnal file. A sin
1083d 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 gle journal.** f
1083e 69 6c 65 20 74 68 61 74 20 72 65 66 65 72 72 65 ile that referre
1083f 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20 d to the master
10840 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 journal file has
10841 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c 6c 65 just been rolle
10842 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 d back..** This
10843 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 routine checks i
10844 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 f it is possible
10845 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d to delete the m
10846 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
10847 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 73 20 le,.** and does
10848 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a so if it is..**.
10849 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d 61 73 ** Argument zMas
1084a 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f ter may point to
1084b 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 Pager.pTmpSpace
1084c 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66 65 72 . So that buffer
1084d 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76 61 69 is not .** avai
1084e 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 77 69 lable for use wi
1084f 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69 thin this functi
10850 6f 6e 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 on..**.**.** The
10851 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
10852 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 file contains th
10853 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63 e names of all c
10854 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2e 0a 2a hild journals..*
10855 2a 20 54 6f 20 74 65 6c 6c 20 69 66 20 61 20 6d * To tell if a m
10856 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 63 61 aster journal ca
10857 6e 20 62 65 20 64 65 6c 65 74 65 64 2c 20 63 68 n be deleted, ch
10858 65 63 6b 20 74 6f 20 65 61 63 68 20 6f 66 20 74 eck to each of t
10859 68 65 0a 2a 2a 20 63 68 69 6c 64 72 65 6e 2e 20 he.** children.
1085a 20 49 66 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e If all children
1085b 20 61 72 65 20 65 69 74 68 65 72 20 6d 69 73 73 are either miss
1085c 69 6e 67 20 6f 72 20 64 6f 20 6e 6f 74 20 72 65 ing or do not re
1085d 66 65 72 20 74 6f 0a 2a 2a 20 61 20 64 69 66 66 fer to.** a diff
1085e 65 72 65 6e 74 20 6d 61 73 74 65 72 20 6a 6f 75 erent master jou
1085f 72 6e 61 6c 2c 20 74 68 65 6e 20 74 68 69 73 20 rnal, then this
10860 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 63 master journal c
10861 61 6e 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 2a an be deleted..*
10862 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
10863 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 67 er_delmaster(Pag
10864 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 er *pPager, cons
10865 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 t char *zMaster)
10866 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
10867 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e *pVfs = pPager->
10868 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 0a pVfs;. int rc;.
10869 20 20 69 6e 74 20 6d 61 73 74 65 72 5f 6f 70 65 int master_ope
1086a 6e 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 n = 0;. sqlite3
1086b 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 3b 0a _file *pMaster;.
1086c 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1086d 70 4a 6f 75 72 6e 61 6c 3b 0a 20 20 63 68 61 72 pJournal;. char
1086e 20 2a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c *zMasterJournal
1086f 20 3d 20 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 = 0; /* Content
10870 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 s of master jour
10871 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 nal file */. i6
10872 34 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 4 nMasterJournal
10873 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 ; /* Size
10874 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 of master journa
10875 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 l file */.. /*
10876 4f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 Open the master
10877 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 63 journal file exc
10878 6c 75 73 69 76 65 6c 79 20 69 6e 20 63 61 73 65 lusively in case
10879 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
1087a 65 73 73 0a 20 20 2a 2a 20 69 73 20 72 75 6e 6e ess. ** is runn
1087b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ing this routine
1087c 20 61 6c 73 6f 2e 20 4e 6f 74 20 74 68 61 74 20 also. Not that
1087d 69 74 20 6d 61 6b 65 73 20 74 6f 6f 20 6d 75 63 it makes too muc
1087e 68 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 20 20 h difference..
1087f 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 28 */. pMaster = (
10880 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 sqlite3_file *)s
10881 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56 66 qlite3Malloc(pVf
10882 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29 s->szOsFile * 2)
10883 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28 ;. pJournal = (
10884 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28 sqlite3_file *)(
10885 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20 ((u8 *)pMaster)
10886 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 + pVfs->szOsFile
10887 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65 );. if( !pMaste
10888 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 r ){. rc = SQ
10889 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 LITE_NOMEM;. }e
1088a 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 66 6c 61 lse{. int fla
1088b 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 gs = (SQLITE_OPE
1088c 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 N_READONLY|SQLIT
1088d 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
1088e 55 52 4e 41 4c 29 3b 0a 20 20 20 20 72 63 20 3d URNAL);. rc =
1088f 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
10890 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4d Vfs, zMaster, pM
10891 61 73 74 65 72 2c 20 66 6c 61 67 73 2c 20 30 29 aster, flags, 0)
10892 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d ;. }. if( rc!=
10893 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
10894 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
10895 20 20 6d 61 73 74 65 72 5f 6f 70 65 6e 20 3d 20 master_open =
10896 31 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 1;.. rc = sqlit
10897 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4d 61 e3OsFileSize(pMa
10898 73 74 65 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f ster, &nMasterJo
10899 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 20 72 63 urnal);. if( rc
1089a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
1089b 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 to delmaster_out
1089c 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73 74 65 72 ;.. if( nMaster
1089d 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a 20 20 20 Journal>0 ){.
1089e 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b char *zJournal;
1089f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 . char *zMast
108a0 65 72 50 74 72 20 3d 20 30 3b 0a 20 20 20 20 69 erPtr = 0;. i
108a1 6e 74 20 6e 4d 61 73 74 65 72 50 74 72 20 3d 20 nt nMasterPtr =
108a2 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 pPager->pVfs->mx
108a3 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 Pathname+1;..
108a4 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 /* Load the ent
108a5 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e ire master journ
108a6 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61 al file into spa
108a7 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ce obtained from
108a8 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f . ** sqlite3_
108a9 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69 malloc() and poi
108aa 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 nted to by zMast
108ab 65 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 erJournal. .
108ac 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f */. zMasterJo
108ad 75 72 6e 61 6c 20 3d 20 28 63 68 61 72 20 2a 29 urnal = (char *)
108ae 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 4d sqlite3Malloc(nM
108af 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e asterJournal + n
108b0 4d 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20 MasterPtr);.
108b1 69 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 if( !zMasterJour
108b2 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 nal ){. rc
108b3 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
108b4 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
108b5 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a ster_out;. }.
108b6 20 20 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d zMasterPtr =
108b7 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c &zMasterJournal
108b8 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5d [nMasterJournal]
108b9 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
108ba 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 e3OsRead(pMaster
108bb 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c , zMasterJournal
108bc 2c 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c , nMasterJournal
108bd 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
108be 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
108bf 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 to delmaster_out
108c0 3b 0a 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 ;.. zJournal
108c1 3d 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c = zMasterJournal
108c2 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a 4a ;. while( (zJ
108c3 6f 75 72 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f ournal-zMasterJo
108c4 75 72 6e 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f urnal)<nMasterJo
108c5 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 urnal ){. i
108c6 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20 20 nt exists;.
108c7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
108c8 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4a 6f 75 ccess(pVfs, zJou
108c9 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 rnal, SQLITE_ACC
108ca 45 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 ESS_EXISTS, &exi
108cb 73 74 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 sts);. if(
108cc 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
108cd 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 . goto de
108ce 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 lmaster_out;.
108cf 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 }. if( e
108d0 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 20 xists ){.
108d1 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a /* One of the j
108d2 6f 75 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 ournals pointed
108d3 74 6f 20 62 79 20 74 68 65 20 6d 61 73 74 65 72 to by the master
108d4 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2e journal exists.
108d5 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 70 65 6e . ** Open
108d6 20 69 74 20 61 6e 64 20 63 68 65 63 6b 20 69 66 it and check if
108d7 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 it points at th
108d8 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
108d9 2e 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 . If. **
108da 73 6f 2c 20 72 65 74 75 72 6e 20 77 69 74 68 6f so, return witho
108db 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 ut deleting the
108dc 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
108dd 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ile.. */.
108de 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 int c;.
108df 20 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 int flags
108e0 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f = (SQLITE_OPEN_
108e1 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f READONLY|SQLITE_
108e2 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 OPEN_MAIN_JOURNA
108e3 4c 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d L);. rc =
108e4 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
108e5 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70 Vfs, zJournal, p
108e6 4a 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73 2c 20 Journal, flags,
108e7 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0);. if(
108e8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
108e9 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
108ea 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 delmaster_out;.
108eb 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
108ec 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 rc = readMaste
108ed 72 4a 6f 75 72 6e 61 6c 28 70 4a 6f 75 72 6e 61 rJournal(pJourna
108ee 6c 2c 20 7a 4d 61 73 74 65 72 50 74 72 2c 20 6e l, zMasterPtr, n
108ef 4d 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20 MasterPtr);.
108f0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
108f1 73 65 28 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 se(pJournal);.
108f2 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
108f3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
108f4 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 goto delmas
108f5 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 ter_out;.
108f6 20 7d 0a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 }.. c =
108f7 7a 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d 30 zMasterPtr[0]!=0
108f8 20 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73 74 && strcmp(zMast
108f9 65 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d erPtr, zMaster)=
108fa 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 =0;. if(
108fb 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f c ){. /
108fc 2a 20 57 65 20 68 61 76 65 20 61 20 6d 61 74 63 * We have a matc
108fd 68 2e 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 h. Do not delete
108fe 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
108ff 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 nal file. */.
10900 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d goto delm
10901 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 aster_out;.
10902 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
10903 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28 zJournal += (
10904 73 74 72 6c 65 6e 28 7a 4a 6f 75 72 6e 61 6c 29 strlen(zJournal)
10905 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 +1);. }. }.
10906 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 . rc = sqlite3
10907 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a OsDelete(pVfs, z
10908 4d 61 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 6c Master, 0);..del
10909 6d 61 73 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66 master_out:. if
1090a 28 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c ( zMasterJournal
1090b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1090c 66 72 65 65 28 7a 4d 61 73 74 65 72 4a 6f 75 72 free(zMasterJour
1090d 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 nal);. } . if
1090e 28 20 6d 61 73 74 65 72 5f 6f 70 65 6e 20 29 7b ( master_open ){
1090f 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
10910 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ose(pMaster);.
10911 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 }. sqlite3_free
10912 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 (pMaster);. ret
10913 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 73 74 61 74 urn rc;.}...stat
10914 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 74 72 ic void pager_tr
10915 75 6e 63 61 74 65 5f 63 61 63 68 65 28 50 61 67 uncate_cache(Pag
10916 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a er *pPager);../*
10917 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 .** Truncate the
10918 20 6d 61 69 6e 20 66 69 6c 65 20 6f 66 20 74 68 main file of th
10919 65 20 67 69 76 65 6e 20 70 61 67 65 72 20 74 6f e given pager to
1091a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
1091b 61 67 65 73 0a 2a 2a 20 69 6e 64 69 63 61 74 65 ages.** indicate
1091c 64 2e 20 41 6c 73 6f 20 74 72 75 6e 63 61 74 65 d. Also truncate
1091d 20 74 68 65 20 63 61 63 68 65 64 20 72 65 70 72 the cached repr
1091e 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 esentation of th
1091f 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4d 69 e file..**.** Mi
10920 67 68 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 ght might be the
10921 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 66 case that the f
10922 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 73 ile on disk is s
10923 6d 61 6c 6c 65 72 20 74 68 61 6e 20 6e 50 61 67 maller than nPag
10924 65 2e 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 68 e..** This can h
10925 61 70 70 65 6e 2c 20 66 6f 72 20 65 78 61 6d 70 appen, for examp
10926 6c 65 2c 20 69 66 20 77 65 20 61 72 65 20 69 6e le, if we are in
10927 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 the middle of a
10928 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 transaction.**
10929 77 68 69 63 68 20 68 61 73 20 65 78 74 65 6e 64 which has extend
1092a 65 64 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 ed the file size
1092b 20 61 6e 64 20 74 68 65 20 6e 65 77 20 70 61 67 and the new pag
1092c 65 73 20 61 72 65 20 73 74 69 6c 6c 20 61 6c 6c es are still all
1092d 20 68 65 6c 64 0a 2a 2a 20 69 6e 20 63 61 63 68 held.** in cach
1092e 65 2c 20 74 68 65 6e 20 61 6e 20 49 4e 53 45 52 e, then an INSER
1092f 54 20 6f 72 20 55 50 44 41 54 45 20 64 6f 65 73 T or UPDATE does
10930 20 61 20 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c a statement rol
10931 6c 62 61 63 6b 2e 20 20 53 6f 6d 65 0a 2a 2a 20 lback. Some.**
10932 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
10933 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
10934 20 63 61 6e 20 67 65 74 20 63 6f 6e 66 75 73 65 can get confuse
10935 64 20 69 66 20 79 6f 75 20 74 72 79 20 74 6f 0a d if you try to.
10936 2a 2a 20 74 72 75 6e 63 61 74 65 20 61 20 66 69 ** truncate a fi
10937 6c 65 20 74 6f 20 73 6f 6d 65 20 73 69 7a 65 20 le to some size
10938 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 that is larger t
10939 68 61 6e 20 69 74 20 63 75 72 72 65 6e 74 6c 79 han it currently
1093a 20 69 73 2c 0a 2a 2a 20 73 6f 20 64 65 74 65 63 is,.** so detec
1093b 74 20 74 68 69 73 20 63 61 73 65 20 61 6e 64 20 t this case and
1093c 77 72 69 74 65 20 61 20 73 69 6e 67 6c 65 20 7a write a single z
1093d 65 72 6f 20 62 79 74 65 20 74 6f 20 74 68 65 20 ero byte to the
1093e 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77 0a 2a end of the new.*
1093f 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a * file instead..
10940 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
10941 67 65 72 5f 74 72 75 6e 63 61 74 65 28 50 61 67 ger_truncate(Pag
10942 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
10943 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 nPage){. int r
10944 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
10945 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
10946 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 te>=PAGER_EXCLUS
10947 49 56 45 20 26 26 20 70 50 61 67 65 72 2d 3e 66 IVE && pPager->f
10948 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 d->pMethods ){.
10949 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53 69 i64 currentSi
1094a 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20 20 ze, newSize;.
1094b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 rc = sqlite3OsF
1094c 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e ileSize(pPager->
1094d 66 64 2c 20 26 63 75 72 72 65 6e 74 53 69 7a 65 fd, ¤tSize
1094e 29 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d );. newSize =
1094f 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
10950 65 2a 28 69 36 34 29 6e 50 61 67 65 3b 0a 20 20 e*(i64)nPage;.
10951 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
10952 5f 4f 4b 20 26 26 20 63 75 72 72 65 6e 74 53 69 _OK && currentSi
10953 7a 65 21 3d 6e 65 77 53 69 7a 65 20 29 7b 0a 20 ze!=newSize ){.
10954 20 20 20 20 20 69 66 28 20 63 75 72 72 65 6e 74 if( current
10955 53 69 7a 65 3e 6e 65 77 53 69 7a 65 20 29 7b 0a Size>newSize ){.
10956 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
10957 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
10958 50 61 67 65 72 2d 3e 66 64 2c 20 6e 65 77 53 69 Pager->fd, newSi
10959 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ze);. }else
1095a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
1095b 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
1095c 61 67 65 72 2d 3e 66 64 2c 20 22 22 2c 20 31 2c ager->fd, "", 1,
1095d 20 6e 65 77 53 69 7a 65 2d 31 29 3b 0a 20 20 20 newSize-1);.
1095e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1095f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
10960 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 OK ){. pPager
10961 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 ->dbSize = nPage
10962 3b 0a 20 20 20 20 70 61 67 65 72 5f 74 72 75 6e ;. pager_trun
10963 63 61 74 65 5f 63 61 63 68 65 28 70 50 61 67 65 cate_cache(pPage
10964 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e r);. }. return
10965 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 rc;.}../*.** Se
10966 74 20 74 68 65 20 73 65 63 74 6f 72 53 69 7a 65 t the sectorSize
10967 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70 for the given p
10968 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ager..**.** The
10969 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 61 sector size is a
1096a 74 20 6c 65 61 73 74 20 61 73 20 62 69 67 20 61 t least as big a
1096b 73 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a s the sector siz
1096c 65 20 72 65 70 6f 72 74 65 64 0a 2a 2a 20 62 79 e reported.** by
1096d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 sqlite3OsSector
1096e 53 69 7a 65 28 29 2e 20 20 54 68 65 20 6d 69 6e Size(). The min
1096f 69 6d 75 6d 20 73 65 63 74 6f 72 20 73 69 7a 65 imum sector size
10970 20 69 73 20 35 31 32 2e 0a 2a 2f 0a 73 74 61 74 is 512..*/.stat
10971 69 63 20 76 6f 69 64 20 73 65 74 53 65 63 74 6f ic void setSecto
10972 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 rSize(Pager *pPa
10973 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 70 ger){. assert(p
10974 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 Pager->fd->pMeth
10975 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e 74 65 6d ods||pPager->tem
10976 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 pFile);. if( !p
10977 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
10978 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 ){. /* Sector
10979 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 size doesn't ma
1097a 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 tter for tempora
1097b 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 ry files. Also,
1097c 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 the file. **
1097d 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 may not have bee
1097e 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e n opened yet, in
1097f 20 77 68 63 69 68 20 63 61 73 65 20 74 68 65 20 whcih case the
10980 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 OsSectorSize().
10981 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 ** call will
10982 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f segfault.. */
10983 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 . pPager->sec
10984 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 torSize = sqlite
10985 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50 3OsSectorSize(pP
10986 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20 ager->fd);. }.
10987 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 if( pPager->sec
10988 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20 torSize<512 ){.
10989 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f pPager->secto
1098a 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d rSize = 512;. }
1098b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 .}../*.** Playba
1098c 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 ck the journal a
1098d 6e 64 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 nd thus restore
1098e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1098f 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 e to.** the stat
10990 65 20 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f e it was in befo
10991 72 65 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 re we started ma
10992 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a king changes. .
10993 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 **.** The journa
10994 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 l file format is
10995 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a as follows: .**
10996 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 .** (1) 8 byte
10997 20 70 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 prefix. A copy
10998 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 of aJournalMagi
10999 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 c[]..** (2) 4
1099a 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
1099b 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 integer which is
1099c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 the number of v
1099d 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 alid page record
1099e 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 s.** in th
1099f 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 e journal. If t
109a0 68 69 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 his value is 0xf
109a1 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f fffffff, then co
109a2 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 mpute the.**
109a3 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 number of pag
109a4 65 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 e records from t
109a5 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e he journal size.
109a6 0a 2a 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 .** (3) 4 byte
109a7 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 big-endian inte
109a8 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 ger which is the
109a9 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 initial value f
109aa 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 or the .**
109ab 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d sanity checksum
109ac 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 ..** (4) 4 byt
109ad 65 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 e integer which
109ae 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
109af 20 70 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 pages to trunca
109b0 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 te the.**
109b1 64 61 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 database to duri
109b2 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a ng a rollback..*
109b3 2a 20 20 28 35 29 20 20 34 20 62 79 74 65 20 62 * (5) 4 byte b
109b4 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
109b5 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 r which is the s
109b6 65 63 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 ector size. The
109b7 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 header.**
109b8 20 69 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 is this many by
109b9 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 tes in size..**
109ba 20 28 36 29 20 20 34 20 62 79 74 65 20 62 69 67 (6) 4 byte big
109bb 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
109bc 77 68 69 63 68 20 69 73 20 74 68 65 20 70 61 67 which is the pag
109bd 65 20 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 e case..** (7)
109be 20 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 4 byte integer
109bf 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d which is the num
109c0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
109c1 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
109c2 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 al.** name
109c3 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79 . The value may
109c4 20 62 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 be zero (indica
109c5 74 65 20 74 68 61 74 20 74 68 65 72 65 20 69 73 te that there is
109c6 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 no master.**
109c7 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a journal.).**
109c8 20 20 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f (8) N bytes o
109c9 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 f the master jou
109ca 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 rnal name. The
109cb 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c name will be nul
109cc 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 -terminated.**
109cd 20 20 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62 and might b
109ce 65 20 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74 e shorter than t
109cf 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 he value read fr
109d0 6f 6d 20 28 35 29 2e 20 20 49 66 20 74 68 65 20 om (5). If the
109d1 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 first byte.**
109d2 20 20 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 of the name
109d3 69 73 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65 is \000 then the
109d4 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 re is no master
109d5 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 journal. The ma
109d6 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f ster.** jo
109d7 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 urnal name is st
109d8 6f 72 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a ored in UTF-8..*
109d9 2a 20 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 * (9) Zero or
109da 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 more pages insta
109db 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f nces, each as fo
109dc 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 llows:.**
109dd 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 20 + 4 byte page
109de 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 number..**
109df 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 + pPager->pag
109e0 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 eSize bytes of d
109e1 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b ata..** +
109e2 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 4 byte checksu
109e3 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 m.**.** When we
109e4 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 speak of the jou
109e5 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 rnal header, we
109e6 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 38 mean the first 8
109e7 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a items above..**
109e8 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 Each entry in t
109e9 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e he journal is an
109ea 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
109eb 20 39 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 9th item..**.**
109ec 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 Call the value
109ed 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 from the second
109ee 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 bullet "nRec".
109ef 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 nRec is the numb
109f0 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 er of.** valid p
109f1 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 age entries in t
109f2 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 he journal. In
109f3 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 most cases, you
109f4 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a can compute the.
109f5 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 ** value of nRec
109f6 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f from the size o
109f7 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
109f8 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f le. But if a po
109f9 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f wer.** failure o
109fa 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 ccurred while th
109fb 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 e journal was be
109fc 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 ing written, it
109fd 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 could be the.**
109fe 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 69 case that the si
109ff 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ze of the journa
10a00 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 l file had alrea
10a01 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 dy been increase
10a02 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 d but.** the ext
10a03 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e ra entries had n
10a04 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 ot yet made it s
10a05 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 afely to disk.
10a06 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a In such a case,.
10a07 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
10a08 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 nRec computed fr
10a09 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 om the file size
10a0a 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 would be too la
10a0b 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 rge. For.** tha
10a0c 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 t reason, we alw
10a0d 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 ays use the nRec
10a0e 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 value in the he
10a0f 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ader..**.** If t
10a10 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 he nRec value is
10a11 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 0xffffffff it m
10a12 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 eans that nRec s
10a13 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 hould be compute
10a14 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 d.** from the fi
10a15 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 le size. This v
10a16 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 65 alue is used whe
10a17 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 n the user selec
10a18 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e ts the.** no-syn
10a19 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 c option for the
10a1a 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 journal. A pow
10a1b 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 er failure could
10a1c 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 lead to corrupt
10a1d 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 ion.** in this c
10a1e 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 ase. But for th
10a1f 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 ings like tempor
10a20 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 ary table (which
10a21 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 will be.** dele
10a22 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 ted when the pow
10a23 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 er is restored)
10a24 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 we don't care.
10a25 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
10a26 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 le opened as the
10a27 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
10a28 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d not a well-form
10a29 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 ed.** journal fi
10a2a 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 le then all page
10a2b 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 s up to the firs
10a2c 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 t corrupted page
10a2d 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 are rolled.** b
10a2e 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 ack (or no pages
10a2f 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 if the journal
10a30 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 header is corrup
10a31 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 ted). The journa
10a32 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 l file.** is the
10a33 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 n deleted and SQ
10a34 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 LITE_OK returned
10a35 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 , just as if no
10a36 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a corruption had.*
10a37 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 * been encounter
10a38 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
10a39 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 I/O or malloc()
10a3a 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 error occurs, th
10a3b 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 e journal-file i
10a3c 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a s not deleted.**
10a3d 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f and an error co
10a3e 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a de is returned..
10a3f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
10a40 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 ger_playback(Pag
10a41 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
10a42 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 isHot){. sqlite
10a43 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 3_vfs *pVfs = pP
10a44 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 ager->pVfs;. i6
10a45 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 4 szJ;
10a46 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
10a47 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
10a48 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 le in bytes */.
10a49 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20 u32 nRec;
10a4a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
10a4b 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69 ber of Records i
10a4c 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f n the journal */
10a4d 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 . u32 u;
10a4e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
10a4f 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 nsigned loop cou
10a50 6e 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d nter */. Pgno m
10a51 78 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 xPg = 0;
10a52 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 /* Size of th
10a53 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 e original file
10a54 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e in pages */. in
10a55 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
10a56 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
10a57 20 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f code of a subro
10a58 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 utine */. int r
10a59 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 es = 1;
10a5a 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 /* Value ret
10a5b 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
10a5c 4f 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 OsAccess() */.
10a5d 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 char *zMaster =
10a5e 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 0; /* Name
10a5f 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e of master journ
10a60 61 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a al file if any *
10a61 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f /.. /* Figure o
10a62 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f ut how many reco
10a63 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a rds are in the j
10a64 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 ournal. Abort e
10a65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 arly if. ** the
10a66 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 journal is empt
10a67 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 y.. */. assert
10a68 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
10a69 6c 4f 70 65 6e 20 29 3b 0a 20 20 72 63 20 3d 20 lOpen );. rc =
10a6a 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
10a6b 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 e(pPager->jfd, &
10a6c 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d szJ);. if( rc!=
10a6d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a SQLITE_OK || szJ
10a6e 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ==0 ){. goto
10a6f 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 end_playback;.
10a70 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 }.. /* Read the
10a71 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
10a72 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f name from the jo
10a73 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 urnal, if it is
10a74 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 present.. ** If
10a75 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
10a76 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 l file name is s
10a77 70 65 63 69 66 69 65 64 2c 20 62 75 74 20 74 68 pecified, but th
10a78 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 e file is not.
10a79 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 ** present on di
10a7a 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 sk, then the jou
10a7b 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 rnal is not hot
10a7c 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 and does not nee
10a7d 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 d to be. ** pla
10a7e 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 yed back.. */.
10a7f 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65 zMaster = pPage
10a80 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 r->pTmpSpace;.
10a81 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a rc = readMasterJ
10a82 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a ournal(pPager->j
10a83 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 fd, zMaster, pPa
10a84 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 ger->pVfs->mxPat
10a85 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69 66 28 20 hname+1);. if(
10a86 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
10a87 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20 zMaster[0] ){.
10a88 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
10a89 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d sAccess(pVfs, zM
10a8a 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 aster, SQLITE_AC
10a8b 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 CESS_EXISTS, &re
10a8c 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 s);. }. zMaste
10a8d 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 21 r = 0;. if( rc!
10a8e 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 72 =SQLITE_OK || !r
10a8f 65 73 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 es ){. goto e
10a90 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d nd_playback;. }
10a91 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e . pPager->journ
10a92 61 6c 4f 66 66 20 3d 20 30 3b 0a 0a 20 20 2f 2a alOff = 0;.. /*
10a93 20 54 68 69 73 20 6c 6f 6f 70 20 74 65 72 6d 69 This loop termi
10a94 6e 61 74 65 73 20 65 69 74 68 65 72 20 77 68 65 nates either whe
10a95 6e 20 74 68 65 20 72 65 61 64 4a 6f 75 72 6e 61 n the readJourna
10a96 6c 48 64 72 28 29 20 63 61 6c 6c 20 72 65 74 75 lHdr() call retu
10a97 72 6e 73 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f rns. ** SQLITE_
10a98 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72 DONE or an IO er
10a99 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f 0a 20 ror occurs. */.
10a9a 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 0a 20 20 while( 1 ){..
10a9b 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 /* Read the ne
10a9c 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 xt journal heade
10a9d 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e r from the journ
10a9e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 al file. If the
10a9f 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f re are. ** no
10aa0 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c t enough bytes l
10aa1 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e eft in the journ
10aa2 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f al file for a co
10aa3 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f mplete header, o
10aa4 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 r. ** it is c
10aa5 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 orrupted, then a
10aa6 20 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 process must of
10aa7 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 failed while wr
10aa8 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a iting it.. **
10aa9 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 This indicates
10aaa 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 nothing more nee
10aab 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 ds to be rolled
10aac 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 back.. */.
10aad 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 rc = readJourna
10aae 6c 48 64 72 28 70 50 61 67 65 72 2c 20 73 7a 4a lHdr(pPager, szJ
10aaf 2c 20 26 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b , &nRec, &mxPg);
10ab0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
10ab1 49 54 45 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20 ITE_OK ){ .
10ab2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
10ab3 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 DONE ){.
10ab4 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
10ab5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f }. go
10ab6 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b to end_playback;
10ab7 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 . }.. /* I
10ab8 66 20 6e 52 65 63 20 69 73 20 30 78 66 66 66 66 f nRec is 0xffff
10ab9 66 66 66 66 2c 20 74 68 65 6e 20 74 68 69 73 20 ffff, then this
10aba 6a 6f 75 72 6e 61 6c 20 77 61 73 20 63 72 65 61 journal was crea
10abb 74 65 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 ted by a process
10abc 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20 . ** working
10abd 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e in no-sync mode.
10abe 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 This means that
10abf 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 the rest of the
10ac0 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 journal. **
10ac1 66 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 file consists of
10ac2 20 70 61 67 65 73 2c 20 74 68 65 72 65 20 61 72 pages, there ar
10ac3 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61 e no more journa
10ac4 6c 20 68 65 61 64 65 72 73 2e 20 43 6f 6d 70 75 l headers. Compu
10ac5 74 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 76 61 te. ** the va
10ac6 6c 75 65 20 6f 66 20 6e 52 65 63 20 62 61 73 65 lue of nRec base
10ac7 64 20 6f 6e 20 74 68 69 73 20 61 73 73 75 6d 70 d on this assump
10ac8 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tion.. */.
10ac9 20 69 66 28 20 6e 52 65 63 3d 3d 30 78 66 66 66 if( nRec==0xfff
10aca 66 66 66 66 66 20 29 7b 0a 20 20 20 20 20 20 61 fffff ){. a
10acb 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a ssert( pPager->j
10acc 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e ournalOff==JOURN
10acd 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
10ace 29 20 29 3b 0a 20 20 20 20 20 20 6e 52 65 63 20 ) );. nRec
10acf 3d 20 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c = (szJ - JOURNAL
10ad0 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 _HDR_SZ(pPager))
10ad1 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 /JOURNAL_PG_SZ(p
10ad2 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 Pager);. }..
10ad3 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 /* If nRec is
10ad4 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 0 and this roll
10ad5 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61 back is of a tra
10ad6 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64 nsaction created
10ad7 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 by this. **
10ad8 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74 process and if t
10ad9 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c his is the final
10ada 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a header in the j
10adb 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 ournal, then it
10adc 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 means. ** tha
10add 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74 t this part of t
10ade 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 he journal was b
10adf 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20 eing filled but
10ae0 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
10ae1 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 . ** synced t
10ae2 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 o disk. Compute
10ae3 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
10ae4 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68 ages based on th
10ae5 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 e remaining.
10ae6 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 ** size of the f
10ae7 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
10ae8 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72 ** The third ter
10ae9 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61 m of the test wa
10aea 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74 s added to fix t
10aeb 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20 icket #2565..
10aec 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 */. if( nRec
10aed 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26 26 ==0 && !isHot &&
10aee 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
10aef 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 >journalHdr+JOUR
10af0 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
10af1 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 r)==pPager->jour
10af2 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 nalOff ){.
10af3 6e 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 70 50 nRec = (szJ - pP
10af4 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
10af5 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 ) / JOURNAL_PG_S
10af6 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d Z(pPager);. }
10af7 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 .. /* If this
10af8 20 69 73 20 74 68 65 20 66 69 72 73 74 20 68 65 is the first he
10af9 61 64 65 72 20 72 65 61 64 20 66 72 6f 6d 20 74 ader read from t
10afa 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 72 75 6e he journal, trun
10afb 63 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 cate the. **
10afc 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 61 database file ba
10afd 63 6b 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e ck to its origin
10afe 61 6c 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a al size.. */.
10aff 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
10b00 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 journalOff==JOUR
10b01 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
10b02 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d r) ){. rc =
10b03 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 pager_truncate(
10b04 70 50 61 67 65 72 2c 20 6d 78 50 67 29 3b 0a 20 pPager, mxPg);.
10b05 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
10b06 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
10b07 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 goto end_playb
10b08 61 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ack;. }.
10b09 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 }.. /* Copy
10b0a 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 6f original pages o
10b0b 75 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ut of the journa
10b0c 6c 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20 l and back into
10b0d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
10b0e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f e.. */. fo
10b0f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 75 r(u=0; u<nRec; u
10b10 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ++){. rc =
10b11 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f pager_playback_o
10b12 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 ne_page(pPager,
10b13 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 pPager->jfd, pPa
10b14 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c ger->journalOff,
10b15 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 1);. if( r
10b16 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
10b17 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
10b18 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 SQLITE_DONE ){.
10b19 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
10b1a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
10b1b 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
10b1c 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 alOff = szJ;.
10b1d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
10b1e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
10b1f 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 /* If we
10b20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f are unable to ro
10b21 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 74 68 65 llback, then the
10b22 20 64 61 74 61 62 61 73 65 20 69 73 20 70 72 6f database is pro
10b23 62 61 62 6c 79 0a 20 20 20 20 20 20 20 20 20 20 bably.
10b24 2a 2a 20 67 6f 69 6e 67 20 74 6f 20 65 6e 64 20 ** going to end
10b25 75 70 20 62 65 69 6e 67 20 63 6f 72 72 75 70 74 up being corrupt
10b26 2e 20 20 49 74 20 69 73 20 63 6f 72 72 75 70 74 . It is corrupt
10b27 20 74 6f 20 75 73 2c 20 61 6e 79 68 6f 77 2e 0a to us, anyhow..
10b28 20 20 20 20 20 20 20 20 20 20 2a 2a 20 50 65 72 ** Per
10b29 68 61 70 73 20 74 68 65 20 6e 65 78 74 20 70 72 haps the next pr
10b2a 6f 63 65 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c ocess to come al
10b2b 6f 6e 67 20 63 61 6e 20 66 69 78 20 69 74 2e 2e ong can fix it..
10b2c 2e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a ... */.
10b2d 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
10b2e 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
10b2f 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f PT;. go
10b30 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b to end_playback;
10b31 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
10b32 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f }. }. }. /
10b33 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 *NOTREACHED*/.
10b34 61 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e assert( 0 );..en
10b35 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 69 66 d_playback:. if
10b36 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
10b37 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d ){. zMaster =
10b38 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 pPager->pTmpSpa
10b39 63 65 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 ce;. rc = rea
10b3a 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 dMasterJournal(p
10b3b 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 Pager->jfd, zMas
10b3c 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 ter, pPager->pVf
10b3d 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 s->mxPathname+1)
10b3e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d ;. }. if( rc==
10b3f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
10b40 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f rc = pager_end_
10b41 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 transaction(pPag
10b42 65 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21 3d er, zMaster[0]!=
10b43 27 5c 30 27 29 3b 0a 20 20 7d 0a 20 20 69 66 28 '\0');. }. if(
10b44 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
10b45 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 26 20 & zMaster[0] &&
10b46 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 res ){. /* If
10b47 20 74 68 65 72 65 20 77 61 73 20 61 20 6d 61 73 there was a mas
10b48 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 ter journal and
10b49 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
10b4a 6c 20 72 65 74 75 72 6e 20 73 75 63 63 65 73 73 l return success
10b4b 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 ,. ** see if
10b4c 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
10b4d 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 o delete the mas
10b4e 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 ter journal..
10b4f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 */. rc = pag
10b50 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 70 50 61 er_delmaster(pPa
10b51 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 ger, zMaster);.
10b52 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 61 67 }.. /* The Pag
10b53 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 er.sectorSize va
10b54 72 69 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20 riable may have
10b55 62 65 65 6e 20 75 70 64 61 74 65 64 20 77 68 69 been updated whi
10b56 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a 2a 20 le rolling. **
10b57 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 63 back a journal c
10b58 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 reated by a proc
10b59 65 73 73 20 77 69 74 68 20 61 20 64 69 66 66 65 ess with a diffe
10b5a 72 65 6e 74 20 73 65 63 74 6f 72 20 73 69 7a 65 rent sector size
10b5b 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 65 73 . ** value. Res
10b5c 65 74 20 69 74 20 74 6f 20 74 68 65 20 63 6f 72 et it to the cor
10b5d 72 65 63 74 20 76 61 6c 75 65 20 66 6f 72 20 74 rect value for t
10b5e 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a his process.. *
10b5f 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a /. setSectorSiz
10b60 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 e(pPager);. ret
10b61 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
10b62 20 50 6c 61 79 62 61 63 6b 20 74 68 65 20 73 74 Playback the st
10b63 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e atement journal.
10b64 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 73 .**.** This is s
10b65 69 6d 69 6c 61 72 20 74 6f 20 70 6c 61 79 69 6e imilar to playin
10b66 67 20 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 g back the trans
10b67 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 62 action journal b
10b68 75 74 20 77 69 74 68 0a 2a 2a 20 61 20 66 65 77 ut with.** a few
10b69 20 65 78 74 72 61 20 74 77 69 73 74 73 2e 0a 2a extra twists..*
10b6a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 68 65 *.** (1) The
10b6b 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10b6c 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
10b6d 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 74 61 file at the sta
10b6e 72 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 rt of.**
10b6f 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 the statement i
10b70 73 20 73 74 6f 72 65 64 20 69 6e 20 70 50 61 67 s stored in pPag
10b71 65 72 2d 3e 73 74 6d 74 53 69 7a 65 2c 20 6e 6f er->stmtSize, no
10b72 74 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 t in the.**
10b73 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 journal file
10b74 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 20 itself..**.**
10b75 20 20 28 32 29 20 20 49 6e 20 61 64 64 69 74 69 (2) In additi
10b76 6f 6e 20 74 6f 20 70 6c 61 79 69 6e 67 20 62 61 on to playing ba
10b77 63 6b 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 ck the statement
10b78 20 6a 6f 75 72 6e 61 6c 2c 20 61 6c 73 6f 0a 2a journal, also.*
10b79 2a 20 20 20 20 20 20 20 20 20 70 6c 61 79 62 61 * playba
10b7a 63 6b 20 61 6c 6c 20 70 61 67 65 73 20 6f 66 20 ck all pages of
10b7b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
10b7c 6a 6f 75 72 6e 61 6c 20 62 65 67 69 6e 6e 69 6e journal beginnin
10b7d 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 74 20 g.** at
10b7e 6f 66 66 73 65 74 20 70 50 61 67 65 72 2d 3e 73 offset pPager->s
10b7f 74 6d 74 4a 53 69 7a 65 2e 0a 2a 2f 0a 73 74 61 tmtJSize..*/.sta
10b80 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 73 74 tic int pager_st
10b81 6d 74 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 mt_playback(Page
10b82 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 36 r *pPager){. i6
10b83 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 4 szJ;
10b84 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
10b85 66 20 74 68 65 20 66 75 6c 6c 20 6a 6f 75 72 6e f the full journ
10b86 61 6c 20 2a 2f 0a 20 20 69 36 34 20 68 64 72 4f al */. i64 hdrO
10b87 66 66 3b 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 ff;. int nRec;
10b88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10b89 2a 20 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 6f * Number of Reco
10b8a 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 rds */. int i;
10b8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b8c 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
10b8d 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a r */. int rc;..
10b8e 20 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e szJ = pPager->
10b8f 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f journalOff;.. /
10b90 2a 20 53 65 74 20 68 64 72 4f 66 66 20 74 6f 20 * Set hdrOff to
10b91 62 65 20 74 68 65 20 6f 66 66 73 65 74 20 6a 75 be the offset ju
10b92 73 74 20 61 66 74 65 72 20 74 68 65 20 65 6e 64 st after the end
10b93 20 6f 66 20 74 68 65 20 6c 61 73 74 20 6a 6f 75 of the last jou
10b94 72 6e 61 6c 0a 20 20 2a 2a 20 70 61 67 65 20 77 rnal. ** page w
10b95 72 69 74 74 65 6e 20 62 65 66 6f 72 65 20 74 68 ritten before th
10b96 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 2d e first journal-
10b97 68 65 61 64 65 72 20 66 6f 72 20 74 68 69 73 20 header for this
10b98 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 statement. ** t
10b99 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 77 ransaction was w
10b9a 72 69 74 74 65 6e 2c 20 6f 72 20 74 68 65 20 65 ritten, or the e
10b9b 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 nd of the file i
10b9c 66 20 6e 6f 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a f no journal. *
10b9d 2a 20 68 65 61 64 65 72 20 77 61 73 20 77 72 69 * header was wri
10b9e 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 68 64 72 tten.. */. hdr
10b9f 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 Off = pPager->st
10ba0 6d 74 48 64 72 4f 66 66 3b 0a 20 20 61 73 73 65 mtHdrOff;. asse
10ba1 72 74 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c rt( pPager->full
10ba2 53 79 6e 63 20 7c 7c 20 21 68 64 72 4f 66 66 20 Sync || !hdrOff
10ba3 29 3b 0a 20 20 69 66 28 20 21 68 64 72 4f 66 66 );. if( !hdrOff
10ba4 20 29 7b 0a 20 20 20 20 68 64 72 4f 66 66 20 3d ){. hdrOff =
10ba5 20 73 7a 4a 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f szJ;. }. . /
10ba6 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 64 * Truncate the d
10ba7 61 74 61 62 61 73 65 20 62 61 63 6b 20 74 6f 20 atabase back to
10ba8 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a its original siz
10ba9 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 e.. */. rc = p
10baa 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 ager_truncate(pP
10bab 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 74 ager, pPager->st
10bac 6d 74 53 69 7a 65 29 3b 0a 20 20 61 73 73 65 72 mtSize);. asser
10bad 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
10bae 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 >=PAGER_SHARED )
10baf 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f ;.. /* Figure o
10bb0 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f ut how many reco
10bb1 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 73 rds are in the s
10bb2 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c tatement journal
10bb3 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
10bb4 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 pPager->stmtInU
10bb5 73 65 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f se && pPager->jo
10bb6 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 6e urnalOpen );. n
10bb7 52 65 63 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 Rec = pPager->st
10bb8 6d 74 4e 52 65 63 3b 0a 20 20 0a 20 20 2f 2a 20 mtNRec;. . /*
10bb9 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61 Copy original pa
10bba 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 73 ges out of the s
10bbb 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c tatement journal
10bbc 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 and back into t
10bbd 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 he. ** database
10bbe 20 66 69 6c 65 2e 20 20 4e 6f 74 65 20 74 68 61 file. Note tha
10bbf 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 t the statement
10bc0 6a 6f 75 72 6e 61 6c 20 6f 6d 69 74 73 20 63 68 journal omits ch
10bc1 65 63 6b 73 75 6d 73 20 66 72 6f 6d 0a 20 20 2a ecksums from. *
10bc2 2a 20 65 61 63 68 20 72 65 63 6f 72 64 20 73 69 * each record si
10bc3 6e 63 65 20 70 6f 77 65 72 2d 66 61 69 6c 75 72 nce power-failur
10bc4 65 20 72 65 63 6f 76 65 72 79 20 69 73 20 6e 6f e recovery is no
10bc5 74 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73 t important to s
10bc6 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 6a 6f tatement. ** jo
10bc7 75 72 6e 61 6c 73 2e 0a 20 20 2a 2f 0a 20 20 66 urnals.. */. f
10bc8 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 63 3b 20 or(i=0; i<nRec;
10bc9 69 2b 2b 29 7b 0a 20 20 20 20 69 36 34 20 6f 66 i++){. i64 of
10bca 66 73 65 74 20 3d 20 69 2a 28 34 2b 70 50 61 67 fset = i*(4+pPag
10bcb 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 er->pageSize);.
10bcc 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c rc = pager_pl
10bcd 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 ayback_one_page(
10bce 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e pPager, pPager->
10bcf 73 74 66 64 2c 20 6f 66 66 73 65 74 2c 20 30 29 stfd, offset, 0)
10bd0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 ;. assert( rc
10bd1 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
10bd2 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
10bd3 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e ITE_OK ) goto en
10bd4 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b d_stmt_playback;
10bd5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 72 . }.. /* Now r
10bd6 6f 6c 6c 20 73 6f 6d 65 20 70 61 67 65 73 20 62 oll some pages b
10bd7 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 74 72 61 ack from the tra
10bd8 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c nsaction journal
10bd9 2e 20 50 61 67 65 72 2e 73 74 6d 74 4a 53 69 7a . Pager.stmtJSiz
10bda 65 0a 20 20 2a 2a 20 77 61 73 20 74 68 65 20 73 e. ** was the s
10bdb 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ize of the journ
10bdc 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 74 68 69 al file when thi
10bdd 73 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 s statement was
10bde 73 74 61 72 74 65 64 2c 20 73 6f 0a 20 20 2a 2a started, so. **
10bdf 20 65 76 65 72 79 74 68 69 6e 67 20 61 66 74 65 everything afte
10be0 72 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 r that needs to
10be1 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 be rolled back,
10be2 65 69 74 68 65 72 20 69 6e 74 6f 20 74 68 65 0a either into the.
10be3 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 74 ** database, t
10be4 68 65 20 6d 65 6d 6f 72 79 20 63 61 63 68 65 2c he memory cache,
10be5 20 6f 72 20 62 6f 74 68 2e 0a 20 20 2a 2a 0a 20 or both.. **.
10be6 20 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 ** If it is not
10be7 20 7a 65 72 6f 2c 20 74 68 65 6e 20 50 61 67 65 zero, then Page
10be8 72 2e 73 74 6d 74 48 64 72 4f 66 66 20 69 73 20 r.stmtHdrOff is
10be9 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 the offset to th
10bea 65 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 e start. ** of
10beb 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 the first journa
10bec 6c 20 68 65 61 64 65 72 20 77 72 69 74 74 65 6e l header written
10bed 20 64 75 72 69 6e 67 20 74 68 69 73 20 73 74 61 during this sta
10bee 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
10bef 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 on.. */. pPage
10bf0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
10bf1 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a pPager->stmtJSiz
10bf2 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 63 6b 73 e;. pPager->cks
10bf3 75 6d 49 6e 69 74 20 3d 20 70 50 61 67 65 72 2d umInit = pPager-
10bf4 3e 73 74 6d 74 43 6b 73 75 6d 3b 0a 20 20 77 68 >stmtCksum;. wh
10bf5 69 6c 65 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ile( pPager->jou
10bf6 72 6e 61 6c 4f 66 66 20 3c 20 68 64 72 4f 66 66 rnalOff < hdrOff
10bf7 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 ){. rc = pag
10bf8 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f er_playback_one_
10bf9 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 61 page(pPager, pPa
10bfa 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 ger->jfd, pPager
10bfb 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 29 ->journalOff, 1)
10bfc 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 ;. assert( rc
10bfd 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
10bfe 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
10bff 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e ITE_OK ) goto en
10c00 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b d_stmt_playback;
10c01 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 70 . }.. while( p
10c02 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
10c03 66 20 3c 20 73 7a 4a 20 29 7b 0a 20 20 20 20 75 f < szJ ){. u
10c04 33 32 20 6e 4a 52 65 63 3b 20 20 20 20 20 20 20 32 nJRec;
10c05 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a /* Number of J
10c06 6f 75 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a ournal Records *
10c07 2f 0a 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b /. u32 dummy;
10c08 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f . rc = readJo
10c09 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c urnalHdr(pPager,
10c0a 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 szJ, &nJRec, &d
10c0b 75 6d 6d 79 29 3b 0a 20 20 20 20 69 66 28 20 72 ummy);. if( r
10c0c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
10c0d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
10c0e 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
10c0f 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f . goto end_
10c10 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 stmt_playback;.
10c11 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4a 52 }. if( nJR
10c12 65 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e ec==0 ){. n
10c13 4a 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 70 50 JRec = (szJ - pP
10c14 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
10c15 29 20 2f 20 28 70 50 61 67 65 72 2d 3e 70 61 67 ) / (pPager->pag
10c16 65 53 69 7a 65 2b 38 29 3b 0a 20 20 20 20 7d 0a eSize+8);. }.
10c17 20 20 20 20 66 6f 72 28 69 3d 6e 4a 52 65 63 2d for(i=nJRec-
10c18 31 3b 20 69 3e 3d 30 20 26 26 20 70 50 61 67 65 1; i>=0 && pPage
10c19 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3c 20 r->journalOff <
10c1a 73 7a 4a 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 szJ; i--){.
10c1b 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 rc = pager_play
10c1c 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 back_one_page(pP
10c1d 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 66 ager, pPager->jf
10c1e 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e d, pPager->journ
10c1f 61 6c 4f 66 66 2c 20 31 29 3b 0a 20 20 20 20 20 alOff, 1);.
10c20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
10c21 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 20 20 ITE_DONE );.
10c22 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
10c23 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73 _OK ) goto end_s
10c24 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 tmt_playback;.
10c25 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 }. }.. pPage
10c26 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
10c27 73 7a 4a 3b 0a 20 20 0a 65 6e 64 5f 73 74 6d 74 szJ;. .end_stmt
10c28 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 69 66 28 _playback:. if(
10c29 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 rc==SQLITE_OK)
10c2a 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
10c2b 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a urnalOff = szJ;.
10c2c 20 20 20 20 2f 2a 20 70 61 67 65 72 5f 72 65 6c /* pager_rel
10c2d 6f 61 64 5f 63 61 63 68 65 28 70 50 61 67 65 72 oad_cache(pPager
10c2e 29 3b 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 ); */. }. retu
10c2f 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
10c30 43 68 61 6e 67 65 20 74 68 65 20 6d 61 78 69 6d Change the maxim
10c31 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d um number of in-
10c32 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 68 61 memory pages tha
10c33 74 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a t are allowed..*
10c34 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10c35 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
10c36 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 erSetCachesize(P
10c37 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
10c38 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 73 71 6c t mxPage){. sql
10c39 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63 ite3PcacheSetCac
10c3a 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 hesize(pPager->p
10c3b 50 43 61 63 68 65 2c 20 6d 78 50 61 67 65 29 3b PCache, mxPage);
10c3c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 .}../*.** Adjust
10c3d 20 74 68 65 20 72 6f 62 75 73 74 6e 65 73 73 20 the robustness
10c3e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
10c3f 74 6f 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f to damage due to
10c40 20 4f 53 20 63 72 61 73 68 65 73 0a 2a 2a 20 6f OS crashes.** o
10c41 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 73 r power failures
10c42 20 62 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65 by changing the
10c43 20 6e 75 6d 62 65 72 20 6f 66 20 73 79 6e 63 73 number of syncs
10c44 28 29 73 20 77 68 65 6e 20 77 72 69 74 69 6e 67 ()s when writing
10c45 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b .** the rollback
10c46 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 72 65 journal. There
10c47 20 61 72 65 20 74 68 72 65 65 20 6c 65 76 65 6c are three level
10c48 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 s:.**.** OFF
10c49 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 sqlite3OsS
10c4a 79 6e 63 28 29 20 69 73 20 6e 65 76 65 72 20 63 ync() is never c
10c4b 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73 20 alled. This is
10c4c 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 the default.**
10c4d 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 for
10c4e 74 65 6d 70 6f 72 61 72 79 20 61 6e 64 20 74 72 temporary and tr
10c4f 61 6e 73 69 65 6e 74 20 66 69 6c 65 73 2e 0a 2a ansient files..*
10c50 2a 0a 2a 2a 20 20 20 20 4e 4f 52 4d 41 4c 20 20 *.** NORMAL
10c51 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 The journal is
10c52 20 73 79 6e 63 65 64 20 6f 6e 63 65 20 62 65 66 synced once bef
10c53 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e ore writes begin
10c54 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 on the.**
10c55 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 database
10c56 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 . This is norma
10c57 6c 6c 79 20 61 64 65 71 75 61 74 65 20 70 72 6f lly adequate pro
10c58 74 65 63 74 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 tection, but.**
10c59 20 20 20 20 20 20 20 20 20 20 20 20 20 69 74 20 it
10c5a 69 73 20 74 68 65 6f 72 65 74 69 63 61 6c 6c 79 is theoretically
10c5b 20 70 6f 73 73 69 62 6c 65 2c 20 74 68 6f 75 67 possible, thoug
10c5c 68 20 76 65 72 79 20 75 6e 6c 69 6b 65 6c 79 2c h very unlikely,
10c5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
10c5e 20 74 68 61 74 20 61 6e 20 69 6e 6f 70 65 72 74 that an inopert
10c5f 75 6e 65 20 70 6f 77 65 72 20 66 61 69 6c 75 72 une power failur
10c60 65 20 63 6f 75 6c 64 20 6c 65 61 76 65 20 74 68 e could leave th
10c61 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 e journal.**
10c62 20 20 20 20 20 20 20 20 20 20 69 6e 20 61 20 73 in a s
10c63 74 61 74 65 20 77 68 69 63 68 20 77 6f 75 6c 64 tate which would
10c64 20 63 61 75 73 65 20 64 61 6d 61 67 65 20 74 6f cause damage to
10c65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
10c66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68 wh
10c67 65 6e 20 69 74 20 69 73 20 72 6f 6c 6c 65 64 20 en it is rolled
10c68 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 back..**.** F
10c69 55 4c 4c 20 20 20 20 20 20 54 68 65 20 6a 6f 75 ULL The jou
10c6a 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 74 rnal is synced t
10c6b 77 69 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 wice before writ
10c6c 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a es begin on the.
10c6d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
10c6e 64 61 74 61 62 61 73 65 20 28 77 69 74 68 20 73 database (with s
10c6f 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 ome additional i
10c70 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65 nformation - the
10c71 20 6e 52 65 63 20 66 69 65 6c 64 0a 2a 2a 20 20 nRec field.**
10c72 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 of t
10c73 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
10c74 72 20 2d 20 62 65 69 6e 67 20 77 72 69 74 74 65 r - being writte
10c75 6e 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 n in between the
10c76 20 74 77 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 two.**
10c77 20 20 20 20 20 73 79 6e 63 73 29 2e 20 20 49 66 syncs). If
10c78 20 77 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 we assume that
10c79 77 72 69 74 69 6e 67 20 61 0a 2a 2a 20 20 20 20 writing a.**
10c7a 20 20 20 20 20 20 20 20 20 20 73 69 6e 67 6c 65 single
10c7b 20 64 69 73 6b 20 73 65 63 74 6f 72 20 69 73 20 disk sector is
10c7c 61 74 6f 6d 69 63 2c 20 74 68 65 6e 20 74 68 69 atomic, then thi
10c7d 73 20 6d 6f 64 65 20 70 72 6f 76 69 64 65 73 0a s mode provides.
10c7e 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
10c7f 61 73 73 75 72 61 6e 63 65 20 74 68 61 74 20 74 assurance that t
10c80 68 65 20 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 he journal will
10c81 6e 6f 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 not be corrupted
10c82 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 to the.**
10c83 20 20 20 20 20 20 20 20 70 6f 69 6e 74 20 6f 66 point of
10c84 20 63 61 75 73 69 6e 67 20 64 61 6d 61 67 65 20 causing damage
10c85 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
10c86 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e during rollback.
10c87 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65 72 69 63 20 76 .**.** Numeric v
10c88 61 6c 75 65 73 20 61 73 73 6f 63 69 61 74 65 64 alues associated
10c89 20 77 69 74 68 20 74 68 65 73 65 20 73 74 61 74 with these stat
10c8a 65 73 20 61 72 65 20 4f 46 46 3d 3d 31 2c 20 4e es are OFF==1, N
10c8b 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20 ORMAL=2,.** and
10c8c 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64 FULL=3..*/.#ifnd
10c8d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 ef SQLITE_OMIT_P
10c8e 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c AGER_PRAGMAS.SQL
10c8f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
10c90 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
10c91 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65 SafetyLevel(Page
10c92 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c r *pPager, int l
10c93 65 76 65 6c 2c 20 69 6e 74 20 62 46 75 6c 6c 46 evel, int bFullF
10c94 73 79 6e 63 29 7b 0a 20 20 70 50 61 67 65 72 2d sync){. pPager-
10c95 3e 6e 6f 53 79 6e 63 20 3d 20 20 6c 65 76 65 6c >noSync = level
10c96 3d 3d 31 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 ==1 || pPager->t
10c97 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61 67 65 empFile;. pPage
10c98 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 6c 65 r->fullSync = le
10c99 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65 vel==3 && !pPage
10c9a 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 r->tempFile;. p
10c9b 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 Pager->sync_flag
10c9c 73 20 3d 20 28 62 46 75 6c 6c 46 73 79 6e 63 3f s = (bFullFsync?
10c9d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c SQLITE_SYNC_FULL
10c9e 3a 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 :SQLITE_SYNC_NOR
10c9f 4d 41 4c 29 3b 0a 20 20 69 66 28 20 70 50 61 67 MAL);. if( pPag
10ca0 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 20 70 50 61 er->noSync ) pPa
10ca1 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 ger->needSync =
10ca2 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 0;.}.#endif../*.
10ca3 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
10ca4 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
10ca5 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
10ca6 77 68 65 6e 65 76 65 72 20 74 68 65 20 6c 69 62 whenever the lib
10ca7 72 61 72 79 0a 2a 2a 20 61 74 74 65 6d 70 74 73 rary.** attempts
10ca8 20 74 6f 20 6f 70 65 6e 20 61 20 74 65 6d 70 6f to open a tempo
10ca9 72 61 72 79 20 66 69 6c 65 2e 20 20 54 68 69 73 rary file. This
10caa 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 information is
10cab 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 used for.** test
10cac 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 ing and analysis
10cad 20 6f 6e 6c 79 2e 20 20 0a 2a 2f 0a 23 69 66 64 only. .*/.#ifd
10cae 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
10caf 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
10cb0 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 lite3_opentemp_c
10cb1 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 ount = 0;.#endif
10cb2 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 74 ../*.** Open a t
10cb3 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 0a emporary file. .
10cb4 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 **.** Write the
10cb5 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
10cb6 69 6e 74 6f 20 2a 66 64 2e 20 20 52 65 74 75 72 into *fd. Retur
10cb7 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
10cb8 75 63 63 65 73 73 20 6f 72 20 73 6f 6d 65 0a 2a uccess or some.*
10cb9 2a 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f * other error co
10cba 64 65 20 69 66 20 77 65 20 66 61 69 6c 2e 20 54 de if we fail. T
10cbb 68 65 20 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d he OS will autom
10cbc 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 20 atically delete
10cbd 74 68 65 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a the temporary.**
10cbe 20 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73 file when it is
10cbf 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 closed..*/.stat
10cc0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 ic int sqlite3Pa
10cc1 67 65 72 4f 70 65 6e 74 65 6d 70 28 0a 20 20 50 gerOpentemp(. P
10cc2 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 ager *pPager,
10cc3 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 /* The page
10cc4 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 r object */. sq
10cc5 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c lite3_file *pFil
10cc6 65 2c 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 e, /* Write the
10cc7 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
10cc8 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 76 here */. int v
10cc9 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 fsFlags
10cca 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 /* Flags passed
10ccb 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
10ccc 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 VFS */.){. int
10ccd 72 63 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 rc;..#ifdef SQLI
10cce 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 TE_TEST. sqlite
10ccf 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 3_opentemp_count
10cd0 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 ++; /* Used for
10cd1 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 testing and ana
10cd2 6c 79 73 69 73 20 6f 6e 6c 79 20 2a 2f 0a 23 65 lysis only */.#e
10cd3 6e 64 69 66 0a 0a 20 20 76 66 73 46 6c 61 67 73 ndif.. vfsFlags
10cd4 20 7c 3d 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e |= SQLITE_OPEN
10cd5 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51 4c _READWRITE | SQL
10cd6 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 ITE_OPEN_CREATE
10cd7 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 53 51 |. SQ
10cd8 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 LITE_OPEN_EXCLUS
10cd9 49 56 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 IVE | SQLITE_OPE
10cda 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 3b N_DELETEONCLOSE;
10cdb 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
10cdc 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 70 56 sOpen(pPager->pV
10cdd 66 73 2c 20 30 2c 20 70 46 69 6c 65 2c 20 76 66 fs, 0, pFile, vf
10cde 73 46 6c 61 67 73 2c 20 30 29 3b 0a 20 20 61 73 sFlags, 0);. as
10cdf 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
10ce0 5f 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 4d _OK || pFile->pM
10ce1 65 74 68 6f 64 73 20 29 3b 0a 20 20 72 65 74 75 ethods );. retu
10ce2 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 rn rc;.}..static
10ce3 20 69 6e 74 20 70 61 67 65 72 53 74 72 65 73 73 int pagerStress
10ce4 28 76 6f 69 64 20 2a 2c 50 67 48 64 72 20 2a 29 (void *,PgHdr *)
10ce5 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 ;../*.** Create
10ce6 61 20 6e 65 77 20 70 61 67 65 20 63 61 63 68 65 a new page cache
10ce7 20 61 6e 64 20 70 75 74 20 61 20 70 6f 69 6e 74 and put a point
10ce8 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 63 er to the page c
10ce9 61 63 68 65 20 69 6e 20 2a 70 70 50 61 67 65 72 ache in *ppPager
10cea 2e 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 74 6f ..** The file to
10ceb 20 62 65 20 63 61 63 68 65 64 20 6e 65 65 64 20 be cached need
10cec 6e 6f 74 20 65 78 69 73 74 2e 20 20 54 68 65 20 not exist. The
10ced 66 69 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 6b file is not lock
10cee 65 64 20 75 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 ed until.** the
10cef 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73 71 first call to sq
10cf0 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 lite3PagerGet()
10cf1 61 6e 64 20 69 73 20 6f 6e 6c 79 20 68 65 6c 64 and is only held
10cf2 20 6f 70 65 6e 20 75 6e 74 69 6c 20 74 68 65 0a open until the.
10cf3 2a 2a 20 6c 61 73 74 20 70 61 67 65 20 69 73 20 ** last page is
10cf4 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20 73 released using s
10cf5 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
10cf6 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69 ()..**.** If zFi
10cf7 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 lename is NULL t
10cf8 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e hen a randomly-n
10cf9 61 6d 65 64 20 74 65 6d 70 6f 72 61 72 79 20 66 amed temporary f
10cfa 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a ile is created.*
10cfb 2a 20 61 6e 64 20 75 73 65 64 20 61 73 20 74 68 * and used as th
10cfc 65 20 66 69 6c 65 20 74 6f 20 62 65 20 63 61 63 e file to be cac
10cfd 68 65 64 2e 20 20 54 68 65 20 66 69 6c 65 20 77 hed. The file w
10cfe 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 0a 2a ill be deleted.*
10cff 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * automatically
10d00 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 when it is close
10d01 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69 6c d..**.** If zFil
10d02 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 ename is ":memor
10d03 79 3a 22 20 74 68 65 6e 20 61 6c 6c 20 69 6e 66 y:" then all inf
10d04 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 65 6c 64 ormation is held
10d05 20 69 6e 20 63 61 63 68 65 2e 0a 2a 2a 20 49 74 in cache..** It
10d06 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 is never writte
10d07 6e 20 74 6f 20 64 69 73 6b 2e 20 20 54 68 69 73 n to disk. This
10d08 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
10d09 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 0a 2a 2a 20 implement an.**
10d0a 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
10d0b 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
10d0c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
10d0d 33 50 61 67 65 72 4f 70 65 6e 28 0a 20 20 73 71 3PagerOpen(. sq
10d0e 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
10d0f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 /* The vi
10d10 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74 65 rtual file syste
10d11 6d 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 50 61 m to use */. Pa
10d12 67 65 72 20 2a 2a 70 70 50 61 67 65 72 2c 20 20 ger **ppPager,
10d13 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
10d14 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 the Pager struc
10d15 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 ture here */. c
10d16 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
10d17 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 name, /* Name
10d18 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
10d19 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a file to open */.
10d1a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 int nExtra,
10d1b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
10d1c 74 72 61 20 62 79 74 65 73 20 61 70 70 65 6e 64 tra bytes append
10d1d 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f to each in-memo
10d1e 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ry page */. int
10d1f 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
10d20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63 /* flags c
10d21 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20 ontrolling this
10d22 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 file */. int vf
10d23 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 sFlags
10d24 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 73 /* flags pass
10d25 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 ed through to sq
10d26 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 lite3_vfs.xOpen(
10d27 29 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 50 ) */.){. u8 *pP
10d28 74 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 tr;. Pager *pPa
10d29 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 ger = 0;. int r
10d2a 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
10d2b 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 74 65 int i;. int te
10d2c 6d 70 46 69 6c 65 20 3d 20 30 3b 0a 20 20 69 6e mpFile = 0;. in
10d2d 74 20 6d 65 6d 44 62 20 3d 20 30 3b 0a 20 20 69 t memDb = 0;. i
10d2e 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b nt readOnly = 0;
10d2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e 61 . int useJourna
10d30 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47 l = (flags & PAG
10d31 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 29 ER_OMIT_JOURNAL)
10d32 3d 3d 30 3b 0a 20 20 69 6e 74 20 6e 6f 52 65 61 ==0;. int noRea
10d33 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20 26 dlock = (flags &
10d34 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f PAGER_NO_READLO
10d35 43 4b 29 21 3d 30 3b 0a 20 20 69 6e 74 20 6a 6f CK)!=0;. int jo
10d36 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 0a 20 urnalFileSize;.
10d37 20 69 6e 74 20 70 63 61 63 68 65 53 69 7a 65 20 int pcacheSize
10d38 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 = sqlite3PcacheS
10d39 69 7a 65 28 29 3b 0a 20 20 69 6e 74 20 73 7a 50 ize();. int szP
10d3a 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 ageDflt = SQLITE
10d3b 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
10d3c 5a 45 3b 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 ZE;. char *zPat
10d3d 68 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 69 6e 74 hname = 0;. int
10d3e 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 0a nPathname = 0;.
10d3f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4a 6f . if( sqlite3Jo
10d40 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3e urnalSize(pVfs)>
10d41 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
10d42 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 20 20 6a lSize() ){. j
10d43 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d ournalFileSize =
10d44 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 sqlite3JournalS
10d45 69 7a 65 28 70 56 66 73 29 3b 0a 20 20 7d 65 6c ize(pVfs);. }el
10d46 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 se{. journalF
10d47 69 6c 65 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 ileSize = sqlite
10d48 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
10d49 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 );. }.. /* The
10d4a 20 64 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20 default return
10d4b 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 is a NULL pointe
10d4c 72 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 r */. *ppPager
10d4d 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 = 0;.. /* Compu
10d4e 74 65 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 te and store the
10d4f 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69 full pathname i
10d50 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 n an allocated b
10d51 75 66 66 65 72 20 70 6f 69 6e 74 65 64 0a 20 20 uffer pointed.
10d52 2a 2a 20 74 6f 20 62 79 20 7a 50 61 74 68 6e 61 ** to by zPathna
10d53 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 50 61 74 68 me, length nPath
10d54 6e 61 6d 65 2e 20 4f 72 2c 20 69 66 20 74 68 69 name. Or, if thi
10d55 73 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 s is a temporary
10d56 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 file,. ** leav
10d57 65 20 62 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65 e both nPathname
10d58 20 61 6e 64 20 7a 50 61 74 68 6e 61 6d 65 20 73 and zPathname s
10d59 65 74 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 et to 0.. */.
10d5a 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 if( zFilename &&
10d5b 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b zFilename[0] ){
10d5c 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d . nPathname =
10d5d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
10d5e 65 2b 31 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61 e+1;. zPathna
10d5f 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c me = sqlite3Mall
10d60 6f 63 28 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b oc(nPathname*2);
10d61 0a 20 20 20 20 69 66 28 20 7a 50 61 74 68 6e 61 . if( zPathna
10d62 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 me==0 ){. r
10d63 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
10d64 45 4d 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 EM;. }.#ifnde
10d65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 f SQLITE_OMIT_ME
10d66 4d 4f 52 59 44 42 0a 20 20 20 20 69 66 28 20 73 MORYDB. if( s
10d67 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c trcmp(zFilename,
10d68 22 3a 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 ":memory:")==0 )
10d69 7b 0a 20 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 {. memDb =
10d6a 31 3b 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 1;. zPathna
10d6b 6d 65 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d me[0] = 0;. }
10d6c 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 else.#endif.
10d6d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
10d6e 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 ite3OsFullPathna
10d6f 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 me(pVfs, zFilena
10d70 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a me, nPathname, z
10d71 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d Pathname);. }
10d72 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
10d73 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
10d74 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 sqlite3_free(zPa
10d75 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 thname);. r
10d76 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
10d77 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 nPathname =
10d78 73 74 72 6c 65 6e 28 7a 50 61 74 68 6e 61 6d 65 strlen(zPathname
10d79 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c );. }.. /* All
10d7a 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 ocate memory for
10d7b 20 74 68 65 20 70 61 67 65 72 20 73 74 72 75 63 the pager struc
10d7c 74 75 72 65 20 2a 2f 0a 20 20 70 50 61 67 65 72 ture */. pPager
10d7d 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
10d7e 5a 65 72 6f 28 0a 20 20 20 20 73 69 7a 65 6f 66 Zero(. sizeof
10d7f 28 2a 70 50 61 67 65 72 29 20 2b 20 20 20 20 20 (*pPager) +
10d80 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 73 /* Pager s
10d81 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 tructure */.
10d82 70 63 61 63 68 65 53 69 7a 65 20 20 20 20 20 20 pcacheSize
10d83 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 + /* P
10d84 43 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a Cache object */.
10d85 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 journalFileS
10d86 69 7a 65 20 2b 20 20 20 20 20 20 20 20 20 20 20 ize +
10d87 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
10d88 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f ile structure */
10d89 20 0a 20 20 20 20 70 56 66 73 2d 3e 73 7a 4f 73 . pVfs->szOs
10d8a 46 69 6c 65 20 20 2b 20 20 20 20 20 20 20 20 20 File +
10d8b 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 /* The main db
10d8c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 file */. jou
10d8d 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 rnalFileSize * 2
10d8e 20 2b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 + /* The
10d8f 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 two journal file
10d90 73 20 2a 2f 20 0a 20 20 20 20 33 2a 6e 50 61 74 s */ . 3*nPat
10d91 68 6e 61 6d 65 20 2b 20 34 30 20 20 20 20 20 20 hname + 40
10d92 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 /* zFilena
10d93 6d 65 2c 20 7a 44 69 72 65 63 74 6f 72 79 2c 20 me, zDirectory,
10d94 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b zJournal */. );
10d95 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 20 29 . if( !pPager )
10d96 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
10d97 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 ee(zPathname);.
10d98 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
10d99 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 _NOMEM;. }. pP
10d9a 61 67 65 72 2d 3e 70 50 43 61 63 68 65 20 3d 20 ager->pPCache =
10d9b 28 50 43 61 63 68 65 20 2a 29 26 70 50 61 67 65 (PCache *)&pPage
10d9c 72 5b 31 5d 3b 0a 20 20 70 50 74 72 20 3d 20 28 r[1];. pPtr = (
10d9d 28 75 38 20 2a 29 26 70 50 61 67 65 72 5b 31 5d (u8 *)&pPager[1]
10d9e 29 20 2b 20 70 63 61 63 68 65 53 69 7a 65 3b 0a ) + pcacheSize;.
10d9f 20 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 pPager->vfsFla
10da0 67 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a 20 gs = vfsFlags;.
10da1 20 70 50 61 67 65 72 2d 3e 66 64 20 3d 20 28 73 pPager->fd = (s
10da2 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50 qlite3_file*)&pP
10da3 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c tr[pVfs->szOsFil
10da4 65 2a 30 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e e*0];. pPager->
10da5 73 74 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f stfd = (sqlite3_
10da6 66 69 6c 65 2a 29 26 70 50 74 72 5b 70 56 66 73 file*)&pPtr[pVfs
10da7 2d 3e 73 7a 4f 73 46 69 6c 65 5d 3b 0a 20 20 70 ->szOsFile];. p
10da8 50 61 67 65 72 2d 3e 6a 66 64 20 3d 20 28 73 71 Pager->jfd = (sq
10da9 6c 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50 74 lite3_file*)&pPt
10daa 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 r[pVfs->szOsFile
10dab 2b 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 +journalFileSize
10dac 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 ];. pPager->zFi
10dad 6c 65 6e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 lename = (char*)
10dae 26 70 50 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 &pPtr[pVfs->szOs
10daf 46 69 6c 65 2b 32 2a 6a 6f 75 72 6e 61 6c 46 69 File+2*journalFi
10db0 6c 65 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 65 leSize];. pPage
10db1 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 20 3d 20 r->zDirectory =
10db2 26 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 &pPager->zFilena
10db3 6d 65 5b 6e 50 61 74 68 6e 61 6d 65 2b 31 5d 3b me[nPathname+1];
10db4 0a 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 . pPager->zJour
10db5 6e 61 6c 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a nal = &pPager->z
10db6 44 69 72 65 63 74 6f 72 79 5b 6e 50 61 74 68 6e Directory[nPathn
10db7 61 6d 65 2b 31 5d 3b 0a 20 20 70 50 61 67 65 72 ame+1];. pPager
10db8 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 ->pVfs = pVfs;.
10db9 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 if( zPathname )
10dba 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 {. memcpy(pPa
10dbb 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 ger->zFilename,
10dbc 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 zPathname, nPath
10dbd 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 73 71 6c name+1);. sql
10dbe 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e ite3_free(zPathn
10dbf 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ame);. }.. /*
10dc0 4f 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 Open the pager f
10dc1 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ile.. */. if(
10dc2 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 zFilename && zFi
10dc3 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 lename[0] && !me
10dc4 6d 44 62 20 29 7b 0a 20 20 20 20 69 66 28 20 6e mDb ){. if( n
10dc5 50 61 74 68 6e 61 6d 65 3e 28 70 56 66 73 2d 3e Pathname>(pVfs->
10dc6 6d 78 50 61 74 68 6e 61 6d 65 20 2d 20 28 69 6e mxPathname - (in
10dc7 74 29 73 69 7a 65 6f 66 28 22 2d 6a 6f 75 72 6e t)sizeof("-journ
10dc8 61 6c 22 29 29 20 29 7b 0a 20 20 20 20 20 20 72 al")) ){. r
10dc9 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f c = SQLITE_CANTO
10dca 50 45 4e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a PEN;. }else{.
10dcb 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d int fout =
10dcc 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 0;. rc = s
10dcd 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
10dce 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 s, pPager->zFile
10dcf 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 name, pPager->fd
10dd0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
10dd1 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 pPage
10dd2 72 2d 3e 76 66 73 46 6c 61 67 73 2c 20 26 66 6f r->vfsFlags, &fo
10dd3 75 74 29 3b 0a 20 20 20 20 20 20 72 65 61 64 4f ut);. readO
10dd4 6e 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 nly = (fout&SQLI
10dd5 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
10dd6 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 );.. /* If
10dd7 74 68 65 20 66 69 6c 65 20 77 61 73 20 73 75 63 the file was suc
10dd8 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 cessfully opened
10dd9 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 for read/write
10dda 61 63 63 65 73 73 2c 0a 20 20 20 20 20 20 2a 2a access,. **
10ddb 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c choose a defaul
10ddc 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63 t page size in c
10ddd 61 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63 ase we have to c
10dde 72 65 61 74 65 20 74 68 65 0a 20 20 20 20 20 20 reate the.
10ddf 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
10de0 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 61 . The default pa
10de1 67 65 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d ge size is the m
10de2 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 20 aximum of:.
10de3 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 **. **
10de4 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 + SQLITE_DEFAULT
10de5 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20 _PAGE_SIZE,.
10de6 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61 ** + The va
10de7 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
10de8 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 sqlite3OsSectorS
10de9 69 7a 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 20 ize(). **
10dea 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20 + The largest
10deb 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63 page size that c
10dec 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 an be written at
10ded 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 20 20 omically..
10dee 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
10def 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 =SQLITE_OK && !r
10df0 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 eadOnly ){.
10df1 20 20 20 69 6e 74 20 69 53 65 63 74 6f 72 53 69 int iSectorSi
10df2 7a 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 ze = sqlite3OsSe
10df3 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d ctorSize(pPager-
10df4 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 >fd);. if
10df5 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 69 53 65 ( szPageDflt<iSe
10df6 63 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20 20 ctorSize ){.
10df7 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 szPageDflt
10df8 20 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a = iSectorSize;.
10df9 20 20 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66 }.#ifdef
10dfa 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 SQLITE_ENABLE_A
10dfb 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 TOMIC_WRITE.
10dfc 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 {.
10dfd 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 int iDc = sqlite
10dfe 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 3OsDeviceCharact
10dff 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d eristics(pPager-
10e00 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 >fd);.
10e01 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 int ii;.
10e02 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f assert(SQLITE_
10e03 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d IOCAP_ATOMIC512=
10e04 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 =(512>>8));.
10e05 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c assert(SQL
10e06 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
10e07 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 64K==(65536>>8))
10e08 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 ;. asse
10e09 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 rt(SQLITE_MAX_DE
10e0a 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c FAULT_PAGE_SIZE<
10e0b 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20 =65536);.
10e0c 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65 for(ii=szPage
10e0d 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45 Dflt; ii<=SQLITE
10e0e 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
10e0f 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29 E_SIZE; ii=ii*2)
10e10 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 {. if
10e11 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f ( iDc&(SQLITE_IO
10e12 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e CAP_ATOMIC|(ii>>
10e13 38 29 29 20 29 20 73 7a 50 61 67 65 44 66 6c 74 8)) ) szPageDflt
10e14 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20 = ii;.
10e15 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e }. }.#en
10e16 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 28 20 dif. if(
10e17 73 7a 50 61 67 65 44 66 6c 74 3e 53 51 4c 49 54 szPageDflt>SQLIT
10e18 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
10e19 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 GE_SIZE ){.
10e1a 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 szPageDflt
10e1b 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 = SQLITE_MAX_DEF
10e1c 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 0a AULT_PAGE_SIZE;.
10e1d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
10e1e 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b }. }. }else{
10e1f 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65 6d . /* If a tem
10e20 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 72 porary file is r
10e21 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 equested, it is
10e22 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 not opened immed
10e23 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49 iately.. ** I
10e24 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 61 n this case we a
10e25 63 63 65 70 74 20 74 68 65 20 64 65 66 61 75 6c ccept the defaul
10e26 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20 t page size and
10e27 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a 20 delay actually.
10e28 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68 ** opening th
10e29 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68 65 e file until the
10e2a 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f first call to O
10e2b 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a sWrite().. **
10e2c 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72 61 . ** This bra
10e2d 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e 20 nch is also run
10e2e 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
10e2f 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69 6e database. An in
10e30 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64 -memory. ** d
10e31 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 73 atabase is the s
10e32 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66 69 ame as a temp-fi
10e33 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 65 72 le that is never
10e34 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a written out to.
10e35 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20 ** disk and
10e36 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 uses an in-memor
10e37 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e y rollback journ
10e38 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 al.. */ .
10e39 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20 tempFile = 1;.
10e3a 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
10e3b 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 = PAGER_EXCLUSIV
10e3c 45 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 E;. }.. if( pP
10e3d 61 67 65 72 20 26 26 20 72 63 3d 3d 53 51 4c 49 ager && rc==SQLI
10e3e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 TE_OK ){. pPa
10e3f 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d ger->pTmpSpace =
10e40 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c sqlite3PageMall
10e41 6f 63 28 73 7a 50 61 67 65 44 66 6c 74 29 3b 0a oc(szPageDflt);.
10e42 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 }.. /* If an
10e43 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 69 6e error occured in
10e44 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 62 either of the b
10e45 6c 6f 63 6b 73 20 61 62 6f 76 65 2e 0a 20 20 2a locks above.. *
10e46 2a 20 46 72 65 65 20 74 68 65 20 50 61 67 65 72 * Free the Pager
10e47 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 structure and c
10e48 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 lose the file..
10e49 20 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 70 61 ** Since the pa
10e4a 67 65 72 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 63 ger is not alloc
10e4b 61 74 65 64 20 74 68 65 72 65 20 69 73 20 6e 6f ated there is no
10e4c 20 6e 65 65 64 20 74 6f 20 73 65 74 20 0a 20 20 need to set .
10e4d 2a 2a 20 61 6e 79 20 50 61 67 65 72 2e 65 72 72 ** any Pager.err
10e4e 4d 61 73 6b 20 76 61 72 69 61 62 6c 65 73 2e 0a Mask variables..
10e4f 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 67 */. if( !pPag
10e50 65 72 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 70 er || !pPager->p
10e51 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 TmpSpace ){.
10e52 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
10e53 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 Pager->fd);.
10e54 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 sqlite3_free(pPa
10e55 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ger);. return
10e56 20 28 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b ((rc==SQLITE_OK
10e57 29 3f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3a 72 )?SQLITE_NOMEM:r
10e58 63 29 3b 0a 20 20 7d 0a 20 20 6e 45 78 74 72 61 c);. }. nExtra
10e59 20 3d 20 46 4f 52 43 45 5f 41 4c 49 47 4e 4d 45 = FORCE_ALIGNME
10e5a 4e 54 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 NT(nExtra);. sq
10e5b 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 lite3PcacheOpen(
10e5c 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 szPageDflt, nExt
10e5d 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 ra, !memDb,.
10e5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10e5f 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65 !memDb?pagerStre
10e60 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 ss:0, (void *)pP
10e61 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 ager, pPager->pP
10e62 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 Cache);.. PAGER
10e63 54 52 41 43 45 33 28 22 4f 50 45 4e 20 25 64 20 TRACE3("OPEN %d
10e64 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c %s\n", FILEHANDL
10e65 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c EID(pPager->fd),
10e66 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
10e67 6d 65 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 me);. IOTRACE((
10e68 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 "OPEN %p %s\n",
10e69 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e pPager, pPager->
10e6a 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 2f zFilename)).. /
10e6b 2a 20 46 69 6c 6c 20 69 6e 20 50 61 67 65 72 2e * Fill in Pager.
10e6c 7a 44 69 72 65 63 74 6f 72 79 5b 5d 20 2a 2f 0a zDirectory[] */.
10e6d 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d memcpy(pPager-
10e6e 3e 7a 44 69 72 65 63 74 6f 72 79 2c 20 70 50 61 >zDirectory, pPa
10e6f 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 ger->zFilename,
10e70 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 nPathname+1);.
10e71 66 6f 72 28 69 3d 73 74 72 6c 65 6e 28 70 50 61 for(i=strlen(pPa
10e72 67 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 29 ger->zDirectory)
10e73 3b 20 69 3e 30 20 26 26 20 70 50 61 67 65 72 2d ; i>0 && pPager-
10e74 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d 31 5d >zDirectory[i-1]
10e75 21 3d 27 2f 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 !='/'; i--){}.
10e76 69 66 28 20 69 3e 30 20 29 20 70 50 61 67 65 72 if( i>0 ) pPager
10e77 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d 31 ->zDirectory[i-1
10e78 5d 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 46 69 6c ] = 0;.. /* Fil
10e79 6c 20 69 6e 20 50 61 67 65 72 2e 7a 4a 6f 75 72 l in Pager.zJour
10e7a 6e 61 6c 5b 5d 20 2a 2f 0a 20 20 69 66 28 20 7a nal[] */. if( z
10e7b 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 Pathname ){.
10e7c 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a memcpy(pPager->z
10e7d 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d Journal, pPager-
10e7e 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 >zFilename, nPat
10e7f 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 hname);. memc
10e80 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 py(&pPager->zJou
10e81 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c rnal[nPathname],
10e82 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 39 29 3b "-journal", 9);
10e83 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 . }else{. pP
10e84 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d ager->zJournal =
10e85 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 70 50 0;. }.. /* pP
10e86 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
10e87 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 n = 0; */. pPag
10e88 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d er->useJournal =
10e89 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70 useJournal;. p
10e8a 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 Pager->noReadloc
10e8b 6b 20 3d 20 6e 6f 52 65 61 64 6c 6f 63 6b 20 26 k = noReadlock &
10e8c 26 20 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a & readOnly;. /*
10e8d 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 pPager->stmtOpe
10e8e 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 n = 0; */. /* p
10e8f 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 Pager->stmtInUse
10e90 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 = 0; */. /* pP
10e91 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 ager->nRef = 0;
10e92 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 */. pPager->dbS
10e93 69 7a 65 56 61 6c 69 64 20 3d 20 6d 65 6d 44 62 izeValid = memDb
10e94 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 ;. pPager->page
10e95 53 69 7a 65 20 3d 20 73 7a 50 61 67 65 44 66 6c Size = szPageDfl
10e96 74 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e t;. /* pPager->
10e97 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f stmtSize = 0; */
10e98 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 . /* pPager->st
10e99 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a mtJSize = 0; */.
10e9a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61 /* pPager->nPa
10e9b 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 ge = 0; */. pPa
10e9c 67 65 72 2d 3e 6d 78 50 61 67 65 20 3d 20 31 30 ger->mxPage = 10
10e9d 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 0;. pPager->mxP
10e9e 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 gno = SQLITE_MAX
10e9f 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f _PAGE_COUNT;. /
10ea0 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 * pPager->state
10ea1 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20 = PAGER_UNLOCK;
10ea2 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
10ea3 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28 74 ger->state == (t
10ea4 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52 5f empFile ? PAGER_
10ea5 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47 45 EXCLUSIVE : PAGE
10ea6 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 2f R_UNLOCK) );. /
10ea7 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d 61 73 * pPager->errMas
10ea8 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 k = 0; */. pPag
10ea9 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20 74 er->tempFile = t
10eaa 65 6d 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 72 empFile;. asser
10eab 74 28 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 t( tempFile==PAG
10eac 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
10ead 4f 52 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 20 ORMAL .
10eae 20 7c 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 || tempFile==PA
10eaf 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
10eb0 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 EXCLUSIVE );. a
10eb1 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 ssert( PAGER_LOC
10eb2 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 KINGMODE_EXCLUSI
10eb3 56 45 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 65 VE==1 );. pPage
10eb4 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
10eb5 20 3d 20 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 = tempFile; .
10eb6 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 pPager->memDb =
10eb7 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65 72 2d memDb;. pPager-
10eb8 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 72 65 61 64 >readOnly = read
10eb9 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 Only;. /* pPage
10eba 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b r->needSync = 0;
10ebb 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f */. pPager->no
10ebc 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e 74 Sync = pPager->t
10ebd 65 6d 70 46 69 6c 65 20 7c 7c 20 21 75 73 65 4a empFile || !useJ
10ebe 6f 75 72 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 ournal;. pPager
10ebf 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 70 50 ->fullSync = (pP
10ec0 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3f 30 3a 31 ager->noSync?0:1
10ec1 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e );. pPager->syn
10ec2 63 5f 66 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 c_flags = SQLITE
10ec3 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 _SYNC_NORMAL;.
10ec4 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 72 73 /* pPager->pFirs
10ec5 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 t = 0; */. /* p
10ec6 50 61 67 65 72 2d 3e 70 46 69 72 73 74 53 79 6e Pager->pFirstSyn
10ec7 63 65 64 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a ced = 0; */. /*
10ec8 20 70 50 61 67 65 72 2d 3e 70 4c 61 73 74 20 3d pPager->pLast =
10ec9 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 0; */. pPager-
10eca 3e 6e 45 78 74 72 61 20 3d 20 6e 45 78 74 72 61 >nExtra = nExtra
10ecb 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 ;. pPager->jour
10ecc 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53 nalSizeLimit = S
10ecd 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f QLITE_DEFAULT_JO
10ece 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 URNAL_SIZE_LIMIT
10ecf 3b 0a 20 20 61 73 73 65 72 74 28 70 50 61 67 65 ;. assert(pPage
10ed0 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c r->fd->pMethods|
10ed1 7c 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 73 65 |tempFile);. se
10ed2 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 tSectorSize(pPag
10ed3 65 72 29 3b 0a 20 20 69 66 28 20 6d 65 6d 44 62 er);. if( memDb
10ed4 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
10ed5 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 journalMode = PA
10ed6 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
10ed7 4d 45 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a MEMORY;. }. /*
10ed8 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 pPager->xBusyHa
10ed9 6e 64 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 ndler = 0; */.
10eda 2f 2a 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 /* pPager->pBusy
10edb 48 61 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 HandlerArg = 0;
10edc 2a 2f 0a 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 */. /* memset(p
10edd 50 61 67 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c Pager->aHash, 0,
10ede 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e sizeof(pPager->
10edf 61 48 61 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 aHash)); */. *p
10ee0 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b pPager = pPager;
10ee1 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
10ee2 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 _OK;.}../*.** Se
10ee3 74 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c t the busy handl
10ee4 65 72 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a er function..*/.
10ee5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10ee6 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
10ee7 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 0a SetBusyhandler(.
10ee8 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c Pager *pPager,
10ee9 20 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 . int (*xBusyH
10eea 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a 29 2c andler)(void *),
10eeb 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 . void *pBusyHa
10eec 6e 64 6c 65 72 41 72 67 0a 29 7b 20 20 0a 20 20 ndlerArg.){ .
10eed 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e pPager->xBusyHan
10eee 64 6c 65 72 20 3d 20 78 42 75 73 79 48 61 6e 64 dler = xBusyHand
10eef 6c 65 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 ler;. pPager->p
10ef0 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d BusyHandlerArg =
10ef1 20 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 pBusyHandlerArg
10ef2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
10ef3 68 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 72 he reinitializer
10ef4 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e for this pager.
10ef5 20 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 If not NULL, t
10ef6 68 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 72 he reinitializer
10ef7 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 77 68 .** is called wh
10ef8 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f en the content o
10ef9 66 20 61 20 70 61 67 65 20 69 6e 20 63 61 63 68 f a page in cach
10efa 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 74 6f e is restored to
10efb 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a its original.**
10efc 20 76 61 6c 75 65 20 61 73 20 61 20 72 65 73 75 value as a resu
10efd 6c 74 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b lt of a rollback
10efe 2e 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 . The callback
10eff 67 69 76 65 73 20 68 69 67 68 65 72 2d 6c 65 76 gives higher-lev
10f00 65 6c 20 63 6f 64 65 0a 2a 2a 20 61 6e 20 6f 70 el code.** an op
10f01 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 72 65 73 portunity to res
10f02 74 6f 72 65 20 74 68 65 20 45 58 54 52 41 20 73 tore the EXTRA s
10f03 65 63 74 69 6f 6e 20 74 6f 20 61 67 72 65 65 20 ection to agree
10f04 77 69 74 68 20 74 68 65 20 72 65 73 74 6f 72 65 with the restore
10f05 64 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 0a d.** page data..
10f06 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
10f07 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
10f08 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 28 50 gerSetReiniter(P
10f09 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 76 6f ager *pPager, vo
10f0a 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44 62 id (*xReinit)(Db
10f0b 50 61 67 65 2a 29 29 7b 0a 20 20 70 50 61 67 65 Page*)){. pPage
10f0c 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 78 r->xReiniter = x
10f0d 52 65 69 6e 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Reinit;.}../*.**
10f0e 20 53 65 74 20 74 68 65 20 70 61 67 65 20 73 69 Set the page si
10f0f 7a 65 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65 ze to *pPageSize
10f10 2e 20 49 66 20 74 68 65 20 73 75 67 67 65 73 74 . If the suggest
10f11 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 69 new page size i
10f12 73 0a 2a 2a 20 69 6e 61 70 70 72 6f 70 72 69 61 s.** inappropria
10f13 74 65 2c 20 74 68 65 6e 20 61 6e 20 61 6c 74 65 te, then an alte
10f14 72 6e 61 74 69 76 65 20 70 61 67 65 20 73 69 7a rnative page siz
10f15 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 61 74 e is set to that
10f16 0a 2a 2a 20 76 61 6c 75 65 20 62 65 66 6f 72 65 .** value before
10f17 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 returning..*/.S
10f18 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
10f19 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 t sqlite3PagerSe
10f1a 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 20 tPagesize(Pager
10f1b 2a 70 50 61 67 65 72 2c 20 75 31 36 20 2a 70 50 *pPager, u16 *pP
10f1c 61 67 65 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 ageSize){. int
10f1d 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 rc = pPager->err
10f1e 43 6f 64 65 3b 0a 20 20 69 66 28 20 72 63 3d 3d Code;. if( rc==
10f1f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
10f20 20 75 31 36 20 70 61 67 65 53 69 7a 65 20 3d 20 u16 pageSize =
10f21 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 *pPageSize;.
10f22 61 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65 assert( pageSize
10f23 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 ==0 || (pageSize
10f24 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a >=512 && pageSiz
10f25 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 e<=SQLITE_MAX_PA
10f26 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 20 20 GE_SIZE) );.
10f27 69 66 28 20 70 61 67 65 53 69 7a 65 20 26 26 20 if( pageSize &&
10f28 70 61 67 65 53 69 7a 65 21 3d 70 50 61 67 65 72 pageSize!=pPager
10f29 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20 20 20 20 ->pageSize .
10f2a 20 26 26 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d && (pPager->mem
10f2b 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d Db==0 || pPager-
10f2c 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20 >dbSize==0).
10f2d 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 && sqlite3Pcach
10f2e 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 eRefCount(pPager
10f2f 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 ->pPCache)==0 .
10f30 20 20 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 ){. char
10f31 20 2a 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a *pNew = (char *
10f32 29 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c )sqlite3PageMall
10f33 6f 63 28 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 oc(pageSize);.
10f34 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b if( !pNew ){
10f35 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
10f36 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
10f37 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
10f38 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 pager_reset(pPa
10f39 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 70 50 ger);. pP
10f3a 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d ager->pageSize =
10f3b 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 pageSize;.
10f3c 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
10f3d 6d 65 6d 44 62 20 29 20 73 65 74 53 65 63 74 6f memDb ) setSecto
10f3e 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 rSize(pPager);.
10f3f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
10f40 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 geFree(pPager->p
10f41 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20 TmpSpace);.
10f42 20 20 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 pPager->pTmpS
10f43 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 pace = pNew;.
10f44 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 sqlite3Pcac
10f45 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 50 heSetPageSize(pP
10f46 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 ager->pPCache, p
10f47 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 ageSize);.
10f48 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 50 61 }. }. *pPa
10f49 67 65 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d geSize = pPager-
10f4a 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 >pageSize;. }.
10f4b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
10f4c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
10f4d 69 6e 74 65 72 20 74 6f 20 74 68 65 20 22 74 65 inter to the "te
10f4e 6d 70 6f 72 61 72 79 20 70 61 67 65 22 20 62 75 mporary page" bu
10f4f 66 66 65 72 20 68 65 6c 64 20 69 6e 74 65 72 6e ffer held intern
10f50 61 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 70 ally.** by the p
10f51 61 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 61 ager. This is a
10f52 20 62 75 66 66 65 72 20 74 68 61 74 20 69 73 20 buffer that is
10f53 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f big enough to ho
10f54 6c 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 ld the.** entire
10f55 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 64 61 content of a da
10f56 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 54 68 tabase page. Th
10f57 69 73 20 62 75 66 66 65 72 20 69 73 20 75 73 65 is buffer is use
10f58 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 d internally.**
10f59 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 during rollback
10f5a 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 and will be over
10f5b 77 72 69 74 74 65 6e 20 77 68 65 6e 65 76 65 72 written whenever
10f5c 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f a rollback.** o
10f5d 63 63 75 72 73 2e 20 20 42 75 74 20 6f 74 68 65 ccurs. But othe
10f5e 72 20 6d 6f 64 75 6c 65 73 20 61 72 65 20 66 72 r modules are fr
10f5f 65 65 20 74 6f 20 75 73 65 20 69 74 20 74 6f 6f ee to use it too
10f60 2c 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 , as long as.**
10f61 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61 72 65 no rollbacks are
10f62 20 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 happening..*/.S
10f63 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
10f64 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
10f65 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 20 TempSpace(Pager
10f66 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 *pPager){. retu
10f67 72 6e 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 rn pPager->pTmpS
10f68 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 pace;.}../*.** A
10f69 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20 74 68 ttempt to set th
10f6a 65 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61 e maximum databa
10f6b 73 65 20 70 61 67 65 20 63 6f 75 6e 74 20 69 66 se page count if
10f6c 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 mxPage is posit
10f6d 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f ive. .** Make no
10f6e 20 63 68 61 6e 67 65 73 20 69 66 20 6d 78 50 61 changes if mxPa
10f6f 67 65 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 ge is zero or ne
10f70 67 61 74 69 76 65 2e 20 20 41 6e 64 20 6e 65 76 gative. And nev
10f71 65 72 20 72 65 64 75 63 65 20 74 68 65 0a 2a 2a er reduce the.**
10f72 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f maximum page co
10f73 75 6e 74 20 62 65 6c 6f 77 20 74 68 65 20 63 75 unt below the cu
10f74 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 rrent size of th
10f75 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a e database..**.*
10f76 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 * Regardless of
10f77 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 mxPage, return t
10f78 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d he current maxim
10f79 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a um page count..*
10f7a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10f7b 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
10f7c 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 rMaxPageCount(Pa
10f7d 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
10f7e 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 66 28 20 mxPage){. if(
10f7f 6d 78 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 mxPage>0 ){.
10f80 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d pPager->mxPgno =
10f81 20 6d 78 50 61 67 65 3b 0a 20 20 7d 0a 20 20 73 mxPage;. }. s
10f82 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
10f83 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b ount(pPager, 0);
10f84 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 . return pPager
10f85 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a ->mxPgno;.}../*.
10f86 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
10f87 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 set of routines
10f88 20 61 72 65 20 75 73 65 64 20 74 6f 20 64 69 73 are used to dis
10f89 61 62 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74 able the simulat
10f8a 65 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 ed.** I/O error
10f8b 6d 65 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 mechanism. Thes
10f8c 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 e routines are u
10f8d 73 65 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d sed to avoid sim
10f8e 75 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 ulated.** errors
10f8f 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 in places where
10f90 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 we do not care
10f91 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a about errors..**
10f92 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c .** Unless -DSQL
10f93 49 54 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73 ITE_TEST=1 is us
10f94 65 64 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e ed, these routin
10f95 65 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 es are all no-op
10f96 73 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 s.** and generat
10f97 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 e no code..*/.#i
10f98 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
10f99 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 .SQLITE_API exte
10f9a 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 rn int sqlite3_i
10f9b 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b o_error_pending;
10f9c 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 .SQLITE_API exte
10f9d 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 rn int sqlite3_i
10f9e 6f 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61 o_error_hit;.sta
10f9f 74 69 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e tic int saved_cn
10fa0 74 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f t;.void disable_
10fa1 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
10fa2 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 61 76 ors(void){. sav
10fa3 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 ed_cnt = sqlite3
10fa4 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
10fa5 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f g;. sqlite3_io_
10fa6 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 error_pending =
10fa7 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c -1;.}.void enabl
10fa8 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
10fa9 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 rrors(void){. s
10faa 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
10fab 70 65 6e 64 69 6e 67 20 3d 20 73 61 76 65 64 5f pending = saved_
10fac 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 cnt;.}.#else.# d
10fad 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 efine disable_si
10fae 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
10faf 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 s().# define ena
10fb0 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
10fb1 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 _errors().#endif
10fb2 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 ../*.** Read the
10fb3 20 66 69 72 73 74 20 4e 20 62 79 74 65 73 20 66 first N bytes f
10fb4 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
10fb5 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e g of the file in
10fb6 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 to memory.** tha
10fb7 74 20 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74 t pDest points t
10fb8 6f 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 20 65 72 72 o. .**.** No err
10fb9 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 or checking is d
10fba 6f 6e 65 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 one. The rationa
10fbb 6c 20 66 6f 72 20 74 68 69 73 20 69 73 20 74 68 l for this is th
10fbc 61 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e at this function
10fbd 20 0a 2a 2a 20 6d 61 79 20 62 65 20 63 61 6c 6c .** may be call
10fbe 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20 66 ed even if the f
10fbf 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 ile does not exi
10fc0 73 74 20 6f 72 20 63 6f 6e 74 61 69 6e 20 61 20 st or contain a
10fc1 68 65 61 64 65 72 2e 20 49 6e 20 0a 2a 2a 20 74 header. In .** t
10fc2 68 65 73 65 20 63 61 73 65 73 20 73 71 6c 69 74 hese cases sqlit
10fc3 65 33 4f 73 52 65 61 64 28 29 20 77 69 6c 6c 20 e3OsRead() will
10fc4 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2c return an error,
10fc5 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 63 6f to which the co
10fc6 72 72 65 63 74 20 0a 2a 2a 20 72 65 73 70 6f 6e rrect .** respon
10fc7 73 65 20 69 73 20 74 6f 20 7a 65 72 6f 20 74 68 se is to zero th
10fc8 65 20 6d 65 6d 6f 72 79 20 61 74 20 70 44 65 73 e memory at pDes
10fc9 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 65 2e 20 t and continue.
10fca 20 41 20 72 65 61 6c 20 49 4f 20 65 72 72 6f 72 A real IO error
10fcb 20 0a 2a 2a 20 77 69 6c 6c 20 70 72 65 73 75 6d .** will presum
10fcc 61 62 6c 79 20 72 65 63 75 72 20 61 6e 64 20 62 ably recur and b
10fcd 65 20 70 69 63 6b 65 64 20 75 70 20 6c 61 74 65 e picked up late
10fce 72 20 28 54 6f 64 6f 3a 20 54 68 69 6e 6b 20 61 r (Todo: Think a
10fcf 62 6f 75 74 20 74 68 69 73 29 2e 0a 2a 2f 0a 53 bout this)..*/.S
10fd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
10fd1 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 t sqlite3PagerRe
10fd2 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 61 67 adFileheader(Pag
10fd3 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
10fd4 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 N, unsigned char
10fd5 20 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e 74 20 *pDest){. int
10fd6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
10fd7 20 20 6d 65 6d 73 65 74 28 70 44 65 73 74 2c 20 memset(pDest,
10fd8 30 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72 74 28 0, N);. assert(
10fd9 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 pPager->fd->pMet
10fda 68 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e 74 65 hods||pPager->te
10fdb 6d 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 mpFile);. if( p
10fdc 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 Pager->fd->pMeth
10fdd 6f 64 73 20 29 7b 0a 20 20 20 20 49 4f 54 52 41 ods ){. IOTRA
10fde 43 45 28 28 22 44 42 48 44 52 20 25 70 20 30 20 CE(("DBHDR %p 0
10fdf 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 4e %d\n", pPager, N
10fe0 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 )). rc = sqli
10fe1 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 te3OsRead(pPager
10fe2 2d 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c 20 ->fd, pDest, N,
10fe3 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 0);. if( rc==
10fe4 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
10fe5 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 RT_READ ){.
10fe6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
10fe7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
10fe8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
10fe9 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 Return the tota
10fea 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
10feb 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 69 s in the disk fi
10fec 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 le associated wi
10fed 74 68 0a 2a 2a 20 70 50 61 67 65 72 2e 20 0a 2a th.** pPager. .*
10fee 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 45 4e 44 *.** If the PEND
10fef 49 4e 47 5f 42 59 54 45 20 6c 69 65 73 20 6f 6e ING_BYTE lies on
10ff0 20 74 68 65 20 70 61 67 65 20 64 69 72 65 63 74 the page direct
10ff1 6c 79 20 61 66 74 65 72 20 74 68 65 20 65 6e 64 ly after the end
10ff2 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 2c of the.** file,
10ff3 20 74 68 65 6e 20 63 6f 6e 73 69 64 65 72 20 74 then consider t
10ff4 68 69 73 20 70 61 67 65 20 70 61 72 74 20 6f 66 his page part of
10ff5 20 74 68 65 20 66 69 6c 65 20 74 6f 6f 2e 20 46 the file too. F
10ff6 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 0a 2a or example, if.*
10ff7 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 69 * PENDING_BYTE i
10ff8 73 20 62 79 74 65 20 34 30 39 36 20 28 74 68 65 s byte 4096 (the
10ff9 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 70 first byte of p
10ffa 61 67 65 20 35 29 20 61 6e 64 20 74 68 65 20 73 age 5) and the s
10ffb 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 ize of the.** fi
10ffc 6c 65 20 69 73 20 34 30 39 36 20 62 79 74 65 73 le is 4096 bytes
10ffd 2c 20 35 20 69 73 20 72 65 74 75 72 6e 65 64 20 , 5 is returned
10ffe 69 6e 73 74 65 61 64 20 6f 66 20 34 2e 0a 2a 2f instead of 4..*/
10fff 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
11000 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
11001 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72 20 Pagecount(Pager
11002 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e *pPager, int *pn
11003 50 61 67 65 29 7b 0a 20 20 69 36 34 20 6e 20 3d Page){. i64 n =
11004 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 0;. int rc;.
11005 61 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d assert( pPager!=
11006 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 0 );. if( pPage
11007 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 r->errCode ){.
11008 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 rc = pPager->e
11009 72 72 43 6f 64 65 3b 0a 20 20 20 20 72 65 74 75 rrCode;. retu
1100a 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 rn rc;. }. if(
1100b 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
1100c 61 6c 69 64 20 29 7b 0a 20 20 20 20 6e 20 3d 20 alid ){. n =
1100d 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a pPager->dbSize;.
1100e 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 61 } else {. a
1100f 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66 64 ssert(pPager->fd
11010 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67 ->pMethods||pPag
11011 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 er->tempFile);.
11012 20 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e if( (pPager->
11013 66 64 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 20 20 fd->pMethods).
11014 20 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 && (rc = sqli
11015 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 te3OsFileSize(pP
11016 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 29 21 3d ager->fd, &n))!=
11017 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
11018 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 pager_error(p
11019 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 Pager, rc);.
1101a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1101b 20 7d 0a 20 20 20 20 69 66 28 20 6e 3e 30 20 26 }. if( n>0 &
1101c 26 20 6e 3c 70 50 61 67 65 72 2d 3e 70 61 67 65 & n<pPager->page
1101d 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6e 20 Size ){. n
1101e 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 1;. }else{.
1101f 20 20 20 20 20 20 6e 20 2f 3d 20 70 50 61 67 65 n /= pPage
11020 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 r->pageSize;.
11021 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 }. if( pPage
11022 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f r->state!=PAGER_
11023 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 UNLOCK ){.
11024 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
11025 20 6e 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 n;. pPager
11026 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 ->dbSizeValid =
11027 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 1;. }. }. i
11028 66 28 20 6e 3d 3d 28 50 45 4e 44 49 4e 47 5f 42 f( n==(PENDING_B
11029 59 54 45 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 YTE/pPager->page
1102a 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 6e 2b 2b Size) ){. n++
1102b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 70 50 ;. }. if( n>pP
1102c 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a ager->mxPgno ){.
1102d 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 pPager->mxPg
1102e 6e 6f 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 69 66 no = n;. }. if
1102f 28 20 70 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 ( pnPage ){.
11030 2a 70 6e 50 61 67 65 20 3d 20 6e 3b 0a 20 20 7d *pnPage = n;. }
11031 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
11032 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f _OK;.}../*.** Fo
11033 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f rward declaratio
11034 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 n.*/.static int
11035 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65 syncJournal(Page
11036 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 r*);../*.** This
11037 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
11038 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 to truncate the
11039 20 63 61 63 68 65 20 77 68 65 6e 20 61 20 64 61 cache when a da
1103a 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 74 72 75 tabase.** is tru
1103b 6e 63 61 74 65 64 2e 20 20 44 72 6f 70 20 66 72 ncated. Drop fr
1103c 6f 6d 20 74 68 65 20 63 61 63 68 65 20 61 6c 6c om the cache all
1103d 20 70 61 67 65 73 20 77 68 6f 73 65 20 70 67 6e pages whose pgn
1103e 6f 20 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 o is.** larger t
1103f 68 61 6e 20 70 50 61 67 65 72 2d 3e 64 62 53 69 han pPager->dbSi
11040 7a 65 20 61 6e 64 20 69 73 20 75 6e 72 65 66 65 ze and is unrefe
11041 72 65 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 renced..**.** Re
11042 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 6c ferenced pages l
11043 61 72 67 65 72 20 74 68 61 6e 20 70 50 61 67 65 arger than pPage
11044 72 2d 3e 64 62 53 69 7a 65 20 61 72 65 20 7a 65 r->dbSize are ze
11045 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 63 74 75 roed..**.** Actu
11046 61 6c 6c 79 2c 20 61 74 20 74 68 65 20 70 6f 69 ally, at the poi
11047 6e 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nt this routine
11048 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 77 6f is called, it wo
11049 75 6c 64 20 62 65 0a 2a 2a 20 61 6e 20 65 72 72 uld be.** an err
1104a 6f 72 20 74 6f 20 68 61 76 65 20 61 20 72 65 66 or to have a ref
1104b 65 72 65 6e 63 65 64 20 70 61 67 65 2e 20 20 42 erenced page. B
1104c 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 64 ut rather than d
1104d 65 6c 65 74 65 0a 2a 2a 20 74 68 61 74 20 70 61 elete.** that pa
1104e 67 65 20 61 6e 64 20 67 75 61 72 61 6e 74 65 65 ge and guarantee
1104f 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 73 65 a subsequent se
11050 67 66 61 75 6c 74 2c 20 69 74 20 73 65 65 6d 73 gfault, it seems
11051 20 62 65 74 74 65 72 0a 2a 2a 20 74 6f 20 7a 65 better.** to ze
11052 72 6f 20 69 74 20 61 6e 64 20 68 6f 70 65 20 74 ro it and hope t
11053 68 61 74 20 77 65 20 65 72 72 6f 72 20 6f 75 74 hat we error out
11054 20 73 61 6e 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 sanely..*/.stat
11055 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 74 72 ic void pager_tr
11056 75 6e 63 61 74 65 5f 63 61 63 68 65 28 50 61 67 uncate_cache(Pag
11057 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 er *pPager){. s
11058 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e qlite3PcacheTrun
11059 63 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 43 cate(pPager->pPC
1105a 61 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64 62 ache, pPager->db
1105b 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Size);.}../*.**
1105c 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 Try to obtain a
1105d 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 2e 20 lock on a file.
1105e 20 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 Invoke the busy
1105f 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 callback if the
11060 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 63 75 72 72 lock.** is curr
11061 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 ently not availa
11062 62 6c 65 2e 20 20 52 65 70 65 61 74 20 75 6e 74 ble. Repeat unt
11063 69 6c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c il the busy call
11064 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 back returns.**
11065 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 false or until t
11066 68 65 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73 he lock succeeds
11067 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 ..**.** Return S
11068 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
11069 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 ess and an error
1106a 20 63 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e code if we cann
1106b 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65 ot obtain.** the
1106c 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 lock..*/.static
1106d 20 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f int pager_wait_
1106e 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 on_lock(Pager *p
1106f 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 Pager, int lockt
11070 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ype){. int rc;.
11071 0a 20 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 . /* The OS loc
11072 6b 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 k values must be
11073 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 the same as the
11074 20 50 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 Pager lock valu
11075 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 es */. assert(
11076 50 41 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 PAGER_SHARED==SH
11077 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
11078 73 73 65 72 74 28 20 50 41 47 45 52 5f 52 45 53 ssert( PAGER_RES
11079 45 52 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f ERVED==RESERVED_
1107a 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
1107b 28 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 ( PAGER_EXCLUSIV
1107c 45 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 E==EXCLUSIVE_LOC
1107d 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 K );.. /* If th
1107e 65 20 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e e file is curren
1107f 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 tly unlocked the
11080 6e 20 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20 n the size must
11081 62 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 be unknown */.
11082 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
11083 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 state>=PAGER_SHA
11084 52 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 RED || pPager->d
11085 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b bSizeValid==0 );
11086 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e .. if( pPager->
11087 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 state>=locktype
11088 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
11089 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
1108a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 do {. r
1108b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 c = sqlite3OsLoc
1108c 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f k(pPager->fd, lo
1108d 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 cktype);. }wh
1108e 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
1108f 42 55 53 59 20 26 26 20 70 50 61 67 65 72 2d 3e BUSY && pPager->
11090 78 42 75 73 79 48 61 6e 64 6c 65 72 28 70 50 61 xBusyHandler(pPa
11091 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 ger->pBusyHandle
11092 72 41 72 67 29 20 29 3b 0a 20 20 20 20 69 66 28 rArg) );. if(
11093 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
11094 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e {. pPager->
11095 73 74 61 74 65 20 3d 20 6c 6f 63 6b 74 79 70 65 state = locktype
11096 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 ;. IOTRACE(
11097 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 2c ("LOCK %p %d\n",
11098 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70 pPager, locktyp
11099 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e)). }. }.
1109a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1109b 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 .** Truncate the
1109c 20 66 69 6c 65 20 74 6f 20 74 68 65 20 6e 75 6d file to the num
1109d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 70 65 ber of pages spe
1109e 63 69 66 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 cified..*/.SQLIT
1109f 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
110a0 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 lite3PagerTrunca
110a1 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 te(Pager *pPager
110a2 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 , Pgno nPage){.
110a3 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
110a4 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 _OK;. assert( p
110a5 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
110a6 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 GER_SHARED );..
110a7 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
110a8 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 ecount(pPager, 0
110a9 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d );. if( pPager-
110aa 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 >errCode ){.
110ab 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 rc = pPager->err
110ac 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Code;. }else if
110ad 28 20 6e 50 61 67 65 3c 70 50 61 67 65 72 2d 3e ( nPage<pPager->
110ae 64 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 dbSize ){. rc
110af 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 = syncJournal(p
110b0 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 Pager);. if(
110b1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
110b2 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e . /* Get an
110b3 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
110b4 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
110b5 62 65 66 6f 72 65 20 74 72 75 6e 63 61 74 69 6e before truncatin
110b6 67 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d g. */. rc =
110b7 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c pager_wait_on_l
110b8 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58 43 4c ock(pPager, EXCL
110b9 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 USIVE_LOCK);.
110ba 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
110bb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
110bc 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 rc = pager_tru
110bd 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 6e 50 ncate(pPager, nP
110be 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a age);. }. }.
110bf 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
110c0 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 ./*.** Shutdown
110c1 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 the page cache.
110c2 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 Free all memory
110c3 20 61 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 and close all f
110c4 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 iles..**.** If a
110c5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
110c6 20 69 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65 in progress whe
110c7 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
110c8 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a s called, that.*
110c9 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 * transaction is
110ca 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 rolled back. A
110cb 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 ll outstanding p
110cc 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 ages are invalid
110cd 61 74 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 ated.** and thei
110ce 72 20 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65 r memory is free
110cf 64 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 d. Any attempt
110d0 74 6f 20 75 73 65 20 61 20 70 61 67 65 20 61 73 to use a page as
110d1 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 sociated.** with
110d2 20 74 68 69 73 20 70 61 67 65 20 63 61 63 68 65 this page cache
110d3 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 after this func
110d4 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c tion returns wil
110d5 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 l likely.** resu
110d6 6c 74 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 lt in a coredump
110d7 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
110d8 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 ction always suc
110d9 63 65 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e ceeds. If a tran
110da 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 saction is activ
110db 65 20 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 e an attempt.**
110dc 69 73 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 is made to roll
110dd 69 74 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 it back. If an e
110de 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
110df 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ng the rollback
110e0 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 .** a hot journa
110e1 6c 20 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e l may be left in
110e2 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 the filesystem
110e3 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 but no error is
110e4 72 65 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 returned.** to t
110e5 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 he caller..*/.SQ
110e6 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
110e7 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
110e8 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 se(Pager *pPager
110e9 29 7b 0a 0a 20 20 64 69 73 61 62 6c 65 5f 73 69 ){.. disable_si
110ea 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
110eb 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 s();. sqlite3Be
110ec 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
110ed 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 72 72 );. pPager->err
110ee 43 6f 64 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 Code = 0;. pPag
110ef 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
110f0 65 20 3d 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 e = 0;. pager_r
110f1 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 eset(pPager);.
110f2 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 if( !MEMDB ){.
110f3 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 pagerUnlockAnd
110f4 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 Rollback(pPager)
110f5 3b 0a 20 20 7d 0a 20 20 65 6e 61 62 6c 65 5f 73 ;. }. enable_s
110f6 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
110f7 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 rs();. sqlite3E
110f8 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
110f9 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 32 28 ;. PAGERTRACE2(
110fa 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 41 "CLOSE %d\n", PA
110fb 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a GERID(pPager));.
110fc 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 4f 53 IOTRACE(("CLOS
110fd 45 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 E %p\n", pPager)
110fe 29 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ). if( pPager->
110ff 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 journalOpen ){.
11100 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
11101 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a e(pPager->jfd);.
11102 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 69 74 }. sqlite3Bit
11103 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 vecDestroy(pPage
11104 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a r->pInJournal);.
11105 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
11106 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 estroy(pPager->p
11107 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 29 3b AlwaysRollback);
11108 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
11109 74 6d 74 4f 70 65 6e 20 29 7b 0a 20 20 20 20 73 tmtOpen ){. s
1110a 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
1110b 61 67 65 72 2d 3e 73 74 66 64 29 3b 0a 20 20 7d ager->stfd);. }
1110c 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 . sqlite3OsClos
1110d 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 e(pPager->fd);.
1110e 20 2f 2a 20 54 65 6d 70 20 66 69 6c 65 73 20 61 /* Temp files a
1110f 72 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 re automatically
11110 20 64 65 6c 65 74 65 64 20 62 79 20 74 68 65 20 deleted by the
11111 4f 53 0a 20 20 2a 2a 20 69 66 28 20 70 50 61 67 OS. ** if( pPag
11112 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a er->tempFile ){.
11113 20 20 2a 2a 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
11114 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 7a Delete(pPager->z
11115 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 2a 2a 20 Filename);. **
11116 7d 0a 20 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 }. */.. sqlite
11117 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 3PageFree(pPager
11118 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 ->pTmpSpace);.
11119 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f sqlite3PcacheClo
1111a 73 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 se(pPager->pPCac
1111b 68 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 he);. sqlite3_f
1111c 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 ree(pPager);. r
1111d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1111e 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .}..#if !defined
1111f 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 (NDEBUG) || defi
11120 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
11121 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
11122 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f e page number fo
11123 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 r the given page
11124 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 data..*/.SQLITE
11125 5f 50 52 49 56 41 54 45 20 50 67 6e 6f 20 73 71 _PRIVATE Pgno sq
11126 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 lite3PagerPagenu
11127 6d 62 65 72 28 44 62 50 61 67 65 20 2a 70 29 7b mber(DbPage *p){
11128 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 67 6e . return p->pgn
11129 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a o;.}.#endif../*.
1112a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 ** Increment the
1112b 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
1112c 20 66 6f 72 20 61 20 70 61 67 65 2e 20 20 54 68 for a page. Th
1112d 65 20 69 6e 70 75 74 20 70 6f 69 6e 74 65 72 20 e input pointer
1112e 69 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 is.** a referenc
1112f 65 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 61 e to the page da
11130 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ta..*/.SQLITE_PR
11131 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
11132 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 65 3PagerRef(DbPage
11133 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 *pPg){. sqlite
11134 33 50 63 61 63 68 65 52 65 66 28 70 50 67 29 3b 3PcacheRef(pPg);
11135 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
11136 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 _OK;.}../*.** Sy
11137 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 nc the journal.
11138 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
11139 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 make sure all t
1113a 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68 61 he pages that ha
1113b 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74 74 ve.** been writt
1113c 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 en to the journa
1113d 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79 20 l have actually
1113e 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72 66 reached the surf
1113f 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 69 ace of the.** di
11140 73 6b 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 73 sk. It is not s
11141 61 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 afe to modify th
11142 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 e original datab
11143 61 73 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 61 ase file until a
11144 66 74 65 72 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 fter.** the jour
11145 6e 61 6c 20 68 61 73 20 62 65 65 6e 20 73 79 6e nal has been syn
11146 63 65 64 2e 20 20 49 66 20 74 68 65 20 6f 72 69 ced. If the ori
11147 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 69 ginal database i
11148 73 20 6d 6f 64 69 66 69 65 64 20 62 65 66 6f 72 s modified befor
11149 65 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c e.** the journal
1114a 20 69 73 20 73 79 6e 63 65 64 20 61 6e 64 20 61 is synced and a
1114b 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f power failure o
1114c 63 63 75 72 73 2c 20 74 68 65 20 75 6e 73 79 6e ccurs, the unsyn
1114d 63 65 64 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 64 ced journal.** d
1114e 61 74 61 20 77 6f 75 6c 64 20 62 65 20 6c 6f 73 ata would be los
1114f 74 20 61 6e 64 20 77 65 20 77 6f 75 6c 64 20 62 t and we would b
11150 65 20 75 6e 61 62 6c 65 20 74 6f 20 63 6f 6d 70 e unable to comp
11151 6c 65 74 65 6c 79 20 72 6f 6c 6c 62 61 63 6b 20 letely rollback
11152 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
11153 63 68 61 6e 67 65 73 2e 20 20 44 61 74 61 62 61 changes. Databa
11154 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 6f se corruption wo
11155 75 6c 64 20 6f 63 63 75 72 2e 0a 2a 2a 20 0a 2a uld occur..** .*
11156 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 * This routine a
11157 6c 73 6f 20 75 70 64 61 74 65 73 20 74 68 65 20 lso updates the
11158 6e 52 65 63 20 66 69 65 6c 64 20 69 6e 20 74 68 nRec field in th
11159 65 20 68 65 61 64 65 72 20 6f 66 20 74 68 65 20 e header of the
1115a 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 28 53 65 65 journal..** (See
1115b 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 comments on the
1115c 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
1115d 29 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 64 ) routine for ad
1115e 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
1115f 74 69 6f 6e 2e 29 0a 2a 2a 20 49 66 20 74 68 65 tion.).** If the
11160 20 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 46 55 sync mode is FU
11161 4c 4c 2c 20 74 77 6f 20 73 79 6e 63 73 20 77 69 LL, two syncs wi
11162 6c 6c 20 6f 63 63 75 72 2e 20 20 46 69 72 73 74 ll occur. First
11163 20 74 68 65 20 77 68 6f 6c 65 20 6a 6f 75 72 6e the whole journ
11164 61 6c 0a 2a 2a 20 69 73 20 73 79 6e 63 65 64 2c al.** is synced,
11165 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66 then the nRec f
11166 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2c ield is updated,
11167 20 74 68 65 6e 20 61 20 73 65 63 6f 6e 64 20 73 then a second s
11168 79 6e 63 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a ync occurs..**.*
11169 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 * For temporary
1116a 64 61 74 61 62 61 73 65 73 2c 20 77 65 20 64 6f databases, we do
1116b 20 6e 6f 74 20 63 61 72 65 20 69 66 20 77 65 20 not care if we
1116c 61 72 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c are able to roll
1116d 62 61 63 6b 0a 2a 2a 20 61 66 74 65 72 20 61 20 back.** after a
1116e 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 73 power failure, s
1116f 6f 20 6e 6f 20 73 79 6e 63 20 6f 63 63 75 72 73 o no sync occurs
11170 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 49 ..**.** If the I
11171 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 OCAP_SEQUENTIAL
11172 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 20 flag is set for
11173 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d the persistent m
11174 65 64 69 61 20 6f 6e 20 77 68 69 63 68 0a 2a 2a edia on which.**
11175 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
11176 20 73 74 6f 72 65 64 2c 20 74 68 65 6e 20 4f 73 stored, then Os
11177 53 79 6e 63 28 29 20 69 73 20 6e 65 76 65 72 20 Sync() is never
11178 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6a 6f called on the jo
11179 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 49 urnal.** file. I
1117a 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20 n this case all
1117b 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 that is required
1117c 20 69 73 20 74 6f 20 75 70 64 61 74 65 20 74 68 is to update th
1117d 65 20 6e 52 65 63 20 66 69 65 6c 64 20 69 6e 0a e nRec field in.
1117e 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 ** the journal h
1117f 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 eader..**.** Thi
11180 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 s routine clears
11181 20 74 68 65 20 6e 65 65 64 53 79 6e 63 20 66 69 the needSync fi
11182 65 6c 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 eld of every pag
11183 65 20 63 75 72 72 65 6e 74 20 68 65 6c 64 20 69 e current held i
11184 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a n.** memory..*/.
11185 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63 4a static int syncJ
11186 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 ournal(Pager *pP
11187 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 ager){. int rc
11188 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
11189 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 /* Sync the jour
1118a 6e 61 6c 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 nal before modif
1118b 79 69 6e 67 20 74 68 65 20 6d 61 69 6e 20 64 61 ying the main da
1118c 74 61 62 61 73 65 0a 20 20 2a 2a 20 28 61 73 73 tabase. ** (ass
1118d 75 6d 69 6e 67 20 74 68 65 72 65 20 69 73 20 61 uming there is a
1118e 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 74 20 journal and it
1118f 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 needs to be sync
11190 65 64 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ed.). */. if(
11191 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
11192 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
11193 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
11194 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 e );. if( pPa
11195 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
11196 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d !=PAGER_JOURNALM
11197 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 ODE_MEMORY ){.
11198 20 20 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71 int iDc = sq
11199 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
1119a 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 racteristics(pPa
1119b 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 ger->fd);.
1119c 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
1119d 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 0a journalOpen );..
1119e 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 if( 0==(iD
1119f 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
111a0 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 AFE_APPEND) ){.
111a1 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
111a2 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 the nRec value i
111a3 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
111a4 66 69 6c 65 20 68 65 61 64 65 72 2e 20 49 66 20 file header. If
111a5 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 in. ** fu
111a6 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d ll-synchronous m
111a7 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f ode, sync the jo
111a8 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 54 68 69 urnal first. Thi
111a9 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 0a 20 s ensures that.
111aa 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 ** all da
111ab 74 61 20 68 61 73 20 72 65 61 6c 6c 79 20 68 69 ta has really hi
111ac 74 20 74 68 65 20 64 69 73 6b 20 62 65 66 6f 72 t the disk befor
111ad 65 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 e nRec is update
111ae 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 d to mark.
111af 20 20 2a 2a 20 69 74 20 61 73 20 61 20 63 61 6e ** it as a can
111b0 64 69 64 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 didate for rollb
111b1 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a ack.. **.
111b2 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 ** This
111b3 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 is not required
111b4 69 66 20 74 68 65 20 70 65 72 73 69 73 74 65 6e if the persisten
111b5 74 20 6d 65 64 69 61 20 73 75 70 70 6f 72 74 73 t media supports
111b6 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 the. **
111b7 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 SAFE_APPEND prop
111b8 65 72 74 79 2e 20 42 65 63 61 75 73 65 20 69 6e erty. Because in
111b9 20 74 68 69 73 20 63 61 73 65 20 69 74 20 69 73 this case it is
111ba 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 not possible .
111bb 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 ** for ga
111bc 72 62 61 67 65 20 64 61 74 61 20 74 6f 20 62 65 rbage data to be
111bd 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 appended to the
111be 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 file, the nRec
111bf 66 69 65 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a field. **
111c0 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 is populated wi
111c1 74 68 20 30 78 46 46 46 46 46 46 46 46 20 77 68 th 0xFFFFFFFF wh
111c2 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 en the journal h
111c3 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e eader is written
111c4 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 . ** and
111c5 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62 never needs to b
111c6 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 20 e updated..
111c7 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 */. i6
111c8 34 20 6a 72 6e 6c 4f 66 66 3b 0a 20 20 20 20 20 4 jrnlOff;.
111c9 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 if( pPager->f
111ca 75 6c 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 ullSync && 0==(i
111cb 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f Dc&SQLITE_IOCAP_
111cc 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 SEQUENTIAL) ){.
111cd 20 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 PAGERTR
111ce 41 43 45 32 28 22 53 59 4e 43 20 6a 6f 75 72 6e ACE2("SYNC journ
111cf 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 al of %d\n", PAG
111d0 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20 ERID(pPager));.
111d1 20 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 IOTRACE
111d2 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 (("JSYNC %p\n",
111d3 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 pPager)).
111d4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
111d5 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 sSync(pPager->jf
111d6 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f d, pPager->sync_
111d7 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 flags);.
111d8 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 if( rc!=0 ) re
111d9 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
111da 20 7d 0a 0a 20 20 20 20 20 20 20 20 6a 72 6e 6c }.. jrnl
111db 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f Off = pPager->jo
111dc 75 72 6e 61 6c 48 64 72 20 2b 20 73 69 7a 65 6f urnalHdr + sizeo
111dd 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
111de 3b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 ;. IOTRAC
111df 45 28 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 E(("JHDR %p %lld
111e0 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %d\n", pPager,
111e1 6a 72 6e 6c 4f 66 66 2c 20 34 29 29 3b 0a 20 20 jrnlOff, 4));.
111e2 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 rc = write
111e3 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
111e4 66 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 70 50 61 fd, jrnlOff, pPa
111e5 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 20 20 20 20 ger->nRec);.
111e6 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
111e7 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
111e8 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 if( 0==(iD
111e9 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
111ea 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 EQUENTIAL) ){.
111eb 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
111ec 32 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 2("SYNC journal
111ed 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 of %d\n", PAGERI
111ee 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 D(pPager));.
111ef 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 IOTRACE(("JS
111f0 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 YNC %p\n", pPage
111f1 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 20 3d r)). rc =
111f2 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
111f3 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 Pager->jfd, pPag
111f4 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c 20 er->sync_flags|
111f5 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61 67 . (pPag
111f6 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d 3d er->sync_flags==
111f7 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c SQLITE_SYNC_FULL
111f8 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 ?SQLITE_SYNC_DAT
111f9 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 20 AONLY:0).
111fa 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
111fb 72 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20 72 rc!=0 ) return r
111fc 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 c;. }.
111fd 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
111fe 53 74 61 72 74 65 64 20 3d 20 31 3b 0a 20 20 20 Started = 1;.
111ff 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e }. pPager->n
11200 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 eedSync = 0;..
11201 20 20 2f 2a 20 45 72 61 73 65 20 74 68 65 20 6e /* Erase the n
11202 65 65 64 53 79 6e 63 20 66 6c 61 67 20 66 72 6f eedSync flag fro
11203 6d 20 65 76 65 72 79 20 70 61 67 65 2e 0a 20 20 m every page..
11204 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
11205 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46 PcacheClearSyncF
11206 6c 61 67 73 28 70 50 61 67 65 72 2d 3e 70 50 43 lags(pPager->pPC
11207 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 ache);. }.. re
11208 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
11209 2a 20 47 69 76 65 6e 20 61 20 6c 69 73 74 20 6f * Given a list o
1120a 66 20 70 61 67 65 73 20 28 63 6f 6e 6e 65 63 74 f pages (connect
1120b 65 64 20 62 79 20 74 68 65 20 50 67 48 64 72 2e ed by the PgHdr.
1120c 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 29 20 pDirty pointer)
1120d 77 72 69 74 65 0a 2a 2a 20 65 76 65 72 79 20 6f write.** every o
1120e 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 67 65 ne of those page
1120f 73 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 s out to the dat
11210 61 62 61 73 65 20 66 69 6c 65 2e 20 4e 6f 20 63 abase file. No c
11211 61 6c 6c 73 20 61 72 65 20 6d 61 64 65 0a 2a 2a alls are made.**
11212 20 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63 to the page-cac
11213 68 65 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 he to mark the p
11214 61 67 65 73 20 61 73 20 63 6c 65 61 6e 2e 20 49 ages as clean. I
11215 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 t is the respons
11216 69 62 69 6c 69 74 79 0a 2a 2a 20 6f 66 20 74 68 ibility.** of th
11217 65 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 e caller to use
11218 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 29 PcacheCleanAll()
11219 20 6f 72 20 50 63 61 63 68 65 4d 61 6b 65 43 6c or PcacheMakeCl
1121a 65 61 6e 28 29 20 74 6f 20 6d 61 72 6b 0a 2a 2a ean() to mark.**
1121b 20 74 68 65 20 70 61 67 65 73 20 61 73 20 63 6c the pages as cl
1121c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ean..*/.static i
1121d 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 nt pager_write_p
1121e 61 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70 agelist(PgHdr *p
1121f 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 72 20 2a List){. Pager *
11220 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 pPager;. int rc
11221 3b 0a 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d ;.. if( pList==
11222 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
11223 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20 3d E_OK;. pPager =
11224 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b 0a pList->pPager;.
11225 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f . /* At this po
11226 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62 65 int there may be
11227 20 65 69 74 68 65 72 20 61 20 52 45 53 45 52 56 either a RESERV
11228 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 ED or EXCLUSIVE
11229 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a lock on the. **
1122a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
1122b 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 If there is alre
1122c 61 64 79 20 61 6e 20 45 58 43 4c 55 53 49 56 45 ady an EXCLUSIVE
1122d 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c 6f lock, the follo
1122e 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 73 20 wing. ** calls
1122f 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b to sqlite3OsLock
11230 28 29 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 20 () are no-ops..
11231 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 **. ** Moving
11232 74 68 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 the lock from RE
11233 53 45 52 56 45 44 20 74 6f 20 45 58 43 4c 55 53 SERVED to EXCLUS
11234 49 56 45 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 IVE actually inv
11235 6f 6c 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a olves going. **
11236 20 74 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 through an inte
11237 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 20 50 rmediate state P
11238 45 4e 44 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 ENDING. A PEND
11239 49 4e 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 ING lock prevent
1123a 73 20 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 s new. ** reade
1123b 72 73 20 66 72 6f 6d 20 61 74 74 61 63 68 69 6e rs from attachin
1123c 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 g to the databas
1123d 65 20 62 75 74 20 69 73 20 75 6e 73 75 66 66 69 e but is unsuffi
1123e 63 69 65 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a cient for us to.
1123f 20 20 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 65 ** write. The
11240 20 69 64 65 61 20 6f 66 20 61 20 50 45 4e 44 49 idea of a PENDI
11241 4e 47 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 NG lock is to pr
11242 65 76 65 6e 74 20 6e 65 77 20 72 65 61 64 65 72 event new reader
11243 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 s from. ** comi
11244 6e 67 20 69 6e 20 77 68 69 6c 65 20 77 65 20 77 ng in while we w
11245 61 69 74 20 66 6f 72 20 65 78 69 73 74 69 6e 67 ait for existing
11246 20 72 65 61 64 65 72 73 20 74 6f 20 63 6c 65 61 readers to clea
11247 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 r.. **. ** Whi
11248 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 le the pager is
11249 69 6e 20 74 68 65 20 52 45 53 45 52 56 45 44 20 in the RESERVED
1124a 73 74 61 74 65 2c 20 74 68 65 20 6f 72 69 67 69 state, the origi
1124b 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c nal database fil
1124c 65 0a 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e e. ** is unchan
1124d 67 65 64 20 61 6e 64 20 77 65 20 63 61 6e 20 72 ged and we can r
1124e 6f 6c 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 20 ollback without
1124f 68 61 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 having to playba
11250 63 6b 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 ck the. ** jour
11251 6e 61 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 nal into the ori
11252 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 ginal database f
11253 69 6c 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 ile. Once we tr
11254 61 6e 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a ansition to. **
11255 20 45 58 43 4c 55 53 49 56 45 2c 20 69 74 20 6d EXCLUSIVE, it m
11256 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 eans the databas
11257 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 e file has been
11258 63 68 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 changed and any
11259 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 rollback. ** wi
1125a 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 ll require a jou
1125b 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 rnal playback..
1125c 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 */. rc = pager
1125d 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 _wait_on_lock(pP
1125e 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f ager, EXCLUSIVE_
1125f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 63 21 LOCK);. if( rc!
11260 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
11261 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
11262 0a 0a 20 20 77 68 69 6c 65 28 20 70 4c 69 73 74 .. while( pList
11263 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 ){.. /* If t
11264 68 65 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 he file has not
11265 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c yet been opened,
11266 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20 2a 2f open it now. */
11267 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 . if( !pPager
11268 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 ->fd->pMethods )
11269 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 {. assert(p
1126a 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 Pager->tempFile)
1126b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
1126c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d ite3PagerOpentem
1126d 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 p(pPager, pPager
1126e 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 76 66 ->fd, pPager->vf
1126f 73 46 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 69 sFlags);. i
11270 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
11271 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a c;. }.. /*
11272 20 49 66 20 74 68 65 72 65 20 61 72 65 20 64 69 If there are di
11273 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 rty pages in the
11274 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74 68 page cache with
11275 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72 page numbers gr
11276 65 61 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 eater. ** tha
11277 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 n Pager.dbSize,
11278 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74 this means sqlit
11279 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 28 e3PagerTruncate(
1127a 29 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a ) was called to.
1127b 20 20 20 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 ** make the
1127c 66 69 6c 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 file smaller (pr
1127d 65 73 75 6d 61 62 6c 79 20 62 79 20 61 75 74 6f esumably by auto
1127e 2d 76 61 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 -vacuum code). D
1127f 6f 20 6e 6f 74 20 77 72 69 74 65 0a 20 20 20 20 o not write.
11280 2a 2a 20 61 6e 79 20 73 75 63 68 20 70 61 67 65 ** any such page
11281 73 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 s to the file..
11282 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c */. if( pL
11283 69 73 74 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 ist->pgno<=pPage
11284 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d 3d r->dbSize && 0==
11285 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 (pList->flags&PG
11286 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20 HDR_DONT_WRITE)
11287 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66 ){. i64 off
11288 73 65 74 20 3d 20 28 70 4c 69 73 74 2d 3e 70 67 set = (pList->pg
11289 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 no-1)*(i64)pPage
1128a 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 r->pageSize;.
1128b 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d char *pData =
1128c 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 CODEC2(pPager,
1128d 70 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20 70 4c pList->pData, pL
1128e 69 73 74 2d 3e 70 67 6e 6f 2c 20 36 29 3b 0a 20 ist->pgno, 6);.
1128f 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 34 PAGERTRACE4
11290 28 22 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 ("STORE %d page
11291 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 %d hash(%08x)\n"
11292 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
11293 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 PAGERID(pPa
11294 67 65 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e ger), pList->pgn
11295 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 o, pager_pagehas
11296 68 28 70 4c 69 73 74 29 29 3b 0a 20 20 20 20 20 h(pList));.
11297 20 49 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54 IOTRACE(("PGOUT
11298 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 %p %d\n", pPage
11299 72 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 29 29 r, pList->pgno))
1129a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
1129b 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
1129c 65 72 2d 3e 66 64 2c 20 70 44 61 74 61 2c 20 70 er->fd, pData, p
1129d 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c Pager->pageSize,
1129e 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 offset);.
1129f 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 PAGER_INCR(sqlit
112a0 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 e3_pager_writedb
112a1 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 _count);. P
112a2 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 AGER_INCR(pPager
112a3 2d 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 20 ->nWrite);.
112a4 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67 6e 6f if( pList->pgno
112a5 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6d ==1 ){. m
112a6 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 emcpy(&pPager->d
112a7 62 46 69 6c 65 56 65 72 73 2c 20 26 70 44 61 74 bFileVers, &pDat
112a8 61 5b 32 34 5d 2c 20 73 69 7a 65 6f 66 28 70 50 a[24], sizeof(pP
112a9 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
112aa 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ));. }.
112ab 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 }.#ifndef NDEBUG
112ac 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 . else{.
112ad 20 50 41 47 45 52 54 52 41 43 45 33 28 22 4e 4f PAGERTRACE3("NO
112ae 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 STORE %d page %d
112af 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
112b0 67 65 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e ger), pList->pgn
112b1 6f 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 o);. }.#endif
112b2 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
112b3 74 75 72 6e 20 72 63 3b 0a 23 69 66 64 65 66 20 turn rc;.#ifdef
112b4 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
112b5 45 53 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61 ES. pList->pa
112b6 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 geHash = pager_p
112b7 61 67 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a agehash(pList);.
112b8 23 65 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74 #endif. pList
112b9 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 = pList->pDirty
112ba 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
112bb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
112bc 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
112bd 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 n is called by t
112be 68 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 he pcache layer
112bf 77 68 65 6e 20 69 74 20 68 61 73 20 72 65 61 63 when it has reac
112c0 68 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 hed some.** soft
112c1 20 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 memory limit. T
112c2 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 he argument is a
112c3 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 75 pointer to a pu
112c4 72 67 65 61 62 6c 65 20 50 61 67 65 72 20 0a 2a rgeable Pager .*
112c5 2a 20 6f 62 6a 65 63 74 2e 20 54 68 69 73 20 66 * object. This f
112c6 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 unction attempts
112c7 20 74 6f 20 6d 61 6b 65 20 61 20 73 69 6e 67 6c to make a singl
112c8 65 20 64 69 72 74 79 20 70 61 67 65 20 74 68 61 e dirty page tha
112c9 74 20 68 61 73 20 6e 6f 0a 2a 2a 20 6f 75 74 73 t has no.** outs
112ca 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 tanding referenc
112cb 65 73 20 28 69 66 20 6f 6e 65 20 65 78 69 73 74 es (if one exist
112cc 73 29 20 63 6c 65 61 6e 20 73 6f 20 74 68 61 74 s) clean so that
112cd 20 69 74 20 63 61 6e 20 62 65 20 72 65 63 79 63 it can be recyc
112ce 6c 65 64 20 0a 2a 2a 20 62 79 20 74 68 65 20 70 led .** by the p
112cf 63 61 63 68 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a cache layer..*/.
112d0 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
112d1 53 74 72 65 73 73 28 76 6f 69 64 20 2a 70 2c 20 Stress(void *p,
112d2 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 PgHdr *pPg){. P
112d3 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 28 ager *pPager = (
112d4 50 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74 Pager *)p;. int
112d5 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
112d6 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e .. if( pPager->
112d7 64 6f 4e 6f 74 53 79 6e 63 20 29 7b 0a 20 20 20 doNotSync ){.
112d8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
112d9 4b 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 K;. }.. assert
112da 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 ( pPg->flags&PGH
112db 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 69 66 DR_DIRTY );. if
112dc 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
112dd 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a e==SQLITE_OK ){.
112de 20 20 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 if( pPg->fla
112df 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
112e0 4e 43 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d NC ){. rc =
112e1 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 syncJournal(pPa
112e2 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ger);. if(
112e3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
112e4 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e pPager->fullSyn
112e5 63 20 26 26 20 0a 20 20 20 20 20 20 20 20 21 28 c && . !(
112e6 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
112e7 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
112e8 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26 ALMODE_MEMORY) &
112e9 26 0a 20 20 20 20 20 20 20 20 21 28 73 71 6c 69 &. !(sqli
112ea 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 te3OsDeviceChara
112eb 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65 cteristics(pPage
112ec 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f r->fd)&SQLITE_IO
112ed 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 CAP_SAFE_APPEND)
112ee 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 . ){.
112ef 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d pPager->nRec =
112f0 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 0;. rc =
112f1 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 writeJournalHdr
112f2 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 (pPager);.
112f3 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
112f4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
112f5 0a 20 20 20 20 20 20 70 50 67 2d 3e 70 44 69 72 . pPg->pDir
112f6 74 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 ty = 0;. rc
112f7 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 = pager_write_p
112f8 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a 20 20 agelist(pPg);.
112f9 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
112fa 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
112fb 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 pager_error(p
112fc 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 Pager, rc);.
112fd 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d }. }.. if( rc=
112fe 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
112ff 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
11300 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 akeClean(pPg);.
11301 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
11302 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e }.../*.** Return
11303 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 1 if there is a
11304 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20 hot journal on
11305 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 2e the given pager.
11306 0a 2a 2a 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61 .** A hot journa
11307 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 6e 65 l is one that ne
11308 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64 eds to be played
11309 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 back..**.** If
1130a 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 the current size
1130b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1130c 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 20 61 file is 0 but a
1130d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a journal file.**
1130e 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 69 73 exists, that is
1130f 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64 probably an old
11310 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76 journal left ov
11311 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a er from a prior.
11312 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 74 68 ** database with
11313 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 the same name.
11314 20 4a 75 73 74 20 64 65 6c 65 74 65 20 74 68 65 Just delete the
11315 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 journal..**.**
11316 52 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 20 Return negative
11317 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 64 65 74 if unable to det
11318 65 72 6d 69 6e 65 20 74 68 65 20 73 74 61 74 75 ermine the statu
11319 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c s of the journal
1131a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1131b 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 6f 70 tine does not op
1131c 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 en the journal f
1131d 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65 20 69 ile to examine i
1131e 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 2e 20 20 ts.** content.
1131f 48 65 6e 63 65 2c 20 74 68 65 20 6a 6f 75 72 6e Hence, the journ
11320 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e al might contain
11321 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d the name of a m
11322 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c aster.** journal
11323 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 62 file that has b
11324 65 65 6e 20 64 65 6c 65 74 65 64 2c 20 61 6e 64 een deleted, and
11325 20 68 65 6e 63 65 20 6e 6f 74 20 62 65 20 68 6f hence not be ho
11326 74 2e 20 20 4f 72 0a 2a 2a 20 74 68 65 20 68 65 t. Or.** the he
11327 61 64 65 72 20 6f 66 20 74 68 65 20 6a 6f 75 72 ader of the jour
11328 6e 61 6c 20 6d 69 67 68 74 20 62 65 20 7a 65 72 nal might be zer
11329 6f 65 64 20 6f 75 74 2e 20 20 54 68 69 73 20 72 oed out. This r
1132a 6f 75 74 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 6e outine.** does n
1132b 6f 74 20 64 69 73 63 6f 76 65 72 20 74 68 65 73 ot discover thes
1132c 65 20 63 61 73 65 73 20 6f 66 20 61 20 6e 6f 6e e cases of a non
1132d 2d 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 2d 20 69 -hot journal - i
1132e 66 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c f the.** journal
1132f 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 file exists and
11330 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 74 68 is not empty th
11331 69 73 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d is routine assum
11332 65 73 20 69 74 0a 2a 2a 20 69 73 20 68 6f 74 2e es it.** is hot.
11333 20 20 54 68 65 20 70 61 67 65 72 5f 70 6c 61 79 The pager_play
11334 62 61 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 77 back() routine w
11335 69 6c 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 ill discover tha
11336 74 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c t the.** journal
11337 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 65 61 file is not rea
11338 6c 6c 79 20 68 6f 74 20 61 6e 64 20 77 69 6c 6c lly hot and will
11339 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
1133a 63 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72 c int hasHotJour
1133b 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 nal(Pager *pPage
1133c 72 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73 29 r, int *pExists)
1133d 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
1133e 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e *pVfs = pPager->
1133f 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d pVfs;. int rc =
11340 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
11341 74 20 65 78 69 73 74 73 3b 0a 20 20 69 6e 74 20 t exists;. int
11342 6c 6f 63 6b 65 64 3b 0a 20 20 61 73 73 65 72 74 locked;. assert
11343 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 ( pPager!=0 );.
11344 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
11345 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 >useJournal );.
11346 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
11347 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b >fd->pMethods );
11348 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b . *pExists = 0;
11349 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
1134a 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70 50 sAccess(pVfs, pP
1134b 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
1134c 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
1134d 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a ISTS, &exists);.
1134e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1134f 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29 7b _OK && exists ){
11350 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
11351 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 3OsCheckReserved
11352 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c Lock(pPager->fd,
11353 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 7d 0a 20 &locked);. }.
11354 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
11355 4f 4b 20 26 26 20 65 78 69 73 74 73 20 26 26 20 OK && exists &&
11356 21 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 69 !locked ){. i
11357 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 72 63 nt nPage;. rc
11358 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 = sqlite3PagerP
11359 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
1135a 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 &nPage);. if
1135b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1135c 29 7b 0a 20 20 20 20 20 69 66 28 20 6e 50 61 67 ){. if( nPag
1135d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e==0 ){.
1135e 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
1135f 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a pVfs, pPager->zJ
11360 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 ournal, 0);.
11361 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
11362 20 2a 70 45 78 69 73 74 73 20 3d 20 31 3b 0a 20 *pExists = 1;.
11363 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
11364 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
11365 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 ./*.** Read the
11366 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 content of page
11367 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64 pPg out of the d
11368 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f atabase file..*/
11369 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 .static int read
1136a 44 62 50 61 67 65 28 50 61 67 65 72 20 2a 70 50 DbPage(Pager *pP
1136b 61 67 65 72 2c 20 50 67 48 64 72 20 2a 70 50 67 ager, PgHdr *pPg
1136c 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 , Pgno pgno){.
1136d 69 6e 74 20 72 63 3b 0a 20 20 69 36 34 20 6f 66 int rc;. i64 of
1136e 66 73 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20 fset;. assert(
1136f 4d 45 4d 44 42 3d 3d 30 20 29 3b 0a 20 20 61 73 MEMDB==0 );. as
11370 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66 64 2d sert(pPager->fd-
11371 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67 65 >pMethods||pPage
11372 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 r->tempFile);.
11373 69 66 28 20 21 70 50 61 67 65 72 2d 3e 66 64 2d if( !pPager->fd-
11374 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 >pMethods ){.
11375 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
11376 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b OERR_SHORT_READ;
11377 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20 . }. offset =
11378 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 (pgno-1)*(i64)pP
11379 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a ager->pageSize;.
1137a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1137b 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c Read(pPager->fd,
1137c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 50 61 pPg->pData, pPa
1137d 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f ger->pageSize, o
1137e 66 66 73 65 74 29 3b 0a 20 20 50 41 47 45 52 5f ffset);. PAGER_
1137f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 INCR(sqlite3_pag
11380 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 29 er_readdb_count)
11381 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 ;. PAGER_INCR(p
11382 50 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 Pager->nRead);.
11383 20 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20 IOTRACE(("PGIN
11384 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 %p %d\n", pPager
11385 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 69 66 28 20 , pgno));. if(
11386 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 6d pgno==1 ){. m
11387 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 emcpy(&pPager->d
11388 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75 38 bFileVers, &((u8
11389 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 5b 32 34 *)pPg->pData)[24
1138a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ],.
1138b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1138c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1138d 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e sizeof(pPager->
1138e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
1138f 7d 0a 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 }. CODEC1(pPage
11390 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 r, pPg->pData, p
11391 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20 20 Pg->pgno, 3);.
11392 50 41 47 45 52 54 52 41 43 45 34 28 22 46 45 54 PAGERTRACE4("FET
11393 43 48 20 25 64 20 70 61 67 65 20 25 64 20 68 61 CH %d page %d ha
11394 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 sh(%08x)\n",.
11395 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 PAGE
11396 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 RID(pPager), pPg
11397 2d 3e 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 ->pgno, pager_pa
11398 67 65 68 61 73 68 28 70 50 67 29 29 3b 0a 20 20 gehash(pPg));.
11399 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
1139a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1139b 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 on is called to
1139c 6f 62 74 61 69 6e 20 74 68 65 20 73 68 61 72 65 obtain the share
1139d 64 20 6c 6f 63 6b 20 72 65 71 75 69 72 65 64 20 d lock required
1139e 62 65 66 6f 72 65 0a 2a 2a 20 64 61 74 61 20 6d before.** data m
1139f 61 79 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 ay be read from
113a0 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e the pager cache.
113a1 20 49 66 20 74 68 65 20 73 68 61 72 65 64 20 6c If the shared l
113a2 6f 63 6b 20 68 61 73 20 61 6c 72 65 61 64 79 0a ock has already.
113a3 2a 2a 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 ** been obtained
113a4 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
113a5 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
113a6 2a 20 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 * Immediately af
113a7 74 65 72 20 6f 62 74 61 69 6e 69 6e 67 20 74 68 ter obtaining th
113a8 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 28 69 e shared lock (i
113a9 66 20 72 65 71 75 69 72 65 64 29 2c 20 74 68 69 f required), thi
113aa 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 63 68 s function.** ch
113ab 65 63 6b 73 20 66 6f 72 20 61 20 68 6f 74 2d 6a ecks for a hot-j
113ac 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
113ad 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 61 6e one is found, an
113ae 20 65 6d 65 72 67 65 6e 63 79 20 72 6f 6c 6c 62 emergency rollb
113af 61 63 6b 0a 2a 2a 20 69 73 20 70 65 72 66 6f 72 ack.** is perfor
113b0 6d 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e med immediately.
113b1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
113b2 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 agerSharedLock(P
113b3 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
113b4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
113b5 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 73 45 72 72 _OK;. int isErr
113b6 6f 72 52 65 73 65 74 20 3d 20 30 3b 0a 0a 20 20 orReset = 0;..
113b7 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 /* If this datab
113b8 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f ase is opened fo
113b9 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 r exclusive acce
113ba 73 73 2c 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 ss, has no outst
113bb 61 6e 64 69 6e 67 20 0a 20 20 2a 2a 20 70 61 67 anding . ** pag
113bc 65 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e 64 e references and
113bd 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 2d is in an error-
113be 73 74 61 74 65 2c 20 6e 6f 77 20 69 73 20 74 68 state, now is th
113bf 65 20 63 68 61 6e 63 65 20 74 6f 20 63 6c 65 61 e chance to clea
113c0 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72 r. ** the error
113c1 2e 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f . Discard the co
113c2 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
113c3 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20 74 72 ger-cache and tr
113c4 65 61 74 20 61 6e 79 0a 20 20 2a 2a 20 6f 70 65 eat any. ** ope
113c5 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 n journal file a
113c6 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2e s a hot-journal.
113c7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 4d 45 4d . */. if( !MEM
113c8 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78 DB && pPager->ex
113c9 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 clusiveMode .
113ca 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 && sqlite3Pcache
113cb 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
113cc 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 26 26 20 >pPCache)==0 &&
113cd 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
113ce 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 . ){. if( pP
113cf 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 ager->journalOpe
113d0 6e 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72 72 n ){. isErr
113d1 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20 20 orReset = 1;.
113d2 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 }. pPager->e
113d3 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f rrCode = SQLITE_
113d4 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72 65 OK;. pager_re
113d5 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d set(pPager);. }
113d6 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
113d7 67 65 72 20 69 73 20 73 74 69 6c 6c 20 69 6e 20 ger is still in
113d8 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 an error state,
113d9 64 6f 20 6e 6f 74 20 70 72 6f 63 65 65 64 2e 20 do not proceed.
113da 54 68 65 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 The error . **
113db 73 74 61 74 65 20 77 69 6c 6c 20 62 65 20 63 6c state will be cl
113dc 65 61 72 65 64 20 61 74 20 73 6f 6d 65 20 70 6f eared at some po
113dd 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 int in the futur
113de 65 20 77 68 65 6e 20 61 6c 6c 20 70 61 67 65 20 e when all page
113df 0a 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 . ** references
113e0 20 61 72 65 20 64 72 6f 70 70 65 64 20 61 6e 64 are dropped and
113e1 20 74 68 65 20 63 61 63 68 65 20 63 61 6e 20 62 the cache can b
113e2 65 20 64 69 73 63 61 72 64 65 64 2e 0a 20 20 2a e discarded.. *
113e3 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
113e4 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65 errCode && pPage
113e5 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 r->errCode!=SQLI
113e6 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72 TE_FULL ){. r
113e7 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 eturn pPager->er
113e8 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 rCode;. }.. if
113e9 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d ( pPager->state=
113ea 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c =PAGER_UNLOCK ||
113eb 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b isErrorReset ){
113ec 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
113ed 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d *pVfs = pPager-
113ee 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 69 >pVfs;. int i
113ef 73 48 6f 74 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 sHotJournal;.
113f0 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 assert( !MEMDB
113f1 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
113f2 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
113f3 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
113f4 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 ache)==0 );.
113f5 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 52 if( !pPager->noR
113f6 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 eadlock ){.
113f7 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 rc = pager_wait
113f8 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c _on_lock(pPager,
113f9 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 SHARED_LOCK);.
113fa 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
113fb 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
113fc 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
113fd 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 ->state==PAGER_U
113fe 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20 NLOCK );.
113ff 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 return pager_er
11400 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b ror(pPager, rc);
11401 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
11402 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
11403 74 61 74 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 tate>=SHARED_LOC
11404 4b 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 K );. }..
11405 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 /* If a journal
11406 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64 file exists, and
11407 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53 there is no RES
11408 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 ERVED lock on th
11409 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 e. ** databas
1140a 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 e file, then it
1140b 65 69 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20 either needs to
1140c 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f be played back o
1140d 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a r deleted.. *
1140e 2f 0a 20 20 20 20 69 66 28 20 21 69 73 45 72 72 /. if( !isErr
1140f 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 orReset ){.
11410 20 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 rc = hasHotJour
11411 6e 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 48 nal(pPager, &isH
11412 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 otJournal);.
11413 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
11414 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
11415 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
11416 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
11417 28 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 7c ( isErrorReset |
11418 7c 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 | isHotJournal )
11419 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61 {. /* Get a
1141a 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
1141b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1141c 20 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70 file. At this p
1141d 6f 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 20 oint it is.
1141e 20 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 ** important th
1141f 61 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f at a RESERVED lo
11420 63 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e ck is not obtain
11421 65 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f ed on the way to
11422 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 the. ** EX
11423 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 CLUSIVE lock. If
11424 20 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 it were, anothe
11425 72 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 r process might
11426 6f 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 2a open the. *
11427 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c * database file,
11428 20 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45 detect the RESE
11429 52 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 RVED lock, and c
1142a 6f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65 onclude that the
1142b 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 . ** databa
1142c 73 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 se is safe to re
1142d 61 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 72 ad while this pr
1142e 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 72 ocess is still r
1142f 6f 6c 6c 69 6e 67 20 69 74 20 0a 20 20 20 20 20 olling it .
11430 20 2a 2a 20 62 61 63 6b 2e 0a 20 20 20 20 20 20 ** back..
11431 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 42 65 63 ** . ** Bec
11432 61 75 73 65 20 74 68 65 20 69 6e 74 65 72 6d 65 ause the interme
11433 64 69 61 74 65 20 52 45 53 45 52 56 45 44 20 6c diate RESERVED l
11434 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 71 75 65 ock is not reque
11435 73 74 65 64 2c 20 74 68 65 0a 20 20 20 20 20 20 sted, the.
11436 2a 2a 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 ** second proces
11437 73 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 74 68 s will get to th
11438 69 73 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 is point in the
11439 63 6f 64 65 20 61 6e 64 20 66 61 69 6c 20 74 6f code and fail to
1143a 0a 20 20 20 20 20 20 2a 2a 20 6f 62 74 61 69 6e . ** obtain
1143b 20 69 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 49 its own EXCLUSI
1143c 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 VE lock on the d
1143d 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 atabase file..
1143e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
1143f 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 pPager->state<E
11440 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b XCLUSIVE_LOCK ){
11441 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
11442 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 lite3OsLock(pPag
11443 65 72 2d 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 er->fd, EXCLUSIV
11444 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 E_LOCK);.
11445 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
11446 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
11447 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 rc = pager_error
11448 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 (pPager, rc);.
11449 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 goto fai
1144a 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 led;. }.
1144b 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 pPager->s
1144c 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 tate = PAGER_EXC
1144d 4c 55 53 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a LUSIVE;. }.
1144e 20 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 . /* Open
1144f 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 the journal for
11450 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 read/write acces
11451 73 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 s. This is becau
11452 73 65 20 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 se in . **
11453 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 exclusive-access
11454 20 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 20 64 mode the file d
11455 65 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 escriptor will b
11456 65 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a e kept open and.
11457 20 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c ** possibl
11458 79 20 75 73 65 64 20 66 6f 72 20 61 20 74 72 61 y used for a tra
11459 6e 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f nsaction later o
1145a 6e 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 n. On some syste
1145b 6d 73 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a ms, the. **
1145c 20 4f 73 54 72 75 6e 63 61 74 65 28 29 20 63 61 OsTruncate() ca
1145d 6c 6c 20 75 73 65 64 20 69 6e 20 65 78 63 6c 75 ll used in exclu
1145e 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 sive-access mode
1145f 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73 0a 20 also requires.
11460 20 20 20 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 ** a read/w
11461 72 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 rite file handle
11462 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
11463 20 69 66 28 20 21 69 73 45 72 72 6f 72 52 65 73 if( !isErrorRes
11464 65 74 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f et && pPager->jo
11465 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29 7b 0a urnalOpen==0 ){.
11466 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b int res;
11467 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
11468 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 lite3OsAccess(pV
11469 66 73 2c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 fs,pPager->zJour
1146a 6e 61 6c 2c 53 51 4c 49 54 45 5f 41 43 43 45 53 nal,SQLITE_ACCES
1146b 53 5f 45 58 49 53 54 53 2c 26 72 65 73 29 3b 0a S_EXISTS,&res);.
1146c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
1146d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1146e 20 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29 if( res )
1146f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e {. in
11470 74 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 20 20 t fout = 0;.
11471 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 int f =
11472 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
11473 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 WRITE|SQLITE_OPE
11474 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a N_MAIN_JOURNAL;.
11475 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 asse
11476 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d rt( !pPager->tem
11477 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 pFile );.
11478 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
11479 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 3OsOpen(pVfs, pP
1147a 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
1147b 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 pPager->jfd, f,
1147c 26 66 6f 75 74 29 3b 0a 20 20 20 20 20 20 20 20 &fout);.
1147d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d assert( rc!=
1147e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 SQLITE_OK || pPa
1147f 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f ger->jfd->pMetho
11480 64 73 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ds );.
11481 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
11482 5f 4f 4b 20 26 26 20 66 6f 75 74 26 53 51 4c 49 _OK && fout&SQLI
11483 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
11484 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
11485 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 rc = SQLITE_CA
11486 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20 20 20 20 NTOPEN;.
11487 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
11488 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 lose(pPager->jfd
11489 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d );. }
1148a 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
1148b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a {. /*
1148c 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
1148d 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 does not exist,
1148e 74 68 61 74 20 6d 65 61 6e 73 20 73 6f 6d 65 20 that means some
1148f 6f 74 68 65 72 20 70 72 6f 63 65 73 73 0a 20 20 other process.
11490 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 73 ** has
11491 20 61 6c 72 65 61 64 79 20 72 6f 6c 6c 65 64 20 already rolled
11492 69 74 20 62 61 63 6b 20 2a 2f 0a 20 20 20 20 20 it back */.
11493 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
11494 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 TE_BUSY;.
11495 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
11496 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
11497 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
11498 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 {. goto f
11499 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 ailed;. }.
1149a 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
1149b 72 6e 61 6c 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 rnalOpen = 1;.
1149c 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
1149d 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a nalStarted = 0;.
1149e 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
1149f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 urnalOff = 0;.
114a0 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d pPager->setM
114a1 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 aster = 0;.
114a2 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
114a3 48 64 72 20 3d 20 30 3b 0a 20 0a 20 20 20 20 20 Hdr = 0;. .
114a4 20 2f 2a 20 50 6c 61 79 62 61 63 6b 20 61 6e 64 /* Playback and
114a5 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 delete the jour
114a6 6e 61 6c 2e 20 20 44 72 6f 70 20 74 68 65 20 64 nal. Drop the d
114a7 61 74 61 62 61 73 65 20 77 72 69 74 65 0a 20 20 atabase write.
114a8 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 ** lock and
114a9 72 65 61 63 71 75 69 72 65 20 74 68 65 20 72 65 reacquire the re
114aa 61 64 20 6c 6f 63 6b 2e 0a 20 20 20 20 20 20 2a ad lock.. *
114ab 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 /. rc = pag
114ac 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 er_playback(pPag
114ad 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66 er, 1);. if
114ae 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
114af 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
114b0 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
114b1 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 er, rc);.
114b2 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 goto failed;.
114b3 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
114b4 72 74 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 rt(pPager->state
114b5 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c ==PAGER_SHARED |
114b6 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 | . (pP
114b7 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
114b8 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d 3e 73 ode && pPager->s
114b9 74 61 74 65 3e 50 41 47 45 52 5f 53 48 41 52 45 tate>PAGER_SHARE
114ba 44 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 D). );.
114bb 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }.. if( sqlit
114bc 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e e3PcachePagecoun
114bd 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
114be 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a e)>0 ){. /*
114bf 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b The shared-lock
114c0 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61 has just been a
114c1 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64 cquired on the d
114c2 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 atabase file.
114c3 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 ** and there
114c4 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 65 are already page
114c5 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 28 s in the cache (
114c6 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a from a previous.
114c7 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f 72 ** read or
114c8 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
114c9 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 on). Check to s
114ca 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ee if the databa
114cb 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 se. ** has
114cc 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 been modified.
114cd 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
114ce 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 6c 75 has changed, flu
114cf 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 sh the. **
114d0 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a cache.. **.
114d1 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62 61 73 ** Databas
114d2 65 20 63 68 61 6e 67 65 73 20 69 73 20 64 65 74 e changes is det
114d3 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 ected by looking
114d4 20 61 74 20 31 35 20 62 79 74 65 73 20 62 65 67 at 15 bytes beg
114d5 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 inning. **
114d6 61 74 20 6f 66 66 73 65 74 20 32 34 20 69 6e 74 at offset 24 int
114d7 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 o the file. The
114d8 20 66 69 72 73 74 20 34 20 6f 66 20 74 68 65 73 first 4 of thes
114d9 65 20 31 36 20 62 79 74 65 73 20 61 72 65 0a 20 e 16 bytes are.
114da 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 ** a 32-bit
114db 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20 69 73 counter that is
114dc 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 incremented wit
114dd 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e 20 20 h each change.
114de 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 The. ** oth
114df 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 65 20 er bytes change
114e0 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61 randomly with ea
114e1 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77 ch file change w
114e2 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 hen. ** a c
114e3 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a odec is in use..
114e4 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 ** .
114e5 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 76 61 ** There is a va
114e6 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 nishingly small
114e7 63 68 61 6e 63 65 20 74 68 61 74 20 61 20 63 68 chance that a ch
114e8 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 ange will not be
114e9 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74 65 63 . ** detec
114ea 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e 63 65 ted. The chance
114eb 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63 74 65 of an undetecte
114ec 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f 20 73 d change is so s
114ed 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20 20 20 mall that.
114ee 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e 65 67 ** it can be neg
114ef 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f lected.. */
114f0 0a 20 20 20 20 20 20 63 68 61 72 20 64 62 46 69 . char dbFi
114f1 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 70 50 leVers[sizeof(pP
114f2 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
114f3 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 )];. sqlite
114f4 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
114f5 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20 20 pPager, 0);..
114f6 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 if( pPager->e
114f7 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 rrCode ){.
114f8 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 rc = pPager->e
114f9 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 rrCode;.
114fa 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
114fb 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 }.. asse
114fc 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 rt( pPager->dbSi
114fd 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 zeValid );.
114fe 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 if( pPager->dbS
114ff 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 ize>0 ){.
11500 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45 52 IOTRACE(("CKVER
11501 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 S %p %d\n", pPag
11502 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c er, sizeof(dbFil
11503 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20 20 eVers)));.
11504 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11505 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c Read(pPager->fd,
11506 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 &dbFileVers, si
11507 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 zeof(dbFileVers)
11508 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20 69 , 24);. i
11509 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1150a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f ){. go
1150b 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 to failed;.
1150c 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
1150d 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 {. memset
1150e 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c 20 (dbFileVers, 0,
1150f 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 sizeof(dbFileVer
11510 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 s));. }..
11511 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 if( memcmp(p
11512 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 Pager->dbFileVer
11513 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c 20 73 s, dbFileVers, s
11514 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 izeof(dbFileVers
11515 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ))!=0 ){.
11516 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 pager_reset(pPa
11517 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ger);. }.
11518 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
11519 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
1151a 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d eMode || pPager-
1151b 3e 73 74 61 74 65 3c 3d 50 41 47 45 52 5f 53 48 >state<=PAGER_SH
1151c 41 52 45 44 20 29 3b 0a 20 20 20 20 69 66 28 20 ARED );. if(
1151d 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
1151e 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 AGER_UNLOCK ){.
1151f 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 pPager->sta
11520 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 te = PAGER_SHARE
11521 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 66 D;. }. }.. f
11522 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21 ailed:. if( rc!
11523 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
11524 20 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63 /* pager_unloc
11525 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 k() is a no-op f
11526 6f 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 or exclusive mod
11527 65 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 e and in-memory
11528 64 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 20 databases. */.
11529 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 pager_unlock(p
1152a 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 Pager);. }. re
1152b 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1152c 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68 * Make sure we h
1152d 61 76 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ave the content
1152e 66 6f 72 20 61 20 70 61 67 65 2e 20 20 49 66 20 for a page. If
1152f 74 68 65 20 70 61 67 65 20 77 61 73 0a 2a 2a 20 the page was.**
11530 70 72 65 76 69 6f 75 73 6c 79 20 61 63 71 75 69 previously acqui
11531 72 65 64 20 77 69 74 68 20 6e 6f 43 6f 6e 74 65 red with noConte
11532 6e 74 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 nt==1, then the
11533 63 6f 6e 74 65 6e 74 20 77 61 73 0a 2a 2a 20 6a content was.** j
11534 75 73 74 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 ust initialized
11535 74 6f 20 7a 65 72 6f 73 20 69 6e 73 74 65 61 64 to zeros instead
11536 20 6f 66 20 62 65 69 6e 67 20 72 65 61 64 20 66 of being read f
11537 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 20 42 75 74 rom disk..** But
11538 20 6e 6f 77 20 77 65 20 6e 65 65 64 20 74 68 65 now we need the
11539 20 72 65 61 6c 20 64 61 74 61 20 6f 66 66 20 6f real data off o
1153a 66 20 64 69 73 6b 2e 20 20 53 6f 20 6d 61 6b 65 f disk. So make
1153b 20 73 75 72 65 20 77 65 0a 2a 2a 20 68 61 76 65 sure we.** have
1153c 20 69 74 2e 20 20 52 65 61 64 20 69 74 20 69 6e it. Read it in
1153d 20 69 66 20 77 65 20 64 6f 20 6e 6f 74 20 68 61 if we do not ha
1153e 76 65 20 69 74 20 61 6c 72 65 61 64 79 2e 0a 2a ve it already..*
1153f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
11540 65 72 5f 67 65 74 5f 63 6f 6e 74 65 6e 74 28 50 er_get_content(P
11541 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 69 66 gHdr *pPg){. if
11542 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 ( pPg->flags&PGH
11543 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 29 7b 0a DR_NEED_READ ){.
11544 20 20 20 20 69 6e 74 20 72 63 20 3d 20 72 65 61 int rc = rea
11545 64 44 62 50 61 67 65 28 70 50 67 2d 3e 70 50 61 dDbPage(pPg->pPa
11546 67 65 72 2c 20 70 50 67 2c 20 70 50 67 2d 3e 70 ger, pPg, pPg->p
11547 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 gno);. if( rc
11548 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
11549 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 pPg->flags
1154a 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 52 &= ~PGHDR_NEED_R
1154b 45 41 44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a EAD;. }else{.
1154c 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1154d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1154e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1154f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ../*.** If the r
11550 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 68 eference count h
11551 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 6f 2c as reached zero,
11552 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 and the pager i
11553 73 20 6e 6f 74 20 69 6e 20 74 68 65 0a 2a 2a 20 s not in the.**
11554 6d 69 64 64 6c 65 20 6f 66 20 61 20 77 72 69 74 middle of a writ
11555 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 e transaction or
11556 20 6f 70 65 6e 65 64 20 69 6e 20 65 78 63 6c 75 opened in exclu
11557 73 69 76 65 20 6d 6f 64 65 2c 20 75 6e 6c 6f 63 sive mode, unloc
11558 6b 20 69 74 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 k it..*/ .static
11559 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 void pagerUnloc
1155a 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 kIfUnused(Pager
1155b 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 *pPager){. if(
1155c 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 (sqlite3PcacheRe
1155d 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
1155e 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 20 PCache)==0).
1155f 26 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78 63 && (!pPager->exc
11560 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 lusiveMode || pP
11561 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11562 3e 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 >0) . ){. pa
11563 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c gerUnlockAndRoll
11564 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 back(pPager);.
11565 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 }.}../*.** Drop
11566 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
11567 63 61 63 68 65 20 75 73 69 6e 67 20 73 71 6c 69 cache using sqli
11568 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 29 2e te3PcacheDrop().
11569 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d .**.** If this m
1156a 65 61 6e 73 20 74 68 65 72 65 20 61 72 65 20 6e eans there are n
1156b 6f 77 20 6e 6f 20 70 61 67 65 73 20 77 69 74 68 ow no pages with
1156c 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
1156d 68 65 6d 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 0a hem, a rollback.
1156e 2a 2a 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 ** occurs and th
1156f 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 e lock on the da
11570 74 61 62 61 73 65 20 69 73 20 72 65 6d 6f 76 65 tabase is remove
11571 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
11572 64 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 28 d pagerDropPage(
11573 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
11574 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
11575 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 pPg->pPager;. s
11576 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 qlite3PcacheDrop
11577 28 70 50 67 29 3b 0a 20 20 70 61 67 65 72 55 6e (pPg);. pagerUn
11578 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61 lockIfUnused(pPa
11579 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ger);.}../*.** A
1157a 63 71 75 69 72 65 20 61 20 70 61 67 65 2e 0a 2a cquire a page..*
1157b 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6c 6f 63 6b *.** A read lock
1157c 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c on the disk fil
1157d 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 77 68 e is obtained wh
1157e 65 6e 20 74 68 65 20 66 69 72 73 74 20 70 61 67 en the first pag
1157f 65 20 69 73 20 61 63 71 75 69 72 65 64 2e 20 0a e is acquired. .
11580 2a 2a 20 54 68 69 73 20 72 65 61 64 20 6c 6f 63 ** This read loc
11581 6b 20 69 73 20 64 72 6f 70 70 65 64 20 77 68 65 k is dropped whe
11582 6e 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 n the last page
11583 69 73 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a is released..**.
11584 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
11585 77 6f 72 6b 73 20 66 6f 72 20 61 6e 79 20 70 61 works for any pa
11586 67 65 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 ge number greate
11587 72 20 74 68 61 6e 20 30 2e 20 20 49 66 20 74 68 r than 0. If th
11588 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 e database.** fi
11589 6c 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 le is smaller th
1158a 61 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 an the requested
1158b 20 70 61 67 65 2c 20 74 68 65 6e 20 6e 6f 20 61 page, then no a
1158c 63 74 75 61 6c 20 64 69 73 6b 0a 2a 2a 20 72 65 ctual disk.** re
1158d 61 64 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 ad occurs and th
1158e 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f e memory image o
1158f 66 20 74 68 65 20 70 61 67 65 20 69 73 20 69 6e f the page is in
11590 69 74 69 61 6c 69 7a 65 64 20 74 6f 0a 2a 2a 20 itialized to.**
11591 61 6c 6c 20 7a 65 72 6f 73 2e 20 20 54 68 65 20 all zeros. The
11592 65 78 74 72 61 20 64 61 74 61 20 61 70 70 65 6e extra data appen
11593 64 65 64 20 74 6f 20 61 20 70 61 67 65 20 69 73 ded to a page is
11594 20 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c 69 always initiali
11595 7a 65 64 0a 2a 2a 20 74 6f 20 7a 65 72 6f 73 20 zed.** to zeros
11596 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 the first time a
11597 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 page is loaded
11598 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a into memory..**.
11599 2a 2a 20 54 68 65 20 61 63 71 75 69 73 69 74 69 ** The acquisiti
1159a 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c 20 66 6f on might fail fo
1159b 72 20 73 65 76 65 72 61 6c 20 72 65 61 73 6f 6e r several reason
1159c 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61 73 65 73 s. In all cases
1159d 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 ,.** an appropri
1159e 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 ate error code i
1159f 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a s returned and *
115a0 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f ppPage is set to
115a1 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 NULL..**.** See
115a2 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67 also sqlite3Pag
115a3 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20 42 6f 74 erLookup(). Bot
115a4 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 h this routine a
115a5 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61 74 74 65 nd Lookup() atte
115a6 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 61 mpt.** to find a
115a7 20 70 61 67 65 20 69 6e 20 74 68 65 20 69 6e 2d page in the in-
115a8 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 66 69 72 memory cache fir
115a9 73 74 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 st. If the page
115aa 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a is not already.
115ab 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c 20 74 68 ** in memory, th
115ac 69 73 20 72 6f 75 74 69 6e 65 20 67 6f 65 73 20 is routine goes
115ad 74 6f 20 64 69 73 6b 20 74 6f 20 72 65 61 64 20 to disk to read
115ae 69 74 20 69 6e 20 77 68 65 72 65 61 73 20 4c 6f it in whereas Lo
115af 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73 74 20 72 okup().** just r
115b0 65 74 75 72 6e 73 20 30 2e 20 20 54 68 69 73 20 eturns 0. This
115b1 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 73 routine acquires
115b2 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 74 68 65 a read-lock the
115b3 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 0a 2a first time it.*
115b4 2a 20 68 61 73 20 74 6f 20 67 6f 20 74 6f 20 64 * has to go to d
115b5 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c 64 20 61 isk, and could a
115b6 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20 61 6e 20 lso playback an
115b7 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69 66 20 6e old journal if n
115b8 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 53 69 6e ecessary..** Sin
115b9 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e 65 76 65 ce Lookup() neve
115ba 72 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 2c 20 r goes to disk,
115bb 69 74 20 6e 65 76 65 72 20 68 61 73 20 74 6f 20 it never has to
115bc 64 65 61 6c 20 77 69 74 68 20 6c 6f 63 6b 73 0a deal with locks.
115bd 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 ** or journal fi
115be 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f les..**.** If no
115bf 43 6f 6e 74 65 6e 74 20 69 73 20 66 61 6c 73 65 Content is false
115c0 2c 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 65 , the page conte
115c1 6e 74 73 20 61 72 65 20 61 63 74 75 61 6c 6c 79 nts are actually
115c2 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 2e read from disk.
115c3 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 .** If noContent
115c4 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65 61 is true, it mea
115c5 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f ns that we do no
115c6 74 20 63 61 72 65 20 61 62 6f 75 74 20 74 68 65 t care about the
115c7 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 contents.** of
115c8 74 68 65 20 70 61 67 65 20 61 74 20 74 68 69 73 the page at this
115c9 20 74 69 6d 65 2c 20 73 6f 20 64 6f 20 6e 6f 74 time, so do not
115ca 20 64 6f 20 61 20 64 69 73 6b 20 72 65 61 64 2e do a disk read.
115cb 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 Just fill in t
115cc 68 65 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 65 he.** page conte
115cd 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 2e 20 20 nt with zeros.
115ce 42 75 74 20 6d 61 72 6b 20 74 68 65 20 66 61 63 But mark the fac
115cf 74 20 74 68 61 74 20 77 65 20 68 61 76 65 20 6e t that we have n
115d0 6f 74 20 72 65 61 64 20 74 68 65 0a 2a 2a 20 63 ot read the.** c
115d1 6f 6e 74 65 6e 74 20 62 79 20 73 65 74 74 69 6e ontent by settin
115d2 67 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 g the PgHdr.need
115d3 52 65 61 64 20 66 6c 61 67 2e 20 20 4c 61 74 65 Read flag. Late
115d4 72 20 6f 6e 2c 20 69 66 20 0a 2a 2a 20 73 71 6c r on, if .** sql
115d5 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 ite3PagerWrite()
115d6 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 is called on th
115d7 69 73 20 70 61 67 65 20 6f 72 20 69 66 20 74 68 is page or if th
115d8 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a is routine is.**
115d9 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 77 69 called again wi
115da 74 68 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d 30 2c th noContent==0,
115db 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 that means that
115dc 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 the content is
115dd 6e 65 65 64 65 64 0a 2a 2a 20 61 6e 64 20 74 68 needed.** and th
115de 65 20 64 69 73 6b 20 72 65 61 64 20 73 68 6f 75 e disk read shou
115df 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 ld occur at that
115e0 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 point..*/.SQLIT
115e1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
115e2 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 lite3PagerAcquir
115e3 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 e(. Pager *pPag
115e4 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 er, /* The
115e5 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68 pager open on th
115e6 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
115e7 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 */. Pgno pgno,
115e8 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
115e9 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 63 68 number to fetch
115ea 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 2a 70 */. DbPage **p
115eb 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 69 pPage, /* Wri
115ec 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 te a pointer to
115ed 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f the page here */
115ee 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 . int noContent
115ef 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 /* Do not
115f0 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e 67 20 bother reading
115f1 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73 content from dis
115f2 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a k if true */.){.
115f3 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30 PgHdr *pPg = 0
115f4 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 ;. int rc;.. a
115f5 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
115f6 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f tate==PAGER_UNLO
115f7 43 4b 20 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 CK . || sq
115f8 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
115f9 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
115fa 63 68 65 29 3e 30 20 0a 20 20 20 20 20 20 20 7c che)>0 . |
115fb 7c 20 70 67 6e 6f 3d 3d 31 0a 20 20 29 3b 0a 0a | pgno==1. );..
115fc 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d /* The maximum
115fd 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
115fe 32 5e 33 31 2e 20 52 65 74 75 72 6e 20 53 51 4c 2^31. Return SQL
115ff 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 20 61 ITE_CORRUPT if a
11600 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d 62 65 page. ** numbe
11601 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 r greater than t
11602 68 69 73 2c 20 6f 72 20 7a 65 72 6f 2c 20 69 73 his, or zero, is
11603 20 72 65 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f requested.. */
11604 0a 20 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45 . if( pgno>PAGE
11605 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 R_MAX_PGNO || pg
11606 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 no==0 || pgno==P
11607 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
11608 67 65 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ger) ){. retu
11609 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
1160a 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f T_BKPT;. }.. /
1160b 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68 * Make sure we h
1160c 61 76 65 20 6e 6f 74 20 68 69 74 20 61 6e 79 20 ave not hit any
1160d 63 72 69 74 69 63 61 6c 20 65 72 72 6f 72 73 2e critical errors.
1160e 0a 20 20 2a 2f 20 0a 20 20 61 73 73 65 72 74 28 . */ . assert(
1160f 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 pPager!=0 );.
11610 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 *ppPage = 0;..
11611 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 /* If this is th
11612 65 20 66 69 72 73 74 20 70 61 67 65 20 61 63 63 e first page acc
11613 65 73 73 65 64 2c 20 74 68 65 6e 20 67 65 74 20 essed, then get
11614 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 0a 20 20 a SHARED lock.
11615 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ** on the databa
11616 73 65 20 66 69 6c 65 2e 20 70 61 67 65 72 53 68 se file. pagerSh
11617 61 72 65 64 4c 6f 63 6b 28 29 20 69 73 20 61 20 aredLock() is a
11618 6e 6f 2d 6f 70 20 69 66 20 0a 20 20 2a 2a 20 61 no-op if . ** a
11619 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 69 database lock i
1161a 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a s already held..
1161b 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 */. rc = page
1161c 72 53 68 61 72 65 64 4c 6f 63 6b 28 70 50 61 67 rSharedLock(pPag
1161d 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 er);. if( rc!=S
1161e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1161f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
11620 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
11621 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e >state!=PAGER_UN
11622 4c 4f 43 4b 20 29 3b 0a 0a 20 20 72 63 20 3d 20 LOCK );.. rc =
11623 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
11624 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ch(pPager->pPCac
11625 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 26 70 50 he, pgno, 1, &pP
11626 67 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 g);. if( rc!=SQ
11627 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
11628 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
11629 69 66 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d if( pPg->pPager=
1162a 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 =0 ){. /* The
1162b 20 70 61 67 65 72 20 63 61 63 68 65 20 68 61 73 pager cache has
1162c 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 20 70 created a new p
1162d 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 age. Its content
1162e 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a needs to . *
1162f 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 * be initialized
11630 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 .. */. int
11631 20 6e 4d 61 78 3b 0a 20 20 20 20 50 41 47 45 52 nMax;. PAGER
11632 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d _INCR(pPager->nM
11633 69 73 73 29 3b 0a 20 20 20 20 70 50 67 2d 3e 70 iss);. pPg->p
11634 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a Pager = pPager;.
11635 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e memset(pPg->
11636 70 45 78 74 72 61 2c 20 30 2c 20 70 50 61 67 65 pExtra, 0, pPage
11637 72 2d 3e 6e 45 78 74 72 61 29 3b 0a 0a 20 20 20 r->nExtra);..
11638 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
11639 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 erPagecount(pPag
1163a 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20 er, &nMax);.
1163b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1163c 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
1163d 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 e3PagerUnref(pPg
1163e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1163f 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 rc;. }.. i
11640 66 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e f( nMax<(int)pgn
11641 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f o || MEMDB || no
11642 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 Content ){.
11643 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 if( pgno>pPager
11644 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20 ->mxPgno ){.
11645 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
11646 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 Unref(pPg);.
11647 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
11648 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a E_FULL;. }.
11649 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 memset(pPg
1164a 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 ->pData, 0, pPag
1164b 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 er->pageSize);.
1164c 20 20 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 65 if( noConte
1164d 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 nt ){. pP
1164e 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 g->flags |= PGHD
1164f 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 R_NEED_READ;.
11650 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54 52 41 }. IOTRA
11651 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25 64 5c CE(("ZERO %p %d\
11652 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f n", pPager, pgno
11653 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ));. }else{.
11654 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 44 62 rc = readDb
11655 50 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 67 Page(pPager, pPg
11656 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 , pgno);. i
11657 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
11658 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 && rc!=SQLITE_I
11659 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 OERR_SHORT_READ
1165a 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 71 ){. /* sq
1165b 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
1165c 70 50 67 29 3b 20 2a 2f 0a 20 20 20 20 20 20 20 pPg); */.
1165d 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 28 70 pagerDropPage(p
1165e 50 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 Pg);. ret
1165f 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
11660 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c }.#ifdef SQL
11661 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a ITE_CHECK_PAGES.
11662 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 pPg->pageHas
11663 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 h = pager_pageha
11664 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a sh(pPg);.#endif.
11665 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
11666 54 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 The requested pa
11667 67 65 20 69 73 20 69 6e 20 74 68 65 20 70 61 67 ge is in the pag
11668 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 e cache. */.
11669 61 73 73 65 72 74 28 73 71 6c 69 74 65 33 50 63 assert(sqlite3Pc
1166a 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 acheRefCount(pPa
1166b 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 ger->pPCache)>0
1166c 7c 7c 20 70 67 6e 6f 3d 3d 31 29 3b 0a 20 20 20 || pgno==1);.
1166d 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 PAGER_INCR(pPag
1166e 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20 20 69 er->nHit);. i
1166f 66 28 20 21 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b f( !noContent ){
11670 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
11671 72 5f 67 65 74 5f 63 6f 6e 74 65 6e 74 28 70 50 r_get_content(pP
11672 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 g);. if( rc
11673 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
11674 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
11675 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 g);. retu
11676 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
11677 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70 50 }. }.. *ppP
11678 61 67 65 20 3d 20 70 50 67 3b 0a 20 20 72 65 74 age = pPg;. ret
11679 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1167a 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 ../*.** Acquire
1167b 61 20 70 61 67 65 20 69 66 20 69 74 20 69 73 20 a page if it is
1167c 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 69 already in the i
1167d 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 n-memory cache.
1167e 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20 Do.** not read
1167f 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 64 69 the page from di
11680 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f sk. Return a po
11681 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 inter to the pag
11682 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 74 68 e,.** or 0 if th
11683 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e e page is not in
11684 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 cache..**.** Se
11685 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 e also sqlite3Pa
11686 67 65 72 47 65 74 28 29 2e 20 20 54 68 65 20 64 gerGet(). The d
11687 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
11688 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a n this routine.*
11689 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 * and sqlite3Pag
1168a 65 72 47 65 74 28 29 20 69 73 20 74 68 61 74 20 erGet() is that
1168b 5f 67 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74 _get() will go t
1168c 6f 20 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72 o the disk and r
1168d 65 61 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 ead.** in the pa
1168e 67 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69 ge if the page i
1168f 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e s not already in
11690 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f cache. This ro
11691 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 utine.** returns
11692 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67 NULL if the pag
11693 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 e is not in cach
11694 65 20 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49 e or if a disk I
11695 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73 /O error .** has
11696 20 65 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a ever happened..
11697 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
11698 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 E DbPage *sqlite
11699 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 3PagerLookup(Pag
1169a 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
1169b 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 pgno){. PgHdr
1169c 2a 70 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65 *pPg = 0;. asse
1169d 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b rt( pPager!=0 );
1169e 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 . assert( pgno!
1169f 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 70 50 =0 );.. if( (pP
116a0 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
116a1 45 52 5f 55 4e 4c 4f 43 4b 29 0a 20 20 20 26 26 ER_UNLOCK). &&
116a2 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 (pPager->errCod
116a3 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 e==SQLITE_OK ||
116a4 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d pPager->errCode=
116a5 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29 0a 20 20 =SQLITE_FULL).
116a6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 ){. sqlite3Pc
116a7 61 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 acheFetch(pPager
116a8 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c ->pPCache, pgno,
116a9 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 7d 0a 0a 0, &pPg);. }..
116aa 20 20 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a return pPg;.}.
116ab 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
116ac 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 page..**.** If
116ad 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 the number of re
116ae 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 ferences to the
116af 70 61 67 65 20 64 72 6f 70 20 74 6f 20 7a 65 72 page drop to zer
116b0 6f 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 70 o, then the.** p
116b1 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 20 age is added to
116b2 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20 20 57 the LRU list. W
116b3 68 65 6e 20 61 6c 6c 20 72 65 66 65 72 65 6e 63 hen all referenc
116b4 65 73 20 74 6f 20 61 6c 6c 20 70 61 67 65 73 0a es to all pages.
116b5 2a 2a 20 61 72 65 20 72 65 6c 65 61 73 65 64 2c ** are released,
116b6 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 a rollback occu
116b7 72 73 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 rs and the lock
116b8 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
116b9 69 73 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e 0a 2a is.** removed..*
116ba 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
116bb 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
116bc 72 55 6e 72 65 66 28 44 62 50 61 67 65 20 2a 70 rUnref(DbPage *p
116bd 50 67 29 7b 0a 20 20 69 66 28 20 70 50 67 20 29 Pg){. if( pPg )
116be 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 {. Pager *pPa
116bf 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 ger = pPg->pPage
116c0 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 r;. sqlite3Pc
116c1 61 63 68 65 52 65 6c 65 61 73 65 28 70 50 67 29 acheRelease(pPg)
116c2 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 ;. pagerUnloc
116c3 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 kIfUnused(pPager
116c4 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
116c5 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
116c6 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6a 6f 75 .** Create a jou
116c7 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 50 rnal file for pP
116c8 61 67 65 72 2e 20 20 54 68 65 72 65 20 73 68 6f ager. There sho
116c9 75 6c 64 20 61 6c 72 65 61 64 79 20 62 65 20 61 uld already be a
116ca 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6f 72 20 RESERVED.** or
116cb 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f EXCLUSIVE lock o
116cc 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
116cd 69 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f ile when this ro
116ce 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
116cf 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
116d0 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 LITE_OK if every
116d1 74 68 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 61 thing. Return a
116d2 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 n error code and
116d3 20 72 65 6c 65 61 73 65 20 74 68 65 0a 2a 2a 20 release the.**
116d4 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 61 6e write lock if an
116d5 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e ything goes wron
116d6 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
116d7 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 pager_open_jour
116d8 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 nal(Pager *pPage
116d9 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 r){. sqlite3_vf
116da 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 s *pVfs = pPager
116db 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 66 6c ->pVfs;. int fl
116dc 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 ags = (SQLITE_OP
116dd 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c EN_READWRITE|SQL
116de 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
116df 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 VE|SQLITE_OPEN_C
116e0 52 45 41 54 45 29 3b 0a 0a 20 20 69 6e 74 20 72 REATE);.. int r
116e1 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 c;. assert( pPa
116e2 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
116e3 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 R_RESERVED );.
116e4 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
116e5 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 useJournal );.
116e6 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
116e7 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b pInJournal==0 );
116e8 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 . sqlite3PagerP
116e9 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
116ea 20 30 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 0);. pPager->p
116eb 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 InJournal = sqli
116ec 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 te3BitvecCreate(
116ed 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b pPager->dbSize);
116ee 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 . if( pPager->p
116ef 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a InJournal==0 ){.
116f0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
116f1 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 NOMEM;. goto
116f2 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a failed_to_open_j
116f3 6f 75 72 6e 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 ournal;. }.. i
116f4 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
116f5 61 6c 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 alOpen==0 ){.
116f6 20 69 66 28 20 70 50 61 67 65 72 2d 3e 74 65 6d if( pPager->tem
116f7 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 66 pFile ){. f
116f8 6c 61 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f lags |= (SQLITE_
116f9 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
116fa 53 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 SE|SQLITE_OPEN_T
116fb 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 EMP_JOURNAL);.
116fc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 }else{. f
116fd 6c 61 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f lags |= (SQLITE_
116fe 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 OPEN_MAIN_JOURNA
116ff 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 L);. }. if
11700 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
11701 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
11702 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 RNALMODE_MEMORY
11703 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
11704 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 MemJournalOpen(p
11705 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 Pager->jfd);.
11706 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
11707 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69 K;. }else{.#i
11708 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
11709 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a LE_ATOMIC_WRITE.
1170a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1170b 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 e3JournalOpen(.
1170c 20 20 20 20 20 20 20 20 20 70 56 66 73 2c 20 70 pVfs, p
1170d 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
1170e 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c pPager->jfd, fl
1170f 61 67 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 ags, jrnlBufferS
11710 69 7a 65 28 70 50 61 67 65 72 29 0a 20 20 20 20 ize(pPager).
11711 20 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 );.#else.
11712 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
11713 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 pen(pVfs, pPager
11714 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 ->zJournal, pPag
11715 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 er->jfd, flags,
11716 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0);.#endif. }
11717 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 . assert( rc!
11718 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 =SQLITE_OK || pP
11719 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 ager->jfd->pMeth
1171a 6f 64 73 20 29 3b 0a 20 20 20 20 70 50 61 67 65 ods );. pPage
1171b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
1171c 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 0;. pPager->s
1171d 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 etMaster = 0;.
1171e 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
1171f 6c 48 64 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 lHdr = 0;. if
11720 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
11721 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d ){. if( rc=
11722 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b =SQLITE_NOMEM ){
11723 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
11724 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 70 OsDelete(pVfs, p
11725 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
11726 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
11727 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f 74 goto failed_t
11728 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a o_open_journal;.
11729 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 50 61 67 }. }. pPag
1172a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 er->journalOpen
1172b 3d 20 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a = 1;. pPager->j
1172c 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 ournalStarted =
1172d 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 0;. pPager->nee
1172e 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 dSync = 0;. pPa
1172f 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 ger->nRec = 0;.
11730 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 if( pPager->err
11731 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 20 3d Code ){. rc =
11732 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11733 3b 0a 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 ;. goto faile
11734 64 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 d_to_open_journa
11735 6c 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d l;. }. pPager-
11736 3e 6f 72 69 67 44 62 53 69 7a 65 20 3d 20 70 50 >origDbSize = pP
11737 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 0a 20 ager->dbSize;..
11738 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e rc = writeJourn
11739 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a 0a alHdr(pPager);..
1173a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
1173b 6d 74 41 75 74 6f 6f 70 65 6e 20 26 26 20 72 63 mtAutoopen && rc
1173c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1173d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1173e 61 67 65 72 53 74 6d 74 42 65 67 69 6e 28 70 50 agerStmtBegin(pP
1173f 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 ager);. }. if(
11740 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc!=SQLITE_OK &
11741 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4e 4f 4d & rc!=SQLITE_NOM
11742 45 4d 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 EM && rc!=SQLITE
11743 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a _IOERR_NOMEM ){.
11744 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 rc = pager_e
11745 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 nd_transaction(p
11746 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 69 Pager, 0);. i
11747 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
11748 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
11749 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 QLITE_FULL;.
1174a 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
1174b 63 3b 0a 0a 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 c;..failed_to_op
1174c 65 6e 5f 6a 6f 75 72 6e 61 6c 3a 0a 20 20 73 71 en_journal:. sq
1174d 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
1174e 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f oy(pPager->pInJo
1174f 75 72 6e 61 6c 29 3b 0a 20 20 70 50 61 67 65 72 urnal);. pPager
11750 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 ->pInJournal = 0
11751 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
11752 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 ../*.** Acquire
11753 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
11754 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 the database. T
11755 68 65 20 6c 6f 63 6b 20 69 73 20 72 65 6d 6f 76 he lock is remov
11756 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 61 ed when.** the a
11757 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ny of the follow
11758 69 6e 67 20 68 61 70 70 65 6e 3a 0a 2a 2a 0a 2a ing happen:.**.*
11759 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50 61 * * sqlite3Pa
1175a 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 gerCommitPhaseTw
1175b 6f 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a o() is called..*
1175c 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50 61 * * sqlite3Pa
1175d 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20 69 73 gerRollback() is
1175e 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 called..** *
1175f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
11760 73 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a se() is called..
11761 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50 ** * sqlite3P
11762 61 67 65 72 55 6e 72 65 66 28 29 20 69 73 20 63 agerUnref() is c
11763 61 6c 6c 65 64 20 74 6f 20 6f 6e 20 65 76 65 72 alled to on ever
11764 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 y outstanding pa
11765 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ge..**.** The fi
11766 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f rst parameter to
11767 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
11768 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
11769 79 20 6f 70 65 6e 20 70 61 67 65 20 6f 66 20 74 y open page of t
1176a 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 he.** database f
1176b 69 6c 65 2e 20 20 4e 6f 74 68 69 6e 67 20 63 68 ile. Nothing ch
1176c 61 6e 67 65 73 20 61 62 6f 75 74 20 74 68 65 20 anges about the
1176d 70 61 67 65 20 2d 20 69 74 20 69 73 20 75 73 65 page - it is use
1176e 64 20 6d 65 72 65 6c 79 20 74 6f 0a 2a 2a 20 61 d merely to.** a
1176f 63 71 75 69 72 65 20 61 20 70 6f 69 6e 74 65 72 cquire a pointer
11770 20 74 6f 20 74 68 65 20 50 61 67 65 72 20 73 74 to the Pager st
11771 72 75 63 74 75 72 65 20 61 6e 64 20 61 73 20 70 ructure and as p
11772 72 6f 6f 66 20 74 68 61 74 20 74 68 65 72 65 20 roof that there
11773 69 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 61 20 is.** already a
11774 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 read-lock on the
11775 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
11776 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 The second para
11777 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65 73 20 meter indicates
11778 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 how much space i
11779 6e 20 62 79 74 65 73 20 74 6f 20 72 65 73 65 72 n bytes to reser
1177a 76 65 20 66 6f 72 20 61 0a 2a 2a 20 6d 61 73 74 ve for a.** mast
1177b 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d er journal file-
1177c 6e 61 6d 65 20 61 74 20 74 68 65 20 73 74 61 72 name at the star
1177d 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c t of the journal
1177e 20 77 68 65 6e 20 69 74 20 69 73 20 63 72 65 61 when it is crea
1177f 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 6f 75 ted..**.** A jou
11780 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 rnal file is ope
11781 6e 65 64 20 69 66 20 74 68 69 73 20 69 73 20 6e ned if this is n
11782 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 ot a temporary f
11783 69 6c 65 2e 20 20 46 6f 72 20 74 65 6d 70 6f 72 ile. For tempor
11784 61 72 79 0a 2a 2a 20 66 69 6c 65 73 2c 20 74 68 ary.** files, th
11785 65 20 6f 70 65 6e 69 6e 67 20 6f 66 20 74 68 65 e opening of the
11786 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
11787 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 deferred until
11788 74 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a 20 61 there is an.** a
11789 63 74 75 61 6c 20 6e 65 65 64 20 74 6f 20 77 72 ctual need to wr
1178a 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ite to the journ
1178b 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 al..**.** If the
1178c 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 database is alr
1178d 65 61 64 79 20 72 65 73 65 72 76 65 64 20 66 6f eady reserved fo
1178e 72 20 77 72 69 74 69 6e 67 2c 20 74 68 69 73 20 r writing, this
1178f 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
11790 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 78 46 op..**.** If exF
11791 6c 61 67 20 69 73 20 74 72 75 65 2c 20 67 6f 20 lag is true, go
11792 61 68 65 61 64 20 61 6e 64 20 67 65 74 20 61 6e ahead and get an
11793 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
11794 6f 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 69 on the file.** i
11795 6d 6d 65 64 69 61 74 65 6c 79 20 69 6e 73 74 65 mmediately inste
11796 61 64 20 6f 66 20 77 61 69 74 69 6e 67 20 75 6e ad of waiting un
11797 74 69 6c 20 77 65 20 74 72 79 20 74 6f 20 66 6c til we try to fl
11798 75 73 68 20 74 68 65 20 63 61 63 68 65 2e 20 20 ush the cache.
11799 54 68 65 0a 2a 2a 20 65 78 46 6c 61 67 20 69 73 The.** exFlag is
1179a 20 69 67 6e 6f 72 65 64 20 69 66 20 61 20 74 72 ignored if a tr
1179b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 ansaction is alr
1179c 65 61 64 79 20 61 63 74 69 76 65 2e 0a 2a 2f 0a eady active..*/.
1179d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1179e 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 nt sqlite3PagerB
1179f 65 67 69 6e 28 44 62 50 61 67 65 20 2a 70 50 67 egin(DbPage *pPg
117a0 2c 20 69 6e 74 20 65 78 46 6c 61 67 29 7b 0a 20 , int exFlag){.
117a1 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
117a2 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
117a3 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
117a4 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 OK;. assert( pP
117a5 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 g->nRef>0 );. a
117a6 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
117a7 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f tate!=PAGER_UNLO
117a8 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 CK );. if( pPag
117a9 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
117aa 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 20 61 _SHARED ){. a
117ab 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
117ac 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a InJournal==0 );.
117ad 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d assert( !MEM
117ae 44 42 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 DB );. rc = s
117af 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 qlite3OsLock(pPa
117b0 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45 ger->fd, RESERVE
117b1 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 D_LOCK);. if(
117b2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
117b3 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e {. pPager->
117b4 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45 state = PAGER_RE
117b5 53 45 52 56 45 44 3b 0a 20 20 20 20 20 20 69 66 SERVED;. if
117b6 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20 ( exFlag ){.
117b7 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 rc = pager_w
117b8 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 ait_on_lock(pPag
117b9 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f er, EXCLUSIVE_LO
117ba 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 CK);. }.
117bb 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 }. if( rc!=S
117bc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
117bd 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
117be 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 }. pPager->d
117bf 69 72 74 79 43 61 63 68 65 20 3d 20 30 3b 0a 20 irtyCache = 0;.
117c0 20 20 20 50 41 47 45 52 54 52 41 43 45 32 28 22 PAGERTRACE2("
117c1 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e TRANSACTION %d\n
117c2 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
117c3 72 29 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 r));. if( pPa
117c4 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 ger->useJournal
117c5 26 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 && !pPager->temp
117c6 46 69 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 File.
117c7 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
117c8 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f alMode!=PAGER_JO
117c9 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b URNALMODE_OFF ){
117ca 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
117cb 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 r_open_journal(p
117cc 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 Pager);. }.
117cd 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 }else if( pPager
117ce 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 26 26 ->journalOpen &&
117cf 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
117d0 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a Off==0 ){. /*
117d1 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 This happens wh
117d2 65 6e 20 74 68 65 20 70 61 67 65 72 20 77 61 73 en the pager was
117d3 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 63 in exclusive-ac
117d4 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 6c 61 cess mode the la
117d5 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d 65 20 61 st. ** time a
117d6 20 28 72 65 61 64 20 6f 72 20 77 72 69 74 65 29 (read or write)
117d7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
117d8 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f successfully co
117d9 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a 2a 20 62 ncluded. ** b
117da 79 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f y this connectio
117db 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 64 65 n. Instead of de
117dc 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e leting the journ
117dd 61 6c 20 66 69 6c 65 20 69 74 20 77 61 73 20 0a al file it was .
117de 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f 70 65 6e ** kept open
117df 20 61 6e 64 20 65 69 74 68 65 72 20 77 61 73 20 and either was
117e0 74 72 75 6e 63 61 74 65 64 20 74 6f 20 30 20 62 truncated to 0 b
117e1 79 74 65 73 20 6f 72 20 69 74 73 20 68 65 61 64 ytes or its head
117e2 65 72 20 77 61 73 0a 20 20 20 20 2a 2a 20 6f 76 er was. ** ov
117e3 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 7a erwritten with z
117e4 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 eros.. */.
117e5 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
117e6 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 >nRec==0 );.
117e7 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
117e8 6f 72 69 67 44 62 53 69 7a 65 3d 3d 30 20 29 3b origDbSize==0 );
117e9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
117ea 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d ger->pInJournal=
117eb 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 =0 );. sqlite
117ec 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
117ed 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 pPager, 0);.
117ee 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
117ef 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 al = sqlite3Bitv
117f0 65 63 43 72 65 61 74 65 28 20 70 50 61 67 65 72 ecCreate( pPager
117f1 2d 3e 64 62 53 69 7a 65 20 29 3b 0a 20 20 20 20 ->dbSize );.
117f2 69 66 28 20 21 70 50 61 67 65 72 2d 3e 70 49 6e if( !pPager->pIn
117f3 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 Journal ){.
117f4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
117f5 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 EM;. }else{.
117f6 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6f 72 69 pPager->ori
117f7 67 44 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 gDbSize = pPager
117f8 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 20 20 ->dbSize;.
117f9 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 rc = writeJourna
117fa 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 lHdr(pPager);.
117fb 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 }. }. assert
117fc 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e ( !pPager->journ
117fd 61 6c 4f 70 65 6e 20 7c 7c 20 70 50 61 67 65 72 alOpen || pPager
117fe 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c ->journalOff>0 |
117ff 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 | rc!=SQLITE_OK
11800 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
11801 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 }../*.** Make a
11802 70 61 67 65 20 64 69 72 74 79 2e 20 20 53 65 74 page dirty. Set
11803 20 69 74 73 20 64 69 72 74 79 20 66 6c 61 67 20 its dirty flag
11804 61 6e 64 20 61 64 64 20 69 74 20 74 6f 20 74 68 and add it to th
11805 65 20 64 69 72 74 79 0a 2a 2a 20 70 61 67 65 20 e dirty.** page
11806 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 list..*/.static
11807 76 6f 69 64 20 6d 61 6b 65 44 69 72 74 79 28 50 void makeDirty(P
11808 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 73 71 gHdr *pPg){. sq
11809 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
1180a 69 72 74 79 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a irty(pPg);.}../*
1180b 0a 2a 2a 20 4d 61 6b 65 20 61 20 70 61 67 65 20 .** Make a page
1180c 63 6c 65 61 6e 2e 20 20 43 6c 65 61 72 20 69 74 clean. Clear it
1180d 73 20 64 69 72 74 79 20 62 69 74 20 61 6e 64 20 s dirty bit and
1180e 72 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d 20 74 remove it from t
1180f 68 65 0a 2a 2a 20 64 69 72 74 79 20 70 61 67 65 he.** dirty page
11810 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 list..*/.static
11811 20 76 6f 69 64 20 6d 61 6b 65 43 6c 65 61 6e 28 void makeClean(
11812 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 73 PgHdr *pPg){. s
11813 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
11814 43 6c 65 61 6e 28 70 50 67 29 3b 0a 7d 0a 0a 0a Clean(pPg);.}...
11815 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74 /*.** Mark a dat
11816 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61 a page as writea
11817 62 6c 65 2e 20 20 54 68 65 20 70 61 67 65 20 69 ble. The page i
11818 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 s written into t
11819 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 69 he journal .** i
1181a 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 f it is not ther
1181b 65 20 61 6c 72 65 61 64 79 2e 20 20 54 68 69 73 e already. This
1181c 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 routine must be
1181d 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d called before m
1181e 61 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e 67 65 73 aking.** changes
1181f 20 74 6f 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a to a page..**.*
11820 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 * The first time
11821 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
11822 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 70 61 67 called, the pag
11823 65 72 20 63 72 65 61 74 65 73 20 61 20 6e 65 77 er creates a new
11824 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 .** journal and
11825 61 63 71 75 69 72 65 73 20 61 20 52 45 53 45 52 acquires a RESER
11826 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 VED lock on the
11827 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 74 68 database. If th
11828 65 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f e RESERVED.** lo
11829 63 6b 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ck could not be
1182a 61 63 71 75 69 72 65 64 2c 20 74 68 69 73 20 72 acquired, this r
1182b 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 outine returns S
1182c 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 QLITE_BUSY. The
1182d 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 .** calling rout
1182e 69 6e 65 20 6d 75 73 74 20 63 68 65 63 6b 20 66 ine must check f
1182f 6f 72 20 74 68 61 74 20 72 65 74 75 72 6e 20 76 or that return v
11830 61 6c 75 65 20 61 6e 64 20 62 65 20 63 61 72 65 alue and be care
11831 66 75 6c 20 6e 6f 74 20 74 6f 0a 2a 2a 20 63 68 ful not to.** ch
11832 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20 64 61 ange any page da
11833 74 61 20 75 6e 74 69 6c 20 74 68 69 73 20 72 6f ta until this ro
11834 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 utine returns SQ
11835 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 LITE_OK..**.** I
11836 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
11837 6c 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 le could not be
11838 77 72 69 74 74 65 6e 20 62 65 63 61 75 73 65 20 written because
11839 74 68 65 20 64 69 73 6b 20 69 73 20 66 75 6c 6c the disk is full
1183a 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 ,.** then this r
1183b 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 outine returns S
1183c 51 4c 49 54 45 5f 46 55 4c 4c 20 61 6e 64 20 64 QLITE_FULL and d
1183d 6f 65 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 oes an immediate
1183e 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 41 6c rollback..** Al
1183f 6c 20 73 75 62 73 65 71 75 65 6e 74 20 77 72 69 l subsequent wri
11840 74 65 20 61 74 74 65 6d 70 74 73 20 61 6c 73 6f te attempts also
11841 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 return SQLITE_F
11842 55 4c 4c 20 75 6e 74 69 6c 20 74 68 65 72 65 0a ULL until there.
11843 2a 2a 20 69 73 20 61 20 63 61 6c 6c 20 74 6f 20 ** is a call to
11844 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
11845 69 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 50 it() or sqlite3P
11846 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20 74 agerRollback() t
11847 6f 0a 2a 2a 20 72 65 73 65 74 2e 0a 2a 2f 0a 73 o.** reset..*/.s
11848 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f tatic int pager_
11849 77 72 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 write(PgHdr *pPg
1184a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 ){. void *pData
1184b 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 = pPg->pData;.
1184c 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
1184d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
1184e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1184f 4f 4b 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 OK;.. /* Check
11850 66 6f 72 20 65 72 72 6f 72 73 0a 20 20 2a 2f 0a for errors. */.
11851 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 if( pPager->er
11852 72 43 6f 64 65 20 29 7b 20 0a 20 20 20 20 72 65 rCode ){ . re
11853 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 turn pPager->err
11854 43 6f 64 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Code;. }. if(
11855 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 pPager->readOnly
11856 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
11857 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 7d 0a QLITE_PERM;. }.
11858 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 . assert( !pPag
11859 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 3b er->setMaster );
1185a 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 45 28 70 .. CHECK_PAGE(p
1185b 50 67 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 Pg);.. /* If th
1185c 69 73 20 70 61 67 65 20 77 61 73 20 70 72 65 76 is page was prev
1185d 69 6f 75 73 6c 79 20 61 63 71 75 69 72 65 64 20 iously acquired
1185e 77 69 74 68 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d with noContent==
1185f 31 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 20 20 1, that means.
11860 2a 2a 20 77 65 20 64 69 64 6e 27 74 20 72 65 61 ** we didn't rea
11861 6c 6c 79 20 72 65 61 64 20 69 6e 20 74 68 65 20 lly read in the
11862 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 content of the p
11863 61 67 65 2e 20 20 54 68 69 73 20 63 61 6e 20 68 age. This can h
11864 61 70 70 65 6e 0a 20 20 2a 2a 20 28 66 6f 72 20 appen. ** (for
11865 65 78 61 6d 70 6c 65 29 20 77 68 65 6e 20 74 68 example) when th
11866 65 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 e page is being
11867 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72 65 moved to the fre
11868 65 6c 69 73 74 2e 20 20 42 75 74 0a 20 20 2a 2a elist. But. **
11869 20 6e 6f 77 20 77 65 20 61 72 65 20 28 70 65 72 now we are (per
1186a 68 61 70 73 29 20 6d 6f 76 69 6e 67 20 74 68 65 haps) moving the
1186b 20 70 61 67 65 20 6f 66 66 20 6f 66 20 74 68 65 page off of the
1186c 20 66 72 65 65 6c 69 73 74 20 66 6f 72 0a 20 20 freelist for.
1186d 2a 2a 20 72 65 75 73 65 20 61 6e 64 20 77 65 20 ** reuse and we
1186e 6e 65 65 64 20 74 6f 20 6b 6e 6f 77 20 69 74 73 need to know its
1186f 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e original conten
11870 74 20 73 6f 20 74 68 61 74 20 63 6f 6e 74 65 6e t so that conten
11871 74 0a 20 20 2a 2a 20 63 61 6e 20 62 65 20 73 74 t. ** can be st
11872 6f 72 65 64 20 69 6e 20 74 68 65 20 72 6f 6c 6c ored in the roll
11873 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 53 back journal. S
11874 6f 20 64 6f 20 74 68 65 20 72 65 61 64 20 61 74 o do the read at
11875 20 74 68 69 73 0a 20 20 2a 2a 20 74 69 6d 65 2e this. ** time.
11876 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 . */. rc = pag
11877 65 72 5f 67 65 74 5f 63 6f 6e 74 65 6e 74 28 70 er_get_content(p
11878 50 67 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b Pg);. if( rc ){
11879 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
1187a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 }.. /* Mark t
1187b 68 65 20 70 61 67 65 20 61 73 20 64 69 72 74 79 he page as dirty
1187c 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 68 . If the page h
1187d 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
1187e 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 74 6f 20 written. ** to
1187f 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 65 6e the journal then
11880 20 77 65 20 63 61 6e 20 72 65 74 75 72 6e 20 72 we can return r
11881 69 67 68 74 20 61 77 61 79 2e 0a 20 20 2a 2f 0a ight away.. */.
11882 20 20 6d 61 6b 65 44 69 72 74 79 28 70 50 67 29 makeDirty(pPg)
11883 3b 0a 20 20 69 66 28 20 70 61 67 65 49 6e 4a 6f ;. if( pageInJo
11884 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 28 70 urnal(pPg) && (p
11885 61 67 65 49 6e 53 74 61 74 65 6d 65 6e 74 28 70 ageInStatement(p
11886 50 67 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 Pg) || pPager->s
11887 74 6d 74 49 6e 55 73 65 3d 3d 30 29 20 29 7b 0a tmtInUse==0) ){.
11888 20 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 pPager->dirt
11889 79 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 20 20 yCache = 1;.
1188a 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
1188b 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b ed = 1;. }else{
1188c 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 67 .. /* If we g
1188d 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 et this far, it
1188e 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 means that the p
1188f 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a age needs to be.
11890 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 ** written t
11891 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f o the transactio
11892 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 n journal or the
11893 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 ckeckpoint jour
11894 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f nal. ** or bo
11895 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a th.. **. *
11896 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74 6f * First check to
11897 20 73 65 65 20 74 68 61 74 20 74 68 65 20 74 72 see that the tr
11898 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 ansaction journa
11899 6c 20 65 78 69 73 74 73 20 61 6e 64 0a 20 20 20 l exists and.
1189a 20 2a 2a 20 63 72 65 61 74 65 20 69 74 20 69 66 ** create it if
1189b 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20 it does not..
1189c 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
1189d 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d pPager->state!=
1189e 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a PAGER_UNLOCK );.
1189f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
118a0 50 61 67 65 72 42 65 67 69 6e 28 70 50 67 2c 20 PagerBegin(pPg,
118a1 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 0);. if( rc!=
118a2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
118a3 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
118a4 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
118a5 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
118a6 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b AGER_RESERVED );
118a7 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 . if( !pPager
118a8 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 26 26 ->journalOpen &&
118a9 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 pPager->useJour
118aa 6e 61 6c 0a 20 20 20 20 20 20 20 20 20 20 26 26 nal. &&
118ab 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
118ac 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode!=PAGER_JOUR
118ad 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 NALMODE_OFF ){.
118ae 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
118af 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 open_journal(pPa
118b0 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ger);. if(
118b1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
118b2 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
118b3 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 . pPager->dir
118b4 74 79 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 20 tyCache = 1;.
118b5 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 pPager->dbModif
118b6 69 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 ied = 1;. .
118b7 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 /* The transacti
118b8 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 on journal now e
118b9 78 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 xists and we hav
118ba 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 e a RESERVED or
118bb 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 an. ** EXCLUS
118bc 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 IVE lock on the
118bd 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
118be 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63 le. Write the c
118bf 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 urrent page to.
118c0 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 ** the transa
118c1 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 ction journal if
118c2 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 it is not there
118c3 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f already.. */
118c4 0a 20 20 20 20 69 66 28 20 21 70 61 67 65 49 6e . if( !pageIn
118c5 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 Journal(pPg) &&
118c6 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
118c7 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 pen ){. if(
118c8 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 pPg->pgno<=pPag
118c9 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 er->origDbSize )
118ca 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b {. u32 ck
118cb 73 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 sum;. cha
118cc 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 r *pData2;..
118cd 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 /* We should
118ce 20 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 never write to
118cf 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
118d0 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 the page that.
118d1 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 ** contai
118d2 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ns the database
118d3 6c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c locks. The foll
118d4 6f 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 owing assert ver
118d5 69 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a ifies. **
118d6 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e that we do not.
118d7 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 */. asse
118d8 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 rt( pPg->pgno!=P
118d9 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
118da 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 ger) );.
118db 70 44 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28 pData2 = CODEC2(
118dc 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 pPager, pData, p
118dd 50 67 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 Pg->pgno, 7);.
118de 20 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 cksum = pa
118df 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 ger_cksum(pPager
118e0 2c 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a , (u8*)pData2);.
118e1 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 rc = wri
118e2 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d te32bits(pPager-
118e3 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f >jfd, pPager->jo
118e4 75 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 urnalOff, pPg->p
118e5 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 gno);. if
118e6 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
118e7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
118e8 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
118e9 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 (pPager->jfd, pD
118ea 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 ata2, pPager->pa
118eb 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 geSize,.
118ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118ed 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
118ee 75 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 urnalOff + 4);.
118ef 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
118f0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 >journalOff += p
118f1 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b Pager->pageSize+
118f2 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 4;. }.
118f3 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
118f4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
118f5 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 rc = write32
118f6 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
118f7 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 , pPager->journa
118f8 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 lOff, cksum);.
118f9 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
118fa 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b journalOff += 4;
118fb 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
118fc 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 IOTRACE(("JOU
118fd 54 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c T %p %d %lld %d\
118fe 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d n", pPager, pPg-
118ff 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 >pgno, .
11900 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
11901 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 >journalOff, pPa
11902 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b ger->pageSize));
11903 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 5f 49 . PAGER_I
11904 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 NCR(sqlite3_page
11905 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b r_writej_count);
11906 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 . PAGERTR
11907 41 43 45 35 28 22 4a 4f 55 52 4e 41 4c 20 25 64 ACE5("JOURNAL %d
11908 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e page %d needSyn
11909 63 3d 25 64 20 68 61 73 68 28 25 30 38 78 29 5c c=%d hash(%08x)\
1190a 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
1190b 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
1190c 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 , pPg->pgno, .
1190d 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 ((pPg
1190e 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
1190f 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 ED_SYNC)?1:0), p
11910 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 ager_pagehash(pP
11911 67 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a g));.. /*
11912 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 An error has oc
11913 63 75 72 65 64 20 77 72 69 74 69 6e 67 20 74 6f cured writing to
11914 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11915 65 2e 20 54 68 65 20 0a 20 20 20 20 20 20 20 20 e. The .
11916 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 ** transaction w
11917 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 ill be rolled ba
11918 63 6b 20 62 79 20 74 68 65 20 6c 61 79 65 72 20 ck by the layer
11919 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a above.. *
1191a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 /. if( rc
1191b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1191c 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
1191d 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 rc;. }..
1191e 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e pPager->n
1191f 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61 Rec++;. a
11920 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
11921 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a InJournal!=0 );.
11922 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
11923 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d itvecSet(pPager-
11924 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 >pInJournal, pPg
11925 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
11926 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f if( !pPager->no
11927 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 Sync ){.
11928 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPg->flags |=
11929 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b PGHDR_NEED_SYNC;
1192a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1192b 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 if( pPager->s
1192c 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20 tmtInUse ){.
1192d 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 sqlite3Bit
1192e 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 vecSet(pPager->p
1192f 49 6e 53 74 6d 74 2c 20 70 50 67 2d 3e 70 67 6e InStmt, pPg->pgn
11930 6f 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 o);. }.
11931 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
11932 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
11933 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26 journalStarted &
11934 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e & !pPager->noSyn
11935 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 c ){. p
11936 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 Pg->flags |= PGH
11937 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
11938 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
11939 50 41 47 45 52 54 52 41 43 45 34 28 22 41 50 50 PAGERTRACE4("APP
1193a 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e END %d page %d n
1193b 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 eedSync=%d\n",.
1193c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
1193d 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
1193e 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 pPg->pgno,.
1193f 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d ((pPg-
11940 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
11941 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 3b 0a 20 D_SYNC)?1:0));.
11942 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
11943 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 pPg->flags&PGHD
11944 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 R_NEED_SYNC ){.
11945 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e pPager->n
11946 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 eedSync = 1;.
11947 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
11948 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 /* If the stat
11949 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73 ement journal is
1194a 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 open and the pa
1194b 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74 2c ge is not in it,
1194c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72 69 . ** then wri
1194d 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 te the current p
1194e 61 67 65 20 74 6f 20 74 68 65 20 73 74 61 74 65 age to the state
1194f 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e ment journal. N
11950 6f 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 ote that. **
11951 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f the statement jo
11952 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69 66 urnal format dif
11953 66 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73 74 fers from the st
11954 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20 66 andard journal f
11955 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e 20 ormat. ** in
11956 74 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74 68 that it omits th
11957 65 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64 20 e checksums and
11958 74 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20 the header..
11959 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 */. if( pPage
1195a 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 0a 20 20 r->stmtInUse .
1195b 20 20 20 26 26 20 21 70 61 67 65 49 6e 53 74 61 && !pageInSta
1195c 74 65 6d 65 6e 74 28 70 50 67 29 20 0a 20 20 20 tement(pPg) .
1195d 20 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d && pPg->pgno<=
1195e 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 pPager->stmtSize
1195f 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 . ){. i
11960 36 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67 64 offset = pPag
11961 65 72 2d 3e 73 74 6d 74 4e 52 65 63 2a 28 34 2b er->stmtNRec*(4+
11962 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
11963 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 );. char *p
11964 44 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70 Data2 = CODEC2(p
11965 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 Pager, pData, pP
11966 67 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20 g->pgno, 7);.
11967 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 49 assert( pageI
11968 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c nJournal(pPg) ||
11969 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 pPg->pgno>pPage
1196a 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 3b r->origDbSize );
1196b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 . rc = writ
1196c 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
1196d 73 74 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50 stfd, offset, pP
1196e 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 g->pgno);.
1196f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
11970 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 K ){. rc
11971 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
11972 28 70 50 61 67 65 72 2d 3e 73 74 66 64 2c 20 70 (pPager->stfd, p
11973 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 Data2, pPager->p
11974 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b ageSize, offset+
11975 34 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 4);. }.
11976 20 20 50 41 47 45 52 54 52 41 43 45 33 28 22 53 PAGERTRACE3("S
11977 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 TMT-JOURNAL %d p
11978 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 age %d\n", PAGER
11979 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d ID(pPager), pPg-
1197a 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 >pgno);. if
1197b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1197c 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
1197d 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
1197e 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 pPager->stmt
1197f 4e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 61 73 NRec++;. as
11980 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 sert( pPager->pI
11981 6e 53 74 6d 74 21 3d 30 20 29 3b 0a 20 20 20 20 nStmt!=0 );.
11982 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 sqlite3BitvecS
11983 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 et(pPager->pInSt
11984 6d 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a mt, pPg->pgno);.
11985 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
11986 55 70 64 61 74 65 20 74 68 65 20 64 61 74 61 62 Update the datab
11987 61 73 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74 ase size and ret
11988 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 urn.. */. asse
11989 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
1198a 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 e>=PAGER_SHARED
1198b 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d );. if( pPager-
1198c 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e >dbSize<pPg->pgn
1198d 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d o ){. pPager-
1198e 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 >dbSize = pPg->p
1198f 67 6e 6f 3b 0a 20 20 20 20 69 66 28 20 70 50 61 gno;. if( pPa
11990 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 28 50 41 ger->dbSize==(PA
11991 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
11992 65 72 29 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 er)-1) ){.
11993 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 2b 2b pPager->dbSize++
11994 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
11995 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
11996 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
11997 69 73 20 75 73 65 64 20 74 6f 20 6d 61 72 6b 20 is used to mark
11998 61 20 64 61 74 61 2d 70 61 67 65 20 61 73 20 77 a data-page as w
11999 72 69 74 61 62 6c 65 2e 20 49 74 20 75 73 65 73 ritable. It uses
1199a 20 0a 2a 2a 20 70 61 67 65 72 5f 77 72 69 74 65 .** pager_write
1199b 28 29 20 74 6f 20 6f 70 65 6e 20 61 20 6a 6f 75 () to open a jou
1199c 72 6e 61 6c 20 66 69 6c 65 20 28 69 66 20 69 74 rnal file (if it
1199d 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
1199e 6f 70 65 6e 29 0a 2a 2a 20 61 6e 64 20 77 72 69 open).** and wri
1199f 74 65 20 74 68 65 20 70 61 67 65 20 2a 70 44 61 te the page *pDa
119a0 74 61 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 ta to the journa
119a1 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 l..**.** The dif
119a2 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 ference between
119a3 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e this function an
119a4 64 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 20 d pager_write()
119a5 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 is that this.**
119a6 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 64 65 function also de
119a7 61 6c 73 20 77 69 74 68 20 74 68 65 20 73 70 65 als with the spe
119a8 63 69 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 cial case where
119a9 32 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 0a 2 or more pages.
119aa 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 ** fit on a sing
119ab 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 le disk sector.
119ac 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c In this case all
119ad 20 63 6f 2d 72 65 73 69 64 65 6e 74 20 70 61 67 co-resident pag
119ae 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 es.** must have
119af 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 been written to
119b0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
119b1 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
119b2 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
119b3 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
119b4 50 61 67 65 72 57 72 69 74 65 28 44 62 50 61 67 PagerWrite(DbPag
119b5 65 20 2a 70 44 62 50 61 67 65 29 7b 0a 20 20 69 e *pDbPage){. i
119b6 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
119b7 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a 70 50 67 K;.. PgHdr *pPg
119b8 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 50 61 = pDbPage;. Pa
119b9 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
119ba 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 50 67 6e g->pPager;. Pgn
119bb 6f 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 o nPagePerSector
119bc 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 = (pPager->sect
119bd 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e 70 orSize/pPager->p
119be 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 69 66 28 ageSize);.. if(
119bf 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 3e nPagePerSector>
119c0 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 50 1 ){. Pgno nP
119c1 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 ageCount;
119c2 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 /* Total numb
119c3 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 er of pages in d
119c4 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
119c5 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20 20 20 Pgno pg1;
119c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
119c7 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 First page of t
119c8 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 he sector pPg is
119c9 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a located on. */.
119ca 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 int nPage;
119cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
119cc 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
119cd 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 31 starting at pg1
119ce 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 to journal */.
119cf 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 69 int ii;. i
119d0 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b nt needSync = 0;
119d1 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 .. /* Set the
119d2 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 doNotSync flag
119d3 74 6f 20 31 2e 20 54 68 69 73 20 69 73 20 62 65 to 1. This is be
119d4 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 cause we cannot
119d5 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a allow a journal.
119d6 20 20 20 20 2a 2a 20 68 65 61 64 65 72 20 74 6f ** header to
119d7 20 62 65 20 77 72 69 74 74 65 6e 20 62 65 74 77 be written betw
119d8 65 65 6e 20 74 68 65 20 70 61 67 65 73 20 6a 6f een the pages jo
119d9 75 72 6e 61 6c 65 64 20 62 79 20 74 68 69 73 20 urnaled by this
119da 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f function.. */
119db 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 . assert( !ME
119dc 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72 MDB );. asser
119dd 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 t( pPager->doNot
119de 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 70 Sync==0 );. p
119df 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 Pager->doNotSync
119e0 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 = 1;.. /* Th
119e1 69 73 20 74 72 69 63 6b 20 61 73 73 75 6d 65 73 is trick assumes
119e2 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 70 that both the p
119e3 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 age-size and sec
119e4 74 6f 72 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 tor-size are.
119e5 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 70 ** an integer p
119e6 6f 77 65 72 20 6f 66 20 32 2e 20 49 74 20 73 65 ower of 2. It se
119e7 74 73 20 76 61 72 69 61 62 6c 65 20 70 67 31 20 ts variable pg1
119e8 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 to the identifie
119e9 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 r. ** of the
119ea 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 first page of th
119eb 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 20 e sector pPg is
119ec 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 located on..
119ed 2a 2f 0a 20 20 20 20 70 67 31 20 3d 20 28 28 70 */. pg1 = ((p
119ee 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 Pg->pgno-1) & ~(
119ef 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31 nPagePerSector-1
119f0 29 29 20 2b 20 31 3b 0a 0a 20 20 20 20 73 71 6c )) + 1;.. sql
119f1 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
119f2 6e 74 28 70 50 61 67 65 72 2c 20 28 69 6e 74 20 nt(pPager, (int
119f3 2a 29 26 6e 50 61 67 65 43 6f 75 6e 74 29 3b 0a *)&nPageCount);.
119f4 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e if( pPg->pgn
119f5 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a o>nPageCount ){.
119f6 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 70 nPage = (p
119f7 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b Pg->pgno - pg1)+
119f8 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 1;. }else if(
119f9 20 28 70 67 31 2b 6e 50 61 67 65 50 65 72 53 65 (pg1+nPagePerSe
119fa 63 74 6f 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75 ctor-1)>nPageCou
119fb 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 nt ){. nPag
119fc 65 20 3d 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31 e = nPageCount+1
119fd 2d 70 67 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b -pg1;. }else{
119fe 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e . nPage = n
119ff 50 61 67 65 50 65 72 53 65 63 74 6f 72 3b 0a 20 PagePerSector;.
11a00 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
11a01 6e 50 61 67 65 3e 30 29 3b 0a 20 20 20 20 61 73 nPage>0);. as
11a02 73 65 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 sert(pg1<=pPg->p
11a03 67 6e 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 gno);. assert
11a04 28 28 70 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 ((pg1+nPage)>pPg
11a05 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f ->pgno);.. fo
11a06 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 r(ii=0; ii<nPage
11a07 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f && rc==SQLITE_O
11a08 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 K; ii++){.
11a09 50 67 6e 6f 20 70 67 20 3d 20 70 67 31 2b 69 69 Pgno pg = pg1+ii
11a0a 3b 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 ;. PgHdr *p
11a0b 50 61 67 65 3b 0a 20 20 20 20 20 20 69 66 28 20 Page;. if(
11a0c 70 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c 7c pg==pPg->pgno ||
11a0d 20 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 !sqlite3BitvecT
11a0e 65 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a est(pPager->pInJ
11a0f 6f 75 72 6e 61 6c 2c 20 70 67 29 20 29 7b 0a 20 ournal, pg) ){.
11a10 20 20 20 20 20 20 20 69 66 28 20 70 67 21 3d 50 if( pg!=P
11a11 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
11a12 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ger) ){.
11a13 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
11a14 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 70 gerGet(pPager, p
11a15 67 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20 20 g, &pPage);.
11a16 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
11a17 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
11a18 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 rc = page
11a19 72 5f 77 72 69 74 65 28 70 50 61 67 65 29 3b 0a r_write(pPage);.
11a1a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
11a1b 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 pPage->flags&PGH
11a1c 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a DR_NEED_SYNC ){.
11a1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 ne
11a1e 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 edSync = 1;.
11a1f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
11a20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
11a21 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a erUnref(pPage);.
11a22 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
11a23 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
11a24 65 20 69 66 28 20 28 70 50 61 67 65 20 3d 20 70 e if( (pPage = p
11a25 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 ager_lookup(pPag
11a26 65 72 2c 20 70 67 29 29 21 3d 30 20 29 7b 0a 20 er, pg))!=0 ){.
11a27 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
11a28 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
11a29 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 ED_SYNC ){.
11a2a 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 needSync =
11a2b 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 1;. }.
11a2c 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
11a2d 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 rUnref(pPage);.
11a2e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
11a2f 20 20 2f 2a 20 49 66 20 74 68 65 20 50 67 48 64 /* If the PgHd
11a30 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 r.needSync flag
11a31 69 73 20 73 65 74 20 66 6f 72 20 61 6e 79 20 6f is set for any o
11a32 66 20 74 68 65 20 6e 50 61 67 65 20 70 61 67 65 f the nPage page
11a33 73 20 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 69 s . ** starti
11a34 6e 67 20 61 74 20 70 67 31 2c 20 74 68 65 6e 20 ng at pg1, then
11a35 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 it needs to be s
11a36 65 74 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 et for all of th
11a37 65 6d 2e 20 42 65 63 61 75 73 65 0a 20 20 20 20 em. Because.
11a38 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e ** writing to an
11a39 79 20 6f 66 20 74 68 65 73 65 20 6e 50 61 67 65 y of these nPage
11a3a 20 70 61 67 65 73 20 6d 61 79 20 64 61 6d 61 67 pages may damag
11a3b 65 20 74 68 65 20 6f 74 68 65 72 73 2c 20 74 68 e the others, th
11a3c 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c e. ** journal
11a3d 20 66 69 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 file must conta
11a3e 69 6e 20 73 79 6e 63 28 29 65 64 20 63 6f 70 69 in sync()ed copi
11a3f 65 73 20 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 es of all of the
11a40 6d 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 m. ** before
11a41 61 6e 79 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 any of them can
11a42 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 be written out t
11a43 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
11a44 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ile.. */.
11a45 69 66 28 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a if( needSync ){.
11a46 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 4d assert( !M
11a47 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e EMDB && pPager->
11a48 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 noSync==0 );.
11a49 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c for(ii=0; ii<
11a4a 6e 50 61 67 65 20 26 26 20 6e 65 65 64 53 79 6e nPage && needSyn
11a4b 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 c; ii++){.
11a4c 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d PgHdr *pPage =
11a4d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 pager_lookup(pP
11a4e 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b 0a 20 ager, pg1+ii);.
11a4f 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
11a50 20 29 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 ) pPage->flags
11a51 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 |= PGHDR_NEED_SY
11a52 4e 43 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 NC;. sqli
11a53 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
11a54 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 age);. }.
11a55 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 assert(pPage
11a56 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20 r->needSync);.
11a57 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 }.. assert(
11a58 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 pPager->doNotSy
11a59 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 50 61 nc==1 );. pPa
11a5a 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d ger->doNotSync =
11a5b 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
11a5c 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 rc = pager_writ
11a5d 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a e(pDbPage);. }.
11a5e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
11a5f 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 /*.** Return TRU
11a60 45 20 69 66 20 74 68 65 20 70 61 67 65 20 67 69 E if the page gi
11a61 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d ven in the argum
11a62 65 6e 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 ent was previous
11a63 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 ly passed.** to
11a64 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
11a65 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 e(). In other w
11a66 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 ords, return TRU
11a67 45 20 69 66 20 69 74 20 69 73 20 6f 6b 0a 2a 2a E if it is ok.**
11a68 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 63 to change the c
11a69 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 ontent of the pa
11a6a 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e ge..*/.#ifndef N
11a6b 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
11a6c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
11a6d 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
11a6e 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 (DbPage *pPg){.
11a6f 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 66 6c 61 return pPg->fla
11a70 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 3b 0a gs&PGHDR_DIRTY;.
11a71 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
11a72 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 A call to this r
11a73 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 74 68 65 outine tells the
11a74 20 70 61 67 65 72 20 74 68 61 74 20 69 74 20 69 pager that it i
11a75 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 s not necessary
11a76 74 6f 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20 to.** write the
11a77 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 70 information on p
11a78 61 67 65 20 70 50 67 20 62 61 63 6b 20 74 6f 20 age pPg back to
11a79 74 68 65 20 64 69 73 6b 2c 20 65 76 65 6e 20 74 the disk, even t
11a7a 68 6f 75 67 68 0a 2a 2a 20 74 68 61 74 20 70 61 hough.** that pa
11a7b 67 65 20 6d 69 67 68 74 20 62 65 20 6d 61 72 6b ge might be mark
11a7c 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 54 68 ed as dirty. Th
11a7d 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72 20 is happens, for
11a7e 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a 2a 2a example, when.**
11a7f 20 74 68 65 20 70 61 67 65 20 68 61 73 20 62 65 the page has be
11a80 65 6e 20 61 64 64 65 64 20 61 73 20 61 20 6c 65 en added as a le
11a81 61 66 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 af of the freeli
11a82 73 74 20 61 6e 64 20 73 6f 20 69 74 73 0a 2a 2a st and so its.**
11a83 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c 6f 6e 67 content no long
11a84 65 72 20 6d 61 74 74 65 72 73 2e 0a 2a 2a 0a 2a er matters..**.*
11a85 2a 20 54 68 65 20 6f 76 65 72 6c 79 69 6e 67 20 * The overlying
11a86 73 6f 66 74 77 61 72 65 20 6c 61 79 65 72 20 63 software layer c
11a87 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e alls this routin
11a88 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66 20 74 68 e when all of th
11a89 65 20 64 61 74 61 0a 2a 2a 20 6f 6e 20 74 68 65 e data.** on the
11a8a 20 67 69 76 65 6e 20 70 61 67 65 20 69 73 20 75 given page is u
11a8b 6e 75 73 65 64 2e 20 20 54 68 65 20 70 61 67 65 nused. The page
11a8c 72 20 6d 61 72 6b 73 20 74 68 65 20 70 61 67 65 r marks the page
11a8d 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a 2a 2a 20 as clean so.**
11a8e 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f 74 that it does not
11a8f 20 67 65 74 20 77 72 69 74 74 65 6e 20 74 6f 20 get written to
11a90 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 disk..**.** Test
11a91 73 20 73 68 6f 77 20 74 68 61 74 20 74 68 69 73 s show that this
11a92 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74 optimization, t
11a93 6f 67 65 74 68 65 72 20 77 69 74 68 20 74 68 65 ogether with the
11a94 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 .** sqlite3Pager
11a95 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 62 DontRollback() b
11a96 65 6c 6f 77 2c 20 6d 6f 72 65 20 74 68 61 6e 20 elow, more than
11a97 64 6f 75 62 6c 65 20 74 68 65 20 73 70 65 65 64 double the speed
11a98 0a 2a 2a 20 6f 66 20 6c 61 72 67 65 20 49 4e 53 .** of large INS
11a99 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 ERT operations a
11a9a 6e 64 20 71 75 61 64 72 75 70 6c 65 20 74 68 65 nd quadruple the
11a9b 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67 65 20 speed of large
11a9c 44 45 4c 45 54 45 73 2e 0a 2a 2a 0a 2a 2a 20 57 DELETEs..**.** W
11a9d 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
11a9e 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 65 74 20 is called, set
11a9f 74 68 65 20 61 6c 77 61 79 73 52 6f 6c 6c 62 61 the alwaysRollba
11aa0 63 6b 20 66 6c 61 67 20 74 6f 20 74 72 75 65 2e ck flag to true.
11aa1 0a 2a 2a 20 53 75 62 73 65 71 75 65 6e 74 20 63 .** Subsequent c
11aa2 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50 alls to sqlite3P
11aa3 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b agerDontRollback
11aa4 28 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 () for the same
11aa5 70 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65 page.** will the
11aa6 72 65 61 66 74 65 72 20 62 65 20 69 67 6e 6f 72 reafter be ignor
11aa7 65 64 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 ed. This is nec
11aa8 65 73 73 61 72 79 20 74 6f 20 61 76 6f 69 64 20 essary to avoid
11aa9 61 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 77 68 65 a problem.** whe
11aaa 72 65 20 61 20 70 61 67 65 20 77 69 74 68 20 64 re a page with d
11aab 61 74 61 20 69 73 20 61 64 64 65 64 20 74 6f 20 ata is added to
11aac 74 68 65 20 66 72 65 65 6c 69 73 74 20 64 75 72 the freelist dur
11aad 69 6e 67 20 6f 6e 65 20 70 61 72 74 20 6f 66 0a ing one part of.
11aae 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ** a transaction
11aaf 20 74 68 65 6e 20 72 65 6d 6f 76 65 64 20 66 72 then removed fr
11ab0 6f 6d 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 om the freelist
11ab1 64 75 72 69 6e 67 20 61 20 6c 61 74 65 72 20 70 during a later p
11ab2 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73 61 art.** of the sa
11ab3 6d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 me transaction a
11ab4 6e 64 20 72 65 75 73 65 64 20 66 6f 72 20 73 6f nd reused for so
11ab5 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 me other purpose
11ab6 2e 20 20 57 68 65 6e 20 69 74 0a 2a 2a 20 69 73 . When it.** is
11ab7 20 66 69 72 73 74 20 61 64 64 65 64 20 74 6f 20 first added to
11ab8 74 68 65 20 66 72 65 65 6c 69 73 74 2c 20 74 68 the freelist, th
11ab9 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
11aba 6c 6c 65 64 2e 20 20 57 68 65 6e 20 72 65 75 73 lled. When reus
11abb 65 64 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 ed,.** the sqlit
11abc 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 e3PagerDontRollb
11abd 61 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 69 73 ack() routine is
11abe 20 63 61 6c 6c 65 64 2e 20 20 42 75 74 20 62 65 called. But be
11abf 63 61 75 73 65 20 74 68 65 0a 2a 2a 20 70 61 67 cause the.** pag
11ac0 65 20 63 6f 6e 74 61 69 6e 73 20 63 72 69 74 69 e contains criti
11ac1 63 61 6c 20 64 61 74 61 2c 20 77 65 20 73 74 69 cal data, we sti
11ac2 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 73 75 ll need to be su
11ac3 72 65 20 69 74 20 67 65 74 73 0a 2a 2a 20 72 6f re it gets.** ro
11ac4 6c 6c 65 64 20 62 61 63 6b 20 69 6e 20 73 70 69 lled back in spi
11ac5 74 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 te of the sqlite
11ac6 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 3PagerDontRollba
11ac7 63 6b 28 29 20 63 61 6c 6c 2e 0a 2a 2f 0a 53 51 ck() call..*/.SQ
11ac8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
11ac9 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e sqlite3PagerDon
11aca 74 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70 tWrite(DbPage *p
11acb 44 62 50 61 67 65 29 7b 0a 20 20 50 67 48 64 72 DbPage){. PgHdr
11acc 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b *pPg = pDbPage;
11acd 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
11ace 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
11acf 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 int rc;.. if(
11ad0 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 pPg->pgno>pPage
11ad1 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 7b r->origDbSize ){
11ad2 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
11ad3 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 TE_OK;. }. if(
11ad4 20 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 pPager->pAlways
11ad5 52 6f 6c 6c 62 61 63 6b 3d 3d 30 20 29 7b 0a 20 Rollback==0 ){.
11ad6 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
11ad7 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b r->pInJournal );
11ad8 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 41 6c . pPager->pAl
11ad9 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 73 waysRollback = s
11ada 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
11adb 74 65 28 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 te(pPager->origD
11adc 62 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 bSize);. if(
11add 21 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 !pPager->pAlways
11ade 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 Rollback ){.
11adf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
11ae0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d NOMEM;. }. }
11ae1 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
11ae2 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d itvecSet(pPager-
11ae3 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b >pAlwaysRollback
11ae4 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 , pPg->pgno);..
11ae5 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
11ae6 4f 4b 20 26 26 20 28 70 50 67 2d 3e 66 6c 61 67 OK && (pPg->flag
11ae7 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 26 s&PGHDR_DIRTY) &
11ae8 26 20 21 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 & !pPager->stmtI
11ae9 6e 55 73 65 20 29 7b 0a 20 20 20 20 61 73 73 65 nUse ){. asse
11aea 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
11aeb 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 e>=PAGER_SHARED
11aec 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 );. if( pPage
11aed 72 2d 3e 64 62 53 69 7a 65 3d 3d 70 50 67 2d 3e r->dbSize==pPg->
11aee 70 67 6e 6f 20 26 26 20 70 50 61 67 65 72 2d 3e pgno && pPager->
11aef 6f 72 69 67 44 62 53 69 7a 65 3c 70 50 61 67 65 origDbSize<pPage
11af0 72 2d 3e 64 62 53 69 7a 65 20 29 7b 0a 20 20 20 r->dbSize ){.
11af1 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 61 /* If this pa
11af2 67 65 73 20 69 73 20 74 68 65 20 6c 61 73 74 20 ges is the last
11af3 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 page in the file
11af4 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61 and the file ha
11af5 73 20 67 72 6f 77 6e 0a 20 20 20 20 20 20 2a 2a s grown. **
11af6 20 64 75 72 69 6e 67 20 74 68 65 20 63 75 72 72 during the curr
11af7 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c ent transaction,
11af8 20 74 68 65 6e 20 64 6f 20 4e 4f 54 20 6d 61 72 then do NOT mar
11af9 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c k the page as cl
11afa 65 61 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 57 68 ean.. ** Wh
11afb 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
11afc 66 69 6c 65 20 67 72 6f 77 73 2c 20 77 65 20 6d file grows, we m
11afd 75 73 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 ust make sure th
11afe 61 74 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 at the last page
11aff 0a 20 20 20 20 20 20 2a 2a 20 67 65 74 73 20 77 . ** gets w
11b00 72 69 74 74 65 6e 20 61 74 20 6c 65 61 73 74 20 ritten at least
11b01 6f 6e 63 65 20 73 6f 20 74 68 61 74 20 74 68 65 once so that the
11b02 20 64 69 73 6b 20 66 69 6c 65 20 77 69 6c 6c 20 disk file will
11b03 62 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a 20 be the correct.
11b04 20 20 20 20 20 2a 2a 20 73 69 7a 65 2e 20 49 66 ** size. If
11b05 20 79 6f 75 20 64 6f 20 6e 6f 74 20 77 72 69 74 you do not writ
11b06 65 20 74 68 69 73 20 70 61 67 65 20 61 6e 64 20 e this page and
11b07 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
11b08 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 6f 6e file. ** on
11b09 20 74 68 65 20 64 69 73 6b 20 65 6e 64 73 20 75 the disk ends u
11b0a 70 20 62 65 69 6e 67 20 74 6f 6f 20 73 6d 61 6c p being too smal
11b0b 6c 2c 20 74 68 61 74 20 63 61 6e 20 6c 65 61 64 l, that can lead
11b0c 20 74 6f 20 64 61 74 61 62 61 73 65 0a 20 20 20 to database.
11b0d 20 20 20 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e ** corruption
11b0e 20 64 75 72 69 6e 67 20 74 68 65 20 6e 65 78 74 during the next
11b0f 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 transaction..
11b10 20 20 20 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 */. }else
11b11 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 {. PAGERTRA
11b12 43 45 33 28 22 44 4f 4e 54 5f 57 52 49 54 45 20 CE3("DONT_WRITE
11b13 70 61 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 page %d of %d\n"
11b14 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 , pPg->pgno, PAG
11b15 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20 ERID(pPager));.
11b16 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 43 IOTRACE(("C
11b17 4c 45 41 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 LEAN %p %d\n", p
11b18 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f Pager, pPg->pgno
11b19 29 29 0a 20 20 20 20 20 20 70 50 67 2d 3e 66 6c )). pPg->fl
11b1a 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 4f 4e ags |= PGHDR_DON
11b1b 54 5f 57 52 49 54 45 3b 0a 23 69 66 64 65 66 20 T_WRITE;.#ifdef
11b1c 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
11b1d 45 53 0a 20 20 20 20 20 20 70 50 67 2d 3e 70 61 ES. pPg->pa
11b1e 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 geHash = pager_p
11b1f 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65 agehash(pPg);.#e
11b20 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ndif. }. }.
11b21 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
11b22 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 *.** A call to t
11b23 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c his routine tell
11b24 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61 74 s the pager that
11b25 20 69 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f if a rollback o
11b26 63 63 75 72 73 2c 0a 2a 2a 20 69 74 20 69 73 20 ccurs,.** it is
11b27 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f not necessary to
11b28 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 restore the dat
11b29 61 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 a on the given p
11b2a 61 67 65 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 65 age. This.** me
11b2b 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67 ans that the pag
11b2c 65 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 er does not have
11b2d 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 67 to record the g
11b2e 69 76 65 6e 20 70 61 67 65 20 69 6e 20 74 68 65 iven page in the
11b2f 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 .** rollback jou
11b30 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 rnal..**.** If w
11b31 65 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 61 e have not yet a
11b32 63 74 75 61 6c 6c 79 20 72 65 61 64 20 74 68 65 ctually read the
11b33 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 content of this
11b34 20 70 61 67 65 20 28 69 66 0a 2a 2a 20 74 68 65 page (if.** the
11b35 20 50 67 48 64 72 2e 6e 65 65 64 52 65 61 64 20 PgHdr.needRead
11b36 66 6c 61 67 20 69 73 20 73 65 74 29 20 74 68 65 flag is set) the
11b37 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 n this routine a
11b38 63 74 73 20 61 73 20 61 20 70 72 6f 6d 69 73 65 cts as a promise
11b39 0a 2a 2a 20 74 68 61 74 20 77 65 20 77 69 6c 6c .** that we will
11b3a 20 6e 65 76 65 72 20 6e 65 65 64 20 74 6f 20 72 never need to r
11b3b 65 61 64 20 74 68 65 20 70 61 67 65 20 63 6f 6e ead the page con
11b3c 74 65 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 tent in the futu
11b3d 72 65 2e 0a 2a 2a 20 73 6f 20 74 68 65 20 6e 65 re..** so the ne
11b3e 65 64 52 65 61 64 20 66 6c 61 67 20 63 61 6e 20 edRead flag can
11b3f 62 65 20 63 6c 65 61 72 65 64 20 61 74 20 74 68 be cleared at th
11b40 69 73 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c is point..*/.SQL
11b41 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
11b42 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e sqlite3PagerDon
11b43 74 52 6f 6c 6c 62 61 63 6b 28 44 62 50 61 67 65 tRollback(DbPage
11b44 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 *pPg){. Pager
11b45 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
11b46 50 61 67 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 Pager;.. assert
11b47 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
11b48 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 =PAGER_RESERVED
11b49 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
11b4a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
11b4b 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 44 6f 6e not open, or Don
11b4c 74 57 72 69 74 65 28 29 20 68 61 73 20 62 65 65 tWrite() has bee
11b4d 6e 20 63 61 6c 6c 65 64 20 6f 6e 0a 20 20 2a 2a n called on. **
11b4e 20 74 68 69 73 20 70 61 67 65 20 28 44 6f 6e 74 this page (Dont
11b4f 57 72 69 74 65 28 29 20 73 65 74 73 20 74 68 65 Write() sets the
11b50 20 61 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 alwaysRollback
11b51 66 6c 61 67 29 2c 20 74 68 65 6e 20 74 68 69 73 flag), then this
11b52 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 . ** function i
11b53 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a s a no-op.. */.
11b54 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f if( pPager->jo
11b55 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 0a 20 20 urnalOpen==0 .
11b56 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 65 || sqlite3Bitve
11b57 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 41 cTest(pPager->pA
11b58 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 2c 20 70 lwaysRollback, p
11b59 50 67 2d 3e 70 67 6e 6f 29 0a 20 20 20 7c 7c 20 Pg->pgno). ||
11b5a 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 pPg->pgno>pPager
11b5b 2d 3e 6f 72 69 67 44 62 53 69 7a 65 0a 20 20 29 ->origDbSize. )
11b5c 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
11b5d 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
11b5e 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 _SECURE_DELETE.
11b5f 20 69 66 28 20 73 71 6c 69 74 65 33 42 69 74 76 if( sqlite3Bitv
11b60 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 ecTest(pPager->p
11b61 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e InJournal, pPg->
11b62 70 67 6e 6f 29 21 3d 30 0a 20 20 20 7c 7c 20 70 pgno)!=0. || p
11b63 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d Pg->pgno>pPager-
11b64 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 7b 0a 20 >origDbSize ){.
11b65 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 return;. }.#
11b66 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 53 endif.. /* If S
11b67 45 43 55 52 45 5f 44 45 4c 45 54 45 20 69 73 20 ECURE_DELETE is
11b68 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 disabled, then t
11b69 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 here is no way t
11b6a 68 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 72 6f hat this. ** ro
11b6b 75 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c utine can be cal
11b6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 20 66 6f led on a page fo
11b6d 72 20 77 68 69 63 68 20 73 71 6c 69 74 65 33 50 r which sqlite3P
11b6e 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 29 0a agerDontWrite().
11b6f 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 ** has not bee
11b70 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 63 61 6c n previously cal
11b71 6c 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 73 led during the s
11b72 61 6d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ame transaction.
11b73 0a 20 20 2a 2a 20 41 6e 64 20 69 66 20 44 6f 6e . ** And if Don
11b74 74 57 72 69 74 65 28 29 20 68 61 73 20 70 72 65 tWrite() has pre
11b75 76 69 6f 75 73 6c 79 20 62 65 65 6e 20 63 61 6c viously been cal
11b76 6c 65 64 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 led, the followi
11b77 6e 67 0a 20 20 2a 2a 20 63 6f 6e 64 69 74 69 6f ng. ** conditio
11b78 6e 73 20 6d 75 73 74 20 62 65 20 6d 65 74 2e 0a ns must be met..
11b79 20 20 2a 2a 0a 20 20 2a 2a 20 28 4c 61 74 65 72 **. ** (Later
11b7a 3a 29 20 20 4e 6f 74 20 74 72 75 65 2e 20 20 49 :) Not true. I
11b7b 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
11b7c 73 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 68 s corrupted by h
11b7d 61 76 69 6e 67 20 64 75 70 6c 69 63 61 74 65 0a aving duplicate.
11b7e 20 20 2a 2a 20 70 61 67 65 73 20 6f 6e 20 74 68 ** pages on th
11b7f 65 20 66 72 65 65 6c 69 73 74 20 28 65 78 3a 20 e freelist (ex:
11b80 63 6f 72 72 75 70 74 39 2e 74 65 73 74 29 20 74 corrupt9.test) t
11b81 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e hen the followin
11b82 67 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 6e 65 g is not. ** ne
11b83 63 65 73 73 61 72 69 6c 79 20 74 72 75 65 3a 0a cessarily true:.
11b84 20 20 2a 2f 0a 20 20 2f 2a 20 61 73 73 65 72 74 */. /* assert
11b85 28 20 21 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61 ( !pPg->inJourna
11b86 6c 20 26 26 20 28 69 6e 74 29 70 50 67 2d 3e 70 l && (int)pPg->p
11b87 67 6e 6f 20 3c 3d 20 70 50 61 67 65 72 2d 3e 6f gno <= pPager->o
11b88 72 69 67 44 62 53 69 7a 65 20 29 3b 20 2a 2f 0a rigDbSize ); */.
11b89 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
11b8a 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 r->pInJournal!=0
11b8b 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 );. sqlite3Bit
11b8c 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 vecSet(pPager->p
11b8d 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e InJournal, pPg->
11b8e 70 67 6e 6f 29 3b 0a 20 20 70 50 67 2d 3e 66 6c pgno);. pPg->fl
11b8f 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 ags &= ~PGHDR_NE
11b90 45 44 5f 52 45 41 44 3b 0a 20 20 69 66 28 20 70 ED_READ;. if( p
11b91 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 Pager->stmtInUse
11b92 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
11b93 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 pPager->stmtSize
11b94 20 3e 3d 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 >= pPager->orig
11b95 44 62 53 69 7a 65 20 29 3b 0a 20 20 20 20 73 71 DbSize );. sq
11b96 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 lite3BitvecSet(p
11b97 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20 Pager->pInStmt,
11b98 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a pPg->pgno);. }.
11b99 20 20 50 41 47 45 52 54 52 41 43 45 33 28 22 44 PAGERTRACE3("D
11b9a 4f 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 70 61 67 ONT_ROLLBACK pag
11b9b 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 e %d of %d\n", p
11b9c 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 Pg->pgno, PAGERI
11b9d 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 49 4f D(pPager));. IO
11b9e 54 52 41 43 45 28 28 22 47 41 52 42 41 47 45 20 TRACE(("GARBAGE
11b9f 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 %p %d\n", pPager
11ba0 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 7d 0a , pPg->pgno)).}.
11ba1 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
11ba2 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 tine is called t
11ba3 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 o increment the
11ba4 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 database file ch
11ba5 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 0a 2a 2a ange-counter,.**
11ba6 20 73 74 6f 72 65 64 20 61 74 20 62 79 74 65 20 stored at byte
11ba7 32 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 24 of the pager
11ba8 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
11ba9 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 int pager_incr_c
11baa 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 hangecounter(Pag
11bab 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
11bac 69 73 44 69 72 65 63 74 29 7b 0a 20 20 50 67 48 isDirect){. PgH
11bad 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 75 33 dr *pPgHdr;. u3
11bae 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2 change_counter
11baf 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
11bb0 49 54 45 5f 4f 4b 3b 0a 0a 23 69 66 6e 64 65 66 ITE_OK;..#ifndef
11bb1 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 SQLITE_ENABLE_A
11bb2 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 61 73 TOMIC_WRITE. as
11bb3 73 65 72 74 28 20 69 73 44 69 72 65 63 74 3d 3d sert( isDirect==
11bb4 30 20 29 3b 20 20 2f 2a 20 69 73 44 69 72 65 63 0 ); /* isDirec
11bb5 74 20 69 73 20 6f 6e 6c 79 20 74 72 75 65 20 66 t is only true f
11bb6 6f 72 20 61 74 6f 6d 69 63 20 77 72 69 74 65 73 or atomic writes
11bb7 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 */.#endif. if(
11bb8 20 21 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 !pPager->change
11bb9 43 6f 75 6e 74 44 6f 6e 65 20 29 7b 0a 20 20 20 CountDone ){.
11bba 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 20 /* Open page 1
11bbb 6f 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 of the file for
11bbc 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 writing. */.
11bbd 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
11bbe 72 47 65 74 28 70 50 61 67 65 72 2c 20 31 2c 20 rGet(pPager, 1,
11bbf 26 70 50 67 48 64 72 29 3b 0a 20 20 20 20 69 66 &pPgHdr);. if
11bc0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
11bc1 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 ) return rc;..
11bc2 20 20 69 66 28 20 21 69 73 44 69 72 65 63 74 20 if( !isDirect
11bc3 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
11bc4 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
11bc5 70 50 67 48 64 72 29 3b 0a 20 20 20 20 20 20 69 pPgHdr);. i
11bc6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
11bc7 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
11bc8 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
11bc9 67 48 64 72 29 3b 0a 20 20 20 20 20 20 20 20 72 gHdr);. r
11bca 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
11bcb 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
11bcc 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 Increment the va
11bcd 6c 75 65 20 6a 75 73 74 20 72 65 61 64 20 61 6e lue just read an
11bce 64 20 77 72 69 74 65 20 69 74 20 62 61 63 6b 20 d write it back
11bcf 74 6f 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 to byte 24. */.
11bd0 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 change_counte
11bd1 72 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 r = sqlite3Get4b
11bd2 79 74 65 28 28 75 38 2a 29 70 50 61 67 65 72 2d yte((u8*)pPager-
11bd3 3e 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20 20 >dbFileVers);.
11bd4 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 change_counter
11bd5 2b 2b 3b 0a 20 20 20 20 70 75 74 33 32 62 69 74 ++;. put32bit
11bd6 73 28 28 28 63 68 61 72 2a 29 70 50 67 48 64 72 s(((char*)pPgHdr
11bd7 2d 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68 61 ->pData)+24, cha
11bd8 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 23 nge_counter);..#
11bd9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
11bda 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 BLE_ATOMIC_WRITE
11bdb 0a 20 20 20 20 69 66 28 20 69 73 44 69 72 65 63 . if( isDirec
11bdc 74 20 26 26 20 70 50 61 67 65 72 2d 3e 66 64 2d t && pPager->fd-
11bdd 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 >pMethods ){.
11bde 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a const void *z
11bdf 42 75 66 20 3d 20 70 50 67 48 64 72 2d 3e 70 44 Buf = pPgHdr->pD
11be0 61 74 61 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 ata;. rc =
11be1 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
11be2 50 61 67 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c Pager->fd, zBuf,
11be3 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
11be4 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e e, 0);. }.#en
11be5 64 69 66 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 dif.. /* Rele
11be6 61 73 65 20 74 68 65 20 70 61 67 65 20 72 65 66 ase the page ref
11be7 65 72 65 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 erence. */. s
11be8 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
11be9 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 70 50 (pPgHdr);. pP
11bea 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e ager->changeCoun
11beb 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 7d 0a 20 tDone = 1;. }.
11bec 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
11bed 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 70 61 *.** Sync the pa
11bee 67 65 72 20 66 69 6c 65 20 74 6f 20 64 69 73 6b ger file to disk
11bef 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
11bf0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
11bf1 61 67 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a agerSync(Pager *
11bf2 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 pPager){. int r
11bf3 63 3b 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 c;. if( MEMDB )
11bf4 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
11bf5 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
11bf6 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
11bf7 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 sSync(pPager->fd
11bf8 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 , pPager->sync_f
11bf9 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 lags);. }. ret
11bfa 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
11bfb 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 Sync the databa
11bfc 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 se file for the
11bfd 70 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a 4d pager pPager. zM
11bfe 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 aster points to
11bff 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 the name.** of a
11c00 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11c01 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 file that should
11c02 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f be written into
11c03 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 0a the individual.
11c04 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e ** journal file.
11c05 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65 20 zMaster may be
11c06 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 69 NULL, which is i
11c07 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 6e 6f nterpreted as no
11c08 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e master.** journ
11c09 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61 74 al (a single dat
11c0a 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f abase transactio
11c0b 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 n)..**.** This r
11c0c 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74 outine ensures t
11c0d 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
11c0e 69 73 20 73 79 6e 63 65 64 2c 20 61 6c 6c 20 64 is synced, all d
11c0f 69 72 74 79 20 70 61 67 65 73 20 77 72 69 74 74 irty pages writt
11c10 65 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 en.** to the dat
11c11 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 abase file and t
11c12 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11c13 20 73 79 6e 63 65 64 2e 20 54 68 65 20 6f 6e 6c synced. The onl
11c14 79 20 74 68 69 6e 67 20 74 68 61 74 0a 2a 2a 20 y thing that.**
11c15 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69 remains to commi
11c16 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
11c17 6e 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 n is to delete t
11c18 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11c19 28 6f 72 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f (or.** master jo
11c1a 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70 urnal file if sp
11c1b 65 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 ecified)..**.**
11c1c 4e 6f 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61 Note that if zMa
11c1d 73 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 ster==NULL, this
11c1e 20 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 does not overwr
11c1f 69 74 65 20 61 20 70 72 65 76 69 6f 75 73 20 76 ite a previous v
11c20 61 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 alue.** passed t
11c21 6f 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 o an sqlite3Page
11c22 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 rCommitPhaseOne(
11c23 29 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 ) call..**.** If
11c24 20 70 61 72 61 6d 65 74 65 72 20 6e 54 72 75 6e parameter nTrun
11c25 63 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 c is non-zero, t
11c26 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 hen the pager fi
11c27 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 le is truncated
11c28 74 6f 0a 2a 2a 20 6e 54 72 75 6e 63 20 70 61 67 to.** nTrunc pag
11c29 65 73 20 28 74 68 69 73 20 69 73 20 75 73 65 64 es (this is used
11c2a 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 by auto-vacuum
11c2b 64 61 74 61 62 61 73 65 73 29 2e 0a 2a 2a 0a 2a databases)..**.*
11c2c 2a 20 49 66 20 74 68 65 20 66 69 6e 61 6c 20 70 * If the final p
11c2d 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f 53 79 6e arameter - noSyn
11c2e 63 20 2d 20 69 73 20 74 72 75 65 2c 20 74 68 65 c - is true, the
11c2f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
11c30 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a 20 69 73 ile itself.** is
11c31 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20 54 68 65 not synced. The
11c32 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c caller must cal
11c33 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 l sqlite3PagerSy
11c34 6e 63 28 29 20 64 69 72 65 63 74 6c 79 20 74 6f nc() directly to
11c35 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20 64 61 74 .** sync the dat
11c36 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
11c37 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 e calling Commit
11c38 50 68 61 73 65 54 77 6f 28 29 20 74 6f 20 64 65 PhaseTwo() to de
11c39 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 lete the.** jour
11c3a 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74 68 69 73 nal file in this
11c3b 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 case..*/.SQLITE
11c3c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
11c3d 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
11c3e 68 61 73 65 4f 6e 65 28 0a 20 20 50 61 67 65 72 haseOne(. Pager
11c3f 20 2a 70 50 61 67 65 72 2c 20 0a 20 20 63 6f 6e *pPager, . con
11c40 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 st char *zMaster
11c41 2c 20 0a 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63 , . Pgno nTrunc
11c42 2c 0a 20 20 69 6e 74 20 6e 6f 53 79 6e 63 0a 29 ,. int noSync.)
11c43 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
11c44 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 ITE_OK;.. if( p
11c45 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 Pager->errCode )
11c46 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 {. return pPa
11c47 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 ger->errCode;.
11c48 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 63 68 }.. /* If no ch
11c49 61 6e 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 anges have been
11c4a 6d 61 64 65 2c 20 77 65 20 63 61 6e 20 6c 65 61 made, we can lea
11c4b 76 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ve the transacti
11c4c 6f 6e 20 65 61 72 6c 79 2e 0a 20 20 2a 2f 0a 20 on early.. */.
11c4d 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d if( pPager->dbM
11c4e 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 0a 20 20 odified==0 &&.
11c4f 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 6a (pPager->j
11c50 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 ournalMode!=PAGE
11c51 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 R_JOURNALMODE_DE
11c52 4c 45 54 45 20 7c 7c 0a 20 20 20 20 20 20 20 20 LETE ||.
11c53 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 pPager->exclus
11c54 69 76 65 4d 6f 64 65 21 3d 30 29 20 29 7b 0a 20 iveMode!=0) ){.
11c55 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
11c56 72 2d 3e 64 69 72 74 79 43 61 63 68 65 3d 3d 30 r->dirtyCache==0
11c57 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
11c58 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29 3b 0a 20 20 nalOpen==0 );.
11c59 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
11c5a 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 OK;. }.. PAGER
11c5b 54 52 41 43 45 34 28 22 44 41 54 41 42 41 53 45 TRACE4("DATABASE
11c5c 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a SYNC: File=%s z
11c5d 4d 61 73 74 65 72 3d 25 73 20 6e 54 72 75 6e 63 Master=%s nTrunc
11c5e 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 =%d\n", . p
11c5f 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 Pager->zFilename
11c60 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 54 72 75 6e , zMaster, nTrun
11c61 63 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 c);.. /* If thi
11c62 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 s is an in-memor
11c63 79 20 64 62 2c 20 6f 72 20 6e 6f 20 70 61 67 65 y db, or no page
11c64 73 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 s have been writ
11c65 74 65 6e 20 74 6f 2c 20 6f 72 20 74 68 69 73 0a ten to, or this.
11c66 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 ** function ha
11c67 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 s already been c
11c68 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e alled, it is a n
11c69 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 o-op.. */. if(
11c6a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d pPager->state!=
11c6b 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26 26 20 PAGER_SYNCED &&
11c6c 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 !MEMDB && pPager
11c6d 2d 3e 64 69 72 74 79 43 61 63 68 65 20 29 7b 0a ->dirtyCache ){.
11c6e 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a PgHdr *pPg;.
11c6f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
11c70 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 NABLE_ATOMIC_WRI
11c71 54 45 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 74 TE. /* The at
11c72 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d omic-write optim
11c73 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 ization can be u
11c74 73 65 64 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 sed if all of th
11c75 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 e. ** followi
11c76 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 20 20 20 ng are true:.
11c77 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 **. ** +
11c78 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 The file-system
11c79 73 75 70 70 6f 72 74 73 20 74 68 65 20 61 74 6f supports the ato
11c7a 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70 65 72 mic-write proper
11c7b 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20 20 20 ty for. **
11c7c 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a blocks of siz
11c7d 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 6e 64 e page-size, and
11c7e 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 69 . ** + Thi
11c7f 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 20 s commit is not
11c80 70 61 72 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d part of a multi-
11c81 66 69 6c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e file transaction
11c82 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 20 20 20 , and. **
11c83 2b 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 + Exactly one pa
11c84 67 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 ge has been modi
11c85 66 69 65 64 20 61 6e 64 20 73 74 6f 72 65 20 69 fied and store i
11c86 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
11c87 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a le.. **. *
11c88 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a * If the optimiz
11c89 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 ation can be use
11c8a 64 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 d, then the jour
11c8b 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 6e 65 nal file will ne
11c8c 76 65 72 0a 20 20 20 20 2a 2a 20 62 65 20 63 72 ver. ** be cr
11c8d 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 74 eated for this t
11c8e 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 ransaction..
11c8f 2a 2f 0a 20 20 20 20 69 6e 74 20 75 73 65 41 74 */. int useAt
11c90 6f 6d 69 63 57 72 69 74 65 3b 0a 20 20 20 20 70 omicWrite;. p
11c91 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 Pg = sqlite3Pcac
11c92 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 heDirtyList(pPag
11c93 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
11c94 20 20 75 73 65 41 74 6f 6d 69 63 57 72 69 74 65 useAtomicWrite
11c95 20 3d 20 28 0a 20 20 20 20 20 20 20 20 21 7a 4d = (. !zM
11c96 61 73 74 65 72 20 26 26 20 0a 20 20 20 20 20 20 aster && .
11c97 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11c98 6c 4f 70 65 6e 20 26 26 0a 20 20 20 20 20 20 20 lOpen &&.
11c99 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11c9a 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 Off==jrnlBufferS
11c9b 69 7a 65 28 70 50 61 67 65 72 29 20 26 26 20 0a ize(pPager) && .
11c9c 20 20 20 20 20 20 20 20 6e 54 72 75 6e 63 3d 3d nTrunc==
11c9d 30 20 26 26 20 0a 20 20 20 20 20 20 20 20 28 70 0 && . (p
11c9e 50 67 3d 3d 30 20 7c 7c 20 70 50 67 2d 3e 70 44 Pg==0 || pPg->pD
11c9f 69 72 74 79 3d 3d 30 29 0a 20 20 20 20 29 3b 0a irty==0). );.
11ca0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
11ca1 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 er->journalOpen
11ca2 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e || pPager->journ
11ca3 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
11ca4 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b URNALMODE_OFF );
11ca5 0a 20 20 20 20 69 66 28 20 75 73 65 41 74 6f 6d . if( useAtom
11ca6 69 63 57 72 69 74 65 20 29 7b 0a 20 20 20 20 20 icWrite ){.
11ca7 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 6e /* Update the n
11ca8 52 65 63 20 66 69 65 6c 64 20 69 6e 20 74 68 65 Rec field in the
11ca9 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a journal file. *
11caa 2f 0a 20 20 20 20 20 20 69 6e 74 20 6f 66 66 73 /. int offs
11cab 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 et = pPager->jou
11cac 72 6e 61 6c 48 64 72 20 2b 20 73 69 7a 65 6f 66 rnalHdr + sizeof
11cad 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 3b (aJournalMagic);
11cae 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 . assert(pP
11caf 61 67 65 72 2d 3e 6e 52 65 63 3d 3d 31 29 3b 0a ager->nRec==1);.
11cb0 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 rc = write
11cb1 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
11cb2 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50 61 67 fd, offset, pPag
11cb3 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 er->nRec);..
11cb4 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
11cb5 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 db file change c
11cb6 6f 75 6e 74 65 72 2e 20 54 68 65 20 66 6f 6c 6c ounter. The foll
11cb7 6f 77 69 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 owing call will
11cb8 6d 6f 64 69 66 79 0a 20 20 20 20 20 20 2a 2a 20 modify. **
11cb9 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 the in-memory re
11cba 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
11cbb 70 61 67 65 20 31 20 74 6f 20 69 6e 63 6c 75 64 page 1 to includ
11cbc 65 20 74 68 65 20 75 70 64 61 74 65 64 0a 20 20 e the updated.
11cbd 20 20 20 20 2a 2a 20 63 68 61 6e 67 65 20 63 6f ** change co
11cbe 75 6e 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77 unter and then w
11cbf 72 69 74 65 20 70 61 67 65 20 31 20 64 69 72 65 rite page 1 dire
11cc0 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61 74 61 ctly to the data
11cc1 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 66 69 base. ** fi
11cc2 6c 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 le. Because of t
11cc3 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 he atomic-write
11cc4 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 property of the
11cc5 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 74 65 6d host file-system
11cc6 2c 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 , . ** this
11cc7 20 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 20 is safe..
11cc8 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
11cc9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
11cca 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
11ccb 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
11ccc 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 3b 0a ter(pPager, 1);.
11ccd 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
11cce 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 e{. rc = sq
11ccf 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 lite3JournalCrea
11cd0 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b te(pPager->jfd);
11cd1 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
11cd2 21 75 73 65 41 74 6f 6d 69 63 57 72 69 74 65 20 !useAtomicWrite
11cd3 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b && rc==SQLITE_OK
11cd4 20 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f ).#endif.. /
11cd5 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f * If a master jo
11cd6 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
11cd7 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
11cd8 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a written to the.
11cd9 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 ** journal f
11cda 69 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 73 79 6e ile, then no syn
11cdb 63 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 54 c is required. T
11cdc 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e his happens when
11cdd 20 69 74 20 69 73 0a 20 20 20 20 2a 2a 20 77 72 it is. ** wr
11cde 69 74 74 65 6e 2c 20 74 68 65 6e 20 74 68 65 20 itten, then the
11cdf 70 72 6f 63 65 73 73 20 66 61 69 6c 73 20 74 6f process fails to
11ce0 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 61 20 upgrade from a
11ce1 52 45 53 45 52 56 45 44 20 74 6f 20 61 6e 0a 20 RESERVED to an.
11ce2 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 ** EXCLUSIVE
11ce3 6c 6f 63 6b 2e 20 54 68 65 20 6e 65 78 74 20 74 lock. The next t
11ce4 69 6d 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 ime the process
11ce5 74 72 69 65 73 20 74 6f 20 63 6f 6d 6d 69 74 20 tries to commit
11ce6 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 the. ** trans
11ce7 61 63 74 69 6f 6e 20 74 68 65 20 6d 2d 6a 20 6e action the m-j n
11ce8 61 6d 65 20 77 69 6c 6c 20 68 61 76 65 20 61 6c ame will have al
11ce9 72 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 ready been writt
11cea 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 en.. */. i
11ceb 66 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d f( !pPager->setM
11cec 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 72 aster ){. r
11ced 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 c = pager_incr_c
11cee 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 hangecounter(pPa
11cef 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 ger, 0);. i
11cf0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
11cf1 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 ) goto sync_exi
11cf2 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 t;. if( pPa
11cf3 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
11cf4 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d !=PAGER_JOURNALM
11cf5 4f 44 45 5f 4f 46 46 20 29 7b 0a 23 69 66 6e 64 ODE_OFF ){.#ifnd
11cf6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
11cf7 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
11cf8 20 20 69 66 28 20 6e 54 72 75 6e 63 21 3d 30 20 if( nTrunc!=0
11cf9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
11cfa 49 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 If this transact
11cfb 69 6f 6e 20 68 61 73 20 6d 61 64 65 20 74 68 65 ion has made the
11cfc 20 64 61 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 database smalle
11cfd 72 2c 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 r, then all page
11cfe 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 s. ** b
11cff 65 69 6e 67 20 64 69 73 63 61 72 64 65 64 20 62 eing discarded b
11d00 79 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e y the truncation
11d01 20 6d 75 73 74 20 62 65 20 77 72 69 74 74 65 6e must be written
11d02 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a to the journal.
11d03 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c ** fil
11d04 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a e.. */.
11d05 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 Pgno i
11d06 3b 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f ;. Pgno
11d07 20 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d iSkip = PAGER_M
11d08 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b 0a J_PGNO(pPager);.
11d09 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 20 69 for( i
11d0a 3d 6e 54 72 75 6e 63 2b 31 3b 20 69 3c 3d 70 50 =nTrunc+1; i<=pP
11d0b 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 ager->origDbSize
11d0c 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 ; i++ ){.
11d0d 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 if( !sqlite
11d0e 33 42 69 74 76 65 63 54 65 73 74 28 70 50 61 67 3BitvecTest(pPag
11d0f 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 er->pInJournal,
11d10 69 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 i) && i!=iSkip )
11d11 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
11d12 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
11d13 72 47 65 74 28 70 50 61 67 65 72 2c 20 69 2c 20 rGet(pPager, i,
11d14 26 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 &pPg);.
11d15 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
11d16 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 ITE_OK ) goto sy
11d17 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 nc_exit;.
11d18 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
11d19 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
11d1a 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 g);.
11d1b 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
11d1c 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 20 20 ref(pPg);.
11d1d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
11d1e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
11d1f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 sync_exit;.
11d20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
11d21 20 20 20 20 7d 20 0a 20 20 20 20 20 20 20 20 7d } . }
11d22 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
11d23 72 63 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72 rc = writeMaster
11d24 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 Journal(pPager,
11d25 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 zMaster);.
11d26 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
11d27 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f _OK ) goto sync_
11d28 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 72 63 exit;. rc
11d29 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 = syncJournal(p
11d2a 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a Pager);. }.
11d2b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
11d2c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
11d2d 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 0a 23 to sync_exit;..#
11d2e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
11d2f 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
11d30 20 20 69 66 28 20 6e 54 72 75 6e 63 21 3d 30 20 if( nTrunc!=0
11d31 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
11d32 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 lite3PagerTrunca
11d33 74 65 28 70 50 61 67 65 72 2c 20 6e 54 72 75 6e te(pPager, nTrun
11d34 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 c);. if( rc
11d35 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
11d36 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 to sync_exit;.
11d37 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
11d38 2f 2a 20 57 72 69 74 65 20 61 6c 6c 20 64 69 72 /* Write all dir
11d39 74 79 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 ty pages to the
11d3a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
11d3b 0a 20 20 20 20 70 50 67 20 3d 20 73 71 6c 69 74 . pPg = sqlit
11d3c 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 e3PcacheDirtyLis
11d3d 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
11d3e 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 e);. rc = pag
11d3f 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 er_write_pagelis
11d40 74 28 70 50 67 29 3b 0a 20 20 20 20 69 66 28 20 t(pPg);. if(
11d41 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
11d42 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
11d43 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f c!=SQLITE_IOERR_
11d44 42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 BLOCKED );.
11d45 20 2f 2a 20 54 68 65 20 65 72 72 6f 72 20 6d 69 /* The error mi
11d46 67 68 74 20 68 61 76 65 20 6c 65 66 74 20 74 68 ght have left th
11d47 65 20 64 69 72 74 79 20 6c 69 73 74 20 61 6c 6c e dirty list all
11d48 20 66 6f 75 6c 65 64 20 75 70 20 68 65 72 65 2c fouled up here,
11d49 0a 20 20 20 20 20 20 2a 2a 20 62 75 74 20 74 68 . ** but th
11d4a 61 74 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 74 at does not matt
11d4b 65 72 20 62 65 63 61 75 73 65 20 69 66 20 74 68 er because if th
11d4c 65 20 69 66 20 74 68 65 20 64 69 72 74 79 20 6c e if the dirty l
11d4d 69 73 74 20 64 69 64 0a 20 20 20 20 20 20 2a 2a ist did. **
11d4e 20 67 65 74 20 63 6f 72 72 75 70 74 65 64 2c 20 get corrupted,
11d4f 74 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 then the transac
11d50 74 69 6f 6e 20 77 69 6c 6c 20 72 6f 6c 6c 20 62 tion will roll b
11d51 61 63 6b 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a ack and. **
11d52 20 64 69 73 63 61 72 64 20 74 68 65 20 64 69 72 discard the dir
11d53 74 79 20 6c 69 73 74 2e 20 20 54 68 65 72 65 20 ty list. There
11d54 69 73 20 61 6e 20 61 73 73 65 72 74 20 69 6e 0a is an assert in.
11d55 20 20 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 67 ** pager_g
11d56 65 74 5f 61 6c 6c 5f 64 69 72 74 79 5f 70 61 67 et_all_dirty_pag
11d57 65 73 28 29 20 74 68 61 74 20 76 65 72 69 66 69 es() that verifi
11d58 65 73 20 74 68 61 74 20 6e 6f 20 61 74 74 65 6d es that no attem
11d59 70 74 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 6d pt. ** is m
11d5a 61 64 65 20 74 6f 20 75 73 65 20 61 6e 20 69 6e ade to use an in
11d5b 76 61 6c 69 64 20 64 69 72 74 79 20 6c 69 73 74 valid dirty list
11d5c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
11d5d 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b goto sync_exit;
11d5e 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
11d5f 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c e3PcacheCleanAll
11d60 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
11d61 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 );.. /* Sync
11d62 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
11d63 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 e. */. if( !p
11d64 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 26 26 Pager->noSync &&
11d65 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 !noSync ){.
11d66 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11d67 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c Sync(pPager->fd,
11d68 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
11d69 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ags);. }.
11d6a 49 4f 54 52 41 43 45 28 28 22 44 42 53 59 4e 43 IOTRACE(("DBSYNC
11d6b 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 %p\n", pPager))
11d6c 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 .. pPager->st
11d6d 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59 4e 43 ate = PAGER_SYNC
11d6e 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ED;. }else if(
11d6f 4d 45 4d 44 42 20 26 26 20 6e 54 72 75 6e 63 21 MEMDB && nTrunc!
11d70 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 =0 ){. rc = s
11d71 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 qlite3PagerTrunc
11d72 61 74 65 28 70 50 61 67 65 72 2c 20 6e 54 72 75 ate(pPager, nTru
11d73 6e 63 29 3b 0a 20 20 7d 0a 0a 73 79 6e 63 5f 65 nc);. }..sync_e
11d74 78 69 74 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53 xit:. if( rc==S
11d75 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 QLITE_IOERR_BLOC
11d76 4b 45 44 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 KED ){. /* pa
11d77 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 ger_incr_changec
11d78 6f 75 6e 74 65 72 28 29 20 6d 61 79 20 61 74 74 ounter() may att
11d79 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 61 empt to obtain a
11d7a 6e 20 65 78 63 6c 75 73 69 76 65 0a 20 20 20 20 n exclusive.
11d7b 20 2a 20 6c 6f 63 6b 20 74 6f 20 73 70 69 6c 6c * lock to spill
11d7c 20 74 68 65 20 63 61 63 68 65 20 61 6e 64 20 72 the cache and r
11d7d 65 74 75 72 6e 20 49 4f 45 52 52 5f 42 4c 4f 43 eturn IOERR_BLOC
11d7e 4b 45 44 2e 20 42 75 74 20 73 69 6e 63 65 20 0a KED. But since .
11d7f 20 20 20 20 20 2a 20 74 68 65 72 65 20 69 73 20 * there is
11d80 6e 6f 20 63 68 61 6e 63 65 20 74 68 65 20 63 61 no chance the ca
11d81 63 68 65 20 69 73 20 69 6e 63 6f 6e 73 69 73 74 che is inconsist
11d82 65 6e 74 2c 20 69 74 20 69 73 0a 20 20 20 20 20 ent, it is.
11d83 2a 20 62 65 74 74 65 72 20 74 6f 20 72 65 74 75 * better to retu
11d84 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a rn SQLITE_BUSY..
11d85 20 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d */. rc =
11d86 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
11d87 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
11d88 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 .../*.** Commit
11d89 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 6f 20 74 all changes to t
11d8a 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 he database and
11d8b 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 release the writ
11d8c 65 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 e lock..**.** If
11d8d 20 74 68 65 20 63 6f 6d 6d 69 74 20 66 61 69 6c the commit fail
11d8e 73 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e s for any reason
11d8f 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 74 74 , a rollback att
11d90 65 6d 70 74 20 69 73 20 6d 61 64 65 0a 2a 2a 20 empt is made.**
11d91 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
11d92 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 e is returned.
11d93 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 77 6f If the commit wo
11d94 72 6b 65 64 2c 20 53 51 4c 49 54 45 5f 4f 4b 0a rked, SQLITE_OK.
11d95 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a ** is returned..
11d96 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
11d97 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
11d98 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f erCommitPhaseTwo
11d99 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
11d9a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
11d9b 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 50 TE_OK;.. if( pP
11d9c 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b ager->errCode ){
11d9d 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 . return pPag
11d9e 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d er->errCode;. }
11d9f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
11da0 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 tate<PAGER_RESER
11da1 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 VED ){. retur
11da2 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
11da3 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 }. if( pPager
11da4 2d 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 ->dbModified==0
11da5 26 26 0a 20 20 20 20 20 20 20 20 28 70 50 61 67 &&. (pPag
11da6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 er->journalMode!
11da7 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
11da8 44 45 5f 44 45 4c 45 54 45 20 7c 7c 0a 20 20 20 DE_DELETE ||.
11da9 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 pPager->e
11daa 78 63 6c 75 73 69 76 65 4d 6f 64 65 21 3d 30 29 xclusiveMode!=0)
11dab 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
11dac 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 pPager->dirtyCac
11dad 68 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d he==0 || pPager-
11dae 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 >journalOpen==0
11daf 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
11db0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 50 LITE_OK;. }. P
11db1 41 47 45 52 54 52 41 43 45 32 28 22 43 4f 4d 4d AGERTRACE2("COMM
11db2 49 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 IT %d\n", PAGERI
11db3 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 61 73 D(pPager));. as
11db4 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
11db5 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 ate==PAGER_SYNCE
11db6 44 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 21 70 D || MEMDB || !p
11db7 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 Pager->dirtyCach
11db8 65 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 e );. rc = page
11db9 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
11dba 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 n(pPager, pPager
11dbb 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 ->setMaster);.
11dbc 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 rc = pager_error
11dbd 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 (pPager, rc);.
11dbe 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
11dbf 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c .** Rollback all
11dc0 20 63 68 61 6e 67 65 73 2e 20 20 54 68 65 20 64 changes. The d
11dc1 61 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61 atabase falls ba
11dc2 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 ck to PAGER_SHAR
11dc3 45 44 20 6d 6f 64 65 2e 0a 2a 2a 20 41 6c 6c 20 ED mode..** All
11dc4 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 in-memory cache
11dc5 70 61 67 65 73 20 72 65 76 65 72 74 20 74 6f 20 pages revert to
11dc6 74 68 65 69 72 20 6f 72 69 67 69 6e 61 6c 20 64 their original d
11dc7 61 74 61 20 63 6f 6e 74 65 6e 74 73 2e 0a 2a 2a ata contents..**
11dc8 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 The journal is
11dc9 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 deleted..**.** T
11dca 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 6e his routine cann
11dcb 6f 74 20 66 61 69 6c 20 75 6e 6c 65 73 73 20 73 ot fail unless s
11dcc 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ome other proces
11dcd 73 20 69 73 20 6e 6f 74 20 66 6f 6c 6c 6f 77 69 s is not followi
11dce 6e 67 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 ng.** the correc
11dcf 74 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 74 6f 63 t locking protoc
11dd0 6f 6c 20 6f 72 20 75 6e 6c 65 73 73 20 73 6f 6d ol or unless som
11dd1 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 e other.** proce
11dd2 73 73 20 69 73 20 77 72 69 74 69 6e 67 20 74 72 ss is writing tr
11dd3 61 73 68 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ash into the jou
11dd4 72 6e 61 6c 20 66 69 6c 65 20 28 53 51 4c 49 54 rnal file (SQLIT
11dd5 45 5f 43 4f 52 52 55 50 54 29 20 6f 72 0a 2a 2a E_CORRUPT) or.**
11dd6 20 75 6e 6c 65 73 73 20 61 20 70 72 69 6f 72 20 unless a prior
11dd7 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 malloc() failed
11dd8 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 2e 20 (SQLITE_NOMEM).
11dd9 20 41 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 Appropriate err
11dda 6f 72 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 or.** codes are
11ddb 72 65 74 75 72 6e 65 64 20 66 6f 72 20 61 6c 6c returned for all
11ddc 20 74 68 65 73 65 20 6f 63 63 61 73 69 6f 6e 73 these occasions
11ddd 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a . Otherwise,.**
11dde 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
11ddf 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 turned..*/.SQLIT
11de0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
11de1 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 lite3PagerRollba
11de2 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
11de3 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
11de4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 41 47 45 52 LITE_OK;. PAGER
11de5 54 52 41 43 45 32 28 22 52 4f 4c 4c 42 41 43 4b TRACE2("ROLLBACK
11de6 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
11de7 70 50 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20 pPager));. if(
11de8 21 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 !pPager->dirtyCa
11de9 63 68 65 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e che || !pPager->
11dea 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 journalOpen ){.
11deb 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e rc = pager_en
11dec 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 d_transaction(pP
11ded 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 ager, pPager->se
11dee 74 4d 61 73 74 65 72 29 3b 0a 20 20 7d 65 6c 73 tMaster);. }els
11def 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 e if( pPager->er
11df0 72 43 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d rCode && pPager-
11df1 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 >errCode!=SQLITE
11df2 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 69 66 28 _FULL ){. if(
11df3 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
11df4 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
11df5 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 70 ){. pager_p
11df6 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 layback(pPager,
11df7 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 0);. }. rc
11df8 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f = pPager->errCo
11df9 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 de;. }else{.
11dfa 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
11dfb 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 te==PAGER_RESERV
11dfc 45 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 ED ){. int
11dfd 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rc2;. rc =
11dfe 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 pager_playback(p
11dff 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 Pager, 0);.
11e00 20 72 63 32 20 3d 20 70 61 67 65 72 5f 65 6e 64 rc2 = pager_end
11e01 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 _transaction(pPa
11e02 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 ger, pPager->set
11e03 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 69 Master);. i
11e04 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
11e05 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
11e06 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rc2;. }.
11e07 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
11e08 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 c = pager_playba
11e09 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 ck(pPager, 0);.
11e0a 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 4d }.. if( !M
11e0b 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 70 50 EMDB ){. pP
11e0c 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 ager->dbSizeVali
11e0d 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 d = 0;. }..
11e0e 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 /* If an error
11e0f 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 occurs during a
11e10 20 52 4f 4c 4c 42 41 43 4b 2c 20 77 65 20 63 61 ROLLBACK, we ca
11e11 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 73 n no longer trus
11e12 74 20 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 t the pager.
11e13 2a 2a 20 63 61 63 68 65 2e 20 53 6f 20 63 61 6c ** cache. So cal
11e14 6c 20 70 61 67 65 72 5f 65 72 72 6f 72 28 29 20 l pager_error()
11e15 6f 6e 20 74 68 65 20 77 61 79 20 6f 75 74 20 74 on the way out t
11e16 6f 20 6d 61 6b 65 20 61 6e 79 20 65 72 72 6f 72 o make any error
11e17 20 0a 20 20 20 20 2a 2a 20 70 65 72 73 69 73 74 . ** persist
11e18 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ent.. */.
11e19 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 rc = pager_error
11e1a 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 (pPager, rc);.
11e1b 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
11e1c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 ../*.** Return T
11e1d 52 55 45 20 69 66 20 74 68 65 20 64 61 74 61 62 RUE if the datab
11e1e 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e ase file is open
11e1f 65 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 ed read-only. R
11e20 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 eturn FALSE.** i
11e21 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
11e22 73 20 28 69 6e 20 74 68 65 6f 72 79 29 20 77 72 s (in theory) wr
11e23 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 itable..*/.SQLIT
11e24 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
11e25 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64 lite3PagerIsread
11e26 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70 50 61 67 only(Pager *pPag
11e27 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
11e28 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b 0a ager->readOnly;.
11e29 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
11e2a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 the number of re
11e2b 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 ferences to the
11e2c 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pager..*/.SQLITE
11e2d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
11e2e 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e ite3PagerRefcoun
11e2f 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 t(Pager *pPager)
11e30 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
11e31 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
11e32 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
11e33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
11e34 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
11e35 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
11e36 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 he specified pag
11e37 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
11e38 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
11e39 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e PagerPageRefcoun
11e3a 74 28 44 62 50 61 67 65 20 2a 70 50 61 67 65 29 t(DbPage *pPage)
11e3b 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
11e3c 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 e3PcachePageRefc
11e3d 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a 7d 0a 0a ount(pPage);.}..
11e3e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
11e3f 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f ST./*.** This ro
11e40 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f utine is used fo
11e41 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e r testing and an
11e42 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a alysis only..*/.
11e43 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
11e44 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 nt *sqlite3Pager
11e45 53 74 61 74 73 28 50 61 67 65 72 20 2a 70 50 61 Stats(Pager *pPa
11e46 67 65 72 29 7b 0a 20 20 73 74 61 74 69 63 20 69 ger){. static i
11e47 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61 5b 30 5d nt a[11];. a[0]
11e48 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 = sqlite3Pcache
11e49 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
11e4a 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 31 >pPCache);. a[1
11e4b 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 ] = sqlite3Pcach
11e4c 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 ePagecount(pPage
11e4d 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 r->pPCache);. a
11e4e 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 [2] = sqlite3Pca
11e4f 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 cheGetCachesize(
11e50 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
11e51 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50 61 67 65 ;. a[3] = pPage
11e52 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3f r->dbSizeValid ?
11e53 20 28 69 6e 74 29 20 70 50 61 67 65 72 2d 3e 64 (int) pPager->d
11e54 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20 20 61 5b bSize : -1;. a[
11e55 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 61 4] = pPager->sta
11e56 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20 70 50 61 te;. a[5] = pPa
11e57 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 ger->errCode;.
11e58 61 5b 36 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e a[6] = pPager->n
11e59 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d 20 70 50 Hit;. a[7] = pP
11e5a 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a 20 20 61 ager->nMiss;. a
11e5b 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20 55 73 65 [8] = 0; /* Use
11e5c 64 20 74 6f 20 62 65 20 70 50 61 67 65 72 2d 3e d to be pPager->
11e5d 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b 39 5d 20 nOvfl */. a[9]
11e5e 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 3b = pPager->nRead;
11e5f 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50 61 67 65 . a[10] = pPage
11e60 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20 72 65 74 r->nWrite;. ret
11e61 75 72 6e 20 61 3b 0a 7d 0a 53 51 4c 49 54 45 5f urn a;.}.SQLITE_
11e62 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
11e63 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 te3PagerIsMemdb(
11e64 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11e65 20 20 72 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a return MEMDB;.
11e66 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
11e67 53 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e Set the statemen
11e68 74 20 72 6f 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 t rollback point
11e69 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
11e6a 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20 63 tine should be c
11e6b 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 74 alled with the t
11e6c 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e ransaction journ
11e6d 61 6c 20 61 6c 72 65 61 64 79 0a 2a 2a 20 6f 70 al already.** op
11e6e 65 6e 2e 20 20 41 20 6e 65 77 20 73 74 61 74 65 en. A new state
11e6f 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 ment journal is
11e70 63 72 65 61 74 65 64 20 74 68 61 74 20 63 61 6e created that can
11e71 20 62 65 20 75 73 65 64 20 74 6f 20 72 6f 6c 6c be used to roll
11e72 62 61 63 6b 0a 2a 2a 20 63 68 61 6e 67 65 73 20 back.** changes
11e73 6f 66 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 of a single SQL
11e74 63 6f 6d 6d 61 6e 64 20 77 69 74 68 69 6e 20 61 command within a
11e75 20 6c 61 72 67 65 72 20 74 72 61 6e 73 61 63 74 larger transact
11e76 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ion..*/.static i
11e77 6e 74 20 70 61 67 65 72 53 74 6d 74 42 65 67 69 nt pagerStmtBegi
11e78 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 n(Pager *pPager)
11e79 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 {. int rc;. as
11e7a 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 73 sert( !pPager->s
11e7b 74 6d 74 49 6e 55 73 65 20 29 3b 0a 20 20 61 73 tmtInUse );. as
11e7c 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
11e7d 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 ate>=PAGER_SHARE
11e7e 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 D );. assert( p
11e7f 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
11e80 69 64 20 29 3b 0a 20 20 50 41 47 45 52 54 52 41 id );. PAGERTRA
11e81 43 45 32 28 22 53 54 4d 54 2d 42 45 47 49 4e 20 CE2("STMT-BEGIN
11e82 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 %d\n", PAGERID(p
11e83 50 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20 21 Pager));. if( !
11e84 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11e85 70 65 6e 20 29 7b 0a 20 20 20 20 70 50 61 67 65 pen ){. pPage
11e86 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70 65 6e 20 r->stmtAutoopen
11e87 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
11e88 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
11e89 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
11e8a 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a >journalOpen );.
11e8b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
11e8c 2d 3e 70 49 6e 53 74 6d 74 3d 3d 30 20 29 3b 0a ->pInStmt==0 );.
11e8d 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d pPager->pInStm
11e8e 74 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 t = sqlite3Bitve
11e8f 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e cCreate(pPager->
11e90 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 dbSize);. if( p
11e91 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 3d 3d Pager->pInStmt==
11e92 30 20 29 7b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 0 ){. /* sqli
11e93 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 te3OsLock(pPager
11e94 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 ->fd, SHARED_LOC
11e95 4b 29 3b 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 K); */. retur
11e96 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
11e97 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 }. pPager->st
11e98 6d 74 4a 53 69 7a 65 20 3d 20 70 50 61 67 65 72 mtJSize = pPager
11e99 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 ->journalOff;.
11e9a 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 pPager->stmtSize
11e9b 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a = pPager->dbSiz
11e9c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d e;. pPager->stm
11e9d 74 48 64 72 4f 66 66 20 3d 20 30 3b 0a 20 20 70 tHdrOff = 0;. p
11e9e 50 61 67 65 72 2d 3e 73 74 6d 74 43 6b 73 75 6d Pager->stmtCksum
11e9f 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d = pPager->cksum
11ea0 49 6e 69 74 3b 0a 20 20 69 66 28 20 21 70 50 61 Init;. if( !pPa
11ea1 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 29 7b ger->stmtOpen ){
11ea2 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
11ea3 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
11ea4 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
11ea5 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 MEMORY ){.
11ea6 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
11ea7 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 74 lOpen(pPager->st
11ea8 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a fd);. }else{.
11ea9 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
11eaa 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 e3PagerOpentemp(
11eab 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e pPager, pPager->
11eac 73 74 66 64 2c 20 53 51 4c 49 54 45 5f 4f 50 45 stfd, SQLITE_OPE
11ead 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 N_SUBJOURNAL);.
11eae 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
11eaf 20 20 20 20 20 20 20 67 6f 74 6f 20 73 74 6d 74 goto stmt
11eb0 5f 62 65 67 69 6e 5f 66 61 69 6c 65 64 3b 0a 20 _begin_failed;.
11eb1 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
11eb2 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 pPager->stmtOpe
11eb3 6e 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 n = 1;. pPage
11eb4 72 2d 3e 73 74 6d 74 4e 52 65 63 20 3d 20 30 3b r->stmtNRec = 0;
11eb5 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 . }. pPager->s
11eb6 74 6d 74 49 6e 55 73 65 20 3d 20 31 3b 0a 20 20 tmtInUse = 1;.
11eb7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
11eb8 3b 0a 20 0a 73 74 6d 74 5f 62 65 67 69 6e 5f 66 ;. .stmt_begin_f
11eb9 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 70 50 61 ailed:. if( pPa
11eba 67 65 72 2d 3e 70 49 6e 53 74 6d 74 20 29 7b 0a ger->pInStmt ){.
11ebb 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 sqlite3Bitve
11ebc 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d cDestroy(pPager-
11ebd 3e 70 49 6e 53 74 6d 74 29 3b 0a 20 20 20 20 70 >pInStmt);. p
11ebe 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 20 3d Pager->pInStmt =
11ebf 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
11ec0 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 rc;.}.SQLITE_PR
11ec1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
11ec2 33 50 61 67 65 72 53 74 6d 74 42 65 67 69 6e 28 3PagerStmtBegin(
11ec3 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11ec4 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d int rc;. rc =
11ec5 20 70 61 67 65 72 53 74 6d 74 42 65 67 69 6e 28 pagerStmtBegin(
11ec6 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 pPager);. retur
11ec7 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
11ec8 6f 6d 6d 69 74 20 61 20 73 74 61 74 65 6d 65 6e ommit a statemen
11ec9 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
11eca 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
11ecb 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28 PagerStmtCommit(
11ecc 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11ecd 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
11ece 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20 50 mtInUse ){. P
11ecf 41 47 45 52 54 52 41 43 45 32 28 22 53 54 4d 54 AGERTRACE2("STMT
11ed0 2d 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c 20 50 -COMMIT %d\n", P
11ed1 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b AGERID(pPager));
11ed2 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 . sqlite3Bitv
11ed3 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 ecDestroy(pPager
11ed4 2d 3e 70 49 6e 53 74 6d 74 29 3b 0a 20 20 20 20 ->pInStmt);.
11ed5 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 20 pPager->pInStmt
11ed6 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d = 0;. pPager-
11ed7 3e 73 74 6d 74 4e 52 65 63 20 3d 20 30 3b 0a 20 >stmtNRec = 0;.
11ed8 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 pPager->stmtI
11ed9 6e 55 73 65 20 3d 20 30 3b 0a 20 20 20 20 69 66 nUse = 0;. if
11eda 28 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f ( sqlite3IsMemJo
11edb 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 74 urnal(pPager->st
11edc 66 64 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c fd) ){. sql
11edd 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
11ede 50 61 67 65 72 2d 3e 73 74 66 64 2c 20 30 29 3b Pager->stfd, 0);
11edf 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 50 61 . }. }. pPa
11ee0 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70 65 ger->stmtAutoope
11ee1 6e 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 n = 0;. return
11ee2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
11ee3 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20 73 .** Rollback a s
11ee4 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c tatement..*/.SQL
11ee5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
11ee6 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 sqlite3PagerStmt
11ee7 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a Rollback(Pager *
11ee8 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 pPager){. int r
11ee9 63 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d c;. if( pPager-
11eea 3e 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 >stmtInUse ){.
11eeb 20 20 50 41 47 45 52 54 52 41 43 45 32 28 22 53 PAGERTRACE2("S
11eec 54 4d 54 2d 52 4f 4c 4c 42 41 43 4b 20 25 64 5c TMT-ROLLBACK %d\
11eed 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
11eee 65 72 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 er));. rc = p
11eef 61 67 65 72 5f 73 74 6d 74 5f 70 6c 61 79 62 61 ager_stmt_playba
11ef0 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 ck(pPager);.
11ef1 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 sqlite3PagerStmt
11ef2 43 6f 6d 6d 69 74 28 70 50 61 67 65 72 29 3b 0a Commit(pPager);.
11ef3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
11ef4 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
11ef5 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 . pPager->stmtA
11ef6 75 74 6f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 72 utoopen = 0;. r
11ef7 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
11ef8 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 ** Return the fu
11ef9 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 ll pathname of t
11efa 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11efb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
11efc 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
11efd 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 sqlite3PagerFile
11efe 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 name(Pager *pPag
11eff 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
11f00 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b ager->zFilename;
11f01 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
11f02 20 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 the VFS structu
11f03 72 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 re for the pager
11f04 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
11f05 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
11f06 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 3_vfs *sqlite3Pa
11f07 67 65 72 56 66 73 28 50 61 67 65 72 20 2a 70 50 gerVfs(Pager *pP
11f08 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
11f09 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a pPager->pVfs;.}.
11f0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
11f0b 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f e file handle fo
11f0c 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 r the database f
11f0d 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a ile associated.*
11f0e 2a 20 77 69 74 68 20 74 68 65 20 70 61 67 65 72 * with the pager
11f0f 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20 72 65 . This might re
11f10 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 turn NULL if the
11f11 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 file has.** not
11f12 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 yet been opened
11f13 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
11f14 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ATE sqlite3_file
11f15 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
11f16 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 le(Pager *pPager
11f17 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
11f18 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a er->fd;.}../*.**
11f19 20 52 65 74 75 72 6e 20 74 68 65 20 64 69 72 65 Return the dire
11f1a 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64 61 74 ctory of the dat
11f1b 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 53 abase file..*/.S
11f1c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
11f1d 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
11f1e 33 50 61 67 65 72 44 69 72 6e 61 6d 65 28 50 61 3PagerDirname(Pa
11f1f 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
11f20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a return pPager->z
11f21 44 69 72 65 63 74 6f 72 79 3b 0a 7d 0a 0a 2f 2a Directory;.}../*
11f22 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 .** Return the f
11f23 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 ull pathname of
11f24 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11f25 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
11f26 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
11f27 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 sqlite3PagerJour
11f28 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 nalname(Pager *p
11f29 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
11f2a 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 pPager->zJourna
11f2b 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 l;.}../*.** Retu
11f2c 72 6e 20 74 72 75 65 20 69 66 20 66 73 79 6e 63 rn true if fsync
11f2d 28 29 20 63 61 6c 6c 73 20 61 72 65 20 64 69 73 () calls are dis
11f2e 61 62 6c 65 64 20 66 6f 72 20 74 68 69 73 20 70 abled for this p
11f2f 61 67 65 72 2e 20 20 52 65 74 75 72 6e 20 46 41 ager. Return FA
11f30 4c 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 LSE.** if fsync(
11f31 29 73 20 61 72 65 20 65 78 65 63 75 74 65 64 20 )s are executed
11f32 6e 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c normally..*/.SQL
11f33 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
11f34 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 sqlite3PagerNosy
11f35 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nc(Pager *pPager
11f36 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
11f37 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 er->noSync;.}..#
11f38 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 ifdef SQLITE_HAS
11f39 5f 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 _CODEC./*.** Set
11f3a 20 74 68 65 20 63 6f 64 65 63 20 66 6f 72 20 74 the codec for t
11f3b 68 69 73 20 70 61 67 65 72 0a 2a 2f 0a 53 51 4c his pager.*/.SQL
11f3c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
11f3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
11f3e 43 6f 64 65 63 28 0a 20 20 50 61 67 65 72 20 2a Codec(. Pager *
11f3f 70 50 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a pPager,. void *
11f40 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c (*xCodec)(void*,
11f41 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c void*,Pgno,int),
11f42 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 41 . void *pCodecA
11f43 72 67 0a 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e rg.){. pPager->
11f44 78 43 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b xCodec = xCodec;
11f45 0a 20 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 . pPager->pCode
11f46 63 41 72 67 20 3d 20 70 43 6f 64 65 63 41 72 67 cArg = pCodecArg
11f47 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ;.}.#endif..#ifn
11f48 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
11f49 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a AUTOVACUUM./*.**
11f4a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 Move the page p
11f4b 50 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 Pg to location p
11f4c 67 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e gno in the file.
11f4d 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 .**.** There mus
11f4e 74 20 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 t be no referenc
11f4f 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 es to the page p
11f50 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 reviously locate
11f51 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 d at.** pgno (wh
11f52 69 63 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f ich we call pPgO
11f53 6c 64 29 20 74 68 6f 75 67 68 20 74 68 61 74 20 ld) though that
11f54 70 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 page is allowed
11f55 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 to be.** in cach
11f56 65 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 e. If the page
11f57 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 previously locat
11f58 65 64 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f ed at pgno is no
11f59 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 t already.** in
11f5a 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
11f5b 72 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 rnal, it is not
11f5c 70 75 74 20 74 68 65 72 65 20 62 79 20 62 79 20 put there by by
11f5d 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a this routine..**
11f5e 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 .** References t
11f5f 6f 20 74 68 65 20 70 61 67 65 20 70 50 67 20 72 o the page pPg r
11f60 65 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 emain valid. Upd
11f61 61 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 ating any.** met
11f62 61 2d 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 a-data associate
11f63 64 20 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e d with pPg (i.e.
11f64 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 data stored in
11f65 74 68 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 the nExtra bytes
11f66 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c .** allocated al
11f67 6f 6e 67 20 77 69 74 68 20 74 68 65 20 70 61 67 ong with the pag
11f68 65 29 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e e) is the respon
11f69 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 sibility of the
11f6a 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 caller..**.** A
11f6b 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 transaction must
11f6c 20 62 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 be active when
11f6d 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
11f6e 63 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 called. It used
11f6f 74 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 to be.** require
11f70 64 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 d that a stateme
11f71 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 nt transaction w
11f72 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 as not active, b
11f73 75 74 20 74 68 69 73 20 72 65 73 74 72 69 63 74 ut this restrict
11f74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ion.** has been
11f75 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 removed (CREATE
11f76 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d INDEX needs to m
11f77 6f 76 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 ove a page when
11f78 61 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 a statement.** t
11f79 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
11f7a 74 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 tive)..**.** If
11f7b 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d the fourth argum
11f7c 65 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 ent, isCommit, i
11f7d 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
11f7e 20 74 68 69 73 20 70 61 67 65 20 69 73 20 62 65 this page is be
11f7f 69 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 ing.** moved as
11f80 70 61 72 74 20 6f 66 20 61 20 64 61 74 61 62 61 part of a databa
11f81 73 65 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f se reorganizatio
11f82 6e 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 n just before th
11f83 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a e transaction .*
11f84 2a 20 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 * is being commi
11f85 74 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 tted. In this ca
11f86 73 65 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e se, it is guaran
11f87 74 65 65 64 20 74 68 61 74 20 74 68 65 20 64 61 teed that the da
11f88 74 61 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 tabase page .**
11f89 70 50 67 20 72 65 66 65 72 73 20 74 6f 20 77 69 pPg refers to wi
11f8a 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 ll not be writte
11f8b 6e 20 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69 n to again withi
11f8c 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 n this transacti
11f8d 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
11f8e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
11f8f 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 3PagerMovepage(P
11f90 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 44 62 ager *pPager, Db
11f91 50 61 67 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20 Page *pPg, Pgno
11f92 70 67 6e 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d pgno, int isComm
11f93 69 74 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 it){. PgHdr *pP
11f94 67 4f 6c 64 3b 20 20 2f 2a 20 54 68 65 20 70 61 gOld; /* The pa
11f95 67 65 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 ge being overwri
11f96 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 tten. */. Pgno
11f97 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 30 needSyncPgno = 0
11f98 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 ;.. assert( pPg
11f99 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 0a 20 20 50 ->nRef>0 );.. P
11f9a 41 47 45 52 54 52 41 43 45 35 28 22 4d 4f 56 45 AGERTRACE5("MOVE
11f9b 20 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 65 %d page %d (nee
11f9c 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 dSync=%d) moves
11f9d 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 to %d\n", .
11f9e 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
11f9f 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50 , pPg->pgno, (pP
11fa0 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
11fa1 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70 EED_SYNC)?1:0, p
11fa2 67 6e 6f 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 gno);. IOTRACE(
11fa3 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25 64 5c ("MOVE %p %d %d\
11fa4 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d n", pPager, pPg-
11fa5 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20 >pgno, pgno))..
11fa6 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e 74 65 pager_get_conte
11fa7 6e 74 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 49 nt(pPg);.. /* I
11fa8 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 f the journal ne
11fa9 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 eds to be sync()
11faa 65 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 ed before page p
11fab 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a Pg->pgno can. *
11fac 2a 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c * be written to,
11fad 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f store pPg->pgno
11fae 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 in local variab
11faf 6c 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e le needSyncPgno.
11fb0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 . **. ** If th
11fb1 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 e isCommit flag
11fb2 69 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 is set, there is
11fb3 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 no need to reme
11fb4 6d 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 mber that. ** t
11fb5 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 he journal needs
11fb6 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 to be sync()ed
11fb7 62 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 before database
11fb8 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a page pPg->pgno .
11fb9 20 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 ** can be writ
11fba 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c ten to. The call
11fbb 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 er has already p
11fbc 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 romised not to w
11fbd 72 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f rite to it.. */
11fbe 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 . if( (pPg->fla
11fbf 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
11fc0 4e 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 NC) && !isCommit
11fc1 20 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 ){. needSync
11fc2 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f Pgno = pPg->pgno
11fc3 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 ;. assert( pa
11fc4 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 geInJournal(pPg)
11fc5 20 7c 7c 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d || pgno>pPager-
11fc6 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 3b 0a 20 >origDbSize );.
11fc7 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e assert( pPg->
11fc8 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
11fc9 59 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 Y );. assert(
11fca 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
11fcb 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 c );. }.. /* I
11fcc 66 20 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 f the cache cont
11fcd 61 69 6e 73 20 61 20 70 61 67 65 20 77 69 74 68 ains a page with
11fce 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 70 67 6e page-number pgn
11fcf 6f 2c 20 72 65 6d 6f 76 65 20 69 74 0a 20 20 2a o, remove it. *
11fd0 2a 20 66 72 6f 6d 20 69 74 73 20 68 61 73 68 20 * from its hash
11fd1 63 68 61 69 6e 2e 20 41 6c 73 6f 2c 20 69 66 20 chain. Also, if
11fd2 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 the PgHdr.needSy
11fd3 6e 63 20 77 61 73 20 73 65 74 20 66 6f 72 20 0a nc was set for .
11fd4 20 20 2a 2a 20 70 61 67 65 20 70 67 6e 6f 20 62 ** page pgno b
11fd5 65 66 6f 72 65 20 74 68 65 20 27 6d 6f 76 65 27 efore the 'move'
11fd6 20 6f 70 65 72 61 74 69 6f 6e 2c 20 69 74 20 6e operation, it n
11fd7 65 65 64 73 20 74 6f 20 62 65 20 72 65 74 61 69 eeds to be retai
11fd8 6e 65 64 20 0a 20 20 2a 2a 20 66 6f 72 20 74 68 ned . ** for th
11fd9 65 20 70 61 67 65 20 6d 6f 76 65 64 20 74 68 65 e page moved the
11fda 72 65 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 2d 3e re.. */. pPg->
11fdb 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f flags &= ~PGHDR_
11fdc 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 70 50 67 NEED_SYNC;. pPg
11fdd 4f 6c 64 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b Old = pager_look
11fde 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 up(pPager, pgno)
11fdf 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 67 ;. assert( !pPg
11fe0 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c 64 2d 3e 6e Old || pPgOld->n
11fe1 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 Ref==1 );. if(
11fe2 70 50 67 4f 6c 64 20 29 7b 0a 20 20 20 20 70 50 pPgOld ){. pP
11fe3 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 50 67 g->flags |= (pPg
11fe4 4f 6c 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 Old->flags&PGHDR
11fe5 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 7d _NEED_SYNC);. }
11fe6 0a 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 .. sqlite3Pcach
11fe7 65 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e 6f 29 eMove(pPg, pgno)
11fe8 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 64 20 29 ;. if( pPgOld )
11fe9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 {. sqlite3Pca
11fea 63 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b cheDrop(pPgOld);
11feb 0a 20 20 7d 0a 0a 20 20 6d 61 6b 65 44 69 72 74 . }.. makeDirt
11fec 79 28 70 50 67 29 3b 0a 20 20 70 50 61 67 65 72 y(pPg);. pPager
11fed 2d 3e 64 69 72 74 79 43 61 63 68 65 20 3d 20 31 ->dirtyCache = 1
11fee 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f ;. pPager->dbMo
11fef 64 69 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 69 dified = 1;.. i
11ff0 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 f( needSyncPgno
11ff1 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65 ){. /* If nee
11ff2 64 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e dSyncPgno is non
11ff3 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 -zero, then the
11ff4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65 journal file nee
11ff5 64 73 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a ds to be . **
11ff6 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 sync()ed before
11ff7 20 61 6e 79 20 64 61 74 61 20 69 73 20 77 72 69 any data is wri
11ff8 74 74 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65 tten to database
11ff9 20 66 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53 file page needS
11ffa 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 yncPgno.. **
11ffb 43 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 Currently, no su
11ffc 63 68 20 70 61 67 65 20 65 78 69 73 74 73 20 69 ch page exists i
11ffd 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 n the page-cache
11ffe 20 61 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a and the . **
11fff 20 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20 "is journaled"
12000 62 69 74 76 65 63 20 66 6c 61 67 20 68 61 73 20 bitvec flag has
12001 62 65 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e been set. This n
12002 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64 eeds to be remed
12003 69 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f ied by. ** lo
12004 61 64 69 6e 67 20 74 68 65 20 70 61 67 65 20 69 ading the page i
12005 6e 74 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61 nto the pager-ca
12006 63 68 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20 che and setting
12007 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 the PgHdr.needSy
12008 6e 63 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e nc . ** flag.
12009 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
1200a 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f f the attempt to
1200b 20 6c 6f 61 64 20 74 68 65 20 70 61 67 65 20 69 load the page i
1200c 6e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63 nto the page-cac
1200d 68 65 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20 he fails, (due.
1200e 20 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f ** to a mallo
1200f 63 28 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72 c() or IO failur
12010 65 29 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69 e), clear the bi
12011 74 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72 t in the pInJour
12012 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 nal[]. ** arr
12013 61 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ay. Otherwise, i
12014 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f f the page is lo
12015 61 64 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e aded and written
12016 20 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a again in. **
12017 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
12018 6e 2c 20 69 74 20 6d 61 79 20 62 65 20 77 72 69 n, it may be wri
12019 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
1201a 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 base file before
1201b 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79 . ** it is sy
1201c 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f nced into the jo
1201d 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 urnal file. This
1201e 20 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64 way, it may end
1201f 20 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 up in. ** th
12020 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 e journal file t
12021 77 69 63 65 2c 20 62 75 74 20 74 68 61 74 20 69 wice, but that i
12022 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e s not a problem.
12023 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
12024 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 he sqlite3PagerG
12025 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 61 et() call may ca
12026 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 use the journal
12027 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 to sync. So make
12028 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74 68 65 . ** sure the
12029 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 Pager.needSync
1202a 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 6f 2e flag is set too.
1202b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 . */. int
1202c 72 63 3b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 rc;. PgHdr *p
1202d 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72 PgHdr;. asser
1202e 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 t( pPager->needS
1202f 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ync );. rc =
12030 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
12031 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 pPager, needSync
12032 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a Pgno, &pPgHdr);.
12033 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
12034 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 TE_OK ){. i
12035 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f f( pPager->pInJo
12036 75 72 6e 61 6c 20 26 26 20 6e 65 65 64 53 79 6e urnal && needSyn
12037 63 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 6f cPgno<=pPager->o
12038 72 69 67 44 62 53 69 7a 65 20 29 7b 0a 20 20 20 rigDbSize ){.
12039 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
1203a 65 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e ecClear(pPager->
1203b 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 pInJournal, need
1203c 53 79 6e 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20 SyncPgno);.
1203d 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 }. return
1203e 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 rc;. }. pP
1203f 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d ager->needSync =
12040 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 1;. assert(
12041 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d pPager->noSync==
12042 30 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 0 && !MEMDB );.
12043 20 20 20 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 pPgHdr->flags
12044 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 |= PGHDR_NEED_S
12045 59 4e 43 3b 0a 20 20 20 20 6d 61 6b 65 44 69 72 YNC;. makeDir
12046 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 ty(pPgHdr);.
12047 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
12048 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a f(pPgHdr);. }..
12049 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1204a 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a OK;.}.#endif../*
1204b 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
1204c 6e 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61 nter to the data
1204d 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
1204e 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 ed page..*/.SQLI
1204f 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12050 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 *sqlite3PagerGet
12051 44 61 74 61 28 44 62 50 61 67 65 20 2a 70 50 67 Data(DbPage *pPg
12052 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 ){. assert( pPg
12053 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d ->nRef>0 || pPg-
12054 3e 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 >pPager->memDb )
12055 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e ;. return pPg->
12056 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pData;.}../*.**
12057 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
12058 20 74 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45 to the Pager.nE
12059 78 74 72 61 20 62 79 74 65 73 20 6f 66 20 22 65 xtra bytes of "e
1205a 78 74 72 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 xtra" space .**
1205b 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 allocated along
1205c 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 with the specifi
1205d 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 ed page..*/.SQLI
1205e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1205f 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 *sqlite3PagerGet
12060 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 70 50 Extra(DbPage *pP
12061 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 g){. Pager *pPa
12062 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 ger = pPg->pPage
12063 72 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 r;. return (pPa
12064 67 65 72 3f 70 50 67 2d 3e 70 45 78 74 72 61 3a ger?pPg->pExtra:
12065 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 0);.}../*.** Get
12066 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 /set the locking
12067 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 -mode for this p
12068 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 ager. Parameter
12069 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e eMode must be on
1206a 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f e.** of PAGER_LO
1206b 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c CKINGMODE_QUERY,
1206c 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f PAGER_LOCKINGMO
1206d 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a DE_NORMAL or .**
1206e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f PAGER_LOCKINGMO
1206f 44 45 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 DE_EXCLUSIVE. If
12070 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 the parameter i
12071 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 s not _QUERY, th
12072 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e en.** the lockin
12073 67 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f g-mode is set to
12074 20 74 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 the value speci
12075 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 fied..**.** The
12076 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 returned value i
12077 73 20 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c s either PAGER_L
12078 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 OCKINGMODE_NORMA
12079 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f L or.** PAGER_LO
1207a 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 CKINGMODE_EXCLUS
1207b 49 56 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 IVE, indicating
1207c 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 the current (pos
1207d 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a sibly updated).*
1207e 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a * locking-mode..
1207f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12080 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
12081 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 erLockingMode(Pa
12082 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
12083 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 eMode){. asser
12084 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f t( eMode==PAGER_
12085 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 LOCKINGMODE_QUER
12086 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c Y. ||
12087 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f eMode==PAGER_LO
12088 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c CKINGMODE_NORMAL
12089 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
1208a 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 eMode==PAGER_LOC
1208b 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 KINGMODE_EXCLUSI
1208c 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 VE );. assert(
1208d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 PAGER_LOCKINGMOD
1208e 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 E_QUERY<0 );. a
1208f 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 ssert( PAGER_LOC
12090 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e KINGMODE_NORMAL>
12091 3d 30 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b =0 && PAGER_LOCK
12092 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
12093 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d E>=0 );. if( eM
12094 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 ode>=0 && !pPage
12095 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 r->tempFile ){.
12096 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 pPager->exclu
12097 73 69 76 65 4d 6f 64 65 20 3d 20 65 4d 6f 64 65 siveMode = eMode
12098 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
12099 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 63 6c int)pPager->excl
1209a 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a usiveMode;.}../*
1209b 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 .** Get/set the
1209c 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f 72 journal-mode for
1209d 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 72 this pager. Par
1209e 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 ameter eMode mus
1209f 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a t be one of:.**.
120a0 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
120a1 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 2a 2a NALMODE_QUERY.**
120a2 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 PAGER_JOURNA
120a3 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 LMODE_DELETE.**
120a4 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c PAGER_JOURNAL
120a5 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 2a 2a MODE_TRUNCATE.**
120a6 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 PAGER_JOURNA
120a7 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 2a 2a LMODE_PERSIST.**
120a8 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 PAGER_JOURNA
120a9 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 0a 2a 2a 20 LMODE_OFF.**.**
120aa 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 If the parameter
120ab 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 is not _QUERY,
120ac 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c then the journal
120ad 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 -mode is set to
120ae 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 65 the.** value spe
120af 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 cified..**.** Th
120b0 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 e returned indic
120b1 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ate the current
120b2 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 (possibly update
120b3 64 29 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 6d 6f d).** journal-mo
120b4 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 de..*/.SQLITE_PR
120b5 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
120b6 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 3PagerJournalMod
120b7 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c e(Pager *pPager,
120b8 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 69 int eMode){. i
120b9 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 f( !MEMDB ){.
120ba 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d assert( eMode==
120bb 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
120bc 45 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20 E_QUERY.
120bd 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d || eMode==
120be 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
120bf 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 20 E_DELETE.
120c0 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d || eMode=
120c1 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
120c2 44 45 5f 54 52 55 4e 43 41 54 45 0a 20 20 20 20 DE_TRUNCATE.
120c3 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
120c4 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
120c5 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 LMODE_PERSIST.
120c6 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
120c7 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
120c8 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 NALMODE_OFF .
120c9 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d || eM
120ca 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
120cb 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b ALMODE_MEMORY );
120cc 0a 20 20 20 20 61 73 73 65 72 74 28 20 50 41 47 . assert( PAG
120cd 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 ER_JOURNALMODE_Q
120ce 55 45 52 59 3c 30 20 29 3b 0a 20 20 20 20 69 66 UERY<0 );. if
120cf 28 20 65 4d 6f 64 65 3e 3d 30 20 29 7b 0a 20 20 ( eMode>=0 ){.
120d0 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
120d1 6e 61 6c 4d 6f 64 65 20 3d 20 65 4d 6f 64 65 3b nalMode = eMode;
120d2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
120d3 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d assert( eMode=
120d4 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
120d5 44 45 5f 51 55 45 52 59 20 29 3b 0a 20 20 20 20 DE_QUERY );.
120d6 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 }. }. return (
120d7 69 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 int)pPager->jour
120d8 6e 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a nalMode;.}../*.*
120d9 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 73 69 * Get/set the si
120da 7a 65 2d 6c 69 6d 69 74 20 75 73 65 64 20 66 6f ze-limit used fo
120db 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 r persistent jou
120dc 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 rnal files..*/.S
120dd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 QLITE_PRIVATE i6
120de 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 4 sqlite3PagerJo
120df 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 urnalSizeLimit(P
120e0 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 36 ager *pPager, i6
120e1 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 28 4 iLimit){. if(
120e2 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a 20 iLimit>=-1 ){.
120e3 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
120e4 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 4c alSizeLimit = iL
120e5 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 imit;. }. retu
120e6 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e rn pPager->journ
120e7 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a alSizeLimit;.}..
120e8 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
120e9 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 20 2a 2f 0a _OMIT_DISKIO */.
120ea 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
120eb 20 45 6e 64 20 6f 66 20 70 61 67 65 72 2e 63 20 End of pager.c
120ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
120ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
120ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
120ef 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
120f0 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74 6d 75 Begin file btmu
120f1 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tex.c **********
120f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
120f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
120f4 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
120f5 73 74 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 27.**.** The
120f6 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
120f7 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
120f8 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
120f9 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
120fa 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
120fb 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
120fc 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
120fd 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
120fe 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
120ff 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
12100 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
12101 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
12102 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
12103 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
12104 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
12105 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
12106 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
12107 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12108 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12109 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1210a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1210b 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ****.**.** $Id:
1210c 62 74 6d 75 74 65 78 2e 63 2c 76 20 31 2e 31 32 btmutex.c,v 1.12
1210d 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39 3a 31 2008/11/17 19:1
1210e 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 8:55 danielk1977
1210f 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 Exp $.**.** Thi
12110 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
12111 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 code used to imp
12112 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 6f lement mutexes o
12113 6e 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 2e n Btree objects.
12114 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72 65 .** This code re
12115 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 ally belongs in
12116 62 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62 74 btree.c. But bt
12117 72 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e 67 ree.c is getting
12118 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 too.** big and
12119 77 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61 6b we want to break
1211a 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 it down some.
1211b 54 68 69 73 20 70 61 63 6b 61 67 65 64 20 73 65 This packaged se
1211c 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 emed like.** a g
1211d 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f ood breakout..*/
1211e 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1211f 20 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49 6e Include btreeIn
12120 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c t.h in the middl
12121 65 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a e of btmutex.c *
12122 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
12123 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
12124 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 Begin file btre
12125 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
12126 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12127 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
12128 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 ./*.** 2004 Apri
12129 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 l 6.**.** The au
1212a 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
1212b 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
1212c 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
1212d 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
1212e 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
1212f 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
12130 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
12131 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
12132 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
12133 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
12134 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
12135 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
12136 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
12137 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
12138 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
12139 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
1213a 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
1213b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1213c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1213d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1213e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1213f 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 **.** $Id: btree
12140 49 6e 74 2e 68 2c 76 20 31 2e 33 36 20 32 30 30 Int.h,v 1.36 200
12141 38 2f 31 31 2f 31 39 20 31 30 3a 32 32 3a 33 33 8/11/19 10:22:33
12142 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
12143 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 $.**.** This fi
12144 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 le implements a
12145 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 external (disk-b
12146 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20 75 ased) database u
12147 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 sing BTrees..**
12148 46 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 For a detailed d
12149 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72 iscussion of BTr
1214a 65 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a ees, refer to.**
1214b 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45 .** Donald E
1214c 2e 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54 . Knuth, THE ART
1214d 20 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f OF COMPUTER PRO
1214e 47 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 GRAMMING, Volume
1214f 20 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74 3:.** "Sort
12150 69 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e ing And Searchin
12151 67 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38 g", pages 473-48
12152 30 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65 0. Addison-Wesle
12153 79 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68 y.** Publish
12154 69 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61 ing Company, Rea
12155 64 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65 ding, Massachuse
12156 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 tts..**.** The b
12157 61 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61 asic idea is tha
12158 74 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 t each page of t
12159 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 he file contains
1215a 20 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65 N database.** e
1215b 6e 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70 ntries and N+1 p
1215c 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61 ointers to subpa
1215d 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d ges..**.** ---
1215e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1215f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
12160 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
12161 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
12162 20 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b | Ptr(0) | K
12163 65 79 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c ey(0) | Ptr(1) |
12164 20 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 Key(1) | ... |
12165 4b 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e Key(N-1) | Ptr(N
12166 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d ) |.** -------
12167 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
12168 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
12169 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1216a 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 ---------.**.**
1216b 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 All of the keys
1216c 6f 6e 20 74 68 65 20 70 61 67 65 20 74 68 61 74 on the page that
1216d 20 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74 Ptr(0) points t
1216e 6f 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65 o have values le
1216f 73 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30 ss.** than Key(0
12170 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b ). All of the k
12171 65 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28 eys on page Ptr(
12172 31 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 1) and its subpa
12173 67 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 ges have.** valu
12174 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 es greater than
12175 4b 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20 Key(0) and less
12176 74 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c than Key(1). Al
12177 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a l of the keys.**
12178 20 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69 on Ptr(N) and i
12179 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65 ts subpages have
1217a 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 values greater
1217b 74 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20 than Key(N-1).
1217c 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e And.** so forth.
1217d 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61 .**.** Finding a
1217e 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20 particular key
1217f 72 65 71 75 69 72 65 73 20 72 65 61 64 69 6e 67 requires reading
12180 20 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73 O(log(M)) pages
12181 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69 from the .** di
12182 73 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74 68 sk where M is th
12183 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
12184 69 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e ies in the tree.
12185 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 .**.** In this i
12186 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 mplementation, a
12187 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e single file can
12188 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 hold one or mor
12189 65 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42 e separate .** B
1218a 54 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72 Trees. Each BTr
1218b 65 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 ee is identified
1218c 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66 by the index of
1218d 20 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 its root page.
1218e 20 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 The.** key and
1218f 64 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74 data for any ent
12190 72 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 ry are combined
12191 74 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79 to form the "pay
12192 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78 load". A.** fix
12193 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 ed amount of pay
12194 6c 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72 72 load can be carr
12195 69 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20 ied directly on
12196 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
12197 70 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61 page. If the pa
12198 79 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20 yload is larger
12199 74 68 61 6e 20 74 68 65 20 70 72 65 73 65 74 20 than the preset
1219a 61 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70 amount then surp
1219b 6c 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65 lus.** bytes are
1219c 20 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66 stored on overf
1219d 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 low pages. The
1219e 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65 payload for an e
1219f 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ntry.** and the
121a0 70 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65 preceding pointe
121a1 72 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 r are combined t
121a2 6f 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e o form a "Cell".
121a3 20 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20 Each .** page
121a4 68 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64 has a small head
121a5 65 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e er which contain
121a6 73 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69 s the Ptr(N) poi
121a7 6e 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a nter and other.*
121a8 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75 * information su
121a9 63 68 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f ch as the size o
121aa 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a f key and data..
121ab 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54 **.** FORMAT DET
121ac 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 AILS.**.** The f
121ad 69 6c 65 20 69 73 20 64 69 76 69 64 65 64 20 69 ile is divided i
121ae 6e 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20 nto pages. The
121af 66 69 72 73 74 20 70 61 67 65 20 69 73 20 63 61 first page is ca
121b0 6c 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20 lled page 1,.**
121b1 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61 the second is pa
121b2 67 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72 ge 2, and so for
121b3 74 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62 th. A page numb
121b4 65 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63 er of zero indic
121b5 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 ates.** "no such
121b6 20 70 61 67 65 22 2e 20 20 54 68 65 20 70 61 67 page". The pag
121b7 65 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e e size can be an
121b8 79 74 68 69 6e 67 20 62 65 74 77 65 65 6e 20 35 ything between 5
121b9 31 32 20 61 6e 64 20 36 35 35 33 36 2e 0a 2a 2a 12 and 65536..**
121ba 20 45 61 63 68 20 70 61 67 65 20 63 61 6e 20 62 Each page can b
121bb 65 20 65 69 74 68 65 72 20 61 20 62 74 72 65 65 e either a btree
121bc 20 70 61 67 65 2c 20 61 20 66 72 65 65 6c 69 73 page, a freelis
121bd 74 20 70 61 67 65 20 6f 72 20 61 6e 20 6f 76 65 t page or an ove
121be 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2e 0a 2a rflow.** page..*
121bf 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 *.** The first p
121c0 61 67 65 20 69 73 20 61 6c 77 61 79 73 20 61 20 age is always a
121c1 62 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 btree page. The
121c2 20 66 69 72 73 74 20 31 30 30 20 62 79 74 65 73 first 100 bytes
121c3 20 6f 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a of the first.**
121c4 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 20 61 20 page contain a
121c5 73 70 65 63 69 61 6c 20 68 65 61 64 65 72 20 28 special header (
121c6 74 68 65 20 22 66 69 6c 65 20 68 65 61 64 65 72 the "file header
121c7 22 29 20 74 68 61 74 20 64 65 73 63 72 69 62 65 ") that describe
121c8 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 s the file..** T
121c9 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 he format of the
121ca 20 66 69 6c 65 20 68 65 61 64 65 72 20 69 73 20 file header is
121cb 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
121cc 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a * OFFSET SIZ
121cd 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e E DESCRIPTION
121ce 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 .** 0
121cf 31 36 20 20 20 20 20 48 65 61 64 65 72 20 73 74 16 Header st
121d0 72 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 66 6f ring: "SQLite fo
121d1 72 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a 20 20 rmat 3\000".**
121d2 20 20 20 31 36 20 20 20 20 20 20 20 32 20 20 20 16 2
121d3 20 20 50 61 67 65 20 73 69 7a 65 20 69 6e 20 62 Page size in b
121d4 79 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20 20 31 ytes. .** 1
121d5 38 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 8 1 Fi
121d6 6c 65 20 66 6f 72 6d 61 74 20 77 72 69 74 65 20 le format write
121d7 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 31 version.** 1
121d8 39 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 9 1 Fi
121d9 6c 65 20 66 6f 72 6d 61 74 20 72 65 61 64 20 76 le format read v
121da 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 30 ersion.** 20
121db 20 20 20 20 20 20 20 31 20 20 20 20 20 42 79 74 1 Byt
121dc 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 es of unused spa
121dd 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 ce at the end of
121de 20 65 61 63 68 20 70 61 67 65 0a 2a 2a 20 20 20 each page.**
121df 20 20 32 31 20 20 20 20 20 20 20 31 20 20 20 20 21 1
121e0 20 4d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 Max embedded pa
121e1 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a yload fraction.*
121e2 2a 20 20 20 20 20 32 32 20 20 20 20 20 20 20 31 * 22 1
121e3 20 20 20 20 20 4d 69 6e 20 65 6d 62 65 64 64 65 Min embedde
121e4 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 d payload fracti
121e5 6f 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20 20 20 on.** 23
121e6 20 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 65 61 1 Min lea
121e7 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 f payload fracti
121e8 6f 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20 20 20 on.** 24
121e9 20 20 20 34 20 20 20 20 20 46 69 6c 65 20 63 68 4 File ch
121ea 61 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 ange counter.**
121eb 20 20 20 20 32 38 20 20 20 20 20 20 20 34 20 20 28 4
121ec 20 20 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 Reserved for
121ed 66 75 74 75 72 65 20 75 73 65 0a 2a 2a 20 20 20 future use.**
121ee 20 20 33 32 20 20 20 20 20 20 20 34 20 20 20 20 32 4
121ef 20 46 69 72 73 74 20 66 72 65 65 6c 69 73 74 20 First freelist
121f0 70 61 67 65 0a 2a 2a 20 20 20 20 20 33 36 20 20 page.** 36
121f1 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65 4 Numbe
121f2 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 70 61 r of freelist pa
121f3 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 0a ges in the file.
121f4 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 36 ** 40 6
121f5 30 20 20 20 20 20 31 35 20 34 2d 62 79 74 65 20 0 15 4-byte
121f6 6d 65 74 61 20 76 61 6c 75 65 73 20 70 61 73 73 meta values pass
121f7 65 64 20 74 6f 20 68 69 67 68 65 72 20 6c 61 79 ed to higher lay
121f8 65 72 73 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 ers.**.** All of
121f9 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c the integer val
121fa 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69 ues are big-endi
121fb 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69 an (most signifi
121fc 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29 cant byte first)
121fd 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 ..**.** The file
121fe 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
121ff 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 is incremented w
12200 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
12201 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54 is changed.** T
12202 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f his counter allo
12203 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ws other process
12204 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 es to know when
12205 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 the file has cha
12206 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73 nged.** and thus
12207 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20 when they need
12208 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63 to flush their c
12209 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ache..**.** The
1220a 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 max embedded pay
1220b 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 load fraction is
1220c 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 the amount of t
1220d 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a he total usable.
1220e 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61 ** space in a pa
1220f 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 ge that can be c
12210 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e onsumed by a sin
12211 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61 gle cell for sta
12212 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20 ndard.** B-tree
12213 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74 (non-LEAFDATA) t
12214 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20 ables. A value
12215 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30 of 255 means 100
12216 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a %. The default.
12217 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74 ** is to limit t
12218 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 he maximum cell
12219 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20 size so that at
1221a 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69 least 4 cells wi
1221b 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 ll fit.** on one
1221c 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65 page. Thus the
1221d 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62 default max emb
1221e 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 edded payload fr
1221f 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a action is 64..**
12220 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f .** If the paylo
12221 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73 ad for a cell is
12222 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
12223 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68 max payload, th
12224 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c en extra.** payl
12225 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74 oad is spilled t
12226 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
12227 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 . Once an overf
12228 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f low page is allo
12229 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e cated,.** as man
1222a 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69 y bytes as possi
1222b 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e ble are moved in
1222c 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 to the overflow
1222d 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65 pages without le
1222e 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c tting.** the cel
1222f 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f l size drop belo
12230 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 w the min embedd
12231 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 ed payload fract
12232 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d ion..**.** The m
12233 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 in leaf payload
12234 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65 fraction is like
12235 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 the min embedde
12236 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 d payload fracti
12237 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 on.** except tha
12238 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20 t it applies to
12239 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20 leaf nodes in a
1223a 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20 LEAFDATA tree.
1223b 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 The maximum.** p
1223c 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 ayload fraction
1223d 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74 for a LEAFDATA t
1223e 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30 ree is always 10
1223f 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20 0% (or 255) and
12240 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66 it.** not specif
12241 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 ied in the heade
12242 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74 r..**.** Each bt
12243 72 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76 ree pages is div
12244 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20 ided into three
12245 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68 sections: The h
12246 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65 eader, the.** ce
12247 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll pointer array
12248 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 , and the cell c
12249 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61 ontent area. Pa
1224a 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20 ge 1 also has a
1224b 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65 100-byte.** file
1224c 20 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 header that occ
1224d 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70 urs before the p
1224e 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a age header..**.*
1224f 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
12250 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
12251 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20 | file header
12252 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73 | 100 bytes
12253 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a . Page 1 only..
12254 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d ** |-------
12255 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 ---------|.**
12256 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72 | page header
12257 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20 | 8 bytes
12258 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20 for leaves. 12
12259 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69 bytes for interi
1225a 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20 or nodes.**
1225b 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |--------------
1225c 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 --|.** | ce
1225d 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20 ll pointer |
1225e 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20 | 2 bytes per
1225f 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72 cell. Sorted or
12260 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 der..** | a
12261 72 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20 rray |
12262 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77 | Grows downw
12263 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20 ard.** |
12264 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 |
12265 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d v.** |----
12266 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a ------------|.**
12267 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 | unalloca
12268 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 ted |.**
12269 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20 | space
1226a 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d |.** |---
1226b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 -------------|
1226c 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64 ^ Grows upward
1226d 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c s.** | cell
1226e 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c content | |
1226f 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65 Arbitrary orde
12270 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77 r interspersed w
12271 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a ith freeblocks..
12272 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20 ** | area
12273 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 | |
12274 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66 and free space f
12275 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 ragments..**
12276 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-------------
12277 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ---|.**.** The p
12278 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b age headers look
12279 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a s like this:.**.
1227a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 ** OFFSET SI
1227b 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49 ZE DESCRIPTI
1227c 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 ON.** 0
1227d 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e 1 Flags.
1227e 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 1: intkey, 2: z
1227f 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66 erodata, 4: leaf
12280 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a data, 8: leaf.**
12281 20 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20 1 2
12282 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74 byte offset
12283 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 to the first fr
12284 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 eeblock.**
12285 33 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e 3 2 n
12286 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
12287 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 n this page.**
12288 20 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20 5 2
12289 20 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 first byte of
1228a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
1228b 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37 t area.** 7
1228c 20 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75 1 nu
1228d 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 mber of fragment
1228e 65 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a ed free bytes.**
1228f 20 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20 8 4
12290 20 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64 Right child
12291 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c (the Ptr(N) val
12292 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e ue). Omitted on
12293 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 leaves..**.** T
12294 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20 he flags define
12295 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 the format of th
12296 69 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 is btree page.
12297 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65 The leaf flag me
12298 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73 ans that.** this
12299 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69 page has no chi
1229a 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f ldren. The zero
1229b 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20 data flag means
1229c 74 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63 that this page c
1229d 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b arries.** only k
1229e 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e eys and no data.
1229f 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61 The intkey fla
122a0 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 g means that the
122a1 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67 65 key is a intege
122a2 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74 r.** which is st
122a3 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 ored in the key
122a4 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68 size entry of th
122a5 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61 e cell header ra
122a6 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 ther than in.**
122a7 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 the payload area
122a8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c ..**.** The cell
122a9 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62 pointer array b
122aa 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72 egins on the fir
122ab 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68 st byte after th
122ac 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a e page header..*
122ad 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 * The cell point
122ae 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e er array contain
122af 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32 s zero or more 2
122b0 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68 -byte numbers wh
122b1 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65 ich are.** offse
122b2 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 ts from the begi
122b3 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 nning of the pag
122b4 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f e to the cell co
122b5 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c ntent in the cel
122b6 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 l.** content are
122b7 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 a. The cell poi
122b8 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73 nters occur in s
122b9 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 orted order. Th
122ba 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73 e system strives
122bb 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65 .** to keep free
122bc 20 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65 space after the
122bd 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 last cell point
122be 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63 er so that new c
122bf 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65 ells can.** be e
122c0 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68 asily added with
122c1 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65 out having to de
122c2 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 fragment the pag
122c3 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f e..**.** Cell co
122c4 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 ntent is stored
122c5 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 at the very end
122c6 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 of the page and
122c7 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65 grows toward the
122c8 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 .** beginning of
122c9 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
122ca 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69 Unused space wi
122cb 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f thin the cell co
122cc 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f ntent area is co
122cd 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c llected into a l
122ce 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a inked list of.**
122cf 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61 freeblocks. Ea
122d0 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 ch freeblock is
122d1 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 at least 4 bytes
122d2 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62 in size. The b
122d3 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f yte offset.** to
122d4 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 the first freeb
122d5 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e lock is given in
122d6 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72 the header. Fr
122d7 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69 eeblocks occur i
122d8 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20 n.** increasing
122d9 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20 order. Because
122da 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74 a freeblock must
122db 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 be at least 4 b
122dc 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a ytes in size,.**
122dd 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20 any group of 3
122de 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20 or fewer unused
122df 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c bytes in the cel
122e0 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63 l content area c
122e1 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f annot.** exist o
122e2 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 n the freeblock
122e3 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20 chain. A group
122e4 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72 of 3 or fewer fr
122e5 65 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c ee bytes is call
122e6 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74 ed.** a fragment
122e7 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d . The total num
122e8 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
122e9 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73 all fragments is
122ea 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e recorded..** in
122eb 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
122ec 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a at offset 7..**
122ed 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 .** SIZE D
122ee 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 ESCRIPTION.**
122ef 20 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66 2 Byte of
122f0 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74 fset of the next
122f1 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 freeblock.**
122f2 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69 2 Bytes i
122f3 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b n this freeblock
122f4 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65 .**.** Cells are
122f5 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e of variable len
122f6 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20 gth. Cells are
122f7 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65 stored in the ce
122f8 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 ll content area
122f9 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 at.** the end of
122fa 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e the page. Poin
122fb 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c ters to the cell
122fc 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c s are in the cel
122fd 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a l pointer array.
122fe 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 ** that immediat
122ff 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 ely follows the
12300 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65 page header. Ce
12301 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 lls is not neces
12302 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 sarily.** contig
12303 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72 uous or in order
12304 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74 , but cell point
12305 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f ers are contiguo
12306 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e us and in order.
12307 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 .**.** Cell cont
12308 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66 ent makes use of
12309 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
1230a 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61 integers. A va
1230b 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 riable.** length
1230c 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f integer is 1 to
1230d 20 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74 9 bytes where t
1230e 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 he lower 7 bits
1230f 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65 of each .** byte
12310 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20 are used. The
12311 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 integer consists
12312 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68 of all bytes th
12313 61 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65 at have bit 8 se
12314 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72 t and.** the fir
12315 73 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74 st byte with bit
12316 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d 8 clear. The m
12317 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 ost significant
12318 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65 byte of the inte
12319 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66 ger.** appears f
1231a 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c irst. A variabl
1231b 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
1231c 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65 may not be more
1231d 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f than 9 bytes lo
1231e 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63 ng..** As a spec
1231f 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20 ial case, all 8
12320 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68 bytes of the 9th
12321 20 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61 byte are used a
12322 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a s data. This.**
12323 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74 allows a 64-bit
12324 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65 integer to be e
12325 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65 ncoded in 9 byte
12326 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30 s..**.** 0x00
12327 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12328 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
12329 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20 x00000000.**
1232a 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20 0x7f
1232b 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 become
1232c 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a s 0x0000007f.**
1232d 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20 0x81 0x00
1232e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 be
1232f 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38 comes 0x0000008
12330 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30 0.** 0x82 0x0
12331 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
12332 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 becomes 0x000
12333 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30 00100.** 0x80
12334 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 0x7f
12335 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
12336 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 x0000007f.**
12337 30 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30 0x8a 0x91 0xd1 0
12338 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65 xac 0x78 become
12339 73 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a s 0x12345678.**
1233a 20 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78 0x81 0x81 0x
1233b 38 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65 81 0x81 0x01 be
1233c 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38 comes 0x1020408
1233d 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 1.**.** Variable
1233e 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 length integers
1233f 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f are used for ro
12340 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64 wids and to hold
12341 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
12342 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61 * bytes of key a
12343 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72 nd data in a btr
12344 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 ee cell..**.** T
12345 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 he content of a
12346 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 cell looks like
12347 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 this:.**.** S
12348 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 IZE DESCRIPTI
12349 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 ON.** 4
1234a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
1234b 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20 the left child.
1234c 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20 Omitted if leaf
1234d 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 flag is set..**
1234e 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 var Numbe
1234f 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 r of bytes of da
12350 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74 ta. Omitted if t
12351 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 he zerodata flag
12352 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 is set..**
12353 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 var Number of
12354 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f bytes of key. O
12355 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66 r the key itself
12356 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 if intkey flag
12357 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20 is set..**
12358 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a * Payload.**
12359 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73 4 Firs
1235a 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76 t page of the ov
1235b 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f erflow chain. O
1235c 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65 mitted if no ove
1235d 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 rflow.**.** Over
1235e 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20 flow pages form
1235f 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 a linked list.
12360 45 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74 Each page except
12361 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d the last is com
12362 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 pletely.** fille
12363 64 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67 d with data (pag
12364 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29 esize - 4 bytes)
12365 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65 . The last page
12366 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74 can have as lit
12367 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65 tle.** as 1 byte
12368 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 of data..**.**
12369 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
1236a 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 IPTION.** 4
1236b 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 Page number
1236c 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f of next overflo
1236d 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a w page.** *
1236e 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 Data.**.**
1236f 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63 Freelist pages c
12370 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79 ome in two subty
12371 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73 pes: trunk pages
12372 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e and leaf pages.
12373 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65 The.** file he
12374 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 ader points to t
12375 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 he first in a li
12376 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75 nked list of tru
12377 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74 nk page. Each t
12378 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69 runk.** page poi
12379 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20 nts to multiple
1237a 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 leaf pages. The
1237b 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65 content of a le
1237c 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e af page is.** un
1237d 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72 specified. A tr
1237e 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c unk page looks l
1237f 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
12380 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
12381 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 IPTION.** 4
12382 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 Page number
12383 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70 of next trunk p
12384 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 age.** 4
12385 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 Number of leaf
12386 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69 pointers on thi
12387 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a s page.** *
12388 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 zero or mor
12389 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 e pages numbers
1238a 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 2f 2a of leaves.*/../*
1238b 20 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 Round up a numb
1238c 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c er to the next l
1238d 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f arger multiple o
1238e 66 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 f 8. This is us
1238f 65 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 ed.** to force 8
12390 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 -byte alignment
12391 6f 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 on 64-bit archit
12392 65 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 ectures..*/.#def
12393 69 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 ine ROUND8(x)
12394 28 28 78 2b 37 29 26 7e 37 29 0a 0a 0a 2f 2a 20 ((x+7)&~7).../*
12395 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 The following va
12396 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d lue is the maxim
12397 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 73 um cell size ass
12398 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d 20 uming a maximum
12399 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 76 page.** size giv
1239a 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 66 e above..*/.#def
1239b 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 ine MX_CELL_SIZE
1239c 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 61 67 (pBt) (pBt->pag
1239d 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 65 eSize-8)../* The
1239e 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1239f 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69 of cells on a si
123a0 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65 ngle page of the
123a1 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 database. This
123a2 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69 .** assumes a mi
123a3 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 nimum cell size
123a4 6f 66 20 36 20 62 79 74 65 73 20 20 28 34 20 62 of 6 bytes (4 b
123a5 79 74 65 73 20 66 6f 72 20 74 68 65 20 63 65 6c ytes for the cel
123a6 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 73 l itself.** plus
123a7 20 32 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 2 bytes for the
123a8 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 63 65 index to the ce
123a9 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 ll in the page h
123aa 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a 2a 2a eader). Such.**
123ab 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 6c small cells wil
123ac 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74 20 74 l be rare, but t
123ad 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65 hey are possible
123ae 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f ..*/.#define MX_
123af 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d CELL(pBt) ((pBt-
123b0 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 0a >pageSize-8)/6).
123b1 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c ./* Forward decl
123b2 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65 arations */.type
123b3 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61 def struct MemPa
123b4 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65 ge MemPage;.type
123b5 64 65 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63 def struct BtLoc
123b6 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a k BtLock;../*.**
123b7 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 69 63 This is a magic
123b8 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 70 string that app
123b9 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 69 ears at the begi
123ba 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a nning of every.*
123bb 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 * SQLite databas
123bc 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 64 e in order to id
123bd 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c 65 20 entify the file
123be 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 62 61 as a real databa
123bf 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 61 se..**.** You ca
123c0 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 76 61 n change this va
123c1 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 lue at compile-t
123c2 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 69 6e ime by specifyin
123c3 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f g a.** -DSQLITE_
123c4 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e 2e FILE_HEADER="...
123c5 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 " on the compile
123c6 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 20 r command-line.
123c7 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 6d The.** header m
123c8 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 31 ust be exactly 1
123c9 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 69 6e 6 bytes includin
123ca 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 g the zero-termi
123cb 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 20 nator so.** the
123cc 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 73 68 string itself sh
123cd 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 72 61 ould be 15 chara
123ce 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 20 cters long. If
123cf 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68 you change.** th
123d0 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e 20 79 e header, then y
123d1 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 our custom libra
123d2 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 ry will not be a
123d3 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a 20 ble to read .**
123d4 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 72 61 databases genera
123d5 74 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 ted by the stand
123d6 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 68 ard tools and th
123d7 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 e standard tools
123d8 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 .** will not be
123d9 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 61 74 able to read dat
123da 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20 62 abases created b
123db 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 y your custom li
123dc 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 brary..*/.#ifnde
123dd 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 f SQLITE_FILE_HE
123de 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 37 38 ADER /* 12345678
123df 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 64 9 123456 */.# d
123e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c efine SQLITE_FIL
123e1 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 74 65 E_HEADER "SQLite
123e2 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 69 format 3".#endi
123e3 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 79 f../*.** Page ty
123e4 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f 52 pe flags. An OR
123e5 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f ed combination o
123e6 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 61 70 f these flags ap
123e7 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 66 pear as the.** f
123e8 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f 6e 2d irst byte of on-
123e9 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 65 76 disk image of ev
123ea 65 72 79 20 42 54 72 65 65 20 70 61 67 65 2e 0a ery BTree page..
123eb 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 49 */.#define PTF_I
123ec 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a 23 64 NTKEY 0x01.#d
123ed 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 41 efine PTF_ZERODA
123ee 54 41 20 20 30 78 30 32 0a 23 64 65 66 69 6e 65 TA 0x02.#define
123ef 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 20 30 PTF_LEAFDATA 0
123f0 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54 46 5f x04.#define PTF_
123f1 4c 45 41 46 20 20 20 20 20 20 30 78 30 38 0a 0a LEAF 0x08..
123f2 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 61 /*.** As each pa
123f3 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ge of the file i
123f4 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 s loaded into me
123f5 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 mory, an instanc
123f6 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
123f7 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 ng.** structure
123f8 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20 is appended and
123f9 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a initialized to z
123fa 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72 75 63 ero. This struc
123fb 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 69 ture stores.** i
123fc 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
123fd 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 69 the page that i
123fe 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 74 s decoded from t
123ff 68 65 20 72 61 77 20 66 69 6c 65 20 70 61 67 65 he raw file page
12400 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72 ..**.** The pPar
12401 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 ent field points
12402 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72 back to the par
12403 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 ent page. This
12404 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a 20 allows us to.**
12405 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65 walk up the BTre
12406 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20 e from any leaf
12407 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61 to the root. Ca
12408 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e re must be taken
12409 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74 to.** unref() t
1240a 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 70 he parent page p
1240b 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73 ointer when this
1240c 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 page is no long
1240d 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a er referenced..*
1240e 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 72 75 * The pageDestru
1240f 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 68 ctor() routine h
12410 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 6f 72 andles that chor
12411 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 e..**.** Access
12412 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 to all fields of
12413 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
12414 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 is controlled by
12415 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73 74 the mutex.** st
12416 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e ored in MemPage.
12417 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 pBt->mutex..*/.s
12418 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 7b 0a truct MemPage {.
12419 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 20 20 u8 isInit;
1241a 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
1241b 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 69 f previously ini
1241c 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20 42 tialized. MUST B
1241d 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 75 38 E FIRST! */. u8
1241e 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 nOverflow;
1241f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
12420 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f overflow cell bo
12421 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 dies in aCell[]
12422 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b 20 */. u8 intKey;
12423 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
12424 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 e if intkey flag
12425 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 is set */. u8
12426 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 leaf;
12427 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 61 /* True if lea
12428 66 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f f flag is set */
12429 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b 20 20 . u8 hasData;
1242a 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1242b 69 66 20 74 68 69 73 20 70 61 67 65 20 73 74 6f if this page sto
1242c 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 res data */. u8
1242d 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20 hdrOffset;
1242e 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20 70 61 /* 100 for pa
1242f 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72 77 69 ge 1. 0 otherwi
12430 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c 64 se */. u8 child
12431 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 PtrSize; /*
12432 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20 34 0 if leaf==1. 4
12433 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a 20 if leaf==0 */.
12434 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 u16 maxLocal;
12435 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 /* Copy of
12436 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f 63 BtShared.maxLoc
12437 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d al or BtShared.m
12438 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 axLeaf */. u16
12439 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 minLocal;
1243a 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 /* Copy of BtSh
1243b 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72 ared.minLocal or
1243c 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 61 BtShared.minLea
1243d 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c 4f f */. u16 cellO
1243e 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 49 ffset; /* I
1243f 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 6f 66 ndex in aData of
12440 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e first cell poin
12441 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 ter */. u16 nFr
12442 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ee; /*
12443 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 Number of free
12444 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67 bytes on the pag
12445 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c 6c e */. u16 nCell
12446 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
12447 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
12448 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c 6f 63 n this page, loc
12449 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a 20 al and ovfl */.
1244a 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 20 u16 maskPage;
1244b 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f /* Mask fo
1244c 72 20 70 61 67 65 20 6f 66 66 73 65 74 20 2a 2f r page offset */
1244d 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 . struct _OvflC
1244e 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c 73 ell { /* Cells
1244f 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 66 that will not f
12450 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a 2f it on aData[] */
12451 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 . u8 *pCell;
12452 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
12453 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f 64 79 ters to the body
12454 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 of the overflow
12455 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 36 cell */. u16
12456 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 idx;
12457 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20 /* Insert this
12458 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 2d cell before idx-
12459 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20 th non-overflow
1245a 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 66 cell */. } aOvf
1245b 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 64 l[5];. BtShared
1245c 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f 2a 20 *pBt; /*
1245d 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53 68 61 Pointer to BtSha
1245e 72 65 64 20 74 68 61 74 20 74 68 69 73 20 70 61 red that this pa
1245f 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f ge is part of */
12460 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 . u8 *aData;
12461 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
12462 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 65 er to disk image
12463 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 74 of the page dat
12464 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 a */. DbPage *p
12465 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 DbPage; /* P
12466 61 67 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65 ager page handle
12467 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b */. Pgno pgno;
12468 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
12469 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 ge number for th
1246a 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f is page */.};../
1246b 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d 6f *.** The in-memo
1246c 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 64 69 ry image of a di
1246d 73 6b 20 70 61 67 65 20 68 61 73 20 74 68 65 20 sk page has the
1246e 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d auxiliary inform
1246f 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a 2a ation appended.*
12470 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 45 * to the end. E
12471 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74 68 65 XTRA_SIZE is the
12472 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
12473 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 of space needed
12474 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 74 to hold.** that
12475 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 extra informati
12476 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 on..*/.#define E
12477 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f 66 XTRA_SIZE sizeof
12478 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 20 41 20 (MemPage)../* A
12479 42 74 72 65 65 20 68 61 6e 64 6c 65 0a 2a 2a 0a Btree handle.**.
1247a 2a 2a 20 41 20 64 61 74 61 62 61 73 65 20 63 6f ** A database co
1247b 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e nnection contain
1247c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1247d 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a n instance of.**
1247e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 66 6f 72 this object for
1247f 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20 every database
12480 66 69 6c 65 20 74 68 61 74 20 69 74 20 68 61 73 file that it has
12481 20 6f 70 65 6e 2e 20 20 54 68 69 73 20 73 74 72 open. This str
12482 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6f 70 61 ucture.** is opa
12483 71 75 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 que to the datab
12484 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 ase connection.
12485 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
12486 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a nnection cannot.
12487 2a 2a 20 73 65 65 20 74 68 65 20 69 6e 74 65 72 ** see the inter
12488 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72 nals of this str
12489 75 63 74 75 72 65 20 61 6e 64 20 6f 6e 6c 79 20 ucture and only
1248a 64 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 deals with point
1248b 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 73 ers to.** this s
1248c 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
1248d 46 6f 72 20 73 6f 6d 65 20 64 61 74 61 62 61 73 For some databas
1248e 65 20 66 69 6c 65 73 2c 20 74 68 65 20 73 61 6d e files, the sam
1248f 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 e underlying dat
12490 61 62 61 73 65 20 63 61 63 68 65 20 6d 69 67 68 abase cache migh
12491 74 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 t be .** shared
12492 62 65 74 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65 between multiple
12493 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49 connections. I
12494 6e 20 74 68 61 74 20 63 61 73 65 2c 20 65 61 63 n that case, eac
12495 68 20 63 6f 6e 74 65 63 74 69 6f 6e 0a 2a 2a 20 h contection.**
12496 68 61 73 20 69 74 20 6f 77 6e 20 70 6f 69 6e 74 has it own point
12497 65 72 20 74 6f 20 74 68 69 73 20 6f 62 6a 65 63 er to this objec
12498 74 2e 20 20 42 75 74 20 65 61 63 68 20 69 6e 73 t. But each ins
12499 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
1249a 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 ject.** points t
1249b 6f 20 74 68 65 20 73 61 6d 65 20 42 74 53 68 61 o the same BtSha
1249c 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 red object. The
1249d 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20 database cache
1249e 61 6e 64 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d and the.** schem
1249f 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
124a0 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 h the database f
124a1 69 6c 65 20 61 72 65 20 61 6c 6c 20 63 6f 6e 74 ile are all cont
124a2 61 69 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 ained within.**
124a3 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a the BtShared obj
124a4 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 ect..**.** All f
124a5 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 ields in this st
124a6 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 ructure are acce
124a7 73 73 65 64 20 75 6e 64 65 72 20 73 71 6c 69 74 ssed under sqlit
124a8 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 e3.mutex..** The
124a9 20 70 42 74 20 70 6f 69 6e 74 65 72 20 69 74 73 pBt pointer its
124aa 65 6c 66 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 elf may not be c
124ab 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74 68 65 hanged while the
124ac 72 65 20 65 78 69 73 74 73 20 63 75 72 73 6f 72 re exists cursor
124ad 73 20 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 66 s .** in the ref
124ae 65 72 65 6e 63 65 64 20 42 74 53 68 61 72 65 64 erenced BtShared
124af 20 74 68 61 74 20 70 6f 69 6e 74 20 62 61 63 6b that point back
124b0 20 74 6f 20 74 68 69 73 20 42 74 72 65 65 20 73 to this Btree s
124b1 69 6e 63 65 20 74 68 6f 73 65 0a 2a 2a 20 63 75 ince those.** cu
124b2 72 73 6f 72 73 20 68 61 76 65 20 74 6f 20 64 6f rsors have to do
124b3 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 69 73 go through this
124b4 20 42 74 72 65 65 20 74 6f 20 66 69 6e 64 20 74 Btree to find t
124b5 68 65 69 72 20 42 74 53 68 61 72 65 64 20 61 6e heir BtShared an
124b6 64 0a 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e 20 d.** they often
124b7 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 68 6f do so without ho
124b8 6c 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75 lding sqlite3.mu
124b9 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 tex..*/.struct B
124ba 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 tree {. sqlite3
124bb 20 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 *db; /* T
124bc 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
124bd 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 74 ection holding t
124be 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 his btree */. B
124bf 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 tShared *pBt;
124c0 20 20 2f 2a 20 53 68 61 72 61 62 6c 65 20 63 6f /* Sharable co
124c1 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 62 74 ntent of this bt
124c2 72 65 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 ree */. u8 inTr
124c3 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ans; /* T
124c4 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53 RANS_NONE, TRANS
124c5 5f 52 45 41 44 20 6f 72 20 54 52 41 4e 53 5f 57 _READ or TRANS_W
124c6 52 49 54 45 20 2a 2f 0a 20 20 75 38 20 73 68 61 RITE */. u8 sha
124c7 72 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 rable; /*
124c8 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20 73 True if we can s
124c9 68 61 72 65 20 70 42 74 20 77 69 74 68 20 61 6e hare pBt with an
124ca 6f 74 68 65 72 20 64 62 20 2a 2f 0a 20 20 75 38 other db */. u8
124cb 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 locked;
124cc 20 2f 2a 20 54 72 75 65 20 69 66 20 64 62 20 63 /* True if db c
124cd 75 72 72 65 6e 74 6c 79 20 68 61 73 20 70 42 74 urrently has pBt
124ce 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 locked */. int
124cf 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20 wantToLock;
124d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 /* Number of nes
124d1 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ted calls to sql
124d2 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 29 ite3BtreeEnter()
124d3 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4e 65 */. Btree *pNe
124d4 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 xt; /* List
124d5 20 6f 66 20 6f 74 68 65 72 20 73 68 61 72 61 62 of other sharab
124d6 6c 65 20 42 74 72 65 65 73 20 66 72 6f 6d 20 74 le Btrees from t
124d7 68 65 20 73 61 6d 65 20 64 62 20 2a 2f 0a 20 20 he same db */.
124d8 42 74 72 65 65 20 2a 70 50 72 65 76 3b 20 20 20 Btree *pPrev;
124d9 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f 69 6e 74 /* Back point
124da 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6c er of the same l
124db 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ist */.};../*.**
124dc 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20 6d Btree.inTrans m
124dd 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20 74 ay take one of t
124de 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c he following val
124df 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ues..**.** If th
124e0 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 65 78 e shared-data ex
124e1 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62 6c tension is enabl
124e2 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65 ed, there may be
124e3 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73 0a multiple users.
124e4 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65 20 ** of the Btree
124e5 73 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d 6f structure. At mo
124e6 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 st one of these
124e7 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74 65 may open a write
124e8 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a transaction,.**
124e9 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72 20 but any number
124ea 6d 61 79 20 68 61 76 65 20 61 63 74 69 76 65 20 may have active
124eb 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e read transaction
124ec 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 s..*/.#define TR
124ed 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 66 ANS_NONE 0.#def
124ee 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20 20 ine TRANS_READ
124ef 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 1.#define TRANS_
124f0 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 WRITE 2../*.** A
124f1 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
124f2 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 is object repres
124f3 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64 61 ents a single da
124f4 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 tabase file..**
124f5 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 .** A single dat
124f6 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 62 abase file can b
124f7 65 20 69 6e 20 75 73 65 20 61 73 20 74 68 65 20 e in use as the
124f8 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77 6f same time by two
124f9 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 .** or more data
124fa 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
124fb 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d . When two or m
124fc 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ore connections
124fd 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20 74 are.** sharing t
124fe 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
124ff 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e 6e file, each conn
12500 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f 77 ection has it ow
12501 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74 72 n.** private Btr
12502 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 ee object for th
12503 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68 20 e file and each
12504 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65 73 20 of those Btrees
12505 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 69 points.** to thi
12506 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20 6f s one BtShared o
12507 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65 64 bject. BtShared
12508 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75 6d .nRef is the num
12509 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63 ber of.** connec
1250a 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 tions currently
1250b 73 68 61 72 69 6e 67 20 74 68 69 73 20 64 61 74 sharing this dat
1250c 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
1250d 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73 * Fields in this
1250e 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61 structure are a
1250f 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 ccessed under th
12510 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 e BtShared.mutex
12511 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65 70 .** mutex, excep
12512 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20 70 t for nRef and p
12513 4e 65 78 74 20 77 68 69 63 68 20 61 72 65 20 61 Next which are a
12514 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 ccessed under th
12515 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c 49 e.** global SQLI
12516 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
12517 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 54 MASTER mutex. T
12518 68 65 20 70 50 61 67 65 72 20 66 69 65 6c 64 0a he pPager field.
12519 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f ** may not be mo
1251a 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20 69 dified once it i
1251b 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74 20 s initially set
1251c 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e as long as nRef>
1251d 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65 6d 0..** The pSchem
1251e 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20 73 a field may be s
1251f 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42 74 et once under Bt
12520 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e 64 Shared.mutex and
12521 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20 69 .** thereafter i
12522 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20 6c s unchanged as l
12523 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a ong as nRef>0..*
12524 2f 0a 73 74 72 75 63 74 20 42 74 53 68 61 72 65 /.struct BtShare
12525 64 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 d {. Pager *pPa
12526 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ger; /* T
12527 68 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f he page cache */
12528 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
12529 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
1252a 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1252b 63 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 currently using
1252c 74 68 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 this Btree */.
1252d 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f BtCursor *pCurso
1252e 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 r; /* A list
1252f 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 of all open curs
12530 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 ors */. MemPage
12531 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f *pPage1; /
12532 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 * First page of
12533 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
12534 20 20 75 38 20 69 6e 53 74 6d 74 3b 20 20 20 20 u8 inStmt;
12535 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
12536 69 66 20 77 65 20 61 72 65 20 69 6e 20 61 20 73 if we are in a s
12537 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e tatement subtran
12538 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 saction */. u8
12539 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 readOnly;
1253a 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
1253b 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c e underlying fil
1253c 65 20 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f e is readonly */
1253d 0a 20 20 75 38 20 70 61 67 65 53 69 7a 65 46 69 . u8 pageSizeFi
1253e 78 65 64 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 xed; /* True
1253f 20 69 66 20 74 68 65 20 70 61 67 65 20 73 69 7a if the page siz
12540 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 e can no longer
12541 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 69 be changed */.#i
12542 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
12543 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 75 T_AUTOVACUUM. u
12544 38 20 61 75 74 6f 56 61 63 75 75 6d 3b 20 20 20 8 autoVacuum;
12545 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
12546 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 auto-vacuum is e
12547 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 38 20 69 nabled */. u8 i
12548 6e 63 72 56 61 63 75 75 6d 3b 20 20 20 20 20 20 ncrVacuum;
12549 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 63 /* True if inc
1254a 72 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 r-vacuum is enab
1254b 6c 65 64 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 54 led */. Pgno nT
1254c 72 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 2f runc; /
1254d 2a 20 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 * Non-zero if th
1254e 65 20 64 62 20 77 69 6c 6c 20 62 65 20 74 72 75 e db will be tru
1254f 6e 63 61 74 65 64 20 28 69 6e 63 72 20 76 61 63 ncated (incr vac
12550 75 75 6d 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 uum) */.#endif.
12551 20 75 31 36 20 70 61 67 65 53 69 7a 65 3b 20 20 u16 pageSize;
12552 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 /* Total
12553 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
12554 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 75 on a page */. u
12555 31 36 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 16 usableSize;
12556 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
12557 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f f usable bytes o
12558 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 n each page */.
12559 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 int maxLocal;
1255a 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
1255b 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 m local payload
1255c 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 in non-LEAFDATA
1255d 74 61 62 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 20 tables */. int
1255e 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 minLocal;
1255f 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 /* Minimum loc
12560 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f al payload in no
12561 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 n-LEAFDATA table
12562 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 78 4c 65 s */. int maxLe
12563 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 af; /*
12564 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 Maximum local pa
12565 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 yload in a LEAFD
12566 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 ATA table */. i
12567 6e 74 20 6d 69 6e 4c 65 61 66 3b 20 20 20 20 20 nt minLeaf;
12568 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 /* Minimum
12569 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e local payload in
1256a 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c a LEAFDATA tabl
1256b 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e e */. u8 inTran
1256c 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 saction; /*
1256d 54 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 Transaction stat
1256e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 72 61 6e e */. int nTran
1256f 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 saction; /*
12570 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 74 Number of open t
12571 72 61 6e 73 61 63 74 69 6f 6e 73 20 28 72 65 61 ransactions (rea
12572 64 20 2b 20 77 72 69 74 65 29 20 2a 2f 0a 20 20 d + write) */.
12573 76 6f 69 64 20 2a 70 53 63 68 65 6d 61 3b 20 20 void *pSchema;
12574 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
12575 20 74 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 to space alloca
12576 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 42 74 ted by sqlite3Bt
12577 72 65 65 53 63 68 65 6d 61 28 29 20 2a 2f 0a 20 reeSchema() */.
12578 20 76 6f 69 64 20 28 2a 78 46 72 65 65 53 63 68 void (*xFreeSch
12579 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a ema)(void*); /*
1257a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 Destructor for
1257b 42 74 53 68 61 72 65 64 2e 70 53 63 68 65 6d 61 BtShared.pSchema
1257c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
1257d 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f 2a 20 4e tex *mutex; /* N
1257e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 on-recursive mut
1257f 65 78 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 ex required to a
12580 63 63 65 73 73 20 74 68 69 73 20 73 74 72 75 63 ccess this struc
12581 74 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c t */.#ifndef SQL
12582 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
12583 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52 65 66 CACHE. int nRef
12584 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
12585 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 Number of refer
12586 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 73 74 ences to this st
12587 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 53 ructure */. BtS
12588 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20 20 20 hared *pNext;
12589 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 61 20 /* Next on a
1258a 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62 6c 65 list of sharable
1258b 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 BtShared struct
1258c 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 s */. BtLock *p
1258d 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 Lock; /*
1258e 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20 68 65 List of locks he
1258f 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65 ld on this share
12590 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 20 2a d-btree struct *
12591 2f 0a 20 20 42 74 72 65 65 20 2a 70 45 78 63 6c /. Btree *pExcl
12592 75 73 69 76 65 3b 20 20 20 20 2f 2a 20 42 74 72 usive; /* Btr
12593 65 65 20 77 69 74 68 20 61 6e 20 45 58 43 4c 55 ee with an EXCLU
12594 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 SIVE lock on the
12595 20 77 68 6f 6c 65 20 64 62 20 2a 2f 0a 23 65 6e whole db */.#en
12596 64 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70 dif. u8 *pTmpSp
12597 61 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 ace; /* B
12598 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 tShared.pageSize
12599 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
1259a 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d for tmp use */.}
1259b 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 ;../*.** An inst
1259c 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
1259d 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
1259e 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 is used to hold
1259f 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 information.** a
125a0 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68 bout a cell. Th
125a1 65 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29 e parseCellPtr()
125a2 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20 function fills
125a3 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 in this structur
125a4 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e e.** based on in
125a5 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63 formation extrac
125a6 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64 t from the raw d
125a7 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70 isk page..*/.typ
125a8 65 64 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c edef struct Cell
125a9 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73 Info CellInfo;.s
125aa 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b truct CellInfo {
125ab 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 . u8 *pCell;
125ac 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
125ad 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c the start of cel
125ae 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 l content */. i
125af 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 64 nKey; /*
125b0 20 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 The key for INT
125b1 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e KEY tables, or n
125b2 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
125b3 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e n key */. u32 n
125b4 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d Data; /* Num
125b5 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
125b6 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50 data */. u32 nP
125b7 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61 ayload; /* Tota
125b8 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c l amount of payl
125b9 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65 oad */. u16 nHe
125ba 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20 ader; /* Size
125bb 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 of the cell cont
125bc 65 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62 79 ent header in by
125bd 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f tes */. u16 nLo
125be 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e cal; /* Amoun
125bf 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c t of payload hel
125c0 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75 d locally */. u
125c1 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a 16 iOverflow; /*
125c2 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66 Offset to overf
125c3 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e low page number.
125c4 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65 Zero if no ove
125c5 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e rflow */. u16 n
125c6 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a Size; /* Siz
125c7 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f e of the cell co
125c8 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69 ntent on the mai
125c9 6e 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f n b-tree page */
125ca 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d .};../*.** Maxim
125cb 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53 um depth of an S
125cc 51 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 72 QLite B-Tree str
125cd 75 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72 ucture. Any B-Tr
125ce 65 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a ee deeper than.*
125cf 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64 * this will be d
125d0 65 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e eclared corrupt.
125d1 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 63 This value is c
125d2 61 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 20 alculated based
125d3 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 on a.** maximum
125d4 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66 database size of
125d5 20 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69 2^31 pages a mi
125d6 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20 nimum fanout of
125d7 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d 2 for a.** root-
125d8 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61 node and 3 for a
125d9 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61 ll other interna
125da 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 l nodes..**.** I
125db 66 20 61 20 74 72 65 65 20 74 68 61 74 20 61 70 f a tree that ap
125dc 70 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c pears to be tall
125dd 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 73 20 er than this is
125de 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20 encountered, it
125df 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 is.** assumed th
125e0 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 at the database
125e1 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23 is corrupt..*/.#
125e2 64 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f define BTCURSOR_
125e3 4d 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a MAX_DEPTH 20../*
125e4 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 .** A cursor is
125e5 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 a pointer to a p
125e6 61 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20 articular entry
125e7 77 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 75 within a particu
125e8 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69 lar.** b-tree wi
125e9 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 thin a database
125ea 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 file..**.** The
125eb 65 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 66 entry is identif
125ec 69 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61 ied by its MemPa
125ed 67 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 ge and the index
125ee 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 in.** MemPage.a
125ef 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e Cell[] of the en
125f0 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 try..**.** When
125f1 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 a single databas
125f2 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65 e file can share
125f3 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61 d by two more da
125f4 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
125f5 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f ns,.** but curso
125f6 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61 rs cannot be sha
125f7 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f red. Each curso
125f8 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 r is associated
125f9 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63 with a.** partic
125fa 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f ular database co
125fb 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66 nnection identif
125fc 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74 ied BtCursor.pBt
125fd 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69 ree.db..**.** Fi
125fe 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 elds in this str
125ff 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 ucture are acces
12600 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 sed under the Bt
12601 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 Shared.mutex.**
12602 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70 found at self->p
12603 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73 Bt->mutex. .*/.s
12604 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b truct BtCursor {
12605 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 . Btree *pBtree
12606 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
12607 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69 The Btree to whi
12608 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 ch this cursor b
12609 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68 elongs */. BtSh
1260a 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 ared *pBt;
1260b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53 /* The BtS
1260c 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f hared this curso
1260d 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20 r points to */.
1260e 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 BtCursor *pNext
1260f 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f , *pPrev; /* Fo
12610 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 rms a linked lis
12611 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 t of all cursors
12612 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 */. struct Key
12613 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
12614 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73 /* Argument pass
12615 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e ed to comparison
12616 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 function */. P
12617 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 gno pgnoRoot;
12618 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
12619 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 root page of thi
1261a 73 20 74 72 65 65 20 2a 2f 0a 20 20 43 65 6c 6c s tree */. Cell
1261b 49 6e 66 6f 20 69 6e 66 6f 3b 20 20 20 20 20 20 Info info;
1261c 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 73 65 /* A parse
1261d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 77 65 20 of the cell we
1261e 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 are pointing at
1261f 2a 2f 0a 20 20 75 38 20 77 72 46 6c 61 67 3b 20 */. u8 wrFlag;
12620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12621 2a 20 54 72 75 65 20 69 66 20 77 72 69 74 61 62 * True if writab
12622 6c 65 20 2a 2f 0a 20 20 75 38 20 61 74 4c 61 73 le */. u8 atLas
12623 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
12624 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e /* Cursor poin
12625 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 ting to the last
12626 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 38 20 76 entry */. u8 v
12627 61 6c 69 64 4e 4b 65 79 3b 20 20 20 20 20 20 20 alidNKey;
12628 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
12629 20 69 6e 66 6f 2e 6e 4b 65 79 20 69 73 20 76 61 info.nKey is va
1262a 6c 69 64 20 2a 2f 0a 20 20 75 38 20 65 53 74 61 lid */. u8 eSta
1262b 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 te;
1262c 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 /* One of the
1262d 20 43 55 52 53 4f 52 5f 58 58 58 20 63 6f 6e 73 CURSOR_XXX cons
1262e 74 61 6e 74 73 20 28 73 65 65 20 62 65 6c 6f 77 tants (see below
1262f 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 ) */. void *pKe
12630 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 y; /* Saved
12631 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 63 75 key that was cu
12632 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f 77 rsor's last know
12633 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 n position */.
12634 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 20 i64 nKey;
12635 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 /* Size of pKey
12636 2c 20 6f 72 20 6c 61 73 74 20 69 6e 74 65 67 65 , or last intege
12637 72 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 73 r key */. int s
12638 6b 69 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 28 kip; /* (
12639 73 6b 69 70 3c 30 29 20 2d 3e 20 50 72 65 76 28 skip<0) -> Prev(
1263a 29 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 28 73 ) is a no-op. (s
1263b 6b 69 70 3e 30 29 20 2d 3e 20 4e 65 78 74 28 29 kip>0) -> Next()
1263c 20 69 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 is */.#ifndef S
1263d 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
1263e 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e 63 72 62 LOB. u8 isIncrb
1263f 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20 20 20 20 lobHandle;
12640 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
12641 63 75 72 73 6f 72 20 69 73 20 61 6e 20 69 6e 63 cursor is an inc
12642 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20 2a 2f 0a r. io handle */.
12643 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66 6c 6f Pgno *aOverflo
12644 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 w; /* C
12645 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c 6f 77 ache of overflow
12646 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 73 20 page locations
12647 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 */.#endif.#ifnde
12648 66 20 4e 44 45 42 55 47 0a 20 20 75 38 20 70 61 f NDEBUG. u8 pa
12649 67 65 73 53 68 75 66 66 6c 65 64 3b 20 20 20 20 gesShuffled;
1264a 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1264b 42 74 72 65 65 20 70 61 67 65 73 20 61 72 65 20 Btree pages are
1264c 72 65 61 72 72 61 6e 67 65 64 20 62 79 20 62 61 rearranged by ba
1264d 6c 61 6e 63 65 28 29 2a 2f 0a 23 65 6e 64 69 66 lance()*/.#endif
1264e 0a 20 20 69 31 36 20 69 50 61 67 65 3b 20 20 20 . i16 iPage;
1264f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12650 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
12651 78 20 6f 66 20 63 75 72 72 65 6e 74 20 70 61 67 x of current pag
12652 65 20 69 6e 20 61 70 50 61 67 65 20 2a 2f 0a 20 e in apPage */.
12653 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 61 67 65 MemPage *apPage
12654 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 [BTCURSOR_MAX_DE
12655 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67 65 73 20 PTH]; /* Pages
12656 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 63 75 72 from root to cur
12657 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 75 rent page */. u
12658 31 36 20 61 69 49 64 78 5b 42 54 43 55 52 53 4f 16 aiIdx[BTCURSO
12659 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 20 R_MAX_DEPTH];
1265a 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
1265b 69 6e 64 65 78 20 69 6e 20 61 70 50 61 67 65 5b index in apPage[
1265c 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 i] */.};../*.**
1265d 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c 75 65 73 Potential values
1265e 20 66 6f 72 20 42 74 43 75 72 73 6f 72 2e 65 53 for BtCursor.eS
1265f 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 tate..**.** CURS
12660 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 OR_VALID:.** C
12661 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 ursor points to
12662 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 67 a valid entry. g
12663 65 74 50 61 79 6c 6f 61 64 28 29 20 65 74 63 2e etPayload() etc.
12664 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 2e 0a may be called..
12665 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 **.** CURSOR_INV
12666 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f ALID:.** Curso
12667 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f 69 6e 74 r does not point
12668 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 to a valid entr
12669 79 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 y. This can happ
1266a 65 6e 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 en (for example)
1266b 20 0a 2a 2a 20 20 20 62 65 63 61 75 73 65 20 74 .** because t
1266c 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 he table is empt
1266d 79 20 6f 72 20 62 65 63 61 75 73 65 20 42 74 72 y or because Btr
1266e 65 65 43 75 72 73 6f 72 46 69 72 73 74 28 29 20 eeCursorFirst()
1266f 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 has not been.**
12670 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 called..**.**
12671 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
12672 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 74 61 62 EK:.** The tab
12673 6c 65 20 74 68 61 74 20 74 68 69 73 20 63 75 72 le that this cur
12674 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f sor was opened o
12675 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 73 2c 20 n still exists,
12676 62 75 74 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a but has been .**
12677 20 20 20 6d 6f 64 69 66 69 65 64 20 73 69 6e 63 modified sinc
12678 65 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 e the cursor was
12679 20 6c 61 73 74 20 75 73 65 64 2e 20 54 68 65 20 last used. The
1267a 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 cursor position
1267b 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 20 69 6e is saved.** in
1267c 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 variables BtCur
1267d 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 42 74 43 sor.pKey and BtC
1267e 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 68 65 6e ursor.nKey. When
1267f 20 61 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 a cursor is in
12680 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 61 74 65 .** this state
12681 2c 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 , restoreCursorP
12682 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e 20 62 65 osition() can be
12683 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 74 65 6d called to attem
12684 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 65 6b 20 pt to.** seek
12685 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
12686 65 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e e saved position
12687 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 46 ..**.** CURSOR_F
12688 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 75 6e 72 AULT:.** A unr
12689 65 63 6f 76 65 72 61 62 6c 65 20 65 72 72 6f 72 ecoverable error
1268a 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f (an I/O error o
1268b 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 r a malloc failu
1268c 72 65 29 20 68 61 73 20 6f 63 63 75 72 72 65 64 re) has occurred
1268d 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 66 66 65 .** on a diffe
1268e 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 rent connection
1268f 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65 20 that shares the
12690 42 74 53 68 61 72 65 64 20 63 61 63 68 65 20 77 BtShared cache w
12691 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 20 63 75 ith this.** cu
12692 72 73 6f 72 2e 20 20 54 68 65 20 65 72 72 6f 72 rsor. The error
12693 20 68 61 73 20 6c 65 66 74 20 74 68 65 20 63 61 has left the ca
12694 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 che in an incons
12695 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a 2a istent state..**
12696 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 20 65 6c Do nothing el
12697 73 65 20 77 69 74 68 20 74 68 69 73 20 63 75 72 se with this cur
12698 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 sor. Any attemp
12699 74 20 74 6f 20 75 73 65 20 74 68 65 20 63 75 72 t to use the cur
1269a 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 6c 64 20 sor.** should
1269b 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
1269c 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e 20 code stored in
1269d 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 0a 2a 2f BtCursor.skip.*/
1269e 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f .#define CURSOR_
1269f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 20 20 20 INVALID
126a0 20 20 30 0a 23 64 65 66 69 6e 65 20 43 55 52 53 0.#define CURS
126a1 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 20 20 20 OR_VALID
126a2 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 43 1.#define C
126a3 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
126a4 4b 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e K 2.#defin
126a5 65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 e CURSOR_FAULT
126a6 20 20 20 20 20 20 20 20 20 20 20 33 0a 0a 2f 2a 3../*
126a7 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 The database pa
126a8 67 65 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 42 ge the PENDING_B
126a9 59 54 45 20 6f 63 63 75 70 69 65 73 2e 20 54 68 YTE occupies. Th
126aa 69 73 20 70 61 67 65 20 69 73 20 6e 65 76 65 72 is page is never
126ab 20 75 73 65 64 2e 0a 2a 2a 20 54 4f 44 4f 3a 20 used..** TODO:
126ac 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 76 65 This macro is ve
126ad 72 79 20 73 69 6d 69 6c 61 72 79 20 74 6f 20 50 ry similary to P
126ae 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 29 20 69 AGER_MJ_PGNO() i
126af 6e 20 70 61 67 65 72 2e 63 2e 20 54 68 65 79 0a n pager.c. They.
126b0 2a 2a 20 73 68 6f 75 6c 64 20 70 6f 73 73 69 62 ** should possib
126b1 6c 79 20 62 65 20 63 6f 6e 73 6f 6c 69 64 61 74 ly be consolidat
126b2 65 64 20 28 70 72 65 73 75 6d 61 62 6c 79 20 69 ed (presumably i
126b3 6e 20 70 61 67 65 72 2e 68 29 2e 0a 2a 2a 0a 2a n pager.h)..**.*
126b4 2a 20 49 66 20 64 69 73 6b 20 49 2f 4f 20 69 73 * If disk I/O is
126b5 20 6f 6d 69 74 74 65 64 20 28 6d 65 61 6e 69 6e omitted (meanin
126b6 67 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 g that the datab
126b7 61 73 65 20 69 73 20 73 74 6f 72 65 64 20 70 75 ase is stored pu
126b8 72 65 6c 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 rely.** in memor
126b9 79 29 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 y) then there is
126ba 20 6e 6f 20 70 65 6e 64 69 6e 67 20 62 79 74 65 no pending byte
126bb 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
126bc 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 23 TE_OMIT_DISKIO.#
126bd 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f define PENDING_
126be 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 20 BYTE_PAGE(pBt)
126bf 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65 0x7fffffff.#else
126c0 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e .# define PENDIN
126c1 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
126c2 20 28 28 50 67 6e 6f 29 28 28 50 45 4e 44 49 4e ((Pgno)((PENDIN
126c3 47 5f 42 59 54 45 2f 28 70 42 74 29 2d 3e 70 61 G_BYTE/(pBt)->pa
126c4 67 65 53 69 7a 65 29 2b 31 29 29 0a 23 65 6e 64 geSize)+1)).#end
126c5 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b if../*.** A link
126c6 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 20 66 ed list of the f
126c7 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
126c8 72 65 73 20 69 73 20 73 74 6f 72 65 64 20 61 74 res is stored at
126c9 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 2e BtShared.pLock.
126ca 0a 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 61 64 .** Locks are ad
126cb 64 65 64 20 28 6f 72 20 75 70 67 72 61 64 65 64 ded (or upgraded
126cc 20 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 4b 20 from READ_LOCK
126cd 74 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 77 to WRITE_LOCK) w
126ce 68 65 6e 20 61 20 63 75 72 73 6f 72 20 0a 2a 2a hen a cursor .**
126cf 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 is opened on th
126d0 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f e table with roo
126d1 74 20 70 61 67 65 20 42 74 53 68 61 72 65 64 2e t page BtShared.
126d2 69 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 61 72 iTable. Locks ar
126d3 65 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f e removed.** fro
126d4 6d 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e m this list when
126d5 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
126d6 73 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 s committed or r
126d7 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 20 77 olled back, or w
126d8 68 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 20 68 hen.** a btree h
126d9 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e andle is closed.
126da 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c 6f 63 .*/.struct BtLoc
126db 6b 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 k {. Btree *pBt
126dc 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 ree; /* B
126dd 74 72 65 65 20 68 61 6e 64 6c 65 20 68 6f 6c 64 tree handle hold
126de 69 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 2a 2f ing this lock */
126df 0a 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 3b 20 . Pgno iTable;
126e0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 /* Root
126e1 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 2a page of table *
126e2 2f 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20 /. u8 eLock;
126e3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 /* REA
126e4 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f D_LOCK or WRITE_
126e5 4c 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b LOCK */. BtLock
126e6 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 *pNext;
126e7 2f 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 68 61 /* Next in BtSha
126e8 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 20 2a red.pLock list *
126e9 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 64 61 /.};../* Candida
126ea 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 te values for Bt
126eb 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64 Lock.eLock */.#d
126ec 65 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20 efine READ_LOCK
126ed 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 57 52 1.#define WR
126ee 49 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f ITE_LOCK 2../
126ef 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f *.** These macro
126f0 73 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 s define the loc
126f1 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 69 ation of the poi
126f2 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 nter-map entry f
126f3 6f 72 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 or a .** databas
126f4 65 20 70 61 67 65 2e 20 54 68 65 20 66 69 72 73 e page. The firs
126f5 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 65 61 t argument to ea
126f6 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ch is the number
126f7 20 6f 66 20 75 73 61 62 6c 65 0a 2a 2a 20 62 79 of usable.** by
126f8 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 tes on each page
126f9 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
126fa 20 28 6f 66 74 65 6e 20 31 30 32 34 29 2e 20 54 (often 1024). T
126fb 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 he second is the
126fc 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 .** page number
126fd 74 6f 20 6c 6f 6f 6b 20 75 70 20 69 6e 20 74 68 to look up in th
126fe 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a e pointer map..*
126ff 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 50 41 47 45 *.** PTRMAP_PAGE
12700 4e 4f 20 72 65 74 75 72 6e 73 20 74 68 65 20 64 NO returns the d
12701 61 74 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d atabase page num
12702 62 65 72 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 ber of the point
12703 65 72 2d 6d 61 70 0a 2a 2a 20 70 61 67 65 20 74 er-map.** page t
12704 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 72 hat stores the r
12705 65 71 75 69 72 65 64 20 70 6f 69 6e 74 65 72 2e equired pointer.
12706 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 PTRMAP_PTROFFSE
12707 54 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 T returns.** the
12708 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 72 offset of the r
12709 65 71 75 65 73 74 65 64 20 6d 61 70 20 65 6e 74 equested map ent
1270a 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ry..**.** If the
1270b 20 70 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 70 pgno argument p
1270c 61 73 73 65 64 20 74 6f 20 50 54 52 4d 41 50 5f assed to PTRMAP_
1270d 50 41 47 45 4e 4f 20 69 73 20 61 20 70 6f 69 6e PAGENO is a poin
1270e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 0a 2a 2a ter-map page,.**
1270f 20 74 68 65 6e 20 70 67 6e 6f 20 69 73 20 72 65 then pgno is re
12710 74 75 72 6e 65 64 2e 20 53 6f 20 28 70 67 6e 6f turned. So (pgno
12711 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 ==PTRMAP_PAGENO(
12712 70 67 73 7a 2c 20 70 67 6e 6f 29 29 20 63 61 6e pgsz, pgno)) can
12713 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 74 be.** used to t
12714 65 73 74 20 69 66 20 70 67 6e 6f 20 69 73 20 61 est if pgno is a
12715 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
12716 65 2e 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 e. PTRMAP_ISPAGE
12717 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 implements.** t
12718 68 69 73 20 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 his test..*/.#de
12719 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 41 47 45 fine PTRMAP_PAGE
1271a 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 29 20 70 74 NO(pBt, pgno) pt
1271b 72 6d 61 70 50 61 67 65 6e 6f 28 70 42 74 2c 20 rmapPageno(pBt,
1271c 70 67 6e 6f 29 0a 23 64 65 66 69 6e 65 20 50 54 pgno).#define PT
1271d 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 70 RMAP_PTROFFSET(p
1271e 67 70 74 72 6d 61 70 2c 20 70 67 6e 6f 29 20 28 gptrmap, pgno) (
1271f 35 2a 28 70 67 6e 6f 2d 70 67 70 74 72 6d 61 70 5*(pgno-pgptrmap
12720 2d 31 29 29 0a 23 64 65 66 69 6e 65 20 50 54 52 -1)).#define PTR
12721 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
12722 70 67 6e 6f 29 20 28 50 54 52 4d 41 50 5f 50 41 pgno) (PTRMAP_PA
12723 47 45 4e 4f 28 28 70 42 74 29 2c 28 70 67 6e 6f GENO((pBt),(pgno
12724 29 29 3d 3d 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a ))==(pgno))../*.
12725 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d ** The pointer m
12726 61 70 20 69 73 20 61 20 6c 6f 6f 6b 75 70 20 74 ap is a lookup t
12727 61 62 6c 65 20 74 68 61 74 20 69 64 65 6e 74 69 able that identi
12728 66 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 fies the parent
12729 70 61 67 65 20 66 6f 72 0a 2a 2a 20 65 61 63 68 page for.** each
1272a 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 20 74 child page in t
1272b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1272c 2e 20 20 54 68 65 20 70 61 72 65 6e 74 20 70 61 . The parent pa
1272d 67 65 20 69 73 20 74 68 65 20 70 61 67 65 20 74 ge is the page t
1272e 68 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 hat.** contains
1272f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
12730 20 63 68 69 6c 64 2e 20 20 45 76 65 72 79 20 70 child. Every p
12731 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 age in the datab
12732 61 73 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 ase contains.**
12733 30 20 6f 72 20 31 20 70 61 72 65 6e 74 20 70 61 0 or 1 parent pa
12734 67 65 73 2e 20 20 28 49 6e 20 74 68 69 73 20 63 ges. (In this c
12735 6f 6e 74 65 78 74 20 27 64 61 74 61 62 61 73 65 ontext 'database
12736 20 70 61 67 65 27 20 72 65 66 65 72 73 0a 2a 2a page' refers.**
12737 20 74 6f 20 61 6e 79 20 70 61 67 65 20 74 68 61 to any page tha
12738 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 t is not part of
12739 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
1273a 20 69 74 73 65 6c 66 2e 29 20 20 45 61 63 68 20 itself.) Each
1273b 70 6f 69 6e 74 65 72 20 6d 61 70 0a 2a 2a 20 65 pointer map.** e
1273c 6e 74 72 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 ntry consists of
1273d 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 27 a single byte '
1273e 74 79 70 65 27 20 61 6e 64 20 61 20 34 20 62 79 type' and a 4 by
1273f 74 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e te parent page n
12740 75 6d 62 65 72 2e 0a 2a 2a 20 54 68 65 20 50 54 umber..** The PT
12741 52 4d 41 50 5f 58 58 58 20 69 64 65 6e 74 69 66 RMAP_XXX identif
12742 69 65 72 73 20 62 65 6c 6f 77 20 61 72 65 20 74 iers below are t
12743 68 65 20 76 61 6c 69 64 20 74 79 70 65 73 2e 0a he valid types..
12744 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 **.** The purpos
12745 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 e of the pointer
12746 20 6d 61 70 20 69 73 20 74 6f 20 66 61 63 69 6c map is to facil
12747 69 74 79 20 6d 6f 76 69 6e 67 20 70 61 67 65 73 ity moving pages
12748 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73 from one.** pos
12749 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c ition in the fil
1274a 65 20 74 6f 20 61 6e 6f 74 68 65 72 20 61 73 20 e to another as
1274b 70 61 72 74 20 6f 66 20 61 75 74 6f 76 61 63 75 part of autovacu
1274c 75 6d 2e 20 20 57 68 65 6e 20 61 20 70 61 67 65 um. When a page
1274d 0a 2a 2a 20 69 73 20 6d 6f 76 65 64 2c 20 74 68 .** is moved, th
1274e 65 20 70 6f 69 6e 74 65 72 20 69 6e 20 69 74 73 e pointer in its
1274f 20 70 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20 parent must be
12750 75 70 64 61 74 65 64 20 74 6f 20 70 6f 69 6e 74 updated to point
12751 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c to the.** new l
12752 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 6f ocation. The po
12753 69 6e 74 65 72 20 6d 61 70 20 69 73 20 75 73 65 inter map is use
12754 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 d to locate the
12755 70 61 72 65 6e 74 20 70 61 67 65 20 71 75 69 63 parent page quic
12756 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 kly..**.** PTRMA
12757 50 5f 52 4f 4f 54 50 41 47 45 3a 20 54 68 65 20 P_ROOTPAGE: The
12758 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 database page is
12759 20 61 20 72 6f 6f 74 2d 70 61 67 65 2e 20 54 68 a root-page. Th
1275a 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 e page-number is
1275b 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 not.**
1275c 20 20 20 20 20 20 20 20 20 75 73 65 64 20 69 6e used in
1275d 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a this case..**.*
1275e 2a 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 * PTRMAP_FREEPAG
1275f 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 E: The database
12760 70 61 67 65 20 69 73 20 61 6e 20 75 6e 75 73 65 page is an unuse
12761 64 20 28 66 72 65 65 29 20 70 61 67 65 2e 20 54 d (free) page. T
12762 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 0a he page-number .
12763 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
12764 20 20 20 20 69 73 20 6e 6f 74 20 75 73 65 64 20 is not used
12765 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a in this case..**
12766 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 .** PTRMAP_OVERF
12767 4c 4f 57 31 3a 20 54 68 65 20 64 61 74 61 62 61 LOW1: The databa
12768 73 65 20 70 61 67 65 20 69 73 20 74 68 65 20 66 se page is the f
12769 69 72 73 74 20 70 61 67 65 20 69 6e 20 61 20 6c irst page in a l
1276a 69 73 74 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 ist of .**
1276b 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 ove
1276c 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 rflow pages. The
1276d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 64 65 page number ide
1276e 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 67 65 ntifies the page
1276f 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 that.**
12770 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 61 conta
12771 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 77 69 74 ins the cell wit
12772 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 h a pointer to t
12773 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 his overflow pag
12774 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f e..**.** PTRMAP_
12775 4f 56 45 52 46 4c 4f 57 32 3a 20 54 68 65 20 64 OVERFLOW2: The d
12776 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 atabase page is
12777 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 20 6c 61 the second or la
12778 74 65 72 20 70 61 67 65 20 69 6e 20 61 20 6c 69 ter page in a li
12779 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 st of.**
1277a 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 overf
1277b 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 low pages. The p
1277c 61 67 65 2d 6e 75 6d 62 65 72 20 69 64 65 6e 74 age-number ident
1277d 69 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f ifies the previo
1277e 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 us.**
1277f 20 20 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 page in
12780 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
12781 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54 e list..**.** PT
12782 52 4d 41 50 5f 42 54 52 45 45 3a 20 54 68 65 20 RMAP_BTREE: The
12783 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 database page is
12784 20 61 20 6e 6f 6e 2d 72 6f 6f 74 20 62 74 72 65 a non-root btre
12785 65 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 e page. The page
12786 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 number.**
12787 20 20 20 20 20 20 20 20 20 69 64 65 6e 74 69 66 identif
12788 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 ies the parent p
12789 61 67 65 20 69 6e 20 74 68 65 20 62 74 72 65 65 age in the btree
1278a 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 ..*/.#define PTR
1278b 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 31 0a 23 MAP_ROOTPAGE 1.#
1278c 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 46 52 define PTRMAP_FR
1278d 45 45 50 41 47 45 20 32 0a 23 64 65 66 69 6e 65 EEPAGE 2.#define
1278e 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
1278f 31 20 33 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 1 3.#define PTRM
12790 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 34 0a 23 AP_OVERFLOW2 4.#
12791 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 42 54 define PTRMAP_BT
12792 52 45 45 20 35 0a 0a 2f 2a 20 41 20 62 75 6e 63 REE 5../* A bunc
12793 68 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 h of assert() st
12794 61 74 65 6d 65 6e 74 73 20 74 6f 20 63 68 65 63 atements to chec
12795 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f k the transactio
12796 6e 20 73 74 61 74 65 20 76 61 72 69 61 62 6c 65 n state variable
12797 73 0a 2a 2a 20 6f 66 20 68 61 6e 64 6c 65 20 70 s.** of handle p
12798 20 28 74 79 70 65 20 42 74 72 65 65 2a 29 20 61 (type Btree*) a
12799 72 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f re internally co
1279a 6e 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65 nsistent..*/.#de
1279b 66 69 6e 65 20 62 74 72 65 65 49 6e 74 65 67 72 fine btreeIntegr
1279c 69 74 79 28 70 29 20 5c 0a 20 20 61 73 73 65 72 ity(p) \. asser
1279d 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 t( p->pBt->inTra
1279e 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f nsaction!=TRANS_
1279f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e NONE || p->pBt->
127a0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 30 20 nTransaction==0
127a1 29 3b 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 ); \. assert( p
127a2 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ->pBt->inTransac
127a3 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73 tion>=p->inTrans
127a4 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ); .../*.** The
127a5 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 ISAUTOVACUUM ma
127a6 63 72 6f 20 69 73 20 75 73 65 64 20 77 69 74 68 cro is used with
127a7 69 6e 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f in balance_nonro
127a8 6f 74 28 29 20 74 6f 20 64 65 74 65 72 6d 69 6e ot() to determin
127a9 65 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 e.** if the data
127aa 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 base supports au
127ab 74 6f 2d 76 61 63 75 75 6d 20 6f 72 20 6e 6f 74 to-vacuum or not
127ac 2e 20 42 65 63 61 75 73 65 20 69 74 20 69 73 20 . Because it is
127ad 75 73 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61 used.** within a
127ae 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 n expression tha
127af 74 20 69 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 t is an argument
127b0 20 74 6f 20 61 6e 6f 74 68 65 72 20 6d 61 63 72 to another macr
127b1 6f 20 0a 2a 2a 20 28 73 71 6c 69 74 65 4d 61 6c o .** (sqliteMal
127b2 6c 6f 63 52 61 77 29 2c 20 69 74 20 69 73 20 6e locRaw), it is n
127b3 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 ot possible to u
127b4 73 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 63 se conditional c
127b5 6f 6d 70 69 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53 ompilation..** S
127b6 6f 2c 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 o, this macro is
127b7 20 64 65 66 69 6e 65 64 20 69 6e 73 74 65 61 64 defined instead
127b8 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
127b9 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
127ba 55 55 4d 0a 23 64 65 66 69 6e 65 20 49 53 41 55 UUM.#define ISAU
127bb 54 4f 56 41 43 55 55 4d 20 28 70 42 74 2d 3e 61 TOVACUUM (pBt->a
127bc 75 74 6f 56 61 63 75 75 6d 29 0a 23 65 6c 73 65 utoVacuum).#else
127bd 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56 .#define ISAUTOV
127be 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a ACUUM 0.#endif..
127bf 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 ./*.** This stru
127c0 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64 20 cture is passed
127c1 61 72 6f 75 6e 64 20 74 68 72 6f 75 67 68 20 61 around through a
127c2 6c 6c 20 74 68 65 20 73 61 6e 69 74 79 20 63 68 ll the sanity ch
127c3 65 63 6b 69 6e 67 20 72 6f 75 74 69 6e 65 73 0a ecking routines.
127c4 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b ** in order to k
127c5 65 65 70 20 74 72 61 63 6b 20 6f 66 20 73 6f 6d eep track of som
127c6 65 20 67 6c 6f 62 61 6c 20 73 74 61 74 65 20 69 e global state i
127c7 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 nformation..*/.t
127c8 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e ypedef struct In
127c9 74 65 67 72 69 74 79 43 6b 20 49 6e 74 65 67 72 tegrityCk Integr
127ca 69 74 79 43 6b 3b 0a 73 74 72 75 63 74 20 49 6e ityCk;.struct In
127cb 74 65 67 72 69 74 79 43 6b 20 7b 0a 20 20 42 74 tegrityCk {. Bt
127cc 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 Shared *pBt;
127cd 2f 2a 20 54 68 65 20 74 72 65 65 20 62 65 69 6e /* The tree bein
127ce 67 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f g checked out */
127cf 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
127d0 3b 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f ; /* The asso
127d1 63 69 61 74 65 64 20 70 61 67 65 72 2e 20 20 41 ciated pager. A
127d2 6c 73 6f 20 61 63 63 65 73 73 69 62 6c 65 20 62 lso accessible b
127d3 79 20 70 42 74 2d 3e 70 50 61 67 65 72 20 2a 2f y pBt->pPager */
127d4 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 . Pgno nPage;
127d5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
127d6 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
127d7 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 atabase */. int
127d8 20 2a 61 6e 52 65 66 3b 20 20 20 20 20 20 20 2f *anRef; /
127d9 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 * Number of time
127da 73 20 65 61 63 68 20 70 61 67 65 20 69 73 20 72 s each page is r
127db 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69 eferenced */. i
127dc 6e 74 20 6d 78 45 72 72 3b 20 20 20 20 20 20 20 nt mxErr;
127dd 20 2f 2a 20 53 74 6f 70 20 61 63 63 75 6d 75 6c /* Stop accumul
127de 61 74 69 6e 67 20 65 72 72 6f 72 73 20 77 68 65 ating errors whe
127df 6e 20 74 68 69 73 20 72 65 61 63 68 65 73 20 7a n this reaches z
127e0 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 ero */. int nEr
127e1 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 r; /* Nu
127e2 6d 62 65 72 20 6f 66 20 6d 65 73 73 61 67 65 73 mber of messages
127e3 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 45 72 72 written to zErr
127e4 4d 73 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 Msg so far */.
127e5 69 6e 74 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 int mallocFailed
127e6 3b 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c ; /* A memory al
127e7 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 location error h
127e8 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 20 as occurred */.
127e9 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73 67 StrAccum errMsg
127ea 3b 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 ; /* Accumulate
127eb 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
127ec 67 65 20 74 65 78 74 20 68 65 72 65 20 2a 2f 0a ge text here */.
127ed 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f };../*.** Read o
127ee 72 20 77 72 69 74 65 20 61 20 74 77 6f 2d 20 61 r write a two- a
127ef 6e 64 20 66 6f 75 72 2d 62 79 74 65 20 62 69 67 nd four-byte big
127f0 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
127f1 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 values..*/.#defi
127f2 6e 65 20 67 65 74 32 62 79 74 65 28 78 29 20 20 ne get2byte(x)
127f3 20 28 28 78 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 ((x)[0]<<8 | (x
127f4 29 5b 31 5d 29 0a 23 64 65 66 69 6e 65 20 70 75 )[1]).#define pu
127f5 74 32 62 79 74 65 28 70 2c 76 29 20 28 28 70 29 t2byte(p,v) ((p)
127f6 5b 30 5d 20 3d 20 28 76 29 3e 3e 38 2c 20 28 70 [0] = (v)>>8, (p
127f7 29 5b 31 5d 20 3d 20 28 76 29 29 0a 23 64 65 66 )[1] = (v)).#def
127f8 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c ine get4byte sql
127f9 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65 ite3Get4byte.#de
127fa 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71 fine put4byte sq
127fb 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f lite3Put4byte../
127fc 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 72 6f *.** Internal ro
127fd 75 74 69 6e 65 73 20 74 68 61 74 20 73 68 6f 75 utines that shou
127fe 6c 64 20 62 65 20 61 63 63 65 73 73 65 64 20 62 ld be accessed b
127ff 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 y the btree laye
12800 72 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 r only..*/.SQLIT
12801 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
12802 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
12803 65 28 42 74 53 68 61 72 65 64 2a 2c 20 50 67 6e e(BtShared*, Pgn
12804 6f 2c 20 4d 65 6d 50 61 67 65 2a 2a 2c 20 69 6e o, MemPage**, in
12805 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
12806 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
12807 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d 50 reeInitPage(MemP
12808 61 67 65 20 2a 70 50 61 67 65 29 3b 0a 53 51 4c age *pPage);.SQL
12809 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1280a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
1280b 73 65 43 65 6c 6c 50 74 72 28 4d 65 6d 50 61 67 seCellPtr(MemPag
1280c 65 2a 2c 20 75 38 2a 2c 20 43 65 6c 6c 49 6e 66 e*, u8*, CellInf
1280d 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 o*);.SQLITE_PRIV
1280e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1280f 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 4d BtreeParseCell(M
12810 65 6d 50 61 67 65 2a 2c 20 69 6e 74 2c 20 43 65 emPage*, int, Ce
12811 6c 6c 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 llInfo*);.SQLITE
12812 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
12813 69 74 65 33 42 74 72 65 65 52 65 73 74 6f 72 65 ite3BtreeRestore
12814 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 CursorPosition(B
12815 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a tCursor *pCur);.
12816 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
12817 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
12818 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74 GetTempCursor(Bt
12819 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74 Cursor *pCur, Bt
1281a 43 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72 Cursor *pTempCur
1281b 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
1281c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
1281d 72 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 reeReleaseTempCu
1281e 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 rsor(BtCursor *p
1281f 43 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Cur);.SQLITE_PRI
12820 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
12821 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 3BtreeMoveToPare
12822 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 nt(BtCursor *pCu
12823 72 29 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a r);../**********
12824 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 **** End of btre
12825 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
12826 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12827 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12828 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
12829 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
1282a 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
1282b 66 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 2a f in btmutex.c *
1282c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1282d 2a 2a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ***/.#if SQLITE_
1282e 54 48 52 45 41 44 53 41 46 45 20 26 26 20 21 64 THREADSAFE && !d
1282f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
12830 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 IT_SHARED_CACHE)
12831 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 .../*.** Enter a
12832 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 67 69 mutex on the gi
12833 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65 63 74 ven BTree object
12834 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f ..**.** If the o
12835 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73 68 61 bject is not sha
12836 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d rable, then no m
12837 75 74 65 78 20 69 73 20 65 76 65 72 20 72 65 71 utex is ever req
12838 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74 68 69 uired.** and thi
12839 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
1283a 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64 65 72 o-op. The under
1283b 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73 20 6e lying mutex is n
1283c 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a on-recursive..**
1283d 20 42 75 74 20 77 65 20 6b 65 65 70 20 61 20 72 But we keep a r
1283e 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 eference count i
1283f 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f 4c 6f n Btree.wantToLo
12840 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61 76 69 ck so the behavi
12841 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20 69 6e or.** of this in
12842 74 65 72 66 61 63 65 20 69 73 20 72 65 63 75 72 terface is recur
12843 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 sive..**.** To a
12844 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73 2c 20 void deadlocks,
12845 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65 73 20 multiple Btrees
12846 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 are locked in th
12847 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a 2a 20 e same order.**
12848 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 by all database
12849 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 connections. Th
1284a 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20 61 20 e p->pNext is a
1284b 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a 2a 2a list of other.**
1284c 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67 69 6e Btrees belongin
1284d 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 g to the same da
1284e 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1284f 6e 20 61 73 20 74 68 65 20 70 20 42 74 72 65 65 n as the p Btree
12850 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20 74 .** which need t
12851 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66 74 65 o be locked afte
12852 72 20 70 2e 20 20 49 66 20 77 65 20 63 61 6e 6e r p. If we cann
12853 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20 6f 6e ot get a lock on
12854 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69 72 73 .** p, then firs
12855 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20 t unlock all of
12856 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20 70 2d the others on p-
12857 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77 61 69 >pNext, then wai
12858 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c 6f 63 t.** for the loc
12859 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76 61 69 k to become avai
1285a 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68 65 6e lable on p, then
1285b 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20 74 relock all of t
1285c 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 he.** subsequent
1285d 20 42 74 72 65 65 73 20 74 68 61 74 20 64 65 73 Btrees that des
1285e 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 ire a lock..*/.S
1285f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
12860 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
12861 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a nter(Btree *p){.
12862 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65 72 3b Btree *pLater;
12863 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 .. /* Some basi
12864 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e c sanity checkin
12865 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2e 20 g on the Btree.
12866 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42 74 72 The list of Btr
12867 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 ees. ** connect
12868 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e 64 20 ed by pNext and
12869 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62 65 20 pPrev should be
1286a 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 in sorted order
1286b 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e 70 42 by. ** Btree.pB
1286c 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65 6c 65 t value. All ele
1286d 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73 ments of the lis
1286e 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e 67 20 t should belong
1286f 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 to. ** the same
12870 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 6c connection. Onl
12871 79 20 73 68 61 72 65 64 20 42 74 72 65 65 73 20 y shared Btrees
12872 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e are on the list.
12873 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d */. assert( p-
12874 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e >pNext==0 || p->
12875 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e 70 42 pNext->pBt>p->pB
12876 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t );. assert( p
12877 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 70 2d ->pPrev==0 || p-
12878 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d 3e 70 >pPrev->pBt<p->p
12879 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Bt );. assert(
1287a 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 p->pNext==0 || p
1287b 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70 2d 3e ->pNext->db==p->
1287c 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 db );. assert(
1287d 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 70 p->pPrev==0 || p
1287e 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70 2d 3e ->pPrev->db==p->
1287f 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 db );. assert(
12880 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 28 p->sharable || (
12881 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26 20 70 p->pNext==0 && p
12882 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b 0a 0a ->pPrev==0) );..
12883 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6c /* Check for l
12884 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74 65 6e ocking consisten
12885 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 cy */. assert(
12886 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d !p->locked || p-
12887 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b >wantToLock>0 );
12888 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 . assert( p->sh
12889 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77 61 6e arable || p->wan
1288a 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 tToLock==0 );..
1288b 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c /* We should al
1288c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 ready hold a loc
1288d 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
1288e 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
1288f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
12890 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
12891 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
12892 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
12893 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d e ) return;. p-
12894 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 >wantToLock++;.
12895 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 if( p->locked )
12896 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 return;.. /* I
12897 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65 n most cases, we
12898 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20 should be able
12899 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c to acquire the l
1289a 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74 ock we. ** want
1289b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
1289c 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74 to go throught t
1289d 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63 he ascending loc
1289e 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65 k. ** procedure
1289f 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20 that follows.
128a0 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74 Just be sure not
128a1 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a to block.. */.
128a2 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75 if( sqlite3_mu
128a3 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e tex_try(p->pBt->
128a4 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f mutex)==SQLITE_O
128a5 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b K ){. p->lock
128a6 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 ed = 1;. retu
128a7 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f rn;. }.. /* To
128a8 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c avoid deadlock,
128a9 20 66 69 72 73 74 20 72 65 6c 65 61 73 65 20 61 first release a
128aa 6c 6c 20 6c 6f 63 6b 73 20 77 69 74 68 20 61 20 ll locks with a
128ab 6c 61 72 67 65 72 0a 20 20 2a 2a 20 42 74 53 68 larger. ** BtSh
128ac 61 72 65 64 20 61 64 64 72 65 73 73 2e 20 20 54 ared address. T
128ad 68 65 6e 20 61 63 71 75 69 72 65 20 6f 75 72 20 hen acquire our
128ae 6c 6f 63 6b 2e 20 20 54 68 65 6e 20 72 65 61 63 lock. Then reac
128af 71 75 69 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f quire. ** the o
128b0 74 68 65 72 20 42 74 53 68 61 72 65 64 20 6c 6f ther BtShared lo
128b1 63 6b 73 20 74 68 61 74 20 77 65 20 75 73 65 64 cks that we used
128b2 20 74 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63 65 to hold in asce
128b3 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72 nding. ** order
128b4 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 .. */. for(pLa
128b5 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c ter=p->pNext; pL
128b6 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 ater; pLater=pLa
128b7 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
128b8 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d assert( pLater-
128b9 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 20 >sharable );.
128ba 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d assert( pLater-
128bb 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 >pNext==0 || pLa
128bc 74 65 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e ter->pNext->pBt>
128bd 70 4c 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20 pLater->pBt );.
128be 20 20 20 61 73 73 65 72 74 28 20 21 70 4c 61 74 assert( !pLat
128bf 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c er->locked || pL
128c0 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b ater->wantToLock
128c1 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c >0 );. if( pL
128c2 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a ater->locked ){.
128c3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
128c4 74 65 78 5f 6c 65 61 76 65 28 70 4c 61 74 65 72 tex_leave(pLater
128c5 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 ->pBt->mutex);.
128c6 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 pLater->loc
128c7 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 ked = 0;. }.
128c8 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 }. sqlite3_mut
128c9 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 2d ex_enter(p->pBt-
128ca 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f >mutex);. p->lo
128cb 63 6b 65 64 20 3d 20 31 3b 0a 20 20 66 6f 72 28 cked = 1;. for(
128cc 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b pLater=p->pNext;
128cd 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d pLater; pLater=
128ce 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a pLater->pNext){.
128cf 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e if( pLater->
128d0 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 wantToLock ){.
128d1 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
128d2 78 5f 65 6e 74 65 72 28 70 4c 61 74 65 72 2d 3e x_enter(pLater->
128d3 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 pBt->mutex);.
128d4 20 20 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 pLater->locke
128d5 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d d = 1;. }. }
128d6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74 20 74 .}../*.** Exit t
128d7 68 65 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 he recursive mut
128d8 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e 0a 2a ex on a Btree..*
128d9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
128da 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
128db 65 65 4c 65 61 76 65 28 42 74 72 65 65 20 2a 70 eeLeave(Btree *p
128dc 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 ){. if( p->shar
128dd 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73 73 65 able ){. asse
128de 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 rt( p->wantToLoc
128df 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 77 61 k>0 );. p->wa
128e0 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 ntToLock--;.
128e1 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 if( p->wantToLoc
128e2 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 k==0 ){. as
128e3 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 sert( p->locked
128e4 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
128e5 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
128e6 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 pBt->mutex);.
128e7 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 p->locked = 0
128e8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 ;. }. }.}..#
128e9 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a ifndef NDEBUG./*
128ea 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
128eb 69 66 20 74 68 65 20 42 74 53 68 61 72 65 64 20 if the BtShared
128ec 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e mutex is held on
128ed 20 74 68 65 20 62 74 72 65 65 2e 20 20 0a 2a 2a the btree. .**
128ee 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
128ef 20 6d 61 6b 65 73 20 6e 6f 20 64 65 74 65 72 6d makes no determ
128f0 69 6e 61 74 69 6f 6e 20 6f 6e 65 20 77 68 79 20 ination one why
128f1 6f 72 20 61 6e 6f 74 68 65 72 20 69 66 20 74 68 or another if th
128f2 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f e.** database co
128f3 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65 78 20 69 nnection mutex i
128f4 73 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 s held..**.** Th
128f5 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
128f6 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 ed only from wit
128f7 68 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 hin assert() sta
128f8 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 tements..*/.SQLI
128f9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
128fa 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
128fb 4d 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b Mutex(Btree *p){
128fc 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 73 68 . return (p->sh
128fd 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 0a 20 20 20 arable==0 ||.
128fe 20 20 20 20 20 20 20 20 20 20 28 70 2d 3e 6c 6f (p->lo
128ff 63 6b 65 64 20 26 26 20 70 2d 3e 77 61 6e 74 54 cked && p->wantT
12900 6f 4c 6f 63 6b 20 26 26 20 73 71 6c 69 74 65 33 oLock && sqlite3
12901 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 _mutex_held(p->p
12902 42 74 2d 3e 6d 75 74 65 78 29 29 29 3b 0a 7d 0a Bt->mutex)));.}.
12903 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 #endif...#ifndef
12904 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
12905 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 RBLOB./*.** Ente
12906 72 20 61 6e 64 20 6c 65 61 76 65 20 61 20 6d 75 r and leave a mu
12907 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 20 67 tex on a Btree g
12908 69 76 65 6e 20 61 20 63 75 72 73 6f 72 20 6f 77 iven a cursor ow
12909 6e 65 64 20 62 79 20 74 68 61 74 0a 2a 2a 20 42 ned by that.** B
1290a 74 72 65 65 2e 20 20 54 68 65 73 65 20 65 6e 74 tree. These ent
1290b 72 79 20 70 6f 69 6e 74 73 20 61 72 65 20 75 73 ry points are us
1290c 65 64 20 62 79 20 69 6e 63 72 65 6d 65 6e 74 61 ed by incrementa
1290d 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e 20 62 65 l I/O and can be
1290e 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69 66 20 74 .** omitted if t
1290f 68 61 74 20 6d 6f 64 75 6c 65 20 69 73 20 6e 6f hat module is no
12910 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 t used..*/.SQLIT
12911 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
12912 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
12913 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 Cursor(BtCursor
12914 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c 69 74 65 *pCur){. sqlite
12915 33 42 74 72 65 65 45 6e 74 65 72 28 70 43 75 72 3BtreeEnter(pCur
12916 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a 53 51 4c ->pBtree);.}.SQL
12917 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
12918 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
12919 76 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f veCursor(BtCurso
1291a 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c 69 r *pCur){. sqli
1291b 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 43 te3BtreeLeave(pC
1291c 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a 23 ur->pBtree);.}.#
1291d 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1291e 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f OMIT_INCRBLOB */
1291f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 .../*.** Enter t
12920 68 65 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 he mutex on ever
12921 79 20 42 74 72 65 65 20 61 73 73 6f 63 69 61 74 y Btree associat
12922 65 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61 ed with a databa
12923 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
12924 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64 65 . This is neede
12925 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 d (for example)
12926 70 72 69 6f 72 20 74 6f 20 70 61 72 73 69 6e 67 prior to parsing
12927 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 6e 74 20 .** a statement
12928 73 69 6e 63 65 20 77 65 20 77 69 6c 6c 20 62 65 since we will be
12929 20 63 6f 6d 70 61 72 69 6e 67 20 74 61 62 6c 65 comparing table
1292a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 and column name
1292b 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 61 6c 6c s.** against all
1292c 20 73 63 68 65 6d 61 73 20 61 6e 64 20 77 65 20 schemas and we
1292d 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 6f 73 do not want thos
1292e 65 20 73 63 68 65 6d 61 73 20 62 65 69 6e 67 0a e schemas being.
1292f 2a 2a 20 72 65 73 65 74 20 6f 75 74 20 66 72 6f ** reset out fro
12930 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a 2a 0a 2a m under us..**.*
12931 2a 20 54 68 65 72 65 20 69 73 20 61 20 63 6f 72 * There is a cor
12932 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65 61 76 65 responding leave
12933 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72 65 73 2e -all procedures.
12934 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 .**.** Enter the
12935 20 6d 75 74 65 78 65 73 20 69 6e 20 61 63 63 65 mutexes in acce
12936 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 42 nding order by B
12937 74 53 68 61 72 65 64 20 70 6f 69 6e 74 65 72 20 tShared pointer
12938 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f 20 61 76 address.** to av
12939 6f 69 64 20 74 68 65 20 70 6f 73 73 69 62 69 6c oid the possibil
1293a 69 74 79 20 6f 66 20 64 65 61 64 6c 6f 63 6b 20 ity of deadlock
1293b 77 68 65 6e 20 74 77 6f 20 74 68 72 65 61 64 73 when two threads
1293c 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20 6f 72 20 with.** two or
1293d 6d 6f 72 65 20 62 74 72 65 65 73 20 69 6e 20 63 more btrees in c
1293e 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72 79 20 74 ommon both try t
1293f 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68 65 69 72 o lock all their
12940 20 62 74 72 65 65 73 0a 2a 2a 20 61 74 20 74 68 btrees.** at th
12941 65 20 73 61 6d 65 20 69 6e 73 74 61 6e 74 2e 0a e same instant..
12942 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12943 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
12944 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 reeEnterAll(sqli
12945 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 te3 *db){. int
12946 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 2a i;. Btree *p, *
12947 70 4c 61 74 65 72 3b 0a 20 20 61 73 73 65 72 74 pLater;. assert
12948 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
12949 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
1294a 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c );. for(i=0; i<
1294b 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 db->nDb; i++){.
1294c 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 p = db->aDb[i
1294d 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 ].pBt;. if( p
1294e 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 && p->sharable
1294f 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 ){. p->want
12950 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 ToLock++;.
12951 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 if( !p->locked )
12952 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
12953 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
12954 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68 =1 );. wh
12955 69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20 29 20 ile( p->pPrev )
12956 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 p = p->pPrev;.
12957 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e while( p->
12958 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 70 4e 65 locked && p->pNe
12959 78 74 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78 xt ) p = p->pNex
1295a 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 t;. for(p
1295b 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74 Later = p->pNext
1295c 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 ; pLater; pLater
1295d 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b =pLater->pNext){
1295e 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
1295f 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b Later->locked ){
12960 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
12961 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
12962 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d 3e 6d 75 (pLater->pBt->mu
12963 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 tex);.
12964 20 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 pLater->locked
12965 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
12966 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
12967 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a while( p ){.
12968 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
12969 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3_mutex_enter(p-
1296a 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 >pBt->mutex);.
1296b 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 p->locke
1296c 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 d++;. p
1296d 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 = p->pNext;.
1296e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
1296f 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 }. }.}.SQLIT
12970 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
12971 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
12972 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 All(sqlite3 *db)
12973 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74 72 {. int i;. Btr
12974 65 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 ee *p;. assert(
12975 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
12976 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
12977 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 ;. for(i=0; i<d
12978 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
12979 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d p = db->aDb[i]
1297a 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 .pBt;. if( p
1297b 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 && p->sharable )
1297c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1297d 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 p->wantToLock>0
1297e 29 3b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 );. p->want
1297f 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 ToLock--;.
12980 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 if( p->wantToLoc
12981 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 k==0 ){.
12982 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 assert( p->locke
12983 64 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c d );. sql
12984 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
12985 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b (p->pBt->mutex);
12986 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b . p->lock
12987 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a ed = 0;. }.
12988 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 }. }.}..#if
12989 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a ndef NDEBUG./*.*
1298a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
1298b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 the current thr
1298c 65 61 64 20 68 6f 6c 64 73 20 74 68 65 20 64 61 ead holds the da
1298d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1298e 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 61 n.** mutex and a
1298f 6c 6c 20 72 65 71 75 69 72 65 64 20 42 74 53 68 ll required BtSh
12990 61 72 65 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2a ared mutexes..**
12991 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
12992 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 is used inside
12993 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
12994 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c nts only..*/.SQL
12995 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
12996 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
12997 73 41 6c 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 sAllMutexes(sqli
12998 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 te3 *db){. int
12999 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 i;. if( !sqlite
1299a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3_mutex_held(db-
1299b 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 72 >mutex) ){. r
1299c 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 eturn 0;. }. f
1299d 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
1299e 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 b; i++){. Btr
1299f 65 65 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20 64 ee *p;. p = d
129a0 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
129a1 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73 if( p && p->s
129a2 68 61 72 61 62 6c 65 20 26 26 0a 20 20 20 20 20 harable &&.
129a3 20 20 20 20 28 70 2d 3e 77 61 6e 74 54 6f 4c 6f (p->wantToLo
129a4 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 6c 69 74 65 ck==0 || !sqlite
129a5 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
129a6 70 42 74 2d 3e 6d 75 74 65 78 29 29 20 29 7b 0a pBt->mutex)) ){.
129a7 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
129a8 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
129a9 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f rn 1;.}.#endif /
129aa 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a * NDEBUG */../*.
129ab 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 42 74 72 ** Add a new Btr
129ac 65 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 ee pointer to a
129ad 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2e BtreeMutexArray.
129ae 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 6f 69 6e .** if the poin
129af 74 65 72 20 63 61 6e 20 70 6f 73 73 69 62 6c 79 ter can possibly
129b0 20 62 65 20 73 68 61 72 65 64 20 77 69 74 68 0a be shared with.
129b1 2a 2a 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62 ** another datab
129b2 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a ase connection..
129b3 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 **.** The pointe
129b4 72 73 20 61 72 65 20 6b 65 70 74 20 69 6e 20 73 rs are kept in s
129b5 6f 72 74 65 64 20 6f 72 64 65 72 20 62 79 20 70 orted order by p
129b6 42 74 72 65 65 2d 3e 70 42 74 2e 20 20 54 68 61 Btree->pBt. Tha
129b7 74 0a 2a 2a 20 77 61 79 20 77 68 65 6e 20 77 65 t.** way when we
129b8 20 67 6f 20 74 6f 20 65 6e 74 65 72 20 61 6c 6c go to enter all
129b9 20 74 68 65 20 6d 75 74 65 78 65 73 2c 20 77 65 the mutexes, we
129ba 20 63 61 6e 20 65 6e 74 65 72 20 74 68 65 6d 0a can enter them.
129bb 2a 2a 20 69 6e 20 6f 72 64 65 72 20 77 69 74 68 ** in order with
129bc 6f 75 74 20 65 76 65 72 79 20 68 61 76 69 6e 67 out every having
129bd 20 74 6f 20 62 61 63 6b 75 70 20 61 6e 64 20 72 to backup and r
129be 65 74 72 79 20 61 6e 64 20 77 69 74 68 6f 75 74 etry and without
129bf 0a 2a 2a 20 77 6f 72 72 79 69 6e 67 20 61 62 6f .** worrying abo
129c0 75 74 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a ut deadlock..**.
129c1 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** The number of
129c2 20 73 68 61 72 65 64 20 62 74 72 65 65 73 20 77 shared btrees w
129c3 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 73 6d ill always be sm
129c4 61 6c 6c 20 28 75 73 75 61 6c 6c 79 20 30 20 6f all (usually 0 o
129c5 72 20 31 29 0a 2a 2a 20 73 6f 20 61 6e 20 69 6e r 1).** so an in
129c6 73 65 72 74 69 6f 6e 20 73 6f 72 74 20 69 73 20 sertion sort is
129c7 61 6e 20 61 64 65 71 75 61 74 65 20 61 6c 67 6f an adequate algo
129c8 72 69 74 68 6d 20 68 65 72 65 2e 0a 2a 2f 0a 53 rithm here..*/.S
129c9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
129ca 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
129cb 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 utexArrayInsert(
129cc 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 BtreeMutexArray
129cd 2a 70 41 72 72 61 79 2c 20 42 74 72 65 65 20 2a *pArray, Btree *
129ce 70 42 74 72 65 65 29 7b 0a 20 20 69 6e 74 20 69 pBtree){. int i
129cf 2c 20 6a 3b 0a 20 20 42 74 53 68 61 72 65 64 20 , j;. BtShared
129d0 2a 70 42 74 3b 0a 20 20 69 66 28 20 70 42 74 72 *pBt;. if( pBtr
129d1 65 65 3d 3d 30 20 7c 7c 20 70 42 74 72 65 65 2d ee==0 || pBtree-
129d2 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 29 20 72 >sharable==0 ) r
129d3 65 74 75 72 6e 3b 0a 23 69 66 6e 64 65 66 20 4e eturn;.#ifndef N
129d4 44 45 42 55 47 0a 20 20 7b 0a 20 20 20 20 66 6f DEBUG. {. fo
129d5 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d r(i=0; i<pArray-
129d6 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 >nMutex; i++){.
129d7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 41 72 assert( pAr
129d8 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d ray->aBtree[i]!=
129d9 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 7d 0a pBtree );. }.
129da 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 }.#endif. ass
129db 65 72 74 28 20 70 41 72 72 61 79 2d 3e 6e 4d 75 ert( pArray->nMu
129dc 74 65 78 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 tex>=0 );. asse
129dd 72 74 28 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 rt( pArray->nMut
129de 65 78 3c 41 72 72 61 79 53 69 7a 65 28 70 41 72 ex<ArraySize(pAr
129df 72 61 79 2d 3e 61 42 74 72 65 65 29 2d 31 20 29 ray->aBtree)-1 )
129e0 3b 0a 20 20 70 42 74 20 3d 20 70 42 74 72 65 65 ;. pBt = pBtree
129e1 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 28 69 3d 30 ->pBt;. for(i=0
129e2 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 ; i<pArray->nMut
129e3 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 ex; i++){. as
129e4 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61 42 sert( pArray->aB
129e5 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 tree[i]!=pBtree
129e6 29 3b 0a 20 20 20 20 69 66 28 20 70 41 72 72 61 );. if( pArra
129e7 79 2d 3e 61 42 74 72 65 65 5b 69 5d 2d 3e 70 42 y->aBtree[i]->pB
129e8 74 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 66 t>pBt ){. f
129e9 6f 72 28 6a 3d 70 41 72 72 61 79 2d 3e 6e 4d 75 or(j=pArray->nMu
129ea 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d 2d 29 7b 0a tex; j>i; j--){.
129eb 20 20 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e pArray->
129ec 61 42 74 72 65 65 5b 6a 5d 20 3d 20 70 41 72 72 aBtree[j] = pArr
129ed 61 79 2d 3e 61 42 74 72 65 65 5b 6a 2d 31 5d 3b ay->aBtree[j-1];
129ee 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
129ef 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d Array->aBtree[i]
129f0 20 3d 20 70 42 74 72 65 65 3b 0a 20 20 20 20 20 = pBtree;.
129f1 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b pArray->nMutex+
129f2 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b +;. return;
129f3 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 41 72 . }. }. pAr
129f4 72 61 79 2d 3e 61 42 74 72 65 65 5b 70 41 72 72 ray->aBtree[pArr
129f5 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 5d 20 3d 20 ay->nMutex++] =
129f6 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pBtree;.}../*.**
129f7 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 Enter the mutex
129f8 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 65 20 of every btree
129f9 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20 54 in the array. T
129fa 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a his routine is.*
129fb 2a 20 63 61 6c 6c 65 64 20 61 74 20 74 68 65 20 * called at the
129fc 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 73 71 6c beginning of sql
129fd 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 ite3VdbeExec().
129fe 20 54 68 65 20 6d 75 74 65 78 65 73 20 61 72 65 The mutexes are
129ff 0a 2a 2a 20 65 78 69 74 65 64 20 61 74 20 74 68 .** exited at th
12a00 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 61 6d e end of the sam
12a01 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 e function..*/.S
12a02 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
12a03 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
12a04 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 42 utexArrayEnter(B
12a05 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a treeMutexArray *
12a06 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69 pArray){. int i
12a07 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
12a08 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 Array->nMutex; i
12a09 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a ++){. Btree *
12a0a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 p = pArray->aBtr
12a0b 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f ee[i];. /* So
12a0c 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20 me basic sanity
12a0d 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 checking */.
12a0e 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20 assert( i==0 ||
12a0f 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 pArray->aBtree[i
12a10 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 -1]->pBt<p->pBt
12a11 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
12a12 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d 3e p->locked || p->
12a13 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a wantToLock>0 );.
12a14 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c . /* We shoul
12a15 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 d already hold a
12a16 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
12a17 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
12a18 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
12a19 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
12a1a 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
12a1b 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 );.. p->want
12a1c 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 69 66 ToLock++;. if
12a1d 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 ( !p->locked &&
12a1e 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 p->sharable ){.
12a1f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
12a20 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 2d ex_enter(p->pBt-
12a21 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 >mutex);. p
12a22 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 ->locked = 1;.
12a23 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
12a24 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74 65 78 Leave the mutex
12a25 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 65 20 of every btree
12a26 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f in the group..*/
12a27 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12a28 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
12a29 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 eMutexArrayLeave
12a2a 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 (BtreeMutexArray
12a2b 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 *pArray){. int
12a2c 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 i;. for(i=0; i
12a2d 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b <pArray->nMutex;
12a2e 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 i++){. Btree
12a2f 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 *p = pArray->aB
12a30 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 tree[i];. /*
12a31 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 Some basic sanit
12a32 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 y checking */.
12a33 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c assert( i==0 |
12a34 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 | pArray->aBtree
12a35 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 [i-1]->pBt<p->pB
12a36 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
12a37 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 21 70 p->locked || !p
12a38 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 ->sharable );.
12a39 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e assert( p->wan
12a3a 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 tToLock>0 );..
12a3b 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 /* We should a
12a3c 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f lready hold a lo
12a3d 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
12a3e 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
12a3f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
12a40 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
12a41 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
12a42 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c .. p->wantToL
12a43 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 ock--;. if( p
12a44 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 ->wantToLock==0
12a45 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a && p->locked ){.
12a46 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
12a47 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 tex_leave(p->pBt
12a48 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
12a49 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 p->locked = 0;.
12a4a 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 23 65 6e }. }.}...#en
12a4b 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 54 dif /* SQLITE_T
12a4c 48 52 45 41 44 53 41 46 45 20 26 26 20 21 53 51 HREADSAFE && !SQ
12a4d 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
12a4e 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a _CACHE */../****
12a4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
12a50 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a f btmutex.c ****
12a51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12a53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
12a54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
12a55 20 66 69 6c 65 20 62 74 72 65 65 2e 63 20 2a 2a file btree.c **
12a56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12a57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12a58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
12a59 20 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 2004 April 6.**
12a5a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
12a5b 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
12a5c 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
12a5d 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
12a5e 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
12a5f 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
12a60 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
12a61 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
12a62 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
12a63 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
12a64 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
12a65 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
12a66 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
12a67 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
12a68 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
12a69 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
12a6a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
12a6b 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
12a6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12a6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12a6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12a6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
12a70 24 49 64 3a 20 62 74 72 65 65 2e 63 2c 76 20 31 $Id: btree.c,v 1
12a71 2e 35 33 39 2e 32 2e 31 20 32 30 30 38 2f 31 31 .539.2.1 2008/11
12a72 2f 32 32 20 31 34 3a 30 37 3a 34 39 20 64 72 68 /22 14:07:49 drh
12a73 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 Exp $.**.** Thi
12a74 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
12a75 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 s a external (di
12a76 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 sk-based) databa
12a77 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e se using BTrees.
12a78 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 61 64 .** See the head
12a79 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 er comment on "b
12a7a 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 treeInt.h" for a
12a7b 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
12a7c 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 ation..** Includ
12a7d 69 6e 67 20 61 20 64 65 73 63 72 69 70 74 69 6f ing a descriptio
12a7e 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 n of file format
12a7f 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77 and an overview
12a80 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a of operation..*
12a81 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 /../*.** The hea
12a82 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 der string that
12a83 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 appears at the b
12a84 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 eginning of ever
12a85 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 y.** SQLite data
12a86 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 base..*/.static
12a87 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 const char zMagi
12a88 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 cHeader[] = SQLI
12a89 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a TE_FILE_HEADER;.
12a8a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 ./*.** Set this
12a8b 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
12a8c 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 to 1 to enable t
12a8d 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 racing using the
12a8e 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e TRACE.** macro.
12a8f 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 .*/.#if 0.int sq
12a90 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65 3d lite3BtreeTrace=
12a91 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 0; /* True to e
12a92 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f nable tracing */
12a93 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 .# define TRACE(
12a94 58 29 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 X) if(sqlite3Bt
12a95 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 reeTrace){printf
12a96 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 X;fflush(stdout
12a97 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 );}.#else.# defi
12a98 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 ne TRACE(X).#end
12a99 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 if../*.** Someti
12a9a 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d mes we need a sm
12a9b 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f all amount of co
12a9c 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61 72 de such as a var
12a9d 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 iable initializa
12a9e 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70 tion.** to setup
12a9f 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73 for a later ass
12aa0 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e ert() statement.
12aa1 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 We do not want
12aa2 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a this code to.**
12aa3 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 73 appear when ass
12aa4 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65 ert() is disable
12aa5 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e d. The followin
12aa6 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65 g macro is there
12aa7 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 fore.** used to
12aa8 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74 contain that set
12aa9 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56 up code. The "V
12aaa 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e VA" acronym stan
12aab 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66 ds for.** "Verif
12aac 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74 ication, Validat
12aad 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69 ion, and Accredi
12aae 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68 tation". In oth
12aaf 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a er words, the.**
12ab0 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41 code within VVA
12ab1 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c _ONLY() will onl
12ab2 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72 y run during ver
12ab3 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73 ification proces
12ab4 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ses..*/.#ifndef
12ab5 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20 NDEBUG.# define
12ab6 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23 VVA_ONLY(X) X.#
12ab7 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56 else.# define VV
12ab8 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 A_ONLY(X).#endif
12ab9 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ....#ifndef SQLI
12aba 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
12abb 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 ACHE./*.** A lis
12abc 74 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62 t of BtShared ob
12abd 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65 jects that are e
12abe 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74 ligible for part
12abf 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 icipation.** in
12ac0 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54 shared cache. T
12ac1 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 his variable has
12ac2 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 file scope duri
12ac3 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 ng normal builds
12ac4 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 ,.** but the tes
12ac5 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 t harness needs
12ac6 74 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20 to access it so
12ac7 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61 we make it globa
12ac8 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62 l for .** test b
12ac9 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 uilds..*/.#ifdef
12aca 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
12acb 49 54 45 5f 50 52 49 56 41 54 45 20 42 74 53 68 ITE_PRIVATE BtSh
12acc 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 ared *SQLITE_WSD
12acd 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 sqlite3SharedCa
12ace 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6c cheList = 0;.#el
12acf 73 65 0a 73 74 61 74 69 63 20 42 74 53 68 61 72 se.static BtShar
12ad0 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 ed *SQLITE_WSD s
12ad1 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 qlite3SharedCach
12ad2 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6e 64 69 eList = 0;.#endi
12ad3 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 f.#endif /* SQLI
12ad4 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
12ad5 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 ACHE */..#ifndef
12ad6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
12ad7 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 RED_CACHE./*.**
12ad8 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c Enable or disabl
12ad9 65 20 74 68 65 20 73 68 61 72 65 64 20 70 61 67 e the shared pag
12ada 65 72 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65 er and schema fe
12adb 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 atures..**.** Th
12adc 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e is routine has n
12add 6f 20 65 66 66 65 63 74 20 6f 6e 20 65 78 69 73 o effect on exis
12ade 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f ting database co
12adf 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 nnections..** Th
12ae0 65 20 73 68 61 72 65 64 20 63 61 63 68 65 20 73 e shared cache s
12ae1 65 74 74 69 6e 67 20 65 66 66 65 63 74 73 20 6f etting effects o
12ae2 6e 6c 79 20 66 75 74 75 72 65 20 63 61 6c 6c 73 nly future calls
12ae3 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f to.** sqlite3_o
12ae4 70 65 6e 28 29 2c 20 73 71 6c 69 74 65 33 5f 6f pen(), sqlite3_o
12ae5 70 65 6e 31 36 28 29 2c 20 6f 72 20 73 71 6c 69 pen16(), or sqli
12ae6 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a te3_open_v2()..*
12ae7 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
12ae8 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f sqlite3_enable_
12ae9 73 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 shared_cache(int
12aea 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69 enable){. sqli
12aeb 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
12aec 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c sharedCacheEnabl
12aed 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 ed = enable;. r
12aee 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
12aef 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a .}.#endif.../*.*
12af0 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 * Forward declar
12af1 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 ation.*/.static
12af2 69 6e 74 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 int checkReadLoc
12af3 6b 73 28 42 74 72 65 65 2a 2c 20 50 67 6e 6f 2c ks(Btree*, Pgno,
12af4 20 42 74 43 75 72 73 6f 72 2a 2c 20 69 36 34 29 BtCursor*, i64)
12af5 3b 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ;...#ifdef SQLIT
12af6 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
12af7 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 CHE. /*. ** Th
12af8 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 e functions quer
12af9 79 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 6c 6f yTableLock(), lo
12afa 63 6b 54 61 62 6c 65 28 29 20 61 6e 64 20 75 6e ckTable() and un
12afb 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 29 0a lockAllTables().
12afc 20 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 ** manipulate
12afd 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 42 entries in the B
12afe 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 tShared.pLock li
12aff 6e 6b 65 64 20 6c 69 73 74 20 75 73 65 64 20 74 nked list used t
12b00 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 o store. ** sha
12b01 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20 red-cache table
12b02 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 level locks. If
12b03 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 the library is c
12b04 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
12b05 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 . ** shared-cac
12b06 68 65 20 66 65 61 74 75 72 65 20 64 69 73 61 62 he feature disab
12b07 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 led, then there
12b08 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 is only ever one
12b09 20 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 user. ** of ea
12b0a 63 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75 ch BtShared stru
12b0b 63 74 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69 cture and so thi
12b0c 73 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 s locking is not
12b0d 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a necessary. . *
12b0e 2a 20 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20 * So define the
12b0f 6c 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e lock related fun
12b10 63 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 ctions as no-ops
12b11 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 .. */. #define
12b12 20 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 queryTableLock(
12b13 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b a,b,c) SQLITE_OK
12b14 0a 20 20 23 64 65 66 69 6e 65 20 6c 6f 63 6b 54 . #define lockT
12b15 61 62 6c 65 28 61 2c 62 2c 63 29 20 53 51 4c 49 able(a,b,c) SQLI
12b16 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 TE_OK. #define
12b17 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 unlockAllTables(
12b18 61 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 a).#endif..#ifnd
12b19 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
12b1a 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a HARED_CACHE./*.*
12b1b 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 * Query to see i
12b1c 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 70 f btree handle p
12b1d 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f may obtain a lo
12b1e 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b ck of type eLock
12b1f 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 .** (READ_LOCK
12b20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f or WRITE_LOCK) o
12b21 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 n the table with
12b22 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e root-page iTab.
12b23 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 Return.** SQLIT
12b24 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b E_OK if the lock
12b25 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 may be obtained
12b26 20 28 62 79 20 63 61 6c 6c 69 6e 67 20 6c 6f 63 (by calling loc
12b27 6b 54 61 62 6c 65 28 29 29 2c 20 6f 72 0a 2a 2a kTable()), or.**
12b28 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 SQLITE_LOCKED i
12b29 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 f not..*/.static
12b2a 20 69 6e 74 20 71 75 65 72 79 54 61 62 6c 65 4c int queryTableL
12b2b 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 ock(Btree *p, Pg
12b2c 6e 6f 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 no iTab, u8 eLoc
12b2d 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a k){. BtShared *
12b2e 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
12b2f 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a BtLock *pIter;..
12b30 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
12b31 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 3BtreeHoldsMutex
12b32 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 (p) );. assert(
12b33 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 eLock==READ_LOC
12b34 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 K || eLock==WRIT
12b35 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 E_LOCK );. asse
12b36 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a rt( p->db!=0 );.
12b37 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 . /* This is
12b38 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 a no-op if the s
12b39 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 6e hared-cache is n
12b3a 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 ot enabled */.
12b3b 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 if( !p->sharable
12b3c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
12b3d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
12b3e 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 /* If some othe
12b3f 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 r connection is
12b40 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 holding an exclu
12b41 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 sive lock, the.
12b42 20 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c 6f ** requested lo
12b43 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 ck may not be ob
12b44 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 tained.. */. i
12b45 66 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 f( pBt->pExclusi
12b46 76 65 20 26 26 20 70 42 74 2d 3e 70 45 78 63 6c ve && pBt->pExcl
12b47 75 73 69 76 65 21 3d 70 20 29 7b 0a 20 20 20 20 usive!=p ){.
12b48 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f return SQLITE_LO
12b49 43 4b 45 44 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 CKED;. }.. /*
12b4a 54 68 69 73 20 28 61 6c 6f 6e 67 20 77 69 74 68 This (along with
12b4b 20 6c 6f 63 6b 54 61 62 6c 65 28 29 29 20 69 73 lockTable()) is
12b4c 20 77 68 65 72 65 20 74 68 65 20 52 65 61 64 55 where the ReadU
12b4d 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 ncommitted flag
12b4e 69 73 0a 20 20 2a 2a 20 64 65 61 6c 74 20 77 69 is. ** dealt wi
12b4f 74 68 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 th. If the calle
12b50 72 20 69 73 20 71 75 65 72 79 69 6e 67 20 66 6f r is querying fo
12b51 72 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 61 6e r a read-lock an
12b52 64 20 74 68 65 20 66 6c 61 67 20 69 73 0a 20 20 d the flag is.
12b53 2a 2a 20 73 65 74 2c 20 69 74 20 69 73 20 75 6e ** set, it is un
12b54 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 67 72 conditionally gr
12b55 61 6e 74 65 64 20 2d 20 65 76 65 6e 20 69 66 20 anted - even if
12b56 74 68 65 72 65 20 61 72 65 20 77 72 69 74 65 2d there are write-
12b57 6c 6f 63 6b 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 locks. ** on th
12b58 65 20 74 61 62 6c 65 2e 20 49 66 20 61 20 77 72 e table. If a wr
12b59 69 74 65 2d 6c 6f 63 6b 20 69 73 20 72 65 71 75 ite-lock is requ
12b5a 65 73 74 65 64 2c 20 74 68 65 20 52 65 61 64 55 ested, the ReadU
12b5b 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 0a ncommitted flag.
12b5c 20 20 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 73 ** is not cons
12b5d 69 64 65 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a idered.. **. *
12b5e 2a 20 49 6e 20 66 75 6e 63 74 69 6f 6e 20 6c 6f * In function lo
12b5f 63 6b 54 61 62 6c 65 28 29 2c 20 69 66 20 61 20 ckTable(), if a
12b60 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 64 65 6d read-lock is dem
12b61 61 6e 64 65 64 20 61 6e 64 20 74 68 65 20 0a 20 anded and the .
12b62 20 2a 2a 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 ** ReadUncommit
12b63 74 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 2c ted flag is set,
12b64 20 6e 6f 20 65 6e 74 72 79 20 69 73 20 61 64 64 no entry is add
12b65 65 64 20 74 6f 20 74 68 65 20 6c 6f 63 6b 73 20 ed to the locks
12b66 6c 69 73 74 20 0a 20 20 2a 2a 20 28 42 74 53 68 list . ** (BtSh
12b67 61 72 65 64 2e 70 4c 6f 63 6b 29 2e 0a 20 20 2a ared.pLock).. *
12b68 2a 0a 20 20 2a 2a 20 54 6f 20 73 75 6d 6d 61 72 *. ** To summar
12b69 69 7a 65 3a 20 49 66 20 74 68 65 20 52 65 61 64 ize: If the Read
12b6a 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 Uncommitted flag
12b6b 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 72 65 is set, then re
12b6c 61 64 20 63 75 72 73 6f 72 73 20 64 6f 0a 20 20 ad cursors do.
12b6d 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 6f 72 ** not create or
12b6e 20 72 65 73 70 65 63 74 20 74 61 62 6c 65 20 6c respect table l
12b6f 6f 63 6b 73 2e 20 54 68 65 20 6c 6f 63 6b 69 6e ocks. The lockin
12b70 67 20 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20 g procedure for
12b71 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 2d 63 75 a . ** write-cu
12b72 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 63 68 rsor does not ch
12b73 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ange.. */. if(
12b74 20 0a 20 20 20 20 30 3d 3d 28 70 2d 3e 64 62 2d . 0==(p->db-
12b75 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 >flags&SQLITE_Re
12b76 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c adUncommitted) |
12b77 7c 20 0a 20 20 20 20 65 4c 6f 63 6b 3d 3d 57 52 | . eLock==WR
12b78 49 54 45 5f 4c 4f 43 4b 20 7c 7c 0a 20 20 20 20 ITE_LOCK ||.
12b79 69 54 61 62 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f iTab==MASTER_ROO
12b7a 54 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72 28 70 T. ){. for(p
12b7b 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b Iter=pBt->pLock;
12b7c 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 pIter; pIter=pI
12b7d 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
12b7e 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 if( pIter->pB
12b7f 74 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 tree!=p && pIter
12b80 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 ->iTable==iTab &
12b81 26 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 49 & . (pI
12b82 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 ter->eLock!=eLoc
12b83 6b 20 7c 7c 20 65 4c 6f 63 6b 21 3d 52 45 41 44 k || eLock!=READ
12b84 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 20 20 _LOCK) ){.
12b85 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12b86 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a LOCKED;. }.
12b87 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
12b88 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
12b89 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 #endif /* !SQLIT
12b8a 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
12b8b 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 CHE */..#ifndef
12b8c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
12b8d 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 ED_CACHE./*.** A
12b8e 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 dd a lock on the
12b8f 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 table with root
12b90 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 -page iTable to
12b91 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 the shared-btree
12b92 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 used.** by Btre
12b93 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 e handle p. Para
12b94 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 meter eLock must
12b95 20 62 65 20 65 69 74 68 65 72 20 52 45 41 44 5f be either READ_
12b96 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 LOCK or .** WRIT
12b97 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 53 51 E_LOCK..**.** SQ
12b98 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
12b99 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 ned if the lock
12b9a 69 73 20 61 64 64 65 64 20 73 75 63 63 65 73 73 is added success
12b9b 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 42 55 fully. SQLITE_BU
12b9c 53 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 SY and.** SQLITE
12b9d 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73 6f 20 _NOMEM may also
12b9e 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a be returned..*/.
12b9f 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 static int lockT
12ba0 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 50 able(Btree *p, P
12ba1 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 gno iTable, u8 e
12ba2 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 Lock){. BtShare
12ba3 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
12ba4 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b . BtLock *pLock
12ba5 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a = 0;. BtLock *
12ba6 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 pIter;.. assert
12ba7 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
12ba8 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
12ba9 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
12baa 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f READ_LOCK || eLo
12bab 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 ck==WRITE_LOCK )
12bac 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 ;. assert( p->d
12bad 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 b!=0 );.. /* Th
12bae 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 is is a no-op if
12baf 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 the shared-cach
12bb0 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 e is not enabled
12bb1 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 */. if( !p->sh
12bb2 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65 arable ){. re
12bb3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
12bb4 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 53 }.. assert( S
12bb5 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 54 QLITE_OK==queryT
12bb6 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 ableLock(p, iTab
12bb7 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 le, eLock) );..
12bb8 20 2f 2a 20 49 66 20 74 68 65 20 72 65 61 64 2d /* If the read-
12bb9 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 uncommitted flag
12bba 20 69 73 20 73 65 74 20 61 6e 64 20 61 20 72 65 is set and a re
12bbb 61 64 2d 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 ad-lock is reque
12bbc 73 74 65 64 2c 0a 20 20 2a 2a 20 72 65 74 75 72 sted,. ** retur
12bbd 6e 20 65 61 72 6c 79 20 77 69 74 68 6f 75 74 20 n early without
12bbe 61 64 64 69 6e 67 20 61 6e 20 65 6e 74 72 79 20 adding an entry
12bbf 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 2e to the BtShared.
12bc0 70 4c 6f 63 6b 20 6c 69 73 74 2e 20 53 65 65 0a pLock list. See.
12bc1 20 20 2a 2a 20 63 6f 6d 6d 65 6e 74 20 69 6e 20 ** comment in
12bc2 66 75 6e 63 74 69 6f 6e 20 71 75 65 72 79 54 61 function queryTa
12bc3 62 6c 65 4c 6f 63 6b 28 29 20 66 6f 72 20 6d 6f bleLock() for mo
12bc4 72 65 20 69 6e 66 6f 20 6f 6e 20 68 61 6e 64 6c re info on handl
12bc5 69 6e 67 20 0a 20 20 2a 2a 20 74 68 65 20 52 65 ing . ** the Re
12bc6 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c adUncommitted fl
12bc7 61 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 0a ag.. */. if( .
12bc8 20 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 (p->db->flag
12bc9 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 s&SQLITE_ReadUnc
12bca 6f 6d 6d 69 74 74 65 64 29 20 26 26 20 0a 20 20 ommitted) && .
12bcb 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c (eLock==READ_L
12bcc 4f 43 4b 29 20 26 26 0a 20 20 20 20 69 54 61 62 OCK) &&. iTab
12bcd 6c 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a le!=MASTER_ROOT.
12bce 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ){. return
12bcf 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
12bd0 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 /* First searc
12bd1 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 h the list for a
12bd2 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 n existing lock
12bd3 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a on this table. *
12bd4 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 /. for(pIter=pB
12bd5 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b t->pLock; pIter;
12bd6 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e pIter=pIter->pN
12bd7 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 ext){. if( pI
12bd8 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 ter->iTable==iTa
12bd9 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 ble && pIter->pB
12bda 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 tree==p ){.
12bdb 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a pLock = pIter;.
12bdc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
12bdd 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
12bde 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 the above search
12bdf 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 did not find a
12be0 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 BtLock struct as
12be1 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 sociating Btree
12be2 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c p. ** with tabl
12be3 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 e iTable, alloca
12be4 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 te one and link
12be5 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 it into the list
12be6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c .. */. if( !pL
12be7 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b ock ){. pLock
12be8 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c = (BtLock *)sql
12be9 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 ite3MallocZero(s
12bea 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a izeof(BtLock));.
12beb 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 if( !pLock )
12bec 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
12bed 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
12bee 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 }. pLock->iT
12bef 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 able = iTable;.
12bf0 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 pLock->pBtree
12bf1 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d = p;. pLock-
12bf2 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c >pNext = pBt->pL
12bf3 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c ock;. pBt->pL
12bf4 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d ock = pLock;. }
12bf5 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 .. /* Set the B
12bf6 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 tLock.eLock vari
12bf7 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 able to the maxi
12bf8 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 mum of the curre
12bf9 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 nt lock. ** and
12bfa 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
12bfb 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 ock. This means
12bfc 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 if a write-lock
12bfd 77 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 was already held
12bfe 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 . ** and a read
12bff 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c -lock requested,
12c00 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 we don't incorr
12c01 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 ectly downgrade
12c02 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 the lock.. */.
12c03 20 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c assert( WRITE_L
12c04 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b OCK>READ_LOCK );
12c05 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f . if( eLock>pLo
12c06 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 ck->eLock ){.
12c07 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 pLock->eLock =
12c08 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 eLock;. }.. re
12c09 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
12c0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
12c0b 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
12c0c 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
12c0d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
12c0e 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
12c0f 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 Release all the
12c10 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f table locks (lo
12c11 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 cks obtained via
12c12 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 6c 6f calls to the lo
12c13 63 6b 54 61 62 6c 65 28 29 0a 2a 2a 20 70 72 6f ckTable().** pro
12c14 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 cedure) held by
12c15 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 0a Btree handle p..
12c16 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
12c17 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 42 nlockAllTables(B
12c18 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 tree *p){. BtSh
12c19 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
12c1a 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 Bt;. BtLock **p
12c1b 70 49 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c pIter = &pBt->pL
12c1c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ock;.. assert(
12c1d 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
12c1e 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 sMutex(p) );. a
12c1f 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 ssert( p->sharab
12c20 6c 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 le || 0==*ppIter
12c21 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 70 );.. while( *p
12c22 70 49 74 65 72 20 29 7b 0a 20 20 20 20 42 74 4c pIter ){. BtL
12c23 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 70 ock *pLock = *pp
12c24 49 74 65 72 3b 0a 20 20 20 20 61 73 73 65 72 74 Iter;. assert
12c25 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76 ( pBt->pExclusiv
12c26 65 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 45 78 e==0 || pBt->pEx
12c27 63 6c 75 73 69 76 65 3d 3d 70 4c 6f 63 6b 2d 3e clusive==pLock->
12c28 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 66 pBtree );. if
12c29 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d ( pLock->pBtree=
12c2a 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 49 =p ){. *ppI
12c2b 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 ter = pLock->pNe
12c2c 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 xt;. sqlite
12c2d 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 3_free(pLock);.
12c2e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
12c2f 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 6b 2d ppIter = &pLock-
12c30 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
12c31 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 45 }.. if( pBt->pE
12c32 78 63 6c 75 73 69 76 65 3d 3d 70 20 29 7b 0a 20 xclusive==p ){.
12c33 20 20 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 pBt->pExclusi
12c34 76 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 ve = 0;. }.}.#e
12c35 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
12c36 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
12c37 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 */..static void
12c38 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d releasePage(Mem
12c39 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 2f Page *pPage); /
12c3a 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 * Forward refere
12c3b 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 65 nce */../*.** Ve
12c3c 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63 75 rify that the cu
12c3d 72 73 6f 72 20 68 6f 6c 64 73 20 61 20 6d 75 74 rsor holds a mut
12c3e 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 ex on the BtShar
12c3f 65 64 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 ed.*/.#ifndef ND
12c40 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20 EBUG.static int
12c41 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
12c42 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 (BtCursor *p){.
12c43 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f return sqlite3_
12c44 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 mutex_held(p->pB
12c45 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e t->mutex);.}.#en
12c46 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 dif...#ifndef SQ
12c47 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
12c48 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 OB./*.** Invalid
12c49 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 ate the overflow
12c4a 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 page-list cache
12c4b 20 66 6f 72 20 63 75 72 73 6f 72 20 70 43 75 72 for cursor pCur
12c4c 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74 61 , if any..*/.sta
12c4d 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 tic void invalid
12c4e 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 ateOverflowCache
12c4f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
12c50 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 {. assert( curs
12c51 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
12c52 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f r) );. sqlite3_
12c53 66 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 65 72 free(pCur->aOver
12c54 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 61 flow);. pCur->a
12c55 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 7d 0a Overflow = 0;.}.
12c56 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 ./*.** Invalidat
12c57 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 e the overflow p
12c58 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 66 age-list cache f
12c59 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f or all cursors o
12c5a 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 pened.** on the
12c5b 73 68 61 72 65 64 20 62 74 72 65 65 20 73 74 72 shared btree str
12c5c 75 63 74 75 72 65 20 70 42 74 2e 0a 2a 2f 0a 73 ucture pBt..*/.s
12c5d 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c tatic void inval
12c5e 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 idateAllOverflow
12c5f 43 61 63 68 65 28 42 74 53 68 61 72 65 64 20 2a Cache(BtShared *
12c60 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 pBt){. BtCursor
12c61 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 *p;. assert( s
12c62 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
12c63 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
12c64 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 . for(p=pBt->pC
12c65 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
12c66 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 76 61 6c Next){. inval
12c67 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 idateOverflowCac
12c68 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c he(p);. }.}.#el
12c69 73 65 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 se. #define inv
12c6a 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 alidateOverflowC
12c6b 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e ache(x). #defin
12c6c 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f e invalidateAllO
12c6d 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a verflowCache(x).
12c6e 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 61 #endif../*.** Sa
12c6f 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 ve the current c
12c70 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 ursor position i
12c71 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 n the variables
12c72 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a BtCursor.nKey .*
12c73 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 * and BtCursor.p
12c74 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 Key. The cursor'
12c75 73 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 s state is set t
12c76 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 o CURSOR_REQUIRE
12c77 53 45 45 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 SEEK..*/.static
12c78 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 6f int saveCursorPo
12c79 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 sition(BtCursor
12c7a 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 *pCur){. int rc
12c7b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 ;.. assert( CUR
12c7c 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d SOR_VALID==pCur-
12c7d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 73 >eState );. ass
12c7e 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b ert( 0==pCur->pK
12c7f 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ey );. assert(
12c80 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
12c81 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 20 (pCur) );.. rc
12c82 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 = sqlite3BtreeKe
12c83 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 75 ySize(pCur, &pCu
12c84 72 2d 3e 6e 4b 65 79 29 3b 0a 0a 20 20 2f 2a 20 r->nKey);.. /*
12c85 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e If this is an in
12c86 74 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e tKey table, then
12c87 20 74 68 65 20 61 62 6f 76 65 20 63 61 6c 6c 20 the above call
12c88 74 6f 20 42 74 72 65 65 4b 65 79 53 69 7a 65 28 to BtreeKeySize(
12c89 29 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 ). ** stores th
12c8a 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 69 6e e integer key in
12c8b 20 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 pCur->nKey. In
12c8c 74 68 69 73 20 63 61 73 65 20 74 68 69 73 20 76 this case this v
12c8d 61 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c alue is. ** all
12c8e 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 that is require
12c8f 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 d. Otherwise, if
12c90 20 70 43 75 72 20 69 73 20 6e 6f 74 20 6f 70 65 pCur is not ope
12c91 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20 n on an intKey.
12c92 20 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 ** table, then
12c93 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f 72 malloc space for
12c94 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 70 and store the p
12c95 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20 Cur->nKey bytes
12c96 6f 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74 of key . ** dat
12c97 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 a.. */. if( rc
12c98 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 ==SQLITE_OK && 0
12c99 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 ==pCur->apPage[0
12c9a 5d 2d 3e 69 6e 74 4b 65 79 29 7b 0a 20 20 20 20 ]->intKey){.
12c9b 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c void *pKey = sql
12c9c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 43 75 72 2d ite3Malloc(pCur-
12c9d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 >nKey);. if(
12c9e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 pKey ){. rc
12c9f 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
12ca0 65 79 28 70 43 75 72 2c 20 30 2c 20 70 43 75 72 ey(pCur, 0, pCur
12ca1 2d 3e 6e 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20 ->nKey, pKey);.
12ca2 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
12ca3 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12ca4 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 pCur->pKey = p
12ca5 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 Key;. }else
12ca6 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
12ca7 33 5f 66 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 3_free(pKey);.
12ca8 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
12ca9 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
12caa 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
12cab 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 }. assert( !p
12cac 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e Cur->apPage[0]->
12cad 69 6e 74 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d intKey || !pCur-
12cae 3e 70 4b 65 79 20 29 3b 0a 0a 20 20 69 66 28 20 >pKey );.. if(
12caf 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
12cb0 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
12cb1 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 for(i=0; i<=pCur
12cb2 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 ->iPage; i++){.
12cb3 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
12cb4 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d (pCur->apPage[i]
12cb5 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 );. pCur->a
12cb6 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 pPage[i] = 0;.
12cb7 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 }. pCur->iP
12cb8 61 67 65 20 3d 20 2d 31 3b 0a 20 20 20 20 70 43 age = -1;. pC
12cb9 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
12cba 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b SOR_REQUIRESEEK;
12cbb 0a 20 20 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 . }.. invalida
12cbc 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 teOverflowCache(
12cbd 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 pCur);. return
12cbe 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 rc;.}../*.** Sav
12cbf 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 e the positions
12cc0 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 of all cursors e
12cc1 78 63 65 70 74 20 70 45 78 63 65 70 74 20 6f 70 xcept pExcept op
12cc2 65 6e 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 en on the table
12cc3 0a 2a 2a 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 .** with root-pa
12cc4 67 65 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c ge iRoot. Usuall
12cc5 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 y, this is calle
12cc6 64 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63 75 d just before cu
12cc7 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 20 rsor.** pExcept
12cc8 69 73 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 is used to modif
12cc9 79 20 74 68 65 20 74 61 62 6c 65 20 28 42 74 72 y the table (Btr
12cca 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 eeDelete() or Bt
12ccb 72 65 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f reeInsert())..*/
12ccc 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 .static int save
12ccd 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 AllCursors(BtSha
12cce 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 red *pBt, Pgno i
12ccf 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a Root, BtCursor *
12cd0 70 45 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 pExcept){. BtCu
12cd1 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 rsor *p;. asser
12cd2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
12cd3 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
12cd4 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
12cd5 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 Except==0 || pEx
12cd6 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 cept->pBt==pBt )
12cd7 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 ;. for(p=pBt->p
12cd8 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e Cursor; p; p=p->
12cd9 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 pNext){. if(
12cda 70 21 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 p!=pExcept && (0
12cdb 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 ==iRoot || p->pg
12cdc 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 noRoot==iRoot) &
12cdd 26 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 & . p->eS
12cde 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
12cdf 49 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 ID ){. int
12ce0 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 rc = saveCursorP
12ce1 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 osition(p);.
12ce2 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
12ce3 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 =rc ){. r
12ce4 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
12ce5 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
12ce6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
12ce7 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 }../*.** Clear t
12ce8 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f he current curso
12ce9 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53 r position..*/.S
12cea 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
12ceb 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 id sqlite3BtreeC
12cec 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 learCursor(BtCur
12ced 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 sor *pCur){. as
12cee 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
12cef 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
12cf0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
12cf1 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 Cur->pKey);. pC
12cf2 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 ur->pKey = 0;.
12cf3 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
12cf4 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d URSOR_INVALID;.}
12cf5 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 ../*.** Restore
12cf6 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
12cf7 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 e position it wa
12cf8 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 s in (or as clos
12cf9 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 e to as possible
12cfa 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 ).** when saveCu
12cfb 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 rsorPosition() w
12cfc 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 as called. Note
12cfd 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 that this call d
12cfe 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 eletes the .** s
12cff 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e aved position in
12d00 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 fo stored by sav
12d01 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
12d02 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 ), so there can
12d03 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e be.** at most on
12d04 65 20 65 66 66 65 63 74 69 76 65 20 72 65 73 74 e effective rest
12d05 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
12d06 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 n() call after e
12d07 61 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 ach .** saveCurs
12d08 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f orPosition()..*/
12d09 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12d0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
12d0b 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 RestoreCursorPos
12d0c 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a ition(BtCursor *
12d0d 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b pCur){. int rc;
12d0e 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
12d0f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
12d10 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
12d11 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 Cur->eState>=CUR
12d12 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 SOR_REQUIRESEEK
12d13 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 );. if( pCur->e
12d14 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 State==CURSOR_FA
12d15 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ULT ){. retur
12d16 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 n pCur->skip;.
12d17 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 }. pCur->eState
12d18 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
12d19 44 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 D;. rc = sqlite
12d1a 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 3BtreeMoveto(pCu
12d1b 72 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 r, pCur->pKey, p
12d1c 43 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 Cur->nKey, 0, &p
12d1d 43 75 72 2d 3e 73 6b 69 70 29 3b 0a 20 20 69 66 Cur->skip);. if
12d1e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
12d1f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
12d20 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b ree(pCur->pKey);
12d21 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 . pCur->pKey
12d22 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 = 0;. assert(
12d23 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
12d24 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 URSOR_VALID || p
12d25 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
12d26 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 SOR_INVALID );.
12d27 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
12d28 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f }..#define resto
12d29 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
12d2a 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 (p) \. (p->eSta
12d2b 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 te>=CURSOR_REQUI
12d2c 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 RESEEK ? \.
12d2d 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
12d2e 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 RestoreCursorPos
12d2f 69 74 69 6f 6e 28 70 29 20 3a 20 5c 0a 20 20 20 ition(p) : \.
12d30 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 29 SQLITE_OK)
12d31 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e ../*.** Determin
12d32 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 e whether or not
12d33 20 61 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f a cursor has mo
12d34 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 6f 73 ved from the pos
12d35 69 74 69 6f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 ition it.** was
12d36 6c 61 73 74 20 70 6c 61 63 65 64 20 61 74 2e 20 last placed at.
12d37 20 43 75 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 Cursors can mov
12d38 65 20 77 68 65 6e 20 74 68 65 20 72 6f 77 20 74 e when the row t
12d39 68 65 79 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 hey are pointing
12d3a 0a 2a 2a 20 61 74 20 69 73 20 64 65 6c 65 74 65 .** at is delete
12d3b 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 d out from under
12d3c 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 them..**.** Thi
12d3d 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
12d3e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 s an error code
12d3f 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 if something goe
12d40 73 20 77 72 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a s wrong. The.**
12d41 20 69 6e 74 65 67 65 72 20 2a 70 48 61 73 4d 6f integer *pHasMo
12d42 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 6f 6e ved is set to on
12d43 65 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 e if the cursor
12d44 68 61 73 20 6d 6f 76 65 64 20 61 6e 64 20 30 20 has moved and 0
12d45 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 if not..*/.SQLIT
12d46 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
12d47 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
12d48 48 61 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f HasMoved(BtCurso
12d49 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 r *pCur, int *pH
12d4a 61 73 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 asMoved){. int
12d4b 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73 74 rc;.. rc = rest
12d4c 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
12d4d 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 n(pCur);. if( r
12d4e 63 20 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f c ){. *pHasMo
12d4f 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 ved = 1;. ret
12d50 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 urn rc;. }. if
12d51 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d ( pCur->eState!=
12d52 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 CURSOR_VALID ||
12d53 70 43 75 72 2d 3e 73 6b 69 70 21 3d 30 20 29 7b pCur->skip!=0 ){
12d54 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 . *pHasMoved
12d55 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 1;. }else{.
12d56 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 *pHasMoved = 0
12d57 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
12d58 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 QLITE_OK;.}..#if
12d59 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
12d5a 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a _AUTOVACUUM./*.*
12d5b 2a 20 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e * Given a page n
12d5c 75 6d 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c umber of a regul
12d5d 61 72 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ar database page
12d5e 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70 61 67 , return the pag
12d5f 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 e.** number for
12d60 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 the pointer-map
12d61 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 page that contai
12d62 6e 73 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 ns the entry for
12d63 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 the.** input pa
12d64 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 ge number..*/.st
12d65 61 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 atic Pgno ptrmap
12d66 50 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 Pageno(BtShared
12d67 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 *pBt, Pgno pgno)
12d68 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 {. int nPagesPe
12d69 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f rMapPage;. Pgno
12d6a 20 69 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 iPtrMap, ret;.
12d6b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
12d6c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
12d6d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 >mutex) );. nPa
12d6e 67 65 73 50 65 72 4d 61 70 50 61 67 65 20 3d 20 gesPerMapPage =
12d6f 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 (pBt->usableSize
12d70 2f 35 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 /5)+1;. iPtrMap
12d71 20 3d 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 = (pgno-2)/nPag
12d72 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 esPerMapPage;.
12d73 72 65 74 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e ret = (iPtrMap*n
12d74 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 29 PagesPerMapPage)
12d75 20 2b 20 32 3b 20 0a 20 20 69 66 28 20 72 65 74 + 2; . if( ret
12d76 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
12d77 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
12d78 72 65 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 ret++;. }. ret
12d79 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a urn ret;.}../*.*
12d7a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 * Write an entry
12d7b 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 into the pointe
12d7c 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 r map..**.** Thi
12d7d 73 20 72 6f 75 74 69 6e 65 20 75 70 64 61 74 65 s routine update
12d7e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 s the pointer ma
12d7f 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65 p entry for page
12d80 20 6e 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a number 'key'.**
12d81 20 73 6f 20 74 68 61 74 20 69 74 20 6d 61 70 73 so that it maps
12d82 20 74 6f 20 74 79 70 65 20 27 65 54 79 70 65 27 to type 'eType'
12d83 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 and parent page
12d84 20 6e 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a number 'pgno'..
12d85 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 ** An error code
12d86 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
12d87 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 something goes w
12d88 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 rong, otherwise
12d89 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 SQLITE_OK..*/.st
12d8a 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 50 atic int ptrmapP
12d8b 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ut(BtShared *pBt
12d8c 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 , Pgno key, u8 e
12d8d 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e Type, Pgno paren
12d8e 74 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 t){. DbPage *pD
12d8f 62 50 61 67 65 3b 20 20 2f 2a 20 54 68 65 20 70 bPage; /* The p
12d90 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 ointer map page
12d91 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 */. u8 *pPtrmap
12d92 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f ; /* The po
12d93 69 6e 74 65 72 20 6d 61 70 20 64 61 74 61 20 2a inter map data *
12d94 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 /. Pgno iPtrmap
12d95 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 ; /* The poi
12d96 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 6e 75 nter map page nu
12d97 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 mber */. int of
12d98 66 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4f fset; /* O
12d99 66 66 73 65 74 20 69 6e 20 70 6f 69 6e 74 65 72 ffset in pointer
12d9a 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 map page */. i
12d9b 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
12d9c 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
12d9d 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
12d9e 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 );. /* The mas
12d9f 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 ter-journal page
12da0 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 number must nev
12da1 65 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20 er be used as a
12da2 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 pointer map page
12da3 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d */. assert( 0=
12da4 3d 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 =PTRMAP_ISPAGE(p
12da5 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 Bt, PENDING_BYTE
12da6 5f 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a _PAGE(pBt)) );..
12da7 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 assert( pBt->a
12da8 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 utoVacuum );. i
12da9 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 f( key==0 ){.
12daa 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
12dab 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
12dac 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 . iPtrmap = PTR
12dad 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 MAP_PAGENO(pBt,
12dae 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c key);. rc = sql
12daf 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 ite3PagerGet(pBt
12db0 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 ->pPager, iPtrma
12db1 70 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 p, &pDbPage);.
12db2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12db3 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
12db4 72 63 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74 rc;. }. offset
12db5 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 = PTRMAP_PTROFF
12db6 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 79 SET(iPtrmap, key
12db7 29 3b 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 );. pPtrmap = (
12db8 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 u8 *)sqlite3Page
12db9 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 rGetData(pDbPage
12dba 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 21 );.. if( eType!
12dbb 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d =pPtrmap[offset]
12dbc 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26 70 50 || get4byte(&pP
12dbd 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 trmap[offset+1])
12dbe 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 !=parent ){.
12dbf 54 52 41 43 45 28 28 22 50 54 52 4d 41 50 5f 55 TRACE(("PTRMAP_U
12dc0 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25 PDATE: %d->(%d,%
12dc1 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70 d)\n", key, eTyp
12dc2 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 20 20 e, parent));.
12dc3 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
12dc4 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29 erWrite(pDbPage)
12dc5 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
12dc6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
12dc7 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d pPtrmap[offset]
12dc8 20 3d 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20 = eType;.
12dc9 70 75 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 put4byte(&pPtrma
12dca 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20 70 61 72 p[offset+1], par
12dcb 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ent);. }. }.
12dcc 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 . sqlite3PagerU
12dcd 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 nref(pDbPage);.
12dce 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
12dcf 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20 65 6e 74 *.** Read an ent
12dd0 72 79 20 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e ry from the poin
12dd1 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 ter map..**.** T
12dd2 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 72 his routine retr
12dd3 69 65 76 65 73 20 74 68 65 20 70 6f 69 6e 74 65 ieves the pointe
12dd4 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 r map entry for
12dd5 70 61 67 65 20 27 6b 65 79 27 2c 20 77 72 69 74 page 'key', writ
12dd6 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79 70 65 20 ing.** the type
12dd7 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 and parent page
12dd8 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45 54 79 70 number to *pETyp
12dd9 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 72 65 73 e and *pPgno res
12dda 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e pectively..** An
12ddb 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
12ddc 65 74 75 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 eturned if somet
12ddd 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c hing goes wrong,
12dde 20 6f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 otherwise SQLIT
12ddf 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 E_OK..*/.static
12de0 69 6e 74 20 70 74 72 6d 61 70 47 65 74 28 42 74 int ptrmapGet(Bt
12de1 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
12de2 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45 54 79 70 o key, u8 *pETyp
12de3 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b e, Pgno *pPgno){
12de4 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 . DbPage *pDbPa
12de5 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 ge; /* The poi
12de6 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f nter map page */
12de7 0a 20 20 69 6e 74 20 69 50 74 72 6d 61 70 3b 20 . int iPtrmap;
12de8 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
12de9 20 6d 61 70 20 70 61 67 65 20 69 6e 64 65 78 20 map page index
12dea 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 */. u8 *pPtrmap
12deb 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 ; /* Point
12dec 65 72 20 6d 61 70 20 70 61 67 65 20 64 61 74 61 er map page data
12ded 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 */. int offset
12dee 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 ; /* Offs
12def 65 74 20 6f 66 20 65 6e 74 72 79 20 69 6e 20 70 et of entry in p
12df0 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f 0a 20 20 ointer map */.
12df1 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 int rc;.. asser
12df2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
12df3 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
12df4 29 20 29 3b 0a 0a 20 20 69 50 74 72 6d 61 70 20 ) );.. iPtrmap
12df5 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 = PTRMAP_PAGENO(
12df6 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 pBt, key);. rc
12df7 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
12df8 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 t(pBt->pPager, i
12df9 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 Ptrmap, &pDbPage
12dfa 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 );. if( rc!=0 )
12dfb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
12dfc 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d . }. pPtrmap =
12dfd 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 (u8 *)sqlite3Pa
12dfe 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61 gerGetData(pDbPa
12dff 67 65 29 3b 0a 0a 20 20 6f 66 66 73 65 74 20 3d ge);.. offset =
12e00 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 PTRMAP_PTROFFSE
12e01 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b T(iPtrmap, key);
12e02 0a 20 20 61 73 73 65 72 74 28 20 70 45 54 79 70 . assert( pETyp
12e03 65 21 3d 30 20 29 3b 0a 20 20 2a 70 45 54 79 70 e!=0 );. *pETyp
12e04 65 20 3d 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 e = pPtrmap[offs
12e05 65 74 5d 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f et];. if( pPgno
12e06 20 29 20 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34 ) *pPgno = get4
12e07 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 byte(&pPtrmap[of
12e08 66 73 65 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c fset+1]);.. sql
12e09 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
12e0a 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 2a DbPage);. if( *
12e0b 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54 pEType<1 || *pET
12e0c 79 70 65 3e 35 20 29 20 72 65 74 75 72 6e 20 53 ype>5 ) return S
12e0d 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
12e0e 50 54 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c PT;. return SQL
12e0f 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 ITE_OK;.}..#else
12e10 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 64 20 53 /* if defined S
12e11 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
12e12 41 43 55 55 4d 20 2a 2f 0a 20 20 23 64 65 66 69 ACUUM */. #defi
12e13 6e 65 20 70 74 72 6d 61 70 50 75 74 28 77 2c 78 ne ptrmapPut(w,x
12e14 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a ,y,z) SQLITE_OK.
12e15 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 #define ptrmap
12e16 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c Get(w,x,y,z) SQL
12e17 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 ITE_OK. #define
12e18 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 79 ptrmapPutOvfl(y
12e19 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 ,z) SQLITE_OK.#e
12e1a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 ndif../*.** Give
12e1b 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 20 61 n a btree page a
12e1c 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20 nd a cell index
12e1d 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72 (0 means the fir
12e1e 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 st cell on.** th
12e1f 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20 e page, 1 means
12e20 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c the second cell,
12e21 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72 and so forth) r
12e22 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a eturn a pointer.
12e23 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 ** to the cell c
12e24 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ontent..**.** Th
12e25 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 is routine works
12e26 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 only for pages
12e27 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 that do not cont
12e28 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c ain overflow cel
12e29 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 ls..*/.#define f
12e2a 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 indCell(P,I) \.
12e2b 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 ((P)->aData + (
12e2c 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 (P)->maskPage &
12e2d 67 65 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61 get2byte(&(P)->a
12e2e 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 Data[(P)->cellOf
12e2f 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a fset+2*(I)])))..
12e30 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 /*.** This a mor
12e31 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f e complex versio
12e32 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 n of findCell()
12e33 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a that works for.*
12e34 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 * pages that do
12e35 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 contain overflow
12e36 20 63 65 6c 6c 73 2e 20 20 53 65 65 20 69 6e 73 cells. See ins
12e37 65 72 74 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 ert.*/.static u8
12e38 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 *findOverflowCe
12e39 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ll(MemPage *pPag
12e3a 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 e, int iCell){.
12e3b 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 int i;. assert
12e3c 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
12e3d 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
12e3e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 >mutex) );. for
12e3f 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 (i=pPage->nOverf
12e40 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d low-1; i>=0; i--
12e41 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 ){. int k;.
12e42 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 struct _OvflCe
12e43 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 70 ll *pOvfl;. p
12e44 4f 76 66 6c 20 3d 20 26 70 50 61 67 65 2d 3e 61 Ovfl = &pPage->a
12e45 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d Ovfl[i];. k =
12e46 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 20 20 pOvfl->idx;.
12e47 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b if( k<=iCell ){
12e48 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43 . if( k==iC
12e49 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ell ){. r
12e4a 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65 eturn pOvfl->pCe
12e4b 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ll;. }.
12e4c 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d iCell--;. }
12e4d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69 . }. return fi
12e4e 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 ndCell(pPage, iC
12e4f 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ell);.}../*.** P
12e50 61 72 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74 arse a cell cont
12e51 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69 ent block and fi
12e52 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e ll in the CellIn
12e53 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 fo structure. T
12e54 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20 here.** are two
12e55 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73 versions of this
12e56 20 66 75 6e 63 74 69 6f 6e 2e 20 20 73 71 6c 69 function. sqli
12e57 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c te3BtreeParseCel
12e58 6c 28 29 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 l() takes a .**
12e59 63 65 6c 6c 20 69 6e 64 65 78 20 61 73 20 74 68 cell index as th
12e5a 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
12e5b 74 20 61 6e 64 20 73 71 6c 69 74 65 33 42 74 72 t and sqlite3Btr
12e5c 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 eeParseCellPtr()
12e5d 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20 70 6f 69 .** takes a poi
12e5e 6e 74 65 72 20 74 6f 20 74 68 65 20 62 6f 64 79 nter to the body
12e5f 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 61 73 20 of the cell as
12e60 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d its second argum
12e61 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 ent..**.** Withi
12e62 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 n this file, the
12e63 20 70 61 72 73 65 43 65 6c 6c 28 29 20 6d 61 63 parseCell() mac
12e64 72 6f 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 ro can be called
12e65 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 73 instead of.** s
12e66 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 qlite3BtreeParse
12e67 43 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 CellPtr(). Using
12e68 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c some compilers,
12e69 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 this will be fa
12e6a 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ster..*/.SQLITE_
12e6b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
12e6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
12e6d 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 llPtr(. MemPage
12e6e 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 *pPage,
12e6f 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e /* Page contain
12e70 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a ing the cell */.
12e71 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 u8 *pCell,
12e72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
12e73 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c nter to the cell
12e74 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c text. */. Cell
12e75 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 Info *pInfo
12e76 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 /* Fill in t
12e77 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
12e78 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 .){. int n;
12e79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12e7a 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e Number bytes in
12e7b 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 cell content he
12e7c 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50 ader */. u32 nP
12e7d 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 ayload;
12e7e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
12e7f 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 ytes of cell pay
12e80 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 load */.. asser
12e81 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
12e82 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
12e83 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 ->mutex) );.. p
12e84 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 Info->pCell = pC
12e85 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ell;. assert( p
12e86 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c Page->leaf==0 ||
12e87 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 pPage->leaf==1
12e88 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e );. n = pPage->
12e89 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 childPtrSize;.
12e8a 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 assert( n==4-4*p
12e8b 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 Page->leaf );.
12e8c 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
12e8d 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 y ){. if( pPa
12e8e 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 ge->hasData ){.
12e8f 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 n += getVar
12e90 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c int32(&pCell[n],
12e91 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 nPayload);.
12e92 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 }else{. nPa
12e93 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d yload = 0;. }
12e94 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 . n += getVar
12e95 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 int(&pCell[n], (
12e96 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 u64*)&pInfo->nKe
12e97 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e y);. pInfo->n
12e98 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b Data = nPayload;
12e99 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
12e9a 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a nfo->nData = 0;.
12e9b 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 n += getVari
12e9c 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 nt32(&pCell[n],
12e9d 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 nPayload);. p
12e9e 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 Info->nKey = nPa
12e9f 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e yload;. }. pIn
12ea0 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e fo->nPayload = n
12ea1 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f Payload;. pInfo
12ea2 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20 ->nHeader = n;.
12ea3 20 69 66 28 20 6c 69 6b 65 6c 79 28 6e 50 61 79 if( likely(nPay
12ea4 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 load<=pPage->max
12ea5 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 20 20 2f 2a Local) ){. /*
12ea6 20 54 68 69 73 20 69 73 20 74 68 65 20 28 65 61 This is the (ea
12ea7 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 sy) common case
12ea8 77 68 65 72 65 20 74 68 65 20 65 6e 74 69 72 65 where the entire
12ea9 20 70 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 20 payload fits.
12eaa 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61 ** on the loca
12eab 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72 l page. No over
12eac 66 6c 6f 77 20 69 73 20 72 65 71 75 69 72 65 64 flow is required
12ead 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 .. */. int
12eae 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 nSize;
12eaf 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f /* Total size o
12eb0 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 f cell content i
12eb1 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 6e n bytes */. n
12eb2 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64 20 Size = nPayload
12eb3 2b 20 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e + n;. pInfo->
12eb4 6e 4c 6f 63 61 6c 20 3d 20 6e 50 61 79 6c 6f 61 nLocal = nPayloa
12eb5 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f d;. pInfo->iO
12eb6 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 verflow = 0;.
12eb7 20 69 66 28 20 28 6e 53 69 7a 65 20 26 20 7e 33 if( (nSize & ~3
12eb8 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 )==0 ){. nS
12eb9 69 7a 65 20 3d 20 34 3b 20 20 20 20 20 20 20 20 ize = 4;
12eba 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 /* Minimum cell
12ebb 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20 20 size is 4 */.
12ebc 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 }. pInfo->nS
12ebd 69 7a 65 20 3d 20 6e 53 69 7a 65 3b 0a 20 20 7d ize = nSize;. }
12ebe 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 else{. /* If
12ebf 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c the payload will
12ec0 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 not fit complet
12ec1 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c ely on the local
12ec2 20 70 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 page, we have.
12ec3 20 20 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 ** to decide
12ec4 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 how much to stor
12ec5 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f e locally and ho
12ec6 77 20 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 w much to spill
12ec7 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 onto. ** over
12ec8 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 flow pages. The
12ec9 20 73 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 strategy is to
12eca 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f minimize the amo
12ecb 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 unt of unused.
12ecc 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 ** space on ov
12ecd 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 erflow pages whi
12ece 6c 65 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 le keeping the a
12ecf 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 mount of local s
12ed0 74 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e torage. ** in
12ed1 20 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 between minLoca
12ed2 6c 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a l and maxLocal..
12ed3 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 **. ** Wa
12ed4 72 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 rning: changing
12ed5 20 74 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f the way overflo
12ed6 77 20 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 w payload is dis
12ed7 74 72 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a tributed in any.
12ed8 20 20 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 ** way will
12ed9 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 result in an inc
12eda 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 ompatible file f
12edb 6f 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 ormat.. */.
12edc 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 int minLocal;
12edd 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 /* Minimum amou
12ede 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 nt of payload he
12edf 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 ld locally */.
12ee0 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 int maxLocal;
12ee1 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 /* Maximum amou
12ee2 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 nt of payload he
12ee3 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 ld locally */.
12ee4 20 20 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 int surplus;
12ee5 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 /* Overflow pay
12ee6 6c 6f 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 load available f
12ee7 6f 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 or local storage
12ee8 20 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 */.. minLoca
12ee9 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f l = pPage->minLo
12eea 63 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 cal;. maxLoca
12eeb 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f l = pPage->maxLo
12eec 63 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 cal;. surplus
12eed 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e = minLocal + (n
12eee 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 Payload - minLoc
12eef 61 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d al)%(pPage->pBt-
12ef0 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 >usableSize - 4)
12ef1 3b 0a 20 20 20 20 69 66 28 20 73 75 72 70 6c 75 ;. if( surplu
12ef2 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b s <= maxLocal ){
12ef3 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c . pInfo->nL
12ef4 6f 63 61 6c 20 3d 20 73 75 72 70 6c 75 73 3b 0a ocal = surplus;.
12ef5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
12ef6 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d pInfo->nLocal =
12ef7 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d minLocal;. }
12ef8 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 . pInfo->iOve
12ef9 72 66 6c 6f 77 20 3d 20 70 49 6e 66 6f 2d 3e 6e rflow = pInfo->n
12efa 4c 6f 63 61 6c 20 2b 20 6e 3b 0a 20 20 20 20 70 Local + n;. p
12efb 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70 49 Info->nSize = pI
12efc 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 2b nfo->iOverflow +
12efd 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 4;. }.}.#defin
12efe 65 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67 e parseCell(pPag
12eff 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 e, iCell, pInfo)
12f00 20 5c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 \. sqlite3Btre
12f01 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 eParseCellPtr((p
12f02 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 Page), findCell(
12f03 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 (pPage), (iCell)
12f04 29 2c 20 28 70 49 6e 66 6f 29 29 0a 53 51 4c 49 ), (pInfo)).SQLI
12f05 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12f06 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 sqlite3BtreePars
12f07 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 eCell(. MemPage
12f08 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 *pPage,
12f09 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e /* Page contain
12f0a 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a ing the cell */.
12f0b 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20 int iCell,
12f0c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
12f0d 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69 cell index. Fi
12f0e 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f rst cell is 0 */
12f0f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e . CellInfo *pIn
12f10 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 fo /* Fi
12f11 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 ll in this struc
12f12 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72 ture */.){. par
12f13 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 seCell(pPage, iC
12f14 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a ell, pInfo);.}..
12f15 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 /*.** Compute th
12f16 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
12f17 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20 43 f bytes that a C
12f18 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 65 ell needs in the
12f19 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 cell.** data ar
12f1a 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 2d ea of the btree-
12f1b 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 72 page. The retur
12f1c 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 n number include
12f1d 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 s the cell.** da
12f1e 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 ta header and th
12f1f 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c e local payload,
12f20 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 but not any ove
12f21 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a rflow page or.**
12f22 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20 the space used
12f23 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e by the cell poin
12f24 74 65 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ter..*/.#ifndef
12f25 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 75 31 NDEBUG.static u1
12f26 36 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 61 6 cellSize(MemPa
12f27 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 ge *pPage, int i
12f28 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49 6e 66 Cell){. CellInf
12f29 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c 69 74 65 o info;. sqlite
12f2a 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 3BtreeParseCell(
12f2b 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 26 69 pPage, iCell, &i
12f2c 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 nfo);. return i
12f2d 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 7d 0a 23 65 6e nfo.nSize;.}.#en
12f2e 64 69 66 0a 73 74 61 74 69 63 20 75 31 36 20 63 dif.static u16 c
12f2f 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d 50 61 ellSizePtr(MemPa
12f30 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 ge *pPage, u8 *p
12f31 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49 6e 66 Cell){. CellInf
12f32 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c 69 74 65 o info;. sqlite
12f33 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 3BtreeParseCellP
12f34 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
12f35 20 26 69 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72 &info);. retur
12f36 6e 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 7d 0a n info.nSize;.}.
12f37 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
12f38 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
12f39 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c /*.** If the cel
12f3a 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 20 6f 66 l pCell, part of
12f3b 20 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74 page pPage cont
12f3c 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a ains a pointer.*
12f3d 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 * to an overflow
12f3e 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e page, insert an
12f3f 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 entry into the
12f40 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 pointer-map.** f
12f41 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 or the overflow
12f42 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 page..*/.static
12f43 69 6e 74 20 70 74 72 6d 61 70 50 75 74 4f 76 66 int ptrmapPutOvf
12f44 6c 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 lPtr(MemPage *pP
12f45 61 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b age, u8 *pCell){
12f46 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f . CellInfo info
12f47 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c ;. assert( pCel
12f48 6c 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 l!=0 );. sqlite
12f49 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 3BtreeParseCellP
12f4a 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
12f4b 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 &info);. asser
12f4c 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 t( (info.nData+(
12f4d 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a pPage->intKey?0:
12f4e 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 info.nKey))==inf
12f4f 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 o.nPayload );.
12f50 69 66 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b if( (info.nData+
12f51 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 (pPage->intKey?0
12f52 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3e 69 6e 66 :info.nKey))>inf
12f53 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 o.nLocal ){.
12f54 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 Pgno ovfl = get4
12f55 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f byte(&pCell[info
12f56 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 .iOverflow]);.
12f57 20 20 72 65 74 75 72 6e 20 70 74 72 6d 61 70 50 return ptrmapP
12f58 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 6f ut(pPage->pBt, o
12f59 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 vfl, PTRMAP_OVER
12f5a 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e 70 67 FLOW1, pPage->pg
12f5b 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 no);. }. retur
12f5c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f n SQLITE_OK;.}./
12f5d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c 6c *.** If the cell
12f5e 20 77 69 74 68 20 69 6e 64 65 78 20 69 43 65 6c with index iCel
12f5f 6c 20 6f 6e 20 70 61 67 65 20 70 50 61 67 65 20 l on page pPage
12f60 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 contains a point
12f61 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 er.** to an over
12f62 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 flow page, inser
12f63 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 t an entry into
12f64 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a the pointer-map.
12f65 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 ** for the overf
12f66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 low page..*/.sta
12f67 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 50 75 tic int ptrmapPu
12f68 74 4f 76 66 6c 28 4d 65 6d 50 61 67 65 20 2a 70 tOvfl(MemPage *p
12f69 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 Page, int iCell)
12f6a 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 {. u8 *pCell;.
12f6b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
12f6c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
12f6d 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
12f6e 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 ;. pCell = find
12f6f 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 OverflowCell(pPa
12f70 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 72 65 ge, iCell);. re
12f71 74 75 72 6e 20 70 74 72 6d 61 70 50 75 74 4f 76 turn ptrmapPutOv
12f72 66 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 flPtr(pPage, pCe
12f73 6c 6c 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a ll);.}.#endif...
12f74 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74 /*.** Defragment
12f75 20 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 2e the page given.
12f76 20 20 41 6c 6c 20 43 65 6c 6c 73 20 61 72 65 20 All Cells are
12f77 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 moved to the.**
12f78 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 end of the page
12f79 61 6e 64 20 61 6c 6c 20 66 72 65 65 20 73 70 61 and all free spa
12f7a 63 65 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 ce is collected
12f7b 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20 62 69 67 20 into one.** big
12f7c 46 72 65 65 42 6c 6b 20 74 68 61 74 20 6f 63 63 FreeBlk that occ
12f7d 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 20 74 urs in between t
12f7e 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 63 65 he header and ce
12f7f 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72 ll.** pointer ar
12f80 72 61 79 20 61 6e 64 20 74 68 65 20 63 65 6c 6c ray and the cell
12f81 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 2a content area..*
12f82 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 66 /.static int def
12f83 72 61 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50 ragmentPage(MemP
12f84 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 age *pPage){. i
12f85 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
12f86 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
12f87 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
12f88 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 nt pc;
12f89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
12f8a 72 65 73 73 20 6f 66 20 61 20 69 2d 74 68 20 63 ress of a i-th c
12f8b 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 ell */. int add
12f8c 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
12f8d 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 /* Offset of
12f8e 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 first byte afte
12f8f 72 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 r cell pointer a
12f90 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 64 rray */. int hd
12f91 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
12f92 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
12f93 6f 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 o the page heade
12f94 72 20 2a 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b r */. int size;
12f95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f96 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 /* Size of a c
12f97 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 ell */. int usa
12f98 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 bleSize;
12f99 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
12f9a 20 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e usable bytes on
12f9b 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 a page */. int
12f9c 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 cellOffset;
12f9d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
12f9e 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f t to the cell po
12f9f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 inter array */.
12fa0 20 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 int cbrk;
12fa1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
12fa2 66 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c ffset to the cel
12fa3 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a l content area *
12fa4 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 /. int nCell;
12fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12fa6 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c * Number of cell
12fa7 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f s on the page */
12fa8 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
12fa9 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a *data; /*
12faa 20 54 68 65 20 70 61 67 65 20 64 61 74 61 20 2a The page data *
12fab 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
12fac 72 20 2a 74 65 6d 70 3b 20 20 20 20 20 20 20 2f r *temp; /
12fad 2a 20 54 65 6d 70 20 61 72 65 61 20 66 6f 72 20 * Temp area for
12fae 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a cell content */.
12faf 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
12fb0 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
12fb1 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
12fb2 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
12fb3 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b pPage->pBt!=0 );
12fb4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
12fb5 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
12fb6 65 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f e <= SQLITE_MAX_
12fb7 50 41 47 45 5f 53 49 5a 45 20 29 3b 0a 20 20 61 PAGE_SIZE );. a
12fb8 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f ssert( pPage->nO
12fb9 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 verflow==0 );.
12fba 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
12fbb 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
12fbc 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
12fbd 0a 20 20 74 65 6d 70 20 3d 20 73 71 6c 69 74 65 . temp = sqlite
12fbe 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 3PagerTempSpace(
12fbf 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 50 61 67 pPage->pBt->pPag
12fc0 65 72 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 er);. data = pP
12fc1 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 age->aData;. hd
12fc2 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
12fc3 66 73 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 fset;. cellOffs
12fc4 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c et = pPage->cell
12fc5 4f 66 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 Offset;. nCell
12fc6 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a = pPage->nCell;.
12fc7 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3d assert( nCell=
12fc8 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b =get2byte(&data[
12fc9 68 64 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73 61 hdr+3]) );. usa
12fca 62 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d bleSize = pPage-
12fcb 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
12fcc 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32 62 ;. cbrk = get2b
12fcd 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
12fce 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 74 65 6d );. memcpy(&tem
12fcf 70 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 p[cbrk], &data[c
12fd0 62 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 brk], usableSize
12fd1 20 2d 20 63 62 72 6b 29 3b 0a 20 20 63 62 72 6b - cbrk);. cbrk
12fd2 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 = usableSize;.
12fd3 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c for(i=0; i<nCel
12fd4 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 l; i++){. u8
12fd5 2a 70 41 64 64 72 3b 20 20 20 20 20 2f 2a 20 54 *pAddr; /* T
12fd6 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69 he i-th cell poi
12fd7 6e 74 65 72 20 2a 2f 0a 20 20 20 20 70 41 64 64 nter */. pAdd
12fd8 72 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 r = &data[cellOf
12fd9 66 73 65 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 fset + i*2];.
12fda 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 70 pc = get2byte(p
12fdb 41 64 64 72 29 3b 0a 20 20 20 20 69 66 28 20 70 Addr);. if( p
12fdc 63 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 7b c>=usableSize ){
12fdd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
12fde 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
12fdf 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 69 7a T;. }. siz
12fe0 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 e = cellSizePtr(
12fe1 70 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d pPage, &temp[pc]
12fe2 29 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20 73 );. cbrk -= s
12fe3 69 7a 65 3b 0a 20 20 20 20 69 66 28 20 63 62 72 ize;. if( cbr
12fe4 6b 3c 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e k<cellOffset+2*n
12fe5 43 65 6c 6c 20 7c 7c 20 70 63 2b 73 69 7a 65 3e Cell || pc+size>
12fe6 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 usableSize ){.
12fe7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
12fe8 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
12fe9 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
12fea 28 20 63 62 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 ( cbrk+size<=usa
12feb 62 6c 65 53 69 7a 65 20 26 26 20 63 62 72 6b 3e bleSize && cbrk>
12fec 3d 30 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 =0 );. memcpy
12fed 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74 (&data[cbrk], &t
12fee 65 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a emp[pc], size);.
12fef 20 20 20 20 70 75 74 32 62 79 74 65 28 70 41 64 put2byte(pAd
12ff0 64 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 dr, cbrk);. }.
12ff1 20 61 73 73 65 72 74 28 20 63 62 72 6b 3e 3d 63 assert( cbrk>=c
12ff2 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c ellOffset+2*nCel
12ff3 6c 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 l );. put2byte(
12ff4 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62 &data[hdr+5], cb
12ff5 72 6b 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b rk);. data[hdr+
12ff6 31 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 1] = 0;. data[h
12ff7 64 72 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61 74 dr+2] = 0;. dat
12ff8 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 a[hdr+7] = 0;.
12ff9 61 64 64 72 20 3d 20 63 65 6c 6c 4f 66 66 73 65 addr = cellOffse
12ffa 74 2b 32 2a 6e 43 65 6c 6c 3b 0a 20 20 6d 65 6d t+2*nCell;. mem
12ffb 73 65 74 28 26 64 61 74 61 5b 61 64 64 72 5d 2c set(&data[addr],
12ffc 20 30 2c 20 63 62 72 6b 2d 61 64 64 72 29 3b 0a 0, cbrk-addr);.
12ffd 20 20 69 66 28 20 63 62 72 6b 2d 61 64 64 72 21 if( cbrk-addr!
12ffe 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b =pPage->nFree ){
12fff 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
13000 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
13001 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
13002 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
13003 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 * Allocate nByte
13004 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
13005 6f 6e 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a on a page..**.**
13006 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 Return the inde
13007 78 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 x into pPage->aD
13008 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 66 69 72 ata[] of the fir
13009 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 st byte of.** th
1300a 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e e new allocation
1300b 2e 20 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75 . The caller gu
1300c 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 arantees that th
1300d 65 72 65 20 69 73 20 65 6e 6f 75 67 68 0a 2a 2a ere is enough.**
1300e 20 73 70 61 63 65 2e 20 20 54 68 69 73 20 72 6f space. This ro
1300f 75 74 69 6e 65 20 77 69 6c 6c 20 6e 65 76 65 72 utine will never
13010 20 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 fail..**.** If
13011 74 68 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e the page contain
13012 73 20 6e 42 79 74 65 73 20 6f 66 20 66 72 65 65 s nBytes of free
13013 20 73 70 61 63 65 20 62 75 74 20 64 6f 65 73 20 space but does
13014 6e 6f 74 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e not contain.** n
13015 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 69 67 75 Bytes of contigu
13016 6f 75 73 20 66 72 65 65 20 73 70 61 63 65 2c 20 ous free space,
13017 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
13018 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a e automatically.
13019 2a 2a 20 63 61 6c 6c 73 20 64 65 66 72 61 67 65 ** calls defrage
1301a 6d 65 6e 74 50 61 67 65 28 29 20 74 6f 20 63 6f mentPage() to co
1301b 6e 73 6f 6c 69 64 61 74 65 20 61 6c 6c 20 66 72 nsolidate all fr
1301c 65 65 20 73 70 61 63 65 20 62 65 66 6f 72 65 20 ee space before
1301d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74 .** allocating t
1301e 68 65 20 6e 65 77 20 63 68 75 6e 6b 2e 0a 2a 2f he new chunk..*/
1301f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f .static int allo
13020 63 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61 67 cateSpace(MemPag
13021 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 42 e *pPage, int nB
13022 79 74 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 yte){. int addr
13023 2c 20 70 63 2c 20 68 64 72 3b 0a 20 20 69 6e 74 , pc, hdr;. int
13024 20 73 69 7a 65 3b 0a 20 20 69 6e 74 20 6e 46 72 size;. int nFr
13025 61 67 3b 0a 20 20 69 6e 74 20 74 6f 70 3b 0a 20 ag;. int top;.
13026 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 69 6e int nCell;. in
13027 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 t cellOffset;.
13028 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 unsigned char *d
13029 61 74 61 3b 0a 20 20 0a 20 20 64 61 74 61 20 3d ata;. . data =
1302a 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 pPage->aData;.
1302b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1302c 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
1302d 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
1302e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
1302f 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 age->pBt );. as
13030 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
13031 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
13032 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
13033 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d assert( nByte>=
13034 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 0 ); /* Minimum
13035 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 cell size is 4
13036 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
13037 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 ge->nFree>=nByte
13038 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
13039 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d age->nOverflow==
1303a 30 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 0 );. pPage->nF
1303b 72 65 65 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 ree -= nByte;.
1303c 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
1303d 4f 66 66 73 65 74 3b 0a 0a 20 20 6e 46 72 61 67 Offset;.. nFrag
1303e 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a = data[hdr+7];.
1303f 20 20 69 66 28 20 6e 46 72 61 67 3c 36 30 20 29 if( nFrag<60 )
13040 7b 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 {. /* Search
13041 74 68 65 20 66 72 65 65 6c 69 73 74 20 6c 6f 6f the freelist loo
13042 6b 69 6e 67 20 66 6f 72 20 61 20 73 6c 6f 74 20 king for a slot
13043 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 big enough to sa
13044 74 69 73 66 79 20 74 68 65 0a 20 20 20 20 2a 2a tisfy the. **
13045 20 73 70 61 63 65 20 72 65 71 75 65 73 74 2e 20 space request.
13046 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 68 64 */. addr = hd
13047 72 2b 31 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 r+1;. while(
13048 28 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 (pc = get2byte(&
13049 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 data[addr]))>0 )
1304a 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 {. size = g
1304b 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 et2byte(&data[pc
1304c 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 +2]);. if(
1304d 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 7b 0a 20 size>=nByte ){.
1304e 20 20 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c if( size<
1304f 6e 42 79 74 65 2b 34 20 29 7b 0a 20 20 20 20 20 nByte+4 ){.
13050 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 memcpy(&dat
13051 61 5b 61 64 64 72 5d 2c 20 26 64 61 74 61 5b 70 a[addr], &data[p
13052 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 c], 2);.
13053 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 data[hdr+7] =
13054 6e 46 72 61 67 20 2b 20 73 69 7a 65 20 2d 20 6e nFrag + size - n
13055 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 20 20 Byte;.
13056 72 65 74 75 72 6e 20 70 63 3b 0a 20 20 20 20 20 return pc;.
13057 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
13058 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
13059 74 61 5b 70 63 2b 32 5d 2c 20 73 69 7a 65 2d 6e ta[pc+2], size-n
1305a 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Byte);.
1305b 20 72 65 74 75 72 6e 20 70 63 20 2b 20 73 69 7a return pc + siz
1305c 65 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 20 20 e - nByte;.
1305d 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1305e 20 20 20 61 64 64 72 20 3d 20 70 63 3b 0a 20 20 addr = pc;.
1305f 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c }. }.. /* Al
13060 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72 locate memory fr
13061 6f 6d 20 74 68 65 20 67 61 70 20 69 6e 20 62 65 om the gap in be
13062 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70 tween the cell p
13063 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a ointer array. *
13064 2a 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 * and the cell c
13065 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 20 20 2a ontent area.. *
13066 2f 0a 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 /. top = get2by
13067 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
13068 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32 ;. nCell = get2
13069 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 byte(&data[hdr+3
1306a 5d 29 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74 ]);. cellOffset
1306b 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 = pPage->cellOf
1306c 66 73 65 74 3b 0a 20 20 69 66 28 20 6e 46 72 61 fset;. if( nFra
1306d 67 3e 3d 36 30 20 7c 7c 20 63 65 6c 6c 4f 66 66 g>=60 || cellOff
1306e 73 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 20 3e 20 set + 2*nCell >
1306f 74 6f 70 20 2d 20 6e 42 79 74 65 20 29 7b 0a 20 top - nByte ){.
13070 20 20 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 defragmentPag
13071 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 74 6f e(pPage);. to
13072 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 p = get2byte(&da
13073 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 0a ta[hdr+5]);. }.
13074 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65 3b 0a top -= nByte;.
13075 20 20 61 73 73 65 72 74 28 20 63 65 6c 6c 4f 66 assert( cellOf
13076 66 73 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 20 3c fset + 2*nCell <
13077 3d 20 74 6f 70 20 29 3b 0a 20 20 70 75 74 32 62 = top );. put2b
13078 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
13079 2c 20 74 6f 70 29 3b 0a 20 20 72 65 74 75 72 6e , top);. return
1307a 20 74 6f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 top;.}../*.** R
1307b 65 74 75 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 eturn a section
1307c 6f 66 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 of the pPage->aD
1307d 61 74 61 20 74 6f 20 74 68 65 20 66 72 65 65 6c ata to the freel
1307e 69 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 ist..** The firs
1307f 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 t byte of the ne
13080 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 w free block is
13081 70 50 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 pPage->aDisk[sta
13082 72 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 rt].** and the s
13083 69 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b ize of the block
13084 20 69 73 20 22 73 69 7a 65 22 20 62 79 74 65 73 is "size" bytes
13085 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 ..**.** Most of
13086 74 68 65 20 65 66 66 6f 72 74 20 68 65 72 65 20 the effort here
13087 69 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 is involved in c
13088 6f 61 6c 65 73 69 6e 67 20 61 64 6a 61 63 65 6e oalesing adjacen
13089 74 0a 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 t.** free blocks
1308a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 into a single b
1308b 69 67 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a ig free block..*
1308c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 /.static int fre
1308d 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a eSpace(MemPage *
1308e 70 50 61 67 65 2c 20 69 6e 74 20 73 74 61 72 74 pPage, int start
1308f 2c 20 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 , int size){. i
13090 6e 74 20 61 64 64 72 2c 20 70 62 65 67 69 6e 2c nt addr, pbegin,
13091 20 68 64 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 hdr;. unsigned
13092 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 char *data = pP
13093 61 67 65 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61 age->aData;.. a
13094 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 ssert( pPage->pB
13095 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 t!=0 );. assert
13096 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
13097 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
13098 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 >pDbPage) );. a
13099 73 73 65 72 74 28 20 73 74 61 72 74 3e 3d 70 50 ssert( start>=pP
1309a 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 age->hdrOffset+6
1309b 2b 28 70 50 61 67 65 2d 3e 6c 65 61 66 3f 30 3a +(pPage->leaf?0:
1309c 34 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 4) );. assert(
1309d 28 73 74 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d (start + size)<=
1309e 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
1309f 6c 65 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 leSize );. asse
130a0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
130a1 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
130a2 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
130a3 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 ssert( size>=0 )
130a4 3b 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 ; /* Minimum c
130a5 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f ell size is 4 */
130a6 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
130a7 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 SECURE_DELETE.
130a8 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 64 65 6c /* Overwrite del
130a9 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e eted information
130aa 20 77 69 74 68 20 7a 65 72 6f 73 20 77 68 65 6e with zeros when
130ab 20 74 68 65 20 53 45 43 55 52 45 5f 44 45 4c 45 the SECURE_DELE
130ac 54 45 20 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 TE . ** option
130ad 69 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f is enabled at co
130ae 6d 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 mpile-time */.
130af 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 73 74 61 memset(&data[sta
130b0 72 74 5d 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23 rt], 0, size);.#
130b1 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20 endif.. /* Add
130b2 74 68 65 20 73 70 61 63 65 20 62 61 63 6b 20 69 the space back i
130b3 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c nto the linked l
130b4 69 73 74 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b ist of freeblock
130b5 73 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70 50 61 s */. hdr = pPa
130b6 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 ge->hdrOffset;.
130b7 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b addr = hdr + 1;
130b8 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 . while( (pbegi
130b9 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 n = get2byte(&da
130ba 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61 72 74 ta[addr]))<start
130bb 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29 7b 0a && pbegin>0 ){.
130bc 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 assert( pbeg
130bd 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e in<=pPage->pBt->
130be 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a usableSize-4 );.
130bf 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 3c 3d if( pbegin<=
130c0 61 64 64 72 20 29 20 7b 0a 20 20 20 20 20 20 72 addr ) {. r
130c1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
130c2 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
130c3 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 67 . addr = pbeg
130c4 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 20 28 20 70 in;. }. if ( p
130c5 62 65 67 69 6e 3e 70 50 61 67 65 2d 3e 70 42 74 begin>pPage->pBt
130c6 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 ->usableSize-4 )
130c7 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 {. return SQ
130c8 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
130c9 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 T;. }. assert(
130ca 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20 pbegin>addr ||
130cb 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 pbegin==0 );. p
130cc 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 ut2byte(&data[ad
130cd 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20 20 70 dr], start);. p
130ce 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74 ut2byte(&data[st
130cf 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20 art], pbegin);.
130d0 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
130d1 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b start+2], size);
130d2 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 . pPage->nFree
130d3 2b 3d 20 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 += size;.. /* C
130d4 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 oalesce adjacent
130d5 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a free blocks */.
130d6 20 20 61 64 64 72 20 3d 20 70 50 61 67 65 2d 3e addr = pPage->
130d7 68 64 72 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20 hdrOffset + 1;.
130d8 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20 while( (pbegin
130d9 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
130da 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20 [addr]))>0 ){.
130db 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 69 int pnext, psi
130dc 7a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ze;. assert(
130dd 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20 pbegin>addr );.
130de 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 assert( pbegi
130df 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 n<=pPage->pBt->u
130e0 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 sableSize-4 );.
130e1 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74 32 62 pnext = get2b
130e2 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
130e3 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 3d 20 ]);. psize =
130e4 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
130e5 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69 begin+2]);. i
130e6 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73 69 7a f( pbegin + psiz
130e7 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 20 26 e + 3 >= pnext &
130e8 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20 & pnext>0 ){.
130e9 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20 70 6e int frag = pn
130ea 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b 70 73 ext - (pbegin+ps
130eb 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ize);. if(
130ec 28 66 72 61 67 3c 30 29 20 7c 7c 20 28 66 72 61 (frag<0) || (fra
130ed 67 3e 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 g>data[pPage->hd
130ee 72 4f 66 66 73 65 74 2b 37 5d 29 20 29 7b 0a 20 rOffset+7]) ){.
130ef 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
130f0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
130f1 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 T;. }.
130f2 20 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 data[pPage->hdr
130f3 4f 66 66 73 65 74 2b 37 5d 20 2d 3d 20 66 72 61 Offset+7] -= fra
130f4 67 3b 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 g;. put2byt
130f5 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c e(&data[pbegin],
130f6 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
130f7 70 6e 65 78 74 5d 29 29 3b 0a 20 20 20 20 20 20 pnext]));.
130f8 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 put2byte(&data[p
130f9 62 65 67 69 6e 2b 32 5d 2c 20 70 6e 65 78 74 2b begin+2], pnext+
130fa 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
130fb 6e 65 78 74 2b 32 5d 29 2d 70 62 65 67 69 6e 29 next+2])-pbegin)
130fc 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
130fd 20 20 20 61 64 64 72 20 3d 20 70 62 65 67 69 6e addr = pbegin
130fe 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
130ff 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f * If the cell co
13100 6e 74 65 6e 74 20 61 72 65 61 20 62 65 67 69 6e ntent area begin
13101 73 20 77 69 74 68 20 61 20 66 72 65 65 62 6c 6f s with a freeblo
13102 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74 2e 20 2a ck, remove it. *
13103 2f 0a 20 20 69 66 28 20 64 61 74 61 5b 68 64 72 /. if( data[hdr
13104 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 35 5d +1]==data[hdr+5]
13105 20 26 26 20 64 61 74 61 5b 68 64 72 2b 32 5d 3d && data[hdr+2]=
13106 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20 29 7b 0a =data[hdr+6] ){.
13107 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 20 int top;.
13108 20 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79 pbegin = get2by
13109 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 te(&data[hdr+1])
1310a 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 ;. memcpy(&da
1310b 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64 61 74 61 ta[hdr+1], &data
1310c 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b 0a 20 20 [pbegin], 2);.
1310d 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 top = get2byte
1310e 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a (&data[hdr+5]);.
1310f 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
13110 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 20 2b ta[hdr+5], top +
13111 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
13112 70 62 65 67 69 6e 2b 32 5d 29 29 3b 0a 20 20 7d pbegin+2]));. }
13113 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
13114 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 _OK;.}../*.** De
13115 63 6f 64 65 20 74 68 65 20 66 6c 61 67 73 20 62 code the flags b
13116 79 74 65 20 28 74 68 65 20 66 69 72 73 74 20 62 yte (the first b
13117 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 yte of the heade
13118 72 29 20 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a r) for a page.**
13119 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 and initialize
1311a 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 fields of the Me
1311b 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 20 mPage structure
1311c 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a accordingly..**.
1311d 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c ** Only the foll
1311e 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f owing combinatio
1311f 6e 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 ns are supported
13120 2e 20 20 41 6e 79 74 68 69 6e 67 20 64 69 66 66 . Anything diff
13121 65 72 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 erent.** indicat
13122 65 73 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 es a corrupt dat
13123 61 62 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a abase files:.**.
13124 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a ** PTF_Z
13125 45 52 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 ERODATA.**
13126 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 PTF_ZERODATA
13127 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 | PTF_LEAF.**
13128 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44 41 PTF_LEAFDA
13129 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a TA | PTF_INTKEY.
1312a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 4c ** PTF_L
1312b 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e EAFDATA | PTF_IN
1312c 54 4b 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a TKEY | PTF_LEAF.
1312d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 */.static int de
1312e 63 6f 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 codeFlags(MemPag
1312f 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c e *pPage, int fl
13130 61 67 42 79 74 65 29 7b 0a 20 20 42 74 53 68 61 agByte){. BtSha
13131 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a red *pBt; /*
13132 20 41 20 63 6f 70 79 20 6f 66 20 70 50 61 67 65 A copy of pPage
13133 2d 3e 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 ->pBt */.. asse
13134 72 74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 rt( pPage->hdrOf
13135 66 73 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 fset==(pPage->pg
13136 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 no==1 ? 100 : 0)
13137 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
13138 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
13139 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
1313a 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e ex) );. pPage->
1313b 6c 65 61 66 20 3d 20 66 6c 61 67 42 79 74 65 3e leaf = flagByte>
1313c 3e 33 3b 20 20 61 73 73 65 72 74 28 20 50 54 46 >3; assert( PTF
1313d 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 20 29 3b _LEAF == 1<<3 );
1313e 0a 20 20 66 6c 61 67 42 79 74 65 20 26 3d 20 7e . flagByte &= ~
1313f 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 50 61 67 PTF_LEAF;. pPag
13140 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 e->childPtrSize
13141 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 = 4-4*pPage->lea
13142 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 f;. pBt = pPage
13143 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 66 6c 61 ->pBt;. if( fla
13144 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c 45 41 46 gByte==(PTF_LEAF
13145 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 DATA | PTF_INTKE
13146 59 29 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d Y) ){. pPage-
13147 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 20 20 >intKey = 1;.
13148 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 pPage->hasData
13149 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 = pPage->leaf;.
1314a 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 pPage->maxLoc
1314b 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 65 61 al = pBt->maxLea
1314c 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 f;. pPage->mi
1314d 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 nLocal = pBt->mi
1314e 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 65 20 69 nLeaf;. }else i
1314f 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 50 54 46 f( flagByte==PTF
13150 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a 20 20 20 _ZERODATA ){.
13151 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d pPage->intKey =
13152 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 0;. pPage->h
13153 61 73 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 asData = 0;.
13154 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 pPage->maxLocal
13155 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b = pBt->maxLocal;
13156 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c . pPage->minL
13157 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c ocal = pBt->minL
13158 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ocal;. }else{.
13159 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1315a 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1315b 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
1315c 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
1315d 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 Initialize the a
1315e 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 uxiliary informa
1315f 74 69 6f 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 tion for a disk
13160 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 block..**.** Ret
13161 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e urn SQLITE_OK on
13162 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 77 65 success. If we
13163 20 73 65 65 20 74 68 61 74 20 74 68 65 20 70 61 see that the pa
13164 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 ge does.** not c
13165 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f ontain a well-fo
13166 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 70 61 rmed database pa
13167 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 ge, then return
13168 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 .** SQLITE_CORRU
13169 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 61 PT. Note that a
1316a 20 72 65 74 75 72 6e 20 6f 66 20 53 51 4c 49 54 return of SQLIT
1316b 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a E_OK does not.**
1316c 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
1316d 74 68 65 20 70 61 67 65 20 69 73 20 77 65 6c 6c the page is well
1316e 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c -formed. It onl
1316f 79 20 73 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20 y shows that.**
13170 77 65 20 66 61 69 6c 65 64 20 74 6f 20 64 65 74 we failed to det
13171 65 63 74 20 61 6e 79 20 63 6f 72 72 75 70 74 69 ect any corrupti
13172 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
13173 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
13174 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d 3BtreeInitPage(M
13175 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a emPage *pPage){.
13176 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13177 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 ->pBt!=0 );. as
13178 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
13179 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1317a 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1317b 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
1317c 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 pgno==sqlite3Pag
1317d 65 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 erPagenumber(pPa
1317e 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
1317f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 assert( pPage
13180 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 == sqlite3PagerG
13181 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 etExtra(pPage->p
13182 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
13183 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 ert( pPage->aDat
13184 61 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 a == sqlite3Page
13185 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e rGetData(pPage->
13186 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 69 pDbPage) );.. i
13187 66 28 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 f( !pPage->isIni
13188 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 63 3b t ){. int pc;
13189 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
1318a 64 64 72 65 73 73 20 6f 66 20 61 20 66 72 65 65 ddress of a free
1318b 62 6c 6f 63 6b 20 77 69 74 68 69 6e 20 70 50 61 block within pPa
1318c 67 65 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 ge->aData[] */.
1318d 20 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 int hdr;
1318e 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
1318f 74 6f 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 to beginning of
13190 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 page header */.
13191 20 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 u8 *data;
13192 20 20 20 20 20 20 2f 2a 20 45 71 75 61 6c 20 74 /* Equal t
13193 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a o pPage->aData *
13194 2f 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a /. BtShared *
13195 70 42 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 pBt; /* T
13196 68 65 20 6d 61 69 6e 20 62 74 72 65 65 20 73 74 he main btree st
13197 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 69 ructure */. i
13198 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 nt usableSize;
13199 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 /* Amount of u
1319a 73 61 62 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 sable space on e
1319b 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 ach page */.
1319c 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 int cellOffset;
1319d 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f /* Offset fro
1319e 6d 20 73 74 61 72 74 20 6f 66 20 70 61 67 65 20 m start of page
1319f 74 6f 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f to first cell po
131a0 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 inter */. int
131a1 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 nFree;
131a2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 /* Number of unu
131a3 73 65 64 20 62 79 74 65 73 20 6f 6e 20 74 68 65 sed bytes on the
131a4 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 69 6e 74 page */. int
131a5 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 top;
131a6 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 /* First byte of
131a7 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
131a8 74 20 61 72 65 61 20 2a 2f 0a 0a 20 20 20 20 70 t area */.. p
131a9 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b Bt = pPage->pBt;
131aa 0a 0a 20 20 20 20 68 64 72 20 3d 20 70 50 61 67 .. hdr = pPag
131ab 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 e->hdrOffset;.
131ac 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e data = pPage->
131ad 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 aData;. if( d
131ae 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 ecodeFlags(pPage
131af 2c 20 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72 , data[hdr]) ) r
131b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
131b1 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 RUPT_BKPT;. a
131b2 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 ssert( pBt->page
131b3 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 Size>=512 && pBt
131b4 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 ->pageSize<=3276
131b5 38 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 8 );. pPage->
131b6 6d 61 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e maskPage = pBt->
131b7 70 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 pageSize - 1;.
131b8 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c pPage->nOverfl
131b9 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 62 ow = 0;. usab
131ba 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 leSize = pBt->us
131bb 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 ableSize;. pP
131bc 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 age->cellOffset
131bd 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 = cellOffset = h
131be 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 dr + 12 - 4*pPag
131bf 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70 e->leaf;. top
131c0 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
131c1 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 a[hdr+5]);. p
131c2 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 Page->nCell = ge
131c3 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
131c4 2b 33 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 +3]);. if( pP
131c5 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 age->nCell>MX_CE
131c6 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 LL(pBt) ){.
131c7 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c /* To many cell
131c8 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 s for a single p
131c9 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d age. The page m
131ca 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a ust be corrupt *
131cb 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 /. return S
131cc 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
131cd 50 54 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 PT;. }. .
131ce 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 /* Compute the
131cf 74 6f 74 61 6c 20 66 72 65 65 20 73 70 61 63 65 total free space
131d0 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a on the page */.
131d1 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 pc = get2byt
131d2 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b e(&data[hdr+1]);
131d3 0a 20 20 20 20 6e 46 72 65 65 20 3d 20 64 61 74 . nFree = dat
131d4 61 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 20 2d a[hdr+7] + top -
131d5 20 28 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 (cellOffset + 2
131d6 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a *pPage->nCell);.
131d7 20 20 20 20 77 68 69 6c 65 28 20 70 63 3e 30 20 while( pc>0
131d8 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 78 ){. int nex
131d9 74 2c 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 69 t, size;. i
131da 66 28 20 70 63 3e 75 73 61 62 6c 65 53 69 7a 65 f( pc>usableSize
131db 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a -4 ){. /*
131dc 20 46 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f Free block is o
131dd 66 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 ff the page */.
131de 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
131df 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
131e0 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 T; . }.
131e1 20 20 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 next = get2byt
131e2 65 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 e(&data[pc]);.
131e3 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 size = get2b
131e4 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 yte(&data[pc+2])
131e5 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 78 74 ;. if( next
131e6 3e 30 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73 >0 && next<=pc+s
131e7 69 7a 65 2b 33 20 29 7b 0a 20 20 20 20 20 20 20 ize+3 ){.
131e8 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 73 20 /* Free blocks
131e9 6d 75 73 74 20 62 65 20 69 6e 20 61 63 63 65 6e must be in accen
131ea 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 20 20 ding order */.
131eb 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
131ec 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
131ed 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ; . }.
131ee 20 6e 46 72 65 65 20 2b 3d 20 73 69 7a 65 3b 0a nFree += size;.
131ef 20 20 20 20 20 20 70 63 20 3d 20 6e 65 78 74 3b pc = next;
131f0 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
131f1 2d 3e 6e 46 72 65 65 20 3d 20 6e 46 72 65 65 3b ->nFree = nFree;
131f2 0a 20 20 20 20 69 66 28 20 6e 46 72 65 65 3e 3d . if( nFree>=
131f3 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 usableSize ){.
131f4 20 20 20 20 2f 2a 20 46 72 65 65 20 73 70 61 63 /* Free spac
131f5 65 20 63 61 6e 6e 6f 74 20 65 78 63 65 65 64 20 e cannot exceed
131f6 74 6f 74 61 6c 20 70 61 67 65 20 73 69 7a 65 20 total page size
131f7 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 */. return
131f8 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
131f9 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 0a 23 69 66 KPT; . }..#if
131fa 20 30 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 0. /* Check th
131fb 61 74 20 61 6c 6c 20 74 68 65 20 6f 66 66 73 65 at all the offse
131fc 74 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 6f ts in the cell o
131fd 66 66 73 65 74 20 61 72 72 61 79 20 61 72 65 20 ffset array are
131fe 77 69 74 68 69 6e 20 72 61 6e 67 65 2e 20 0a 20 within range. .
131ff 20 2a 2a 20 0a 20 20 2a 2a 20 4f 6d 69 74 74 69 ** . ** Omitti
13200 6e 67 20 74 68 69 73 20 63 6f 6e 73 69 73 74 65 ng this consiste
13201 6e 63 79 20 63 68 65 63 6b 20 61 6e 64 20 75 73 ncy check and us
13202 69 6e 67 20 74 68 65 20 70 50 61 67 65 2d 3e 6d ing the pPage->m
13203 61 73 6b 50 61 67 65 20 6d 61 73 6b 0a 20 20 2a askPage mask. *
13204 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 6f 76 65 * to prevent ove
13205 72 72 75 6e 6e 69 6e 67 20 74 68 65 20 70 61 67 rrunning the pag
13206 65 20 62 75 66 66 65 72 20 69 6e 20 66 69 6e 64 e buffer in find
13207 43 65 6c 6c 28 29 20 72 65 73 75 6c 74 73 20 69 Cell() results i
13208 6e 20 61 0a 20 20 2a 2a 20 32 2e 35 25 20 70 65 n a. ** 2.5% pe
13209 72 66 6f 72 6d 61 6e 63 65 20 67 61 69 6e 2e 0a rformance gain..
1320a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 38 20 */. {. u8
1320b 2a 70 4f 66 66 3b 20 20 20 20 20 20 20 20 2f 2a *pOff; /*
1320c 20 49 74 65 72 61 74 6f 72 20 75 73 65 64 20 74 Iterator used t
1320d 6f 20 63 68 65 63 6b 20 61 6c 6c 20 63 65 6c 6c o check all cell
1320e 20 6f 66 66 73 65 74 73 20 61 72 65 20 69 6e 20 offsets are in
1320f 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20 75 38 20 range */. u8
13210 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a *pEnd; /*
13211 20 50 6f 69 6e 74 65 72 20 74 6f 20 65 6e 64 20 Pointer to end
13212 6f 66 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61 of cell offset a
13213 72 72 61 79 20 2a 2f 0a 20 20 20 20 75 38 20 6d rray */. u8 m
13214 61 73 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ask; /*
13215 4d 61 73 6b 20 6f 66 20 62 69 74 73 20 74 68 61 Mask of bits tha
13216 74 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 20 69 t must be zero i
13217 6e 20 4d 53 42 20 6f 66 20 63 65 6c 6c 20 6f 66 n MSB of cell of
13218 66 73 65 74 73 20 2a 2f 0a 20 20 20 20 6d 61 73 fsets */. mas
13219 6b 20 3d 20 7e 28 28 28 75 38 29 28 70 42 74 2d k = ~(((u8)(pBt-
1321a 3e 70 61 67 65 53 69 7a 65 3e 3e 38 29 29 2d 31 >pageSize>>8))-1
1321b 29 3b 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 64 );. pEnd = &d
1321c 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b ata[cellOffset +
1321d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2a 32 5d pPage->nCell*2]
1321e 3b 0a 20 20 20 20 66 6f 72 28 70 4f 66 66 3d 26 ;. for(pOff=&
1321f 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 5d data[cellOffset]
13220 3b 20 70 4f 66 66 21 3d 70 45 6e 64 20 26 26 20 ; pOff!=pEnd &&
13221 21 28 28 2a 70 4f 66 66 29 26 6d 61 73 6b 29 3b !((*pOff)&mask);
13222 20 70 4f 66 66 2b 3d 32 29 3b 0a 20 20 20 20 69 pOff+=2);. i
13223 66 28 20 70 4f 66 66 21 3d 70 45 6e 64 20 29 7b f( pOff!=pEnd ){
13224 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
13225 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
13226 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e T;. }. }.#en
13227 64 69 66 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e dif.. pPage->
13228 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a isInit = 1;. }.
13229 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1322a 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 OK;.}../*.** Set
1322b 20 75 70 20 61 20 72 61 77 20 70 61 67 65 20 73 up a raw page s
1322c 6f 20 74 68 61 74 20 69 74 20 6c 6f 6f 6b 73 20 o that it looks
1322d 6c 69 6b 65 20 61 20 64 61 74 61 62 61 73 65 20 like a database
1322e 70 61 67 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 page holding.**
1322f 6e 6f 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 no entries..*/.s
13230 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f 50 tatic void zeroP
13231 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 age(MemPage *pPa
13232 67 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a ge, int flags){.
13233 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
13234 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 *data = pPage->a
13235 44 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64 Data;. BtShared
13236 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 *pBt = pPage->p
13237 42 74 3b 0a 20 20 69 6e 74 20 68 64 72 20 3d 20 Bt;. int hdr =
13238 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
13239 3b 0a 20 20 69 6e 74 20 66 69 72 73 74 3b 0a 0a ;. int first;..
1323a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1323b 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 3PagerPagenumber
1323c 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
1323d 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 29 3b ==pPage->pgno );
1323e 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1323f 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
13240 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
13241 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20 == (void*)pPage
13242 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
13243 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
13244 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
13245 20 3d 3d 20 64 61 74 61 20 29 3b 0a 20 20 61 73 == data );. as
13246 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
13247 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
13248 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
13249 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1324a 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
1324b 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f t->mutex) );. /
1324c 2a 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64 *memset(&data[hd
1324d 72 5d 2c 20 30 2c 20 70 42 74 2d 3e 75 73 61 62 r], 0, pBt->usab
1324e 6c 65 53 69 7a 65 20 2d 20 68 64 72 29 3b 2a 2f leSize - hdr);*/
1324f 0a 20 20 64 61 74 61 5b 68 64 72 5d 20 3d 20 66 . data[hdr] = f
13250 6c 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 lags;. first =
13251 68 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c hdr + 8 + 4*((fl
13252 61 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 ags&PTF_LEAF)==0
13253 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 );. memset(&dat
13254 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 3b a[hdr+1], 0, 4);
13255 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d . data[hdr+7] =
13256 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 0;. put2byte(&
13257 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 74 data[hdr+5], pBt
13258 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 ->usableSize);.
13259 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 pPage->nFree =
1325a 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
1325b 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 63 6f 64 - first;. decod
1325c 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c eFlags(pPage, fl
1325d 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e 68 ags);. pPage->h
1325e 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0a drOffset = hdr;.
1325f 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 pPage->cellOff
13260 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70 set = first;. p
13261 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
13262 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 = 0;. assert( p
13263 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 Bt->pageSize>=51
13264 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 2 && pBt->pageSi
13265 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 70 ze<=32768 );. p
13266 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d Page->maskPage =
13267 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d pBt->pageSize -
13268 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 1;. pPage->nCe
13269 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d ll = 0;. pPage-
1326a 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a >isInit = 1;.}..
1326b 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
1326c 20 44 62 50 61 67 65 20 6f 62 74 61 69 6e 65 64 DbPage obtained
1326d 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 from the pager
1326e 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 75 into a MemPage u
1326f 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 74 sed by.** the bt
13270 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74 ree layer..*/.st
13271 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 atic MemPage *bt
13272 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
13273 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 e(DbPage *pDbPag
13274 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74 e, Pgno pgno, Bt
13275 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
13276 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d MemPage *pPage =
13277 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 74 (MemPage*)sqlit
13278 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
13279 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 pDbPage);. pPag
1327a 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 74 e->aData = sqlit
1327b 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
1327c 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 DbPage);. pPage
1327d 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 44 62 50 ->pDbPage = pDbP
1327e 61 67 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42 age;. pPage->pB
1327f 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 65 t = pBt;. pPage
13280 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 ->pgno = pgno;.
13281 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
13282 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d t = pPage->pgno=
13283 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20 =1 ? 100 : 0;.
13284 72 65 74 75 72 6e 20 70 50 61 67 65 3b 20 0a 7d return pPage; .}
13285 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 ../*.** Get a pa
13286 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ge from the page
13287 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 r. Initialize t
13288 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 he MemPage.pBt a
13289 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 nd.** MemPage.aD
1328a 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 ata elements if
1328b 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 needed..**.** If
1328c 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66 the noContent f
1328d 6c 61 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d lag is set, it m
1328e 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 eans that we do
1328f 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 0a 2a not care about.*
13290 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 * the content of
13291 20 74 68 65 20 70 61 67 65 20 61 74 20 74 68 69 the page at thi
13292 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e s time. So do n
13293 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 ot go to the dis
13294 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68 k.** to fetch th
13295 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74 e content. Just
13296 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e fill in the con
13297 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20 tent with zeros
13298 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69 for now..** If i
13299 6e 20 74 68 65 20 66 75 74 75 72 65 20 77 65 20 n the future we
1329a 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 call sqlite3Page
1329b 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73 rWrite() on this
1329c 20 70 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d page, that.** m
1329d 65 61 6e 73 20 77 65 20 68 61 76 65 20 73 74 61 eans we have sta
1329e 72 74 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65 rted to be conce
1329f 72 6e 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65 rned about conte
132a0 6e 74 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a nt and the disk.
132a1 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f ** read should o
132a2 63 63 75 72 20 61 74 20 74 68 61 74 20 70 6f 69 ccur at that poi
132a3 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
132a4 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
132a5 33 42 74 72 65 65 47 65 74 50 61 67 65 28 0a 20 3BtreeGetPage(.
132a6 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
132a7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 /* The btr
132a8 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e ee */. Pgno pgn
132a9 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 o, /*
132aa 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 Number of the pa
132ab 67 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 ge to fetch */.
132ac 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 MemPage **ppPag
132ad 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 e, /* Return
132ae 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 the page in this
132af 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 parameter */.
132b0 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 int noContent
132b1 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6c /* Do not l
132b2 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 oad page content
132b3 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 if true */.){.
132b4 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61 67 int rc;. DbPag
132b5 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20 61 e *pDbPage;.. a
132b6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
132b7 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
132b8 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 utex) );. rc =
132b9 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 sqlite3PagerAcqu
132ba 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ire(pBt->pPager,
132bb 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a 2a pgno, (DbPage**
132bc 29 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f 6e )&pDbPage, noCon
132bd 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 20 tent);. if( rc
132be 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a ) return rc;. *
132bf 70 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 61 ppPage = btreePa
132c0 67 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62 geFromDbPage(pDb
132c1 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29 Page, pgno, pBt)
132c2 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
132c3 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 E_OK;.}../*.** R
132c4 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f eturn the size o
132c5 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
132c6 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49 66 ile in pages. If
132c7 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b 69 there is any ki
132c8 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c 20 nd of.** error,
132c9 72 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e 65 return ((unsigne
132ca 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73 74 d int)-1)..*/.st
132cb 61 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72 50 atic Pgno pagerP
132cc 61 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72 65 agecount(BtShare
132cd 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 6e d *pBt){. int n
132ce 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 Page = -1;. int
132cf 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 rc;. assert( p
132d0 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 20 Bt->pPage1 );.
132d1 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
132d2 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e rPagecount(pBt->
132d3 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b pPager, &nPage);
132d4 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
132d5 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 67 QLITE_OK || nPag
132d6 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75 72 e==-1 );. retur
132d7 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 7d n (Pgno)nPage;.}
132d8 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 ../*.** Get a pa
132d9 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ge from the page
132da 72 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 r and initialize
132db 20 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 it. This routi
132dc 6e 65 0a 2a 2a 20 69 73 20 6a 75 73 74 20 61 20 ne.** is just a
132dd 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 convenience wrap
132de 70 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61 72 per around separ
132df 61 74 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 ate calls to.**
132e0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 sqlite3BtreeGetP
132e1 61 67 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 age() and sqlite
132e2 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 3BtreeInitPage()
132e3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
132e4 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 0a getAndInitPage(.
132e5 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c BtShared *pBt,
132e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
132e7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
132e8 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 /. Pgno pgno,
132e9 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
132ea 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 er of the page t
132eb 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 o get */. MemPa
132ec 67 65 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20 ge **ppPage
132ed 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 /* Write the pag
132ee 65 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20 2a e pointer here *
132ef 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
132f0 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 DbPage *pDbPage
132f1 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
132f2 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ge;.. assert( s
132f3 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
132f4 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
132f5 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 . if( pgno==0 )
132f6 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
132f7 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
132f8 3b 20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20 ; . }.. /* It
132f9 69 73 20 6f 66 74 65 6e 20 74 68 65 20 63 61 73 is often the cas
132fa 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
132fb 77 65 20 77 61 6e 74 20 69 73 20 61 6c 72 65 61 we want is alrea
132fc 64 79 20 69 6e 20 63 61 63 68 65 2e 0a 20 20 2a dy in cache.. *
132fd 2a 20 49 66 20 73 6f 2c 20 67 65 74 20 69 74 20 * If so, get it
132fe 64 69 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 directly. This
132ff 73 61 76 65 73 20 75 73 20 66 72 6f 6d 20 68 61 saves us from ha
13300 76 69 6e 67 20 74 6f 20 63 61 6c 6c 0a 20 20 2a ving to call. *
13301 2a 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 * pagerPagecount
13302 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 () to make sure
13303 70 67 6e 6f 20 69 73 20 77 69 74 68 69 6e 20 6c pgno is within l
13304 69 6d 69 74 73 2c 20 77 68 69 63 68 20 72 65 73 imits, which res
13305 75 6c 74 73 0a 20 20 2a 2a 20 69 6e 20 61 20 6d ults. ** in a m
13306 65 61 73 75 72 65 61 62 6c 65 20 70 65 72 66 6f easureable perfo
13307 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 rmance improveme
13308 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 44 62 50 nts.. */. pDbP
13309 61 67 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 age = sqlite3Pag
1330a 65 72 4c 6f 6f 6b 75 70 28 70 42 74 2d 3e 70 50 erLookup(pBt->pP
1330b 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 ager, pgno);. i
1330c 66 28 20 70 44 62 50 61 67 65 20 29 7b 0a 20 20 f( pDbPage ){.
1330d 20 20 2f 2a 20 50 61 67 65 20 69 73 20 61 6c 72 /* Page is alr
1330e 65 61 64 79 20 69 6e 20 63 61 63 68 65 20 2a 2f eady in cache */
1330f 0a 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 . *ppPage = p
13310 50 61 67 65 20 3d 20 62 74 72 65 65 50 61 67 65 Page = btreePage
13311 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62 50 61 FromDbPage(pDbPa
13312 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a ge, pgno, pBt);.
13313 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
13314 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
13315 20 2f 2a 20 50 61 67 65 20 6e 6f 74 20 69 6e 20 /* Page not in
13316 63 61 63 68 65 2e 20 20 41 63 71 75 69 72 65 20 cache. Acquire
13317 69 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 it. */. if( p
13318 67 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75 gno>pagerPagecou
13319 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 nt(pBt) ){.
1331a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1331b 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 ORRUPT_BKPT; .
1331c 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
1331d 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
1331e 28 70 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61 (pBt, pgno, ppPa
1331f 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 ge, 0);. if(
13320 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
13321 20 20 20 20 70 50 61 67 65 20 3d 20 2a 70 70 50 pPage = *ppP
13322 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 age;. }. if( !
13323 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b pPage->isInit ){
13324 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
13325 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 3BtreeInitPage(p
13326 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 Page);. }. if(
13327 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13328 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 {. releasePag
13329 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 2a 70 e(pPage);. *p
1332a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 pPage = 0;. }.
1332b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1332c 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 4d *.** Release a M
1332d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20 73 68 emPage. This sh
1332e 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 6f ould be called o
1332f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70 72 69 nce for each pri
13330 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 or.** call to sq
13331 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
13332 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
13333 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 d releasePage(Me
13334 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 mPage *pPage){.
13335 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 if( pPage ){.
13336 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
13337 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 >aData );. as
13338 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 sert( pPage->pBt
13339 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1333a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
1333b 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 xtra(pPage->pDbP
1333c 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 age) == (void*)p
1333d 50 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65 Page );. asse
1333e 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
1333f 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 GetData(pPage->p
13340 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e DbPage)==pPage->
13341 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 aData );. ass
13342 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
13343 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
13344 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
13345 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
13346 72 65 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ref(pPage->pDbPa
13347 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ge);. }.}../*.*
13348 2a 20 44 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 * During a rollb
13349 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 20 70 61 ack, when the pa
1334a 67 65 72 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f ger reloads info
1334b 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 rmation into the
1334c 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 cache.** so tha
1334d 74 20 74 68 65 20 63 61 63 68 65 20 69 73 20 72 t the cache is r
1334e 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 6f estored to its o
1334f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 61 74 riginal state at
13350 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a the start of.**
13351 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
13352 2c 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 , for each page
13353 72 65 73 74 6f 72 65 64 20 74 68 69 73 20 72 6f restored this ro
13354 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
13355 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
13356 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 72 65 73 ine needs to res
13357 65 74 20 74 68 65 20 65 78 74 72 61 20 64 61 74 et the extra dat
13358 61 20 73 65 63 74 69 6f 6e 20 61 74 20 74 68 65 a section at the
13359 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 end of the.** p
1335a 61 67 65 20 74 6f 20 61 67 72 65 65 20 77 69 74 age to agree wit
1335b 68 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 64 h the restored d
1335c 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ata..*/.static v
1335d 6f 69 64 20 70 61 67 65 52 65 69 6e 69 74 28 44 oid pageReinit(D
1335e 62 50 61 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 bPage *pData){.
1335f 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
13360 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 . pPage = (MemP
13361 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67 age *)sqlite3Pag
13362 65 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61 erGetExtra(pData
13363 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e );. if( pPage->
13364 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 isInit ){. as
13365 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
13366 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
13367 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
13368 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 pPage->isInit
13369 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 = 0;. if( sq
1336a 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 lite3PagerPageRe
1336b 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 30 20 fcount(pData)>0
1336c 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1336d 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 BtreeInitPage(pP
1336e 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a age);. }. }.
1336f 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 }../*.** Invoke
13370 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 the busy handler
13371 20 66 6f 72 20 61 20 62 74 72 65 65 2e 0a 2a 2f for a btree..*/
13372 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
13373 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c eInvokeBusyHandl
13374 65 72 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a er(void *pArg){.
13375 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
13376 3d 20 28 42 74 53 68 61 72 65 64 2a 29 70 41 72 = (BtShared*)pAr
13377 67 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 g;. assert( pBt
13378 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 ->db );. assert
13379 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1337a 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75 held(pBt->db->mu
1337b 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e tex) );. return
1337c 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 sqlite3InvokeBu
1337d 73 79 48 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e syHandler(&pBt->
1337e 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 29 db->busyHandler)
1337f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
13380 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e a database file.
13381 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d .** .** zFilenam
13382 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 e is the name of
13383 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13384 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d le. If zFilenam
13385 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20 6e e is NULL.** a n
13386 65 77 20 64 61 74 61 62 61 73 65 20 77 69 74 68 ew database with
13387 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20 69 a random name i
13388 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 s created. This
13389 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64 0a randomly named.
1338a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
1338b 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 will be deleted
1338c 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 74 72 when sqlite3Btr
1338d 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c eeClose() is cal
1338e 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 led..** If zFile
1338f 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 name is ":memory
13390 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 :" then an in-me
13391 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73 mory database is
13392 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74 created.** that
13393 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c is automaticall
13394 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e y destroyed when
13395 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a it is closed..*
13396 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13397 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
13398 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 eOpen(. const c
13399 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
1339a 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1339b 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 file containing
1339c 74 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61 the BTree databa
1339d 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 se */. sqlite3
1339e 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
1339f 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 /* Associated da
133a0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f tabase handle */
133a1 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 . Btree **ppBtr
133a2 65 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f ee, /* Po
133a3 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74 72 inter to new Btr
133a4 65 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74 65 ee object writte
133a5 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 n here */. int
133a6 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
133a7 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a /* Options *
133a8 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 /. int vfsFlags
133a9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
133aa 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f lags passed thro
133ab 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 ugh to sqlite3_v
133ac 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b fs.xOpen() */.){
133ad 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
133ae 70 56 66 73 3b 20 20 20 20 20 20 2f 2a 20 54 68 pVfs; /* Th
133af 65 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 e VFS to use for
133b0 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 this btree */.
133b1 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
133b2 20 30 3b 20 20 20 20 20 20 2f 2a 20 53 68 61 72 0; /* Shar
133b3 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 65 ed part of btree
133b4 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 structure */.
133b5 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 20 Btree *p;
133b6 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c /* Handl
133b7 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 e to return */.
133b8 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
133b9 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 _OK;. int nRese
133ba 72 76 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 rve;. unsigned
133bb 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31 char zDbHeader[1
133bc 30 30 5d 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 00];.. /* Set t
133bd 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 he variable isMe
133be 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 mdb to true for
133bf 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
133c0 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 abase, or . **
133c1 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 false for a file
133c2 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e -based database.
133c3 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20 This symbol is
133c4 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 only required if
133c5 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20 . ** either of
133c6 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 the shared-data
133c7 6f 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65 or autovacuum fe
133c8 61 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69 atures are compi
133c9 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 led . ** into t
133ca 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f he library.. */
133cb 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
133cc 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
133cd 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69 _CACHE) || !defi
133ce 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
133cf 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69 AUTOVACUUM). #i
133d0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
133d1 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f _MEMORYDB. co
133d2 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 nst int isMemdb
133d3 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 = 0;. #else.
133d4 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d const int isMem
133d5 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26 db = zFilename &
133d6 26 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e & !strcmp(zFilen
133d7 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 ame, ":memory:")
133d8 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69 ;. #endif.#endi
133d9 66 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 f.. assert( db!
133da 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
133db 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
133dc 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b ld(db->mutex) );
133dd 0a 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70 .. pVfs = db->p
133de 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 Vfs;. p = sqlit
133df 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a e3MallocZero(siz
133e0 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 eof(Btree));. i
133e1 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 f( !p ){. ret
133e2 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
133e3 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 ;. }. p->inTra
133e4 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b ns = TRANS_NONE;
133e5 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a . p->db = db;..
133e6 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
133e7 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
133e8 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e CACHE) && !defin
133e9 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ed(SQLITE_OMIT_D
133ea 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a ISKIO). /*. **
133eb 20 49 66 20 74 68 69 73 20 42 74 72 65 65 20 69 If this Btree i
133ec 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f s a candidate fo
133ed 72 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 r shared cache,
133ee 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 try to find an.
133ef 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53 ** existing BtS
133f0 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61 hared object tha
133f1 74 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 77 t we can share w
133f2 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 ith. */. if( i
133f3 73 4d 65 6d 64 62 3d 3d 30 0a 20 20 20 26 26 20 sMemdb==0. &&
133f4 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c (db->flags & SQL
133f5 49 54 45 5f 56 74 61 62 29 3d 3d 30 0a 20 20 20 ITE_Vtab)==0.
133f6 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 && zFilename &&
133f7 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 0a 20 20 29 zFilename[0]. )
133f8 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
133f9 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 68 3GlobalConfig.sh
133fa 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 aredCacheEnabled
133fb 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 ){. int nF
133fc 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 70 56 ullPathname = pV
133fd 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 fs->mxPathname+1
133fe 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 46 ;. char *zF
133ff 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 ullPathname = sq
13400 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 46 75 6c lite3Malloc(nFul
13401 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 lPathname);.
13402 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
13403 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20 20 *mutexShared;.
13404 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 p->sharable
13405 3d 20 31 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 = 1;. db->f
13406 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 lags |= SQLITE_S
13407 68 61 72 65 64 43 61 63 68 65 3b 0a 20 20 20 20 haredCache;.
13408 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68 if( !zFullPath
13409 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 name ){.
1340a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
1340b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1340c 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1340d 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
1340e 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d te3OsFullPathnam
1340f 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d e(pVfs, zFilenam
13410 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 e, nFullPathname
13411 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 , zFullPathname)
13412 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 ;. mutexSha
13413 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 red = sqlite3Mut
13414 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
13415 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
13416 45 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ER);. sqlit
13417 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
13418 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 utexShared);.
13419 20 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 41 for(pBt=GLOBA
1341a 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 L(BtShared*,sqli
1341b 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
1341c 73 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 42 st); pBt; pBt=pB
1341d 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 t->pNext){.
1341e 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
1341f 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 20 nRef>0 );.
13420 20 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 28 if( 0==strcmp(
13421 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 73 zFullPathname, s
13422 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e qlite3PagerFilen
13423 61 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 ame(pBt->pPager)
13424 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ).
13425 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 && sqlite3Pag
13426 65 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67 65 erVfs(pBt->pPage
13427 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 r)==pVfs ){.
13428 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 p->pBt = p
13429 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 Bt;. pB
1342a 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 t->nRef++;.
1342b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1342c 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1342d 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1342e 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 x_leave(mutexSha
1342f 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 red);. sqli
13430 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 te3_free(zFullPa
13431 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 thname);. }.#
13432 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
13433 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 UG. else{.
13434 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d /* In debug m
13435 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c ode, we mark all
13436 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 74 61 persistent data
13437 62 61 73 65 73 20 61 73 20 73 68 61 72 61 62 6c bases as sharabl
13438 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 e. ** even
13439 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f when they are no
1343a 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 69 73 t. This exercis
1343b 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 es the locking c
1343c 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a ode and. **
1343d 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f gives more oppo
1343e 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65 rtunity for asse
1343f 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 rts(sqlite3_mute
13440 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20 20 x_held()).
13441 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f ** statements to
13442 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72 find locking pr
13443 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f oblems.. */
13444 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 . p->sharab
13445 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 le = 1;. }.#e
13446 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ndif. }.#endif.
13447 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a if( pBt==0 ){.
13448 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 /*. ** Th
13449 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 e following asse
1344a 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68 rts make sure th
1344b 61 74 20 73 74 72 75 63 74 75 72 65 73 20 75 73 at structures us
1344c 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20 ed by the btree
1344d 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 are. ** the r
1344e 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73 ight size. This
1344f 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 61 is to guard aga
13450 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65 inst size change
13451 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20 s that result.
13452 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c ** when compil
13453 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65 ing on a differe
13454 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 65 2e nt architecture.
13455 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
13456 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29 3d rt( sizeof(i64)=
13457 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34 =8 || sizeof(i64
13458 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 )==4 );. asse
13459 72 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d rt( sizeof(u64)=
1345a 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34 =8 || sizeof(u64
1345b 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 )==4 );. asse
1345c 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d rt( sizeof(u32)=
1345d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =4 );. assert
1345e 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32 ( sizeof(u16)==2
1345f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
13460 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 sizeof(Pgno)==4
13461 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 3d 20 );. . pBt =
13462 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
13463 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 20 o( sizeof(*pBt)
13464 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d );. if( pBt==
13465 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
13466 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
13467 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f goto btree_o
13468 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
13469 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1346a 61 67 65 72 4f 70 65 6e 28 70 56 66 73 2c 20 26 agerOpen(pVfs, &
1346b 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69 pBt->pPager, zFi
1346c 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 lename,.
1346d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1346e 20 20 45 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c EXTRA_SIZE, fl
1346f 61 67 73 2c 20 76 66 73 46 6c 61 67 73 29 3b 0a ags, vfsFlags);.
13470 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
13471 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
13472 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
13473 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 70 ReadFileheader(p
13474 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f Bt->pPager,sizeo
13475 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44 62 f(zDbHeader),zDb
13476 48 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 Header);. }.
13477 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
13478 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f E_OK ){. go
13479 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 to btree_open_ou
1347a 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c t;. }. sql
1347b 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 ite3PagerSetBusy
1347c 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 handler(pBt->pPa
1347d 67 65 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 ger, btreeInvoke
1347e 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 BusyHandler, pBt
1347f 29 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 );. p->pBt =
13480 70 42 74 3b 0a 20 20 0a 20 20 20 20 73 71 6c 69 pBt;. . sqli
13481 74 65 33 50 61 67 65 72 53 65 74 52 65 69 6e 69 te3PagerSetReini
13482 74 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ter(pBt->pPager,
13483 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 pageReinit);.
13484 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d pBt->pCursor =
13485 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 0;. pBt->pPa
13486 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 ge1 = 0;. pBt
13487 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 73 71 6c ->readOnly = sql
13488 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f ite3PagerIsreado
13489 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72 29 nly(pBt->pPager)
1348a 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 ;. pBt->pageS
1348b 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 ize = get2byte(&
1348c 7a 44 62 48 65 61 64 65 72 5b 31 36 5d 29 3b 0a zDbHeader[16]);.
1348d 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 if( pBt->pag
1348e 65 53 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 eSize<512 || pBt
1348f 2d 3e 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 ->pageSize>SQLIT
13490 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a E_MAX_PAGE_SIZE.
13491 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 70 42 || ((pB
13492 74 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 70 t->pageSize-1)&p
13493 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 Bt->pageSize)!=0
13494 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 ){. pBt->p
13495 61 67 65 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 ageSize = 0;.
13496 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 sqlite3PagerS
13497 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e etPagesize(pBt->
13498 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 pPager, &pBt->pa
13499 67 65 53 69 7a 65 29 3b 0a 23 69 66 6e 64 65 66 geSize);.#ifndef
1349a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1349b 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a OVACUUM. /*
1349c 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e 61 If the magic na
1349d 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 me ":memory:" wi
1349e 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e 2d ll create an in-
1349f 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c memory database,
134a0 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6c then. ** l
134a1 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 63 eave the autoVac
134a2 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 64 uum mode at 0 (d
134a3 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 75 o not auto-vacuu
134a4 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 m), even if.
134a5 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 ** SQLITE_DEFA
134a6 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69 ULT_AUTOVACUUM i
134a7 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f s true. On the o
134a8 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 20 ther hand, if.
134a9 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d ** SQLITE_OM
134aa 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20 IT_MEMORYDB has
134ab 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68 been defined, th
134ac 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 en ":memory:" is
134ad 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a 2a just a. **
134ae 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61 regular file-na
134af 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 me. In this case
134b0 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d the auto-vacuum
134b1 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 20 applies as per
134b2 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a 2f normal.. */
134b3 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 . if( zFile
134b4 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62 name && !isMemdb
134b5 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d ){. pBt-
134b6 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 53 >autoVacuum = (S
134b7 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 QLITE_DEFAULT_AU
134b8 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 30 TOVACUUM ? 1 : 0
134b9 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e );. pBt->
134ba 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 51 incrVacuum = (SQ
134bb 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 LITE_DEFAULT_AUT
134bc 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 3a OVACUUM==2 ? 1 :
134bd 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 0);. }.#en
134be 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 72 dif. nReser
134bf 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ve = 0;. }els
134c0 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76 e{. nReserv
134c1 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 30 e = zDbHeader[20
134c2 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 ];. pBt->pa
134c3 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b geSizeFixed = 1;
134c4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
134c5 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
134c6 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 pBt->autoV
134c7 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 acuum = (get4byt
134c8 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 e(&zDbHeader[36
134c9 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 + 4*4])?1:0);.
134ca 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 pBt->incrVac
134cb 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 uum = (get4byte(
134cc 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 &zDbHeader[36 +
134cd 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 7*4])?1:0);.#end
134ce 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 if. }. pBt
134cf 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 ->usableSize = p
134d0 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 6e Bt->pageSize - n
134d1 52 65 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 Reserve;. ass
134d2 65 72 74 28 20 28 70 42 74 2d 3e 70 61 67 65 53 ert( (pBt->pageS
134d3 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 20 20 ize & 7)==0 );
134d4 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d /* 8-byte alignm
134d5 65 6e 74 20 6f 66 20 70 61 67 65 53 69 7a 65 20 ent of pageSize
134d6 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 */. sqlite3Pa
134d7 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 gerSetPagesize(p
134d8 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 Bt->pPager, &pBt
134d9 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
134da 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
134db 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
134dc 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 _CACHE) && !defi
134dd 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
134de 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 DISKIO). /* A
134df 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 61 dd the new BtSha
134e0 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 red object to th
134e1 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 e linked list sh
134e2 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 73 arable BtShareds
134e3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
134e4 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a p->sharable ){.
134e5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
134e6 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 tex *mutexShared
134e7 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 ;. pBt->nRe
134e8 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 75 74 f = 1;. mut
134e9 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74 exShared = sqlit
134ea 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
134eb 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
134ec 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 _MASTER);.
134ed 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 41 if( SQLITE_THREA
134ee 44 53 41 46 45 20 26 26 20 73 71 6c 69 74 65 33 DSAFE && sqlite3
134ef 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f GlobalConfig.bCo
134f0 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 20 reMutex ){.
134f1 20 20 20 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20 pBt->mutex =
134f2 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
134f3 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 c(SQLITE_MUTEX_F
134f4 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 69 66 AST);. if
134f5 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20 ( pBt->mutex==0
134f6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
134f7 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
134f8 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 db->ma
134f9 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a llocFailed = 0;.
134fa 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 goto b
134fb 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 tree_open_out;.
134fc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
134fd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
134fe 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
134ff 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70 Shared);. p
13500 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42 Bt->pNext = GLOB
13501 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
13502 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
13503 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42 ist);. GLOB
13504 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
13505 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
13506 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 20 20 ist) = pBt;.
13507 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
13508 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 leave(mutexShare
13509 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 d);. }.#endif
1350a 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e . }..#if !defin
1350b 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
1350c 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 HARED_CACHE) &&
1350d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1350e 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f OMIT_DISKIO). /
1350f 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 74 72 * If the new Btr
13510 65 65 20 75 73 65 73 20 61 20 73 68 61 72 61 62 ee uses a sharab
13511 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 74 68 le pBtShared, th
13512 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a en link the new.
13513 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 6f 20 ** Btree into
13514 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 the list of all
13515 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20 sharable Btrees
13516 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 6f 6e for the same con
13517 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 nection.. ** Th
13518 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 20 69 e list is kept i
13519 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
1351a 72 20 62 79 20 70 42 74 20 61 64 64 72 65 73 73 r by pBt address
1351b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
1351c 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
1351d 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 65 65 int i;. Btree
1351e 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f 72 28 *pSib;. for(
1351f 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
13520 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 i++){. if(
13521 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b (pSib = db->aDb[
13522 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70 53 i].pBt)!=0 && pS
13523 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a ib->sharable ){.
13524 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 while( p
13525 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70 53 Sib->pPrev ){ pS
13526 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65 76 ib = pSib->pPrev
13527 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 ; }. if(
13528 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 42 74 p->pBt<pSib->pBt
13529 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d ){. p-
1352a 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b 0a 20 >pNext = pSib;.
1352b 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 65 p->pPre
1352c 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 v = 0;.
1352d 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d 20 70 pSib->pPrev = p
1352e 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
1352f 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
13530 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 26 26 ( pSib->pNext &&
13531 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 pSib->pNext->pB
13532 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 t<p->pBt ){.
13533 20 20 20 20 20 20 20 20 70 53 69 62 20 3d 20 70 pSib = p
13534 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Sib->pNext;.
13535 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13536 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 p->pNext = pSi
13537 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 b->pNext;.
13538 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 70 p->pPrev = p
13539 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69 Sib;. i
1353a 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 f( p->pNext ){.
1353b 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e p->pN
1353c 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a ext->pPrev = p;.
1353d 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1353e 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65 78 pSib->pNex
1353f 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 7d t = p;. }
13540 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
13541 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
13542 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 42 74 }.#endif. *ppBt
13543 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f ree = p;..btree_
13544 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 open_out:. if(
13545 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
13546 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26 20 . if( pBt &&
13547 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 pBt->pPager ){.
13548 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
13549 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 rClose(pBt->pPag
1354a 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 er);. }. s
1354b 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 qlite3_free(pBt)
1354c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
1354d 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 42 74 ee(p);. *ppBt
1354e 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 ree = 0;. }. r
1354f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
13550 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 ** Decrement the
13551 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 BtShared.nRef c
13552 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20 69 74 ounter. When it
13553 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a 2a reaches zero,.*
13554 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42 74 53 * remove the BtS
13555 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 hared structure
13556 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69 6e 67 from the sharing
13557 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 0a 2a list. Return.*
13558 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 42 74 * true if the Bt
13559 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e Shared.nRef coun
1355a 74 65 72 20 72 65 61 63 68 65 73 20 7a 65 72 6f ter reaches zero
1355b 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 66 and return.** f
1355c 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 74 alse if it is st
1355d 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2f ill positive..*/
1355e 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f .static int remo
1355f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 veFromSharingLis
13560 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 t(BtShared *pBt)
13561 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
13562 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
13563 48 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 HE. sqlite3_mut
13564 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20 42 ex *pMaster;. B
13565 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74 3b 0a tShared *pList;.
13566 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d 20 int removed =
13567 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 0;.. assert( sq
13568 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
13569 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
1356a 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73 );. pMaster = s
1356b 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
1356c 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
1356d 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 ATIC_MASTER);.
1356e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1356f 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ter(pMaster);.
13570 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 pBt->nRef--;. i
13571 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20 f( pBt->nRef<=0
13572 29 7b 0a 20 20 20 20 69 66 28 20 47 4c 4f 42 41 ){. if( GLOBA
13573 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 L(BtShared*,sqli
13574 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
13575 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 st)==pBt ){.
13576 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 GLOBAL(BtShare
13577 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
13578 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 CacheList) = pBt
13579 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c ->pNext;. }el
1357a 73 65 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20 se{. pList
1357b 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 = GLOBAL(BtShare
1357c 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 d*,sqlite3Shared
1357d 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 CacheList);.
1357e 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 while( ALWAYS(
1357f 70 4c 69 73 74 29 20 26 26 20 70 4c 69 73 74 2d pList) && pList-
13580 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20 >pNext!=pBt ){.
13581 20 20 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69 pList=pLi
13582 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 st->pNext;.
13583 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 }. if( ALW
13584 41 59 53 28 70 4c 69 73 74 29 20 29 7b 0a 20 20 AYS(pList) ){.
13585 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65 pList->pNe
13586 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b xt = pBt->pNext;
13587 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
13588 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 if( SQLITE_TH
13589 52 45 41 44 53 41 46 45 20 29 7b 0a 20 20 20 20 READSAFE ){.
1358a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1358b 66 72 65 65 28 70 42 74 2d 3e 6d 75 74 65 78 29 free(pBt->mutex)
1358c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f ;. }. remo
1358d 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 ved = 1;. }. s
1358e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1358f 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 ve(pMaster);. r
13590 65 74 75 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23 eturn removed;.#
13591 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 31 3b else. return 1;
13592 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
13593 20 4d 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e Make sure pBt->
13594 70 54 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73 pTmpSpace points
13595 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f to an allocatio
13596 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c n of .** MX_CELL
13597 5f 53 49 5a 45 28 70 42 74 29 20 62 79 74 65 73 _SIZE(pBt) bytes
13598 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
13599 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 allocateTempSpa
1359a 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ce(BtShared *pBt
1359b 29 7b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 ){. if( !pBt->p
1359c 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 TmpSpace ){.
1359d 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d pBt->pTmpSpace =
1359e 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c sqlite3PageMall
1359f 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a oc( pBt->pageSiz
135a0 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a e );. }.}../*.*
135a1 2a 20 46 72 65 65 20 74 68 65 20 70 42 74 2d 3e * Free the pBt->
135a2 70 54 6d 70 53 70 61 63 65 20 61 6c 6c 6f 63 61 pTmpSpace alloca
135a3 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tion.*/.static v
135a4 6f 69 64 20 66 72 65 65 54 65 6d 70 53 70 61 63 oid freeTempSpac
135a5 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 e(BtShared *pBt)
135a6 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 {. sqlite3PageF
135a7 72 65 65 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 ree( pBt->pTmpSp
135a8 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e 70 54 6d ace);. pBt->pTm
135a9 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f pSpace = 0;.}../
135aa 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 *.** Close an op
135ab 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 en database and
135ac 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 63 invalidate all c
135ad 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ursors..*/.SQLIT
135ae 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
135af 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 lite3BtreeClose(
135b0 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 Btree *p){. BtS
135b1 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
135b2 70 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 pBt;. BtCursor
135b3 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f *pCur;.. /* Clo
135b4 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f se all cursors o
135b5 70 65 6e 65 64 20 76 69 61 20 74 68 69 73 20 68 pened via this h
135b6 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 andle. */. ass
135b7 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
135b8 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
135b9 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
135ba 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
135bb 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e . pBt->db = p->
135bc 64 62 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 74 db;. pCur = pBt
135bd 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 69 ->pCursor;. whi
135be 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20 le( pCur ){.
135bf 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d BtCursor *pTmp =
135c0 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 20 pCur;. pCur
135c1 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 = pCur->pNext;.
135c2 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 74 if( pTmp->pBt
135c3 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 ree==p ){.
135c4 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 sqlite3BtreeClos
135c5 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20 eCursor(pTmp);.
135c6 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 }. }.. /* R
135c7 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 69 ollback any acti
135c8 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 ve transaction a
135c9 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 6e 64 nd free the hand
135ca 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 le structure..
135cb 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 ** The call to s
135cc 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
135cd 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 20 ack() drops any
135ce 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 table-locks held
135cf 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 by. ** this ha
135d0 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c ndle.. */. sql
135d1 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 ite3BtreeRollbac
135d2 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 k(p);. sqlite3B
135d3 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 treeLeave(p);..
135d4 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 /* If there are
135d5 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 still other out
135d6 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e standing referen
135d7 63 65 73 20 74 6f 20 74 68 65 20 73 68 61 72 65 ces to the share
135d8 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 d-btree. ** str
135d9 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e ucture, return n
135da 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65 ow. The remainde
135db 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64 r of this proced
135dc 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a ure cleans . **
135dd 20 75 70 20 74 68 65 20 73 68 61 72 65 64 2d 62 up the shared-b
135de 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 tree.. */. ass
135df 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f ert( p->wantToLo
135e0 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b ck==0 && p->lock
135e1 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 ed==0 );. if( !
135e2 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 p->sharable || r
135e3 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 emoveFromSharing
135e4 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20 20 20 List(pBt) ){.
135e5 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e /* The pBt is n
135e6 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 o longer on the
135e7 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f sharing list, so
135e8 20 77 65 20 63 61 6e 20 61 63 63 65 73 73 0a 20 we can access.
135e9 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 ** it without
135ea 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 having to hold
135eb 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a the mutex.. *
135ec 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f *. ** Clean o
135ed 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 ut and delete th
135ee 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
135ef 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 t.. */. as
135f0 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72 sert( !pBt->pCur
135f1 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 sor );. sqlit
135f2 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 e3PagerClose(pBt
135f3 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 ->pPager);. i
135f4 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 f( pBt->xFreeSch
135f5 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68 ema && pBt->pSch
135f6 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74 ema ){. pBt
135f7 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28 70 42 ->xFreeSchema(pB
135f8 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 t->pSchema);.
135f9 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 }. sqlite3_f
135fa 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 ree(pBt->pSchema
135fb 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 );. freeTempS
135fc 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73 pace(pBt);. s
135fd 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 qlite3_free(pBt)
135fe 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 ;. }..#ifndef S
135ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
13600 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74 D_CACHE. assert
13601 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
13602 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
13603 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a p->locked==0 );.
13604 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 if( p->pPrev )
13605 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 p->pPrev->pNext
13606 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 = p->pNext;. i
13607 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d f( p->pNext ) p-
13608 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 >pNext->pPrev =
13609 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 p->pPrev;.#endif
1360a 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 .. sqlite3_free
1360b 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
1360c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1360d 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d * Change the lim
1360e 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 it on the number
1360f 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 of pages allowe
13610 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a d in the cache..
13611 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 **.** The maximu
13612 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68 m number of cach
13613 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74 e pages is set t
13614 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a o the absolute.*
13615 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 * value of mxPag
13616 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73 e. If mxPage is
13617 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70 negative, the p
13618 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65 ager will.** ope
13619 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75 rate asynchronou
1361a 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f sly - it will no
1361b 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79 t stop to do fsy
1361c 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75 nc()s.** to insu
1361d 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74 re data is writt
1361e 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73 en to the disk s
1361f 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a urface before.**
13620 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72 continuing. Tr
13621 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c ansactions still
13622 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f work if synchro
13623 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20 nous is off,.**
13624 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 and the database
13625 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75 cannot be corru
13626 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f pted if this pro
13627 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e gram.** crashes.
13628 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65 But if the ope
13629 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72 rating system cr
1362a 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69 ashes or there i
1362b 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70 s.** an abrupt p
1362c 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65 ower failure whe
1362d 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 n synchronous is
1362e 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61 off, the databa
1362f 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c se.** could be l
13630 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 eft in an incons
13631 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63 istent and unrec
13632 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a overable state..
13633 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69 ** Synchronous i
13634 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20 s on by default
13635 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 so database corr
13636 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a uption is not.**
13637 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72 normally a worr
13638 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
13639 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1363a 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a BtreeSetCacheSiz
1363b 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 e(Btree *p, int
1363c 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61 mxPage){. BtSha
1363d 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1363e 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c t;. assert( sql
1363f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
13640 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
13641 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
13642 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 nter(p);. sqlit
13643 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73 e3PagerSetCaches
13644 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ize(pBt->pPager,
13645 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 mxPage);. sqli
13646 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
13647 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
13648 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
13649 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 64 61 hange the way da
1364a 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20 ta is synced to
1364b 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f disk in order to
1364c 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65 63 increase or dec
1364d 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c rease.** how wel
1364e 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 l the database r
1364f 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 64 75 esists damage du
13650 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 20 e to OS crashes
13651 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 and power.** fai
13652 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 20 lures. Level 1
13653 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 is the same as a
13654 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20 synchronous (no
13655 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61 6e syncs() occur an
13656 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 d.** there is a
13657 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 high probability
13658 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65 76 of damage) Lev
13659 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 66 61 el 2 is the defa
1365a 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69 ult. There.** i
1365b 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75 74 s a very low but
1365c 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 non-zero probab
1365d 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 2e ility of damage.
1365e 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63 65 Level 3 reduce
1365f 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69 s the.** probabi
13660 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20 74 lity of damage t
13661 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 20 o near zero but
13662 77 69 74 68 20 61 20 77 72 69 74 65 20 70 65 72 with a write per
13663 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74 69 formance reducti
13664 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 on..*/.#ifndef S
13665 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 QLITE_OMIT_PAGER
13666 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f _PRAGMAS.SQLITE_
13667 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
13668 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74 te3BtreeSetSafet
13669 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a 70 2c yLevel(Btree *p,
1366a 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 int level, int
1366b 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53 fullSync){. BtS
1366c 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
1366d 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 pBt;. assert( s
1366e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1366f 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
13670 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
13671 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c eEnter(p);. sql
13672 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 ite3PagerSetSafe
13673 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 tyLevel(pBt->pPa
13674 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c ger, level, full
13675 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 Sync);. sqlite3
13676 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
13677 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
13678 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a K;.}.#endif../*.
13679 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
1367a 66 20 74 68 65 20 67 69 76 65 6e 20 62 74 72 65 f the given btre
1367b 65 20 69 73 20 73 65 74 20 74 6f 20 73 61 66 65 e is set to safe
1367c 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20 ty level 1. In
1367d 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 other.** words,
1367e 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e return TRUE if n
1367f 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20 o sync() occurs
13680 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 on the disk file
13681 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
13682 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
13683 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 BtreeSyncDisable
13684 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 d(Btree *p){. B
13685 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
13686 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b ->pBt;. int rc;
13687 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
13688 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
13689 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 >db->mutex) );
1368a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1368b 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 nter(p);. asser
1368c 74 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 t( pBt && pBt->p
1368d 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 Pager );. rc =
1368e 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 sqlite3PagerNosy
1368f 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b nc(pBt->pPager);
13690 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
13691 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
13692 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 n rc;.}..#if !de
13693 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
13694 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 T_PAGER_PRAGMAS)
13695 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
13696 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 ITE_OMIT_VACUUM)
13697 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
13698 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20 e default pages
13699 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d size and the num
1369a 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 ber of reserved
1369b 62 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a bytes per page..
1369c 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 73 **.** The page s
1369d 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f ize must be a po
1369e 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e wer of 2 between
1369f 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e 20 512 and 65536.
136a0 20 49 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 If the page.**
136a1 73 69 7a 65 20 73 75 70 70 6c 69 65 64 20 64 6f size supplied do
136a2 65 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 69 73 es not meet this
136a3 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 65 6e constraint then
136a4 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 the page size i
136a5 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 64 s not.** changed
136a6 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69 7a ..**.** Page siz
136a7 65 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69 6e es are constrain
136a8 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 ed to be a power
136a9 20 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74 20 of two so that
136aa 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 the region.** of
136ab 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
136ac 6c 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b le used for lock
136ad 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 61 ing (beginning a
136ae 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0a t PENDING_BYTE,.
136af 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74 ** the first byt
136b0 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62 e past the 1GB b
136b1 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30 30 oundary, 0x40000
136b2 30 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f 63 000) needs to oc
136b3 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 62 65 cur.** at the be
136b4 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61 67 ginning of a pag
136b5 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 e..**.** If para
136b6 6d 65 74 65 72 20 6e 52 65 73 65 72 76 65 20 69 meter nReserve i
136b7 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f s less than zero
136b8 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 , then the numbe
136b9 72 20 6f 66 20 72 65 73 65 72 76 65 64 0a 2a 2a r of reserved.**
136ba 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65 20 bytes per page
136bb 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 is left unchange
136bc 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
136bd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
136be 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 BtreeSetPageSize
136bf 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 (Btree *p, int p
136c0 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 ageSize, int nRe
136c1 73 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63 serve){. int rc
136c2 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
136c3 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
136c4 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 p->pBt;. sqlite
136c5 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
136c6 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 if( pBt->pageS
136c7 69 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20 izeFixed ){.
136c8 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
136c9 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e e(p);. return
136ca 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 SQLITE_READONLY
136cb 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73 ;. }. if( nRes
136cc 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52 erve<0 ){. nR
136cd 65 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 eserve = pBt->pa
136ce 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 geSize - pBt->us
136cf 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 ableSize;. }.
136d0 69 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 if( pageSize>=51
136d1 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 2 && pageSize<=S
136d2 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
136d3 49 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20 28 IZE &&. (
136d4 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 (pageSize-1)&pag
136d5 65 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 eSize)==0 ){.
136d6 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 assert( (pageSi
136d7 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 ze & 7)==0 );.
136d8 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e assert( !pBt->
136d9 70 50 61 67 65 31 20 26 26 20 21 70 42 74 2d 3e pPage1 && !pBt->
136da 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 pCursor );. p
136db 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 Bt->pageSize = p
136dc 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65 ageSize;. fre
136dd 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
136de 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
136df 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
136e0 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 e(pBt->pPager, &
136e1 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a pBt->pageSize);.
136e2 20 20 7d 0a 20 20 70 42 74 2d 3e 75 73 61 62 6c }. pBt->usabl
136e3 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 eSize = pBt->pag
136e4 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 eSize - nReserve
136e5 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
136e6 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
136e7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
136e8 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 Return the curre
136e9 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67 ntly defined pag
136ea 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 e size.*/.SQLITE
136eb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
136ec 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 ite3BtreeGetPage
136ed 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a Size(Btree *p){.
136ee 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d return p->pBt-
136ef 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 53 51 4c >pageSize;.}.SQL
136f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
136f1 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 sqlite3BtreeGetR
136f2 65 73 65 72 76 65 28 42 74 72 65 65 20 2a 70 29 eserve(Btree *p)
136f3 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c {. int n;. sql
136f4 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
136f5 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d );. n = p->pBt-
136f6 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70 >pageSize - p->p
136f7 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
136f8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
136f9 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
136fa 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 n;.}../*.** Set
136fb 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 the maximum pag
136fc 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61 e count for a da
136fd 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 67 65 tabase if mxPage
136fe 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a is positive..**
136ff 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 No changes are
13700 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 20 69 made if mxPage i
13701 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e s 0 or negative.
13702 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f .** Regardless o
13703 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d f the value of m
13704 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 xPage, return th
13705 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 e maximum page c
13706 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ount..*/.SQLITE_
13707 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
13708 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 43 te3BtreeMaxPageC
13709 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 ount(Btree *p, i
1370a 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e nt mxPage){. in
1370b 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 t n;. sqlite3Bt
1370c 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e reeEnter(p);. n
1370d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d = sqlite3PagerM
1370e 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70 axPageCount(p->p
1370f 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 Bt->pPager, mxPa
13710 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ge);. sqlite3Bt
13711 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
13712 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e 64 69 eturn n;.}.#endi
13713 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 f /* !defined(SQ
13714 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f LITE_OMIT_PAGER_
13715 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 PRAGMAS) || !def
13716 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
13717 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a _VACUUM) */../*.
13718 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61 ** Change the 'a
13719 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 uto-vacuum' prop
1371a 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 erty of the data
1371b 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75 base. If the 'au
1371c 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72 toVacuum'.** par
1371d 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 ameter is non-ze
1371e 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61 ro, then auto-va
1371f 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61 cuum mode is ena
13720 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69 bled. If zero, i
13721 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 t.** is disabled
13722 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 . The default va
13723 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f lue for the auto
13724 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79 -vacuum property
13725 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e is .** determin
13726 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 ed by the SQLITE
13727 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 _DEFAULT_AUTOVAC
13728 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51 UUM macro..*/.SQ
13729 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1372a 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
1372b 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 AutoVacuum(Btree
1372c 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 *p, int autoVac
1372d 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c uum){.#ifdef SQL
1372e 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
1372f 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c UUM. return SQL
13730 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65 ITE_READONLY;.#e
13731 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a lse. BtShared *
13732 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
13733 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
13734 4f 4b 3b 0a 20 20 69 6e 74 20 61 76 20 3d 20 28 OK;. int av = (
13735 61 75 74 6f 56 61 63 75 75 6d 3f 31 3a 30 29 3b autoVacuum?1:0);
13736 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 .. sqlite3Btree
13737 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 Enter(p);. if(
13738 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 pBt->pageSizeFix
13739 65 64 20 26 26 20 61 76 21 3d 70 42 74 2d 3e 61 ed && av!=pBt->a
1373a 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
1373b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 rc = SQLITE_REA
1373c 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a DONLY;. }else{.
1373d 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 pBt->autoVac
1373e 75 75 6d 20 3d 20 61 76 3b 0a 20 20 7d 0a 20 20 uum = av;. }.
1373f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
13740 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
13741 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a c;.#endif.}../*.
13742 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 ** Return the va
13743 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f lue of the 'auto
13744 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 -vacuum' propert
13745 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 y. If auto-vacuu
13746 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 m is .** enabled
13747 20 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 1 is returned.
13748 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a Otherwise 0..*/.
13749 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1374a 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 nt sqlite3BtreeG
1374b 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 etAutoVacuum(Btr
1374c 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 ee *p){.#ifdef S
1374d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
1374e 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 ACUUM. return B
1374f 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
13750 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e NONE;.#else. in
13751 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 t rc;. sqlite3B
13752 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
13753 72 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e rc = (. (!p->
13754 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
13755 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 ?BTREE_AUTOVACUU
13756 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d M_NONE:. (!p-
13757 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d >pBt->incrVacuum
13758 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 )?BTREE_AUTOVACU
13759 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 UM_FULL:. BTR
1375a 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e EE_AUTOVACUUM_IN
1375b 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 CR. );. sqlite
1375c 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1375d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e return rc;.#en
1375e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 dif.}.../*.** Ge
1375f 74 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f t a reference to
13760 20 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 pPage1 of the d
13761 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
13762 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f his will.** also
13763 20 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c acquire a readl
13764 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 ock on that file
13765 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f ..**.** SQLITE_O
13766 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e K is returned on
13767 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 success. If th
13768 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a e file is not a.
13769 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 ** well-formed d
1376a 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
1376b 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 en SQLITE_CORRUP
1376c 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a T is returned..*
1376d 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 * SQLITE_BUSY is
1376e 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 returned if the
1376f 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 database is loc
13770 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d ked. SQLITE_NOM
13771 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 EM.** is returne
13772 64 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 d if we run out
13773 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 of memory. .*/.s
13774 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 tatic int lockBt
13775 72 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 ree(BtShared *pB
13776 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 t){. int rc;.
13777 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b MemPage *pPage1;
13778 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 . int nPage;..
13779 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1377a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
1377b 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 >mutex) );. if(
1377c 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 20 72 pBt->pPage1 ) r
1377d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1377e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
1377f 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
13780 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3b 1, &pPage1, 0);
13781 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
13782 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
13783 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20 ;.. /* Do some
13784 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70 checking to help
13785 20 69 6e 73 75 72 65 20 74 68 65 20 66 69 6c 65 insure the file
13786 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c we opened reall
13787 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69 y is. ** a vali
13788 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e d database file.
13789 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 . */. rc = sq
1378a 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
1378b 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c unt(pBt->pPager,
1378c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 &nPage);. if(
1378d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1378e 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f . goto page1_
1378f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 7d init_failed;. }
13790 65 6c 73 65 20 69 66 28 20 6e 50 61 67 65 3e 30 else if( nPage>0
13791 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 61 67 65 ){. int page
13792 53 69 7a 65 3b 0a 20 20 20 20 69 6e 74 20 75 73 Size;. int us
13793 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 75 38 ableSize;. u8
13794 20 2a 70 61 67 65 31 20 3d 20 70 50 61 67 65 31 *page1 = pPage1
13795 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 72 63 20 ->aData;. rc
13796 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b = SQLITE_NOTADB;
13797 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 . if( memcmp(
13798 70 61 67 65 31 2c 20 7a 4d 61 67 69 63 48 65 61 page1, zMagicHea
13799 64 65 72 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20 der, 16)!=0 ){.
1379a 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f goto page1_
1379b 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 init_failed;.
1379c 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 }. if( page1
1379d 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 [18]>1 ){.
1379e 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 pBt->readOnly =
1379f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
137a0 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a page1[19]>1 ){.
137a1 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 goto page1
137a2 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 _init_failed;.
137a3 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 }.. /* The
137a4 6d 61 78 69 6d 75 6d 20 65 6d 62 65 64 64 65 64 maximum embedded
137a5 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 fraction must b
137a6 65 20 65 78 61 63 74 6c 79 20 32 35 25 2e 20 20 e exactly 25%.
137a7 41 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a And the minimum.
137a8 20 20 20 20 2a 2a 20 65 6d 62 65 64 64 65 64 20 ** embedded
137a9 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 fraction must be
137aa 20 31 32 2e 35 25 20 66 6f 72 20 62 6f 74 68 20 12.5% for both
137ab 6c 65 61 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f leaf-data and no
137ac 6e 2d 6c 65 61 66 2d 64 61 74 61 2e 0a 20 20 20 n-leaf-data..
137ad 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c ** The original
137ae 20 64 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20 design allowed
137af 74 68 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f these amounts to
137b0 20 76 61 72 79 2c 20 62 75 74 20 61 73 20 6f 66 vary, but as of
137b1 0a 20 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20 . ** version
137b2 33 2e 36 2e 30 2c 20 77 65 20 72 65 71 75 69 72 3.6.0, we requir
137b3 65 20 74 68 65 6d 20 74 6f 20 62 65 20 66 69 78 e them to be fix
137b4 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
137b5 66 28 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 31 f( memcmp(&page1
137b6 5b 32 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c [21], "\100\040\
137b7 30 34 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20 040",3)!=0 ){.
137b8 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 goto page1_i
137b9 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 nit_failed;.
137ba 7d 0a 20 20 20 20 70 61 67 65 53 69 7a 65 20 3d }. pageSize =
137bb 20 67 65 74 32 62 79 74 65 28 26 70 61 67 65 31 get2byte(&page1
137bc 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 28 [16]);. if( (
137bd 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 (pageSize-1)&pag
137be 65 53 69 7a 65 29 21 3d 30 20 7c 7c 20 70 61 67 eSize)!=0 || pag
137bf 65 53 69 7a 65 3c 35 31 32 20 7c 7c 0a 20 20 20 eSize<512 ||.
137c0 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4d 41 58 (SQLITE_MAX
137c1 5f 50 41 47 45 5f 53 49 5a 45 3c 33 32 37 36 38 _PAGE_SIZE<32768
137c2 20 26 26 20 70 61 67 65 53 69 7a 65 3e 53 51 4c && pageSize>SQL
137c3 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
137c4 45 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 E). ){.
137c5 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
137c6 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
137c7 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 assert( (pageS
137c8 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 ize & 7)==0 );.
137c9 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 usableSize =
137ca 70 61 67 65 53 69 7a 65 20 2d 20 70 61 67 65 31 pageSize - page1
137cb 5b 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 70 61 [20];. if( pa
137cc 67 65 53 69 7a 65 21 3d 70 42 74 2d 3e 70 61 67 geSize!=pBt->pag
137cd 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f eSize ){. /
137ce 2a 20 41 66 74 65 72 20 72 65 61 64 69 6e 67 20 * After reading
137cf 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f the first page o
137d0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 f the database a
137d1 73 73 75 6d 69 6e 67 20 61 20 70 61 67 65 20 73 ssuming a page s
137d2 69 7a 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 ize. ** of
137d3 42 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a BtShared.pageSiz
137d4 65 2c 20 77 65 20 68 61 76 65 20 64 69 73 63 6f e, we have disco
137d5 76 65 72 65 64 20 74 68 61 74 20 74 68 65 20 70 vered that the p
137d6 61 67 65 2d 73 69 7a 65 20 69 73 0a 20 20 20 20 age-size is.
137d7 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 70 61 ** actually pa
137d8 67 65 53 69 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74 geSize. Unlock t
137d9 68 65 20 64 61 74 61 62 61 73 65 2c 20 6c 65 61 he database, lea
137da 76 65 20 70 42 74 2d 3e 70 50 61 67 65 31 20 61 ve pBt->pPage1 a
137db 74 0a 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20 t. ** zero
137dc 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 and return SQLIT
137dd 45 5f 4f 4b 2e 20 54 68 65 20 63 61 6c 6c 65 72 E_OK. The caller
137de 20 77 69 6c 6c 20 63 61 6c 6c 20 74 68 69 73 20 will call this
137df 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a function. *
137e0 2a 20 61 67 61 69 6e 20 77 69 74 68 20 74 68 65 * again with the
137e1 20 63 6f 72 72 65 63 74 20 70 61 67 65 2d 73 69 correct page-si
137e2 7a 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ze.. */.
137e3 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
137e4 50 61 67 65 31 29 3b 0a 20 20 20 20 20 20 70 42 Page1);. pB
137e5 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 t->usableSize =
137e6 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 usableSize;.
137e7 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
137e8 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 = pageSize;.
137e9 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 freeTempSpace(
137ea 70 42 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 pBt);. sqli
137eb 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 te3PagerSetPages
137ec 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c ize(pBt->pPager,
137ed 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 &pBt->pageSize)
137ee 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
137ef 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
137f0 20 20 20 20 69 66 28 20 75 73 61 62 6c 65 53 69 if( usableSi
137f1 7a 65 3c 35 30 30 20 29 7b 0a 20 20 20 20 20 20 ze<500 ){.
137f2 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
137f3 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
137f4 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
137f5 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 = pageSize;.
137f6 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
137f7 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23 69 = usableSize;.#i
137f8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
137f9 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
137fa 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
137fb 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61 = (get4byte(&pa
137fc 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 ge1[36 + 4*4])?1
137fd 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e :0);. pBt->in
137fe 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 crVacuum = (get4
137ff 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b byte(&page1[36 +
13800 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 7*4])?1:0);.#en
13801 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d 61 dif. }.. /* ma
13802 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d 61 xLocal is the ma
13803 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 ximum amount of
13804 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72 65 payload to store
13805 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20 2a locally for. *
13806 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65 20 * a cell. Make
13807 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c 6c sure it is small
13808 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74 20 enough so that
13809 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e 6f at least minFano
1380a 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63 61 ut. ** cells ca
1380b 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f 6e n will fit on on
1380c 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73 75 e page. We assu
1380d 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61 67 me a 10-byte pag
1380e 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 42 e header.. ** B
1380f 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c 6f esides the paylo
13810 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75 73 ad, the cell mus
13811 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20 20 t store:. **
13812 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 72 2-byte pointer
13813 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20 2a to the cell. *
13814 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68 69 * 4-byte chi
13815 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 ld pointer. **
13816 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 20 9-byte nKey
13817 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 value. ** 4
13818 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 75 -byte nData valu
13819 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 e. ** 4-byt
1381a 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 e overflow page
1381b 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f 20 pointer. ** So
1381c 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20 a cell consists
1381d 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69 6e of a 2-byte poin
1381e 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68 69 er, a header whi
1381f 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61 73 ch is as much as
13820 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20 6c . ** 17 bytes l
13821 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74 65 ong, 0 to N byte
13822 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61 6e s of payload, an
13823 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34 20 d an optional 4
13824 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 byte overflow.
13825 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2e ** page pointer.
13826 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61 78 . */. pBt->max
13827 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75 73 Local = (pBt->us
13828 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34 2f ableSize-12)*64/
13829 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74 2d 255 - 23;. pBt-
1382a 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42 74 >minLocal = (pBt
1382b 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 ->usableSize-12)
1382c 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 *32/255 - 23;.
1382d 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 70 pBt->maxLeaf = p
1382e 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d Bt->usableSize -
1382f 20 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 35;. pBt->minL
13830 65 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 eaf = (pBt->usab
13831 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 leSize-12)*32/25
13832 35 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72 74 5 - 23;. assert
13833 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 2b ( pBt->maxLeaf +
13834 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f 53 23 <= MX_CELL_S
13835 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70 42 IZE(pBt) );. pB
13836 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 67 t->pPage1 = pPag
13837 65 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c e1;. return SQL
13838 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f 69 ITE_OK;..page1_i
13839 6e 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72 65 nit_failed:. re
1383a 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 leasePage(pPage1
1383b 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 );. pBt->pPage1
1383c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 = 0;. return r
1383d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
1383e 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6c routine works l
1383f 69 6b 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 ike lockBtree()
13840 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 61 except that it a
13841 6c 73 6f 20 69 6e 76 6f 6b 65 73 20 74 68 65 0a lso invokes the.
13842 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b ** busy callback
13843 20 69 66 20 74 68 65 72 65 20 69 73 20 6c 6f 63 if there is loc
13844 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 0a 2a 2f k contention..*/
13845 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b .static int lock
13846 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28 42 BtreeWithRetry(B
13847 74 72 65 65 20 2a 70 52 65 66 29 7b 0a 20 20 69 tree *pRef){. i
13848 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
13849 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 K;.. assert( sq
1384a 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
1384b 75 74 65 78 28 70 52 65 66 29 20 29 3b 0a 20 20 utex(pRef) );.
1384c 69 66 28 20 70 52 65 66 2d 3e 69 6e 54 72 61 6e if( pRef->inTran
1384d 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b s==TRANS_NONE ){
1384e 0a 20 20 20 20 75 38 20 69 6e 54 72 61 6e 73 61 . u8 inTransa
1384f 63 74 69 6f 6e 20 3d 20 70 52 65 66 2d 3e 70 42 ction = pRef->pB
13850 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
13851 3b 0a 20 20 20 20 62 74 72 65 65 49 6e 74 65 67 ;. btreeInteg
13852 72 69 74 79 28 70 52 65 66 29 3b 0a 20 20 20 20 rity(pRef);.
13853 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
13854 65 42 65 67 69 6e 54 72 61 6e 73 28 70 52 65 66 eBeginTrans(pRef
13855 2c 20 30 29 3b 0a 20 20 20 20 70 52 65 66 2d 3e , 0);. pRef->
13856 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
13857 6f 6e 20 3d 20 69 6e 54 72 61 6e 73 61 63 74 69 on = inTransacti
13858 6f 6e 3b 0a 20 20 20 20 70 52 65 66 2d 3e 69 6e on;. pRef->in
13859 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f Trans = TRANS_NO
1385a 4e 45 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d NE;. if( rc==
1385b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1385c 20 20 20 70 52 65 66 2d 3e 70 42 74 2d 3e 6e 54 pRef->pBt->nT
1385d 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 ransaction--;.
1385e 20 20 7d 0a 20 20 20 20 62 74 72 65 65 49 6e 74 }. btreeInt
1385f 65 67 72 69 74 79 28 70 52 65 66 29 3b 0a 20 20 egrity(pRef);.
13860 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
13861 0a 20 20 20 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 . ../*.**
13862 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 If there are no
13863 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 73 outstanding curs
13864 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20 6e ors and we are n
13865 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 ot in the middle
13866 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61 63 .** of a transac
13867 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20 69 tion but there i
13868 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e s a read lock on
13869 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 the database, t
1386a 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 hen.** this rout
1386b 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20 66 ine unrefs the f
1386c 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
1386d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
1386e 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68 65 hich .** has the
1386f 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65 61 effect of relea
13870 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c 6f sing the read lo
13871 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ck..**.** If the
13872 72 65 20 61 72 65 20 61 6e 79 20 6f 75 74 73 74 re are any outst
13873 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 2c 20 anding cursors,
13874 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
13875 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 a no-op..**.** I
13876 66 20 74 68 65 72 65 20 69 73 20 61 20 74 72 61 f there is a tra
13877 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 nsaction in prog
13878 72 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74 69 ress, this routi
13879 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
1387a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e /.static void un
1387b 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 lockBtreeIfUnuse
1387c 64 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 d(BtShared *pBt)
1387d 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1387e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1387f 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
13880 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 if( pBt->inTrans
13881 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f action==TRANS_NO
13882 4e 45 20 26 26 20 70 42 74 2d 3e 70 43 75 72 73 NE && pBt->pCurs
13883 6f 72 3d 3d 30 20 26 26 20 70 42 74 2d 3e 70 50 or==0 && pBt->pP
13884 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 20 69 age1!=0 ){. i
13885 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 f( sqlite3PagerR
13886 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 efcount(pBt->pPa
13887 67 65 72 29 3e 3d 31 20 29 7b 0a 20 20 20 20 20 ger)>=1 ){.
13888 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 assert( pBt->pP
13889 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 23 age1->aData );.#
1388a 69 66 20 30 0a 20 20 20 20 20 20 69 66 28 20 70 if 0. if( p
1388b 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
1388c 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 a==0 ){.
1388d 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d MemPage *pPage =
1388e 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 pBt->pPage1;.
1388f 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61 44 61 pPage->aDa
13890 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ta = sqlite3Page
13891 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e rGetData(pPage->
13892 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
13893 20 20 70 50 61 67 65 2d 3e 70 42 74 20 3d 20 70 pPage->pBt = p
13894 42 74 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 Bt;. pPag
13895 65 2d 3e 70 67 6e 6f 20 3d 20 31 3b 0a 20 20 20 e->pgno = 1;.
13896 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
13897 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 42 releasePage(pB
13898 74 2d 3e 70 50 61 67 65 31 29 3b 0a 20 20 20 20 t->pPage1);.
13899 7d 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 }. pBt->pPage
1389a 31 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 1 = 0;. pBt->
1389b 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 7d 0a inStmt = 0;. }.
1389c 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 }../*.** Create
1389d 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 62 a new database b
1389e 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 y initializing t
1389f 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 he first page of
138a0 20 74 68 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f the.** file..*/
138a1 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 77 44 .static int newD
138a2 61 74 61 62 61 73 65 28 42 74 53 68 61 72 65 64 atabase(BtShared
138a3 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 *pBt){. MemPag
138a4 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 69 67 6e e *pP1;. unsign
138a5 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 0a 20 ed char *data;.
138a6 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e int rc;. int n
138a7 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
138a8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
138a9 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
138aa 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
138ab 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
138ac 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 pBt->pPager, &nP
138ad 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d age);. if( rc!=
138ae 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 SQLITE_OK || nPa
138af 67 65 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 ge>0 ){. retu
138b0 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 31 rn rc;. }. pP1
138b1 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a = pBt->pPage1;.
138b2 20 20 61 73 73 65 72 74 28 20 70 50 31 21 3d 30 assert( pP1!=0
138b3 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 31 );. data = pP1
138b4 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 ->aData;. rc =
138b5 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
138b6 65 28 70 50 31 2d 3e 70 44 62 50 61 67 65 29 3b e(pP1->pDbPage);
138b7 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
138b8 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63 70 79 28 rn rc;. memcpy(
138b9 64 61 74 61 2c 20 7a 4d 61 67 69 63 48 65 61 64 data, zMagicHead
138ba 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 er, sizeof(zMagi
138bb 63 48 65 61 64 65 72 29 29 3b 0a 20 20 61 73 73 cHeader));. ass
138bc 65 72 74 28 20 73 69 7a 65 6f 66 28 7a 4d 61 67 ert( sizeof(zMag
138bd 69 63 48 65 61 64 65 72 29 3d 3d 31 36 20 29 3b icHeader)==16 );
138be 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
138bf 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70 61 67 65 a[16], pBt->page
138c0 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 31 38 Size);. data[18
138c1 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 31 39 ] = 1;. data[19
138c2 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 32 30 ] = 1;. data[20
138c3 5d 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a ] = pBt->pageSiz
138c4 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e - pBt->usableS
138c5 69 7a 65 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 ize;. data[21]
138c6 3d 20 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d = 64;. data[22]
138c7 20 3d 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 = 32;. data[23
138c8 5d 20 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 ] = 32;. memset
138c9 28 26 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 (&data[24], 0, 1
138ca 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 00-24);. zeroPa
138cb 67 65 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b ge(pP1, PTF_INTK
138cc 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f EY|PTF_LEAF|PTF_
138cd 4c 45 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 LEAFDATA );. pB
138ce 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 t->pageSizeFixed
138cf 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 = 1;.#ifndef SQ
138d0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
138d1 43 55 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70 CUUM. assert( p
138d2 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d Bt->autoVacuum==
138d3 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 1 || pBt->autoVa
138d4 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 cuum==0 );. ass
138d5 65 72 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 ert( pBt->incrVa
138d6 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e cuum==1 || pBt->
138d7 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b incrVacuum==0 );
138d8 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74 . put4byte(&dat
138d9 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 a[36 + 4*4], pBt
138da 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 ->autoVacuum);.
138db 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b put4byte(&data[
138dc 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e 36 + 7*4], pBt->
138dd 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e incrVacuum);.#en
138de 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c dif. return SQL
138df 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
138e0 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 Attempt to star
138e1 74 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 t a new transact
138e2 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 ion. A write-tra
138e3 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 nsaction.** is s
138e4 74 61 72 74 65 64 20 69 66 20 74 68 65 20 73 65 tarted if the se
138e5 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
138e6 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 nonzero, otherw
138e7 69 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 ise a read-.** t
138e8 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 ransaction. If
138e9 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
138ea 65 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 ent is 2 or more
138eb 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a and exclusive.*
138ec 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 * transaction is
138ed 20 73 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e started, meanin
138ee 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 g that no other
138ef 70 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 process is allow
138f0 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 ed.** to access
138f1 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 the database. A
138f2 20 70 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 preexisting tra
138f3 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 nsaction may not
138f4 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 be.** upgraded
138f5 74 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 to exclusive by
138f6 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 calling this rou
138f7 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 tine a second ti
138f8 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c me - the.** excl
138f9 75 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c usivity flag onl
138fa 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 y works for a ne
138fb 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a w transaction..*
138fc 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 *.** A write-tra
138fd 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 nsaction must be
138fe 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 started before
138ff 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a attempting any .
13900 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 ** changes to th
13901 65 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e e database. Non
13902 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
13903 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 ng routines .**
13904 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 will work unless
13905 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
13906 73 20 73 74 61 72 74 65 64 20 66 69 72 73 74 3a s started first:
13907 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 .**.** sqli
13908 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 te3BtreeCreateTa
13909 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 ble().** sq
1390a 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 lite3BtreeCreate
1390b 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 Index().**
1390c 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
1390d 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 rTable().**
1390e 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f sqlite3BtreeDro
1390f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 pTable().**
13910 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 sqlite3BtreeIns
13911 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 ert().** sq
13912 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 lite3BtreeDelete
13913 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
13914 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 e3BtreeUpdateMet
13915 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 a().**.** If an
13916 69 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 initial attempt
13917 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c to acquire the l
13918 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 ock fails becaus
13919 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e e of lock conten
1391a 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 tion.** and the
1391b 64 61 74 61 62 61 73 65 20 77 61 73 20 70 72 65 database was pre
1391c 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 viously unlocked
1391d 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 , then invoke th
1391e 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a e busy handler.*
1391f 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e * if there is on
13920 65 2e 20 20 42 75 74 20 69 66 20 74 68 65 72 65 e. But if there
13921 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 was previously
13922 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 a read-lock, do
13923 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 not.** invoke th
13924 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d e busy handler -
13925 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c just return SQL
13926 49 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 ITE_BUSY. SQLIT
13927 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 E_BUSY is .** re
13928 74 75 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 turned when ther
13929 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 e is already a r
1392a 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 ead-lock in orde
1392b 72 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 r to avoid a dea
1392c 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 dlock..**.** Sup
1392d 70 6f 73 65 20 74 68 65 72 65 20 61 72 65 20 74 pose there are t
1392e 77 6f 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 wo processes A a
1392f 6e 64 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 nd B. A has a r
13930 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 ead lock and B h
13931 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 as.** a reserved
13932 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 lock. B tries
13933 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 to promote to ex
13934 63 6c 75 73 69 76 65 20 62 75 74 20 69 73 20 62 clusive but is b
13935 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a locked because.*
13936 2a 20 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f * of A's read lo
13937 63 6b 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 ck. A tries to
13938 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 promote to reser
13939 76 65 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b ved but is block
1393a 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 ed by B..** One
1393b 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 or the other of
1393c 74 68 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 the two processe
1393d 73 20 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 s must give way
1393e 6f 72 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a or there can be.
1393f 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 ** no progress.
13940 20 42 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 By returning SQ
13941 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f LITE_BUSY and no
13942 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 t invoking the b
13943 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 usy callback.**
13944 77 68 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 when A already h
13945 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 as a read lock,
13946 77 65 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 we encourage A t
13947 6f 20 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 o give up and le
13948 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a t B.** proceed..
13949 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1394a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1394b 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 eeBeginTrans(Btr
1394c 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 ee *p, int wrfla
1394d 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a g){. BtShared *
1394e 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1394f 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
13950 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 OK;.. sqlite3Bt
13951 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 reeEnter(p);. p
13952 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
13953 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 btreeIntegrity
13954 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 (p);.. /* If th
13955 65 20 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 e btree is alrea
13956 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 dy in a write-tr
13957 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 ansaction, or it
13958 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 . ** is already
13959 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 in a read-trans
1395a 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 action and a rea
1395b 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 d-transaction.
1395c 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c ** is requested,
1395d 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 this is a no-op
1395e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
1395f 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
13960 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 RITE || (p->inTr
13961 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 ans==TRANS_READ
13962 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 && !wrflag) ){.
13963 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 goto trans_be
13964 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 gun;. }.. /* W
13965 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
13966 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 s are not possib
13967 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c le on a read-onl
13968 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 y database */.
13969 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c if( pBt->readOnl
1396a 79 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 y && wrflag ){.
1396b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 rc = SQLITE_R
1396c 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 EADONLY;. got
1396d 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 o trans_begun;.
1396e 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 }.. /* If anot
1396f 68 65 72 20 64 61 74 61 62 61 73 65 20 68 61 6e her database han
13970 64 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 dle has already
13971 6f 70 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 opened a write t
13972 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a ransaction . **
13973 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d on this shared-
13974 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 btree structure
13975 61 6e 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 and a second wri
13976 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 te transaction i
13977 73 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 s. ** requested
13978 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
13979 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 BUSY.. */. if(
1397a 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
1397b 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
1397c 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 && wrflag ){.
1397d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 rc = SQLITE_BU
1397e 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 SY;. goto tra
1397f 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 ns_begun;. }..#
13980 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
13981 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
13982 20 20 69 66 28 20 77 72 66 6c 61 67 3e 31 20 29 if( wrflag>1 )
13983 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 49 {. BtLock *pI
13984 74 65 72 3b 0a 20 20 20 20 66 6f 72 28 70 49 74 ter;. for(pIt
13985 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 er=pBt->pLock; p
13986 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 Iter; pIter=pIte
13987 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 r->pNext){.
13988 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 if( pIter->pBtr
13989 65 65 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 20 ee!=p ){.
1398a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
1398b 59 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 Y;. goto
1398c 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 20 trans_begun;.
1398d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 }. }. }.#
1398e 65 6e 64 69 66 0a 0a 20 20 64 6f 20 7b 0a 20 20 endif.. do {.
1398f 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 if( pBt->pPage
13990 31 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 1==0 ){. do
13991 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c {. rc = l
13992 6f 63 6b 42 74 72 65 65 28 70 42 74 29 3b 0a 20 ockBtree(pBt);.
13993 20 20 20 20 20 7d 77 68 69 6c 65 28 20 70 42 74 }while( pBt
13994 2d 3e 70 50 61 67 65 31 3d 3d 30 20 26 26 20 72 ->pPage1==0 && r
13995 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c==SQLITE_OK );.
13996 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 }.. if( r
13997 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
13998 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 wrflag ){.
13999 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c if( pBt->readOnl
1399a 79 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 y ){. rc
1399b 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c = SQLITE_READONL
1399c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a Y;. }else{.
1399d 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1399e 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 ite3PagerBegin(p
1399f 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 Bt->pPage1->pDbP
139a0 61 67 65 2c 20 77 72 66 6c 61 67 3e 31 29 3b 0a age, wrflag>1);.
139a1 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
139a2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
139a3 20 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77 44 rc = newD
139a4 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a 20 20 atabase(pBt);.
139a5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
139a6 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 }. . if(
139a7 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
139a8 7b 0a 20 20 20 20 20 20 69 66 28 20 77 72 66 6c {. if( wrfl
139a9 61 67 20 29 20 70 42 74 2d 3e 69 6e 53 74 6d 74 ag ) pBt->inStmt
139aa 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 0;. }else{
139ab 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 . unlockBtr
139ac 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b eeIfUnused(pBt);
139ad 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 . }. }while(
139ae 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc==SQLITE_BUSY
139af 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 && pBt->inTrans
139b0 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f action==TRANS_NO
139b1 4e 45 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 NE &&.
139b2 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 btreeInvokeBusyH
139b3 61 6e 64 6c 65 72 28 70 42 74 29 20 29 3b 0a 0a andler(pBt) );..
139b4 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
139b5 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 _OK ){. if( p
139b6 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
139b7 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 70 _NONE ){. p
139b8 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e Bt->nTransaction
139b9 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d ++;. }. p-
139ba 3e 69 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c >inTrans = (wrfl
139bb 61 67 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54 ag?TRANS_WRITE:T
139bc 52 41 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20 RANS_READ);.
139bd 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 if( p->inTrans>p
139be 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
139bf 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e n ){. pBt->
139c0 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 inTransaction =
139c1 70 2d 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20 p->inTrans;.
139c2 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
139c3 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
139c4 48 45 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61 HE. if( wrfla
139c5 67 3e 31 20 29 7b 0a 20 20 20 20 20 20 61 73 73 g>1 ){. ass
139c6 65 72 74 28 20 21 70 42 74 2d 3e 70 45 78 63 6c ert( !pBt->pExcl
139c7 75 73 69 76 65 20 29 3b 0a 20 20 20 20 20 20 70 usive );. p
139c8 42 74 2d 3e 70 45 78 63 6c 75 73 69 76 65 20 3d Bt->pExclusive =
139c9 20 70 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 p;. }.#endif
139ca 0a 20 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 . }...trans_beg
139cb 75 6e 3a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 un:. btreeInteg
139cc 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 rity(p);. sqlit
139cd 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
139ce 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
139cf 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
139d0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
139d1 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 ./*.** Set the p
139d2 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
139d3 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 es for all child
139d4 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61 67 ren of page pPag
139d5 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 e. Also, if.** p
139d6 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 Page contains ce
139d7 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 lls that point t
139d8 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
139d9 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74 65 , set the pointe
139da 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 r.** map entries
139db 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f for the overflo
139dc 77 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c 2e w pages as well.
139dd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
139de 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 4d etChildPtrmaps(M
139df 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a emPage *pPage){.
139e0 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
139e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139e2 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 /* Counter
139e3 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e variable */. in
139e4 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 t nCell;
139e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139e6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 /* Number of ce
139e7 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61 67 lls in page pPag
139e8 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
139e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
139eb 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 urn code */. Bt
139ec 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 Shared *pBt = pP
139ed 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 age->pBt;. int
139ee 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 isInitOrig = pPa
139ef 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 ge->isInit;. Pg
139f0 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d no pgno = pPage-
139f1 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 >pgno;.. assert
139f2 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
139f3 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
139f4 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 >mutex) );. rc
139f5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e = sqlite3BtreeIn
139f6 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 itPage(pPage);.
139f7 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
139f8 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 OK ){. goto s
139f9 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 et_child_ptrmaps
139fa 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c _out;. }. nCel
139fb 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c l = pPage->nCell
139fc 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c ;.. for(i=0; i<
139fd 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 nCell; i++){.
139fe 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e u8 *pCell = fin
139ff 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b dCell(pPage, i);
13a00 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 .. rc = ptrma
13a01 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 pPutOvflPtr(pPag
13a02 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 e, pCell);. i
13a03 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13a04 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 ){. goto s
13a05 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 et_child_ptrmaps
13a06 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 _out;. }..
13a07 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
13a08 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 f ){. Pgno
13a09 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34 childPgno = get4
13a0a 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20 20 20 byte(pCell);.
13a0b 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 rc = ptrmapPu
13a0c 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f t(pBt, childPgno
13a0d 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 , PTRMAP_BTREE,
13a0e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 pgno);. if(
13a0f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13a10 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f goto set_child_
13a11 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 20 ptrmaps_out;.
13a12 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 }. }.. if( !p
13a13 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
13a14 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f Pgno childPgno
13a15 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
13a16 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d ge->aData[pPage-
13a17 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a >hdrOffset+8]);.
13a18 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 rc = ptrmapP
13a19 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e ut(pBt, childPgn
13a1a 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c o, PTRMAP_BTREE,
13a1b 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a 73 65 74 pgno);. }..set
13a1c 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f _child_ptrmaps_o
13a1d 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 ut:. pPage->isI
13a1e 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 nit = isInitOrig
13a1f 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
13a20 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 ../*.** Somewher
13a21 65 20 6f 6e 20 70 50 61 67 65 2c 20 77 68 69 63 e on pPage, whic
13a22 68 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 h is guarenteed
13a23 74 6f 20 62 65 20 61 20 62 74 72 65 65 20 70 61 to be a btree pa
13a24 67 65 2c 20 6e 6f 74 20 61 6e 20 6f 76 65 72 66 ge, not an overf
13a25 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 69 73 20 low.** page, is
13a26 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 a pointer to pag
13a27 65 20 69 46 72 6f 6d 2e 20 4d 6f 64 69 66 79 20 e iFrom. Modify
13a28 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f 20 this pointer so
13a29 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
13a2a 6f 0a 2a 2a 20 69 54 6f 2e 20 50 61 72 61 6d 65 o.** iTo. Parame
13a2b 74 65 72 20 65 54 79 70 65 20 64 65 73 63 72 69 ter eType descri
13a2c 62 65 73 20 74 68 65 20 74 79 70 65 20 6f 66 20 bes the type of
13a2d 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 6d 6f pointer to be mo
13a2e 64 69 66 69 65 64 2c 20 61 73 20 0a 2a 2a 20 66 dified, as .** f
13a2f 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 ollows:.**.** PT
13a30 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20 20 RMAP_BTREE:
13a31 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 pPage is a btree
13a32 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 -page. The point
13a33 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 63 er points at a c
13a34 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 hild .**
13a35 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 page
13a36 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 of pPage..**.**
13a37 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 PTRMAP_OVERFLOW1
13a38 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 : pPage is a btr
13a39 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 ee-page. The poi
13a3a 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 nter points at a
13a3b 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 n overflow.**
13a3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13a3d 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 page pointed to
13a3e 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 by one of the ce
13a3f 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a lls on pPage..**
13a40 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 .** PTRMAP_OVERF
13a41 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20 61 LOW2: pPage is a
13a42 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e n overflow-page.
13a43 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 The pointer poi
13a44 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 74 0a nts at the next.
13a45 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
13a46 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 overflow pa
13a47 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a ge in the list..
13a48 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f */.static int mo
13a49 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72 28 difyPagePointer(
13a4a 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
13a4b 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f Pgno iFrom, Pgno
13a4c 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29 7b iTo, u8 eType){
13a4d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
13a4e 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
13a4f 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
13a50 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 65 3d );. if( eType=
13a51 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 =PTRMAP_OVERFLOW
13a52 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 2 ){. /* The
13a53 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79 pointer is alway
13a54 73 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 s the first 4 by
13a55 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 tes of the page
13a56 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a in this case. *
13a57 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 62 79 /. if( get4by
13a58 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 te(pPage->aData)
13a59 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 !=iFrom ){.
13a5a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
13a5b 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
13a5c 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 }. put4byte(
13a5d 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 pPage->aData, iT
13a5e 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 o);. }else{.
13a5f 20 69 6e 74 20 69 73 49 6e 69 74 4f 72 69 67 20 int isInitOrig
13a60 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b = pPage->isInit;
13a61 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
13a62 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 int nCell;..
13a63 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 sqlite3BtreeInit
13a64 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
13a65 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e nCell = pPage->
13a66 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 nCell;.. for(
13a67 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b i=0; i<nCell; i+
13a68 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 +){. u8 *pC
13a69 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
13a6a 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 Page, i);.
13a6b 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 if( eType==PTRMA
13a6c 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 P_OVERFLOW1 ){.
13a6d 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 CellInfo
13a6e 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 73 71 info;. sq
13a6f 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 lite3BtreeParseC
13a70 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 ellPtr(pPage, pC
13a71 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 ell, &info);.
13a72 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f if( info.iO
13a73 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 verflow ){.
13a74 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d if( iFrom==
13a75 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b get4byte(&pCell[
13a76 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 info.iOverflow])
13a77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
13a78 70 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b put4byte(&pCell[
13a79 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 2c info.iOverflow],
13a7a 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 iTo);.
13a7b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
13a7c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
13a7d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
13a7e 20 20 20 20 20 69 66 28 20 67 65 74 34 62 79 74 if( get4byt
13a7f 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20 e(pCell)==iFrom
13a80 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 ){. put
13a81 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54 6f 4byte(pCell, iTo
13a82 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 );. bre
13a83 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
13a84 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
13a85 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c 20 if( i==nCell
13a86 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 ){. if( eTy
13a87 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 pe!=PTRMAP_BTREE
13a88 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 67 || . g
13a89 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
13a8a 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
13a8b 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72 6f Offset+8])!=iFro
13a8c 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 m ){. ret
13a8d 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
13a8e 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d PT_BKPT;. }
13a8f 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 . put4byte(
13a90 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
13a91 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
13a92 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a 0a ], iTo);. }..
13a93 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 pPage->isIni
13a94 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a t = isInitOrig;.
13a95 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
13a96 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a ITE_OK;.}.../*.*
13a97 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e 20 * Move the open
13a98 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44 database page pD
13a99 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69 6f bPage to locatio
13a9a 6e 20 69 46 72 65 65 50 61 67 65 20 69 6e 20 74 n iFreePage in t
13a9b 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e he .** database.
13a9c 20 54 68 65 20 70 44 62 50 61 67 65 20 72 65 66 The pDbPage ref
13a9d 65 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20 76 erence remains v
13a9e 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 alid..*/.static
13a9f 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 65 int relocatePage
13aa0 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 (. BtShared *pB
13aa1 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t, /*
13aa2 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 Btree */. MemPa
13aa3 67 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 20 ge *pDbPage,
13aa4 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 /* Open page
13aa5 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 to move */. u8
13aa6 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 eType,
13aa7 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
13aa8 72 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e 74 r map 'type' ent
13aa9 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a ry for pDbPage *
13aaa 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 /. Pgno iPtrPag
13aab 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e, /*
13aac 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 67 Pointer map 'pag
13aad 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 e-no' entry for
13aae 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e pDbPage */. Pgn
13aaf 6f 20 69 46 72 65 65 50 61 67 65 2c 20 20 20 20 o iFreePage,
13ab0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 /* The loc
13ab1 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 ation to move pD
13ab2 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e bPage to */. in
13ab3 74 20 69 73 43 6f 6d 6d 69 74 0a 29 7b 0a 20 20 t isCommit.){.
13ab4 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67 MemPage *pPtrPag
13ab5 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 e; /* The page
13ab6 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 that contains a
13ab7 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50 pointer to pDbP
13ab8 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 age */. Pgno iD
13ab9 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d bPage = pDbPage-
13aba 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a >pgno;. Pager *
13abb 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 pPager = pBt->pP
13abc 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a ager;. int rc;.
13abd 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 . assert( eType
13abe 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f ==PTRMAP_OVERFLO
13abf 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 W2 || eType==PTR
13ac0 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c MAP_OVERFLOW1 ||
13ac1 20 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50 . eType==P
13ac2 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 TRMAP_BTREE || e
13ac3 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f Type==PTRMAP_ROO
13ac4 54 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72 TPAGE );. asser
13ac5 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
13ac6 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
13ac7 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
13ac8 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74 DbPage->pBt==pBt
13ac9 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 );.. /* Move p
13aca 61 67 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d age iDbPage from
13acb 20 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63 its current loc
13acc 61 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75 ation to page nu
13acd 6d 62 65 72 20 69 46 72 65 65 50 61 67 65 20 2a mber iFreePage *
13ace 2f 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 4f /. TRACE(("AUTO
13acf 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 VACUUM: Moving %
13ad0 64 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 25 d to free page %
13ad1 64 20 28 70 74 72 20 70 61 67 65 20 25 64 20 74 d (ptr page %d t
13ad2 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20 ype %d)\n", .
13ad3 20 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 iDbPage, iFre
13ad4 65 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c ePage, iPtrPage,
13ad5 20 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d eType));. rc =
13ad6 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 sqlite3PagerMov
13ad7 65 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 44 epage(pPager, pD
13ad8 62 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20 bPage->pDbPage,
13ad9 69 46 72 65 65 50 61 67 65 2c 20 69 73 43 6f 6d iFreePage, isCom
13ada 6d 69 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d mit);. if( rc!=
13adb 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13adc 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
13add 20 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 pDbPage->pgno
13ade 3d 20 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20 = iFreePage;..
13adf 2f 2a 20 49 66 20 70 44 62 50 61 67 65 20 77 61 /* If pDbPage wa
13ae0 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20 s a btree-page,
13ae1 74 68 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65 then it may have
13ae2 20 63 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64 child pages and
13ae3 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 /or cells. ** t
13ae4 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 hat point to ove
13ae5 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 rflow pages. The
13ae6 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 pointer map ent
13ae7 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 ries for all the
13ae8 73 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 se. ** pages ne
13ae9 65 64 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 ed to be changed
13aea 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 .. **. ** If p
13aeb 44 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 DbPage is an ove
13aec 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e rflow page, then
13aed 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74 the first 4 byt
13aee 65 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20 es may store a.
13aef 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 ** pointer to a
13af0 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 subsequent over
13af1 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68 flow page. If th
13af2 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 is is the case,
13af3 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f then. ** the po
13af4 69 6e 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20 inter map needs
13af5 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 66 6f to be updated fo
13af6 72 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74 r the subsequent
13af7 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a overflow page..
13af8 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 */. if( eType
13af9 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c ==PTRMAP_BTREE |
13afa 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f | eType==PTRMAP_
13afb 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 ROOTPAGE ){.
13afc 72 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 rc = setChildPtr
13afd 6d 61 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20 maps(pDbPage);.
13afe 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
13aff 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
13b00 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
13b01 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f }else{. Pgno
13b02 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34 nextOvfl = get4
13b03 62 79 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44 byte(pDbPage->aD
13b04 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 ata);. if( ne
13b05 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20 xtOvfl!=0 ){.
13b06 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 rc = ptrmapPu
13b07 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c 2c t(pBt, nextOvfl,
13b08 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
13b09 32 2c 20 69 46 72 65 65 50 61 67 65 29 3b 0a 20 2, iFreePage);.
13b0a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13b0b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13b0c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
13b0d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
13b0e 20 20 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74 /* Fix the dat
13b0f 61 62 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e abase pointer on
13b10 20 70 61 67 65 20 69 50 74 72 50 61 67 65 20 74 page iPtrPage t
13b11 68 61 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69 hat pointed at i
13b12 44 62 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74 DbPage so. ** t
13b13 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74 hat it points at
13b14 20 69 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f iFreePage. Also
13b15 20 66 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 fix the pointer
13b16 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 map entry for.
13b17 20 2a 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20 ** iPtrPage..
13b18 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d */. if( eType!=
13b19 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 PTRMAP_ROOTPAGE
13b1a 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
13b1b 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
13b1c 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 pBt, iPtrPage, &
13b1d 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 pPtrPage, 0);.
13b1e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13b1f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
13b20 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
13b21 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
13b22 67 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67 gerWrite(pPtrPag
13b23 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
13b24 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13b25 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 OK ){. rele
13b26 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65 asePage(pPtrPage
13b27 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
13b28 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 rc;. }. rc
13b29 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 = modifyPagePoi
13b2a 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 nter(pPtrPage, i
13b2b 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 DbPage, iFreePag
13b2c 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 e, eType);. r
13b2d 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 eleasePage(pPtrP
13b2e 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
13b2f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
13b30 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 rc = ptrmap
13b31 50 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 Put(pBt, iFreePa
13b32 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 ge, eType, iPtrP
13b33 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a age);. }. }.
13b34 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
13b35 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 /* Forward decla
13b36 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 ration required
13b37 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 by incrVacuumSte
13b38 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 p(). */.static i
13b39 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 nt allocateBtree
13b3a 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c Page(BtShared *,
13b3b 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e MemPage **, Pgn
13b3c 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a o *, Pgno, u8);.
13b3d 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 ./*.** Perform a
13b3e 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 single step of
13b3f 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 an incremental-v
13b40 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 acuum. If succes
13b41 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 sful,.** return
13b42 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 SQLITE_OK. If th
13b43 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 ere is no work t
13b44 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 o do (and theref
13b45 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20 ore no.** point
13b46 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 in calling this
13b47 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c function again),
13b48 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
13b49 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 ONE..**.** More
13b4a 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73 specificly, this
13b4b 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 function attemp
13b4c 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a ts to re-organiz
13b4d 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 e the .** databa
13b4e 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c se so that the l
13b4f 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 ast page of the
13b50 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 file currently i
13b51 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c n use.** is no l
13b52 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a onger in use..**
13b53 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20 .** If the nFin
13b54 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e parameter is non
13b55 2d 7a 65 72 6f 2c 20 74 68 65 20 69 6d 70 6c 65 -zero, the imple
13b56 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 mentation assume
13b57 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61 s.** that the ca
13b58 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 ller will keep c
13b59 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 alling incrVacuu
13b5a 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a mStep() until.**
13b5b 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 it returns SQLI
13b5c 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 TE_DONE or an er
13b5d 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 ror, and that nF
13b5e 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d in is the.** num
13b5f 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65 ber of pages the
13b60 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
13b61 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 ill contain afte
13b62 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 r this .** proce
13b63 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a ss is complete..
13b64 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e */.static int in
13b65 63 72 56 61 63 75 75 6d 53 74 65 70 28 42 74 53 crVacuumStep(BtS
13b66 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f hared *pBt, Pgno
13b67 20 6e 46 69 6e 29 7b 0a 20 20 50 67 6e 6f 20 69 nFin){. Pgno i
13b68 4c 61 73 74 50 67 3b 20 20 20 20 20 20 20 20 20 LastPg;
13b69 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 61 67 65 /* Last page
13b6a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
13b6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 46 72 65 65 */. Pgno nFree
13b6c 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 List;
13b6d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
13b6e 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20 es still on the
13b6f 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 free-list */..
13b70 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
13b71 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
13b72 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 4c 61 73 mutex) );. iLas
13b73 74 50 67 20 3d 20 70 42 74 2d 3e 6e 54 72 75 6e tPg = pBt->nTrun
13b74 63 3b 0a 20 20 69 66 28 20 69 4c 61 73 74 50 67 c;. if( iLastPg
13b75 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73 74 ==0 ){. iLast
13b76 50 67 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f Pg = pagerPageco
13b77 75 6e 74 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 20 unt(pBt);. }..
13b78 20 69 66 28 20 21 50 54 52 4d 41 50 5f 49 53 50 if( !PTRMAP_ISP
13b79 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 AGE(pBt, iLastPg
13b7a 29 20 26 26 20 69 4c 61 73 74 50 67 21 3d 50 45 ) && iLastPg!=PE
13b7b 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
13b7c 70 42 74 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 pBt) ){. int
13b7d 72 63 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 rc;. u8 eType
13b7e 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 ;. Pgno iPtrP
13b7f 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c age;.. nFreeL
13b80 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 ist = get4byte(&
13b81 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
13b82 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 ta[36]);. if(
13b83 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20 7c 7c nFreeList==0 ||
13b84 20 6e 46 69 6e 3d 3d 69 4c 61 73 74 50 67 20 29 nFin==iLastPg )
13b85 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
13b86 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 QLITE_DONE;.
13b87 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d }.. rc = ptrm
13b88 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61 73 74 apGet(pBt, iLast
13b89 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 Pg, &eType, &iPt
13b8a 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 rPage);. if(
13b8b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
13b8c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
13b8d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
13b8e 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f eType==PTRMAP_RO
13b8f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 OTPAGE ){.
13b90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
13b91 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
13b92 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70 65 }.. if( eType
13b93 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 ==PTRMAP_FREEPAG
13b94 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e E ){. if( n
13b95 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Fin==0 ){.
13b96 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 /* Remove the
13b97 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66 69 page from the fi
13b98 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 les free-list. T
13b99 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 his is not requi
13b9a 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 red. ** i
13b9b 66 20 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 f nFin is non-ze
13b9c 72 6f 2e 20 49 6e 20 74 68 61 74 20 63 61 73 65 ro. In that case
13b9d 2c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 , the free-list
13b9e 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20 will be.
13b9f 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 ** truncated to
13ba0 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 73 20 zero after this
13ba1 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
13ba2 2c 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 , so it doesn't
13ba3 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 . ** matt
13ba4 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c 20 63 er if it still c
13ba5 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 ontains some gar
13ba6 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 bage entries..
13ba7 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
13ba8 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 Pgno iFreePg;.
13ba9 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a MemPage *
13baa 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20 pFreePg;.
13bab 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 rc = allocateBt
13bac 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 46 reePage(pBt, &pF
13bad 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 67 2c reePg, &iFreePg,
13bae 20 69 4c 61 73 74 50 67 2c 20 31 29 3b 0a 20 20 iLastPg, 1);.
13baf 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
13bb0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13bb1 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
13bb2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13bb3 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65 50 assert( iFreeP
13bb4 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 g==iLastPg );.
13bb5 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
13bb6 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 e(pFreePg);.
13bb7 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b }. } else {
13bb8 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 . Pgno iFre
13bb9 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 ePg;
13bba 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 72 65 /* Index of fre
13bbb 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 70 e page to move p
13bbc 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20 LastPg to */.
13bbd 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 MemPage *pLas
13bbe 74 50 67 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d tPg;.. rc =
13bbf 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
13bc0 50 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 50 Page(pBt, iLastP
13bc1 67 2c 20 26 70 4c 61 73 74 50 67 2c 20 30 29 3b g, &pLastPg, 0);
13bc2 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
13bc3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
13bc4 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
13bc5 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
13bc6 20 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f If nFin is zero
13bc7 2c 20 74 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 , this loop runs
13bc8 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e exactly once an
13bc9 64 20 70 61 67 65 20 70 4c 61 73 74 50 67 0a 20 d page pLastPg.
13bca 20 20 20 20 20 2a 2a 20 69 73 20 73 77 61 70 70 ** is swapp
13bcb 65 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73 ed with the firs
13bcc 74 20 66 72 65 65 20 70 61 67 65 20 70 75 6c 6c t free page pull
13bcd 65 64 20 6f 66 66 20 74 68 65 20 66 72 65 65 20 ed off the free
13bce 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 list.. **.
13bcf 20 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f ** On the o
13bd0 74 68 65 72 20 68 61 6e 64 2c 20 69 66 20 6e 46 ther hand, if nF
13bd1 69 6e 20 69 73 20 67 72 65 61 74 65 72 20 74 68 in is greater th
13bd2 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65 an zero, then ke
13bd3 65 70 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 6f 70 ep. ** loop
13bd4 69 6e 67 20 75 6e 74 69 6c 20 61 20 66 72 65 65 ing until a free
13bd5 2d 70 61 67 65 20 6c 6f 63 61 74 65 64 20 77 69 -page located wi
13bd6 74 68 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e thin the first n
13bd7 46 69 6e 20 70 61 67 65 73 0a 20 20 20 20 20 20 Fin pages.
13bd8 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ** of the file i
13bd9 73 20 66 6f 75 6e 64 2e 0a 20 20 20 20 20 20 2a s found.. *
13bda 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 /. do {.
13bdb 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 MemPage *pF
13bdc 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 reePg;. r
13bdd 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 c = allocateBtre
13bde 65 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 ePage(pBt, &pFre
13bdf 65 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 30 ePg, &iFreePg, 0
13be0 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 0);. if
13be1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13be2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c ){. rel
13be3 65 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 easePage(pLastPg
13be4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
13be5 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
13be6 7d 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 }. releas
13be7 65 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a ePage(pFreePg);.
13be8 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6e 46 }while( nF
13be9 69 6e 21 3d 30 20 26 26 20 69 46 72 65 65 50 67 in!=0 && iFreePg
13bea 3e 6e 46 69 6e 20 29 3b 0a 20 20 20 20 20 20 61 >nFin );. a
13beb 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3c 69 ssert( iFreePg<i
13bec 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 LastPg );.
13bed 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
13bee 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4c te3PagerWrite(pL
13bef 61 73 74 50 67 2d 3e 70 44 62 50 61 67 65 29 3b astPg->pDbPage);
13bf0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
13bf1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
13bf2 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 rc = relocat
13bf3 65 50 61 67 65 28 70 42 74 2c 20 70 4c 61 73 74 ePage(pBt, pLast
13bf4 50 67 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 Pg, eType, iPtrP
13bf5 61 67 65 2c 20 69 46 72 65 65 50 67 2c 20 6e 46 age, iFreePg, nF
13bf6 69 6e 21 3d 30 29 3b 0a 20 20 20 20 20 20 7d 0a in!=0);. }.
13bf7 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
13bf8 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 e(pLastPg);.
13bf9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13bfa 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
13bfb 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
13bfc 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 }. }. }.. p
13bfd 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 69 4c 61 Bt->nTrunc = iLa
13bfe 73 74 50 67 20 2d 20 31 3b 0a 20 20 77 68 69 6c stPg - 1;. whil
13bff 65 28 20 70 42 74 2d 3e 6e 54 72 75 6e 63 3d 3d e( pBt->nTrunc==
13c00 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
13c01 45 28 70 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49 E(pBt)||PTRMAP_I
13c02 53 50 41 47 45 28 70 42 74 2c 20 70 42 74 2d 3e SPAGE(pBt, pBt->
13c03 6e 54 72 75 6e 63 29 20 29 7b 0a 20 20 20 20 70 nTrunc) ){. p
13c04 42 74 2d 3e 6e 54 72 75 6e 63 2d 2d 3b 0a 20 20 Bt->nTrunc--;.
13c05 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
13c06 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 E_OK;.}../*.** A
13c07 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
13c08 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 on must be opene
13c09 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 d before calling
13c0a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
13c0b 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61 ** It performs a
13c0c 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 single unit of
13c0d 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 work towards an
13c0e 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
13c0f 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 um..**.** If the
13c10 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 incremental vac
13c11 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 uum is finished
13c12 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
13c13 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 ion has run,.**
13c14 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 SQLITE_DONE is r
13c15 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 eturned. If it i
13c16 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 s not finished,
13c17 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 but no error occ
13c18 75 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f ured,.** SQLITE_
13c19 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 OK is returned.
13c1a 4f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c Otherwise an SQL
13c1b 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 ite error code.
13c1c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13c1d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
13c1e 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 reeIncrVacuum(Bt
13c1f 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 ree *p){. int r
13c20 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 c;. BtShared *p
13c21 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 Bt = p->pBt;..
13c22 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
13c23 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 r(p);. pBt->db
13c24 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 = p->db;. asser
13c25 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 t( pBt->inTransa
13c26 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
13c27 54 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 TE && p->inTrans
13c28 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
13c29 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74 . if( !pBt->aut
13c2a 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 oVacuum ){. r
13c2b 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
13c2c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
13c2d 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 validateAllOverf
13c2e 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 lowCache(pBt);.
13c2f 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75 rc = incrVacu
13c30 75 6d 53 74 65 70 28 70 42 74 2c 20 30 29 3b 0a umStep(pBt, 0);.
13c31 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
13c32 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
13c33 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
13c34 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
13c35 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 s called prior t
13c36 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f o sqlite3PagerCo
13c37 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e mmit when a tran
13c38 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f saction.** is co
13c39 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e 20 61 75 mmited for an au
13c3a 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 to-vacuum databa
13c3b 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c se..**.** If SQL
13c3c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
13c3d 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e ed, then *pnTrun
13c3e 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 c is set to the
13c3f 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 0a number of pages.
13c40 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
13c41 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 74 file should be t
13c42 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72 69 runcated to duri
13c43 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 72 ng the commit pr
13c44 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 ocess. .** i.e.
13c45 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 the database has
13c46 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65 been reorganize
13c47 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74 d so that only t
13c48 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75 6e he first *pnTrun
13c49 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20 69 c.** pages are i
13c4a 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 n use..*/.static
13c4b 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d 43 int autoVacuumC
13c4c 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20 2a ommit(BtShared *
13c4d 70 42 74 2c 20 50 67 6e 6f 20 2a 70 6e 54 72 75 pBt, Pgno *pnTru
13c4e 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 nc){. int rc =
13c4f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 SQLITE_OK;. Pag
13c50 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 er *pPager = pBt
13c51 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 41 5f ->pPager;. VVA_
13c52 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 20 3d ONLY( int nRef =
13c53 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 sqlite3PagerRef
13c54 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 3b count(pPager) );
13c55 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
13c56 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
13c57 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
13c58 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 invalidateAllOve
13c59 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b rflowCache(pBt);
13c5a 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d 3e 61 . assert(pBt->a
13c5b 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 utoVacuum);. if
13c5c 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 ( !pBt->incrVacu
13c5d 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e um ){. Pgno n
13c5e 46 69 6e 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 Fin = 0;.. if
13c5f 28 20 70 42 74 2d 3e 6e 54 72 75 6e 63 3d 3d 30 ( pBt->nTrunc==0
13c60 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e ){. Pgno n
13c61 46 72 65 65 3b 0a 20 20 20 20 20 20 50 67 6e 6f Free;. Pgno
13c62 20 6e 50 74 72 6d 61 70 3b 0a 20 20 20 20 20 20 nPtrmap;.
13c63 63 6f 6e 73 74 20 69 6e 74 20 70 67 73 7a 20 3d const int pgsz =
13c64 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a pBt->pageSize;.
13c65 20 20 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 67 Pgno nOrig
13c66 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e = pagerPagecoun
13c67 74 28 70 42 74 29 3b 0a 0a 20 20 20 20 20 20 69 t(pBt);.. i
13c68 66 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 f( PTRMAP_ISPAGE
13c69 28 70 42 74 2c 20 6e 4f 72 69 67 29 20 29 7b 0a (pBt, nOrig) ){.
13c6a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
13c6b 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
13c6c 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 PT;. }.
13c6d 20 20 69 66 28 20 6e 4f 72 69 67 3d 3d 50 45 4e if( nOrig==PEN
13c6e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
13c6f 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6e Bt) ){. n
13c70 4f 72 69 67 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a Orig--;. }.
13c71 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 67 65 nFree = ge
13c72 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 t4byte(&pBt->pPa
13c73 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b ge1->aData[36]);
13c74 0a 20 20 20 20 20 20 6e 50 74 72 6d 61 70 20 3d . nPtrmap =
13c75 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 2b 50 54 (nFree-nOrig+PT
13c76 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c RMAP_PAGENO(pBt,
13c77 20 6e 4f 72 69 67 29 2b 70 67 73 7a 2f 35 29 2f nOrig)+pgsz/5)/
13c78 28 70 67 73 7a 2f 35 29 3b 0a 20 20 20 20 20 20 (pgsz/5);.
13c79 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 6e nFin = nOrig - n
13c7a 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b 0a Free - nPtrmap;.
13c7b 20 20 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3e if( nOrig>
13c7c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
13c7d 45 28 70 42 74 29 20 26 26 20 6e 46 69 6e 3c 3d E(pBt) && nFin<=
13c7e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
13c7f 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 E(pBt) ){.
13c80 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 20 20 nFin--;.
13c81 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 50 }. while( P
13c82 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
13c83 2c 20 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d , nFin) || nFin=
13c84 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
13c85 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 GE(pBt) ){.
13c86 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 20 nFin--;.
13c87 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 77 68 }. }.. wh
13c88 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
13c89 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
13c8a 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 incrVacuumStep(
13c8b 70 42 74 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 pBt, nFin);.
13c8c 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
13c8d 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 LITE_DONE ){.
13c8e 20 20 20 61 73 73 65 72 74 28 6e 46 69 6e 3d 3d assert(nFin==
13c8f 30 20 7c 7c 20 70 42 74 2d 3e 6e 54 72 75 6e 63 0 || pBt->nTrunc
13c90 3d 3d 30 20 7c 7c 20 6e 46 69 6e 3c 3d 70 42 74 ==0 || nFin<=pBt
13c91 2d 3e 6e 54 72 75 6e 63 29 3b 0a 20 20 20 20 20 ->nTrunc);.
13c92 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
13c93 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
13c94 6e 54 72 75 6e 63 20 26 26 20 6e 46 69 6e 20 29 nTrunc && nFin )
13c95 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
13c96 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
13c97 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 (pBt->pPage1->pD
13c98 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
13c99 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 put4byte(&pBt->p
13c9a 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
13c9b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 75 , 0);. pu
13c9c 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 t4byte(&pBt->pPa
13c9d 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 ge1->aData[36],
13c9e 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 0);. pBt-
13c9f 3e 6e 54 72 75 6e 63 20 3d 20 6e 46 69 6e 3b 0a >nTrunc = nFin;.
13ca0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
13ca1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13ca2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
13ca3 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 ite3PagerRollbac
13ca4 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d k(pPager);. }
13ca5 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d . }.. if( rc==
13ca6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13ca7 20 2a 70 6e 54 72 75 6e 63 20 3d 20 70 42 74 2d *pnTrunc = pBt-
13ca8 3e 6e 54 72 75 6e 63 3b 0a 20 20 20 20 70 42 74 >nTrunc;. pBt
13ca9 2d 3e 6e 54 72 75 6e 63 20 3d 20 30 3b 0a 20 20 ->nTrunc = 0;.
13caa 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 66 }. assert( nRef
13cab 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 ==sqlite3PagerRe
13cac 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 fcount(pPager) )
13cad 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
13cae 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 ..#endif /* ifnd
13caf 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
13cb0 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a UTOVACUUM */../*
13cb1 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
13cb2 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 does the first
13cb3 70 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 phase of a two-p
13cb4 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 hase commit. Th
13cb5 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 is routine.** ca
13cb6 75 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 uses a rollback
13cb7 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 journal to be cr
13cb8 65 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 eated (if it doe
13cb9 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 s not already ex
13cba 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 ist).** and popu
13cbb 6c 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 lated with enoug
13cbc 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f h information so
13cbd 20 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 that if a power
13cbe 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 loss occurs.**
13cbf 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e the database can
13cc0 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 be restored to
13cc1 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 its original sta
13cc2 74 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 te by playing ba
13cc3 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 ck.** the journa
13cc4 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e l. Then the con
13cc5 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 tents of the jou
13cc6 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 rnal are flushed
13cc7 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 out to.** the d
13cc8 69 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 isk. After the
13cc9 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c journal is safel
13cca 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 y on oxide, the
13ccb 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a changes to the.*
13ccc 2a 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77 * database are w
13ccd 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
13cce 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
13ccf 64 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 d flushed to oxi
13cd0 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e de..** At the en
13cd1 64 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 d of this call,
13cd2 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
13cd3 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 rnal still exist
13cd4 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b s on the.** disk
13cd5 20 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c and we are stil
13cd6 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f l holding all lo
13cd7 63 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e cks, so the tran
13cd8 73 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a saction has not.
13cd9 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 ** committed. S
13cda 65 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 ee sqlite3BtreeC
13cdb 6f 6d 6d 69 74 28 29 20 66 6f 72 20 74 68 65 20 ommit() for the
13cdc 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 second phase of
13cdd 74 68 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 the.** commit pr
13cde 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ocess..**.** Thi
13cdf 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f s call is a no-o
13ce0 70 20 69 66 20 6e 6f 20 77 72 69 74 65 2d 74 72 p if no write-tr
13ce1 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 75 72 ansaction is cur
13ce2 72 65 6e 74 6c 79 20 61 63 74 69 76 65 20 6f 6e rently active on
13ce3 20 70 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 pBt..**.** Othe
13ce4 72 77 69 73 65 2c 20 73 79 6e 63 20 74 68 65 20 rwise, sync the
13ce5 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f database file fo
13ce6 72 20 74 68 65 20 62 74 72 65 65 20 70 42 74 2e r the btree pBt.
13ce7 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 zMaster points
13ce8 74 6f 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f to.** the name o
13ce9 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e f a master journ
13cea 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f al file that sho
13ceb 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 uld be written i
13cec 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 nto the.** indiv
13ced 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 idual journal fi
13cee 6c 65 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 le, or is NULL,
13cef 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d 61 indicating no ma
13cf0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
13cf1 65 20 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64 61 e .** (single da
13cf2 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 tabase transacti
13cf3 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 on)..**.** When
13cf4 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 this is called,
13cf5 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
13cf6 61 6c 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 al should alread
13cf7 79 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 63 y have been.** c
13cf8 72 65 61 74 65 64 2c 20 70 6f 70 75 6c 61 74 65 reated, populate
13cf9 64 20 77 69 74 68 20 74 68 69 73 20 6a 6f 75 72 d with this jour
13cfa 6e 61 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 nal pointer and
13cfb 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a synced to disk..
13cfc 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73 20 **.** Once this
13cfd 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72 is routine has r
13cfe 65 74 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e 6c eturned, the onl
13cff 79 20 74 68 69 6e 67 20 72 65 71 75 69 72 65 64 y thing required
13d00 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68 to commit.** th
13d01 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 e write-transact
13d02 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 64 61 74 ion for this dat
13d03 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 74 6f abase file is to
13d04 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 delete the jour
13d05 6e 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nal..*/.SQLITE_P
13d06 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13d07 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
13d08 73 65 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20 seOne(Btree *p,
13d09 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
13d0a 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ter){. int rc =
13d0b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
13d0c 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
13d0d 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 ANS_WRITE ){.
13d0e 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
13d0f 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 50 67 6e p->pBt;. Pgn
13d10 6f 20 6e 54 72 75 6e 63 20 3d 20 30 3b 0a 20 20 o nTrunc = 0;.
13d11 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
13d12 74 65 72 28 70 29 3b 0a 20 20 20 20 70 42 74 2d ter(p);. pBt-
13d13 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 23 69 66 >db = p->db;.#if
13d14 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
13d15 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
13d16 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
13d17 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 uum ){. rc
13d18 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d = autoVacuumComm
13d19 69 74 28 70 42 74 2c 20 26 6e 54 72 75 6e 63 29 it(pBt, &nTrunc)
13d1a 3b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 ; . if( rc!
13d1b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
13d1c 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
13d1d 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 eeLeave(p);.
13d1e 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
13d1f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e }. }.#en
13d20 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c dif. rc = sql
13d21 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
13d22 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e 70 50 61 haseOne(pBt->pPa
13d23 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 54 ger, zMaster, nT
13d24 72 75 6e 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 runc, 0);. sq
13d25 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
13d26 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e p);. }. return
13d27 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f rc;.}../*.** Co
13d28 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 mmit the transac
13d29 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 tion currently i
13d2a 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a n progress..**.*
13d2b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
13d2c 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65 mplements the se
13d2d 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20 cond phase of a
13d2e 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 2-phase commit.
13d2f 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 The.** sqlite3B
13d30 74 72 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69 treeSync() routi
13d31 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 ne does the firs
13d32 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75 t phase and shou
13d33 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a ld be invoked.**
13d34 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e prior to callin
13d35 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 g this routine.
13d36 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 The sqlite3Btre
13d37 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 20 eSync() routine
13d38 64 69 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 77 did.** all the w
13d39 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69 ork of writing i
13d3a 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 nformation out t
13d3b 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 o disk and flush
13d3c 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 ing the.** conte
13d3d 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 nts so that they
13d3e 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 are written ont
13d3f 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74 o the disk platt
13d40 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a er. All this.**
13d41 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20 routine has to
13d42 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20 do is delete or
13d43 74 72 75 6e 63 61 74 65 20 74 68 65 20 72 6f 6c truncate the rol
13d44 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a 2a lback journal.**
13d45 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 74 (which causes t
13d46 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 he transaction t
13d47 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64 72 o commit) and dr
13d48 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 op locks..**.**
13d49 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 This will releas
13d4a 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b e the write lock
13d4b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
13d4c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 file. If there
13d4d 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 .** are no activ
13d4e 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c e cursors, it al
13d4f 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 so releases the
13d50 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 read lock..*/.SQ
13d51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13d52 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
13d53 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 mitPhaseTwo(Btre
13d54 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 e *p){. BtShare
13d55 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
13d56 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 .. sqlite3Btree
13d57 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d Enter(p);. pBt-
13d58 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 62 >db = p->db;. b
13d59 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 treeIntegrity(p)
13d5a 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 ;.. /* If the h
13d5b 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69 74 andle has a writ
13d5c 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 e-transaction op
13d5d 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 en, commit the s
13d5e 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20 hared-btrees .
13d5f 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 ** transaction a
13d60 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72 65 nd set the share
13d61 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 d state to TRANS
13d62 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 _READ.. */. if
13d63 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
13d64 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 ANS_WRITE ){.
13d65 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 int rc;. ass
13d66 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
13d67 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
13d68 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65 RITE );. asse
13d69 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 rt( pBt->nTransa
13d6a 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72 ction>0 );. r
13d6b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
13d6c 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
13d6d 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
13d6e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13d6f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
13d70 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
13d71 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
13d72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 c;. }. pBt
13d73 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
13d74 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 = TRANS_READ;.
13d75 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 pBt->inStmt =
13d76 30 3b 0a 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b 41 0;. }. unlockA
13d77 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20 20 llTables(p);..
13d78 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 /* If the handle
13d79 20 68 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 has any kind of
13d7a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 transaction ope
13d7b 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 n, decrement the
13d7c 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a transaction. *
13d7d 2a 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 * count of the s
13d7e 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 hared btree. If
13d7f 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
13d80 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 30 2c count reaches 0,
13d81 20 73 65 74 0a 20 20 2a 2a 20 74 68 65 20 73 68 set. ** the sh
13d82 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 ared state to TR
13d83 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e ANS_NONE. The un
13d84 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 lockBtreeIfUnuse
13d85 64 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a 20 d() call below.
13d86 20 2a 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 ** will unlock
13d87 74 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f 0a the pager.. */.
13d88 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
13d89 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a !=TRANS_NONE ){.
13d8a 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 pBt->nTransa
13d8b 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66 28 ction--;. if(
13d8c 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 0==pBt->nTransa
13d8d 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70 ction ){. p
13d8e 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
13d8f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a n = TRANS_NONE;.
13d90 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
13d91 53 65 74 20 74 68 65 20 68 61 6e 64 6c 65 73 20 Set the handles
13d92 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
13d93 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41 ion state to TRA
13d94 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f NS_NONE and unlo
13d95 63 6b 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 ck. ** the page
13d96 72 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20 63 r if this call c
13d97 6c 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 losed the only r
13d98 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 ead or write tra
13d99 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 nsaction.. */.
13d9a 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 p->inTrans = TR
13d9b 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f ANS_NONE;. unlo
13d9c 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 ckBtreeIfUnused(
13d9d 70 42 74 29 3b 0a 0a 20 20 62 74 72 65 65 49 6e pBt);.. btreeIn
13d9e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 tegrity(p);. sq
13d9f 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
13da0 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c p);. return SQL
13da1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
13da2 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65 73 20 Do both phases
13da3 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a of a commit..*/.
13da4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
13da5 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
13da6 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 29 7b ommit(Btree *p){
13da7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c . int rc;. sql
13da8 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
13da9 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
13daa 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
13dab 65 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69 66 eOne(p, 0);. if
13dac 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
13dad 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
13dae 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
13daf 61 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d 0a 20 aseTwo(p);. }.
13db0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
13db1 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
13db2 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e rc;.}..#ifndef N
13db3 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 DEBUG./*.** Retu
13db4 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
13db5 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73 20 6f write-cursors o
13db6 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e 64 pen on this hand
13db7 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f 72 20 le. This is for
13db8 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 use.** in assert
13db9 28 29 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 () expressions,
13dba 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f so it is only co
13dbb 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 42 55 47 mpiled if NDEBUG
13dbc 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e is not.** defin
13dbd 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 ed..**.** For th
13dbe 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 e purposes of th
13dbf 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 77 72 is routine, a wr
13dc0 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 61 6e ite-cursor is an
13dc1 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a y cursor that.**
13dc2 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 77 is capable of w
13dc3 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 riting to the da
13dc4 74 61 62 73 65 2e 20 20 54 68 61 74 20 6d 65 61 tabse. That mea
13dc5 6e 73 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 ns the cursor wa
13dc6 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 s.** originally
13dc7 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 opened for writi
13dc8 6e 67 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f ng and the curso
13dc9 72 20 68 61 73 20 6e 6f 74 20 62 65 20 64 69 73 r has not be dis
13dca 61 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61 76 69 abled.** by havi
13dcb 6e 67 20 69 74 73 20 73 74 61 74 65 20 63 68 61 ng its state cha
13dcc 6e 67 65 64 20 74 6f 20 43 55 52 53 4f 52 5f 46 nged to CURSOR_F
13dcd 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 AULT..*/.static
13dce 69 6e 74 20 63 6f 75 6e 74 57 72 69 74 65 43 75 int countWriteCu
13dcf 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a rsors(BtShared *
13dd0 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 pBt){. BtCursor
13dd1 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 *pCur;. int r
13dd2 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d = 0;. for(pCur=
13dd3 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 pBt->pCursor; pC
13dd4 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 ur; pCur=pCur->p
13dd5 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 Next){. if( p
13dd6 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 Cur->wrFlag && p
13dd7 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 Cur->eState!=CUR
13dd8 53 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b SOR_FAULT ) r++;
13dd9 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 . }. return r
13dda 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
13ddb 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 * This routine s
13ddc 65 74 73 20 74 68 65 20 73 74 61 74 65 20 74 6f ets the state to
13ddd 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 61 6e CURSOR_FAULT an
13dde 64 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 d the error.** c
13ddf 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 65 20 66 ode to errCode f
13de0 6f 72 20 65 76 65 72 79 20 63 75 72 73 6f 72 20 or every cursor
13de1 6f 6e 20 42 74 53 68 61 72 65 64 20 74 68 61 74 on BtShared that
13de2 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 65 72 pBtree.** refer
13de3 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 ences..**.** Eve
13de4 72 79 20 63 75 72 73 6f 72 20 69 73 20 74 72 69 ry cursor is tri
13de5 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e 67 20 pped, including
13de6 63 75 72 73 6f 72 73 20 74 68 61 74 20 62 65 6c cursors that bel
13de7 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 20 ong.** to other
13de8 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
13de9 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70 65 6e ions that happen
13dea 20 74 6f 20 62 65 20 73 68 61 72 69 6e 67 0a 2a to be sharing.*
13deb 2a 20 74 68 65 20 63 61 63 68 65 20 77 69 74 68 * the cache with
13dec 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 pBtree..**.** T
13ded 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 74 73 his routine gets
13dee 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 called when a r
13def 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e 0a ollback occurs..
13df0 2a 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 75 ** All cursors u
13df1 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 61 sing the same ca
13df2 63 68 65 20 6d 75 73 74 20 62 65 20 74 72 69 70 che must be trip
13df3 70 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e ped.** to preven
13df4 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 79 69 t them from tryi
13df5 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20 62 74 ng to use the bt
13df6 72 65 65 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 ree after.** the
13df7 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 rollback. The
13df8 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 rollback may hav
13df9 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c 65 73 e deleted tables
13dfa 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72 6f 6f .** or moved roo
13dfb 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74 20 69 t pages, so it i
13dfc 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 s not sufficient
13dfd 20 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68 65 20 to.** save the
13dfe 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 75 72 state of the cur
13dff 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72 sor. The cursor
13e00 20 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e 76 61 must be.** inva
13e01 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 lidated..*/.SQLI
13e02 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
13e03 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 sqlite3BtreeTrip
13e04 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65 AllCursors(Btree
13e05 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 65 72 *pBtree, int er
13e06 72 43 6f 64 65 29 7b 0a 20 20 42 74 43 75 72 73 rCode){. BtCurs
13e07 6f 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 or *p;. sqlite3
13e08 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 BtreeEnter(pBtre
13e09 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 e);. for(p=pBtr
13e0a 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 ee->pBt->pCursor
13e0b 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 ; p; p=p->pNext)
13e0c 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
13e0d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
13e0e 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20 20 arCursor(p);.
13e0f 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 p->eState = CUR
13e10 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 20 70 SOR_FAULT;. p
13e11 2d 3e 73 6b 69 70 20 3d 20 65 72 72 43 6f 64 65 ->skip = errCode
13e12 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
13e13 3c 3d 70 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 <=p->iPage; i++)
13e14 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 {. releaseP
13e15 61 67 65 28 70 2d 3e 61 70 50 61 67 65 5b 69 5d age(p->apPage[i]
13e16 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 50 61 );. p->apPa
13e17 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d ge[i] = 0;. }
13e18 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
13e19 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 reeLeave(pBtree)
13e1a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 ;.}../*.** Rollb
13e1b 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 ack the transact
13e1c 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2e ion in progress.
13e1d 20 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 77 69 All cursors wi
13e1e 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 ll be.** invalid
13e1f 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 ed by this opera
13e20 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d tion. Any attem
13e21 70 74 20 74 6f 20 75 73 65 20 61 20 63 75 72 73 pt to use a curs
13e22 6f 72 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 6f or.** that was o
13e23 70 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e pen at the begin
13e24 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 6f 70 65 ning of this ope
13e25 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75 ration will resu
13e26 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 65 72 72 6f lt.** in an erro
13e27 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 r..**.** This wi
13e28 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 ll release the w
13e29 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 rite lock on the
13e2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
13e2b 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 If there.** are
13e2c 20 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f no active curso
13e2d 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 rs, it also rele
13e2e 61 73 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f ases the read lo
13e2f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ck..*/.SQLITE_PR
13e30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
13e31 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 3BtreeRollback(B
13e32 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 tree *p){. int
13e33 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a rc;. BtShared *
13e34 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
13e35 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b MemPage *pPage1;
13e36 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 .. sqlite3Btree
13e37 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d Enter(p);. pBt-
13e38 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 >db = p->db;. r
13e39 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f c = saveAllCurso
13e3a 72 73 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 23 rs(pBt, 0, 0);.#
13e3b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
13e3c 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
13e3d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13e3e 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 _OK ){. /* Th
13e3f 69 73 20 69 73 20 61 20 68 6f 72 72 69 62 6c 65 is is a horrible
13e40 20 73 69 74 75 61 74 69 6f 6e 2e 20 41 6e 20 49 situation. An I
13e41 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 O or malloc() er
13e42 72 6f 72 20 6f 63 63 75 72 65 64 20 77 68 69 6c ror occured whil
13e43 73 74 0a 20 20 20 20 2a 2a 20 74 72 79 69 6e 67 st. ** trying
13e44 20 74 6f 20 73 61 76 65 20 63 75 72 73 6f 72 20 to save cursor
13e45 70 6f 73 69 74 69 6f 6e 73 2e 20 49 66 20 74 68 positions. If th
13e46 69 73 20 69 73 20 61 6e 20 61 75 74 6f 6d 61 74 is is an automat
13e47 69 63 20 72 6f 6c 6c 62 61 63 6b 20 28 61 73 0a ic rollback (as.
13e48 20 20 20 20 2a 2a 20 74 68 65 20 72 65 73 75 6c ** the resul
13e49 74 20 6f 66 20 61 20 63 6f 6e 73 74 72 61 69 6e t of a constrain
13e4a 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c t, malloc() fail
13e4b 75 72 65 20 6f 72 20 49 4f 20 65 72 72 6f 72 29 ure or IO error)
13e4c 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68 then . ** th
13e4d 65 20 63 61 63 68 65 20 6d 61 79 20 62 65 20 69 e cache may be i
13e4e 6e 74 65 72 6e 61 6c 6c 79 20 69 6e 63 6f 6e 73 nternally incons
13e4f 69 73 74 65 6e 74 20 28 6e 6f 74 20 63 6f 6e 74 istent (not cont
13e50 61 69 6e 20 76 61 6c 69 64 20 74 72 65 65 73 29 ain valid trees)
13e51 20 73 6f 0a 20 20 20 20 2a 2a 20 77 65 20 63 61 so. ** we ca
13e52 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 72 65 74 75 nnot simply retu
13e53 72 6e 20 74 68 65 20 65 72 72 6f 72 20 74 6f 20 rn the error to
13e54 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 73 74 the caller. Inst
13e55 65 61 64 2c 20 61 62 6f 72 74 20 0a 20 20 20 20 ead, abort .
13e56 2a 2a 20 61 6c 6c 20 71 75 65 72 69 65 73 20 74 ** all queries t
13e57 68 61 74 20 6d 61 79 20 62 65 20 75 73 69 6e 67 hat may be using
13e58 20 61 6e 79 20 6f 66 20 74 68 65 20 63 75 72 73 any of the curs
13e59 6f 72 73 20 74 68 61 74 20 66 61 69 6c 65 64 20 ors that failed
13e5a 74 6f 20 73 61 76 65 2e 0a 20 20 20 20 2a 2f 0a to save.. */.
13e5b 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
13e5c 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 70 TripAllCursors(p
13e5d 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 , rc);. }.#endi
13e5e 66 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 f. btreeIntegri
13e5f 74 79 28 70 29 3b 0a 20 20 75 6e 6c 6f 63 6b 41 ty(p);. unlockA
13e60 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20 20 llTables(p);..
13e61 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d if( p->inTrans==
13e62 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 TRANS_WRITE ){.
13e63 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 23 69 66 int rc2;..#if
13e64 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
13e65 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
13e66 70 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 30 3b pBt->nTrunc = 0;
13e67 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 61 73 73 .#endif.. ass
13e68 65 72 74 28 20 54 52 41 4e 53 5f 57 52 49 54 45 ert( TRANS_WRITE
13e69 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 ==pBt->inTransac
13e6a 74 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 32 20 tion );. rc2
13e6b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f = sqlite3PagerRo
13e6c 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 61 67 llback(pBt->pPag
13e6d 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32 er);. if( rc2
13e6e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
13e6f 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 rc = rc2;.
13e70 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 }.. /* The
13e71 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 rollback may ha
13e72 76 65 20 64 65 73 74 72 6f 79 65 64 20 74 68 65 ve destroyed the
13e73 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 76 pPage1->aData v
13e74 61 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 2a 2a alue. So. **
13e75 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 42 74 72 call sqlite3Btr
13e76 65 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20 70 eeGetPage() on p
13e77 61 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20 6d age 1 again to m
13e78 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 ake. ** sure
13e79 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69 73 pPage1->aData is
13e7a 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 set correctly.
13e7b 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
13e7c 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 e3BtreeGetPage(p
13e7d 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c 20 Bt, 1, &pPage1,
13e7e 30 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0)==SQLITE_OK ){
13e7f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
13e80 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20 ge(pPage1);.
13e81 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f }. assert( co
13e82 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28 untWriteCursors(
13e83 70 42 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 pBt)==0 );. p
13e84 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
13e85 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a n = TRANS_READ;.
13e86 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e }.. if( p->in
13e87 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e Trans!=TRANS_NON
13e88 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 E ){. assert(
13e89 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 pBt->nTransacti
13e8a 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 70 42 74 2d on>0 );. pBt-
13e8b 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b >nTransaction--;
13e8c 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d . if( 0==pBt-
13e8d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b >nTransaction ){
13e8e 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 . pBt->inTr
13e8f 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e ansaction = TRAN
13e90 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 S_NONE;. }.
13e91 7d 0a 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 }.. p->inTrans
13e92 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 = TRANS_NONE;.
13e93 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b pBt->inStmt = 0;
13e94 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 . unlockBtreeIf
13e95 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 0a 20 20 Unused(pBt);..
13e96 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
13e97 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
13e98 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
13e99 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
13e9a 20 53 74 61 72 74 20 61 20 73 74 61 74 65 6d 65 Start a stateme
13e9b 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f nt subtransactio
13e9c 6e 2e 20 20 54 68 65 20 73 75 62 74 72 61 6e 73 n. The subtrans
13e9d 61 63 74 69 6f 6e 20 63 61 6e 0a 2a 2a 20 63 61 action can.** ca
13e9e 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b n be rolled back
13e9f 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 6f independently o
13ea0 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 f the main trans
13ea1 61 63 74 69 6f 6e 2e 0a 2a 2a 20 59 6f 75 20 6d action..** You m
13ea2 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e ust start a tran
13ea3 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 20 73 saction before s
13ea4 74 61 72 74 69 6e 67 20 61 20 73 75 62 74 72 61 tarting a subtra
13ea5 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 nsaction..** The
13ea6 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 subtransaction
13ea7 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 is ended automat
13ea8 69 63 61 6c 6c 79 20 69 66 20 74 68 65 20 6d 61 ically if the ma
13ea9 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a in transaction.*
13eaa 2a 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f 6c * commits or rol
13eab 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4f ls back..**.** O
13eac 6e 6c 79 20 6f 6e 65 20 73 75 62 74 72 61 6e 73 nly one subtrans
13ead 61 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 61 63 action may be ac
13eae 74 69 76 65 20 61 74 20 61 20 74 69 6d 65 2e 20 tive at a time.
13eaf 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 It is an error
13eb0 74 6f 20 74 72 79 0a 2a 2a 20 74 6f 20 73 74 61 to try.** to sta
13eb1 72 74 20 61 20 6e 65 77 20 73 75 62 74 72 61 6e rt a new subtran
13eb2 73 61 63 74 69 6f 6e 20 69 66 20 61 6e 6f 74 68 saction if anoth
13eb3 65 72 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f er subtransactio
13eb4 6e 20 69 73 20 61 6c 72 65 61 64 79 20 61 63 74 n is already act
13eb5 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 ive..**.** State
13eb6 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 ment subtransact
13eb7 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 61 72 ions are used ar
13eb8 6f 75 6e 64 20 69 6e 64 69 76 69 64 75 61 6c 20 ound individual
13eb9 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a SQL statements.*
13eba 2a 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 61 * that are conta
13ebb 69 6e 65 64 20 77 69 74 68 69 6e 20 61 20 42 45 ined within a BE
13ebc 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 6c 6f GIN...COMMIT blo
13ebd 63 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 ck. If a constr
13ebe 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 aint.** error oc
13ebf 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 curs within the
13ec0 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 65 statement, the e
13ec1 66 66 65 63 74 20 6f 66 20 74 68 61 74 20 6f 6e ffect of that on
13ec2 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 e statement.** c
13ec3 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 an be rolled bac
13ec4 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 k without having
13ec5 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 to rollback the
13ec6 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 entire transact
13ec7 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
13ec8 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13ec9 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 e3BtreeBeginStmt
13eca 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e (Btree *p){. in
13ecb 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 t rc;. BtShared
13ecc 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
13ecd 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
13ece 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 ter(p);. pBt->d
13ecf 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 b = p->db;. if(
13ed0 20 28 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 (p->inTrans!=TR
13ed1 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c 20 70 42 ANS_WRITE) || pB
13ed2 74 2d 3e 69 6e 53 74 6d 74 20 29 7b 0a 20 20 20 t->inStmt ){.
13ed3 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f rc = pBt->readO
13ed4 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41 nly ? SQLITE_REA
13ed5 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45 DONLY : SQLITE_E
13ed6 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 RROR;. }else{.
13ed7 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
13ed8 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
13ed9 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
13eda 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 rc = pBt->read
13edb 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 4f 4b Only ? SQLITE_OK
13edc 20 3a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 : sqlite3PagerS
13edd 74 6d 74 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 tmtBegin(pBt->pP
13ede 61 67 65 72 29 3b 0a 20 20 20 20 70 42 74 2d 3e ager);. pBt->
13edf 69 6e 53 74 6d 74 20 3d 20 31 3b 0a 20 20 7d 0a inStmt = 1;. }.
13ee0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
13ee1 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
13ee2 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 rc;.}.../*.** C
13ee3 6f 6d 6d 69 74 20 74 68 65 20 73 74 61 74 6d 65 ommit the statme
13ee4 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f nt subtransactio
13ee5 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 70 n currently in p
13ee6 72 6f 67 72 65 73 73 2e 20 20 49 66 20 6e 6f 0a rogress. If no.
13ee7 2a 2a 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f ** subtransactio
13ee8 6e 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 69 n is active, thi
13ee9 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f s is a no-op..*/
13eea 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13eeb 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
13eec 43 6f 6d 6d 69 74 53 74 6d 74 28 42 74 72 65 65 CommitStmt(Btree
13eed 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *p){. int rc;.
13eee 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
13eef 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 = p->pBt;. sqli
13ef0 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
13ef1 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d ;. pBt->db = p-
13ef2 3e 64 62 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e >db;. if( pBt->
13ef3 69 6e 53 74 6d 74 20 26 26 20 21 70 42 74 2d 3e inStmt && !pBt->
13ef4 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 readOnly ){.
13ef5 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
13ef6 72 53 74 6d 74 43 6f 6d 6d 69 74 28 70 42 74 2d rStmtCommit(pBt-
13ef7 3e 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73 >pPager);. }els
13ef8 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 e{. rc = SQLI
13ef9 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 42 74 TE_OK;. }. pBt
13efa 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 ->inStmt = 0;.
13efb 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
13efc 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
13efd 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c c;.}../*.** Roll
13efe 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 back the active
13eff 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 statement subtra
13f00 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 6e 6f nsaction. If no
13f01 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 0a subtransaction.
13f02 2a 2a 20 69 73 20 61 63 74 69 76 65 20 74 68 69 ** is active thi
13f03 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
13f04 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 o-op..**.** All
13f05 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 20 cursors will be
13f06 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 74 invalidated by t
13f07 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 his operation.
13f08 41 6e 79 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 Any attempt.** t
13f09 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72 20 74 o use a cursor t
13f0a 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74 20 hat was open at
13f0b 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
13f0c 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 0a this operation.
13f0d 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 ** will result i
13f0e 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 n an error..*/.S
13f0f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
13f10 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f t sqlite3BtreeRo
13f11 6c 6c 62 61 63 6b 53 74 6d 74 28 42 74 72 65 65 llbackStmt(Btree
13f12 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d *p){. int rc =
13f13 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 SQLITE_OK;. Bt
13f14 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
13f15 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 >pBt;. sqlite3B
13f16 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
13f17 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b pBt->db = p->db;
13f18 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 53 74 . if( pBt->inSt
13f19 6d 74 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 mt && !pBt->read
13f1a 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d Only ){. rc =
13f1b 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d sqlite3PagerStm
13f1c 74 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 tRollback(pBt->p
13f1d 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 74 2d Pager);. pBt-
13f1e 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 7d >inStmt = 0;. }
13f1f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
13f20 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
13f21 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
13f22 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 reate a new curs
13f23 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 or for the BTree
13f24 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f whose root is o
13f25 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 n the page.** iT
13f26 61 62 6c 65 2e 20 20 54 68 65 20 61 63 74 20 6f able. The act o
13f27 66 20 61 63 71 75 69 72 69 6e 67 20 61 20 63 75 f acquiring a cu
13f28 72 73 6f 72 20 67 65 74 73 20 61 20 72 65 61 64 rsor gets a read
13f29 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 74 68 65 lock on .** the
13f2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
13f2b 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d **.** If wrFlag=
13f2c 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 =0, then the cur
13f2d 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 sor can only be
13f2e 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 used for reading
13f2f 2e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d ..** If wrFlag==
13f30 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 1, then the curs
13f31 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 or can be used f
13f32 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 66 6f or reading or fo
13f33 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69 66 20 r.** writing if
13f34 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 73 other conditions
13f35 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 72 65 for writing are
13f36 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68 65 73 also met. Thes
13f37 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 63 6f 6e e.** are the con
13f38 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d 75 73 ditions that mus
13f39 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72 64 65 t be met in orde
13f3a 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20 74 6f r for writing to
13f3b 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 65 64 3a 0a .** be allowed:.
13f3c 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63 75 **.** 1: The cu
13f3d 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 rsor must have b
13f3e 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74 68 20 een opened with
13f3f 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20 wrFlag==1.**.**
13f40 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61 62 61 2: Other databa
13f41 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 se connections t
13f42 68 61 74 20 73 68 61 72 65 20 74 68 65 20 73 61 hat share the sa
13f43 6d 65 20 70 61 67 65 72 20 63 61 63 68 65 0a 2a me pager cache.*
13f44 2a 20 20 20 20 20 62 75 74 20 77 68 69 63 68 20 * but which
13f45 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 are not in the R
13f46 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 EAD_UNCOMMITTED
13f47 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20 68 61 state may not ha
13f48 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73 6f 72 ve.** cursor
13f49 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72 46 6c s open with wrFl
13f4a 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73 61 6d ag==0 on the sam
13f4b 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65 72 77 e table. Otherw
13f4c 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65 20 63 ise.** the c
13f4d 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 hanges made by t
13f4e 68 69 73 20 77 72 69 74 65 20 63 75 72 73 6f 72 his write cursor
13f4f 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69 62 6c would be visibl
13f50 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20 e to.** the
13f51 72 65 61 64 20 63 75 72 73 6f 72 73 20 69 6e 20 read cursors in
13f52 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 62 61 the other databa
13f53 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a se connection..*
13f54 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64 61 74 *.** 3: The dat
13f55 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 77 72 abase must be wr
13f56 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 itable (not on r
13f57 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 0a ead-only media).
13f58 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72 65 20 **.** 4: There
13f59 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74 69 76 must be an activ
13f5a 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e transaction..*
13f5b 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69 6e 67 *.** No checking
13f5c 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 is done to make
13f5d 20 73 75 72 65 20 74 68 61 74 20 70 61 67 65 20 sure that page
13f5e 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20 69 73 iTable really is
13f5f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 the.** root pag
13f60 65 20 6f 66 20 61 20 62 2d 74 72 65 65 2e 20 20 e of a b-tree.
13f61 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 If it is not, th
13f62 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 en the cursor ac
13f63 71 75 69 72 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e quired.** will n
13f64 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c ot work correctl
13f65 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 y..**.** It is a
13f66 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 ssumed that the
13f67 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
13f68 6f 72 53 69 7a 65 28 29 20 62 79 74 65 73 20 6f orSize() bytes o
13f69 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70 6f 69 f memory .** poi
13f6a 6e 74 65 64 20 74 6f 20 62 79 20 70 43 75 72 20 nted to by pCur
13f6b 68 61 76 65 20 62 65 65 6e 20 7a 65 72 6f 65 64 have been zeroed
13f6c 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a by the caller..
13f6d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 */.static int bt
13f6e 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 reeCursor(. Btr
13f6f 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 ee *p,
13f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13f71 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 /* The btree
13f72 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */. int iTable
13f73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
13f74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13f75 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 Root page of tab
13f76 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 le to open */.
13f77 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 int wrFlag,
13f78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13f79 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 /* 1 to w
13f7a 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c rite. 0 read-onl
13f7b 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 y */. struct Ke
13f7c 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c yInfo *pKeyInfo,
13f7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13f7e 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f First arg to co
13f7f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f mparison functio
13f80 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 n */. BtCursor
13f81 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 *pCur
13f82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13f83 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20 63 Space for new c
13f84 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e ursor */.){. in
13f85 74 20 72 63 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 t rc;. Pgno nPa
13f86 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge;. BtShared *
13f87 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 pBt = p->pBt;..
13f88 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
13f89 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
13f8a 70 29 20 29 3b 0a 20 20 69 66 28 20 77 72 46 6c p) );. if( wrFl
13f8b 61 67 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 ag ){. if( pB
13f8c 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 t->readOnly ){.
13f8d 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
13f8e 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 TE_READONLY;.
13f8f 20 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b }. if( check
13f90 52 65 61 64 4c 6f 63 6b 73 28 70 2c 20 69 54 61 ReadLocks(p, iTa
13f91 62 6c 65 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 ble, 0, 0) ){.
13f92 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
13f93 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 7d 0a E_LOCKED;. }.
13f94 20 20 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e }.. if( pBt->
13f95 70 50 61 67 65 31 3d 3d 30 20 29 7b 0a 20 20 20 pPage1==0 ){.
13f96 20 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 57 rc = lockBtreeW
13f97 69 74 68 52 65 74 72 79 28 70 29 3b 0a 20 20 20 ithRetry(p);.
13f98 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13f99 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
13f9a 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
13f9b 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e if( pBt->readOn
13f9c 6c 79 20 26 26 20 77 72 46 6c 61 67 20 29 7b 0a ly && wrFlag ){.
13f9d 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
13f9e 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ITE_READONLY;.
13f9f 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e }. }. pCur->
13fa0 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f pgnoRoot = (Pgno
13fa1 29 69 54 61 62 6c 65 3b 0a 20 20 72 63 20 3d 20 )iTable;. rc =
13fa2 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
13fa3 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 count(pBt->pPage
13fa4 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50 61 67 65 r, (int *)&nPage
13fa5 29 3b 20 0a 20 20 69 66 28 20 72 63 21 3d 53 51 ); . if( rc!=SQ
13fa6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
13fa7 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
13fa8 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 if( iTable==1 &&
13fa9 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 nPage==0 ){.
13faa 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 4d 50 rc = SQLITE_EMP
13fab 54 59 3b 0a 20 20 20 20 67 6f 74 6f 20 63 72 65 TY;. goto cre
13fac 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70 ate_cursor_excep
13fad 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d tion;. }. rc =
13fae 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 getAndInitPage(
13faf 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 pBt, pCur->pgnoR
13fb0 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 oot, &pCur->apPa
13fb1 67 65 5b 30 5d 29 3b 0a 20 20 69 66 28 20 72 63 ge[0]);. if( rc
13fb2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
13fb3 20 20 20 67 6f 74 6f 20 63 72 65 61 74 65 5f 63 goto create_c
13fb4 75 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3b ursor_exception;
13fb5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74 . }.. /* Now t
13fb6 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72 hat no other err
13fb7 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66 ors can occur, f
13fb8 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e inish filling in
13fb9 20 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20 the BtCursor.
13fba 2a 2a 20 76 61 72 69 61 62 6c 65 73 2c 20 6c 69 ** variables, li
13fbb 6e 6b 20 74 68 65 20 63 75 72 73 6f 72 20 69 6e nk the cursor in
13fbc 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 20 to the BtShared
13fbd 6c 69 73 74 20 61 6e 64 20 73 65 74 20 2a 70 70 list and set *pp
13fbe 43 75 72 20 28 74 68 65 0a 20 20 2a 2a 20 6f 75 Cur (the. ** ou
13fbf 74 70 75 74 20 61 72 67 75 6d 65 6e 74 20 74 6f tput argument to
13fc0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 29 2e this function).
13fc1 0a 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 4b . */. pCur->pK
13fc2 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 eyInfo = pKeyInf
13fc3 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 65 o;. pCur->pBtre
13fc4 65 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70 e = p;. pCur->p
13fc5 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 43 75 72 Bt = pBt;. pCur
13fc6 2d 3e 77 72 46 6c 61 67 20 3d 20 77 72 46 6c 61 ->wrFlag = wrFla
13fc7 67 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 g;. pCur->pNext
13fc8 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b = pBt->pCursor;
13fc9 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 . if( pCur->pNe
13fca 78 74 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e xt ){. pCur->
13fcb 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 pNext->pPrev = p
13fcc 43 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e Cur;. }. pBt->
13fcd 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a pCursor = pCur;.
13fce 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
13fcf 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b CURSOR_INVALID;
13fd0 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
13fd1 45 5f 4f 4b 3b 0a 0a 63 72 65 61 74 65 5f 63 75 E_OK;..create_cu
13fd2 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3a 0a rsor_exception:.
13fd3 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 releasePage(pC
13fd4 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a ur->apPage[0]);.
13fd5 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 unlockBtreeIfU
13fd6 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 72 65 nused(pBt);. re
13fd7 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
13fd8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13fd9 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
13fda 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 (. Btree *p,
13fdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fdd 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a /* The btree */.
13fde 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 int iTable,
13fdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13fe1 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 Root page of ta
13fe2 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 ble to open */.
13fe3 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 int wrFlag,
13fe4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fe5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13fe6 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 1 to write. 0 re
13fe7 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 ad-only */. str
13fe8 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 uct KeyInfo *pKe
13fe9 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 yInfo,
13fea 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
13feb 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 61 72 t arg to xCompar
13fec 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f e() */. BtCurso
13fed 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 r *pCur
13fee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fef 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 /* Write ne
13ff0 77 20 63 75 72 73 6f 72 20 68 65 72 65 20 2a 2f w cursor here */
13ff1 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
13ff2 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
13ff3 72 28 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e r(p);. p->pBt->
13ff4 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 63 db = p->db;. rc
13ff5 20 3d 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 = btreeCursor(p
13ff6 2c 20 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 , iTable, wrFlag
13ff7 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 , pKeyInfo, pCur
13ff8 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
13ff9 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
13ffa 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 urn rc;.}.SQLITE
13ffb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
13ffc 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 ite3BtreeCursorS
13ffd 69 7a 65 28 29 7b 0a 20 20 72 65 74 75 72 6e 20 ize(){. return
13ffe 73 69 7a 65 6f 66 28 42 74 43 75 72 73 6f 72 29 sizeof(BtCursor)
13fff 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f ;.}..../*.** Clo
14000 73 65 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68 se a cursor. Th
14001 65 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 e read lock on t
14002 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
14003 20 69 73 20 72 65 6c 65 61 73 65 64 0a 2a 2a 20 is released.**
14004 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 63 75 when the last cu
14005 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a rsor is closed..
14006 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14007 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
14008 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 eeCloseCursor(Bt
14009 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
1400a 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 20 3d Btree *pBtree =
1400b 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 pCur->pBtree;.
1400c 20 69 66 28 20 70 42 74 72 65 65 20 29 7b 0a 20 if( pBtree ){.
1400d 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 int i;. Bt
1400e 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 Shared *pBt = pC
1400f 75 72 2d 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c ur->pBt;. sql
14010 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
14011 42 74 72 65 65 29 3b 0a 20 20 20 20 70 42 74 2d Btree);. pBt-
14012 3e 64 62 20 3d 20 70 42 74 72 65 65 2d 3e 64 62 >db = pBtree->db
14013 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
14014 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 eeClearCursor(pC
14015 75 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 ur);. if( pCu
14016 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 r->pPrev ){.
14017 20 20 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70 pCur->pPrev->p
14018 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 Next = pCur->pNe
14019 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 xt;. }else{.
1401a 20 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f pBt->pCurso
1401b 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b r = pCur->pNext;
1401c 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1401d 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 Cur->pNext ){.
1401e 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d pCur->pNext-
1401f 3e 70 50 72 65 76 20 3d 20 70 43 75 72 2d 3e 70 >pPrev = pCur->p
14020 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Prev;. }.
14021 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 for(i=0; i<=pCur
14022 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 ->iPage; i++){.
14023 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
14024 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d (pCur->apPage[i]
14025 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 6c );. }. unl
14026 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 ockBtreeIfUnused
14027 28 70 42 74 29 3b 0a 20 20 20 20 69 6e 76 61 6c (pBt);. inval
14028 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 idateOverflowCac
14029 68 65 28 70 43 75 72 29 3b 0a 20 20 20 20 2f 2a he(pCur);. /*
1402a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
1402b 75 72 29 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 ur); */. sqli
1402c 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 te3BtreeLeave(pB
1402d 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 tree);. }. ret
1402e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1402f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 74 ../*.** Make a t
14030 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20 emporary cursor
14031 62 79 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 by filling in th
14032 65 20 66 69 65 6c 64 73 20 6f 66 20 70 54 65 6d e fields of pTem
14033 70 43 75 72 2e 0a 2a 2a 20 54 68 65 20 74 65 6d pCur..** The tem
14034 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20 69 73 porary cursor is
14035 20 6e 6f 74 20 6f 6e 20 74 68 65 20 63 75 72 73 not on the curs
14036 6f 72 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 or list for the
14037 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Btree..*/.SQLITE
14038 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
14039 6c 69 74 65 33 42 74 72 65 65 47 65 74 54 65 6d lite3BtreeGetTem
1403a 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 pCursor(BtCursor
1403b 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73 6f 72 *pCur, BtCursor
1403c 20 2a 70 54 65 6d 70 43 75 72 29 7b 0a 20 20 69 *pTempCur){. i
1403d 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 nt i;. assert(
1403e 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
1403f 28 70 43 75 72 29 20 29 3b 0a 20 20 6d 65 6d 63 (pCur) );. memc
14040 70 79 28 70 54 65 6d 70 43 75 72 2c 20 70 43 75 py(pTempCur, pCu
14041 72 2c 20 73 69 7a 65 6f 66 28 42 74 43 75 72 73 r, sizeof(BtCurs
14042 6f 72 29 29 3b 0a 20 20 70 54 65 6d 70 43 75 72 or));. pTempCur
14043 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 ->pNext = 0;. p
14044 54 65 6d 70 43 75 72 2d 3e 70 50 72 65 76 20 3d TempCur->pPrev =
14045 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;. for(i=0; i
14046 3c 3d 70 54 65 6d 70 43 75 72 2d 3e 69 50 61 67 <=pTempCur->iPag
14047 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c e; i++){. sql
14048 69 74 65 33 50 61 67 65 72 52 65 66 28 70 54 65 ite3PagerRef(pTe
14049 6d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d mpCur->apPage[i]
1404a 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a ->pDbPage);. }.
1404b 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
1404c 61 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 a temporary curs
1404d 6f 72 20 73 75 63 68 20 61 73 20 77 61 73 20 6d or such as was m
1404e 61 64 65 20 62 79 20 74 68 65 20 43 72 65 61 74 ade by the Creat
1404f 65 54 65 6d 70 6f 72 61 72 79 43 75 72 73 6f 72 eTemporaryCursor
14050 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 ().** function a
14051 62 6f 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f bove..*/.SQLITE_
14052 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
14053 69 74 65 33 42 74 72 65 65 52 65 6c 65 61 73 65 ite3BtreeRelease
14054 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 TempCursor(BtCur
14055 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e sor *pCur){. in
14056 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 63 t i;. assert( c
14057 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
14058 70 43 75 72 29 20 29 3b 0a 20 20 66 6f 72 28 69 pCur) );. for(i
14059 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 =0; i<=pCur->iPa
1405a 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 ge; i++){. sq
1405b 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
1405c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 2d pCur->apPage[i]-
1405d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d >pDbPage);. }.}
1405e 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
1405f 65 20 74 68 65 20 42 74 43 75 72 73 6f 72 2a 20 e the BtCursor*
14060 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 given in the arg
14061 75 6d 65 6e 74 20 68 61 73 20 61 20 76 61 6c 69 ument has a vali
14062 64 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e d.** BtCursor.in
14063 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 fo structure. I
14064 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 f it is not alre
14065 61 64 79 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a ady valid, call.
14066 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 ** sqlite3BtreeP
14067 61 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 arseCell() to fi
14068 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 ll it in..**.**
14069 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 BtCursor.info is
1406a 20 61 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 a cache of the
1406b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 information in t
1406c 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e he current cell.
1406d 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 20 63 .** Using this c
1406e 61 63 68 65 20 72 65 64 75 63 65 73 20 74 68 65 ache reduces the
1406f 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 number of calls
14070 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
14071 50 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a ParseCell()..**.
14072 2a 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 ** 2007-06-25:
14073 54 68 65 72 65 20 69 73 20 61 20 62 75 67 20 69 There is a bug i
14074 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 n some versions
14075 6f 66 20 4d 53 56 43 20 74 68 61 74 20 63 61 75 of MSVC that cau
14076 73 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c se the.** compil
14077 65 72 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e er to crash when
14078 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 getCellInfo() i
14079 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 s implemented as
1407a 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 a macro..** But
1407b 20 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 73 there is a meas
1407c 75 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 64 ureable speed ad
1407d 76 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67 vantage to using
1407e 20 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 the macro on gc
1407f 63 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 c.** (when less
14080 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a compiler optimiz
14081 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 ations like -Os
14082 6f 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20 or -O0 are used
14083 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 and the.** compi
14084 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 ler is not doing
14085 20 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69 6e agressive inlin
14086 69 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73 65 ing.) So we use
14087 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e a real function
14088 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 .** for MSVC and
14089 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 a macro for eve
1408a 72 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 rything else. T
1408b 69 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a icket #2457..*/.
1408c 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
1408d 20 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 static void ass
1408e 65 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 ertCellInfo(BtCu
1408f 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 rsor *pCur){.
14090 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
14091 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 int iPage =
14092 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 pCur->iPage;.
14093 20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 memset(&info, 0
14094 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b , sizeof(info));
14095 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
14096 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d eParseCell(pCur-
14097 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 >apPage[iPage],
14098 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 pCur->aiIdx[iPag
14099 65 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 e], &info);.
1409a 61 73 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 assert( memcmp(&
1409b 69 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 info, &pCur->inf
1409c 6f 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 o, sizeof(info))
1409d 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 ==0 );. }.#else
1409e 0a 20 20 23 64 65 66 69 6e 65 20 61 73 73 65 72 . #define asser
1409f 74 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e tCellInfo(x).#en
140a0 64 69 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f dif.#ifdef _MSC_
140a1 56 45 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 VER. /* Use a r
140a2 65 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 eal function in
140a3 4d 53 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f MSVC to work aro
140a4 75 6e 64 20 62 75 67 73 20 69 6e 20 74 68 61 74 und bugs in that
140a5 20 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 compiler. */.
140a6 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 static void getC
140a7 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 ellInfo(BtCursor
140a8 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28 *pCur){. if(
140a9 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
140aa 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e e==0 ){. in
140ab 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e t iPage = pCur->
140ac 69 50 61 67 65 3b 0a 20 20 20 20 20 20 73 71 6c iPage;. sql
140ad 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
140ae 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ll(pCur->apPage[
140af 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 iPage],pCur->aiI
140b0 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d dx[iPage],&pCur-
140b1 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 >info);. pC
140b2 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 ur->validNKey =
140b3 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
140b4 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e assertCellIn
140b5 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a fo(pCur);. }.
140b6 20 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 }.#else /* if
140b7 6e 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a not _MSC_VER */.
140b8 20 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f /* Use a macro
140b9 20 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f in all other co
140ba 6d 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 mpilers so that
140bb 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 the function is
140bc 69 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 inlined */.#defi
140bd 6e 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 ne getCellInfo(p
140be 43 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 Cur)
140bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140c1 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 \. if
140c2 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 ( pCur->info.nSi
140c3 7a 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 ze==0 ){
140c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140c6 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
140c7 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 int iPage = pCu
140c8 72 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 r->iPage;
140c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140cb 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
140cc 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 sqlite3BtreePa
140cd 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 rseCell(pCur->ap
140ce 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 Page[iPage],pCur
140cf 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 ->aiIdx[iPage],&
140d0 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 pCur->info); \.
140d1 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b pCur->validNK
140d2 65 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 ey = 1;
140d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
140d6 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 }else{
140d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
140db 0a 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 . assertCellI
140dc 6e 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20 nfo(pCur);
140dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140e0 5c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 \. }.#endif /*
140e1 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 2f 2a 0a _MSC_VER */../*.
140e2 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f ** Set *pSize to
140e3 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
140e4 20 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 74 buffer needed t
140e5 6f 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 o hold the value
140e6 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 of.** the key f
140e7 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 65 or the current e
140e8 6e 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 75 ntry. If the cu
140e9 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e rsor is not poin
140ea 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c ting.** to a val
140eb 69 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 id entry, *pSize
140ec 20 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a is set to 0. .*
140ed 2a 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 *.** For a table
140ee 20 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 59 with the INTKEY
140ef 20 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 20 flag set, this
140f0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
140f1 74 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c the key.** itsel
140f2 66 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 f, not the numbe
140f3 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
140f4 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 e key..*/.SQLITE
140f5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
140f6 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
140f7 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
140f8 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 i64 *pSize){.
140f9 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 int rc;.. asser
140fa 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
140fb 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 tex(pCur) );. r
140fc 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f c = restoreCurso
140fd 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b rPosition(pCur);
140fe 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
140ff 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 E_OK ){. asse
14100 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
14101 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ==CURSOR_INVALID
14102 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 || pCur->eState
14103 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
14104 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e ;. if( pCur->
14105 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 eState==CURSOR_I
14106 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 NVALID ){.
14107 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 *pSize = 0;.
14108 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 74 }else{. get
14109 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a CellInfo(pCur);.
1410a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 *pSize = p
1410b 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a Cur->info.nKey;.
1410c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1410d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1410e 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 Set *pSize to th
1410f 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
14110 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68 65 s of data in the
14111 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 entry the.** cu
14112 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 rsor currently p
14113 6f 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77 61 79 oints to. Alway
14114 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f s return SQLITE_
14115 4f 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 OK..** Failure i
14116 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 s not possible.
14117 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 If the cursor i
14118 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 0a s not currently.
14119 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 ** pointing to a
1411a 6e 20 65 6e 74 72 79 20 28 77 68 69 63 68 20 63 n entry (which c
1411b 61 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 20 65 an happen, for e
1411c 78 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 74 68 xample, if.** th
1411d 65 20 64 61 74 61 62 61 73 65 20 69 73 20 65 6d e database is em
1411e 70 74 79 29 20 74 68 65 6e 20 2a 70 53 69 7a 65 pty) then *pSize
1411f 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f is set to 0..*/
14120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14121 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
14122 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f DataSize(BtCurso
14123 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 r *pCur, u32 *pS
14124 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ize){. int rc;.
14125 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
14126 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
14127 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 ) );. rc = rest
14128 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
14129 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 n(pCur);. if( r
1412a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1412b 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
1412c 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1412d 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 _INVALID || pCur
1412e 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1412f 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 _VALID );. if
14130 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
14131 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 CURSOR_INVALID )
14132 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 70 {. /* Not p
14133 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c ointing at a val
14134 69 64 20 65 6e 74 72 79 20 2d 20 73 65 74 20 2a id entry - set *
14135 70 53 69 7a 65 20 74 6f 20 30 2e 20 2a 2f 0a 20 pSize to 0. */.
14136 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b *pSize = 0;
14137 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
14138 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 getCellInfo(pC
14139 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a ur);. *pSiz
1413a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e e = pCur->info.n
1413b 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Data;. }. }.
1413c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1413d 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 /*.** Given the
1413e 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 page number of a
1413f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 n overflow page
14140 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
14141 28 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 (parameter.** ov
14142 66 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 fl), this functi
14143 6f 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67 on finds the pag
14144 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
14145 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 next page in the
14146 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 .** linked list
14147 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 of overflow pag
14148 65 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c es. If possible,
14149 20 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74 it uses the aut
1414a 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e o-vacuum.** poin
1414b 74 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 ter-map data ins
1414c 74 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20 tead of reading
1414d 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 the content of p
1414e 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 age ovfl to do s
1414f 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 o. .**.** If an
14150 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 error occurs an
14151 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 SQLite error cod
14152 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f e is returned. O
14153 74 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 therwise:.**.**
14154 55 6e 6c 65 73 73 20 70 50 67 6e 6f 4e 65 78 74 Unless pPgnoNext
14155 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 70 61 is NULL, the pa
14156 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
14157 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 0a next overflow .
14158 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c ** page in the l
14159 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 77 72 inked list is wr
1415a 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e itten to *pPgnoN
1415b 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66 ext. If page ovf
1415c 6c 0a 2a 2a 20 69 73 20 74 68 65 20 6c 61 73 74 l.** is the last
1415d 20 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e page in its lin
1415e 6b 65 64 20 6c 69 73 74 2c 20 2a 70 50 67 6e 6f ked list, *pPgno
1415f 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f 20 7a Next is set to z
14160 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70 ero. .**.** If p
14161 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c pPage is not NUL
14162 4c 2c 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 L, *ppPage is se
14163 74 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 65 t to the MemPage
14164 2a 20 68 61 6e 64 6c 65 0a 2a 2a 20 66 6f 72 20 * handle.** for
14165 70 61 67 65 20 6f 76 66 6c 2e 20 54 68 65 20 75 page ovfl. The u
14166 6e 64 65 72 6c 79 69 6e 67 20 70 61 67 65 72 20 nderlying pager
14167 70 61 67 65 20 6d 61 79 20 68 61 76 65 20 62 65 page may have be
14168 65 6e 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 en requested.**
14169 77 69 74 68 20 74 68 65 20 6e 6f 43 6f 6e 74 65 with the noConte
1416a 6e 74 20 66 6c 61 67 20 73 65 74 2c 20 73 6f 20 nt flag set, so
1416b 74 68 65 20 70 61 67 65 20 64 61 74 61 20 61 63 the page data ac
1416c 63 65 73 73 61 62 6c 65 20 76 69 61 0a 2a 2a 20 cessable via.**
1416d 74 68 69 73 20 68 61 6e 64 6c 65 20 6d 61 79 20 this handle may
1416e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 0a not be trusted..
1416f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
14170 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 tOverflowPage(.
14171 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
14172 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 . Pgno ovfl,
14173 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14174 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 /* Overflow page
14175 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
14176 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 ppPage,
14177 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 /* OUT: MemPa
14178 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 ge handle */. P
14179 67 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 gno *pPgnoNext
1417a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1417b 55 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f UT: Next overflo
1417c 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f w page number */
1417d 0a 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 .){. Pgno next
1417e 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a = 0;. int rc;..
1417f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14180 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
14181 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a ->mutex) );. /*
14182 20 4f 6e 65 20 6f 66 20 74 68 65 73 65 20 6d 75 One of these mu
14183 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 st not be NULL.
14184 4f 74 68 65 72 77 69 73 65 2c 20 77 68 79 20 63 Otherwise, why c
14185 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
14186 6e 3f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 70 n? */. assert(p
14187 70 50 61 67 65 20 7c 7c 20 70 50 67 6e 6f 4e 65 pPage || pPgnoNe
14188 78 74 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 50 xt);.. /* If pP
14189 67 6e 6f 4e 65 78 74 20 69 73 20 4e 55 4c 4c 2c gnoNext is NULL,
1418a 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
1418b 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c ion is being cal
1418c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 0a 20 20 led to obtain.
1418d 2a 2a 20 61 20 4d 65 6d 50 61 67 65 2a 20 72 65 ** a MemPage* re
1418e 66 65 72 65 6e 63 65 20 6f 6e 6c 79 2e 20 4e 6f ference only. No
1418f 20 70 61 67 65 2d 64 61 74 61 20 69 73 20 72 65 page-data is re
14190 71 75 69 72 65 64 20 69 6e 20 74 68 69 73 20 63 quired in this c
14191 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ase.. */. if(
14192 21 70 50 67 6e 6f 4e 65 78 74 20 29 7b 0a 20 20 !pPgnoNext ){.
14193 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
14194 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 BtreeGetPage(pBt
14195 2c 20 6f 76 66 6c 2c 20 70 70 50 61 67 65 2c 20 , ovfl, ppPage,
14196 31 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 1);. }..#ifndef
14197 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
14198 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 54 72 79 OVACUUM. /* Try
14199 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 65 78 to find the nex
1419a 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 t page in the ov
1419b 65 72 66 6c 6f 77 20 6c 69 73 74 20 75 73 69 6e erflow list usin
1419c 67 20 74 68 65 0a 20 20 2a 2a 20 61 75 74 6f 76 g the. ** autov
1419d 61 63 75 75 6d 20 70 6f 69 6e 74 65 72 2d 6d 61 acuum pointer-ma
1419e 70 20 70 61 67 65 73 2e 20 47 75 65 73 73 20 74 p pages. Guess t
1419f 68 61 74 20 74 68 65 20 6e 65 78 74 20 70 61 67 hat the next pag
141a0 65 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 20 6f e in . ** the o
141a1 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 69 73 20 verflow list is
141a2 70 61 67 65 20 6e 75 6d 62 65 72 20 28 6f 76 66 page number (ovf
141a3 6c 2b 31 29 2e 20 49 66 20 74 68 61 74 20 67 75 l+1). If that gu
141a4 65 73 73 20 74 75 72 6e 73 20 0a 20 20 2a 2a 20 ess turns . **
141a5 6f 75 74 20 74 6f 20 62 65 20 77 72 6f 6e 67 2c out to be wrong,
141a6 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 6c 6f fall back to lo
141a7 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 20 6f ading the data o
141a8 66 20 70 61 67 65 20 0a 20 20 2a 2a 20 6e 75 6d f page . ** num
141a9 62 65 72 20 6f 76 66 6c 20 74 6f 20 64 65 74 65 ber ovfl to dete
141aa 72 6d 69 6e 65 20 74 68 65 20 6e 65 78 74 20 70 rmine the next p
141ab 61 67 65 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f age number.. */
141ac 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f . if( pBt->auto
141ad 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 Vacuum ){. Pg
141ae 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 50 67 6e no pgno;. Pgn
141af 6f 20 69 47 75 65 73 73 20 3d 20 6f 76 66 6c 2b o iGuess = ovfl+
141b0 31 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 1;. u8 eType;
141b1 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 .. while( PTR
141b2 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
141b3 69 47 75 65 73 73 29 20 7c 7c 20 69 47 75 65 73 iGuess) || iGues
141b4 73 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f s==PENDING_BYTE_
141b5 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
141b6 20 20 20 69 47 75 65 73 73 2b 2b 3b 0a 20 20 20 iGuess++;.
141b7 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 47 75 65 }.. if( iGue
141b8 73 73 3c 3d 70 61 67 65 72 50 61 67 65 63 6f 75 ss<=pagerPagecou
141b9 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 nt(pBt) ){.
141ba 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 rc = ptrmapGet(
141bb 70 42 74 2c 20 69 47 75 65 73 73 2c 20 26 65 54 pBt, iGuess, &eT
141bc 79 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 20 ype, &pgno);.
141bd 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
141be 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
141bf 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
141c0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 }. if( eTy
141c1 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 pe==PTRMAP_OVERF
141c2 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d 6f 76 LOW2 && pgno==ov
141c3 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 65 fl ){. ne
141c4 78 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 20 20 xt = iGuess;.
141c5 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 }. }. }.#
141c6 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 65 78 endif.. if( nex
141c7 74 3d 3d 30 20 7c 7c 20 70 70 50 61 67 65 20 29 t==0 || ppPage )
141c8 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 {. MemPage *p
141c9 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 72 Page = 0;.. r
141ca 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
141cb 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 GetPage(pBt, ovf
141cc 6c 2c 20 26 70 50 61 67 65 2c 20 6e 65 78 74 21 l, &pPage, next!
141cd 3d 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 =0);. assert(
141ce 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
141cf 20 70 50 61 67 65 3d 3d 30 29 3b 0a 20 20 20 20 pPage==0);.
141d0 69 66 28 20 6e 65 78 74 3d 3d 30 20 26 26 20 72 if( next==0 && r
141d1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
141d2 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 next = get
141d3 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 4byte(pPage->aDa
141d4 74 61 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ta);. }..
141d5 69 66 28 20 70 70 50 61 67 65 20 29 7b 0a 20 20 if( ppPage ){.
141d6 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 *ppPage = pP
141d7 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a age;. }else{.
141d8 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
141d9 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a e(pPage);. }.
141da 20 20 7d 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 }. *pPgnoNext
141db 20 3d 20 6e 65 78 74 3b 0a 0a 20 20 72 65 74 75 = next;.. retu
141dc 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
141dd 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 61 Copy data from a
141de 20 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 67 buffer to a pag
141df 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 67 e, or from a pag
141e0 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a e to a buffer..*
141e1 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 73 *.** pPayload is
141e2 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 61 a pointer to da
141e3 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 74 ta stored on dat
141e4 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 abase page pDbPa
141e5 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 ge..** If argume
141e6 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 2c nt eOp is false,
141e7 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 65 then nByte byte
141e8 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f s of data are co
141e9 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 pied.** from pPa
141ea 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 66 yload to the buf
141eb 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 62 fer pointed at b
141ec 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 69 y pBuf. If eOp i
141ed 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 s true,.** then
141ee 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
141ef 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e e() is called on
141f0 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 79 pDbPage and nBy
141f1 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 te bytes.** of d
141f2 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 ata are copied f
141f3 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 rom the buffer p
141f4 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e Buf to pPayload.
141f5 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
141f6 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
141f7 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 69 success, otherwi
141f8 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 se an error code
141f9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
141fa 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 76 copyPayload(. v
141fb 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20 oid *pPayload,
141fc 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
141fd 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61 ter to page data
141fe 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 */. void *pBuf
141ff 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
14200 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 /* Pointer to bu
14201 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 ffer */. int nB
14202 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 yte,
14203 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
14204 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a bytes to copy *
14205 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 20 /. int eOp,
14206 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14207 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20 0 -> copy from
14208 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20 page, 1 -> copy
14209 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 to page */. DbP
1420a 61 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 20 age *pDbPage
1420b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 /* Page c
1420c 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f ontaining pPaylo
1420d 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65 ad */.){. if( e
1420e 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 Op ){. /* Cop
1420f 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66 y data from buff
14210 65 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 72 er to page (a wr
14211 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a ite operation) *
14212 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 /. int rc = s
14213 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
14214 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 (pDbPage);. i
14215 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
14216 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
14217 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d rc;. }. m
14218 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 emcpy(pPayload,
14219 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20 pBuf, nByte);.
1421a 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f }else{. /* Co
1421b 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67 py data from pag
1421c 65 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 72 e to buffer (a r
1421d 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a ead operation) *
1421e 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75 /. memcpy(pBu
1421f 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 f, pPayload, nBy
14220 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 te);. }. retur
14221 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
14222 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
14223 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 ion is used to r
14224 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 65 ead or overwrite
14225 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 payload informa
14226 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 tion.** for the
14227 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 70 entry that the p
14228 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f Cur cursor is po
14229 69 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74 68 inting to. If th
1422a 65 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74 e eOp.** paramet
1422b 65 72 20 69 73 20 30 2c 20 74 68 69 73 20 69 73 er is 0, this is
1422c 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f a read operatio
1422d 6e 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 69 n (data copied i
1422e 6e 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 nto.** buffer pB
1422f 75 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e 6f uf). If it is no
14230 6e 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65 20 n-zero, a write
14231 28 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f (data copied fro
14232 6d 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 m.** buffer pBuf
14233 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c )..**.** A total
14234 20 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73 20 of "amt" bytes
14235 61 72 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 are read or writ
14236 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 ten beginning at
14237 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 "offset"..** Da
14238 74 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f 72 ta is read to or
14239 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 from the buffer
1423a 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 pBuf..**.** Thi
1423b 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e s routine does n
1423c 6f 74 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e ot make a distin
1423d 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 6b 65 ction between ke
1423e 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 49 y and data..** I
1423f 74 20 6a 75 73 74 20 72 65 61 64 73 20 6f 72 20 t just reads or
14240 77 72 69 74 65 73 20 62 79 74 65 73 20 66 72 6f writes bytes fro
14241 6d 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 m the payload ar
14242 65 61 2e 20 20 44 61 74 61 20 6d 69 67 68 74 20 ea. Data might
14243 0a 2a 2a 20 61 70 70 65 61 72 20 6f 6e 20 74 68 .** appear on th
14244 65 20 6d 61 69 6e 20 70 61 67 65 20 6f 72 20 62 e main page or b
14245 65 20 73 63 61 74 74 65 72 65 64 20 6f 75 74 20 e scattered out
14246 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 on multiple over
14247 66 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 73 2e 0a flow .** pages..
14248 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74 43 **.** If the BtC
14249 75 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62 ursor.isIncrblob
1424a 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 Handle flag is s
1424b 65 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 et, and the curr
1424c 65 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e ent.** cursor en
1424d 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 20 try uses one or
1424e 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 more overflow pa
1424f 67 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ges, this functi
14250 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20 on.** allocates
14251 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c 61 space for and la
14252 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20 74 zily popluates t
14253 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
14254 2d 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65 20 -list .** cache
14255 61 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 2e array (BtCursor.
14256 61 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62 73 aOverflow). Subs
14257 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 65 equent calls use
14258 20 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20 74 this.** cache t
14259 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 o make seeking t
1425a 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6f o the supplied o
1425b 66 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69 63 ffset more effic
1425c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 ient..**.** Once
1425d 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
1425e 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 e-list cache has
1425f 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c been allocated,
14260 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e it may be.** in
14261 76 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f 6d validated if som
14262 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 77 e other cursor w
14263 72 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 6d rites to the sam
14264 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a e table, or if.*
14265 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 * the cursor is
14266 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 65 moved to a diffe
14267 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 69 rent row. Additi
14268 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d onally, in auto-
14269 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 vacuum.** mode,
1426a 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 the following ev
1426b 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 64 ents may invalid
1426c 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 ate an overflow
1426d 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e page-list cache.
1426e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e .**.** * An in
1426f 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
14270 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69 ,.** * A commi
14271 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d t in auto_vacuum
14272 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a ="full" mode,.**
14273 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 20 * Creating a
14274 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 69 table (may requi
14275 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65 re moving an ove
14276 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a rflow page)..*/.
14277 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 73 static int acces
14278 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 sPayload(. BtCu
14279 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 rsor *pCur,
1427a 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 /* Cursor point
1427b 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 ing to entry to
1427c 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 read from */. u
1427d 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20 20 20 32 offset,
1427e 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 /* Begin rea
1427f 64 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 6e ding this far in
14280 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 to payload */.
14281 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20 20 20 u32 amt,
14282 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 69 /* Read thi
14283 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a s many bytes */.
14284 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
14285 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 20 *pBuf, /* Write
14286 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 74 the bytes into t
14287 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a 20 his buffer */ .
14288 20 69 6e 74 20 73 6b 69 70 4b 65 79 2c 20 20 20 int skipKey,
14289 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 /* offset
1428a 62 65 67 69 6e 73 20 61 74 20 64 61 74 61 20 69 begins at data i
1428b 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a f this is true *
1428c 2f 0a 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20 /. int eOp
1428d 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f /* zero
1428e 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 to read. non-ze
1428f 72 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a ro to write. */.
14290 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 ){. unsigned ch
14291 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 ar *aPayload;.
14292 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
14293 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a OK;. u32 nKey;.
14294 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a int iIdx = 0;.
14295 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
14296 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
14297 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a pCur->iPage]; /*
14298 20 42 74 72 65 65 20 70 61 67 65 20 6f 66 20 63 Btree page of c
14299 75 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a urrent entry */.
1429a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
1429b 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 = pCur->pBt;
1429c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1429d 20 42 74 72 65 65 20 74 68 69 73 20 63 75 72 73 Btree this curs
1429e 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f or belongs to */
1429f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
142a0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 e );. assert( p
142a1 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
142a2 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 SOR_VALID );. a
142a3 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 ssert( pCur->aiI
142a4 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c dx[pCur->iPage]<
142a5 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
142a6 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
142a7 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
142a8 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e );.. getCellIn
142a9 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 fo(pCur);. aPay
142aa 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 load = pCur->inf
142ab 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e o.pCell + pCur->
142ac 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 info.nHeader;.
142ad 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 nKey = (pPage->i
142ae 6e 74 4b 65 79 20 3f 20 30 20 3a 20 70 43 75 72 ntKey ? 0 : pCur
142af 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 0a 20 ->info.nKey);..
142b0 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a if( skipKey ){.
142b1 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 6e 4b offset += nK
142b2 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f 66 ey;. }. if( of
142b3 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79 2b fset+amt > nKey+
142b4 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 pCur->info.nData
142b5 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c 6f 61 . || &aPayloa
142b6 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f d[pCur->info.nLo
142b7 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d 3e 61 cal] > &pPage->a
142b8 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 Data[pBt->usable
142b9 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20 20 2f Size]. ){. /
142ba 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61 64 * Trying to read
142bb 20 6f 72 20 77 72 69 74 65 20 70 61 73 74 20 74 or write past t
142bc 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61 he end of the da
142bd 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20 2a ta is an error *
142be 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c /. return SQL
142bf 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
142c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 ;. }.. /* Chec
142c1 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20 62 k if data must b
142c2 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 74 e read/written t
142c3 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 o/from the btree
142c4 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a 2f page itself. */
142c5 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 43 . if( offset<pC
142c6 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 ur->info.nLocal
142c7 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 61 ){. int a = a
142c8 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f 66 mt;. if( a+of
142c9 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e fset>pCur->info.
142ca 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 nLocal ){.
142cb 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e a = pCur->info.n
142cc 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b 0a Local - offset;.
142cd 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 }. rc = c
142ce 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 opyPayload(&aPay
142cf 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70 42 load[offset], pB
142d0 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61 67 uf, a, eOp, pPag
142d1 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
142d2 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 offset = 0;.
142d3 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 pBuf += a;.
142d4 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73 amt -= a;. }els
142d5 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d 3d e{. offset -=
142d6 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 pCur->info.nLoc
142d7 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 al;. }.. if( r
142d8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
142d9 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 6f 6e amt>0 ){. con
142da 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a 65 20 st u32 ovflSize
142db 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
142dc 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 65 73 e - 4; /* Bytes
142dd 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f 76 66 content per ovf
142de 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 67 l page */. Pg
142df 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a 20 20 no nextPage;..
142e0 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 nextPage = get
142e1 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 64 5b 4byte(&aPayload[
142e2 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
142e3 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 l]);..#ifndef SQ
142e4 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
142e5 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 OB. /* If the
142e6 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c isIncrblobHandl
142e7 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e e flag is set an
142e8 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 61 d the BtCursor.a
142e9 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 20 2a Overflow[]. *
142ea 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 * has not been a
142eb 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63 61 llocated, alloca
142ec 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 61 te it now. The a
142ed 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 61 74 rray is sized at
142ee 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e 74 72 . ** one entr
142ef 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72 66 y for each overf
142f0 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 low page in the
142f1 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 overflow chain.
142f2 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 The. ** page
142f3 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 69 number of the fi
142f4 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 rst overflow pag
142f5 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 e is stored in a
142f6 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 20 20 Overflow[0],.
142f7 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c 75 65 ** etc. A value
142f8 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f 76 of 0 in the aOv
142f9 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 6d erflow[] array m
142fa 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 6b 6e eans "not yet kn
142fb 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 68 65 own". ** (the
142fc 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c 79 cache is lazily
142fd 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20 20 populated)..
142fe 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 */. if( pCur
142ff 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 ->isIncrblobHand
14300 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 le && !pCur->aOv
14301 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 erflow ){.
14302 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 43 75 int nOvfl = (pCu
14303 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 r->info.nPayload
14304 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 -pCur->info.nLoc
14305 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 2f 6f al+ovflSize-1)/o
14306 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 vflSize;. p
14307 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d Cur->aOverflow =
14308 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 65 33 (Pgno *)sqlite3
14309 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f MallocZero(sizeo
1430a 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0a f(Pgno)*nOvfl);.
1430b 20 20 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20 if( nOvfl
1430c 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 && !pCur->aOverf
1430d 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 low ){. r
1430e 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
1430f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
14310 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f . /* If the o
14311 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
14312 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e t cache has been
14313 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 allocated and t
14314 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 he. ** entry
14315 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 72 65 for the first re
14316 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f 77 20 quired overflow
14317 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c 20 73 page is valid, s
14318 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 kip. ** direc
14319 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a tly to it.. *
1431a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e /. if( pCur->
1431b 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75 aOverflow && pCu
1431c 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 r->aOverflow[off
1431d 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b set/ovflSize] ){
1431e 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20 28 6f . iIdx = (o
1431f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b ffset/ovflSize);
14320 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 . nextPage
14321 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f = pCur->aOverflo
14322 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f w[iIdx];. o
14323 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65 74 25 ffset = (offset%
14324 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d ovflSize);. }
14325 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66 6f 72 .#endif.. for
14326 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f ( ; rc==SQLITE_O
14327 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20 6e 65 K && amt>0 && ne
14328 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b 29 7b xtPage; iIdx++){
14329 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1432a 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
1432b 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71 75 69 /* If requi
1432c 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 red, populate th
1432d 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d e overflow page-
1432e 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f 0a 20 list cache. */.
1432f 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 if( pCur->a
14330 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
14331 20 20 20 20 61 73 73 65 72 74 28 21 70 43 75 72 assert(!pCur
14332 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
14333 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72 ] || pCur->aOver
14334 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 flow[iIdx]==next
14335 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70 Page);. p
14336 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
14337 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b Idx] = nextPage;
14338 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
14339 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66 73 65 . if( offse
1433a 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 t>=ovflSize ){.
1433b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e /* The on
1433c 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72 65 61 ly reason to rea
1433d 64 20 74 68 69 73 20 70 61 67 65 20 69 73 20 74 d this page is t
1433e 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70 61 67 o obtain the pag
1433f 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d e. ** num
14340 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 78 74 ber for the next
14341 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 page in the ove
14342 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 rflow chain. The
14343 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a page. **
14344 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 65 71 data is not req
14345 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 74 20 uired. So first
14346 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 try to lookup th
14347 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20 e overflow.
14348 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 74 20 ** page-list
14349 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c 20 74 cache, if any, t
1434a 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f hen fall back to
1434b 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c 6f 77 the getOverflow
1434c 50 61 67 65 28 29 0a 20 20 20 20 20 20 20 20 2a Page(). *
1434d 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 * function..
1434e 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 */.#ifndef S
1434f 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
14350 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66 28 20 LOB. if(
14351 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 pCur->aOverflow
14352 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c && pCur->aOverfl
14353 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a 20 20 ow[iIdx+1] ){.
14354 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 nextPage
14355 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c = pCur->aOverfl
14356 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 20 20 ow[iIdx+1];.
14357 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65 6e 64 } else .#end
14358 69 66 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 if. rc
14359 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 = getOverflowPag
1435a 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67 65 2c e(pBt, nextPage,
1435b 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29 3b 0a 0, &nextPage);.
1435c 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2d offset -
1435d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 = ovflSize;.
1435e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1435f 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65 61 64 /* Need to read
14360 20 74 68 69 73 20 70 61 67 65 20 70 72 6f 70 65 this page prope
14361 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69 6e 73 rly. It contains
14362 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20 20 20 some of the.
14363 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 ** range of
14364 20 64 61 74 61 20 74 68 61 74 20 69 73 20 62 65 data that is be
14365 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d 3d 30 ing read (eOp==0
14366 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28 65 4f ) or written (eO
14367 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20 20 2a p!=0).. *
14368 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 /. DbPage
14369 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20 *pDbPage;.
1436a 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a int a = amt;.
1436b 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1436c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 ite3PagerGet(pBt
1436d 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74 50 61 ->pPager, nextPa
1436e 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 ge, &pDbPage);.
1436f 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
14370 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
14371 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d aPayload =
14372 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
14373 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 Data(pDbPage);.
14374 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 nextPag
14375 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 61 e = get4byte(aPa
14376 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 20 yload);.
14377 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73 65 74 if( a + offset
14378 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 > ovflSize ){.
14379 20 20 20 20 20 20 20 20 20 20 20 61 20 3d 20 6f a = o
1437a 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 65 74 vflSize - offset
1437b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1437c 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f 70 rc = cop
1437d 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f yPayload(&aPaylo
1437e 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42 ad[offset+4], pB
1437f 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 62 50 uf, a, eOp, pDbP
14380 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
14381 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
14382 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 f(pDbPage);.
14383 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 offset = 0
14384 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d 74 20 ;. amt
14385 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 20 20 -= a;.
14386 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 20 pBuf += a;.
14387 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
14388 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 }. }.. if( rc
14389 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 ==SQLITE_OK && a
1438a 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 mt>0 ){. retu
1438b 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
1438c 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 T_BKPT;. }. re
1438d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1438e 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 * Read part of t
1438f 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 65 he key associate
14390 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 d with cursor pC
14391 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 ur. Exactly.**
14392 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c "amt" bytes will
14393 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69 be transfered i
14394 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 nto pBuf[]. The
14395 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 transfer.** beg
14396 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e ins at "offset".
14397 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
14398 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
14399 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 ss or an error c
1439a 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 ode if anything
1439b 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 goes.** wrong.
1439c 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 An error is retu
1439d 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b rned if "offset+
1439e 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 amt" is larger t
1439f 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c han.** the avail
143a0 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f able payload..*/
143a1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
143a2 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
143a3 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 Key(BtCursor *pC
143a4 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 ur, u32 offset,
143a5 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 u32 amt, void *p
143a6 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Buf){. int rc;.
143a7 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
143a8 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
143a9 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 ) );. rc = rest
143aa 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f oreCursorPositio
143ab 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 n(pCur);. if( r
143ac 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
143ad 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
143ae 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
143af 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 61 73 _VALID );. as
143b0 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 sert( pCur->iPag
143b1 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 e>=0 && pCur->ap
143b2 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
143b3 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 ] );. if( pCu
143b4 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
143b5 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 65 tKey ){. re
143b6 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
143b7 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
143b8 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
143b9 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
143ba 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 age]<pCur->apPag
143bb 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
143bc 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 nCell );. rc
143bd 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 = accessPayload(
143be 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d pCur, offset, am
143bf 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 t, (unsigned cha
143c0 72 2a 29 70 42 75 66 2c 20 30 2c 20 30 29 3b 0a r*)pBuf, 0, 0);.
143c1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
143c2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 .}../*.** Read p
143c3 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 art of the data
143c4 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
143c5 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78 cursor pCur. Ex
143c6 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62 actly.** "amt" b
143c7 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61 ytes will be tra
143c8 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75 nsfered into pBu
143c9 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66 f[]. The transf
143ca 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 er.** begins at
143cb 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 "offset"..**.**
143cc 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
143cd 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 on success or a
143ce 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 n error code if
143cf 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a anything goes.**
143d0 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f wrong. An erro
143d1 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 r is returned if
143d2 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73 "offset+amt" is
143d3 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20 larger than.**
143d4 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 70 61 the available pa
143d5 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 yload..*/.SQLITE
143d6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
143d7 69 74 65 33 42 74 72 65 65 44 61 74 61 28 42 74 ite3BtreeData(Bt
143d8 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 Cursor *pCur, u3
143d9 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 2 offset, u32 am
143da 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a t, void *pBuf){.
143db 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 6e 64 int rc;..#ifnd
143dc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
143dd 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 20 70 NCRBLOB. if ( p
143de 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
143df 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 SOR_INVALID ){.
143e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
143e1 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 6e 64 _ABORT;. }.#end
143e2 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 if.. assert( cu
143e3 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
143e4 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 Cur) );. rc = r
143e5 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
143e6 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 tion(pCur);. if
143e7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
143e8 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
143e9 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
143ea 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 SOR_VALID );.
143eb 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 assert( pCur->i
143ec 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d Page>=0 && pCur-
143ed 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
143ee 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 73 65 age] );. asse
143ef 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
143f0 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 pCur->iPage]<pCu
143f1 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
143f2 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b iPage]->nCell );
143f3 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 . rc = access
143f4 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 Payload(pCur, of
143f5 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 66 2c fset, amt, pBuf,
143f6 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 1, 0);. }. re
143f7 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
143f8 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
143f9 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e er to payload in
143fa 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 formation from t
143fb 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 he entry that th
143fc 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f e .** pCur curso
143fd 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
143fe 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 . The pointer i
143ff 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 s to the beginni
14400 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 ng of.** the key
14401 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61 if skipKey==0 a
14402 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 nd it points to
14403 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
14404 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70 data if.** skip
14405 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d Key==1. The num
14406 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
14407 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61 available key/da
14408 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a ta is written.**
14409 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66 into *pAmt. If
1440a 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 *pAmt==0, then
1440b 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
1440c 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a ed will not be.*
1440d 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 * a valid pointe
1440e 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f r..**.** This ro
1440f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69 utine is an opti
14410 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 mization. It is
14411 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20 common for the
14412 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e entire key.** an
14413 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e d data to fit on
14414 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 the local page
14415 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f and for there to
14416 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a be no overflow.
14417 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 ** pages. When
14418 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 that is so, this
14419 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 routine can be
1441a 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 used to access t
1441b 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 he.** key and da
1441c 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e ta without makin
1441d 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 g a copy. If th
1441e 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 e key and/or dat
1441f 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f a spills.** onto
14420 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c overflow pages,
14421 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c then accessPayl
14422 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 73 oad() must be us
14423 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 79 ed to reassembly
14424 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61 .** the key/data
14425 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74 and copy it int
14426 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 o a preallocated
14427 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 buffer..**.** T
14428 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 he pointer retur
14429 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 ned by this rout
1442a 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74 ine looks direct
1442b 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 ly into the cach
1442c 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68 ed.** page of th
1442d 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 e database. The
1442e 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e data might chan
1442f 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e ge or move the n
14430 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 ext time.** any
14431 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73 btree routine is
14432 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 called..*/.stat
14433 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 ic const unsigne
14434 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 79 d char *fetchPay
14435 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 load(. BtCursor
14436 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 *pCur, /*
14437 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 Cursor pointing
14438 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 to entry to read
14439 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a from */. int *
1443a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 pAmt,
1443b 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d /* Write the num
1443c 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 ber of available
1443d 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20 bytes here */.
1443e 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20 int skipKey
1443f 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65 /* read be
14440 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20 ginning at data
14441 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20 if this is true
14442 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 */.){. unsigned
14443 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b char *aPayload;
14444 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
14445 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 e;. u32 nKey;.
14446 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 u32 nLocal;..
14447 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 assert( pCur!=0
14448 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d && pCur->iPage>=
14449 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 0 && pCur->apPag
1444a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b e[pCur->iPage]);
1444b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
1444c 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
1444d 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
1444e 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
1444f 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 tex(pCur) );. p
14450 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
14451 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
14452 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
14453 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
14454 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c age]<pPage->nCel
14455 6c 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e l );. getCellIn
14456 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 fo(pCur);. aPay
14457 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 load = pCur->inf
14458 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c o.pCell;. aPayl
14459 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 oad += pCur->inf
1445a 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 66 28 o.nHeader;. if(
1445b 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 pPage->intKey )
1445c 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a {. nKey = 0;.
1445d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 }else{. nKe
1445e 79 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e y = pCur->info.n
1445f 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 Key;. }. if( s
14460 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 50 kipKey ){. aP
14461 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a ayload += nKey;.
14462 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 nLocal = pCu
14463 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d r->info.nLocal -
14464 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a nKey;. }else{.
14465 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 nLocal = pCu
14466 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a r->info.nLocal;.
14467 20 20 20 20 69 66 28 20 6e 4c 6f 63 61 6c 3e 6e if( nLocal>n
14468 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 4c 6f Key ){. nLo
14469 63 61 6c 20 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 cal = nKey;.
1446a 7d 0a 20 20 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 }. }. *pAmt =
1446b 6e 4c 6f 63 61 6c 3b 0a 20 20 72 65 74 75 72 6e nLocal;. return
1446c 20 61 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f aPayload;.}.../
1446d 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65 6e 74 *.** For the ent
1446e 72 79 20 74 68 61 74 20 63 75 72 73 6f 72 20 70 ry that cursor p
1446f 43 75 72 20 69 73 20 70 6f 69 6e 74 20 74 6f 2c Cur is point to,
14470 20 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 return as.** ma
14471 6e 79 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 ny bytes of the
14472 6b 65 79 20 6f 72 20 64 61 74 61 20 61 73 20 61 key or data as a
14473 72 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 re available on
14474 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 the local.** b-t
14475 72 65 65 20 70 61 67 65 2e 20 20 57 72 69 74 65 ree page. Write
14476 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 the number of a
14477 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20 69 vailable bytes i
14478 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a nto *pAmt..**.**
14479 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 The pointer ret
1447a 75 72 6e 65 64 20 69 73 20 65 70 68 65 6d 65 72 urned is ephemer
1447b 61 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64 61 74 al. The key/dat
1447c 61 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 a may move.** or
1447d 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 6f 6e be destroyed on
1447e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 the next call t
1447f 6f 20 61 6e 79 20 42 74 72 65 65 20 72 6f 75 74 o any Btree rout
14480 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e ine,.** includin
14481 67 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 g calls from oth
14482 65 72 20 74 68 72 65 61 64 73 20 61 67 61 69 6e er threads again
14483 73 74 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 st the same cach
14484 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6d e..** Hence, a m
14485 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68 utex on the BtSh
14486 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65 20 68 ared should be h
14487 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c eld prior to cal
14488 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 ling.** this rou
14489 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 tine..**.** Thes
1448a 65 20 72 6f 75 74 69 6e 65 73 20 69 73 20 75 73 e routines is us
1448b 65 64 20 74 6f 20 67 65 74 20 71 75 69 63 6b 20 ed to get quick
1448c 61 63 63 65 73 73 20 74 6f 20 6b 65 79 20 61 6e access to key an
1448d 64 20 64 61 74 61 0a 2a 2a 20 69 6e 20 74 68 65 d data.** in the
1448e 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 common case whe
1448f 72 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 re no overflow p
14490 61 67 65 73 20 61 72 65 20 75 73 65 64 2e 0a 2a ages are used..*
14491 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14492 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
14493 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 ite3BtreeKeyFetc
14494 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 h(BtCursor *pCur
14495 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 , int *pAmt){.
14496 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
14497 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
14498 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 ;. if( pCur->eS
14499 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
1449a 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ID ){. return
1449b 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 (const void*)fe
1449c 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c tchPayload(pCur,
1449d 20 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 pAmt, 0);. }.
1449e 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c return 0;.}.SQL
1449f 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
144a0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 t void *sqlite3B
144a1 74 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74 treeDataFetch(Bt
144a2 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
144a3 74 20 2a 70 41 6d 74 29 7b 0a 20 20 61 73 73 65 t *pAmt){. asse
144a4 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
144a5 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
144a6 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
144a7 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
144a8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 63 6f {. return (co
144a9 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 nst void*)fetchP
144aa 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d ayload(pCur, pAm
144ab 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 t, 1);. }. ret
144ac 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn 0;.}.../*.**
144ad 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 Move the cursor
144ae 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 63 down to a new c
144af 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20 hild page. The
144b0 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74 newPgno argument
144b1 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 is the.** page
144b2 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 68 number of the ch
144b3 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 ild page to move
144b4 20 74 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 to..*/.static i
144b5 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 nt moveToChild(B
144b6 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 tCursor *pCur, u
144b7 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 32 newPgno){. i
144b8 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d nt rc;. int i =
144b9 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 pCur->iPage;.
144ba 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67 MemPage *pNewPag
144bb 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e;. BtShared *p
144bc 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a Bt = pCur->pBt;.
144bd 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
144be 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
144bf 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
144c0 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
144c1 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 SOR_VALID );. a
144c2 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 ssert( pCur->iPa
144c3 67 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f ge<BTCURSOR_MAX_
144c4 44 45 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70 DEPTH );. if( p
144c5 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 Cur->iPage>=(BTC
144c6 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d URSOR_MAX_DEPTH-
144c7 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 1) ){. return
144c8 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
144c9 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d BKPT;. }. rc =
144ca 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 getAndInitPage(
144cb 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 pBt, newPgno, &p
144cc 4e 65 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20 NewPage);. if(
144cd 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
144ce 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 pCur->apPage[i
144cf 2b 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a +1] = pNewPage;.
144d0 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b pCur->aiIdx[i+
144d1 31 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 1] = 0;. pCur->
144d2 69 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 iPage++;.. pCur
144d3 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
144d4 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e ;. pCur->validN
144d5 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 Key = 0;. if( p
144d6 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 NewPage->nCell<1
144d7 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
144d8 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
144d9 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e PT;. }. return
144da 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 SQLITE_OK;.}..#
144db 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a ifndef NDEBUG./*
144dc 0a 2a 2a 20 50 61 67 65 20 70 50 61 72 65 6e 74 .** Page pParent
144dd 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 is an internal
144de 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 72 65 65 20 (non-leaf) tree
144df 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 74 page. This funct
144e0 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 72 74 73 20 ion .** asserts
144e1 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 that page number
144e2 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 6c iChild is the l
144e3 65 66 74 2d 63 68 69 6c 64 20 69 66 20 74 68 65 eft-child if the
144e4 20 69 49 64 78 27 74 68 0a 2a 2a 20 63 65 6c 6c iIdx'th.** cell
144e5 20 69 6e 20 70 61 67 65 20 70 50 61 72 65 6e 74 in page pParent
144e6 2e 20 4f 72 2c 20 69 66 20 69 49 64 78 20 69 73 . Or, if iIdx is
144e7 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 6f equal to the to
144e8 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a tal number of.**
144e9 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e cells in pParen
144ea 74 2c 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d t, that page num
144eb 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 68 ber iChild is th
144ec 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 e right-child of
144ed 0a 2a 2a 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f .** the page..*/
144ee 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 .static void ass
144ef 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 4d ertParentIndex(M
144f0 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c emPage *pParent,
144f1 20 69 6e 74 20 69 49 64 78 2c 20 50 67 6e 6f 20 int iIdx, Pgno
144f2 69 43 68 69 6c 64 29 7b 0a 20 20 61 73 73 65 72 iChild){. asser
144f3 74 28 20 69 49 64 78 3c 3d 70 50 61 72 65 6e 74 t( iIdx<=pParent
144f4 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28 ->nCell );. if(
144f5 20 69 49 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e iIdx==pParent->
144f6 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61 73 73 nCell ){. ass
144f7 65 72 74 28 20 67 65 74 34 62 79 74 65 28 26 70 ert( get4byte(&p
144f8 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 Parent->aData[pP
144f9 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 arent->hdrOffset
144fa 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a +8])==iChild );.
144fb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
144fc 65 72 74 28 20 67 65 74 34 62 79 74 65 28 66 69 ert( get4byte(fi
144fd 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 ndCell(pParent,
144fe 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 20 29 iIdx))==iChild )
144ff 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 ;. }.}.#else.#
14500 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 50 61 define assertPa
14501 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c 7a 29 rentIndex(x,y,z)
14502 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 .#endif../*.**
14503 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
14504 75 70 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 up to the parent
14505 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 page..**.** pCu
14506 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f r->idx is set to
14507 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 the cell index
14508 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
14509 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 e pointer.** to
1450a 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65 20 the page we are
1450b 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66 coming from. If
1450c 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 we are coming f
1450d 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68 74 rom the.** right
1450e 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67 65 -most child page
1450f 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 78 20 then pCur->idx
14510 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f is set to one mo
14511 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c re than.** the l
14512 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 65 argest cell inde
14513 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 x..*/.SQLITE_PRI
14514 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
14515 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 3BtreeMoveToPare
14516 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 nt(BtCursor *pCu
14517 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 r){. assert( cu
14518 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
14519 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
1451a 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
1451b 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
1451c 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1451d 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73 73 iPage>0 );. ass
1451e 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
1451f 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 e[pCur->iPage] )
14520 3b 0a 20 20 61 73 73 65 72 74 50 61 72 65 6e 74 ;. assertParent
14521 49 6e 64 65 78 28 0a 20 20 20 20 70 43 75 72 2d Index(. pCur-
14522 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
14523 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75 age-1], . pCu
14524 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
14525 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43 Page-1], . pC
14526 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
14527 3e 69 50 61 67 65 5d 2d 3e 70 67 6e 6f 0a 20 20 >iPage]->pgno.
14528 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 );. releasePage
14529 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 (pCur->apPage[pC
1452a 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 20 70 ur->iPage]);. p
1452b 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 Cur->iPage--;.
1452c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
1452d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 = 0;. pCur->va
1452e 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 7d 0a 0a lidNKey = 0;.}..
1452f 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 /*.** Move the c
14530 75 72 73 6f 72 20 74 6f 20 74 68 65 20 72 6f 6f ursor to the roo
14531 74 20 70 61 67 65 0a 2a 2f 0a 73 74 61 74 69 63 t page.*/.static
14532 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 int moveToRoot(
14533 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
14534 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f 6f . MemPage *pRoo
14535 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
14536 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72 65 65 LITE_OK;. Btree
14537 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 *p = pCur->pBtr
14538 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ee;. BtShared *
14539 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 pBt = p->pBt;..
1453a 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
1453b 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
1453c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 );. assert( CUR
1453d 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20 43 55 SOR_INVALID < CU
1453e 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
1453f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 );. assert( CU
14540 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c 20 43 RSOR_VALID < C
14541 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
14542 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 K );. assert( C
14543 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 3e 20 URSOR_FAULT >
14544 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
14545 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 EK );. if( pCur
14546 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 ->eState>=CURSOR
14547 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 7b 0a _REQUIRESEEK ){.
14548 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 if( pCur->eS
14549 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 tate==CURSOR_FAU
1454a 4c 54 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 LT ){. retu
1454b 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 rn pCur->skip;.
1454c 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1454d 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 BtreeClearCursor
1454e 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 (pCur);. }.. i
1454f 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d f( pCur->iPage>=
14550 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0 ){. int i;.
14551 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d for(i=1; i<=
14552 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b pCur->iPage; i++
14553 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
14554 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 Page(pCur->apPag
14555 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d e[i]);. }. }
14556 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 0a 20 else{. if( .
14557 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d SQLITE_OK!=
14558 28 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 (rc = getAndInit
14559 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e Page(pBt, pCur->
1455a 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d pgnoRoot, &pCur-
1455b 3e 61 70 50 61 67 65 5b 30 5d 29 29 0a 20 20 20 >apPage[0])).
1455c 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e ){. pCur->
1455d 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
1455e 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 72 INVALID;. r
1455f 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
14560 20 20 7d 0a 0a 20 20 70 52 6f 6f 74 20 3d 20 70 }.. pRoot = p
14561 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a Cur->apPage[0];.
14562 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d assert( pRoot-
14563 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e >pgno==pCur->pgn
14564 6f 52 6f 6f 74 20 29 3b 0a 20 20 70 43 75 72 2d oRoot );. pCur-
14565 3e 69 50 61 67 65 20 3d 20 30 3b 0a 20 20 70 43 >iPage = 0;. pC
14566 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 ur->aiIdx[0] = 0
14567 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e ;. pCur->info.n
14568 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 Size = 0;. pCur
14569 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 ->atLast = 0;.
1456a 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
1456b 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f = 0;.. if( pRoo
1456c 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 t->nCell==0 && !
1456d 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 pRoot->leaf ){.
1456e 20 20 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b Pgno subpage;
1456f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52 6f . assert( pRo
14570 6f 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29 3b 0a 20 ot->pgno==1 );.
14571 20 20 20 73 75 62 70 61 67 65 20 3d 20 67 65 74 subpage = get
14572 34 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44 4byte(&pRoot->aD
14573 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 ata[pRoot->hdrOf
14574 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 61 73 fset+8]);. as
14575 73 65 72 74 28 20 73 75 62 70 61 67 65 3e 30 20 sert( subpage>0
14576 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 );. pCur->eSt
14577 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c ate = CURSOR_VAL
14578 49 44 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 ID;. rc = mov
14579 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 73 eToChild(pCur, s
1457a 75 62 70 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 ubpage);. }else
1457b 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 {. pCur->eSta
1457c 74 65 20 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 te = ((pRoot->nC
1457d 65 6c 6c 3e 30 29 3f 43 55 52 53 4f 52 5f 56 41 ell>0)?CURSOR_VA
1457e 4c 49 44 3a 43 55 52 53 4f 52 5f 49 4e 56 41 4c LID:CURSOR_INVAL
1457f 49 44 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ID);. }. retur
14580 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d n rc;.}../*.** M
14581 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 ove the cursor d
14582 6f 77 6e 20 74 6f 20 74 68 65 20 6c 65 66 74 2d own to the left-
14583 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 most leaf entry
14584 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 beneath the.** e
14585 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 69 74 ntry to which it
14586 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f is currently po
14587 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 inting..**.** Th
14588 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 e left-most leaf
14589 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 is the one with
1458a 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 the smallest ke
1458b 79 20 2d 20 74 68 65 20 66 69 72 73 74 0a 2a 2a y - the first.**
1458c 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 in ascending or
1458d 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 der..*/.static i
1458e 6e 74 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 nt moveToLeftmos
1458f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 t(BtCursor *pCur
14590 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a ){. Pgno pgno;.
14591 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
14592 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 E_OK;. MemPage
14593 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 *pPage;.. asser
14594 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
14595 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
14596 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
14597 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
14598 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 D );. while( rc
14599 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 ==SQLITE_OK && !
1459a 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 (pPage = pCur->a
1459b 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
1459c 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 e])->leaf ){.
1459d 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
1459e 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
1459f 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 ]<pPage->nCell )
145a0 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 ;. pgno = get
145a1 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 4byte(findCell(p
145a2 50 61 67 65 2c 20 70 43 75 72 2d 3e 61 69 49 64 Page, pCur->aiId
145a3 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 29 x[pCur->iPage]))
145a4 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 ;. rc = moveT
145a5 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e oChild(pCur, pgn
145a6 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e o);. }. return
145a7 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f rc;.}../*.** Mo
145a8 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f ve the cursor do
145a9 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d wn to the right-
145aa 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 most leaf entry
145ab 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 70 beneath the.** p
145ac 61 67 65 20 74 6f 20 77 68 69 63 68 20 69 74 20 age to which it
145ad 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 is currently poi
145ae 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74 nting. Notice t
145af 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a he difference.**
145b0 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c between moveToL
145b1 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f eftmost() and mo
145b2 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e veToRightmost().
145b3 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 moveToLeftmost
145b4 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 ().** finds the
145b5 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 left-most entry
145b6 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65 6e 74 beneath the *ent
145b7 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f 76 65 ry* whereas move
145b8 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a ToRightmost().**
145b9 20 66 69 6e 64 73 20 74 68 65 20 72 69 67 68 74 finds the right
145ba 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 -most entry bene
145bb 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a ath the *page*..
145bc 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 74 2d **.** The right-
145bd 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20 74 68 most entry is th
145be 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6c e one with the l
145bf 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74 68 65 argest key - the
145c0 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 last.** key in
145c1 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e ascending order.
145c2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
145c3 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 42 oveToRightmost(B
145c4 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
145c5 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 Pgno pgno;. i
145c6 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
145c7 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 K;. MemPage *pP
145c8 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 age;.. assert(
145c9 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
145ca 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
145cb 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
145cc 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
145cd 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 ;. while( rc==S
145ce 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 QLITE_OK && !(pP
145cf 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
145d0 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
145d1 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 ->leaf ){. pg
145d2 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
145d3 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
145d4 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
145d5 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 ;. pCur->aiId
145d6 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d x[pCur->iPage] =
145d7 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 pPage->nCell;.
145d8 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
145d9 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b ild(pCur, pgno);
145da 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
145db 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
145dc 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
145dd 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 ->iPage] = pPage
145de 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 ->nCell-1;. p
145df 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
145e0 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 = 0;. pCur->v
145e1 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 alidNKey = 0;.
145e2 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
145e3 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 ../* Move the cu
145e4 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73 rsor to the firs
145e5 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 t entry in the t
145e6 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 able. Return SQ
145e7 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 LITE_OK.** on su
145e8 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 ccess. Set *pRe
145e9 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 s to 0 if the cu
145ea 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f rsor actually po
145eb 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e ints to somethin
145ec 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 g.** or set *pRe
145ed 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 s to 1 if the ta
145ee 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f ble is empty..*/
145ef 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
145f0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
145f1 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a First(BtCursor *
145f2 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 pCur, int *pRes)
145f3 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 {. int rc;.. a
145f4 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
145f5 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
145f6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
145f7 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
145f8 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e ur->pBtree->db->
145f9 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
145fa 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 moveToRoot(pCur
145fb 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
145fc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
145fd 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
145fe 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 CURSOR_INVALID )
145ff 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
14600 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
14601 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
14602 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 ==0 );. *pR
14603 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 es = 1;. rc
14604 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
14605 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
14606 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
14607 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
14608 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 ->nCell>0 );.
14609 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
1460a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c rc = moveToL
1460b 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 eftmost(pCur);.
1460c 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1460d 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 n rc;.}../* Move
1460e 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 the cursor to t
1460f 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
14610 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 the table. Ret
14611 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a urn SQLITE_OK.**
14612 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 on success. Se
14613 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 t *pRes to 0 if
14614 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 the cursor actua
14615 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f lly points to so
14616 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 mething.** or se
14617 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 t *pRes to 1 if
14618 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 the table is emp
14619 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ty..*/.SQLITE_PR
1461a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1461b 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 3BtreeLast(BtCur
1461c 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a sor *pCur, int *
1461d 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b pRes){. int rc;
1461e 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 . . assert( cur
1461f 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
14620 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
14621 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14622 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 eld(pCur->pBtree
14623 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
14624 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f rc = moveToRoo
14625 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 t(pCur);. if( r
14626 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
14627 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 if( CURSOR_I
14628 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 NVALID==pCur->eS
14629 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 tate ){. as
1462a 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 sert( pCur->apPa
1462b 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
1462c 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 >nCell==0 );.
1462d 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 *pRes = 1;.
1462e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
1462f 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
14630 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
14631 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 D );. *pRes
14632 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d = 0;. rc =
14633 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
14634 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 67 65 (pCur);. ge
14635 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b tCellInfo(pCur);
14636 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 74 4c . pCur->atL
14637 61 73 74 20 3d 20 72 63 3d 3d 53 51 4c 49 54 45 ast = rc==SQLITE
14638 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 _OK;. }. }.
14639 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1463a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
1463b 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 r so that it poi
1463c 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 nts to an entry
1463d 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a near the key .**
1463e 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49 specified by pI
1463f 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e dxKey or intKey.
14640 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 Return a succ
14641 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ess code..**.**
14642 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 For INTKEY table
14643 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 s, the intKey pa
14644 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e rameter is used.
14645 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 pIdxKey .** mu
14646 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 st be NULL. For
14647 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 index tables, p
14648 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 IdxKey is used a
14649 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 nd intKey.** is
1464a 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 ignored..**.** I
1464b 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 f an exact match
1464c 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 is not found, t
1464d 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 hen the cursor i
1464e 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 s always.** left
1464f 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c pointing at a l
14650 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77 eaf page which w
14651 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e ould hold the en
14652 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 try if it.** wer
14653 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 e present. The
14654 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 cursor might poi
14655 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 nt to an entry t
14656 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 hat comes.** bef
14657 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 ore or after the
14658 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 key..**.** The
14659 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d 70 61 72 result of compar
1465a 69 6e 67 20 74 68 65 20 6b 65 79 20 77 69 74 68 ing the key with
1465b 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 77 68 the entry to wh
1465c 69 63 68 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f ich the.** curso
1465d 72 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 r is written to
1465e 2a 70 52 65 73 20 69 66 20 70 52 65 73 21 3d 4e *pRes if pRes!=N
1465f 55 4c 4c 2e 20 20 54 68 65 20 6d 65 61 6e 69 6e ULL. The meanin
14660 67 20 6f 66 0a 2a 2a 20 74 68 69 73 20 76 61 6c g of.** this val
14661 75 65 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 ue is as follows
14662 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 :.**.** *pRe
14663 73 3c 30 20 20 20 20 20 20 54 68 65 20 63 75 72 s<0 The cur
14664 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e sor is left poin
14665 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 ting at an entry
14666 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 that.**
14667 20 20 20 20 20 20 20 20 20 20 69 73 20 73 6d 61 is sma
14668 6c 6c 65 72 20 74 68 61 6e 20 70 4b 65 79 20 6f ller than pKey o
14669 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 r if the table i
1466a 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20 20 20 s empty.**
1466b 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 and
1466c 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 74 68 the cursor is th
1466d 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70 6f 69 erefore left poi
1466e 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a nt to nothing..*
1466f 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3d 3d *.** *pRes==
14670 30 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 0 The cursor
14671 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
14672 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 g at an entry th
14673 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 at.**
14674 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 6d exactly m
14675 61 74 63 68 65 73 20 70 4b 65 79 2e 0a 2a 2a 0a atches pKey..**.
14676 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 ** *pRes>0
14677 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 The cursor i
14678 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
14679 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 at an entry that
1467a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1467b 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 is larger t
1467c 68 61 6e 20 70 4b 65 79 2e 0a 2a 2a 0a 2a 2f 0a han pKey..**.*/.
1467d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1467e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d nt sqlite3BtreeM
1467f 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 ovetoUnpacked(.
14680 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c BtCursor *pCur,
14681 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
14682 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20 6d 6f cursor to be mo
14683 76 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 ved */. Unpacke
14684 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 dRecord *pIdxKey
14685 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e , /* Unpacked in
14686 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 36 34 dex key */. i64
14687 20 69 6e 74 4b 65 79 2c 20 20 20 20 20 20 20 20 intKey,
14688 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 /* The tab
14689 6c 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 le key */. int
1468a 62 69 61 73 52 69 67 68 74 2c 20 20 20 20 20 20 biasRight,
1468b 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c /* If true,
1468c 20 62 69 61 73 20 74 68 65 20 73 65 61 72 63 68 bias the search
1468d 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 to the high end
1468e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 */. int *pRes
1468f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
14690 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 * Write search r
14691 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 esults here */.)
14692 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 {. int rc;.. a
14693 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
14694 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
14695 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
14696 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
14697 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e ur->pBtree->db->
14698 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 mutex) );.. /*
14699 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 If the cursor is
1469a 20 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f already positio
1469b 6e 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74 ned at the point
1469c 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 0a 20 we are trying.
1469d 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f 2c 20 ** to move to,
1469e 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e then just return
1469f 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 without doing a
146a0 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69 66 28 ny work */. if(
146a1 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
146a2 55 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 20 70 URSOR_VALID && p
146a3 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 0a Cur->validNKey .
146a4 20 20 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 && pCur->apPa
146a5 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 0a 20 ge[0]->intKey .
146a6 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 ){. if( pCur
146a7 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 ->info.nKey==int
146a8 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 Key ){. *pR
146a9 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 es = 0;. re
146aa 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
146ab 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 }. if( pC
146ac 75 72 2d 3e 61 74 4c 61 73 74 20 26 26 20 70 43 ur->atLast && pC
146ad 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e ur->info.nKey<in
146ae 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 tKey ){. *p
146af 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 Res = -1;.
146b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
146b1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 ;. }. }.. r
146b2 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 c = moveToRoot(p
146b3 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 Cur);. if( rc )
146b4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
146b5 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
146b6 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
146b7 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 ->iPage] );. as
146b8 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 sert( pCur->apPa
146b9 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
146ba 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 >isInit );. if(
146bb 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
146bc 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b URSOR_INVALID ){
146bd 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b . *pRes = -1;
146be 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
146bf 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
146c0 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 iPage]->nCell==0
146c1 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 );. return S
146c2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
146c3 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
146c4 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 Page[0]->intKey
146c5 7c 7c 20 70 49 64 78 4b 65 79 20 29 3b 0a 20 20 || pIdxKey );.
146c6 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 69 6e 74 for(;;){. int
146c7 20 6c 77 72 2c 20 75 70 72 3b 0a 20 20 20 20 50 lwr, upr;. P
146c8 67 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 gno chldPg;.
146c9 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d MemPage *pPage =
146ca 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
146cb 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
146cc 69 6e 74 20 63 20 3d 20 2d 31 3b 20 20 2f 2a 20 int c = -1; /*
146cd 70 52 65 73 20 72 65 74 75 72 6e 20 69 66 20 74 pRes return if t
146ce 61 62 6c 65 20 69 73 20 65 6d 70 74 79 20 6d 75 able is empty mu
146cf 73 74 20 62 65 20 2d 31 20 2a 2f 0a 20 20 20 20 st be -1 */.
146d0 6c 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72 lwr = 0;. upr
146d1 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d = pPage->nCell-
146d2 31 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 1;. if( !pPag
146d3 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 70 49 64 e->intKey && pId
146d4 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 xKey==0 ){.
146d5 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
146d6 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
146d7 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e goto moveto_fin
146d8 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ish;. }. i
146d9 66 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a f( biasRight ){.
146da 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 pCur->aiId
146db 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d x[pCur->iPage] =
146dc 20 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b upr;. }else{
146dd 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 . pCur->aiI
146de 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
146df 3d 20 28 75 70 72 2b 6c 77 72 29 2f 32 3b 0a 20 = (upr+lwr)/2;.
146e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 77 72 }. if( lwr
146e1 3c 3d 75 70 72 20 29 20 66 6f 72 28 3b 3b 29 7b <=upr ) for(;;){
146e2 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 . void *pCe
146e3 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 69 36 34 llKey;. i64
146e4 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 nCellKey;.
146e5 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d int idx = pCur-
146e6 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
146e7 67 65 5d 3b 0a 20 20 20 20 20 20 70 43 75 72 2d ge];. pCur-
146e8 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b >info.nSize = 0;
146e9 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c . pCur->val
146ea 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 idNKey = 1;.
146eb 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 if( pPage->int
146ec 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 75 Key ){. u
146ed 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 8 *pCell;.
146ee 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 pCell = findCe
146ef 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b ll(pPage, idx) +
146f0 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
146f1 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 69 66 Size;. if
146f2 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 ( pPage->hasData
146f3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 ){. u3
146f4 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 2 dummy;.
146f5 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 pCell += getV
146f6 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 arint32(pCell, d
146f7 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d ummy);. }
146f8 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69 . getVari
146f9 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 nt(pCell, (u64*)
146fa 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 &nCellKey);.
146fb 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 if( nCellKey
146fc 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ==intKey ){.
146fd 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 c = 0;.
146fe 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e }else if( n
146ff 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 CellKey<intKey )
14700 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 {. c =
14701 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 -1;. }els
14702 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 e{. ass
14703 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e ert( nCellKey>in
14704 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 tKey );.
14705 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 c = +1;.
14706 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
14707 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 76 61 . int ava
14708 69 6c 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 ilable;.
14709 70 43 65 6c 6c 4b 65 79 20 3d 20 28 76 6f 69 64 pCellKey = (void
1470a 20 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28 *)fetchPayload(
1470b 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 pCur, &available
1470c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 6e 43 , 0);. nC
1470d 65 6c 6c 4b 65 79 20 3d 20 70 43 75 72 2d 3e 69 ellKey = pCur->i
1470e 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 nfo.nKey;.
1470f 20 20 69 66 28 20 61 76 61 69 6c 61 62 6c 65 3e if( available>
14710 3d 6e 43 65 6c 6c 4b 65 79 20 29 7b 0a 20 20 20 =nCellKey ){.
14711 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 c = sqlit
14712 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 e3VdbeRecordComp
14713 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c 20 70 43 are(nCellKey, pC
14714 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65 79 29 ellKey, pIdxKey)
14715 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
14716 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c . pCell
14717 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Key = sqlite3Mal
14718 6c 6f 63 28 20 6e 43 65 6c 6c 4b 65 79 20 29 3b loc( nCellKey );
14719 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
1471a 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 CellKey==0 ){.
1471b 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
1471c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1471d 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f goto mo
1471e 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 veto_finish;.
1471f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
14720 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
14721 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c treeKey(pCur, 0,
14722 20 6e 43 65 6c 6c 4b 65 79 2c 20 28 76 6f 69 64 nCellKey, (void
14723 20 2a 29 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 *)pCellKey);.
14724 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 c = sqli
14725 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d te3VdbeRecordCom
14726 70 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c 20 70 pare(nCellKey, p
14727 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65 79 CellKey, pIdxKey
14728 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
14729 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c 4b ite3_free(pCellK
1472a 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ey);. i
1472b 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 f( rc ) goto mov
1472c 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 eto_finish;.
1472d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1472e 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a if( c==0 ){.
1472f 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e pCur->in
14730 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b fo.nKey = nCellK
14731 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ey;. if(
14732 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 pPage->intKey &&
14733 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
14734 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d . lwr =
14735 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 idx;.
14736 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 upr = lwr - 1;.
14737 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
14738 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
14739 20 20 20 20 20 20 20 20 20 69 66 28 20 70 52 65 if( pRe
1473a 73 20 29 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 s ) *pRes = 0;.
1473b 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
1473c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
1473d 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 goto moveto_f
1473e 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 7d inish;. }
1473f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
14740 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 f( c<0 ){.
14741 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20 lwr = idx+1;.
14742 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
14743 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d 31 3b upr = idx-1;
14744 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
14745 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a 20 20 f( lwr>upr ){.
14746 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f pCur->info
14747 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 .nKey = nCellKey
14748 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
14749 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1474a 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
1474b 3e 69 50 61 67 65 5d 20 3d 20 28 6c 77 72 2b 75 >iPage] = (lwr+u
1474c 70 72 29 2f 32 3b 0a 20 20 20 20 7d 0a 20 20 20 pr)/2;. }.
1474d 20 61 73 73 65 72 74 28 20 6c 77 72 3d 3d 75 70 assert( lwr==up
1474e 72 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 r+1 );. asser
1474f 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 t( pPage->isInit
14750 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 );. if( pPag
14751 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
14752 20 63 68 6c 64 50 67 20 3d 20 30 3b 0a 20 20 20 chldPg = 0;.
14753 20 7d 65 6c 73 65 20 69 66 28 20 6c 77 72 3e 3d }else if( lwr>=
14754 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a pPage->nCell ){.
14755 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 chldPg = g
14756 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
14757 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
14758 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
14759 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 6c }else{. chl
1475a 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 66 dPg = get4byte(f
1475b 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6c indCell(pPage, l
1475c 77 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 wr));. }.
1475d 69 66 28 20 63 68 6c 64 50 67 3d 3d 30 20 29 7b if( chldPg==0 ){
1475e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1475f 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
14760 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 >iPage]<pCur->ap
14761 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
14762 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 ]->nCell );.
14763 20 20 69 66 28 20 70 52 65 73 20 29 20 2a 70 52 if( pRes ) *pR
14764 65 73 20 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 es = c;. rc
14765 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
14766 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f goto moveto_
14767 66 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 finish;. }.
14768 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
14769 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 6c 77 72 ur->iPage] = lwr
1476a 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f ;. pCur->info
1476b 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 .nSize = 0;.
1476c 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
1476d 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f = 0;. rc = mo
1476e 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
1476f 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 28 chldPg);. if(
14770 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65 74 rc ) goto movet
14771 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f o_finish;. }.mo
14772 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 72 veto_finish:. r
14773 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
14774 2a 2a 20 49 6e 20 74 68 69 73 20 76 65 72 73 69 ** In this versi
14775 6f 6e 20 6f 66 20 42 74 72 65 65 4d 6f 76 65 74 on of BtreeMovet
14776 6f 2c 20 70 4b 65 79 20 69 73 20 61 20 70 61 63 o, pKey is a pac
14777 6b 65 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 ked index record
14778 0a 2a 2a 20 73 75 63 68 20 61 73 20 69 73 20 67 .** such as is g
14779 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 enerated by the
1477a 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 OP_MakeRecord op
1477b 63 6f 64 65 2e 20 20 55 6e 70 61 63 6b 20 74 68 code. Unpack th
1477c 65 0a 2a 2a 20 72 65 63 6f 72 64 20 61 6e 64 20 e.** record and
1477d 74 68 65 6e 20 63 61 6c 6c 20 42 74 72 65 65 4d then call BtreeM
1477e 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 ovetoUnpacked()
1477f 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 2e 0a to do the work..
14780 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14781 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
14782 65 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 eeMoveto(. BtCu
14783 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 rsor *pCur,
14784 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f /* Cursor open o
14785 6e 20 74 68 65 20 62 74 72 65 65 20 74 6f 20 62 n the btree to b
14786 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 e searched */.
14787 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
14788 2c 20 20 20 2f 2a 20 50 61 63 6b 65 64 20 6b 65 , /* Packed ke
14789 79 20 69 66 20 74 68 65 20 62 74 72 65 65 20 69 y if the btree i
1478a 73 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 s an index */.
1478b 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 i64 nKey,
1478c 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 6b /* Integer k
1478d 65 79 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 ey for tables.
1478e 53 69 7a 65 20 6f 66 20 70 4b 65 79 20 66 6f 72 Size of pKey for
1478f 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e indices */. in
14790 74 20 62 69 61 73 2c 20 20 20 20 20 20 20 20 20 t bias,
14791 20 20 2f 2a 20 42 69 61 73 20 73 65 61 72 63 68 /* Bias search
14792 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 to the high end
14793 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 */. int *pRes
14794 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
14795 74 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 te search result
14796 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 s here */.){. i
14797 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
14798 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
14799 74 75 73 20 63 6f 64 65 20 2a 2f 0a 20 20 55 6e tus code */. Un
1479a 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 packedRecord *pI
1479b 64 78 4b 65 79 3b 20 20 20 2f 2a 20 55 6e 70 61 dxKey; /* Unpa
1479c 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a cked index key *
1479d 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f /. UnpackedReco
1479e 72 64 20 61 53 70 61 63 65 5b 31 36 5d 3b 20 2f rd aSpace[16]; /
1479f 2a 20 54 65 6d 70 20 73 70 61 63 65 20 66 6f 72 * Temp space for
147a0 20 70 49 64 78 4b 65 79 20 2d 20 74 6f 20 61 76 pIdxKey - to av
147a1 6f 69 64 20 61 20 6d 61 6c 6c 6f 63 20 2a 2f 0a oid a malloc */.
147a2 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 . if( pKey ){.
147a3 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c pIdxKey = sql
147a4 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e ite3VdbeRecordUn
147a5 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 pack(pCur->pKeyI
147a6 6e 66 6f 2c 20 6e 4b 65 79 2c 20 70 4b 65 79 2c nfo, nKey, pKey,
147a7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
147a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
147a9 20 20 20 20 20 20 20 61 53 70 61 63 65 2c 20 73 aSpace, s
147aa 69 7a 65 6f 66 28 61 53 70 61 63 65 29 29 3b 0a izeof(aSpace));.
147ab 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d if( pIdxKey=
147ac 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
147ad 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 TE_NOMEM;. }els
147ae 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d e{. pIdxKey =
147af 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 0;. }. rc = s
147b0 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 qlite3BtreeMovet
147b1 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72 2c 20 oUnpacked(pCur,
147b2 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c 20 62 pIdxKey, nKey, b
147b3 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20 69 66 ias, pRes);. if
147b4 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 ( pKey ){. sq
147b5 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 lite3VdbeDeleteU
147b6 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 npackedRecord(pI
147b7 64 78 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65 dxKey);. }. re
147b8 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
147b9 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
147ba 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
147bb 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 not pointing at
147bc 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 an entry of the
147bd 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 55 table..**.** TRU
147be 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e E will be return
147bf 65 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 ed after a call
147c0 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e to sqlite3BtreeN
147c1 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 70 ext() moves.** p
147c2 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e 74 ast the last ent
147c3 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ry in the table
147c4 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 or sqlite3BtreeP
147c5 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73 74 rev() moves past
147c6 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65 6e .** the first en
147c7 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61 6c try. TRUE is al
147c8 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 so returned if t
147c9 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 he table is empt
147ca 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
147cb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
147cc 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 6f BtreeEof(BtCurso
147cd 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 r *pCur){. /* T
147ce 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 68 65 ODO: What if the
147cf 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 43 55 cursor is in CU
147d0 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
147d1 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 20 65 but all table e
147d2 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 76 65 ntries. ** have
147d3 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f 20 54 been deleted? T
147d4 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e 65 65 his API will nee
147d5 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f 20 72 d to change to r
147d6 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
147d7 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c 6c ode. ** as well
147d8 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 as the boolean
147d9 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20 20 result value..
147da 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 55 52 */. return (CUR
147db 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 72 2d SOR_VALID!=pCur-
147dc 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a >eState);.}../*.
147dd 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 ** Return the da
147de 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
147df 6e 20 68 61 6e 64 6c 65 20 66 6f 72 20 61 20 63 n handle for a c
147e0 75 72 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ursor..*/.SQLITE
147e1 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
147e2 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 *sqlite3BtreeCu
147e3 72 73 6f 72 44 62 28 63 6f 6e 73 74 20 42 74 43 rsorDb(const BtC
147e4 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
147e5 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
147e6 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d mutex_held(pCur-
147e7 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 >pBtree->db->mut
147e8 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ex) );. return
147e9 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
147ea 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e ;.}../*.** Advan
147eb 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ce the cursor to
147ec 20 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20 the next entry
147ed 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e in the database.
147ee 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 If.** successf
147ef 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 ul then set *pRe
147f0 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72 s=0. If the cur
147f1 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 sor.** was alrea
147f2 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 dy pointing to t
147f3 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
147f4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65 the database be
147f5 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 fore.** this rou
147f6 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c tine was called,
147f7 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d then set *pRes=
147f8 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 1..*/.SQLITE_PRI
147f9 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
147fa 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 BtreeNext(BtCurs
147fb 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
147fc 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Res){. int rc;.
147fd 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d int idx;. Mem
147fe 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 Page *pPage;..
147ff 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
14800 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
14801 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 ;. rc = restore
14802 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
14803 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d Cur);. if( rc!=
14804 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
14805 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
14806 20 20 61 73 73 65 72 74 28 20 70 52 65 73 21 3d assert( pRes!=
14807 30 20 29 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 0 );. if( CURSO
14808 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d R_INVALID==pCur-
14809 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 2a >eState ){. *
1480a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 pRes = 1;. re
1480b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1480c 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e }. if( pCur->
1480d 73 6b 69 70 3e 30 20 29 7b 0a 20 20 20 20 70 43 skip>0 ){. pC
1480e 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 ur->skip = 0;.
1480f 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 *pRes = 0;.
14810 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
14811 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 K;. }. pCur->s
14812 6b 69 70 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 kip = 0;.. pPag
14813 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
14814 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
14815 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 idx = ++pCur->a
14816 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
14817 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ];. assert( pPa
14818 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 ge->isInit );.
14819 61 73 73 65 72 74 28 20 69 64 78 3c 3d 70 50 61 assert( idx<=pPa
1481a 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 ge->nCell );..
1481b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
1481c 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 = 0;. pCur->va
1481d 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 lidNKey = 0;. i
1481e 66 28 20 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e f( idx>=pPage->n
1481f 43 65 6c 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 Cell ){. if(
14820 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
14821 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 rc = moveT
14822 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 oChild(pCur, get
14823 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
14824 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
14825 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20 fset+8]));.
14826 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
14827 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rc;. rc =
14828 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 moveToLeftmost(p
14829 43 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 Cur);. *pRe
1482a 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 s = 0;. ret
1482b 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1482c 20 20 64 6f 7b 0a 20 20 20 20 20 20 69 66 28 20 do{. if(
1482d 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 pCur->iPage==0 )
1482e 7b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 {. *pRes
1482f 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 75 = 1;. pCu
14830 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
14831 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 OR_INVALID;.
14832 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
14833 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 E_OK;. }.
14834 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
14835 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 MoveToParent(pCu
14836 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 r);. pPage
14837 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
14838 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 Cur->iPage];.
14839 20 7d 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 }while( pCur->a
1483a 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
1483b 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 ]>=pPage->nCell
1483c 29 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 );. *pRes = 0
1483d 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d ;. if( pPage-
1483e 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 >intKey ){.
1483f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
14840 65 65 4e 65 78 74 28 70 43 75 72 2c 20 70 52 65 eeNext(pCur, pRe
14841 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 s);. }else{.
14842 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
14843 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 _OK;. }. r
14844 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
14845 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 69 66 28 *pRes = 0;. if(
14846 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a pPage->leaf ){.
14847 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
14848 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d E_OK;. }. rc =
14849 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 moveToLeftmost(
1484a 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 pCur);. return
1484b 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 rc;.}.../*.** St
1484c 65 70 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ep the cursor to
1484d 20 74 68 65 20 62 61 63 6b 20 74 6f 20 74 68 65 the back to the
1484e 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20 previous entry
1484f 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e in the database.
14850 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 If.** successf
14851 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 ul then set *pRe
14852 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72 s=0. If the cur
14853 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 sor.** was alrea
14854 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 dy pointing to t
14855 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 he first entry i
14856 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 n the database b
14857 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f efore.** this ro
14858 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 utine was called
14859 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 , then set *pRes
1485a 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 =1..*/.SQLITE_PR
1485b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1485c 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42 3BtreePrevious(B
1485d 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 tCursor *pCur, i
1485e 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 nt *pRes){. int
1485f 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a rc;. MemPage *
14860 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 pPage;.. assert
14861 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
14862 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 ex(pCur) );. rc
14863 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
14864 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
14865 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
14866 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 _OK ){. retur
14867 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43 75 72 n rc;. }. pCur
14868 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 ->atLast = 0;.
14869 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c if( CURSOR_INVAL
1486a 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 ID==pCur->eState
1486b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 ){. *pRes =
1486c 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 1;. return SQ
1486d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 LITE_OK;. }. i
1486e 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 3c 30 20 f( pCur->skip<0
1486f 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 ){. pCur->ski
14870 70 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 p = 0;. *pRes
14871 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e = 0;. return
14872 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
14873 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 pCur->skip = 0
14874 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 ;.. pPage = pCu
14875 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
14876 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 iPage];. assert
14877 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 ( pPage->isInit
14878 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d );. if( !pPage-
14879 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 69 6e 74 >leaf ){. int
1487a 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 idx = pCur->aiI
1487b 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b dx[pCur->iPage];
1487c 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f . rc = moveTo
1487d 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 Child(pCur, get4
1487e 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 byte(findCell(pP
1487f 61 67 65 2c 20 69 64 78 29 29 29 3b 0a 20 20 20 age, idx)));.
14880 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
14881 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
14882 7d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 }. rc = moveT
14883 6f 52 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 oRightmost(pCur)
14884 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 ;. }else{. w
14885 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 hile( pCur->aiId
14886 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d x[pCur->iPage]==
14887 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
14888 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b Cur->iPage==0 ){
14889 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65 . pCur->e
1488a 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 State = CURSOR_I
1488b 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 NVALID;.
1488c 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 *pRes = 1;.
1488d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1488e 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 _OK;. }.
1488f 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d sqlite3BtreeM
14890 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 oveToParent(pCur
14891 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 );. }. pCu
14892 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 r->info.nSize =
14893 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 0;. pCur->val
14894 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 20 idNKey = 0;..
14895 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
14896 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a 20 20 20 r->iPage]--;.
14897 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
14898 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
14899 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 e];. if( pPag
1489a 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50 e->intKey && !pP
1489b 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
1489c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1489d 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 75 treePrevious(pCu
1489e 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 r, pRes);. }e
1489f 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
148a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
148a1 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 . }. *pRes = 0
148a2 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
148a3 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
148a4 20 61 20 6e 65 77 20 70 61 67 65 20 66 72 6f 6d a new page from
148a5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
148a6 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 le..**.** The ne
148a7 77 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 w page is marked
148a8 20 61 73 20 64 69 72 74 79 2e 20 20 28 49 6e 20 as dirty. (In
148a9 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 73 71 6c other words, sql
148aa 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 ite3PagerWrite()
148ab 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 .** has already
148ac 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 74 been called on t
148ad 68 65 20 6e 65 77 20 70 61 67 65 2e 29 20 20 54 he new page.) T
148ae 68 65 20 6e 65 77 20 70 61 67 65 20 68 61 73 20 he new page has
148af 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20 72 65 66 also.** been ref
148b0 65 72 65 6e 63 65 64 20 61 6e 64 20 74 68 65 20 erenced and the
148b1 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 calling routine
148b2 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
148b3 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 or calling.** sq
148b4 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
148b5 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 ) on the new pag
148b6 65 20 77 68 65 6e 20 69 74 20 69 73 20 64 6f 6e e when it is don
148b7 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f e..**.** SQLITE_
148b8 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f OK is returned o
148b9 6e 20 73 75 63 63 65 73 73 2e 20 20 41 6e 79 20 n success. Any
148ba 6f 74 68 65 72 20 72 65 74 75 72 6e 20 76 61 6c other return val
148bb 75 65 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 ue indicates.**
148bc 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70 70 50 61 an error. *ppPa
148bd 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 61 72 ge and *pPgno ar
148be 65 20 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 e undefined in t
148bf 68 65 20 65 76 65 6e 74 20 6f 66 20 61 6e 20 65 he event of an e
148c0 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 rror..** Do not
148c1 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 50 61 invoke sqlite3Pa
148c2 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 2a 70 gerUnref() on *p
148c3 70 50 61 67 65 20 69 66 20 61 6e 20 65 72 72 6f pPage if an erro
148c4 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a r is returned..*
148c5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 6e 65 61 *.** If the "nea
148c6 72 62 79 22 20 70 61 72 61 6d 65 74 65 72 20 69 rby" parameter i
148c7 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 61 20 s not 0, then a
148c8 28 66 65 65 62 6c 65 29 20 65 66 66 6f 72 74 20 (feeble) effort
148c9 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a 20 6c is made to .** l
148ca 6f 63 61 74 65 20 61 20 70 61 67 65 20 63 6c 6f ocate a page clo
148cb 73 65 20 74 6f 20 74 68 65 20 70 61 67 65 20 6e se to the page n
148cc 75 6d 62 65 72 20 22 6e 65 61 72 62 79 22 2e 20 umber "nearby".
148cd 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 This can be use
148ce 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74 65 6d d in an.** attem
148cf 70 74 20 74 6f 20 6b 65 65 70 20 72 65 6c 61 74 pt to keep relat
148d0 65 64 20 70 61 67 65 73 20 63 6c 6f 73 65 20 74 ed pages close t
148d1 6f 20 65 61 63 68 20 6f 74 68 65 72 20 69 6e 20 o each other in
148d2 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
148d3 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e 20 74 e,.** which in t
148d4 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64 61 74 urn can make dat
148d5 61 62 61 73 65 20 61 63 63 65 73 73 20 66 61 73 abase access fas
148d6 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ter..**.** If th
148d7 65 20 22 65 78 61 63 74 22 20 70 61 72 61 6d 65 e "exact" parame
148d8 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 61 6e ter is not 0, an
148d9 64 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 d the page-numbe
148da 72 20 6e 65 61 72 62 79 20 65 78 69 73 74 73 20 r nearby exists
148db 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f 6e 20 .** anywhere on
148dc 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 the free-list, t
148dd 68 65 6e 20 69 74 20 69 73 20 67 75 61 72 65 6e hen it is guaren
148de 74 65 65 64 20 74 6f 20 62 65 20 72 65 74 75 72 teed to be retur
148df 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 ned. This.** is
148e0 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 61 75 74 only used by aut
148e1 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 o-vacuum databas
148e2 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63 61 74 69 es when allocati
148e3 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65 2e 0a ng a new table..
148e4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c */.static int al
148e5 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 locateBtreePage(
148e6 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
148e7 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 , . MemPage **p
148e8 70 50 61 67 65 2c 20 0a 20 20 50 67 6e 6f 20 2a pPage, . Pgno *
148e9 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e 6f 20 6e pPgno, . Pgno n
148ea 65 61 72 62 79 2c 0a 20 20 75 38 20 65 78 61 63 earby,. u8 exac
148eb 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a t.){. MemPage *
148ec 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 pPage1;. int rc
148ed 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 2f ;. int n; /
148ee 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
148ef 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 s on the freelis
148f0 74 20 2a 2f 0a 20 20 69 6e 74 20 6b 3b 20 20 20 t */. int k;
148f1 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c /* Number of l
148f2 65 61 76 65 73 20 6f 6e 20 74 68 65 20 74 72 75 eaves on the tru
148f3 6e 6b 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 nk of the freeli
148f4 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 st */. MemPage
148f5 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d *pTrunk = 0;. M
148f6 65 6d 50 61 67 65 20 2a 70 50 72 65 76 54 72 75 emPage *pPrevTru
148f7 6e 6b 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 nk = 0;.. asser
148f8 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
148f9 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
148fa 29 20 29 3b 0a 20 20 70 50 61 67 65 31 20 3d 20 ) );. pPage1 =
148fb 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 6e pBt->pPage1;. n
148fc 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
148fd 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b ge1->aData[36]);
148fe 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b 0a 20 20 . if( n>0 ){.
148ff 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 70 /* There are p
14900 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 ages on the free
14901 6c 69 73 74 2e 20 20 52 65 75 73 65 20 6f 6e 65 list. Reuse one
14902 20 6f 66 20 74 68 6f 73 65 20 70 61 67 65 73 2e of those pages.
14903 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 54 72 */. Pgno iTr
14904 75 6e 6b 3b 0a 20 20 20 20 75 38 20 73 65 61 72 unk;. u8 sear
14905 63 68 4c 69 73 74 20 3d 20 30 3b 20 2f 2a 20 49 chList = 0; /* I
14906 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 f the free-list
14907 6d 75 73 74 20 62 65 20 73 65 61 72 63 68 65 64 must be searched
14908 20 66 6f 72 20 27 6e 65 61 72 62 79 27 20 2a 2f for 'nearby' */
14909 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 . . /* If
1490a 74 68 65 20 27 65 78 61 63 74 27 20 70 61 72 61 the 'exact' para
1490b 6d 65 74 65 72 20 77 61 73 20 74 72 75 65 20 61 meter was true a
1490c 6e 64 20 61 20 71 75 65 72 79 20 6f 66 20 74 68 nd a query of th
1490d 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 e pointer-map.
1490e 20 20 2a 2a 20 73 68 6f 77 73 20 74 68 61 74 20 ** shows that
1490f 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 the page 'nearby
14910 27 20 69 73 20 73 6f 6d 65 77 68 65 72 65 20 6f ' is somewhere o
14911 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c n the free-list,
14912 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 then. ** the
14913 20 65 6e 74 69 72 65 2d 6c 69 73 74 20 77 69 6c entire-list wil
14914 6c 20 62 65 20 73 65 61 72 63 68 65 64 20 66 6f l be searched fo
14915 72 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 20 r that page..
14916 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
14917 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
14918 55 4d 0a 20 20 20 20 69 66 28 20 65 78 61 63 74 UM. if( exact
14919 20 26 26 20 6e 65 61 72 62 79 3c 3d 70 61 67 65 && nearby<=page
1491a 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 rPagecount(pBt)
1491b 29 7b 0a 20 20 20 20 20 20 75 38 20 65 54 79 70 ){. u8 eTyp
1491c 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e;. assert(
1491d 20 6e 65 61 72 62 79 3e 30 20 29 3b 0a 20 20 20 nearby>0 );.
1491e 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
1491f 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 autoVacuum );.
14920 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 rc = ptrmapG
14921 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 2c 20 et(pBt, nearby,
14922 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 20 20 &eType, 0);.
14923 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
14924 6e 20 72 63 3b 0a 20 20 20 20 20 20 69 66 28 20 n rc;. if(
14925 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 eType==PTRMAP_FR
14926 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 EEPAGE ){.
14927 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 31 searchList = 1
14928 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
14929 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 72 62 79 3b *pPgno = nearby;
1492a 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
1492b 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 /* Decrement
1492c 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 63 6f the free-list co
1492d 75 6e 74 20 62 79 20 31 2e 20 53 65 74 20 69 54 unt by 1. Set iT
1492e 72 75 6e 6b 20 74 6f 20 74 68 65 20 69 6e 64 65 runk to the inde
1492f 78 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 x of the. **
14930 66 69 72 73 74 20 66 72 65 65 2d 6c 69 73 74 20 first free-list
14931 74 72 75 6e 6b 20 70 61 67 65 2e 20 69 50 72 65 trunk page. iPre
14932 76 54 72 75 6e 6b 20 69 73 20 69 6e 69 74 69 61 vTrunk is initia
14933 6c 6c 79 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20 lly 1.. */.
14934 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
14935 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d gerWrite(pPage1-
14936 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
14937 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
14938 63 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 c;. put4byte(
14939 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
1493a 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f 6], n-1);.. /
1493b 2a 20 54 68 65 20 63 6f 64 65 20 77 69 74 68 69 * The code withi
1493c 6e 20 74 68 69 73 20 6c 6f 6f 70 20 69 73 20 72 n this loop is r
1493d 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20 un only once if
1493e 74 68 65 20 27 73 65 61 72 63 68 4c 69 73 74 27 the 'searchList'
1493f 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a variable. **
14940 20 69 73 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74 is not true. Ot
14941 68 65 72 77 69 73 65 2c 20 69 74 20 72 75 6e 73 herwise, it runs
14942 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74 once for each t
14943 72 75 6e 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65 runk-page on the
14944 0a 20 20 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73 . ** free-lis
14945 74 20 75 6e 74 69 6c 20 74 68 65 20 70 61 67 65 t until the page
14946 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f 63 'nearby' is loc
14947 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ated.. */.
14948 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 50 72 65 do {. pPre
14949 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b vTrunk = pTrunk;
1494a 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 65 76 . if( pPrev
1494b 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 Trunk ){.
1494c 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 iTrunk = get4by
1494d 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e te(&pPrevTrunk->
1494e 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 aData[0]);.
1494f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
14950 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 iTrunk = get4byt
14951 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
14952 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 [32]);. }.
14953 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
14954 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 3BtreeGetPage(pB
14955 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 t, iTrunk, &pTru
14956 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 nk, 0);. if
14957 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 ( rc ){.
14958 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 pTrunk = 0;.
14959 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
1495a 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
1495b 20 20 7d 0a 0a 20 20 20 20 20 20 6b 20 3d 20 67 }.. k = g
1495c 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d et4byte(&pTrunk-
1495d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20 >aData[4]);.
1495e 20 20 69 66 28 20 6b 3d 3d 30 20 26 26 20 21 73 if( k==0 && !s
1495f 65 61 72 63 68 4c 69 73 74 20 29 7b 0a 20 20 20 earchList ){.
14960 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e /* The trun
14961 6b 20 68 61 73 20 6e 6f 20 6c 65 61 76 65 73 20 k has no leaves
14962 61 6e 64 20 74 68 65 20 6c 69 73 74 20 69 73 20 and the list is
14963 6e 6f 74 20 62 65 69 6e 67 20 73 65 61 72 63 68 not being search
14964 65 64 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ed. . **
14965 53 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 74 So extract the t
14966 72 75 6e 6b 20 70 61 67 65 20 69 74 73 65 6c 66 runk page itself
14967 20 61 6e 64 20 75 73 65 20 69 74 20 61 73 20 74 and use it as t
14968 68 65 20 6e 65 77 6c 79 20 0a 20 20 20 20 20 20 he newly .
14969 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 ** allocated p
1496a 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 age */. a
1496b 73 73 65 72 74 28 20 70 50 72 65 76 54 72 75 6e ssert( pPrevTrun
1496c 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 k==0 );.
1496d 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1496e 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 rWrite(pTrunk->p
1496f 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
14970 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
14971 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c goto end_al
14972 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 locate_page;.
14973 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a }. *
14974 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a pPgno = iTrunk;.
14975 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
14976 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
14977 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 ], &pTrunk->aDat
14978 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 a[0], 4);.
14979 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75 *ppPage = pTru
1497a 6e 6b 3b 0a 20 20 20 20 20 20 20 20 70 54 72 75 nk;. pTru
1497b 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 nk = 0;.
1497c 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
1497d 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 : %d trunk - %d
1497e 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c free pages left\
1497f 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 n", *pPgno, n-1)
14980 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
14981 66 28 20 6b 3e 70 42 74 2d 3e 75 73 61 62 6c 65 f( k>pBt->usable
14982 53 69 7a 65 2f 34 20 2d 20 32 20 29 7b 0a 20 20 Size/4 - 2 ){.
14983 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f /* Value o
14984 66 20 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 f k is out of ra
14985 6e 67 65 2e 20 20 44 61 74 61 62 61 73 65 20 63 nge. Database c
14986 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 orruption */.
14987 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
14988 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
14989 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
1498a 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 allocate_page;.#
1498b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1498c 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
1498d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 }else if( se
1498e 61 72 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72 archList && near
1498f 62 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 by==iTrunk ){.
14990 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 /* The lis
14991 74 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 t is being searc
14992 68 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 hed and this tru
14993 6e 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70 nk page is the p
14994 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 age. ** t
14995 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 o allocate, rega
14996 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 rdless of whethe
14997 72 20 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e r it has leaves.
14998 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
14999 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 assert( *pPg
1499a 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 no==iTrunk );.
1499b 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 *ppPage =
1499c 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 pTrunk;.
1499d 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a searchList = 0;.
1499e 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1499f 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
149a0 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
149a1 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 . if( rc
149a2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
149a3 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
149a4 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 age;. }.
149a5 20 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 if( k==0
149a6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
149a7 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a !pPrevTrunk ){.
149a8 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 memc
149a9 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 py(&pPage1->aDat
149aa 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e a[32], &pTrunk->
149ab 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
149ac 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
149ad 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 memcp
149ae 79 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 y(&pPrevTrunk->a
149af 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b Data[0], &pTrunk
149b0 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a ->aData[0], 4);.
149b1 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
149b2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
149b3 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e /* The trun
149b4 6b 20 70 61 67 65 20 69 73 20 72 65 71 75 69 72 k page is requir
149b5 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ed by the caller
149b6 20 62 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 but it contains
149b7 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 . ** p
149b8 6f 69 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d ointers to free-
149b9 6c 69 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 list leaves. The
149ba 20 66 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f first leaf beco
149bb 6d 65 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 mes a trunk.
149bc 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e ** page in
149bd 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 this case..
149be 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
149bf 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 MemPage *pNew
149c0 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 Trunk;.
149c1 20 50 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 Pgno iNewTrunk
149c2 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 = get4byte(&pTru
149c3 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 nk->aData[8]);.
149c4 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
149c5 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
149c6 65 28 70 42 74 2c 20 69 4e 65 77 54 72 75 6e 6b e(pBt, iNewTrunk
149c7 2c 20 26 70 4e 65 77 54 72 75 6e 6b 2c 20 30 29 , &pNewTrunk, 0)
149c8 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
149c9 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
149ca 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 . got
149cb 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
149cc 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d age;. }
149cd 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
149ce 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
149cf 65 28 70 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 62 e(pNewTrunk->pDb
149d0 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
149d1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
149d2 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
149d3 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e releasePage(pN
149d4 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 ewTrunk);.
149d5 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
149d6 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
149d7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
149d8 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 memcpy(&pNew
149d9 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c Trunk->aData[0],
149da 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b &pTrunk->aData[
149db 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 0], 4);.
149dc 20 20 70 75 74 34 62 79 74 65 28 26 70 4e 65 77 put4byte(&pNew
149dd 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c Trunk->aData[4],
149de 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 k-1);.
149df 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75 memcpy(&pNewTru
149e0 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 26 70 nk->aData[8], &p
149e1 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 31 32 5d Trunk->aData[12]
149e2 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a 20 20 20 20 , (k-1)*4);.
149e3 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
149e4 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 e(pNewTrunk);.
149e5 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50 72 if( !pPr
149e6 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 evTrunk ){.
149e7 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
149e8 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
149e9 32 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 2], iNewTrunk);.
149ea 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
149eb 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
149ec 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
149ed 69 74 65 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e ite(pPrevTrunk->
149ee 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
149ef 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
149f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6f go
149f1 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
149f2 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 page;.
149f3 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
149f4 70 75 74 34 62 79 74 65 28 26 70 50 72 65 76 54 put4byte(&pPrevT
149f5 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 runk->aData[0],
149f6 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 iNewTrunk);.
149f7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
149f8 7d 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b }. pTrunk
149f9 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 = 0;. TR
149fa 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 ACE(("ALLOCATE:
149fb 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 %d trunk - %d fr
149fc 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 ee pages left\n"
149fd 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b , *pPgno, n-1));
149fe 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 65 .#endif. }e
149ff 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
14a00 45 78 74 72 61 63 74 20 61 20 6c 65 61 66 20 66 Extract a leaf f
14a01 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20 2a 2f rom the trunk */
14a02 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 6c 6f . int clo
14a03 73 65 73 74 3b 0a 20 20 20 20 20 20 20 20 50 67 sest;. Pg
14a04 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20 no iPage;.
14a05 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
14a06 2a 61 44 61 74 61 20 3d 20 70 54 72 75 6e 6b 2d *aData = pTrunk-
14a07 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 >aData;.
14a08 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
14a09 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 rWrite(pTrunk->p
14a0a 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
14a0b 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
14a0c 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c goto end_al
14a0d 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 locate_page;.
14a0e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
14a0f 66 28 20 6e 65 61 72 62 79 3e 30 20 29 7b 0a 20 f( nearby>0 ){.
14a10 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 2c 20 int i,
14a11 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 dist;.
14a12 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 closest = 0;.
14a13 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 67 65 dist = ge
14a14 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 5d t4byte(&aData[8]
14a15 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 ) - nearby;.
14a16 20 20 20 20 20 20 69 66 28 20 64 69 73 74 3c 30 if( dist<0
14a17 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73 74 3b ) dist = -dist;
14a18 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 . for(i
14a19 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 =1; i<k; i++){.
14a1a 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 64 int d
14a1b 32 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 2 = get4byte(&aD
14a1c 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e 65 ata[8+i*4]) - ne
14a1d 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20 arby;.
14a1e 20 20 69 66 28 20 64 32 3c 30 20 29 20 64 32 20 if( d2<0 ) d2
14a1f 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 20 20 = -d2;.
14a20 20 20 20 69 66 28 20 64 32 3c 64 69 73 74 20 29 if( d2<dist )
14a21 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
14a22 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20 closest = i;.
14a23 20 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 dist
14a24 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20 = d2;.
14a25 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
14a26 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
14a27 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74 closest
14a28 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 0;. }.
14a29 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 20 3d . iPage =
14a2a 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 get4byte(&aData
14a2b 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0a [8+closest*4]);.
14a2c 20 20 20 20 20 20 20 20 69 66 28 20 21 73 65 61 if( !sea
14a2d 72 63 68 4c 69 73 74 20 7c 7c 20 69 50 61 67 65 rchList || iPage
14a2e 3d 3d 6e 65 61 72 62 79 20 29 7b 0a 20 20 20 20 ==nearby ){.
14a2f 20 20 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 Pgno nPage
14a30 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 ;. *pPg
14a31 6e 6f 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 no = iPage;.
14a32 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 70 61 nPage = pa
14a33 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
14a34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
14a35 20 2a 70 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b *pPgno>nPage ){
14a36 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
14a37 46 72 65 65 20 70 61 67 65 20 6f 66 66 20 74 68 Free page off th
14a38 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c e end of the fil
14a39 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 e */.
14a3a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
14a3b 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
14a3c 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
14a3d 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
14a3e 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
14a3f 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c TRACE(("ALL
14a40 4f 43 41 54 45 3a 20 25 64 20 77 61 73 20 6c 65 OCATE: %d was le
14a41 61 66 20 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 af %d of %d on t
14a42 72 75 6e 6b 20 25 64 22 0a 20 20 20 20 20 20 20 runk %d".
14a43 20 20 20 20 20 20 20 20 20 20 22 3a 20 25 64 20 ": %d
14a44 6d 6f 72 65 20 66 72 65 65 20 70 61 67 65 73 5c more free pages\
14a45 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
14a46 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f *pPgno, clo
14a47 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e sest+1, k, pTrun
14a48 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a k->pgno, n-1));.
14a49 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6c if( cl
14a4a 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 osest<k-1 ){.
14a4b 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
14a4c 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 &aData[8+closest
14a4d 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a *4], &aData[4+k*
14a4e 34 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 4], 4);.
14a4f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 75 }. pu
14a50 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 34 5d t4byte(&aData[4]
14a51 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 , k-1);.
14a52 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
14a53 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
14a54 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 *pPgno, ppPage,
14a55 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 1);. if
14a56 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
14a57 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 ){. s
14a58 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 52 qlite3PagerDontR
14a59 6f 6c 6c 62 61 63 6b 28 28 2a 70 70 50 61 67 65 ollback((*ppPage
14a5a 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 )->pDbPage);.
14a5b 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
14a5c 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
14a5d 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 (*ppPage)->pDbPa
14a5e 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ge);.
14a5f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
14a60 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
14a61 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
14a62 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 *ppPage);.
14a63 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
14a64 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 65 }. se
14a65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 archList = 0;.
14a66 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
14a67 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
14a68 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 e(pPrevTrunk);.
14a69 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20 pPrevTrunk
14a6a 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 = 0;. }while(
14a6b 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0a 20 searchList );.
14a6c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
14a6d 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67 65 here are no page
14a6e 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 s on the freelis
14a6f 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61 20 6e t, so create a n
14a70 65 77 20 70 61 67 65 20 61 74 20 74 68 65 0a 20 ew page at the.
14a71 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 ** end of the
14a72 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 file */. int
14a73 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 61 nPage = pagerPa
14a74 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20 gecount(pBt);.
14a75 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 61 67 65 *pPgno = nPage
14a76 20 2b 20 31 3b 0a 0a 23 69 66 6e 64 65 66 20 53 + 1;..#ifndef S
14a77 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
14a78 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 ACUUM. if( pB
14a79 74 2d 3e 6e 54 72 75 6e 63 20 29 7b 0a 20 20 20 t->nTrunc ){.
14a7a 20 20 20 2f 2a 20 41 6e 20 69 6e 63 72 2d 76 61 /* An incr-va
14a7b 63 75 75 6d 20 68 61 73 20 61 6c 72 65 61 64 79 cuum has already
14a7c 20 72 75 6e 20 77 69 74 68 69 6e 20 74 68 69 73 run within this
14a7d 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 53 6f transaction. So
14a7e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 the. ** pa
14a7f 67 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 69 ge to allocate i
14a80 73 20 6e 6f 74 20 66 72 6f 6d 20 74 68 65 20 70 s not from the p
14a81 68 79 73 69 63 61 6c 20 65 6e 64 20 6f 66 20 74 hysical end of t
14a82 68 65 20 66 69 6c 65 2c 20 62 75 74 0a 20 20 20 he file, but.
14a83 20 20 20 2a 2a 20 61 74 20 70 42 74 2d 3e 6e 54 ** at pBt->nT
14a84 72 75 6e 63 2e 20 0a 20 20 20 20 20 20 2a 2f 0a runc. . */.
14a85 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 70 *pPgno = p
14a86 42 74 2d 3e 6e 54 72 75 6e 63 2b 31 3b 0a 20 20 Bt->nTrunc+1;.
14a87 20 20 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d if( *pPgno==
14a88 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
14a89 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 E(pBt) ){.
14a8a 20 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 (*pPgno)++;.
14a8b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
14a8c 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
14a8d 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 uum && PTRMAP_IS
14a8e 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f PAGE(pBt, *pPgno
14a8f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 ) ){. /* If
14a90 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 *pPgno refers t
14a91 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 o a pointer-map
14a92 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 page, allocate t
14a93 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 wo new pages.
14a94 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 ** at the end
14a95 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 of the file ins
14a96 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 tead of one. The
14a97 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 first allocated
14a98 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 page. ** b
14a99 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 ecomes a new poi
14a9a 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 nter-map page, t
14a9b 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 he second is use
14a9c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e d by the caller.
14a9d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
14a9e 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 TRACE(("ALLOCATE
14a9f 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 : %d from end of
14aa0 20 66 69 6c 65 20 28 70 6f 69 6e 74 65 72 2d 6d file (pointer-m
14aa1 61 70 20 70 61 67 65 29 5c 6e 22 2c 20 2a 70 50 ap page)\n", *pP
14aa2 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 61 73 73 gno));. ass
14aa3 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e ert( *pPgno!=PEN
14aa4 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
14aa5 42 74 29 20 29 3b 0a 20 20 20 20 20 20 28 2a 70 Bt) );. (*p
14aa6 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69 Pgno)++;. i
14aa7 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 f( *pPgno==PENDI
14aa8 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
14aa9 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b ) ){ (*pPgno)++;
14aaa 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
14aab 20 70 42 74 2d 3e 6e 54 72 75 6e 63 20 29 7b 0a pBt->nTrunc ){.
14aac 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 75 6e pBt->nTrun
14aad 63 20 3d 20 2a 70 50 67 6e 6f 3b 0a 20 20 20 20 c = *pPgno;.
14aae 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 61 73 }.#endif.. as
14aaf 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 sert( *pPgno!=PE
14ab0 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
14ab1 70 42 74 29 20 29 3b 0a 20 20 20 20 72 63 20 3d pBt) );. rc =
14ab2 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
14ab3 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f Page(pBt, *pPgno
14ab4 2c 20 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 , ppPage, 0);.
14ab5 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
14ab6 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 n rc;. rc = s
14ab7 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
14ab8 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 ((*ppPage)->pDbP
14ab9 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
14aba 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
14abb 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
14abc 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d (*ppPage);. }
14abd 0a 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c . TRACE(("ALL
14abe 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 OCATE: %d from e
14abf 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a nd of file\n", *
14ac0 70 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 pPgno));. }..
14ac1 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d assert( *pPgno!=
14ac2 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
14ac3 45 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 E(pBt) );..end_a
14ac4 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 llocate_page:.
14ac5 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 releasePage(pTru
14ac6 6e 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 nk);. releasePa
14ac7 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a ge(pPrevTrunk);.
14ac8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
14ac9 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 _OK ){. if( s
14aca 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 qlite3PagerPageR
14acb 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 efcount((*ppPage
14acc 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b )->pDbPage)>1 ){
14acd 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
14ace 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 ge(*ppPage);.
14acf 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
14ad0 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
14ad1 20 20 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 }. (*ppPag
14ad2 65 29 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a e)->isInit = 0;.
14ad3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
14ad4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 .}../*.** Add a
14ad5 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
14ad6 62 61 73 65 20 66 69 6c 65 20 74 6f 20 74 68 65 base file to the
14ad7 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a freelist..**.**
14ad8 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
14ad9 65 66 28 29 20 69 73 20 4e 4f 54 20 63 61 6c 6c ef() is NOT call
14ada 65 64 20 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f ed for pPage..*/
14adb 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 .static int free
14adc 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 Page(MemPage *pP
14add 61 67 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 age){. BtShared
14ade 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 *pBt = pPage->p
14adf 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 Bt;. MemPage *p
14ae0 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 Page1 = pBt->pPa
14ae1 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 2c 20 6e ge1;. int rc, n
14ae2 2c 20 6b 3b 0a 0a 20 20 2f 2a 20 50 72 65 70 61 , k;.. /* Prepa
14ae3 72 65 20 74 68 65 20 70 61 67 65 20 66 6f 72 20 re the page for
14ae4 66 72 65 65 69 6e 67 20 2a 2f 0a 20 20 61 73 73 freeing */. ass
14ae5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14ae6 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
14ae7 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
14ae8 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
14ae9 67 6e 6f 3e 31 20 29 3b 0a 20 20 70 50 61 67 65 gno>1 );. pPage
14aea 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 0a 20 ->isInit = 0;..
14aeb 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 /* Increment th
14aec 65 20 66 72 65 65 20 70 61 67 65 20 63 6f 75 6e e free page coun
14aed 74 20 6f 6e 20 70 50 61 67 65 31 20 2a 2f 0a 20 t on pPage1 */.
14aee 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
14aef 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e erWrite(pPage1->
14af0 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 pDbPage);. if(
14af1 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
14af2 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 n = get4byte(&
14af3 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
14af4 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 ]);. put4byte(&
14af5 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
14af6 5d 2c 20 6e 2b 31 29 3b 0a 0a 23 69 66 64 65 66 ], n+1);..#ifdef
14af7 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 SQLITE_SECURE_D
14af8 45 4c 45 54 45 0a 20 20 2f 2a 20 49 66 20 74 68 ELETE. /* If th
14af9 65 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f e SQLITE_SECURE_
14afa 44 45 4c 45 54 45 20 63 6f 6d 70 69 6c 65 2d 74 DELETE compile-t
14afb 69 6d 65 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e ime option is en
14afc 61 62 6c 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a abled, then. **
14afd 20 61 6c 77 61 79 73 20 66 75 6c 6c 79 20 6f 76 always fully ov
14afe 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 erwrite deleted
14aff 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 information with
14b00 20 7a 65 72 6f 73 2e 0a 20 20 2a 2f 0a 20 20 72 zeros.. */. r
14b01 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
14b02 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
14b03 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 Page);. if( rc
14b04 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d ) return rc;. m
14b05 65 6d 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 emset(pPage->aDa
14b06 74 61 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 ta, 0, pPage->pB
14b07 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 65 t->pageSize);.#e
14b08 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ndif.. /* If th
14b09 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
14b0a 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c rts auto-vacuum,
14b0b 20 77 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 write an entry
14b0c 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d in the pointer-m
14b0d 61 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 ap. ** to indic
14b0e 61 74 65 20 74 68 61 74 20 74 68 65 20 70 61 67 ate that the pag
14b0f 65 20 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a e is free.. */.
14b10 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
14b11 55 4d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 UM ){. rc = p
14b12 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 50 trmapPut(pBt, pP
14b13 61 67 65 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 age->pgno, PTRMA
14b14 50 5f 46 52 45 45 50 41 47 45 2c 20 30 29 3b 0a P_FREEPAGE, 0);.
14b15 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
14b16 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 urn rc;. }.. i
14b17 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f f( n==0 ){. /
14b18 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 66 69 * This is the fi
14b19 72 73 74 20 66 72 65 65 20 70 61 67 65 20 2a 2f rst free page */
14b1a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
14b1b 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 3PagerWrite(pPag
14b1c 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
14b1d 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
14b1e 20 72 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 rc;. memset(
14b1f 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 30 2c pPage->aData, 0,
14b20 20 38 29 3b 0a 20 20 20 20 70 75 74 34 62 79 74 8);. put4byt
14b21 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
14b22 5b 33 32 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e [32], pPage->pgn
14b23 6f 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22 o);. TRACE(("
14b24 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 66 69 FREE-PAGE: %d fi
14b25 72 73 74 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 rst\n", pPage->p
14b26 67 6e 6f 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a gno));. }else{.
14b27 20 20 20 20 2f 2a 20 4f 74 68 65 72 20 66 72 65 /* Other fre
14b28 65 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 e pages already
14b29 65 78 69 73 74 2e 20 20 52 65 74 72 69 76 65 20 exist. Retrive
14b2a 74 68 65 20 66 69 72 73 74 20 74 72 75 6e 6b 20 the first trunk
14b2b 70 61 67 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 page. ** of t
14b2c 68 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20 he freelist and
14b2d 66 69 6e 64 20 6f 75 74 20 68 6f 77 20 6d 61 6e find out how man
14b2e 79 20 6c 65 61 76 65 73 20 69 74 20 68 61 73 2e y leaves it has.
14b2f 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 */. MemPage
14b30 2a 70 54 72 75 6e 6b 3b 0a 20 20 20 20 72 63 20 *pTrunk;. rc
14b31 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
14b32 74 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 tPage(pBt, get4b
14b33 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
14b34 74 61 5b 33 32 5d 29 2c 20 26 70 54 72 75 6e 6b ta[32]), &pTrunk
14b35 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
14b36 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
14b37 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 k = get4byte(&
14b38 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d pTrunk->aData[4]
14b39 29 3b 0a 20 20 20 20 69 66 28 20 6b 3e 3d 70 42 );. if( k>=pB
14b3a 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 t->usableSize/4
14b3b 2d 20 38 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 - 8 ){. /*
14b3c 54 68 65 20 74 72 75 6e 6b 20 69 73 20 66 75 6c The trunk is ful
14b3d 6c 2e 20 20 54 75 72 6e 20 74 68 65 20 70 61 67 l. Turn the pag
14b3e 65 20 62 65 69 6e 67 20 66 72 65 65 64 20 69 6e e being freed in
14b3f 74 6f 20 61 20 6e 65 77 0a 20 20 20 20 20 20 2a to a new. *
14b40 2a 20 74 72 75 6e 6b 20 70 61 67 65 20 77 69 74 * trunk page wit
14b41 68 20 6e 6f 20 6c 65 61 76 65 73 2e 0a 20 20 20 h no leaves..
14b42 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e **. ** N
14b43 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72 75 ote that the tru
14b44 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72 nk page is not r
14b45 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c eally full until
14b46 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 it contains.
14b47 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 ** usableSize
14b48 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 /4 - 2 entries,
14b49 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 not usableSize/4
14b4a 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 20 - 8 entries as
14b4b 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a we have. **
14b4c 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65 coded. But due
14b4d 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 to a coding err
14b4e 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f or in versions o
14b4f 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74 f SQLite prior t
14b50 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 o. ** 3.6.0
14b51 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68 , databases with
14b52 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 freelist trunk
14b53 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f pages holding mo
14b54 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a re than. **
14b55 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
14b56 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 8 entries will b
14b57 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f e reported as co
14b58 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 rrupt. In order
14b59 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 . ** to mai
14b5a 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 ntain backwards
14b5b 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 compatibility wi
14b5c 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e th older version
14b5d 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 s of SQLite,.
14b5e 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f ** we will co
14b5f 6e 74 61 69 6e 20 74 6f 20 72 65 73 74 72 69 63 ntain to restric
14b60 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
14b61 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62 6c entries to usabl
14b62 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20 20 eSize/4 - 8.
14b63 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 41 ** for now. A
14b64 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 t some point in
14b65 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63 65 the future (once
14b66 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75 70 everyone has up
14b67 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 graded. **
14b68 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74 65 to 3.6.0 or late
14b69 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e r) we should con
14b6a 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 68 65 sider fixing the
14b6b 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62 6f conditional abo
14b6c 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 72 ve. ** to r
14b6d 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65 2f ead "usableSize/
14b6e 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66 20 4-2" instead of
14b6f 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 22 "usableSize/4-8"
14b70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
14b71 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
14b72 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 erWrite(pPage->p
14b73 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 DbPage);. i
14b74 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
14b75 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34 ){. put4
14b76 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 byte(pPage->aDat
14b77 61 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 a, pTrunk->pgno)
14b78 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 ;. put4by
14b79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
14b7a 5b 34 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 [4], 0);.
14b7b 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
14b7c 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 70 50 1->aData[32], pP
14b7d 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 age->pgno);.
14b7e 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45 TRACE(("FREE
14b7f 2d 50 41 47 45 3a 20 25 64 20 6e 65 77 20 74 72 -PAGE: %d new tr
14b80 75 6e 6b 20 70 61 67 65 20 72 65 70 6c 61 63 69 unk page replaci
14b81 6e 67 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 ng %d\n",.
14b82 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 2d pPage-
14b83 3e 70 67 6e 6f 2c 20 70 54 72 75 6e 6b 2d 3e 70 >pgno, pTrunk->p
14b84 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 gno));. }.
14b85 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3c 30 }else if( k<0
14b86 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
14b87 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 QLITE_CORRUPT;.
14b88 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
14b89 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77 6c 79 /* Add the newly
14b8a 20 66 72 65 65 64 20 70 61 67 65 20 61 73 20 61 freed page as a
14b8b 20 6c 65 61 66 20 6f 6e 20 74 68 65 20 63 75 72 leaf on the cur
14b8c 72 65 6e 74 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 rent trunk */.
14b8d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
14b8e 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
14b8f 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
14b90 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
14b91 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
14b92 70 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b put4byte(&pTrunk
14b93 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2b 31 29 ->aData[4], k+1)
14b94 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 ;. put4by
14b95 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 te(&pTrunk->aDat
14b96 61 5b 38 2b 6b 2a 34 5d 2c 20 70 50 61 67 65 2d a[8+k*4], pPage-
14b97 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64 65 66 20 >pgno);.#ifndef
14b98 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 SQLITE_SECURE_DE
14b99 4c 45 54 45 0a 20 20 20 20 20 20 20 20 72 63 20 LETE. rc
14b9a 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f = sqlite3PagerDo
14b9b 6e 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 ntWrite(pPage->p
14b9c 44 62 50 61 67 65 29 3b 0a 23 65 6e 64 69 66 0a DbPage);.#endif.
14b9d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 54 52 }. TR
14b9e 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a ACE(("FREE-PAGE:
14b9f 20 25 64 20 6c 65 61 66 20 6f 6e 20 74 72 75 6e %d leaf on trun
14ba0 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c 70 50 61 k page %d\n",pPa
14ba1 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d ge->pgno,pTrunk-
14ba2 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 >pgno));. }.
14ba3 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
14ba4 54 72 75 6e 6b 29 3b 0a 20 20 7d 0a 20 20 72 65 Trunk);. }. re
14ba5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
14ba6 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 * Free any overf
14ba7 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 low pages associ
14ba8 61 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 ated with the gi
14ba9 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 ven Cell..*/.sta
14baa 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c tic int clearCel
14bab 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 l(MemPage *pPage
14bac 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 , unsigned char
14bad 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 *pCell){. BtSha
14bae 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
14baf 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 ->pBt;. CellInf
14bb0 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f o info;. Pgno o
14bb1 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 vflPgno;. int r
14bb2 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a c;. int nOvfl;.
14bb3 20 20 69 6e 74 20 6f 76 66 6c 50 61 67 65 53 69 int ovflPageSi
14bb4 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ze;.. assert( s
14bb5 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14bb6 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
14bb7 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
14bb8 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 3BtreeParseCellP
14bb9 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
14bba 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69 &info);. if( i
14bbb 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 nfo.iOverflow==0
14bbc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
14bbd 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f QLITE_OK; /* No
14bbe 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
14bbf 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 Return without
14bc0 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a doing anything *
14bc1 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f /. }. ovflPgno
14bc2 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 = get4byte(&pCe
14bc3 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
14bc4 77 5d 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65 53 w]);. ovflPageS
14bc5 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c ize = pBt->usabl
14bc6 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 eSize - 4;. nOv
14bc7 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79 6c fl = (info.nPayl
14bc8 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 oad - info.nLoca
14bc9 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 l + ovflPageSize
14bca 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53 69 - 1)/ovflPageSi
14bcb 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 76 ze;. assert( ov
14bcc 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76 flPgno==0 || nOv
14bcd 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 fl>0 );. while(
14bce 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 nOvfl-- ){.
14bcf 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 3b 0a MemPage *pOvfl;.
14bd0 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f if( ovflPgno
14bd1 3d 3d 30 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e ==0 || ovflPgno>
14bd2 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
14bd3 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 Bt) ){. ret
14bd4 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
14bd5 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a PT_BKPT;. }..
14bd6 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72 rc = getOver
14bd7 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6f 76 flowPage(pBt, ov
14bd8 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20 flPgno, &pOvfl,
14bd9 28 6e 4f 76 66 6c 3d 3d 30 29 3f 30 3a 26 6f 76 (nOvfl==0)?0:&ov
14bda 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 flPgno);. if(
14bdb 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
14bdc 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 . rc = freePa
14bdd 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20 73 ge(pOvfl);. s
14bde 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
14bdf 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29 (pOvfl->pDbPage)
14be0 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
14be1 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
14be2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
14be3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 ;.}../*.** Creat
14be4 65 20 74 68 65 20 62 79 74 65 20 73 65 71 75 65 e the byte seque
14be5 6e 63 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 nce used to repr
14be6 65 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 esent a cell on
14be7 70 61 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e page pPage.** an
14be8 64 20 77 72 69 74 65 20 74 68 61 74 20 62 79 74 d write that byt
14be9 65 20 73 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 e sequence into
14bea 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c pCell[]. Overfl
14beb 6f 77 20 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 ow pages are.**
14bec 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 69 allocated and fi
14bed 6c 6c 65 64 20 69 6e 20 61 73 20 6e 65 63 65 73 lled in as neces
14bee 73 61 72 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 sary. The calli
14bef 6e 67 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 ng procedure.**
14bf0 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
14bf1 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 or making sure s
14bf2 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 ufficient space
14bf3 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 has been allocat
14bf4 65 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b ed.** for pCell[
14bf5 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 ]..**.** Note th
14bf6 61 74 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f at pCell does no
14bf7 74 20 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64 t necessary need
14bf8 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 to point to the
14bf9 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a pPage->aData.**
14bfa 20 61 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 area. pCell mi
14bfb 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d ght point to som
14bfc 65 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 e temporary stor
14bfd 61 67 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 age. The cell w
14bfe 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 ill.** be constr
14bff 75 63 74 65 64 20 69 6e 20 74 68 69 73 20 74 65 ucted in this te
14c00 6d 70 6f 72 61 72 79 20 61 72 65 61 20 74 68 65 mporary area the
14c01 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 n copied into pP
14c02 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 age->aData.** la
14c03 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ter..*/.static i
14c04 6e 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 nt fillInCell(.
14c05 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c MemPage *pPage,
14c06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14c07 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 /* The page that
14c08 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 contains the ce
14c09 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ll */. unsigned
14c0a 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 char *pCell,
14c0b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 /* Comple
14c0c 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 te text of the c
14c0d 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ell */. const v
14c0e 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e oid *pKey, i64 n
14c0f 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b Key, /* The k
14c10 65 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f ey */. const vo
14c11 69 64 20 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 id *pData,int nD
14c12 61 74 61 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 ata, /* The da
14c13 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 ta */. int nZer
14c14 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o,
14c15 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 /* Extra
14c16 7a 65 72 6f 20 62 79 74 65 73 20 74 6f 20 61 70 zero bytes to ap
14c17 70 65 6e 64 20 74 6f 20 70 44 61 74 61 20 2a 2f pend to pData */
14c18 0a 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 . int *pnSize
14c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14c1a 20 20 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 /* Write cell
14c1b 73 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a size here */.){.
14c1c 20 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a int nPayload;.
14c1d 20 20 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 const u8 *pSrc
14c1e 3b 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c ;. int nSrc, n,
14c1f 20 72 63 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 rc;. int space
14c20 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 Left;. MemPage
14c21 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 *pOvfl = 0;. Me
14c22 6d 50 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 mPage *pToReleas
14c23 65 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 e = 0;. unsigne
14c24 64 20 63 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a d char *pPrior;.
14c25 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
14c26 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 *pPayload;. BtS
14c27 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 hared *pBt = pPa
14c28 67 65 2d 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 ge->pBt;. Pgno
14c29 70 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 pgnoOvfl = 0;.
14c2a 69 6e 74 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 int nHeader;. C
14c2b 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 ellInfo info;..
14c2c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
14c2d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
14c2e 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
14c2f 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 ;.. /* Fill in
14c30 74 68 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 the header. */.
14c31 20 6e 48 65 61 64 65 72 20 3d 20 30 3b 0a 20 20 nHeader = 0;.
14c32 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
14c33 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 ){. nHeader
14c34 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20 += 4;. }. if(
14c35 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 pPage->hasData )
14c36 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d {. nHeader +=
14c37 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c putVarint(&pCel
14c38 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e 44 61 74 l[nHeader], nDat
14c39 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73 a+nZero);. }els
14c3a 65 7b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 6e e{. nData = n
14c3b 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Zero = 0;. }.
14c3c 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 nHeader += putVa
14c3d 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 rint(&pCell[nHea
14c3e 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b der], *(u64*)&nK
14c3f 65 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ey);. sqlite3Bt
14c40 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
14c41 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 pPage, pCell, &i
14c42 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 nfo);. assert(
14c43 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 info.nHeader==nH
14c44 65 61 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 eader );. asser
14c45 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b t( info.nKey==nK
14c46 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ey );. assert(
14c47 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 info.nData==(u32
14c48 29 28 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 )(nData+nZero) )
14c49 3b 0a 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 ;. . /* Fill i
14c4a 6e 20 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f n the payload */
14c4b 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 . nPayload = nD
14c4c 61 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 ata + nZero;. i
14c4d 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 f( pPage->intKey
14c4e 20 29 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 ){. pSrc = p
14c4f 44 61 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d Data;. nSrc =
14c50 20 6e 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 nData;. nDat
14c51 61 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a a = 0;. }else{.
14c52 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b 3d 20 nPayload +=
14c53 6e 4b 65 79 3b 0a 20 20 20 20 70 53 72 63 20 3d nKey;. pSrc =
14c54 20 70 4b 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 pKey;. nSrc
14c55 3d 20 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 = nKey;. }. *p
14c56 6e 53 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 nSize = info.nSi
14c57 7a 65 3b 0a 20 20 73 70 61 63 65 4c 65 66 74 20 ze;. spaceLeft
14c58 3d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 = info.nLocal;.
14c59 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 pPayload = &pCe
14c5a 6c 6c 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 ll[nHeader];. p
14c5b 50 72 69 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 Prior = &pCell[i
14c5c 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a nfo.iOverflow];.
14c5d 0a 20 20 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f . while( nPaylo
14c5e 61 64 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 ad>0 ){. if(
14c5f 73 70 61 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a spaceLeft==0 ){.
14c60 20 20 20 20 20 20 69 6e 74 20 69 73 45 78 61 63 int isExac
14c61 74 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 t = 0;.#ifndef S
14c62 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
14c63 41 43 55 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f ACUUM. Pgno
14c64 20 70 67 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 pgnoPtrmap = pg
14c65 6e 6f 4f 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 noOvfl; /* Overf
14c66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 low page pointer
14c67 2d 6d 61 70 20 65 6e 74 72 79 20 70 61 67 65 20 -map entry page
14c68 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 */. if( pBt
14c69 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
14c6a 20 20 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 do{.
14c6b 20 20 20 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b pgnoOvfl++
14c6c 3b 0a 20 20 20 20 20 20 20 20 7d 20 77 68 69 6c ;. } whil
14c6d 65 28 20 0a 20 20 20 20 20 20 20 20 20 20 50 54 e( . PT
14c6e 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c RMAP_ISPAGE(pBt,
14c6f 20 70 67 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 pgnoOvfl) || pg
14c70 6e 6f 4f 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f noOvfl==PENDING_
14c71 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 0a BYTE_PAGE(pBt) .
14c72 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 );.
14c73 20 20 20 69 66 28 20 70 67 6e 6f 4f 76 66 6c 3e if( pgnoOvfl>
14c74 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 1 ){. /
14c75 2a 20 69 73 45 78 61 63 74 20 3d 20 31 3b 20 2a * isExact = 1; *
14c76 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 /. }.
14c77 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
14c78 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 rc = allocateBt
14c79 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4f reePage(pBt, &pO
14c7a 76 66 6c 2c 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 vfl, &pgnoOvfl,
14c7b 70 67 6e 6f 4f 76 66 6c 2c 20 69 73 45 78 61 63 pgnoOvfl, isExac
14c7c 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 t);.#ifndef SQLI
14c7d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
14c7e 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 UM. /* If t
14c7f 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 he database supp
14c80 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d orts auto-vacuum
14c81 2c 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 , and the second
14c82 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 or subsequent.
14c83 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 ** overflow
14c84 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 page is being a
14c85 6c 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e llocated, add an
14c86 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f entry to the po
14c87 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 inter-map.
14c88 2a 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 ** for that page
14c89 20 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a now. . **.
14c8a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 ** If this
14c8b 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 is the first ov
14c8c 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 erflow page, the
14c8d 6e 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61 n write a partia
14c8e 6c 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a l entry . *
14c8f 2a 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 * to the pointer
14c90 2d 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 -map. If we writ
14c91 65 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 e nothing to thi
14c92 73 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c s pointer-map sl
14c93 6f 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 ot,. ** the
14c94 6e 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 n the optimistic
14c95 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 overflow chain
14c96 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c processing in cl
14c97 65 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 earCell().
14c98 2a 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 ** may misinterp
14c99 72 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 ret the uninitia
14c9a 6c 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 lised values and
14c9b 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 delete the.
14c9c 20 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 ** wrong pages
14c9d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
14c9e 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 se.. */.
14c9f 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f if( pBt->auto
14ca0 56 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 Vacuum && rc==SQ
14ca1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
14ca2 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 u8 eType = (p
14ca3 67 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 gnoPtrmap?PTRMAP
14ca4 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 _OVERFLOW2:PTRMA
14ca5 50 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 P_OVERFLOW1);.
14ca6 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
14ca7 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 pPut(pBt, pgnoOv
14ca8 66 6c 2c 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 fl, eType, pgnoP
14ca9 74 72 6d 61 70 29 3b 0a 20 20 20 20 20 20 20 20 trmap);.
14caa 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
14cab 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
14cac 70 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 pOvfl);.
14cad 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
14cae 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b . if( rc ){
14caf 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
14cb0 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 Page(pToRelease)
14cb1 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
14cb2 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
14cb3 20 20 20 70 75 74 34 62 79 74 65 28 70 50 72 69 put4byte(pPri
14cb4 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 or, pgnoOvfl);.
14cb5 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
14cb6 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 (pToRelease);.
14cb7 20 20 20 20 70 54 6f 52 65 6c 65 61 73 65 20 3d pToRelease =
14cb8 20 70 4f 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 pOvfl;. pP
14cb9 72 69 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 rior = pOvfl->aD
14cba 61 74 61 3b 0a 20 20 20 20 20 20 70 75 74 34 62 ata;. put4b
14cbb 79 74 65 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a yte(pPrior, 0);.
14cbc 20 20 20 20 20 20 70 50 61 79 6c 6f 61 64 20 3d pPayload =
14cbd 20 26 70 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 &pOvfl->aData[4
14cbe 5d 3b 0a 20 20 20 20 20 20 73 70 61 63 65 4c 65 ];. spaceLe
14cbf 66 74 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 ft = pBt->usable
14cc0 53 69 7a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a Size - 4;. }.
14cc1 20 20 20 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 n = nPayload
14cc2 3b 0a 20 20 20 20 69 66 28 20 6e 3e 73 70 61 63 ;. if( n>spac
14cc3 65 4c 65 66 74 20 29 20 6e 20 3d 20 73 70 61 63 eLeft ) n = spac
14cc4 65 4c 65 66 74 3b 0a 20 20 20 20 69 66 28 20 6e eLeft;. if( n
14cc5 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 Src>0 ){. i
14cc6 66 28 20 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 f( n>nSrc ) n =
14cc7 6e 53 72 63 3b 0a 20 20 20 20 20 20 61 73 73 65 nSrc;. asse
14cc8 72 74 28 20 70 53 72 63 20 29 3b 0a 20 20 20 20 rt( pSrc );.
14cc9 20 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 memcpy(pPayloa
14cca 64 2c 20 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20 d, pSrc, n);.
14ccb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 }else{. me
14ccc 6d 73 65 74 28 70 50 61 79 6c 6f 61 64 2c 20 30 mset(pPayload, 0
14ccd 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , n);. }.
14cce 6e 50 61 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 nPayload -= n;.
14ccf 20 20 20 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e pPayload += n
14cd0 3b 0a 20 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b ;. pSrc += n;
14cd1 0a 20 20 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a . nSrc -= n;.
14cd2 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 2d 3d spaceLeft -=
14cd3 20 6e 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63 n;. if( nSrc
14cd4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 72 ==0 ){. nSr
14cd5 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 20 c = nData;.
14cd6 20 70 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 pSrc = pData;.
14cd7 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 }. }. relea
14cd8 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 sePage(pToReleas
14cd9 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c e);. return SQL
14cda 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
14cdb 20 52 65 6d 6f 76 65 20 74 68 65 20 69 2d 74 68 Remove the i-th
14cdc 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 cell from pPage
14cdd 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
14cde 65 66 66 65 63 74 73 20 70 50 61 67 65 20 6f 6e effects pPage on
14cdf 6c 79 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 ly..** The cell
14ce0 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 66 content is not f
14ce1 72 65 65 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61 reed or dealloca
14ce2 74 65 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 ted. It is assu
14ce3 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 med that.** the
14ce4 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 cell content has
14ce5 20 62 65 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d been copied som
14ce6 65 70 6c 61 63 65 20 65 6c 73 65 2e 20 20 54 68 eplace else. Th
14ce7 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a is routine just.
14ce8 2a 2a 20 72 65 6d 6f 76 65 73 20 74 68 65 20 72 ** removes the r
14ce9 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 eference to the
14cea 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e cell from pPage.
14ceb 0a 2a 2a 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74 .**.** "sz" must
14cec 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f be the number o
14ced 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 f bytes in the c
14cee 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ell..*/.static i
14cef 6e 74 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 nt dropCell(MemP
14cf0 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 age *pPage, int
14cf1 69 64 78 2c 20 69 6e 74 20 73 7a 29 7b 0a 20 20 idx, int sz){.
14cf2 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
14cf3 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
14cf4 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 */. int pc;
14cf5 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
14cf6 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f o cell content o
14cf7 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c f cell being del
14cf8 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61 eted */. u8 *da
14cf9 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 70 50 61 ta; /* pPa
14cfa 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75 ge->aData */. u
14cfb 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 2f 8 *ptr; /
14cfc 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 65 20 62 * Used to move b
14cfd 79 74 65 73 20 61 72 6f 75 6e 64 20 77 69 74 68 ytes around with
14cfe 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 in data[] */. i
14cff 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f nt rc; /
14d00 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 * The return cod
14d01 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
14d02 69 64 78 3e 3d 30 20 26 26 20 69 64 78 3c 70 50 idx>=0 && idx<pP
14d03 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
14d04 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c assert( sz==cell
14d05 53 69 7a 65 28 70 50 61 67 65 2c 20 69 64 78 29 Size(pPage, idx)
14d06 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
14d07 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
14d08 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
14d09 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
14d0a 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14d0b 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
14d0c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 61 ->mutex) );. da
14d0d 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
14d0e 61 3b 0a 20 20 70 74 72 20 3d 20 26 64 61 74 61 a;. ptr = &data
14d0f 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 [pPage->cellOffs
14d10 65 74 20 2b 20 32 2a 69 64 78 5d 3b 0a 20 20 70 et + 2*idx];. p
14d11 63 20 3d 20 67 65 74 32 62 79 74 65 28 70 74 72 c = get2byte(ptr
14d12 29 3b 0a 20 20 69 66 20 28 20 28 70 63 3c 70 50 );. if ( (pc<pP
14d13 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 age->hdrOffset+6
14d14 2b 28 70 50 61 67 65 2d 3e 6c 65 61 66 3f 30 3a +(pPage->leaf?0:
14d15 34 29 29 20 7c 7c 20 28 70 63 2b 73 7a 3e 70 50 4)) || (pc+sz>pP
14d16 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
14d17 53 69 7a 65 29 20 29 20 7b 0a 20 20 20 20 72 65 Size) ) {. re
14d18 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
14d19 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 UPT_BKPT;. }.
14d1a 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70 rc = freeSpace(p
14d1b 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 Page, pc, sz);.
14d1c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
14d1d 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
14d1e 20 72 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 rc;. }. for(i
14d1f 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 2d =idx+1; i<pPage-
14d20 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 72 >nCell; i++, ptr
14d21 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 5d +=2){. ptr[0]
14d22 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 70 = ptr[2];. p
14d23 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b 0a tr[1] = ptr[3];.
14d24 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 }. pPage->nCe
14d25 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 65 ll--;. put2byte
14d26 28 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 (&data[pPage->hd
14d27 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 67 rOffset+3], pPag
14d28 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61 e->nCell);. pPa
14d29 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a ge->nFree += 2;.
14d2a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
14d2b 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 OK;.}../*.** Ins
14d2c 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 6f ert a new cell o
14d2d 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c 20 n pPage at cell
14d2e 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 6c index "i". pCel
14d2f 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a l points to the.
14d30 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 ** content of th
14d31 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 e cell..**.** If
14d32 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
14d33 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68 t will fit on th
14d34 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 74 e page, then put
14d35 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 69 it there. If i
14d36 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 69 t.** will not fi
14d37 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 63 t, then make a c
14d38 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 opy of the cell
14d39 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 65 content into pTe
14d3a 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 69 mp if.** pTemp i
14d3b 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 67 s not null. Reg
14d3c 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d 70 ardless of pTemp
14d3d 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 , allocate a new
14d3e 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 61 entry.** in pPa
14d3f 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 ge->aOvfl[] and
14d40 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 6f make it point to
14d41 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
14d42 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e 20 t (either.** in
14d43 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 69 pTemp or the ori
14d44 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e 64 ginal pCell) and
14d45 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 73 also record its
14d46 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f index. .** Allo
14d47 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 cating a new ent
14d48 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 65 ry in pPage->aCe
14d49 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 61 ll[] implies tha
14d4a 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 t .** pPage->nOv
14d4b 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 6d erflow is increm
14d4c 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ented..**.** If
14d4d 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 nSkip is non-zer
14d4e 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 o, then do not c
14d4f 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 6e 53 opy the first nS
14d50 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 65 kip bytes of the
14d51 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 61 .** cell. The ca
14d52 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 72 ller will overwr
14d53 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 74 ite them after t
14d54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
14d55 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 urns. If.** nSki
14d56 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 p is non-zero, t
14d57 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e 6f hen pCell may no
14d58 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 6e t point to an in
14d59 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 valid memory loc
14d5a 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 70 ation .** (but p
14d5b 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 6c Cell+nSkip is al
14d5c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a ways valid)..*/.
14d5d 73 74 61 74 69 63 20 69 6e 74 20 69 6e 73 65 72 static int inser
14d5e 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 tCell(. MemPage
14d5f 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50 61 *pPage, /* Pa
14d60 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 ge into which we
14d61 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f 0a are copying */.
14d62 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 int i,
14d63 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 /* New cell
14d64 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74 68 becomes the i-th
14d65 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 cell of the pag
14d66 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c e */. u8 *pCell
14d67 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 , /* Cont
14d68 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 63 ent of the new c
14d69 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c ell */. int sz,
14d6a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
14d6b 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 tes of content i
14d6c 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 n pCell */. u8
14d6d 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f *pTemp, /
14d6e 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 73 * Temp storage s
14d6f 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20 pace for pCell,
14d70 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 75 if needed */. u
14d71 38 20 6e 53 6b 69 70 20 20 20 20 20 20 20 20 20 8 nSkip
14d72 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 /* Do not write
14d73 20 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 the first nSkip
14d74 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 63 65 bytes of the ce
14d75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 ll */.){. int i
14d76 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 dx; /*
14d77 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 6e Where to write n
14d78 65 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 ew cell content
14d79 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 in data[] */. i
14d7a 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 nt j;
14d7b 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
14d7c 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 */. int top;
14d7d 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
14d7e 20 62 79 74 65 20 6f 66 20 63 6f 6e 74 65 6e 74 byte of content
14d7f 20 66 6f 72 20 61 6e 79 20 63 65 6c 6c 20 69 6e for any cell in
14d80 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 data[] */. int
14d81 20 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f end; /
14d82 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 * First byte pas
14d83 74 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 t the last cell
14d84 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b pointer in data[
14d85 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 ] */. int ins;
14d86 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
14d87 78 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 x in data[] wher
14d88 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 e new cell point
14d89 65 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a er is inserted *
14d8a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 /. int hdr;
14d8b 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
14d8c 69 6e 74 6f 20 64 61 74 61 5b 5d 20 6f 66 20 74 into data[] of t
14d8d 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a he page header *
14d8e 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 /. int cellOffs
14d8f 65 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 et; /* Address
14d90 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 of first cell p
14d91 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d ointer in data[]
14d92 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 */. u8 *data;
14d93 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
14d94 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 ontent of the wh
14d95 6f 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 ole page */. u8
14d96 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 *ptr;
14d97 2f 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 /* Used for movi
14d98 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ng information a
14d99 72 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 round in data[]
14d9a 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e */.. assert( i>
14d9b 3d 30 20 26 26 20 69 3c 3d 70 50 61 67 65 2d 3e =0 && i<=pPage->
14d9c 6e 43 65 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 nCell+pPage->nOv
14d9d 65 72 66 6c 6f 77 20 29 3b 0a 20 20 61 73 73 65 erflow );. asse
14d9e 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 rt( sz==cellSize
14d9f 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
14da0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
14da1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14da2 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
14da3 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 50 tex) );. if( pP
14da4 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c age->nOverflow |
14da5 7c 20 73 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 | sz+2>pPage->nF
14da6 72 65 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ree ){. if( p
14da7 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 6d 65 Temp ){. me
14da8 6d 63 70 79 28 70 54 65 6d 70 2b 6e 53 6b 69 70 mcpy(pTemp+nSkip
14da9 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 , pCell+nSkip, s
14daa 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 20 20 z-nSkip);.
14dab 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 pCell = pTemp;.
14dac 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 }. j = pPa
14dad 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b ge->nOverflow++;
14dae 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 28 . assert( j<(
14daf 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 61 67 int)(sizeof(pPag
14db0 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 e->aOvfl)/sizeof
14db1 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d (pPage->aOvfl[0]
14db2 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d )) );. pPage-
14db3 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 >aOvfl[j].pCell
14db4 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 = pCell;. pPa
14db5 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 ge->aOvfl[j].idx
14db6 20 3d 20 69 3b 0a 20 20 20 20 70 50 61 67 65 2d = i;. pPage-
14db7 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 7d 65 >nFree = 0;. }e
14db8 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 lse{. int rc
14db9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
14dba 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
14dbb 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
14dbc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
14dbd 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
14dbe 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
14dbf 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
14dc0 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
14dc1 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 pDbPage) );.
14dc2 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
14dc3 61 74 61 3b 0a 20 20 20 20 68 64 72 20 3d 20 70 ata;. hdr = p
14dc4 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
14dc5 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 . top = get2b
14dc6 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
14dc7 29 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 );. cellOffse
14dc8 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f t = pPage->cellO
14dc9 66 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d ffset;. end =
14dca 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a cellOffset + 2*
14dcb 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 2b 20 32 pPage->nCell + 2
14dcc 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63 65 6c 6c ;. ins = cell
14dcd 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b 0a 20 20 Offset + 2*i;.
14dce 20 20 69 66 28 20 65 6e 64 20 3e 20 74 6f 70 20 if( end > top
14dcf 2d 20 73 7a 20 29 7b 0a 20 20 20 20 20 20 72 63 - sz ){. rc
14dd0 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 = defragmentPag
14dd1 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
14dd2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
14dd3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 K ){. ret
14dd4 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
14dd5 20 20 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 top = get2
14dd6 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
14dd7 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ]);. assert
14dd8 28 20 65 6e 64 20 2b 20 73 7a 20 3c 3d 20 74 6f ( end + sz <= to
14dd9 70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 p );. }. i
14dda 64 78 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61 dx = allocateSpa
14ddb 63 65 28 70 50 61 67 65 2c 20 73 7a 29 3b 0a 20 ce(pPage, sz);.
14ddc 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 30 assert( idx>0
14ddd 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
14dde 65 6e 64 20 3c 3d 20 67 65 74 32 62 79 74 65 28 end <= get2byte(
14ddf 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 29 3b &data[hdr+5]) );
14de0 0a 20 20 20 20 69 66 20 28 69 64 78 2b 73 7a 20 . if (idx+sz
14de1 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 > pPage->pBt->us
14de2 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20 20 20 20 ableSize) {.
14de3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
14de4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
14de5 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e }. pPage->n
14de6 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 61 67 Cell++;. pPag
14de7 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 3b 0a 20 e->nFree -= 2;.
14de8 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b memcpy(&data[
14de9 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65 6c idx+nSkip], pCel
14dea 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 l+nSkip, sz-nSki
14deb 70 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e p);. for(j=en
14dec 64 2d 32 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a d-2, ptr=&data[j
14ded 5d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 ]; j>ins; j-=2,
14dee 70 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 ptr-=2){. p
14def 74 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b tr[0] = ptr[-2];
14df0 0a 20 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 . ptr[1] =
14df1 70 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 ptr[-1];. }.
14df2 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 put2byte(&dat
14df3 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 a[ins], idx);.
14df4 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
14df5 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d 3e [hdr+3], pPage->
14df6 6e 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 nCell);.#ifndef
14df7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
14df8 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 VACUUM. if( p
14df9 50 61 67 65 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 Page->pBt->autoV
14dfa 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 2f acuum ){. /
14dfb 2a 20 54 68 65 20 63 65 6c 6c 20 6d 61 79 20 63 * The cell may c
14dfc 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72 ontain a pointer
14dfd 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 to an overflow
14dfe 70 61 67 65 2e 20 49 66 20 73 6f 2c 20 77 72 69 page. If so, wri
14dff 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 te. ** the
14e00 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 entry for the ov
14e01 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 74 6f erflow page into
14e02 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
14e03 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
14e04 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
14e05 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
14e06 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
14e07 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
14e08 66 6f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 fo);. asser
14e09 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 t( (info.nData+(
14e0a 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a pPage->intKey?0:
14e0b 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 info.nKey))==inf
14e0c 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 o.nPayload );.
14e0d 20 20 20 20 69 66 28 20 28 69 6e 66 6f 2e 6e 44 if( (info.nD
14e0e 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b ata+(pPage->intK
14e0f 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 ey?0:info.nKey))
14e10 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a >info.nLocal ){.
14e11 20 20 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e Pgno pgn
14e12 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 oOvfl = get4byte
14e13 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 (&pCell[info.iOv
14e14 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20 erflow]);.
14e15 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 rc = ptrmapPut
14e16 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 67 6e (pPage->pBt, pgn
14e17 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 oOvfl, PTRMAP_OV
14e18 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e ERFLOW1, pPage->
14e19 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 pgno);. i
14e1a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
14e1b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
14e1c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 }. }.#end
14e1d 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e if. }.. return
14e1e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
14e1f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 73 74 20 *.** Add a list
14e20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 20 70 61 of cells to a pa
14e21 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 73 68 ge. The page sh
14e22 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 6c ould be initiall
14e23 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 65 20 y empty..** The
14e24 63 65 6c 6c 73 20 61 72 65 20 67 75 61 72 61 6e cells are guaran
14e25 74 65 65 64 20 74 6f 20 66 69 74 20 6f 6e 20 74 teed to fit on t
14e26 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 he page..*/.stat
14e27 69 63 20 76 6f 69 64 20 61 73 73 65 6d 62 6c 65 ic void assemble
14e28 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 65 20 Page(. MemPage
14e29 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 54 68 65 *pPage, /* The
14e2a 20 70 61 67 65 20 74 6f 20 62 65 20 61 73 73 65 page to be asse
14e2b 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 6e 74 20 mblied */. int
14e2c 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a nCell, /*
14e2d 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 The number of c
14e2e 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 6f 20 74 ells to add to t
14e2f 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 his page */. u8
14e30 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20 **apCell,
14e31 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 63 /* Pointers to c
14e32 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f 0a 20 20 ell bodies */.
14e33 75 31 36 20 2a 61 53 69 7a 65 20 20 20 20 20 20 u16 *aSize
14e34 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 74 68 /* Sizes of th
14e35 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a 20 20 e cells */.){.
14e36 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
14e37 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
14e38 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 74 61 6c r */. int total
14e39 53 69 7a 65 3b 20 20 20 20 2f 2a 20 54 6f 74 61 Size; /* Tota
14e3a 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 l size of all ce
14e3b 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 lls */. int hdr
14e3c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e ; /* In
14e3d 64 65 78 20 6f 66 20 70 61 67 65 20 68 65 61 64 dex of page head
14e3e 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c er */. int cell
14e3f 70 74 72 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 ptr; /* Add
14e40 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c ress of next cel
14e41 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 l pointer */. i
14e42 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 20 20 nt cellbody;
14e43 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e /* Address of n
14e44 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 2a 2f ext cell body */
14e45 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 . u8 *data;
14e46 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 /* Data for
14e47 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20 the page */..
14e48 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
14e49 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 Overflow==0 );.
14e4a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
14e4b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
14e4c 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
14e4d 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 3d 20 ;. totalSize =
14e4e 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 0;. for(i=0; i<
14e4f 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 nCell; i++){.
14e50 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d 20 61 53 totalSize += aS
14e51 69 7a 65 5b 69 5d 3b 0a 20 20 7d 0a 20 20 61 73 ize[i];. }. as
14e52 73 65 72 74 28 20 74 6f 74 61 6c 53 69 7a 65 2b sert( totalSize+
14e53 32 2a 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 2*nCell<=pPage->
14e54 6e 46 72 65 65 20 29 3b 0a 20 20 61 73 73 65 72 nFree );. asser
14e55 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d t( pPage->nCell=
14e56 3d 30 20 29 3b 0a 20 20 63 65 6c 6c 70 74 72 20 =0 );. cellptr
14e57 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 = pPage->cellOff
14e58 73 65 74 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 set;. data = pP
14e59 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 age->aData;. hd
14e5a 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
14e5b 66 73 65 74 3b 0a 20 20 70 75 74 32 62 79 74 65 fset;. put2byte
14e5c 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e (&data[hdr+3], n
14e5d 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 6e 43 65 Cell);. if( nCe
14e5e 6c 6c 20 29 7b 0a 20 20 20 20 63 65 6c 6c 62 6f ll ){. cellbo
14e5f 64 79 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61 dy = allocateSpa
14e60 63 65 28 70 50 61 67 65 2c 20 74 6f 74 61 6c 53 ce(pPage, totalS
14e61 69 7a 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ize);. assert
14e62 28 20 63 65 6c 6c 62 6f 64 79 3e 30 20 29 3b 0a ( cellbody>0 );.
14e63 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
14e64 65 2d 3e 6e 46 72 65 65 20 3e 3d 20 32 2a 6e 43 e->nFree >= 2*nC
14e65 65 6c 6c 20 29 3b 0a 20 20 20 20 70 50 61 67 65 ell );. pPage
14e66 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 2a 6e 43 65 ->nFree -= 2*nCe
14e67 6c 6c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ll;. for(i=0;
14e68 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a i<nCell; i++){.
14e69 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 put2byte(&
14e6a 64 61 74 61 5b 63 65 6c 6c 70 74 72 5d 2c 20 63 data[cellptr], c
14e6b 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 20 20 20 20 ellbody);.
14e6c 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 65 6c memcpy(&data[cel
14e6d 6c 62 6f 64 79 5d 2c 20 61 70 43 65 6c 6c 5b 69 lbody], apCell[i
14e6e 5d 2c 20 61 53 69 7a 65 5b 69 5d 29 3b 0a 20 20 ], aSize[i]);.
14e6f 20 20 20 20 63 65 6c 6c 70 74 72 20 2b 3d 20 32 cellptr += 2
14e70 3b 0a 20 20 20 20 20 20 63 65 6c 6c 62 6f 64 79 ;. cellbody
14e71 20 2b 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 += aSize[i];.
14e72 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
14e73 63 65 6c 6c 62 6f 64 79 3d 3d 70 50 61 67 65 2d cellbody==pPage-
14e74 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
14e75 20 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d );. }. pPage-
14e76 3e 6e 43 65 6c 6c 20 3d 20 6e 43 65 6c 6c 3b 0a >nCell = nCell;.
14e77 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c }../*.** The fol
14e78 6c 6f 77 69 6e 67 20 70 61 72 61 6d 65 74 65 72 lowing parameter
14e79 73 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 s determine how
14e7a 6d 61 6e 79 20 61 64 6a 61 63 65 6e 74 20 70 61 many adjacent pa
14e7b 67 65 73 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 ges get involved
14e7c 0a 2a 2a 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 .** in a balanci
14e7d 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e ng operation. N
14e7e 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 N is the number
14e7f 6f 66 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 of neighbors on
14e80 65 69 74 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f either side.** o
14e81 66 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 f the page that
14e82 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 participate in t
14e83 68 65 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 he balancing ope
14e84 72 61 74 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74 ration. NB is t
14e85 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62 he.** total numb
14e86 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 er of pages that
14e87 20 70 61 72 74 69 63 69 70 61 74 65 2c 20 69 6e participate, in
14e88 63 6c 75 64 69 6e 67 20 74 68 65 20 74 61 72 67 cluding the targ
14e89 65 74 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e et page and.** N
14e8a 4e 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 N neighbors on e
14e8b 69 74 68 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a ither side..**.*
14e8c 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61 * The minimum va
14e8d 6c 75 65 20 6f 66 20 4e 4e 20 69 73 20 31 20 28 lue of NN is 1 (
14e8e 6f 66 20 63 6f 75 72 73 65 29 2e 20 20 49 6e 63 of course). Inc
14e8f 72 65 61 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65 reasing NN above
14e90 20 31 0a 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33 1.** (to 2 or 3
14e91 29 20 67 69 76 65 73 20 61 20 6d 6f 64 65 73 74 ) gives a modest
14e92 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 improvement in
14e93 53 45 4c 45 43 54 20 61 6e 64 20 44 45 4c 45 54 SELECT and DELET
14e94 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a E performance.**
14e95 20 69 6e 20 65 78 63 68 61 6e 67 65 20 66 6f 72 in exchange for
14e96 20 61 20 6c 61 72 67 65 72 20 64 65 67 72 61 64 a larger degrad
14e97 61 74 69 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20 ation in INSERT
14e98 61 6e 64 20 55 50 44 41 54 45 20 70 65 72 66 6f and UPDATE perfo
14e99 72 6d 61 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76 rmance..** The v
14e9a 61 6c 75 65 20 6f 66 20 4e 4e 20 61 70 70 65 61 alue of NN appea
14e9b 72 73 20 74 6f 20 67 69 76 65 20 74 68 65 20 62 rs to give the b
14e9c 65 73 74 20 72 65 73 75 6c 74 73 20 6f 76 65 72 est results over
14e9d 61 6c 6c 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 all..*/.#define
14e9e 4e 4e 20 31 20 20 20 20 20 20 20 20 20 20 20 20 NN 1
14e9f 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 /* Number of ne
14ea0 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 ighbors on eithe
14ea1 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 20 r side of pPage
14ea2 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 42 20 28 4e */.#define NB (N
14ea3 4e 2a 32 2b 31 29 20 20 20 20 20 20 2f 2a 20 54 N*2+1) /* T
14ea4 6f 74 61 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c otal pages invol
14ea5 76 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e ved in the balan
14ea6 63 65 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 ce */../* Forwar
14ea7 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 d reference */.s
14ea8 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 tatic int balanc
14ea9 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 e(BtCursor*, int
14eaa 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
14eab 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c TE_OMIT_QUICKBAL
14eac 41 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ANCE./*.** This
14ead 76 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e version of balan
14eae 63 65 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 ce() handles the
14eaf 20 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 common special
14eb0 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 case where.** a
14eb1 6e 65 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 new entry is bei
14eb2 6e 67 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 ng inserted on t
14eb3 68 65 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 he extreme right
14eb4 2d 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 -end of the.** t
14eb5 72 65 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f ree, in other wo
14eb6 72 64 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 rds, when the ne
14eb7 77 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 w entry will bec
14eb8 6f 6d 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a ome the largest.
14eb9 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 ** entry in the
14eba 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 tree..**.** Inst
14ebb 65 61 64 20 6f 66 20 74 72 79 69 6e 67 20 62 61 ead of trying ba
14ebc 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67 68 lance the 3 righ
14ebd 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67 65 t-most leaf page
14ebe 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20 61 s, just add.** a
14ebf 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68 65 new page to the
14ec0 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 right-hand side
14ec1 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e 65 and put the one
14ec2 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a 2a new entry in.**
14ec3 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68 69 that page. Thi
14ec4 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69 67 s leaves the rig
14ec5 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20 74 ht side of the t
14ec6 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a 20 ree somewhat.**
14ec7 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75 74 unbalanced. But
14ec8 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20 77 odds are that w
14ec9 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 e will be insert
14eca 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73 0a ing new entries.
14ecb 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73 6f ** at the end so
14ecc 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73 6f on afterwards so
14ecd 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70 74 the nearly empt
14ece 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69 63 y page will quic
14ecf 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e 20 kly.** fill up.
14ed0 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a 0a On average..**.
14ed1 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65 20 ** pPage is the
14ed2 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 leaf page which
14ed3 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 is the right-mos
14ed4 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74 72 t page in the tr
14ed5 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20 69 ee..** pParent i
14ed6 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 70 s its parent. p
14ed7 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 61 Page must have a
14ed8 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f 77 single overflow
14ed9 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68 20 entry.** which
14eda 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67 68 is also the righ
14edb 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e 20 t-most entry on
14edc 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 the page..*/.sta
14edd 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f tic int balance_
14ede 71 75 69 63 6b 28 42 74 43 75 72 73 6f 72 20 2a quick(BtCursor *
14edf 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b pCur){. int rc;
14ee0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 . MemPage *pNew
14ee1 20 3d 20 30 3b 0a 20 20 50 67 6e 6f 20 70 67 6e = 0;. Pgno pgn
14ee2 6f 4e 65 77 3b 0a 20 20 75 38 20 2a 70 43 65 6c oNew;. u8 *pCel
14ee3 6c 3b 0a 20 20 75 31 36 20 73 7a 43 65 6c 6c 3b l;. u16 szCell;
14ee4 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f . CellInfo info
14ee5 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
14ee6 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
14ee7 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
14ee8 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 MemPage *pPare
14ee9 6e 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 nt = pCur->apPag
14eea 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d e[pCur->iPage-1]
14eeb 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
14eec 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
14eed 20 20 69 6e 74 20 70 61 72 65 6e 74 49 64 78 20 int parentIdx
14eee 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c = pParent->nCell
14eef 3b 20 20 20 2f 2a 20 70 50 61 72 65 6e 74 20 6e ; /* pParent n
14ef0 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 ew divider cell
14ef1 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 70 index */. int p
14ef2 61 72 65 6e 74 53 69 7a 65 3b 20 20 20 20 20 20 arentSize;
14ef3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
14ef4 53 69 7a 65 20 6f 66 20 6e 65 77 20 64 69 76 69 Size of new divi
14ef5 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 der cell */. u8
14ef6 20 70 61 72 65 6e 74 43 65 6c 6c 5b 36 34 5d 3b parentCell[64];
14ef7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14ef8 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 74 68 65 /* Space for the
14ef9 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c new divider cel
14efa 6c 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 l */.. assert(
14efb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14efc 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
14efd 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 utex) );.. /* A
14efe 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 llocate a new pa
14eff 67 65 2e 20 49 6e 73 65 72 74 20 74 68 65 20 6f ge. Insert the o
14f00 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72 6f verflow cell fro
14f01 6d 20 70 50 61 67 65 0a 20 20 2a 2a 20 69 6e 74 m pPage. ** int
14f02 6f 20 69 74 2e 20 54 68 65 6e 20 72 65 6d 6f 76 o it. Then remov
14f03 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 e the overflow c
14f04 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a ell from pPage..
14f05 20 20 2a 2f 0a 20 20 72 63 20 3d 20 61 6c 6c 6f */. rc = allo
14f06 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
14f07 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e t, &pNew, &pgnoN
14f08 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 ew, 0, 0);. if(
14f09 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
14f0a 7b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 50 {. pCell = pP
14f0b 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 age->aOvfl[0].pC
14f0c 65 6c 6c 3b 0a 20 20 20 20 73 7a 43 65 6c 6c 20 ell;. szCell
14f0d 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 = cellSizePtr(pP
14f0e 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 age, pCell);.
14f0f 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 zeroPage(pNew,
14f10 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 29 pPage->aData[0])
14f11 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 ;. assemblePa
14f12 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 65 ge(pNew, 1, &pCe
14f13 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20 20 ll, &szCell);.
14f14 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c pPage->nOverfl
14f15 6f 77 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 2f ow = 0;. . /
14f16 2a 20 70 50 61 67 65 20 69 73 20 63 75 72 72 65 * pPage is curre
14f17 6e 74 6c 79 20 74 68 65 20 72 69 67 68 74 2d 63 ntly the right-c
14f18 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 2e hild of pParent.
14f19 20 43 68 61 6e 67 65 20 74 68 69 73 0a 20 20 20 Change this.
14f1a 20 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 ** so that the
14f1b 72 69 67 68 74 2d 63 68 69 6c 64 20 69 73 20 74 right-child is t
14f1c 68 65 20 6e 65 77 20 70 61 67 65 20 61 6c 6c 6f he new page allo
14f1d 63 61 74 65 64 20 61 62 6f 76 65 20 61 6e 64 0a cated above and.
14f1e 20 20 20 20 2a 2a 20 70 50 61 67 65 20 69 73 20 ** pPage is
14f1f 74 68 65 20 6e 65 78 74 2d 74 6f 2d 72 69 67 68 the next-to-righ
14f20 74 20 63 68 69 6c 64 2e 20 0a 20 20 20 20 2a 2a t child. . **
14f21 0a 20 20 20 20 2a 2a 20 49 67 6e 6f 72 65 20 74 . ** Ignore t
14f22 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
14f23 6f 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 66 of the call to f
14f24 69 6c 6c 49 6e 43 65 6c 6c 28 29 2e 20 66 69 6c illInCell(). fil
14f25 6c 49 6e 43 65 6c 6c 28 29 0a 20 20 20 20 2a 2a lInCell(). **
14f26 20 6d 61 79 20 6f 6e 6c 79 20 72 65 74 75 72 6e may only return
14f27 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 other than SQLI
14f28 54 45 5f 4f 4b 20 69 66 20 69 74 20 69 73 20 72 TE_OK if it is r
14f29 65 71 75 69 72 65 64 20 74 6f 20 61 6c 6c 6f 63 equired to alloc
14f2a 61 74 65 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6f ate. ** one o
14f2b 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 r more overflow
14f2c 70 61 67 65 73 2e 20 53 69 6e 63 65 20 61 6e 20 pages. Since an
14f2d 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 42 internal table B
14f2e 2d 54 72 65 65 20 63 65 6c 6c 20 0a 20 20 20 20 -Tree cell .
14f2f 2a 2a 20 6d 61 79 20 6e 65 76 65 72 20 73 70 69 ** may never spi
14f30 6c 6c 20 6f 76 65 72 20 6f 6e 74 6f 20 61 6e 20 ll over onto an
14f31 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 28 69 overflow page (i
14f32 74 20 69 73 20 61 20 6d 61 78 69 6d 75 6d 20 6f t is a maximum o
14f33 66 20 0a 20 20 20 20 2a 2a 20 31 33 20 62 79 74 f . ** 13 byt
14f34 65 73 20 69 6e 20 73 69 7a 65 29 2c 20 69 74 20 es in size), it
14f35 69 73 20 6e 6f 74 20 6e 65 63 63 65 73 73 61 72 is not neccessar
14f36 79 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72 y to check the r
14f37 65 74 75 72 6e 20 63 6f 64 65 2e 0a 20 20 20 20 eturn code..
14f38 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c 61 **. ** Simila
14f39 72 6c 79 2c 20 74 68 65 20 69 6e 73 65 72 74 43 rly, the insertC
14f3a 65 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 ell() function c
14f3b 61 6e 6e 6f 74 20 66 61 69 6c 20 69 66 20 74 68 annot fail if th
14f3c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 62 65 e page. ** be
14f3d 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 ing inserted int
14f3e 6f 20 69 73 20 61 6c 72 65 61 64 79 20 77 72 69 o is already wri
14f3f 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 63 65 table and the ce
14f40 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 0a 20 20 20 ll does not .
14f41 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 6e 20 6f ** contain an o
14f42 76 65 72 66 6c 6f 77 20 70 6f 69 6e 74 65 72 2e verflow pointer.
14f43 20 53 6f 20 69 67 6e 6f 72 65 20 74 68 69 73 20 So ignore this
14f44 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f 6f 2e return code too.
14f45 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
14f46 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c rt( pPage->nCell
14f47 3e 30 20 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20 >0 );. pCell
14f48 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
14f49 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 , pPage->nCell-1
14f4a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
14f4b 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
14f4c 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 pPage, pCell, &i
14f4d 6e 66 6f 29 3b 0a 20 20 20 20 66 69 6c 6c 49 6e nfo);. fillIn
14f4e 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 Cell(pParent, pa
14f4f 72 65 6e 74 43 65 6c 6c 2c 20 30 2c 20 69 6e 66 rentCell, 0, inf
14f50 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c 20 30 2c o.nKey, 0, 0, 0,
14f51 20 26 70 61 72 65 6e 74 53 69 7a 65 29 3b 0a 20 &parentSize);.
14f52 20 20 20 61 73 73 65 72 74 28 20 70 61 72 65 6e assert( paren
14f53 74 53 69 7a 65 3c 36 34 20 29 3b 0a 20 20 20 20 tSize<64 );.
14f54 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
14f55 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
14f56 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 pParent->pDbPage
14f57 29 20 29 3b 0a 20 20 20 20 69 6e 73 65 72 74 43 ) );. insertC
14f58 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 72 ell(pParent, par
14f59 65 6e 74 49 64 78 2c 20 70 61 72 65 6e 74 43 65 entIdx, parentCe
14f5a 6c 6c 2c 20 70 61 72 65 6e 74 53 69 7a 65 2c 20 ll, parentSize,
14f5b 30 2c 20 34 29 3b 0a 20 20 20 20 70 75 74 34 62 0, 4);. put4b
14f5c 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 yte(findOverflow
14f5d 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 70 61 72 Cell(pParent,par
14f5e 65 6e 74 49 64 78 29 2c 20 70 50 61 67 65 2d 3e entIdx), pPage->
14f5f 70 67 6e 6f 29 3b 0a 20 20 20 20 70 75 74 34 62 pgno);. put4b
14f60 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 yte(&pParent->aD
14f61 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 ata[pParent->hdr
14f62 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e Offset+8], pgnoN
14f63 65 77 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 ew);. . /* I
14f64 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 f this is an aut
14f65 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 o-vacuum databas
14f66 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f e, update the po
14f67 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a inter map. **
14f68 20 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f with entries fo
14f69 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20 r the new page,
14f6a 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 and any pointer
14f6b 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a from the . **
14f6c 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 cell on the pag
14f6d 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 e to an overflow
14f6e 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
14f6f 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
14f70 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d UM ){. rc =
14f71 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
14f72 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f pgnoNew, PTRMAP_
14f73 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e BTREE, pParent->
14f74 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 pgno);. if(
14f75 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
14f76 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 {. rc = p
14f77 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 trmapPutOvfl(pNe
14f78 77 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 w, 0);. }.
14f79 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c }.. /* Rel
14f7a 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e ease the referen
14f7b 63 65 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 ce to the new pa
14f7c 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65 61 ge. */. relea
14f7d 73 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 sePage(pNew);.
14f7e 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 }.. /* At this
14f7f 70 6f 69 6e 74 20 74 68 65 20 70 50 61 67 65 2d point the pPage-
14f80 3e 6e 46 72 65 65 20 76 61 72 69 61 62 6c 65 20 >nFree variable
14f81 69 73 20 6e 6f 74 20 73 65 74 20 63 6f 72 72 65 is not set corre
14f82 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20 72 ctly with. ** r
14f83 65 73 70 65 63 74 20 74 6f 20 74 68 65 20 63 6f espect to the co
14f84 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 ntent of the pag
14f85 65 20 28 62 65 63 61 75 73 65 20 69 74 20 77 61 e (because it wa
14f86 73 20 73 65 74 20 74 6f 20 30 20 62 79 20 0a 20 s set to 0 by .
14f87 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 29 2e ** insertCell).
14f88 20 53 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 So call sqlite3
14f89 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 BtreeInitPage()
14f8a 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 to make sure it
14f8b 69 73 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 2e is. ** correct.
14f8c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 . **. ** This
14f8d 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 20 65 has to be done e
14f8e 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ven if an error
14f8f 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 will be returned
14f90 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 0a 20 . Normally, if.
14f91 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 ** an error occ
14f92 75 72 73 20 64 75 72 69 6e 67 20 74 72 65 65 20 urs during tree
14f93 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 20 63 balancing, the c
14f94 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65 6d 50 61 ontents of MemPa
14f95 67 65 20 61 72 65 0a 20 20 2a 2a 20 6e 6f 74 20 ge are. ** not
14f96 69 6d 70 6f 72 74 61 6e 74 2c 20 61 73 20 74 68 important, as th
14f97 65 79 20 77 69 6c 6c 20 62 65 20 72 65 63 61 6c ey will be recal
14f98 63 75 6c 61 74 65 64 20 77 68 65 6e 20 74 68 65 culated when the
14f99 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 0a page is rolled.
14f9a 20 20 2a 2a 20 62 61 63 6b 2e 20 42 75 74 20 68 ** back. But h
14f9b 65 72 65 2c 20 69 6e 20 62 61 6c 61 6e 63 65 5f ere, in balance_
14f9c 71 75 69 63 6b 28 29 2c 20 69 74 20 69 73 20 70 quick(), it is p
14f9d 6f 73 73 69 62 6c 65 20 74 68 61 74 20 70 50 61 ossible that pPa
14f9e 67 65 20 68 61 73 20 0a 20 20 2a 2a 20 6e 6f 74 ge has . ** not
14f9f 20 79 65 74 20 62 65 65 6e 20 6d 61 72 6b 65 64 yet been marked
14fa0 20 64 69 72 74 79 20 6f 72 20 77 72 69 74 74 65 dirty or writte
14fa1 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e n into the journ
14fa2 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 66 6f al file. Therefo
14fa3 72 65 0a 20 20 2a 2a 20 69 74 20 77 69 6c 6c 20 re. ** it will
14fa4 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 not be rolled ba
14fa5 63 6b 20 61 6e 64 20 73 6f 20 69 74 20 69 73 20 ck and so it is
14fa6 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6d 61 6b important to mak
14fa7 65 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a e sure that. **
14fa8 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 61 the page data a
14fa9 6e 64 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d nd contents of M
14faa 65 6d 50 61 67 65 20 61 72 65 20 63 6f 6e 73 69 emPage are consi
14fab 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 50 stent.. */. pP
14fac 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b age->isInit = 0;
14fad 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 . sqlite3BtreeI
14fae 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a nitPage(pPage);.
14faf 0a 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74 68 . /* If everyth
14fb0 69 6e 67 20 65 6c 73 65 20 73 75 63 63 65 65 64 ing else succeed
14fb1 65 64 2c 20 62 61 6c 61 6e 63 65 20 74 68 65 20 ed, balance the
14fb2 70 61 72 65 6e 74 20 70 61 67 65 2c 20 69 6e 20 parent page, in
14fb3 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 64 . ** case the d
14fb4 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 73 65 ivider cell inse
14fb5 72 74 65 64 20 63 61 75 73 65 64 20 69 74 20 74 rted caused it t
14fb6 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c o become overful
14fb7 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 l.. */. if( rc
14fb8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
14fb9 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
14fba 50 61 67 65 29 3b 0a 20 20 20 20 70 43 75 72 2d Page);. pCur-
14fbb 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20 72 63 >iPage--;. rc
14fbc 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c = balance(pCur,
14fbd 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 0);. }. retur
14fbe 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f n rc;.}.#endif /
14fbf 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 * SQLITE_OMIT_QU
14fc0 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 2f ICKBALANCE */../
14fc1 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
14fc2 65 20 72 65 64 69 73 74 72 69 62 75 74 65 73 20 e redistributes
14fc3 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 20 61 Cells on pPage a
14fc4 6e 64 20 75 70 20 74 6f 20 4e 4e 2a 32 20 73 69 nd up to NN*2 si
14fc5 62 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20 70 50 61 blings.** of pPa
14fc6 67 65 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70 ge so that all p
14fc7 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 20 ages have about
14fc8 74 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 the same amount
14fc9 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e 0a 2a of free space..*
14fca 2a 20 55 73 75 61 6c 6c 79 20 4e 4e 20 73 69 62 * Usually NN sib
14fcb 6c 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 20 lings on either
14fcc 73 69 64 65 20 6f 66 20 70 50 61 67 65 20 69 73 side of pPage is
14fcd 20 75 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c used in the bal
14fce 61 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68 6f 75 67 ancing,.** thoug
14fcf 68 20 6d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 h more siblings
14fd0 6d 69 67 68 74 20 63 6f 6d 65 20 66 72 6f 6d 20 might come from
14fd1 6f 6e 65 20 73 69 64 65 20 69 66 20 70 50 61 67 one side if pPag
14fd2 65 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a e is the first.*
14fd3 2a 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20 * or last child
14fd4 6f 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 of its parent.
14fd5 49 66 20 70 50 61 67 65 20 68 61 73 20 66 65 77 If pPage has few
14fd6 65 72 20 74 68 61 6e 20 32 2a 4e 4e 20 73 69 62 er than 2*NN sib
14fd7 6c 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d 65 74 68 lings.** (someth
14fd8 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e ing which can on
14fd9 6c 79 20 68 61 70 70 65 6e 20 69 66 20 70 50 61 ly happen if pPa
14fda 67 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 ge is the root p
14fdb 61 67 65 20 6f 72 20 61 20 0a 2a 2a 20 63 68 69 age or a .** chi
14fdc 6c 64 20 6f 66 20 72 6f 6f 74 29 20 74 68 65 6e ld of root) then
14fdd 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 73 all available s
14fde 69 62 6c 69 6e 67 73 20 70 61 72 74 69 63 69 70 iblings particip
14fdf 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e ate in the balan
14fe0 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 cing..**.** The
14fe1 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e number of siblin
14fe2 67 73 20 6f 66 20 70 50 61 67 65 20 6d 69 67 68 gs of pPage migh
14fe3 74 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 6f t be increased o
14fe4 72 20 64 65 63 72 65 61 73 65 64 20 62 79 20 6f r decreased by o
14fe5 6e 65 20 6f 72 0a 2a 2a 20 74 77 6f 20 69 6e 20 ne or.** two in
14fe6 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 an effort to kee
14fe7 70 20 70 61 67 65 73 20 6e 65 61 72 6c 79 20 66 p pages nearly f
14fe8 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 ull but not over
14fe9 20 66 75 6c 6c 2e 20 54 68 65 20 72 6f 6f 74 20 full. The root
14fea 70 61 67 65 0a 2a 2a 20 69 73 20 73 70 65 63 69 page.** is speci
14feb 61 6c 20 61 6e 64 20 69 73 20 61 6c 6c 6f 77 65 al and is allowe
14fec 64 20 74 6f 20 62 65 20 6e 65 61 72 6c 79 20 65 d to be nearly e
14fed 6d 70 74 79 2e 20 49 66 20 70 50 61 67 65 20 69 mpty. If pPage i
14fee 73 20 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 s .** the root p
14fef 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 64 65 age, then the de
14ff0 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 pth of the tree
14ff1 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 might be increas
14ff2 65 64 0a 2a 2a 20 6f 72 20 64 65 63 72 65 61 73 ed.** or decreas
14ff3 65 64 20 62 79 20 6f 6e 65 2c 20 61 73 20 6e 65 ed by one, as ne
14ff4 63 65 73 73 61 72 79 2c 20 74 6f 20 6b 65 65 70 cessary, to keep
14ff5 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 66 the root page f
14ff6 72 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20 6f 76 65 rom being.** ove
14ff7 72 66 75 6c 6c 20 6f 72 20 63 6f 6d 70 6c 65 74 rfull or complet
14ff8 65 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a ely empty..**.**
14ff9 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 Note that when
14ffa 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
14ffb 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66 20 called, some of
14ffc 74 68 65 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 the Cells on pPa
14ffd 67 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 ge.** might not
14ffe 61 63 74 75 61 6c 6c 79 20 62 65 20 73 74 6f 72 actually be stor
14fff 65 64 20 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 ed in pPage->aDa
15000 74 61 5b 5d 2e 20 20 54 68 69 73 20 63 61 6e 20 ta[]. This can
15001 68 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68 65 happen.** if the
15002 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c page is overful
15003 6c 2e 20 20 50 61 72 74 20 6f 66 20 74 68 65 20 l. Part of the
15004 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75 74 job of this rout
15005 69 6e 65 20 69 73 20 74 6f 0a 2a 2a 20 6d 61 6b ine is to.** mak
15006 65 20 73 75 72 65 20 61 6c 6c 20 43 65 6c 6c 73 e sure all Cells
15007 20 66 6f 72 20 70 50 61 67 65 20 6f 6e 63 65 20 for pPage once
15008 61 67 61 69 6e 20 66 69 74 20 69 6e 20 70 50 61 again fit in pPa
15009 67 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a 2a 2a 0a ge->aData[]..**.
1500a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75 72 73 65 ** In the course
1500b 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 of balancing th
1500c 65 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 70 50 e siblings of pP
1500d 61 67 65 2c 20 74 68 65 20 70 61 72 65 6e 74 20 age, the parent
1500e 6f 66 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 68 of pPage.** migh
1500f 74 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c t become overful
15010 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 l or underfull.
15011 20 49 66 20 74 68 61 74 20 68 61 70 70 65 6e 73 If that happens
15012 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
15013 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 ine.** is called
15014 20 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e 20 recursively on
15015 74 68 65 20 70 61 72 65 6e 74 2e 0a 2a 2a 0a 2a the parent..**.*
15016 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e * If this routin
15017 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 e fails for any
15018 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 reason, it might
15019 20 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62 leave the datab
1501a 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 ase.** in a corr
1501b 75 70 74 65 64 20 73 74 61 74 65 2e 20 20 53 6f upted state. So
1501c 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 if this routine
1501d 20 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 fails, the data
1501e 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 base should.** b
1501f 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a e rolled back..*
15020 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c /.static int bal
15021 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 42 74 43 ance_nonroot(BtC
15022 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
15023 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 MemPage *pPage;
15024 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15025 54 68 65 20 6f 76 65 72 20 6f 72 20 75 6e 64 65 The over or unde
15026 72 66 75 6c 6c 20 70 61 67 65 20 74 6f 20 62 61 rfull page to ba
15027 6c 61 6e 63 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 lance */. MemPa
15028 67 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 ge *pParent;
15029 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
1502a 61 72 65 6e 74 20 6f 66 20 70 50 61 67 65 20 2a arent of pPage *
1502b 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
1502c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
1502d 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61 /* The whole da
1502e 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 tabase */. int
1502f 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 nCell = 0;
15030 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
15031 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 er of cells in a
15032 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 pCell[] */. int
15033 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b 20 nMaxCells = 0;
15034 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c /* All
15035 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 ocated size of a
15036 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 pCell, szCell, a
15037 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e From. */. int n
15038 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 Old;
15039 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1503a 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 r of pages in ap
1503b 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e Old[] */. int n
1503c 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 New;
1503d 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1503e 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 r of pages in ap
1503f 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e New[] */. int n
15040 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 Div;
15041 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
15042 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 r of cells in ap
15043 44 69 76 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 Div[] */. int i
15044 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 , j, k;
15045 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
15046 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e counters */. in
15047 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 t idx;
15048 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
15049 64 65 78 20 6f 66 20 70 50 61 67 65 20 69 6e 20 dex of pPage in
1504a 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d pParent->aCell[]
1504b 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b */. int nxDiv;
1504c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1504d 20 20 20 2f 2a 20 4e 65 78 74 20 64 69 76 69 64 /* Next divid
1504e 65 72 20 73 6c 6f 74 20 69 6e 20 70 50 61 72 65 er slot in pPare
1504f 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 nt->aCell[] */.
15050 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
15051 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15052 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 The return code
15053 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 43 6f */. int leafCo
15054 72 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 rrection;
15055 20 20 20 2f 2a 20 34 20 69 66 20 70 50 61 67 65 /* 4 if pPage
15056 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 is a leaf. 0 i
15057 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 6c f not */. int l
15058 65 61 66 44 61 74 61 3b 20 20 20 20 20 20 20 20 eafData;
15059 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1505a 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 if pPage is a le
1505b 61 66 20 6f 66 20 61 20 4c 45 41 46 44 41 54 41 af of a LEAFDATA
1505c 20 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75 tree */. int u
1505d 73 61 62 6c 65 53 70 61 63 65 3b 20 20 20 20 20 sableSpace;
1505e 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
1505f 20 69 6e 20 70 50 61 67 65 20 62 65 79 6f 6e 64 in pPage beyond
15060 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 the header */.
15061 20 69 6e 74 20 70 61 67 65 46 6c 61 67 73 3b 20 int pageFlags;
15062 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15063 20 56 61 6c 75 65 20 6f 66 20 70 50 61 67 65 2d Value of pPage-
15064 3e 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 >aData[0] */. i
15065 6e 74 20 73 75 62 74 6f 74 61 6c 3b 20 20 20 20 nt subtotal;
15066 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
15067 75 62 74 6f 74 61 6c 20 6f 66 20 62 79 74 65 73 ubtotal of bytes
15068 20 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 in cells on one
15069 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 page */. int i
1506a 53 70 61 63 65 31 20 3d 20 30 3b 20 20 20 20 20 Space1 = 0;
1506b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
1506c 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20 unused byte of
1506d 61 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 aSpace1[] */. i
1506e 6e 74 20 69 53 70 61 63 65 32 20 3d 20 30 3b 20 nt iSpace2 = 0;
1506f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
15070 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 irst unused byte
15071 20 6f 66 20 61 53 70 61 63 65 32 5b 5d 20 2a 2f of aSpace2[] */
15072 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 . int szScratch
15073 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
15074 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74 /* Size of scrat
15075 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 ch memory reques
15076 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 ted */. MemPage
15077 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 *apOld[NB];
15078 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 /* pPage a
15079 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62 nd up to two sib
1507a 6c 69 6e 67 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 lings */. Pgno
1507b 70 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 pgnoOld[NB];
1507c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
1507d 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 68 numbers for each
1507e 20 70 61 67 65 20 69 6e 20 61 70 4f 6c 64 5b 5d page in apOld[]
1507f 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 */. MemPage *a
15080 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 pCopy[NB];
15081 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 6f /* Private co
15082 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 pies of apOld[]
15083 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 pages */. MemPa
15084 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b ge *apNew[NB+2];
15085 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 /* pPage
15086 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73 69 and up to NB si
15087 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61 6c blings after bal
15088 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 50 67 6e 6f ancing */. Pgno
15089 20 70 67 6e 6f 4e 65 77 5b 4e 42 2b 32 5d 3b 20 pgnoNew[NB+2];
1508a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
1508b 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 numbers for eac
1508c 68 20 70 61 67 65 20 69 6e 20 61 70 4e 65 77 5b h page in apNew[
1508d 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69 76 ] */. u8 *apDiv
1508e 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 20 [NB];
1508f 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72 20 63 /* Divider c
15090 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 ells in pParent
15091 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77 5b */. int cntNew[
15092 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 NB+2];
15093 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 43 /* Index in aC
15094 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 66 ell[] of cell af
15095 74 65 72 20 69 2d 74 68 20 70 61 67 65 20 2a 2f ter i-th page */
15096 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e 42 2b . int szNew[NB+
15097 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2];
15098 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 7a 65 /* Combined size
15099 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 65 20 of cells place
1509a 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a on i-th page */.
1509b 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 3d 20 u8 **apCell =
1509c 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
1509d 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 67 69 * All cells begi
1509e 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 n balanced */.
1509f 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 u16 *szCell;
150a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
150a1 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c Local size of al
150a2 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c l cells in apCel
150a3 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 43 6f l[] */. u8 *aCo
150a4 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 py[NB];
150a5 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 68 6f 6c /* Space for hol
150a6 64 69 6e 67 20 64 61 74 61 20 6f 66 20 61 70 43 ding data of apC
150a7 6f 70 79 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 opy[] */. u8 *a
150a8 53 70 61 63 65 31 3b 20 20 20 20 20 20 20 20 20 Space1;
150a9 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 /* Space for c
150aa 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 opies of divider
150ab 73 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 62 s cells before b
150ac 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a alance */. u8 *
150ad 61 53 70 61 63 65 32 20 3d 20 30 3b 20 20 20 20 aSpace2 = 0;
150ae 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 /* Space for
150af 6f 76 65 72 66 6c 6f 77 20 64 69 76 69 64 65 72 overflow divider
150b0 73 20 63 65 6c 6c 73 20 61 66 74 65 72 20 62 61 s cells after ba
150b1 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a 61 lance */. u8 *a
150b2 46 72 6f 6d 20 3d 20 30 3b 0a 0a 20 20 70 50 61 From = 0;.. pPa
150b3 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
150b4 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
150b5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
150b6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
150b7 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
150b8 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 );. VVA_ONLY( p
150b9 43 75 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c Cur->pagesShuffl
150ba 65 64 20 3d 20 31 20 29 3b 0a 0a 20 20 2f 2a 20 ed = 1 );.. /*
150bb 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 70 . ** Find the p
150bc 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 2a 2f arent page.. */
150bd 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
150be 3e 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73 >iPage>0 );. as
150bf 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
150c0 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nit );. assert(
150c1 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
150c2 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
150c3 70 44 62 50 61 67 65 29 20 7c 7c 20 70 50 61 67 pDbPage) || pPag
150c4 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 e->nOverflow==1
150c5 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 );. pBt = pPage
150c6 2d 3e 70 42 74 3b 0a 20 20 70 50 61 72 65 6e 74 ->pBt;. pParent
150c7 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
150c8 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a pCur->iPage-1];.
150c9 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e assert( pParen
150ca 74 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 t );. if( SQLIT
150cb 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK!=(rc = sqli
150cc 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
150cd 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 29 arent->pDbPage))
150ce 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
150cf 63 3b 0a 20 20 7d 0a 0a 20 20 54 52 41 43 45 28 c;. }.. TRACE(
150d0 28 22 42 41 4c 41 4e 43 45 3a 20 62 65 67 69 6e ("BALANCE: begin
150d1 20 70 61 67 65 20 25 64 20 63 68 69 6c 64 20 6f page %d child o
150d2 66 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e f %d\n", pPage->
150d3 70 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d 3e 70 pgno, pParent->p
150d4 67 6e 6f 29 29 3b 0a 0a 23 69 66 6e 64 65 66 20 gno));..#ifndef
150d5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 SQLITE_OMIT_QUIC
150d6 4b 42 41 4c 41 4e 43 45 0a 20 20 2f 2a 0a 20 20 KBALANCE. /*.
150d7 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 63 61 73 ** A special cas
150d8 65 3a 20 20 49 66 20 61 20 6e 65 77 20 65 6e 74 e: If a new ent
150d9 72 79 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e ry has just been
150da 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 inserted into a
150db 0a 20 20 2a 2a 20 74 61 62 6c 65 20 28 74 68 61 . ** table (tha
150dc 74 20 69 73 2c 20 61 20 62 74 72 65 65 20 77 69 t is, a btree wi
150dd 74 68 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 th integer keys
150de 61 6e 64 20 61 6c 6c 20 64 61 74 61 20 61 74 20 and all data at
150df 74 68 65 20 6c 65 61 76 65 73 29 0a 20 20 2a 2a the leaves). **
150e0 20 61 6e 64 20 74 68 65 20 6e 65 77 20 65 6e 74 and the new ent
150e1 72 79 20 69 73 20 74 68 65 20 72 69 67 68 74 2d ry is the right-
150e2 6d 6f 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 most entry in th
150e3 65 20 74 72 65 65 20 28 69 74 20 68 61 73 20 74 e tree (it has t
150e4 68 65 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 he. ** largest
150e5 6b 65 79 29 20 74 68 65 6e 20 75 73 65 20 74 68 key) then use th
150e6 65 20 73 70 65 63 69 61 6c 20 62 61 6c 61 6e 63 e special balanc
150e7 65 5f 71 75 69 63 6b 28 29 20 72 6f 75 74 69 6e e_quick() routin
150e8 65 20 66 6f 72 0a 20 20 2a 2a 20 62 61 6c 61 6e e for. ** balan
150e9 63 69 6e 67 2e 20 20 62 61 6c 61 6e 63 65 5f 71 cing. balance_q
150ea 75 69 63 6b 28 29 20 69 73 20 6d 75 63 68 20 66 uick() is much f
150eb 61 73 74 65 72 20 61 6e 64 20 72 65 73 75 6c 74 aster and result
150ec 73 20 69 6e 20 61 20 74 69 67 68 74 65 72 0a 20 s in a tighter.
150ed 20 2a 2a 20 70 61 63 6b 69 6e 67 20 6f 66 20 64 ** packing of d
150ee 61 74 61 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f ata in the commo
150ef 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 n case.. */. i
150f0 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 26 f( pPage->leaf &
150f1 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 69 &. pPage->i
150f2 6e 74 4b 65 79 20 26 26 0a 20 20 20 20 20 20 70 ntKey &&. p
150f3 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d Page->nOverflow=
150f4 3d 31 20 26 26 0a 20 20 20 20 20 20 70 50 61 67 =1 &&. pPag
150f5 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d e->aOvfl[0].idx=
150f6 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 =pPage->nCell &&
150f7 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e . pParent->
150f8 70 67 6e 6f 21 3d 31 20 26 26 0a 20 20 20 20 20 pgno!=1 &&.
150f9 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65 get4byte(&pPare
150fa 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e nt->aData[pParen
150fb 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 t->hdrOffset+8])
150fc 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 0a 20 20 ==pPage->pgno.
150fd 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
150fe 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a Page->intKey );.
150ff 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 4f /*. ** TO
15100 44 4f 3a 20 43 68 65 63 6b 20 74 68 65 20 73 69 DO: Check the si
15101 62 6c 69 6e 67 73 20 74 6f 20 74 68 65 20 6c 65 blings to the le
15102 66 74 20 6f 66 20 70 50 61 67 65 2e 20 49 74 20 ft of pPage. It
15103 6d 61 79 20 62 65 20 74 68 61 74 0a 20 20 20 20 may be that.
15104 2a 2a 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 ** they are not
15105 66 75 6c 6c 20 61 6e 64 20 6e 6f 20 6e 65 77 20 full and no new
15106 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 page is required
15107 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 .. */. ret
15108 75 72 6e 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 urn balance_quic
15109 6b 28 70 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e k(pCur);. }.#en
1510a 64 69 66 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 dif.. if( SQLIT
1510b 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK!=(rc = sqli
1510c 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
1510d 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 20 29 age->pDbPage)) )
1510e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
1510f 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 . }.. /*. **
15110 46 69 6e 64 20 74 68 65 20 63 65 6c 6c 20 69 6e Find the cell in
15111 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
15112 20 77 68 6f 73 65 20 6c 65 66 74 20 63 68 69 6c whose left chil
15113 64 20 70 6f 69 6e 74 73 20 62 61 63 6b 0a 20 20 d points back.
15114 2a 2a 20 74 6f 20 70 50 61 67 65 2e 20 20 54 68 ** to pPage. Th
15115 65 20 22 69 64 78 22 20 76 61 72 69 61 62 6c 65 e "idx" variable
15116 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 is the index of
15117 20 74 68 61 74 20 63 65 6c 6c 2e 20 20 49 66 20 that cell. If
15118 70 50 61 67 65 0a 20 20 2a 2a 20 69 73 20 74 68 pPage. ** is th
15119 65 20 72 69 67 68 74 6d 6f 73 74 20 63 68 69 6c e rightmost chil
1511a 64 20 6f 66 20 70 50 61 72 65 6e 74 20 74 68 65 d of pParent the
1511b 6e 20 73 65 74 20 69 64 78 20 74 6f 20 70 50 61 n set idx to pPa
1511c 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 0a 20 20 2a rent->nCell . *
1511d 2f 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e /. idx = pCur->
1511e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
1511f 65 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 50 61 e-1];. assertPa
15120 72 65 6e 74 49 6e 64 65 78 28 70 50 61 72 65 6e rentIndex(pParen
15121 74 2c 20 69 64 78 2c 20 70 50 61 67 65 2d 3e 70 t, idx, pPage->p
15122 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a gno);.. /*. **
15123 20 49 6e 69 74 69 61 6c 69 7a 65 20 76 61 72 69 Initialize vari
15124 61 62 6c 65 73 20 73 6f 20 74 68 61 74 20 69 74 ables so that it
15125 20 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74 6f will be safe to
15126 20 6a 75 6d 70 0a 20 20 2a 2a 20 64 69 72 65 63 jump. ** direc
15127 74 6c 79 20 74 6f 20 62 61 6c 61 6e 63 65 5f 63 tly to balance_c
15128 6c 65 61 6e 75 70 20 61 74 20 61 6e 79 20 6d 6f leanup at any mo
15129 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 6e 4f 6c ment.. */. nOl
1512a 64 20 3d 20 6e 4e 65 77 20 3d 20 30 3b 0a 0a 20 d = nNew = 0;..
1512b 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 73 69 /*. ** Find si
1512c 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 70 bling pages to p
1512d 50 61 67 65 20 61 6e 64 20 74 68 65 20 63 65 6c Page and the cel
1512e 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 74 68 ls in pParent th
1512f 61 74 20 64 69 76 69 64 65 0a 20 20 2a 2a 20 74 at divide. ** t
15130 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 20 41 6e he siblings. An
15131 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
15132 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c to find NN sibl
15133 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 0a 20 ings on either.
15134 20 2a 2a 20 73 69 64 65 20 6f 66 20 70 50 61 67 ** side of pPag
15135 65 2e 20 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 e. More sibling
15136 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d s are taken from
15137 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76 one side, howev
15138 65 72 2c 20 69 66 0a 20 20 2a 2a 20 70 50 61 67 er, if. ** pPag
15139 65 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65 e there are fewe
1513a 72 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e r than NN siblin
1513b 67 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 gs on the other
1513c 73 69 64 65 2e 20 20 49 66 20 70 50 61 72 65 6e side. If pParen
1513d 74 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 t. ** has NB or
1513e 20 66 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20 fewer children
1513f 74 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 then all childre
15140 6e 20 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65 n of pParent are
15141 20 74 61 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 6e taken.. */. n
15142 78 44 69 76 20 3d 20 69 64 78 20 2d 20 4e 4e 3b xDiv = idx - NN;
15143 0a 20 20 69 66 28 20 6e 78 44 69 76 20 2b 20 4e . if( nxDiv + N
15144 42 20 3e 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 B > pParent->nCe
15145 6c 6c 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 ll ){. nxDiv
15146 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c = pParent->nCell
15147 20 2d 20 4e 42 20 2b 20 31 3b 0a 20 20 7d 0a 20 - NB + 1;. }.
15148 20 69 66 28 20 6e 78 44 69 76 3c 30 20 29 7b 0a if( nxDiv<0 ){.
15149 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 nxDiv = 0;.
1514a 20 7d 0a 20 20 6e 44 69 76 20 3d 20 30 3b 0a 20 }. nDiv = 0;.
1514b 20 66 6f 72 28 69 3d 30 2c 20 6b 3d 6e 78 44 69 for(i=0, k=nxDi
1514c 76 3b 20 69 3c 4e 42 3b 20 69 2b 2b 2c 20 6b 2b v; i<NB; i++, k+
1514d 2b 29 7b 0a 20 20 20 20 69 66 28 20 6b 3c 70 50 +){. if( k<pP
1514e 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a arent->nCell ){.
1514f 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d apDiv[i] =
15150 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e findCell(pParen
15151 74 2c 20 6b 29 3b 0a 20 20 20 20 20 20 6e 44 69 t, k);. nDi
15152 76 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 v++;. asser
15153 74 28 20 21 70 50 61 72 65 6e 74 2d 3e 6c 65 61 t( !pParent->lea
15154 66 20 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4f f );. pgnoO
15155 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62 79 74 65 ld[i] = get4byte
15156 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 (apDiv[i]);.
15157 7d 65 6c 73 65 20 69 66 28 20 6b 3d 3d 70 50 61 }else if( k==pPa
15158 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 rent->nCell ){.
15159 20 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 pgnoOld[i]
1515a 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 = get4byte(&pPar
1515b 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 ent->aData[pPare
1515c 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d nt->hdrOffset+8]
1515d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
1515e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1515f 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64 . rc = getAnd
15160 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 67 InitPage(pBt, pg
15161 6e 6f 4f 6c 64 5b 69 5d 2c 20 26 61 70 4f 6c 64 noOld[i], &apOld
15162 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 [i]);. if( rc
15163 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f ) goto balance_
15164 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 2f 2a 20 cleanup;. /*
15165 61 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72 apOld[i]->idxPar
15166 65 6e 74 20 3d 20 6b 3b 20 2a 2f 0a 20 20 20 20 ent = k; */.
15167 61 70 43 6f 70 79 5b 69 5d 20 3d 20 30 3b 0a 20 apCopy[i] = 0;.
15168 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 4f assert( i==nO
15169 6c 64 20 29 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b ld );. nOld++
1516a 3b 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 ;. nMaxCells
1516b 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e += 1+apOld[i]->n
1516c 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e Cell+apOld[i]->n
1516d 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20 Overflow;. }..
1516e 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c /* Make nMaxCel
1516f 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 ls a multiple of
15170 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 4 in order to p
15171 72 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20 reserve 8-byte.
15172 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f ** alignment */
15173 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 . nMaxCells = (
15174 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e nMaxCells + 3)&~
15175 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 3;.. /*. ** Al
15176 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
15177 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 memory structur
15178 65 73 0a 20 20 2a 2f 0a 20 20 73 7a 53 63 72 61 es. */. szScra
15179 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 tch =. nMa
1517a 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 xCells*sizeof(u8
1517b 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *)
1517c 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 /* apCe
1517d 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 ll */. + nMa
1517e 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 xCells*sizeof(u1
1517f 36 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6)
15180 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 /* szCe
15181 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 28 52 4f ll */. + (RO
15182 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 UND8(sizeof(MemP
15183 61 67 65 29 29 2b 70 42 74 2d 3e 70 61 67 65 53 age))+pBt->pageS
15184 69 7a 65 29 2a 4e 42 20 20 2f 2a 20 61 43 6f 70 ize)*NB /* aCop
15185 79 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d y */. + pBt-
15186 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20 >pageSize
15187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15188 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63 /* aSpac
15189 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 28 49 53 e1 */. + (IS
1518a 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 6e 4d 61 AUTOVACUUM ? nMa
1518b 78 43 65 6c 6c 73 20 3a 20 30 29 3b 20 20 20 20 xCells : 0);
1518c 20 20 20 20 20 20 20 20 20 2f 2a 20 61 46 72 6f /* aFro
1518d 6d 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 m */. apCell =
1518e 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 sqlite3ScratchMa
1518f 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20 lloc( szScratch
15190 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c ); . if( apCell
15191 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
15192 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
15193 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 goto balance_c
15194 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a leanup;. }. sz
15195 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 Cell = (u16*)&ap
15196 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b Cell[nMaxCells];
15197 0a 20 20 61 43 6f 70 79 5b 30 5d 20 3d 20 28 75 . aCopy[0] = (u
15198 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 8*)&szCell[nMaxC
15199 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 ells];. assert(
1519a 20 28 28 61 43 6f 70 79 5b 30 5d 20 2d 20 28 75 ((aCopy[0] - (u
1519b 38 2a 29 61 70 43 65 6c 6c 29 20 26 20 37 29 3d 8*)apCell) & 7)=
1519c 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 =0 ); /* 8-byte
1519d 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 alignment requir
1519e 65 64 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b ed */. for(i=1;
1519f 20 69 3c 4e 42 3b 20 69 2b 2b 29 7b 0a 20 20 20 i<NB; i++){.
151a0 20 61 43 6f 70 79 5b 69 5d 20 3d 20 26 61 43 6f aCopy[i] = &aCo
151a1 70 79 5b 69 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 py[i-1][pBt->pag
151a2 65 53 69 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a eSize+ROUND8(siz
151a3 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a eof(MemPage))];.
151a4 20 20 20 20 61 73 73 65 72 74 28 20 28 28 61 43 assert( ((aC
151a5 6f 70 79 5b 69 5d 20 2d 20 28 75 38 2a 29 61 70 opy[i] - (u8*)ap
151a6 43 65 6c 6c 29 20 26 20 37 29 3d 3d 30 20 29 3b Cell) & 7)==0 );
151a7 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 6e /* 8-byte align
151a8 6d 65 6e 74 20 72 65 71 75 69 72 65 64 20 2a 2f ment required */
151a9 0a 20 20 7d 0a 20 20 61 53 70 61 63 65 31 20 3d . }. aSpace1 =
151aa 20 26 61 43 6f 70 79 5b 4e 42 2d 31 5d 5b 70 42 &aCopy[NB-1][pB
151ab 74 2d 3e 70 61 67 65 53 69 7a 65 2b 52 4f 55 4e t->pageSize+ROUN
151ac 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 D8(sizeof(MemPag
151ad 65 29 29 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 e))];. assert(
151ae 28 28 61 53 70 61 63 65 31 20 2d 20 28 75 38 2a ((aSpace1 - (u8*
151af 29 61 70 43 65 6c 6c 29 20 26 20 37 29 3d 3d 30 )apCell) & 7)==0
151b0 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c ); /* 8-byte al
151b1 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 64 ignment required
151b2 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55 54 4f */. if( ISAUTO
151b3 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 61 46 VACUUM ){. aF
151b4 72 6f 6d 20 3d 20 26 61 53 70 61 63 65 31 5b 70 rom = &aSpace1[p
151b5 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 3b 0a 20 Bt->pageSize];.
151b6 20 7d 0a 20 20 61 53 70 61 63 65 32 20 3d 20 73 }. aSpace2 = s
151b7 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 qlite3PageMalloc
151b8 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b (pBt->pageSize);
151b9 0a 20 20 69 66 28 20 61 53 70 61 63 65 32 3d 3d . if( aSpace2==
151ba 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 0 ){. rc = SQ
151bb 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
151bc 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
151bd 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f anup;. }. . /
151be 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 63 6f 70 69 *. ** Make copi
151bf 65 73 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e es of the conten
151c0 74 20 6f 66 20 70 50 61 67 65 20 61 6e 64 20 69 t of pPage and i
151c1 74 73 20 73 69 62 6c 69 6e 67 73 20 69 6e 74 6f ts siblings into
151c2 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a 2a 20 54 68 aOld[].. ** Th
151c3 65 20 72 65 73 74 20 6f 66 20 74 68 69 73 20 66 e rest of this f
151c4 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75 73 65 unction will use
151c5 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 63 data from the c
151c6 6f 70 69 65 73 20 72 61 74 68 65 72 0a 20 20 2a opies rather. *
151c7 2a 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 * that the origi
151c8 6e 61 6c 20 70 61 67 65 73 20 73 69 6e 63 65 20 nal pages since
151c9 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 the original pag
151ca 65 73 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 es will be in th
151cb 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f e. ** process o
151cc 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 f being overwrit
151cd 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 ten.. */. for(
151ce 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b i=0; i<nOld; i++
151cf 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a ){. MemPage *
151d0 70 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20 3d 20 p = apCopy[i] =
151d1 28 4d 65 6d 50 61 67 65 2a 29 61 43 6f 70 79 5b (MemPage*)aCopy[
151d2 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 i];. memcpy(p
151d3 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 , apOld[i], size
151d4 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 of(MemPage));.
151d5 20 20 70 2d 3e 61 44 61 74 61 20 3d 20 28 76 6f p->aData = (vo
151d6 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 6d id*)&p[1];. m
151d7 65 6d 63 70 79 28 70 2d 3e 61 44 61 74 61 2c 20 emcpy(p->aData,
151d8 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74 61 2c apOld[i]->aData,
151d9 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b pBt->pageSize);
151da 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 . }.. /*. **
151db 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20 74 6f Load pointers to
151dc 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 69 all cells on si
151dd 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e 64 20 bling pages and
151de 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c the divider cell
151df 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 s. ** into the
151e0 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 61 local apCell[] a
151e1 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f 70 69 rray. Make copi
151e2 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 es of the divide
151e3 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 r cells. ** int
151e4 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 o space obtained
151e5 20 66 6f 72 6d 20 61 53 70 61 63 65 31 5b 5d 20 form aSpace1[]
151e6 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20 74 and remove the t
151e7 68 65 20 64 69 76 69 64 65 72 20 43 65 6c 6c 73 he divider Cells
151e8 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72 65 . ** from pPare
151e9 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 nt.. **. ** If
151ea 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 the siblings ar
151eb 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 73 2c e on leaf pages,
151ec 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20 then the child
151ed 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 65 0a pointers of the.
151ee 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 65 6c ** divider cel
151ef 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 64 20 ls are stripped
151f0 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20 62 from the cells b
151f1 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 63 efore they are c
151f2 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20 opied. ** into
151f3 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e 20 74 aSpace1[]. In t
151f4 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c his way, all cel
151f5 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 ls in apCell[] a
151f6 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 re without. **
151f7 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 child pointers.
151f8 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 72 65 If siblings are
151f9 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65 not leaves, the
151fa 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20 n all cell in.
151fb 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c ** apCell[] incl
151fc 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 ude child pointe
151fd 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c rs. Either way,
151fe 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 all cells in ap
151ff 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20 Cell[]. ** are
15200 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a alike.. **. **
15201 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a leafCorrection:
15202 20 20 34 20 69 66 20 70 50 61 67 65 20 69 73 20 4 if pPage is
15203 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 70 50 a leaf. 0 if pP
15204 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 age is not a lea
15205 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c 65 f.. ** le
15206 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 70 50 afData: 1 if pP
15207 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61 age holds key+da
15208 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 20 68 ta and pParent h
15209 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a olds only keys..
1520a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 3d 20 30 */. nCell = 0
1520b 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69 ;. leafCorrecti
1520c 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 on = pPage->leaf
1520d 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 3d *4;. leafData =
1520e 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 3b pPage->hasData;
1520f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f . for(i=0; i<nO
15210 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 ld; i++){. Me
15211 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70 mPage *pOld = ap
15212 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 Copy[i];. int
15213 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e limit = pOld->n
15214 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 Cell+pOld->nOver
15215 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28 6a 3d flow;. for(j=
15216 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 0; j<limit; j++)
15217 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
15218 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 nCell<nMaxCells
15219 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c 6c 5b );. apCell[
1521a 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 65 nCell] = findOve
1521b 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20 rflowCell(pOld,
1521c 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c j);. szCell
1521d 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69 [nCell] = cellSi
1521e 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 43 65 zePtr(pOld, apCe
1521f 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 20 20 20 ll[nCell]);.
15220 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
15221 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e UM ){. in
15222 74 20 61 3b 0a 20 20 20 20 20 20 20 20 61 46 72 t a;. aFr
15223 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 69 3b 0a 20 om[nCell] = i;.
15224 20 20 20 20 20 20 20 66 6f 72 28 61 3d 30 3b 20 for(a=0;
15225 61 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f a<pOld->nOverflo
15226 77 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20 20 w; a++){.
15227 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f 76 if( pOld->aOv
15228 66 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70 43 fl[a].pCell==apC
15229 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20 20 ell[nCell] ){.
1522a 20 20 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b aFrom[
1522b 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 nCell] = 0xFF;.
1522c 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
1522d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1522e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1522f 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 nCell++;.
15230 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e }. if( i<n
15231 4f 6c 64 2d 31 20 29 7b 0a 20 20 20 20 20 20 75 Old-1 ){. u
15232 31 36 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 16 sz = cellSize
15233 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 Ptr(pParent, apD
15234 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 iv[i]);. if
15235 28 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 ( leafData ){.
15236 20 20 20 20 20 20 2f 2a 20 57 69 74 68 20 74 68 /* With th
15237 65 20 4c 45 41 46 44 41 54 41 20 66 6c 61 67 2c e LEAFDATA flag,
15238 20 70 50 61 72 65 6e 74 20 63 65 6c 6c 73 20 68 pParent cells h
15239 6f 6c 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59 73 old only INTKEYs
1523a 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a that. **
1523b 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 20 are duplicates
1523c 6f 66 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 63 of keys on the c
1523d 68 69 6c 64 20 70 61 67 65 73 2e 20 20 57 65 20 hild pages. We
1523e 6e 65 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a 20 need to remove.
1523f 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 69 ** the di
15240 76 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d vider cells from
15241 20 70 50 61 72 65 6e 74 2c 20 62 75 74 20 74 68 pParent, but th
15242 65 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 e dividers cells
15243 20 61 72 65 20 6e 6f 74 0a 20 20 20 20 20 20 20 are not.
15244 20 2a 2a 20 61 64 64 65 64 20 74 6f 20 61 70 43 ** added to apC
15245 65 6c 6c 5b 5d 20 62 65 63 61 75 73 65 20 74 68 ell[] because th
15246 65 79 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 ey are duplicate
15247 73 20 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c 73 s of child cells
15248 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
15249 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 dropCell(pP
1524a 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a arent, nxDiv, sz
1524b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1524c 20 20 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d u8 *pTem
1524d 70 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 p;. asser
1524e 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c t( nCell<nMaxCel
1524f 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a ls );. sz
15250 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a Cell[nCell] = sz
15251 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70 20 ;. pTemp
15252 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 = &aSpace1[iSpac
15253 65 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 53 70 e1];. iSp
15254 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 ace1 += sz;.
15255 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d assert( sz<=
15256 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 pBt->pageSize/4
15257 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
15258 74 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d t( iSpace1<=pBt-
15259 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 >pageSize );.
1525a 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d memcpy(pTem
1525b 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 p, apDiv[i], sz)
1525c 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c ;. apCell
1525d 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b [nCell] = pTemp+
1525e 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a leafCorrection;.
1525f 20 20 20 20 20 20 20 20 69 66 28 20 49 53 41 55 if( ISAU
15260 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
15261 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c aFrom[nCel
15262 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20 20 20 l] = 0xFF;.
15263 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 72 6f }. dro
15264 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e pCell(pParent, n
15265 78 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20 xDiv, sz);.
15266 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d szCell[nCell]
15267 20 2d 3d 20 6c 65 61 66 43 6f 72 72 65 63 74 69 -= leafCorrecti
15268 6f 6e 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 on;. asse
15269 72 74 28 20 67 65 74 34 62 79 74 65 28 70 54 65 rt( get4byte(pTe
1526a 6d 70 29 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20 mp)==pgnoOld[i]
1526b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 );. if( !
1526c 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 pOld->leaf ){.
1526d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1526e 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d leafCorrection==
1526f 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 0 );. /
15270 2a 20 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e * The right poin
15271 74 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 ter of the child
15272 20 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d page pOld becom
15273 65 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20 es the left.
15274 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 ** pointer
15275 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 of the divider
15276 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 cell */.
15277 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b memcpy(apCell[
15278 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 nCell], &pOld->a
15279 44 61 74 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66 Data[pOld->hdrOf
1527a 66 73 65 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20 fset+8], 4);.
1527b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1527c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 assert( le
1527d 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 afCorrection==4
1527e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
1527f 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 szCell[nCell]<4
15280 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
15281 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 /* Do not allow
15282 61 6e 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 any cells smalle
15283 72 20 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20 r than 4 bytes.
15284 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 */. s
15285 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 zCell[nCell] = 4
15286 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
15287 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
15288 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d nCell++;. }
15289 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1528a 0a 20 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 . ** Figure out
1528b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
1528c 61 67 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68 ages needed to h
1528d 6f 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 old all nCell ce
1528e 6c 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20 lls.. ** Store
1528f 74 68 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22 this number in "
15290 6b 22 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74 k". Also comput
15291 65 20 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20 e szNew[] which
15292 69 73 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a is the total. *
15293 2a 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 * size of all ce
15294 6c 6c 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 lls on the i-th
15295 70 61 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b page and cntNew[
15296 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 ] which is the i
15297 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43 ndex. ** in apC
15298 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c ell[] of the cel
15299 6c 20 74 68 61 74 20 64 69 76 69 64 65 73 20 70 l that divides p
1529a 61 67 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20 age i from page
1529b 69 2b 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e i+1. . ** cntN
1529c 65 77 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 ew[k] should equ
1529d 61 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 al nCell.. **.
1529e 20 2a 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 ** Values compu
1529f 74 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63 ted by this bloc
152a0 6b 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 k:. **. **
152a1 20 20 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f k: The to
152a2 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 tal number of si
152a3 62 6c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a bling pages. **
152a4 20 20 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 szNew[i]: Sp
152a5 61 63 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65 aced used on the
152a6 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61 i-th sibling pa
152a7 67 65 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65 ge.. ** cntNe
152a8 77 5b 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61 w[i]: Index in a
152a9 70 43 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 pCell[] and szCe
152aa 6c 6c 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72 ll[] for the fir
152ab 73 74 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 st cell to. **
152ac 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
152ad 20 72 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d right of the i-
152ae 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e th sibling page.
152af 0a 20 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63 . ** usableSpac
152b0 65 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 e: Number of byt
152b1 65 73 20 6f 66 20 73 70 61 63 65 20 61 76 61 69 es of space avai
152b2 6c 61 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69 lable on each si
152b3 62 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a bling.. ** . *
152b4 2f 0a 20 20 75 73 61 62 6c 65 53 70 61 63 65 20 /. usableSpace
152b5 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
152b6 65 20 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72 e - 12 + leafCor
152b7 72 65 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 rection;. for(s
152b8 75 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 ubtotal=k=i=0; i
152b9 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 <nCell; i++){.
152ba 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78 assert( i<nMax
152bb 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62 Cells );. sub
152bc 74 6f 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b total += szCell[
152bd 69 5d 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20 i] + 2;. if(
152be 73 75 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c subtotal > usabl
152bf 65 53 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20 eSpace ){.
152c0 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f szNew[k] = subto
152c1 74 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b tal - szCell[i];
152c2 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d . cntNew[k]
152c3 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20 = i;. if(
152c4 6c 65 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b leafData ){ i--;
152c5 20 7d 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61 }. subtota
152c6 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b l = 0;. k++
152c7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a ;. }. }. sz
152c8 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 New[k] = subtota
152c9 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d l;. cntNew[k] =
152ca 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a nCell;. k++;..
152cb 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 /*. ** The pa
152cc 63 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 cking computed b
152cd 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 y the previous b
152ce 6c 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20 74 lock is biased t
152cf 6f 77 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e oward the siblin
152d0 67 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c gs. ** on the l
152d1 65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20 6c eft side. The l
152d2 65 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72 65 eft siblings are
152d3 20 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 66 always nearly f
152d4 75 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 ull, while the.
152d5 20 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 ** right-most s
152d6 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20 ibling might be
152d7 6e 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 nearly empty. T
152d8 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 his block of cod
152d9 65 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 e attempts. **
152da 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70 61 to adjust the pa
152db 63 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 cking of sibling
152dc 73 20 74 6f 20 67 65 74 20 61 20 62 65 74 74 65 s to get a bette
152dd 72 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a r balance.. **.
152de 20 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 74 ** This adjust
152df 6d 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 61 ment is more tha
152e0 6e 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f n an optimizatio
152e1 6e 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67 20 n. The packing
152e2 61 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a above might. **
152e3 20 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 be so out of ba
152e4 6c 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20 69 lance as to be i
152e5 6c 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 llegal. For exa
152e6 6d 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74 2d mple, the right-
152e7 6d 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e most. ** siblin
152e8 67 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 6c g might be compl
152e9 65 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 etely empty. Th
152ea 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 is adjustment is
152eb 20 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 not optional..
152ec 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b */. for(i=k-1;
152ed 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 i>0; i--){.
152ee 69 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73 7a int szRight = sz
152ef 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 New[i]; /* Size
152f0 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 of sibling on t
152f1 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 he right */.
152f2 69 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e int szLeft = szN
152f3 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 ew[i-1]; /* Size
152f4 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 of sibling on t
152f5 68 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 he left */. i
152f6 6e 74 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 nt r;
152f7 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 /* Index of r
152f8 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 ight-most cell i
152f9 6e 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 2a n left sibling *
152fa 2f 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20 20 /. int d;
152fb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
152fc 65 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c ex of first cell
152fd 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 to the left of
152fe 72 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f right sibling */
152ff 0a 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 .. r = cntNew
15300 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 [i-1] - 1;. d
15301 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 = r + 1 - leafD
15302 61 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ata;. assert(
15303 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a d<nMaxCells );.
15304 20 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e 4d assert( r<nM
15305 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 axCells );. w
15306 68 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 hile( szRight==0
15307 20 7c 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 || szRight+szCe
15308 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d ll[d]+2<=szLeft-
15309 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b (szCell[r]+2) ){
1530a 0a 20 20 20 20 20 20 73 7a 52 69 67 68 74 20 2b . szRight +
1530b 3d 20 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b = szCell[d] + 2;
1530c 0a 20 20 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d . szLeft -=
1530d 20 73 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a szCell[r] + 2;.
1530e 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 cntNew[i-1
1530f 5d 2d 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 ]--;. r = c
15310 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a ntNew[i-1] - 1;.
15311 20 20 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 d = r + 1
15312 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 - leafData;.
15313 7d 0a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d }. szNew[i] =
15314 20 73 7a 52 69 67 68 74 3b 0a 20 20 20 20 73 7a szRight;. sz
15315 4e 65 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 New[i-1] = szLef
15316 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 t;. }.. /* Eit
15317 68 65 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 her we found one
15318 20 6f 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 or more cells (
15319 63 6e 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 cntnew[0])>0) or
1531a 20 77 65 20 61 72 65 20 74 68 65 0a 20 20 2a 2a we are the. **
1531b 20 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 a virtual root
1531c 70 61 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c page. A virtual
1531d 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68 root page is wh
1531e 65 6e 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 en the real root
1531f 0a 20 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61 . ** page is pa
15320 67 65 20 31 20 61 6e 64 20 77 65 20 61 72 65 20 ge 1 and we are
15321 74 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f the only child o
15322 66 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a f that page.. *
15323 2f 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e /. assert( cntN
15324 65 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 ew[0]>0 || (pPar
15325 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 ent->pgno==1 &&
15326 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d pParent->nCell==
15327 30 29 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 0) );.. /*. **
15328 20 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 Allocate k new
15329 70 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c pages. Reuse ol
1532a 64 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f d pages where po
1532b 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 ssible.. */. a
1532c 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 ssert( pPage->pg
1532d 6e 6f 3e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c no>1 );. pageFl
1532e 61 67 73 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 ags = pPage->aDa
1532f 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 ta[0];. for(i=0
15330 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 ; i<k; i++){.
15331 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a MemPage *pNew;.
15332 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 if( i<nOld )
15333 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 {. pNew = a
15334 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b pNew[i] = apOld[
15335 69 5d 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 i];. pgnoNe
15336 77 5b 69 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69 w[i] = pgnoOld[i
15337 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 ];. apOld[i
15338 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 ] = 0;. rc
15339 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1533a 69 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 ite(pNew->pDbPag
1533b 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b e);. nNew++
1533c 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
1533d 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
1533e 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 eanup;. }else
1533f 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
15340 69 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 i>0 );. rc
15341 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
15342 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 age(pBt, &pNew,
15343 26 70 67 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e &pgnoNew[i], pgn
15344 6f 4e 65 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20 oNew[i-1], 0);.
15345 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
15346 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
15347 75 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b up;. apNew[
15348 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 i] = pNew;.
15349 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20 nNew++;. }.
1534a 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e }.. /* Free an
1534b 79 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74 y old pages that
1534c 20 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64 were not reused
1534d 20 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 as new pages..
1534e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e */. while( i<n
1534f 4f 6c 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 Old ){. rc =
15350 66 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 freePage(apOld[i
15351 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 ]);. if( rc )
15352 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
15353 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 eanup;. relea
15354 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 sePage(apOld[i])
15355 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d ;. apOld[i] =
15356 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0;. i++;. }
15357 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 .. /*. ** Put
15358 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e the new pages in
15359 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 accending order
1535a 2e 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f . This helps to
1535b 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 . ** keep entri
1535c 65 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 es in the disk f
1535d 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 ile in order so
1535e 74 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a that a scan. **
1535f 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 of the table is
15360 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 a linear scan t
15361 68 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e hrough the file.
15362 20 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 That. ** in t
15363 75 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70 urn helps the op
15364 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74 erating system t
15365 6f 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a o deliver pages.
15366 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 ** from the di
15367 73 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e sk more rapidly.
15368 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 . **. ** An O(
15369 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 n^2) insertion s
1536a 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 ort algorithm is
1536b 20 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65 used, but since
1536c 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 . ** n is never
1536d 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61 more than NB (a
1536e 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 small constant)
1536f 2c 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 , that should.
15370 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 ** not be a prob
15371 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 lem.. **. ** W
15372 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 hen NB==3, this
15373 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e one optimization
15374 20 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62 makes the datab
15375 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 ase. ** about 2
15376 35 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61 5% faster for la
15377 72 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 rge insertions a
15378 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 nd deletions..
15379 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c */. for(i=0; i<
1537a 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 k-1; i++){. i
1537b 6e 74 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 nt minV = pgnoNe
1537c 77 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69 w[i];. int mi
1537d 6e 49 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 nI = i;. for(
1537e 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 j=i+1; j<k; j++)
1537f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f {. if( pgno
15380 4e 65 77 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64 New[j]<(unsigned
15381 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 )minV ){.
15382 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 minI = j;.
15383 20 20 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 minV = pgnoNe
15384 77 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 w[j];. }.
15385 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 }. if( minI
15386 3e 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 >i ){. int
15387 74 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 t;. MemPage
15388 20 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 *pT;. t =
15389 70 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 pgnoNew[i];.
1538a 20 20 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b pT = apNew[i];
1538b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 . pgnoNew[i
1538c 5d 20 3d 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 ] = pgnoNew[minI
1538d 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 ];. apNew[i
1538e 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b ] = apNew[minI];
1538f 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d . pgnoNew[m
15390 69 6e 49 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20 inI] = t;.
15391 61 70 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 apNew[minI] = pT
15392 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 ;. }. }. TR
15393 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f ACE(("BALANCE: o
15394 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20 6e 65 ld: %d %d %d ne
15395 77 3a 20 25 64 28 25 64 29 20 25 64 28 25 64 29 w: %d(%d) %d(%d)
15396 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 %d(%d) %d(%d) %
15397 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67 d(%d)\n",. pg
15398 6e 6f 4f 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e noOld[0], . n
15399 4f 6c 64 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64 Old>=2 ? pgnoOld
1539a 5b 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c [1] : 0,. nOl
1539b 64 3e 3d 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32 d>=3 ? pgnoOld[2
1539c 5d 20 3a 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e ] : 0,. pgnoN
1539d 65 77 5b 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c ew[0], szNew[0],
1539e 0a 20 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70 . nNew>=2 ? p
1539f 67 6e 6f 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e gnoNew[1] : 0, n
153a0 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 New>=2 ? szNew[1
153a1 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e ] : 0,. nNew>
153a2 3d 33 20 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20 =3 ? pgnoNew[2]
153a3 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 : 0, nNew>=3 ? s
153a4 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 zNew[2] : 0,.
153a5 20 6e 4e 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e nNew>=4 ? pgnoN
153a6 65 77 5b 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e ew[3] : 0, nNew>
153a7 3d 34 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 =4 ? szNew[3] :
153a8 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 0,. nNew>=5 ?
153a9 20 70 67 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c pgnoNew[4] : 0,
153aa 20 6e 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 nNew>=5 ? szNew
153ab 5b 34 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a [4] : 0));.. /*
153ac 0a 20 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 . ** Evenly dis
153ad 74 72 69 62 75 74 65 20 74 68 65 20 64 61 74 61 tribute the data
153ae 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 in apCell[] acr
153af 6f 73 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 oss the new page
153b0 73 2e 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 s.. ** Insert d
153b1 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 ivider cells int
153b2 6f 20 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 o pParent as nec
153b3 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a essary.. */. j
153b4 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b = 0;. for(i=0;
153b5 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 i<nNew; i++){.
153b6 20 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 /* Assemble t
153b7 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 he new sibling p
153b8 61 67 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 age. */. MemP
153b9 61 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 age *pNew = apNe
153ba 77 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 w[i];. assert
153bb 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( j<nMaxCells );
153bc 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 . assert( pNe
153bd 77 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77 w->pgno==pgnoNew
153be 5b 69 5d 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 [i] );. zeroP
153bf 61 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c age(pNew, pageFl
153c0 61 67 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 ags);. assemb
153c1 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 lePage(pNew, cnt
153c2 4e 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c New[i]-j, &apCel
153c3 6c 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d l[j], &szCell[j]
153c4 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
153c5 4e 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 New->nCell>0 ||
153c6 28 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e (nNew==1 && cntN
153c7 65 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 ew[0]==0) );.
153c8 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e assert( pNew->n
153c9 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a Overflow==0 );..
153ca 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
153cb 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d s an auto-vacuum
153cc 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 database, updat
153cd 65 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 e the pointer ma
153ce 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a p entries. **
153cf 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 that point to t
153d0 68 65 20 73 69 62 6c 69 6e 67 73 20 74 68 61 74 he siblings that
153d1 20 77 65 72 65 20 72 65 61 72 72 61 6e 67 65 64 were rearranged
153d2 2e 20 54 68 65 73 65 20 63 61 6e 20 62 65 3a 20 . These can be:
153d3 6c 65 66 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c left. ** chil
153d4 64 72 65 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74 dren of cells, t
153d5 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f he right-child o
153d6 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 6f f the page, or o
153d7 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 verflow pages.
153d8 20 20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 ** pointed to
153d9 62 79 20 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f by cells.. */
153da 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 . if( ISAUTOV
153db 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 66 ACUUM ){. f
153dc 6f 72 28 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77 or(k=j; k<cntNew
153dd 5b 69 5d 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 [i]; k++){.
153de 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61 assert( k<nMa
153df 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 xCells );.
153e0 20 20 69 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d if( aFrom[k]==
153e1 30 78 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 0xFF || apCopy[a
153e2 46 72 6f 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d From[k]]->pgno!=
153e3 70 4e 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 pNew->pgno ){.
153e4 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 rc = ptr
153e5 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c mapPutOvfl(pNew,
153e6 20 6b 2d 6a 29 3b 0a 20 20 20 20 20 20 20 20 20 k-j);.
153e7 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
153e8 4f 4b 20 26 26 20 6c 65 61 66 43 6f 72 72 65 63 OK && leafCorrec
153e9 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 tion==0 ){.
153ea 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d rc = ptrm
153eb 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62 apPut(pBt, get4b
153ec 79 74 65 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20 yte(apCell[k]),
153ed 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e PTRMAP_BTREE, pN
153ee 65 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 ew->pgno);.
153ef 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
153f0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
153f1 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
153f2 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 goto balance_c
153f3 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 leanup;.
153f4 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
153f5 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
153f6 20 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a j = cntNew[i];.
153f7 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 . /* If the s
153f8 69 62 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65 ibling page asse
153f9 6d 62 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20 mbled above was
153fa 6e 6f 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f not the right-mo
153fb 73 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 st sibling,.
153fc 2a 2a 20 69 6e 73 65 72 74 20 61 20 64 69 76 69 ** insert a divi
153fd 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 der cell into th
153fe 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 e parent page..
153ff 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c */. if( i<
15400 6e 4e 65 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c nNew-1 && j<nCel
15401 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 l ){. u8 *p
15402 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a Cell;. u8 *
15403 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 pTemp;. int
15404 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 sz;.. asse
15405 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 rt( j<nMaxCells
15406 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d );. pCell =
15407 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 apCell[j];.
15408 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d sz = szCell[j]
15409 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f + leafCorrectio
1540a 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d n;. pTemp =
1540b 20 26 61 53 70 61 63 65 32 5b 69 53 70 61 63 65 &aSpace2[iSpace
1540c 32 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 2];. if( !p
1540d 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 New->leaf ){.
1540e 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 memcpy(&pNe
1540f 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65 w->aData[8], pCe
15410 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 ll, 4);.
15411 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d if( ISAUTOVACUUM
15412 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 61 . && (a
15413 46 72 6f 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c From[j]==0xFF ||
15414 20 61 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d apCopy[aFrom[j]
15415 5d 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 ]->pgno!=pNew->p
15416 67 6e 6f 29 0a 20 20 20 20 20 20 20 20 29 7b 0a gno). ){.
15417 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 rc = p
15418 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65 trmapPut(pBt, ge
15419 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 50 t4byte(pCell), P
1541a 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 TRMAP_BTREE, pNe
1541b 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 w->pgno);.
1541c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1541d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1541e 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 goto balanc
1541f 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 e_cleanup;.
15420 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
15421 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
15422 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 leafData ){.
15423 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 /* If the t
15424 72 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 ree is a leaf-da
15425 74 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 ta tree, and the
15426 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 siblings are le
15427 61 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a aves, . *
15428 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 * then there is
15429 6e 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 no divider cell
1542a 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 in apCell[]. Ins
1542b 74 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 tead, the divide
1542c 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 r . ** ce
1542d 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 ll consists of t
1542e 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 he integer key f
1542f 6f 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 or the right-mos
15430 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 t cell of .
15431 20 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e ** the siblin
15432 67 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 g-page assembled
15433 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 above only..
15434 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
15435 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
15436 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 j--;.
15437 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
15438 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 ParseCellPtr(pNe
15439 77 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 w, apCell[j], &i
1543a 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43 nfo);. pC
1543b 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 ell = pTemp;.
1543c 20 20 20 20 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 fillInCell(
1543d 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 2c 20 pParent, pCell,
1543e 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 2c 0, info.nKey, 0,
1543f 20 30 2c 20 30 2c 20 26 73 7a 29 3b 0a 20 20 20 0, 0, &sz);.
15440 20 20 20 20 20 70 54 65 6d 70 20 3d 20 30 3b 0a pTemp = 0;.
15441 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
15442 20 20 20 20 20 70 43 65 6c 6c 20 2d 3d 20 34 3b pCell -= 4;
15443 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f 62 73 63 . /* Obsc
15444 75 72 65 20 63 61 73 65 20 66 6f 72 20 6e 6f 6e ure case for non
15445 2d 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 73 -leaf-data trees
15446 3a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 61 74 : If the cell at
15447 20 70 43 65 6c 6c 20 77 61 73 0a 20 20 20 20 20 pCell was.
15448 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 ** previously
15449 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 65 61 stored on a lea
1544a 66 20 6e 6f 64 65 2c 20 61 6e 64 20 69 74 73 20 f node, and its
1544b 72 65 70 6f 72 74 65 64 20 73 69 7a 65 20 77 61 reported size wa
1544c 73 20 34 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 s 4. ** b
1544d 79 74 65 73 2c 20 74 68 65 6e 20 69 74 20 6d 61 ytes, then it ma
1544e 79 20 61 63 74 75 61 6c 6c 79 20 62 65 20 73 6d y actually be sm
1544f 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 73 20 aller than this
15450 0a 20 20 20 20 20 20 20 20 2a 2a 20 28 73 65 65 . ** (see
15451 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 sqlite3BtreePar
15452 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 34 20 62 seCellPtr(), 4 b
15453 79 74 65 73 20 69 73 20 74 68 65 20 6d 69 6e 69 ytes is the mini
15454 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 20 20 20 mum size of.
15455 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c 29 ** any cell)
15456 2e 20 42 75 74 20 69 74 20 69 73 20 69 6d 70 6f . But it is impo
15457 72 74 61 6e 74 20 74 6f 20 70 61 73 73 20 74 68 rtant to pass th
15458 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 74 e correct size t
15459 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e o . ** in
1545a 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20 72 sertCell(), so r
1545b 65 70 61 72 73 65 20 74 68 65 20 63 65 6c 6c 20 eparse the cell
1545c 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a now.. **.
1545d 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 ** Note
1545e 74 68 61 74 20 74 68 69 73 20 63 61 6e 20 6e 65 that this can ne
1545f 76 65 72 20 68 61 70 70 65 6e 20 69 6e 20 61 6e ver happen in an
15460 20 53 51 4c 69 74 65 20 64 61 74 61 20 66 69 6c SQLite data fil
15461 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 20 20 20 e, as all.
15462 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 61 ** cells are a
15463 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 2e t least 4 bytes.
15464 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 It only happens
15465 20 69 6e 20 62 2d 74 72 65 65 73 20 75 73 65 64 in b-trees used
15466 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 65 . ** to e
15467 76 61 6c 75 61 74 65 20 22 49 4e 20 28 53 45 4c valuate "IN (SEL
15468 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73 69 ECT ...)" and si
15469 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a 20 milar clauses..
1546a 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
1546b 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d if( szCell[j]=
1546c 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =4 ){.
1546d 61 73 73 65 72 74 28 6c 65 61 66 43 6f 72 72 65 assert(leafCorre
1546e 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 20 ction==4);.
1546f 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 sz = cellSi
15470 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 70 zePtr(pParent, p
15471 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d Cell);. }
15472 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
15473 53 70 61 63 65 32 20 2b 3d 20 73 7a 3b 0a 20 20 Space2 += sz;.
15474 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d assert( sz<=
15475 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 pBt->pageSize/4
15476 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
15477 20 69 53 70 61 63 65 32 3c 3d 70 42 74 2d 3e 70 iSpace2<=pBt->p
15478 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 ageSize );.
15479 20 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c rc = insertCell
1547a 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c (pParent, nxDiv,
1547b 20 70 43 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d pCell, sz, pTem
1547c 70 2c 20 34 29 3b 0a 20 20 20 20 20 20 69 66 28 p, 4);. if(
1547d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1547e 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
1547f 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 70 75 74 eanup;. put
15480 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 4byte(findOverfl
15481 6f 77 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 6e owCell(pParent,n
15482 78 44 69 76 29 2c 20 70 4e 65 77 2d 3e 70 67 6e xDiv), pNew->pgn
15483 6f 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 o);.. /* If
15484 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f this is an auto
15485 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 -vacuum database
15486 2c 20 61 6e 64 20 6e 6f 74 20 61 20 6c 65 61 66 , and not a leaf
15487 2d 64 61 74 61 20 74 72 65 65 2c 0a 20 20 20 20 -data tree,.
15488 20 20 2a 2a 20 74 68 65 6e 20 75 70 64 61 74 65 ** then update
15489 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
1548a 20 77 69 74 68 20 61 6e 20 65 6e 74 72 79 20 66 with an entry f
1548b 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 or the overflow
1548c 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 page. ** th
1548d 61 74 20 74 68 65 20 63 65 6c 6c 20 6a 75 73 74 at the cell just
1548e 20 69 6e 73 65 72 74 65 64 20 70 6f 69 6e 74 73 inserted points
1548f 20 74 6f 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 to (if any)..
15490 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
15491 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 26 26 ISAUTOVACUUM &&
15492 20 21 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 !leafData ){.
15493 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
15494 70 50 75 74 4f 76 66 6c 28 70 50 61 72 65 6e 74 pPutOvfl(pParent
15495 2c 20 6e 78 44 69 76 29 3b 0a 20 20 20 20 20 20 , nxDiv);.
15496 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
15497 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
15498 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
15499 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d eanup;. }
1549a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a . }. j
1549b 2b 2b 3b 0a 20 20 20 20 20 20 6e 78 44 69 76 2b ++;. nxDiv+
1549c 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a +;. }.. /*
1549d 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 Set the pointer
1549e 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 74 -map entry for t
1549f 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 he new sibling p
154a0 61 67 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 age. */. if(
154a1 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a ISAUTOVACUUM ){.
154a2 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 rc = ptrma
154a3 70 50 75 74 28 70 42 74 2c 20 70 4e 65 77 2d 3e pPut(pBt, pNew->
154a4 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 pgno, PTRMAP_BTR
154a5 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e EE, pParent->pgn
154a6 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 o);. if( rc
154a7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
154a8 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 goto bala
154a9 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
154aa 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
154ab 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e 43 65 6c assert( j==nCel
154ac 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e l );. assert( n
154ad 4f 6c 64 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 Old>0 );. asser
154ae 74 28 20 6e 4e 65 77 3e 30 20 29 3b 0a 20 20 69 t( nNew>0 );. i
154af 66 28 20 28 70 61 67 65 46 6c 61 67 73 20 26 20 f( (pageFlags &
154b0 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 29 7b 0a PTF_LEAF)==0 ){.
154b1 20 20 20 20 75 38 20 2a 7a 43 68 69 6c 64 20 3d u8 *zChild =
154b2 20 26 61 70 43 6f 70 79 5b 6e 4f 6c 64 2d 31 5d &apCopy[nOld-1]
154b3 2d 3e 61 44 61 74 61 5b 38 5d 3b 0a 20 20 20 20 ->aData[8];.
154b4 6d 65 6d 63 70 79 28 26 61 70 4e 65 77 5b 6e 4e memcpy(&apNew[nN
154b5 65 77 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 2c ew-1]->aData[8],
154b6 20 7a 43 68 69 6c 64 2c 20 34 29 3b 0a 20 20 20 zChild, 4);.
154b7 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
154b8 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 M ){. rc =
154b9 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 ptrmapPut(pBt, g
154ba 65 74 34 62 79 74 65 28 7a 43 68 69 6c 64 29 2c et4byte(zChild),
154bb 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 61 PTRMAP_BTREE, a
154bc 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70 67 pNew[nNew-1]->pg
154bd 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 no);. if( r
154be 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
154bf 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c goto bal
154c0 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
154c1 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
154c2 20 20 69 66 28 20 6e 78 44 69 76 3d 3d 70 50 61 if( nxDiv==pPa
154c3 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 rent->nCell+pPar
154c4 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 ent->nOverflow )
154c5 7b 0a 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d {. /* Right-m
154c6 6f 73 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74 ost sibling is t
154c7 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 he right-most ch
154c8 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 20 2a ild of pParent *
154c9 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 /. put4byte(&
154ca 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 pParent->aData[p
154cb 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 Parent->hdrOffse
154cc 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e t+8], pgnoNew[nN
154cd 65 77 2d 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b ew-1]);. }else{
154ce 0a 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f . /* Right-mo
154cf 73 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 st sibling is th
154d0 65 20 6c 65 66 74 20 63 68 69 6c 64 20 6f 66 20 e left child of
154d1 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 the first entry
154d2 69 6e 20 70 50 61 72 65 6e 74 0a 20 20 20 20 2a in pParent. *
154d3 2a 20 70 61 73 74 20 74 68 65 20 72 69 67 68 74 * past the right
154d4 2d 6d 6f 73 74 20 64 69 76 69 64 65 72 20 65 6e -most divider en
154d5 74 72 79 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 try */. put4b
154d6 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 yte(findOverflow
154d7 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 Cell(pParent, nx
154d8 44 69 76 29 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e Div), pgnoNew[nN
154d9 65 77 2d 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f ew-1]);. }.. /
154da 2a 0a 20 20 2a 2a 20 42 61 6c 61 6e 63 65 20 74 *. ** Balance t
154db 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 he parent page.
154dc 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 Note that the c
154dd 75 72 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 urrent page (pPa
154de 67 65 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 68 ge) might. ** h
154df 61 76 65 20 62 65 65 6e 20 61 64 64 65 64 20 74 ave been added t
154e0 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 73 o the freelist s
154e1 6f 20 69 74 20 6d 69 67 68 74 20 6e 6f 20 6c 6f o it might no lo
154e2 6e 67 65 72 20 62 65 20 69 6e 69 74 69 61 6c 69 nger be initiali
154e3 7a 65 64 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68 zed.. ** But th
154e4 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 77 69 e parent page wi
154e5 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69 ll always be ini
154e6 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 tialized.. */.
154e7 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 assert( pParent
154e8 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 73 71 ->isInit );. sq
154e9 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 lite3ScratchFree
154ea 28 61 70 43 65 6c 6c 29 3b 0a 20 20 61 70 43 65 (apCell);. apCe
154eb 6c 6c 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61 73 ll = 0;. releas
154ec 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
154ed 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 pCur->iPage--;.
154ee 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 rc = balance(pC
154ef 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20 2f 2a 0a ur, 0);. . /*.
154f0 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 65 66 ** Cleanup bef
154f1 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 20 ore returning..
154f2 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 61 */.balance_clea
154f3 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 50 61 nup:. sqlite3Pa
154f4 67 65 46 72 65 65 28 61 53 70 61 63 65 32 29 3b geFree(aSpace2);
154f5 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 . sqlite3Scratc
154f6 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 hFree(apCell);.
154f7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 for(i=0; i<nOld
154f8 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 ; i++){. rele
154f9 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d asePage(apOld[i]
154fa 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 );. }. for(i=0
154fb 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a ; i<nNew; i++){.
154fc 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
154fd 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a apNew[i]);. }..
154fe 20 20 2f 2a 20 72 65 6c 65 61 73 65 50 61 67 65 /* releasePage
154ff 28 70 50 61 72 65 6e 74 29 3b 20 2a 2f 0a 20 20 (pParent); */.
15500 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a TRACE(("BALANCE:
15501 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 25 finished with %
15502 64 3a 20 6f 6c 64 3d 25 64 20 6e 65 77 3d 25 64 d: old=%d new=%d
15503 20 63 65 6c 6c 73 3d 25 64 5c 6e 22 2c 0a 20 20 cells=%d\n",.
15504 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 pPage->p
15505 67 6e 6f 2c 20 6e 4f 6c 64 2c 20 6e 4e 65 77 2c gno, nOld, nNew,
15506 20 6e 43 65 6c 6c 29 29 3b 0a 0a 20 20 72 65 74 nCell));.. ret
15507 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
15508 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
15509 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 68 65 20 called for the
1550a 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 root page of a b
1550b 74 72 65 65 20 77 68 65 6e 20 74 68 65 20 72 6f tree when the ro
1550c 6f 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 ot.** page conta
1550d 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 20 54 ins no cells. T
1550e 68 69 73 20 69 73 20 61 6e 20 6f 70 70 6f 72 74 his is an opport
1550f 75 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20 74 68 unity to make th
15510 65 20 74 72 65 65 0a 2a 2a 20 73 68 61 6c 6c 6f e tree.** shallo
15511 77 65 72 20 62 79 20 6f 6e 65 20 6c 65 76 65 6c wer by one level
15512 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
15513 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f 77 65 balance_shallowe
15514 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 r(BtCursor *pCur
15515 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 ){. MemPage *pP
15516 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
15517 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f /* Root page o
15518 66 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 4d 65 f B-Tree */. Me
15519 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 20 mPage *pChild;
1551a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1551b 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 70 61 67 e only child pag
1551c 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 20 20 e of pPage */.
1551d 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 3b 20 Pgno pgnoChild;
1551e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1551f 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 Page number for
15520 70 43 68 69 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 pChild */. int
15521 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
15522 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
15523 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 rn code from sub
15524 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20 20 procedures */.
15525 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
15526 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15527 2f 2a 20 54 68 65 20 6d 61 69 6e 20 42 54 72 65 /* The main BTre
15528 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
15529 20 69 6e 74 20 6d 78 43 65 6c 6c 50 65 72 50 61 int mxCellPerPa
1552a 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ge; /*
1552b 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 Maximum number
1552c 6f 66 20 63 65 6c 6c 73 20 70 65 72 20 70 61 67 of cells per pag
1552d 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 e */. u8 **apCe
1552e 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
1552f 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 /* All cells
15530 20 66 72 6f 6d 20 70 61 67 65 73 20 62 65 69 6e from pages bein
15531 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 g balanced */.
15532 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 u16 *szCell;
15533 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15534 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c Local size of al
15535 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a 20 20 61 73 l cells */.. as
15536 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 sert( pCur->iPag
15537 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61 67 65 20 e==0 );. pPage
15538 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 = pCur->apPage[0
15539 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 ];.. assert( pP
1553a 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b age->nCell==0 );
1553b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1553c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
1553d 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
1553e 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 );. pBt = pPag
1553f 65 2d 3e 70 42 74 3b 0a 20 20 6d 78 43 65 6c 6c e->pBt;. mxCell
15540 50 65 72 50 61 67 65 20 3d 20 4d 58 5f 43 45 4c PerPage = MX_CEL
15541 4c 28 70 42 74 29 3b 0a 20 20 61 70 43 65 6c 6c L(pBt);. apCell
15542 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
15543 28 20 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 2a ( mxCellPerPage*
15544 28 73 69 7a 65 6f 66 28 75 38 2a 29 2b 73 69 7a (sizeof(u8*)+siz
15545 65 6f 66 28 75 31 36 29 29 20 29 3b 0a 20 20 69 eof(u16)) );. i
15546 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 20 72 f( apCell==0 ) r
15547 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
15548 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 28 EM;. szCell = (
15549 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6d 78 43 u16*)&apCell[mxC
1554a 65 6c 6c 50 65 72 50 61 67 65 5d 3b 0a 20 20 69 ellPerPage];. i
1554b 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 f( pPage->leaf )
1554c 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 {. /* The tab
1554d 6c 65 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 le is completely
1554e 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 54 52 empty */. TR
1554f 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 65 ACE(("BALANCE: e
15550 6d 70 74 79 20 74 61 62 6c 65 20 25 64 5c 6e 22 mpty table %d\n"
15551 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b , pPage->pgno));
15552 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
15553 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 The root page i
15554 73 20 65 6d 70 74 79 20 62 75 74 20 68 61 73 20 s empty but has
15555 6f 6e 65 20 63 68 69 6c 64 2e 20 20 54 72 61 6e one child. Tran
15556 73 66 65 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 sfer the. **
15557 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d information from
15558 20 74 68 61 74 20 6f 6e 65 20 63 68 69 6c 64 20 that one child
15559 69 6e 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 into the root pa
1555a 67 65 20 69 66 20 69 74 20 0a 20 20 20 20 2a 2a ge if it . **
1555b 20 77 69 6c 6c 20 66 69 74 2e 20 20 54 68 69 73 will fit. This
1555c 20 72 65 64 75 63 65 73 20 74 68 65 20 64 65 70 reduces the dep
1555d 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 62 th of the tree b
1555e 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 y one.. **.
1555f 20 20 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74 ** If the root
15560 20 70 61 67 65 20 69 73 20 70 61 67 65 20 31 2c page is page 1,
15561 20 69 74 20 68 61 73 20 6c 65 73 73 20 73 70 61 it has less spa
15562 63 65 20 61 76 61 69 6c 61 62 6c 65 20 74 68 61 ce available tha
15563 6e 0a 20 20 20 20 2a 2a 20 69 74 73 20 63 68 69 n. ** its chi
15564 6c 64 20 28 64 75 65 20 74 6f 20 74 68 65 20 31 ld (due to the 1
15565 30 30 20 62 79 74 65 20 68 65 61 64 65 72 20 74 00 byte header t
15566 68 61 74 20 6f 63 63 75 72 73 20 61 74 20 74 68 hat occurs at th
15567 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 e beginning.
15568 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ** of the databa
15569 73 65 20 66 6c 65 29 2c 20 73 6f 20 69 74 20 6d se fle), so it m
1556a 69 67 68 74 20 6e 6f 74 20 62 65 20 61 62 6c 65 ight not be able
1556b 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6f 66 20 to hold all of
1556c 74 68 65 20 0a 20 20 20 20 2a 2a 20 69 6e 66 6f the . ** info
1556d 72 6d 61 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c rmation currentl
1556e 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 y contained in t
1556f 68 65 20 63 68 69 6c 64 2e 20 20 49 66 20 74 68 he child. If th
15570 69 73 20 69 73 20 74 68 65 20 0a 20 20 20 20 2a is is the . *
15571 2a 20 63 61 73 65 2c 20 74 68 65 6e 20 64 6f 20 * case, then do
15572 6e 6f 74 20 64 6f 20 74 68 65 20 74 72 61 6e 73 not do the trans
15573 66 65 72 2e 20 20 4c 65 61 76 65 20 70 61 67 65 fer. Leave page
15574 20 31 20 65 6d 70 74 79 20 65 78 63 65 70 74 0a 1 empty except.
15575 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 72 ** for the r
15576 69 67 68 74 2d 70 6f 69 6e 74 65 72 20 74 6f 20 ight-pointer to
15577 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 2e 20 the child page.
15578 20 54 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 The child page
15579 62 65 63 6f 6d 65 73 0a 20 20 20 20 2a 2a 20 74 becomes. ** t
1557a 68 65 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 he virtual root
1557b 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 20 20 20 of the tree..
1557c 20 2a 2f 0a 20 20 20 20 56 56 41 5f 4f 4e 4c 59 */. VVA_ONLY
1557d 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68 75 ( pCur->pagesShu
1557e 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 20 20 20 ffled = 1 );.
1557f 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 67 65 74 pgnoChild = get
15580 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 4byte(&pPage->aD
15581 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 ata[pPage->hdrOf
15582 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 61 73 fset+8]);. as
15583 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c 64 3e sert( pgnoChild>
15584 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
15585 20 70 67 6e 6f 43 68 69 6c 64 3c 3d 70 61 67 65 pgnoChild<=page
15586 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 rPagecount(pPage
15587 2d 3e 70 42 74 29 20 29 3b 0a 20 20 20 20 72 63 ->pBt) );. rc
15588 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
15589 65 74 50 61 67 65 28 70 50 61 67 65 2d 3e 70 42 etPage(pPage->pB
1558a 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 26 70 t, pgnoChild, &p
1558b 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20 20 20 69 Child, 0);. i
1558c 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 f( rc ) goto end
1558d 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 _shallow_balance
1558e 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d ;. if( pPage-
1558f 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 >pgno==1 ){.
15590 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
15591 72 65 65 49 6e 69 74 50 61 67 65 28 70 43 68 69 reeInitPage(pChi
15592 6c 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ld);. if( r
15593 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73 68 61 c ) goto end_sha
15594 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a 20 20 llow_balance;.
15595 20 20 20 20 61 73 73 65 72 74 28 20 70 43 68 69 assert( pChi
15596 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 ld->nOverflow==0
15597 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 );. if( pC
15598 68 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d 31 30 30 hild->nFree>=100
15599 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
1559a 68 65 20 63 68 69 6c 64 20 69 6e 66 6f 72 6d 61 he child informa
1559b 74 69 6f 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e tion will fit on
1559c 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 the root page,
1559d 73 6f 20 64 6f 20 74 68 65 0a 20 20 20 20 20 20 so do the.
1559e 20 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a 20 20 20 ** copy */.
1559f 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
155a0 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 zeroPage(pPa
155a1 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 ge, pChild->aDat
155a2 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 66 a[0]);. f
155a3 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 69 6c 64 or(i=0; i<pChild
155a4 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 ->nCell; i++){.
155a5 20 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c 5b apCell[
155a6 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 43 i] = findCell(pC
155a7 68 69 6c 64 2c 69 29 3b 0a 20 20 20 20 20 20 20 hild,i);.
155a8 20 20 20 73 7a 43 65 6c 6c 5b 69 5d 20 3d 20 63 szCell[i] = c
155a9 65 6c 6c 53 69 7a 65 50 74 72 28 70 43 68 69 6c ellSizePtr(pChil
155aa 64 2c 20 61 70 43 65 6c 6c 5b 69 5d 29 3b 0a 20 d, apCell[i]);.
155ab 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
155ac 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 50 assemblePage(pP
155ad 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 6e 43 65 age, pChild->nCe
155ae 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 ll, apCell, szCe
155af 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ll);. /*
155b0 43 6f 70 79 20 74 68 65 20 72 69 67 68 74 2d 70 Copy the right-p
155b1 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 68 ointer of the ch
155b2 69 6c 64 20 74 6f 20 74 68 65 20 70 61 72 65 6e ild to the paren
155b3 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 t. */. pu
155b4 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
155b5 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
155b6 66 66 73 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 ffset+8], .
155b7 20 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 get4byte(
155b8 26 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 &pChild->aData[p
155b9 43 68 69 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 Child->hdrOffset
155ba 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 66 +8]));. f
155bb 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b reePage(pChild);
155bc 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 . TRACE((
155bd 22 42 41 4c 41 4e 43 45 3a 20 63 68 69 6c 64 20 "BALANCE: child
155be 25 64 20 74 72 61 6e 73 66 65 72 20 74 6f 20 70 %d transfer to p
155bf 61 67 65 20 31 5c 6e 22 2c 20 70 43 68 69 6c 64 age 1\n", pChild
155c0 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 ->pgno));.
155c1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f }else{. /
155c2 2a 20 54 68 65 20 63 68 69 6c 64 20 68 61 73 20 * The child has
155c3 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e more information
155c4 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 6f that will fit o
155c5 6e 20 74 68 65 20 72 6f 6f 74 2e 0a 20 20 20 20 n the root..
155c6 20 20 20 20 2a 2a 20 54 68 65 20 74 72 65 65 20 ** The tree
155c7 69 73 20 61 6c 72 65 61 64 79 20 62 61 6c 61 6e is already balan
155c8 63 65 64 2e 20 20 44 6f 20 6e 6f 74 68 69 6e 67 ced. Do nothing
155c9 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 54 52 41 . */. TRA
155ca 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 CE(("BALANCE: ch
155cb 69 6c 64 20 25 64 20 77 69 6c 6c 20 6e 6f 74 20 ild %d will not
155cc 66 69 74 20 6f 6e 20 70 61 67 65 20 31 5c 6e 22 fit on page 1\n"
155cd 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 , pChild->pgno))
155ce 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
155cf 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 lse{. memcp
155d0 79 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 y(pPage->aData,
155d1 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 2c 20 70 pChild->aData, p
155d2 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
155d3 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70 50 eSize);. pP
155d4 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b age->isInit = 0;
155d5 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
155d6 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 te3BtreeInitPage
155d7 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 61 (pPage);. a
155d8 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
155d9 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 66 72 E_OK );. fr
155da 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a eePage(pChild);.
155db 20 20 20 20 20 20 54 52 41 43 45 28 28 22 42 41 TRACE(("BA
155dc 4c 41 4e 43 45 3a 20 74 72 61 6e 73 66 65 72 20 LANCE: transfer
155dd 63 68 69 6c 64 20 25 64 20 69 6e 74 6f 20 72 6f child %d into ro
155de 6f 74 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 ot %d\n",.
155df 20 20 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e pChild->
155e0 70 67 6e 6f 2c 20 70 50 61 67 65 2d 3e 70 67 6e pgno, pPage->pgn
155e1 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 o));. }. a
155e2 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f ssert( pPage->nO
155e3 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 23 69 verflow==0 );.#i
155e4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
155e5 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
155e6 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
155e7 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 M ){. rc =
155e8 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
155e9 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 pPage);. }.#e
155ea 6e 64 69 66 0a 20 20 20 20 72 65 6c 65 61 73 65 ndif. release
155eb 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 Page(pChild);.
155ec 7d 0a 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 }.end_shallow_ba
155ed 6c 61 6e 63 65 3a 0a 20 20 73 71 6c 69 74 65 33 lance:. sqlite3
155ee 5f 66 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 _free(apCell);.
155ef 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
155f0 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 /*.** The root p
155f1 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c 6c 0a age is overfull.
155f2 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 **.** When this
155f3 68 61 70 70 65 6e 73 2c 20 43 72 65 61 74 65 20 happens, Create
155f4 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 a new child page
155f5 20 61 6e 64 20 63 6f 70 79 20 74 68 65 0a 2a 2a and copy the.**
155f6 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
155f7 20 72 6f 6f 74 20 69 6e 74 6f 20 74 68 65 20 63 root into the c
155f8 68 69 6c 64 2e 20 20 54 68 65 6e 20 6d 61 6b 65 hild. Then make
155f9 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 the root.** pag
155fa 65 20 61 6e 20 65 6d 70 74 79 20 70 61 67 65 20 e an empty page
155fb 77 69 74 68 20 72 69 67 68 74 43 68 69 6c 64 20 with rightChild
155fc 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
155fd 6e 65 77 0a 2a 2a 20 63 68 69 6c 64 2e 20 20 20 new.** child.
155fe 46 69 6e 61 6c 6c 79 2c 20 63 61 6c 6c 20 62 61 Finally, call ba
155ff 6c 61 6e 63 65 5f 69 6e 74 65 72 6e 61 6c 28 29 lance_internal()
15600 20 6f 6e 20 74 68 65 20 6e 65 77 20 63 68 69 6c on the new chil
15601 64 0a 2a 2a 20 74 6f 20 63 61 75 73 65 20 69 74 d.** to cause it
15602 20 74 6f 20 73 70 6c 69 74 2e 0a 2a 2f 0a 73 74 to split..*/.st
15603 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 atic int balance
15604 5f 64 65 65 70 65 72 28 42 74 43 75 72 73 6f 72 _deeper(BtCursor
15605 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 *pCur){. int r
15606 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f c; /
15607 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 66 * Return value f
15608 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65 rom subprocedure
15609 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a s */. MemPage *
1560a 70 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 6f pPage; /* Po
1560b 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 6f 6f inter to the roo
1560c 74 20 70 61 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 t page */. MemP
1560d 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 20 20 20 age *pChild;
1560e 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 /* Pointer to a
1560f 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a new child page *
15610 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 /. Pgno pgnoChi
15611 6c 64 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 20 ld; /* Page
15612 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 number of the ne
15613 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a w child page */.
15614 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b BtShared *pBt;
15615 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
15616 42 54 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75 BTree */. int u
15617 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f sableSize; /
15618 2a 20 54 6f 74 61 6c 20 75 73 61 62 6c 65 20 73 * Total usable s
15619 69 7a 65 20 6f 66 20 61 20 70 61 67 65 20 2a 2f ize of a page */
1561a 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 . u8 *data;
1561b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e /* Conten
1561c 74 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 t of the parent
1561d 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 63 64 page */. u8 *cd
1561e 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ata; /*
1561f 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 Content of the
15620 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20 child page */.
15621 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 int hdr;
15622 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f /* Offset to
15623 20 70 61 67 65 20 68 65 61 64 65 72 20 69 6e 20 page header in
15624 70 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 parent */. int
15625 63 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 cbrk;
15626 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 6f 6e /* Offset to con
15627 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20 63 65 tent of first ce
15628 6c 6c 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a ll in parent */.
15629 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
1562a 3e 69 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 61 >iPage==0 );. a
1562b 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
1562c 61 67 65 5b 30 5d 2d 3e 6e 4f 76 65 72 66 6c 6f age[0]->nOverflo
1562d 77 3e 30 20 29 3b 0a 0a 20 20 56 56 41 5f 4f 4e w>0 );.. VVA_ON
1562e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 LY( pCur->pagesS
1562f 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 20 huffled = 1 );.
15630 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
15631 70 50 61 67 65 5b 30 5d 3b 0a 20 20 70 42 74 20 pPage[0];. pBt
15632 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
15633 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15634 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
15635 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
15636 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
15637 67 65 28 70 42 74 2c 20 26 70 43 68 69 6c 64 2c ge(pBt, &pChild,
15638 20 26 70 67 6e 6f 43 68 69 6c 64 2c 20 70 50 61 &pgnoChild, pPa
15639 67 65 2d 3e 70 67 6e 6f 2c 20 30 29 3b 0a 20 20 ge->pgno, 0);.
1563a 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
1563b 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 rc;. assert( sq
1563c 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
1563d 65 61 62 6c 65 28 70 43 68 69 6c 64 2d 3e 70 44 eable(pChild->pD
1563e 62 50 61 67 65 29 20 29 3b 0a 20 20 75 73 61 62 bPage) );. usab
1563f 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 leSize = pBt->us
15640 61 62 6c 65 53 69 7a 65 3b 0a 20 20 64 61 74 61 ableSize;. data
15641 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
15642 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e . hdr = pPage->
15643 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 63 62 72 hdrOffset;. cbr
15644 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 k = get2byte(&da
15645 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 63 64 ta[hdr+5]);. cd
15646 61 74 61 20 3d 20 70 43 68 69 6c 64 2d 3e 61 44 ata = pChild->aD
15647 61 74 61 3b 0a 20 20 6d 65 6d 63 70 79 28 63 64 ata;. memcpy(cd
15648 61 74 61 2c 20 26 64 61 74 61 5b 68 64 72 5d 2c ata, &data[hdr],
15649 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 pPage->cellOffs
1564a 65 74 2b 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c et+2*pPage->nCel
1564b 6c 2d 68 64 72 29 3b 0a 20 20 6d 65 6d 63 70 79 l-hdr);. memcpy
1564c 28 26 63 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 (&cdata[cbrk], &
1564d 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 61 62 data[cbrk], usab
1564e 6c 65 53 69 7a 65 2d 63 62 72 6b 29 3b 0a 0a 20 leSize-cbrk);..
1564f 20 61 73 73 65 72 74 28 20 70 43 68 69 6c 64 2d assert( pChild-
15650 3e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a 20 20 >isInit==0 );.
15651 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
15652 65 49 6e 69 74 50 61 67 65 28 70 43 68 69 6c 64 eInitPage(pChild
15653 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
15654 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e ITE_OK ){. in
15655 74 20 6e 43 6f 70 79 20 3d 20 70 50 61 67 65 2d t nCopy = pPage-
15656 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69 7a 65 6f >nOverflow*sizeo
15657 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 f(pPage->aOvfl[0
15658 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 ]);. memcpy(p
15659 43 68 69 6c 64 2d 3e 61 4f 76 66 6c 2c 20 70 50 Child->aOvfl, pP
1565a 61 67 65 2d 3e 61 4f 76 66 6c 2c 20 6e 43 6f 70 age->aOvfl, nCop
1565b 79 29 3b 0a 20 20 20 20 70 43 68 69 6c 64 2d 3e y);. pChild->
1565c 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 50 61 67 nOverflow = pPag
1565d 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 e->nOverflow;.
1565e 20 20 69 66 28 20 70 43 68 69 6c 64 2d 3e 6e 4f if( pChild->nO
1565f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 verflow ){.
15660 20 70 43 68 69 6c 64 2d 3e 6e 46 72 65 65 20 3d pChild->nFree =
15661 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 0;. }. as
15662 73 65 72 74 28 20 70 43 68 69 6c 64 2d 3e 6e 43 sert( pChild->nC
15663 65 6c 6c 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c ell==pPage->nCel
15664 6c 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 l );. zeroPag
15665 65 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d e(pPage, pChild-
15666 3e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 >aData[0] & ~PTF
15667 5f 4c 45 41 46 29 3b 0a 20 20 20 20 70 75 74 34 _LEAF);. put4
15668 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
15669 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
1566a 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c set+8], pgnoChil
1566b 64 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22 d);. TRACE(("
1566c 42 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f BALANCE: copy ro
1566d 6f 74 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 ot %d into %d\n"
1566e 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 , pPage->pgno, p
1566f 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 Child->pgno));.
15670 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
15671 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 UUM ){. rc
15672 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c = ptrmapPut(pBt,
15673 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50 pChild->pgno, P
15674 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 TRMAP_BTREE, pPa
15675 67 65 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64 ge->pgno);.#ifnd
15676 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
15677 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
15678 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
15679 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 K ){. rc
1567a 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 = setChildPtrmap
1567b 73 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 s(pChild);.
1567c 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a }.#endif. }.
1567d 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 }.. if( rc==S
1567e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1567f 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a 20 pCur->iPage++;.
15680 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b pCur->apPage[
15681 31 5d 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 20 1] = pChild;.
15682 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 pCur->aiIdx[0]
15683 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 62 61 = 0;. rc = ba
15684 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 lance_nonroot(pC
15685 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ur);. }else{.
15686 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 releasePage(pC
15687 68 69 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 hild);. }.. re
15688 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
15689 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 * The page that
1568a 70 43 75 72 20 63 75 72 72 65 6e 74 6c 79 20 70 pCur currently p
1568b 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 6a 75 73 oints to has jus
1568c 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 t been modified
1568d 69 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e 20 in.** some way.
1568e 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 This function fi
1568f 67 75 72 65 73 20 6f 75 74 20 69 66 20 74 68 69 gures out if thi
15690 73 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d s modification m
15691 65 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65 65 eans the.** tree
15692 20 6e 65 65 64 73 20 74 6f 20 62 65 20 62 61 6c needs to be bal
15693 61 6e 63 65 64 2c 20 61 6e 64 20 69 66 20 73 6f anced, and if so
15694 20 63 61 6c 6c 73 20 74 68 65 20 61 70 70 72 6f calls the appro
15695 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69 6e 67 priate balancing
15696 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a .** routine..**
15697 20 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 69 .** Parameter i
15698 73 49 6e 73 65 72 74 20 69 73 20 74 72 75 65 20 sInsert is true
15699 69 66 20 61 20 6e 65 77 20 63 65 6c 6c 20 77 61 if a new cell wa
1569a 73 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20 s just inserted
1569b 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 into the.** page
1569c 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 , or false other
1569d 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 wise..*/.static
1569e 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 74 43 75 int balance(BtCu
1569f 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
156a0 69 73 49 6e 73 65 72 74 29 7b 0a 20 20 69 6e 74 isInsert){. int
156a1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
156a2 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
156a3 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
156a4 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 0a [pCur->iPage];..
156a5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
156a6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
156a7 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
156a8 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 );. if( pCur->i
156a9 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 Page==0 ){. r
156aa 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
156ab 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
156ac 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
156ad 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
156ae 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
156af 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d >0 ){. rc =
156b0 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 balance_deeper(
156b1 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 pCur);. }.
156b2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
156b3 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65 OK && pPage->nCe
156b4 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 ll==0 ){. r
156b5 63 20 3d 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c c = balance_shal
156b6 6c 6f 77 65 72 28 70 43 75 72 29 3b 0a 20 20 20 lower(pCur);.
156b7 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
156b8 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 if( pPage->nOver
156b9 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 20 20 20 20 flow>0 || .
156ba 20 20 20 28 21 69 73 49 6e 73 65 72 74 20 26 26 (!isInsert &&
156bb 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e 70 50 pPage->nFree>pP
156bc 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
156bd 53 69 7a 65 2a 32 2f 33 29 20 29 7b 0a 20 20 20 Size*2/3) ){.
156be 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f rc = balance_
156bf 6e 6f 6e 72 6f 6f 74 28 70 43 75 72 29 3b 0a 20 nonroot(pCur);.
156c0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
156c1 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
156c2 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
156c3 6b 73 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 74 ks all cursors t
156c4 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 61 62 hat point to tab
156c5 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a 20 le pgnoRoot..**
156c6 49 66 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 20 If any of those
156c7 63 75 72 73 6f 72 73 20 77 65 72 65 20 6f 70 65 cursors were ope
156c8 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d ned with wrFlag=
156c9 3d 30 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e =0 in a differen
156ca 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f t.** database co
156cb 6e 6e 65 63 74 69 6f 6e 20 28 61 20 64 61 74 61 nnection (a data
156cc 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
156cd 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65 20 that shares the
156ce 70 61 67 65 72 0a 2a 2a 20 63 61 63 68 65 20 77 pager.** cache w
156cf 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 ith the current
156d0 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 61 6e 64 20 connection) and
156d1 74 68 61 74 20 6f 74 68 65 72 20 63 6f 6e 6e 65 that other conne
156d2 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 6e 6f 74 ction .** is not
156d3 20 69 6e 20 74 68 65 20 52 65 61 64 55 6e 63 6f in the ReadUnco
156d4 6d 6d 6d 69 74 74 65 64 20 73 74 61 74 65 2c 20 mmmitted state,
156d5 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
156d6 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 e returns .** SQ
156d7 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2a 0a LITE_LOCKED..**.
156d8 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 63 75 ** As well as cu
156d9 72 73 6f 72 73 20 77 69 74 68 20 77 72 46 6c 61 rsors with wrFla
156da 67 3d 3d 30 2c 20 63 75 72 73 6f 72 73 20 77 69 g==0, cursors wi
156db 74 68 20 77 72 46 6c 61 67 3d 3d 31 20 61 6e 64 th wrFlag==1 and
156dc 20 0a 2a 2a 20 69 73 49 6e 63 72 62 6c 6f 62 48 .** isIncrblobH
156dd 61 6e 64 6c 65 3d 3d 31 20 61 72 65 20 61 6c 73 andle==1 are als
156de 6f 20 63 6f 6e 73 69 64 65 72 65 64 20 27 72 65 o considered 're
156df 61 64 27 20 63 75 72 73 6f 72 73 2e 20 49 6e 63 ad' cursors. Inc
156e0 72 65 6d 65 6e 74 61 6c 20 0a 2a 2a 20 62 6c 6f remental .** blo
156e1 62 20 63 75 72 73 6f 72 73 20 61 72 65 20 75 73 b cursors are us
156e2 65 64 20 66 6f 72 20 62 6f 74 68 20 72 65 61 64 ed for both read
156e3 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2e ing and writing.
156e4 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 67 6e 6f .**.** When pgno
156e5 52 6f 6f 74 20 69 73 20 74 68 65 20 72 6f 6f 74 Root is the root
156e6 20 70 61 67 65 20 6f 66 20 61 6e 20 69 6e 74 6b page of an intk
156e7 65 79 20 74 61 62 6c 65 2c 20 74 68 69 73 20 66 ey table, this f
156e8 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f 0a unction is also.
156e9 2a 2a 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 ** responsible f
156ea 6f 72 20 69 6e 76 61 6c 69 64 61 74 69 6e 67 20 or invalidating
156eb 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 incremental blob
156ec 20 63 75 72 73 6f 72 73 20 77 68 65 6e 20 74 68 cursors when th
156ed 65 20 74 61 62 6c 65 20 72 6f 77 0a 2a 2a 20 6f e table row.** o
156ee 6e 20 77 68 69 63 68 20 74 68 65 79 20 61 72 65 n which they are
156ef 20 6f 70 65 6e 65 64 20 69 73 20 64 65 6c 65 74 opened is delet
156f0 65 64 20 6f 72 20 6d 6f 64 69 66 69 65 64 2e 20 ed or modified.
156f1 43 75 72 73 6f 72 73 20 61 72 65 20 69 6e 76 61 Cursors are inva
156f2 6c 69 64 61 74 65 64 0a 2a 2a 20 61 63 63 6f 72 lidated.** accor
156f3 64 69 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c ding to the foll
156f4 6f 77 69 6e 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a owing rules:.**.
156f5 2a 2a 20 20 20 31 29 20 57 68 65 6e 20 42 74 72 ** 1) When Btr
156f6 65 65 43 6c 65 61 72 54 61 62 6c 65 28 29 20 69 eeClearTable() i
156f7 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 s called to comp
156f8 6c 65 74 65 6c 79 20 64 65 6c 65 74 65 20 74 68 letely delete th
156f9 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 20 20 e contents.**
156fa 20 20 20 6f 66 20 61 20 42 2d 54 72 65 65 20 74 of a B-Tree t
156fb 61 62 6c 65 2c 20 70 45 78 63 6c 75 64 65 20 69 able, pExclude i
156fc 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 6e s set to zero an
156fd 64 20 70 61 72 61 6d 65 74 65 72 20 69 52 6f 77 d parameter iRow
156fe 20 69 73 20 0a 2a 2a 20 20 20 20 20 20 73 65 74 is .** set
156ff 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 6e to non-zero. In
15700 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20 69 this case all i
15701 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 ncremental blob
15702 63 75 72 73 6f 72 73 20 6f 70 65 6e 0a 2a 2a 20 cursors open.**
15703 20 20 20 20 20 6f 6e 20 74 68 65 20 74 61 62 6c on the tabl
15704 65 20 72 6f 6f 74 65 64 20 61 74 20 70 67 6e 6f e rooted at pgno
15705 52 6f 6f 74 20 61 72 65 20 69 6e 76 61 6c 69 64 Root are invalid
15706 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 ated..**.** 2)
15707 20 57 68 65 6e 20 42 74 72 65 65 49 6e 73 65 72 When BtreeInser
15708 74 28 29 2c 20 42 74 72 65 65 44 65 6c 65 74 65 t(), BtreeDelete
15709 28 29 20 6f 72 20 42 74 72 65 65 50 75 74 44 61 () or BtreePutDa
1570a 74 61 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74 ta() is called t
1570b 6f 20 0a 2a 2a 20 20 20 20 20 20 6d 6f 64 69 66 o .** modif
1570c 79 20 61 20 74 61 62 6c 65 20 72 6f 77 20 76 69 y a table row vi
1570d 61 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 a an SQL stateme
1570e 6e 74 2c 20 70 45 78 63 6c 75 64 65 20 69 73 20 nt, pExclude is
1570f 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 20 set to the .**
15710 20 20 20 20 77 72 69 74 65 20 63 75 72 73 6f 72 write cursor
15711 20 75 73 65 64 20 74 6f 20 64 6f 20 74 68 65 20 used to do the
15712 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 61 6e 64 modification and
15713 20 70 61 72 61 6d 65 74 65 72 20 69 52 6f 77 20 parameter iRow
15714 69 73 20 73 65 74 0a 2a 2a 20 20 20 20 20 20 74 is set.** t
15715 6f 20 74 68 65 20 69 6e 74 65 67 65 72 20 72 6f o the integer ro
15716 77 20 69 64 20 6f 66 20 74 68 65 20 42 2d 54 72 w id of the B-Tr
15717 65 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 6d ee entry being m
15718 6f 64 69 66 69 65 64 2e 20 55 6e 6c 65 73 73 0a odified. Unless.
15719 2a 2a 20 20 20 20 20 20 70 45 78 63 6c 75 64 65 ** pExclude
1571a 20 69 73 20 69 74 73 65 6c 66 20 61 6e 20 69 6e is itself an in
1571b 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 63 cremental blob c
1571c 75 72 73 6f 72 2c 20 74 68 65 6e 20 61 6c 6c 20 ursor, then all
1571d 69 6e 63 72 65 6d 65 6e 74 61 6c 0a 2a 2a 20 20 incremental.**
1571e 20 20 20 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 blob cursors
1571f 20 6f 70 65 6e 20 6f 6e 20 72 6f 77 20 69 52 6f open on row iRo
15720 77 20 6f 66 20 74 68 65 20 42 2d 54 72 65 65 20 w of the B-Tree
15721 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e are invalidated.
15722 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66 20 62 .**.** 3) If b
15723 6f 74 68 20 70 45 78 63 6c 75 64 65 20 61 6e 64 oth pExclude and
15724 20 69 52 6f 77 20 61 72 65 20 73 65 74 20 74 6f iRow are set to
15725 20 7a 65 72 6f 2c 20 6e 6f 20 69 6e 63 72 65 6d zero, no increm
15726 65 6e 74 61 6c 20 62 6c 6f 62 20 0a 2a 2a 20 20 ental blob .**
15727 20 20 20 20 63 75 72 73 6f 72 73 20 61 72 65 20 cursors are
15728 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a invalidated..*/.
15729 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b static int check
1572a 52 65 61 64 4c 6f 63 6b 73 28 0a 20 20 42 74 72 ReadLocks(. Btr
1572b 65 65 20 2a 70 42 74 72 65 65 2c 20 0a 20 20 50 ee *pBtree, . P
1572c 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 2c 20 0a 20 gno pgnoRoot, .
1572d 20 42 74 43 75 72 73 6f 72 20 2a 70 45 78 63 6c BtCursor *pExcl
1572e 75 64 65 2c 0a 20 20 69 36 34 20 69 52 6f 77 0a ude,. i64 iRow.
1572f 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
15730 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
15731 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b t = pBtree->pBt;
15732 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
15733 20 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 61 pBtree->db;. a
15734 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
15735 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 reeHoldsMutex(pB
15736 74 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 tree) );. for(p
15737 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 =pBt->pCursor; p
15738 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 ; p=p->pNext){.
15739 20 20 20 69 66 28 20 70 3d 3d 70 45 78 63 6c 75 if( p==pExclu
1573a 64 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 de ) continue;.
1573b 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f if( p->pgnoRo
1573c 6f 74 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 20 63 ot!=pgnoRoot ) c
1573d 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 ontinue;.#ifndef
1573e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
1573f 52 42 4c 4f 42 0a 20 20 20 20 69 66 28 20 70 2d RBLOB. if( p-
15740 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c >isIncrblobHandl
15741 65 20 26 26 20 28 20 0a 20 20 20 20 20 20 20 20 e && ( .
15742 20 28 21 70 45 78 63 6c 75 64 65 20 26 26 20 69 (!pExclude && i
15743 52 6f 77 29 0a 20 20 20 20 20 20 7c 7c 20 28 70 Row). || (p
15744 45 78 63 6c 75 64 65 20 26 26 20 21 70 45 78 63 Exclude && !pExc
15745 6c 75 64 65 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 lude->isIncrblob
15746 48 61 6e 64 6c 65 20 26 26 20 70 2d 3e 69 6e 66 Handle && p->inf
15747 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 0a 20 20 o.nKey==iRow).
15748 20 20 29 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 )){. p->e
15749 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 State = CURSOR_I
1574a 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 23 65 NVALID;. }.#e
1574b 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70 2d 3e ndif. if( p->
1574c 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 eState!=CURSOR_V
1574d 41 4c 49 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b ALID ) continue;
1574e 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c . if( p->wrFl
1574f 61 67 3d 3d 30 20 0a 23 69 66 6e 64 65 66 20 53 ag==0 .#ifndef S
15750 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
15751 4c 4f 42 0a 20 20 20 20 20 7c 7c 20 70 2d 3e 69 LOB. || p->i
15752 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 0a sIncrblobHandle.
15753 23 65 6e 64 69 66 0a 20 20 20 20 29 7b 0a 20 20 #endif. ){.
15754 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4f sqlite3 *dbO
15755 74 68 65 72 20 3d 20 70 2d 3e 70 42 74 72 65 65 ther = p->pBtree
15756 2d 3e 64 62 3b 0a 20 20 20 20 20 20 69 66 28 20 ->db;. if(
15757 64 62 4f 74 68 65 72 3d 3d 30 20 7c 7c 0a 20 20 dbOther==0 ||.
15758 20 20 20 20 20 20 20 28 64 62 4f 74 68 65 72 21 (dbOther!
15759 3d 64 62 20 26 26 20 28 64 62 4f 74 68 65 72 2d =db && (dbOther-
1575a 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
1575b 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 ReadUncommitted)
1575c 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ==0) ){.
1575d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f return SQLITE_LO
1575e 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 CKED;. }.
1575f 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
15760 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
15761 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 *.** Insert a ne
15762 77 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 w record into th
15763 65 20 42 54 72 65 65 2e 20 20 54 68 65 20 6b 65 e BTree. The ke
15764 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 y is given by (p
15765 4b 65 79 2c 6e 4b 65 79 29 0a 2a 2a 20 61 6e 64 Key,nKey).** and
15766 20 74 68 65 20 64 61 74 61 20 69 73 20 67 69 76 the data is giv
15767 65 6e 20 62 79 20 28 70 44 61 74 61 2c 6e 44 61 en by (pData,nDa
15768 74 61 29 2e 20 20 54 68 65 20 63 75 72 73 6f 72 ta). The cursor
15769 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f is used only to
1576a 0a 2a 2a 20 64 65 66 69 6e 65 20 77 68 61 74 20 .** define what
1576b 74 61 62 6c 65 20 74 68 65 20 72 65 63 6f 72 64 table the record
1576c 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 should be inser
1576d 74 65 64 20 69 6e 74 6f 2e 20 20 54 68 65 20 63 ted into. The c
1576e 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74 ursor.** is left
1576f 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 72 pointing at a r
15770 61 6e 64 6f 6d 20 6c 6f 63 61 74 69 6f 6e 2e 0a andom location..
15771 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 49 4e 54 **.** For an INT
15772 4b 45 59 20 74 61 62 6c 65 2c 20 6f 6e 6c 79 20 KEY table, only
15773 74 68 65 20 6e 4b 65 79 20 76 61 6c 75 65 20 6f the nKey value o
15774 66 20 74 68 65 20 6b 65 79 20 69 73 20 75 73 65 f the key is use
15775 64 2e 20 20 70 4b 65 79 20 69 73 0a 2a 2a 20 69 d. pKey is.** i
15776 67 6e 6f 72 65 64 2e 20 20 46 6f 72 20 61 20 5a gnored. For a Z
15777 45 52 4f 44 41 54 41 20 74 61 62 6c 65 2c 20 74 ERODATA table, t
15778 68 65 20 70 44 61 74 61 20 61 6e 64 20 6e 44 61 he pData and nDa
15779 74 61 20 61 72 65 20 62 6f 74 68 20 69 67 6e 6f ta are both igno
1577a 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 red..*/.SQLITE_P
1577b 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1577c 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 0a 20 e3BtreeInsert(.
1577d 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c BtCursor *pCur,
1577e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1577f 2f 2a 20 49 6e 73 65 72 74 20 64 61 74 61 20 69 /* Insert data i
15780 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 nto the table of
15781 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a this cursor */.
15782 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b const void *pK
15783 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 ey, i64 nKey,
15784 20 2f 2a 20 54 68 65 20 6b 65 79 20 6f 66 20 74 /* The key of t
15785 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f he new record */
15786 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 . const void *p
15787 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c Data, int nData,
15788 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 6f 66 /* The data of
15789 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 the new record
1578a 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 */. int nZero,
1578b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1578c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1578d 20 65 78 74 72 61 20 30 20 62 79 74 65 73 20 74 extra 0 bytes t
1578e 6f 20 61 70 70 65 6e 64 20 74 6f 20 64 61 74 61 o append to data
1578f 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 */. int append
15790 42 69 61 73 20 20 20 20 20 20 20 20 20 20 20 20 Bias
15791 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
15792 74 68 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 61 this is likely a
15793 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 29 7b 0a 20 n append */.){.
15794 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6c int rc;. int l
15795 6f 63 3b 0a 20 20 69 6e 74 20 73 7a 4e 65 77 3b oc;. int szNew;
15796 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 . int idx;. Me
15797 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 mPage *pPage;.
15798 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d Btree *p = pCur-
15799 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 >pBtree;. BtSha
1579a 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1579b 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 t;. unsigned ch
1579c 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 ar *oldCell;. u
1579d 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e 65 nsigned char *ne
1579e 77 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 20 61 73 wCell = 0;.. as
1579f 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
157a0 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
157a1 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 if( pBt->inTra
157a2 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f nsaction!=TRANS_
157a3 57 52 49 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 WRITE ){. /*
157a4 4d 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61 Must start a tra
157a5 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 20 nsaction before
157a6 64 6f 69 6e 67 20 61 6e 20 69 6e 73 65 72 74 20 doing an insert
157a7 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d */. rc = pBt-
157a8 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 >readOnly ? SQLI
157a9 54 45 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 TE_READONLY : SQ
157aa 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
157ab 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
157ac 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 assert( !pBt->r
157ad 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 69 66 28 eadOnly );. if(
157ae 20 21 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 !pCur->wrFlag )
157af 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
157b0 49 54 45 5f 50 45 52 4d 3b 20 20 20 2f 2a 20 43 ITE_PERM; /* C
157b1 75 72 73 6f 72 20 6e 6f 74 20 6f 70 65 6e 20 66 ursor not open f
157b2 6f 72 20 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 or writing */.
157b3 7d 0a 20 20 69 66 28 20 63 68 65 63 6b 52 65 61 }. if( checkRea
157b4 64 4c 6f 63 6b 73 28 70 43 75 72 2d 3e 70 42 74 dLocks(pCur->pBt
157b5 72 65 65 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 ree, pCur->pgnoR
157b6 6f 6f 74 2c 20 70 43 75 72 2c 20 6e 4b 65 79 29 oot, pCur, nKey)
157b7 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
157b8 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a QLITE_LOCKED; /*
157b9 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20 The table pCur
157ba 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20 points to has a
157bb 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d read lock */. }
157bc 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
157bd 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c ate==CURSOR_FAUL
157be 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 T ){. return
157bf 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a pCur->skip;. }.
157c0 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 . /* Save the p
157c1 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 ositions of any
157c2 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 other cursors op
157c3 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 en on this table
157c4 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 */. sqlite3Btr
157c5 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 eeClearCursor(pC
157c6 75 72 29 3b 0a 20 20 69 66 28 20 0a 20 20 20 20 ur);. if( .
157c7 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
157c8 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 saveAllCursors(
157c9 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 pBt, pCur->pgnoR
157ca 6f 6f 74 2c 20 70 43 75 72 29 29 20 7c 7c 0a 20 oot, pCur)) ||.
157cb 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 SQLITE_OK!=(r
157cc 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
157cd 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 4b 65 Moveto(pCur, pKe
157ce 79 2c 20 6e 4b 65 79 2c 20 61 70 70 65 6e 64 42 y, nKey, appendB
157cf 69 61 73 2c 20 26 6c 6f 63 29 29 0a 20 20 29 7b ias, &loc)). ){
157d0 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
157d1 20 20 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 }.. pPage = p
157d2 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
157d3 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 ->iPage];. asse
157d4 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 rt( pPage->intKe
157d5 79 20 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a y || nKey>=0 );.
157d6 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
157d7 3e 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d >leaf || !pPage-
157d8 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 54 52 41 >intKey );. TRA
157d9 43 45 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62 CE(("INSERT: tab
157da 6c 65 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 le=%d nkey=%lld
157db 6e 64 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64 ndata=%d page=%d
157dc 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %s\n",.
157dd 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 pCur->pgnoRoot
157de 2c 20 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 , nKey, nData, p
157df 50 61 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 Page->pgno,.
157e0 20 20 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 loc==0 ? "
157e1 6f 76 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65 overwrite" : "ne
157e2 77 20 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 w entry"));. as
157e3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
157e4 6e 69 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 nit );. allocat
157e5 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
157e6 0a 20 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 . newCell = pBt
157e7 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 ->pTmpSpace;. i
157e8 66 28 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 f( newCell==0 )
157e9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
157ea 4d 45 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c MEM;. rc = fill
157eb 49 6e 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 InCell(pPage, ne
157ec 77 43 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 wCell, pKey, nKe
157ed 79 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c y, pData, nData,
157ee 20 6e 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b nZero, &szNew);
157ef 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f . if( rc ) goto
157f0 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 end_insert;. a
157f1 73 73 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 ssert( szNew==ce
157f2 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c llSizePtr(pPage,
157f3 20 6e 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 newCell) );. a
157f4 73 73 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 ssert( szNew<=MX
157f5 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 _CELL_SIZE(pBt)
157f6 29 3b 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d );. idx = pCur-
157f7 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
157f8 67 65 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d ge];. if( loc==
157f9 30 20 26 26 20 43 55 52 53 4f 52 5f 56 41 4c 49 0 && CURSOR_VALI
157fa 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 D==pCur->eState
157fb 29 7b 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 ){. u16 szOld
157fc 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 ;. assert( id
157fd 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 x<pPage->nCell )
157fe 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
157ff 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
15800 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
15801 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
15802 20 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 goto end_inser
15803 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 t;. }. old
15804 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 Cell = findCell(
15805 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 pPage, idx);.
15806 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
15807 66 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 f ){. memcp
15808 79 28 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 y(newCell, oldCe
15809 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 ll, 4);. }.
1580a 20 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 szOld = cellSi
1580b 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 zePtr(pPage, old
1580c 43 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 Cell);. rc =
1580d 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c clearCell(pPage,
1580e 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 69 oldCell);. i
1580f 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 f( rc ) goto end
15810 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20 72 63 20 _insert;. rc
15811 3d 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 = dropCell(pPage
15812 2c 20 69 64 78 2c 20 73 7a 4f 6c 64 29 3b 0a 20 , idx, szOld);.
15813 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
15814 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 67 E_OK ) {. g
15815 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a oto end_insert;.
15816 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
15817 28 20 6c 6f 63 3c 30 20 26 26 20 70 50 61 67 65 ( loc<0 && pPage
15818 2d 3e 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 ->nCell>0 ){.
15819 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
1581a 6c 65 61 66 20 29 3b 0a 20 20 20 20 69 64 78 20 leaf );. idx
1581b 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b = ++pCur->aiIdx[
1581c 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
1581d 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
1581e 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 ze = 0;. pCur
1581f 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b ->validNKey = 0;
15820 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 . }else{. as
15821 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 sert( pPage->lea
15822 66 20 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 f );. }. rc =
15823 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 insertCell(pPage
15824 2c 20 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 , idx, newCell,
15825 73 7a 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20 szNew, 0, 0);.
15826 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15827 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 K ) goto end_ins
15828 65 72 74 3b 0a 20 20 72 63 20 3d 20 62 61 6c 61 ert;. rc = bala
15829 6e 63 65 28 70 43 75 72 2c 20 31 29 3b 0a 20 20 nce(pCur, 1);.
1582a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1582b 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 K ){. moveToR
1582c 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 65 oot(pCur);. }.e
1582d 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 74 nd_insert:. ret
1582e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1582f 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74 72 Delete the entr
15830 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f y that the curso
15831 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
15832 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a . The cursor.**
15833 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
15834 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72 79 g at a arbitrary
15835 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 location..*/.SQ
15836 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15837 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c sqlite3BtreeDel
15838 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ete(BtCursor *pC
15839 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a ur){. MemPage *
1583a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
1583b 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1583c 5d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 ];. int idx;.
1583d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
1583e 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a Cell;. int rc;.
1583f 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 Pgno pgnoChild
15840 20 3d 20 30 3b 0a 20 20 42 74 72 65 65 20 2a 70 = 0;. Btree *p
15841 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b = pCur->pBtree;
15842 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
15843 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 = p->pBt;.. as
15844 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
15845 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
15846 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15847 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 >isInit );. if(
15848 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
15849 69 6f 6e 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion!=TRANS_WRITE
1584a 20 29 7b 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 ){. /* Must
1584b 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 start a transact
1584c 69 6f 6e 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 ion before doing
1584d 20 61 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20 a delete */.
1584e 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f rc = pBt->readO
1584f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41 nly ? SQLITE_REA
15850 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45 DONLY : SQLITE_E
15851 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e RROR;. return
15852 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 rc;. }. asser
15853 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c t( !pBt->readOnl
15854 79 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d y );. if( pCur-
15855 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
15856 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 FAULT ){. ret
15857 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a urn pCur->skip;.
15858 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e }. if( pCur->
15859 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
1585a 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c e]>=pPage->nCell
1585b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1585c 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a QLITE_ERROR; /*
1585d 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e The cursor is n
1585e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 ot pointing to a
1585f 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 nything */. }.
15860 20 69 66 28 20 21 70 43 75 72 2d 3e 77 72 46 6c if( !pCur->wrFl
15861 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ag ){. return
15862 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 20 20 20 SQLITE_PERM;
15863 2f 2a 20 44 69 64 20 6e 6f 74 20 6f 70 65 6e 20 /* Did not open
15864 74 68 69 73 20 63 75 72 73 6f 72 20 66 6f 72 20 this cursor for
15865 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 writing */. }.
15866 20 69 66 28 20 63 68 65 63 6b 52 65 61 64 4c 6f if( checkReadLo
15867 63 6b 73 28 70 43 75 72 2d 3e 70 42 74 72 65 65 cks(pCur->pBtree
15868 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 , pCur->pgnoRoot
15869 2c 20 70 43 75 72 2c 20 70 43 75 72 2d 3e 69 6e , pCur, pCur->in
1586a 66 6f 2e 6e 4b 65 79 29 20 29 7b 0a 20 20 20 20 fo.nKey) ){.
1586b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f return SQLITE_LO
1586c 43 4b 45 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 CKED; /* The tab
1586d 6c 65 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 le pCur points t
1586e 6f 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 o has a read loc
1586f 6b 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 k */. }.. /* R
15870 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 72 65 estore the curre
15871 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 nt cursor positi
15872 6f 6e 20 28 61 20 6e 6f 2d 6f 70 20 69 66 20 74 on (a no-op if t
15873 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 he cursor is not
15874 20 69 6e 20 0a 20 20 2a 2a 20 43 55 52 53 4f 52 in . ** CURSOR
15875 5f 52 45 51 55 49 52 45 53 45 45 4b 20 73 74 61 _REQUIRESEEK sta
15876 74 65 29 20 61 6e 64 20 73 61 76 65 20 74 68 65 te) and save the
15877 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e positions of an
15878 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 y other cursors
15879 0a 20 20 2a 2a 20 6f 70 65 6e 20 6f 6e 20 74 68 . ** open on th
1587a 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 54 68 e same table. Th
1587b 65 6e 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 en call sqlite3P
1587c 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74 agerWrite() on t
1587d 68 65 20 70 61 67 65 0a 20 20 2a 2a 20 74 68 61 he page. ** tha
1587e 74 20 74 68 65 20 65 6e 74 72 79 20 77 69 6c 6c t the entry will
1587f 20 62 65 20 64 65 6c 65 74 65 64 20 66 72 6f 6d be deleted from
15880 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 0a 20 20 .. */. if( .
15881 20 20 28 72 63 20 3d 20 72 65 73 74 6f 72 65 43 (rc = restoreC
15882 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 ursorPosition(pC
15883 75 72 29 29 21 3d 30 20 7c 7c 0a 20 20 20 20 28 ur))!=0 ||. (
15884 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 rc = saveAllCurs
15885 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 ors(pBt, pCur->p
15886 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 29 21 gnoRoot, pCur))!
15887 3d 30 20 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 =0 ||. (rc =
15888 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
15889 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
1588a 29 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 72 ))!=0. ){. r
1588b 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 eturn rc;. }..
1588c 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 63 /* Locate the c
1588d 65 6c 6c 20 77 69 74 68 69 6e 20 69 74 73 20 70 ell within its p
1588e 61 67 65 20 61 6e 64 20 6c 65 61 76 65 20 70 43 age and leave pC
1588f 65 6c 6c 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 ell pointing to
15890 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 2e 20 54 the. ** data. T
15891 68 65 20 63 6c 65 61 72 43 65 6c 6c 28 29 20 63 he clearCell() c
15892 61 6c 6c 20 66 72 65 65 73 20 61 6e 79 20 6f 76 all frees any ov
15893 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73 erflow pages ass
15894 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
15895 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 . ** cell. The
15896 63 65 6c 6c 20 69 74 73 65 6c 66 20 69 73 20 73 cell itself is s
15897 74 69 6c 6c 20 69 6e 74 61 63 74 2e 0a 20 20 2a till intact.. *
15898 2f 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e /. idx = pCur->
15899 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
1589a 65 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69 e];. pCell = fi
1589b 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 ndCell(pPage, id
1589c 78 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 x);. if( !pPage
1589d 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 ->leaf ){. pg
1589e 6e 6f 43 68 69 6c 64 20 3d 20 67 65 74 34 62 79 noChild = get4by
1589f 74 65 28 70 43 65 6c 6c 29 3b 0a 20 20 7d 0a 20 te(pCell);. }.
158a0 20 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 rc = clearCell(
158a1 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 pPage, pCell);.
158a2 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 if( rc ){. r
158a3 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 eturn rc;. }..
158a4 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
158a5 66 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 f ){. /*.
158a6 2a 2a 20 54 68 65 20 65 6e 74 72 79 20 77 65 20 ** The entry we
158a7 61 72 65 20 61 62 6f 75 74 20 74 6f 20 64 65 6c are about to del
158a8 65 74 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 ete is not a lea
158a9 66 20 73 6f 20 69 66 20 77 65 20 64 6f 20 6e 6f f so if we do no
158aa 74 0a 20 20 20 20 2a 2a 20 64 6f 20 73 6f 6d 65 t. ** do some
158ab 74 68 69 6e 67 20 77 65 20 77 69 6c 6c 20 6c 65 thing we will le
158ac 61 76 65 20 61 20 68 6f 6c 65 20 6f 6e 20 61 6e ave a hole on an
158ad 20 69 6e 74 65 72 6e 61 6c 20 70 61 67 65 2e 0a internal page..
158ae 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 74 ** We have t
158af 6f 20 66 69 6c 6c 20 74 68 65 20 68 6f 6c 65 20 o fill the hole
158b0 62 79 20 6d 6f 76 69 6e 67 20 69 6e 20 61 20 63 by moving in a c
158b1 65 6c 6c 20 66 72 6f 6d 20 61 20 6c 65 61 66 2e ell from a leaf.
158b2 20 20 54 68 65 0a 20 20 20 20 2a 2a 20 6e 65 78 The. ** nex
158b3 74 20 43 65 6c 6c 20 61 66 74 65 72 20 74 68 65 t Cell after the
158b4 20 6f 6e 65 20 74 6f 20 62 65 20 64 65 6c 65 74 one to be delet
158b5 65 64 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 ed is guaranteed
158b6 20 74 6f 20 65 78 69 73 74 20 61 6e 64 0a 20 20 to exist and.
158b7 20 20 2a 2a 20 74 6f 20 62 65 20 61 20 6c 65 61 ** to be a lea
158b8 66 20 73 6f 20 77 65 20 63 61 6e 20 75 73 65 20 f so we can use
158b9 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 42 it.. */. B
158ba 74 43 75 72 73 6f 72 20 6c 65 61 66 43 75 72 3b tCursor leafCur;
158bb 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c . MemPage *pL
158bc 65 61 66 50 61 67 65 3b 0a 0a 20 20 20 20 75 6e eafPage;.. un
158bd 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 4e 65 signed char *pNe
158be 78 74 3b 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 xt;. int notU
158bf 73 65 64 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 sed;. unsigne
158c0 64 20 63 68 61 72 20 2a 74 65 6d 70 43 65 6c 6c d char *tempCell
158c1 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 = 0;. assert
158c2 28 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 ( !pPage->intKey
158c3 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 );. sqlite3B
158c4 74 72 65 65 47 65 74 54 65 6d 70 43 75 72 73 6f treeGetTempCurso
158c5 72 28 70 43 75 72 2c 20 26 6c 65 61 66 43 75 72 r(pCur, &leafCur
158c6 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
158c7 74 65 33 42 74 72 65 65 4e 65 78 74 28 26 6c 65 te3BtreeNext(&le
158c8 61 66 43 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 afCur, ¬Used)
158c9 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
158ca 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
158cb 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 assert( leafCur
158cc 2e 61 69 49 64 78 5b 6c 65 61 66 43 75 72 2e 69 .aiIdx[leafCur.i
158cd 50 61 67 65 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 Page]==0 );.
158ce 20 20 70 4c 65 61 66 50 61 67 65 20 3d 20 6c 65 pLeafPage = le
158cf 61 66 43 75 72 2e 61 70 50 61 67 65 5b 6c 65 61 afCur.apPage[lea
158d0 66 43 75 72 2e 69 50 61 67 65 5d 3b 0a 20 20 20 fCur.iPage];.
158d1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
158d2 61 67 65 72 57 72 69 74 65 28 70 4c 65 61 66 50 agerWrite(pLeafP
158d3 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
158d4 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
158d5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
158d6 20 20 20 20 69 6e 74 20 6c 65 61 66 43 75 72 73 int leafCurs
158d7 6f 72 49 6e 76 61 6c 69 64 20 3d 20 30 3b 0a 20 orInvalid = 0;.
158d8 20 20 20 20 20 75 31 36 20 73 7a 4e 65 78 74 3b u16 szNext;
158d9 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 44 . TRACE(("D
158da 45 4c 45 54 45 3a 20 74 61 62 6c 65 3d 25 64 20 ELETE: table=%d
158db 64 65 6c 65 74 65 20 69 6e 74 65 72 6e 61 6c 20 delete internal
158dc 66 72 6f 6d 20 25 64 20 72 65 70 6c 61 63 65 20 from %d replace
158dd 66 72 6f 6d 20 6c 65 61 66 20 25 64 5c 6e 22 2c from leaf %d\n",
158de 0a 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e . pCur->
158df 70 67 6e 6f 52 6f 6f 74 2c 20 70 50 61 67 65 2d pgnoRoot, pPage-
158e0 3e 70 67 6e 6f 2c 20 70 4c 65 61 66 50 61 67 65 >pgno, pLeafPage
158e1 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 ->pgno));.
158e2 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 dropCell(pPage,
158e3 69 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 idx, cellSizePtr
158e4 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b (pPage, pCell));
158e5 0a 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 66 . pNext = f
158e6 69 6e 64 43 65 6c 6c 28 70 4c 65 61 66 50 61 67 indCell(pLeafPag
158e7 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 7a 4e e, 0);. szN
158e8 65 78 74 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 ext = cellSizePt
158e9 72 28 70 4c 65 61 66 50 61 67 65 2c 20 70 4e 65 r(pLeafPage, pNe
158ea 78 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 xt);. asser
158eb 74 28 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 t( MX_CELL_SIZE(
158ec 70 42 74 29 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 pBt)>=szNext+4 )
158ed 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 ;. allocate
158ee 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a TempSpace(pBt);.
158ef 20 20 20 20 20 20 74 65 6d 70 43 65 6c 6c 20 3d tempCell =
158f0 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b pBt->pTmpSpace;
158f1 0a 20 20 20 20 20 20 69 66 28 20 74 65 6d 70 43 . if( tempC
158f2 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ell==0 ){.
158f3 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
158f4 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 MEM;. }.
158f5 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
158f6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
158f7 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 rc = insertCell(
158f8 70 50 61 67 65 2c 20 69 64 78 2c 20 70 4e 65 78 pPage, idx, pNex
158f9 74 2d 34 2c 20 73 7a 4e 65 78 74 2b 34 2c 20 74 t-4, szNext+4, t
158fa 65 6d 70 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 empCell, 0);.
158fb 20 20 20 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 }... /*
158fc 54 68 65 20 22 69 66 22 20 73 74 61 74 65 6d 65 The "if" stateme
158fd 6e 74 20 69 6e 20 74 68 65 20 6e 65 78 74 20 63 nt in the next c
158fe 6f 64 65 20 62 6c 6f 63 6b 20 69 73 20 63 72 69 ode block is cri
158ff 74 69 63 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 tical. The.
15900 20 20 2a 2a 20 73 6c 69 67 68 74 65 73 74 20 65 ** slightest e
15901 72 72 6f 72 20 69 6e 20 74 68 61 74 20 73 74 61 rror in that sta
15902 74 65 6d 65 6e 74 20 77 6f 75 6c 64 20 61 6c 6c tement would all
15903 6f 77 20 53 51 4c 69 74 65 20 74 6f 20 6f 70 65 ow SQLite to ope
15904 72 61 74 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f rate. ** co
15905 72 72 65 63 74 6c 79 20 6d 6f 73 74 20 6f 66 20 rrectly most of
15906 74 68 65 20 74 69 6d 65 20 62 75 74 20 70 72 6f the time but pro
15907 64 75 63 65 20 76 65 72 79 20 72 61 72 65 20 66 duce very rare f
15908 61 69 6c 75 72 65 73 2e 20 20 54 6f 0a 20 20 20 ailures. To.
15909 20 20 20 2a 2a 20 67 75 61 72 64 20 61 67 61 69 ** guard agai
1590a 6e 73 74 20 74 68 69 73 2c 20 74 68 65 20 66 6f nst this, the fo
1590b 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 68 llowing macros h
1590c 65 6c 70 20 74 6f 20 76 65 72 69 66 79 20 74 68 elp to verify th
1590d 61 74 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 at. ** the
1590e 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69 "if" statement i
1590f 73 20 77 65 6c 6c 20 74 65 73 74 65 64 2e 0a 20 s well tested..
15910 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 74 65 */. te
15911 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e stcase( pPage->n
15912 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 Overflow==0 && p
15913 50 61 67 65 2d 3e 6e 46 72 65 65 3c 70 42 74 2d Page->nFree<pBt-
15914 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 >usableSize*2/3
15915 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
15916 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e && pLeafPage->
15917 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e nFree+2+szNext >
15918 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
15919 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 *2/3 );. te
1591a 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e stcase( pPage->n
1591b 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 Overflow==0 && p
1591c 50 61 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 Page->nFree==pBt
1591d 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 ->usableSize*2/3
1591e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1591f 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d && pLeafPage-
15920 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 >nFree+2+szNext
15921 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a > pBt->usableSiz
15922 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 e*2/3 );. t
15923 65 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e estcase( pPage->
15924 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 nOverflow==0 &&
15925 70 50 61 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 pPage->nFree==pB
15926 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f t->usableSize*2/
15927 33 2b 31 20 0a 20 20 20 20 20 20 20 20 20 20 20 3+1 .
15928 20 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 && pLeafPa
15929 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 ge->nFree+2+szNe
1592a 78 74 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 xt > pBt->usable
1592b 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 Size*2/3 );.
1592c 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 67 testcase( pPag
1592d 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 26 e->nOverflow>0 &
1592e 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d & pPage->nFree<=
1592f 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a pBt->usableSize*
15930 32 2f 33 0a 20 20 20 20 20 20 20 20 20 20 20 20 2/3.
15931 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 && pLeafPag
15932 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 e->nFree+2+szNex
15933 74 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 t > pBt->usableS
15934 69 7a 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 ize*2/3 );.
15935 20 74 65 73 74 63 61 73 65 28 20 28 70 50 61 67 testcase( (pPag
15936 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c e->nOverflow>0 |
15937 7c 20 28 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 | (pPage->nFree
15938 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a > pBt->usableSiz
15939 65 2a 32 2f 33 29 29 0a 20 20 20 20 20 20 20 20 e*2/3)).
1593a 20 20 20 20 20 20 20 20 20 26 26 20 70 4c 65 61 && pLea
1593b 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 fPage->nFree+2+s
1593c 7a 4e 65 78 74 20 3d 3d 20 70 42 74 2d 3e 75 73 zNext == pBt->us
1593d 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 29 3b 0a ableSize*2/3 );.
1593e 0a 0a 20 20 20 20 20 20 69 66 28 20 28 70 50 61 .. if( (pPa
1593f 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 ge->nOverflow>0
15940 7c 7c 20 28 70 50 61 67 65 2d 3e 6e 46 72 65 65 || (pPage->nFree
15941 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 > pBt->usableSi
15942 7a 65 2a 32 2f 33 29 29 20 26 26 0a 20 20 20 20 ze*2/3)) &&.
15943 20 20 20 20 20 20 28 70 4c 65 61 66 50 61 67 65 (pLeafPage
15944 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 ->nFree+2+szNext
15945 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 > pBt->usableSi
15946 7a 65 2a 32 2f 33 29 0a 20 20 20 20 20 20 29 7b ze*2/3). ){
15947 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 . /* This
15948 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e branch is taken
15949 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c if the internal
1594a 20 6e 6f 64 65 20 69 73 20 6e 6f 77 20 65 69 74 node is now eit
1594b 68 65 72 20 6f 76 65 72 66 6c 6f 77 69 6e 67 0a her overflowing.
1594c 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 75 6e ** or un
1594d 64 65 72 66 75 6c 6c 20 61 6e 64 20 74 68 65 20 derfull and the
1594e 6c 65 61 66 20 6e 6f 64 65 20 77 69 6c 6c 20 62 leaf node will b
1594f 65 20 75 6e 64 65 72 66 75 6c 6c 20 61 66 74 65 e underfull afte
15950 72 20 74 68 65 20 6a 75 73 74 20 63 65 6c 6c 20 r the just cell
15951 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 69 . ** copi
15952 65 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e ed to the intern
15953 61 6c 20 6e 6f 64 65 20 69 73 20 64 65 6c 65 74 al node is delet
15954 65 64 20 66 72 6f 6d 20 69 74 2e 20 54 68 69 73 ed from it. This
15955 20 69 73 20 61 20 73 70 65 63 69 61 6c 0a 20 20 is a special.
15956 20 20 20 20 20 20 2a 2a 20 63 61 73 65 20 62 65 ** case be
15957 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 cause the call t
15958 6f 20 62 61 6c 61 6e 63 65 28 29 20 74 6f 20 63 o balance() to c
15959 6f 72 72 65 63 74 20 74 68 65 20 69 6e 74 65 72 orrect the inter
1595a 6e 61 6c 20 6e 6f 64 65 0a 20 20 20 20 20 20 20 nal node.
1595b 20 2a 2a 20 6d 61 79 20 63 68 61 6e 67 65 20 74 ** may change t
1595c 68 65 20 74 72 65 65 20 73 74 72 75 63 74 75 72 he tree structur
1595d 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 e and invalidate
1595e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
1595f 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
15960 6c 65 61 66 43 75 72 2e 61 70 50 61 67 65 5b 5d leafCur.apPage[]
15961 20 61 6e 64 20 6c 65 61 66 43 75 72 2e 61 69 49 and leafCur.aiI
15962 64 78 5b 5d 20 61 72 72 61 79 73 2c 20 77 68 69 dx[] arrays, whi
15963 63 68 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 ch will be.
15964 20 20 20 2a 2a 20 75 73 65 64 20 62 79 20 74 68 ** used by th
15965 65 20 62 61 6c 61 6e 63 65 28 29 20 72 65 71 75 e balance() requ
15966 69 72 65 64 20 74 6f 20 63 6f 72 72 65 63 74 20 ired to correct
15967 74 68 65 20 75 6e 64 65 72 66 75 6c 6c 20 6c 65 the underfull le
15968 61 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f af. ** no
15969 64 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 de.. **.
1596a 20 20 20 20 20 20 20 2a 2a 20 54 68 65 20 66 6f ** The fo
1596b 72 6d 75 6c 61 20 75 73 65 64 20 69 6e 20 74 68 rmula used in th
1596c 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 62 6f e expression abo
1596d 76 65 20 61 72 65 20 62 61 73 65 64 20 6f 6e 20 ve are based on
1596e 66 61 63 65 74 73 20 6f 66 0a 20 20 20 20 20 20 facets of.
1596f 20 20 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 ** the SQLite
15970 66 69 6c 65 2d 66 6f 72 6d 61 74 20 74 68 61 74 file-format that
15971 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f do not change o
15972 76 65 72 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 ver time..
15973 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 65 73 */. tes
15974 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 46 tcase( pPage->nF
15975 72 65 65 3d 3d 70 42 74 2d 3e 75 73 61 62 6c 65 ree==pBt->usable
15976 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 Size*2/3+1 );.
15977 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
15978 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 65 pLeafPage->nFree
15979 2b 32 2b 73 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e +2+szNext==pBt->
1597a 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 usableSize*2/3+1
1597b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 65 61 66 );. leaf
1597c 43 75 72 73 6f 72 49 6e 76 61 6c 69 64 20 3d 20 CursorInvalid =
1597d 31 3b 0a 20 20 20 20 20 20 7d 20 20 20 20 20 20 1;. }
1597e 20 20 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 .. if( rc
1597f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
15980 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
15981 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c findOverflowCell
15982 28 70 50 61 67 65 2c 20 69 64 78 29 2c 20 70 67 (pPage, idx), pg
15983 6e 6f 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 noChild);.
15984 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 43 75 72 VVA_ONLY( pCur
15985 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 ->pagesShuffled
15986 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 = 0 );. r
15987 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 c = balance(pCur
15988 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 , 0);. }..
15989 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1598a 49 54 45 5f 4f 4b 20 26 26 20 6c 65 61 66 43 75 ITE_OK && leafCu
1598b 72 73 6f 72 49 6e 76 61 6c 69 64 20 29 7b 0a 20 rsorInvalid ){.
1598c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 /* The le
1598d 61 66 2d 6e 6f 64 65 20 69 73 20 6e 6f 77 20 75 af-node is now u
1598e 6e 64 65 72 66 75 6c 6c 20 61 6e 64 20 73 6f 20 nderfull and so
1598f 74 68 65 20 74 72 65 65 20 6e 65 65 64 73 20 74 the tree needs t
15990 6f 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a 2a o be . **
15991 20 72 65 62 61 6c 61 6e 63 65 64 2e 20 48 6f 77 rebalanced. How
15992 65 76 65 72 2c 20 74 68 65 20 62 61 6c 61 6e 63 ever, the balanc
15993 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e e() operation on
15994 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20 20 the internal.
15995 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65 20 61 62 ** node ab
15996 6f 76 65 20 6d 61 79 20 68 61 76 65 20 6d 6f 64 ove may have mod
15997 69 66 69 65 64 20 74 68 65 20 73 74 72 75 63 74 ified the struct
15998 75 72 65 20 6f 66 20 74 68 65 20 42 2d 54 72 65 ure of the B-Tre
15999 65 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a e and. **
1599a 20 73 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 so the current
1599b 63 6f 6e 74 65 6e 74 73 20 6f 66 20 6c 65 61 66 contents of leaf
1599c 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64 Cur.apPage[] and
1599d 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 5d leafCur.aiIdx[]
1599e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 . ** may
1599f 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 0a not be trusted..
159a0 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
159a1 20 20 20 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 ** It is not
159a2 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 6f 70 79 possible to copy
159a3 20 74 68 65 20 61 6e 63 65 73 74 72 79 20 66 72 the ancestry fr
159a4 6f 6d 20 70 43 75 72 2c 20 61 73 20 74 68 65 20 om pCur, as the
159a5 73 61 6d 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 same. **
159a6 62 61 6c 61 6e 63 65 28 29 20 63 61 6c 6c 20 68 balance() call h
159a7 61 73 20 69 6e 76 61 6c 69 64 61 74 65 64 20 74 as invalidated t
159a8 68 65 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b he pCur->apPage[
159a9 5d 20 61 6e 64 20 61 69 49 64 78 5b 5d 0a 20 20 ] and aiIdx[].
159aa 20 20 20 20 20 20 2a 2a 20 61 72 72 61 79 73 2e ** arrays.
159ab 20 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 . **.
159ac 20 20 20 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c ** The call
159ad 20 74 6f 20 73 61 76 65 43 75 72 73 6f 72 50 6f to saveCursorPo
159ae 73 69 74 69 6f 6e 28 29 20 62 65 6c 6f 77 20 69 sition() below i
159af 6e 74 65 72 6e 61 6c 6c 79 20 73 61 76 65 73 20 nternally saves
159b0 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
159b1 6b 65 79 20 74 68 61 74 20 6c 65 61 66 43 75 72 key that leafCur
159b2 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f is currently po
159b3 69 6e 74 69 6e 67 20 74 6f 2e 20 43 75 72 72 65 inting to. Curre
159b4 6e 74 6c 79 2c 20 74 68 65 72 65 0a 20 20 20 20 ntly, there.
159b5 20 20 20 20 2a 2a 20 61 72 65 20 74 77 6f 20 63 ** are two c
159b6 6f 70 69 65 73 20 6f 66 20 74 68 61 74 20 6b 65 opies of that ke
159b7 79 20 69 6e 20 74 68 65 20 74 72 65 65 20 2d 20 y in the tree -
159b8 6f 6e 65 20 68 65 72 65 20 6f 6e 20 74 68 65 20 one here on the
159b9 6c 65 61 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 leaf. **
159ba 70 61 67 65 20 61 6e 64 20 6f 6e 65 20 6f 6e 20 page and one on
159bb 73 6f 6d 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f some internal no
159bc 64 65 20 69 6e 20 74 68 65 20 74 72 65 65 2e 20 de in the tree.
159bd 54 68 65 20 63 6f 70 79 20 6f 6e 0a 20 20 20 20 The copy on.
159be 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 61 66 20 ** the leaf
159bf 6e 6f 64 65 20 69 73 20 61 6c 77 61 79 73 20 74 node is always t
159c0 68 65 20 6e 65 78 74 20 6b 65 79 20 69 6e 20 74 he next key in t
159c1 72 65 65 2d 6f 72 64 65 72 20 61 66 74 65 72 20 ree-order after
159c2 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
159c3 63 6f 70 79 20 6f 6e 20 74 68 65 20 69 6e 74 65 copy on the inte
159c4 72 6e 61 6c 20 6e 6f 64 65 2e 20 53 6f 2c 20 74 rnal node. So, t
159c5 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 he call to sqlit
159c6 65 33 42 74 72 65 65 4e 65 78 74 28 29 0a 20 20 e3BtreeNext().
159c7 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 72 ** calls r
159c8 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
159c9 74 69 6f 6e 28 29 20 74 6f 20 70 6f 69 6e 74 20 tion() to point
159ca 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
159cb 65 20 63 6f 70 79 0a 20 20 20 20 20 20 20 20 2a e copy. *
159cc 2a 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 * stored on the
159cd 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2c 20 74 internal node, t
159ce 68 65 6e 20 61 64 76 61 6e 63 65 73 20 74 6f 20 hen advances to
159cf 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79 2c 0a the next entry,.
159d0 20 20 20 20 20 20 20 20 2a 2a 20 77 68 69 63 68 ** which
159d1 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 74 happens to be t
159d2 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b he copy of the k
159d3 65 79 20 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e ey on the intern
159d4 61 6c 20 6e 6f 64 65 2e 0a 20 20 20 20 20 20 20 al node..
159d5 20 2a 2a 20 4e 65 74 20 65 66 66 65 63 74 3a 20 ** Net effect:
159d6 6c 65 61 66 43 75 72 20 69 73 20 70 6f 69 6e 74 leafCur is point
159d7 69 6e 67 20 62 61 63 6b 20 74 6f 20 74 68 65 20 ing back to the
159d8 64 75 70 6c 69 63 61 74 65 20 63 65 6c 6c 0a 20 duplicate cell.
159d9 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 6e ** that n
159da 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d 6f 76 eeds to be remov
159db 65 64 2c 20 61 6e 64 20 74 68 65 20 6c 65 61 66 ed, and the leaf
159dc 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64 Cur.apPage[] and
159dd 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 65 61 66 . ** leaf
159de 43 75 72 2e 61 69 49 64 78 5b 5d 20 61 72 72 61 Cur.aiIdx[] arra
159df 79 73 20 61 72 65 20 63 6f 72 72 65 63 74 2e 0a ys are correct..
159e0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
159e1 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 50 67 6e VVA_ONLY( Pgn
159e2 6f 20 6c 65 61 66 50 67 6e 6f 20 3d 20 70 4c 65 o leafPgno = pLe
159e3 61 66 50 61 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a afPage->pgno );.
159e4 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 61 76 rc = sav
159e5 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
159e6 26 6c 65 61 66 43 75 72 29 3b 0a 20 20 20 20 20 &leafCur);.
159e7 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
159e8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
159e9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
159ea 72 65 65 4e 65 78 74 28 26 6c 65 61 66 43 75 72 reeNext(&leafCur
159eb 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 20 , ¬Used);.
159ec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
159ed 4c 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66 43 LeafPage = leafC
159ee 75 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43 75 ur.apPage[leafCu
159ef 72 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 r.iPage];.
159f0 20 20 61 73 73 65 72 74 28 20 70 4c 65 61 66 50 assert( pLeafP
159f1 61 67 65 2d 3e 70 67 6e 6f 3d 3d 6c 65 61 66 50 age->pgno==leafP
159f2 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20 20 61 gno );. a
159f3 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61 ssert( leafCur.a
159f4 69 49 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61 iIdx[leafCur.iPa
159f5 67 65 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ge]==0 );.
159f6 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d }.. if( rc=
159f7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
159f8 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 dropCell(p
159f9 4c 65 61 66 50 61 67 65 2c 20 30 2c 20 73 7a 4e LeafPage, 0, szN
159fa 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 56 56 ext);. VV
159fb 41 5f 4f 4e 4c 59 28 20 6c 65 61 66 43 75 72 2e A_ONLY( leafCur.
159fc 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20 pagesShuffled =
159fd 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 0 );. rc
159fe 3d 20 62 61 6c 61 6e 63 65 28 26 6c 65 61 66 43 = balance(&leafC
159ff 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 ur, 0);.
15a00 61 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 73 assert( leafCurs
15a01 6f 72 49 6e 76 61 6c 69 64 20 7c 7c 20 21 6c 65 orInvalid || !le
15a02 61 66 43 75 72 2e 70 61 67 65 73 53 68 75 66 66 afCur.pagesShuff
15a03 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 led.
15a04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15a05 20 20 20 20 20 20 20 7c 7c 20 21 70 43 75 72 2d || !pCur-
15a06 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 29 >pagesShuffled )
15a07 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
15a08 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
15a09 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73 6f ReleaseTempCurso
15a0a 72 28 26 6c 65 61 66 43 75 72 29 3b 0a 20 20 7d r(&leafCur);. }
15a0b 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43 45 28 else{. TRACE(
15a0c 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c 65 3d ("DELETE: table=
15a0d 25 64 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 6c %d delete from l
15a0e 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 eaf %d\n",.
15a0f 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 pCur->pgnoRoot
15a10 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b , pPage->pgno));
15a11 0a 20 20 20 20 72 63 20 3d 20 64 72 6f 70 43 65 . rc = dropCe
15a12 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 63 ll(pPage, idx, c
15a13 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
15a14 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 20 69 , pCell));. i
15a15 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15a16 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 ){. rc = b
15a17 61 6c 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b alance(pCur, 0);
15a18 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
15a19 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
15a1a 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 {. moveToRoot
15a1b 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72 65 (pCur);. }. re
15a1c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
15a1d 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 42 * Create a new B
15a1e 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 72 69 Tree table. Wri
15a1f 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c 65 te into *piTable
15a20 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d the page.** num
15a21 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 ber for the root
15a22 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77 page of the new
15a23 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 table..**.** Th
15a24 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 20 69 e type of type i
15a25 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 s determined by
15a26 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
15a27 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a 2a ter. Only the.*
15a28 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 * following valu
15a29 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 65 20 es of flags are
15a2a 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 currently in use
15a2b 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20 . Other values
15a2c 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69 67 for.** flags mig
15a2d 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a 0a ht not work:.**.
15a2e 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 ** BTREE_INT
15a2f 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 44 41 KEY|BTREE_LEAFDA
15a30 54 41 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 TA Used for
15a31 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 68 20 SQL tables with
15a32 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20 20 rowid keys.**
15a33 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 BTREE_ZERODATA
15a34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15a35 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c 20 69 Used for SQL i
15a36 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69 63 ndices.*/.static
15a37 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 74 65 int btreeCreate
15a38 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 Table(Btree *p,
15a39 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69 6e int *piTable, in
15a3a 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 53 68 t flags){. BtSh
15a3b 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
15a3c 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 Bt;. MemPage *p
15a3d 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e Root;. Pgno pgn
15a3e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 3b oRoot;. int rc;
15a3f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
15a40 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
15a41 65 78 28 70 29 20 29 3b 0a 20 20 69 66 28 20 70 ex(p) );. if( p
15a42 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
15a43 6e 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 n!=TRANS_WRITE )
15a44 7b 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 73 74 {. /* Must st
15a45 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f art a transactio
15a46 6e 20 66 69 72 73 74 20 2a 2f 0a 20 20 20 20 72 n first */. r
15a47 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c c = pBt->readOnl
15a48 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41 44 4f y ? SQLITE_READO
15a49 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 NLY : SQLITE_ERR
15a4a 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 OR;. return r
15a4b 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 c;. }. assert(
15a4c 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 !pBt->readOnly
15a4d 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
15a4e 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
15a4f 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 M. rc = allocat
15a50 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
15a51 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f &pRoot, &pgnoRoo
15a52 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 t, 1, 0);. if(
15a53 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e rc ){. return
15a54 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 rc;. }.#else.
15a55 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
15a56 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f cuum ){. Pgno
15a57 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20 pgnoMove;
15a58 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 68 /* Move a page h
15a59 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d ere to make room
15a5a 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 for the root-pa
15a5b 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 ge */. MemPag
15a5c 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a e *pPageMove; /*
15a5d 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 The page to mov
15a5e 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a e to. */.. /*
15a5f 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 Creating a new
15a60 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 62 table may probab
15a61 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e ly require movin
15a62 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 61 g an existing da
15a63 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f tabase. ** to
15a64 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74 make room for t
15a65 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 6f he new tables ro
15a66 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 65 ot page. In case
15a67 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e 73 this page turns
15a68 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 . ** out to b
15a69 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 e an overflow pa
15a6a 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f ge, delete all o
15a6b 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70 verflow page-map
15a6c 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 68 caches. ** h
15a6d 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 73 eld by open curs
15a6e 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ors.. */.
15a6f 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 invalidateAllOve
15a70 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b rflowCache(pBt);
15a71 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 .. /* Read th
15a72 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b e value of meta[
15a73 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 3] from the data
15a74 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 6e base to determin
15a75 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 20 e where the.
15a76 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 ** root page of
15a77 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 68 the new table sh
15a78 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d ould go. meta[3]
15a79 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 is the largest
15a7a 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a root-page. **
15a7b 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 2c created so far,
15a7c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 so the new root
15a7d 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b 33 -page is (meta[3
15a7e 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ]+1).. */.
15a7f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
15a80 65 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c 20 eeGetMeta(p, 4,
15a81 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 &pgnoRoot);.
15a82 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15a83 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
15a84 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
15a85 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20 pgnoRoot++;..
15a86 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f 74 /* The new root
15a87 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 -page may not be
15a88 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 allocated on a
15a89 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
15a8a 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 , or the. **
15a8b 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 67 PENDING_BYTE pag
15a8c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 e.. */. wh
15a8d 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 ile( pgnoRoot==P
15a8e 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 TRMAP_PAGENO(pBt
15a8f 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20 , pgnoRoot) ||.
15a90 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d pgnoRoot=
15a91 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
15a92 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 GE(pBt) ){.
15a93 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20 pgnoRoot++;.
15a94 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
15a95 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20 gnoRoot>=3 );..
15a96 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /* Allocate a
15a97 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 20 page. The page
15a98 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 72 that currently r
15a99 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52 6f esides at pgnoRo
15a9a 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 ot will. ** b
15a9b 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 61 e moved to the a
15a9c 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28 75 llocated page (u
15a9d 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 61 nless the alloca
15a9e 74 65 64 20 70 61 67 65 20 68 61 70 70 65 6e 73 ted page happens
15a9f 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 64 . ** to resid
15aa0 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a e at pgnoRoot)..
15aa1 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
15aa2 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
15aa3 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f 76 e(pBt, &pPageMov
15aa4 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67 e, &pgnoMove, pg
15aa5 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20 noRoot, 1);.
15aa6 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15aa7 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
15aa8 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 n rc;. }..
15aa9 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70 if( pgnoMove!=p
15aaa 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 gnoRoot ){.
15aab 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 /* pgnoRoot is
15aac 74 68 65 20 70 61 67 65 20 74 68 61 74 20 77 69 the page that wi
15aad 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 ll be used for t
15aae 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a he root-page of.
15aaf 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 ** the new
15ab0 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 table (assuming
15ab1 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e 6f an error did no
15ab2 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77 65 t occur). But we
15ab3 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 were. ** a
15ab4 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76 llocated pgnoMov
15ab5 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 20 28 e. If required (
15ab6 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20 6e i.e. if it was n
15ab7 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 ot allocated.
15ab8 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 69 ** by extendi
15ab9 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74 68 ng the file), th
15aba 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 61 e current page a
15abb 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d t position pgnoM
15abc 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 ove. ** is
15abd 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65 already journale
15abe 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 d.. */.
15abf 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 u8 eType;.
15ac0 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b Pgno iPtrPage;
15ac1 0a 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 .. releaseP
15ac2 61 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a age(pPageMove);.
15ac3 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 . /* Move t
15ac4 68 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c he page currentl
15ac5 79 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f y at pgnoRoot to
15ac6 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 pgnoMove. */.
15ac7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
15ac8 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 BtreeGetPage(pBt
15ac9 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f , pgnoRoot, &pRo
15aca 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 ot, 0);. if
15acb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
15acc 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
15acd 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
15ace 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 rc = ptrmapG
15acf 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 et(pBt, pgnoRoot
15ad0 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 , &eType, &iPtrP
15ad1 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 age);. if(
15ad2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
15ad3 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 eType==PTRMAP_R
15ad4 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70 65 OOTPAGE || eType
15ad5 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 ==PTRMAP_FREEPAG
15ad6 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c E ){. rel
15ad7 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
15ad8 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
15ad9 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
15ada 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 assert( eType!
15adb 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 =PTRMAP_ROOTPAGE
15adc 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
15add 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f ( eType!=PTRMAP_
15ade 46 52 45 45 50 41 47 45 20 29 3b 0a 20 20 20 20 FREEPAGE );.
15adf 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
15ae0 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e gerWrite(pRoot->
15ae1 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
15ae2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15ae3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c K ){. rel
15ae4 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
15ae5 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
15ae6 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
15ae7 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 rc = relocateP
15ae8 61 67 65 28 70 42 74 2c 20 70 52 6f 6f 74 2c 20 age(pBt, pRoot,
15ae9 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c eType, iPtrPage,
15aea 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b 0a 20 pgnoMove, 0);.
15aeb 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
15aec 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 20 20 (pRoot);..
15aed 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61 /* Obtain the pa
15aee 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 2a ge at pgnoRoot *
15aef 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d /. if( rc!=
15af0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15af1 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
15af2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
15af3 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
15af4 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f etPage(pBt, pgno
15af5 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 Root, &pRoot, 0)
15af6 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
15af7 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15af8 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
15af9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
15afa 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
15afb 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 rite(pRoot->pDbP
15afc 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 age);. if(
15afd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
15afe 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
15aff 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 Page(pRoot);.
15b00 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
15b01 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
15b02 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d e{. pRoot =
15b03 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20 pPageMove;.
15b04 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 } .. /* Updat
15b05 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 e the pointer-ma
15b06 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20 p and meta-data
15b07 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f with the new roo
15b08 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a t-page number. *
15b09 2f 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 /. rc = ptrma
15b0a 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f pPut(pBt, pgnoRo
15b0b 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 ot, PTRMAP_ROOTP
15b0c 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 AGE, 0);. if(
15b0d 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c rc ){. rel
15b0e 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
15b0f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
15b10 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
15b11 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
15b12 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67 ateMeta(p, 4, pg
15b13 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28 noRoot);. if(
15b14 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c rc ){. rel
15b15 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
15b16 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
15b17 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 ;. }.. }else
15b18 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 {. rc = alloc
15b19 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
15b1a 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 , &pRoot, &pgnoR
15b1b 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 oot, 1, 0);.
15b1c 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
15b1d 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 rc;. }.#endif.
15b1e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15b1f 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
15b20 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 (pRoot->pDbPage)
15b21 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 );. zeroPage(p
15b22 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54 Root, flags | PT
15b23 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74 F_LEAF);. sqlit
15b24 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f e3PagerUnref(pRo
15b25 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ot->pDbPage);.
15b26 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29 *piTable = (int)
15b27 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 pgnoRoot;. retu
15b28 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
15b29 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
15b2a 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
15b2b 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 reateTable(Btree
15b2c 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c *p, int *piTabl
15b2d 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 e, int flags){.
15b2e 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
15b2f 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
15b30 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 . p->pBt->db =
15b31 70 2d 3e 64 62 3b 0a 20 20 72 63 20 3d 20 62 74 p->db;. rc = bt
15b32 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 reeCreateTable(p
15b33 2c 20 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 , piTable, flags
15b34 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
15b35 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
15b36 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
15b37 20 45 72 61 73 65 20 74 68 65 20 67 69 76 65 6e Erase the given
15b38 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 61 database page a
15b39 6e 64 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 nd all its child
15b3a 72 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 ren. Return.**
15b3b 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 the page to the
15b3c 66 72 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 freelist..*/.sta
15b3d 74 69 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74 tic int clearDat
15b3e 61 62 61 73 65 50 61 67 65 28 0a 20 20 42 74 53 abasePage(. BtS
15b3f 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 hared *pBt,
15b40 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 /* The BTr
15b41 65 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 ee that contains
15b42 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 the table */.
15b43 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 Pgno pgno,
15b44 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
15b45 6d 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f mber to clear */
15b46 0a 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 . int freePageF
15b47 6c 61 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c lag, /* Deal
15b48 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74 locate page if t
15b49 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e rue */. int *pn
15b4a 43 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 Change.){. MemP
15b4b 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a age *pPage = 0;.
15b4c 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 int rc;. unsi
15b4d 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c gned char *pCell
15b4e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 ;. int i;.. as
15b4f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15b50 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
15b51 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67 tex) );. if( pg
15b52 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75 6e no>pagerPagecoun
15b53 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 t(pBt) ){. re
15b54 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
15b55 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 UPT_BKPT;. }..
15b56 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 rc = getAndInit
15b57 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 Page(pBt, pgno,
15b58 26 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 &pPage);. if( r
15b59 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 c ) goto clearda
15b5a 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a tabasepage_out;.
15b5b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
15b5c 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b ge->nCell; i++){
15b5d 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e . pCell = fin
15b5e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b dCell(pPage, i);
15b5f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d . if( !pPage-
15b60 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 >leaf ){. r
15b61 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 c = clearDatabas
15b62 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 ePage(pBt, get4b
15b63 79 74 65 28 70 43 65 6c 6c 29 2c 20 31 2c 20 70 yte(pCell), 1, p
15b64 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 nChange);.
15b65 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c if( rc ) goto cl
15b66 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f eardatabasepage_
15b67 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 out;. }. r
15b68 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 c = clearCell(pP
15b69 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 age, pCell);.
15b6a 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 if( rc ) goto c
15b6b 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 leardatabasepage
15b6c 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 _out;. }. if(
15b6d 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
15b6e 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61 rc = clearDa
15b6f 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20 tabasePage(pBt,
15b70 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
15b71 3e 61 44 61 74 61 5b 38 5d 29 2c 20 31 2c 20 70 >aData[8]), 1, p
15b72 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 69 66 nChange);. if
15b73 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 ( rc ) goto clea
15b74 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 rdatabasepage_ou
15b75 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 t;. }else if( p
15b76 6e 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20 61 nChange ){. a
15b77 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e ssert( pPage->in
15b78 74 4b 65 79 20 29 3b 0a 20 20 20 20 2a 70 6e 43 tKey );. *pnC
15b79 68 61 6e 67 65 20 2b 3d 20 70 50 61 67 65 2d 3e hange += pPage->
15b7a 6e 43 65 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 nCell;. }. if(
15b7b 20 66 72 65 65 50 61 67 65 46 6c 61 67 20 29 7b freePageFlag ){
15b7c 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 . rc = freePa
15b7d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c ge(pPage);. }el
15b7e 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c se if( (rc = sql
15b7f 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
15b80 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 3d Page->pDbPage))=
15b81 3d 30 20 29 7b 0a 20 20 20 20 7a 65 72 6f 50 61 =0 ){. zeroPa
15b82 67 65 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d ge(pPage, pPage-
15b83 3e 61 44 61 74 61 5b 30 5d 20 7c 20 50 54 46 5f >aData[0] | PTF_
15b84 4c 45 41 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61 LEAF);. }..clea
15b85 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 rdatabasepage_ou
15b86 74 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 t:. releasePage
15b87 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 (pPage);. retur
15b88 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 n rc;.}../*.** D
15b89 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d elete all inform
15b8a 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73 69 6e ation from a sin
15b8b 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 gle table in the
15b8c 20 64 61 74 61 62 61 73 65 2e 20 20 69 54 61 62 database. iTab
15b8d 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 70 61 67 le is.** the pag
15b8e 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
15b8f 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c root of the tabl
15b90 65 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72 e. After this r
15b91 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2c 0a outine returns,.
15b92 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 ** the root page
15b93 20 69 73 20 65 6d 70 74 79 2c 20 62 75 74 20 73 is empty, but s
15b94 74 69 6c 6c 20 65 78 69 73 74 73 2e 0a 2a 2a 0a till exists..**.
15b95 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
15b96 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 will fail with S
15b97 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 QLITE_LOCKED if
15b98 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 there are any op
15b99 65 6e 0a 2a 2a 20 72 65 61 64 20 63 75 72 73 6f en.** read curso
15b9a 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e rs on the table.
15b9b 20 20 4f 70 65 6e 20 77 72 69 74 65 20 63 75 72 Open write cur
15b9c 73 6f 72 73 20 61 72 65 20 6d 6f 76 65 64 20 74 sors are moved t
15b9d 6f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66 o the.** root of
15b9e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a the table..**.*
15b9f 2a 20 49 66 20 70 6e 43 68 61 6e 67 65 20 69 73 * If pnChange is
15ba0 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
15ba1 74 61 62 6c 65 20 69 54 61 62 6c 65 20 6d 75 73 table iTable mus
15ba2 74 20 62 65 20 61 6e 20 69 6e 74 6b 65 79 20 74 t be an intkey t
15ba3 61 62 6c 65 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 able. The.** int
15ba4 65 67 65 72 20 76 61 6c 75 65 20 70 6f 69 6e 74 eger value point
15ba5 65 64 20 74 6f 20 62 79 20 70 6e 43 68 61 6e 67 ed to by pnChang
15ba6 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 e is incremented
15ba7 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f by the number o
15ba8 66 0a 2a 2a 20 65 6e 74 72 69 65 73 20 69 6e 20 f.** entries in
15ba9 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 the table..*/.SQ
15baa 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
15bab 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
15bac 61 72 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 arTable(Btree *p
15bad 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e , int iTable, in
15bae 74 20 2a 70 6e 43 68 61 6e 67 65 29 7b 0a 20 20 t *pnChange){.
15baf 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 int rc;. BtShar
15bb0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
15bb1 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
15bb2 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d Enter(p);. pBt-
15bb3 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 >db = p->db;. i
15bb4 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 f( p->inTrans!=T
15bb5 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 RANS_WRITE ){.
15bb6 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 rc = pBt->read
15bb7 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 Only ? SQLITE_RE
15bb8 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f ADONLY : SQLITE_
15bb9 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 20 69 ERROR;. }else i
15bba 66 28 20 28 72 63 20 3d 20 63 68 65 63 6b 52 65 f( (rc = checkRe
15bbb 61 64 4c 6f 63 6b 73 28 70 2c 20 69 54 61 62 6c adLocks(p, iTabl
15bbc 65 2c 20 30 2c 20 31 29 29 21 3d 53 51 4c 49 54 e, 0, 1))!=SQLIT
15bbd 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 6e E_OK ){. /* n
15bbe 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a othing to do */.
15bbf 20 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 }else if( SQLI
15bc0 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 61 76 TE_OK!=(rc = sav
15bc1 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c eAllCursors(pBt,
15bc2 20 69 54 61 62 6c 65 2c 20 30 29 29 20 29 7b 0a iTable, 0)) ){.
15bc3 20 20 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 /* nothing t
15bc4 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b o do */. }else{
15bc5 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 . rc = clearD
15bc6 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c atabasePage(pBt,
15bc7 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 (Pgno)iTable, 0
15bc8 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 7d , pnChange);. }
15bc9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
15bca 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
15bcb 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 n rc;.}../*.** E
15bcc 72 61 73 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 rase all informa
15bcd 74 69 6f 6e 20 69 6e 20 61 20 74 61 62 6c 65 20 tion in a table
15bce 61 6e 64 20 61 64 64 20 74 68 65 20 72 6f 6f 74 and add the root
15bcf 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
15bd0 0a 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 .** the freelist
15bd1 2e 20 20 45 78 63 65 70 74 2c 20 74 68 65 20 72 . Except, the r
15bd2 6f 6f 74 20 6f 66 20 74 68 65 20 70 72 69 6e 63 oot of the princ
15bd3 69 70 6c 65 20 74 61 62 6c 65 20 28 74 68 65 20 iple table (the
15bd4 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61 67 65 20 31 one on.** page 1
15bd5 29 20 69 73 20 6e 65 76 65 72 20 61 64 64 65 64 ) is never added
15bd6 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 to the freelist
15bd7 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
15bd8 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77 tine will fail w
15bd9 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 ith SQLITE_LOCKE
15bda 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 D if there are a
15bdb 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63 75 72 73 6f ny open.** curso
15bdc 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e rs on the table.
15bdd 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 .**.** If AUTOVA
15bde 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 64 20 CUUM is enabled
15bdf 61 6e 64 20 74 68 65 20 70 61 67 65 20 61 74 20 and the page at
15be0 69 54 61 62 6c 65 20 69 73 20 6e 6f 74 20 74 68 iTable is not th
15be1 65 20 6c 61 73 74 0a 2a 2a 20 72 6f 6f 74 20 70 e last.** root p
15be2 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 age in the datab
15be3 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 ase file, then t
15be4 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 he last root pag
15be5 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 e .** in the dat
15be6 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d 6f abase file is mo
15be7 76 65 64 20 69 6e 74 6f 20 74 68 65 20 73 6c 6f ved into the slo
15be8 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 t formerly occup
15be9 69 65 64 20 62 79 0a 2a 2a 20 69 54 61 62 6c 65 ied by.** iTable
15bea 20 61 6e 64 20 74 68 61 74 20 6c 61 73 74 20 73 and that last s
15beb 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63 lot formerly occ
15bec 75 70 69 65 64 20 62 79 20 74 68 65 20 6c 61 73 upied by the las
15bed 74 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 t root page.** i
15bee 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 s added to the f
15bef 72 65 65 6c 69 73 74 20 69 6e 73 74 65 61 64 20 reelist instead
15bf0 6f 66 20 69 54 61 62 6c 65 2e 20 20 49 6e 20 74 of iTable. In t
15bf1 68 69 73 20 73 61 79 2c 20 61 6c 6c 0a 2a 2a 20 his say, all.**
15bf2 72 6f 6f 74 20 70 61 67 65 73 20 61 72 65 20 6b root pages are k
15bf3 65 70 74 20 61 74 20 74 68 65 20 62 65 67 69 6e ept at the begin
15bf4 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 ning of the data
15bf5 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 base file, which
15bf6 0a 2a 2a 20 69 73 20 6e 65 63 65 73 73 61 72 79 .** is necessary
15bf7 20 66 6f 72 20 41 55 54 4f 56 41 43 55 55 4d 20 for AUTOVACUUM
15bf8 74 6f 20 77 6f 72 6b 20 72 69 67 68 74 2e 20 20 to work right.
15bf9 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65 74 20 *piMoved is set
15bfa 74 6f 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 to the .** page
15bfb 6e 75 6d 62 65 72 20 74 68 61 74 20 75 73 65 64 number that used
15bfc 20 74 6f 20 62 65 20 74 68 65 20 6c 61 73 74 20 to be the last
15bfd 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 root page in the
15bfe 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 2a 2a 20 file before.**
15bff 74 68 65 20 6d 6f 76 65 2e 20 20 49 66 20 6e 6f the move. If no
15c00 20 70 61 67 65 20 67 65 74 73 20 6d 6f 76 65 64 page gets moved
15c01 2c 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65 , *piMoved is se
15c02 74 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 65 20 6c t to 0..** The l
15c03 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 69 73 ast root page is
15c04 20 72 65 63 6f 72 64 65 64 20 69 6e 20 6d 65 74 recorded in met
15c05 61 5b 33 5d 20 61 6e 64 20 74 68 65 20 76 61 6c a[3] and the val
15c06 75 65 20 6f 66 0a 2a 2a 20 6d 65 74 61 5b 33 5d ue of.** meta[3]
15c07 20 69 73 20 75 70 64 61 74 65 64 20 62 79 20 74 is updated by t
15c08 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a his procedure..*
15c09 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 /.static int btr
15c0a 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 eeDropTable(Btre
15c0b 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c e *p, Pgno iTabl
15c0c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29 e, int *piMoved)
15c0d 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 {. int rc;. Me
15c0e 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 mPage *pPage = 0
15c0f 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
15c10 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 t = p->pBt;.. a
15c11 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
15c12 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 reeHoldsMutex(p)
15c13 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 );. if( p->inT
15c14 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 rans!=TRANS_WRIT
15c15 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 E ){. return
15c16 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 pBt->readOnly ?
15c17 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 SQLITE_READONLY
15c18 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a : SQLITE_ERROR;.
15c19 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 }.. /* It is
15c1a 69 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 20 illegal to drop
15c1b 61 20 74 61 62 6c 65 20 69 66 20 61 6e 79 20 63 a table if any c
15c1c 75 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 20 ursors are open
15c1d 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 on the. ** data
15c1e 62 61 73 65 2e 20 54 68 69 73 20 69 73 20 62 65 base. This is be
15c1f 63 61 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 61 cause in auto-va
15c20 63 75 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 61 cuum mode the ba
15c21 63 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 6e ckend may. ** n
15c22 65 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74 eed to move anot
15c23 68 65 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f her root-page to
15c24 20 66 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 74 fill a gap left
15c25 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 0a by the deleted.
15c26 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 20 ** root page.
15c27 49 66 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f If an open curso
15c28 72 20 77 61 73 20 75 73 69 6e 67 20 74 68 69 73 r was using this
15c29 20 70 61 67 65 20 61 20 70 72 6f 62 6c 65 6d 20 page a problem
15c2a 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 75 would . ** occu
15c2b 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 r.. */. if( pB
15c2c 74 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 t->pCursor ){.
15c2d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15c2e 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 0a 20 20 72 LOCKED;. }.. r
15c2f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
15c30 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50 67 GetPage(pBt, (Pg
15c31 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50 61 67 no)iTable, &pPag
15c32 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 e, 0);. if( rc
15c33 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 ) return rc;. r
15c34 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
15c35 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20 69 54 ClearTable(p, iT
15c36 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 able, 0);. if(
15c37 72 63 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 rc ){. releas
15c38 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
15c39 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
15c3a 0a 0a 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 .. *piMoved = 0
15c3b 3b 0a 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3e ;.. if( iTable>
15c3c 31 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 1 ){.#ifdef SQLI
15c3d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
15c3e 55 4d 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 UM. rc = free
15c3f 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
15c40 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
15c41 67 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 ge);.#else. i
15c42 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 f( pBt->autoVacu
15c43 75 6d 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f um ){. Pgno
15c44 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 maxRootPgno;.
15c45 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
15c46 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20 BtreeGetMeta(p,
15c47 34 2c 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 4, &maxRootPgno)
15c48 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
15c49 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15c4a 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
15c4b 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
15c4c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
15c4d 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 69 }.. if( i
15c4e 54 61 62 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 Table==maxRootPg
15c4f 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a no ){. /*
15c50 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 If the table be
15c51 69 6e 67 20 64 72 6f 70 70 65 64 20 69 73 20 74 ing dropped is t
15c52 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 he table with th
15c53 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 e largest root-p
15c54 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e age. ** n
15c55 75 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61 74 umber in the dat
15c56 61 62 61 73 65 2c 20 70 75 74 20 74 68 65 20 72 abase, put the r
15c57 6f 6f 74 20 70 61 67 65 20 6f 6e 20 74 68 65 20 oot page on the
15c58 66 72 65 65 20 6c 69 73 74 2e 20 0a 20 20 20 20 free list. .
15c59 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 */. r
15c5a 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 c = freePage(pPa
15c5b 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c ge);. rel
15c5c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
15c5d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
15c5e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15c5f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
15c60 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
15c61 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
15c62 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 /* The table b
15c63 65 69 6e 67 20 64 72 6f 70 70 65 64 20 64 6f 65 eing dropped doe
15c64 73 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 6c s not have the l
15c65 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 argest root-page
15c66 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 . ** numb
15c67 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 er in the databa
15c68 73 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65 20 se. So move the
15c69 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 69 page that does i
15c6a 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20 20 nto the .
15c6b 20 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79 20 ** gap left by
15c6c 74 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 the deleted root
15c6d 2d 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a -page.. *
15c6e 2f 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 /. MemPag
15c6f 65 20 2a 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20 e *pMove;.
15c70 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
15c71 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 63 age);. rc
15c72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
15c73 65 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 etPage(pBt, maxR
15c74 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c ootPgno, &pMove,
15c75 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0);. if(
15c76 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
15c77 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
15c78 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
15c79 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 . rc = re
15c7a 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 locatePage(pBt,
15c7b 70 4d 6f 76 65 2c 20 50 54 52 4d 41 50 5f 52 4f pMove, PTRMAP_RO
15c7c 4f 54 50 41 47 45 2c 20 30 2c 20 69 54 61 62 6c OTPAGE, 0, iTabl
15c7d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 72 e, 0);. r
15c7e 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76 65 eleasePage(pMove
15c7f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
15c80 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
15c81 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
15c82 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
15c83 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
15c84 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 te3BtreeGetPage(
15c85 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f pBt, maxRootPgno
15c86 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 , &pMove, 0);.
15c87 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
15c88 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
15c89 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
15c8a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
15c8b 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 rc = freePage(
15c8c 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20 pMove);.
15c8d 72 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76 releasePage(pMov
15c8e 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
15c8f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
15c90 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
15c91 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
15c92 20 20 20 20 20 20 20 20 2a 70 69 4d 6f 76 65 64 *piMoved
15c93 20 3d 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a = maxRootPgno;.
15c94 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f }.. /
15c95 2a 20 53 65 74 20 74 68 65 20 6e 65 77 20 27 6d * Set the new 'm
15c96 61 78 2d 72 6f 6f 74 2d 70 61 67 65 27 20 76 61 ax-root-page' va
15c97 6c 75 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 lue in the datab
15c98 61 73 65 20 68 65 61 64 65 72 2e 20 54 68 69 73 ase header. This
15c99 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 . ** is the
15c9a 20 6f 6c 64 20 76 61 6c 75 65 20 6c 65 73 73 20 old value less
15c9b 6f 6e 65 2c 20 6c 65 73 73 20 6f 6e 65 20 6d 6f one, less one mo
15c9c 72 65 20 69 66 20 74 68 61 74 20 68 61 70 70 65 re if that happe
15c9d 6e 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 ns to. ** b
15c9e 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 e a root-page nu
15c9f 6d 62 65 72 2c 20 6c 65 73 73 20 6f 6e 65 20 61 mber, less one a
15ca0 67 61 69 6e 20 69 66 20 74 68 61 74 20 69 73 20 gain if that is
15ca1 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 50 45 4e the. ** PEN
15ca2 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 2e 0a DING_BYTE_PAGE..
15ca3 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6d */. m
15ca4 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 axRootPgno--;.
15ca5 20 20 20 20 69 66 28 20 6d 61 78 52 6f 6f 74 50 if( maxRootP
15ca6 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 gno==PENDING_BYT
15ca7 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 E_PAGE(pBt) ){.
15ca8 20 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 maxRootPg
15ca9 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 no--;. }.
15caa 20 20 20 20 69 66 28 20 6d 61 78 52 6f 6f 74 50 if( maxRootP
15cab 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 gno==PTRMAP_PAGE
15cac 4e 4f 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 NO(pBt, maxRootP
15cad 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 gno) ){.
15cae 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 maxRootPgno--;.
15caf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
15cb0 65 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f ert( maxRootPgno
15cb1 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
15cb2 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20 AGE(pBt) );..
15cb3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
15cb4 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 treeUpdateMeta(p
15cb5 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f , 4, maxRootPgno
15cb6 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
15cb7 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 rc = freePag
15cb8 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 e(pPage);.
15cb9 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
15cba 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 e);. }.#endif
15cbb 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
15cbc 20 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 If sqlite3Btree
15cbd 44 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61 DropTable was ca
15cbe 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 20 lled on page 1.
15cbf 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 */. zeroPage(
15cc0 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b 45 pPage, PTF_INTKE
15cc1 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20 20 Y|PTF_LEAF );.
15cc2 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
15cc3 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 age);. }. retu
15cc4 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 rn rc; .}.SQLIT
15cc5 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
15cc6 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 lite3BtreeDropTa
15cc7 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ble(Btree *p, in
15cc8 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 t iTable, int *p
15cc9 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 iMoved){. int r
15cca 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 c;. sqlite3Btre
15ccb 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 2d 3e eEnter(p);. p->
15ccc 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b pBt->db = p->db;
15ccd 0a 20 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f . rc = btreeDro
15cce 70 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 pTable(p, iTable
15ccf 2c 20 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 , piMoved);. sq
15cd0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
15cd1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
15cd2 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 .}.../*.** Read
15cd3 74 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 the meta-informa
15cd4 74 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 tion out of a da
15cd5 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 tabase file. Me
15cd6 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20 ta[0].** is the
15cd7 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 number of free p
15cd8 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 ages currently i
15cd9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
15cda 20 4d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f Meta[1].** thro
15cdb 75 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65 ugh meta[15] are
15cdc 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 available for u
15cdd 73 65 20 62 79 20 68 69 67 68 65 72 20 6c 61 79 se by higher lay
15cde 65 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a ers. Meta[0].**
15cdf 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 is read-only, t
15ce0 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 72 65 he others are re
15ce1 61 64 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a ad/write..** .**
15ce2 20 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 The schema laye
15ce3 72 20 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76 r numbers meta v
15ce4 61 6c 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c alues differentl
15ce5 79 2e 20 20 41 74 20 74 68 65 20 73 63 68 65 6d y. At the schem
15ce6 61 0a 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20 a.** layer (and
15ce7 74 68 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e the SetCookie an
15ce8 64 20 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63 d ReadCookie opc
15ce9 6f 64 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72 odes) the number
15cea 20 6f 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65 of.** free page
15ceb 73 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 s is not visible
15cec 2e 20 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 . So Cookie[0]
15ced 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d is the same as M
15cee 65 74 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 eta[1]..*/.SQLIT
15cef 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
15cf0 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 lite3BtreeGetMet
15cf1 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 a(Btree *p, int
15cf2 69 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29 idx, u32 *pMeta)
15cf3 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 {. DbPage *pDbP
15cf4 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 age;. int rc;.
15cf5 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
15cf6 70 50 31 3b 0a 20 20 42 74 53 68 61 72 65 64 20 pP1;. BtShared
15cf7 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a *pBt = p->pBt;..
15cf8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
15cf9 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 ter(p);. pBt->d
15cfa 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a b = p->db;.. /*
15cfb 20 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61 2d Reading a meta-
15cfc 64 61 74 61 20 76 61 6c 75 65 20 72 65 71 75 69 data value requi
15cfd 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 res a read-lock
15cfe 6f 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20 68 on page 1 (and h
15cff 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73 71 ence. ** the sq
15d00 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c lite_master tabl
15d01 65 2e 20 57 65 20 67 72 61 62 20 74 68 69 73 20 e. We grab this
15d02 6c 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73 20 lock regardless
15d03 6f 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20 20 of whether or.
15d04 2a 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49 54 ** not the SQLIT
15d05 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 E_ReadUncommitte
15d06 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 28 74 d flag is set (t
15d07 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 he table rooted
15d08 61 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20 69 at page. ** 1 i
15d09 73 20 74 72 65 61 74 65 64 20 61 73 20 61 20 73 s treated as a s
15d0a 70 65 63 69 61 6c 20 63 61 73 65 20 62 79 20 71 pecial case by q
15d0b 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 29 20 ueryTableLock()
15d0c 61 6e 64 20 6c 6f 63 6b 54 61 62 6c 65 28 29 29 and lockTable())
15d0d 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 .. */. rc = qu
15d0e 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 eryTableLock(p,
15d0f 31 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 1, READ_LOCK);.
15d10 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
15d11 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
15d12 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
15d13 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
15d14 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 }.. assert( id
15d15 78 3e 3d 30 20 26 26 20 69 64 78 3c 3d 31 35 20 x>=0 && idx<=15
15d16 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 50 );. if( pBt->pP
15d17 61 67 65 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54 age1 ){. /* T
15d18 68 65 20 62 2d 74 72 65 65 20 69 73 20 61 6c 72 he b-tree is alr
15d19 65 61 64 79 20 68 6f 6c 64 69 6e 67 20 61 20 72 eady holding a r
15d1a 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 eference to page
15d1b 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 1 of the databa
15d1c 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 se. ** file.
15d1d 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
15d1e 20 72 65 71 75 69 72 65 64 20 6d 65 74 61 2d 64 required meta-d
15d1f 61 74 61 20 76 61 6c 75 65 20 63 61 6e 20 62 65 ata value can be
15d20 20 72 65 61 64 20 64 69 72 65 63 74 6c 79 0a 20 read directly.
15d21 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70 ** from the p
15d22 61 67 65 20 64 61 74 61 20 6f 66 20 74 68 69 73 age data of this
15d23 20 72 65 66 65 72 65 6e 63 65 2e 20 54 68 69 73 reference. This
15d24 20 69 73 20 73 6c 69 67 68 74 6c 79 20 66 61 73 is slightly fas
15d25 74 65 72 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 ter than. **
15d26 72 65 71 75 65 73 74 69 6e 67 20 61 20 6e 65 77 requesting a new
15d27 20 72 65 66 65 72 65 6e 63 65 20 66 72 6f 6d 20 reference from
15d28 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 2e the pager layer.
15d29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 31 20 . */. pP1
15d2a 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
15d2b 20 2a 29 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e *)pBt->pPage1->
15d2c 61 44 61 74 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a aData;. }else{.
15d2d 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74 72 65 /* The b-tre
15d2e 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 e does not have
15d2f 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 a reference to p
15d30 61 67 65 20 31 20 6f 66 20 74 68 65 20 64 61 74 age 1 of the dat
15d31 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 abase file..
15d32 2a 2a 20 4f 62 74 61 69 6e 20 6f 6e 65 20 66 72 ** Obtain one fr
15d33 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 om the pager lay
15d34 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 er.. */. r
15d35 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
15d36 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c Get(pBt->pPager,
15d37 20 31 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 1, &pDbPage);.
15d38 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
15d39 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
15d3a 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72 eave(p);. r
15d3b 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
15d3c 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73 69 67 pP1 = (unsig
15d3d 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69 74 ned char *)sqlit
15d3e 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
15d3f 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 2a DbPage);. }. *
15d40 70 4d 65 74 61 20 3d 20 67 65 74 34 62 79 74 65 pMeta = get4byte
15d41 28 26 70 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 (&pP1[36 + idx*4
15d42 5d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 ]);.. /* If the
15d43 20 62 2d 74 72 65 65 20 69 73 20 6e 6f 74 20 68 b-tree is not h
15d44 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e olding a referen
15d45 63 65 20 74 6f 20 70 61 67 65 20 31 2c 20 74 68 ce to page 1, th
15d46 65 6e 20 6f 6e 65 20 77 61 73 20 0a 20 20 2a 2a en one was . **
15d47 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 requested from
15d48 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 20 the pager layer
15d49 69 6e 20 74 68 65 20 61 62 6f 76 65 20 62 6c 6f in the above blo
15d4a 63 6b 2e 20 52 65 6c 65 61 73 65 20 69 74 20 6e ck. Release it n
15d4b 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 ow.. */. if( !
15d4c 70 42 74 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 pBt->pPage1 ){.
15d4d 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
15d4e 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 nref(pDbPage);.
15d4f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f }.. /* If auto
15d50 76 61 63 75 75 6d 65 64 20 69 73 20 64 69 73 61 vacuumed is disa
15d51 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 69 bled in this bui
15d52 6c 64 20 62 75 74 20 77 65 20 61 72 65 20 74 72 ld but we are tr
15d53 79 69 6e 67 20 74 6f 20 0a 20 20 2a 2a 20 61 63 ying to . ** ac
15d54 63 65 73 73 20 61 6e 20 61 75 74 6f 76 61 63 75 cess an autovacu
15d55 75 6d 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 umed database, t
15d56 68 65 6e 20 6d 61 6b 65 20 74 68 65 20 64 61 74 hen make the dat
15d57 61 62 61 73 65 20 72 65 61 64 6f 6e 6c 79 2e 20 abase readonly.
15d58 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
15d59 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
15d5a 55 55 4d 0a 20 20 69 66 28 20 69 64 78 3d 3d 34 UUM. if( idx==4
15d5b 20 26 26 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 && *pMeta>0 ) p
15d5c 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 Bt->readOnly = 1
15d5d 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 ;.#endif.. /* G
15d5e 72 61 62 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 rab the read-loc
15d5f 6b 20 6f 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a k on page 1. */.
15d60 20 20 72 63 20 3d 20 6c 6f 63 6b 54 61 62 6c 65 rc = lockTable
15d61 28 70 2c 20 31 2c 20 52 45 41 44 5f 4c 4f 43 4b (p, 1, READ_LOCK
15d62 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
15d63 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
15d64 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
15d65 20 57 72 69 74 65 20 6d 65 74 61 2d 69 6e 66 6f Write meta-info
15d66 72 6d 61 74 69 6f 6e 20 62 61 63 6b 20 69 6e 74 rmation back int
15d67 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 o the database.
15d68 20 4d 65 74 61 5b 30 5d 20 69 73 0a 2a 2a 20 72 Meta[0] is.** r
15d69 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 6d 61 79 ead-only and may
15d6a 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 2e not be written.
15d6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
15d6c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
15d6d 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 reeUpdateMeta(Bt
15d6e 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c ree *p, int idx,
15d6f 20 75 33 32 20 69 4d 65 74 61 29 7b 0a 20 20 42 u32 iMeta){. B
15d70 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
15d71 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65 ->pBt;. unsigne
15d72 64 20 63 68 61 72 20 2a 70 50 31 3b 0a 20 20 69 d char *pP1;. i
15d73 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 nt rc;. assert(
15d74 20 69 64 78 3e 3d 31 20 26 26 20 69 64 78 3c 3d idx>=1 && idx<=
15d75 31 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 15 );. sqlite3B
15d76 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
15d77 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b pBt->db = p->db;
15d78 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
15d79 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s!=TRANS_WRITE )
15d7a 7b 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e {. rc = pBt->
15d7b 72 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 readOnly ? SQLIT
15d7c 45 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c E_READONLY : SQL
15d7d 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c ITE_ERROR;. }el
15d7e 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
15d7f 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 pBt->pPage1!=0 )
15d80 3b 0a 20 20 20 20 70 50 31 20 3d 20 70 42 74 2d ;. pP1 = pBt-
15d81 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a >pPage1->aData;.
15d82 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
15d83 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e PagerWrite(pBt->
15d84 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
15d85 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
15d86 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
15d87 20 70 75 74 34 62 79 74 65 28 26 70 50 31 5b 33 put4byte(&pP1[3
15d88 36 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74 6 + idx*4], iMet
15d89 61 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 a);.#ifndef SQLI
15d8a 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
15d8b 55 4d 0a 20 20 20 20 20 20 69 66 28 20 69 64 78 UM. if( idx
15d8c 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ==7 ){. a
15d8d 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f ssert( pBt->auto
15d8e 56 61 63 75 75 6d 20 7c 7c 20 69 4d 65 74 61 3d Vacuum || iMeta=
15d8f 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 =0 );. as
15d90 73 65 72 74 28 20 69 4d 65 74 61 3d 3d 30 20 7c sert( iMeta==0 |
15d91 7c 20 69 4d 65 74 61 3d 3d 31 20 29 3b 0a 20 20 | iMeta==1 );.
15d92 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 pBt->incrV
15d93 61 63 75 75 6d 20 3d 20 69 4d 65 74 61 3b 0a 20 acuum = iMeta;.
15d94 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
15d95 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
15d96 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
15d97 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
15d98 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
15d99 20 66 6c 61 67 20 62 79 74 65 20 61 74 20 74 68 flag byte at th
15d9a 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
15d9b 68 65 20 70 61 67 65 20 74 68 61 74 20 74 68 65 he page that the
15d9c 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 63 75 cursor.** is cu
15d9d 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
15d9e 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 to..*/.SQLITE_P
15d9f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15da0 65 33 42 74 72 65 65 46 6c 61 67 73 28 42 74 43 e3BtreeFlags(BtC
15da1 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
15da2 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 61 62 /* TODO: What ab
15da3 6f 75 74 20 43 55 52 53 4f 52 5f 52 45 51 55 49 out CURSOR_REQUI
15da4 52 45 53 45 45 4b 20 73 74 61 74 65 3f 20 50 72 RESEEK state? Pr
15da5 6f 62 61 62 6c 79 20 6e 65 65 64 20 74 6f 20 63 obably need to c
15da6 61 6c 6c 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 all. ** restore
15da7 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
15da8 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 4d 65 here.. */. Me
15da9 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 mPage *pPage;.
15daa 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
15dab 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 70 ition(pCur);. p
15dac 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
15dad 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
15dae 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 ;. assert( curs
15daf 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
15db0 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
15db1 70 50 61 67 65 2d 3e 70 42 74 3d 3d 70 43 75 72 pPage->pBt==pCur
15db2 2d 3e 70 42 74 20 29 3b 0a 20 20 72 65 74 75 72 ->pBt );. retur
15db3 6e 20 70 50 61 67 65 20 3f 20 70 50 61 67 65 2d n pPage ? pPage-
15db4 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
15db5 72 4f 66 66 73 65 74 5d 20 3a 20 30 3b 0a 7d 0a rOffset] : 0;.}.
15db6 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
15db7 68 65 20 70 61 67 65 72 20 61 73 73 6f 63 69 61 he pager associa
15db8 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65 ted with a BTree
15db9 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
15dba 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 is used for.** t
15dbb 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 esting and debug
15dbc 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 ging only..*/.SQ
15dbd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 61 67 LITE_PRIVATE Pag
15dbe 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 er *sqlite3Btree
15dbf 50 61 67 65 72 28 42 74 72 65 65 20 2a 70 29 7b Pager(Btree *p){
15dc0 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 . return p->pBt
15dc1 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 ->pPager;.}..#if
15dc2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
15dc3 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b _INTEGRITY_CHECK
15dc4 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 ./*.** Append a
15dc5 6d 65 73 73 61 67 65 20 74 6f 20 74 68 65 20 65 message to the e
15dc6 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 rror message str
15dc7 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ing..*/.static v
15dc8 6f 69 64 20 63 68 65 63 6b 41 70 70 65 6e 64 4d oid checkAppendM
15dc9 73 67 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 sg(. IntegrityC
15dca 6b 20 2a 70 43 68 65 63 6b 2c 0a 20 20 63 68 61 k *pCheck,. cha
15dcb 72 20 2a 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 r *zMsg1,. cons
15dcc 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
15dcd 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c . ....){. va_l
15dce 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 ist ap;. if( !p
15dcf 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72 Check->mxErr ) r
15dd0 65 74 75 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d eturn;. pCheck-
15dd1 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65 >mxErr--;. pChe
15dd2 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 ck->nErr++;. va
15dd3 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
15dd4 61 74 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 at);. if( pChec
15dd5 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43 68 61 72 20 k->errMsg.nChar
15dd6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 ){. sqlite3St
15dd7 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43 rAccumAppend(&pC
15dd8 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c heck->errMsg, "\
15dd9 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 n", 1);. }. if
15dda 28 20 7a 4d 73 67 31 20 29 7b 0a 20 20 20 20 73 ( zMsg1 ){. s
15ddb 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
15ddc 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 pend(&pCheck->er
15ddd 72 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 rMsg, zMsg1, -1)
15dde 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 ;. }. sqlite3V
15ddf 58 50 72 69 6e 74 66 28 26 70 43 68 65 63 6b 2d XPrintf(&pCheck-
15de0 3e 65 72 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 >errMsg, 1, zFor
15de1 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
15de2 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 43 nd(ap);. if( pC
15de3 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c heck->errMsg.mal
15de4 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
15de5 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 pCheck->mallocF
15de6 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d ailed = 1;. }.}
15de7 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
15de8 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 E_OMIT_INTEGRITY
15de9 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 _CHECK */..#ifnd
15dea 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
15deb 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f NTEGRITY_CHECK./
15dec 2a 0a 2a 2a 20 41 64 64 20 31 20 74 6f 20 74 68 *.** Add 1 to th
15ded 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
15dee 74 20 66 6f 72 20 70 61 67 65 20 69 50 61 67 65 t for page iPage
15def 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 74 68 . If this is th
15df0 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 e second.** refe
15df1 72 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 rence to the pag
15df2 65 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 e, add an error
15df3 6d 65 73 73 61 67 65 20 74 6f 20 70 43 68 65 63 message to pChec
15df4 6b 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 k->zErrMsg..** R
15df5 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65 eturn 1 if there
15df6 20 61 72 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 are 2 ore more
15df7 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 references to th
15df8 65 20 70 61 67 65 20 61 6e 64 20 30 20 69 66 0a e page and 0 if.
15df9 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 68 ** if this is th
15dfa 65 20 66 69 72 73 74 20 72 65 66 65 72 65 6e 63 e first referenc
15dfb 65 20 74 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a e to the page..*
15dfc 2a 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 *.** Also check
15dfd 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 75 that the page nu
15dfe 6d 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 mber is in bound
15dff 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
15e00 20 63 68 65 63 6b 52 65 66 28 49 6e 74 65 67 72 checkRef(Integr
15e01 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 ityCk *pCheck, P
15e02 67 6e 6f 20 69 50 61 67 65 2c 20 63 68 61 72 20 gno iPage, char
15e03 2a 7a 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 *zContext){. if
15e04 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74 ( iPage==0 ) ret
15e05 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 69 50 61 urn 1;. if( iPa
15e06 67 65 3e 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65 ge>pCheck->nPage
15e07 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 ){. checkApp
15e08 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
15e09 43 6f 6e 74 65 78 74 2c 20 22 69 6e 76 61 6c 69 Context, "invali
15e0a 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 25 64 d page number %d
15e0b 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 ", iPage);. r
15e0c 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 eturn 1;. }. i
15e0d 66 28 20 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 f( pCheck->anRef
15e0e 5b 69 50 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 [iPage]==1 ){.
15e0f 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
15e10 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
15e11 74 2c 20 22 32 6e 64 20 72 65 66 65 72 65 6e 63 t, "2nd referenc
15e12 65 20 74 6f 20 70 61 67 65 20 25 64 22 2c 20 69 e to page %d", i
15e13 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Page);. retur
15e14 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n 1;. }. retur
15e15 6e 20 20 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65 n (pCheck->anRe
15e16 66 5b 69 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d f[iPage]++)>1;.}
15e17 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
15e18 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
15e19 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 ./*.** Check tha
15e1a 74 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 74 t the entry in t
15e1b 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 he pointer-map f
15e1c 6f 72 20 70 61 67 65 20 69 43 68 69 6c 64 20 6d or page iChild m
15e1d 61 70 73 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 aps to .** page
15e1e 69 50 61 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 iParent, pointer
15e1f 20 74 79 70 65 20 70 74 72 54 79 70 65 2e 20 49 type ptrType. I
15e20 66 20 6e 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e f not, append an
15e21 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a error message.*
15e22 2a 20 74 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a * to pCheck..*/.
15e23 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 static void chec
15e24 6b 50 74 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 kPtrmap(. Integ
15e25 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 rityCk *pCheck,
15e26 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63 /* Integrity c
15e27 68 65 63 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a heck context */.
15e28 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 Pgno iChild,
15e29 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 69 6c /* Chil
15e2a 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f d page number */
15e2b 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 20 . u8 eType,
15e2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
15e2d 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 ected pointer ma
15e2e 70 20 74 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f p type */. Pgno
15e2f 20 69 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 iParent,
15e30 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70 /* Expected p
15e31 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 72 65 6e ointer map paren
15e32 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f t page number */
15e33 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 . char *zContex
15e34 74 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e t /* Con
15e35 74 65 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e text description
15e36 20 28 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 (used for error
15e37 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e msg) */.){. in
15e38 74 20 72 63 3b 0a 20 20 75 38 20 65 50 74 72 6d t rc;. u8 ePtrm
15e39 61 70 54 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 apType;. Pgno i
15e3a 50 74 72 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 PtrmapParent;..
15e3b 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 rc = ptrmapGet(
15e3c 70 43 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 pCheck->pBt, iCh
15e3d 69 6c 64 2c 20 26 65 50 74 72 6d 61 70 54 79 70 ild, &ePtrmapTyp
15e3e 65 2c 20 26 69 50 74 72 6d 61 70 50 61 72 65 6e e, &iPtrmapParen
15e3f 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 t);. if( rc!=SQ
15e40 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63 LITE_OK ){. c
15e41 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
15e42 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
15e43 22 46 61 69 6c 65 64 20 74 6f 20 72 65 61 64 20 "Failed to read
15e44 70 74 72 6d 61 70 20 6b 65 79 3d 25 64 22 2c 20 ptrmap key=%d",
15e45 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 iChild);. ret
15e46 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 urn;. }.. if(
15e47 65 50 74 72 6d 61 70 54 79 70 65 21 3d 65 54 79 ePtrmapType!=eTy
15e48 70 65 20 7c 7c 20 69 50 74 72 6d 61 70 50 61 72 pe || iPtrmapPar
15e49 65 6e 74 21 3d 69 50 61 72 65 6e 74 20 29 7b 0a ent!=iParent ){.
15e4a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
15e4b 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
15e4c 65 78 74 2c 20 0a 20 20 20 20 20 20 22 42 61 64 ext, . "Bad
15e4d 20 70 74 72 20 6d 61 70 20 65 6e 74 72 79 20 6b ptr map entry k
15e4e 65 79 3d 25 64 20 65 78 70 65 63 74 65 64 3d 28 ey=%d expected=(
15e4f 25 64 2c 25 64 29 20 67 6f 74 3d 28 25 64 2c 25 %d,%d) got=(%d,%
15e50 64 29 22 2c 20 0a 20 20 20 20 20 20 69 43 68 69 d)", . iChi
15e51 6c 64 2c 20 65 54 79 70 65 2c 20 69 50 61 72 65 ld, eType, iPare
15e52 6e 74 2c 20 65 50 74 72 6d 61 70 54 79 70 65 2c nt, ePtrmapType,
15e53 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b iPtrmapParent);
15e54 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f . }.}.#endif../
15e55 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 *.** Check the i
15e56 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 ntegrity of the
15e57 66 72 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61 freelist or of a
15e58 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 n overflow page
15e59 6c 69 73 74 2e 0a 2a 2a 20 56 65 72 69 66 79 20 list..** Verify
15e5a 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 that the number
15e5b 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 of pages on the
15e5c 6c 69 73 74 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 list is N..*/.st
15e5d 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c atic void checkL
15e5e 69 73 74 28 0a 20 20 49 6e 74 65 67 72 69 74 79 ist(. Integrity
15e5f 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 Ck *pCheck, /*
15e60 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 69 Integrity checki
15e61 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
15e62 69 6e 74 20 69 73 46 72 65 65 4c 69 73 74 2c 20 int isFreeList,
15e63 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
15e64 72 20 61 20 66 72 65 65 6c 69 73 74 2e 20 20 46 r a freelist. F
15e65 61 6c 73 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f alse for overflo
15e66 77 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 w page list */.
15e67 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20 int iPage,
15e68 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
15e69 75 6d 62 65 72 20 66 6f 72 20 66 69 72 73 74 20 umber for first
15e6a 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 page in the list
15e6b 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 */. int N,
15e6c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
15e6d 78 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f xpected number o
15e6e 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c f pages in the l
15e6f 69 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ist */. char *z
15e70 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20 20 2f Context /
15e71 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 * Context for er
15e72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a ror messages */.
15e73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e ){. int i;. in
15e74 74 20 65 78 70 65 63 74 65 64 20 3d 20 4e 3b 0a t expected = N;.
15e75 20 20 69 6e 74 20 69 46 69 72 73 74 20 3d 20 69 int iFirst = i
15e76 50 61 67 65 3b 0a 20 20 77 68 69 6c 65 28 20 4e Page;. while( N
15e77 2d 2d 20 3e 20 30 20 26 26 20 70 43 68 65 63 6b -- > 0 && pCheck
15e78 2d 3e 6d 78 45 72 72 20 29 7b 0a 20 20 20 20 44 ->mxErr ){. D
15e79 62 50 61 67 65 20 2a 70 4f 76 66 6c 50 61 67 65 bPage *pOvflPage
15e7a 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 ;. unsigned c
15e7b 68 61 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0a har *pOvflData;.
15e7c 20 20 20 20 69 66 28 20 69 50 61 67 65 3c 31 20 if( iPage<1
15e7d 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 ){. checkAp
15e7e 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
15e7f 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 zContext,.
15e80 20 20 20 22 25 64 20 6f 66 20 25 64 20 70 61 67 "%d of %d pag
15e81 65 73 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 es missing from
15e82 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 73 74 overflow list st
15e83 61 72 74 69 6e 67 20 61 74 20 25 64 22 2c 0a 20 arting at %d",.
15e84 20 20 20 20 20 20 20 20 20 4e 2b 31 2c 20 65 78 N+1, ex
15e85 70 65 63 74 65 64 2c 20 69 46 69 72 73 74 29 3b pected, iFirst);
15e86 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
15e87 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63 }. if( chec
15e88 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 kRef(pCheck, iPa
15e89 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 20 29 20 ge, zContext) )
15e8a 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 break;. if( s
15e8b 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
15e8c 43 68 65 63 6b 2d 3e 70 50 61 67 65 72 2c 20 28 Check->pPager, (
15e8d 50 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 4f 76 Pgno)iPage, &pOv
15e8e 66 6c 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 flPage) ){.
15e8f 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
15e90 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 pCheck, zContext
15e91 2c 20 22 66 61 69 6c 65 64 20 74 6f 20 67 65 74 , "failed to get
15e92 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 page %d", iPage
15e93 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
15e94 20 20 20 20 7d 0a 20 20 20 20 70 4f 76 66 6c 44 }. pOvflD
15e95 61 74 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 ata = (unsigned
15e96 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 char *)sqlite3Pa
15e97 67 65 72 47 65 74 44 61 74 61 28 70 4f 76 66 6c gerGetData(pOvfl
15e98 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 69 Page);. if( i
15e99 73 46 72 65 65 4c 69 73 74 20 29 7b 0a 20 20 20 sFreeList ){.
15e9a 20 20 20 69 6e 74 20 6e 20 3d 20 67 65 74 34 62 int n = get4b
15e9b 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 34 yte(&pOvflData[4
15e9c 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ]);.#ifndef SQLI
15e9d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
15e9e 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 UM. if( pCh
15e9f 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 eck->pBt->autoVa
15ea0 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 cuum ){.
15ea1 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 checkPtrmap(pChe
15ea2 63 6b 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 ck, iPage, PTRMA
15ea3 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a P_FREEPAGE, 0, z
15ea4 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
15ea5 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 }.#endif. i
15ea6 66 28 20 6e 3e 70 43 68 65 63 6b 2d 3e 70 42 74 f( n>pCheck->pBt
15ea7 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 ->usableSize/4-2
15ea8 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
15ea9 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
15eaa 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 k, zContext,.
15eab 20 20 20 20 20 20 20 20 22 66 72 65 65 6c 69 73 "freelis
15eac 74 20 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f 6f t leaf count too
15ead 20 62 69 67 20 6f 6e 20 70 61 67 65 20 25 64 22 big on page %d"
15eae 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 , iPage);.
15eaf 20 20 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 6c N--;. }el
15eb0 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 se{. for(
15eb1 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a i=0; i<n; i++){.
15eb2 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 Pgno i
15eb3 46 72 65 65 50 61 67 65 20 3d 20 67 65 74 34 62 FreePage = get4b
15eb4 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 38 yte(&pOvflData[8
15eb5 2b 69 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20 +i*4]);.#ifndef
15eb6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
15eb7 56 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20 20 VACUUM.
15eb8 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 if( pCheck->pBt
15eb9 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
15eba 20 20 20 20 20 20 20 20 20 20 20 20 63 68 65 63 chec
15ebb 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 kPtrmap(pCheck,
15ebc 69 46 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41 iFreePage, PTRMA
15ebd 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a P_FREEPAGE, 0, z
15ebe 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
15ebf 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
15ec0 20 20 20 20 20 20 20 63 68 65 63 6b 52 65 66 28 checkRef(
15ec1 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 pCheck, iFreePag
15ec2 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 e, zContext);.
15ec3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
15ec4 4e 20 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a N -= n;. }.
15ec5 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
15ec6 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
15ec7 43 55 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a 20 CUUM. else{.
15ec8 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 /* If this
15ec9 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 database support
15eca 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 6e s auto-vacuum an
15ecb 64 20 69 50 61 67 65 20 69 73 20 6e 6f 74 20 74 d iPage is not t
15ecc 68 65 20 6c 61 73 74 0a 20 20 20 20 20 20 2a 2a he last. **
15ecd 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 6f 76 page in this ov
15ece 65 72 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 65 erflow list, che
15ecf 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e ck that the poin
15ed0 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f ter-map entry fo
15ed1 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 r. ** the f
15ed2 6f 6c 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d 61 ollowing page ma
15ed3 74 63 68 65 73 20 69 50 61 67 65 2e 0a 20 20 20 tches iPage..
15ed4 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
15ed5 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 pCheck->pBt->aut
15ed6 6f 56 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29 oVacuum && N>0 )
15ed7 7b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 67 65 {. i = ge
15ed8 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61 t4byte(pOvflData
15ed9 29 3b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b );. check
15eda 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 Ptrmap(pCheck, i
15edb 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f , PTRMAP_OVERFLO
15edc 57 32 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 W2, iPage, zCont
15edd 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ext);. }.
15ede 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 }.#endif. i
15edf 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 Page = get4byte(
15ee0 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 pOvflData);.
15ee1 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
15ee2 66 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 f(pOvflPage);.
15ee3 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.}.#endif /* SQ
15ee4 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
15ee5 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 ITY_CHECK */..#i
15ee6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15ee7 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
15ee8 4b 0a 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f K./*.** Do vario
15ee9 75 73 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 us sanity checks
15eea 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 on a single pag
15eeb 65 20 6f 66 20 61 20 74 72 65 65 2e 20 20 52 65 e of a tree. Re
15eec 74 75 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 turn.** the tree
15eed 20 64 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 61 depth. Root pa
15eee 67 65 73 20 72 65 74 75 72 6e 20 30 2e 20 20 50 ges return 0. P
15eef 61 72 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70 arents of root p
15ef0 61 67 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 31 ages.** return 1
15ef1 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a , and so forth..
15ef2 2a 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 65 ** .** These che
15ef3 63 6b 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a cks are done:.**
15ef4 0a 2a 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 6b .** 1. Mak
15ef5 65 20 73 75 72 65 20 74 68 61 74 20 63 65 6c 6c e sure that cell
15ef6 73 20 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b 73 s and freeblocks
15ef7 20 64 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 0a do not overlap.
15ef8 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75 74 20 ** but
15ef9 63 6f 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 6c combine to compl
15efa 65 74 65 6c 79 20 63 6f 76 65 72 20 74 68 65 20 etely cover the
15efb 70 61 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e page..** NO 2.
15efc 20 20 4d 61 6b 65 20 73 75 72 65 20 63 65 6c 6c Make sure cell
15efd 20 6b 65 79 73 20 61 72 65 20 69 6e 20 6f 72 64 keys are in ord
15efe 65 72 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 er..** NO 3.
15eff 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 Make sure no key
15f00 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 is less than or
15f01 20 65 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 equal to zLower
15f02 42 6f 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 Bound..** NO 4
15f03 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 . Make sure no
15f04 6b 65 79 20 69 73 20 67 72 65 61 74 65 72 20 74 key is greater t
15f05 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
15f06 7a 55 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 zUpperBound..**
15f07 20 20 20 20 20 35 2e 20 20 43 68 65 63 6b 20 74 5. Check t
15f08 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 he integrity of
15f09 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a overflow pages..
15f0a 2a 2a 20 20 20 20 20 20 36 2e 20 20 52 65 63 75 ** 6. Recu
15f0b 72 73 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 65 rsively call che
15f0c 63 6b 54 72 65 65 50 61 67 65 20 6f 6e 20 61 6c ckTreePage on al
15f0d 6c 20 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 l children..**
15f0e 20 20 20 20 37 2e 20 20 56 65 72 69 66 79 20 74 7. Verify t
15f0f 68 61 74 20 74 68 65 20 64 65 70 74 68 20 6f 66 hat the depth of
15f10 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 73 all children is
15f11 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 the same..**
15f12 20 20 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 65 8. Make sure
15f13 20 74 68 69 73 20 70 61 67 65 20 69 73 20 61 74 this page is at
15f14 20 6c 65 61 73 74 20 33 33 25 20 66 75 6c 6c 20 least 33% full
15f15 6f 72 20 65 6c 73 65 20 69 74 20 69 73 0a 2a 2a or else it is.**
15f16 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72 6f the ro
15f17 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a ot of the tree..
15f18 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 */.static int ch
15f19 65 63 6b 54 72 65 65 50 61 67 65 28 0a 20 20 49 eckTreePage(. I
15f1a 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 ntegrityCk *pChe
15f1b 63 6b 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 ck, /* Context
15f1c 66 6f 72 20 74 68 65 20 73 61 6e 69 74 79 20 63 for the sanity c
15f1d 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 50 heck */. int iP
15f1e 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 age,
15f1f 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f /* Page number o
15f20 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 63 68 f the page to ch
15f21 65 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a eck */. char *z
15f22 50 61 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f ParentContext /
15f23 2a 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74 * Parent context
15f24 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 */.){. MemPage
15f25 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 *pPage;. int i
15f26 2c 20 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c , rc, depth, d2,
15f27 20 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e pgno, cnt;. in
15f28 74 20 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 t hdr, cellStart
15f29 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 ;. int nCell;.
15f2a 20 75 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 u8 *data;. BtS
15f2b 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e hared *pBt;. in
15f2c 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 t usableSize;.
15f2d 63 68 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 char zContext[10
15f2e 30 5d 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20 0];. char *hit
15f2f 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f = 0;.. sqlite3_
15f30 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
15f31 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 zContext), zCont
15f32 65 78 74 2c 20 22 50 61 67 65 20 25 64 3a 20 22 ext, "Page %d: "
15f33 2c 20 69 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 , iPage);.. /*
15f34 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 Check that the p
15f35 61 67 65 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a age exists. */.
15f36 20 20 70 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e pBt = pCheck->
15f37 70 42 74 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a pBt;. usableSiz
15f38 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e = pBt->usableS
15f39 69 7a 65 3b 0a 20 20 69 66 28 20 69 50 61 67 65 ize;. if( iPage
15f3a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
15f3b 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 if( checkRef(p
15f3c 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50 Check, iPage, zP
15f3d 61 72 65 6e 74 43 6f 6e 74 65 78 74 29 20 29 20 arentContext) )
15f3e 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
15f3f 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 (rc = sqlite3Btr
15f40 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 eeGetPage(pBt, (
15f41 50 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 50 61 Pgno)iPage, &pPa
15f42 67 65 2c 20 30 29 29 21 3d 30 20 29 7b 0a 20 20 ge, 0))!=0 ){.
15f43 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
15f44 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
15f45 74 2c 0a 20 20 20 20 20 20 20 22 75 6e 61 62 6c t,. "unabl
15f46 65 20 74 6f 20 67 65 74 20 74 68 65 20 70 61 67 e to get the pag
15f47 65 2e 20 65 72 72 6f 72 20 63 6f 64 65 3d 25 64 e. error code=%d
15f48 22 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 74 75 ", rc);. retu
15f49 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 0;. }. if(
15f4a 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 (rc = sqlite3Btr
15f4b 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 eeInitPage(pPage
15f4c 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 63 68 65 ))!=0 ){. che
15f4d 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
15f4e 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 ck, zContext, .
15f4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15f50 20 20 22 73 71 6c 69 74 65 33 42 74 72 65 65 49 "sqlite3BtreeI
15f51 6e 69 74 50 61 67 65 28 29 20 72 65 74 75 72 6e nitPage() return
15f52 73 20 65 72 72 6f 72 20 63 6f 64 65 20 25 64 22 s error code %d"
15f53 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 , rc);. relea
15f54 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
15f55 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
15f56 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 6f 75 74 .. /* Check out
15f57 20 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 2e 0a all the cells..
15f58 20 20 2a 2f 0a 20 20 64 65 70 74 68 20 3d 20 30 */. depth = 0
15f59 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
15f5a 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 Page->nCell && p
15f5b 43 68 65 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b Check->mxErr; i+
15f5c 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c +){. u8 *pCel
15f5d 6c 3b 0a 20 20 20 20 75 33 32 20 73 7a 3b 0a 20 l;. u32 sz;.
15f5e 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f CellInfo info
15f5f 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 ;.. /* Check
15f60 70 61 79 6c 6f 61 64 20 6f 76 65 72 66 6c 6f 77 payload overflow
15f61 20 70 61 67 65 73 0a 20 20 20 20 2a 2f 0a 20 20 pages. */.
15f62 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
15f63 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 tf(sizeof(zConte
15f64 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 xt), zContext,.
15f65 20 20 20 20 20 20 20 20 20 20 20 20 22 4f 6e 20 "On
15f66 74 72 65 65 20 70 61 67 65 20 25 64 20 63 65 6c tree page %d cel
15f67 6c 20 25 64 3a 20 22 2c 20 69 50 61 67 65 2c 20 l %d: ", iPage,
15f68 69 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 i);. pCell =
15f69 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 69 findCell(pPage,i
15f6a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 );. sqlite3Bt
15f6b 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
15f6c 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 pPage, pCell, &i
15f6d 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d 20 69 nfo);. sz = i
15f6e 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 20 69 nfo.nData;. i
15f6f 66 28 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 f( !pPage->intKe
15f70 79 20 29 20 73 7a 20 2b 3d 20 69 6e 66 6f 2e 6e y ) sz += info.n
15f71 4b 65 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 Key;. assert(
15f72 20 73 7a 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f sz==info.nPaylo
15f73 61 64 20 29 3b 0a 20 20 20 20 69 66 28 20 73 7a ad );. if( sz
15f74 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a >info.nLocal ){.
15f75 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 int nPage
15f76 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f = (sz - info.nLo
15f77 63 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 cal + usableSize
15f78 20 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a - 5)/(usableSiz
15f79 65 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67 e - 4);. Pg
15f7a 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 no pgnoOvfl = ge
15f7b 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e t4byte(&pCell[in
15f7c 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a fo.iOverflow]);.
15f7d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
15f7e 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
15f7f 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 if( pBt->au
15f80 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
15f81 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 checkPtrmap(
15f82 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c pCheck, pgnoOvfl
15f83 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f , PTRMAP_OVERFLO
15f84 57 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 W1, iPage, zCont
15f85 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 ext);. }.#e
15f86 6e 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b ndif. check
15f87 4c 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 List(pCheck, 0,
15f88 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c pgnoOvfl, nPage,
15f89 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 zContext);.
15f8a 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 }.. /* Check
15f8b 73 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63 sanity of left c
15f8c 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a hild page.. *
15f8d 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 /. if( !pPage
15f8e 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
15f8f 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 pgno = get4byte(
15f90 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 pCell);.#ifndef
15f91 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
15f92 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 VACUUM. if(
15f93 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
15f94 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
15f95 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 kPtrmap(pCheck,
15f96 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 pgno, PTRMAP_BTR
15f97 45 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 EE, iPage, zCont
15f98 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 ext);. }.#e
15f99 6e 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20 ndif. d2 =
15f9a 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 checkTreePage(pC
15f9b 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e heck, pgno, zCon
15f9c 74 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 text);. if(
15f9d 20 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 i>0 && d2!=dept
15f9e 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 h ){. che
15f9f 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
15fa0 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 ck, zContext, "C
15fa1 68 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 hild page depth
15fa2 64 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 differs");.
15fa3 20 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d }. depth =
15fa4 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 d2;. }. }.
15fa5 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
15fa6 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 f ){. pgno =
15fa7 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
15fa8 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
15fa9 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 rOffset+8]);.
15faa 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
15fab 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 f(sizeof(zContex
15fac 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 t), zContext, .
15fad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15fae 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 "On page %d
15faf 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 at right child:
15fb0 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 ", iPage);.#ifnd
15fb1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
15fb2 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
15fb3 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
15fb4 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b m ){. check
15fb5 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 Ptrmap(pCheck, p
15fb6 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
15fb7 45 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 E, iPage, 0);.
15fb8 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 }.#endif. c
15fb9 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 heckTreePage(pCh
15fba 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 eck, pgno, zCont
15fbb 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a ext);. }. . /*
15fbc 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c Check for compl
15fbd 65 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20 ete coverage of
15fbe 74 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20 the page. */.
15fbf 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
15fc0 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 ata;. hdr = pPa
15fc1 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 ge->hdrOffset;.
15fc2 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61 hit = sqlite3Pa
15fc3 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 geMalloc( pBt->p
15fc4 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 ageSize );. if(
15fc5 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 hit==0 ){. p
15fc6 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 Check->mallocFai
15fc7 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 led = 1;. }else
15fc8 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e {. u16 conten
15fc9 74 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79 tOffset = get2by
15fca 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
15fcb 3b 0a 20 20 20 20 69 66 20 28 63 6f 6e 74 65 6e ;. if (conten
15fcc 74 4f 66 66 73 65 74 20 3e 20 75 73 61 62 6c 65 tOffset > usable
15fcd 53 69 7a 65 29 20 7b 0a 20 20 20 20 20 20 63 68 Size) {. ch
15fce 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
15fcf 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 eck, 0, .
15fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 43 "C
15fd1 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 orruption detect
15fd2 65 64 20 69 6e 20 68 65 61 64 65 72 20 6f 6e 20 ed in header on
15fd3 70 61 67 65 20 25 64 22 2c 69 50 61 67 65 2c 30 page %d",iPage,0
15fd4 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 68 );. goto ch
15fd5 65 63 6b 5f 70 61 67 65 5f 61 62 6f 72 74 3b 0a eck_page_abort;.
15fd6 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 }. memset
15fd7 28 68 69 74 2b 63 6f 6e 74 65 6e 74 4f 66 66 73 (hit+contentOffs
15fd8 65 74 2c 20 30 2c 20 75 73 61 62 6c 65 53 69 7a et, 0, usableSiz
15fd9 65 2d 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 e-contentOffset)
15fda 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74 ;. memset(hit
15fdb 2c 20 31 2c 20 63 6f 6e 74 65 6e 74 4f 66 66 73 , 1, contentOffs
15fdc 65 74 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d et);. nCell =
15fdd 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15fde 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 63 65 6c hdr+3]);. cel
15fdf 6c 53 74 61 72 74 20 3d 20 68 64 72 20 2b 20 31 lStart = hdr + 1
15fe0 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 2 - 4*pPage->lea
15fe1 66 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 f;. for(i=0;
15fe2 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 i<nCell; i++){.
15fe3 20 20 20 20 20 69 6e 74 20 70 63 20 3d 20 67 65 int pc = ge
15fe4 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c t2byte(&data[cel
15fe5 6c 53 74 61 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 lStart+i*2]);.
15fe6 20 20 20 20 75 31 36 20 73 69 7a 65 20 3d 20 31 u16 size = 1
15fe7 30 32 34 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 024;. int j
15fe8 3b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c 3d ;. if( pc<=
15fe9 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 usableSize ){.
15fea 20 20 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c size = cel
15feb 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 lSizePtr(pPage,
15fec 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 &data[pc]);.
15fed 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 70 }. if( (p
15fee 63 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c c+size-1)>=usabl
15fef 65 53 69 7a 65 20 7c 7c 20 70 63 3c 30 20 29 7b eSize || pc<0 ){
15ff0 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 . checkAp
15ff1 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
15ff2 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 0, .
15ff3 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 "Corruption dete
15ff4 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 20 cted in cell %d
15ff5 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 50 on page %d",i,iP
15ff6 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 age,0);. }e
15ff7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 lse{. for
15ff8 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a 3e (j=pc+size-1; j>
15ff9 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d =pc; j--) hit[j]
15ffa 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ++;. }.
15ffb 7d 0a 20 20 20 20 66 6f 72 28 63 6e 74 3d 30 2c }. for(cnt=0,
15ffc 20 69 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 i=get2byte(&dat
15ffd 61 5b 68 64 72 2b 31 5d 29 3b 20 69 3e 30 20 26 a[hdr+1]); i>0 &
15ffe 26 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 20 26 & i<usableSize &
15fff 26 20 63 6e 74 3c 31 30 30 30 30 3b 20 0a 20 20 & cnt<10000; .
16000 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 29 7b cnt++){
16001 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 . int size
16002 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
16003 5b 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 6e [i+2]);. in
16004 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 28 t j;. if( (
16005 69 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c i+size-1)>=usabl
16006 65 53 69 7a 65 20 7c 7c 20 69 3c 30 20 29 7b 0a eSize || i<0 ){.
16007 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 checkApp
16008 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 endMsg(pCheck, 0
16009 2c 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 , .
1600a 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 "Corruption dete
1600b 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 20 cted in cell %d
1600c 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 50 on page %d",i,iP
1600d 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 age,0);. }e
1600e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 lse{. for
1600f 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d (j=i+size-1; j>=
16010 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b i; j--) hit[j]++
16011 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
16012 69 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 i = get2byte(&da
16013 74 61 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 ta[i]);. }.
16014 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 69 for(i=cnt=0; i
16015 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b 2b <usableSize; i++
16016 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69 74 ){. if( hit
16017 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 [i]==0 ){.
16018 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d cnt++;. }
16019 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d 3e else if( hit[i]>
1601a 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 1 ){. che
1601b 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
1601c 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 ck, 0,.
1601d 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73 20 "Multiple uses
1601e 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20 70 for byte %d of p
1601f 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61 67 age %d", i, iPag
16020 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 e);. brea
16021 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
16022 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64 61 . if( cnt!=da
16023 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20 20 ta[hdr+7] ){.
16024 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
16025 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 g(pCheck, 0, .
16026 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65 6e "Fragmen
16027 74 65 64 20 73 70 61 63 65 20 69 73 20 25 64 20 ted space is %d
16028 62 79 74 65 20 72 65 70 6f 72 74 65 64 20 61 73 byte reported as
16029 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c %d on page %d",
1602a 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 . cnt,
1602b 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 data[hdr+7], iPa
1602c 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 63 ge);. }. }.c
1602d 68 65 63 6b 5f 70 61 67 65 5f 61 62 6f 72 74 3a heck_page_abort:
1602e 0a 20 20 69 66 20 28 68 69 74 29 20 73 71 6c 69 . if (hit) sqli
1602f 74 65 33 50 61 67 65 46 72 65 65 28 68 69 74 29 te3PageFree(hit)
16030 3b 0a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 ;.. releasePage
16031 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 (pPage);. retur
16032 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a 23 65 6e n depth+1;.}.#en
16033 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
16034 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 IT_INTEGRITY_CHE
16035 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 CK */..#ifndef S
16036 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
16037 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a RITY_CHECK./*.**
16038 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f This routine do
16039 65 73 20 61 20 63 6f 6d 70 6c 65 74 65 20 63 68 es a complete ch
1603a 65 63 6b 20 6f 66 20 74 68 65 20 67 69 76 65 6e eck of the given
1603b 20 42 54 72 65 65 20 66 69 6c 65 2e 20 20 61 52 BTree file. aR
1603c 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61 6e 20 61 oot[] is.** an a
1603d 72 72 61 79 20 6f 66 20 70 61 67 65 73 20 6e 75 rray of pages nu
1603e 6d 62 65 72 73 20 77 65 72 65 20 65 61 63 68 20 mbers were each
1603f 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 74 page number is t
16040 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a he root page of.
16041 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20 6e 52 6f ** a table. nRo
16042 6f 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ot is the number
16043 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
16044 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 Root..**.** Writ
16045 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
16046 65 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70 error seen in *p
16047 6e 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f nErr. Except fo
16048 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a r some memory.**
16049 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
1604a 72 73 2c 20 20 6e 6e 20 65 72 72 6f 72 20 6d 65 rs, nn error me
1604b 73 73 61 67 65 20 69 73 20 68 65 6c 64 20 69 6e ssage is held in
1604c 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
1604d 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 from.** malloc
1604e 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 2a is returned if *
1604f 70 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 pnErr is non-zer
16050 6f 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30 o. If *pnErr==0
16051 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a then NULL is.**
16052 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 returned..*/.SQ
16053 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
16054 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 49 r *sqlite3BtreeI
16055 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 0a 20 ntegrityCheck(.
16056 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 2f Btree *p, /
16057 2a 20 54 68 65 20 62 74 72 65 65 20 74 6f 20 62 * The btree to b
16058 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 69 e checked */. i
16059 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 20 2f 2a 20 nt *aRoot, /*
1605a 41 6e 20 61 72 72 61 79 20 6f 66 20 72 6f 6f 74 An array of root
1605b 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 66 pages numbers f
1605c 6f 72 20 69 6e 64 69 76 69 64 75 61 6c 20 74 72 or individual tr
1605d 65 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f ees */. int nRo
1605e 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ot, /* Number
1605f 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
16060 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 Root[] */. int
16061 6d 78 45 72 72 2c 20 20 20 20 2f 2a 20 53 74 6f mxErr, /* Sto
16062 70 20 72 65 70 6f 72 74 69 6e 67 20 65 72 72 6f p reporting erro
16063 72 73 20 61 66 74 65 72 20 74 68 69 73 20 6d 61 rs after this ma
16064 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 ny */. int *pnE
16065 72 72 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e rr /* Write n
16066 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 umber of errors
16067 73 65 65 6e 20 74 6f 20 74 68 69 73 20 76 61 72 seen to this var
16068 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 iable */.){. Pg
16069 6e 6f 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65 66 no i;. int nRef
1606a 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 ;. IntegrityCk
1606b 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 68 61 72 sCheck;. BtShar
1606c 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1606d 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 30 ;. char zErr[10
1606e 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 0];.. sqlite3Bt
1606f 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 reeEnter(p);. p
16070 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
16071 20 20 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33 nRef = sqlite3
16072 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 PagerRefcount(pB
16073 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 69 66 t->pPager);. if
16074 28 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52 ( lockBtreeWithR
16075 65 74 72 79 28 70 29 21 3d 53 51 4c 49 54 45 5f etry(p)!=SQLITE_
16076 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 6e 45 72 72 OK ){. *pnErr
16077 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 1;. sqlite
16078 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
16079 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
1607a 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 22 63 e3DbStrDup(0, "c
1607b 61 6e 6e 6f 74 20 61 63 71 75 69 72 65 20 61 20 annot acquire a
1607c 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 read lock on the
1607d 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 7d database");. }
1607e 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d 20 . sCheck.pBt =
1607f 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 50 pBt;. sCheck.pP
16080 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 ager = pBt->pPag
16081 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 61 er;. sCheck.nPa
16082 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f ge = pagerPageco
16083 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 3b unt(sCheck.pBt);
16084 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 20 . sCheck.mxErr
16085 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 63 = mxErr;. sChec
16086 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 43 k.nErr = 0;. sC
16087 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 heck.mallocFaile
16088 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 20 d = 0;. *pnErr
16089 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c = 0;.#ifndef SQL
1608a 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
1608b 55 55 4d 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e UUM. if( pBt->n
1608c 54 72 75 6e 63 21 3d 30 20 29 7b 0a 20 20 20 20 Trunc!=0 ){.
1608d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 3d 20 70 sCheck.nPage = p
1608e 42 74 2d 3e 6e 54 72 75 6e 63 3b 0a 20 20 7d 0a Bt->nTrunc;. }.
1608f 23 65 6e 64 69 66 0a 20 20 69 66 28 20 73 43 68 #endif. if( sCh
16090 65 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a eck.nPage==0 ){.
16091 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 unlockBtreeI
16092 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 fUnused(pBt);.
16093 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
16094 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 ave(p);. retu
16095 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65 rn 0;. }. sChe
16096 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74 ck.anRef = sqlit
16097 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63 e3Malloc( (sChec
16098 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f k.nPage+1)*sizeo
16099 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 f(sCheck.anRef[0
1609a 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68 ]) );. if( !sCh
1609b 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20 eck.anRef ){.
1609c 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
1609d 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 2a used(pBt);. *
1609e 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 73 pnErr = 1;. s
1609f 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
160a0 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 (p);. return
160a1 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 0;. }. for(i=0
160a2 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 ; i<=sCheck.nPag
160a3 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63 6b 2e e; i++){ sCheck.
160a4 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20 7d 0a anRef[i] = 0; }.
160a5 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 i = PENDING_BY
160a6 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a 20 20 TE_PAGE(pBt);.
160a7 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 if( i<=sCheck.nP
160a8 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68 65 63 age ){. sChec
160a9 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31 3b 0a k.anRef[i] = 1;.
160aa 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72 }. sqlite3Str
160ab 41 63 63 75 6d 49 6e 69 74 28 26 73 43 68 65 63 AccumInit(&sChec
160ac 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72 2c 20 k.errMsg, zErr,
160ad 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 32 30 sizeof(zErr), 20
160ae 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 000);.. /* Chec
160af 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 k the integrity
160b0 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 0a of the freelist.
160b1 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69 73 74 */. checkList
160b2 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 67 65 74 (&sCheck, 1, get
160b3 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 4byte(&pBt->pPag
160b4 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 2c 0a e1->aData[32]),.
160b5 20 20 20 20 20 20 20 20 20 20 20 20 67 65 74 34 get4
160b6 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 byte(&pBt->pPage
160b7 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c 20 22 1->aData[36]), "
160b8 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 3a 20 22 Main freelist: "
160b9 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 61 );.. /* Check a
160ba 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 2e 0a 20 ll the tables..
160bb 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 */. for(i=0; (
160bc 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26 20 73 int)i<nRoot && s
160bd 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b Check.mxErr; i++
160be 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f 6f 74 ){. if( aRoot
160bf 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 [i]==0 ) continu
160c0 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 e;.#ifndef SQLIT
160c1 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
160c2 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 M. if( pBt->a
160c3 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61 52 6f utoVacuum && aRo
160c4 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 ot[i]>1 ){.
160c5 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26 73 43 checkPtrmap(&sC
160c6 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 heck, aRoot[i],
160c7 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c PTRMAP_ROOTPAGE,
160c8 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 0, 0);. }.#e
160c9 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54 72 ndif. checkTr
160ca 65 65 50 61 67 65 28 26 73 43 68 65 63 6b 2c 20 eePage(&sCheck,
160cb 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73 74 20 aRoot[i], "List
160cc 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 3a 20 22 of tree roots: "
160cd 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b );. }.. /* Mak
160ce 65 20 73 75 72 65 20 65 76 65 72 79 20 70 61 67 e sure every pag
160cf 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73 e in the file is
160d0 20 72 65 66 65 72 65 6e 63 65 64 0a 20 20 2a 2f referenced. */
160d1 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 . for(i=1; i<=s
160d2 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 26 20 73 Check.nPage && s
160d3 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b Check.mxErr; i++
160d4 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ){.#ifdef SQLITE
160d5 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
160d6 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e . if( sCheck.
160d7 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b 0a 20 anRef[i]==0 ){.
160d8 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 checkAppend
160d9 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 Msg(&sCheck, 0,
160da 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 "Page %d is neve
160db 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 r used", i);.
160dc 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 }.#else. /*
160dd 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
160de 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 supports auto-va
160df 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72 65 20 cuum, make sure
160e0 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74 61 69 no tables contai
160e1 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e n. ** referen
160e2 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 72 2d 6d ces to pointer-m
160e3 61 70 20 70 61 67 65 73 2e 0a 20 20 20 20 2a 2f ap pages.. */
160e4 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e . if( sCheck.
160e5 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26 20 0a anRef[i]==0 && .
160e6 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 (PTRMAP_P
160e7 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 21 3d 69 AGENO(pBt, i)!=i
160e8 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 || !pBt->autoVa
160e9 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 cuum) ){. c
160ea 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 heckAppendMsg(&s
160eb 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 67 65 20 Check, 0, "Page
160ec 25 64 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 %d is never used
160ed 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ", i);. }.
160ee 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e 52 65 if( sCheck.anRe
160ef 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20 20 20 f[i]!=0 && .
160f0 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e (PTRMAP_PAGEN
160f1 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 26 26 20 O(pBt, i)==i &&
160f2 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
160f3 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 ){. checkA
160f4 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b ppendMsg(&sCheck
160f5 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 20 6d 61 , 0, "Pointer ma
160f6 70 20 70 61 67 65 20 25 64 20 69 73 20 72 65 66 p page %d is ref
160f7 65 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a 20 20 erenced", i);.
160f8 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a }.#endif. }..
160f9 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 /* Make sure t
160fa 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64 69 64 his analysis did
160fb 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79 20 75 not leave any u
160fc 6e 72 65 66 28 29 20 70 61 67 65 73 0a 20 20 2a nref() pages. *
160fd 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 /. unlockBtreeI
160fe 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 fUnused(pBt);.
160ff 69 66 28 20 6e 52 65 66 20 21 3d 20 73 71 6c 69 if( nRef != sqli
16100 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
16101 28 70 42 74 2d 3e 70 50 61 67 65 72 29 20 29 7b (pBt->pPager) ){
16102 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 . checkAppend
16103 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 Msg(&sCheck, 0,
16104 0a 20 20 20 20 20 20 22 4f 75 74 73 74 61 6e 64 . "Outstand
16105 69 6e 67 20 70 61 67 65 20 63 6f 75 6e 74 20 67 ing page count g
16106 6f 65 73 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 oes from %d to %
16107 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 61 6e d during this an
16108 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 20 6e alysis",. n
16109 52 65 66 2c 20 73 71 6c 69 74 65 33 50 61 67 65 Ref, sqlite3Page
1610a 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 rRefcount(pBt->p
1610b 50 61 67 65 72 29 0a 20 20 20 20 29 3b 0a 20 20 Pager). );.
1610c 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 20 75 }.. /* Clean u
1610d 70 20 61 6e 64 20 72 65 70 6f 72 74 20 65 72 72 p and report err
1610e 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 ors.. */. sqli
1610f 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
16110 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
16111 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 29 3b 0a (sCheck.anRef);.
16112 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6d 61 6c if( sCheck.mal
16113 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
16114 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
16115 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 Reset(&sCheck.er
16116 72 4d 73 67 29 3b 0a 20 20 20 20 2a 70 6e 45 72 rMsg);. *pnEr
16117 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 2b r = sCheck.nErr+
16118 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 1;. return 0;
16119 0a 20 20 7d 0a 20 20 2a 70 6e 45 72 72 20 3d 20 . }. *pnErr =
1611a 73 43 68 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 69 sCheck.nErr;. i
1611b 66 28 20 73 43 68 65 63 6b 2e 6e 45 72 72 3d 3d f( sCheck.nErr==
1611c 30 20 29 20 73 71 6c 69 74 65 33 53 74 72 41 63 0 ) sqlite3StrAc
1611d 63 75 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b cumReset(&sCheck
1611e 2e 65 72 72 4d 73 67 29 3b 0a 20 20 72 65 74 75 .errMsg);. retu
1611f 72 6e 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 rn sqlite3StrAcc
16120 75 6d 46 69 6e 69 73 68 28 26 73 43 68 65 63 6b umFinish(&sCheck
16121 2e 65 72 72 4d 73 67 29 3b 0a 7d 0a 23 65 6e 64 .errMsg);.}.#end
16122 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
16123 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
16124 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 K */../*.** Retu
16125 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 rn the full path
16126 6e 61 6d 65 20 6f 66 20 74 68 65 20 75 6e 64 65 name of the unde
16127 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 rlying database
16128 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 file..**.** The
16129 70 61 67 65 72 20 66 69 6c 65 6e 61 6d 65 20 69 pager filename i
1612a 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c s invariant as l
1612b 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67 65 72 ong as the pager
1612c 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 is.** open so i
1612d 74 20 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 t is safe to acc
1612e 65 73 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 ess without the
1612f 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a BtShared mutex..
16130 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
16131 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 E const char *sq
16132 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c lite3BtreeGetFil
16133 65 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b ename(Btree *p){
16134 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 . assert( p->pB
16135 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a t->pPager!=0 );.
16136 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
16137 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d PagerFilename(p-
16138 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d >pBt->pPager);.}
16139 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1613a 68 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 he pathname of t
1613b 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 68 61 he directory tha
1613c 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 t contains the d
1613d 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
1613e 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 64 69 .** The pager di
1613f 72 65 63 74 6f 72 79 20 6e 61 6d 65 20 69 73 20 rectory name is
16140 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e invariant as lon
16141 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69 g as the pager i
16142 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 s.** open so it
16143 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 is safe to acces
16144 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74 s without the Bt
16145 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f Shared mutex..*/
16146 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16147 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
16148 74 65 33 42 74 72 65 65 47 65 74 44 69 72 6e 61 te3BtreeGetDirna
16149 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 me(Btree *p){.
1614a 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e assert( p->pBt->
1614b 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 pPager!=0 );. r
1614c 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 eturn sqlite3Pag
1614d 65 72 44 69 72 6e 61 6d 65 28 70 2d 3e 70 42 74 erDirname(p->pBt
1614e 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a ->pPager);.}../*
1614f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 .** Return the p
16150 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a athname of the j
16151 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 ournal file for
16152 74 68 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 this database. T
16153 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c he return.** val
16154 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 ue of this routi
16155 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 ne is the same r
16156 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 egardless of whe
16157 74 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c ther the journal
16158 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 file.** has bee
16159 6e 20 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 n created or not
1615a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 ..**.** The page
1615b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 r journal filena
1615c 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 me is invariant
1615d 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 as long as the p
1615e 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 ager is.** open
1615f 73 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f so it is safe to
16160 20 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 access without
16161 74 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 the BtShared mut
16162 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ex..*/.SQLITE_PR
16163 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
16164 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 *sqlite3BtreeGe
16165 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 tJournalname(Btr
16166 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ee *p){. assert
16167 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 ( p->pBt->pPager
16168 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 !=0 );. return
16169 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 sqlite3PagerJour
1616a 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e nalname(p->pBt->
1616b 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 23 69 66 6e pPager);.}..#ifn
1616c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1616d 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 VACUUM./*.** Cop
1616e 79 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 y the complete c
1616f 6f 6e 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f ontent of pBtFro
16170 6d 20 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41 m into pBtTo. A
16171 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 transaction.**
16172 6d 75 73 74 20 62 65 20 61 63 74 69 76 65 20 66 must be active f
16173 6f 72 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a or both files..*
16174 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 *.** The size of
16175 20 66 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65 file pTo may be
16176 20 72 65 64 75 63 65 64 20 62 79 20 74 68 69 73 reduced by this
16177 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 20 49 operation..** I
16178 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 f anything goes
16179 77 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 wrong, the trans
1617a 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 action on pTo is
1617b 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 0a 2a rolled back. .*
1617c 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 *.** If successf
1617d 75 6c 2c 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f ul, CommitPhaseO
1617e 6e 65 28 29 20 6d 61 79 20 62 65 20 63 61 6c 6c ne() may be call
1617f 65 64 20 6f 6e 20 70 54 6f 20 62 65 66 6f 72 65 ed on pTo before
16180 20 72 65 74 75 72 6e 69 6e 67 2e 20 0a 2a 2a 20 returning. .**
16181 54 68 65 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c The caller shoul
16182 64 20 66 69 6e 69 73 68 20 63 6f 6d 6d 69 74 74 d finish committ
16183 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63 74 ing the transact
16184 69 6f 6e 20 6f 6e 20 70 54 6f 20 62 79 20 63 61 ion on pTo by ca
16185 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 lling.** sqlite3
16186 42 74 72 65 65 43 6f 6d 6d 69 74 28 29 2e 0a 2a BtreeCommit()..*
16187 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 /.static int btr
16188 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 eeCopyFile(Btree
16189 20 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46 *pTo, Btree *pF
1618a 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d rom){. int rc =
1618b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 67 SQLITE_OK;. Pg
1618c 6e 6f 20 69 3b 0a 0a 20 20 50 67 6e 6f 20 6e 46 no i;.. Pgno nF
1618d 72 6f 6d 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 romPage; /*
1618e 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
1618f 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 50 67 in pFrom */. Pg
16190 6e 6f 20 6e 54 6f 50 61 67 65 3b 20 20 20 20 20 no nToPage;
16191 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
16192 61 67 65 73 20 69 6e 20 70 54 6f 20 2a 2f 0a 20 ages in pTo */.
16193 20 50 67 6e 6f 20 6e 4e 65 77 50 61 67 65 3b 20 Pgno nNewPage;
16194 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
16195 66 20 70 61 67 65 73 20 69 6e 20 70 54 6f 20 61 f pages in pTo a
16196 66 74 65 72 20 74 68 65 20 63 6f 70 79 20 2a 2f fter the copy */
16197 0a 0a 20 20 50 67 6e 6f 20 69 53 6b 69 70 3b 20 .. Pgno iSkip;
16198 20 20 20 20 20 20 20 20 2f 2a 20 50 65 6e 64 69 /* Pendi
16199 6e 67 20 62 79 74 65 20 70 61 67 65 20 69 6e 20 ng byte page in
1619a 70 54 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f pTo */. int nTo
1619b 50 61 67 65 53 69 7a 65 3b 20 20 20 20 2f 2a 20 PageSize; /*
1619c 50 61 67 65 20 73 69 7a 65 20 6f 66 20 70 54 6f Page size of pTo
1619d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 in bytes */. i
1619e 6e 74 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 nt nFromPageSize
1619f 3b 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 ; /* Page size
161a0 6f 66 20 70 46 72 6f 6d 20 69 6e 20 62 79 74 65 of pFrom in byte
161a1 73 20 2a 2f 0a 0a 20 20 42 74 53 68 61 72 65 64 s */.. BtShared
161a2 20 2a 70 42 74 54 6f 20 3d 20 70 54 6f 2d 3e 70 *pBtTo = pTo->p
161a3 42 74 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a Bt;. BtShared *
161a4 70 42 74 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d pBtFrom = pFrom-
161a5 3e 70 42 74 3b 0a 20 20 70 42 74 54 6f 2d 3e 64 >pBt;. pBtTo->d
161a6 62 20 3d 20 70 54 6f 2d 3e 64 62 3b 0a 20 20 70 b = pTo->db;. p
161a7 42 74 46 72 6f 6d 2d 3e 64 62 20 3d 20 70 46 72 BtFrom->db = pFr
161a8 6f 6d 2d 3e 64 62 3b 0a 0a 20 20 6e 54 6f 50 61 om->db;.. nToPa
161a9 67 65 53 69 7a 65 20 3d 20 70 42 74 54 6f 2d 3e geSize = pBtTo->
161aa 70 61 67 65 53 69 7a 65 3b 0a 20 20 6e 46 72 6f pageSize;. nFro
161ab 6d 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74 46 mPageSize = pBtF
161ac 72 6f 6d 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 0a rom->pageSize;..
161ad 20 20 69 66 28 20 70 54 6f 2d 3e 69 6e 54 72 61 if( pTo->inTra
161ae 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns!=TRANS_WRITE
161af 7c 7c 20 70 46 72 6f 6d 2d 3e 69 6e 54 72 61 6e || pFrom->inTran
161b0 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s!=TRANS_WRITE )
161b1 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
161b2 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 ITE_ERROR;. }.
161b3 20 69 66 28 20 70 42 74 54 6f 2d 3e 70 43 75 72 if( pBtTo->pCur
161b4 73 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 sor ){. retur
161b5 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 n SQLITE_BUSY;.
161b6 20 7d 0a 0a 20 20 6e 54 6f 50 61 67 65 20 3d 20 }.. nToPage =
161b7 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
161b8 42 74 54 6f 29 3b 0a 20 20 6e 46 72 6f 6d 50 61 BtTo);. nFromPa
161b9 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f ge = pagerPageco
161ba 75 6e 74 28 70 42 74 46 72 6f 6d 29 3b 0a 20 20 unt(pBtFrom);.
161bb 69 53 6b 69 70 20 3d 20 50 45 4e 44 49 4e 47 5f iSkip = PENDING_
161bc 42 59 54 45 5f 50 41 47 45 28 70 42 74 54 6f 29 BYTE_PAGE(pBtTo)
161bd 3b 0a 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 ;.. /* Variable
161be 20 6e 4e 65 77 50 61 67 65 20 69 73 20 74 68 65 nNewPage is the
161bf 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
161c0 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
161c1 72 65 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6e 74 re the. ** cont
161c2 65 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20 75 73 ents of pFrom us
161c3 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 ing the current
161c4 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 70 54 6f page-size of pTo
161c5 2e 0a 20 20 2a 2f 0a 20 20 6e 4e 65 77 50 61 67 .. */. nNewPag
161c6 65 20 3d 20 28 28 69 36 34 29 6e 46 72 6f 6d 50 e = ((i64)nFromP
161c7 61 67 65 20 2a 20 28 69 36 34 29 6e 46 72 6f 6d age * (i64)nFrom
161c8 50 61 67 65 53 69 7a 65 20 2b 20 28 69 36 34 29 PageSize + (i64)
161c9 6e 54 6f 50 61 67 65 53 69 7a 65 20 2d 20 31 29 nToPageSize - 1)
161ca 20 2f 20 0a 20 20 20 20 20 20 28 69 36 34 29 6e / . (i64)n
161cb 54 6f 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 66 ToPageSize;.. f
161cc 6f 72 28 69 3d 31 3b 20 72 63 3d 3d 53 51 4c 49 or(i=1; rc==SQLI
161cd 54 45 5f 4f 4b 20 26 26 20 28 69 3c 3d 6e 54 6f TE_OK && (i<=nTo
161ce 50 61 67 65 20 7c 7c 20 69 3c 3d 6e 4e 65 77 50 Page || i<=nNewP
161cf 61 67 65 29 3b 20 69 2b 2b 29 7b 0a 0a 20 20 20 age); i++){..
161d0 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 74 68 65 20 /* Journal the
161d1 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 2e 0a 20 original page..
161d2 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 69 53 6b **. ** iSk
161d3 69 70 20 69 73 20 74 68 65 20 70 61 67 65 20 6e ip is the page n
161d4 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c 6f 63 umber of the loc
161d5 6b 69 6e 67 20 70 61 67 65 20 28 50 45 4e 44 49 king page (PENDI
161d6 4e 47 5f 42 59 54 45 5f 50 41 47 45 29 0a 20 20 NG_BYTE_PAGE).
161d7 20 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 ** in database
161d8 20 2a 70 54 6f 20 28 62 65 66 6f 72 65 20 74 68 *pTo (before th
161d9 65 20 63 6f 70 79 29 2e 20 54 68 69 73 20 70 61 e copy). This pa
161da 67 65 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 ge is never writ
161db 74 65 6e 20 0a 20 20 20 20 2a 2a 20 69 6e 74 6f ten . ** into
161dc 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
161dd 65 2e 20 55 6e 6c 65 73 73 20 69 3d 3d 69 53 6b e. Unless i==iSk
161de 69 70 20 6f 72 20 74 68 65 20 70 61 67 65 20 77 ip or the page w
161df 61 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 70 72 as not. ** pr
161e0 65 73 65 6e 74 20 69 6e 20 70 54 6f 20 62 65 66 esent in pTo bef
161e1 6f 72 65 20 74 68 65 20 63 6f 70 79 20 6f 70 65 ore the copy ope
161e2 72 61 74 69 6f 6e 2c 20 6a 6f 75 72 6e 61 6c 20 ration, journal
161e3 70 61 67 65 20 69 20 66 72 6f 6d 20 70 54 6f 2e page i from pTo.
161e4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
161e5 69 21 3d 69 53 6b 69 70 20 26 26 20 69 3c 3d 6e i!=iSkip && i<=n
161e6 54 6f 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 ToPage ){.
161e7 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 DbPage *pDbPage
161e8 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
161e9 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
161ea 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20 69 pBtTo->pPager, i
161eb 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 , &pDbPage);.
161ec 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
161ed 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
161ee 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
161ef 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b rWrite(pDbPage);
161f0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
161f1 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3e =SQLITE_OK && i>
161f2 6e 46 72 6f 6d 50 61 67 65 20 29 7b 0a 20 20 20 nFromPage ){.
161f3 20 20 20 20 20 20 20 2f 2a 20 59 65 61 68 2e 20 /* Yeah.
161f4 20 49 74 20 73 65 65 6d 73 20 77 69 65 72 64 20 It seems wierd
161f5 74 6f 20 63 61 6c 6c 20 44 6f 6e 74 57 72 69 74 to call DontWrit
161f6 65 28 29 20 72 69 67 68 74 20 61 66 74 65 72 20 e() right after
161f7 57 72 69 74 65 28 29 2e 20 42 75 74 0a 20 20 20 Write(). But.
161f8 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 69 ** that i
161f9 73 20 62 65 63 61 75 73 65 20 74 68 65 20 6e 61 s because the na
161fa 6d 65 73 20 6f 66 20 74 68 6f 73 65 20 70 72 6f mes of those pro
161fb 63 65 64 75 72 65 73 20 64 6f 20 6e 6f 74 20 65 cedures do not e
161fc 78 61 63 74 6c 79 20 0a 20 20 20 20 20 20 20 20 xactly .
161fd 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 20 77 ** represent w
161fe 68 61 74 20 74 68 65 79 20 64 6f 2e 20 20 57 72 hat they do. Wr
161ff 69 74 65 28 29 20 72 65 61 6c 6c 79 20 6d 65 61 ite() really mea
16200 6e 73 20 22 70 75 74 20 74 68 69 73 20 70 61 67 ns "put this pag
16201 65 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 e in the.
16202 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a ** rollback j
16203 6f 75 72 6e 61 6c 20 61 6e 64 20 6d 61 72 6b 20 ournal and mark
16204 69 74 20 61 73 20 64 69 72 74 79 20 73 6f 20 74 it as dirty so t
16205 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 77 hat it will be w
16206 72 69 74 74 65 6e 0a 20 20 20 20 20 20 20 20 20 ritten.
16207 20 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 ** to the datab
16208 61 73 65 20 66 69 6c 65 20 6c 61 74 65 72 2e 22 ase file later."
16209 20 20 44 6f 6e 74 57 72 69 74 65 28 29 20 75 6e DontWrite() un
1620a 64 6f 65 73 20 74 68 65 20 73 65 63 6f 6e 64 20 does the second
1620b 70 61 72 74 20 6f 66 0a 20 20 20 20 20 20 20 20 part of.
1620c 20 20 2a 2a 20 74 68 61 74 20 61 6e 64 20 70 72 ** that and pr
1620d 65 76 65 6e 74 73 20 74 68 65 20 70 61 67 65 20 events the page
1620e 66 72 6f 6d 20 62 65 69 6e 67 20 77 72 69 74 74 from being writt
1620f 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 en to the databa
16210 73 65 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20 se. The.
16211 20 20 2a 2a 20 70 61 67 65 20 69 73 20 73 74 69 ** page is sti
16212 6c 6c 20 6f 6e 20 74 68 65 20 72 6f 6c 6c 62 61 ll on the rollba
16213 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 6f 75 ck journal, thou
16214 67 68 2e 20 20 41 6e 64 20 74 68 61 74 20 69 73 gh. And that is
16215 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 the .
16216 2a 2a 20 77 68 6f 6c 65 20 70 6f 69 6e 74 20 6f ** whole point o
16217 66 20 74 68 69 73 20 62 6c 6f 63 6b 3a 20 74 6f f this block: to
16218 20 70 75 74 20 70 61 67 65 73 20 6f 6e 20 74 68 put pages on th
16219 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
1621a 61 6c 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a al. . *
1621b 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d /. rc =
1621c 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e sqlite3PagerDon
1621d 74 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b tWrite(pDbPage);
1621e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1621f 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
16220 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 nref(pDbPage);.
16221 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
16222 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74 /* Overwrite t
16223 68 65 20 64 61 74 61 20 69 6e 20 70 61 67 65 20 he data in page
16224 69 20 6f 66 20 74 68 65 20 74 61 72 67 65 74 20 i of the target
16225 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 database */.
16226 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16227 4b 20 26 26 20 69 21 3d 69 53 6b 69 70 20 26 26 K && i!=iSkip &&
16228 20 69 3c 3d 6e 4e 65 77 50 61 67 65 20 29 7b 0a i<=nNewPage ){.
16229 0a 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 . DbPage *p
1622a 54 6f 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 ToPage = 0;.
1622b 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
1622c 69 4f 66 66 3b 0a 0a 20 20 20 20 20 20 72 63 20 iOff;.. rc
1622d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
1622e 74 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c t(pBtTo->pPager,
1622f 20 69 2c 20 26 70 54 6f 50 61 67 65 29 3b 0a 20 i, &pToPage);.
16230 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
16231 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
16232 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
16233 67 65 72 57 72 69 74 65 28 70 54 6f 50 61 67 65 gerWrite(pToPage
16234 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
16235 20 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 69 for(. i
16236 4f 66 66 3d 28 69 2d 31 29 2a 6e 54 6f 50 61 67 Off=(i-1)*nToPag
16237 65 53 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 eSize; .
16238 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
16239 20 69 4f 66 66 3c 69 2a 6e 54 6f 50 61 67 65 53 iOff<i*nToPageS
1623a 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 69 4f ize; . iO
1623b 66 66 20 2b 3d 20 6e 46 72 6f 6d 50 61 67 65 53 ff += nFromPageS
1623c 69 7a 65 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 ize. ){.
1623d 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 46 72 DbPage *pFr
1623e 6f 6d 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 omPage = 0;.
1623f 20 20 20 20 50 67 6e 6f 20 69 46 72 6f 6d 20 3d Pgno iFrom =
16240 20 28 69 4f 66 66 2f 6e 46 72 6f 6d 50 61 67 65 (iOff/nFromPage
16241 53 69 7a 65 29 2b 31 3b 0a 0a 20 20 20 20 20 20 Size)+1;..
16242 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 50 45 4e if( iFrom==PEN
16243 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
16244 42 74 46 72 6f 6d 29 20 29 7b 0a 20 20 20 20 20 BtFrom) ){.
16245 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
16246 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
16247 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
16248 67 65 72 47 65 74 28 70 42 74 46 72 6f 6d 2d 3e gerGet(pBtFrom->
16249 70 50 61 67 65 72 2c 20 69 46 72 6f 6d 2c 20 26 pPager, iFrom, &
1624a 70 46 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20 pFromPage);.
1624b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1624c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1624d 20 20 20 63 68 61 72 20 2a 7a 54 6f 20 3d 20 73 char *zTo = s
1624e 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
1624f 74 61 28 70 54 6f 50 61 67 65 29 3b 0a 20 20 20 ta(pToPage);.
16250 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 46 72 char *zFr
16251 6f 6d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 om = sqlite3Page
16252 72 47 65 74 44 61 74 61 28 70 46 72 6f 6d 50 61 rGetData(pFromPa
16253 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
16254 6e 74 20 6e 43 6f 70 79 3b 0a 0a 20 20 20 20 20 nt nCopy;..
16255 20 20 20 20 20 69 66 28 20 6e 46 72 6f 6d 50 61 if( nFromPa
16256 67 65 53 69 7a 65 3e 3d 6e 54 6f 50 61 67 65 53 geSize>=nToPageS
16257 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ize ){.
16258 20 20 20 7a 46 72 6f 6d 20 2b 3d 20 28 28 69 2d zFrom += ((i-
16259 31 29 2a 6e 54 6f 50 61 67 65 53 69 7a 65 20 2d 1)*nToPageSize -
1625a 20 28 28 69 46 72 6f 6d 2d 31 29 2a 6e 46 72 6f ((iFrom-1)*nFro
1625b 6d 50 61 67 65 53 69 7a 65 29 29 3b 0a 20 20 20 mPageSize));.
1625c 20 20 20 20 20 20 20 20 20 6e 43 6f 70 79 20 3d nCopy =
1625d 20 6e 54 6f 50 61 67 65 53 69 7a 65 3b 0a 20 20 nToPageSize;.
1625e 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1625f 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 20 2b zTo +
16260 3d 20 28 28 28 69 46 72 6f 6d 2d 31 29 2a 6e 46 = (((iFrom-1)*nF
16261 72 6f 6d 50 61 67 65 53 69 7a 65 29 20 2d 20 28 romPageSize) - (
16262 69 2d 31 29 2a 6e 54 6f 50 61 67 65 53 69 7a 65 i-1)*nToPageSize
16263 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e );. n
16264 43 6f 70 79 20 3d 20 6e 46 72 6f 6d 50 61 67 65 Copy = nFromPage
16265 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 Size;.
16266 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d }.. mem
16267 63 70 79 28 7a 54 6f 2c 20 7a 46 72 6f 6d 2c 20 cpy(zTo, zFrom,
16268 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 20 20 nCopy);.
16269 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
1626a 72 65 66 28 70 46 72 6f 6d 50 61 67 65 29 3b 0a ref(pFromPage);.
1626b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1626c 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f }.. if( pTo
1626d 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Page ){.
1626e 4d 65 6d 50 61 67 65 20 2a 70 20 3d 20 28 4d 65 MemPage *p = (Me
1626f 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50 mPage *)sqlite3P
16270 61 67 65 72 47 65 74 45 78 74 72 61 28 70 54 6f agerGetExtra(pTo
16271 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70 Page);. p
16272 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 ->isInit = 0;.
16273 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
16274 65 72 55 6e 72 65 66 28 70 54 6f 50 61 67 65 29 erUnref(pToPage)
16275 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
16276 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 }.. /* If thi
16277 6e 67 73 20 68 61 76 65 20 77 6f 72 6b 65 64 20 ngs have worked
16278 73 6f 20 66 61 72 2c 20 74 68 65 20 64 61 74 61 so far, the data
16279 62 61 73 65 20 66 69 6c 65 20 6d 61 79 20 6e 65 base file may ne
1627a 65 64 20 74 6f 20 62 65 20 0a 20 20 2a 2a 20 74 ed to be . ** t
1627b 72 75 6e 63 61 74 65 64 2e 20 54 68 65 20 63 6f runcated. The co
1627c 6d 70 6c 65 78 20 70 61 72 74 20 69 73 20 74 68 mplex part is th
1627d 61 74 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74 at it may need t
1627e 6f 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 o be truncated t
1627f 6f 0a 20 20 2a 2a 20 61 20 73 69 7a 65 20 74 68 o. ** a size th
16280 61 74 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 at is not an int
16281 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 eger multiple of
16282 20 6e 54 6f 50 61 67 65 53 69 7a 65 20 2d 20 74 nToPageSize - t
16283 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 he current. **
16284 70 61 67 65 20 73 69 7a 65 20 75 73 65 64 20 62 page size used b
16285 79 20 74 68 65 20 70 61 67 65 72 20 61 73 73 6f y the pager asso
16286 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 ciated with B-Tr
16287 65 65 20 70 54 6f 2e 0a 20 20 2a 2a 0a 20 20 2a ee pTo.. **. *
16288 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 * For example, s
16289 61 79 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 ay the page-size
1628a 20 6f 66 20 70 54 6f 20 69 73 20 32 30 34 38 20 of pTo is 2048
1628b 62 79 74 65 73 20 61 6e 64 20 74 68 65 20 6f 72 bytes and the or
1628c 69 67 69 6e 61 6c 20 0a 20 20 2a 2a 20 6e 75 6d iginal . ** num
1628d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 73 20 ber of pages is
1628e 35 20 28 31 30 20 4b 42 20 66 69 6c 65 29 2e 20 5 (10 KB file).
1628f 49 66 20 70 46 72 6f 6d 20 68 61 73 20 61 20 70 If pFrom has a p
16290 61 67 65 20 73 69 7a 65 20 6f 66 20 31 30 32 34 age size of 1024
16291 20 0a 20 20 2a 2a 20 62 79 74 65 73 20 61 6e 64 . ** bytes and
16292 20 39 20 70 61 67 65 73 2c 20 74 68 65 6e 20 74 9 pages, then t
16293 68 65 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f he file needs to
16294 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f be truncated to
16295 20 39 4b 42 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 9KB.. */. if(
16296 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
16297 7b 0a 20 20 20 20 69 66 28 20 6e 46 72 6f 6d 50 {. if( nFromP
16298 61 67 65 53 69 7a 65 21 3d 6e 54 6f 50 61 67 65 ageSize!=nToPage
16299 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 73 71 Size ){. sq
1629a 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c lite3_file *pFil
1629b 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 e = sqlite3Pager
1629c 46 69 6c 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 File(pBtTo->pPag
1629d 65 72 29 3b 0a 20 20 20 20 20 20 69 36 34 20 69 er);. i64 i
1629e 53 69 7a 65 20 3d 20 28 69 36 34 29 6e 46 72 6f Size = (i64)nFro
1629f 6d 50 61 67 65 53 69 7a 65 20 2a 20 28 69 36 34 mPageSize * (i64
162a0 29 6e 46 72 6f 6d 50 61 67 65 3b 0a 20 20 20 20 )nFromPage;.
162a1 20 20 69 36 34 20 69 4e 6f 77 20 3d 20 28 69 36 i64 iNow = (i6
162a2 34 29 28 28 6e 54 6f 50 61 67 65 3e 6e 4e 65 77 4)((nToPage>nNew
162a3 50 61 67 65 29 3f 6e 54 6f 50 61 67 65 3a 6e 4e Page)?nToPage:nN
162a4 65 77 50 61 67 65 29 20 2a 20 28 69 36 34 29 6e ewPage) * (i64)n
162a5 54 6f 50 61 67 65 53 69 7a 65 3b 20 0a 20 20 20 ToPageSize; .
162a6 20 20 20 69 36 34 20 69 50 65 6e 64 69 6e 67 20 i64 iPending
162a7 3d 20 28 28 69 36 34 29 50 45 4e 44 49 4e 47 5f = ((i64)PENDING_
162a8 42 59 54 45 5f 50 41 47 45 28 70 42 74 54 6f 29 BYTE_PAGE(pBtTo)
162a9 2d 31 29 20 2a 28 69 36 34 29 6e 54 6f 50 61 67 -1) *(i64)nToPag
162aa 65 53 69 7a 65 3b 0a 20 20 0a 20 20 20 20 20 20 eSize;. .
162ab 61 73 73 65 72 74 28 20 69 53 69 7a 65 3c 3d 69 assert( iSize<=i
162ac 4e 6f 77 20 29 3b 0a 20 20 0a 20 20 20 20 20 20 Now );. .
162ad 2f 2a 20 43 6f 6d 6d 69 74 20 70 68 61 73 65 20 /* Commit phase
162ae 6f 6e 65 20 73 79 6e 63 73 20 74 68 65 20 6a 6f one syncs the jo
162af 75 72 6e 61 6c 20 66 69 6c 65 20 61 73 73 6f 63 urnal file assoc
162b0 69 61 74 65 64 20 77 69 74 68 20 70 54 6f 20 0a iated with pTo .
162b1 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e ** contain
162b2 69 6e 67 20 74 68 65 20 6f 72 69 67 69 6e 61 6c ing the original
162b3 20 64 61 74 61 2e 20 49 74 20 64 6f 65 73 20 6e data. It does n
162b4 6f 74 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 ot sync the data
162b5 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 base file.
162b6 2a 2a 20 69 74 73 65 6c 66 2e 20 41 66 74 65 72 ** itself. After
162b7 20 64 6f 69 6e 67 20 74 68 69 73 20 69 74 20 69 doing this it i
162b8 73 20 73 61 66 65 20 74 6f 20 75 73 65 20 4f 73 s safe to use Os
162b9 54 72 75 6e 63 61 74 65 28 29 20 61 6e 64 20 6f Truncate() and o
162ba 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 66 69 ther. ** fi
162bb 6c 65 20 41 50 49 73 20 6f 6e 20 74 68 65 20 64 le APIs on the d
162bc 61 74 61 62 61 73 65 20 66 69 6c 65 20 64 69 72 atabase file dir
162bd 65 63 74 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a ectly.. */.
162be 20 20 20 20 20 20 70 42 74 54 6f 2d 3e 64 62 20 pBtTo->db
162bf 3d 20 70 54 6f 2d 3e 64 62 3b 0a 20 20 20 20 20 = pTo->db;.
162c0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
162c1 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 erCommitPhaseOne
162c2 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20 (pBtTo->pPager,
162c3 30 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20 20 20 0, 0, 1);.
162c4 69 66 28 20 69 53 69 7a 65 3c 69 4e 6f 77 20 26 if( iSize<iNow &
162c5 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 & rc==SQLITE_OK
162c6 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
162c7 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
162c8 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 3b e(pFile, iSize);
162c9 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 . }. .
162ca 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 70 20 74 68 /* The loop th
162cb 61 74 20 63 6f 70 69 65 64 20 64 61 74 61 20 66 at copied data f
162cc 72 6f 6d 20 64 61 74 61 62 61 73 65 20 70 46 72 rom database pFr
162cd 6f 6d 20 74 6f 20 70 54 6f 20 64 69 64 20 6e 6f om to pTo did no
162ce 74 0a 20 20 20 20 20 20 2a 2a 20 70 6f 70 75 6c t. ** popul
162cf 61 74 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ate the locking
162d0 70 61 67 65 20 6f 66 20 64 61 74 61 62 61 73 65 page of database
162d1 20 70 54 6f 2e 20 49 66 20 74 68 65 20 70 61 67 pTo. If the pag
162d2 65 2d 73 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 e-size of.
162d3 2a 2a 20 70 46 72 6f 6d 20 69 73 20 73 6d 61 6c ** pFrom is smal
162d4 6c 65 72 20 74 68 61 6e 20 74 68 61 74 20 6f 66 ler than that of
162d5 20 70 54 6f 2c 20 74 68 69 73 20 6d 65 61 6e 73 pTo, this means
162d6 20 73 6f 6d 65 20 64 61 74 61 20 77 69 6c 6c 0a some data will.
162d7 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61 76 ** not hav
162d8 65 20 62 65 65 6e 20 63 6f 70 69 65 64 2e 20 0a e been copied. .
162d9 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
162da 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 * This block cop
162db 69 65 73 20 74 68 65 20 6d 69 73 73 69 6e 67 20 ies the missing
162dc 64 61 74 61 20 66 72 6f 6d 20 64 61 74 61 62 61 data from databa
162dd 73 65 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 20 se pFrom to pTo
162de 0a 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 . ** using
162df 66 69 6c 65 20 41 50 49 73 2e 20 54 68 69 73 20 file APIs. This
162e0 69 73 20 73 61 66 65 20 62 65 63 61 75 73 65 20 is safe because
162e1 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 77 65 at this point we
162e2 20 6b 6e 6f 77 20 74 68 61 74 0a 20 20 20 20 20 know that.
162e3 20 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f ** all of the o
162e4 72 69 67 69 6e 61 6c 20 64 61 74 61 20 66 72 6f riginal data fro
162e5 6d 20 70 54 6f 20 68 61 73 20 62 65 65 6e 20 73 m pTo has been s
162e6 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 0a ynced into the .
162e7 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c ** journal
162e8 20 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70 file. At this p
162e9 6f 69 6e 74 20 69 74 20 77 6f 75 6c 64 20 62 65 oint it would be
162ea 20 73 61 66 65 20 74 6f 20 64 6f 20 61 6e 79 74 safe to do anyt
162eb 68 69 6e 67 20 61 74 0a 20 20 20 20 20 20 2a 2a hing at. **
162ec 20 61 6c 6c 20 74 6f 20 74 68 65 20 64 61 74 61 all to the data
162ed 62 61 73 65 20 66 69 6c 65 20 65 78 63 65 70 74 base file except
162ee 20 74 72 75 6e 63 61 74 65 20 69 74 20 74 6f 20 truncate it to
162ef 7a 65 72 6f 20 62 79 74 65 73 2e 0a 20 20 20 20 zero bytes..
162f0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 */. if( r
162f1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
162f2 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 3c 6e 54 nFromPageSize<nT
162f3 6f 50 61 67 65 53 69 7a 65 20 26 26 20 69 53 69 oPageSize && iSi
162f4 7a 65 3e 69 50 65 6e 64 69 6e 67 29 7b 0a 20 20 ze>iPending){.
162f5 20 20 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a i64 iOff;.
162f6 20 20 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20 for(.
162f7 20 20 20 20 20 20 20 69 4f 66 66 3d 69 50 65 6e iOff=iPen
162f8 64 69 6e 67 3b 20 0a 20 20 20 20 20 20 20 20 20 ding; .
162f9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
162fa 26 20 69 4f 66 66 3c 28 69 50 65 6e 64 69 6e 67 & iOff<(iPending
162fb 2b 6e 54 6f 50 61 67 65 53 69 7a 65 29 3b 20 0a +nToPageSize); .
162fc 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 20 2b iOff +
162fd 3d 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 0a = nFromPageSize.
162fe 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ){.
162ff 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 46 72 DbPage *pFr
16300 6f 6d 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 omPage = 0;.
16301 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 6f 6d Pgno iFrom
16302 20 3d 20 28 69 4f 66 66 2f 6e 46 72 6f 6d 50 61 = (iOff/nFromPa
16303 67 65 53 69 7a 65 29 2b 31 3b 0a 20 20 0a 20 20 geSize)+1;. .
16304 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f if( iFro
16305 6d 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f m==PENDING_BYTE_
16306 50 41 47 45 28 70 42 74 46 72 6f 6d 29 20 7c 7c PAGE(pBtFrom) ||
16307 20 69 46 72 6f 6d 3e 6e 46 72 6f 6d 50 61 67 65 iFrom>nFromPage
16308 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
16309 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
1630a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 }. .
1630b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1630c 61 67 65 72 47 65 74 28 70 42 74 46 72 6f 6d 2d agerGet(pBtFrom-
1630d 3e 70 50 61 67 65 72 2c 20 69 46 72 6f 6d 2c 20 >pPager, iFrom,
1630e 26 70 46 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20 &pFromPage);.
1630f 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
16310 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16311 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 46 char *zF
16312 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rom = sqlite3Pag
16313 65 72 47 65 74 44 61 74 61 28 70 46 72 6f 6d 50 erGetData(pFromP
16314 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
16315 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
16316 57 72 69 74 65 28 70 46 69 6c 65 2c 20 7a 46 72 Write(pFile, zFr
16317 6f 6d 2c 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a om, nFromPageSiz
16318 65 2c 20 69 4f 66 66 29 3b 0a 20 20 20 20 20 20 e, iOff);.
16319 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
1631a 65 72 55 6e 72 65 66 28 70 46 72 6f 6d 50 61 67 erUnref(pFromPag
1631b 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a e);. }.
1631c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1631d 7d 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 53 79 }. . /* Sy
1631e 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nc the database
1631f 66 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 file */. if
16320 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16321 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
16322 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 sqlite3PagerSync
16323 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 29 3b (pBtTo->pPager);
16324 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
16325 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 se{. rc = s
16326 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 qlite3PagerTrunc
16327 61 74 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 ate(pBtTo->pPage
16328 72 2c 20 6e 4e 65 77 50 61 67 65 29 3b 0a 20 20 r, nNewPage);.
16329 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
1632a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1632b 20 20 20 70 42 74 54 6f 2d 3e 70 61 67 65 53 69 pBtTo->pageSi
1632c 7a 65 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 20 zeFixed = 0;.
1632d 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 }. }.. if( rc
1632e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
1632f 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 54 6f treeRollback(pTo
16330 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e );. }.. return
16331 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 45 5f rc; .}.SQLITE_
16332 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
16333 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 te3BtreeCopyFile
16334 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 74 72 (Btree *pTo, Btr
16335 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e ee *pFrom){. in
16336 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 t rc;. sqlite3B
16337 74 72 65 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a treeEnter(pTo);.
16338 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
16339 74 65 72 28 70 46 72 6f 6d 29 3b 0a 20 20 72 63 ter(pFrom);. rc
1633a 20 3d 20 62 74 72 65 65 43 6f 70 79 46 69 6c 65 = btreeCopyFile
1633b 28 70 54 6f 2c 20 70 46 72 6f 6d 29 3b 0a 20 20 (pTo, pFrom);.
1633c 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1633d 65 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 e(pFrom);. sqli
1633e 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 te3BtreeLeave(pT
1633f 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b o);. return rc;
16340 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
16341 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d LITE_OMIT_VACUUM
16342 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 */../*.** Retur
16343 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 n non-zero if a
16344 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
16345 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ctive..*/.SQLITE
16346 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
16347 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 ite3BtreeIsInTra
16348 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 ns(Btree *p){.
16349 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 assert( p==0 ||
1634a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1634b 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
1634c 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 );. return (p
1634d 26 26 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d && (p->inTrans==
1634e 54 52 41 4e 53 5f 57 52 49 54 45 29 29 3b 0a 7d TRANS_WRITE));.}
1634f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e ../*.** Return n
16350 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 73 74 61 on-zero if a sta
16351 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
16352 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f on is active..*/
16353 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16354 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16355 49 73 49 6e 53 74 6d 74 28 42 74 72 65 65 20 2a IsInStmt(Btree *
16356 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 p){. assert( sq
16357 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
16358 75 74 65 78 28 70 29 20 29 3b 0a 20 20 72 65 74 utex(p) );. ret
16359 75 72 6e 20 28 70 2d 3e 70 42 74 20 26 26 20 70 urn (p->pBt && p
1635a 2d 3e 70 42 74 2d 3e 69 6e 53 74 6d 74 29 3b 0a ->pBt->inStmt);.
1635b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1635c 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 72 65 non-zero if a re
1635d 61 64 20 28 6f 72 20 77 72 69 74 65 29 20 74 72 ad (or write) tr
1635e 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
1635f 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ive..*/.SQLITE_P
16360 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16361 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 e3BtreeIsInReadT
16362 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a rans(Btree *p){.
16363 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16364 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
16365 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
16366 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 2d return (p && (p-
16367 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f >inTrans!=TRANS_
16368 4e 4f 4e 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a NONE));.}../*.**
16369 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 This function r
1636a 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
1636b 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 6d 65 to a blob of me
1636c 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 mory associated
1636d 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 with.** a single
1636e 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e 20 54 shared-btree. T
1636f 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 75 73 65 he memory is use
16370 64 20 62 79 20 63 6c 69 65 6e 74 20 63 6f 64 65 d by client code
16371 20 66 6f 72 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 for its own.**
16372 70 75 72 70 6f 73 65 73 20 28 66 6f 72 20 65 78 purposes (for ex
16373 61 6d 70 6c 65 2c 20 74 6f 20 73 74 6f 72 65 20 ample, to store
16374 61 20 68 69 67 68 2d 6c 65 76 65 6c 20 73 63 68 a high-level sch
16375 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ema associated w
16376 69 74 68 20 0a 2a 2a 20 74 68 65 20 73 68 61 72 ith .** the shar
16377 65 64 2d 62 74 72 65 65 29 2e 20 54 68 65 20 62 ed-btree). The b
16378 74 72 65 65 20 6c 61 79 65 72 20 6d 61 6e 61 67 tree layer manag
16379 65 73 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 es reference cou
1637a 6e 74 69 6e 67 20 69 73 73 75 65 73 2e 0a 2a 2a nting issues..**
1637b 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 .** The first ti
1637c 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 me this is calle
1637d 64 20 6f 6e 20 61 20 73 68 61 72 65 64 2d 62 74 d on a shared-bt
1637e 72 65 65 2c 20 6e 42 79 74 65 73 20 62 79 74 65 ree, nBytes byte
1637f 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 s of memory.** a
16380 72 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 7a 65 re allocated, ze
16381 72 6f 65 64 2c 20 61 6e 64 20 72 65 74 75 72 6e roed, and return
16382 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
16383 2e 20 46 6f 72 20 65 61 63 68 20 73 75 62 73 65 . For each subse
16384 71 75 65 6e 74 20 0a 2a 2a 20 63 61 6c 6c 20 74 quent .** call t
16385 68 65 20 6e 42 79 74 65 73 20 70 61 72 61 6d 65 he nBytes parame
16386 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20 61 ter is ignored a
16387 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 nd a pointer to
16388 74 68 65 20 73 61 6d 65 20 62 6c 6f 62 0a 2a 2a the same blob.**
16389 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 74 75 72 of memory retur
1638a 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ned. .**.** If t
1638b 68 65 20 6e 42 79 74 65 73 20 70 61 72 61 6d 65 he nBytes parame
1638c 74 65 72 20 69 73 20 30 20 61 6e 64 20 74 68 65 ter is 0 and the
1638d 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 blob of memory
1638e 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
1638f 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 .** allocated, a
16390 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 null pointer is
16391 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68 returned. If th
16392 65 20 62 6c 6f 62 20 68 61 73 20 61 6c 72 65 61 e blob has alrea
16393 64 79 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 dy been.** alloc
16394 61 74 65 64 2c 20 69 74 20 69 73 20 72 65 74 75 ated, it is retu
16395 72 6e 65 64 20 61 73 20 6e 6f 72 6d 61 6c 2e 0a rned as normal..
16396 2a 2a 0a 2a 2a 20 4a 75 73 74 20 62 65 66 6f 72 **.** Just befor
16397 65 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 e the shared-btr
16398 65 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 68 ee is closed, th
16399 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65 e function passe
1639a 64 20 61 73 20 74 68 65 20 0a 2a 2a 20 78 46 72 d as the .** xFr
1639b 65 65 20 61 72 67 75 6d 65 6e 74 20 77 68 65 6e ee argument when
1639c 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
1639d 63 61 74 69 6f 6e 20 77 61 73 20 6d 61 64 65 20 cation was made
1639e 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 is invoked on th
1639f 65 20 0a 2a 2a 20 62 6c 6f 62 20 6f 66 20 61 6c e .** blob of al
163a0 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 located memory.
163a1 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 This function sh
163a2 6f 75 6c 64 20 6e 6f 74 20 63 61 6c 6c 20 73 71 ould not call sq
163a3 6c 69 74 65 33 5f 66 72 65 65 28 29 0a 2a 2a 20 lite3_free().**
163a4 6f 6e 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74 on the memory, t
163a5 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 64 he btree layer d
163a6 6f 65 73 20 74 68 61 74 2e 0a 2a 2f 0a 53 51 4c oes that..*/.SQL
163a7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
163a8 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 *sqlite3BtreeSc
163a9 68 65 6d 61 28 42 74 72 65 65 20 2a 70 2c 20 69 hema(Btree *p, i
163aa 6e 74 20 6e 42 79 74 65 73 2c 20 76 6f 69 64 28 nt nBytes, void(
163ab 2a 78 46 72 65 65 29 28 76 6f 69 64 20 2a 29 29 *xFree)(void *))
163ac 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
163ad 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 t = p->pBt;. sq
163ae 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
163af 70 29 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e p);. if( !pBt->
163b0 70 53 63 68 65 6d 61 20 26 26 20 6e 42 79 74 65 pSchema && nByte
163b1 73 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 53 s ){. pBt->pS
163b2 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 4d chema = sqlite3M
163b3 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 79 74 65 73 allocZero(nBytes
163b4 29 3b 0a 20 20 20 20 70 42 74 2d 3e 78 46 72 65 );. pBt->xFre
163b5 65 53 63 68 65 6d 61 20 3d 20 78 46 72 65 65 3b eSchema = xFree;
163b6 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
163b7 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
163b8 65 74 75 72 6e 20 70 42 74 2d 3e 70 53 63 68 65 eturn pBt->pSche
163b9 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ma;.}../*.** Ret
163ba 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 6f 74 urn true if anot
163bb 68 65 72 20 75 73 65 72 20 6f 66 20 74 68 65 20 her user of the
163bc 73 61 6d 65 20 73 68 61 72 65 64 20 62 74 72 65 same shared btre
163bd 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e e as the argumen
163be 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 68 6f 6c 64 t.** handle hold
163bf 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c s an exclusive l
163c0 6f 63 6b 20 6f 6e 20 74 68 65 20 73 71 6c 69 74 ock on the sqlit
163c1 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a e_master table..
163c2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
163c3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
163c4 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 eeSchemaLocked(B
163c5 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 tree *p){. int
163c6 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 rc;. assert( sq
163c7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
163c8 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
163c9 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
163ca 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d Enter(p);. rc =
163cb 20 28 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b (queryTableLock
163cc 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c (p, MASTER_ROOT,
163cd 20 52 45 41 44 5f 4c 4f 43 4b 29 21 3d 53 51 4c READ_LOCK)!=SQL
163ce 49 54 45 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69 74 ITE_OK);. sqlit
163cf 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
163d0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
163d1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
163d2 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
163d3 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 HE./*.** Obtain
163d4 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 a lock on the ta
163d5 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 ble whose root p
163d6 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68 age is iTab. Th
163d7 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77 e.** lock is a w
163d8 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57 rite lock if isW
163d9 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65 ritelock is true
163da 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a or a read lock.
163db 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73 ** if it is fals
163dc 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
163dd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
163de 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 BtreeLockTable(B
163df 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 tree *p, int iTa
163e0 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 b, u8 isWriteLoc
163e1 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 k){. int rc = S
163e2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
163e3 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 p->sharable ){.
163e4 20 20 20 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d u8 lockType =
163e5 20 52 45 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 57 READ_LOCK + isW
163e6 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 61 73 riteLock;. as
163e7 73 65 72 74 28 20 52 45 41 44 5f 4c 4f 43 4b 2b sert( READ_LOCK+
163e8 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 1==WRITE_LOCK );
163e9 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 57 . assert( isW
163ea 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 riteLock==0 || i
163eb 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b sWriteLock==1 );
163ec 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
163ed 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72 eEnter(p);. r
163ee 63 20 3d 20 71 75 65 72 79 54 61 62 6c 65 4c 6f c = queryTableLo
163ef 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b ck(p, iTab, lock
163f0 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Type);. if( r
163f1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
163f2 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 54 rc = lockT
163f3 61 62 6c 65 28 70 2c 20 69 54 61 62 2c 20 6c 6f able(p, iTab, lo
163f4 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 ckType);. }.
163f5 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
163f6 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 eave(p);. }. r
163f7 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
163f8 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
163f9 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
163fa 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 ./*.** Argument
163fb 70 43 73 72 20 6d 75 73 74 20 62 65 20 61 20 63 pCsr must be a c
163fc 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72 ursor opened for
163fd 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a writing on an .
163fe 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20 ** INTKEY table
163ff 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
16400 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20 74 61 ng at a valid ta
16401 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54 ble entry. .** T
16402 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 his function mod
16403 69 66 69 65 73 20 74 68 65 20 64 61 74 61 20 73 ifies the data s
16404 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 tored as part of
16405 20 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 20 that entry..**
16406 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 63 6f Only the data co
16407 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 20 62 ntent may only b
16408 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 20 69 e modified, it i
16409 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a s not possible.*
1640a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 * to change the
1640b 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 64 61 length of the da
1640c 74 61 20 73 74 6f 72 65 64 2e 0a 2a 2f 0a 53 51 ta stored..*/.SQ
1640d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1640e 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 sqlite3BtreePut
1640f 44 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70 Data(BtCursor *p
16410 43 73 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c Csr, u32 offset,
16411 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a u32 amt, void *
16412 7a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 z){. assert( cu
16413 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
16414 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Csr) );. assert
16415 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
16416 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72 65 held(pCsr->pBtre
16417 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b e->db->mutex) );
16418 0a 20 20 61 73 73 65 72 74 28 70 43 73 72 2d 3e . assert(pCsr->
16419 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 isIncrblobHandle
1641a 29 3b 0a 0a 20 20 72 65 73 74 6f 72 65 43 75 72 );.. restoreCur
1641b 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 73 72 sorPosition(pCsr
1641c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 );. assert( pCs
1641d 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f r->eState!=CURSO
1641e 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b R_REQUIRESEEK );
1641f 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53 74 . if( pCsr->eSt
16420 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate!=CURSOR_VALI
16421 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 D ){. return
16422 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 SQLITE_ABORT;.
16423 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 73 6f }.. /* Check so
16424 6d 65 20 70 72 65 63 6f 6e 64 69 74 69 6f 6e 73 me preconditions
16425 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68 : . ** (a) th
16426 65 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e e cursor is open
16427 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 for writing,.
16428 2a 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69 ** (b) there i
16429 73 20 6e 6f 20 72 65 61 64 2d 6c 6f 63 6b 20 6f s no read-lock o
1642a 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e n the table bein
1642b 67 20 6d 6f 64 69 66 69 65 64 20 61 6e 64 0a 20 g modified and.
1642c 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20 63 75 ** (c) the cu
1642d 72 73 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 rsor points at a
1642e 20 76 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e valid row of an
1642f 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 intKey table..
16430 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d */. if( !pCsr-
16431 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 >wrFlag ){. r
16432 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 eturn SQLITE_REA
16433 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 DONLY;. }. ass
16434 65 72 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d ert( !pCsr->pBt-
16435 3e 72 65 61 64 4f 6e 6c 79 20 0a 20 20 20 20 20 >readOnly .
16436 20 20 20 20 20 26 26 20 70 43 73 72 2d 3e 70 42 && pCsr->pB
16437 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
16438 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
16439 0a 20 20 69 66 28 20 63 68 65 63 6b 52 65 61 64 . if( checkRead
1643a 4c 6f 63 6b 73 28 70 43 73 72 2d 3e 70 42 74 72 Locks(pCsr->pBtr
1643b 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f ee, pCsr->pgnoRo
1643c 6f 74 2c 20 70 43 73 72 2c 20 30 29 20 29 7b 0a ot, pCsr, 0) ){.
1643d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1643e 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a 20 54 68 65 E_LOCKED; /* The
1643f 20 74 61 62 6c 65 20 70 43 75 72 20 70 6f 69 6e table pCur poin
16440 74 73 20 74 6f 20 68 61 73 20 61 20 72 65 61 64 ts to has a read
16441 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d 0a 20 20 69 lock */. }. i
16442 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 3d f( pCsr->eState=
16443 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
16444 7c 7c 20 21 70 43 73 72 2d 3e 61 70 50 61 67 65 || !pCsr->apPage
16445 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 [pCsr->iPage]->i
16446 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74 ntKey ){. ret
16447 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
16448 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
16449 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 accessPayload(pC
1644a 73 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c sr, offset, amt,
1644b 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
1644c 2a 29 7a 2c 20 30 2c 20 31 29 3b 0a 7d 0a 0a 2f *)z, 0, 1);.}../
1644d 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 * .** Set a flag
1644e 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 on this cursor
1644f 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63 to cache the loc
16450 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20 ations of pages
16451 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65 from the .** ove
16452 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74 rflow list for t
16453 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20 he current row.
16454 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
16455 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a cursors opened.*
16456 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61 * for incrementa
16457 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a l blob IO only..
16458 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
16459 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20 ion sets a flag
1645a 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c only. The actual
1645b 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63 page location c
1645c 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20 ache.** (stored
1645d 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 in BtCursor.aOve
1645e 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f rflow[]) is allo
1645f 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62 cated and used b
16460 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63 y function.** ac
16461 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74 cessPayload() (t
16462 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69 he worker functi
16463 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74 on for sqlite3Bt
16464 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a reeData() and.**
16465 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 sqlite3BtreePut
16466 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 Data())..*/.SQLI
16467 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
16468 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 sqlite3BtreeCach
16469 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73 eOverflow(BtCurs
1646a 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
1646b 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
1646c 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
1646d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1646e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
1646f 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
16470 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
16471 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c (!pCur->isIncrbl
16472 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73 obHandle);. ass
16473 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 ert(!pCur->aOver
16474 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69 flow);. pCur->i
16475 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 sIncrblobHandle
16476 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f = 1;.}.#endif../
16477 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
16478 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a nd of btree.c **
16479 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1647a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1647b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1647c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
1647d 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 66 69 egin file vdbefi
1647e 66 6f 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a fo.c ***********
1647f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
16481 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6e 65 20 31 *.** 2005 June 1
16482 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6.**.** The auth
16483 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
16484 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
16485 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
16486 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
16487 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
16488 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
16489 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1648a 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1648b 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1648c 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1648d 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1648e 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1648f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
16490 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
16491 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
16492 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
16493 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
16494 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16495 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16496 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16497 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16498 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d .** This file im
16499 70 6c 65 6d 65 6e 74 73 20 61 20 46 49 46 4f 20 plements a FIFO
1649a 71 75 65 75 65 20 6f 66 20 72 6f 77 69 64 73 20 queue of rowids
1649b 75 73 65 64 20 66 6f 72 20 70 72 6f 63 65 73 73 used for process
1649c 69 6e 67 0a 2a 2a 20 55 50 44 41 54 45 20 61 6e ing.** UPDATE an
1649d 64 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 d DELETE stateme
1649e 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 nts..**.** $Id:
1649f 76 64 62 65 66 69 66 6f 2e 63 2c 76 20 31 2e 39 vdbefifo.c,v 1.9
164a0 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39 3a 31 2008/11/17 19:1
164a1 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 8:55 danielk1977
164a2 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
164a3 20 43 6f 6e 73 74 61 6e 74 73 20 46 49 46 4f 53 Constants FIFOS
164a4 49 5a 45 5f 46 49 52 53 54 20 61 6e 64 20 46 49 IZE_FIRST and FI
164a5 46 4f 53 49 5a 45 5f 4d 41 58 20 61 72 65 20 74 FOSIZE_MAX are t
164a6 68 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 6e 75 he initial.** nu
164a7 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
164a8 69 6e 20 61 20 66 69 66 6f 20 70 61 67 65 20 61 in a fifo page a
164a9 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e nd the maximum n
164aa 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 umber of.** entr
164ab 69 65 73 20 69 6e 20 61 20 66 69 66 6f 20 70 61 ies in a fifo pa
164ac 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 ge..*/.#define F
164ad 49 46 4f 53 49 5a 45 5f 46 49 52 53 54 20 28 28 IFOSIZE_FIRST ((
164ae 28 31 32 38 2d 73 69 7a 65 6f 66 28 46 69 66 6f (128-sizeof(Fifo
164af 50 61 67 65 29 29 2f 38 29 2b 31 29 0a 23 69 66 Page))/8)+1).#if
164b0 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f def SQLITE_MALLO
164b1 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 0a 23 20 64 C_SOFT_LIMIT.# d
164b2 65 66 69 6e 65 20 46 49 46 4f 53 49 5a 45 5f 4d efine FIFOSIZE_M
164b3 41 58 20 20 20 28 69 6e 74 29 28 28 28 53 51 4c AX (int)(((SQL
164b4 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f ITE_MALLOC_SOFT_
164b5 4c 49 4d 49 54 2d 73 69 7a 65 6f 66 28 46 69 66 LIMIT-sizeof(Fif
164b6 6f 50 61 67 65 29 29 2f 38 29 2b 31 29 0a 23 65 oPage))/8)+1).#e
164b7 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 46 49 46 lse.# define FIF
164b8 4f 53 49 5a 45 5f 4d 41 58 20 20 20 28 69 6e 74 OSIZE_MAX (int
164b9 29 28 28 28 32 36 32 31 34 34 2d 73 69 7a 65 6f )(((262144-sizeo
164ba 66 28 46 69 66 6f 50 61 67 65 29 29 2f 38 29 2b f(FifoPage))/8)+
164bb 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 1).#endif../*.**
164bc 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 Allocate a new
164bd 46 69 66 6f 50 61 67 65 20 61 6e 64 20 72 65 74 FifoPage and ret
164be 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
164bf 20 69 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c it. Return NUL
164c0 4c 20 69 66 0a 2a 2a 20 77 65 20 72 75 6e 20 6f L if.** we run o
164c1 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 4c ut of memory. L
164c2 65 61 76 65 20 73 70 61 63 65 20 6f 6e 20 74 68 eave space on th
164c3 65 20 70 61 67 65 20 66 6f 72 20 6e 45 6e 74 72 e page for nEntr
164c4 79 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 y entries..*/.st
164c5 61 74 69 63 20 46 69 66 6f 50 61 67 65 20 2a 61 atic FifoPage *a
164c6 6c 6c 6f 63 61 74 65 46 69 66 6f 50 61 67 65 28 llocateFifoPage(
164c7 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
164c8 20 6e 45 6e 74 72 79 29 7b 0a 20 20 46 69 66 6f nEntry){. Fifo
164c9 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 Page *pPage;. i
164ca 66 28 20 6e 45 6e 74 72 79 3e 46 49 46 4f 53 49 f( nEntry>FIFOSI
164cb 5a 45 5f 4d 41 58 20 29 7b 0a 20 20 20 20 6e 45 ZE_MAX ){. nE
164cc 6e 74 72 79 20 3d 20 46 49 46 4f 53 49 5a 45 5f ntry = FIFOSIZE_
164cd 4d 41 58 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 MAX;. }. pPage
164ce 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
164cf 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 ocRaw(db, sizeof
164d0 28 46 69 66 6f 50 61 67 65 29 20 2b 20 73 69 7a (FifoPage) + siz
164d1 65 6f 66 28 69 36 34 29 2a 28 6e 45 6e 74 72 79 eof(i64)*(nEntry
164d2 2d 31 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 -1) );. if( pPa
164d3 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d ge ){. pPage-
164d4 3e 6e 53 6c 6f 74 20 3d 20 6e 45 6e 74 72 79 3b >nSlot = nEntry;
164d5 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 57 72 69 . pPage->iWri
164d6 74 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 te = 0;. pPag
164d7 65 2d 3e 69 52 65 61 64 20 3d 20 30 3b 0a 20 20 e->iRead = 0;.
164d8 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d pPage->pNext =
164d9 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
164da 20 70 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pPage;.}../*.**
164db 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 46 69 Initialize a Fi
164dc 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f fo structure..*/
164dd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
164de 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
164df 46 69 66 6f 49 6e 69 74 28 46 69 66 6f 20 2a 70 FifoInit(Fifo *p
164e0 46 69 66 6f 2c 20 73 71 6c 69 74 65 33 20 2a 64 Fifo, sqlite3 *d
164e1 62 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 b){. memset(pFi
164e2 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 fo, 0, sizeof(*p
164e3 46 69 66 6f 29 29 3b 0a 20 20 70 46 69 66 6f 2d Fifo));. pFifo-
164e4 3e 64 62 20 3d 20 64 62 3b 0a 7d 0a 0a 2f 2a 0a >db = db;.}../*.
164e5 2a 2a 20 50 75 73 68 20 61 20 73 69 6e 67 6c 65 ** Push a single
164e6 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 64-bit integer
164e7 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 46 value into the F
164e8 69 66 6f 2e 20 20 52 65 74 75 72 6e 20 53 51 4c ifo. Return SQL
164e9 49 54 45 5f 4f 4b 0a 2a 2a 20 6e 6f 72 6d 61 6c ITE_OK.** normal
164ea 6c 79 2e 20 20 20 53 51 4c 49 54 45 5f 4e 4f 4d ly. SQLITE_NOM
164eb 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 EM is returned i
164ec 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 f we are unable
164ed 74 6f 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d to allocate.** m
164ee 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 emory..*/.SQLITE
164ef 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
164f0 69 74 65 33 56 64 62 65 46 69 66 6f 50 75 73 68 ite3VdbeFifoPush
164f1 28 46 69 66 6f 20 2a 70 46 69 66 6f 2c 20 69 36 (Fifo *pFifo, i6
164f2 34 20 76 61 6c 29 7b 0a 20 20 46 69 66 6f 50 61 4 val){. FifoPa
164f3 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 50 61 ge *pPage;. pPa
164f4 67 65 20 3d 20 70 46 69 66 6f 2d 3e 70 4c 61 73 ge = pFifo->pLas
164f5 74 3b 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d t;. if( pPage==
164f6 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 0 ){. pPage =
164f7 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 pFifo->pLast =
164f8 70 46 69 66 6f 2d 3e 70 46 69 72 73 74 20 3d 0a pFifo->pFirst =.
164f9 20 20 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 allocat
164fa 65 46 69 66 6f 50 61 67 65 28 70 46 69 66 6f 2d eFifoPage(pFifo-
164fb 3e 64 62 2c 20 46 49 46 4f 53 49 5a 45 5f 46 49 >db, FIFOSIZE_FI
164fc 52 53 54 29 3b 0a 20 20 20 20 69 66 28 20 70 50 RST);. if( pP
164fd 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 age==0 ){.
164fe 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
164ff 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c MEM;. }. }el
16500 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e 69 57 se if( pPage->iW
16501 72 69 74 65 3e 3d 70 50 61 67 65 2d 3e 6e 53 6c rite>=pPage->nSl
16502 6f 74 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d ot ){. pPage-
16503 3e 70 4e 65 78 74 20 3d 20 61 6c 6c 6f 63 61 74 >pNext = allocat
16504 65 46 69 66 6f 50 61 67 65 28 70 46 69 66 6f 2d eFifoPage(pFifo-
16505 3e 64 62 2c 20 70 46 69 66 6f 2d 3e 6e 45 6e 74 >db, pFifo->nEnt
16506 72 79 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 ry);. if( pPa
16507 67 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29 7b 0a ge->pNext==0 ){.
16508 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
16509 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
1650a 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 46 69 . pPage = pFi
1650b 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 70 50 61 67 fo->pLast = pPag
1650c 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 e->pNext;. }.
1650d 70 50 61 67 65 2d 3e 61 53 6c 6f 74 5b 70 50 61 pPage->aSlot[pPa
1650e 67 65 2d 3e 69 57 72 69 74 65 2b 2b 5d 20 3d 20 ge->iWrite++] =
1650f 76 61 6c 3b 0a 20 20 70 46 69 66 6f 2d 3e 6e 45 val;. pFifo->nE
16510 6e 74 72 79 2b 2b 3b 0a 20 20 72 65 74 75 72 6e ntry++;. return
16511 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
16512 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61 20 73 *.** Extract a s
16513 69 6e 67 6c 65 20 36 34 2d 62 69 74 20 69 6e 74 ingle 64-bit int
16514 65 67 65 72 20 76 61 6c 75 65 20 66 72 6f 6d 20 eger value from
16515 74 68 65 20 46 69 66 6f 2e 20 20 54 68 65 20 69 the Fifo. The i
16516 6e 74 65 67 65 72 0a 2a 2a 20 65 78 74 72 61 63 nteger.** extrac
16517 74 65 64 20 69 73 20 74 68 65 20 6f 6e 65 20 6c ted is the one l
16518 65 61 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e east recently in
16519 73 65 72 74 65 64 2e 20 20 49 66 20 74 68 65 20 serted. If the
1651a 46 69 66 6f 20 69 73 20 65 6d 70 74 79 0a 2a 2a Fifo is empty.**
1651b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
1651c 4f 4e 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ONE..*/.SQLITE_P
1651d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1651e 65 33 56 64 62 65 46 69 66 6f 50 6f 70 28 46 69 e3VdbeFifoPop(Fi
1651f 66 6f 20 2a 70 46 69 66 6f 2c 20 69 36 34 20 2a fo *pFifo, i64 *
16520 70 56 61 6c 29 7b 0a 20 20 46 69 66 6f 50 61 67 pVal){. FifoPag
16521 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 66 28 20 e *pPage;. if(
16522 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3d 3d 30 pFifo->nEntry==0
16523 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16524 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a QLITE_DONE;. }.
16525 20 20 61 73 73 65 72 74 28 20 70 46 69 66 6f 2d assert( pFifo-
16526 3e 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20 20 70 >nEntry>0 );. p
16527 50 61 67 65 20 3d 20 70 46 69 66 6f 2d 3e 70 46 Page = pFifo->pF
16528 69 72 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 irst;. assert(
16529 70 50 61 67 65 21 3d 30 20 29 3b 0a 20 20 61 73 pPage!=0 );. as
1652a 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 57 72 sert( pPage->iWr
1652b 69 74 65 3e 70 50 61 67 65 2d 3e 69 52 65 61 64 ite>pPage->iRead
1652c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
1652d 61 67 65 2d 3e 69 57 72 69 74 65 3c 3d 70 50 61 age->iWrite<=pPa
1652e 67 65 2d 3e 6e 53 6c 6f 74 20 29 3b 0a 20 20 61 ge->nSlot );. a
1652f 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 52 ssert( pPage->iR
16530 65 61 64 3c 70 50 61 67 65 2d 3e 6e 53 6c 6f 74 ead<pPage->nSlot
16531 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
16532 61 67 65 2d 3e 69 52 65 61 64 3e 3d 30 20 29 3b age->iRead>=0 );
16533 0a 20 20 2a 70 56 61 6c 20 3d 20 70 50 61 67 65 . *pVal = pPage
16534 2d 3e 61 53 6c 6f 74 5b 70 50 61 67 65 2d 3e 69 ->aSlot[pPage->i
16535 52 65 61 64 2b 2b 5d 3b 0a 20 20 70 46 69 66 6f Read++];. pFifo
16536 2d 3e 6e 45 6e 74 72 79 2d 2d 3b 0a 20 20 69 66 ->nEntry--;. if
16537 28 20 70 50 61 67 65 2d 3e 69 52 65 61 64 3e 3d ( pPage->iRead>=
16538 70 50 61 67 65 2d 3e 69 57 72 69 74 65 20 29 7b pPage->iWrite ){
16539 0a 20 20 20 20 70 46 69 66 6f 2d 3e 70 46 69 72 . pFifo->pFir
1653a 73 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 st = pPage->pNex
1653b 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 t;. sqlite3Db
1653c 46 72 65 65 28 70 46 69 66 6f 2d 3e 64 62 2c 20 Free(pFifo->db,
1653d 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 pPage);. if(
1653e 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3d 3d 30 pFifo->nEntry==0
1653f 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
16540 28 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 3d 3d ( pFifo->pLast==
16541 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 70 pPage );. p
16542 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 30 3b Fifo->pLast = 0;
16543 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
16544 20 20 61 73 73 65 72 74 28 20 70 46 69 66 6f 2d assert( pFifo-
16545 3e 70 46 69 72 73 74 21 3d 30 20 29 3b 0a 20 20 >pFirst!=0 );.
16546 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
16547 20 61 73 73 65 72 74 28 20 70 46 69 66 6f 2d 3e assert( pFifo->
16548 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20 20 7d 0a nEntry>0 );. }.
16549 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1654a 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c OK;.}../*.** Del
1654b 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 ete all informat
1654c 69 6f 6e 20 66 72 6f 6d 20 61 20 46 69 66 6f 20 ion from a Fifo
1654d 6f 62 6a 65 63 74 2e 20 20 20 46 72 65 65 20 61 object. Free a
1654e 6c 6c 20 6d 65 6d 6f 72 79 20 68 65 6c 64 0a 2a ll memory held.*
1654f 2a 20 62 79 20 74 68 65 20 46 69 66 6f 2e 0a 2a * by the Fifo..*
16550 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16551 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
16552 65 46 69 66 6f 43 6c 65 61 72 28 46 69 66 6f 20 eFifoClear(Fifo
16553 2a 70 46 69 66 6f 29 7b 0a 20 20 46 69 66 6f 50 *pFifo){. FifoP
16554 61 67 65 20 2a 70 50 61 67 65 2c 20 2a 70 4e 65 age *pPage, *pNe
16555 78 74 50 61 67 65 3b 0a 20 20 66 6f 72 28 70 50 xtPage;. for(pP
16556 61 67 65 3d 70 46 69 66 6f 2d 3e 70 46 69 72 73 age=pFifo->pFirs
16557 74 3b 20 70 50 61 67 65 3b 20 70 50 61 67 65 3d t; pPage; pPage=
16558 70 4e 65 78 74 50 61 67 65 29 7b 0a 20 20 20 20 pNextPage){.
16559 70 4e 65 78 74 50 61 67 65 20 3d 20 70 50 61 67 pNextPage = pPag
1655a 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 e->pNext;. sq
1655b 6c 69 74 65 33 44 62 46 72 65 65 28 70 46 69 66 lite3DbFree(pFif
1655c 6f 2d 3e 64 62 2c 20 70 50 61 67 65 29 3b 0a 20 o->db, pPage);.
1655d 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 }. sqlite3Vdbe
1655e 46 69 66 6f 49 6e 69 74 28 70 46 69 66 6f 2c 20 FifoInit(pFifo,
1655f 70 46 69 66 6f 2d 3e 64 62 29 3b 0a 7d 0a 0a 2f pFifo->db);.}../
16560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
16561 6e 64 20 6f 66 20 76 64 62 65 66 69 66 6f 2e 63 nd of vdbefifo.c
16562 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
16563 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16564 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
16565 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
16566 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 6d 65 egin file vdbeme
16567 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m.c ************
16568 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16569 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1656a 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 *.** 2004 May 26
1656b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1656c 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1656d 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1656e 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1656f 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
16570 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
16571 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
16572 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
16573 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
16574 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
16575 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
16576 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
16577 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
16578 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
16579 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1657a 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1657b 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1657c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1657d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1657e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1657f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
16581 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
16582 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 contains code us
16583 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20 e to manipulate
16584 22 4d 65 6d 22 20 73 74 72 75 63 74 75 72 65 2e "Mem" structure.
16585 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f A "Mem".** sto
16586 72 65 73 20 61 20 73 69 6e 67 6c 65 20 76 61 6c res a single val
16587 75 65 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 ue in the VDBE.
16588 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 71 75 Mem is an opaqu
16589 65 20 73 74 72 75 63 74 75 72 65 20 76 69 73 69 e structure visi
1658a 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 ble.** only with
1658b 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 49 6e in the VDBE. In
1658c 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 terface routines
1658d 20 72 65 66 65 72 20 74 6f 20 61 20 4d 65 6d 20 refer to a Mem
1658e 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d using the.** nam
1658f 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a e sqlite_value.*
16590 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 6d 65 *.** $Id: vdbeme
16591 6d 2e 63 2c 76 20 31 2e 31 32 36 20 32 30 30 38 m.c,v 1.126 2008
16592 2f 31 31 2f 31 31 20 30 30 3a 32 31 3a 33 30 20 /11/11 00:21:30
16593 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
16594 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 .** Call sqlite3
16595 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
16596 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c b() on the suppl
16597 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 ied value (type
16598 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 Mem*).** P if re
16599 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 quired..*/.#defi
1659a 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50 29 ne expandBlob(P)
1659b 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 (((P)->flags&ME
1659c 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 M_Zero)?sqlite3V
1659d 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
1659e 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 (P):0)../*.** If
1659f 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65 pMem is an obje
165a0 63 74 20 77 69 74 68 20 61 20 76 61 6c 69 64 20 ct with a valid
165a1 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 string represent
165a2 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74 ation, this rout
165a3 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74 ine.** ensures t
165a4 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f he internal enco
165a5 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74 72 ding for the str
165a6 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
165a7 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72 65 on is.** 'desire
165a8 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51 dEnc', one of SQ
165a9 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
165aa 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c E_UTF16LE or SQL
165ab 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a ITE_UTF16BE..**.
165ac 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f ** If pMem is no
165ad 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65 63 t a string objec
165ae 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 t, or the encodi
165af 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 ng of the string
165b0 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 .** representati
165b1 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73 74 on is already st
165b2 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 ored using the r
165b3 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69 6e equested encodin
165b4 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 g, then this.**
165b5 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
165b6 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 op..**.** SQLITE
165b7 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 _OK is returned
165b8 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f if the conversio
165b9 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20 n is successful
165ba 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65 64 (or not required
165bb 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d )..** SQLITE_NOM
165bc 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e EM may be return
165bd 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 ed if a malloc()
165be 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63 6f fails during co
165bf 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77 nversion.** betw
165c0 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a een formats..*/.
165c1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
165c2 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 nt sqlite3VdbeCh
165c3 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d angeEncoding(Mem
165c4 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69 *pMem, int desi
165c5 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72 redEnc){. int r
165c6 63 3b 0a 20 20 69 66 28 20 21 28 70 4d 65 6d 2d c;. if( !(pMem-
165c7 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 >flags&MEM_Str)
165c8 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 || pMem->enc==de
165c9 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20 20 20 siredEnc ){.
165ca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
165cb 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
165cc 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
165cd 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
165ce 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
165cf 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c x) );.#ifdef SQL
165d0 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 ITE_OMIT_UTF16.
165d1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
165d2 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 20 20 2f RROR;.#else.. /
165d3 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 * MemTranslate()
165d4 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 may return SQLI
165d5 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 45 5f TE_OK or SQLITE_
165d6 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45 4d 20 NOMEM. If NOMEM
165d7 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20 20 2a is returned,. *
165d8 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63 6f 64 * then the encod
165d9 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ing of the value
165da 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 63 68 may not have ch
165db 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 anged.. */. rc
165dc 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
165dd 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65 6d 2c mTranslate(pMem,
165de 20 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 desiredEnc);.
165df 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 assert(rc==SQLIT
165e0 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 E_OK || rc==S
165e1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 QLITE_NOMEM);.
165e2 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 assert(rc==SQLIT
165e3 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d E_OK || pMem-
165e4 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 >enc!=desiredEnc
165e5 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d );. assert(rc==
165e6 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 SQLITE_NOMEM ||
165e7 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 pMem->enc==desir
165e8 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e edEnc);. return
165e9 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f rc;.#endif.}../
165ea 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 *.** Make sure p
165eb 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f Mem->z points to
165ec 20 61 20 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f a writable allo
165ed 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 cation of at lea
165ee 73 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a st .** n bytes..
165ef 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d **.** If the mem
165f0 6f 72 79 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 ory cell current
165f1 6c 79 20 63 6f 6e 74 61 69 6e 73 20 73 74 72 69 ly contains stri
165f2 6e 67 20 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a ng or blob data.
165f3 2a 2a 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 ** and the third
165f4 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 argument passed
165f5 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
165f6 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 0a n is true, the .
165f7 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 ** current conte
165f8 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 nt of the cell i
165f9 73 20 70 72 65 73 65 72 76 65 64 2e 20 4f 74 68 s preserved. Oth
165fa 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a erwise, it may.*
165fb 2a 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 20 * be discarded.
165fc 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e .**.** This fun
165fd 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 4d ction sets the M
165fe 45 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 EM_Dyn flag and
165ff 63 6c 65 61 72 73 20 61 6e 79 20 78 44 65 6c 20 clears any xDel
16600 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 callback..** It
16601 61 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f also clears MEM_
16602 45 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 Ephem and MEM_St
16603 61 74 69 63 2e 20 49 66 20 74 68 65 20 70 72 65 atic. If the pre
16604 73 65 72 76 65 20 66 6c 61 67 20 69 73 20 0a 2a serve flag is .*
16605 2a 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e * not set, Mem.n
16606 20 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 is zeroed..*/.S
16607 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16608 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
16609 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 Grow(Mem *pMem,
1660a 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 int n, int prese
1660b 72 76 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 rve){. assert(
1660c 31 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 1 >=. ((pMem-
1660d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d >zMalloc && pMem
1660e 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d ->zMalloc==pMem-
1660f 3e 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20 >z) ? 1 : 0) +.
16610 20 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 (((pMem->flag
16611 73 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d s&MEM_Dyn)&&pMem
16612 2d 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20 30 29 ->xDel) ? 1 : 0)
16613 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e + . ((pMem->
16614 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 flags&MEM_Ephem)
16615 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 ? 1 : 0) + .
16616 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d ((pMem->flags&M
16617 45 4d 5f 53 74 61 74 69 63 29 20 3f 20 31 20 3a EM_Static) ? 1 :
16618 20 30 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 0). );.. if(
16619 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b 0a 20 n<32 ) n = 32;.
1661a 20 69 66 28 20 73 71 6c 69 74 65 33 44 62 4d 61 if( sqlite3DbMa
1661b 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e 64 llocSize(pMem->d
1661c 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 b, pMem->zMalloc
1661d 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 70 )<n ){. if( p
1661e 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 6d 2d reserve && pMem-
1661f 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f >z==pMem->zMallo
16620 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d c ){. pMem-
16621 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c >z = pMem->zMall
16622 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 oc = sqlite3DbRe
16623 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d 65 6d allocOrFree(pMem
16624 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6e ->db, pMem->z, n
16625 29 3b 0a 20 20 20 20 20 20 70 72 65 73 65 72 76 );. preserv
16626 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 e = 0;. }else
16627 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
16628 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 bFree(pMem->db,
16629 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a pMem->zMalloc);.
1662a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c pMem->zMal
1662b 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d loc = sqlite3DbM
1662c 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64 allocRaw(pMem->d
1662d 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d b, n);. }. }
1662e 0a 0a 20 20 69 66 28 20 70 72 65 73 65 72 76 65 .. if( preserve
1662f 20 26 26 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70 && pMem->z && p
16630 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 Mem->zMalloc &&
16631 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a pMem->z!=pMem->z
16632 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 6d 65 Malloc ){. me
16633 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c mcpy(pMem->zMall
16634 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d 65 oc, pMem->z, pMe
16635 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 m->n);. }. if(
16636 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d pMem->flags&MEM
16637 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78 44 _Dyn && pMem->xD
16638 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e el ){. pMem->
16639 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70 4d xDel((void *)(pM
1663a 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 20 em->z));. }..
1663b 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e pMem->z = pMem->
1663c 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28 20 70 zMalloc;. if( p
1663d 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 Mem->z==0 ){.
1663e 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1663f 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 EM_Null;. }else
16640 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 {. pMem->flag
16641 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d s &= ~(MEM_Ephem
16642 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 |MEM_Static);.
16643 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d }. pMem->xDel =
16644 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 4d 0;. return (pM
16645 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f 4f em->z ? SQLITE_O
16646 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d K : SQLITE_NOMEM
16647 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 );.}../*.** Make
16648 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 6f the given Mem o
16649 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 20 bject MEM_Dyn.
1664a 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
1664b 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74 68 make it so.** th
1664c 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20 42 at any TEXT or B
1664d 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 LOB content is s
1664e 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 tored in memory
1664f 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
16650 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20 74 malloc(). In t
16651 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f 77 his way, we know
16652 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 that the memory
16653 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a 2a is safe to be.*
16654 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 * overwritten or
16655 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 altered..**.**
16656 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
16657 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 53 on success or S
16658 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 6d QLITE_NOMEM if m
16659 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a alloc fails..*/.
1665a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1665b 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
1665c 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d mMakeWriteable(M
1665d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 em *pMem){. int
1665e 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d f;. assert( pM
1665f 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
16660 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
16661 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
16662 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 );. expandBlob
16663 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d (pMem);. f = pM
16664 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 em->flags;. if(
16665 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d (f&(MEM_Str|MEM
16666 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d _Blob)) && pMem-
16667 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f >z!=pMem->zMallo
16668 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c c ){. if( sql
16669 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1666a 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 pMem, pMem->n +
1666b 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 2, 1) ){. r
1666c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1666d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d EM;. }. pM
1666e 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d em->z[pMem->n] =
1666f 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 0;. pMem->z[
16670 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a pMem->n+1] = 0;.
16671 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
16672 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d |= MEM_Term;. }
16673 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
16674 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 E_OK;.}../*.** I
16675 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a f the given Mem*
16676 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c has a zero-fill
16677 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 ed tail, turn it
16678 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 into an ordinar
16679 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 y.** blob stored
1667a 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 in dynamically
1667b 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e allocated space.
1667c 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1667d 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
1667e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1667f 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
16680 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d emExpandBlob(Mem
16681 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 *pMem){. if( p
16682 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
16683 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 _Zero ){. int
16684 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 nByte;. asse
16685 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 rt( pMem->flags&
16686 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 MEM_Blob );.
16687 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
16688 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
16689 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
1668a 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
1668b 20 20 20 2f 2a 20 53 65 74 20 6e 42 79 74 65 20 /* Set nByte
1668c 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
1668d 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 bytes required
1668e 74 6f 20 73 74 6f 72 65 20 74 68 65 20 65 78 70 to store the exp
1668f 61 6e 64 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a 20 anded blob. */.
16690 20 20 20 6e 42 79 74 65 20 3d 20 70 4d 65 6d 2d nByte = pMem-
16691 3e 6e 20 2b 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a >n + pMem->u.i;.
16692 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 if( nByte<=0
16693 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 ){. nByte
16694 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 1;. }. i
16695 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
16696 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 mGrow(pMem, nByt
16697 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 e, 1) ){. r
16698 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
16699 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d EM;. }.. m
1669a 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 emset(&pMem->z[p
1669b 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d Mem->n], 0, pMem
1669c 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 70 4d 65 6d ->u.i);. pMem
1669d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 69 ->n += pMem->u.i
1669e 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
1669f 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f 7c s &= ~(MEM_Zero|
166a0 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a 20 MEM_Term);. }.
166a1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
166a2 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a K;.}.#endif.../*
166a3 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 .** Make sure th
166a4 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 20 5c e given Mem is \
166a5 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 u0000 terminated
166a6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
166a7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
166a8 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 dbeMemNulTermina
166a9 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 te(Mem *pMem){.
166aa 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
166ab 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
166ac 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
166ad 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
166ae 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 if( (pMem->flag
166af 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 s & MEM_Term)!=0
166b0 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 || (pMem->flags
166b1 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 & MEM_Str)==0 )
166b2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
166b3 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f 74 ITE_OK; /* Not
166b4 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 hing to do */.
166b5 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 }. if( sqlite3V
166b6 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c dbeMemGrow(pMem,
166b7 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20 29 pMem->n+2, 1) )
166b8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
166b9 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
166ba 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e pMem->z[pMem->n
166bb 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 7a ] = 0;. pMem->z
166bc 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b [pMem->n+1] = 0;
166bd 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c . pMem->flags |
166be 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 72 65 = MEM_Term;. re
166bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
166c0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45 4d }../*.** Add MEM
166c1 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74 20 _Str to the set
166c2 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f of representatio
166c3 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e ns for the given
166c4 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0a 2a Mem. Numbers.*
166c5 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20 * are converted
166c6 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 73 6e using sqlite3_sn
166c7 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e 76 65 printf(). Conve
166c8 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 6f 20 rting a BLOB to
166c9 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 61 a string.** is a
166ca 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 no-op..**.** Ex
166cb 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 6e 74 isting represent
166cc 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20 61 ations MEM_Int a
166cd 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 65 20 nd MEM_Real are
166ce 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 74 65 *not* invalidate
166cf 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f 4e d..**.** A MEM_N
166d0 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c 20 6e ull value will n
166d1 65 76 65 72 20 62 65 20 70 61 73 73 65 64 20 74 ever be passed t
166d2 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e o this function.
166d3 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
166d4 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 63 6f s.** used for co
166d5 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 73 20 nverting values
166d6 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 74 75 to text for retu
166d7 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 73 65 rning to the use
166d8 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a 20 73 r (i.e. via.** s
166d9 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
166da 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 6e 73 t()), or for ens
166db 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c 75 65 uring that value
166dc 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 73 20 s to be used as
166dd 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 61 72 btree.** keys ar
166de 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 74 68 e strings. In th
166df 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 61 20 e former case a
166e0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 NULL pointer is
166e1 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a 2a 20 returned the.**
166e2 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c 61 74 user and the lat
166e3 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 er is an interna
166e4 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 65 72 l programming er
166e5 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ror..*/.SQLITE_P
166e6 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
166e7 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 e3VdbeMemStringi
166e8 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e fy(Mem *pMem, in
166e9 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 t enc){. int rc
166ea 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
166eb 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e 66 int fg = pMem->f
166ec 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20 69 6e lags;. const in
166ed 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a 0a 20 t nByte = 32;..
166ee 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
166ef 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
166f0 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
166f1 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
166f2 20 61 73 73 65 72 74 28 20 21 28 66 67 26 4d 45 assert( !(fg&ME
166f3 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61 73 73 M_Zero) );. ass
166f4 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d 5f 53 ert( !(fg&(MEM_S
166f5 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 29 3b tr|MEM_Blob)) );
166f6 0a 20 20 61 73 73 65 72 74 28 20 66 67 26 28 4d . assert( fg&(M
166f7 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 EM_Int|MEM_Real)
166f8 20 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 );.. if( sqlit
166f9 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
166fa 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20 29 7b em, nByte, 0) ){
166fb 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
166fc 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 TE_NOMEM;. }..
166fd 20 2f 2a 20 46 6f 72 20 61 20 52 65 61 6c 20 6f /* For a Real o
166fe 72 20 49 6e 74 65 67 65 72 2c 20 75 73 65 20 73 r Integer, use s
166ff 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 qlite3_mprintf()
16700 20 74 6f 20 70 72 6f 64 75 63 65 20 74 68 65 20 to produce the
16701 55 54 46 2d 38 0a 20 20 2a 2a 20 73 74 72 69 6e UTF-8. ** strin
16702 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e g representation
16703 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 54 of the value. T
16704 68 65 6e 2c 20 69 66 20 74 68 65 20 72 65 71 75 hen, if the requ
16705 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 0a 20 20 ired encoding.
16706 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c 65 20 6f ** is UTF-16le o
16707 72 20 55 54 46 2d 31 36 62 65 20 64 6f 20 61 20 r UTF-16be do a
16708 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0a 20 20 2a translation.. *
16709 2a 20 0a 20 20 2a 2a 20 46 49 58 20 4d 45 3a 20 * . ** FIX ME:
1670a 49 74 20 77 6f 75 6c 64 20 62 65 20 62 65 74 74 It would be bett
1670b 65 72 20 69 66 20 73 71 6c 69 74 65 33 5f 73 6e er if sqlite3_sn
1670c 70 72 69 6e 74 66 28 29 20 63 6f 75 6c 64 20 64 printf() could d
1670d 6f 20 55 54 46 2d 31 36 2e 0a 20 20 2a 2f 0a 20 o UTF-16.. */.
1670e 20 69 66 28 20 66 67 20 26 20 4d 45 4d 5f 49 6e if( fg & MEM_In
1670f 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
16710 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 65 2c _snprintf(nByte,
16711 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 6c 6c 64 22 pMem->z, "%lld"
16712 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20 20 , pMem->u.i);.
16713 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
16714 74 28 20 66 67 20 26 20 4d 45 4d 5f 52 65 61 6c t( fg & MEM_Real
16715 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f );. sqlite3_
16716 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 65 2c 20 snprintf(nByte,
16717 70 4d 65 6d 2d 3e 7a 2c 20 22 25 21 2e 31 35 67 pMem->z, "%!.15g
16718 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 7d ", pMem->r);. }
16719 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72 . pMem->n = str
1671a 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 len(pMem->z);.
1671b 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
1671c 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d TE_UTF8;. pMem-
1671d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
1671e 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 r|MEM_Term;. sq
1671f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
16720 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e ncoding(pMem, en
16721 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b c);. return rc;
16722 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 .}../*.** Memory
16723 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 cell pMem conta
16724 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 ins the context
16725 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 of an aggregate
16726 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 function..** Thi
16727 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 s routine calls
16728 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74 the finalize met
16729 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e hod for that fun
1672a 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 ction. The.** r
1672b 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67 esult of the agg
1672c 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64 regate is stored
1672d 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e back into pMem.
1672e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
1672f 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68 LITE_ERROR if th
16730 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f e finalizer repo
16731 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 rts an error. S
16732 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 QLITE_OK.** othe
16733 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rwise..*/.SQLITE
16734 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
16735 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c ite3VdbeMemFinal
16736 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 ize(Mem *pMem, F
16737 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a uncDef *pFunc){.
16738 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
16739 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 46 75 6e E_OK;. if( pFun
1673a 63 20 26 26 20 70 46 75 6e 63 2d 3e 78 46 69 6e c && pFunc->xFin
1673b 61 6c 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c alize ){. sql
1673c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 ite3_context ctx
1673d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
1673e 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
1673f 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20 70 46 75 _Null)!=0 || pFu
16740 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 nc==pMem->u.pDef
16741 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
16742 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
16743 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
16744 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
16745 78 29 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 x) );. memset
16746 28 26 63 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 (&ctx, 0, sizeof
16747 28 63 74 78 29 29 3b 0a 20 20 20 20 63 74 78 2e (ctx));. ctx.
16748 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 s.flags = MEM_Nu
16749 6c 6c 3b 0a 20 20 20 20 63 74 78 2e 73 2e 64 62 ll;. ctx.s.db
1674a 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 20 = pMem->db;.
1674b 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d 65 6d ctx.pMem = pMem
1674c 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20 ;. ctx.pFunc
1674d 3d 20 70 46 75 6e 63 3b 0a 20 20 20 20 70 46 75 = pFunc;. pFu
1674e 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 28 26 63 nc->xFinalize(&c
1674f 74 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 tx);. assert(
16750 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 0==(pMem->flags
16751 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70 4d &MEM_Dyn) && !pM
16752 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20 em->xDel );.
16753 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d sqlite3DbFree(pM
16754 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d em->db, pMem->zM
16755 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 2a 70 4d 65 alloc);. *pMe
16756 6d 20 3d 20 63 74 78 2e 73 3b 0a 20 20 20 20 72 m = ctx.s;. r
16757 63 20 3d 20 28 63 74 78 2e 69 73 45 72 72 6f 72 c = (ctx.isError
16758 3f 53 51 4c 49 54 45 5f 45 52 52 4f 52 3a 53 51 ?SQLITE_ERROR:SQ
16759 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 7d 0a 20 20 LITE_OK);. }.
1675a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1675b 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 .** If the memor
1675c 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 y cell contains
1675d 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 74 a string value t
1675e 68 61 74 20 6d 75 73 74 20 62 65 20 66 72 65 65 hat must be free
1675f 64 20 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e 67 d by.** invoking
16760 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 63 61 6c an external cal
16761 6c 62 61 63 6b 2c 20 66 72 65 65 20 69 74 20 6e lback, free it n
16762 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 ow. Calling this
16763 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f 65 function.** doe
16764 73 20 6e 6f 74 20 66 72 65 65 20 61 6e 79 20 4d s not free any M
16765 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66 65 em.zMalloc buffe
16766 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
16767 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
16768 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 3VdbeMemReleaseE
16769 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 7b xternal(Mem *p){
1676a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 . assert( p->db
1676b 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
1676c 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
1676d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 >mutex) );. if(
1676e 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 p->flags&MEM_Ag
1676f 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 g ){. sqlite3
16770 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 VdbeMemFinalize(
16771 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 p, p->u.pDef);.
16772 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 assert( (p->f
16773 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d lags & MEM_Agg)=
16774 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 =0 );. sqlite
16775 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
16776 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 p);. }else if(
16777 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e p->flags&MEM_Dyn
16778 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20 && p->xDel ){.
16779 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 p->xDel((void
1677a 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 2d *)p->z);. p-
1677b 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 0a 7d >xDel = 0;. }.}
1677c 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
1677d 61 6e 79 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 any memory held
1677e 62 79 20 74 68 65 20 4d 65 6d 2e 20 54 68 69 73 by the Mem. This
1677f 20 6d 61 79 20 6c 65 61 76 65 20 74 68 65 20 4d may leave the M
16780 65 6d 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f em in an.** inco
16781 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2c 20 nsistent state,
16782 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 69 74 68 for example with
16783 20 28 4d 65 6d 2e 7a 3d 3d 30 29 20 61 6e 64 0a (Mem.z==0) and.
16784 2a 2a 20 28 4d 65 6d 2e 74 79 70 65 3d 3d 53 51 ** (Mem.type==SQ
16785 4c 49 54 45 5f 54 45 58 54 29 2e 0a 2a 2f 0a 53 LITE_TEXT)..*/.S
16786 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
16787 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
16788 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 mRelease(Mem *p)
16789 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d {. sqlite3VdbeM
1678a 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
1678b 6c 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 l(p);. sqlite3D
1678c 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e bFree(p->db, p->
1678d 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 70 2d 3e 7a zMalloc);. p->z
1678e 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a 4d 61 6c 6c = 0;. p->zMall
1678f 6f 63 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 44 65 oc = 0;. p->xDe
16790 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 l = 0;.}../*.**
16791 43 6f 6e 76 65 72 74 20 61 20 36 34 2d 62 69 74 Convert a 64-bit
16792 20 49 45 45 45 20 64 6f 75 62 6c 65 20 69 6e 74 IEEE double int
16793 6f 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 o a 64-bit signe
16794 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 d integer..** If
16795 20 74 68 65 20 64 6f 75 62 6c 65 20 69 73 20 74 the double is t
16796 6f 6f 20 6c 61 72 67 65 2c 20 72 65 74 75 72 6e oo large, return
16797 20 30 78 38 30 30 30 30 30 30 30 30 30 30 30 30 0x8000000000000
16798 30 30 30 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 000..**.** Most
16799 73 79 73 74 65 6d 73 20 61 70 70 65 61 72 20 74 systems appear t
1679a 6f 20 64 6f 20 74 68 69 73 20 73 69 6d 70 6c 79 o do this simply
1679b 20 62 79 20 61 73 73 69 67 6e 69 6e 67 0a 2a 2a by assigning.**
1679c 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 77 variables and w
1679d 69 74 68 6f 75 74 20 74 68 65 20 65 78 74 72 61 ithout the extra
1679e 20 72 61 6e 67 65 20 74 65 73 74 73 2e 20 20 42 range tests. B
1679f 75 74 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 20 ut.** there are
167a0 72 65 70 6f 72 74 73 20 74 68 61 74 20 77 69 6e reports that win
167a1 64 6f 77 73 20 74 68 72 6f 77 73 20 61 6e 20 65 dows throws an e
167a2 78 70 65 63 74 69 6f 6e 0a 2a 2a 20 69 66 20 74 xpection.** if t
167a3 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e he floating poin
167a4 74 20 76 61 6c 75 65 20 69 73 20 6f 75 74 20 6f t value is out o
167a5 66 20 72 61 6e 67 65 2e 20 28 53 65 65 20 74 69 f range. (See ti
167a6 63 6b 65 74 20 23 32 38 38 30 2e 29 0a 2a 2a 20 cket #2880.).**
167a7 42 65 63 61 75 73 65 20 77 65 20 64 6f 20 6e 6f Because we do no
167a8 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 75 6e 64 t completely und
167a9 65 72 73 74 61 6e 64 20 74 68 65 20 70 72 6f 62 erstand the prob
167aa 6c 65 6d 2c 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 lem, we will.**
167ab 74 61 6b 65 20 74 68 65 20 63 6f 6e 73 65 72 76 take the conserv
167ac 61 74 69 76 65 20 61 70 70 72 6f 61 63 68 20 61 ative approach a
167ad 6e 64 20 61 6c 77 61 79 73 20 64 6f 20 72 61 6e nd always do ran
167ae 67 65 20 74 65 73 74 73 0a 2a 2a 20 62 65 66 6f ge tests.** befo
167af 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 68 re attempting th
167b0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2f e conversion..*/
167b1 0a 73 74 61 74 69 63 20 69 36 34 20 64 6f 75 62 .static i64 doub
167b2 6c 65 54 6f 49 6e 74 36 34 28 64 6f 75 62 6c 65 leToInt64(double
167b3 20 72 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d r){. /*. ** M
167b4 61 6e 79 20 63 6f 6d 70 69 6c 65 72 73 20 77 65 any compilers we
167b5 20 65 6e 63 6f 75 6e 74 65 72 20 64 6f 20 6e 6f encounter do no
167b6 74 20 64 65 66 69 6e 65 20 63 6f 6e 73 74 61 6e t define constan
167b7 74 73 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 ts for the. **
167b8 6d 69 6e 69 6d 75 6d 20 61 6e 64 20 6d 61 78 69 minimum and maxi
167b9 6d 75 6d 20 36 34 2d 62 69 74 20 69 6e 74 65 67 mum 64-bit integ
167ba 65 72 73 2c 20 6f 72 20 74 68 65 79 20 64 65 66 ers, or they def
167bb 69 6e 65 20 74 68 65 6d 0a 20 20 2a 2a 20 69 6e ine them. ** in
167bc 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 20 20 41 consistently. A
167bd 6e 64 20 6d 61 6e 79 20 64 6f 20 6e 6f 74 20 75 nd many do not u
167be 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 22 4c nderstand the "L
167bf 4c 22 20 6e 6f 74 61 74 69 6f 6e 2e 0a 20 20 2a L" notation.. *
167c0 2a 20 53 6f 20 77 65 20 64 65 66 69 6e 65 20 6f * So we define o
167c1 75 72 20 6f 77 6e 20 73 74 61 74 69 63 20 63 6f ur own static co
167c2 6e 73 74 61 6e 74 73 20 68 65 72 65 20 75 73 69 nstants here usi
167c3 6e 67 20 6e 6f 74 68 69 6e 67 0a 20 20 2a 2a 20 ng nothing. **
167c4 6c 61 72 67 65 72 20 74 68 61 6e 20 61 20 33 32 larger than a 32
167c5 2d 62 69 74 20 69 6e 74 65 67 65 72 20 63 6f 6e -bit integer con
167c6 73 74 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 stant.. */. st
167c7 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20 6d atic const i64 m
167c8 61 78 49 6e 74 20 3d 20 4c 41 52 47 45 53 54 5f axInt = LARGEST_
167c9 49 4e 54 36 34 3b 0a 20 20 73 74 61 74 69 63 20 INT64;. static
167ca 63 6f 6e 73 74 20 69 36 34 20 6d 69 6e 49 6e 74 const i64 minInt
167cb 20 3d 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 = SMALLEST_INT6
167cc 34 3b 0a 0a 20 20 69 66 28 20 72 3c 28 64 6f 75 4;.. if( r<(dou
167cd 62 6c 65 29 6d 69 6e 49 6e 74 20 29 7b 0a 20 20 ble)minInt ){.
167ce 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b return minInt;
167cf 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 3e 28 . }else if( r>(
167d0 64 6f 75 62 6c 65 29 6d 61 78 49 6e 74 20 29 7b double)maxInt ){
167d1 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 . return minI
167d2 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 nt;. }else{.
167d3 20 72 65 74 75 72 6e 20 28 69 36 34 29 72 3b 0a return (i64)r;.
167d4 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
167d5 75 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 urn some kind of
167d6 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 integer value w
167d7 68 69 63 68 20 69 73 20 74 68 65 20 62 65 73 74 hich is the best
167d8 20 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 we can do.** at
167d9 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 representing th
167da 65 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d e value that *pM
167db 65 6d 20 64 65 73 63 72 69 62 65 73 20 61 73 20 em describes as
167dc 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 an integer..** I
167dd 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 f pMem is an int
167de 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 76 eger, then the v
167df 61 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20 20 alue is exact.
167e0 49 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 If pMem is.** a
167e1 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 floating-point t
167e2 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
167e3 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 69 6e turned is the in
167e4 74 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 teger part..** I
167e5 66 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 f pMem is a stri
167e6 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e ng or blob, then
167e7 20 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 we make an atte
167e8 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a mpt to convert.*
167e9 2a 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 * it into a inte
167ea 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 ger and return t
167eb 68 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 69 73 hat. If pMem is
167ec 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e NULL, return 0.
167ed 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 .**.** If pMem i
167ee 73 20 61 20 73 74 72 69 6e 67 2c 20 69 74 73 20 s a string, its
167ef 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20 62 encoding might b
167f0 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 e changed..*/.SQ
167f1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 LITE_PRIVATE i64
167f2 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
167f3 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b alue(Mem *pMem){
167f4 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 . int flags;.
167f5 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
167f6 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
167f7 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
167f8 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
167f9 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c flags = pMem->fl
167fa 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 ags;. if( flags
167fb 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 & MEM_Int ){.
167fc 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 return pMem->u
167fd 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 .i;. }else if(
167fe 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c flags & MEM_Real
167ff 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 ){. return d
16800 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 oubleToInt64(pMe
16801 6d 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 m->r);. }else i
16802 66 28 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f f( flags & (MEM_
16803 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b Str|MEM_Blob) ){
16804 0a 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a . i64 value;.
16805 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
16806 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 |= MEM_Str;.
16807 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 if( sqlite3VdbeC
16808 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d hangeEncoding(pM
16809 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 em, SQLITE_UTF8)
1680a 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 . || sqlit
1680b 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d e3VdbeMemNulTerm
1680c 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 inate(pMem) ){.
1680d 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
1680e 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1680f 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 pMem->z );.
16810 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d sqlite3Atoi64(pM
16811 65 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a em->z, &value);.
16812 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 return value
16813 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
16814 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a eturn 0;. }.}..
16815 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
16816 20 62 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 best representa
16817 74 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 tion of pMem tha
16818 74 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 t we can get int
16819 6f 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 o a.** double.
1681a 49 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 If pMem is alrea
1681b 64 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 dy a double or a
1681c 6e 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 n integer, retur
1681d 6e 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 n its.** value.
1681e 20 49 66 20 69 74 20 69 73 20 61 20 73 74 72 69 If it is a stri
1681f 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 ng or blob, try
16820 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f to convert it to
16821 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 a double..** If
16822 20 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 it is a NULL, r
16823 65 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 eturn 0.0..*/.SQ
16824 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 LITE_PRIVATE dou
16825 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 ble sqlite3VdbeR
16826 65 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d ealValue(Mem *pM
16827 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 em){. assert( p
16828 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
16829 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1682a 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
1682b 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d ) );. if( pMem-
1682c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
1682d 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 l ){. return
1682e 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c 73 65 pMem->r;. }else
1682f 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
16830 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 & MEM_Int ){.
16831 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 return (double
16832 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 )pMem->u.i;. }e
16833 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c lse if( pMem->fl
16834 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d ags & (MEM_Str|M
16835 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 EM_Blob) ){.
16836 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 30 2e 30 double val = 0.0
16837 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
16838 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 s |= MEM_Str;.
16839 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1683a 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
1683b 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 pMem, SQLITE_UTF
1683c 38 29 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 8). || sql
1683d 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
1683e 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b rminate(pMem) ){
1683f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 2e . return 0.
16840 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 0;. }. ass
16841 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a ert( pMem->z );.
16842 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 sqlite3AtoF(
16843 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a pMem->z, &val);.
16844 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a return val;.
16845 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
16846 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 7d 0a 0a urn 0.0;. }.}..
16847 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73 74 /*.** The MEM st
16848 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65 61 ructure is alrea
16849 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 20 dy a MEM_Real.
1684a 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b 65 Try to also make
1684b 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 it a.** MEM_Int
1684c 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53 if we can..*/.S
1684d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1684e 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e id sqlite3VdbeIn
1684f 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65 tegerAffinity(Me
16850 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 m *pMem){. asse
16851 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 rt( pMem->flags
16852 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 & MEM_Real );.
16853 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
16854 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
16855 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
16856 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 db->mutex) );..
16857 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75 pMem->u.i = dou
16858 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d bleToInt64(pMem-
16859 3e 72 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d >r);. if( pMem-
1685a 3e 72 3d 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d >r==(double)pMem
1685b 2d 3e 75 2e 69 20 29 7b 0a 20 20 20 20 70 4d 65 ->u.i ){. pMe
1685c 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f m->flags |= MEM_
1685d 49 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 Int;. }.}..stat
1685e 69 63 20 76 6f 69 64 20 73 65 74 54 79 70 65 46 ic void setTypeF
1685f 6c 61 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 lag(Mem *pMem, i
16860 6e 74 20 66 29 7b 0a 20 20 4d 65 6d 53 65 74 54 nt f){. MemSetT
16861 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 66 29 ypeFlag(pMem, f)
16862 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
16863 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65 20 rt pMem to type
16864 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c 69 integer. Invali
16865 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72 date any prior r
16866 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a epresentations..
16867 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
16868 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
16869 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d eMemIntegerify(M
1686a 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 em *pMem){. ass
1686b 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 ert( pMem->db==0
1686c 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
1686d 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d x_held(pMem->db-
1686e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 >mutex) );. pMe
1686f 6d 2d 3e 75 2e 69 20 3d 20 73 71 6c 69 74 65 33 m->u.i = sqlite3
16870 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 4d 65 VdbeIntValue(pMe
16871 6d 29 3b 0a 20 20 73 65 74 54 79 70 65 46 6c 61 m);. setTypeFla
16872 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 g(pMem, MEM_Int)
16873 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
16874 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
16875 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 onvert pMem so t
16876 68 61 74 20 69 74 20 69 73 20 6f 66 20 74 79 70 hat it is of typ
16877 65 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 e MEM_Real..** I
16878 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 nvalidate any pr
16879 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 ior representati
1687a 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ons..*/.SQLITE_P
1687b 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1687c 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 e3VdbeMemRealify
1687d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 (Mem *pMem){. a
1687e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
1687f 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
16880 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
16881 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 b->mutex) );. p
16882 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65 33 Mem->r = sqlite3
16883 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d VdbeRealValue(pM
16884 65 6d 29 3b 0a 20 20 73 65 74 54 79 70 65 46 6c em);. setTypeFl
16885 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 ag(pMem, MEM_Rea
16886 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c l);. return SQL
16887 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
16888 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f Convert pMem so
16889 20 74 68 61 74 20 69 74 20 68 61 73 20 74 79 70 that it has typ
1688a 65 73 20 4d 45 4d 5f 52 65 61 6c 20 6f 72 20 4d es MEM_Real or M
1688b 45 4d 5f 49 6e 74 20 6f 72 20 62 6f 74 68 2e 0a EM_Int or both..
1688c 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6e ** Invalidate an
1688d 79 20 70 72 69 6f 72 20 72 65 70 72 65 73 65 6e y prior represen
1688e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 tations..*/.SQLI
1688f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16890 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d qlite3VdbeMemNum
16891 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 erify(Mem *pMem)
16892 7b 0a 20 20 64 6f 75 62 6c 65 20 72 31 2c 20 72 {. double r1, r
16893 32 3b 0a 20 20 69 36 34 20 69 3b 0a 20 20 61 73 2;. i64 i;. as
16894 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
16895 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 gs & (MEM_Int|ME
16896 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e 75 6c 6c 29 M_Real|MEM_Null)
16897 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 )==0 );. assert
16898 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
16899 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 (MEM_Blob|MEM_S
1689a 74 72 29 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 tr))!=0 );. ass
1689b 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 ert( pMem->db==0
1689c 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
1689d 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d x_held(pMem->db-
1689e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 31 20 >mutex) );. r1
1689f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 = sqlite3VdbeRea
168a0 6c 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 lValue(pMem);.
168a1 69 20 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 i = doubleToInt6
168a2 34 28 72 31 29 3b 0a 20 20 72 32 20 3d 20 28 64 4(r1);. r2 = (d
168a3 6f 75 62 6c 65 29 69 3b 0a 20 20 69 66 28 20 72 ouble)i;. if( r
168a4 31 3d 3d 72 32 20 29 7b 0a 20 20 20 20 73 71 6c 1==r2 ){. sql
168a5 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
168a6 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 7d erify(pMem);. }
168a7 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e else{. pMem->
168a8 72 20 3d 20 72 31 3b 0a 20 20 20 20 73 65 74 54 r = r1;. setT
168a9 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 ypeFlag(pMem, ME
168aa 4d 5f 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 M_Real);. }. r
168ab 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
168ac 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
168ad 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 any previous va
168ae 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 lue and set the
168af 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 value stored in
168b0 2a 70 4d 65 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a *pMem to NULL..*
168b1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
168b2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
168b3 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 eMemSetNull(Mem
168b4 2a 70 4d 65 6d 29 7b 0a 20 20 73 65 74 54 79 70 *pMem){. setTyp
168b5 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f eFlag(pMem, MEM_
168b6 4e 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 Null);. pMem->t
168b7 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c ype = SQLITE_NUL
168b8 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 L;.}../*.** Dele
168b9 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 te any previous
168ba 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 value and set th
168bb 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 e value to be a
168bc 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a BLOB of length.*
168bd 2a 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 * n containing a
168be 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c ll zeros..*/.SQL
168bf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
168c0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
168c1 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a etZeroBlob(Mem *
168c2 70 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 pMem, int n){.
168c3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
168c4 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 73 lease(pMem);. s
168c5 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c etTypeFlag(pMem,
168c6 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 70 4d MEM_Blob);. pM
168c7 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
168c8 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 Blob|MEM_Zero;.
168c9 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
168ca 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 LITE_BLOB;. pMe
168cb 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 m->n = 0;. if(
168cc 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 n<0 ) n = 0;. p
168cd 4d 65 6d 2d 3e 75 2e 69 20 3d 20 6e 3b 0a 20 20 Mem->u.i = n;.
168ce 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
168cf 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a 0a 2a TE_UTF8;.}../*.*
168d0 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 * Delete any pre
168d1 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 vious value and
168d2 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 set the value st
168d3 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f ored in *pMem to
168d4 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 val,.** manifes
168d5 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e 0a t type INTEGER..
168d6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
168d7 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
168d8 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65 beMemSetInt64(Me
168d9 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 6c m *pMem, i64 val
168da 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ){. sqlite3Vdbe
168db 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 MemRelease(pMem)
168dc 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 ;. pMem->u.i =
168dd 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 val;. pMem->fla
168de 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
168df 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
168e0 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a 0a ITE_INTEGER;.}..
168e1 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 /*.** Delete any
168e2 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
168e3 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 and set the valu
168e4 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 e stored in *pMe
168e5 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e m to val,.** man
168e6 69 66 65 73 74 20 74 79 70 65 20 52 45 41 4c 2e ifest type REAL.
168e7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
168e8 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
168e9 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 dbeMemSetDouble(
168ea 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62 6c Mem *pMem, doubl
168eb 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20 73 71 e val){. if( sq
168ec 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 29 20 lite3IsNaN(val)
168ed 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
168ee 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 beMemSetNull(pMe
168ef 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 m);. }else{.
168f0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
168f1 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
168f2 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c 3b pMem->r = val;
168f3 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
168f4 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 = MEM_Real;.
168f5 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
168f6 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a LITE_FLOAT;. }.
168f7 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
168f8 74 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d 20 true if the Mem
168f9 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20 object contains
168fa 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 74 a TEXT or BLOB t
168fb 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 hat is.** too la
168fc 72 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a 65 rge - whose size
168fd 20 65 78 63 65 65 64 73 20 53 51 4c 49 54 45 5f exceeds SQLITE_
168fe 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 MAX_LENGTH..*/.S
168ff 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16900 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
16901 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a TooBig(Mem *p){.
16902 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 assert( p->db!
16903 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 =0 );. if( p->f
16904 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c lags & (MEM_Str|
16905 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 MEM_Blob) ){.
16906 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 int n = p->n;.
16907 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 if( p->flags
16908 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
16909 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 69 3b n += p->u.i;
1690a 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
1690b 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d 69 n n>p->db->aLimi
1690c 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1690d 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72 65 ENGTH];. }. re
1690e 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a turn 0; .}../*.*
1690f 2a 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63 74 * Size of struct
16910 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 69 Mem not includi
16911 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c ng the Mem.zMall
16912 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 oc member..*/.#d
16913 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 5a efine MEMCELLSIZ
16914 45 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28 4d E (size_t)(&(((M
16915 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63 em *)0)->zMalloc
16916 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 ))../*.** Make a
16917 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f n shallow copy o
16918 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f f pFrom into pTo
16919 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 . Prior content
1691a 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65 20 s of.** pTo are
1691b 66 72 65 65 64 2e 20 20 54 68 65 20 70 46 72 6f freed. The pFro
1691c 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e 6f m->z field is no
1691d 74 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20 49 t duplicated. I
1691e 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 73 f.** pFrom->z is
1691f 20 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f 2d used, then pTo-
16920 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 >z points to the
16921 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 70 same thing as p
16922 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66 From->z.** and f
16923 6c 61 67 73 20 67 65 74 73 20 73 72 63 54 79 70 lags gets srcTyp
16924 65 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45 70 e (either MEM_Ep
16925 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 hem or MEM_Stati
16926 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 c)..*/.SQLITE_PR
16927 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
16928 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
16929 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 Copy(Mem *pTo, c
1692a 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c onst Mem *pFrom,
1692b 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a 20 int srcType){.
1692c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1692d 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 eleaseExternal(p
1692e 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 To);. memcpy(pT
1692f 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c o, pFrom, MEMCEL
16930 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78 LSIZE);. pTo->x
16931 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 Del = 0;. if( (
16932 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d pFrom->flags&MEM
16933 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f _Dyn)!=0 || pFro
16934 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 m->z==pFrom->zMa
16935 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d lloc ){. pTo-
16936 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
16937 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d Dyn|MEM_Static|M
16938 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 61 EM_Ephem);. a
16939 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d 3d ssert( srcType==
1693a 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63 MEM_Ephem || src
1693b 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63 Type==MEM_Static
1693c 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 );. pTo->fla
1693d 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20 gs |= srcType;.
1693e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 }.}../*.** Make
1693f 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20 a full copy of
16940 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 pFrom into pTo.
16941 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 Prior contents
16942 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72 of pTo are.** fr
16943 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 63 eed before the c
16944 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a opy is made..*/.
16945 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16946 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
16947 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 mCopy(Mem *pTo,
16948 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d const Mem *pFrom
16949 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1694a 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 LITE_OK;.. sqli
1694b 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1694c 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a eExternal(pTo);.
1694d 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 memcpy(pTo, pF
1694e 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 rom, MEMCELLSIZE
1694f 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 );. pTo->flags
16950 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 &= ~MEM_Dyn;..
16951 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28 if( pTo->flags&(
16952 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
16953 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d ) ){. if( 0==
16954 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 (pFrom->flags&ME
16955 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20 M_Static) ){.
16956 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d pTo->flags |=
16957 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 MEM_Ephem;.
16958 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
16959 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
1695a 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 le(pTo);. }.
1695b 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
1695c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 .}../*.** Transf
1695d 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 er the contents
1695e 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e of pFrom to pTo.
1695f 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61 Any existing va
16960 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a lue in pTo is.**
16961 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d freed. If pFrom
16962 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65 contains epheme
16963 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79 ral data, a copy
16964 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 is made..**.**
16965 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61 pFrom contains a
16966 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 n SQL NULL when
16967 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
16968 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f urns..*/.SQLITE_
16969 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1696a 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
1696b 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 Mem *pTo, Mem *p
1696c 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 From){. assert(
1696d 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c pFrom->db==0 ||
1696e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1696f 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d eld(pFrom->db->m
16970 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
16971 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c t( pTo->db==0 ||
16972 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
16973 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 eld(pTo->db->mut
16974 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
16975 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c pFrom->db==0 ||
16976 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 pTo->db==0 || p
16977 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 From->db==pTo->d
16978 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56 b );.. sqlite3V
16979 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54 dbeMemRelease(pT
1697a 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f o);. memcpy(pTo
1697b 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 , pFrom, sizeof(
1697c 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e Mem));. pFrom->
1697d 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1697e 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 ;. pFrom->xDel
1697f 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d = 0;. pFrom->zM
16980 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a alloc = 0;.}../*
16981 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 .** Change the v
16982 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f alue of a Mem to
16983 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20 be a string or
16984 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 a BLOB..**.** Th
16985 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d e memory managem
16986 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 70 ent strategy dep
16987 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 ends on the valu
16988 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a e of the xDel.**
16989 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74 parameter. If t
1698a 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 he value passed
1698b 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 is SQLITE_TRANSI
1698c 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a ENT, then the .*
1698d 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69 * string is copi
1698e 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69 ed into a (possi
1698f 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 75 bly existing) bu
16990 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20 ffer managed by
16991 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 the .** Mem stru
16992 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 cture. Otherwise
16993 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62 , any existing b
16994 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20 61 uffer is freed a
16995 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 nd the.** pointe
16996 72 20 63 6f 70 69 65 64 2e 0a 2a 2f 0a 53 51 4c r copied..*/.SQL
16997 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
16998 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
16999 74 53 74 72 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 tStr(. Mem *pMe
1699a 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d m, /* M
1699b 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 65 emory cell to se
1699c 74 20 74 6f 20 73 74 72 69 6e 67 20 76 61 6c 75 t to string valu
1699d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
1699e 72 20 2a 7a 2c 20 20 20 20 20 20 2f 2a 20 53 74 r *z, /* St
1699f 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a ring pointer */.
169a0 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 int n,
169a1 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 /* Bytes i
169a2 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67 n string, or neg
169a3 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e ative */. u8 en
169a4 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f c, /
169a5 2a 20 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e * Encoding of z.
169a6 20 20 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0 for BLOBs */
169a7 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
169a8 76 6f 69 64 2a 29 20 2f 2a 20 44 65 73 74 72 75 void*) /* Destru
169a9 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f ctor function */
169aa 0a 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 .){. int nByte
169ab 3d 20 6e 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 = n; /* New
169ac 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d value for pMem-
169ad 3e 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d >n */. int iLim
169ae 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d it; /* M
169af 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 aximum allowed s
169b0 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 73 69 tring or blob si
169b1 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 ze */. int flag
169b2 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e s = 0; /* N
169b3 65 77 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 ew value for pMe
169b4 6d 2d 3e 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 m->flags */.. a
169b5 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
169b6 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
169b7 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
169b8 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 b->mutex) );..
169b9 2f 2a 20 49 66 20 7a 20 69 73 20 61 20 4e 55 4c /* If z is a NUL
169ba 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65 74 20 70 L pointer, set p
169bb 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 Mem to contain a
169bc 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 n SQL NULL. */.
169bd 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20 20 73 if( !z ){. s
169be 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
169bf 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 20 Null(pMem);.
169c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
169c1 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d 65 ;. }.. if( pMe
169c2 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 69 4c 69 m->db ){. iLi
169c3 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 62 2d 3e mit = pMem->db->
169c4 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
169c5 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d MIT_LENGTH];. }
169c6 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69 74 else{. iLimit
169c7 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 = SQLITE_MAX_LE
169c8 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66 6c 61 67 NGTH;. }. flag
169c9 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d 45 4d 5f s = (enc==0?MEM_
169ca 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29 3b 0a 20 Blob:MEM_Str);.
169cb 20 69 66 28 20 6e 42 79 74 65 3c 30 20 29 7b 0a if( nByte<0 ){.
169cc 20 20 20 20 61 73 73 65 72 74 28 20 65 6e 63 21 assert( enc!
169cd 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 65 6e =0 );. if( en
169ce 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 c==SQLITE_UTF8 )
169cf 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e 42 79 74 {. for(nByt
169d0 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 4c 69 6d e=0; nByte<=iLim
169d1 69 74 20 26 26 20 7a 5b 6e 42 79 74 65 5d 3b 20 it && z[nByte];
169d2 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20 20 20 7d nByte++){}. }
169d3 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f 72 28 else{. for(
169d4 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d nByte=0; nByte<=
169d5 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b 6e 42 79 iLimit && (z[nBy
169d6 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65 2b 31 5d te] | z[nByte+1]
169d7 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b 7d 0a 20 ); nByte+=2){}.
169d8 20 20 20 7d 0a 20 20 20 20 66 6c 61 67 73 20 7c }. flags |
169d9 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a = MEM_Term;. }.
169da 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . /* The follow
169db 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 ing block sets t
169dc 68 65 20 6e 65 77 20 76 61 6c 75 65 73 20 6f 66 he new values of
169dd 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 6d 2e 78 Mem.z and Mem.x
169de 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20 61 6c 73 Del. It. ** als
169df 6f 20 73 65 74 73 20 61 20 66 6c 61 67 20 69 6e o sets a flag in
169e0 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 local variable
169e1 22 66 6c 61 67 73 22 20 74 6f 20 69 6e 64 69 63 "flags" to indic
169e2 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 0a 20 ate the memory.
169e3 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e 74 20 28 ** management (
169e4 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 6e 20 6f one of MEM_Dyn o
169e5 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a 20 r MEM_Static)..
169e6 20 2a 2f 0a 20 20 69 66 28 20 78 44 65 6c 3d 3d */. if( xDel==
169e7 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
169e8 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c ){. int nAll
169e9 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 oc = nByte;.
169ea 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 54 65 if( flags&MEM_Te
169eb 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e 41 6c 6c rm ){. nAll
169ec 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 oc += (enc==SQLI
169ed 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 TE_UTF8?1:2);.
169ee 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 42 79 74 }. if( nByt
169ef 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 e>iLimit ){.
169f0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
169f1 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d 0a 20 20 TOOBIG;. }.
169f2 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
169f3 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e eMemGrow(pMem, n
169f4 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a 20 20 20 Alloc, 0) ){.
169f5 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
169f6 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
169f7 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a memcpy(pMem->z
169f8 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b 0a 20 20 , z, nAlloc);.
169f9 7d 65 6c 73 65 20 69 66 28 20 78 44 65 6c 3d 3d }else if( xDel==
169fa 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 SQLITE_DYNAMIC )
169fb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
169fc 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d eMemRelease(pMem
169fd 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 );. pMem->zMa
169fe 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 20 3d lloc = pMem->z =
169ff 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 (char *)z;.
16a00 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a pMem->xDel = 0;.
16a01 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
16a02 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
16a03 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d se(pMem);. pM
16a04 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 em->z = (char *)
16a05 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 z;. pMem->xDe
16a06 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 66 6c l = xDel;. fl
16a07 61 67 73 20 7c 3d 20 28 28 78 44 65 6c 3d 3d 53 ags |= ((xDel==S
16a08 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3f 4d 45 QLITE_STATIC)?ME
16a09 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f 44 79 6e M_Static:MEM_Dyn
16a0a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 );. }. if( nBy
16a0b 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 te>iLimit ){.
16a0c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 54 return SQLITE_T
16a0d 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 70 4d OOBIG;. }.. pM
16a0e 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20 em->n = nByte;.
16a0f 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 66 pMem->flags = f
16a10 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e lags;. pMem->en
16a11 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 c = (enc==0 ? SQ
16a12 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63 29 LITE_UTF8 : enc)
16a13 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d ;. pMem->type =
16a14 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49 54 (enc==0 ? SQLIT
16a15 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45 5f E_BLOB : SQLITE_
16a16 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66 20 TEXT);..#ifndef
16a17 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
16a18 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 6. if( pMem->en
16a19 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 c!=SQLITE_UTF8 &
16a1a 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d & sqlite3VdbeMem
16a1b 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29 20 HandleBom(pMem)
16a1c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
16a1d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
16a1e 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
16a1f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
16a20 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 *.** Compare the
16a21 20 76 61 6c 75 65 73 20 63 6f 6e 74 61 69 6e 65 values containe
16a22 64 20 62 79 20 74 68 65 20 74 77 6f 20 6d 65 6d d by the two mem
16a23 6f 72 79 20 63 65 6c 6c 73 2c 20 72 65 74 75 72 ory cells, retur
16a24 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 74 69 76 65 ning.** negative
16a25 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 , zero or positi
16a26 76 65 20 69 66 20 70 4d 65 6d 31 20 69 73 20 6c ve if pMem1 is l
16a27 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 ess than, equal
16a28 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 0a 2a to, or greater.*
16a29 2a 20 74 68 61 6e 20 70 4d 65 6d 32 2e 20 53 6f * than pMem2. So
16a2a 72 74 69 6e 67 20 6f 72 64 65 72 20 69 73 20 4e rting order is N
16a2b 55 4c 4c 27 73 20 66 69 72 73 74 2c 20 66 6f 6c ULL's first, fol
16a2c 6c 6f 77 65 64 20 62 79 20 6e 75 6d 62 65 72 73 lowed by numbers
16a2d 20 28 69 6e 74 65 67 65 72 73 0a 2a 2a 20 61 6e (integers.** an
16a2e 64 20 72 65 61 6c 73 29 20 73 6f 72 74 65 64 20 d reals) sorted
16a2f 6e 75 6d 65 72 69 63 61 6c 6c 79 2c 20 66 6f 6c numerically, fol
16a30 6c 6f 77 65 64 20 62 79 20 74 65 78 74 20 6f 72 lowed by text or
16a31 64 65 72 65 64 20 62 79 20 74 68 65 20 63 6f 6c dered by the col
16a32 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e lating.** sequen
16a33 63 65 20 70 43 6f 6c 6c 20 61 6e 64 20 66 69 6e ce pColl and fin
16a34 61 6c 6c 79 20 62 6c 6f 62 27 73 20 6f 72 64 65 ally blob's orde
16a35 72 65 64 20 62 79 20 6d 65 6d 63 6d 70 28 29 2e red by memcmp().
16a36 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e 55 4c 4c 20 .**.** Two NULL
16a37 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 values are consi
16a38 64 65 72 65 64 20 65 71 75 61 6c 20 62 79 20 74 dered equal by t
16a39 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f his function..*/
16a3a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16a3b 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f int sqlite3MemCo
16a3c 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 20 mpare(const Mem
16a3d 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 74 20 4d 65 *pMem1, const Me
16a3e 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f 6e 73 74 20 m *pMem2, const
16a3f 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b CollSeq *pColl){
16a40 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 . int rc;. int
16a41 20 66 31 2c 20 66 32 3b 0a 20 20 69 6e 74 20 63 f1, f2;. int c
16a42 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 3b 0a 0a ombined_flags;..
16a43 20 20 2f 2a 20 49 6e 74 65 72 63 68 61 6e 67 65 /* Interchange
16a44 20 70 4d 65 6d 31 20 61 6e 64 20 70 4d 65 6d 32 pMem1 and pMem2
16a45 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e if the collatin
16a46 67 20 73 65 71 75 65 6e 63 65 20 73 70 65 63 69 g sequence speci
16a47 66 69 65 73 0a 20 20 2a 2a 20 44 45 53 43 20 6f fies. ** DESC o
16a48 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 31 20 rder.. */. f1
16a49 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 67 73 3b 0a = pMem1->flags;.
16a4a 20 20 66 32 20 3d 20 70 4d 65 6d 32 2d 3e 66 6c f2 = pMem2->fl
16a4b 61 67 73 3b 0a 20 20 63 6f 6d 62 69 6e 65 64 5f ags;. combined_
16a4c 66 6c 61 67 73 20 3d 20 66 31 7c 66 32 3b 0a 20 flags = f1|f2;.
16a4d 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c . /* If one val
16a4e 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20 69 ue is NULL, it i
16a4f 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 s less than the
16a50 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 20 76 other. If both v
16a51 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 20 4e alues. ** are N
16a52 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 0a 20 ULL, return 0..
16a53 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e */. if( combin
16a54 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c ed_flags&MEM_Nul
16a55 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 l ){. return
16a56 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 2d 20 (f2&MEM_Null) -
16a57 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 (f1&MEM_Null);.
16a58 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 }.. /* If one
16a59 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d 62 65 value is a numbe
16a5a 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 r and the other
16a5b 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 6d 62 is not, the numb
16a5c 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a er is less.. **
16a5d 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e 75 6d If both are num
16a5e 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 61 73 bers, compare as
16a5f 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 69 73 reals if one is
16a60 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 20 69 a real, or as i
16a61 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 66 20 ntegers. ** if
16a62 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 20 both values are
16a63 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 integers.. */.
16a64 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c if( combined_fl
16a65 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d ags&(MEM_Int|MEM
16a66 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 69 66 _Real) ){. if
16a67 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e 74 7c ( !(f1&(MEM_Int|
16a68 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20 MEM_Real)) ){.
16a69 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
16a6a 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 66 32 }. if( !(f2
16a6b 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 &(MEM_Int|MEM_Re
16a6c 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 al)) ){. re
16a6d 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 turn -1;. }.
16a6e 20 20 20 69 66 28 20 28 66 31 20 26 20 66 32 20 if( (f1 & f2
16a6f 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b & MEM_Int)==0 ){
16a70 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 31 . double r1
16a71 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 28 20 , r2;. if(
16a72 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 (f1&MEM_Real)==0
16a73 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d ){. r1 =
16a74 20 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 pMem1->u.i;.
16a75 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
16a76 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b r1 = pMem1->r;
16a77 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
16a78 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29 f( (f2&MEM_Real)
16a79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
16a7a 32 20 3d 20 70 4d 65 6d 32 2d 3e 75 2e 69 3b 0a 2 = pMem2->u.i;.
16a7b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
16a7c 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32 2d r2 = pMem2-
16a7d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 >r;. }.
16a7e 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 72 65 if( r1<r2 ) re
16a7f 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 turn -1;. i
16a80 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 75 72 f( r1>r2 ) retur
16a81 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 n 1;. retur
16a82 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a n 0;. }else{.
16a83 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 31 assert( f1
16a84 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 &MEM_Int );.
16a85 20 20 61 73 73 65 72 74 28 20 66 32 26 4d 45 4d assert( f2&MEM
16a86 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 69 66 _Int );. if
16a87 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c 20 70 ( pMem1->u.i < p
16a88 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75 Mem2->u.i ) retu
16a89 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 66 28 rn -1;. if(
16a8a 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 70 4d pMem1->u.i > pM
16a8b 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75 72 em2->u.i ) retur
16a8c 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 n 1;. retur
16a8d 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a n 0;. }. }..
16a8e 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 /* If one valu
16a8f 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 61 6e e is a string an
16a90 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20 61 d the other is a
16a91 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 69 6e blob, the strin
16a92 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a 20 g is less.. **
16a93 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 72 69 If both are stri
16a94 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 73 69 ngs, compare usi
16a95 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 ng the collating
16a96 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f functions.. */
16a97 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f . if( combined_
16a98 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b flags&MEM_Str ){
16a99 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 4d . if( (f1 & M
16a9a 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 EM_Str)==0 ){.
16a9b 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
16a9c 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 32 20 }. if( (f2
16a9d 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b & MEM_Str)==0 ){
16a9e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 . return -1
16a9f 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 ;. }.. ass
16aa0 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d ert( pMem1->enc=
16aa1 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b 0a 20 =pMem2->enc );.
16aa2 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31 assert( pMem1
16aa3 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 ->enc==SQLITE_UT
16aa4 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 F8 || .
16aa5 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 pMem1->enc==S
16aa6 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c 7c QLITE_UTF16LE ||
16aa7 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c pMem1->enc==SQL
16aa8 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 0a ITE_UTF16BE );..
16aa9 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 /* The colla
16aaa 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6d 75 tion sequence mu
16aab 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 74 st be defined at
16aac 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 76 65 this point, eve
16aad 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 n if. ** the
16aae 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 68 65 user deletes the
16aaf 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
16ab0 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 76 64 nce after the vd
16ab1 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a 20 20 be program is.
16ab2 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 28 74 ** compiled (t
16ab3 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c 77 61 his was not alwa
16ab4 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a 20 20 ys the case)..
16ab5 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
16ab6 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f 6c 6c !pColl || pColl
16ab7 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 20 69 ->xCmp );.. i
16ab8 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 f( pColl ){.
16ab9 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 if( pMem1->enc
16aba 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a ==pColl->enc ){.
16abb 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 /* The s
16abc 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 65 61 trings are alrea
16abd 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 dy in the correc
16abe 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 61 6c t encoding. Cal
16abf 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a l the. **
16ac0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 comparison func
16ac1 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 2a 2f tion directly */
16ac2 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
16ac3 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c pColl->xCmp(pCol
16ac4 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 2d 3e l->pUser,pMem1->
16ac5 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 6d 32 n,pMem1->z,pMem2
16ac6 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b 0a 20 ->n,pMem2->z);.
16ac7 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
16ac8 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a const void *
16ac9 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 20 20 v1, *v2;.
16aca 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 20 20 int n1, n2;.
16acb 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 20 20 Mem c1;.
16acc 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 20 20 Mem c2;.
16acd 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 31 2c memset(&c1,
16ace 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 29 3b 0, sizeof(c1));
16acf 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 . memset(
16ad0 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 &c2, 0, sizeof(c
16ad1 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 2));. sql
16ad2 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c ite3VdbeMemShall
16ad3 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d 65 6d owCopy(&c1, pMem
16ad4 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 1, MEM_Ephem);.
16ad5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
16ad6 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
16ad7 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d 45 4d (&c2, pMem2, MEM
16ad8 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 _Ephem);.
16ad9 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61 6c v1 = sqlite3Val
16ada 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f ueText((sqlite3_
16adb 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 6f 6c value*)&c1, pCol
16adc 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 l->enc);.
16add 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30 20 n1 = v1==0 ? 0
16ade 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 20 20 : c1.n;.
16adf 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 v2 = sqlite3Valu
16ae0 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f 76 eText((sqlite3_v
16ae1 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f 6c 6c alue*)&c2, pColl
16ae2 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 ->enc);.
16ae3 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 20 3a n2 = v2==0 ? 0 :
16ae4 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 20 72 c2.n;. r
16ae5 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 c = pColl->xCmp(
16ae6 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e 31 pColl->pUser, n1
16ae7 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b 0a 20 , v1, n2, v2);.
16ae8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
16ae9 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 31 beMemRelease(&c1
16aea 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
16aeb 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
16aec 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 20 72 (&c2);. r
16aed 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
16aee 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 49 }. }. /* I
16aef 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 f a NULL pointer
16af0 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 was passed as t
16af1 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e 63 74 he collate funct
16af2 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 ion, fall throug
16af3 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 h. ** to the
16af4 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 75 73 blob case and us
16af5 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a e memcmp(). */.
16af6 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 68 20 }. . /* Both
16af7 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 62 values must be b
16af8 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 20 75 lobs. Compare u
16af9 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20 20 sing memcmp().
16afa 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70 */. rc = memcmp
16afb 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d 32 (pMem1->z, pMem2
16afc 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e 70 ->z, (pMem1->n>p
16afd 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d 3e Mem2->n)?pMem2->
16afe 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 20 69 n:pMem1->n);. i
16aff 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 f( rc==0 ){.
16b00 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d 20 rc = pMem1->n -
16b01 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a 20 20 pMem2->n;. }.
16b02 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
16b03 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 6f 75 .** Move data ou
16b04 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b 65 79 t of a btree key
16b05 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 20 61 or data field a
16b06 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 73 74 nd into a Mem st
16b07 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 ructure..** The
16b08 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 20 74 data or key is t
16b09 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e aken from the en
16b0a 74 72 79 20 74 68 61 74 20 70 43 75 72 20 69 73 try that pCur is
16b0b 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
16b0c 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 66 73 ing.** to. offs
16b0d 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 65 72 et and amt deter
16b0e 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 69 6f mine what portio
16b0f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 6f 72 n of the data or
16b10 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 76 65 key to retrieve
16b11 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 75 65 ..** key is true
16b12 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 79 20 to get the key
16b13 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 74 20 or false to get
16b14 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 75 6c data. The resul
16b15 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 t is written.**
16b16 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 65 6c into the pMem el
16b17 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ement..**.** The
16b18 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 pMem structure
16b19 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 is assumed to be
16b1a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 uninitialized.
16b1b 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e 74 65 Any prior conte
16b1c 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72 69 nt.** is overwri
16b1d 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 65 69 tten without bei
16b1e 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 ng freed..**.**
16b1f 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 If this routine
16b20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65 fails for any re
16b21 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65 74 ason (malloc ret
16b22 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e 61 urns NULL or una
16b23 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 20 66 ble.** to read f
16b24 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74 68 rom the disk) th
16b25 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20 6c en the pMem is l
16b26 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 eft in an incons
16b27 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a 2f istent state..*/
16b28 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16b29 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
16b2a 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 20 42 emFromBtree(. B
16b2b 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 tCursor *pCur,
16b2c 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 /* Cursor point
16b2d 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 74 6f ing at record to
16b2e 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a 20 20 retrieve. */.
16b2f 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 20 20 int offset,
16b30 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d /* Offset from
16b31 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 64 61 the start of da
16b32 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 79 74 ta to return byt
16b33 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e es from. */. in
16b34 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 t amt,
16b35 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
16b36 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 2a 2f es to return. */
16b37 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 20 20 . int key,
16b38 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c /* If true,
16b39 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d 20 74 retrieve from t
16b3a 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 6e 6f he btree key, no
16b3b 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d 65 6d t data. */. Mem
16b3c 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 2f *pMem /
16b3d 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 64 61 * OUT: Return da
16b3e 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d 20 73 ta in this Mem s
16b3f 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 7b 0a tructure. */.){.
16b40 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 char *zData;
16b41 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 72 6f /* Data fro
16b42 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 m the btree laye
16b43 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69 6c r */. int avail
16b44 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e 75 6d able = 0; /* Num
16b45 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 ber of bytes ava
16b46 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f ilable on the lo
16b47 63 61 6c 20 62 74 72 65 65 20 70 61 67 65 20 2a cal btree page *
16b48 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b /. sqlite3 *db;
16b49 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
16b4a 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
16b4b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
16b4c 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d 20 73 TE_OK;.. db = s
16b4d 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
16b4e 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61 73 73 rDb(pCur);. ass
16b4f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
16b50 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
16b51 78 29 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 20 x) );. if( key
16b52 29 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28 ){. zData = (
16b53 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74 char *)sqlite3Bt
16b54 72 65 65 4b 65 79 46 65 74 63 68 28 70 43 75 72 reeKeyFetch(pCur
16b55 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 , &available);.
16b56 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 }else{. zDat
16b57 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 a = (char *)sqli
16b58 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 te3BtreeDataFetc
16b59 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 h(pCur, &availab
16b5a 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 le);. }. asser
16b5b 74 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a t( zData!=0 );..
16b5c 20 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 if( offset+amt
16b5d 3c 3d 61 76 61 69 6c 61 62 6c 65 20 26 26 20 28 <=available && (
16b5e 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d (pMem->flags&MEM
16b5f 5f 44 79 6e 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d _Dyn)==0 || pMem
16b60 2d 3e 78 44 65 6c 29 20 29 7b 0a 20 20 20 20 73 ->xDel) ){. s
16b61 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
16b62 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 ease(pMem);.
16b63 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 pMem->z = &zData
16b64 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70 4d [offset];. pM
16b65 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
16b66 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a Blob|MEM_Ephem;.
16b67 20 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 }else if( SQLI
16b68 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
16b69 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
16b6a 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 29 pMem, amt+2, 0))
16b6b 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ){. pMem->fl
16b6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d ags = MEM_Blob|M
16b6d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b EM_Dyn|MEM_Term;
16b6e 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d . pMem->enc =
16b6f 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 0;. pMem->ty
16b70 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 pe = SQLITE_BLOB
16b71 3b 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29 7b ;. if( key ){
16b72 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
16b73 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 te3BtreeKey(pCur
16b74 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 , offset, amt, p
16b75 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c Mem->z);. }el
16b76 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 se{. rc = s
16b77 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 qlite3BtreeData(
16b78 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d pCur, offset, am
16b79 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 t, pMem->z);.
16b7a 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 }. pMem->z[a
16b7b 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 mt] = 0;. pMe
16b7c 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b m->z[amt+1] = 0;
16b7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
16b7e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
16b7f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
16b80 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 lease(pMem);.
16b81 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e }. }. pMem->n
16b82 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 74 75 72 = amt;.. retur
16b83 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 30 0a 2f n rc;.}..#if 0./
16b84 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 76 61 72 *.** Perform var
16b85 69 6f 75 73 20 63 68 65 63 6b 73 20 6f 6e 20 74 ious checks on t
16b86 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70 he memory cell p
16b87 4d 65 6d 2e 20 41 6e 20 61 73 73 65 72 74 28 29 Mem. An assert()
16b88 20 77 69 6c 6c 0a 2a 2a 20 66 61 69 6c 20 69 66 will.** fail if
16b89 20 70 4d 65 6d 20 69 73 20 69 6e 74 65 72 6e 61 pMem is interna
16b8a 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 lly inconsistent
16b8b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
16b8c 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
16b8d 56 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65 VdbeMemSanity(Me
16b8e 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 m *pMem){. int
16b8f 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c flags = pMem->fl
16b90 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 66 ags;. assert( f
16b91 6c 61 67 73 21 3d 30 20 29 3b 20 20 2f 2a 20 4d lags!=0 ); /* M
16b92 75 73 74 20 64 65 66 69 6e 65 20 73 6f 6d 65 20 ust define some
16b93 74 79 70 65 20 2a 2f 0a 20 20 69 66 28 20 66 6c type */. if( fl
16b94 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d ags & (MEM_Str|M
16b95 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 EM_Blob) ){.
16b96 69 6e 74 20 78 20 3d 20 66 6c 61 67 73 20 26 20 int x = flags &
16b97 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f (MEM_Static|MEM_
16b98 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 Dyn|MEM_Ephem|ME
16b99 4d 5f 53 68 6f 72 74 29 3b 0a 20 20 20 20 61 73 M_Short);. as
16b9a 73 65 72 74 28 20 78 21 3d 30 20 29 3b 20 20 20 sert( x!=0 );
16b9b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 /* Stri
16b9c 6e 67 73 20 6d 75 73 74 20 64 65 66 69 6e 65 20 ngs must define
16b9d 61 20 73 74 72 69 6e 67 20 73 75 62 74 79 70 65 a string subtype
16b9e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
16b9f 28 78 20 26 20 28 78 2d 31 29 29 3d 3d 30 20 29 (x & (x-1))==0 )
16ba0 3b 20 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 ; /* Only one s
16ba1 74 72 69 6e 67 20 73 75 62 74 79 70 65 20 63 61 tring subtype ca
16ba2 6e 20 62 65 20 64 65 66 69 6e 65 64 20 2a 2f 0a n be defined */.
16ba3 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
16ba4 2d 3e 7a 21 3d 30 20 29 3b 20 20 20 20 20 20 2f ->z!=0 ); /
16ba5 2a 20 53 74 72 69 6e 67 73 20 6d 75 73 74 20 68 * Strings must h
16ba6 61 76 65 20 61 20 76 61 6c 75 65 20 2a 2f 0a 20 ave a value */.
16ba7 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e /* Mem.z poin
16ba8 74 73 20 74 6f 20 4d 65 6d 2e 7a 53 68 6f 72 74 ts to Mem.zShort
16ba9 20 69 66 66 20 74 68 65 20 73 75 62 74 79 70 65 iff the subtype
16baa 20 69 73 20 4d 45 4d 5f 53 68 6f 72 74 20 2a 2f is MEM_Short */
16bab 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 78 20 . assert( (x
16bac 26 20 4d 45 4d 5f 53 68 6f 72 74 29 3d 3d 30 20 & MEM_Short)==0
16bad 7c 7c 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d || pMem->z==pMem
16bae 2d 3e 7a 53 68 6f 72 74 20 29 3b 0a 20 20 20 20 ->zShort );.
16baf 61 73 73 65 72 74 28 20 28 78 20 26 20 4d 45 4d assert( (x & MEM
16bb0 5f 53 68 6f 72 74 29 21 3d 30 20 7c 7c 20 70 4d _Short)!=0 || pM
16bb1 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 53 68 em->z!=pMem->zSh
16bb2 6f 72 74 20 29 3b 0a 20 20 20 20 2f 2a 20 4e 6f ort );. /* No
16bb3 20 64 65 73 74 72 75 63 74 6f 72 20 75 6e 6c 65 destructor unle
16bb4 73 73 20 74 68 65 72 65 20 69 73 20 4d 45 4d 5f ss there is MEM_
16bb5 44 79 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 Dyn */. asser
16bb6 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d 30 t( pMem->xDel==0
16bb7 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 || (pMem->flags
16bb8 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 29 & MEM_Dyn)!=0 )
16bb9 3b 0a 0a 20 20 20 20 69 66 28 20 28 66 6c 61 67 ;.. if( (flag
16bba 73 20 26 20 4d 45 4d 5f 53 74 72 29 20 29 7b 0a s & MEM_Str) ){.
16bbb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d assert( pM
16bbc 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f em->enc==SQLITE_
16bbd 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 UTF8 || .
16bbe 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 pMem->enc
16bbf 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 ==SQLITE_UTF16BE
16bc0 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ||.
16bc1 20 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c pMem->enc==SQL
16bc2 49 54 45 5f 55 54 46 31 36 4c 45 20 0a 20 20 20 ITE_UTF16LE .
16bc3 20 20 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 49 );. /* I
16bc4 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 f the string is
16bc5 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 61 6e UTF-8 encoded an
16bc6 64 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 d nul terminated
16bc7 2c 20 74 68 65 6e 20 70 4d 65 6d 2d 3e 6e 0a 20 , then pMem->n.
16bc8 20 20 20 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 ** must be
16bc9 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 the length of th
16bca 65 20 73 74 72 69 6e 67 2e 20 20 28 4c 61 74 65 e string. (Late
16bcb 72 3a 29 20 20 49 66 20 74 68 65 20 64 61 74 61 r:) If the data
16bcc 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 base file.
16bcd 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72 ** has been corr
16bce 75 70 74 65 64 2c 20 27 5c 30 30 30 27 20 63 68 upted, '\000' ch
16bcf 61 72 61 63 74 65 72 73 20 6d 69 67 68 74 20 68 aracters might h
16bd0 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 65 ave been inserte
16bd1 64 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 d. ** into
16bd2 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 the middle of th
16bd3 65 20 73 74 72 69 6e 67 2e 20 20 49 6e 20 74 68 e string. In th
16bd4 61 74 20 63 61 73 65 2c 20 74 68 65 20 73 74 72 at case, the str
16bd5 6c 65 6e 28 29 20 6d 69 67 68 74 0a 20 20 20 20 len() might.
16bd6 20 20 2a 2a 20 62 65 20 6c 65 73 73 2e 0a 20 20 ** be less..
16bd7 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
16bd8 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 pMem->enc==SQLI
16bd9 54 45 5f 55 54 46 38 20 26 26 20 28 66 6c 61 67 TE_UTF8 && (flag
16bda 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 20 29 7b s & MEM_Term) ){
16bdb 20 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 . assert
16bdc 28 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e 7a ( strlen(pMem->z
16bdd 29 3c 3d 70 4d 65 6d 2d 3e 6e 20 29 3b 0a 20 20 )<=pMem->n );.
16bde 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d assert( pM
16bdf 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 3d 3d em->z[pMem->n]==
16be0 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0 );. }.
16be1 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
16be2 2f 2a 20 43 61 6e 6e 6f 74 20 64 65 66 69 6e 65 /* Cannot define
16be3 20 61 20 73 74 72 69 6e 67 20 73 75 62 74 79 70 a string subtyp
16be4 65 20 66 6f 72 20 6e 6f 6e 2d 73 74 72 69 6e 67 e for non-string
16be5 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 20 20 objects */.
16be6 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
16be7 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 61 74 lags & (MEM_Stat
16be8 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 ic|MEM_Dyn|MEM_E
16be9 70 68 65 6d 7c 4d 45 4d 5f 53 68 6f 72 74 29 29 phem|MEM_Short))
16bea 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
16beb 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d 30 t( pMem->xDel==0
16bec 20 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 4d 45 4d );. }. /* MEM
16bed 5f 4e 75 6c 6c 20 65 78 63 6c 75 64 65 73 20 61 _Null excludes a
16bee 6c 6c 20 6f 74 68 65 72 20 74 79 70 65 73 20 2a ll other types *
16bef 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 /. assert( (pMe
16bf0 6d 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 m->flags&(MEM_St
16bf1 72 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 r|MEM_Int|MEM_Re
16bf2 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 al|MEM_Blob))==0
16bf3 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 . || (p
16bf4 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e Mem->flags&MEM_N
16bf5 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 2f 2a 20 ull)==0 );. /*
16bf6 49 66 20 74 68 65 20 4d 45 4d 20 69 73 20 62 6f If the MEM is bo
16bf7 74 68 20 72 65 61 6c 20 61 6e 64 20 69 6e 74 65 th real and inte
16bf8 67 65 72 2c 20 74 68 65 20 76 61 6c 75 65 73 20 ger, the values
16bf9 61 72 65 20 65 71 75 61 6c 20 2a 2f 0a 20 20 61 are equal */. a
16bfa 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
16bfb 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d ags & (MEM_Int|M
16bfc 45 4d 5f 52 65 61 6c 29 29 21 3d 28 4d 45 4d 5f EM_Real))!=(MEM_
16bfd 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 0a 20 Int|MEM_Real) .
16bfe 20 20 20 20 20 20 20 20 20 7c 7c 20 70 4d 65 6d || pMem
16bff 2d 3e 72 3d 3d 70 4d 65 6d 2d 3e 75 2e 69 20 29 ->r==pMem->u.i )
16c00 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 54 ;.}.#endif../* T
16c01 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
16c02 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
16c03 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 73 nternally, it is
16c04 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 not part of the
16c05 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 49 .** external API
16c06 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 20 . It works in a
16c07 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 73 similar way to s
16c08 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
16c09 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 74 t(),.** except t
16c0a 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 he data returned
16c0b 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 is in the encod
16c0c 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 79 ing specified by
16c0d 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 the second.** p
16c0e 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 20 arameter, which
16c0f 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 must be one of S
16c10 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 QLITE_UTF16BE, S
16c11 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 QLITE_UTF16LE or
16c12 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2e .** SQLITE_UTF8.
16c13 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 2d .**.** (2006-02-
16c14 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 61 16:) The enc va
16c15 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 64 lue can be or-ed
16c16 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 46 with SQLITE_UTF
16c17 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 49 16_ALIGNED..** I
16c18 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61 f that is the ca
16c19 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 se, then the res
16c1a 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67 ult must be alig
16c1b 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 62 ned on an even b
16c1c 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 2e yte.** boundary.
16c1d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
16c1e 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 TE const void *s
16c1f 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
16c20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 sqlite3_value* p
16c21 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 Val, u8 enc){.
16c22 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 75 if( !pVal ) retu
16c23 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 rn 0;.. assert(
16c24 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pVal->db==0 ||
16c25 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
16c26 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 74 ld(pVal->db->mut
16c27 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
16c28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 7e (enc&3)==(enc&~
16c29 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 SQLITE_UTF16_ALI
16c2a 47 4e 45 44 29 20 29 3b 0a 0a 20 20 69 66 28 20 GNED) );.. if(
16c2b 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pVal->flags&MEM_
16c2c 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 Null ){. retu
16c2d 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 rn 0;. }. asse
16c2e 72 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 rt( (MEM_Blob>>3
16c2f 29 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 0a ) == MEM_Str );.
16c30 20 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d pVal->flags |=
16c31 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 (pVal->flags &
16c32 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 MEM_Blob)>>3;.
16c33 65 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29 expandBlob(pVal)
16c34 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c ;. if( pVal->fl
16c35 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 ags&MEM_Str ){.
16c36 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
16c37 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 angeEncoding(pVa
16c38 6c 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 l, enc & ~SQLITE
16c39 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b _UTF16_ALIGNED);
16c3a 0a 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 20 . if( (enc &
16c3b 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 SQLITE_UTF16_ALI
16c3c 47 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28 GNED)!=0 && 1==(
16c3d 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 1&SQLITE_PTR_TO_
16c3e 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 7b INT(pVal->z)) ){
16c3f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
16c40 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d pVal->flags & (M
16c41 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 EM_Ephem|MEM_Sta
16c42 74 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 tic))!=0 );.
16c43 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
16c44 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c eMemMakeWriteabl
16c45 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f e(pVal)!=SQLITE_
16c46 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
16c47 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a turn 0;. }.
16c48 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
16c49 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 3VdbeMemNulTermi
16c4a 6e 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d 65 nate(pVal);. }e
16c4b 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
16c4c 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 (pVal->flags&ME
16c4d 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 M_Blob)==0 );.
16c4e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
16c4f 53 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c 20 Stringify(pVal,
16c50 65 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 enc);. assert
16c51 28 20 30 3d 3d 28 31 26 28 69 6e 74 29 70 56 61 ( 0==(1&(int)pVa
16c52 6c 2d 3e 7a 29 20 29 3b 0a 20 20 7d 0a 20 20 61 l->z) );. }. a
16c53 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e 63 3d ssert(pVal->enc=
16c54 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f =(enc & ~SQLITE_
16c55 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 7c UTF16_ALIGNED) |
16c56 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 20 | pVal->db==0.
16c57 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 || p
16c58 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 Val->db->mallocF
16c59 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 ailed );. if( p
16c5a 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 Val->enc==(enc &
16c5b 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 ~SQLITE_UTF16_A
16c5c 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 20 72 LIGNED) ){. r
16c5d 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a 20 eturn pVal->z;.
16c5e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
16c5f 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a rn 0;. }.}../*.
16c60 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
16c61 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
16c62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ject..*/.SQLITE_
16c63 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
16c64 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61 value *sqlite3Va
16c65 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a lueNew(sqlite3 *
16c66 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 db){. Mem *p =
16c67 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
16c68 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a ero(db, sizeof(*
16c69 70 29 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a p));. if( p ){.
16c6a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d p->flags = M
16c6b 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d 3e EM_Null;. p->
16c6c 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 type = SQLITE_NU
16c6d 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 3d 20 LL;. p->db =
16c6e 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e db;. }. return
16c6f 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 p;.}../*.** Cre
16c70 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 ate a new sqlite
16c71 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2c 20 3_value object,
16c72 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 76 containing the v
16c73 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2e 0a 2a alue of pExpr..*
16c74 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 *.** This only w
16c75 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 20 73 69 orks for very si
16c76 6d 70 6c 65 20 65 78 70 72 65 73 73 69 6f 6e 73 mple expressions
16c77 20 74 68 61 74 20 63 6f 6e 73 69 73 74 20 6f 66 that consist of
16c78 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a one constant.**
16c79 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22 token (i.e. "5"
16c7a 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 73 74 72 , "5.1", "'a str
16c7b 69 6e 67 27 22 29 2e 20 49 66 20 74 68 65 20 65 ing'"). If the e
16c7c 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a 2a xpression can.**
16c7d 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 64 69 be converted di
16c7e 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 76 61 rectly into a va
16c7f 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76 61 lue, then the va
16c80 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 lue is allocated
16c81 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 and.** a pointe
16c82 72 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 r written to *pp
16c83 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 Val. The caller
16c84 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
16c85 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0a or deallocating.
16c86 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79 20 ** the value by
16c87 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71 passing it to sq
16c88 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 29 lite3ValueFree()
16c89 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74 68 later on. If th
16c8a 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 e expression.**
16c8b 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 cannot be conver
16c8c 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c 20 ted to a value,
16c8d 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20 73 then *ppVal is s
16c8e 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 et to NULL..*/.S
16c8f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16c90 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 t sqlite3ValueFr
16c91 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65 omExpr(. sqlite
16c92 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
16c93 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
16c94 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
16c95 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c /. Expr *pExpr,
16c96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16c97 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 The expression
16c98 74 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f 0a 20 to evaluate */.
16c99 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 u8 enc,
16c9a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e /* En
16c9b 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f coding to use */
16c9c 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 . u8 affinity,
16c9d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16c9e 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 Affinity to use
16c9f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c */. sqlite3_val
16ca0 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 20 2f ue **ppVal /
16ca1 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20 * Write the new
16ca2 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 29 7b value here */.){
16ca3 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 68 61 . int op;. cha
16ca4 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 73 r *zVal = 0;. s
16ca5 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
16ca6 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 21 al = 0;.. if( !
16ca7 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70 70 pExpr ){. *pp
16ca8 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 Val = 0;. ret
16ca9 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
16caa 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d }. op = pExpr-
16cab 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f 70 3d 3d >op;.. if( op==
16cac 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 3d TK_STRING || op=
16cad 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 3d =TK_FLOAT || op=
16cae 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 =TK_INTEGER ){.
16caf 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74 65 zVal = sqlite
16cb0 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 3DbStrNDup(db, (
16cb1 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b char*)pExpr->tok
16cb2 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b en.z, pExpr->tok
16cb3 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 61 6c 20 en.n);. pVal
16cb4 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 = sqlite3ValueNe
16cb5 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 21 w(db);. if( !
16cb6 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20 29 20 zVal || !pVal )
16cb7 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 goto no_mem;.
16cb8 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 sqlite3Dequote(
16cb9 7a 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 zVal);. sqlit
16cba 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 e3ValueSetStr(pV
16cbb 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 al, -1, zVal, SQ
16cbc 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
16cbd 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 E_DYNAMIC);.
16cbe 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54 45 if( (op==TK_INTE
16cbf 47 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c GER || op==TK_FL
16cc0 4f 41 54 20 29 20 26 26 20 61 66 66 69 6e 69 74 OAT ) && affinit
16cc1 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f y==SQLITE_AFF_NO
16cc2 4e 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 NE ){. sqli
16cc3 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 te3ValueApplyAff
16cc4 69 6e 69 74 79 28 70 56 61 6c 2c 20 53 51 4c 49 inity(pVal, SQLI
16cc5 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 TE_AFF_NUMERIC,
16cc6 65 6e 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b enc);. }else{
16cc7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 . sqlite3Va
16cc8 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 lueApplyAffinity
16cc9 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c (pVal, affinity,
16cca 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d enc);. }. }
16ccb 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f else if( op==TK_
16ccc 55 4d 49 4e 55 53 20 29 20 7b 0a 20 20 20 20 69 UMINUS ) {. i
16ccd 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 f( SQLITE_OK==sq
16cce 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 lite3ValueFromEx
16ccf 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c 65 pr(db,pExpr->pLe
16cd0 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79 2c ft,enc,affinity,
16cd1 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 &pVal) ){.
16cd2 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20 2a pVal->u.i = -1 *
16cd3 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20 pVal->u.i;.
16cd4 20 20 70 56 61 6c 2d 3e 72 20 3d 20 2d 31 2e 30 pVal->r = -1.0
16cd5 20 2a 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 * pVal->r;.
16cd6 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }. }.#ifndef SQ
16cd7 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c LITE_OMIT_BLOB_L
16cd8 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 ITERAL. else if
16cd9 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b ( op==TK_BLOB ){
16cda 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 . int nVal;.
16cdb 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
16cdc 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a ->token.n>=3 );.
16cdd 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
16cde 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 r->token.z[0]=='
16cdf 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 74 6f 6b x' || pExpr->tok
16ce0 65 6e 2e 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a en.z[0]=='X' );.
16ce1 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
16ce2 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27 r->token.z[1]=='
16ce3 5c 27 27 20 29 3b 0a 20 20 20 20 61 73 73 65 72 \'' );. asser
16ce4 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e t( pExpr->token.
16ce5 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e z[pExpr->token.n
16ce6 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 -1]=='\'' );.
16ce7 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 pVal = sqlite3V
16ce8 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 alueNew(db);.
16ce9 20 69 66 28 20 21 70 56 61 6c 20 29 20 67 6f 74 if( !pVal ) got
16cea 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 6e 56 o no_mem;. nV
16ceb 61 6c 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 al = pExpr->toke
16cec 6e 2e 6e 20 2d 20 33 3b 0a 20 20 20 20 7a 56 61 n.n - 3;. zVa
16ced 6c 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72 l = (char*)pExpr
16cee 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 ->token.z + 2;.
16cef 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
16cf0 6d 53 65 74 53 74 72 28 70 56 61 6c 2c 20 73 71 mSetStr(pVal, sq
16cf1 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 64 lite3HexToBlob(d
16cf2 62 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 b, zVal, nVal),
16cf3 6e 56 61 6c 2f 32 2c 0a 20 20 20 20 20 20 20 20 nVal/2,.
16cf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16cf5 20 30 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 0, SQLITE_DYNAM
16cf6 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a IC);. }.#endif.
16cf7 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 61 6c . *ppVal = pVal
16cf8 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
16cf9 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 E_OK;..no_mem:.
16cfa 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
16cfb 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 d = 1;. sqlite3
16cfc 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61 6c 29 DbFree(db, zVal)
16cfd 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 ;. sqlite3Value
16cfe 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 2a 70 Free(pVal);. *p
16cff 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 pVal = 0;. retu
16d00 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
16d01 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
16d02 20 74 68 65 20 73 74 72 69 6e 67 20 76 61 6c 75 the string valu
16d03 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 5f e of an sqlite3_
16d04 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a value object.*/.
16d05 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
16d06 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 oid sqlite3Value
16d07 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69 74 65 SetStr(. sqlite
16d08 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20 20 20 3_value *v,
16d09 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65 20 73 /* Value to be s
16d0a 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 et */. int n,
16d0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16d0c 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e Length of strin
16d0d 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 g z */. const v
16d0e 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20 20 2f oid *z, /
16d0f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6e 65 * Text of the ne
16d10 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 75 38 w string */. u8
16d11 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 enc,
16d12 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 /* Encoding
16d13 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f 69 64 to use */. void
16d14 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 (*xDel)(void*)
16d15 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 /* Destructor
16d16 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 2a for the string *
16d17 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29 20 73 /.){. if( v ) s
16d18 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
16d19 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c Str((Mem *)v, z,
16d1a 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29 3b 0a n, enc, xDel);.
16d1b 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e }../*.** Free an
16d1c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
16d1d 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f bject.*/.SQLITE_
16d1e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
16d1f 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 ite3ValueFree(sq
16d20 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 29 7b lite3_value *v){
16d21 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75 . if( !v ) retu
16d22 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 rn;. sqlite3Vdb
16d23 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d 65 6d eMemRelease((Mem
16d24 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 *)v);. sqlite3
16d25 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29 76 29 DbFree(((Mem*)v)
16d26 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a ->db, v);.}../*.
16d27 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
16d28 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
16d29 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c the sqlite3_val
16d2a 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75 6d 69 ue object assumi
16d2b 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20 75 73 ng.** that it us
16d2c 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 es the encoding
16d2d 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f "enc".*/.SQLITE_
16d2e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
16d2f 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73 71 te3ValueBytes(sq
16d30 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
16d31 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 4d 65 l, u8 enc){. Me
16d32 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 m *p = (Mem*)pVa
16d33 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 l;. if( (p->fla
16d34 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d gs & MEM_Blob)!=
16d35 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61 6c 75 0 || sqlite3Valu
16d36 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e 63 29 eText(pVal, enc)
16d37 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 ){. if( p->f
16d38 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
16d39 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
16d3a 70 2d 3e 6e 2b 70 2d 3e 75 2e 69 3b 0a 20 20 20 p->n+p->u.i;.
16d3b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 }else{. re
16d3c 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d turn p->n;. }
16d3d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
16d3e 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
16d3f 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 6d *** End of vdbem
16d40 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a em.c ***********
16d41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16d42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16d43 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
16d44 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 *** Begin file v
16d45 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a dbeaux.c *******
16d46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16d47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16d48 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 **/./*.** 2003 S
16d49 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a eptember 6.**.**
16d4a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
16d4b 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
16d4c 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
16d4d 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
16d4e 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
16d4f 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
16d50 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
16d51 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
16d52 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
16d53 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
16d54 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
16d55 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
16d56 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
16d57 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
16d58 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
16d59 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
16d5a 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
16d5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16d5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16d5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16d5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
16d5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
16d60 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
16d61 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63 72 code used for cr
16d62 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79 69 eating, destroyi
16d63 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74 69 ng, and populati
16d64 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f 72 ng.** a VDBE (or
16d65 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d an "sqlite3_stm
16d66 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f 77 t" as it is know
16d67 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64 65 n to the outside
16d68 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72 0a world.) Prior.
16d69 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32 2e ** to version 2.
16d6a 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63 6f 8.7, all this co
16d6b 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64 20 de was combined
16d6c 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63 20 into the vdbe.c
16d6d 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 20 source file..**
16d6e 42 75 74 20 74 68 61 74 20 66 69 6c 65 20 77 61 But that file wa
16d6f 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62 69 s getting too bi
16d70 67 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f 75 g so this subrou
16d71 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69 74 tines were split
16d72 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a out..**.** $Id:
16d73 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e 34 vdbeaux.c,v 1.4
16d74 32 30 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39 20 2008/11/17 19
16d75 3a 31 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39 :18:55 danielk19
16d76 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f 77 Exp $.*/..../
16d77 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 75 67 67 *.** When debugg
16d78 69 6e 67 20 74 68 65 20 63 6f 64 65 20 67 65 6e ing the code gen
16d79 65 72 61 74 6f 72 20 69 6e 20 61 20 73 79 6d 62 erator in a symb
16d7a 6f 6c 69 63 20 64 65 62 75 67 67 65 72 2c 20 6f olic debugger, o
16d7b 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68 ne can.** set th
16d7c 65 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 e sqlite3VdbeAdd
16d7d 6f 70 54 72 61 63 65 20 74 6f 20 31 20 61 6e 64 opTrace to 1 and
16d7e 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c all opcodes wil
16d7f 6c 20 62 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20 l be printed.**
16d80 61 73 20 74 68 65 79 20 61 72 65 20 61 64 64 65 as they are adde
16d81 64 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 d to the instruc
16d82 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a tion stream..*/.
16d83 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
16d84 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
16d85 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
16d86 62 65 41 64 64 6f 70 54 72 61 63 65 20 3d 20 30 beAddopTrace = 0
16d87 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a ;.#endif.../*.**
16d88 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 Create a new vi
16d89 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 rtual database e
16d8a 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ngine..*/.SQLITE
16d8b 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 _PRIVATE Vdbe *s
16d8c 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 qlite3VdbeCreate
16d8d 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
16d8e 20 56 64 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20 Vdbe *p;. p =
16d8f 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
16d90 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 ero(db, sizeof(V
16d91 64 62 65 29 20 29 3b 0a 20 20 69 66 28 20 70 3d dbe) );. if( p=
16d92 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
16d93 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 p->db = db;. i
16d94 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a f( db->pVdbe ){.
16d95 20 20 20 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70 db->pVdbe->p
16d96 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 Prev = p;. }.
16d97 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 p->pNext = db->p
16d98 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76 Vdbe;. p->pPrev
16d99 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62 = 0;. db->pVdb
16d9a 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 e = p;. p->magi
16d9b 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 c = VDBE_MAGIC_I
16d9c 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b NIT;. return p;
16d9d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 .}../*.** Rememb
16d9e 65 72 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e er the SQL strin
16d9f 67 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 64 g for a prepared
16da0 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 statement..*/.S
16da1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
16da2 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 id sqlite3VdbeSe
16da3 74 53 71 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f tSql(Vdbe *p, co
16da4 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
16da5 20 6e 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 n){. if( p==0
16da6 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
16da7 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29 rt( p->zSql==0 )
16da8 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 71 ;. p->zSql = sq
16da9 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 lite3DbStrNDup(p
16daa 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 7d 0a 0a ->db, z, n);.}..
16dab 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
16dac 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65 64 20 SQL associated
16dad 77 69 74 68 20 61 20 70 72 65 70 61 72 65 64 20 with a prepared
16dae 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c statement.*/.SQL
16daf 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
16db0 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 ar *sqlite3_sql(
16db1 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
16db2 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 tmt){. return (
16db3 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 2d 3e (Vdbe *)pStmt)->
16db4 7a 53 71 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 zSql;.}../*.** S
16db5 77 61 70 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 wap all content
16db6 62 65 74 77 65 65 6e 20 74 77 6f 20 56 44 42 45 between two VDBE
16db7 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a structures..*/.
16db8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
16db9 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 oid sqlite3VdbeS
16dba 77 61 70 28 56 64 62 65 20 2a 70 41 2c 20 56 64 wap(Vdbe *pA, Vd
16dbb 62 65 20 2a 70 42 29 7b 0a 20 20 56 64 62 65 20 be *pB){. Vdbe
16dbc 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68 tmp, *pTmp;. ch
16dbd 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 69 6e 74 20 ar *zTmp;. int
16dbe 6e 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a 70 nTmp;. tmp = *p
16dbf 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b 0a A;. *pA = *pB;.
16dc0 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20 70 *pB = tmp;. p
16dc1 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74 3b Tmp = pA->pNext;
16dc2 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20 70 . pA->pNext = p
16dc3 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d 3e B->pNext;. pB->
16dc4 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20 20 pNext = pTmp;.
16dc5 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65 76 pTmp = pA->pPrev
16dc6 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d 20 ;. pA->pPrev =
16dc7 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42 2d pB->pPrev;. pB-
16dc8 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a 20 >pPrev = pTmp;.
16dc9 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 6c zTmp = pA->zSql
16dca 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 70 ;. pA->zSql = p
16dcb 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e 7a B->zSql;. pB->z
16dcc 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 6e 54 Sql = zTmp;. nT
16dcd 6d 70 20 3d 20 70 41 2d 3e 6e 53 71 6c 3b 0a 20 mp = pA->nSql;.
16dce 20 70 41 2d 3e 6e 53 71 6c 20 3d 20 70 42 2d 3e pA->nSql = pB->
16dcf 6e 53 71 6c 3b 0a 20 20 70 42 2d 3e 6e 53 71 6c nSql;. pB->nSql
16dd0 20 3d 20 6e 54 6d 70 3b 0a 7d 0a 0a 23 69 66 64 = nTmp;.}..#ifd
16dd1 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
16dd2 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 63 69 /*.** Turn traci
16dd3 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a ng on or off.*/.
16dd4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
16dd5 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54 oid sqlite3VdbeT
16dd6 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 46 49 race(Vdbe *p, FI
16dd7 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d LE *trace){. p-
16dd8 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 3b 0a >trace = trace;.
16dd9 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
16dda 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e Resize the Vdbe.
16ddb 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 68 61 aOp array so tha
16ddc 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 73 74 t it is at least
16ddd 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 20 74 one op larger t
16dde 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a han .** it was..
16ddf 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d **.** If an out-
16de0 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 of-memory error
16de1 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73 occurs while res
16de2 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c izing the array,
16de3 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 return.** SQLIT
16de4 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73 E_NOMEM. In this
16de5 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 20 61 case Vdbe.aOp a
16de6 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63 nd Vdbe.nOpAlloc
16de7 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68 remain .** unch
16de8 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 20 73 anged (this is s
16de9 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 6f 64 o that any opcod
16dea 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 es already alloc
16deb 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20 ated can be .**
16dec 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f correctly deallo
16ded 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 cated along with
16dee 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 the rest of the
16def 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 Vdbe)..*/.stati
16df0 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 72 61 c int growOpArra
16df1 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 56 64 y(Vdbe *p){. Vd
16df2 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e beOp *pNew;. in
16df3 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70 t nNew = (p->nOp
16df4 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c Alloc ? p->nOpAl
16df5 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 31 30 loc*2 : (int)(10
16df6 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b 24/sizeof(Op)));
16df7 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
16df8 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62 3DbRealloc(p->db
16df9 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73 , p->aOp, nNew*s
16dfa 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66 izeof(Op));. if
16dfb 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d ( pNew ){. p-
16dfc 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 6e 4e 65 77 >nOpAlloc = nNew
16dfd 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70 ;. p->aOp = p
16dfe 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 New;. }. retur
16dff 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45 n (pNew ? SQLITE
16e00 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d _OK : SQLITE_NOM
16e01 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 EM);.}../*.** Ad
16e02 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74 d a new instruct
16e03 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 ion to the list
16e04 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 of instructions
16e05 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a current in the.*
16e06 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20 * VDBE. Return
16e07 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 the address of t
16e08 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 he new instructi
16e09 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 on..**.** Parame
16e0a 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 ters:.**.** p
16e0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
16e0c 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44 ointer to the VD
16e0d 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 BE.**.** op
16e0e 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
16e0f 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20 opcode for this
16e10 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a instruction.**.*
16e11 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20 * p1, p2, p3
16e12 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a Operands.**
16e13 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69 .** Use the sqli
16e14 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
16e15 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 bel() function t
16e16 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73 o fix an address
16e17 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69 and.** the sqli
16e18 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
16e19 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68 ) function to ch
16e1a 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f ange the value o
16e1b 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72 f the P4.** oper
16e1c 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 and..*/.SQLITE_P
16e1d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16e1e 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 e3VdbeAddOp3(Vdb
16e1f 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e e *p, int op, in
16e20 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e t p1, int p2, in
16e21 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a t p3){. int i;.
16e22 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a VdbeOp *pOp;..
16e23 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 i = p->nOp;.
16e24 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
16e25 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
16e26 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f T );. if( p->nO
16e27 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 pAlloc<=i ){.
16e28 20 69 66 28 20 67 72 6f 77 4f 70 41 72 72 61 79 if( growOpArray
16e29 28 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 (p) ){. ret
16e2a 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 0;. }. }
16e2b 0a 20 20 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 . p->nOp++;. p
16e2c 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b Op = &p->aOp[i];
16e2d 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d . pOp->opcode =
16e2e 20 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20 3d op;. pOp->p5 =
16e2f 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d 20 0;. pOp->p1 =
16e30 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d 20 p1;. pOp->p2 =
16e31 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d 20 p2;. pOp->p3 =
16e32 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 p3;. pOp->p4.p
16e33 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74 79 = 0;. pOp->p4ty
16e34 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b pe = P4_NOTUSED;
16e35 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 . p->expired =
16e36 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 0;.#ifdef SQLITE
16e37 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a 43 _DEBUG. pOp->zC
16e38 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69 66 omment = 0;. if
16e39 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 ( sqlite3VdbeAdd
16e3a 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 opTrace ) sqlite
16e3b 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 3VdbePrintOp(0,
16e3c 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a i, &p->aOp[i]);.
16e3d 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56 44 #endif.#ifdef VD
16e3e 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f 70 BE_PROFILE. pOp
16e3f 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 ->cycles = 0;.
16e40 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23 65 pOp->cnt = 0;.#e
16e41 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69 3b ndif. return i;
16e42 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
16e43 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
16e44 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70 2c eAddOp0(Vdbe *p,
16e45 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74 75 int op){. retu
16e46 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 rn sqlite3VdbeAd
16e47 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20 30 dOp3(p, op, 0, 0
16e48 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 , 0);.}.SQLITE_P
16e49 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16e4a 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 62 e3VdbeAddOp1(Vdb
16e4b 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e e *p, int op, in
16e4c 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e 20 t p1){. return
16e4d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
16e4e 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c 20 3(p, op, p1, 0,
16e4f 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 0);.}.SQLITE_PRI
16e50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16e51 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 20 VdbeAddOp2(Vdbe
16e52 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 *p, int op, int
16e53 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20 72 p1, int p2){. r
16e54 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 eturn sqlite3Vdb
16e55 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 eAddOp3(p, op, p
16e56 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 1, p2, 0);.}.../
16e57 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 6f *.** Add an opco
16e58 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 73 de that includes
16e59 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 73 the p4 value as
16e5a 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 a pointer..*/.S
16e5b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16e5c 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
16e5d 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 Op4(. Vdbe *p,
16e5e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 /* Ad
16e5f 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f 20 d the opcode to
16e60 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e 74 this VM */. int
16e61 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 op,
16e62 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63 6f /* The new opco
16e63 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 de */. int p1,
16e64 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
16e65 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f he P1 operand */
16e66 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20 20 . int p2,
16e67 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 32 /* The P2
16e68 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
16e69 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20 20 t p3,
16e6a 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65 72 /* The P3 oper
16e6b 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 and */. const c
16e6c 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a 20 har *zP4, /*
16e6d 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 2a The P4 operand *
16e6e 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20 20 /. int p4type
16e6f 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f 70 /* P4 op
16e70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29 7b erand type */.){
16e71 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71 . int addr = sq
16e72 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
16e73 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 70 p, op, p1, p2, p
16e74 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 3);. sqlite3Vdb
16e75 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64 64 eChangeP4(p, add
16e76 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29 3b r, zP4, p4type);
16e77 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a . return addr;.
16e78 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 }../*.** Create
16e79 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20 6c a new symbolic l
16e7a 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73 74 abel for an inst
16e7b 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61 73 ruction that has
16e7c 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63 6f yet to be.** co
16e7d 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f 6c ded. The symbol
16e7e 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61 6c ic label is real
16e7f 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74 69 ly just a negati
16e80 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 0a ve number. The.
16e81 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65 20 ** label can be
16e82 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20 76 used as the P2 v
16e83 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72 61 alue of an opera
16e84 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77 68 tion. Later, wh
16e85 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c 20 en.** the label
16e86 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 61 is resolved to a
16e87 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65 73 specific addres
16e88 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c 6c s, the VDBE will
16e89 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67 68 scan.** through
16e8a 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20 6c its operation l
16e8b 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20 61 ist and change a
16e8c 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32 20 ll values of P2
16e8d 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20 74 which match.** t
16e8e 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74 68 he label into th
16e8f 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72 65 e resolved addre
16e90 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 ss..**.** The VD
16e91 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61 20 BE knows that a
16e92 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 P2 value is a la
16e93 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62 65 bel because labe
16e94 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79 73 ls are.** always
16e95 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50 32 negative and P2
16e96 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70 70 values are supp
16e97 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e 65 ose to be non-ne
16e98 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63 65 gative..** Hence
16e99 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32 20 , a negative P2
16e9a 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c value is a label
16e9b 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 6f that has yet to
16e9c 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a be resolved..**
16e9d 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74 75 .** Zero is retu
16e9e 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 rned if a malloc
16e9f 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c () fails..*/.SQL
16ea0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
16ea1 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
16ea2 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 abel(Vdbe *p){.
16ea3 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70 2d int i;. i = p-
16ea4 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73 73 >nLabel++;. ass
16ea5 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 ert( p->magic==V
16ea6 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 DBE_MAGIC_INIT )
16ea7 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e 4c ;. if( i>=p->nL
16ea8 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 abelAlloc ){.
16ea9 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 p->nLabelAlloc
16eaa 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 = p->nLabelAlloc
16eab 2a 32 20 2b 20 31 30 3b 0a 20 20 20 20 70 2d 3e *2 + 10;. p->
16eac 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 aLabel = sqlite3
16ead 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 DbReallocOrFree(
16eae 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c p->db, p->aLabel
16eaf 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
16eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16eb1 20 20 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 p->nLabelA
16eb2 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 lloc*sizeof(p->a
16eb3 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a 20 20 7d 0a Label[0]));. }.
16eb4 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 if( p->aLabel
16eb5 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c ){. p->aLabel
16eb6 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 [i] = -1;. }.
16eb7 72 65 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a return -1-i;.}..
16eb8 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 /*.** Resolve la
16eb9 62 65 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68 bel "x" to be th
16eba 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
16ebb 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
16ebc 6e 20 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72 n to.** be inser
16ebd 74 65 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 ted. The parame
16ebe 74 65 72 20 22 78 22 20 6d 75 73 74 20 68 61 76 ter "x" must hav
16ebf 65 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 e been obtained
16ec0 66 72 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20 from.** a prior
16ec1 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 call to sqlite3V
16ec2 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a dbeMakeLabel()..
16ec3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
16ec4 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
16ec5 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 beResolveLabel(V
16ec6 64 62 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a dbe *p, int x){.
16ec7 20 20 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a int j = -1-x;.
16ec8 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 assert( p->mag
16ec9 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
16eca 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 NIT );. assert(
16ecb 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c j>=0 && j<p->nL
16ecc 61 62 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d abel );. if( p-
16ecd 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 >aLabel ){. p
16ece 2d 3e 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d ->aLabel[j] = p-
16ecf 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a >nOp;. }.}../*.
16ed0 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 ** Loop through
16ed1 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b the program look
16ed2 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65 ing for P2 value
16ed3 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61 74 s that are negat
16ed4 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69 ive.** on jump i
16ed5 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61 nstructions. Ea
16ed6 63 68 20 73 75 63 68 20 76 61 6c 75 65 20 69 73 ch such value is
16ed7 20 61 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c a label. Resol
16ed8 76 65 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 ve the.** label
16ed9 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 by setting the P
16eda 32 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63 2 value to its c
16edb 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20 orrect non-zero
16edc 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 value..**.** Thi
16edd 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
16ede 6c 65 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61 led once after a
16edf 6c 6c 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 ll opcodes have
16ee0 62 65 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a been inserted..*
16ee1 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70 *.** Variable *p
16ee2 4d 61 78 46 75 6e 63 41 72 67 73 20 69 73 20 73 MaxFuncArgs is s
16ee3 65 74 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 et to the maximu
16ee4 6d 20 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50 m value of any P
16ee5 32 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 2 argument .** t
16ee6 6f 20 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e o an OP_Function
16ee7 2c 20 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20 , OP_AggStep or
16ee8 4f 50 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64 OP_VFilter opcod
16ee9 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 e. This is used
16eea 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 by .** sqlite3Vd
16eeb 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f beMakeReady() to
16eec 20 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 size the Vdbe.a
16eed 70 41 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a pArg[] array..**
16eee 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
16eef 20 61 6c 73 6f 20 64 6f 65 73 20 74 68 65 20 66 also does the f
16ef0 6f 6c 6c 6f 77 69 6e 67 20 6f 70 74 69 6d 69 7a ollowing optimiz
16ef1 61 74 69 6f 6e 3a 20 20 49 74 20 73 63 61 6e 73 ation: It scans
16ef2 20 66 6f 72 0a 2a 2a 20 69 6e 73 74 72 75 63 74 for.** instruct
16ef3 69 6f 6e 73 20 74 68 61 74 20 6d 69 67 68 74 20 ions that might
16ef4 63 61 75 73 65 20 61 20 73 74 61 74 65 6d 65 6e cause a statemen
16ef5 74 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 53 75 63 t rollback. Suc
16ef6 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 0a 2a h instructions.*
16ef7 2a 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a * are:.**.** *
16ef8 20 20 4f 50 5f 48 61 6c 74 20 77 69 74 68 20 50 OP_Halt with P
16ef9 31 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 1=SQLITE_CONSTRA
16efa 49 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62 INT and P2=OE_Ab
16efb 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f ort..** * OP_
16efc 44 65 73 74 72 6f 79 0a 2a 2a 20 20 20 2a 20 20 Destroy.** *
16efd 4f 50 5f 56 55 70 64 61 74 65 0a 2a 2a 20 20 20 OP_VUpdate.**
16efe 2a 20 20 4f 50 5f 56 52 65 6e 61 6d 65 0a 2a 2a * OP_VRename.**
16eff 0a 2a 2a 20 49 66 20 6e 6f 20 73 75 63 68 20 69 .** If no such i
16f00 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 66 6f nstruction is fo
16f01 75 6e 64 2c 20 74 68 65 6e 20 65 76 65 72 79 20 und, then every
16f02 53 74 61 74 65 6d 65 6e 74 20 69 6e 73 74 72 75 Statement instru
16f03 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 63 68 61 ction .** is cha
16f04 6e 67 65 64 20 74 6f 20 61 20 4e 6f 6f 70 2e 20 nged to a Noop.
16f05 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65 In this way, we
16f06 20 61 76 6f 69 64 20 63 72 65 61 74 69 6e 67 20 avoid creating
16f07 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 0a 2a the statement .*
16f08 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75 * journal file u
16f09 6e 6e 65 63 65 73 73 61 72 69 6c 79 2e 0a 2a 2f nnecessarily..*/
16f0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 .static void res
16f0b 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 56 64 62 olveP2Values(Vdb
16f0c 65 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 78 46 e *p, int *pMaxF
16f0d 75 6e 63 41 72 67 73 29 7b 0a 20 20 69 6e 74 20 uncArgs){. int
16f0e 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 i;. int nMaxArg
16f0f 73 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 s = 0;. Op *pOp
16f10 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 20 ;. int *aLabel
16f11 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20 69 = p->aLabel;. i
16f12 6e 74 20 64 6f 65 73 53 74 61 74 65 6d 65 6e 74 nt doesStatement
16f13 52 6f 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 Rollback = 0;.
16f14 69 6e 74 20 68 61 73 53 74 61 74 65 6d 65 6e 74 int hasStatement
16f15 42 65 67 69 6e 20 3d 20 30 3b 0a 20 20 70 2d 3e Begin = 0;. p->
16f16 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 readOnly = 1;.
16f17 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e p->usesStmtJourn
16f18 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 4f al = 0;. for(pO
16f19 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e p=p->aOp, i=p->n
16f1a 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c Op-1; i>=0; i--,
16f1b 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20 pOp++){. u8
16f1c 6f 70 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 opcode = pOp->op
16f1d 63 6f 64 65 3b 0a 0a 20 20 20 20 69 66 28 20 6f code;.. if( o
16f1e 70 63 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69 pcode==OP_Functi
16f1f 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 on || opcode==OP
16f20 5f 41 67 67 53 74 65 70 20 29 7b 0a 20 20 20 20 _AggStep ){.
16f21 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 3e 6e 4d if( pOp->p5>nM
16f22 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 axArgs ) nMaxArg
16f23 73 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 23 69 66 s = pOp->p5;.#if
16f24 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16f25 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
16f26 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f }else if( opco
16f27 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 29 de==OP_VUpdate )
16f28 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d {. if( pOp-
16f29 3e 70 32 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e >p2>nMaxArgs ) n
16f2a 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 MaxArgs = pOp->p
16f2b 32 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 2;.#endif. }.
16f2c 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d if( opcode==
16f2d 4f 50 5f 48 61 6c 74 20 29 7b 0a 20 20 20 20 20 OP_Halt ){.
16f2e 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 53 51 if( pOp->p1==SQ
16f2f 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 LITE_CONSTRAINT
16f30 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 && pOp->p2==OE_A
16f31 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 bort ){.
16f32 64 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c doesStatementRol
16f33 6c 62 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 lback = 1;.
16f34 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
16f35 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 74 61 74 opcode==OP_Stat
16f36 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 68 ement ){. h
16f37 61 73 53 74 61 74 65 6d 65 6e 74 42 65 67 69 6e asStatementBegin
16f38 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 75 = 1;. p->u
16f39 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d sesStmtJournal =
16f3a 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 1;. }else if
16f3b 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 44 65 73 ( opcode==OP_Des
16f3c 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 64 6f troy ){. do
16f3d 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 esStatementRollb
16f3e 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c ack = 1;. }el
16f3f 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f se if( opcode==O
16f40 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 P_Transaction &&
16f41 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 7b 0a 20 pOp->p2!=0 ){.
16f42 20 20 20 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 p->readOnly
16f43 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 = 0;.#ifndef SQ
16f44 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
16f45 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 LTABLE. }else
16f46 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f if( opcode==OP_
16f47 56 55 70 64 61 74 65 20 7c 7c 20 6f 70 63 6f 64 VUpdate || opcod
16f48 65 3d 3d 4f 50 5f 56 52 65 6e 61 6d 65 20 29 7b e==OP_VRename ){
16f49 0a 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 . doesState
16f4a 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 mentRollback = 1
16f4b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
16f4c 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 opcode==OP_VFilt
16f4d 65 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 er ){. int
16f4e 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 n;. assert(
16f4f 20 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 p->nOp - i >= 3
16f50 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
16f51 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 ( pOp[-1].opcode
16f52 3d 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a ==OP_Integer );.
16f53 20 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 n = pOp[-1
16f54 5d 2e 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 ].p1;. if(
16f55 6e 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 n>nMaxArgs ) nMa
16f56 78 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 xArgs = n;.#endi
16f57 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 f. }.. if(
16f58 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f sqlite3VdbeOpco
16f59 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 6f 70 deHasProperty(op
16f5a 63 6f 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50 code, OPFLG_JUMP
16f5b 29 20 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 ) && pOp->p2<0 )
16f5c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
16f5d 2d 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c -1-pOp->p2<p->nL
16f5e 61 62 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f abel );. pO
16f5f 70 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d p->p2 = aLabel[-
16f60 31 2d 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 1-pOp->p2];.
16f61 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 }. }. sqlite3D
16f62 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e bFree(p->db, p->
16f63 61 4c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c aLabel);. p->aL
16f64 61 62 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d abel = 0;.. *pM
16f65 61 78 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 axFuncArgs = nMa
16f66 78 41 72 67 73 3b 0a 0a 20 20 2f 2a 20 49 66 20 xArgs;.. /* If
16f67 77 65 20 6e 65 76 65 72 20 72 6f 6c 6c 62 61 63 we never rollbac
16f68 6b 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 k a statement tr
16f69 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 ansaction, then
16f6a 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 statement. ** t
16f6b 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 ransactions are
16f6c 6e 6f 74 20 6e 65 65 64 65 64 2e 20 20 53 6f 20 not needed. So
16f6d 63 68 61 6e 67 65 20 65 76 65 72 79 20 4f 50 5f change every OP_
16f6e 53 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 6f Statement. ** o
16f6f 70 63 6f 64 65 20 69 6e 74 6f 20 61 6e 20 4f 50 pcode into an OP
16f70 5f 4e 6f 6f 70 2e 20 20 54 68 69 73 20 61 76 6f _Noop. This avo
16f71 69 64 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c id a call to sql
16f72 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73 ite3OsOpenExclus
16f73 69 76 65 28 29 0a 20 20 2a 2a 20 77 68 69 63 68 ive(). ** which
16f74 20 63 61 6e 20 62 65 20 65 78 70 65 6e 73 69 76 can be expensiv
16f75 65 20 6f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f e on some platfo
16f76 72 6d 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 rms.. */. if(
16f77 68 61 73 53 74 61 74 65 6d 65 6e 74 42 65 67 69 hasStatementBegi
16f78 6e 20 26 26 20 21 64 6f 65 73 53 74 61 74 65 6d n && !doesStatem
16f79 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 entRollback ){.
16f7a 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f p->usesStmtJo
16f7b 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 66 urnal = 0;. f
16f7c 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69 or(pOp=p->aOp, i
16f7d 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b =p->nOp-1; i>=0;
16f7e 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 i--, pOp++){.
16f7f 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 if( pOp->opc
16f80 6f 64 65 3d 3d 4f 50 5f 53 74 61 74 65 6d 65 6e ode==OP_Statemen
16f81 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 70 t ){. pOp
16f82 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f ->opcode = OP_No
16f83 6f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 op;. }.
16f84 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 }. }.}../*.** R
16f85 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 eturn the addres
16f86 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e s of the next in
16f87 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 struction to be
16f88 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c inserted..*/.SQL
16f89 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
16f8a 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
16f8b 6e 74 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b ntAddr(Vdbe *p){
16f8c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 . assert( p->ma
16f8d 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic==VDBE_MAGIC_
16f8e 49 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e INIT );. return
16f8f 20 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a p->nOp;.}../*.*
16f90 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69 * Add a whole li
16f91 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 st of operations
16f92 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f to the operatio
16f93 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e n stack. Return
16f94 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20 the.** address
16f95 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f 70 65 of the first ope
16f96 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f ration added..*/
16f97 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16f98 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
16f99 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70 ddOpList(Vdbe *p
16f9a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f , int nOp, VdbeO
16f9b 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 pList const *aOp
16f9c 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 ){. int addr;.
16f9d 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
16f9e 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e c==VDBE_MAGIC_IN
16f9f 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e IT );. if( p->n
16fa0 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f Op + nOp > p->nO
16fa1 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70 pAlloc && growOp
16fa2 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 Array(p) ){.
16fa3 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
16fa4 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 addr = p->nOp;.
16fa5 20 69 66 28 20 6e 4f 70 3e 30 20 29 7b 0a 20 20 if( nOp>0 ){.
16fa6 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 int i;. Vdb
16fa7 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70 eOpList const *p
16fa8 49 6e 20 3d 20 61 4f 70 3b 0a 20 20 20 20 66 6f In = aOp;. fo
16fa9 72 28 69 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b r(i=0; i<nOp; i+
16faa 2b 2c 20 70 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 +, pIn++){.
16fab 20 69 6e 74 20 70 32 20 3d 20 70 49 6e 2d 3e 70 int p2 = pIn->p
16fac 32 3b 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2;. VdbeOp
16fad 2a 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4f 70 5b *pOut = &p->aOp[
16fae 69 2b 61 64 64 72 5d 3b 0a 20 20 20 20 20 20 70 i+addr];. p
16faf 4f 75 74 2d 3e 6f 70 63 6f 64 65 20 3d 20 70 49 Out->opcode = pI
16fb0 6e 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 20 n->opcode;.
16fb1 20 70 4f 75 74 2d 3e 70 31 20 3d 20 70 49 6e 2d pOut->p1 = pIn-
16fb2 3e 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 >p1;. if( p
16fb3 32 3c 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 2<0 && sqlite3Vd
16fb4 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65 beOpcodeHasPrope
16fb5 72 74 79 28 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 rty(pOut->opcode
16fb6 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 29 7b , OPFLG_JUMP) ){
16fb7 0a 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 . pOut->p
16fb8 32 20 3d 20 61 64 64 72 20 2b 20 41 44 44 52 28 2 = addr + ADDR(
16fb9 70 32 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 p2);. }else
16fba 7b 0a 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e {. pOut->
16fbb 70 32 20 3d 20 70 32 3b 0a 20 20 20 20 20 20 7d p2 = p2;. }
16fbc 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 33 20 . pOut->p3
16fbd 3d 20 70 49 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 = pIn->p3;.
16fbe 20 70 4f 75 74 2d 3e 70 34 74 79 70 65 20 3d 20 pOut->p4type =
16fbf 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 P4_NOTUSED;.
16fc0 20 20 70 4f 75 74 2d 3e 70 34 2e 70 20 3d 20 30 pOut->p4.p = 0
16fc1 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 35 ;. pOut->p5
16fc2 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c = 0;.#ifdef SQL
16fc3 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 ITE_DEBUG.
16fc4 70 4f 75 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d pOut->zComment =
16fc5 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 0;. if( sq
16fc6 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 lite3VdbeAddopTr
16fc7 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ace ){. s
16fc8 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f qlite3VdbePrintO
16fc9 70 28 30 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d p(0, i+addr, &p-
16fca 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 >aOp[i+addr]);.
16fcb 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
16fcc 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b }. p->nOp +
16fcd 3d 20 6e 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 = nOp;. }. ret
16fce 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a urn addr;.}../*.
16fcf 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 ** Change the va
16fd0 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20 6f 70 lue of the P1 op
16fd1 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 erand for a spec
16fd2 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e ific instruction
16fd3 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
16fd4 65 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e e is useful when
16fd5 20 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d a large program
16fd6 20 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 is loaded from
16fd7 61 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 a.** static arra
16fd8 79 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 y using sqlite3V
16fd9 64 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 dbeAddOpList but
16fda 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 we want to make
16fdb 20 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 a.** few minor
16fdc 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 changes to the p
16fdd 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 rogram..*/.SQLIT
16fde 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
16fdf 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
16fe0 50 31 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 P1(Vdbe *p, int
16fe1 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a addr, int val){.
16fe2 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c assert( p==0 |
16fe3 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 | p->magic==VDBE
16fe4 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
16fe5 20 69 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d if( p && addr>=
16fe6 30 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 0 && p->nOp>addr
16fe7 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 && p->aOp ){.
16fe8 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 p->aOp[addr].p
16fe9 31 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 1 = val;. }.}..
16fea 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
16feb 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 value of the P2
16fec 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 operand for a s
16fed 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 pecific instruct
16fee 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ion..** This rou
16fef 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c 20 66 tine is useful f
16ff0 6f 72 20 73 65 74 74 69 6e 67 20 61 20 6a 75 6d or setting a jum
16ff1 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a p destination..*
16ff2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16ff3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
16ff4 65 43 68 61 6e 67 65 50 32 28 56 64 62 65 20 2a eChangeP2(Vdbe *
16ff5 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 p, int addr, int
16ff6 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 val){. assert(
16ff7 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 p==0 || p->magi
16ff8 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e c==VDBE_MAGIC_IN
16ff9 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26 26 IT );. if( p &&
16ffa 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e 6e addr>=0 && p->n
16ffb 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61 4f Op>addr && p->aO
16ffc 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b p ){. p->aOp[
16ffd 61 64 64 72 5d 2e 70 32 20 3d 20 76 61 6c 3b 0a addr].p2 = val;.
16ffe 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 }.}../*.** Cha
16fff 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 nge the value of
17000 20 74 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 the P3 operand
17001 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 for a specific i
17002 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 nstruction..*/.S
17003 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
17004 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
17005 61 6e 67 65 50 33 28 56 64 62 65 20 2a 70 2c 20 angeP3(Vdbe *p,
17006 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61 int addr, int va
17007 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d l){. assert( p=
17008 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d =0 || p->magic==
17009 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
1700a 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 61 64 );. if( p && ad
1700b 64 72 3e 3d 30 20 26 26 20 70 2d 3e 6e 4f 70 3e dr>=0 && p->nOp>
1700c 61 64 64 72 20 26 26 20 70 2d 3e 61 4f 70 20 29 addr && p->aOp )
1700d 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 {. p->aOp[add
1700e 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a 20 20 7d r].p3 = val;. }
1700f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
17010 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
17011 65 20 50 35 20 6f 70 65 72 61 6e 64 20 66 6f 72 e P5 operand for
17012 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
17013 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f 70 65 72 ly.** added oper
17014 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ation..*/.SQLITE
17015 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
17016 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
17017 35 28 56 64 62 65 20 2a 70 2c 20 75 38 20 76 61 5(Vdbe *p, u8 va
17018 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d l){. assert( p=
17019 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d =0 || p->magic==
1701a 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
1701b 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d );. if( p && p-
1701c 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65 >aOp ){. asse
1701d 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a rt( p->nOp>0 );.
1701e 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f p->aOp[p->nO
1701f 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20 p-1].p5 = val;.
17020 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
17021 67 65 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e ge the P2 operan
17022 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e d of instruction
17023 20 61 64 64 72 20 73 6f 20 74 68 61 74 20 69 74 addr so that it
17024 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 points to.** th
17025 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
17026 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
17027 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a n to be coded..*
17028 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
17029 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1702a 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a eJumpHere(Vdbe *
1702b 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 p, int addr){.
1702c 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1702d 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e eP2(p, addr, p->
1702e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 nOp);.}.../*.**
1702f 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e If the input Fun
17030 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69 cDef structure i
17031 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65 s ephemeral, the
17032 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a n free it. If.*
17033 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69 73 * the FuncDef is
17034 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74 not ephermal, t
17035 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a hen do nothing..
17036 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 */.static void f
17037 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 reeEphemeralFunc
17038 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 tion(sqlite3 *db
17039 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29 , FuncDef *pDef)
1703a 7b 0a 20 20 69 66 28 20 70 44 65 66 20 26 26 20 {. if( pDef &&
1703b 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 (pDef->flags & S
1703c 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d QLITE_FUNC_EPHEM
1703d 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 )!=0 ){. sqli
1703e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 te3DbFree(db, pD
1703f 65 66 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ef);. }.}../*.*
17040 2a 20 44 65 6c 65 74 65 20 61 20 50 34 20 76 61 * Delete a P4 va
17041 6c 75 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 lue if necessary
17042 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
17043 20 66 72 65 65 50 34 28 73 71 6c 69 74 65 33 20 freeP4(sqlite3
17044 2a 64 62 2c 20 69 6e 74 20 70 34 74 79 70 65 2c *db, int p4type,
17045 20 76 6f 69 64 20 2a 70 34 29 7b 0a 20 20 69 66 void *p4){. if
17046 28 20 70 34 20 29 7b 0a 20 20 20 20 73 77 69 74 ( p4 ){. swit
17047 63 68 28 20 70 34 74 79 70 65 20 29 7b 0a 20 20 ch( p4type ){.
17048 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c case P4_REAL
17049 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f :. case P4_
1704a 49 4e 54 36 34 3a 0a 20 20 20 20 20 20 63 61 73 INT64:. cas
1704b 65 20 50 34 5f 4d 50 52 49 4e 54 46 3a 0a 20 20 e P4_MPRINTF:.
1704c 20 20 20 20 63 61 73 65 20 50 34 5f 44 59 4e 41 case P4_DYNA
1704d 4d 49 43 3a 0a 20 20 20 20 20 20 63 61 73 65 20 MIC:. case
1704e 50 34 5f 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20 P4_KEYINFO:.
1704f 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 case P4_INTARR
17050 41 59 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 AY:. case P
17051 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 4_KEYINFO_HANDOF
17052 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c F: {. sql
17053 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
17054 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 4);. brea
17055 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
17056 20 63 61 73 65 20 50 34 5f 56 44 42 45 46 55 4e case P4_VDBEFUN
17057 43 3a 20 7b 0a 20 20 20 20 20 20 20 20 56 64 62 C: {. Vdb
17058 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 eFunc *pVdbeFunc
17059 20 3d 20 28 56 64 62 65 46 75 6e 63 20 2a 29 70 = (VdbeFunc *)p
1705a 34 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 45 4;. freeE
1705b 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e phemeralFunction
1705c 28 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2d 3e (db, pVdbeFunc->
1705d 70 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 pFunc);.
1705e 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
1705f 65 41 75 78 44 61 74 61 28 70 56 64 62 65 46 75 eAuxData(pVdbeFu
17060 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 nc, 0);.
17061 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
17062 2c 20 70 56 64 62 65 46 75 6e 63 29 3b 0a 20 20 , pVdbeFunc);.
17063 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
17064 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 }. case
17065 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 P4_FUNCDEF: {.
17066 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 freeEpheme
17067 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 ralFunction(db,
17068 28 46 75 6e 63 44 65 66 2a 29 70 34 29 3b 0a 20 (FuncDef*)p4);.
17069 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1706a 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 }. case
1706b 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 P4_MEM: {.
1706c 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 sqlite3ValueF
1706d 72 65 65 28 28 73 71 6c 69 74 65 33 5f 76 61 6c ree((sqlite3_val
1706e 75 65 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 ue*)p4);.
1706f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
17070 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a }. }.}.../*
17071 0a 2a 2a 20 43 68 61 6e 67 65 20 4e 20 6f 70 63 .** Change N opc
17072 6f 64 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 odes starting at
17073 20 61 64 64 72 20 74 6f 20 4e 6f 2d 6f 70 73 2e addr to No-ops.
17074 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
17075 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
17076 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 dbeChangeToNoop(
17077 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 Vdbe *p, int add
17078 72 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 r, int N){. if(
17079 20 70 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a p && p->aOp ){.
1707a 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 VdbeOp *pOp
1707b 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b = &p->aOp[addr];
1707c 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
1707d 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68 = p->db;. wh
1707e 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 ile( N-- ){.
1707f 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 freeP4(db, pOp
17080 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 ->p4type, pOp->p
17081 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 4.p);. mems
17082 65 74 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f et(pOp, 0, sizeo
17083 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 f(pOp[0]));.
17084 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 pOp->opcode =
17085 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70 OP_Noop;. p
17086 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Op++;. }. }.
17087 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
17088 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
17089 20 50 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 P4 operand for
1708a 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 a specific instr
1708b 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 uction..** This
1708c 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 routine is usefu
1708d 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 l when a large p
1708e 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 rogram is loaded
1708f 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 from a.** stati
17090 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 c array using sq
17091 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 lite3VdbeAddOpLi
17092 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 st but we want t
17093 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 o make a.** few
17094 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f minor changes to
17095 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a the program..**
17096 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e .** If n>=0 then
17097 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 the P4 operand
17098 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e is dynamic, mean
17099 69 6e 67 20 74 68 61 74 20 61 20 63 6f 70 79 20 ing that a copy
1709a 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 of.** the string
1709b 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65 is made into me
1709c 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
1709d 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
1709e 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20 c()..** A value
1709f 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f of n==0 means co
170a0 70 79 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20 py bytes of zP4
170a1 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 up to and includ
170a2 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 ing the.** first
170a3 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20 null byte. If
170a4 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b n>0 then copy n+
170a5 31 20 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a 1 bytes of zP4..
170a6 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b **.** If n==P4_K
170a7 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20 EYINFO it means
170a8 74 68 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f that zP4 is a po
170a9 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e inter to a KeyIn
170aa 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a fo structure..**
170ab 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 A copy is made
170ac 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 of the KeyInfo s
170ad 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65 tructure into me
170ae 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
170af 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 om.** sqlite3_ma
170b0 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65 lloc, to be free
170b1 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 d when the Vdbe
170b2 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a is finalized..**
170b3 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 n==P4_KEYINFO_H
170b4 41 4e 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73 ANDOFF indicates
170b5 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 that zP4 points
170b6 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 to a KeyInfo st
170b7 72 75 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65 ructure.** store
170b8 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74 d in memory that
170b9 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 the caller has
170ba 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
170bb 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68 lite3_malloc. Th
170bc 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f e .** caller sho
170bd 75 6c 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65 uld not free the
170be 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 allocation, it
170bf 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68 will be freed wh
170c0 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a en the Vdbe is.*
170c1 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 * finalized..**
170c2 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73 .** Other values
170c3 20 6f 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43 of n (P4_STATIC
170c4 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63 , P4_COLLSEQ etc
170c5 2e 29 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 .) indicate that
170c6 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 zP4 points.** t
170c7 6f 20 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74 o a string or st
170c8 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20 ructure that is
170c9 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 guaranteed to ex
170ca 69 73 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65 ist for the life
170cb 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56 time of.** the V
170cc 64 62 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61 dbe. In these ca
170cd 73 65 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20 ses we can just
170ce 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72 copy the pointer
170cf 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c ..**.** If addr<
170d0 30 20 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34 0 then change P4
170d1 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 on the most rec
170d2 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69 ently inserted i
170d3 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 nstruction..*/.S
170d4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
170d5 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
170d6 61 6e 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20 angeP4(Vdbe *p,
170d7 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 int addr, const
170d8 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e char *zP4, int n
170d9 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 ){. Op *pOp;.
170da 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61 sqlite3 *db;. a
170db 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
170dc 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 db = p->db;. a
170dd 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d ssert( p->magic=
170de 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 =VDBE_MAGIC_INIT
170df 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 );. if( p->aOp
170e0 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f ==0 || db->mallo
170e1 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 cFailed ){. i
170e2 66 20 28 6e 20 21 3d 20 50 34 5f 4b 45 59 49 4e f (n != P4_KEYIN
170e3 46 4f 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 FO) {. free
170e4 50 34 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a P4(db, n, (void*
170e5 29 2a 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b )*(char**)&zP4);
170e6 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
170e7 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 n;. }. assert(
170e8 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 20 29 3b 0a addr<p->nOp );.
170e9 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a if( addr<0 ){.
170ea 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f addr = p->nO
170eb 70 20 2d 20 31 3b 0a 20 20 20 20 69 66 28 20 61 p - 1;. if( a
170ec 64 64 72 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a ddr<0 ) return;.
170ed 20 20 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e }. pOp = &p->
170ee 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65 aOp[addr];. fre
170ef 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 eP4(db, pOp->p4t
170f0 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b ype, pOp->p4.p);
170f1 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 . pOp->p4.p = 0
170f2 3b 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e ;. if( n==P4_IN
170f3 54 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f T32 ){. /* No
170f4 74 65 3a 20 74 68 69 73 20 63 61 73 74 20 69 73 te: this cast is
170f5 20 73 61 66 65 2c 20 62 65 63 61 75 73 65 20 74 safe, because t
170f6 68 65 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70 he origin data p
170f7 6f 69 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a oint was an int.
170f8 20 20 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20 ** that was
170f9 63 61 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74 cast to a (const
170fa 20 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 char *). */.
170fb 20 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c pOp->p4.i = SQL
170fc 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a ITE_PTR_TO_INT(z
170fd 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 P4);. pOp->p4
170fe 74 79 70 65 20 3d 20 6e 3b 0a 20 20 7d 65 6c 73 type = n;. }els
170ff 65 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a e if( zP4==0 ){.
17100 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 pOp->p4.p =
17101 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 0;. pOp->p4ty
17102 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b pe = P4_NOTUSED;
17103 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d . }else if( n==
17104 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 P4_KEYINFO ){.
17105 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
17106 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 nfo;. int nFi
17107 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20 eld, nByte;..
17108 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49 nField = ((KeyI
17109 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c nfo*)zP4)->nFiel
1710a 64 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73 d;. nByte = s
1710b 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 izeof(*pKeyInfo)
1710c 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69 + (nField-1)*si
1710d 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 zeof(pKeyInfo->a
1710e 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c Coll[0]) + nFiel
1710f 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 d;. pKeyInfo
17110 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
17111 20 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f nByte );. pO
17112 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d p->p4.pKeyInfo =
17113 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 pKeyInfo;. i
17114 66 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 f( pKeyInfo ){.
17115 20 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 u8 *aSortOr
17116 64 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 der;. memcp
17117 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c y(pKeyInfo, zP4,
17118 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 nByte);. a
17119 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 SortOrder = pKey
1711a 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 Info->aSortOrder
1711b 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53 6f 72 ;. if( aSor
1711c 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20 tOrder ){.
1711d 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 pKeyInfo->aSor
1711e 74 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e tOrder = (unsign
1711f 65 64 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e ed char*)&pKeyIn
17120 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64 fo->aColl[nField
17121 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ];. memcp
17122 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 y(pKeyInfo->aSor
17123 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64 tOrder, aSortOrd
17124 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20 er, nField);.
17125 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e }. pOp->
17126 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 p4type = P4_KEYI
17127 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a NFO;. }else{.
17128 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c p->db->mal
17129 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
1712a 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 pOp->p4type
1712b 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 = P4_NOTUSED;.
1712c 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
1712d 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 n==P4_KEYINFO_H
1712e 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f ANDOFF ){. pO
1712f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a p->p4.p = (void*
17130 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 )zP4;. pOp->p
17131 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 4type = P4_KEYIN
17132 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 FO;. }else if(
17133 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e n<0 ){. pOp->
17134 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 p4.p = (void*)zP
17135 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 4;. pOp->p4ty
17136 70 65 20 3d 20 6e 3b 0a 20 20 7d 65 6c 73 65 7b pe = n;. }else{
17137 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 . if( n==0 )
17138 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 50 34 29 3b n = strlen(zP4);
17139 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d . pOp->p4.z =
1713a 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 sqlite3DbStrNDu
1713b 70 28 70 2d 3e 64 62 2c 20 7a 50 34 2c 20 6e 29 p(p->db, zP4, n)
1713c 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 ;. pOp->p4typ
1713d 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a e = P4_DYNAMIC;.
1713e 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e }.}..#ifndef N
1713f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 43 68 61 6e DEBUG./*.** Chan
17140 67 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f ge the comment o
17141 6e 20 74 68 65 20 74 68 65 20 6d 6f 73 74 20 72 n the the most r
17142 65 63 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e ecently coded in
17143 73 74 72 75 63 74 69 6f 6e 2e 20 20 4f 72 0a 2a struction. Or.*
17144 2a 20 69 6e 73 65 72 74 20 61 20 4e 6f 2d 6f 70 * insert a No-op
17145 20 61 6e 64 20 61 64 64 20 74 68 65 20 63 6f 6d and add the com
17146 6d 65 6e 74 20 74 6f 20 74 68 61 74 20 6e 65 77 ment to that new
17147 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 54 instruction. T
17148 68 69 73 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 his.** makes the
17149 20 63 6f 64 65 20 65 61 73 69 65 72 20 74 6f 20 code easier to
1714a 72 65 61 64 20 64 75 72 69 6e 67 20 64 65 62 75 read during debu
1714b 67 67 69 6e 67 2e 20 20 4e 6f 6e 65 20 6f 66 20 gging. None of
1714c 74 68 69 73 20 68 61 70 70 65 6e 73 0a 2a 2a 20 this happens.**
1714d 69 6e 20 61 20 70 72 6f 64 75 63 74 69 6f 6e 20 in a production
1714e 62 75 69 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 build..*/.SQLITE
1714f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
17150 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 lite3VdbeComment
17151 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 (Vdbe *p, const
17152 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
17153 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
17154 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e p;. assert( p->
17155 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d nOp>0 || p->aOp=
17156 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
17157 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e p->aOp==0 || p->
17158 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 aOp[p->nOp-1].zC
17159 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e omment==0 || p->
1715a 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1715b 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 );. if( p->nOp
1715c 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 ){. char **p
1715d 7a 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e z = &p->aOp[p->n
1715e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a Op-1].zComment;.
1715f 20 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c va_start(ap,
17160 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 zFormat);. s
17161 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e qlite3DbFree(p->
17162 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 db, *pz);. *p
17163 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 z = sqlite3VMPri
17164 6e 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d ntf(p->db, zForm
17165 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f at, ap);. va_
17166 65 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 53 end(ap);. }.}.S
17167 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
17168 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f id sqlite3VdbeNo
17169 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a opComment(Vdbe *
1716a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a p, const char *z
1716b 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 Format, ...){.
1716c 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 73 71 va_list ap;. sq
1716d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
1716e 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61 p, OP_Noop);. a
1716f 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 ssert( p->nOp>0
17170 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a || p->aOp==0 );.
17171 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 assert( p->aOp
17172 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d ==0 || p->aOp[p-
17173 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 >nOp-1].zComment
17174 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 ==0 || p->db->ma
17175 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
17176 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 if( p->nOp ){.
17177 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 char **pz = &p
17178 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e ->aOp[p->nOp-1].
17179 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 zComment;. va
1717a 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
1717b 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 at);. sqlite3
1717c 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 DbFree(p->db, *p
1717d 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 z);. *pz = sq
1717e 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d lite3VMPrintf(p-
1717f 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 >db, zFormat, ap
17180 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 );. va_end(ap
17181 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 );. }.}.#endif
17182 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f /* NDEBUG */../
17183 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
17184 6f 70 63 6f 64 65 20 66 6f 72 20 61 20 67 69 76 opcode for a giv
17185 65 6e 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 53 en address..*/.S
17186 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
17187 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62 beOp *sqlite3Vdb
17188 65 47 65 74 4f 70 28 56 64 62 65 20 2a 70 2c 20 eGetOp(Vdbe *p,
17189 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 61 73 73 int addr){. ass
1718a 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 ert( p->magic==V
1718b 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 DBE_MAGIC_INIT )
1718c 3b 0a 20 20 61 73 73 65 72 74 28 20 28 61 64 64 ;. assert( (add
1718d 72 3e 3d 30 20 26 26 20 61 64 64 72 3c 70 2d 3e r>=0 && addr<p->
1718e 6e 4f 70 29 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d nOp) || p->db->m
1718f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
17190 20 72 65 74 75 72 6e 20 28 28 61 64 64 72 3e 3d return ((addr>=
17191 30 20 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 0 && addr<p->nOp
17192 29 3f 28 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d )?(&p->aOp[addr]
17193 29 3a 30 29 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 ):0);.}..#if !de
17194 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
17195 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21 64 T_EXPLAIN) || !d
17196 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 5c efined(NDEBUG) \
17197 0a 20 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 . || defined
17198 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c (VDBE_PROFILE) |
17199 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
1719a 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 43 6f _DEBUG)./*.** Co
1719b 6d 70 75 74 65 20 61 20 73 74 72 69 6e 67 20 74 mpute a string t
1719c 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 hat describes th
1719d 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 66 e P4 parameter f
1719e 6f 72 20 61 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a or an opcode..**
1719f 20 55 73 65 20 7a 54 65 6d 70 20 66 6f 72 20 61 Use zTemp for a
171a0 6e 79 20 72 65 71 75 69 72 65 64 20 74 65 6d 70 ny required temp
171a1 6f 72 61 72 79 20 62 75 66 66 65 72 20 73 70 61 orary buffer spa
171a2 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 ce..*/.static ch
171a3 61 72 20 2a 64 69 73 70 6c 61 79 50 34 28 4f 70 ar *displayP4(Op
171a4 20 2a 70 4f 70 2c 20 63 68 61 72 20 2a 7a 54 65 *pOp, char *zTe
171a5 6d 70 2c 20 69 6e 74 20 6e 54 65 6d 70 29 7b 0a mp, int nTemp){.
171a6 20 20 63 68 61 72 20 2a 7a 50 34 20 3d 20 7a 54 char *zP4 = zT
171a7 65 6d 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e emp;. assert( n
171a8 54 65 6d 70 3e 3d 32 30 20 29 3b 0a 20 20 73 77 Temp>=20 );. sw
171a9 69 74 63 68 28 20 70 4f 70 2d 3e 70 34 74 79 70 itch( pOp->p4typ
171aa 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 50 34 e ){. case P4
171ab 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 3a _KEYINFO_STATIC:
171ac 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 . case P4_KEY
171ad 49 4e 46 4f 3a 20 7b 0a 20 20 20 20 20 20 69 6e INFO: {. in
171ae 74 20 69 2c 20 6a 3b 0a 20 20 20 20 20 20 4b 65 t i, j;. Ke
171af 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 yInfo *pKeyInfo
171b0 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e = pOp->p4.pKeyIn
171b1 66 6f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 fo;. sqlite
171b2 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
171b3 2c 20 7a 54 65 6d 70 2c 20 22 6b 65 79 69 6e 66 , zTemp, "keyinf
171b4 6f 28 25 64 22 2c 20 70 4b 65 79 49 6e 66 6f 2d o(%d", pKeyInfo-
171b5 3e 6e 46 69 65 6c 64 29 3b 0a 20 20 20 20 20 20 >nField);.
171b6 69 20 3d 20 73 74 72 6c 65 6e 28 7a 54 65 6d 70 i = strlen(zTemp
171b7 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 );. for(j=0
171b8 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 ; j<pKeyInfo->nF
171b9 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 ield; j++){.
171ba 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f CollSeq *pCo
171bb 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 ll = pKeyInfo->a
171bc 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 Coll[j];.
171bd 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( pColl ){.
171be 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 int n =
171bf 73 74 72 6c 65 6e 28 70 43 6f 6c 6c 2d 3e 7a 4e strlen(pColl->zN
171c0 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ame);.
171c1 69 66 28 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 if( i+n>nTemp-6
171c2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d ){. m
171c3 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c emcpy(&zTemp[i],
171c4 22 2c 2e 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 ",...",4);.
171c5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
171c6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
171c7 20 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d zTemp[i++] =
171c8 20 27 2c 27 3b 0a 20 20 20 20 20 20 20 20 20 20 ',';.
171c9 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 if( pKeyInfo->aS
171ca 6f 72 74 4f 72 64 65 72 20 26 26 20 70 4b 65 79 ortOrder && pKey
171cb 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 Info->aSortOrder
171cc 5b 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 [j] ){.
171cd 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 zTemp[i++] =
171ce 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d '-';. }
171cf 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
171d0 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f y(&zTemp[i], pCo
171d1 6c 6c 2d 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a ll->zName,n+1);.
171d2 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 6e i += n
171d3 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 ;. }else
171d4 69 66 28 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 if( i+4<nTemp-6
171d5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d ){. mem
171d6 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c cpy(&zTemp[i],",
171d7 6e 69 6c 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 nil",4);.
171d8 20 20 20 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20 i += 4;.
171d9 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
171da 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 zTemp[i++] =
171db 27 29 27 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70 ')';. zTemp
171dc 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 [i] = 0;. a
171dd 73 73 65 72 74 28 20 69 3c 6e 54 65 6d 70 20 29 ssert( i<nTemp )
171de 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
171df 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 }. case P4
171e0 5f 43 4f 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20 _COLLSEQ: {.
171e1 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
171e2 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c = pOp->p4.pColl
171e3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
171e4 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
171e5 7a 54 65 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28 zTemp, "collseq(
171e6 25 2e 32 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e %.20s)", pColl->
171e7 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 zName);. br
171e8 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
171e9 61 73 65 20 50 34 5f 46 55 4e 43 44 45 46 3a 20 ase P4_FUNCDEF:
171ea 7b 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 {. FuncDef
171eb 2a 70 44 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e *pDef = pOp->p4.
171ec 70 46 75 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c pFunc;. sql
171ed 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
171ee 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28 emp, zTemp, "%s(
171ef 25 64 29 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d %d)", pDef->zNam
171f0 65 2c 20 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a e, pDef->nArg);.
171f1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
171f2 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 }. case P4_I
171f3 4e 54 36 34 3a 20 7b 0a 20 20 20 20 20 20 73 71 NT64: {. sq
171f4 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
171f5 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c Temp, zTemp, "%l
171f6 6c 64 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 ld", *pOp->p4.pI
171f7 36 34 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 64);. break
171f8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
171f9 20 50 34 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20 P4_INT32: {.
171fa 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
171fb 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 ntf(nTemp, zTemp
171fc 2c 20 22 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e , "%d", pOp->p4.
171fd 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b i);. break;
171fe 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
171ff 50 34 5f 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20 P4_REAL: {.
17200 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
17201 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
17202 22 25 2e 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 "%.16g", *pOp->p
17203 34 2e 70 52 65 61 6c 29 3b 0a 20 20 20 20 20 20 4.pReal);.
17204 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
17205 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a case P4_MEM: {.
17206 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 Mem *pMem
17207 3d 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a = pOp->p4.pMem;.
17208 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
17209 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
1720a 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 _Null)==0 );.
1720b 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 if( pMem->fla
1720c 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a gs & MEM_Str ){.
1720d 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4d zP4 = pM
1720e 65 6d 2d 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c em->z;. }el
1720f 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 se if( pMem->fla
17210 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a gs & MEM_Int ){.
17211 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
17212 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
17213 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70 zTemp, "%lld", p
17214 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 Mem->u.i);.
17215 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d }else if( pMem-
17216 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 >flags & MEM_Rea
17217 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c l ){. sql
17218 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
17219 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 emp, zTemp, "%.1
1721a 36 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 6g", pMem->r);.
1721b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
1721c 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 ak;. }.#ifnde
1721d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1721e 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 63 RTUALTABLE. c
1721f 61 73 65 20 50 34 5f 56 54 41 42 3a 20 7b 0a 20 ase P4_VTAB: {.
17220 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 sqlite3_vta
17221 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e b *pVtab = pOp->
17222 70 34 2e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 p4.pVtab;.
17223 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
17224 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 (nTemp, zTemp, "
17225 76 74 61 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 vtab:%p:%p", pVt
17226 61 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 ab, pVtab->pModu
17227 6c 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b le);. break
17228 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
17229 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 case P4_INTAR
1722a 52 41 59 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c RAY: {. sql
1722b 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
1722c 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 emp, zTemp, "int
1722d 61 72 72 61 79 22 29 3b 0a 20 20 20 20 20 20 62 array");. b
1722e 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1722f 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
17230 20 7a 50 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a zP4 = pOp->p4.z
17231 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 34 3d ;. if( zP4=
17232 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 =0 ){. zP
17233 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 4 = zTemp;.
17234 20 20 20 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b zTemp[0] = 0;
17235 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
17236 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 50 34 }. assert( zP4
17237 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 !=0 );. return
17238 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f zP4;.}.#endif../
17239 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 *.** Declare to
1723a 74 68 65 20 56 64 62 65 20 74 68 61 74 20 74 68 the Vdbe that th
1723b 65 20 42 54 72 65 65 20 6f 62 6a 65 63 74 20 61 e BTree object a
1723c 74 20 64 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 t db->aDb[i] is
1723d 75 73 65 64 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 used..**.*/.SQLI
1723e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1723f 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 sqlite3VdbeUsesB
17240 74 72 65 65 28 56 64 62 65 20 2a 70 2c 20 69 6e tree(Vdbe *p, in
17241 74 20 69 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b t i){. int mask
17242 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 ;. assert( i>=0
17243 20 26 26 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 && i<p->db->nDb
17244 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c );. assert( i<
17245 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 (int)sizeof(p->b
17246 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 treeMask)*8 );.
17247 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 3b 0a 20 20 mask = 1<<i;.
17248 69 66 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 if( (p->btreeMas
17249 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a k & mask)==0 ){.
1724a 20 20 20 20 70 2d 3e 62 74 72 65 65 4d 61 73 6b p->btreeMask
1724b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 73 71 |= mask;. sq
1724c 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
1724d 72 72 61 79 49 6e 73 65 72 74 28 26 70 2d 3e 61 rrayInsert(&p->a
1724e 4d 75 74 65 78 2c 20 70 2d 3e 64 62 2d 3e 61 44 Mutex, p->db->aD
1724f 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 7d 0a 7d b[i].pBt);. }.}
17250 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 56 ...#if defined(V
17251 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 DBE_PROFILE) ||
17252 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
17253 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e EBUG)./*.** Prin
17254 74 20 61 20 73 69 6e 67 6c 65 20 6f 70 63 6f 64 t a single opcod
17255 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 e. This routine
17256 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62 is used for deb
17257 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a ugging only..*/.
17258 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
17259 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 oid sqlite3VdbeP
1725a 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a 70 4f 75 rintOp(FILE *pOu
1725b 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70 20 2a 70 t, int pc, Op *p
1725c 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 Op){. char *zP4
1725d 3b 0a 20 20 63 68 61 72 20 7a 50 74 72 5b 35 30 ;. char zPtr[50
1725e 5d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ];. static cons
1725f 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 31 t char *zFormat1
17260 20 3d 20 22 25 34 64 20 25 2d 31 33 73 20 25 34 = "%4d %-13s %4
17261 64 20 25 34 64 20 25 34 64 20 25 2d 34 73 20 25 d %4d %4d %-4s %
17262 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20 69 66 28 .2X %s\n";. if(
17263 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f 75 74 20 pOut==0 ) pOut
17264 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a 50 34 20 = stdout;. zP4
17265 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70 2c = displayP4(pOp,
17266 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66 28 7a 50 zPtr, sizeof(zP
17267 74 72 29 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 tr));. fprintf(
17268 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74 31 2c 20 pOut, zFormat1,
17269 70 63 2c 20 0a 20 20 20 20 20 20 73 71 6c 69 74 pc, . sqlit
1726a 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70 e3OpcodeName(pOp
1726b 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f 70 2d 3e ->opcode), pOp->
1726c 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f 70 p1, pOp->p2, pOp
1726d 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f 70 2d 3e ->p3, zP4, pOp->
1726e 70 35 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 p5,.#ifdef SQLIT
1726f 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f E_DEBUG. pO
17270 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f 20 70 4f p->zComment ? pO
17271 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a 20 22 22 p->zComment : ""
17272 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 22 22 0a .#else. "".
17273 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20 66 66 #endif. );. ff
17274 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d 0a 23 65 lush(pOut);.}.#e
17275 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 ndif../*.** Rele
17276 61 73 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 ase an array of
17277 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 73 0a 2a N Mem elements.*
17278 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 /.static void re
17279 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 4d 65 leaseMemArray(Me
1727a 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 m *p, int N){.
1727b 69 66 28 20 70 20 26 26 20 4e 20 29 7b 0a 20 20 if( p && N ){.
1727c 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a 20 20 20 Mem *pEnd;.
1727d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1727e 2d 3e 64 62 3b 0a 20 20 20 20 69 6e 74 20 6d 61 ->db;. int ma
1727f 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20 64 62 lloc_failed = db
17280 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 0a ->mallocFailed;.
17281 20 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70 5b for(pEnd=&p[
17282 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b 29 N]; p<pEnd; p++)
17283 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
17284 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 20 7c 7c (&p[1])==pEnd ||
17285 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64 p[0].db==p[1].d
17286 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 b );.. /* T
17287 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 72 65 61 his block is rea
17288 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 64 20 76 lly an inlined v
17289 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 ersion of sqlite
1728a 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1728b 29 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 ). ** that
1728c 74 61 6b 65 73 20 61 64 76 61 6e 74 61 67 65 20 takes advantage
1728d 6f 66 20 74 68 65 20 66 61 63 74 20 74 68 61 74 of the fact that
1728e 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
1728f 20 76 61 6c 75 65 20 69 73 20 0a 20 20 20 20 20 value is .
17290 20 2a 2a 20 62 65 69 6e 67 20 73 65 74 20 74 6f ** being set to
17291 20 4e 55 4c 4c 20 61 66 74 65 72 20 72 65 6c 65 NULL after rele
17292 61 73 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d 69 asing any dynami
17293 63 20 72 65 73 6f 75 72 63 65 73 2e 0a 20 20 20 c resources..
17294 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 **. ** T
17295 68 65 20 6a 75 73 74 69 66 69 63 61 74 69 6f 6e he justification
17296 20 66 6f 72 20 64 75 70 6c 69 63 61 74 69 6e 67 for duplicating
17297 20 63 6f 64 65 20 69 73 20 74 68 61 74 20 61 63 code is that ac
17298 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20 20 20 20 cording to .
17299 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e 64 2c 20 ** callgrind,
1729a 74 68 69 73 20 63 61 75 73 65 73 20 61 20 63 65 this causes a ce
1729b 72 74 61 69 6e 20 74 65 73 74 20 63 61 73 65 20 rtain test case
1729c 74 6f 20 68 69 74 20 74 68 65 20 43 50 55 20 34 to hit the CPU 4
1729d 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20 70 65 72 .7 . ** per
1729e 63 65 6e 74 20 6c 65 73 73 20 28 78 38 36 20 6c cent less (x86 l
1729f 69 6e 75 78 2c 20 67 63 63 20 76 65 72 73 69 6f inux, gcc versio
172a0 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 20 74 68 n 4.1.2, -O6) th
172a1 61 6e 20 69 66 20 0a 20 20 20 20 20 20 2a 2a 20 an if . **
172a2 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c 65 61 73 sqlite3MemReleas
172a3 65 28 29 20 77 65 72 65 20 63 61 6c 6c 65 64 20 e() were called
172a4 66 72 6f 6d 20 68 65 72 65 2e 20 57 69 74 68 20 from here. With
172a5 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d 70 73 0a -O2, this jumps.
172a6 20 20 20 20 20 20 2a 2a 20 74 6f 20 36 2e 36 20 ** to 6.6
172a7 70 65 72 63 65 6e 74 2e 20 54 68 65 20 74 65 73 percent. The tes
172a8 74 20 63 61 73 65 20 69 73 20 69 6e 73 65 72 74 t case is insert
172a9 69 6e 67 20 31 30 30 30 20 72 6f 77 73 20 69 6e ing 1000 rows in
172aa 74 6f 20 61 20 74 61 62 6c 65 20 0a 20 20 20 20 to a table .
172ab 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20 69 6e 64 ** with no ind
172ac 65 78 65 73 20 75 73 69 6e 67 20 61 20 73 69 6e exes using a sin
172ad 67 6c 65 20 70 72 65 70 61 72 65 64 20 49 4e 53 gle prepared INS
172ae 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 62 ERT statement, b
172af 69 6e 64 28 29 20 0a 20 20 20 20 20 20 2a 2a 20 ind() . **
172b0 61 6e 64 20 72 65 73 65 74 28 29 2e 20 49 6e 73 and reset(). Ins
172b1 65 72 74 73 20 61 72 65 20 67 72 6f 75 70 65 64 erts are grouped
172b2 20 69 6e 74 6f 20 61 20 74 72 61 6e 73 61 63 74 into a transact
172b3 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion.. */.
172b4 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 if( p->flags
172b5 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 &(MEM_Agg|MEM_Dy
172b6 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 n) ){. sq
172b7 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
172b8 61 73 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 ase(p);. }e
172b9 6c 73 65 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c lse if( p->zMall
172ba 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 oc ){. sq
172bb 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
172bc 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 p->zMalloc);.
172bd 20 20 20 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 p->zMalloc
172be 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 = 0;. }..
172bf 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d p->flags = M
172c0 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 EM_Null;. }.
172c1 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
172c2 6c 65 64 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 led = malloc_fai
172c3 6c 65 64 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 led;. }.}..#ifd
172c4 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
172c5 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
172c6 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 NT.SQLITE_PRIVAT
172c7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
172c8 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28 eReleaseBuffers(
172c9 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Vdbe *p){. int
172ca 69 69 3b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 ii;. int nFree
172cb 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 = 0;. assert( s
172cc 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
172cd 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
172ce 29 3b 0a 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 );. for(ii=1; i
172cf 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 69 2b 2b i<=p->nMem; ii++
172d0 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d ){. Mem *pMem
172d1 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 69 69 5d 3b = &p->aMem[ii];
172d2 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a . if( pMem->z
172d3 20 26 26 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 && pMem->flags&
172d4 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 MEM_Dyn ){.
172d5 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 2d 3e assert( !pMem->
172d6 78 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 6e 46 xDel );. nF
172d7 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 44 62 ree += sqlite3Db
172d8 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d MallocSize(pMem-
172d9 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 >db, pMem->z);.
172da 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
172db 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 MemRelease(pMem)
172dc 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
172dd 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 turn nFree;.}.#e
172de 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
172df 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 LITE_OMIT_EXPLAI
172e0 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c N./*.** Give a l
172e1 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70 72 isting of the pr
172e2 6f 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69 72 ogram in the vir
172e3 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a tual machine..**
172e4 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 .** The interfac
172e5 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 e is the same as
172e6 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 sqlite3VdbeExec
172e7 28 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64 (). But instead
172e8 20 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 of.** running t
172e9 68 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f he code, it invo
172ea 6b 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b kes the callback
172eb 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 once for each i
172ec 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 nstruction..** T
172ed 68 69 73 20 66 65 61 74 75 72 65 20 69 73 20 75 his feature is u
172ee 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
172ef 20 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a "EXPLAIN"..**.*
172f0 2a 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 * When p->explai
172f1 6e 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72 n==1, each instr
172f2 75 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 uction is listed
172f3 2e 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78 . When.** p->ex
172f4 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f plain==2, only O
172f5 50 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75 P_Explain instru
172f6 63 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65 ctions are liste
172f7 64 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61 d and these.** a
172f8 72 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 69 re shown in a di
172f9 66 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20 fferent format.
172fa 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69 p->explain==2 i
172fb 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d s used to implem
172fc 65 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51 ent.** EXPLAIN Q
172fd 55 45 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 UERY PLAN..*/.SQ
172fe 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
172ff 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 sqlite3VdbeList
17300 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 20 (. Vdbe *p
17301 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17302 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a The VDBE */.){.
17303 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
17304 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b 0a p->db;. int i;.
17305 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
17306 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 E_OK;. Mem *pMe
17307 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 m = p->pResultSe
17308 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d 3b t = &p->aMem[1];
17309 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 .. assert( p->e
1730a 78 70 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28 20 xplain );. if(
1730b 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d p->magic!=VDBE_M
1730c 41 47 49 43 5f 52 55 4e 20 29 20 72 65 74 75 72 AGIC_RUN ) retur
1730d 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
1730e 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d . assert( db->m
1730f 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 agic==SQLITE_MAG
17310 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 61 73 73 IC_BUSY );. ass
17311 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 ert( p->rc==SQLI
17312 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d TE_OK || p->rc==
17313 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 0a SQLITE_BUSY );..
17314 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 /* Even though
17315 20 74 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 this opcode doe
17316 73 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 s not use dynami
17317 63 20 73 74 72 69 6e 67 73 20 66 6f 72 0a 20 20 c strings for.
17318 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c 20 72 ** the result, r
17319 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 esult columns ma
1731a 79 20 62 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 y become dynamic
1731b 20 69 66 20 74 68 65 20 75 73 65 72 20 63 61 6c if the user cal
1731c 6c 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f ls. ** sqlite3_
1731d 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c column_text16(),
1731e 20 63 61 75 73 69 6e 67 20 61 20 74 72 61 6e 73 causing a trans
1731f 6c 61 74 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 lation to UTF-16
17320 20 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a encoding.. */.
17321 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 releaseMemArra
17322 79 28 70 4d 65 6d 2c 20 70 2d 3e 6e 4d 65 6d 29 y(pMem, p->nMem)
17323 3b 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20 3d ;.. do{. i =
17324 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68 69 p->pc++;. }whi
17325 6c 65 28 20 69 3c 70 2d 3e 6e 4f 70 20 26 26 20 le( i<p->nOp &&
17326 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 p->explain==2 &&
17327 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 p->aOp[i].opcod
17328 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b e!=OP_Explain );
17329 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e 4f 70 . if( i>=p->nOp
1732a 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
1732b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 SQLITE_OK;. r
1732c 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
1732d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d . }else if( db-
1732e 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 >u1.isInterrupte
1732f 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d d ){. p->rc =
17330 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 SQLITE_INTERRUP
17331 54 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 T;. rc = SQLI
17332 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 TE_ERROR;. sq
17333 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
17334 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
17335 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 "%s", sqlite3Err
17336 53 74 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d Str(p->rc));. }
17337 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a else{. char *
17338 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d z;. Op *pOp =
17339 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 &p->aOp[i];.
1733a 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d if( p->explain=
1733b 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d =1 ){. pMem
1733c 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
1733d 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 t;. pMem->t
1733e 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 ype = SQLITE_INT
1733f 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d EGER;. pMem
17340 2d 3e 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20 ->u.i = i;
17341 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17342 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f /* Pro
17343 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a gram counter */.
17344 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 pMem++;.
17345 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
17346 67 73 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63 7c gs = MEM_Static|
17347 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d MEM_Str|MEM_Term
17348 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 ;. pMem->z
17349 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 = (char*)sqlite3
1734a 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e OpcodeName(pOp->
1734b 6f 70 63 6f 64 65 29 3b 20 20 2f 2a 20 4f 70 63 opcode); /* Opc
1734c 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 ode */. ass
1734d 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 ert( pMem->z!=0
1734e 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e );. pMem->n
1734f 20 3d 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e = strlen(pMem->
17350 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e z);. pMem->
17351 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 type = SQLITE_TE
17352 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e XT;. pMem->
17353 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 enc = SQLITE_UTF
17354 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 8;. pMem++;
17355 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d . }.. pMem
17356 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
17357 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 t;. pMem->u.i
17358 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20 = pOp->p1;
17359 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1735a 20 20 20 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 /* P1 */.
1735b 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
1735c 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 QLITE_INTEGER;.
1735d 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 pMem++;..
1735e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1735f 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d M_Int;. pMem-
17360 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 >u.i = pOp->p2;
17361 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17362 20 20 20 20 20 20 20 20 20 2f 2a 20 50 32 20 2a /* P2 *
17363 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 /. pMem->type
17364 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 = SQLITE_INTEGE
17365 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a R;. pMem++;..
17366 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 if( p->expla
17367 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 in==1 ){. p
17368 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
17369 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d _Int;. pMem
1736a 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b ->u.i = pOp->p3;
1736b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1736c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 33 20 /* P3
1736d 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 */. pMem->t
1736e 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 ype = SQLITE_INT
1736f 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d EGER;. pMem
17370 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 ++;. }.. i
17371 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
17372 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 33 32 2c 20 mGrow(pMem, 32,
17373 30 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 0) ){
17374 20 2f 2a 20 50 34 20 2a 2f 0a 20 20 20 20 20 20 /* P4 */.
17375 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->db->mallocFai
17376 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 led = 1;. r
17377 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
17378 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d EM;. }. pM
17379 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1737a 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f Dyn|MEM_Str|MEM_
1737b 54 65 72 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 Term;. z = di
1737c 73 70 6c 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 splayP4(pOp, pMe
1737d 6d 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 m->z, 32);. i
1737e 66 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b f( z!=pMem->z ){
1737f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
17380 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4d 65 6d beMemSetStr(pMem
17381 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f , z, -1, SQLITE_
17382 55 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 UTF8, 0);. }e
17383 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
17384 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b t( pMem->z!=0 );
17385 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d . pMem->n =
17386 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29 strlen(pMem->z)
17387 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e ;. pMem->en
17388 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b c = SQLITE_UTF8;
17389 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
1738a 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 >type = SQLITE_T
1738b 45 58 54 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b EXT;. pMem++;
1738c 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 .. if( p->exp
1738d 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 lain==1 ){.
1738e 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
1738f 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 34 2c MemGrow(pMem, 4,
17390 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 0) ){. p
17391 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
17392 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 ed = 1;.
17393 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
17394 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 MEM;. }.
17395 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
17396 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 MEM_Dyn|MEM_Str
17397 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 |MEM_Term;.
17398 20 70 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 pMem->n = 2;.
17399 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1739a 69 6e 74 66 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c intf(3, pMem->z,
1739b 20 22 25 2e 32 78 22 2c 20 70 4f 70 2d 3e 70 35 "%.2x", pOp->p5
1739c 29 3b 20 20 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 ); /* P5 */.
1739d 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1739e 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 SQLITE_TEXT;.
1739f 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 pMem->enc =
173a0 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 SQLITE_UTF8;.
173a1 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 pMem++;. .#i
173a2 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
173a3 47 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d G. if( pOp-
173a4 3e 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 >zComment ){.
173a5 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
173a6 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 = MEM_Str|MEM_T
173a7 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 erm;. pMe
173a8 6d 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d m->z = pOp->zCom
173a9 6d 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 4d ment;. pM
173aa 65 6d 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 70 em->n = strlen(p
173ab 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 20 Mem->z);.
173ac 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c pMem->enc = SQL
173ad 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 ITE_UTF8;.
173ae 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
173af 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 QLITE_TEXT;.
173b0 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 }else.#endif.
173b1 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 {. p
173b2 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
173b3 5f 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 _Null;
173b4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
173b5 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 Comment */.
173b6 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
173b7 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 SQLITE_NULL;.
173b8 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
173b9 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 p->nResColumn =
173ba 38 20 2d 20 35 2a 28 70 2d 3e 65 78 70 6c 61 69 8 - 5*(p->explai
173bb 6e 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 n-1);. p->rc
173bc 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
173bd 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 rc = SQLITE_ROW
173be 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
173bf 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 c;.}.#endif /* S
173c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 QLITE_OMIT_EXPLA
173c1 49 4e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 IN */..#ifdef SQ
173c2 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a LITE_DEBUG./*.**
173c3 20 50 72 69 6e 74 20 74 68 65 20 53 51 4c 20 74 Print the SQL t
173c4 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 hat was used to
173c5 67 65 6e 65 72 61 74 65 20 61 20 56 44 42 45 20 generate a VDBE
173c6 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 program..*/.SQLI
173c7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
173c8 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 sqlite3VdbePrint
173c9 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 Sql(Vdbe *p){.
173ca 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 int nOp = p->nOp
173cb 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b ;. VdbeOp *pOp;
173cc 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 . if( nOp<1 ) r
173cd 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 eturn;. pOp = &
173ce 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 p->aOp[0];. if(
173cf 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
173d0 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 _Trace && pOp->p
173d1 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 63 6f 4.z!=0 ){. co
173d2 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f nst char *z = pO
173d3 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 68 69 p->p4.z;. whi
173d4 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 38 le( isspace(*(u8
173d5 2a 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 *)z) ) z++;.
173d6 70 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25 73 printf("SQL: [%s
173d7 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a ]\n", z);. }.}.
173d8 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 #endif..#if !def
173d9 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
173da 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e _TRACE) && defin
173db 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ed(SQLITE_ENABLE
173dc 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 _IOTRACE)./*.**
173dd 50 72 69 6e 74 20 61 6e 20 49 4f 54 52 41 43 45 Print an IOTRACE
173de 20 6d 65 73 73 61 67 65 20 73 68 6f 77 69 6e 67 message showing
173df 20 53 51 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f SQL content..*/
173e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
173e1 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
173e2 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65 20 IOTraceSql(Vdbe
173e3 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d *p){. int nOp =
173e4 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f p->nOp;. VdbeO
173e5 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 73 71 p *pOp;. if( sq
173e6 6c 69 74 65 33 49 6f 54 72 61 63 65 3d 3d 30 20 lite3IoTrace==0
173e7 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
173e8 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a nOp<1 ) return;.
173e9 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b pOp = &p->aOp[
173ea 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 0];. if( pOp->o
173eb 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 pcode==OP_Trace
173ec 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 && pOp->p4.z!=0
173ed 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b ){. int i, j;
173ee 0a 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30 30 . char z[1000
173ef 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ];. sqlite3_s
173f0 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
173f1 29 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70 2d ), z, "%s", pOp-
173f2 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 66 6f 72 28 >p4.z);. for(
173f3 69 3d 30 3b 20 69 73 73 70 61 63 65 28 28 75 6e i=0; isspace((un
173f4 73 69 67 6e 65 64 20 63 68 61 72 29 7a 5b 69 5d signed char)z[i]
173f5 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f ); i++){}. fo
173f6 72 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b r(j=0; z[i]; i++
173f7 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 73 ){. if( iss
173f8 70 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 pace((unsigned c
173f9 68 61 72 29 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 har)z[i]) ){.
173fa 20 20 20 20 20 69 66 28 20 7a 5b 69 2d 31 5d 21 if( z[i-1]!
173fb 3d 27 20 27 20 29 7b 0a 20 20 20 20 20 20 20 20 =' ' ){.
173fc 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b 0a z[j++] = ' ';.
173fd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
173fe 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a }else{. z
173ff 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 [j++] = z[i];.
17400 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
17401 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 z[j] = 0;. sq
17402 6c 69 74 65 33 49 6f 54 72 61 63 65 28 22 53 51 lite3IoTrace("SQ
17403 4c 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d L %s\n", z);. }
17404 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 .}.#endif /* !SQ
17405 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 LITE_OMIT_TRACE
17406 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
17407 5f 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 0a 2f 2a _IOTRACE */.../*
17408 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 .** Prepare a vi
17409 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f rtual machine fo
1740a 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 r execution. Th
1740b 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e is involves thin
1740c 67 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c gs such.** as al
1740d 6c 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 locating stack s
1740e 70 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c pace and initial
1740f 69 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 izing the progra
17410 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 m counter..** Af
17411 74 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73 ter the VDBE has
17412 20 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 be prepped, it
17413 63 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 can be executed
17414 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a by one or more.*
17415 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 * calls to sqlit
17416 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a e3VdbeExec(). .
17417 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 **.** This is th
17418 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f e only way to mo
17419 76 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 ve a VDBE from V
1741a 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 DBE_MAGIC_INIT t
1741b 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f o.** VDBE_MAGIC_
1741c 52 55 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 RUN..*/.SQLITE_P
1741d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1741e 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 te3VdbeMakeReady
1741f 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 (. Vdbe *p,
17420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17421 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a /* The VDBE *
17422 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 /. int nVar,
17423 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17424 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
17425 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20 53 '?' see in the S
17426 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a QL statement */.
17427 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 int nMem,
17428 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17429 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 /* Number of me
1742a 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c mory cells to al
1742b 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 locate */. int
1742c 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20 nCursor,
1742d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1742e 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 mber of cursors
1742f 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 to allocate */.
17430 20 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 20 20 int isExplain
17431 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17432 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 45 /* True if the E
17433 58 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 XPLAIN keywords
17434 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 29 7b is present */.){
17435 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 . int n;. sqli
17436 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b te3 *db = p->db;
17437 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 .. assert( p!=0
17438 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
17439 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1743a 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f 2a IC_INIT );.. /*
1743b 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 There should be
1743c 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 at least one op
1743d 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 code.. */. ass
1743e 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b ert( p->nOp>0 );
1743f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d .. /* Set the m
17440 61 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41 47 agic to VDBE_MAG
17441 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72 61 IC_RUN sooner ra
17442 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 2e ther than later.
17443 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d */. p->magic =
17444 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b VDBE_MAGIC_RUN;
17445 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 .. /* For each
17446 63 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 2c cursor required,
17447 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 61 also allocate a
17448 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d 65 memory cell. Me
17449 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 mory. ** cells
1744a 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 29 (nMem+1-nCursor)
1744b 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 76 ..nMem, inclusiv
1744c 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 e, will never be
1744d 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68 used by. ** th
1744e 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 e vdbe program.
1744f 49 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 65 Instead they are
17450 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 used to allocat
17451 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a e space for. **
17452 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 75 VdbeCursor/BtCu
17453 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 2e rsor structures.
17454 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d The blob of mem
17455 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 ory associated w
17456 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 ith . ** cursor
17457 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 0 is stored in
17458 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 6d memory cell nMem
17459 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 6e . Memory cell (n
1745a 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f 72 Mem-1). ** stor
1745b 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d es the blob of m
1745c 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 emory associated
1745d 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c 20 with cursor 1,
1745e 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 etc.. **. ** S
1745f 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 74 ee also: allocat
17460 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f 0a eCursor().. */.
17461 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 6f nMem += nCurso
17462 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c r;.. /*. ** Al
17463 6c 6f 63 61 74 69 6f 6e 20 73 70 61 63 65 20 66 location space f
17464 6f 72 20 72 65 67 69 73 74 65 72 73 2e 0a 20 20 or registers..
17465 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d */. if( p->aMem
17466 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e ==0 ){. int n
17467 41 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 Arg; /* Ma
17468 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
17469 61 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 61 args passed to a
1746a 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 user function.
1746b 2a 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 */. resolveP2
1746c 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 Values(p, &nArg)
1746d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 56 ;. assert( nV
1746e 61 72 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 ar>=0 );. if(
1746f 20 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e 4d isExplain && nM
17470 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20 6e em<10 ){. n
17471 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d 0a Mem = 10;. }.
17472 20 20 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 73 71 p->aMem = sq
17473 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
17474 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 6e 4d o(db,. nM
17475 65 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 20 20 em*sizeof(Mem)
17476 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17477 61 4d 65 6d 20 2a 2f 0a 20 20 20 20 20 20 2b 20 aMem */. +
17478 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 nVar*sizeof(Mem)
17479 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1747a 2a 20 61 56 61 72 20 2a 2f 0a 20 20 20 20 20 20 * aVar */.
1747b 2b 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 + nArg*sizeof(Me
1747c 6d 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 m*)
1747d 20 2f 2a 20 61 70 41 72 67 20 2a 2f 0a 20 20 20 /* apArg */.
1747e 20 20 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f 66 + nVar*sizeof
1747f 28 63 68 61 72 2a 29 20 20 20 20 20 20 20 20 20 (char*)
17480 20 20 20 20 2f 2a 20 61 7a 56 61 72 20 2a 2f 0a /* azVar */.
17481 20 20 20 20 20 20 2b 20 6e 43 75 72 73 6f 72 2a + nCursor*
17482 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f sizeof(VdbeCurso
17483 72 2a 29 2b 31 20 20 2f 2a 20 61 70 43 73 72 20 r*)+1 /* apCsr
17484 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 */. );. if
17485 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 ( !db->mallocFai
17486 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e led ){. p->
17487 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20 20 aMem--;
17488 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f /* aMem[] go
17489 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 es from 1..nMem
1748a 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d */. p->nMem
1748b 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 = nMem;
1748c 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20 66 72 6f /* not fro
1748d 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a 2f 0a 20 m 0..nMem-1 */.
1748e 20 20 20 20 20 70 2d 3e 61 56 61 72 20 3d 20 26 p->aVar = &
1748f 70 2d 3e 61 4d 65 6d 5b 6e 4d 65 6d 2b 31 5d 3b p->aMem[nMem+1];
17490 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d . p->nVar =
17491 20 6e 56 61 72 3b 0a 20 20 20 20 20 20 70 2d 3e nVar;. p->
17492 6f 6b 56 61 72 20 3d 20 30 3b 0a 20 20 20 20 20 okVar = 0;.
17493 20 70 2d 3e 61 70 41 72 67 20 3d 20 28 4d 65 6d p->apArg = (Mem
17494 2a 2a 29 26 70 2d 3e 61 56 61 72 5b 6e 56 61 72 **)&p->aVar[nVar
17495 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 ];. p->azVa
17496 72 20 3d 20 28 63 68 61 72 2a 2a 29 26 70 2d 3e r = (char**)&p->
17497 61 70 41 72 67 5b 6e 41 72 67 5d 3b 0a 20 20 20 apArg[nArg];.
17498 20 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28 56 p->apCsr = (V
17499 64 62 65 43 75 72 73 6f 72 2a 2a 29 26 70 2d 3e dbeCursor**)&p->
1749a 61 7a 56 61 72 5b 6e 56 61 72 5d 3b 0a 20 20 20 azVar[nVar];.
1749b 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 p->nCursor =
1749c 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 66 nCursor;. f
1749d 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20 or(n=0; n<nVar;
1749e 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d n++){. p-
1749f 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d >aVar[n].flags =
174a0 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 MEM_Null;.
174a1 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 p->aVar[n].db
174a2 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 = db;. }.
174a3 20 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c for(n=1; n<
174a4 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 =nMem; n++){.
174a5 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e p->aMem[n].
174a6 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
174a7 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 ;. p->aMe
174a8 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 m[n].db = db;.
174a9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
174aa 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
174ab 42 55 47 0a 20 20 66 6f 72 28 6e 3d 31 3b 20 6e BUG. for(n=1; n
174ac 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a <p->nMem; n++){.
174ad 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 assert( p->a
174ae 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 20 29 3b Mem[n].db==db );
174af 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70 . }.#endif.. p
174b0 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d 3e ->pc = -1;. p->
174b1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
174b2 20 20 70 2d 3e 75 6e 69 71 75 65 43 6e 74 20 3d p->uniqueCnt =
174b3 20 30 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 0;. p->errorAc
174b4 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b tion = OE_Abort;
174b5 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d . p->explain |=
174b6 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d isExplain;. p-
174b7 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 >magic = VDBE_MA
174b8 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 GIC_RUN;. p->nC
174b9 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e hange = 0;. p->
174ba 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 cacheCtr = 1;.
174bb 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 p->minWriteFileF
174bc 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 ormat = 255;. p
174bd 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e ->openedStatemen
174be 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44 t = 0;.#ifdef VD
174bf 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 BE_PROFILE. {.
174c0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
174c1 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b r(i=0; i<p->nOp;
174c2 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e i++){. p->
174c3 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a aOp[i].cnt = 0;.
174c4 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e p->aOp[i].
174c5 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 cycles = 0;.
174c6 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a }. }.#endif.}..
174c7 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 /*.** Close a VD
174c8 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 BE cursor and re
174c9 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 lease all the re
174ca 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72 sources that cur
174cb 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 sor .** happens
174cc 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 to hold..*/.SQLI
174cd 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
174ce 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 sqlite3VdbeFreeC
174cf 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 ursor(Vdbe *p, V
174d0 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b dbeCursor *pCx){
174d1 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b . if( pCx==0 ){
174d2 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
174d3 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20 . if( pCx->pBt
174d4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
174d5 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 reeClose(pCx->pB
174d6 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 t);. /* The p
174d7 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c Cx->pCursor will
174d8 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 be close automa
174d9 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 tically, if it e
174da 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a xists, by. **
174db 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e the call above.
174dc 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 */. }else if(
174dd 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a pCx->pCursor ){.
174de 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
174df 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d CloseCursor(pCx-
174e0 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 >pCursor);. }.#
174e1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
174e2 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
174e3 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 if( pCx->pVtab
174e4 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 Cursor ){. sq
174e5 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
174e6 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d r *pVtabCursor =
174e7 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f pCx->pVtabCurso
174e8 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c r;. const sql
174e9 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
174ea 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 dule = pCx->pMod
174eb 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 ule;. p->inVt
174ec 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 abMethod = 1;.
174ed 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
174ee 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b afetyOff(p->db);
174ef 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 . pModule->xC
174f0 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 lose(pVtabCursor
174f1 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c );. (void)sql
174f2 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e ite3SafetyOn(p->
174f3 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 db);. p->inVt
174f4 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
174f5 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 }.#endif. if( !
174f6 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f pCx->ephemPseudo
174f7 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c Table ){. sql
174f8 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 ite3DbFree(p->db
174f9 2c 20 70 43 78 2d 3e 70 44 61 74 61 29 3b 0a 20 , pCx->pData);.
174fa 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 }.}../*.** Clos
174fb 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 e all cursors ex
174fc 63 65 70 74 20 66 6f 72 20 56 54 61 62 20 63 75 cept for VTab cu
174fd 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 63 rsors that are c
174fe 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 69 6e 20 75 urrently.** in u
174ff 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f se..*/.static vo
17500 69 64 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f id closeAllCurso
17501 72 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74 rsExceptActiveVt
17502 61 62 73 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 abs(Vdbe *p){.
17503 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 2d 3e int i;. if( p->
17504 61 70 43 73 72 3d 3d 30 20 29 20 72 65 74 75 72 apCsr==0 ) retur
17505 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c n;. for(i=0; i<
17506 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b 29 p->nCursor; i++)
17507 7b 0a 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 {. VdbeCursor
17508 20 2a 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b *pC = p->apCsr[
17509 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 20 26 i];. if( pC &
1750a 26 20 28 21 70 2d 3e 69 6e 56 74 61 62 4d 65 74 & (!p->inVtabMet
1750b 68 6f 64 20 7c 7c 20 21 70 43 2d 3e 70 56 74 61 hod || !pC->pVta
1750c 62 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 bCursor) ){.
1750d 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 sqlite3VdbeFre
1750e 65 43 75 72 73 6f 72 28 70 2c 20 70 43 29 3b 0a eCursor(p, pC);.
1750f 20 20 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 p->apCsr[i
17510 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d ] = 0;. }. }
17511 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 .}../*.** Clean
17512 75 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 20 up the VM after
17513 65 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a execution..**.**
17514 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
17515 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ll automatically
17516 20 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f close any curso
17517 72 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f rs, lists, and/o
17518 72 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61 r.** sorters tha
17519 74 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e t were left open
1751a 2e 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 . It also delet
1751b 65 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 es the values of
1751c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e .** variables in
1751d 20 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61 the aVar[] arra
1751e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 y..*/.static voi
1751f 64 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a d Cleanup(Vdbe *
17520 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 p){. int i;. s
17521 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e qlite3 *db = p->
17522 64 62 3b 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 db;. closeAllCu
17523 72 73 6f 72 73 45 78 63 65 70 74 41 63 74 69 76 rsorsExceptActiv
17524 65 56 74 61 62 73 28 70 29 3b 0a 20 20 66 6f 72 eVtabs(p);. for
17525 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d (i=1; i<=p->nMem
17526 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 53 ; i++){. MemS
17527 65 74 54 79 70 65 46 6c 61 67 28 26 70 2d 3e 61 etTypeFlag(&p->a
17528 4d 65 6d 5b 69 5d 2c 20 4d 45 4d 5f 4e 75 6c 6c Mem[i], MEM_Null
17529 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 );. }. release
1752a 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d 65 MemArray(&p->aMe
1752b 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a m[1], p->nMem);.
1752c 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 sqlite3VdbeFif
1752d 6f 43 6c 65 61 72 28 26 70 2d 3e 73 46 69 66 6f oClear(&p->sFifo
1752e 29 3b 0a 20 20 69 66 28 20 70 2d 3e 63 6f 6e 74 );. if( p->cont
1752f 65 78 74 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 extStack ){.
17530 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 63 6f for(i=0; i<p->co
17531 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 3b 20 69 ntextStackTop; i
17532 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
17533 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61 72 28 e3VdbeFifoClear(
17534 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b &p->contextStack
17535 5b 69 5d 2e 73 46 69 66 6f 29 3b 0a 20 20 20 20 [i].sFifo);.
17536 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 }. sqlite3DbF
17537 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f 6e 74 65 ree(db, p->conte
17538 78 74 53 74 61 63 6b 29 3b 0a 20 20 7d 0a 20 20 xtStack);. }.
17539 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 20 p->contextStack
1753a 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 = 0;. p->contex
1753b 74 53 74 61 63 6b 44 65 70 74 68 20 3d 20 30 3b tStackDepth = 0;
1753c 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 . p->contextSta
1753d 63 6b 54 6f 70 20 3d 20 30 3b 0a 20 20 73 71 6c ckTop = 0;. sql
1753e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1753f 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d ->zErrMsg);. p-
17540 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
17541 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 p->pResultSet =
17542 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 0;.}../*.** Set
17543 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 the number of re
17544 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 74 68 61 sult columns tha
17545 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e t will be return
17546 65 64 20 62 79 20 74 68 69 73 20 53 51 4c 0a 2a ed by this SQL.*
17547 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 * statement. Thi
17548 73 20 69 73 20 6e 6f 77 20 73 65 74 20 61 74 20 s is now set at
17549 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 72 61 compile time, ra
1754a 74 68 65 72 20 74 68 61 6e 20 64 75 72 69 6e 67 ther than during
1754b 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 .** execution of
1754c 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 the vdbe progra
1754d 6d 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 m so that sqlite
1754e 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 3_column_count()
1754f 20 63 61 6e 0a 2a 2a 20 62 65 20 63 61 6c 6c 65 can.** be calle
17550 64 20 6f 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 d on an SQL stat
17551 65 6d 65 6e 74 20 62 65 66 6f 72 65 20 73 71 6c ement before sql
17552 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a ite3_step()..*/.
17553 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
17554 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 oid sqlite3VdbeS
17555 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 20 2a etNumCols(Vdbe *
17556 70 2c 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75 6d p, int nResColum
17557 6e 29 7b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e n){. Mem *pColN
17558 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 ame;. int n;.
17559 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d sqlite3 *db = p-
1755a 3e 64 62 3b 0a 0a 20 20 72 65 6c 65 61 73 65 4d >db;.. releaseM
1755b 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e emArray(p->aColN
1755c 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 ame, p->nResColu
1755d 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 mn*COLNAME_N);.
1755e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1755f 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b b, p->aColName);
17560 0a 20 20 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d . n = nResColum
17561 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 n*COLNAME_N;. p
17562 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 6e ->nResColumn = n
17563 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e ResColumn;. p->
17564 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e aColName = pColN
17565 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 ame = (Mem*)sqli
17566 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
17567 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a db, sizeof(Mem)*
17568 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 43 n );. if( p->aC
17569 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 olName==0 ) retu
1756a 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d rn;. while( n--
1756b 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c > 0 ){. pCol
1756c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 Name->flags = ME
1756d 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c M_Null;. pCol
1756e 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 Name->db = p->db
1756f 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b ;. pColName++
17570 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ;. }.}../*.** S
17571 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 et the name of t
17572 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d 6e he idx'th column
17573 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 to be returned
17574 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 by the SQL state
17575 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d ment..** zName m
17576 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 ust be a pointer
17577 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e to a nul termin
17578 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a ated string..**.
17579 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 73 ** This call mus
1757a 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 20 t be made after
1757b 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
1757c 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 3VdbeSetNumCols(
1757d 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6e )..**.** The fin
1757e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78 44 al parameter, xD
1757f 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 el, must be one
17580 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 of SQLITE_DYNAMI
17581 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 C, SQLITE_STATIC
17582 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 52 .** or SQLITE_TR
17583 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20 69 ANSIENT. If it i
17584 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 s SQLITE_DYNAMIC
17585 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66 65 , then the buffe
17586 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 r pointed.** to
17587 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 65 by zName will be
17588 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 65 freed by sqlite
17589 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20 74 3DbFree() when t
1758a 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74 72 he vdbe is destr
1758b 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oyed..*/.SQLITE_
1758c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1758d 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d te3VdbeSetColNam
1758e 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 e(. Vdbe *p,
1758f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17590 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62 65 /* Vdbe be
17591 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20 2a ing configured *
17592 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20 20 /. int idx,
17593 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17594 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
17595 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 70 column zName ap
17596 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 6e plies to */. in
17597 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20 20 t var,
17598 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17599 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f 4c * One of the COL
1759a 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 NAME_* constants
1759b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1759c 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 *zName,
1759d 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1759e 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 r to buffer cont
1759f 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20 aining name */.
175a0 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
175a1 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 id*)
175a2 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e 61 /* Memory mana
175a3 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 gement strategy
175a4 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a for zName */.){.
175a5 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 20 int rc;. Mem
175a6 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 73 *pColName;. ass
175a7 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 73 ert( idx<p->nRes
175a8 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 65 Column );. asse
175a9 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f rt( var<COLNAME_
175aa 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 N );. if( p->db
175ab 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
175ac 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 7a {. assert( !z
175ad 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 51 Name || xDel!=SQ
175ae 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b 0a LITE_DYNAMIC );.
175af 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
175b0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 E_NOMEM;. }. a
175b1 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e 61 ssert( p->aColNa
175b2 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 4e me!=0 );. pColN
175b3 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e ame = &(p->aColN
175b4 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e 6e ame[idx+var*p->n
175b5 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72 ResColumn]);. r
175b6 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
175b7 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d emSetStr(pColNam
175b8 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 e, zName, -1, SQ
175b9 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 LITE_UTF8, xDel)
175ba 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d ;. assert( rc!=
175bb 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 28 0 || !zName || (
175bc 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 pColName->flags&
175bd 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b 0a MEM_Term)!=0 );.
175be 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
175bf 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 /*.** A read or
175c0 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
175c1 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 n may or may not
175c2 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64 61 be active on da
175c3 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a tabase handle.**
175c4 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73 61 db. If a transa
175c5 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c ction is active,
175c6 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 74 commit it. If t
175c7 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 69 here is a.** wri
175c8 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 te-transaction s
175c9 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 61 panning more tha
175ca 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 n one database f
175cb 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e ile, this routin
175cc 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 20 e.** takes care
175cd 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f of the master jo
175ce 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e 0a urnal trickery..
175cf 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 */.static int vd
175d0 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 beCommit(sqlite3
175d1 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 0a *db, Vdbe *p){.
175d2 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e int i;. int n
175d3 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 4e Trans = 0; /* N
175d4 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 umber of databas
175d5 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69 76 es with an activ
175d6 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 e write-transact
175d7 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ion */. int rc
175d8 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
175d9 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d nt needXcommit =
175da 20 30 3b 0a 0a 20 20 2f 2a 20 42 65 66 6f 72 65 0;.. /* Before
175db 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 doing anything
175dc 65 6c 73 65 2c 20 63 61 6c 6c 20 74 68 65 20 78 else, call the x
175dd 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 Sync() callback
175de 66 6f 72 20 61 6e 79 0a 20 20 2a 2a 20 76 69 72 for any. ** vir
175df 74 75 61 6c 20 6d 6f 64 75 6c 65 20 74 61 62 6c tual module tabl
175e0 65 73 20 77 72 69 74 74 65 6e 20 69 6e 20 74 68 es written in th
175e1 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 is transaction.
175e2 54 68 69 73 20 68 61 73 20 74 6f 0a 20 20 2a 2a This has to. **
175e3 20 62 65 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 be done before
175e4 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 74 determining whet
175e5 68 65 72 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 her a master jou
175e6 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 20 20 rnal file is .
175e7 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 61 73 20 ** required, as
175e8 61 6e 20 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 an xSync() callb
175e9 61 63 6b 20 6d 61 79 20 61 64 64 20 61 6e 20 61 ack may add an a
175ea 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
175eb 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 72 61 . ** to the tra
175ec 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 nsaction.. */.
175ed 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 rc = sqlite3Vta
175ee 62 53 79 6e 63 28 64 62 2c 20 26 70 2d 3e 7a 45 bSync(db, &p->zE
175ef 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72 63 rrMsg);. if( rc
175f0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
175f1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
175f2 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f }.. /* This loo
175f3 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28 61 29 p determines (a)
175f4 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 if the commit h
175f5 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20 69 6e ook should be in
175f6 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a 20 28 voked and. ** (
175f7 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61 74 61 b) how many data
175f8 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65 20 base files have
175f9 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 open write trans
175fa 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a 20 20 actions, not .
175fb 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 ** including the
175fc 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 20 temp database.
175fd 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 (b) is important
175fe 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f 72 65 because if more
175ff 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e 65 20 than . ** one
17600 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
17601 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 s an open write
17602 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 6d transaction, a m
17603 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 20 20 aster journal.
17604 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71 75 69 ** file is requi
17605 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d 69 red for an atomi
17606 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f 20 0a c commit.. */ .
17607 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
17608 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 >nDb; i++){ .
17609 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 Btree *pBt = db
1760a 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
1760b 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 if( sqlite3Btr
1760c 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 eeIsInTrans(pBt)
1760d 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64 58 63 ){. needXc
1760e 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 ommit = 1;.
1760f 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54 72 61 if( i!=1 ) nTra
17610 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ns++;. }. }.
17611 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 . /* If there a
17612 72 65 20 61 6e 79 20 77 72 69 74 65 2d 74 72 61 re any write-tra
17613 6e 73 61 63 74 69 6f 6e 73 20 61 74 20 61 6c 6c nsactions at all
17614 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 6d , invoke the com
17615 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69 66 mit hook */. if
17616 28 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 26 26 ( needXcommit &&
17617 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c db->xCommitCall
17618 62 61 63 6b 20 29 7b 0a 20 20 20 20 28 76 6f 69 back ){. (voi
17619 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f d)sqlite3SafetyO
1761a 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d ff(db);. rc =
1761b 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c db->xCommitCall
1761c 62 61 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69 74 back(db->pCommit
1761d 41 72 67 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 Arg);. (void)
1761e 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1761f 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 db);. if( rc
17620 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
17621 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
17622 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 T;. }. }..
17623 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 63 61 /* The simple ca
17624 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 68 61 se - no more tha
17625 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 n one database f
17626 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74 69 6e ile (not countin
17627 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d 50 20 g the. ** TEMP
17628 64 61 74 61 62 61 73 65 29 20 68 61 73 20 61 20 database) has a
17629 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 transaction acti
1762a 76 65 2e 20 20 20 54 68 65 72 65 20 69 73 20 6e ve. There is n
1762b 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65 0a 20 o need for the.
1762c 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e ** master-journ
1762d 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 al.. **. ** If
1762e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
1762f 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72 65 e of sqlite3Btre
17630 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29 20 69 eGetFilename() i
17631 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74 68 0a s a zero length.
17632 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69 74 20 ** string, it
17633 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 64 means the main d
17634 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65 6d 6f atabase is :memo
17635 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20 66 69 ry: or a temp fi
17636 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20 74 68 le. In . ** th
17637 61 74 20 63 61 73 65 20 77 65 20 64 6f 20 6e 6f at case we do no
17638 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d 69 63 t support atomic
17639 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f 6d 6d multi-file comm
1763a 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68 65 20 its, so use the
1763b 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63 61 73 . ** simple cas
1763c 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20 2a 2f e then too.. */
1763d 0a 20 20 69 66 28 20 30 3d 3d 73 74 72 6c 65 6e . if( 0==strlen
1763e 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 (sqlite3BtreeGet
1763f 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 Filename(db->aDb
17640 5b 30 5d 2e 70 42 74 29 29 20 7c 7c 20 6e 54 72 [0].pBt)) || nTr
17641 61 6e 73 3c 3d 31 20 29 7b 0a 20 20 20 20 66 6f ans<=1 ){. fo
17642 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 r(i=0; rc==SQLIT
17643 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 E_OK && i<db->nD
17644 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 b; i++){ .
17645 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d Btree *pBt = db-
17646 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
17647 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 if( pBt ){.
17648 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
17649 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
1764a 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20 seOne(pBt, 0);.
1764b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
1764c 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d /* Do the comm
1764d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64 it only if all d
1764e 61 74 61 62 61 73 65 73 20 73 75 63 63 65 73 73 atabases success
1764f 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70 fully complete p
17650 68 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20 hase 1. . **
17651 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74 If one of the Bt
17652 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e reeCommitPhaseOn
17653 65 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c e() calls fails,
17654 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 this indicates
17655 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72 an. ** IO err
17656 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e or while deletin
17657 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 g or truncating
17658 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 a journal file.
17659 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a It is unlikely,.
1765a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64 ** but could
1765b 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73 happen. In this
1765c 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72 case abandon pr
1765d 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 ocessing and ret
1765e 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 urn the error..
1765f 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d */. for(i=
17660 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0; rc==SQLITE_OK
17661 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 && i<db->nDb; i
17662 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 ++){. Btree
17663 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b *pBt = db->aDb[
17664 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 i].pBt;. if
17665 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 ( pBt ){.
17666 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
17667 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
17668 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 (pBt);. }.
17669 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
1766a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1766b 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 sqlite3VtabC
1766c 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d ommit(db);. }
1766d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63 . }.. /* The c
1766e 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68 omplex case - Th
1766f 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66 ere is a multi-f
17670 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 ile write-transa
17671 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20 ction active..
17672 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72 65 73 ** This requires
17673 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
17674 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65 l file to ensure
17675 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
17676 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 is. ** committ
17677 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a ed atomicly.. *
17678 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
17679 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65 _OMIT_DISKIO. e
1767a 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
1767b 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d _vfs *pVfs = db-
1767c 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e >pVfs;. int n
1767d 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 eedSync = 0;.
1767e 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d char *zMaster =
1767f 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61 0; /* File-na
17680 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65 me for the maste
17681 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 r journal */.
17682 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 char const *zMa
17683 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 inFile = sqlite3
17684 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 BtreeGetFilename
17685 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 (db->aDb[0].pBt)
17686 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 ;. sqlite3_fi
17687 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b le *pMaster = 0;
17688 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 . i64 offset
17689 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 = 0;. int res
1768a 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 ;.. /* Select
1768b 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
1768c 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 l file name */.
1768d 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33 do {. u3
1768e 32 20 72 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 20 2 random;.
1768f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
17690 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 , zMaster);.
17691 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d sqlite3_random
17692 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 61 6e 64 ness(sizeof(rand
17693 6f 6d 29 2c 20 26 72 61 6e 64 6f 6d 29 3b 0a 20 om), &random);.
17694 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 73 zMaster = s
17695 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
17696 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20 7a , "%s-mj%08X", z
17697 4d 61 69 6e 46 69 6c 65 2c 20 72 61 6e 64 6f 6d MainFile, random
17698 26 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 &0x7fffffff);.
17699 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 if( !zMaster
1769a 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
1769b 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1769c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
1769d 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 c = sqlite3OsAcc
1769e 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 ess(pVfs, zMaste
1769f 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 r, SQLITE_ACCESS
176a0 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a _EXISTS, &res);.
176a1 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d }while( rc==
176a2 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 SQLITE_OK && res
176a3 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d );. if( rc==
176a4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
176a5 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6d /* Open the m
176a6 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 2a aster journal. *
176a7 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c /. rc = sql
176a8 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 ite3OsOpenMalloc
176a9 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 (pVfs, zMaster,
176aa 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20 20 20 &pMaster, .
176ab 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
176ac 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 _READWRITE|SQLIT
176ad 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a 20 E_OPEN_CREATE|.
176ae 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
176af 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 7c 53 OPEN_EXCLUSIVE|S
176b0 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 QLITE_OPEN_MASTE
176b1 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20 20 20 R_JOURNAL, 0.
176b2 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 );. }.
176b3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
176b4 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
176b5 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 e3DbFree(db, zMa
176b6 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 ster);. ret
176b7 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 0a urn rc;. }. .
176b8 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
176b9 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20 64 61 name of each da
176ba 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 74 tabase file in t
176bb 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
176bc 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 20 20 nto the new.
176bd 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 ** master journa
176be 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20 65 72 l file. If an er
176bf 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20 74 68 ror occurs at th
176c0 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65 0a 20 is point close.
176c1 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65 74 65 ** and delete
176c2 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
176c3 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20 74 68 nal file. All th
176c4 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 e individual jou
176c5 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20 20 2a rnal files. *
176c6 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27 6e 75 * still have 'nu
176c7 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73 74 65 ll' as the maste
176c8 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 r journal pointe
176c9 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c 6c 20 r, so they will
176ca 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61 63 6b roll. ** back
176cb 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 69 independently i
176cc 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 f a failure occu
176cd 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 rs.. */. f
176ce 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
176cf 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 b; i++){. B
176d0 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e tree *pBt = db->
176d1 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
176d2 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e if( i==1 ) con
176d3 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49 67 6e 6f tinue; /* Igno
176d4 72 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 re the TEMP data
176d5 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 base */. if
176d6 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 ( sqlite3BtreeIs
176d7 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29 7b 0a InTrans(pBt) ){.
176d8 20 20 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e char con
176d9 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73 71 6c 69 st *zFile = sqli
176da 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e te3BtreeGetJourn
176db 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a 20 20 20 alname(pBt);.
176dc 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 5b 30 if( zFile[0
176dd 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b ]==0 ) continue;
176de 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a 6d 65 6d /* Ignore :mem
176df 6f 72 79 3a 20 64 61 74 61 62 61 73 65 73 20 2a ory: databases *
176e0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6e /. if( !n
176e1 65 65 64 53 79 6e 63 20 26 26 20 21 73 71 6c 69 eedSync && !sqli
176e2 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61 te3BtreeSyncDisa
176e3 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20 20 20 bled(pBt) ){.
176e4 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 needSync
176e5 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 1;. }.
176e6 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
176e7 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61 73 74 te3OsWrite(pMast
176e8 65 72 2c 20 7a 46 69 6c 65 2c 20 73 74 72 6c 65 er, zFile, strle
176e9 6e 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66 73 n(zFile)+1, offs
176ea 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 et);. off
176eb 73 65 74 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 46 set += strlen(zF
176ec 69 6c 65 29 2b 31 3b 0a 20 20 20 20 20 20 20 20 ile)+1;.
176ed 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
176ee 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 K ){. s
176ef 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 qlite3OsCloseFre
176f0 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 e(pMaster);.
176f1 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 sqlite3OsD
176f2 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 elete(pVfs, zMas
176f3 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ter, 0);.
176f4 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
176f5 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 (db, zMaster);.
176f6 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
176f7 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
176f8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
176f9 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6d 61 73 /* Sync the mas
176fa 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
176fb 2e 20 49 66 20 74 68 65 20 49 4f 43 41 50 5f 53 . If the IOCAP_S
176fc 45 51 55 45 4e 54 49 41 4c 20 64 65 76 69 63 65 EQUENTIAL device
176fd 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 . ** flag is
176fe 73 65 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20 set this is not
176ff 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f required.. */
17700 0a 20 20 20 20 7a 4d 61 69 6e 46 69 6c 65 20 3d . zMainFile =
17701 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
17702 44 69 72 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b Dirname(db->aDb[
17703 30 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 0].pBt);. if(
17704 20 28 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 20 (needSync .
17705 20 26 26 20 28 30 3d 3d 28 73 71 6c 69 74 65 33 && (0==(sqlite3
17706 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 OsDeviceCharacte
17707 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 72 29 ristics(pMaster)
17708 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 &SQLITE_IOCAP_SE
17709 51 55 45 4e 54 49 41 4c 29 29 0a 20 20 20 20 20 QUENTIAL)).
1770a 26 26 20 28 72 63 3d 73 71 6c 69 74 65 33 4f 73 && (rc=sqlite3Os
1770b 53 79 6e 63 28 70 4d 61 73 74 65 72 2c 20 53 51 Sync(pMaster, SQ
1770c 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
1770d 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 ))!=SQLITE_OK) )
1770e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f {. sqlite3O
1770f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 sCloseFree(pMast
17710 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 er);. sqlit
17711 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c e3OsDelete(pVfs,
17712 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 zMaster, 0);.
17713 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
17714 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a e(db, zMaster);.
17715 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
17716 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 . }.. /* S
17717 79 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66 ync all the db f
17718 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e iles involved in
17719 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1771a 2e 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a . The same call.
1771b 20 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20 ** sets the
1771c 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 master journal p
1771d 6f 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69 ointer in each i
1771e 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 ndividual journa
1771f 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20 l. If. ** an
17720 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72 error occurs her
17721 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 e, do not delete
17722 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
17723 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a nal file.. **
17724 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65 . ** If the e
17725 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
17726 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61 6c ng the first cal
17727 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 l to. ** sqli
17728 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
17729 61 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74 aseOne(), then t
1772a 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 here is a chance
1772b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a that the. **
1772c 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1772d 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70 file will be orp
1772e 68 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61 haned. But we ca
1772f 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a nnot delete it,.
17730 20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 ** in case t
17731 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
17732 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20 l file name was
17733 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
17734 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 journal. **
17735 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20 file before the
17736 66 61 69 6c 75 72 65 20 6f 63 63 75 72 65 64 2e failure occured.
17737 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 . */. for(
17738 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f i=0; rc==SQLITE_
17739 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b OK && i<db->nDb;
1773a 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 i++){ . Bt
1773b 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 ree *pBt = db->a
1773c 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 Db[i].pBt;.
1773d 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 if( pBt ){.
1773e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1773f 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
17740 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 72 One(pBt, zMaster
17741 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
17742 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
17743 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 oseFree(pMaster)
17744 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
17745 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17746 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
17747 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 b, zMaster);.
17748 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
17749 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 }.. /* Dele
1774a 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
1774b 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 urnal file. This
1774c 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61 commits the tra
1774d 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a nsaction. After.
1774e 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69 ** doing thi
1774f 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 s the directory
17750 69 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20 is synced again
17751 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76 before any indiv
17752 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61 idual. ** tra
17753 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61 nsaction files a
17754 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 re deleted..
17755 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
17756 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 te3OsDelete(pVfs
17757 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a 20 , zMaster, 1);.
17758 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
17759 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 (db, zMaster);.
1775a 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a zMaster = 0;.
1775b 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
1775c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1775d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c }.. /* All
1775e 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 63 files and direc
1775f 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 65 tories have alre
17760 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2c ady been synced,
17761 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e so the followin
17762 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 g. ** calls t
17763 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f o sqlite3BtreeCo
17764 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 mmitPhaseTwo() a
17765 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 20 re only closing
17766 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a 2a files and. **
17767 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 deleting or tru
17768 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 73 ncating journals
17769 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 . If something g
1776a 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 0a oes wrong while.
1776b 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 ** this is h
1776c 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e 27 appening we don'
1776d 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 54 t really care. T
1776e 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 he integrity of
1776f 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 the. ** trans
17770 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 action is alread
17771 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 75 y guaranteed, bu
17772 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 6f t some stray 'co
17773 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 ld' journals.
17774 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e 67 ** may be lying
17775 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e 69 around. Returni
17776 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 ng an error code
17777 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 74 won't help matt
17778 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ers.. */.
17779 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 disable_simulate
1777a 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 d_io_errors();.
1777b 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 sqlite3BeginB
1777c 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
1777d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 for(i=0; i<db
1777e 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 ->nDb; i++){ .
1777f 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d Btree *pBt =
17780 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
17781 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 . if( pBt )
17782 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
17783 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
17784 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 eTwo(pBt);.
17785 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c }. }. sql
17786 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
17787 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c loc();. enabl
17788 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
17789 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71 rrors();.. sq
1778a 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 lite3VtabCommit(
1778b 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a db);. }.#endif.
1778c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1778d 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ./* .** This rou
1778e 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74 tine checks that
1778f 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74 the sqlite3.act
17790 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74 iveVdbeCnt count
17791 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74 variable.** mat
17792 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 ches the number
17793 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65 of vdbe's in the
17794 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56 list sqlite3.pV
17795 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 dbe that are.**
17796 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 currently active
17797 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 . An assertion f
17798 61 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20 ails if the two
17799 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 counts do not ma
1779a 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 tch..** This is
1779b 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66 an internal self
1779c 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74 -check only - it
1779d 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e is not an essen
1779e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a tial processing.
1779f 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 ** step..**.** T
177a0 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 his is a no-op i
177a1 66 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 f NDEBUG is defi
177a2 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ned..*/.#ifndef
177a3 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f NDEBUG.static vo
177a4 69 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 id checkActiveVd
177a5 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 beCnt(sqlite3 *d
177a6 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 b){. Vdbe *p;.
177a7 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 int cnt = 0;.
177a8 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a int nWrite = 0;.
177a9 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b p = db->pVdbe;
177aa 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 . while( p ){.
177ab 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d if( p->magic=
177ac 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 =VDBE_MAGIC_RUN
177ad 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 && p->pc>=0 ){.
177ae 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 cnt++;.
177af 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c if( p->readOnl
177b0 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b y==0 ) nWrite++;
177b1 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 . }. p = p
177b2 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61 ->pNext;. }. a
177b3 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e ssert( cnt==db->
177b4 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b activeVdbeCnt );
177b5 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74 . assert( nWrit
177b6 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 e==db->writeVdbe
177b7 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 Cnt );.}.#else.#
177b8 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69 define checkActi
177b9 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e veVdbeCnt(x).#en
177ba 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 dif../*.** For e
177bb 76 65 72 79 20 42 74 72 65 65 20 74 68 61 74 20 very Btree that
177bc 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e in database conn
177bd 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 20 ection db which
177be 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 .** has been mod
177bf 69 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f 72 ified, "trip" or
177c0 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 68 invalidate each
177c1 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 68 cursor in.** th
177c2 61 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 68 at Btree might h
177c3 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 ave been modifie
177c4 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 75 d so that the cu
177c5 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 65 rsor.** can neve
177c6 72 20 62 65 20 75 73 65 64 20 61 67 61 69 6e 2e r be used again.
177c7 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 This happens w
177c8 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a hen a rollback.*
177c9 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 68 ** occurs. We h
177ca 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c 20 ave to trip all
177cb 74 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 the other cursor
177cc 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 6f s, even.** curso
177cd 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d 73 r from other VMs
177ce 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 61 in different da
177cf 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
177d0 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6e ns,.** so that n
177d1 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 20 one of them try
177d2 74 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 20 to use the data
177d3 61 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a at which they.**
177d4 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 were pointing a
177d5 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 79 nd which now may
177d6 20 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e 67 have been chang
177d7 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 65 ed due.** to the
177d8 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
177d9 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 61 Remember that a
177da 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 65 rollback can de
177db 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d 70 lete tables comp
177dc 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f 72 lete and.** reor
177dd 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 20 der rootpages.
177de 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 So it is not suf
177df 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f 20 ficient just to
177e0 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 save.** the stat
177e1 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e e of the cursor.
177e2 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76 We have to inv
177e3 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73 alidate the curs
177e4 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 or.** so that it
177e5 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 is never used a
177e6 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 gain..*/.static
177e7 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 43 void invalidateC
177e8 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 ursorsOnModified
177e9 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 2a Btrees(sqlite3 *
177ea 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 db){. int i;.
177eb 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e for(i=0; i<db->n
177ec 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 Db; i++){. Bt
177ed 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 ree *p = db->aDb
177ee 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 [i].pBt;. if(
177ef 20 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 p && sqlite3Btr
177f0 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 29 eeIsInTrans(p) )
177f1 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 {. sqlite3B
177f2 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f treeTripAllCurso
177f3 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 4f rs(p, SQLITE_ABO
177f4 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d RT);. }. }.}
177f5 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
177f6 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 tine is called t
177f7 68 65 20 77 68 65 6e 20 61 20 56 44 42 45 20 74 he when a VDBE t
177f8 72 69 65 73 20 74 6f 20 68 61 6c 74 2e 20 20 49 ries to halt. I
177f9 66 20 74 68 65 20 56 44 42 45 0a 2a 2a 20 68 61 f the VDBE.** ha
177fa 73 20 6d 61 64 65 20 63 68 61 6e 67 65 73 20 61 s made changes a
177fb 6e 64 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d nd is in autocom
177fc 6d 69 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20 63 mit mode, then c
177fd 6f 6d 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20 63 ommit those.** c
177fe 68 61 6e 67 65 73 2e 20 20 49 66 20 61 20 72 6f hanges. If a ro
177ff 6c 6c 62 61 63 6b 20 69 73 20 6e 65 65 64 65 64 llback is needed
17800 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 20 72 6f , then do the ro
17801 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 llback..**.** Th
17802 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
17803 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f e only way to mo
17804 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 ve the state of
17805 61 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51 4c a VM from.** SQL
17806 49 54 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74 6f ITE_MAGIC_RUN to
17807 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 SQLITE_MAGIC_HA
17808 4c 54 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c LT. It is harml
17809 65 73 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20 74 ess to.** call t
1780a 68 69 73 20 6f 6e 20 61 20 56 4d 20 74 68 61 74 his on a VM that
1780b 20 69 73 20 69 6e 20 74 68 65 20 53 51 4c 49 54 is in the SQLIT
1780c 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74 61 E_MAGIC_HALT sta
1780d 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e te..**.** Return
1780e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 an error code.
1780f 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 If the commit c
17810 6f 75 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 ould not complet
17811 65 20 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20 e because of.**
17812 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2c lock contention,
17813 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
17814 55 53 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f USY. If SQLITE_
17815 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 BUSY is returned
17816 2c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 , it.** means th
17817 65 20 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 20 e close did not
17818 68 61 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 73 happen and needs
17819 20 74 6f 20 62 65 20 72 65 70 65 61 74 65 64 2e to be repeated.
1781a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1781b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1781c 62 65 48 61 6c 74 28 56 64 62 65 20 2a 70 29 7b beHalt(Vdbe *p){
1781d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1781e 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b p->db;. int i;
1781f 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 . int (*xFunc)(
17820 42 74 72 65 65 20 2a 70 42 74 29 20 3d 20 30 3b Btree *pBt) = 0;
17821 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f /* Function to
17822 20 63 61 6c 6c 20 6f 6e 20 65 61 63 68 20 62 74 call on each bt
17823 72 65 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 ree backend */.
17824 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c 45 72 int isSpecialEr
17825 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 ror;
17826 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 /* Set to true i
17827 66 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f f SQLITE_NOMEM o
17828 72 20 49 4f 45 52 52 20 2a 2f 0a 0a 20 20 2f 2a r IOERR */.. /*
17829 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 This function c
1782a 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 ontains the logi
1782b 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 c that determine
1782c 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 s if a statement
1782d 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 or. ** transac
1782e 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d tion will be com
1782f 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 mitted or rolled
17830 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c back as a resul
17831 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 t of the. ** ex
17832 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 ecution of this
17833 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e virtual machine.
17834 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 . **. ** If a
17835 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ny of the follow
17836 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72 ing errors occur
17837 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 :. **. **
17838 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a SQLITE_NOMEM. *
17839 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 * SQLITE_IOE
1783a 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 RR. ** SQLI
1783b 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 TE_FULL. **
1783c 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 SQLITE_INTERRUP
1783d 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e T. **. ** Then
1783e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 the internal ca
1783f 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 che might have b
17840 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 een left in an i
17841 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a nconsistent. **
17842 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64 state. We need
17843 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 to rollback the
17844 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
17845 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 action, if there
17846 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 is. ** one, or
17847 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 the complete tr
17848 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 ansaction if the
17849 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 re is no stateme
1784a 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a nt transaction..
1784b 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 */.. if( p->d
1784c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1784d 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 ){. p->rc = S
1784e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
1784f 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f . closeAllCurso
17850 72 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74 rsExceptActiveVt
17851 61 62 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d abs(p);. if( p-
17852 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 >magic!=VDBE_MAG
17853 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 IC_RUN ){. re
17854 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
17855 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76 }. checkActiv
17856 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20 eVdbeCnt(db);..
17857 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 /* No commit or
17858 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64 rollback needed
17859 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 if the program
1785a 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f never started */
1785b 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 . if( p->pc>=0
1785c 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20 ){. int mrc;
1785d 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72 /* Primary err
1785e 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e or code from p->
1785f 72 63 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f rc */.. /* Lo
17860 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 20 75 73 ck all btrees us
17861 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d ed by the statem
17862 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 ent */. sqlit
17863 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
17864 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 yEnter(&p->aMute
17865 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 x);.. /* Chec
17866 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 k for one of the
17867 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72 73 20 special errors
17868 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20 70 2d 3e */. mrc = p->
17869 72 63 20 26 20 30 78 66 66 3b 0a 20 20 20 20 69 rc & 0xff;. i
1786a 73 53 70 65 63 69 61 6c 45 72 72 6f 72 20 3d 20 sSpecialError =
1786b 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 mrc==SQLITE_NOME
1786c 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 M || mrc==SQLITE
1786d 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 20 20 20 _IOERR.
1786e 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6d || m
1786f 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 rc==SQLITE_INTER
17870 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c RUPT || mrc==SQL
17871 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 69 66 ITE_FULL;. if
17872 28 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 ( isSpecialError
17873 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
17874 74 68 65 20 71 75 65 72 79 20 77 61 73 20 72 65 the query was re
17875 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e 65 65 64 ad-only, we need
17876 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 do no rollback
17877 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 77 69 73 at all. Otherwis
17878 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 e,. ** proc
17879 65 65 64 20 77 69 74 68 20 74 68 65 20 73 70 65 eed with the spe
1787a 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a 20 cial handling..
1787b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
1787c 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 7c ( !p->readOnly |
1787d 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e | mrc!=SQLITE_IN
1787e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 20 20 20 TERRUPT ){.
1787f 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 if( p->rc==SQ
17880 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b LITE_IOERR_BLOCK
17881 45 44 20 26 26 20 70 2d 3e 75 73 65 73 53 74 6d ED && p->usesStm
17882 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 tJournal ){.
17883 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 71 xFunc = sq
17884 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 lite3BtreeRollba
17885 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 20 20 ckStmt;.
17886 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 p->rc = SQLITE
17887 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d _BUSY;. }
17888 65 6c 73 65 20 69 66 28 20 28 6d 72 63 3d 3d 53 else if( (mrc==S
17889 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d QLITE_NOMEM || m
1788a 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29 rc==SQLITE_FULL)
1788b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1788c 20 20 20 20 26 26 20 70 2d 3e 75 73 65 73 53 74 && p->usesSt
1788d 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 mtJournal ){.
1788e 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 xFunc = s
1788f 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
17890 61 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 20 ackStmt;.
17891 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
17892 20 20 2f 2a 20 57 65 20 61 72 65 20 66 6f 72 63 /* We are forc
17893 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 ed to roll back
17894 74 68 65 20 61 63 74 69 76 65 20 74 72 61 6e 73 the active trans
17895 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72 65 20 64 action. Before d
17896 6f 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a oing. *
17897 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e 79 20 * so, abort any
17898 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 other statements
17899 20 74 68 69 73 20 68 61 6e 64 6c 65 20 63 75 72 this handle cur
1789a 72 65 6e 74 6c 79 20 68 61 73 20 61 63 74 69 76 rently has activ
1789b 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a e.. */.
1789c 20 20 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69 invali
1789d 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 dateCursorsOnMod
1789e 69 66 69 65 64 42 74 72 65 65 73 28 64 62 29 3b ifiedBtrees(db);
1789f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
178a0 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 e3RollbackAll(db
178a1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d );. db-
178a2 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b >autoCommit = 1;
178a3 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
178a4 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 }. }. .
178a5 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d 63 /* If the auto-c
178a6 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 ommit flag is se
178a7 74 20 61 6e 64 20 74 68 69 73 20 69 73 20 74 68 t and this is th
178a8 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 76 64 e only active vd
178a9 62 65 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 be, then. **
178aa 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 63 we do either a c
178ab 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63 ommit or rollbac
178ac 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 k of the current
178ad 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 20 transaction. .
178ae 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 **. ** Not
178af 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 6c e: This block al
178b0 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 6f so runs if one o
178b1 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 f the special er
178b2 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 20 rors handled .
178b3 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 6f ** above has o
178b4 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a 2f ccurred. . */
178b5 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 . if( !sqlite
178b6 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 3VtabInSync(db)
178b7 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 74 . && db->aut
178b8 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 26 oCommit . &&
178b9 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e db->writeVdbeCn
178ba 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d t==(p->readOnly=
178bb 3d 30 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 =0) . ){.
178bc 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c if( p->rc==SQL
178bd 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 ITE_OK || (p->er
178be 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 rorAction==OE_Fa
178bf 69 6c 20 26 26 20 21 69 73 53 70 65 63 69 61 6c il && !isSpecial
178c0 45 72 72 6f 72 29 20 29 7b 0a 20 20 20 20 20 20 Error) ){.
178c1 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f /* The auto-co
178c2 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 74 72 75 mmit flag is tru
178c3 65 2c 20 61 6e 64 20 74 68 65 20 76 64 62 65 20 e, and the vdbe
178c4 70 72 6f 67 72 61 6d 20 77 61 73 20 0a 20 20 20 program was .
178c5 20 20 20 20 20 2a 2a 20 73 75 63 63 65 73 73 66 ** successf
178c6 75 6c 20 6f 72 20 68 69 74 20 61 6e 20 27 4f 52 ul or hit an 'OR
178c7 20 46 41 49 4c 27 20 63 6f 6e 73 74 72 61 69 6e FAIL' constrain
178c8 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61 20 t. This means a
178c9 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20 20 commit .
178ca 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a ** is required..
178cb 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
178cc 20 20 20 69 6e 74 20 72 63 20 3d 20 76 64 62 65 int rc = vdbe
178cd 43 6f 6d 6d 69 74 28 64 62 2c 20 70 29 3b 0a 20 Commit(db, p);.
178ce 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
178cf 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
178d0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
178d1 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 treeMutexArrayLe
178d2 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b ave(&p->aMutex);
178d3 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
178d4 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 n SQLITE_BUSY;.
178d5 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
178d6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
178d7 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 {. p->r
178d8 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 20 20 c = rc;.
178d9 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 sqlite3Rollbac
178da 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 kAll(db);.
178db 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
178dc 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 sqlite3Commit
178dd 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 InternalChanges(
178de 64 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 db);. }.
178df 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
178e0 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 sqlite3Rollb
178e1 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 ackAll(db);.
178e2 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 }. }else if
178e3 28 20 21 78 46 75 6e 63 20 29 7b 0a 20 20 20 20 ( !xFunc ){.
178e4 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c if( p->rc==SQL
178e5 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 65 72 72 ITE_OK || p->err
178e6 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 orAction==OE_Fai
178e7 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 l ){. if(
178e8 20 70 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d p->openedStatem
178e9 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ent ){.
178ea 20 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 xFunc = sqlite3
178eb 42 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 3b BtreeCommitStmt;
178ec 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 . } .
178ed 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 }else if( p->e
178ee 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 rrorAction==OE_A
178ef 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 bort ){.
178f0 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 42 xFunc = sqlite3B
178f1 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 treeRollbackStmt
178f2 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
178f3 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 invalidat
178f4 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 eCursorsOnModifi
178f5 65 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 edBtrees(db);.
178f6 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c sqlite3Rol
178f7 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 lbackAll(db);.
178f8 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f db->autoCo
178f9 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 mmit = 1;.
178fa 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
178fb 2a 20 49 66 20 78 46 75 6e 63 20 69 73 20 6e 6f * If xFunc is no
178fc 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 t NULL, then it
178fd 69 73 20 6f 6e 65 20 6f 66 20 73 71 6c 69 74 65 is one of sqlite
178fe 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 3BtreeRollbackSt
178ff 6d 74 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c mt or. ** sql
17900 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 ite3BtreeCommitS
17901 74 6d 74 2e 20 43 61 6c 6c 20 69 74 20 6f 6e 63 tmt. Call it onc
17902 65 20 6f 6e 20 65 61 63 68 20 62 61 63 6b 65 6e e on each backen
17903 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f d. If an error o
17904 63 63 75 72 73 0a 20 20 20 20 2a 2a 20 61 6e 64 ccurs. ** and
17905 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 the return code
17906 20 69 73 20 73 74 69 6c 6c 20 53 51 4c 49 54 45 is still SQLITE
17907 5f 4f 4b 2c 20 73 65 74 20 74 68 65 20 72 65 74 _OK, set the ret
17908 75 72 6e 20 63 6f 64 65 20 74 6f 20 74 68 65 20 urn code to the
17909 6e 65 77 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 new. ** error
1790a 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 value.. */.
1790b 20 20 20 61 73 73 65 72 74 28 21 78 46 75 6e 63 assert(!xFunc
1790c 20 7c 7c 0a 20 20 20 20 20 20 78 46 75 6e 63 3d ||. xFunc=
1790d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d =sqlite3BtreeCom
1790e 6d 69 74 53 74 6d 74 20 7c 7c 0a 20 20 20 20 20 mitStmt ||.
1790f 20 78 46 75 6e 63 3d 3d 73 71 6c 69 74 65 33 42 xFunc==sqlite3B
17910 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 treeRollbackStmt
17911 0a 20 20 20 20 29 3b 0a 20 20 20 20 66 6f 72 28 . );. for(
17912 69 3d 30 3b 20 78 46 75 6e 63 20 26 26 20 69 3c i=0; xFunc && i<
17913 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a db->nDb; i++){ .
17914 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 int rc;.
17915 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d Btree *pBt =
17916 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
17917 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 . if( pBt )
17918 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 78 {. rc = x
17919 46 75 6e 63 28 70 42 74 29 3b 0a 20 20 20 20 20 Func(pBt);.
1791a 20 20 20 69 66 28 20 72 63 20 26 26 20 28 70 2d if( rc && (p-
1791b 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c >rc==SQLITE_OK |
1791c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f | p->rc==SQLITE_
1791d 43 4f 4e 53 54 52 41 49 4e 54 29 20 29 7b 0a 20 CONSTRAINT) ){.
1791e 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d p->rc =
1791f 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 rc;. s
17920 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
17921 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
17922 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d p->zErrM
17923 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 sg = 0;.
17924 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
17925 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 . /* If thi
17926 73 20 77 61 73 20 61 6e 20 49 4e 53 45 52 54 2c s was an INSERT,
17927 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 UPDATE or DELET
17928 45 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d E and the statem
17929 65 6e 74 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 ent was committe
1792a 64 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 d, . ** set t
1792b 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 he change counte
1792c 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 r. . */. i
1792d 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f f( p->changeCntO
1792e 6e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b n && p->pc>=0 ){
1792f 0a 20 20 20 20 20 20 69 66 28 20 21 78 46 75 6e . if( !xFun
17930 63 20 7c 7c 20 78 46 75 6e 63 3d 3d 73 71 6c 69 c || xFunc==sqli
17931 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 74 te3BtreeCommitSt
17932 6d 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 mt ){. sq
17933 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
17934 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e ges(db, p->nChan
17935 67 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ge);. }else
17936 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
17937 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
17938 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a db, 0);. }.
17939 20 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 p->nChange
1793a 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 = 0;. }. .
1793b 20 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f /* Rollback o
1793c 72 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 r commit any sch
1793d 65 6d 61 20 63 68 61 6e 67 65 73 20 74 68 61 74 ema changes that
1793e 20 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 occurred. */.
1793f 20 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c if( p->rc!=SQL
17940 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c ITE_OK && db->fl
17941 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 ags&SQLITE_Inter
17942 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 nChanges ){.
17943 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e sqlite3ResetIn
17944 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c ternalSchema(db,
17945 20 30 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 0);. db->f
17946 6c 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 lags = (db->flag
17947 73 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 s | SQLITE_Inter
17948 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d nChanges);. }
17949 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 .. /* Release
1794a 20 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 the locks */.
1794b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 sqlite3BtreeMu
1794c 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 texArrayLeave(&p
1794d 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a ->aMutex);. }..
1794e 20 20 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63 /* We have suc
1794f 63 65 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 cessfully halted
17950 20 61 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20 and closed the
17951 56 4d 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 VM. Record this
17952 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 fact. */. if(
17953 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 p->pc>=0 ){.
17954 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
17955 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d t--;. if( !p-
17956 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 >readOnly ){.
17957 20 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 db->writeVdbe
17958 43 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 Cnt--;. }.
17959 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 assert( db->act
1795a 69 76 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e iveVdbeCnt>=db->
1795b 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a writeVdbeCnt );.
1795c 20 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d }. p->magic =
1795d 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 VDBE_MAGIC_HALT
1795e 3b 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 ;. checkActiveV
1795f 64 62 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 dbeCnt(db);. if
17960 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ( p->db->mallocF
17961 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e ailed ){. p->
17962 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
17963 4d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e M;. }.. return
17964 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a SQLITE_OK;.}...
17965 2f 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20 /*.** Each VDBE
17966 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 holds the result
17967 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
17968 65 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 ent sqlite3_step
17969 28 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d () call.** in p-
1796a 3e 72 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69 >rc. This routi
1796b 6e 65 20 73 65 74 73 20 74 68 61 74 20 72 65 73 ne sets that res
1796c 75 6c 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 ult back to SQLI
1796d 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 TE_OK..*/.SQLITE
1796e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1796f 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 lite3VdbeResetSt
17970 65 70 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70 epResult(Vdbe *p
17971 29 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c ){. p->rc = SQL
17972 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
17973 20 43 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45 Clean up a VDBE
17974 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e after execution
17975 20 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 but do not dele
17976 74 65 20 74 68 65 20 56 44 42 45 20 6a 75 73 74 te the VDBE just
17977 20 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 yet..** Write a
17978 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 ny error message
17979 73 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 s into *pzErrMsg
1797a 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 . Return the re
1797b 73 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a sult code..**.**
1797c 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 After this rout
1797d 69 6e 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20 ine is run, the
1797e 56 44 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72 VDBE should be r
1797f 65 61 64 79 20 74 6f 20 62 65 20 65 78 65 63 75 eady to be execu
17980 74 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a ted.** again..**
17981 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 .** To look at i
17982 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 t another way, t
17983 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65 his routine rese
17984 74 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 ts the state of
17985 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d the.** virtual m
17986 61 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 achine from VDBE
17987 5f 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 _MAGIC_RUN or VD
17988 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 BE_MAGIC_HALT ba
17989 63 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 ck to.** VDBE_MA
1798a 47 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c GIC_INIT..*/.SQL
1798b 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1798c 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 sqlite3VdbeReset
1798d 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c (Vdbe *p){. sql
1798e 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d ite3 *db;. db =
1798f 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 p->db;.. /* If
17990 20 74 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 the VM did not
17991 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f run to completio
17992 6e 20 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75 n or if it encou
17993 6e 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 ntered an. ** e
17994 72 72 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 rror, then it mi
17995 67 68 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65 ght not have bee
17996 6e 20 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c n halted properl
17997 79 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a y. So halt. **
17998 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 it now.. */.
17999 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1799a 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c etyOn(db);. sql
1799b 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b ite3VdbeHalt(p);
1799c 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 . (void)sqlite3
1799d 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a SafetyOff(db);..
1799e 20 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45 /* If the VDBE
1799f 20 68 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e has be run even
179a0 20 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e partially, then
179a1 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 65 72 transfer the er
179a2 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e ror code. ** an
179a3 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 d error message
179a4 66 72 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e from the VDBE in
179a5 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 to the main data
179a6 62 61 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 base structure.
179a7 20 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 But. ** if the
179a8 20 56 44 42 45 20 68 61 73 20 6a 75 73 74 20 62 VDBE has just b
179a9 65 65 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62 een set to run b
179aa 75 74 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61 ut has not actua
179ab 6c 6c 79 20 65 78 65 63 75 74 65 64 20 61 6e 79 lly executed any
179ac 0a 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f . ** instructio
179ad 6e 73 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68 ns yet, leave th
179ae 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
179af 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f error informatio
179b0 6e 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a n unchanged.. *
179b1 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 /. if( p->pc>=0
179b2 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a ){. if( p->z
179b3 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 ErrMsg ){.
179b4 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
179b5 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
179b6 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 sqlite3ValueSe
179b7 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 tStr(db->pErr,-1
179b8 2c 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 ,p->zErrMsg,SQLI
179b9 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 TE_UTF8,SQLITE_T
179ba 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 RANSIENT);.
179bb 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
179bc 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 nMalloc();.
179bd 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 db->errCode = p
179be 2d 3e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 ->rc;. sqli
179bf 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
179c0 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 >zErrMsg);.
179c1 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b p->zErrMsg = 0;
179c2 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
179c3 2d 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 ->rc ){. sq
179c4 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 lite3Error(db, p
179c5 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 ->rc, 0);. }e
179c6 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
179c7 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 e3Error(db, SQLI
179c8 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d TE_OK, 0);. }
179c9 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e . }else if( p->
179ca 72 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 rc && p->expired
179cb 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 ){. /* The e
179cc 78 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20 xpired flag was
179cd 73 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20 set on the VDBE
179ce 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 before the first
179cf 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 call. ** to
179d0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 sqlite3_step().
179d1 46 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 For consistency
179d2 28 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 (since sqlite3_s
179d3 74 65 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a tep() was. **
179d4 20 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 called), set th
179d5 65 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 e database error
179d6 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 73 in this case as
179d7 20 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 well.. */.
179d8 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 sqlite3Error(d
179d9 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 b, p->rc, 0);.
179da 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 sqlite3ValueSe
179db 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d tStr(db->pErr, -
179dc 31 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 1, p->zErrMsg, S
179dd 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 QLITE_UTF8, SQLI
179de 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 TE_TRANSIENT);.
179df 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
179e0 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
179e1 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 ;. p->zErrMsg
179e2 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 0;. }.. /*
179e3 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f Reclaim all memo
179e4 72 79 20 75 73 65 64 20 62 79 20 74 68 65 20 56 ry used by the V
179e5 44 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e DBE. */. Clean
179e6 75 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 up(p);.. /* Sav
179e7 65 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f e profiling info
179e8 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 rmation from thi
179e9 73 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f s VDBE run.. */
179ea 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f .#ifdef VDBE_PRO
179eb 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c FILE. {. FIL
179ec 45 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 E *out = fopen("
179ed 76 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 vdbe_profile.out
179ee 22 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28 ", "a");. if(
179ef 20 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e out ){. in
179f0 74 20 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e t i;. fprin
179f1 74 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 tf(out, "---- ")
179f2 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b ;. for(i=0;
179f3 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b i<p->nOp; i++){
179f4 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 . fprintf
179f5 28 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d (out, "%02x", p-
179f6 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b >aOp[i].opcode);
179f7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 . }. f
179f8 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 printf(out, "\n"
179f9 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 );. for(i=0
179fa 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 ; i<p->nOp; i++)
179fb 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 {. fprint
179fc 66 28 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c f(out, "%6d %10l
179fd 6c 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 ld %8lld ",.
179fe 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d p->aOp[i]
179ff 2e 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 .cnt,.
17a00 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 p->aOp[i].cycle
17a01 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d s,. p-
17a02 3e 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 >aOp[i].cnt>0 ?
17a03 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 p->aOp[i].cycles
17a04 2f 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a /p->aOp[i].cnt :
17a05 20 30 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 0. );.
17a06 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
17a07 65 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c ePrintOp(out, i,
17a08 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 &p->aOp[i]);.
17a09 20 20 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f }. fclo
17a0a 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 se(out);. }.
17a0b 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d }.#endif. p->m
17a0c 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
17a0d 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e C_INIT;. return
17a0e 20 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 p->rc & db->err
17a0f 4d 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 Mask;.}. ./*.**
17a10 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c Clean up and del
17a11 65 74 65 20 61 20 56 44 42 45 20 61 66 74 65 72 ete a VDBE after
17a12 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 execution. Ret
17a13 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77 urn an integer w
17a14 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 hich is.** the r
17a15 65 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 esult code. Wri
17a16 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 te any error mes
17a17 73 61 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a sage text into *
17a18 70 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c pzErrMsg..*/.SQL
17a19 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
17a1a 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c sqlite3VdbeFinal
17a1b 69 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ize(Vdbe *p){.
17a1c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
17a1d 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 OK;. if( p->mag
17a1e 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 ic==VDBE_MAGIC_R
17a1f 55 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d UN || p->magic==
17a20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 VDBE_MAGIC_HALT
17a21 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
17a22 74 65 33 56 64 62 65 52 65 73 65 74 28 70 29 3b te3VdbeReset(p);
17a23 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63 . assert( (rc
17a24 20 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 & p->db->errMas
17a25 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 65 6c 73 k)==rc );. }els
17a26 65 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d e if( p->magic!=
17a27 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
17a28 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
17a29 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
17a2a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 . sqlite3VdbeDe
17a2b 6c 65 74 65 28 70 29 3b 0a 20 20 72 65 74 75 72 lete(p);. retur
17a2c 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
17a2d 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 all the destruct
17a2e 6f 72 20 66 6f 72 20 65 61 63 68 20 61 75 78 64 or for each auxd
17a2f 61 74 61 20 65 6e 74 72 79 20 69 6e 20 70 56 64 ata entry in pVd
17a30 62 65 46 75 6e 63 20 66 6f 72 20 77 68 69 63 68 beFunc for which
17a31 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70 6f .** the correspo
17a32 6e 64 69 6e 67 20 62 69 74 20 69 6e 20 6d 61 73 nding bit in mas
17a33 6b 20 69 73 20 63 6c 65 61 72 2e 20 20 41 75 78 k is clear. Aux
17a34 64 61 74 61 20 65 6e 74 72 69 65 73 20 62 65 79 data entries bey
17a35 6f 6e 64 20 33 31 0a 2a 2a 20 61 72 65 20 61 6c ond 31.** are al
17a36 77 61 79 73 20 64 65 73 74 72 6f 79 65 64 2e 20 ways destroyed.
17a37 20 54 6f 20 64 65 73 74 72 6f 79 20 61 6c 6c 20 To destroy all
17a38 61 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 2c auxdata entries,
17a39 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f call this.** ro
17a3a 75 74 69 6e 65 20 77 69 74 68 20 6d 61 73 6b 3d utine with mask=
17a3b 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 =0..*/.SQLITE_PR
17a3c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
17a3d 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 e3VdbeDeleteAuxD
17a3e 61 74 61 28 56 64 62 65 46 75 6e 63 20 2a 70 56 ata(VdbeFunc *pV
17a3f 64 62 65 46 75 6e 63 2c 20 69 6e 74 20 6d 61 73 dbeFunc, int mas
17a40 6b 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 k){. int i;. f
17a41 6f 72 28 69 3d 30 3b 20 69 3c 70 56 64 62 65 46 or(i=0; i<pVdbeF
17a42 75 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b unc->nAux; i++){
17a43 0a 20 20 20 20 73 74 72 75 63 74 20 41 75 78 44 . struct AuxD
17a44 61 74 61 20 2a 70 41 75 78 20 3d 20 26 70 56 64 ata *pAux = &pVd
17a45 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 5d beFunc->apAux[i]
17a46 3b 0a 20 20 20 20 69 66 28 20 28 69 3e 33 31 20 ;. if( (i>31
17a47 7c 7c 20 21 28 6d 61 73 6b 26 28 31 3c 3c 69 29 || !(mask&(1<<i)
17a48 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75 78 )) && pAux->pAux
17a49 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 41 ){. if( pA
17a4a 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 ux->xDelete ){.
17a4b 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44 65 pAux->xDe
17a4c 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78 29 lete(pAux->pAux)
17a4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
17a4e 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b 0a pAux->pAux = 0;.
17a4f 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
17a50 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 ** Delete an ent
17a51 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c ire VDBE..*/.SQL
17a52 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
17a53 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
17a54 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 te(Vdbe *p){. i
17a55 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 nt i;. sqlite3
17a56 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30 *db;.. if( p==0
17a57 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 ) return;. db
17a58 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 = p->db;. if( p
17a59 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 ->pPrev ){. p
17a5a 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d ->pPrev->pNext =
17a5b 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c p->pNext;. }el
17a5c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
17a5d 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a db->pVdbe==p );.
17a5e 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 db->pVdbe =
17a5f 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 p->pNext;. }.
17a60 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a if( p->pNext ){.
17a61 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 p->pNext->pP
17a62 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a rev = p->pPrev;.
17a63 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 }. if( p->aOp
17a64 20 29 7b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 20 ){. Op *pOp
17a65 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 66 6f = p->aOp;. fo
17a66 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b r(i=0; i<p->nOp;
17a67 20 69 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 i++, pOp++){.
17a68 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 freeP4(db, p
17a69 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d Op->p4type, pOp-
17a6a 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66 20 53 >p4.p);.#ifdef S
17a6b 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
17a6c 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
17a6d 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e db, pOp->zCommen
17a6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20 20 20 0a t);.#endif .
17a6f 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
17a70 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 3DbFree(db, p->a
17a71 4f 70 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 Op);. }. relea
17a72 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 seMemArray(p->aV
17a73 61 72 2c 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 ar, p->nVar);.
17a74 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
17a75 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 , p->aLabel);.
17a76 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 if( p->aMem ){.
17a77 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
17a78 28 64 62 2c 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d (db, &p->aMem[1]
17a79 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 );. }. release
17a7a 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c MemArray(p->aCol
17a7b 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c Name, p->nResCol
17a7c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a umn*COLNAME_N);.
17a7d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
17a7e 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 db, p->aColName)
17a7f 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
17a80 65 28 64 62 2c 20 70 2d 3e 7a 53 71 6c 29 3b 0a e(db, p->zSql);.
17a81 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 p->magic = VDB
17a82 45 5f 4d 41 47 49 43 5f 44 45 41 44 3b 0a 20 20 E_MAGIC_DEAD;.
17a83 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
17a84 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 , p);.}../*.** I
17a85 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61 f a MoveTo opera
17a86 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20 tion is pending
17a87 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 on the given cur
17a88 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61 sor, then do tha
17a89 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e t.** MoveTo now.
17a8a 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f Return an erro
17a8b 72 20 63 6f 64 65 2e 20 20 49 66 20 6e 6f 20 4d r code. If no M
17a8c 6f 76 65 54 6f 20 69 73 20 70 65 6e 64 69 6e 67 oveTo is pending
17a8d 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e , this.** routin
17a8e 65 20 64 6f 65 73 20 6e 6f 74 68 69 6e 67 20 61 e does nothing a
17a8f 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 nd returns SQLIT
17a90 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f E_OK..*/.SQLITE_
17a91 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
17a92 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 te3VdbeCursorMov
17a93 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 2a eto(VdbeCursor *
17a94 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 66 p){. if( p->def
17a95 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 erredMoveto ){.
17a96 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b 0a int res, rc;.
17a97 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
17a98 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e ST. extern in
17a99 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 t sqlite3_search
17a9a 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 _count;.#endif.
17a9b 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73 assert( p->is
17a9c 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 20 Table );. rc
17a9d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
17a9e 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d 3e vetoUnpacked(p->
17a9f 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d pCursor, 0, p->m
17aa0 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c 20 ovetoTarget, 0,
17aa1 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72 &res);. if( r
17aa2 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
17aa3 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 20 p->lastRowid
17aa4 3d 20 6b 65 79 54 6f 49 6e 74 28 70 2d 3e 6d 6f = keyToInt(p->mo
17aa5 76 65 74 6f 54 61 72 67 65 74 29 3b 0a 20 20 20 vetoTarget);.
17aa6 20 70 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 p->rowidIsValid
17aa7 20 3d 20 72 65 73 3d 3d 30 3b 0a 20 20 20 20 69 = res==0;. i
17aa8 66 28 20 72 65 73 3c 30 20 29 7b 0a 20 20 20 20 f( res<0 ){.
17aa9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
17aaa 72 65 65 4e 65 78 74 28 70 2d 3e 70 43 75 72 73 reeNext(p->pCurs
17aab 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 or, &res);.
17aac 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
17aad 20 72 63 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 rc;. }.#ifde
17aae 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
17aaf 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 sqlite3_search
17ab0 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
17ab1 0a 20 20 20 20 70 2d 3e 64 65 66 65 72 72 65 64 . p->deferred
17ab2 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 Moveto = 0;.
17ab3 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d p->cacheStatus =
17ab4 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
17ab5 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 70 43 75 }else if( p->pCu
17ab6 72 73 6f 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 rsor ){. int
17ab7 68 61 73 4d 6f 76 65 64 3b 0a 20 20 20 20 69 6e hasMoved;. in
17ab8 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 t rc = sqlite3Bt
17ab9 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 reeCursorHasMove
17aba 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 68 d(p->pCursor, &h
17abb 61 73 4d 6f 76 65 64 29 3b 0a 20 20 20 20 69 66 asMoved);. if
17abc 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
17abd 3b 0a 20 20 20 20 69 66 28 20 68 61 73 4d 6f 76 ;. if( hasMov
17abe 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 63 ed ){. p->c
17abf 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 acheStatus = CAC
17ac0 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 20 20 HE_STALE;.
17ac1 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a p->nullRow = 1;.
17ac2 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
17ac3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
17ac4 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
17ac5 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 3a 0a wing functions:.
17ac6 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 **.** sqlite3Vdb
17ac7 65 53 65 72 69 61 6c 54 79 70 65 28 29 0a 2a 2a eSerialType().**
17ac8 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
17ac9 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a 2a 20 73 alTypeLen().** s
17aca 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
17acb 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 Len().** sqlite3
17acc 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 29 0a VdbeSerialPut().
17acd 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 ** sqlite3VdbeSe
17ace 72 69 61 6c 47 65 74 28 29 0a 2a 2a 0a 2a 2a 20 rialGet().**.**
17acf 65 6e 63 61 70 73 75 6c 61 74 65 20 74 68 65 20 encapsulate the
17ad0 63 6f 64 65 20 74 68 61 74 20 73 65 72 69 61 6c code that serial
17ad1 69 7a 65 73 20 76 61 6c 75 65 73 20 66 6f 72 20 izes values for
17ad2 73 74 6f 72 61 67 65 20 69 6e 20 53 51 4c 69 74 storage in SQLit
17ad3 65 0a 2a 2a 20 64 61 74 61 20 61 6e 64 20 69 6e e.** data and in
17ad4 64 65 78 20 72 65 63 6f 72 64 73 2e 20 45 61 63 dex records. Eac
17ad5 68 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c h serialized val
17ad6 75 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 ue consists of a
17ad7 0a 2a 2a 20 27 73 65 72 69 61 6c 2d 74 79 70 65 .** 'serial-type
17ad8 27 20 61 6e 64 20 61 20 62 6c 6f 62 20 6f 66 20 ' and a blob of
17ad9 64 61 74 61 2e 20 54 68 65 20 73 65 72 69 61 6c data. The serial
17ada 20 74 79 70 65 20 69 73 20 61 6e 20 38 2d 62 79 type is an 8-by
17adb 74 65 20 75 6e 73 69 67 6e 65 64 0a 2a 2a 20 69 te unsigned.** i
17adc 6e 74 65 67 65 72 2c 20 73 74 6f 72 65 64 20 61 nteger, stored a
17add 73 20 61 20 76 61 72 69 6e 74 2e 0a 2a 2a 0a 2a s a varint..**.*
17ade 2a 20 49 6e 20 61 6e 20 53 51 4c 69 74 65 20 69 * In an SQLite i
17adf 6e 64 65 78 20 72 65 63 6f 72 64 2c 20 74 68 65 ndex record, the
17ae0 20 73 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 serial type is
17ae1 73 74 6f 72 65 64 20 64 69 72 65 63 74 6c 79 20 stored directly
17ae2 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 62 6c before.** the bl
17ae3 6f 62 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 ob of data that
17ae4 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 it corresponds t
17ae5 6f 2e 20 49 6e 20 61 20 74 61 62 6c 65 20 72 65 o. In a table re
17ae6 63 6f 72 64 2c 20 61 6c 6c 20 73 65 72 69 61 6c cord, all serial
17ae7 0a 2a 2a 20 74 79 70 65 73 20 61 72 65 20 73 74 .** types are st
17ae8 6f 72 65 64 20 61 74 20 74 68 65 20 73 74 61 72 ored at the star
17ae9 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 2c t of the record,
17aea 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 73 20 6f and the blobs o
17aeb 66 20 64 61 74 61 20 61 74 0a 2a 2a 20 74 68 65 f data at.** the
17aec 20 65 6e 64 2e 20 48 65 6e 63 65 20 74 68 65 73 end. Hence thes
17aed 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6c 6c 6f e functions allo
17aee 77 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 w the caller to
17aef 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 73 65 handle the.** se
17af0 72 69 61 6c 2d 74 79 70 65 20 61 6e 64 20 64 61 rial-type and da
17af1 74 61 20 62 6c 6f 62 20 73 65 70 65 72 61 74 65 ta blob seperate
17af2 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ly..**.** The fo
17af3 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 llowing table de
17af4 73 63 72 69 62 65 73 20 74 68 65 20 76 61 72 69 scribes the vari
17af5 6f 75 73 20 73 74 6f 72 61 67 65 20 63 6c 61 73 ous storage clas
17af6 73 65 73 20 66 6f 72 20 64 61 74 61 3a 0a 2a 2a ses for data:.**
17af7 0a 2a 2a 20 20 20 73 65 72 69 61 6c 20 74 79 70 .** serial typ
17af8 65 20 20 20 20 20 20 20 20 62 79 74 65 73 20 6f e bytes o
17af9 66 20 64 61 74 61 20 20 20 20 20 20 74 79 70 65 f data type
17afa 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ----------
17afb 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d ---- -------
17afc 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d 2d -------- ----
17afd 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 -----------.**
17afe 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 0
17aff 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20 20 0
17b00 20 20 20 20 20 20 20 4e 55 4c 4c 0a 2a 2a 20 20 NULL.**
17b01 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20 1
17b02 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20 20 1
17b03 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e signed in
17b04 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 32 20 teger.** 2
17b05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17b06 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 2
17b07 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a signed integer.
17b08 2a 2a 20 20 20 20 20 20 33 20 20 20 20 20 20 20 ** 3
17b09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 20 3
17b0a 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 signe
17b0b 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 d integer.**
17b0c 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20 20 4
17b0d 20 20 20 20 20 20 20 20 34 20 20 20 20 20 20 20 4
17b0e 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 signed inte
17b0f 67 65 72 0a 2a 2a 20 20 20 20 20 20 35 20 20 20 ger.** 5
17b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17b11 20 20 36 20 20 20 20 20 20 20 20 20 20 20 20 73 6 s
17b12 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
17b13 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20 6
17b14 20 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20 8
17b15 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 signed
17b16 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 integer.**
17b17 37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7
17b18 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20 8
17b19 20 20 20 49 45 45 45 20 66 6c 6f 61 74 0a 2a 2a IEEE float.**
17b1a 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20 8
17b1b 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 0
17b1c 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 65 72 Integer
17b1d 20 63 6f 6e 73 74 61 6e 74 20 30 0a 2a 2a 20 20 constant 0.**
17b1e 20 20 20 20 39 20 20 20 20 20 20 20 20 20 20 20 9
17b1f 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20 20 0
17b20 20 20 20 20 20 20 20 49 6e 74 65 67 65 72 20 63 Integer c
17b21 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a 20 20 20 20 onstant 1.**
17b22 20 31 30 2c 31 31 20 20 20 20 20 20 20 20 20 20 10,11
17b23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17b24 20 20 20 20 20 72 65 73 65 72 76 65 64 20 66 6f reserved fo
17b25 72 20 65 78 70 61 6e 73 69 6f 6e 0a 2a 2a 20 20 r expansion.**
17b26 20 20 4e 3e 3d 31 32 20 61 6e 64 20 65 76 65 6e N>=12 and even
17b27 20 20 20 20 20 20 20 28 4e 2d 31 32 29 2f 32 20 (N-12)/2
17b28 20 20 20 20 20 20 20 42 4c 4f 42 0a 2a 2a 20 20 BLOB.**
17b29 20 20 4e 3e 3d 31 33 20 61 6e 64 20 6f 64 64 20 N>=13 and odd
17b2a 20 20 20 20 20 20 20 28 4e 2d 31 33 29 2f 32 20 (N-13)/2
17b2b 20 20 20 20 20 20 20 74 65 78 74 0a 2a 2a 0a 2a text.**.*
17b2c 2a 20 54 68 65 20 38 20 61 6e 64 20 39 20 74 79 * The 8 and 9 ty
17b2d 70 65 73 20 77 65 72 65 20 61 64 64 65 64 20 69 pes were added i
17b2e 6e 20 33 2e 33 2e 30 2c 20 66 69 6c 65 20 66 6f n 3.3.0, file fo
17b2f 72 6d 61 74 20 34 2e 20 20 50 72 69 6f 72 20 76 rmat 4. Prior v
17b30 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 ersions.** of SQ
17b31 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 75 6e Lite will not un
17b32 64 65 72 73 74 61 6e 64 20 74 68 6f 73 65 20 73 derstand those s
17b33 65 72 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2f 0a erial types..*/.
17b34 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
17b35 65 20 73 65 72 69 61 6c 2d 74 79 70 65 20 66 6f e serial-type fo
17b36 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 r the value stor
17b37 65 64 20 69 6e 20 70 4d 65 6d 2e 0a 2a 2f 0a 53 ed in pMem..*/.S
17b38 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 QLITE_PRIVATE u3
17b39 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 2 sqlite3VdbeSer
17b3a 69 61 6c 54 79 70 65 28 4d 65 6d 20 2a 70 4d 65 ialType(Mem *pMe
17b3b 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d m, int file_form
17b3c 61 74 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 at){. int flags
17b3d 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a = pMem->flags;.
17b3e 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28 20 int n;.. if(
17b3f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 flags&MEM_Null )
17b40 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
17b41 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 }. if( flags&
17b42 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 2f MEM_Int ){. /
17b43 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 * Figure out whe
17b44 74 68 65 72 20 74 6f 20 75 73 65 20 31 2c 20 32 ther to use 1, 2
17b45 2c 20 34 2c 20 36 20 6f 72 20 38 20 62 79 74 65 , 4, 6 or 8 byte
17b46 73 2e 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e 65 s. */.# define
17b47 20 4d 41 58 5f 36 42 59 54 45 20 28 28 28 28 69 MAX_6BYTE ((((i
17b48 36 34 29 30 78 30 30 30 30 38 30 30 30 29 3c 3c 64)0x00008000)<<
17b49 33 32 29 2d 31 29 0a 20 20 20 20 69 36 34 20 69 32)-1). i64 i
17b4a 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 = pMem->u.i;.
17b4b 20 20 75 36 34 20 75 3b 0a 20 20 20 20 69 66 28 u64 u;. if(
17b4c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 file_format>=4
17b4d 26 26 20 28 69 26 31 29 3d 3d 69 20 29 7b 0a 20 && (i&1)==i ){.
17b4e 20 20 20 20 20 72 65 74 75 72 6e 20 38 2b 69 3b return 8+i;
17b4f 0a 20 20 20 20 7d 0a 20 20 20 20 75 20 3d 20 69 . }. u = i
17b50 3c 30 20 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20 <0 ? -i : i;.
17b51 20 69 66 28 20 75 3c 3d 31 32 37 20 29 20 72 65 if( u<=127 ) re
17b52 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 turn 1;. if(
17b53 75 3c 3d 33 32 37 36 37 20 29 20 72 65 74 75 72 u<=32767 ) retur
17b54 6e 20 32 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d n 2;. if( u<=
17b55 38 33 38 38 36 30 37 20 29 20 72 65 74 75 72 6e 8388607 ) return
17b56 20 33 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 32 3;. if( u<=2
17b57 31 34 37 34 38 33 36 34 37 20 29 20 72 65 74 75 147483647 ) retu
17b58 72 6e 20 34 3b 0a 20 20 20 20 69 66 28 20 75 3c rn 4;. if( u<
17b59 3d 4d 41 58 5f 36 42 59 54 45 20 29 20 72 65 74 =MAX_6BYTE ) ret
17b5a 75 72 6e 20 35 3b 0a 20 20 20 20 72 65 74 75 72 urn 5;. retur
17b5b 6e 20 36 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 n 6;. }. if( f
17b5c 6c 61 67 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b lags&MEM_Real ){
17b5d 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 . return 7;.
17b5e 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 }. assert( pMe
17b5f 6d 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 m->db->mallocFai
17b60 6c 65 64 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45 led || flags&(ME
17b61 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
17b62 29 3b 0a 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e );. n = pMem->n
17b63 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 ;. if( flags &
17b64 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 MEM_Zero ){.
17b65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a n += pMem->u.i;.
17b66 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e }. assert( n>
17b67 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 =0 );. return (
17b68 28 6e 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 66 (n*2) + 12 + ((f
17b69 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 lags&MEM_Str)!=0
17b6a 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ));.}../*.** Ret
17b6b 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f urn the length o
17b6c 66 20 74 68 65 20 64 61 74 61 20 63 6f 72 72 65 f the data corre
17b6d 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 sponding to the
17b6e 73 75 70 70 6c 69 65 64 20 73 65 72 69 61 6c 2d supplied serial-
17b6f 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f type..*/.SQLITE_
17b70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
17b71 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
17b72 65 4c 65 6e 28 75 33 32 20 73 65 72 69 61 6c 5f eLen(u32 serial_
17b73 74 79 70 65 29 7b 0a 20 20 69 66 28 20 73 65 72 type){. if( ser
17b74 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a ial_type>=12 ){.
17b75 20 20 20 20 72 65 74 75 72 6e 20 28 73 65 72 69 return (seri
17b76 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 al_type-12)/2;.
17b77 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 }else{. stat
17b78 69 63 20 63 6f 6e 73 74 20 75 38 20 61 53 69 7a ic const u8 aSiz
17b79 65 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c e[] = { 0, 1, 2,
17b7a 20 33 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 3, 4, 6, 8, 8,
17b7b 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 0, 0, 0, 0 };.
17b7c 20 20 72 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 return aSize[s
17b7d 65 72 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d erial_type];. }
17b7e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 .}../*.** If we
17b7f 61 72 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 are on an archit
17b80 65 63 74 75 72 65 20 77 69 74 68 20 6d 69 78 65 ecture with mixe
17b81 64 2d 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e d-endian floatin
17b82 67 20 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 g .** points (ex
17b83 3a 20 41 52 4d 37 29 20 74 68 65 6e 20 73 77 61 : ARM7) then swa
17b84 70 20 74 68 65 20 6c 6f 77 65 72 20 34 20 62 79 p the lower 4 by
17b85 74 65 73 20 77 69 74 68 20 74 68 65 20 0a 2a 2a tes with the .**
17b86 20 75 70 70 65 72 20 34 20 62 79 74 65 73 2e 20 upper 4 bytes.
17b87 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 Return the resu
17b88 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f lt..**.** For mo
17b89 73 74 20 61 72 63 68 69 74 65 63 74 75 72 65 73 st architectures
17b8a 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f , this is a no-o
17b8b 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 p..**.** (later)
17b8c 3a 20 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 : It is reporte
17b8d 64 20 74 6f 20 6d 65 20 74 68 61 74 20 74 68 65 d to me that the
17b8e 20 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 mixed-endian pr
17b8f 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 oblem.** on ARM7
17b90 20 69 73 20 61 6e 20 69 73 73 75 65 20 77 69 74 is an issue wit
17b91 68 20 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 h GCC, not with
17b92 74 68 65 20 41 52 4d 37 20 63 68 69 70 2e 20 20 the ARM7 chip.
17b93 49 74 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 It seems.** that
17b94 20 65 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 early versions
17b95 6f 66 20 47 43 43 20 73 74 6f 72 65 64 20 74 68 of GCC stored th
17b96 65 20 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 e two words of a
17b97 20 36 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 64-bit.** float
17b98 20 69 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 in the wrong or
17b99 64 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 65 der. And that e
17b9a 72 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 rror has been pr
17b9b 6f 70 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 opagated.** ever
17b9c 20 73 69 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 since. The bla
17b9d 6d 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 me is not necess
17b9e 61 72 69 6c 79 20 77 69 74 68 20 47 43 43 2c 20 arily with GCC,
17b9f 74 68 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d though..** GCC m
17ba0 69 67 68 74 20 68 61 76 65 20 6a 75 73 74 20 63 ight have just c
17ba1 6f 70 79 69 6e 67 20 74 68 65 20 70 72 6f 62 6c opying the probl
17ba2 65 6d 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 em from a prior
17ba3 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 compiler..** I a
17ba4 6d 20 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 m also told that
17ba5 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 newer versions
17ba6 6f 66 20 47 43 43 20 74 68 61 74 20 66 6f 6c 6c of GCC that foll
17ba7 6f 77 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a ow a different.*
17ba8 2a 20 41 42 49 20 67 65 74 20 74 68 65 20 62 79 * ABI get the by
17ba9 74 65 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0a te order right..
17baa 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 **.** Developers
17bab 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e using SQLite on
17bac 20 61 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 an ARM7 should
17bad 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 compile and run
17bae 74 68 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 their.** applica
17baf 74 69 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c tion using -DSQL
17bb0 49 54 45 5f 44 45 42 55 47 3d 31 20 61 74 20 6c ITE_DEBUG=1 at l
17bb1 65 61 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 east once. With
17bb2 20 44 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 DEBUG.** enable
17bb3 64 2c 20 73 6f 6d 65 20 61 73 73 65 72 74 73 20 d, some asserts
17bb4 62 65 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 below will ensur
17bb5 65 20 74 68 61 74 20 74 68 65 20 62 79 74 65 20 e that the byte
17bb6 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 order of.** floa
17bb7 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
17bb8 73 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a s is correct..**
17bb9 0a 2a 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 29 .** (2007-08-30)
17bba 20 20 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 Frank van Vugt
17bbb 20 68 61 73 20 73 74 75 64 69 65 64 20 74 68 69 has studied thi
17bbc 73 20 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c s problem closel
17bbd 79 0a 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 6e y.** and has sen
17bbe 64 20 68 69 73 20 66 69 6e 64 69 6e 67 73 20 74 d his findings t
17bbf 6f 20 74 68 65 20 53 51 4c 69 74 65 20 64 65 76 o the SQLite dev
17bc0 65 6c 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a elopers. Frank.
17bc1 2a 2a 20 77 72 69 74 65 73 20 74 68 61 74 20 73 ** writes that s
17bc2 6f 6d 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c ome Linux kernel
17bc3 73 20 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 s offer floating
17bc4 20 70 6f 69 6e 74 20 68 61 72 64 77 61 72 65 0a point hardware.
17bc5 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 ** emulation tha
17bc6 74 20 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 t uses only 32-b
17bc7 69 74 20 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 it mantissas ins
17bc8 74 65 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a tead of a full .
17bc9 2a 2a 20 34 38 2d 62 69 74 73 20 61 73 20 72 65 ** 48-bits as re
17bca 71 75 69 72 65 64 20 62 79 20 74 68 65 20 49 45 quired by the IE
17bcb 45 45 20 73 74 61 6e 64 61 72 64 2e 20 20 28 54 EE standard. (T
17bcc 68 69 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f his is the.** CO
17bcd 4e 46 49 47 5f 46 50 45 5f 46 41 53 54 46 50 45 NFIG_FPE_FASTFPE
17bce 20 6f 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 option.) On su
17bcf 63 68 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 ch systems, floa
17bd0 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 ting point.** by
17bd1 74 65 20 73 77 61 70 70 69 6e 67 20 62 65 63 6f te swapping beco
17bd2 6d 65 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 mes very complic
17bd3 61 74 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 ated. To avoid
17bd4 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 problems,.** the
17bd5 20 6e 65 63 65 73 73 61 72 79 20 62 79 74 65 20 necessary byte
17bd6 73 77 61 70 70 69 6e 67 20 69 73 20 63 61 72 72 swapping is carr
17bd7 69 65 64 20 6f 75 74 20 75 73 69 6e 67 20 61 20 ied out using a
17bd8 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 64-bit integer.*
17bd9 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 * rather than a
17bda 36 34 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 64-bit float. F
17bdb 72 61 6e 6b 20 61 73 73 75 72 65 73 20 75 73 20 rank assures us
17bdc 74 68 61 74 20 74 68 65 20 63 6f 64 65 20 68 65 that the code he
17bdd 72 65 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 re.** works for
17bde 68 69 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65 him. We, the de
17bdf 76 65 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e velopers, have n
17be0 6f 20 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e o way to indepen
17be1 64 65 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 dently.** verify
17be2 20 74 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b this, but Frank
17be3 20 73 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 seems to know w
17be4 68 61 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e hat he is talkin
17be5 67 20 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 g about.** so we
17be6 20 74 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 trust him..*/.#
17be7 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 ifdef SQLITE_MIX
17be8 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f ED_ENDIAN_64BIT_
17be9 46 4c 4f 41 54 0a 73 74 61 74 69 63 20 75 36 34 FLOAT.static u64
17bea 20 66 6c 6f 61 74 53 77 61 70 28 75 36 34 20 69 floatSwap(u64 i
17beb 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 n){. union {.
17bec 20 20 75 36 34 20 72 3b 0a 20 20 20 20 75 33 32 u64 r;. u32
17bed 20 69 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 i[2];. } u;.
17bee 75 33 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 u32 t;.. u.r =
17bef 69 6e 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d in;. t = u.i[0]
17bf0 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 ;. u.i[0] = u.i
17bf1 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 [1];. u.i[1] =
17bf2 74 3b 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b t;. return u.r;
17bf3 0a 7d 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 .}.# define swap
17bf4 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 MixedEndianFloat
17bf5 28 58 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 (X) X = floatSw
17bf6 61 70 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 ap(X).#else.# de
17bf7 66 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45 6e fine swapMixedEn
17bf8 64 69 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e dianFloat(X).#en
17bf9 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 dif../*.** Write
17bfa 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 the serialized
17bfb 64 61 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 data blob for th
17bfc 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 e value stored i
17bfd 6e 20 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 n pMem into .**
17bfe 62 75 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d buf. It is assum
17bff 65 64 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c ed that the call
17c00 65 72 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 er has allocated
17c01 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 sufficient spac
17c02 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 e..** Return the
17c03 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
17c04 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 written..**.**
17c05 6e 42 75 66 20 69 73 20 74 68 65 20 61 6d 6f 75 nBuf is the amou
17c06 6e 74 20 6f 66 20 73 70 61 63 65 20 6c 65 66 74 nt of space left
17c07 20 69 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 in buf[]. nBuf
17c08 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 0a must always be.
17c09 2a 2a 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 ** large enough
17c0a 74 6f 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 to hold the enti
17c0b 72 65 20 66 69 65 6c 64 2e 20 20 45 78 63 65 70 re field. Excep
17c0c 74 2c 20 69 66 20 74 68 65 20 66 69 65 6c 64 20 t, if the field
17c0d 69 73 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 is.** a blob wit
17c0e 68 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 h a zero-filled
17c0f 74 61 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d tail, then buf[]
17c10 20 6d 69 67 68 74 20 62 65 20 6a 75 73 74 20 74 might be just t
17c11 68 65 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 he right.** size
17c12 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68 to hold everyth
17c13 69 6e 67 20 65 78 63 65 70 74 20 66 6f 72 20 74 ing except for t
17c14 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 he zero-filled t
17c15 61 69 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a ail. If buf[].*
17c16 2a 20 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e * is only big en
17c17 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 ough to hold the
17c18 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 non-zero prefix
17c19 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 , then only writ
17c1a 65 20 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 e that.** prefix
17c1b 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 into buf[]. Bu
17c1c 74 20 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 t if buf[] is la
17c1d 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f rge enough to ho
17c1e 6c 64 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 ld both the.** p
17c1f 72 65 66 69 78 20 61 6e 64 20 74 68 65 20 74 61 refix and the ta
17c20 69 6c 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 il then write th
17c21 65 20 70 72 65 66 69 78 20 61 6e 64 20 73 65 74 e prefix and set
17c22 20 74 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c the tail to all
17c23 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a .** zeros..**.**
17c24 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
17c25 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 er of bytes actu
17c26 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 ally written int
17c27 6f 20 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 o buf[]. The nu
17c28 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 mber.** of bytes
17c29 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c in the zero-fil
17c2a 6c 65 64 20 74 61 69 6c 20 69 73 20 69 6e 63 6c led tail is incl
17c2b 75 64 65 64 20 69 6e 20 74 68 65 20 72 65 74 75 uded in the retu
17c2c 72 6e 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a rn value only.**
17c2d 20 69 66 20 74 68 6f 73 65 20 62 79 74 65 73 20 if those bytes
17c2e 77 65 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 were zeroed in b
17c2f 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 uf[]..*/ .SQLITE
17c30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
17c31 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 ite3VdbeSerialPu
17c32 74 28 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e t(u8 *buf, int n
17c33 42 75 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 Buf, Mem *pMem,
17c34 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 int file_format)
17c35 7b 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 {. u32 serial_t
17c36 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ype = sqlite3Vdb
17c37 65 53 65 72 69 61 6c 54 79 70 65 28 70 4d 65 6d eSerialType(pMem
17c38 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a , file_format);.
17c39 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 2f 2a int len;.. /*
17c3a 20 49 6e 74 65 67 65 72 20 61 6e 64 20 52 65 61 Integer and Rea
17c3b 6c 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 l */. if( seria
17c3c 6c 5f 74 79 70 65 3c 3d 37 20 26 26 20 73 65 72 l_type<=7 && ser
17c3d 69 61 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 ial_type>0 ){.
17c3e 20 20 75 36 34 20 76 3b 0a 20 20 20 20 69 6e 74 u64 v;. int
17c3f 20 69 3b 0a 20 20 20 20 69 66 28 20 73 65 72 69 i;. if( seri
17c40 61 6c 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 al_type==7 ){.
17c41 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 assert( size
17c42 6f 66 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d of(v)==sizeof(pM
17c43 65 6d 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 em->r) );.
17c44 6d 65 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 6d memcpy(&v, &pMem
17c45 2d 3e 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b ->r, sizeof(v));
17c46 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 . swapMixed
17c47 45 6e 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a EndianFloat(v);.
17c48 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
17c49 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a v = pMem->u.i;.
17c4a 20 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 }. len =
17c4b 69 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 i = sqlite3VdbeS
17c4c 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 erialTypeLen(ser
17c4d 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 ial_type);. a
17c4e 73 73 65 72 74 28 20 6c 65 6e 3c 3d 6e 42 75 66 ssert( len<=nBuf
17c4f 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 );. while( i
17c50 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 75 66 5b -- ){. buf[
17c51 69 5d 20 3d 20 28 76 26 30 78 46 46 29 3b 0a 20 i] = (v&0xFF);.
17c52 20 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 v >>= 8;.
17c53 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c }. return l
17c54 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 en;. }.. /* St
17c55 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 2a 2f 0a ring or blob */.
17c56 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 if( serial_typ
17c57 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 61 73 73 e>=12 ){. ass
17c58 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 20 2b 20 28 ert( pMem->n + (
17c59 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d (pMem->flags & M
17c5a 45 4d 5f 5a 65 72 6f 29 3f 70 4d 65 6d 2d 3e 75 EM_Zero)?pMem->u
17c5b 2e 69 3a 30 29 0a 20 20 20 20 20 20 20 20 20 20 .i:0).
17c5c 20 20 20 3d 3d 20 73 71 6c 69 74 65 33 56 64 62 == sqlite3Vdb
17c5d 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 eSerialTypeLen(s
17c5e 65 72 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20 erial_type) );.
17c5f 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d assert( pMem-
17c60 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 >n<=nBuf );.
17c61 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 len = pMem->n;.
17c62 20 20 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70 memcpy(buf, p
17c63 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 Mem->z, len);.
17c64 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 if( pMem->flag
17c65 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a s & MEM_Zero ){.
17c66 20 20 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 len += pMe
17c67 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 69 66 m->u.i;. if
17c68 28 20 6c 65 6e 3e 6e 42 75 66 20 29 7b 0a 20 20 ( len>nBuf ){.
17c69 20 20 20 20 20 20 6c 65 6e 20 3d 20 6e 42 75 66 len = nBuf
17c6a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
17c6b 6d 65 6d 73 65 74 28 26 62 75 66 5b 70 4d 65 6d memset(&buf[pMem
17c6c 2d 3e 6e 5d 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65 ->n], 0, len-pMe
17c6d 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 m->n);. }.
17c6e 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d return len;. }
17c6f 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63 .. /* NULL or c
17c70 6f 6e 73 74 61 6e 74 73 20 30 20 6f 72 20 31 20 onstants 0 or 1
17c71 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d */. return 0;.}
17c72 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c ../*.** Deserial
17c73 69 7a 65 20 74 68 65 20 64 61 74 61 20 62 6c 6f ize the data blo
17c74 62 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 b pointed to by
17c75 62 75 66 20 61 73 20 73 65 72 69 61 6c 20 74 79 buf as serial ty
17c76 70 65 20 73 65 72 69 61 6c 5f 74 79 70 65 0a 2a pe serial_type.*
17c77 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 * and store the
17c78 72 65 73 75 6c 74 20 69 6e 20 70 4d 65 6d 2e 20 result in pMem.
17c79 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
17c7a 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 er of bytes read
17c7b 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 ..*/ .SQLITE_PRI
17c7c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
17c7d 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 0a 20 VdbeSerialGet(.
17c7e 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
17c7f 63 68 61 72 20 2a 62 75 66 2c 20 20 20 20 20 2f char *buf, /
17c80 2a 20 42 75 66 66 65 72 20 74 6f 20 64 65 73 65 * Buffer to dese
17c81 72 69 61 6c 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a rialize from */.
17c82 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 u32 serial_typ
17c83 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
17c84 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 74 /* Serial type t
17c85 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 2a 2f o deserialize */
17c86 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 . Mem *pMem
17c87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c88 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 /* Memory cell
17c89 74 6f 20 77 72 69 74 65 20 76 61 6c 75 65 20 69 to write value i
17c8a 6e 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 nto */.){. swit
17c8b 63 68 28 20 73 65 72 69 61 6c 5f 74 79 70 65 20 ch( serial_type
17c8c 29 7b 0a 20 20 20 20 63 61 73 65 20 31 30 3a 20 ){. case 10:
17c8d 20 20 2f 2a 20 52 65 73 65 72 76 65 64 20 66 6f /* Reserved fo
17c8e 72 20 66 75 74 75 72 65 20 75 73 65 20 2a 2f 0a r future use */.
17c8f 20 20 20 20 63 61 73 65 20 31 31 3a 20 20 20 2f case 11: /
17c90 2a 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 * Reserved for f
17c91 75 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 uture use */.
17c92 20 63 61 73 65 20 30 3a 20 7b 20 20 2f 2a 20 4e case 0: { /* N
17c93 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 ULL */. pMe
17c94 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e m->flags = MEM_N
17c95 75 6c 6c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ull;. break
17c96 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
17c97 20 31 3a 20 7b 20 2f 2a 20 31 2d 62 79 74 65 20 1: { /* 1-byte
17c98 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
17c99 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e /. pMem->u.
17c9a 69 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 i = (signed char
17c9b 29 62 75 66 5b 30 5d 3b 0a 20 20 20 20 20 20 70 )buf[0];. p
17c9c 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
17c9d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 _Int;. retu
17c9e 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
17c9f 63 61 73 65 20 32 3a 20 7b 20 2f 2a 20 32 2d 62 case 2: { /* 2-b
17ca0 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
17ca1 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d er */. pMem
17ca2 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 ->u.i = (((signe
17ca3 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c d char)buf[0])<<
17ca4 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 8) | buf[1];.
17ca5 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
17ca6 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
17ca7 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 7d 0a return 2;. }.
17ca8 20 20 20 20 63 61 73 65 20 33 3a 20 7b 20 2f 2a case 3: { /*
17ca9 20 33 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 3-byte signed i
17caa 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 nteger */.
17cab 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 73 pMem->u.i = (((s
17cac 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 igned char)buf[0
17cad 5d 29 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 31 ])<<16) | (buf[1
17cae 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 32 5d 3b 0a ]<<8) | buf[2];.
17caf 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
17cb0 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
17cb1 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 return 3;.
17cb2 20 7d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7b }. case 4: {
17cb3 20 2f 2a 20 34 2d 62 79 74 65 20 73 69 67 6e 65 /* 4-byte signe
17cb4 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 d integer */.
17cb5 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 pMem->u.i = (
17cb6 62 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 buf[0]<<24) | (b
17cb7 75 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 uf[1]<<16) | (bu
17cb8 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 f[2]<<8) | buf[3
17cb9 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 ];. pMem->f
17cba 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
17cbb 20 20 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a return 4;.
17cbc 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 35 }. case 5
17cbd 3a 20 7b 20 2f 2a 20 36 2d 62 79 74 65 20 73 69 : { /* 6-byte si
17cbe 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
17cbf 20 20 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 u64 x = ((
17cc0 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 (signed char)buf
17cc1 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 [0])<<8) | buf[1
17cc2 5d 3b 0a 20 20 20 20 20 20 75 33 32 20 79 20 3d ];. u32 y =
17cc3 20 28 62 75 66 5b 32 5d 3c 3c 32 34 29 20 7c 20 (buf[2]<<24) |
17cc4 28 62 75 66 5b 33 5d 3c 3c 31 36 29 20 7c 20 28 (buf[3]<<16) | (
17cc5 62 75 66 5b 34 5d 3c 3c 38 29 20 7c 20 62 75 66 buf[4]<<8) | buf
17cc6 5b 35 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 [5];. x = (
17cc7 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 x<<32) | y;.
17cc8 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 pMem->u.i = *(
17cc9 69 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 70 i64*)&x;. p
17cca 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
17ccb 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 _Int;. retu
17ccc 72 6e 20 36 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 6;. }.
17ccd 63 61 73 65 20 36 3a 20 20 20 2f 2a 20 38 2d 62 case 6: /* 8-b
17cce 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
17ccf 65 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 37 er */. case 7
17cd0 3a 20 7b 20 2f 2a 20 49 45 45 45 20 66 6c 6f 61 : { /* IEEE floa
17cd1 74 69 6e 67 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 ting point */.
17cd2 20 20 20 20 75 36 34 20 78 3b 0a 20 20 20 20 20 u64 x;.
17cd3 20 75 33 32 20 79 3b 0a 23 69 66 20 21 64 65 66 u32 y;.#if !def
17cd4 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 ined(NDEBUG) &&
17cd5 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
17cd6 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
17cd7 49 4e 54 29 0a 20 20 20 20 20 20 2f 2a 20 56 65 INT). /* Ve
17cd8 72 69 66 79 20 74 68 61 74 20 69 6e 74 65 67 65 rify that intege
17cd9 72 73 20 61 6e 64 20 66 6c 6f 61 74 69 6e 67 20 rs and floating
17cda 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 75 73 65 point values use
17cdb 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20 20 the same.
17cdc 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 ** byte order.
17cdd 4f 72 2c 20 74 68 61 74 20 69 66 20 53 51 4c 49 Or, that if SQLI
17cde 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f TE_MIXED_ENDIAN_
17cdf 36 34 42 49 54 5f 46 4c 4f 41 54 20 69 73 0a 20 64BIT_FLOAT is.
17ce0 20 20 20 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 ** defined
17ce1 74 68 61 74 20 36 34 2d 62 69 74 20 66 6c 6f 61 that 64-bit floa
17ce2 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
17ce3 73 20 72 65 61 6c 6c 79 20 61 72 65 20 6d 69 78 s really are mix
17ce4 65 64 0a 20 20 20 20 20 20 2a 2a 20 65 6e 64 69 ed. ** endi
17ce5 61 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 an.. */.
17ce6 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
17ce7 75 36 34 20 74 31 20 3d 20 28 28 75 36 34 29 30 u64 t1 = ((u64)0
17ce8 78 33 66 66 30 30 30 30 30 29 3c 3c 33 32 3b 0a x3ff00000)<<32;.
17ce9 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e static con
17cea 73 74 20 64 6f 75 62 6c 65 20 72 31 20 3d 20 31 st double r1 = 1
17ceb 2e 30 3b 0a 20 20 20 20 20 20 75 36 34 20 74 32 .0;. u64 t2
17cec 20 3d 20 74 31 3b 0a 20 20 20 20 20 20 73 77 61 = t1;. swa
17ced 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 pMixedEndianFloa
17cee 74 28 74 32 29 3b 0a 20 20 20 20 20 20 61 73 73 t(t2);. ass
17cef 65 72 74 28 20 73 69 7a 65 6f 66 28 72 31 29 3d ert( sizeof(r1)=
17cf0 3d 73 69 7a 65 6f 66 28 74 32 29 20 26 26 20 6d =sizeof(t2) && m
17cf1 65 6d 63 6d 70 28 26 72 31 2c 20 26 74 32 2c 20 emcmp(&r1, &t2,
17cf2 73 69 7a 65 6f 66 28 72 31 29 29 3d 3d 30 20 29 sizeof(r1))==0 )
17cf3 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 ;.#endif..
17cf4 78 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 x = (buf[0]<<24)
17cf5 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 | (buf[1]<<16)
17cf6 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 | (buf[2]<<8) |
17cf7 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 79 20 buf[3];. y
17cf8 3d 20 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c = (buf[4]<<24) |
17cf9 20 28 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 (buf[5]<<16) |
17cfa 28 62 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 (buf[6]<<8) | bu
17cfb 66 5b 37 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 f[7];. x =
17cfc 28 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 (x<<32) | y;.
17cfd 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 if( serial_ty
17cfe 70 65 3d 3d 36 20 29 7b 0a 20 20 20 20 20 20 20 pe==6 ){.
17cff 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 pMem->u.i = *(i
17d00 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 20 20 64*)&x;.
17d01 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
17d02 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c M_Int;. }el
17d03 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 se{. asse
17d04 72 74 28 20 73 69 7a 65 6f 66 28 78 29 3d 3d 38 rt( sizeof(x)==8
17d05 20 26 26 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d && sizeof(pMem-
17d06 3e 72 29 3d 3d 38 20 29 3b 0a 20 20 20 20 20 20 >r)==8 );.
17d07 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 swapMixedEndia
17d08 6e 46 6c 6f 61 74 28 78 29 3b 0a 20 20 20 20 20 nFloat(x);.
17d09 20 20 20 6d 65 6d 63 70 79 28 26 70 4d 65 6d 2d memcpy(&pMem-
17d0a 3e 72 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 >r, &x, sizeof(x
17d0b 29 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d ));. pMem
17d0c 2d 3e 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 ->flags = sqlite
17d0d 33 49 73 4e 61 4e 28 70 4d 65 6d 2d 3e 72 29 20 3IsNaN(pMem->r)
17d0e 3f 20 4d 45 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d ? MEM_Null : MEM
17d0f 5f 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 _Real;. }.
17d10 20 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 return 8;.
17d11 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38 3a }. case 8:
17d12 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 30 /* Integer 0
17d13 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a 20 */. case 9:
17d14 7b 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31 20 { /* Integer 1
17d15 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 */. pMem->u
17d16 2e 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70 65 .i = serial_type
17d17 2d 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e -8;. pMem->
17d18 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
17d19 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
17d1a 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
17d1b 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 lt: {. int
17d1c 6c 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74 79 len = (serial_ty
17d1d 70 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20 20 pe-12)/2;.
17d1e 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 pMem->z = (char
17d1f 2a 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d 65 *)buf;. pMe
17d20 6d 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20 20 m->n = len;.
17d21 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 pMem->xDel = 0
17d22 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69 ;. if( seri
17d23 61 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b 0a al_type&0x01 ){.
17d24 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c pMem->fl
17d25 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c 20 ags = MEM_Str |
17d26 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 MEM_Ephem;.
17d27 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
17d28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
17d29 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70 68 M_Blob | MEM_Eph
17d2a 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 em;. }.
17d2b 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 return len;.
17d2c 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
17d2d 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69 0;.}.../*.** Gi
17d2e 76 65 6e 20 74 68 65 20 6e 4b 65 79 2d 62 79 74 ven the nKey-byt
17d2f 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 e encoding of a
17d30 72 65 63 6f 72 64 20 69 6e 20 70 4b 65 79 5b 5d record in pKey[]
17d31 2c 20 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 72 , parse the.** r
17d32 65 63 6f 72 64 20 69 6e 74 6f 20 61 20 55 6e 70 ecord into a Unp
17d33 61 63 6b 65 64 52 65 63 6f 72 64 20 73 74 72 75 ackedRecord stru
17d34 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 61 cture. Return a
17d35 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 pointer to.** t
17d36 68 61 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a hat structure..*
17d37 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 *.** The calling
17d38 20 66 75 6e 63 74 69 6f 6e 20 6d 69 67 68 74 20 function might
17d39 70 72 6f 76 69 64 65 20 73 7a 53 70 61 63 65 20 provide szSpace
17d3a 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a bytes of memory.
17d3b 2a 2a 20 73 70 61 63 65 20 61 74 20 70 53 70 61 ** space at pSpa
17d3c 63 65 2e 20 20 54 68 69 73 20 73 70 61 63 65 20 ce. This space
17d3d 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 68 can be used to h
17d3e 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 old the returned
17d3f 0a 2a 2a 20 56 44 62 65 50 61 72 73 65 64 52 65 .** VDbeParsedRe
17d40 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 20 69 cord structure i
17d41 66 20 69 74 20 69 73 20 6c 61 72 67 65 20 65 6e f it is large en
17d42 6f 75 67 68 2e 20 20 49 66 20 69 74 20 69 73 0a ough. If it is.
17d43 2a 2a 20 6e 6f 74 20 62 69 67 20 65 6e 6f 75 67 ** not big enoug
17d44 68 2c 20 73 70 61 63 65 20 69 73 20 6f 62 74 61 h, space is obta
17d45 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
17d46 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 3_malloc()..**.*
17d47 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 * The returned s
17d48 74 72 75 63 74 75 72 65 20 73 68 6f 75 6c 64 20 tructure should
17d49 62 65 20 63 6c 6f 73 65 64 20 62 79 20 61 20 63 be closed by a c
17d4a 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 all to.** sqlite
17d4b 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 3VdbeDeleteUnpac
17d4c 6b 65 64 52 65 63 6f 72 64 28 29 2e 0a 2a 2f 20 kedRecord()..*/
17d4d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
17d4e 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
17d4f 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 sqlite3VdbeRecor
17d50 64 55 6e 70 61 63 6b 28 0a 20 20 4b 65 79 49 6e dUnpack(. KeyIn
17d51 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 fo *pKeyInfo,
17d52 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e /* Information
17d53 20 61 62 6f 75 74 20 74 68 65 20 72 65 63 6f 72 about the recor
17d54 64 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 69 6e d format */. in
17d55 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 t nKey,
17d56 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
17d57 74 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72 the binary recor
17d58 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 d */. const voi
17d59 64 20 2a 70 4b 65 79 2c 20 20 20 20 20 20 2f 2a d *pKey, /*
17d5a 20 54 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f The binary reco
17d5b 72 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 rd */. Unpacked
17d5c 52 65 63 6f 72 64 20 2a 70 53 70 61 63 65 2c 2f Record *pSpace,/
17d5d 2a 20 53 70 61 63 65 20 61 76 61 69 6c 61 62 6c * Space availabl
17d5e 65 20 74 6f 20 68 6f 6c 64 20 72 65 73 75 6c 74 e to hold result
17d5f 69 6e 67 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 ing object */.
17d60 69 6e 74 20 73 7a 53 70 61 63 65 20 20 20 20 20 int szSpace
17d61 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
17d62 66 20 70 53 70 61 63 65 5b 5d 20 69 6e 20 62 79 f pSpace[] in by
17d63 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 tes */.){. cons
17d64 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
17d65 2a 61 4b 65 79 20 3d 20 28 63 6f 6e 73 74 20 75 *aKey = (const u
17d66 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 nsigned char *)p
17d67 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 Key;. UnpackedR
17d68 65 63 6f 72 64 20 2a 70 3b 0a 20 20 69 6e 74 20 ecord *p;. int
17d69 6e 42 79 74 65 2c 20 64 3b 0a 20 20 75 33 32 20 nByte, d;. u32
17d6a 69 64 78 3b 0a 20 20 75 31 36 20 75 3b 20 20 20 idx;. u16 u;
17d6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17d6c 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 Unsigned loop c
17d6d 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 33 32 20 ounter */. u32
17d6e 73 7a 48 64 72 3b 0a 20 20 4d 65 6d 20 2a 70 4d szHdr;. Mem *pM
17d6f 65 6d 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 em;. . assert(
17d70 20 73 69 7a 65 6f 66 28 4d 65 6d 29 3e 73 69 7a sizeof(Mem)>siz
17d71 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 6e 42 79 eof(*p) );. nBy
17d72 74 65 20 3d 20 73 69 7a 65 6f 66 28 4d 65 6d 29 te = sizeof(Mem)
17d73 2a 28 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 *(pKeyInfo->nFie
17d74 6c 64 2b 32 29 3b 0a 20 20 69 66 28 20 6e 42 79 ld+2);. if( nBy
17d75 74 65 3e 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 te>szSpace ){.
17d76 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d p = sqlite3DbM
17d77 61 6c 6c 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 allocRaw(pKeyInf
17d78 6f 2d 3e 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 o->db, nByte);.
17d79 20 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 if( p==0 ) re
17d7a 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 turn 0;. p->f
17d7b 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f lags = UNPACKED_
17d7c 4e 45 45 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 NEED_FREE | UNPA
17d7d 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f CKED_NEED_DESTRO
17d7e 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 Y;. }else{.
17d7f 70 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 20 20 p = pSpace;.
17d80 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 p->flags = UNPAC
17d81 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 KED_NEED_DESTROY
17d82 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 ;. }. p->pKeyI
17d83 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a nfo = pKeyInfo;.
17d84 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b p->nField = pK
17d85 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b eyInfo->nField +
17d86 20 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 1;. p->aMem =
17d87 70 4d 65 6d 20 3d 20 26 28 28 4d 65 6d 2a 29 70 pMem = &((Mem*)p
17d88 29 5b 31 5d 3b 0a 20 20 69 64 78 20 3d 20 67 65 )[1];. idx = ge
17d89 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 2c 20 tVarint32(aKey,
17d8a 73 7a 48 64 72 29 3b 0a 20 20 64 20 3d 20 73 7a szHdr);. d = sz
17d8b 48 64 72 3b 0a 20 20 75 20 3d 20 30 3b 0a 20 20 Hdr;. u = 0;.
17d8c 77 68 69 6c 65 28 20 69 64 78 3c 73 7a 48 64 72 while( idx<szHdr
17d8d 20 26 26 20 75 3c 70 2d 3e 6e 46 69 65 6c 64 20 && u<p->nField
17d8e 29 7b 0a 20 20 20 20 75 33 32 20 73 65 72 69 61 ){. u32 seria
17d8f 6c 5f 74 79 70 65 3b 0a 0a 20 20 20 20 69 64 78 l_type;.. idx
17d90 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 += getVarint32(
17d91 26 61 4b 65 79 5b 69 64 78 5d 2c 20 73 65 72 69 &aKey[idx], seri
17d92 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69 66 al_type);. if
17d93 28 20 64 3e 3d 6e 4b 65 79 20 26 26 20 73 71 6c ( d>=nKey && sql
17d94 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 ite3VdbeSerialTy
17d95 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 peLen(serial_typ
17d96 65 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 e)>0 ) break;.
17d97 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 70 4b pMem->enc = pK
17d98 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 20 eyInfo->enc;.
17d99 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b 65 79 pMem->db = pKey
17d9a 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 70 4d Info->db;. pM
17d9b 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 em->flags = 0;.
17d9c 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 pMem->zMalloc
17d9d 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d 20 73 = 0;. d += s
17d9e 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
17d9f 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 73 65 Get(&aKey[d], se
17da0 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 6d 29 rial_type, pMem)
17da1 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 ;. pMem++;.
17da2 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 73 73 u++;. }. ass
17da3 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e 66 6f ert( u<=pKeyInfo
17da4 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 3b 0a ->nField + 1 );.
17da5 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 75 3b p->nField = u;
17da6 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a . return (void*
17da7 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 )p;.}../*.** Thi
17da8 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 6f s routine destro
17da9 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 ys a UnpackedRec
17daa 6f 72 64 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 ord object.*/.SQ
17dab 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
17dac 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c d sqlite3VdbeDel
17dad 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
17dae 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 d(UnpackedRecord
17daf 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
17db0 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 . if( p->flag
17db1 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 s & UNPACKED_NEE
17db2 44 5f 44 45 53 54 52 4f 59 20 29 7b 0a 20 20 20 D_DESTROY ){.
17db3 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
17db4 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 Mem *pMem;.
17db5 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d 70 for(i=0, pMem=p
17db6 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 69 ->aMem; i<p->nFi
17db7 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b 2b eld; i++, pMem++
17db8 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
17db9 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a Mem->zMalloc ){.
17dba 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
17dbb 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
17dbc 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 7d pMem);. }
17dbd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
17dbe 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 if( p->flags
17dbf 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f & UNPACKED_NEED_
17dc0 46 52 45 45 20 29 7b 0a 20 20 20 20 20 20 73 71 FREE ){. sq
17dc1 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70 lite3DbFree(p->p
17dc2 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b KeyInfo->db, p);
17dc3 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
17dc4 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
17dc5 6e 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 74 n compares the t
17dc6 77 6f 20 74 61 62 6c 65 20 72 6f 77 73 20 6f 72 wo table rows or
17dc7 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 0a 2a index records.*
17dc8 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 7b * specified by {
17dc9 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 61 6e nKey1, pKey1} an
17dca 64 20 70 50 4b 65 79 32 2e 20 20 49 74 20 72 65 d pPKey2. It re
17dcb 74 75 72 6e 73 20 61 20 6e 65 67 61 74 69 76 65 turns a negative
17dcc 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 70 6f 73 , zero.** or pos
17dcd 69 74 69 76 65 20 69 6e 74 65 67 65 72 20 69 66 itive integer if
17dce 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74 68 key1 is less th
17dcf 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 an, equal to or
17dd0 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e .** greater than
17dd1 20 6b 65 79 32 2e 20 20 54 68 65 20 7b 6e 4b 65 key2. The {nKe
17dd2 79 31 2c 20 70 4b 65 79 31 7d 20 6b 65 79 20 6d y1, pKey1} key m
17dd3 75 73 74 20 62 65 20 61 20 62 6c 6f 62 0a 2a 2a ust be a blob.**
17dd4 20 63 72 65 61 74 65 64 20 62 79 20 74 68 20 4f created by th O
17dd5 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 P_MakeRecord opc
17dd6 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e ode of the VDBE.
17dd7 20 20 54 68 65 20 70 50 4b 65 79 32 0a 2a 2a 20 The pPKey2.**
17dd8 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 70 61 key must be a pa
17dd9 72 73 65 64 20 6b 65 79 20 73 75 63 68 20 61 73 rsed key such as
17dda 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
17ddb 2a 20 73 71 6c 69 74 65 33 56 64 62 65 50 61 72 * sqlite3VdbePar
17ddc 73 65 52 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 seRecord..**.**
17ddd 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 20 64 6f Key1 and Key2 do
17dde 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 63 6f 6e not have to con
17ddf 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20 6e 75 tain the same nu
17de0 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 2e 0a mber of fields..
17de1 2a 2a 20 54 68 65 20 6b 65 79 20 77 69 74 68 20 ** The key with
17de2 66 65 77 65 72 20 66 69 65 6c 64 73 20 69 73 20 fewer fields is
17de3 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61 72 65 73 usually compares
17de4 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 0a less than the .
17de5 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79 2e 20 20 ** longer key.
17de6 48 6f 77 65 76 65 72 20 69 66 20 74 68 65 20 55 However if the U
17de7 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 NPACKED_INCRKEY
17de8 66 6c 61 67 73 20 69 6e 20 70 50 4b 65 79 32 20 flags in pPKey2
17de9 69 73 20 73 65 74 0a 2a 2a 20 61 6e 64 20 74 68 is set.** and th
17dea 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65 e common prefixe
17deb 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68 65 s are equal, the
17dec 6e 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74 n key1 is less t
17ded 68 61 6e 20 6b 65 79 32 2e 0a 2a 2a 20 4f 72 20 han key2..** Or
17dee 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f if the UNPACKED_
17def 4d 41 54 43 48 5f 50 52 45 46 49 58 20 66 6c 61 MATCH_PREFIX fla
17df0 67 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 65 g is set and the
17df1 20 70 72 65 66 69 78 65 73 20 61 72 65 0a 2a 2a prefixes are.**
17df2 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 74 68 65 equal, then the
17df3 20 6b 65 79 73 20 61 72 65 20 63 6f 6e 73 69 64 keys are consid
17df4 65 72 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c ered to be equal
17df5 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 74 and.** the part
17df6 73 20 62 65 79 6f 6e 64 20 74 68 65 20 63 6f 6d s beyond the com
17df7 6d 6f 6e 20 70 72 65 66 69 78 20 61 72 65 20 69 mon prefix are i
17df8 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 gnored..**.** If
17df9 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 the UNPACKED_IG
17dfa 4e 4f 52 45 5f 52 4f 57 49 44 20 66 6c 61 67 20 NORE_ROWID flag
17dfb 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 is set, then the
17dfc 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a last byte of.**
17dfd 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20 70 the header of p
17dfe 4b 65 79 31 20 69 73 20 69 67 6e 6f 72 65 64 2e Key1 is ignored.
17dff 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 It is assumed
17e00 74 68 61 74 20 70 4b 65 79 31 20 69 73 0a 2a 2a that pKey1 is.**
17e01 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 2c 20 61 an index key, a
17e02 6e 64 20 74 68 75 73 20 65 6e 64 73 20 77 69 74 nd thus ends wit
17e03 68 20 61 20 72 6f 77 69 64 20 76 61 6c 75 65 2e h a rowid value.
17e04 20 20 54 68 65 20 6c 61 73 74 20 62 79 74 65 0a The last byte.
17e05 2a 2a 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 ** of the header
17e06 20 77 69 6c 6c 20 74 68 65 72 65 66 6f 72 65 20 will therefore
17e07 62 65 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 be the serial ty
17e08 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 3a pe of the rowid:
17e09 0a 2a 2a 20 6f 6e 65 20 6f 66 20 31 2c 20 32 2c .** one of 1, 2,
17e0a 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 38 2c 20 3, 4, 5, 6, 8,
17e0b 6f 72 20 39 20 2d 20 74 68 65 20 69 6e 74 65 67 or 9 - the integ
17e0c 65 72 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e er serial types.
17e0d 0a 2a 2a 20 54 68 65 20 73 65 72 69 61 6c 20 74 .** The serial t
17e0e 79 70 65 20 6f 66 20 74 68 65 20 66 69 6e 61 6c ype of the final
17e0f 20 72 6f 77 69 64 20 77 69 6c 6c 20 61 6c 77 61 rowid will alwa
17e10 79 73 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 ys be a single b
17e11 79 74 65 2e 0a 2a 2a 20 42 79 20 69 67 6e 6f 72 yte..** By ignor
17e12 69 6e 67 20 74 68 69 73 20 6c 61 73 74 20 62 79 ing this last by
17e13 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 te of the header
17e14 2c 20 77 65 20 66 6f 72 63 65 20 74 68 65 20 63 , we force the c
17e15 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 74 6f 20 omparison.** to
17e16 69 67 6e 6f 72 65 20 74 68 65 20 72 6f 77 69 64 ignore the rowid
17e17 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 6b at the end of k
17e18 65 79 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ey1..*/.SQLITE_P
17e19 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
17e1a 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 e3VdbeRecordComp
17e1b 61 72 65 28 0a 20 20 69 6e 74 20 6e 4b 65 79 31 are(. int nKey1
17e1c 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b , const void *pK
17e1d 65 79 31 2c 20 2f 2a 20 4c 65 66 74 20 6b 65 79 ey1, /* Left key
17e1e 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 */. UnpackedRe
17e1f 63 6f 72 64 20 2a 70 50 4b 65 79 32 20 20 20 20 cord *pPKey2
17e20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6b 65 79 /* Right key
17e21 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 64 31 3b */.){. int d1;
17e22 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
17e23 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b ffset into aKey[
17e24 5d 20 6f 66 20 6e 65 78 74 20 64 61 74 61 20 65 ] of next data e
17e25 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 lement */. u32
17e26 69 64 78 31 3b 20 20 20 20 20 20 20 20 20 20 2f idx1; /
17e27 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b * Offset into aK
17e28 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 68 65 61 ey[] of next hea
17e29 64 65 72 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 der element */.
17e2a 20 75 33 32 20 73 7a 48 64 72 31 3b 20 20 20 20 u32 szHdr1;
17e2b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
17e2c 20 62 79 74 65 73 20 69 6e 20 68 65 61 64 65 72 bytes in header
17e2d 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b */. int i = 0;
17e2e 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 . int nField;.
17e2f 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 63 int rc = 0;. c
17e30 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
17e31 61 72 20 2a 61 4b 65 79 31 20 3d 20 28 63 6f 6e ar *aKey1 = (con
17e32 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
17e33 20 2a 29 70 4b 65 79 31 3b 0a 20 20 4b 65 79 49 *)pKey1;. KeyI
17e34 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 nfo *pKeyInfo;.
17e35 20 4d 65 6d 20 6d 65 6d 31 3b 0a 0a 20 20 70 4b Mem mem1;.. pK
17e36 65 79 49 6e 66 6f 20 3d 20 70 50 4b 65 79 32 2d eyInfo = pPKey2-
17e37 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 6d 65 6d >pKeyInfo;. mem
17e38 31 2e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f 1.enc = pKeyInfo
17e39 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 31 2e 64 62 ->enc;. mem1.db
17e3a 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b = pKeyInfo->db;
17e3b 0a 20 20 6d 65 6d 31 2e 66 6c 61 67 73 20 3d 20 . mem1.flags =
17e3c 30 3b 0a 20 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 0;. mem1.zMallo
17e3d 63 20 3d 20 30 3b 0a 20 20 0a 20 20 69 64 78 31 c = 0;. . idx1
17e3e 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 61 = getVarint32(a
17e3f 4b 65 79 31 2c 20 73 7a 48 64 72 31 29 3b 0a 20 Key1, szHdr1);.
17e40 20 64 31 20 3d 20 73 7a 48 64 72 31 3b 0a 20 20 d1 = szHdr1;.
17e41 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 if( pPKey2->flag
17e42 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e s & UNPACKED_IGN
17e43 4f 52 45 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 ORE_ROWID ){.
17e44 20 73 7a 48 64 72 31 2d 2d 3b 0a 20 20 7d 0a 20 szHdr1--;. }.
17e45 20 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e nField = pKeyIn
17e46 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 77 68 fo->nField;. wh
17e47 69 6c 65 28 20 69 64 78 31 3c 73 7a 48 64 72 31 ile( idx1<szHdr1
17e48 20 26 26 20 69 3c 70 50 4b 65 79 32 2d 3e 6e 46 && i<pPKey2->nF
17e49 69 65 6c 64 20 29 7b 0a 20 20 20 20 75 33 32 20 ield ){. u32
17e4a 73 65 72 69 61 6c 5f 74 79 70 65 31 3b 0a 0a 20 serial_type1;..
17e4b 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73 /* Read the s
17e4c 65 72 69 61 6c 20 74 79 70 65 73 20 66 6f 72 20 erial types for
17e4d 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e 74 the next element
17e4e 20 69 6e 20 65 61 63 68 20 6b 65 79 2e 20 2a 2f in each key. */
17e4f 0a 20 20 20 20 69 64 78 31 20 2b 3d 20 67 65 74 . idx1 += get
17e50 56 61 72 69 6e 74 33 32 28 20 61 4b 65 79 31 2b Varint32( aKey1+
17e51 69 64 78 31 2c 20 73 65 72 69 61 6c 5f 74 79 70 idx1, serial_typ
17e52 65 31 20 29 3b 0a 20 20 20 20 69 66 28 20 64 31 e1 );. if( d1
17e53 3e 3d 6e 4b 65 79 31 20 26 26 20 73 71 6c 69 74 >=nKey1 && sqlit
17e54 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
17e55 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 31 Len(serial_type1
17e56 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 )>0 ) break;..
17e57 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68 65 /* Extract the
17e58 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 63 6f values to be co
17e59 6d 70 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 mpared.. */.
17e5a 20 20 20 64 31 20 2b 3d 20 73 71 6c 69 74 65 33 d1 += sqlite3
17e5b 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 VdbeSerialGet(&a
17e5c 4b 65 79 31 5b 64 31 5d 2c 20 73 65 72 69 61 6c Key1[d1], serial
17e5d 5f 74 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b 0a _type1, &mem1);.
17e5e 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 . /* Do the c
17e5f 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 20 20 2a 2f omparison. */
17e60 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
17e61 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 6d 65 6d 3MemCompare(&mem
17e62 31 2c 20 26 70 50 4b 65 79 32 2d 3e 61 4d 65 6d 1, &pPKey2->aMem
17e63 5b 69 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 [i],.
17e64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e65 69 3c 6e 46 69 65 6c 64 20 3f 20 70 4b 65 79 49 i<nField ? pKeyI
17e66 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3a 20 nfo->aColl[i] :
17e67 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 0);. if( rc!=
17e68 30 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 ){. break
17e69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 2b 2b 3b ;. }. i++;
17e6a 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 31 2e . }. if( mem1.
17e6b 7a 4d 61 6c 6c 6f 63 20 29 20 73 71 6c 69 74 65 zMalloc ) sqlite
17e6c 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
17e6d 26 6d 65 6d 31 29 3b 0a 0a 20 20 69 66 28 20 72 &mem1);.. if( r
17e6e 63 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72 c==0 ){. /* r
17e6f 63 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20 c==0 here means
17e70 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 that one of the
17e71 6b 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 keys ran out of
17e72 66 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a fields and. *
17e73 2a 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 * all the fields
17e74 20 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e up to that poin
17e75 74 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 t were equal. If
17e76 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e the UNPACKED_IN
17e77 43 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61 CRKEY. ** fla
17e78 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62 g is set, then b
17e79 72 65 61 6b 20 74 68 65 20 74 69 65 20 62 79 20 reak the tie by
17e7a 74 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73 treating key2 as
17e7b 20 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20 larger.. **
17e7c 49 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 If the UPACKED_P
17e7d 52 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 REFIX_MATCH flag
17e7e 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 is set, then ke
17e7f 79 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 ys with common p
17e80 72 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61 refixes. ** a
17e81 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f re considered to
17e82 20 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 be equal. Othe
17e83 72 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 rwise, the longe
17e84 72 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 r key is the .
17e85 20 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 ** larger. As
17e86 20 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 it happens, the
17e87 20 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 pPKey2 will alw
17e88 61 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 ays be the longe
17e89 72 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 r. ** if ther
17e8a 65 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 e is a differenc
17e8b 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 e.. */. if
17e8c 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 ( pPKey2->flags
17e8d 26 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b & UNPACKED_INCRK
17e8e 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d EY ){. rc =
17e8f 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 -1;. }else i
17e90 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 f( pPKey2->flags
17e91 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 & UNPACKED_PREF
17e92 49 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 IX_MATCH ){.
17e93 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 /* Leave rc==0
17e94 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 */. }else if
17e95 28 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b ( idx1<szHdr1 ){
17e96 0a 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 . rc = 1;.
17e97 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
17e98 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 pKeyInfo->aSort
17e99 4f 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 Order && i<pKeyI
17e9a 6e 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 nfo->nField.
17e9b 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70 4b && pK
17e9c 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 eyInfo->aSortOrd
17e9d 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 er[i] ){. rc
17e9e 3d 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 = -rc;. }.. re
17e9f 74 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a turn rc;.}. ../*
17ea0 0a 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 .** pCur points
17ea1 61 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 at an index entr
17ea2 79 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 y created using
17ea3 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 the OP_MakeRecor
17ea4 64 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61 d opcode..** Rea
17ea5 64 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 d the rowid (the
17ea6 20 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 last field in t
17ea7 68 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 he record) and s
17ea8 74 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 tore it in *rowi
17ea9 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c d..** Return SQL
17eaa 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 ITE_OK if everyt
17eab 68 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61 hing works, or a
17eac 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 n error code oth
17ead 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erwise..*/.SQLIT
17eae 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
17eaf 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 lite3VdbeIdxRowi
17eb0 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 d(BtCursor *pCur
17eb1 2c 20 69 36 34 20 2a 72 6f 77 69 64 29 7b 0a 20 , i64 *rowid){.
17eb2 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 i64 nCellKey =
17eb3 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 0;. int rc;. u
17eb4 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20 32 szHdr;
17eb5 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
17eb6 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 header */. u32
17eb7 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20 2f 2a typeRowid; /*
17eb8 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 Serial type of
17eb9 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 75 the rowid */. u
17eba 33 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20 20 20 32 lenRowid;
17ebb 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
17ebc 72 6f 77 69 64 20 2a 2f 0a 20 20 4d 65 6d 20 6d rowid */. Mem m
17ebd 2c 20 76 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 , v;.. sqlite3B
17ebe 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 treeKeySize(pCur
17ebf 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 , &nCellKey);.
17ec0 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 if( nCellKey<=0
17ec1 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
17ec2 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
17ec3 54 3b 0a 20 20 7d 0a 20 20 6d 2e 66 6c 61 67 73 T;. }. m.flags
17ec4 20 3d 20 30 3b 0a 20 20 6d 2e 64 62 20 3d 20 30 = 0;. m.db = 0
17ec5 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 ;. m.zMalloc =
17ec6 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 0;. rc = sqlite
17ec7 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 3VdbeMemFromBtre
17ec8 65 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 6c 6c e(pCur, 0, nCell
17ec9 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 Key, 1, &m);. i
17eca 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 f( rc ){. ret
17ecb 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 28 76 urn rc;. }. (v
17ecc 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32 28 oid)getVarint32(
17ecd 28 75 38 2a 29 6d 2e 7a 2c 20 73 7a 48 64 72 29 (u8*)m.z, szHdr)
17ece 3b 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72 ;. (void)getVar
17ecf 69 6e 74 33 32 28 28 75 38 2a 29 26 6d 2e 7a 5b int32((u8*)&m.z[
17ed0 73 7a 48 64 72 2d 31 5d 2c 20 74 79 70 65 52 6f szHdr-1], typeRo
17ed1 77 69 64 29 3b 0a 20 20 6c 65 6e 52 6f 77 69 64 wid);. lenRowid
17ed2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
17ed3 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 65 rialTypeLen(type
17ed4 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 Rowid);. sqlite
17ed5 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 3VdbeSerialGet((
17ed6 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e u8*)&m.z[m.n-len
17ed7 52 6f 77 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 Rowid], typeRowi
17ed8 64 2c 20 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 d, &v);. *rowid
17ed9 20 3d 20 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 = v.u.i;. sqli
17eda 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
17edb 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 e(&m);. return
17edc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
17edd 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 .** Compare the
17ede 6b 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78 key of the index
17edf 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 entry that curs
17ee0 6f 72 20 70 43 20 69 73 20 70 6f 69 6e 74 20 74 or pC is point t
17ee1 6f 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 o against.** the
17ee2 20 6b 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70 key string in p
17ee3 4b 65 79 20 28 6f 66 20 6c 65 6e 67 74 68 20 6e Key (of length n
17ee4 4b 65 79 29 2e 20 20 57 72 69 74 65 20 69 6e 74 Key). Write int
17ee5 6f 20 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72 o *pRes a number
17ee6 0a 2a 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61 .** that is nega
17ee7 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 tive, zero, or p
17ee8 6f 73 69 74 69 76 65 20 69 66 20 70 43 20 69 73 ositive if pC is
17ee9 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 less than, equa
17eea 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 l to,.** or grea
17eeb 74 65 72 20 74 68 61 6e 20 70 4b 65 79 2e 20 20 ter than pKey.
17eec 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
17eed 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a on success..**.
17eee 2a 2a 20 70 4b 65 79 20 69 73 20 65 69 74 68 65 ** pKey is eithe
17eef 72 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75 r created withou
17ef0 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20 t a rowid or is
17ef1 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61 truncated so tha
17ef2 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 t it.** omits th
17ef3 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 e rowid at the e
17ef4 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61 nd. The rowid a
17ef5 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
17ef6 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20 index entry.**
17ef7 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65 is ignored as we
17ef8 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73 ll. Hence, this
17ef9 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f routine only co
17efa 6d 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69 mpares the prefi
17efb 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b xes .** of the k
17efc 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65 eys prior to the
17efd 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f final rowid, no
17efe 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 t the entire key
17eff 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65 ..**.** pUnpacke
17f00 64 20 6d 61 79 20 62 65 20 61 6e 20 75 6e 70 61 d may be an unpa
17f01 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 cked version of
17f02 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 49 66 20 70 pKey,nKey. If p
17f03 55 6e 70 61 63 6b 65 64 20 69 73 0a 2a 2a 20 73 Unpacked is.** s
17f04 75 70 70 6c 69 65 64 20 69 74 20 69 73 20 75 73 upplied it is us
17f05 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 70 ed in place of p
17f06 4b 65 79 2c 6e 4b 65 79 2e 0a 2a 2f 0a 53 51 4c Key,nKey..*/.SQL
17f07 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
17f08 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 sqlite3VdbeIdxKe
17f09 79 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65 yCompare(. Vdbe
17f0a 43 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 Cursor *pC,
17f0b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
17f0c 75 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65 ursor to compare
17f0d 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e against */. Un
17f0e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 packedRecord *pU
17f0f 6e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70 npacked, /* Unp
17f10 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 acked version of
17f11 20 70 4b 65 79 20 61 6e 64 20 6e 4b 65 79 20 2a pKey and nKey *
17f12 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20 20 /. int *res
17f13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f14 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d /* Write the com
17f15 70 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20 68 parison result h
17f16 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20 ere */.){. i64
17f17 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 nCellKey = 0;.
17f18 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 73 int rc;. BtCurs
17f19 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70 or *pCur = pC->p
17f1a 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d 3b Cursor;. Mem m;
17f1b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 .. sqlite3Btree
17f1c 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e KeySize(pCur, &n
17f1d 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 69 66 28 20 CellKey);. if(
17f1e 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 29 7b 0a 20 nCellKey<=0 ){.
17f1f 20 20 20 2a 72 65 73 20 3d 20 30 3b 0a 20 20 20 *res = 0;.
17f20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
17f21 4b 3b 0a 20 20 7d 0a 20 20 6d 2e 64 62 20 3d 20 K;. }. m.db =
17f22 30 3b 0a 20 20 6d 2e 66 6c 61 67 73 20 3d 20 30 0;. m.flags = 0
17f23 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 ;. m.zMalloc =
17f24 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 0;. rc = sqlite
17f25 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 3VdbeMemFromBtre
17f26 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 e(pC->pCursor, 0
17f27 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 , nCellKey, 1, &
17f28 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a m);. if( rc ){.
17f29 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
17f2a 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55 6e }. assert( pUn
17f2b 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 20 packed->flags &
17f2c 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f UNPACKED_IGNORE_
17f2d 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 20 ROWID );. *res
17f2e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 = sqlite3VdbeRec
17f2f 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 ordCompare(m.n,
17f30 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 3b m.z, pUnpacked);
17f31 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
17f32 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 mRelease(&m);.
17f33 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
17f34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
17f35 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 routine sets the
17f36 20 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65 74 value to be ret
17f37 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 urned by subsequ
17f38 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 ent calls to.**
17f39 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 sqlite3_changes(
17f3a 29 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 ) on the databas
17f3b 65 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 0a e handle 'db'. .
17f3c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
17f3d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
17f3e 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c beSetChanges(sql
17f3f 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 43 ite3 *db, int nC
17f40 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 74 hange){. assert
17f41 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
17f42 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
17f43 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 );. db->nChange
17f44 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 = nChange;. db
17f45 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b ->nTotalChange +
17f46 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a = nChange;.}../*
17f47 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69 .** Set a flag i
17f48 6e 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 70 n the vdbe to up
17f49 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 20 date the change
17f4a 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20 counter when it
17f4b 69 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 is finalised.**
17f4c 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c or reset..*/.SQL
17f4d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
17f4e 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e sqlite3VdbeCoun
17f4f 74 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76 tChanges(Vdbe *v
17f50 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e ){. v->changeCn
17f51 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a tOn = 1;.}../*.*
17f52 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65 * Mark every pre
17f53 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pared statement
17f54 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
17f55 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 a database conne
17f56 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69 ction.** as expi
17f57 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 red..**.** An ex
17f58 70 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 pired statement
17f59 6d 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d means that recom
17f5a 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 pilation of the
17f5b 73 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 statement is.**
17f5c 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74 recommend. Stat
17f5d 65 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77 68 ements expire wh
17f5e 65 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e en things happen
17f5f 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72 that make their
17f60 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73 .** programs obs
17f61 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67 olete. Removing
17f62 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 user-defined fu
17f63 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 nctions or colla
17f64 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 ting.** sequence
17f65 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61 s, or changing a
17f66 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 n authorization
17f67 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 function are the
17f68 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69 types of.** thi
17f69 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72 ngs that make pr
17f6a 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
17f6b 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 s obsolete..*/.S
17f6c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
17f6d 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 id sqlite3Expire
17f6e 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e PreparedStatemen
17f6f 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b ts(sqlite3 *db){
17f70 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f . Vdbe *p;. fo
17f71 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b r(p = db->pVdbe;
17f72 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
17f73 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 . p->expired
17f74 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a = 1;. }.}../*.*
17f75 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 * Return the dat
17f76 61 62 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 abase associated
17f77 20 77 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a with the Vdbe..
17f78 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
17f79 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 E sqlite3 *sqlit
17f7a 65 33 56 64 62 65 44 62 28 56 64 62 65 20 2a 76 e3VdbeDb(Vdbe *v
17f7b 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64 ){. return v->d
17f7c 62 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a b;.}../*********
17f7d 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
17f7e 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eaux.c *********
17f7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17f81 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
17f82 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
17f83 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a vdbeapi.c *****
17f84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17f85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17f86 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
17f87 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 May 26.**.** Th
17f88 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
17f89 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
17f8a 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
17f8b 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
17f8c 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
17f8d 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
17f8e 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
17f8f 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
17f90 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
17f91 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
17f92 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
17f93 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
17f94 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
17f95 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
17f96 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
17f97 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
17f98 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
17f99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17f9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17f9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17f9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17f9d 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
17f9e 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
17f9f 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c code use to impl
17fa0 65 6d 65 6e 74 20 41 50 49 73 20 74 68 61 74 20 ement APIs that
17fa1 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a are part of the.
17fa2 2a 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 ** VDBE..**.** $
17fa3 49 64 3a 20 76 64 62 65 61 70 69 2e 63 2c 76 20 Id: vdbeapi.c,v
17fa4 31 2e 31 34 39 20 32 30 30 38 2f 31 31 2f 31 39 1.149 2008/11/19
17fa5 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65 6c 09:05:27 daniel
17fa6 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
17fa7 23 69 66 20 30 20 26 26 20 64 65 66 69 6e 65 64 #if 0 && defined
17fa8 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d (SQLITE_ENABLE_M
17fa9 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
17faa 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c )./*.** The foll
17fab 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
17fac 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 contains pointer
17fad 73 20 74 6f 20 74 68 65 20 65 6e 64 20 70 6f 69 s to the end poi
17fae 6e 74 73 20 6f 66 20 61 0a 2a 2a 20 64 6f 75 62 nts of a.** doub
17faf 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f ly-linked list o
17fb0 66 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 64 20 53 f all compiled S
17fb1 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 QL statements th
17fb2 61 74 20 6d 61 79 20 62 65 20 68 6f 6c 64 69 6e at may be holdin
17fb3 67 0a 2a 2a 20 62 75 66 66 65 72 73 20 65 6c 69 g.** buffers eli
17fb4 67 69 62 6c 65 20 66 6f 72 20 72 65 6c 65 61 73 gible for releas
17fb5 65 20 77 68 65 6e 20 74 68 65 20 73 71 6c 69 74 e when the sqlit
17fb6 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
17fb7 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 y() interface is
17fb8 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 20 41 63 63 .** invoked. Acc
17fb9 65 73 73 20 74 6f 20 74 68 69 73 20 6c 69 73 74 ess to this list
17fba 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 is protected by
17fbb 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 the SQLITE_MUTE
17fbc 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a X_STATIC_LRU2.**
17fbd 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 74 mutex..**.** St
17fbe 61 74 65 6d 65 6e 74 73 20 61 72 65 20 61 64 64 atements are add
17fbf 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 ed to the end of
17fc0 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e 20 this list when
17fc1 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 sqlite3_reset()
17fc2 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 20 54 68 is.** called. Th
17fc3 65 79 20 61 72 65 20 72 65 6d 6f 76 65 64 20 65 ey are removed e
17fc4 69 74 68 65 72 20 77 68 65 6e 20 73 71 6c 69 74 ither when sqlit
17fc5 65 33 5f 73 74 65 70 28 29 20 6f 72 20 73 71 6c e3_step() or sql
17fc6 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 0a ite3_finalize().
17fc7 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 57 68 ** is called. Wh
17fc8 65 6e 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 en statements ar
17fc9 65 20 61 64 64 65 64 20 74 6f 20 74 68 69 73 20 e added to this
17fca 6c 69 73 74 2c 20 74 68 65 20 61 73 73 6f 63 69 list, the associ
17fcb 61 74 65 64 20 0a 2a 2a 20 72 65 67 69 73 74 65 ated .** registe
17fcc 72 20 61 72 72 61 79 20 28 70 2d 3e 61 4d 65 6d r array (p->aMem
17fcd 5b 31 2e 2e 70 2d 3e 6e 4d 65 6d 5d 29 20 6d 61 [1..p->nMem]) ma
17fce 79 20 63 6f 6e 74 61 69 6e 20 64 79 6e 61 6d 69 y contain dynami
17fcf 63 20 62 75 66 66 65 72 73 20 74 68 61 74 0a 2a c buffers that.*
17fd0 2a 20 63 61 6e 20 62 65 20 66 72 65 65 64 20 75 * can be freed u
17fd1 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 sing sqlite3Vdbe
17fd2 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 29 2e ReleaseMemory().
17fd3 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 73 74 61 74 .**.** When stat
17fd4 65 6d 65 6e 74 73 20 61 72 65 20 61 64 64 65 64 ements are added
17fd5 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d or removed from
17fd6 20 74 68 69 73 20 6c 69 73 74 2c 20 74 68 65 20 this list, the
17fd7 6d 75 74 65 78 0a 2a 2a 20 61 73 73 6f 63 69 61 mutex.** associa
17fd8 74 65 64 20 77 69 74 68 20 74 68 65 20 56 64 62 ted with the Vdb
17fd9 65 20 62 65 69 6e 67 20 61 64 64 65 64 20 6f 72 e being added or
17fda 20 72 65 6d 6f 76 65 64 20 28 56 64 62 65 2e 64 removed (Vdbe.d
17fdb 62 2d 3e 6d 75 74 65 78 29 20 69 73 0a 2a 2a 20 b->mutex) is.**
17fdc 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 20 54 68 already held. Th
17fdd 65 20 4c 52 55 32 20 6d 75 74 65 78 20 69 73 20 e LRU2 mutex is
17fde 74 68 65 6e 20 6f 62 74 61 69 6e 65 64 2c 20 62 then obtained, b
17fdf 6c 6f 63 6b 69 6e 67 20 69 66 20 6e 65 63 65 73 locking if neces
17fe0 73 61 72 79 2c 0a 2a 2a 20 74 68 65 20 6c 69 6e sary,.** the lin
17fe1 6b 65 64 2d 6c 69 73 74 20 70 6f 69 6e 74 65 72 ked-list pointer
17fe2 73 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 61 6e s manipulated an
17fe3 64 20 74 68 65 20 4c 52 55 32 20 6d 75 74 65 78 d the LRU2 mutex
17fe4 20 72 65 6c 69 6e 71 75 69 73 68 65 64 2e 0a 2a relinquished..*
17fe5 2f 0a 73 74 72 75 63 74 20 53 74 61 74 65 6d 65 /.struct Stateme
17fe6 6e 74 4c 72 75 4c 69 73 74 20 7b 0a 20 20 56 64 ntLruList {. Vd
17fe7 62 65 20 2a 70 46 69 72 73 74 3b 0a 20 20 56 64 be *pFirst;. Vd
17fe8 62 65 20 2a 70 4c 61 73 74 3b 0a 7d 3b 0a 73 74 be *pLast;.};.st
17fe9 61 74 69 63 20 73 74 72 75 63 74 20 53 74 61 74 atic struct Stat
17fea 65 6d 65 6e 74 4c 72 75 4c 69 73 74 20 73 71 6c ementLruList sql
17feb 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 ite3LruStatement
17fec 73 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 s;../*.** Check
17fed 74 68 61 74 20 74 68 65 20 6c 69 73 74 20 6c 6f that the list lo
17fee 6f 6b 73 20 74 6f 20 62 65 20 69 6e 74 65 72 6e oks to be intern
17fef 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e ally consistent.
17ff0 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a This is used.**
17ff1 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 61 as part of an a
17ff2 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
17ff3 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a t as follows:.**
17ff4 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 73 74 .** assert( st
17ff5 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a mtLruCheck() );.
17ff6 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 */.#ifndef NDEBU
17ff7 47 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 6d G.static int stm
17ff8 74 4c 72 75 43 68 65 63 6b 28 29 7b 0a 20 20 56 tLruCheck(){. V
17ff9 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d dbe *p;. for(p=
17ffa 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d sqlite3LruStatem
17ffb 65 6e 74 73 2e 70 46 69 72 73 74 3b 20 70 3b 20 ents.pFirst; p;
17ffc 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a p=p->pLruNext){.
17ffd 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 70 4c assert(p->pL
17ffe 72 75 4e 65 78 74 20 7c 7c 20 70 3d 3d 73 71 6c ruNext || p==sql
17fff 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 ite3LruStatement
18000 73 2e 70 4c 61 73 74 29 3b 0a 20 20 20 20 61 73 s.pLast);. as
18001 73 65 72 74 28 21 70 2d 3e 70 4c 72 75 4e 65 78 sert(!p->pLruNex
18002 74 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e 65 78 74 t || p->pLruNext
18003 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 70 29 3b 0a ->pLruPrev==p);.
18004 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 70 4c assert(p->pL
18005 72 75 50 72 65 76 20 7c 7c 20 70 3d 3d 73 71 6c ruPrev || p==sql
18006 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 ite3LruStatement
18007 73 2e 70 46 69 72 73 74 29 3b 0a 20 20 20 20 61 s.pFirst);. a
18008 73 73 65 72 74 28 21 70 2d 3e 70 4c 72 75 50 72 ssert(!p->pLruPr
18009 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 50 72 65 ev || p->pLruPre
1800a 76 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d 70 29 3b v->pLruNext==p);
1800b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b . }. return 1;
1800c 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1800d 20 41 64 64 20 76 64 62 65 20 70 20 74 6f 20 74 Add vdbe p to t
1800e 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 74 he end of the st
1800f 61 74 65 6d 65 6e 74 20 6c 72 75 20 6c 69 73 74 atement lru list
18010 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 . It is assumed
18011 74 68 61 74 0a 2a 2a 20 70 20 69 73 20 6e 6f 74 that.** p is not
18012 20 61 6c 72 65 61 64 79 20 70 61 72 74 20 6f 66 already part of
18013 20 74 68 65 20 6c 69 73 74 20 77 68 65 6e 20 74 the list when t
18014 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 54 his is called. T
18015 68 65 20 6c 72 75 20 6c 69 73 74 0a 2a 2a 20 69 he lru list.** i
18016 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 s protected by t
18017 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f he SQLITE_MUTEX_
18018 53 54 41 54 49 43 5f 4c 52 55 20 6d 75 74 65 78 STATIC_LRU mutex
18019 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1801a 20 73 74 6d 74 4c 72 75 41 64 64 28 56 64 62 65 stmtLruAdd(Vdbe
1801b 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f *p){. sqlite3_
1801c 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 mutex_enter(sqli
1801d 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
1801e 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
1801f 43 5f 4c 52 55 32 29 29 3b 0a 0a 20 20 69 66 28 C_LRU2));.. if(
18020 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 p->pLruPrev ||
18021 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 73 p->pLruNext || s
18022 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 qlite3LruStateme
18023 6e 74 73 2e 70 46 69 72 73 74 3d 3d 70 20 29 7b nts.pFirst==p ){
18024 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
18025 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 ex_leave(sqlite3
18026 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
18027 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
18028 52 55 32 29 29 3b 0a 20 20 20 20 72 65 74 75 72 RU2));. retur
18029 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 n;. }.. assert
1802a 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 ( stmtLruCheck()
1802b 20 29 3b 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 );.. if( !sqli
1802c 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 te3LruStatements
1802d 2e 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20 61 .pFirst ){. a
1802e 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 4c ssert( !sqlite3L
1802f 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 ruStatements.pLa
18030 73 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 st );. sqlite
18031 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 3LruStatements.p
18032 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 73 First = p;. s
18033 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 qlite3LruStateme
18034 6e 74 73 2e 70 4c 61 73 74 20 3d 20 70 3b 0a 20 nts.pLast = p;.
18035 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
18036 72 74 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53 rt( !sqlite3LruS
18037 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 2d tatements.pLast-
18038 3e 70 4c 72 75 4e 65 78 74 20 29 3b 0a 20 20 20 >pLruNext );.
18039 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 73 p->pLruPrev = s
1803a 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 qlite3LruStateme
1803b 6e 74 73 2e 70 4c 61 73 74 3b 0a 20 20 20 20 73 nts.pLast;. s
1803c 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 qlite3LruStateme
1803d 6e 74 73 2e 70 4c 61 73 74 2d 3e 70 4c 72 75 4e nts.pLast->pLruN
1803e 65 78 74 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c ext = p;. sql
1803f 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 ite3LruStatement
18040 73 2e 70 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d s.pLast = p;. }
18041 0a 0a 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 .. assert( stmt
18042 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 0a 20 LruCheck() );..
18043 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
18044 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 eave(sqlite3Mute
18045 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
18046 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 TEX_STATIC_LRU2)
18047 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 );.}../*.** Assu
18048 6d 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f ming the SQLITE_
18049 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 MUTEX_STATIC_LRU
1804a 32 20 6d 75 74 65 78 74 20 69 73 20 61 6c 72 65 2 mutext is alre
1804b 61 64 79 20 68 65 6c 64 2c 20 72 65 6d 6f 76 65 ady held, remove
1804c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70 20 .** statement p
1804d 66 72 6f 6d 20 74 68 65 20 6c 65 61 73 74 2d 72 from the least-r
1804e 65 63 65 6e 74 6c 79 2d 75 73 65 64 20 73 74 61 ecently-used sta
1804f 74 65 6d 65 6e 74 20 6c 69 73 74 2e 20 49 66 20 tement list. If
18050 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e the .** statemen
18051 74 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 t is not current
18052 6c 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c ly part of the l
18053 69 73 74 2c 20 74 68 69 73 20 63 61 6c 6c 20 69 ist, this call i
18054 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 s a no-op..*/.st
18055 61 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c 72 atic void stmtLr
18056 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 56 uRemoveNomutex(V
18057 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 dbe *p){. if( p
18058 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d ->pLruPrev || p-
18059 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 3d 3d >pLruNext || p==
1805a 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d sqlite3LruStatem
1805b 65 6e 74 73 2e 70 46 69 72 73 74 20 29 7b 0a 20 ents.pFirst ){.
1805c 20 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c assert( stmtL
1805d 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 20 20 20 ruCheck() );.
1805e 20 69 66 28 20 70 2d 3e 70 4c 72 75 4e 65 78 74 if( p->pLruNext
1805f 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 72 ){. p->pLr
18060 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 uNext->pLruPrev
18061 3d 20 70 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 = p->pLruPrev;.
18062 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
18063 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d sqlite3LruStatem
18064 65 6e 74 73 2e 70 4c 61 73 74 20 3d 20 70 2d 3e ents.pLast = p->
18065 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a pLruPrev;. }.
18066 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 50 if( p->pLruP
18067 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e rev ){. p->
18068 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e 65 pLruPrev->pLruNe
18069 78 74 20 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 xt = p->pLruNext
1806a 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1806b 20 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 sqlite3LruSta
1806c 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 3d tements.pFirst =
1806d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 p->pLruNext;.
1806e 20 20 7d 0a 20 20 20 20 70 2d 3e 70 4c 72 75 4e }. p->pLruN
1806f 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e ext = 0;. p->
18070 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 pLruPrev = 0;.
18071 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 assert( stmtLr
18072 75 43 68 65 63 6b 28 29 20 29 3b 0a 20 20 7d 0a uCheck() );. }.
18073 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e }../*.** Assumin
18074 67 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 g the SQLITE_MUT
18075 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 6d EX_STATIC_LRU2 m
18076 75 74 65 78 74 20 69 73 20 6e 6f 74 20 68 65 6c utext is not hel
18077 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20 73 74 61 d, remove.** sta
18078 74 65 6d 65 6e 74 20 70 20 66 72 6f 6d 20 74 68 tement p from th
18079 65 20 6c 65 61 73 74 2d 72 65 63 65 6e 74 6c 79 e least-recently
1807a 2d 75 73 65 64 20 73 74 61 74 65 6d 65 6e 74 20 -used statement
1807b 6c 69 73 74 2e 20 49 66 20 74 68 65 20 0a 2a 2a list. If the .**
1807c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f statement is no
1807d 74 20 63 75 72 72 65 6e 74 6c 79 20 70 61 72 74 t currently part
1807e 20 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68 of the list, th
1807f 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d is call is a no-
18080 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f op..*/.static vo
18081 69 64 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 id stmtLruRemove
18082 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c (Vdbe *p){. sql
18083 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
18084 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
18085 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
18086 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20 STATIC_LRU2));.
18087 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 4e 6f stmtLruRemoveNo
18088 6d 75 74 65 78 28 70 29 3b 0a 20 20 73 71 6c 69 mutex(p);. sqli
18089 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1808a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
1808b 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
1808c 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a TATIC_LRU2));.}.
1808d 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 72 65 ./*.** Try to re
1808e 6c 65 61 73 65 20 6e 20 62 79 74 65 73 20 6f 66 lease n bytes of
1808f 20 6d 65 6d 6f 72 79 20 62 79 20 66 72 65 65 69 memory by freei
18090 6e 67 20 62 75 66 66 65 72 73 20 61 73 73 6f 63 ng buffers assoc
18091 69 61 74 65 64 20 0a 2a 2a 20 77 69 74 68 20 74 iated .** with t
18092 68 65 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 he memory regist
18093 65 72 73 20 6f 66 20 63 75 72 72 65 6e 74 6c 79 ers of currently
18094 20 75 6e 75 73 65 64 20 76 64 62 65 73 2e 0a 2a unused vdbes..*
18095 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18096 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
18097 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e ReleaseMemory(in
18098 74 20 6e 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b t n){. Vdbe *p;
18099 0a 20 20 56 64 62 65 20 2a 70 4e 65 78 74 3b 0a . Vdbe *pNext;.
1809a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b int nFree = 0;
1809b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .. sqlite3_mute
1809c 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d x_enter(sqlite3M
1809d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
1809e 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
1809f 55 32 29 29 3b 0a 20 20 66 6f 72 28 70 3d 73 71 U2));. for(p=sq
180a0 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e lite3LruStatemen
180a1 74 73 2e 70 46 69 72 73 74 3b 20 70 20 26 26 20 ts.pFirst; p &&
180a2 6e 46 72 65 65 3c 6e 3b 20 70 3d 70 4e 65 78 74 nFree<n; p=pNext
180a3 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 ){. pNext = p
180a4 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 0a 20 20 20 ->pLruNext;..
180a5 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 73 74 61 /* For each sta
180a6 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 6e tement handle in
180a7 20 74 68 65 20 6c 72 75 20 6c 69 73 74 2c 20 61 the lru list, a
180a8 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e ttempt to obtain
180a9 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 73 73 6f the. ** asso
180aa 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 ciated database
180ab 6d 75 74 65 78 2e 20 49 66 20 69 74 20 63 61 6e mutex. If it can
180ac 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c not be obtained,
180ad 20 63 6f 6e 74 69 6e 75 65 0a 20 20 20 20 2a 2a continue. **
180ae 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 74 61 to the next sta
180af 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 20 49 tement handle. I
180b0 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
180b1 65 20 74 6f 20 62 6c 6f 63 6b 20 6f 6e 0a 20 20 e to block on.
180b2 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 ** the databas
180b3 65 20 6d 75 74 65 78 20 2d 20 74 68 61 74 20 63 e mutex - that c
180b4 6f 75 6c 64 20 63 61 75 73 65 20 64 65 61 64 6c ould cause deadl
180b5 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ock.. */.
180b6 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 if( SQLITE_OK==s
180b7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
180b8 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
180b9 7b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d {. nFree +=
180ba 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 sqlite3VdbeRele
180bb 61 73 65 42 75 66 66 65 72 73 28 70 29 3b 0a 20 aseBuffers(p);.
180bc 20 20 20 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f stmtLruRemo
180bd 76 65 4e 6f 6d 75 74 65 78 28 70 29 3b 0a 20 20 veNomutex(p);.
180be 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
180bf 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d x_leave(p->db->m
180c0 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d utex);. }. }
180c1 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
180c2 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 _leave(sqlite3Mu
180c3 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
180c4 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 MUTEX_STATIC_LRU
180c5 32 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6e 2));.. return n
180c6 46 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 Free;.}../*.** C
180c7 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 all sqlite3Repre
180c8 70 61 72 65 28 29 20 6f 6e 20 74 68 65 20 73 74 pare() on the st
180c9 61 74 65 6d 65 6e 74 2e 20 52 65 6d 6f 76 65 20 atement. Remove
180ca 69 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6c it from the.** l
180cb 72 75 20 6c 69 73 74 20 62 65 66 6f 72 65 20 64 ru list before d
180cc 6f 69 6e 67 20 73 6f 2c 20 61 73 20 52 65 70 72 oing so, as Repr
180cd 65 70 61 72 65 28 29 20 77 69 6c 6c 20 66 72 65 epare() will fre
180ce 65 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 6d 65 6d e all the.** mem
180cf 6f 72 79 20 72 65 67 69 73 74 65 72 20 62 75 66 ory register buf
180d0 66 65 72 73 20 61 6e 79 77 61 79 2e 0a 2a 2f 0a fers anyway..*/.
180d1 69 6e 74 20 76 64 62 65 52 65 70 72 65 70 61 72 int vdbeReprepar
180d2 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 74 e(Vdbe *p){. st
180d3 6d 74 4c 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a mtLruRemove(p);.
180d4 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
180d5 52 65 70 72 65 70 61 72 65 28 70 29 3b 0a 7d 0a Reprepare(p);.}.
180d6 0a 23 65 6c 73 65 20 20 20 20 20 20 20 2f 2a 20 .#else /*
180d7 21 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d !SQLITE_ENABLE_M
180d8 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
180d9 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 73 74 */. #define st
180da 6d 74 4c 72 75 52 65 6d 6f 76 65 28 78 29 0a 20 mtLruRemove(x).
180db 20 23 64 65 66 69 6e 65 20 73 74 6d 74 4c 72 75 #define stmtLru
180dc 41 64 64 28 78 29 0a 20 20 23 64 65 66 69 6e 65 Add(x). #define
180dd 20 76 64 62 65 52 65 70 72 65 70 61 72 65 28 78 vdbeReprepare(x
180de 29 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 ) sqlite3Reprepa
180df 72 65 28 78 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 re(x).#endif...#
180e0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
180e1 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a IT_DEPRECATED./*
180e2 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
180e3 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66 20 74 68 (non-zero) of th
180e4 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 70 70 e statement supp
180e5 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d lied as an argum
180e6 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 ent needs.** to
180e7 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 2e 20 20 be recompiled.
180e8 41 20 73 74 61 74 65 6d 65 6e 74 20 6e 65 65 64 A statement need
180e9 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c s to be recompil
180ea 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a ed whenever the.
180eb 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 65 6e 76 ** execution env
180ec 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e 67 65 73 ironment changes
180ed 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 20 77 in a way that w
180ee 6f 75 6c 64 20 61 6c 74 65 72 20 74 68 65 20 70 ould alter the p
180ef 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61 74 20 73 rogram.** that s
180f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
180f1 20 67 65 6e 65 72 61 74 65 73 2e 20 20 46 6f 72 generates. For
180f2 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 6e 65 77 example, if new
180f3 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a functions or.**
180f4 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
180f5 6e 63 65 73 20 61 72 65 20 72 65 67 69 73 74 65 nces are registe
180f6 72 65 64 20 6f 72 20 69 66 20 61 6e 20 61 75 74 red or if an aut
180f7 68 6f 72 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e horizer function
180f8 20 69 73 0a 2a 2a 20 61 64 64 65 64 20 6f 72 20 is.** added or
180f9 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 changed..*/.SQLI
180fa 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
180fb 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74 e3_expired(sqlit
180fc 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
180fd 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
180fe 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 be*)pStmt;. ret
180ff 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 urn p==0 || p->e
18100 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 xpired;.}.#endif
18101 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
18102 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 64 65 owing routine de
18103 73 74 72 6f 79 73 20 61 20 76 69 72 74 75 61 6c stroys a virtual
18104 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 69 73 machine that is
18105 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 created by.** t
18106 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 he sqlite3_compi
18107 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 54 68 le() routine. Th
18108 65 20 69 6e 74 65 67 65 72 20 72 65 74 75 72 6e e integer return
18109 65 64 20 69 73 20 61 6e 20 53 51 4c 49 54 45 5f ed is an SQLITE_
1810a 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 61 69 6c .** success/fail
1810b 75 72 65 20 63 6f 64 65 20 74 68 61 74 20 64 65 ure code that de
1810c 73 63 72 69 62 65 73 20 74 68 65 20 72 65 73 75 scribes the resu
1810d 6c 74 20 6f 66 20 65 78 65 63 75 74 69 6e 67 20 lt of executing
1810e 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d the virtual.** m
1810f 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 achine..**.** Th
18110 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
18111 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 the error code a
18112 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e nd string return
18113 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 ed by.** sqlite3
18114 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 _errcode(), sqli
18115 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 te3_errmsg() and
18116 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 sqlite3_errmsg1
18117 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 6()..*/.SQLITE_A
18118 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 PI int sqlite3_f
18119 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f inalize(sqlite3_
1811a 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 stmt *pStmt){.
1811b 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53 int rc;. if( pS
1811c 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 tmt==0 ){. rc
1811d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1811e 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 20 }else{. Vdbe
1811f 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *v = (Vdbe*)pStm
18120 74 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 t;.#if SQLITE_TH
18121 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c READSAFE. sql
18122 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
18123 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 x = v->db->mutex
18124 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c ;.#endif. sql
18125 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
18126 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 74 6d (mutex);. stm
18127 74 4c 72 75 52 65 6d 6f 76 65 28 76 29 3b 0a 20 tLruRemove(v);.
18128 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
18129 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a dbeFinalize(v);.
1812a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1812b 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a x_leave(mutex);.
1812c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1812d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e .}../*.** Termin
1812e 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ate the current
1812f 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 61 6e 20 execution of an
18130 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 6e SQL statement an
18131 64 20 72 65 73 65 74 20 69 74 0a 2a 2a 20 62 61 d reset it.** ba
18132 63 6b 20 74 6f 20 69 74 73 20 73 74 61 72 74 69 ck to its starti
18133 6e 67 20 73 74 61 74 65 20 73 6f 20 74 68 61 74 ng state so that
18134 20 69 74 20 63 61 6e 20 62 65 20 72 65 75 73 65 it can be reuse
18135 64 2e 20 41 20 73 75 63 63 65 73 73 20 63 6f 64 d. A success cod
18136 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 70 72 e from.** the pr
18137 69 6f 72 20 65 78 65 63 75 74 69 6f 6e 20 69 73 ior execution is
18138 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
18139 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 This routine se
1813a 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 ts the error cod
1813b 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74 e and string ret
1813c 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 urned by.** sqli
1813d 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 te3_errcode(), s
1813e 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 qlite3_errmsg()
1813f 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d and sqlite3_errm
18140 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 sg16()..*/.SQLIT
18141 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
18142 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 3_reset(sqlite3_
18143 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 stmt *pStmt){.
18144 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53 int rc;. if( pS
18145 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 tmt==0 ){. rc
18146 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
18147 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 20 }else{. Vdbe
18148 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *v = (Vdbe*)pStm
18149 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d t;. sqlite3_m
1814a 75 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 utex_enter(v->db
1814b 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 ->mutex);. rc
1814c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1814d 73 65 74 28 76 29 3b 0a 20 20 20 20 73 74 6d 74 set(v);. stmt
1814e 4c 72 75 41 64 64 28 76 29 3b 0a 20 20 20 20 73 LruAdd(v);. s
1814f 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 qlite3VdbeMakeRe
18150 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c 20 30 2c ady(v, -1, 0, 0,
18151 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0);. assert(
18152 20 28 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 (rc & (v->db->e
18153 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a rrMask))==rc );.
18154 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
18155 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d x_leave(v->db->m
18156 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 utex);. }. ret
18157 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
18158 20 53 65 74 20 61 6c 6c 20 74 68 65 20 70 61 72 Set all the par
18159 61 6d 65 74 65 72 73 20 69 6e 20 74 68 65 20 63 ameters in the c
1815a 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 ompiled SQL stat
1815b 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a ement to NULL..*
1815c 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1815d 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 sqlite3_clear_b
1815e 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f indings(sqlite3_
1815f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 stmt *pStmt){.
18160 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 int i;. int rc
18161 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 56 = SQLITE_OK;. V
18162 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 dbe *p = (Vdbe*)
18163 70 53 74 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54 pStmt;.#if SQLIT
18164 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 E_THREADSAFE. s
18165 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
18166 74 65 78 20 3d 20 28 28 56 64 62 65 2a 29 70 53 tex = ((Vdbe*)pS
18167 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b tmt)->db->mutex;
18168 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 .#endif. sqlite
18169 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 3_mutex_enter(mu
1816a 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b tex);. for(i=0;
1816b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 i<p->nVar; i++)
1816c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1816d 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 70 2d 3e eMemRelease(&p->
1816e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 20 70 2d aVar[i]);. p-
1816f 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67 73 20 3d >aVar[i].flags =
18170 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 MEM_Null;. }.
18171 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
18172 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 eave(mutex);. r
18173 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
18174 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 *********** sqli
18176 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a te3_value_ ****
18177 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18178 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
18179 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
1817a 74 69 6e 65 73 20 65 78 74 72 61 63 74 20 69 6e tines extract in
1817b 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 formation from a
1817c 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f Mem or sqlite3_
1817d 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75 63 74 75 value.** structu
1817e 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 re..*/.SQLITE_AP
1817f 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
18180 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
18181 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
18182 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 pVal){. Mem *p
18183 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 = (Mem*)pVal;.
18184 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 if( p->flags & (
18185 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 MEM_Blob|MEM_Str
18186 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
18187 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
18188 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 b(p);. p->fla
18189 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a gs &= ~MEM_Str;.
1818a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 p->flags |=
1818b 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 MEM_Blob;. re
1818c 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c turn p->z;. }el
1818d 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 se{. return s
1818e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1818f 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 t(pVal);. }.}.S
18190 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
18191 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
18192 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 s(sqlite3_value
18193 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e *pVal){. return
18194 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 sqlite3ValueByt
18195 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f es(pVal, SQLITE_
18196 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f UTF8);.}.SQLITE_
18197 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
18198 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 value_bytes16(sq
18199 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1819a 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c l){. return sql
1819b 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 ite3ValueBytes(p
1819c 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 Val, SQLITE_UTF1
1819d 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 6NATIVE);.}.SQLI
1819e 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 TE_API double sq
1819f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 lite3_value_doub
181a0 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le(sqlite3_value
181a1 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 *pVal){. retur
181a2 6e 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 n sqlite3VdbeRea
181a3 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 lValue((Mem*)pVa
181a4 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 l);.}.SQLITE_API
181a5 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
181a6 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 ue_int(sqlite3_v
181a7 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 alue *pVal){. r
181a8 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 eturn sqlite3Vdb
181a9 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 eIntValue((Mem*)
181aa 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f pVal);.}.SQLITE_
181ab 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 API sqlite_int64
181ac 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 sqlite3_value_i
181ad 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c nt64(sqlite3_val
181ae 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue *pVal){. ret
181af 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 49 urn sqlite3VdbeI
181b0 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 ntValue((Mem*)pV
181b1 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 al);.}.SQLITE_AP
181b2 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
181b3 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 char *sqlite3_v
181b4 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 alue_text(sqlite
181b5 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 3_value *pVal){.
181b6 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 return (const
181b7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
181b8 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
181b9 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 (pVal, SQLITE_UT
181ba 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 F8);.}.#ifndef S
181bb 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
181bc 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
181bd 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
181be 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c value_text16(sql
181bf 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c ite3_value* pVal
181c0 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
181c1 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 te3ValueText(pVa
181c2 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e l, SQLITE_UTF16N
181c3 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 ATIVE);.}.SQLITE
181c4 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
181c5 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 *sqlite3_value_t
181c6 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f ext16be(sqlite3_
181c7 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 value *pVal){.
181c8 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 return sqlite3Va
181c9 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 lueText(pVal, SQ
181ca 4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b 0a 7d LITE_UTF16BE);.}
181cb 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
181cc 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
181cd 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 value_text16le(s
181ce 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
181cf 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 al){. return sq
181d0 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 lite3ValueText(p
181d1 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 Val, SQLITE_UTF1
181d2 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 6LE);.}.#endif /
181d3 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
181d4 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 F16 */.SQLITE_AP
181d5 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
181d6 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33 lue_type(sqlite3
181d7 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 _value* pVal){.
181d8 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 74 79 return pVal->ty
181d9 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a pe;.}../********
181da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
181db 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 **** sqlite3_res
181dc 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ult_ **********
181dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
181de 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c *****.** The fol
181df 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 lowing routines
181e0 61 72 65 20 75 73 65 64 20 62 79 20 75 73 65 72 are used by user
181e1 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
181e2 6e 73 20 74 6f 20 73 70 65 63 69 66 79 0a 2a 2a ns to specify.**
181e3 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 the function re
181e4 73 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f sult..*/.SQLITE_
181e5 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
181e6 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 _result_blob(.
181e7 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
181e8 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 *pCtx, . const
181e9 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 void *z, . int
181ea 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 n, . void (*xDe
181eb 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 l)(void *).){.
181ec 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a assert( n>=0 );.
181ed 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
181ee 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
181ef 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
181f0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
181f1 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d MemSetStr(&pCtx-
181f2 3e 73 2c 20 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 >s, z, n, 0, xDe
181f3 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 l);.}.SQLITE_API
181f4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
181f5 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 sult_double(sqli
181f6 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
181f7 78 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b x, double rVal){
181f8 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
181f9 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
181fa 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
181fb 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
181fc 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 eMemSetDouble(&p
181fd 43 74 78 2d 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d Ctx->s, rVal);.}
181fe 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
181ff 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
18200 65 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f error(sqlite3_co
18201 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e ntext *pCtx, con
18202 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 st char *z, int
18203 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 n){. assert( sq
18204 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
18205 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
18206 65 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 ex) );. pCtx->i
18207 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f sError = SQLITE_
18208 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 ERROR;. sqlite3
18209 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 VdbeMemSetStr(&p
1820a 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 Ctx->s, z, n, SQ
1820b 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
1820c 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a E_TRANSIENT);.}.
1820d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1820e 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
1820f 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
18210 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
18211 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
18212 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 76 6f *pCtx, const vo
18213 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 id *z, int n){.
18214 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
18215 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
18216 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
18217 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f ;. pCtx->isErro
18218 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 r = SQLITE_ERROR
18219 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1821a 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e emSetStr(&pCtx->
1821b 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f s, z, n, SQLITE_
1821c 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c UTF16NATIVE, SQL
1821d 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
1821e 7d 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f }.#endif.SQLITE_
1821f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
18220 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 _result_int(sqli
18221 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
18222 78 2c 20 69 6e 74 20 69 56 61 6c 29 7b 0a 20 20 x, int iVal){.
18223 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
18224 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
18225 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
18226 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
18227 6d 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d mSetInt64(&pCtx-
18228 3e 73 2c 20 28 69 36 34 29 69 56 61 6c 29 3b 0a >s, (i64)iVal);.
18229 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 }.SQLITE_API voi
1822a 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1822b 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63 _int64(sqlite3_c
1822c 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 36 ontext *pCtx, i6
1822d 34 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 4 iVal){. asser
1822e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1822f 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
18230 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
18231 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 lite3VdbeMemSetI
18232 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 69 nt64(&pCtx->s, i
18233 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 Val);.}.SQLITE_A
18234 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
18235 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 result_null(sqli
18236 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
18237 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 x){. assert( sq
18238 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
18239 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1823a 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
1823b 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 VdbeMemSetNull(&
1823c 70 43 74 78 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 pCtx->s);.}.SQLI
1823d 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
1823e 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
1823f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
18240 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e xt *pCtx, . con
18241 73 74 20 63 68 61 72 20 2a 7a 2c 20 0a 20 20 69 st char *z, . i
18242 6e 74 20 6e 2c 0a 20 20 76 6f 69 64 20 28 2a 78 nt n,. void (*x
18243 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a Del)(void *).){.
18244 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
18245 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
18246 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
18247 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
18248 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d MemSetStr(&pCtx-
18249 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 >s, z, n, SQLITE
1824a 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a _UTF8, xDel);.}.
1824b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1824c 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
1824d 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1824e 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 3_result_text16(
1824f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
18250 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e xt *pCtx, . con
18251 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 st void *z, . i
18252 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a nt n, . void (*
18253 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b xDel)(void *).){
18254 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
18255 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
18256 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
18257 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
18258 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 eMemSetStr(&pCtx
18259 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 ->s, z, n, SQLIT
1825a 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 78 E_UTF16NATIVE, x
1825b 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 Del);.}.SQLITE_A
1825c 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
1825d 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 result_text16be(
1825e 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
1825f 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e xt *pCtx, . con
18260 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 st void *z, . i
18261 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a nt n, . void (*
18262 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b xDel)(void *).){
18263 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
18264 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
18265 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
18266 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
18267 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 eMemSetStr(&pCtx
18268 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 ->s, z, n, SQLIT
18269 45 5f 55 54 46 31 36 42 45 2c 20 78 44 65 6c 29 E_UTF16BE, xDel)
1826a 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ;.}.SQLITE_API v
1826b 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1826c 6c 74 5f 74 65 78 74 31 36 6c 65 28 0a 20 20 73 lt_text16le(. s
1826d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1826e 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 pCtx, . const v
1826f 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e oid *z, . int n
18270 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c , . void (*xDel
18271 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 )(void *).){. a
18272 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
18273 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
18274 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
18275 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
18276 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c SetStr(&pCtx->s,
18277 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 z, n, SQLITE_UT
18278 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a F16LE, xDel);.}.
18279 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1827a 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 _OMIT_UTF16 */.S
1827b 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1827c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 qlite3_result_va
1827d 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 lue(sqlite3_cont
1827e 65 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69 74 ext *pCtx, sqlit
1827f 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 e3_value *pValue
18280 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
18281 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
18282 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
18283 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 x) );. sqlite3V
18284 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74 78 dbeMemCopy(&pCtx
18285 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a ->s, pValue);.}.
18286 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
18287 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a sqlite3_result_z
18288 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f eroblob(sqlite3_
18289 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 context *pCtx, i
1828a 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 nt n){. assert(
1828b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1828c 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1828d 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 mutex) );. sqli
1828e 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 te3VdbeMemSetZer
1828f 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20 oBlob(&pCtx->s,
18290 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 n);.}.SQLITE_API
18291 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
18292 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 sult_error_code(
18293 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
18294 2a 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43 6f *pCtx, int errCo
18295 64 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45 de){. pCtx->isE
18296 72 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a rror = errCode;.
18297 7d 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 }../* Force an S
18298 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 QLITE_TOOBIG err
18299 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 or. */.SQLITE_AP
1829a 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1829b 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 esult_error_toob
1829c 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ig(sqlite3_conte
1829d 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 xt *pCtx){. ass
1829e 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1829f 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
182a0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
182a1 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 pCtx->isError =
182a2 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 SQLITE_TOOBIG;.
182a3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
182a4 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 etStr(&pCtx->s,
182a5 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 "string or blob
182a6 74 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 too big", -1, .
182a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
182a8 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 SQLITE_UTF
182a9 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 8, SQLITE_STATIC
182aa 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 );.}../* An SQLI
182ab 54 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 TE_NOMEM error.
182ac 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
182ad 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
182ae 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 t_error_nomem(sq
182af 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
182b0 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Ctx){. assert(
182b1 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
182b2 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
182b3 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
182b4 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c e3VdbeMemSetNull
182b5 28 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 (&pCtx->s);. pC
182b6 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 tx->isError = SQ
182b7 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 LITE_NOMEM;. pC
182b8 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 tx->s.db->malloc
182b9 46 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f Failed = 1;.}../
182ba 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 *.** Execute the
182bb 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 statement pStmt
182bc 2c 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 61 , either until a
182bd 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 row of data is
182be 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 ready, the.** st
182bf 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c atement is compl
182c0 65 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 6f etely executed o
182c1 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 r an error occur
182c2 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
182c3 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 utine implements
182c4 20 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 the bulk of the
182c5 20 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68 logic behind th
182c6 65 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a e sqlite_step().
182c7 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c ** API. The onl
182c8 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 y thing omitted
182c9 69 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 is the automatic
182ca 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 recompile if a
182cb 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 .** schema chang
182cc 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 e has occurred.
182cd 20 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 20 That detail is
182ce 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a handled by the.*
182cf 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 5f * outer sqlite3_
182d0 73 74 65 70 28 29 20 77 72 61 70 70 65 72 20 70 step() wrapper p
182d1 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 rocedure..*/.sta
182d2 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 53 tic int sqlite3S
182d3 74 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 tep(Vdbe *p){.
182d4 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 sqlite3 *db;. i
182d5 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
182d6 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 (p);. if( p->ma
182d7 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic!=VDBE_MAGIC_
182d8 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 RUN ){. retur
182d9 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
182da 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 . }.. /* Asser
182db 74 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 t that malloc()
182dc 68 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a has not failed *
182dd 2f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a /. db = p->db;.
182de 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
182df 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 Failed ){. re
182e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
182e1 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d M;. }.. if( p-
182e2 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 >pc<=0 && p->exp
182e3 69 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28 20 ired ){. if(
182e4 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b p->rc==SQLITE_OK
182e5 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 ){. p->rc
182e6 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b = SQLITE_SCHEMA;
182e7 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
182e8 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
182e9 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 goto end_of_st
182ea 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 ep;. }. if( sq
182eb 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
182ec 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d ) ){. p->rc =
182ed 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
182ee 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
182ef 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 E_MISUSE;. }.
182f0 69 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 if( p->pc<0 ){.
182f1 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 /* If there a
182f2 72 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 re no other stat
182f3 65 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 ements currently
182f4 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 running, then.
182f5 20 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20 ** reset the
182f6 69 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20 interrupt flag.
182f7 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 This prevents a
182f8 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
182f9 5f 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a _interrupt. *
182fa 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 * from interrupt
182fb 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 ing a statement
182fc 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 that has not yet
182fd 20 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f started.. */
182fe 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 . if( db->act
182ff 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b iveVdbeCnt==0 ){
18300 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 . db->u1.is
18301 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b Interrupted = 0;
18302 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 . }..#ifndef
18303 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
18304 45 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 E. if( db->xP
18305 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 rofile && !db->i
18306 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 nit.busy ){.
18307 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 double rNow;.
18308 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 sqlite3OsCu
18309 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 rrentTime(db->pV
1830a 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 fs, &rNow);.
1830b 20 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d p->startTime =
1830c 20 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e (rNow - (int)rN
1830d 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a ow)*3600.0*24.0*
1830e 31 30 30 30 30 30 30 30 30 30 2e 30 3b 0a 20 20 1000000000.0;.
1830f 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
18310 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
18311 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e t++;. if( p->
18312 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 readOnly==0 ) db
18313 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2b 2b ->writeVdbeCnt++
18314 3b 0a 20 20 20 20 70 2d 3e 70 63 20 3d 20 30 3b ;. p->pc = 0;
18315 0a 20 20 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f . stmtLruRemo
18316 76 65 28 70 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 ve(p);. }.#ifnd
18317 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 ef SQLITE_OMIT_E
18318 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e XPLAIN. if( p->
18319 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 explain ){. r
1831a 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4c c = sqlite3VdbeL
1831b 69 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a ist(p);. }else.
1831c 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1831d 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f _OMIT_EXPLAIN */
1831e 0a 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 . {. rc = sq
1831f 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 70 29 lite3VdbeExec(p)
18320 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c ;. }.. if( sql
18321 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
18322 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 ) ){. rc = SQ
18323 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
18324 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
18325 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a _OMIT_TRACE. /*
18326 20 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f 66 Invoke the prof
18327 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 ile callback if
18328 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a there is one. *
18329 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 /. if( rc!=SQLI
1832a 54 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e 78 50 TE_ROW && db->xP
1832b 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 rofile && !db->i
1832c 6e 69 74 2e 62 75 73 79 20 26 26 20 70 2d 3e 6e nit.busy && p->n
1832d 4f 70 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 Op>0.
1832e 26 26 20 70 2d 3e 61 4f 70 5b 30 5d 2e 6f 70 63 && p->aOp[0].opc
1832f 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 ode==OP_Trace &&
18330 20 70 2d 3e 61 4f 70 5b 30 5d 2e 70 34 2e 7a 21 p->aOp[0].p4.z!
18331 3d 30 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 =0 ){. double
18332 20 72 4e 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 rNow;. u64 e
18333 6c 61 70 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 lapseTime;..
18334 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
18335 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 Time(db->pVfs, &
18336 72 4e 6f 77 29 3b 0a 20 20 20 20 65 6c 61 70 73 rNow);. elaps
18337 65 54 69 6d 65 20 3d 20 28 72 4e 6f 77 20 2d 20 eTime = (rNow -
18338 28 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e (int)rNow)*3600.
18339 30 2a 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 0*24.0*100000000
1833a 30 2e 30 20 2d 20 70 2d 3e 73 74 61 72 74 54 69 0.0 - p->startTi
1833b 6d 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f me;. db->xPro
1833c 66 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c file(db->pProfil
1833d 65 41 72 67 2c 20 70 2d 3e 61 4f 70 5b 30 5d 2e eArg, p->aOp[0].
1833e 70 34 2e 7a 2c 20 65 6c 61 70 73 65 54 69 6d 65 p4.z, elapseTime
1833f 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 );. }.#endif..
18340 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 db->errCode = r
18341 63 3b 0a 20 20 2f 2a 73 71 6c 69 74 65 33 45 72 c;. /*sqlite3Er
18342 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30 ror(p->db, rc, 0
18343 29 3b 2a 2f 0a 20 20 70 2d 3e 72 63 20 3d 20 73 );*/. p->rc = s
18344 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d qlite3ApiExit(p-
18345 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a 65 6e 64 >db, p->rc);.end
18346 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 61 73 73 65 _of_step:. asse
18347 72 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d 72 rt( (rc&0xff)==r
18348 63 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 53 c );. if( p->zS
18349 71 6c 20 26 26 20 28 72 63 26 30 78 66 66 29 3c ql && (rc&0xff)<
1834a 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 SQLITE_ROW ){.
1834b 20 20 2f 2a 20 54 68 69 73 20 62 65 68 61 76 69 /* This behavi
1834c 6f 72 20 6f 63 63 75 72 73 20 69 66 20 73 71 6c or occurs if sql
1834d 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
1834e 29 20 77 61 73 20 75 73 65 64 20 74 6f 20 62 75 ) was used to bu
1834f 69 6c 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 ild. ** the p
18350 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
18351 74 2e 20 20 52 65 74 75 72 6e 20 65 72 72 6f 72 t. Return error
18352 20 63 6f 64 65 73 20 64 69 72 65 63 74 6c 79 20 codes directly
18353 2a 2f 0a 20 20 20 20 70 2d 3e 64 62 2d 3e 65 72 */. p->db->er
18354 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 rCode = p->rc;.
18355 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 45 72 72 /* sqlite3Err
18356 6f 72 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 2c or(p->db, p->rc,
18357 20 30 29 3b 20 2a 2f 0a 20 20 20 20 72 65 74 75 0); */. retu
18358 72 6e 20 70 2d 3e 72 63 3b 0a 20 20 7d 65 6c 73 rn p->rc;. }els
18359 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 e{. /* This i
1835a 73 20 66 6f 72 20 6c 65 67 61 63 79 20 73 71 6c s for legacy sql
1835b 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 62 ite3_prepare() b
1835c 75 69 6c 64 73 20 61 6e 64 20 77 68 65 6e 20 74 uilds and when t
1835d 68 65 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20 69 he code. ** i
1835e 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 6f 72 20 s SQLITE_ROW or
1835f 53 51 4c 49 54 45 5f 44 4f 4e 45 20 2a 2f 0a 20 SQLITE_DONE */.
18360 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
18361 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 }.}../*.** This
18362 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c is the top-level
18363 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
18364 6f 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 of sqlite3_step(
18365 29 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 ). Call.** sqli
18366 74 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 te3Step() to do
18367 6d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b most of the work
18368 2e 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 . If a schema e
18369 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 rror occurs,.**
1836a 63 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 call sqlite3Repr
1836b 65 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 epare() and try
1836c 61 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 again..*/.#ifdef
1836d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 SQLITE_OMIT_PAR
1836e 53 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 SER.SQLITE_API i
1836f 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 nt sqlite3_step(
18370 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
18371 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d tmt){. int rc =
18372 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
18373 20 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 if( pStmt ){.
18374 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 Vdbe *v;.
18375 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 v = (Vdbe*)pStmt
18376 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
18377 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d tex_enter(v->db-
18378 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 >mutex);. rc
18379 3d 20 73 71 6c 69 74 65 33 53 74 65 70 28 76 29 = sqlite3Step(v)
1837a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1837b 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d tex_leave(v->db-
1837c 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 >mutex);. }. r
1837d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 eturn rc;.}.#els
1837e 65 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 e.SQLITE_API int
1837f 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 sqlite3_step(sq
18380 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
18381 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
18382 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
18383 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 if( pStmt ){.
18384 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 int cnt = 0;.
18385 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 Vdbe *v = (Vdb
18386 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 e*)pStmt;. sq
18387 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 lite3 *db = v->d
18388 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d b;. sqlite3_m
18389 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d utex_enter(db->m
1838a 75 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 utex);. while
1838b 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 ( (rc = sqlite3S
1838c 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f tep(v))==SQLITE_
1838d 53 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 SCHEMA.
1838e 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 && cnt++ < 5.
1838f 20 20 20 20 20 20 20 20 20 20 26 26 20 76 64 62 && vdb
18390 65 52 65 70 72 65 70 61 72 65 28 76 29 20 29 7b eReprepare(v) ){
18391 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
18392 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 eset(pStmt);.
18393 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20 3d 20 v->expired =
18394 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 0;. }. if(
18395 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 rc==SQLITE_SCHE
18396 4d 41 20 26 26 20 76 2d 3e 7a 53 71 6c 20 26 26 MA && v->zSql &&
18397 20 64 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 db->pErr ){.
18398 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 /* This case
18399 6f 63 63 75 72 73 20 61 66 74 65 72 20 66 61 69 occurs after fai
1839a 6c 69 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c ling to recompil
1839b 65 20 61 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 e an sql stateme
1839c 6e 74 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 nt. . ** Th
1839d 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
1839e 66 72 6f 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d from the SQL com
1839f 70 69 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 piler has alread
183a0 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 y been loaded .
183a1 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 ** into the
183a2 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
183a3 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 . This block cop
183a4 69 65 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65 ies the error me
183a5 73 73 61 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 ssage . **
183a6 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
183a7 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 e handle into th
183a8 65 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 e statement and
183a9 73 65 74 73 20 74 68 65 20 73 74 61 74 65 6d 65 sets the stateme
183aa 6e 74 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 nt. ** prog
183ab 72 61 6d 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 ram counter to 0
183ac 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 to ensure that
183ad 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 when the stateme
183ae 6e 74 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 nt is . **
183af 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 finalized or res
183b0 65 74 20 74 68 65 20 70 61 72 73 65 72 20 65 72 et the parser er
183b1 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 61 ror message is a
183b2 76 61 69 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 vailable via.
183b3 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 ** sqlite3_er
183b4 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 rmsg() and sqlit
183b5 65 33 5f 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 e3_errcode()..
183b6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e */. con
183b7 73 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 st char *zErr =
183b8 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 (const char *)sq
183b9 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
183ba 28 64 62 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 (db->pErr); .
183bb 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
183bc 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29 (db, v->zErrMsg)
183bd 3b 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d ;. if( !db-
183be 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
183bf 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 . v->zErr
183c0 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 Msg = sqlite3DbS
183c1 74 72 44 75 70 28 64 62 2c 20 7a 45 72 72 29 3b trDup(db, zErr);
183c2 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a . } else {.
183c3 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d v->zErrM
183c4 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 sg = 0;.
183c5 76 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e v->rc = SQLITE_N
183c6 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 OMEM;. }.
183c7 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
183c8 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 ite3ApiExit(db,
183c9 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 rc);. sqlite3
183ca 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d _mutex_leave(db-
183cb 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 >mutex);. }. r
183cc 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
183cd 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 if../*.** Extrac
183ce 74 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 t the user data
183cf 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 from a sqlite3_c
183d0 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 ontext structure
183d1 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a and return a.**
183d2 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a pointer to it..
183d3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
183d4 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 65 72 id *sqlite3_user
183d5 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f _data(sqlite3_co
183d6 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 ntext *p){. ass
183d7 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75 ert( p && p->pFu
183d8 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 nc );. return p
183d9 2d 3e 70 46 75 6e 63 2d 3e 70 55 73 65 72 44 61 ->pFunc->pUserDa
183da 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 ta;.}../*.** Ext
183db 72 61 63 74 20 74 68 65 20 75 73 65 72 20 64 61 ract the user da
183dc 74 61 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 ta from a sqlite
183dd 33 5f 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74 3_context struct
183de 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ure and return a
183df 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 .** pointer to i
183e0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
183e1 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 sqlite3 *sqlite
183e2 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
183e3 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 dle(sqlite3_cont
183e4 65 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ext *p){. asser
183e5 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 t( p && p->pFunc
183e6 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e );. return p->
183e7 73 2e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 s.db;.}../*.** T
183e8 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 he following is
183e9 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
183ea 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 66 75 6e on of an SQL fun
183eb 63 74 69 6f 6e 20 74 68 61 74 20 61 6c 77 61 79 ction that alway
183ec 73 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 68 20 s.** fails with
183ed 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
183ee 20 73 74 61 74 69 6e 67 20 74 68 61 74 20 74 68 stating that th
183ef 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 e function is us
183f0 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 6f ed in the.** wro
183f1 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 ng context. The
183f2 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 sqlite3_overloa
183f3 64 5f 66 75 6e 63 74 69 6f 6e 28 29 20 41 50 49 d_function() API
183f4 20 6d 69 67 68 74 20 63 6f 6e 73 74 72 75 63 74 might construct
183f5 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e .** SQL function
183f6 20 74 68 61 74 20 75 73 65 20 74 68 69 73 20 72 that use this r
183f7 6f 75 74 69 6e 65 20 73 6f 20 74 68 61 74 20 74 outine so that t
183f8 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 6c he functions wil
183f9 6c 20 65 78 69 73 74 0a 2a 2a 20 66 6f 72 20 6e l exist.** for n
183fa 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 62 ame resolution b
183fb 75 74 20 61 72 65 20 61 63 74 75 61 6c 6c 79 20 ut are actually
183fc 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 74 68 overloaded by th
183fd 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 0a e xFindFunction.
183fe 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 76 69 72 ** method of vir
183ff 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a tual tables..*/.
18400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
18401 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c oid sqlite3Inval
18402 69 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 idFunction(. sq
18403 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
18404 6f 6e 74 65 78 74 2c 20 20 2f 2a 20 54 68 65 20 ontext, /* The
18405 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 69 6e 67 function calling
18406 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e context */. in
18407 74 20 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 20 t NotUsed,
18408 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
18409 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
1840a 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 to the function
1840b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c */. sqlite3_val
1840c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 20 20 20 ue **NotUsed2
1840d 2f 2a 20 56 61 6c 75 65 20 6f 66 20 65 61 63 68 /* Value of each
1840e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a argument */.){.
1840f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
18410 61 6d 65 20 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 ame = context->p
18411 46 75 6e 63 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63 Func->zName;. c
18412 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 har *zErr;. UNU
18413 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
18414 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
18415 29 3b 0a 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 );. zErr = sqli
18416 74 65 33 4d 50 72 69 6e 74 66 28 30 2c 0a 20 20 te3MPrintf(0,.
18417 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 75 "unable to u
18418 73 65 20 66 75 6e 63 74 69 6f 6e 20 25 73 20 69 se function %s i
18419 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 n the requested
1841a 63 6f 6e 74 65 78 74 22 2c 20 7a 4e 61 6d 65 29 context", zName)
1841b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 ;. sqlite3_resu
1841c 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 lt_error(context
1841d 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 73 , zErr, -1);. s
1841e 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 qlite3_free(zErr
1841f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f );.}../*.** Allo
18420 63 61 74 65 20 6f 72 20 72 65 74 75 72 6e 20 74 cate or return t
18421 68 65 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e he aggregate con
18422 74 65 78 74 20 66 6f 72 20 61 20 75 73 65 72 20 text for a user
18423 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 6e 65 77 function. A new
18424 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 61 .** context is a
18425 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 llocated on the
18426 66 69 72 73 74 20 63 61 6c 6c 2e 20 20 53 75 62 first call. Sub
18427 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 72 65 sequent calls re
18428 74 75 72 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 turn the.** same
18429 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 77 61 context that wa
1842a 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 70 72 s returned on pr
1842b 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 53 51 ior calls..*/.SQ
1842c 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
1842d 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 qlite3_aggregate
1842e 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 _context(sqlite3
1842f 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 _context *p, int
18430 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20 2a nByte){. Mem *
18431 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 pMem;. assert(
18432 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 26 26 p && p->pFunc &&
18433 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70 p->pFunc->xStep
18434 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
18435 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
18436 28 70 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 (p->s.db->mutex)
18437 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 70 2d 3e );. pMem = p->
18438 70 4d 65 6d 3b 0a 20 20 69 66 28 20 28 70 4d 65 pMem;. if( (pMe
18439 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 m->flags & MEM_A
1843a 67 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 gg)==0 ){. if
1843b 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 ( nByte==0 ){.
1843c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1843d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
1843e 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 70 l(pMem);. p
1843f 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
18440 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 70 4d 65 _Null;. pMe
18441 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 7d 65 m->z = 0;. }e
18442 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
18443 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
18444 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 3b 0a 20 em, nByte, 0);.
18445 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
18446 20 3d 20 4d 45 4d 5f 41 67 67 3b 0a 20 20 20 20 = MEM_Agg;.
18447 20 20 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 20 3d pMem->u.pDef =
18448 20 70 2d 3e 70 46 75 6e 63 3b 0a 20 20 20 20 20 p->pFunc;.
18449 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a if( pMem->z ){.
1844a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 memset(p
1844b 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 65 Mem->z, 0, nByte
1844c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1844d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 . }. return (v
1844e 6f 69 64 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 7d 0a oid*)pMem->z;.}.
1844f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
18450 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 61 20 e auxilary data
18451 70 6f 69 6e 74 65 72 2c 20 69 66 20 61 6e 79 2c pointer, if any,
18452 20 66 6f 72 20 74 68 65 20 69 41 72 67 27 74 68 for the iArg'th
18453 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 argument to.**
18454 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f the user-functio
18455 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70 43 74 n defined by pCt
18456 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 x..*/.SQLITE_API
18457 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 void *sqlite3_g
18458 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 et_auxdata(sqlit
18459 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1845a 2c 20 69 6e 74 20 69 41 72 67 29 7b 0a 20 20 56 , int iArg){. V
1845b 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 dbeFunc *pVdbeFu
1845c 6e 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 nc;.. assert( s
1845d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1845e 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
1845f 74 65 78 29 20 29 3b 0a 20 20 70 56 64 62 65 46 tex) );. pVdbeF
18460 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 64 62 unc = pCtx->pVdb
18461 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 70 56 eFunc;. if( !pV
18462 64 62 65 46 75 6e 63 20 7c 7c 20 69 41 72 67 3e dbeFunc || iArg>
18463 3d 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 =pVdbeFunc->nAux
18464 20 7c 7c 20 69 41 72 67 3c 30 20 29 7b 0a 20 20 || iArg<0 ){.
18465 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
18466 20 20 72 65 74 75 72 6e 20 70 56 64 62 65 46 75 return pVdbeFu
18467 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 2e nc->apAux[iArg].
18468 70 41 75 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 pAux;.}../*.** S
18469 65 74 20 74 68 65 20 61 75 78 69 6c 61 72 79 20 et the auxilary
1846a 64 61 74 61 20 70 6f 69 6e 74 65 72 20 61 6e 64 data pointer and
1846b 20 64 65 6c 65 74 65 20 66 75 6e 63 74 69 6f 6e delete function
1846c 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27 74 , for the iArg't
1846d 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f h.** argument to
1846e 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 the user-functi
1846f 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70 43 on defined by pC
18470 74 78 2e 20 41 6e 79 20 70 72 65 76 69 6f 75 73 tx. Any previous
18471 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 64 65 6c value is.** del
18472 65 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 eted by calling
18473 74 68 65 20 64 65 6c 65 74 65 20 66 75 6e 63 74 the delete funct
18474 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 77 68 ion specified wh
18475 65 6e 20 69 74 20 77 61 73 20 73 65 74 2e 0a 2a en it was set..*
18476 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
18477 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 d sqlite3_set_au
18478 78 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65 33 xdata(. sqlite3
18479 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1847a 0a 20 20 69 6e 74 20 69 41 72 67 2c 20 0a 20 20 . int iArg, .
1847b 76 6f 69 64 20 2a 70 41 75 78 2c 20 0a 20 20 76 void *pAux, . v
1847c 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29 28 76 oid (*xDelete)(v
1847d 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 74 72 75 63 oid*).){. struc
1847e 74 20 41 75 78 44 61 74 61 20 2a 70 41 75 78 44 t AuxData *pAuxD
1847f 61 74 61 3b 0a 20 20 56 64 62 65 46 75 6e 63 20 ata;. VdbeFunc
18480 2a 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 *pVdbeFunc;. if
18481 28 20 69 41 72 67 3c 30 20 29 20 67 6f 74 6f 20 ( iArg<0 ) goto
18482 66 61 69 6c 65 64 3b 0a 0a 20 20 61 73 73 65 72 failed;.. asser
18483 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
18484 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
18485 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 56 ->mutex) );. pV
18486 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e dbeFunc = pCtx->
18487 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 28 pVdbeFunc;. if(
18488 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 70 !pVdbeFunc || p
18489 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3c 3d VdbeFunc->nAux<=
1848a 69 41 72 67 20 29 7b 0a 20 20 20 20 69 6e 74 20 iArg ){. int
1848b 6e 41 75 78 20 3d 20 28 70 56 64 62 65 46 75 6e nAux = (pVdbeFun
1848c 63 20 3f 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e c ? pVdbeFunc->n
1848d 41 75 78 20 3a 20 30 29 3b 0a 20 20 20 20 69 6e Aux : 0);. in
1848e 74 20 6e 4d 61 6c 6c 6f 63 20 3d 20 73 69 7a 65 t nMalloc = size
1848f 6f 66 28 56 64 62 65 46 75 6e 63 29 20 2b 20 73 of(VdbeFunc) + s
18490 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75 78 izeof(struct Aux
18491 44 61 74 61 29 2a 69 41 72 67 3b 0a 20 20 20 20 Data)*iArg;.
18492 70 56 64 62 65 46 75 6e 63 20 3d 20 73 71 6c 69 pVdbeFunc = sqli
18493 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 43 74 te3DbRealloc(pCt
18494 78 2d 3e 73 2e 64 62 2c 20 70 56 64 62 65 46 75 x->s.db, pVdbeFu
18495 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 29 3b 0a 20 20 nc, nMalloc);.
18496 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 if( !pVdbeFunc
18497 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 ){. goto f
18498 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 ailed;. }.
18499 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 pCtx->pVdbeFunc
1849a 20 3d 20 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 = pVdbeFunc;.
1849b 20 20 6d 65 6d 73 65 74 28 26 70 56 64 62 65 46 memset(&pVdbeF
1849c 75 6e 63 2d 3e 61 70 41 75 78 5b 6e 41 75 78 5d unc->apAux[nAux]
1849d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 74 72 75 , 0, sizeof(stru
1849e 63 74 20 41 75 78 44 61 74 61 29 2a 28 69 41 72 ct AuxData)*(iAr
1849f 67 2b 31 2d 6e 41 75 78 29 29 3b 0a 20 20 20 20 g+1-nAux));.
184a0 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 20 pVdbeFunc->nAux
184a1 3d 20 69 41 72 67 2b 31 3b 0a 20 20 20 20 70 56 = iArg+1;. pV
184a2 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 20 3d dbeFunc->pFunc =
184a3 20 70 43 74 78 2d 3e 70 46 75 6e 63 3b 0a 20 20 pCtx->pFunc;.
184a4 7d 0a 0a 20 20 70 41 75 78 44 61 74 61 20 3d 20 }.. pAuxData =
184a5 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 &pVdbeFunc->apAu
184a6 78 5b 69 41 72 67 5d 3b 0a 20 20 69 66 28 20 70 x[iArg];. if( p
184a7 41 75 78 44 61 74 61 2d 3e 70 41 75 78 20 26 26 AuxData->pAux &&
184a8 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 pAuxData->xDele
184a9 74 65 20 29 7b 0a 20 20 20 20 70 41 75 78 44 61 te ){. pAuxDa
184aa 74 61 2d 3e 78 44 65 6c 65 74 65 28 70 41 75 78 ta->xDelete(pAux
184ab 44 61 74 61 2d 3e 70 41 75 78 29 3b 0a 20 20 7d Data->pAux);. }
184ac 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 70 41 75 . pAuxData->pAu
184ad 78 20 3d 20 70 41 75 78 3b 0a 20 20 70 41 75 78 x = pAux;. pAux
184ae 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 3d 20 Data->xDelete =
184af 78 44 65 6c 65 74 65 3b 0a 20 20 72 65 74 75 72 xDelete;. retur
184b0 6e 3b 0a 0a 66 61 69 6c 65 64 3a 0a 20 20 69 66 n;..failed:. if
184b1 28 20 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 ( xDelete ){.
184b2 20 78 44 65 6c 65 74 65 28 70 41 75 78 29 3b 0a xDelete(pAux);.
184b3 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 }.}..#ifndef S
184b4 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 QLITE_OMIT_DEPRE
184b5 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75 CATED./*.** Retu
184b6 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
184b7 20 74 69 6d 65 73 20 74 68 65 20 53 74 65 70 20 times the Step
184b8 66 75 6e 63 74 69 6f 6e 20 6f 66 20 61 20 61 67 function of a ag
184b9 67 72 65 67 61 74 65 20 68 61 73 20 62 65 65 6e gregate has been
184ba 20 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a .** called..**.
184bb 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
184bc 20 69 73 20 64 65 70 72 65 63 61 74 65 64 2e 20 is deprecated.
184bd 20 44 6f 20 6e 6f 74 20 75 73 65 20 69 74 20 66 Do not use it f
184be 6f 72 20 6e 65 77 20 63 6f 64 65 2e 20 20 49 74 or new code. It
184bf 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 65 20 6f is.** provide o
184c0 6e 6c 79 20 74 6f 20 61 76 6f 69 64 20 62 72 65 nly to avoid bre
184c1 61 6b 69 6e 67 20 6c 65 67 61 63 79 20 63 6f 64 aking legacy cod
184c2 65 2e 20 20 4e 65 77 20 61 67 67 72 65 67 61 74 e. New aggregat
184c3 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6d e function.** im
184c4 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 plementations sh
184c5 6f 75 6c 64 20 6b 65 65 70 20 74 68 65 69 72 20 ould keep their
184c6 6f 77 6e 20 63 6f 75 6e 74 73 20 77 69 74 68 69 own counts withi
184c7 6e 20 74 68 65 69 72 20 61 67 67 72 65 67 61 74 n their aggregat
184c8 65 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 0a 2a 2f e.** context..*/
184c9 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
184ca 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 sqlite3_aggregat
184cb 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f e_count(sqlite3_
184cc 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61 context *p){. a
184cd 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 ssert( p && p->p
184ce 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 Func && p->pFunc
184cf 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72 65 74 ->xStep );. ret
184d0 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a urn p->pMem->n;.
184d1 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
184d2 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
184d3 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
184d4 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 the result set f
184d5 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 or the statement
184d6 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 pStmt..*/.SQLIT
184d7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
184d8 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 3_column_count(s
184d9 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
184da 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d mt){. Vdbe *pVm
184db 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 = (Vdbe *)pStmt
184dc 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 20 3f ;. return pVm ?
184dd 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e pVm->nResColumn
184de 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 : 0;.}../*.** R
184df 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
184e0 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61 69 6c of values avail
184e1 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 75 able from the cu
184e2 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 rrent row of the
184e3 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 78 .** currently ex
184e4 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e ecuting statemen
184e5 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 t pStmt..*/.SQLI
184e6 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
184e7 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 e3_data_count(sq
184e8 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
184e9 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 t){. Vdbe *pVm
184ea 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
184eb 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20 7c 7c . if( pVm==0 ||
184ec 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 pVm->pResultSet
184ed 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
184ee 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e 6e 52 return pVm->nR
184ef 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a esColumn;.}.../*
184f0 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 .** Check to see
184f1 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 if column iCol
184f2 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 61 of the given sta
184f3 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69 64 2e tement is valid.
184f4 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c 20 72 If.** it is, r
184f5 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
184f6 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72 20 74 to the Mem for t
184f7 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 61 74 he value of that
184f8 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69 column..** If i
184f9 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c 69 64 Col is not valid
184fa 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 , return a point
184fb 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68 69 63 er to a Mem whic
184fc 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a 2a 2a h has a value.**
184fd 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 of NULL..*/.sta
184fe 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d tic Mem *columnM
184ff 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 em(sqlite3_stmt
18500 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
18501 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20 20 69 Vdbe *pVm;. i
18502 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d 20 2a nt vals;. Mem *
18503 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d 20 28 pOut;.. pVm = (
18504 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
18505 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d 2d 3e if( pVm && pVm->
18506 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20 26 26 pResultSet!=0 &&
18507 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 i<pVm->nResColu
18508 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a 20 20 mn && i>=0 ){.
18509 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1850a 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d enter(pVm->db->m
1850b 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c 73 20 utex);. vals
1850c 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 = sqlite3_data_c
1850d 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 ount(pStmt);.
1850e 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e 70 52 pOut = &pVm->pR
1850f 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20 20 7d esultSet[i];. }
18510 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 else{. static
18511 20 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d const Mem nullM
18512 65 6d 20 3d 20 7b 7b 30 7d 2c 20 30 2e 30 2c 20 em = {{0}, 0.0,
18513 30 2c 20 22 22 2c 20 30 2c 20 4d 45 4d 5f 4e 75 0, "", 0, MEM_Nu
18514 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c ll, SQLITE_NULL,
18515 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 0, 0, 0 };.
18516 69 66 28 20 70 56 6d 2d 3e 64 62 20 29 7b 0a 20 if( pVm->db ){.
18517 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
18518 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 ex_enter(pVm->db
18519 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
1851a 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 6d sqlite3Error(pVm
1851b 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e ->db, SQLITE_RAN
1851c 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 GE, 0);. }.
1851d 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 26 pOut = (Mem*)&
1851e 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 nullMem;. }. r
1851f 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f eturn pOut;.}../
18520 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
18521 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 on is called aft
18522 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 73 er invoking an s
18523 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 58 qlite3_value_XXX
18524 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 0a function on a .
18525 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 ** column value
18526 28 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 65 (i.e. a value re
18527 74 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 61 turned by evalua
18528 74 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72 ting an SQL expr
18529 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a ession in the.**
1852a 20 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 20 select list of
1852b 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 a SELECT stateme
1852c 6e 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 75 nt) that may cau
1852d 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 se a malloc() fa
1852e 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d 61 ilure. If .** ma
1852f 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 lloc() has faile
18530 64 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 6d d, the threads m
18531 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 allocFailed flag
18532 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20 is cleared and
18533 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f the result.** co
18534 64 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 de of statement
18535 70 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 4c pStmt set to SQL
18536 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a ITE_NOMEM..**.**
18537 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 Specifically, t
18538 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 72 his is called fr
18539 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a om within:.**.**
1853a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c sqlite3_col
1853b 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 20 umn_int().**
1853c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1853d 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 73 int64().** s
1853e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1853f 78 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 xt().** sqli
18540 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
18541 36 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 6().** sqlit
18542 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29 e3_column_real()
18543 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f .** sqlite3_
18544 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a column_bytes().*
18545 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
18546 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a 2a lumn_bytes16().*
18547 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f 72 *.** But not for
18548 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
18549 62 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e 65 blob(), which ne
1854a 76 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63 ver calls malloc
1854b 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ()..*/.static vo
1854c 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 id columnMallocF
1854d 61 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f 73 ailure(sqlite3_s
1854e 74 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20 20 tmt *pStmt).{.
1854f 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20 66 /* If malloc() f
18550 61 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e 20 ailed during an
18551 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 encoding convers
18552 69 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20 20 ion within an.
18553 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
18554 6e 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e 20 n_XXX API, then
18555 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 set the return c
18556 6f 64 65 20 6f 66 20 74 68 65 20 73 74 61 74 65 ode of the state
18557 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51 4c ment to. ** SQL
18558 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e ITE_NOMEM. The n
18559 65 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 65 ext call to _ste
1855a 70 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 6c p() (if any) wil
1855b 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f l return SQLITE_
1855c 45 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20 5f ERROR. ** and _
1855d 66 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20 finalize() will
1855e 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 return NOMEM..
1855f 2a 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 */. Vdbe *p = (
18560 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
18561 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e if( p ){. p->
18562 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 rc = sqlite3ApiE
18563 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 xit(p->db, p->rc
18564 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
18565 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 utex_leave(p->db
18566 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a ->mutex);. }.}.
18567 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
18568 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 ************** s
18569 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20 qlite3_column_
1856a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1856b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1856c 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
1856d 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 routines are us
1856e 65 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c 65 ed to access ele
1856f 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72 ments of the cur
18570 72 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 rent row.** in t
18571 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a he result set..*
18572 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
18573 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
18574 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c _column_blob(sql
18575 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
18576 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 , int i){. cons
18577 74 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 76 t void *val;. v
18578 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
18579 75 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d ue_blob( columnM
1857a 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
1857b 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 /* Even though
1857c 74 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f there is no enco
1857d 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c ding conversion,
1857e 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69 value_blob() mi
1857f 67 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f ght. ** need to
18580 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 74 call malloc() t
18581 6f 20 65 78 70 61 6e 64 20 74 68 65 20 72 65 73 o expand the res
18582 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c 6f ult of a zeroblo
18583 62 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 b() . ** expres
18584 73 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f sion. . */. co
18585 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
18586 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 e(pStmt);. retu
18587 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 rn val;.}.SQLITE
18588 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
18589 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71 _column_bytes(sq
1858a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1858b 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 t, int i){. int
1858c 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 val = sqlite3_v
1858d 61 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c 75 alue_bytes( colu
1858e 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
1858f 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 ;. columnMalloc
18590 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
18591 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a return val;.}.
18592 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
18593 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
18594 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 tes16(sqlite3_st
18595 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
18596 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 ){. int val = s
18597 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
18598 65 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 es16( columnMem(
18599 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f pStmt,i) );. co
1859a 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
1859b 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 e(pStmt);. retu
1859c 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 rn val;.}.SQLITE
1859d 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 _API double sqli
1859e 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c te3_column_doubl
1859f 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
185a0 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
185a1 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73 71 double val = sq
185a2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 lite3_value_doub
185a3 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 le( columnMem(pS
185a4 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 tmt,i) );. colu
185a5 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 mnMallocFailure(
185a6 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e pStmt);. return
185a7 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 val;.}.SQLITE_A
185a8 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
185a9 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65 olumn_int(sqlite
185aa 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
185ab 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c nt i){. int val
185ac 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
185ad 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 _int( columnMem(
185ae 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f pStmt,i) );. co
185af 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
185b0 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 e(pStmt);. retu
185b1 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 rn val;.}.SQLITE
185b2 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 _API sqlite_int6
185b3 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 4 sqlite3_column
185b4 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 _int64(sqlite3_s
185b5 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
185b6 69 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 i){. sqlite_int
185b7 36 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 64 val = sqlite3
185b8 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63 6f _value_int64( co
185b9 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 lumnMem(pStmt,i)
185ba 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c );. columnMall
185bb 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 ocFailure(pStmt)
185bc 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a ;. return val;.
185bd 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e }.SQLITE_API con
185be 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
185bf 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
185c0 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 _text(sqlite3_st
185c1 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
185c2 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 ){. const unsig
185c3 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d 20 ned char *val =
185c4 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
185c5 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 xt( columnMem(pS
185c6 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 tmt,i) );. colu
185c7 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 mnMallocFailure(
185c8 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e pStmt);. return
185c9 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 val;.}.SQLITE_A
185ca 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 PI sqlite3_value
185cb 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
185cc 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 _value(sqlite3_s
185cd 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
185ce 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 i){. Mem *pOut
185cf 3d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d = columnMem(pStm
185d0 74 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f 75 t, i);. if( pOu
185d1 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 t->flags&MEM_Sta
185d2 74 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d tic ){. pOut-
185d3 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 >flags &= ~MEM_S
185d4 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d tatic;. pOut-
185d5 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 >flags |= MEM_Ep
185d6 68 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d hem;. }. colum
185d7 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
185d8 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
185d9 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
185da 29 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65 66 )pOut;.}.#ifndef
185db 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
185dc 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 16.SQLITE_API co
185dd 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
185de 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
185df 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
185e0 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 tmt, int i){. c
185e1 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d onst void *val =
185e2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
185e3 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d ext16( columnMem
185e4 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 (pStmt,i) );. c
185e5 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
185e6 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 re(pStmt);. ret
185e7 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69 urn val;.}.#endi
185e8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
185e9 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 _UTF16 */.SQLITE
185ea 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
185eb 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c _column_type(sql
185ec 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
185ed 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 , int i){. int
185ee 69 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f iType = sqlite3_
185ef 76 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c 75 value_type( colu
185f0 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
185f1 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 ;. columnMalloc
185f2 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
185f3 20 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b 0a return iType;.
185f4 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 }../* The follow
185f5 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ing function is
185f6 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 experimental and
185f7 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e subject to chan
185f8 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c ge or.** removal
185f9 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 65 */./*int sqlite
185fa 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63 3_column_numeric
185fb 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 _type(sqlite3_st
185fc 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
185fd 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 71 ){.** return sq
185fe 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 lite3_value_nume
185ff 72 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e ric_type( column
18600 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a Mem(pStmt,i) );.
18601 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f **}.*/../*.** Co
18602 6e 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20 65 nvert the N-th e
18603 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74 2d lement of pStmt-
18604 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f >pColName[] into
18605 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 0a a string using.
18606 2a 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e 20 ** xFunc() then
18607 72 65 74 75 72 6e 20 74 68 61 74 20 73 74 72 69 return that stri
18608 6e 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75 74 ng. If N is out
18609 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 of range, retur
1860a 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 n 0..**.** There
1860b 20 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61 6d are up to 5 nam
1860c 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 es for each colu
1860d 6d 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65 74 mn. useType det
1860e 65 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a 2a ermines which.**
1860f 20 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e 65 name is returne
18610 64 2e 20 20 48 65 72 65 20 61 72 65 20 74 68 65 d. Here are the
18611 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 names:.**.**
18612 20 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c 75 0 The colu
18613 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73 68 mn name as it sh
18614 6f 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79 65 ould be displaye
18615 64 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a 20 d for output.**
18616 20 20 20 31 20 20 20 20 20 20 54 68 65 20 64 61 1 The da
18617 74 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72 20 tatype name for
18618 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 the column.**
18619 20 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 2 The name
1861a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1861b 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e that the column
1861c 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a derives from.**
1861d 20 20 20 20 33 20 20 20 20 20 20 54 68 65 20 6e 3 The n
1861e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1861f 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e that the column
18620 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a derives from.**
18621 20 20 20 20 34 20 20 20 20 20 20 54 68 65 20 6e 4 The n
18622 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
18623 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 column that the
18624 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 result column d
18625 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a erives from.**.*
18626 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 * If the result
18627 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 is not a simple
18628 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 column reference
18629 20 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 78 (if it is an ex
1862a 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61 pression.** or a
1862b 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e 20 constant) then
1862c 75 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 61 useTypes 2, 3, a
1862d 6e 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c 4c nd 4 return NULL
1862e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
1862f 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61 t void *columnNa
18630 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 me(. sqlite3_st
18631 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e 74 mt *pStmt,. int
18632 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 N,. const void
18633 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a 29 *(*xFunc)(Mem*)
18634 2c 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65 0a ,. int useType.
18635 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 ){. const void
18636 2a 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62 65 *ret = 0;. Vdbe
18637 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 *p = (Vdbe *)pS
18638 74 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 tmt;. int n;.
18639 0a 0a 20 20 69 66 28 20 70 21 3d 30 20 29 7b 0a .. if( p!=0 ){.
1863a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f n = sqlite3_
1863b 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 column_count(pSt
1863c 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 4e 3c 6e mt);. if( N<n
1863d 20 26 26 20 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 && N>=0 ){.
1863e 20 20 4e 20 2b 3d 20 75 73 65 54 79 70 65 2a 6e N += useType*n
1863f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
18640 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 mutex_enter(p->d
18641 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 b->mutex);.
18642 20 72 65 74 20 3d 20 78 46 75 6e 63 28 26 70 2d ret = xFunc(&p-
18643 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 0a >aColName[N]);..
18644 20 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f /* A mallo
18645 63 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 c may have faile
18646 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 d inside of the
18647 78 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66 xFunc() call. If
18648 20 74 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 this. ** i
18649 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c 65 61 s the case, clea
1864a 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c r the mallocFail
1864b 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75 ed flag and retu
1864c 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 2a rn NULL.. *
1864d 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 64 /. if( p->d
1864e 62 20 26 26 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c b && p->db->mall
1864f 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
18650 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f p->db->mallo
18651 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 cFailed = 0;.
18652 20 20 20 20 20 72 65 74 20 3d 20 30 3b 0a 20 20 ret = 0;.
18653 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
18654 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
18655 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
18656 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
18657 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n ret;.}../*.**
18658 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 Return the name
18659 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d of the Nth colum
1865a 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 n of the result
1865b 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 set returned by
1865c 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 SQL.** statement
1865d 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 pStmt..*/.SQLIT
1865e 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
1865f 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
18660 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
18661 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e mt *pStmt, int N
18662 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 ){. return colu
18663 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 mnName(. pS
18664 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 tmt, N, (const v
18665 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 oid*(*)(Mem*))sq
18666 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
18667 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b , COLNAME_NAME);
18668 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
18669 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1866a 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1866b 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
1866c 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 mn_name16(sqlite
1866d 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1866e 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt N){. return
1866f 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 columnName(.
18670 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e pStmt, N, (con
18671 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a st void*(*)(Mem*
18672 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ))sqlite3_value_
18673 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f text16, COLNAME_
18674 4e 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a NAME);.}.#endif.
18675 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e ./*.** Constrain
18676 74 3a 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 t: If you have
18677 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 ENABLE_COLUMN_ME
18678 54 41 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 TADATA then you
18679 6d 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 must.** not defi
1867a 6e 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 ne OMIT_DECLTYPE
1867b 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
1867c 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 (SQLITE_OMIT_DEC
1867d 4c 54 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 LTYPE) && define
1867e 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
1867f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 COLUMN_METADATA)
18680 0a 23 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e .# error "Must n
18681 6f 74 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 ot define both S
18682 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 QLITE_OMIT_DECLT
18683 59 50 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 YPE \. a
18684 6e 64 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 nd SQLITE_ENABLE
18685 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
18686 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 ".#endif..#ifnde
18687 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 f SQLITE_OMIT_DE
18688 43 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 CLTYPE./*.** Ret
18689 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 urn the column d
1868a 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 eclaration type
1868b 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 (if applicable)
1868c 6f 66 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c of the 'i'th col
1868d 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 umn.** of the re
1868e 73 75 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 sult set of SQL
1868f 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e statement pStmt.
18690 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
18691 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
18692 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 e3_column_declty
18693 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 pe(sqlite3_stmt
18694 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
18695 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
18696 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
18697 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
18698 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
18699 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 e3_value_text, C
1869a 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 OLNAME_DECLTYPE)
1869b 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1869c 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
1869d 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
1869e 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
1869f 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 umn_decltype16(s
186a0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
186a1 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
186a2 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
186a3 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
186a4 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
186a5 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
186a6 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c alue_text16, COL
186a7 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a NAME_DECLTYPE);.
186a8 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
186a9 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
186aa 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
186ab 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 E_OMIT_DECLTYPE
186ac 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
186ad 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f E_ENABLE_COLUMN_
186ae 4d 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 METADATA./*.** R
186af 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f eturn the name o
186b0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
186b1 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 rom which a resu
186b2 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 lt column derive
186b3 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 s..** NULL is re
186b4 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 turned if the re
186b5 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 sult column is a
186b6 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 n expression or
186b7 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 constant or.** a
186b8 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 nything else whi
186b9 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 ch is not an una
186ba 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 biguous referenc
186bb 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 e to a database
186bc 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 column..*/.SQLIT
186bd 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
186be 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
186bf 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 _database_name(s
186c0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
186c1 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 mt, int N){. re
186c2 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 turn columnName(
186c3 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c . pStmt, N,
186c4 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 (const void*(*)
186c5 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 (Mem*))sqlite3_v
186c6 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 alue_text, COLNA
186c7 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a ME_DATABASE);.}.
186c8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
186c9 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
186ca 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
186cb 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
186cc 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 database_name16(
186cd 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
186ce 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 tmt, int N){. r
186cf 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 eturn columnName
186d0 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e (. pStmt, N
186d1 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a , (const void*(*
186d2 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f )(Mem*))sqlite3_
186d3 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f value_text16, CO
186d4 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b LNAME_DATABASE);
186d5 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
186d6 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
186d7 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 /../*.** Return
186d8 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
186d9 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 table from which
186da 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e a result column
186db 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c derives..** NUL
186dc 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 L is returned if
186dd 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 the result colu
186de 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 mn is an express
186df 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 ion or constant
186e0 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 or.** anything e
186e1 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 lse which is not
186e2 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 an unabiguous r
186e3 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 eference to a da
186e4 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a tabase column..*
186e5 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
186e6 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
186e7 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 _column_table_na
186e8 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 me(sqlite3_stmt
186e9 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
186ea 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
186eb 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
186ec 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
186ed 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
186ee 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 e3_value_text, C
186ef 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d OLNAME_TABLE);.}
186f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
186f1 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 OMIT_UTF16.SQLIT
186f2 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
186f3 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
186f4 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 _table_name16(sq
186f5 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
186f6 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
186f7 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
186f8 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
186f9 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
186fa 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
186fb 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e lue_text16, COLN
186fc 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 AME_TABLE);.}.#e
186fd 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
186fe 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a MIT_UTF16 */../*
186ff 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
18700 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
18701 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 column from whi
18702 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 ch a result colu
18703 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e mn derives..** N
18704 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 ULL is returned
18705 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f if the result co
18706 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 lumn is an expre
18707 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e ssion or constan
18708 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 t or.** anything
18709 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e else which is n
1870a 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 ot an unabiguous
1870b 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 reference to a
1870c 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e database column.
1870d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
1870e 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
1870f 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e e3_column_origin
18710 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
18711 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e mt *pStmt, int N
18712 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 ){. return colu
18713 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 mnName(. pS
18714 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 tmt, N, (const v
18715 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 oid*(*)(Mem*))sq
18716 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
18717 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e , COLNAME_COLUMN
18718 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c );.}.#ifndef SQL
18719 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 ITE_OMIT_UTF16.S
1871a 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1871b 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
1871c 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 lumn_origin_name
1871d 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 16(sqlite3_stmt
1871e 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
1871f 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
18720 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
18721 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
18722 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
18723 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c e3_value_text16,
18724 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 COLNAME_COLUMN)
18725 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
18726 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 LITE_OMIT_UTF16
18727 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c */.#endif /* SQL
18728 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d ITE_ENABLE_COLUM
18729 4e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a N_METADATA */...
1872a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
1872b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1872c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 sqlite3_bind_
1872d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1872e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a ***********.** .
1872f 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 ** Routines used
18730 20 74 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 to attach value
18731 73 20 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 s to wildcards i
18732 6e 20 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c n a compiled SQL
18733 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f statement..*/./
18734 2a 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 *.** Unbind the
18735 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 value bound to v
18736 61 72 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 ariable i in vir
18737 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 tual machine p.
18738 54 68 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 This is the .**
18739 74 68 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 the same as bind
1873a 69 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 ing a NULL value
1873b 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 to the column.
1873c 49 66 20 74 68 65 20 22 69 22 20 70 61 72 61 6d If the "i" param
1873d 65 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f eter is.** out o
1873e 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 f range, then SQ
1873f 4c 49 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 LITE_RANGE is re
18740 74 75 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 turned. Othewise
18741 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a SQLITE_OK..**.*
18742 2a 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 * A successful e
18743 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 valuation of thi
18744 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 s routine acquir
18745 65 73 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 es the mutex on
18746 70 2e 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 p..** the mutex
18747 69 73 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 is released if a
18748 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 ny kind of error
18749 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 occurs..**.** T
1874a 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 he error code st
1874b 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 ored in database
1874c 20 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 p->db is overwr
1874d 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 itten with the r
1874e 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 eturn.** value i
1874f 6e 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 n any case..*/.s
18750 74 61 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e tatic int vdbeUn
18751 62 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e bind(Vdbe *p, in
18752 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 t i){. Mem *pVa
18753 72 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 r;. if( p==0 )
18754 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
18755 53 55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f SUSE;. sqlite3_
18756 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 mutex_enter(p->d
18757 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 b->mutex);. if(
18758 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f p->magic!=VDBE_
18759 4d 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e MAGIC_RUN || p->
1875a 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c pc>=0 ){. sql
1875b 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c ite3Error(p->db,
1875c 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 SQLITE_MISUSE,
1875d 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 0);. sqlite3_
1875e 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 mutex_leave(p->d
1875f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 b->mutex);. r
18760 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
18761 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 USE;. }. if( i
18762 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 <1 || i>p->nVar
18763 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
18764 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 ror(p->db, SQLIT
18765 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 E_RANGE, 0);.
18766 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
18767 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
18768 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 x);. return S
18769 51 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d QLITE_RANGE;. }
1876a 0a 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d . i--;. pVar =
1876b 20 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 &p->aVar[i];.
1876c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1876d 6c 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 lease(pVar);. p
1876e 56 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Var->flags = MEM
1876f 5f 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 _Null;. sqlite3
18770 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c Error(p->db, SQL
18771 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 ITE_OK, 0);. re
18772 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
18773 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 }../*.** Bind a
18774 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c text or BLOB val
18775 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ue..*/.static in
18776 74 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 t bindText(. sq
18777 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
18778 74 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 t, /* The stat
18779 65 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 ement to bind ag
1877a 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ainst */. int i
1877b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1877c 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 /* Index of th
1877d 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 e parameter to b
1877e 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ind */. const v
1877f 6f 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 oid *zData,
18780 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
18781 65 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 e data to be bou
18782 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 nd */. int nDat
18783 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f a, /
18784 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
18785 73 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 s of data to be
18786 62 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 bound */. void
18787 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 (*xDel)(void*),
18788 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 /* Destructor
18789 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a for the data */.
1878a 20 20 69 6e 74 20 65 6e 63 6f 64 69 6e 67 20 20 int encoding
1878b 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f /* Enco
1878c 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 ding for the dat
1878d 61 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a a */.){. Vdbe *
1878e 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d p = (Vdbe *)pStm
1878f 74 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a t;. Mem *pVar;.
18790 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 int rc;.. rc
18791 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 = vdbeUnbind(p,
18792 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 i);. if( rc==SQ
18793 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 LITE_OK ){. i
18794 66 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20 f( zData!=0 ){.
18795 20 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e pVar = &p->
18796 61 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 aVar[i-1];.
18797 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
18798 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c eMemSetStr(pVar,
18799 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65 zData, nData, e
1879a 6e 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a ncoding, xDel);.
1879b 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1879c 4c 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 LITE_OK && encod
1879d 69 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ing!=0 ){.
1879e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1879f 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
187a0 28 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62 (pVar, ENC(p->db
187a1 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ));. }.
187a2 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 sqlite3Error(p
187a3 2d 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 ->db, rc, 0);.
187a4 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
187a5 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72 ApiExit(p->db, r
187a6 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 c);. }. sq
187a7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
187a8 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
187a9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
187aa 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 ;.}.../*.** Bind
187ab 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f a blob value to
187ac 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e an SQL statemen
187ad 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 t variable..*/.S
187ae 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
187af 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 lite3_bind_blob(
187b0 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
187b1 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 *pStmt, . int i
187b2 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
187b3 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e *zData, . int n
187b4 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a Data, . void (*
187b5 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a xDel)(void*).){.
187b6 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 return bindTex
187b7 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 t(pStmt, i, zDat
187b8 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 a, nData, xDel,
187b9 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 0);.}.SQLITE_API
187ba 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
187bb 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 d_double(sqlite3
187bc 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
187bd 74 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c t i, double rVal
187be 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ue){. int rc;.
187bf 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
187c0 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d *)pStmt;. rc =
187c1 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 vdbeUnbind(p, i
187c2 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
187c3 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 ITE_OK ){. sq
187c4 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 lite3VdbeMemSetD
187c5 6f 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69 ouble(&p->aVar[i
187c6 2d 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20 -1], rValue);.
187c7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
187c8 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 leave(p->db->mut
187c9 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ex);. }. retur
187ca 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 n rc;.}.SQLITE_A
187cb 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
187cc 69 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f ind_int(sqlite3_
187cd 73 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20 stmt *p, int i,
187ce 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72 int iValue){. r
187cf 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 eturn sqlite3_bi
187d0 6e 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28 nd_int64(p, i, (
187d1 69 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 i64)iValue);.}.S
187d2 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
187d3 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 lite3_bind_int64
187d4 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
187d5 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c Stmt, int i, sql
187d6 69 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65 ite_int64 iValue
187d7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 ){. int rc;. V
187d8 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a dbe *p = (Vdbe *
187d9 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 )pStmt;. rc = v
187da 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b dbeUnbind(p, i);
187db 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
187dc 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 E_OK ){. sqli
187dd 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 te3VdbeMemSetInt
187de 36 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 64(&p->aVar[i-1]
187df 2c 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 , iValue);. s
187e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
187e1 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
187e2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
187e3 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 c;.}.SQLITE_API
187e4 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
187e5 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 _null(sqlite3_st
187e6 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
187e7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 ){. int rc;. V
187e8 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 dbe *p = (Vdbe*)
187e9 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 pStmt;. rc = vd
187ea 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a beUnbind(p, i);.
187eb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
187ec 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
187ed 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
187ee 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
187ef 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
187f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
187f1 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
187f2 74 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 t( . sqlite3_st
187f3 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e mt *pStmt, . in
187f4 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 t i, . const ch
187f5 61 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e ar *zData, . in
187f6 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 t nData, . void
187f7 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a (*xDel)(void*).
187f8 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 ){. return bind
187f9 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a Text(pStmt, i, z
187fa 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 Data, nData, xDe
187fb 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b l, SQLITE_UTF8);
187fc 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
187fd 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
187fe 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
187ff 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 te3_bind_text16(
18800 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
18801 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 *pStmt, . int i
18802 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
18803 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e *zData, . int n
18804 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a Data, . void (*
18805 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a xDel)(void*).){.
18806 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 return bindTex
18807 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 t(pStmt, i, zDat
18808 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 a, nData, xDel,
18809 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 SQLITE_UTF16NATI
1880a 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a VE);.}.#endif /*
1880b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1880c 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 16 */.SQLITE_API
1880d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1880e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f d_value(sqlite3_
1880f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
18810 20 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 i, const sqlite
18811 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 3_value *pValue)
18812 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 {. int rc;. Vd
18813 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 be *p = (Vdbe *)
18814 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 pStmt;. rc = vd
18815 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a beUnbind(p, i);.
18816 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
18817 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 _OK ){. rc =
18818 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f sqlite3VdbeMemCo
18819 70 79 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d py(&p->aVar[i-1]
1881a 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 69 , pValue);. i
1881b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1881c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
1881d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1881e 45 6e 63 6f 64 69 6e 67 28 26 70 2d 3e 61 56 61 Encoding(&p->aVa
1881f 72 5b 69 2d 31 5d 2c 20 45 4e 43 28 70 2d 3e 64 r[i-1], ENC(p->d
18820 62 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 b));. }. s
18821 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
18822 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
18823 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c ;. }. rc = sql
18824 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 ite3ApiExit(p->d
18825 62 2c 20 72 63 29 3b 0a 20 20 72 65 74 75 72 6e b, rc);. return
18826 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 rc;.}.SQLITE_AP
18827 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
18828 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 nd_zeroblob(sqli
18829 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1882a 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29 7b 0a int i, int n){.
1882b 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 int rc;. Vdbe
1882c 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 *p = (Vdbe *)pS
1882d 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 tmt;. rc = vdbe
1882e 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 Unbind(p, i);.
1882f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
18830 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 K ){. sqlite3
18831 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c VdbeMemSetZeroBl
18832 6f 62 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d ob(&p->aVar[i-1]
18833 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , n);. sqlite
18834 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
18835 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d >db->mutex);. }
18836 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
18837 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
18838 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64 e number of wild
18839 63 61 72 64 73 20 74 68 61 74 20 63 61 6e 20 62 cards that can b
1883a 65 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f e potentially bo
1883b 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 73 20 und to..** This
1883c 72 6f 75 74 69 6e 65 20 69 73 20 61 64 64 65 64 routine is added
1883d 20 74 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a to support DBD:
1883e 3a 53 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a 53 51 :SQLite. .*/.SQ
1883f 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
18840 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
18841 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 ter_count(sqlite
18842 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 3_stmt *pStmt){.
18843 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 Vdbe *p = (Vdb
18844 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 e*)pStmt;. retu
18845 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61 72 20 3a rn p ? p->nVar :
18846 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 0;.}../*.** Cre
18847 61 74 65 20 61 20 6d 61 70 70 69 6e 67 20 66 72 ate a mapping fr
18848 6f 6d 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 om variable numb
18849 65 72 73 20 74 6f 20 76 61 72 69 61 62 6c 65 20 ers to variable
1884a 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 names.** in the
1884b 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72 Vdbe.azVar[] arr
1884c 61 79 2c 20 69 66 20 73 75 63 68 20 61 20 6d 61 ay, if such a ma
1884d 70 70 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 61 pping does not a
1884e 6c 72 65 61 64 79 0a 2a 2a 20 65 78 69 73 74 2e lready.** exist.
1884f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
18850 63 72 65 61 74 65 56 61 72 4d 61 70 28 56 64 62 createVarMap(Vdb
18851 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d e *p){. if( !p-
18852 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 73 71 >okVar ){. sq
18853 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
18854 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b r(p->db->mutex);
18855 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 6f 6b 56 . if( !p->okV
18856 61 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 ar ){. int
18857 6a 3b 0a 20 20 20 20 20 20 4f 70 20 2a 70 4f 70 j;. Op *pOp
18858 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c ;. for(j=0,
18859 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70 pOp=p->aOp; j<p
1885a 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b ->nOp; j++, pOp+
1885b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 +){. if(
1885c 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
1885d 56 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20 Variable ){.
1885e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1885f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e p->p1>0 && pOp->
18860 70 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 p1<=p->nVar );.
18861 20 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 p->azVa
18862 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70 r[pOp->p1-1] = p
18863 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 Op->p4.z;.
18864 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
18865 20 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 31 3b 0a p->okVar = 1;.
18866 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
18867 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
18868 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d >db->mutex);. }
18869 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1886a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77 the name of a w
1886b 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 ildcard paramete
1886c 72 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 r. Return NULL
1886d 69 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 if the index.**
1886e 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 is out of range
1886f 6f 72 20 69 66 20 74 68 65 20 77 69 6c 64 63 61 or if the wildca
18870 72 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a rd is unnamed..*
18871 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 *.** The result
18872 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e is always UTF-8.
18873 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
18874 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
18875 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
18876 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 r_name(sqlite3_s
18877 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
18878 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 i){. Vdbe *p =
18879 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 (Vdbe*)pStmt;.
1887a 69 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 if( p==0 || i<1
1887b 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a || i>p->nVar ){.
1887c 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1887d 7d 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 }. createVarMap
1887e 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d (p);. return p-
1887f 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a >azVar[i-1];.}..
18880 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69 /*.** Given a wi
18881 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 ldcard parameter
18882 20 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68 name, return th
18883 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 e index of the v
18884 61 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20 ariable.** with
18885 74 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74 that name. If t
18886 68 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61 here is no varia
18887 62 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76 ble with the giv
18888 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 en name,.** retu
18889 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rn 0..*/.SQLITE_
1888a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1888b 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 bind_parameter_i
1888c 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d ndex(sqlite3_stm
1888d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 t *pStmt, const
1888e 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 char *zName){.
1888f 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a Vdbe *p = (Vdbe*
18890 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b )pStmt;. int i;
18891 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 . if( p==0 ){.
18892 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
18893 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 . createVarMap(
18894 70 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65 p); . if( zName
18895 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ){. for(i=0;
18896 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 i<p->nVar; i++)
18897 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 {. const ch
18898 61 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 ar *z = p->azVar
18899 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a [i];. if( z
1889a 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 && strcmp(z,zNa
1889b 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 me)==0 ){.
1889c 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 return i+1;.
1889d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1889e 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
1889f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c *.** Transfer al
188a0 6c 20 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20 l bindings from
188a1 74 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d the first statem
188a2 65 6e 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20 ent over to the
188a3 73 65 63 6f 6e 64 2e 0a 2a 2a 20 49 66 20 74 68 second..** If th
188a4 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73 e two statements
188a5 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 65 contain a diffe
188a6 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 62 rent number of b
188a7 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a indings, then.**
188a8 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 an SQLITE_ERROR
188a9 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
188aa 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
188ab 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 int sqlite3Trans
188ac 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 ferBindings(sqli
188ad 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 te3_stmt *pFromS
188ae 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d tmt, sqlite3_stm
188af 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 t *pToStmt){. V
188b0 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 dbe *pFrom = (Vd
188b1 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 be*)pFromStmt;.
188b2 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 Vdbe *pTo = (Vd
188b3 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 be*)pToStmt;. i
188b4 6e 74 20 69 2c 20 72 63 20 3d 20 53 51 4c 49 54 nt i, rc = SQLIT
188b5 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 28 70 46 72 E_OK;. if( (pFr
188b6 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f om->magic!=VDBE_
188b7 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 70 46 72 MAGIC_RUN && pFr
188b8 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f om->magic!=VDBE_
188b9 4d 41 47 49 43 5f 48 41 4c 54 29 0a 20 20 20 20 MAGIC_HALT).
188ba 7c 7c 20 28 70 54 6f 2d 3e 6d 61 67 69 63 21 3d || (pTo->magic!=
188bb 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 VDBE_MAGIC_RUN &
188bc 26 20 70 54 6f 2d 3e 6d 61 67 69 63 21 3d 56 44 & pTo->magic!=VD
188bd 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 29 0a 20 BE_MAGIC_HALT).
188be 20 20 20 7c 7c 20 70 54 6f 2d 3e 64 62 21 3d 70 || pTo->db!=p
188bf 46 72 6f 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 From->db ){.
188c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
188c1 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 SUSE;. }. if(
188c2 70 46 72 6f 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f pFrom->nVar!=pTo
188c3 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72 65 ->nVar ){. re
188c4 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
188c5 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 R;. }. sqlite3
188c6 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 54 6f _mutex_enter(pTo
188c7 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
188c8 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c for(i=0; rc==SQL
188c9 49 54 45 5f 4f 4b 20 26 26 20 69 3c 70 46 72 6f ITE_OK && i<pFro
188ca 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 m->nVar; i++){.
188cb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
188cc 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61 72 mMove(&pTo->aVar
188cd 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56 61 [i], &pFrom->aVa
188ce 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c r[i]);. }. sql
188cf 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
188d0 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 (pTo->db->mutex)
188d1 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d ;. assert( rc==
188d2 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d SQLITE_OK || rc=
188d3 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
188d4 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
188d5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
188d6 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a OMIT_DEPRECATED.
188d7 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 /*.** Deprecated
188d8 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 external interf
188d9 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 ace. Internal/c
188da 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a ore SQLite code.
188db 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 ** should call s
188dc 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 qlite3TransferBi
188dd 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ndings..*/.SQLIT
188de 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
188df 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 3_transfer_bindi
188e0 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ngs(sqlite3_stmt
188e1 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c *pFromStmt, sql
188e2 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 ite3_stmt *pToSt
188e3 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 mt){. return sq
188e4 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e lite3TransferBin
188e5 64 69 6e 67 73 28 70 46 72 6f 6d 53 74 6d 74 2c dings(pFromStmt,
188e6 20 70 54 6f 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e pToStmt);.}.#en
188e7 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 dif../*.** Retur
188e8 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64 n the sqlite3* d
188e9 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 atabase handle t
188ea 6f 20 77 68 69 63 68 20 74 68 65 20 70 72 65 70 o which the prep
188eb 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67 ared statement g
188ec 69 76 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 61 iven.** in the a
188ed 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 2e rgument belongs.
188ee 20 20 54 68 69 73 20 69 73 20 74 68 65 20 73 61 This is the sa
188ef 6d 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 me database hand
188f0 6c 65 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 74 le that was.** t
188f1 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
188f2 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 t to the sqlite3
188f3 5f 70 72 65 70 61 72 65 28 29 20 74 68 61 74 20 _prepare() that
188f4 77 61 73 20 75 73 65 64 20 74 6f 20 63 72 65 61 was used to crea
188f5 74 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d te.** the statem
188f6 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 ent in the first
188f7 20 70 6c 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 place..*/.SQLIT
188f8 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 E_API sqlite3 *s
188f9 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 qlite3_db_handle
188fa 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
188fb 53 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 Stmt){. return
188fc 70 53 74 6d 74 20 3f 20 28 28 56 64 62 65 2a 29 pStmt ? ((Vdbe*)
188fd 70 53 74 6d 74 29 2d 3e 64 62 20 3a 20 30 3b 0a pStmt)->db : 0;.
188fe 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
188ff 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
18900 20 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73 next prepared s
18901 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 70 tatement after p
18902 53 74 6d 74 20 61 73 73 6f 63 69 61 74 65 64 0a Stmt associated.
18903 2a 2a 20 77 69 74 68 20 64 61 74 61 62 61 73 65 ** with database
18904 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62 2e connection pDb.
18905 20 20 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55 If pStmt is NU
18906 4c 4c 2c 20 72 65 74 75 72 6e 20 74 68 65 20 66 LL, return the f
18907 69 72 73 74 0a 2a 2a 20 70 72 65 70 61 72 65 64 irst.** prepared
18908 20 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 74 statement for t
18909 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
1890a 65 63 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 ection. Return
1890b 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 0a 2a 2a NULL if there.**
1890c 20 61 72 65 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f are no more..*/
1890d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
1890e 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69 74 65 te3_stmt *sqlite
1890f 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71 6c 69 3_next_stmt(sqli
18910 74 65 33 20 2a 70 44 62 2c 20 73 71 6c 69 74 65 te3 *pDb, sqlite
18911 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 3_stmt *pStmt){.
18912 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
18913 70 4e 65 78 74 3b 0a 20 20 73 71 6c 69 74 65 33 pNext;. sqlite3
18914 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44 62 _mutex_enter(pDb
18915 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 ->mutex);. if(
18916 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 pStmt==0 ){.
18917 70 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33 pNext = (sqlite3
18918 5f 73 74 6d 74 2a 29 70 44 62 2d 3e 70 56 64 62 _stmt*)pDb->pVdb
18919 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 e;. }else{.
1891a 70 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33 pNext = (sqlite3
1891b 5f 73 74 6d 74 2a 29 28 28 56 64 62 65 2a 29 70 _stmt*)((Vdbe*)p
1891c 53 74 6d 74 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 Stmt)->pNext;.
1891d 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
1891e 78 5f 6c 65 61 76 65 28 70 44 62 2d 3e 6d 75 74 x_leave(pDb->mut
1891f 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e ex);. return pN
18920 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ext;.}../*.** Re
18921 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f turn the value o
18922 66 20 61 20 73 74 61 74 75 73 20 63 6f 75 6e 74 f a status count
18923 65 72 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 er for a prepare
18924 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 d statement.*/.S
18925 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
18926 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 lite3_stmt_statu
18927 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a s(sqlite3_stmt *
18928 70 53 74 6d 74 2c 20 69 6e 74 20 6f 70 2c 20 69 pStmt, int op, i
18929 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 nt resetFlag){.
1892a 20 56 64 62 65 20 2a 70 56 64 62 65 20 3d 20 28 Vdbe *pVdbe = (
1892b 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 Vdbe*)pStmt;. i
1892c 6e 74 20 76 20 3d 20 70 56 64 62 65 2d 3e 61 43 nt v = pVdbe->aC
1892d 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 ounter[op-1];.
1892e 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 20 if( resetFlag )
1892f 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b pVdbe->aCounter[
18930 6f 70 2d 31 5d 20 3d 20 30 3b 0a 20 20 72 65 74 op-1] = 0;. ret
18931 75 72 6e 20 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a urn v;.}../*****
18932 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
18933 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a vdbeapi.c *****
18934 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18935 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18936 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
18937 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
18938 66 69 6c 65 20 76 64 62 65 2e 63 20 2a 2a 2a 2a file vdbe.c ****
18939 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1893a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1893b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
1893c 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
1893d 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
1893e 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
1893f 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
18940 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
18941 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
18942 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
18943 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
18944 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
18945 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
18946 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
18947 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
18948 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
18949 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1894a 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1894b 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1894c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1894d 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
1894e 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
1894f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18951 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18952 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18953 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 .** The code in
18954 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d this file implem
18955 65 6e 74 73 20 65 78 65 63 75 74 69 6f 6e 20 6d ents execution m
18956 65 74 68 6f 64 20 6f 66 20 74 68 65 20 0a 2a 2a ethod of the .**
18957 20 56 69 72 74 75 61 6c 20 44 61 74 61 62 61 73 Virtual Databas
18958 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 2e e Engine (VDBE).
18959 20 20 41 20 73 65 70 61 72 61 74 65 20 66 69 6c A separate fil
1895a 65 20 28 22 76 64 62 65 61 75 78 2e 63 22 29 0a e ("vdbeaux.c").
1895b 2a 2a 20 68 61 6e 64 6c 65 73 20 68 6f 75 73 65 ** handles house
1895c 6b 65 65 70 69 6e 67 20 64 65 74 61 69 6c 73 20 keeping details
1895d 73 75 63 68 20 61 73 20 63 72 65 61 74 69 6e 67 such as creating
1895e 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 0a 2a 2a and deleting.**
1895f 20 56 44 42 45 20 69 6e 73 74 61 6e 63 65 73 2e VDBE instances.
18960 20 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 73 This file is s
18961 6f 6c 65 6c 79 20 69 6e 74 65 72 65 73 74 65 64 olely interested
18962 20 69 6e 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a in executing.**
18963 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 the VDBE progra
18964 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 m..**.** In the
18965 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 external interfa
18966 63 65 2c 20 61 6e 20 22 73 71 6c 69 74 65 33 5f ce, an "sqlite3_
18967 73 74 6d 74 2a 22 20 69 73 20 61 6e 20 6f 70 61 stmt*" is an opa
18968 71 75 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 que pointer.** t
18969 6f 20 61 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 o a VDBE..**.**
1896a 54 68 65 20 53 51 4c 20 70 61 72 73 65 72 20 67 The SQL parser g
1896b 65 6e 65 72 61 74 65 73 20 61 20 70 72 6f 67 72 enerates a progr
1896c 61 6d 20 77 68 69 63 68 20 69 73 20 74 68 65 6e am which is then
1896d 20 65 78 65 63 75 74 65 64 20 62 79 0a 2a 2a 20 executed by.**
1896e 74 68 65 20 56 44 42 45 20 74 6f 20 64 6f 20 74 the VDBE to do t
1896f 68 65 20 77 6f 72 6b 20 6f 66 20 74 68 65 20 53 he work of the S
18970 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 56 QL statement. V
18971 44 42 45 20 70 72 6f 67 72 61 6d 73 20 61 72 65 DBE programs are
18972 20 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 69 6e 20 .** similar in
18973 66 6f 72 6d 20 74 6f 20 61 73 73 65 6d 62 6c 79 form to assembly
18974 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65 20 language. The
18975 70 72 6f 67 72 61 6d 20 63 6f 6e 73 69 73 74 73 program consists
18976 20 6f 66 0a 2a 2a 20 61 20 6c 69 6e 65 61 72 20 of.** a linear
18977 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 70 65 72 sequence of oper
18978 61 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 6f 70 ations. Each op
18979 65 72 61 74 69 6f 6e 20 68 61 73 20 61 6e 20 6f eration has an o
1897a 70 63 6f 64 65 20 0a 2a 2a 20 61 6e 64 20 35 20 pcode .** and 5
1897b 6f 70 65 72 61 6e 64 73 2e 20 20 4f 70 65 72 61 operands. Opera
1897c 6e 64 73 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 nds P1, P2, and
1897d 50 33 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e P3 are integers.
1897e 20 20 4f 70 65 72 61 6e 64 20 50 34 20 0a 2a 2a Operand P4 .**
1897f 20 69 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 is a null-termi
18980 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 20 20 4f nated string. O
18981 70 65 72 61 6e 64 20 50 35 20 69 73 20 61 6e 20 perand P5 is an
18982 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 unsigned charact
18983 65 72 2e 0a 2a 2a 20 46 65 77 20 6f 70 63 6f 64 er..** Few opcod
18984 65 73 20 75 73 65 20 61 6c 6c 20 35 20 6f 70 65 es use all 5 ope
18985 72 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d rands..**.** Com
18986 70 75 74 61 74 69 6f 6e 20 72 65 73 75 6c 74 73 putation results
18987 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 are stored on a
18988 20 73 65 74 20 6f 66 20 72 65 67 69 73 74 65 72 set of register
18989 73 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69 6e s numbered begin
1898a 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20 31 20 61 ning.** with 1 a
1898b 6e 64 20 67 6f 69 6e 67 20 75 70 20 74 6f 20 56 nd going up to V
1898c 64 62 65 2e 6e 4d 65 6d 2e 20 20 45 61 63 68 20 dbe.nMem. Each
1898d 72 65 67 69 73 74 65 72 20 63 61 6e 20 73 74 6f register can sto
1898e 72 65 0a 2a 2a 20 65 69 74 68 65 72 20 61 6e 20 re.** either an
1898f 69 6e 74 65 67 65 72 2c 20 61 20 6e 75 6c 6c 2d integer, a null-
18990 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e terminated strin
18991 67 2c 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f g, a floating po
18992 69 6e 74 0a 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f int.** number, o
18993 72 20 74 68 65 20 53 51 4c 20 22 4e 55 4c 4c 22 r the SQL "NULL"
18994 20 76 61 6c 75 65 2e 20 20 41 6e 20 69 6d 70 6c value. An impl
18995 69 63 69 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 icit conversion
18996 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 from one.** type
18997 20 74 6f 20 74 68 65 20 6f 74 68 65 72 20 6f 63 to the other oc
18998 63 75 72 73 20 61 73 20 6e 65 63 65 73 73 61 72 curs as necessar
18999 79 2e 0a 2a 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f y..** .** Most o
1899a 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 f the code in th
1899b 69 73 20 66 69 6c 65 20 69 73 20 74 61 6b 65 6e is file is taken
1899c 20 75 70 20 62 79 20 74 68 65 20 73 71 6c 69 74 up by the sqlit
1899d 65 33 56 64 62 65 45 78 65 63 28 29 0a 2a 2a 20 e3VdbeExec().**
1899e 66 75 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 64 function which d
1899f 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 oes the work of
189a0 69 6e 74 65 72 70 72 65 74 69 6e 67 20 61 20 56 interpreting a V
189a1 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 DBE program..**
189a2 42 75 74 20 6f 74 68 65 72 20 72 6f 75 74 69 6e But other routin
189a3 65 73 20 61 72 65 20 61 6c 73 6f 20 70 72 6f 76 es are also prov
189a4 69 64 65 64 20 74 6f 20 68 65 6c 70 20 69 6e 20 ided to help in
189a5 62 75 69 6c 64 69 6e 67 20 75 70 0a 2a 2a 20 61 building up.** a
189a6 20 70 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 program instruc
189a7 74 69 6f 6e 20 62 79 20 69 6e 73 74 72 75 63 74 tion by instruct
189a8 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f ion..**.** Vario
189a9 75 73 20 73 63 72 69 70 74 73 20 73 63 61 6e 20 us scripts scan
189aa 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 this source file
189ab 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e in order to gen
189ac 65 72 61 74 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f erate HTML.** do
189ad 63 75 6d 65 6e 74 61 74 69 6f 6e 2c 20 68 65 61 cumentation, hea
189ae 64 65 72 73 20 66 69 6c 65 73 2c 20 6f 72 20 6f ders files, or o
189af 74 68 65 72 20 64 65 72 69 76 65 64 20 66 69 6c ther derived fil
189b0 65 73 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 74 es. The formatt
189b1 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f ing.** of the co
189b2 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
189b3 69 73 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 69 is, therefore, i
189b4 6d 70 6f 72 74 61 6e 74 2e 20 20 53 65 65 20 6f mportant. See o
189b5 74 68 65 72 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a ther comments.**
189b6 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f in this file fo
189b7 72 20 64 65 74 61 69 6c 73 2e 20 20 49 66 20 69 r details. If i
189b8 6e 20 64 6f 75 62 74 2c 20 64 6f 20 6e 6f 74 20 n doubt, do not
189b9 64 65 76 69 61 74 65 20 66 72 6f 6d 20 65 78 69 deviate from exi
189ba 73 74 69 6e 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 sting.** comment
189bb 69 6e 67 20 61 6e 64 20 69 6e 64 65 6e 74 61 74 ing and indentat
189bc 69 6f 6e 20 70 72 61 63 74 69 63 65 73 20 77 68 ion practices wh
189bd 65 6e 20 63 68 61 6e 67 69 6e 67 20 6f 72 20 61 en changing or a
189be 64 64 69 6e 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a dding code..**.*
189bf 2a 20 24 49 64 3a 20 76 64 62 65 2e 63 2c 76 20 * $Id: vdbe.c,v
189c0 31 2e 37 38 38 20 32 30 30 38 2f 31 31 2f 31 37 1.788 2008/11/17
189c1 20 31 35 3a 33 31 3a 34 38 20 64 61 6e 69 65 6c 15:31:48 daniel
189c2 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
189c3 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
189c4 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 ing global varia
189c5 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 ble is increment
189c6 65 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 20 ed every time a
189c7 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c cursor.** moves,
189c8 20 65 69 74 68 65 72 20 62 79 20 74 68 65 20 4f either by the O
189c9 50 5f 4d 6f 76 65 58 58 2c 20 4f 50 5f 4e 65 78 P_MoveXX, OP_Nex
189ca 74 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70 t, or OP_Prev op
189cb 63 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 74 codes. The test
189cc 0a 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 75 .** procedures u
189cd 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 se this informat
189ce 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 ion to make sure
189cf 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72 that indices ar
189d0 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72 e.** working cor
189d1 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 61 rectly. This va
189d2 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 riable has no fu
189d3 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 nction other tha
189d4 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 n to.** help ver
189d5 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 ify the correct
189d6 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 operation of the
189d7 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 library..*/.#if
189d8 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
189d9 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
189da 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f qlite3_search_co
189db 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a unt = 0;.#endif.
189dc 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 ./*.** When this
189dd 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
189de 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 74 is positive, it
189df 20 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 65 gets decremente
189e0 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a d once before.**
189e1 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f each instructio
189e2 6e 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 n in the VDBE.
189e3 57 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65 72 When reaches zer
189e4 6f 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74 65 o, the u1.isInte
189e5 72 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64 rrupted.** field
189e6 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 20 of the sqlite3
189e7 73 74 72 75 63 74 75 72 65 20 69 73 20 73 65 74 structure is set
189e8 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 6d in order to sim
189e9 75 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72 72 ulate and interr
189ea 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 upt..**.** This
189eb 66 61 63 69 6c 69 74 79 20 69 73 20 75 73 65 64 facility is used
189ec 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 for testing pur
189ed 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 poses only. It
189ee 64 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f does not functio
189ef 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e n.** in an ordin
189f0 61 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 ary build..*/.#i
189f1 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
189f2 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
189f3 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
189f4 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e t_count = 0;.#en
189f5 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e dif../*.** The n
189f6 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 ext global varia
189f7 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 ble is increment
189f8 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68 65 ed each type the
189f9 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a OP_Sort opcode.
189fa 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 ** is executed.
189fb 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 The test proced
189fc 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e ures use this in
189fd 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b formation to mak
189fe 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 73 e sure that.** s
189ff 6f 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 72 orting is occurr
18a00 69 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72 ing or not occur
18a01 72 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69 ring at appropri
18a02 61 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69 ate times. Thi
18a03 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 s variable.** ha
18a04 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 s no function ot
18a05 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 her than to help
18a06 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 verify the corr
18a07 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 ect operation of
18a08 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e the.** library.
18a09 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
18a0a 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
18a0b 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f I int sqlite3_so
18a0c 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 rt_count = 0;.#e
18a0d 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
18a0e 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 next global vari
18a0f 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65 able records the
18a10 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 size of the lar
18a11 67 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a gest MEM_Blob.**
18a12 20 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 74 or MEM_Str that
18a13 20 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 has been used b
18a14 79 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 2e y a VDBE opcode.
18a15 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 The test proce
18a16 64 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 69 dures.** use thi
18a17 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f s information to
18a18 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
18a19 74 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 the zero-blob fu
18a1a 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 nctionality.** i
18a1b 73 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 s working correc
18a1c 74 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 69 tly. This vari
18a1d 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 able has no func
18a1e 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 tion other than
18a1f 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 to.** help verif
18a20 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 y the correct op
18a21 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c eration of the l
18a22 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 ibrary..*/.#ifde
18a23 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
18a24 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
18a25 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a ite3_max_blobsiz
18a26 65 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f e = 0;.static vo
18a27 69 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 id updateMaxBlob
18a28 73 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 size(Mem *p){.
18a29 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 if( (p->flags &
18a2a 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f (MEM_Str|MEM_Blo
18a2b 62 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 b))!=0 && p->n>s
18a2c 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 qlite3_max_blobs
18a2d 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ize ){. sqlit
18a2e 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 e3_max_blobsize
18a2f 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 = p->n;. }.}.#e
18a30 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 ndif../*.** Test
18a31 20 61 20 72 65 67 69 73 74 65 72 20 74 6f 20 73 a register to s
18a32 65 65 20 69 66 20 69 74 20 65 78 63 65 65 64 73 ee if it exceeds
18a33 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 the current max
18a34 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a imum blob size..
18a35 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 72 ** If it does, r
18a36 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d 61 ecord the new ma
18a37 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e ximum blob size.
18a38 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
18a39 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 SQLITE_TEST) &&
18a3a 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
18a3b 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 OMIT_BUILTIN_TES
18a3c 54 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41 T).# define UPDA
18a3d 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
18a3e 50 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c 6f P) updateMaxBlo
18a3f 62 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 bsize(P).#else.#
18a40 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d define UPDATE_M
18a41 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 AX_BLOBSIZE(P).#
18a42 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e endif../*.** Con
18a43 76 65 72 74 20 74 68 65 20 67 69 76 65 6e 20 72 vert the given r
18a44 65 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20 73 egister into a s
18a45 74 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e 27 tring if it isn'
18a46 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 t one.** already
18a47 2e 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 . Return non-zer
18a48 6f 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 o if a malloc()
18a49 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e fails..*/.#defin
18a4a 65 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20 65 e Stringify(P, e
18a4b 6e 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50 29 nc) \. if(((P)
18a4c 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 ->flags&(MEM_Str
18a4d 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 |MEM_Blob))==0 &
18a4e 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d & sqlite3VdbeMem
18a4f 53 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 Stringify(P,enc)
18a50 29 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 ) \. { goto
18a51 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a no_mem; }../*.**
18a52 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74 An ephemeral st
18a53 72 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67 6e ring value (sign
18a54 69 66 69 65 64 20 62 79 20 74 68 65 20 4d 45 4d ified by the MEM
18a55 5f 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e _Ephem flag) con
18a56 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 tains.** a point
18a57 65 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 er to a dynamica
18a58 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 lly allocated st
18a59 72 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65 20 ring where some
18a5a 6f 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 other entity.**
18a5b 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
18a5c 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 or deallocating
18a5d 74 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42 65 that string. Be
18a5e 63 61 75 73 65 20 74 68 65 20 72 65 67 69 73 74 cause the regist
18a5f 65 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 er.** does not c
18a60 6f 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69 6e ontrol the strin
18a61 67 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 g, it might be d
18a62 65 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20 74 eleted without t
18a63 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b he register.** k
18a64 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a nowing it..**.**
18a65 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f This routine co
18a66 6e 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d 65 nverts an epheme
18a67 72 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 ral string into
18a68 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c a dynamically al
18a69 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e located.** strin
18a6a 67 20 74 68 61 74 20 74 68 65 20 72 65 67 69 73 g that the regis
18a6b 74 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 ter itself contr
18a6c 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 ols. In other w
18a6d 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 ords, it.** conv
18a6e 65 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 erts an MEM_Ephe
18a6f 6d 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e m string into an
18a70 20 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e MEM_Dyn string.
18a71 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65 70 .*/.#define Deep
18a72 68 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a hemeralize(P) \.
18a73 20 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 if( ((P)->fla
18a74 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 gs&MEM_Ephem)!=0
18a75 20 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c \. && sql
18a76 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 ite3VdbeMemMakeW
18a77 72 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20 67 riteable(P) ){ g
18a78 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a oto no_mem;}../*
18a79 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 .** Call sqlite3
18a7a 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
18a7b 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c b() on the suppl
18a7c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 ied value (type
18a7d 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 Mem*).** P if re
18a7e 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 quired..*/.#defi
18a7f 6e 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 ne ExpandBlob(P)
18a80 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 (((P)->flags&ME
18a81 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 M_Zero)?sqlite3V
18a82 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
18a83 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 (P):0)../*.** Ar
18a84 67 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e gument pMem poin
18a85 74 73 20 61 74 20 61 20 72 65 67 69 73 74 65 72 ts at a register
18a86 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70 61 that will be pa
18a87 73 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 ssed to a.** use
18a88 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 r-defined functi
18a89 6f 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20 74 on or returned t
18a8a 6f 20 74 68 65 20 75 73 65 72 20 61 73 20 74 68 o the user as th
18a8b 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71 75 e result of a qu
18a8c 65 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f ery..** The seco
18a8d 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 27 64 62 nd argument, 'db
18a8e 5f 65 6e 63 27 20 69 73 20 74 68 65 20 74 65 78 _enc' is the tex
18a8f 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 t encoding used
18a90 62 79 20 74 68 65 20 76 64 62 65 20 66 6f 72 0a by the vdbe for.
18a91 2a 2a 20 72 65 67 69 73 74 65 72 20 76 61 72 69 ** register vari
18a92 61 62 6c 65 73 2e 20 20 54 68 69 73 20 72 6f 75 ables. This rou
18a93 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 4d tine sets the pM
18a94 65 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d 65 6d em->enc and pMem
18a95 2d 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69 61 62 ->type.** variab
18a96 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 les used by the
18a97 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 sqlite3_value_*(
18a98 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 ) routines..*/.#
18a99 64 65 66 69 6e 65 20 73 74 6f 72 65 54 79 70 65 define storeType
18a9a 49 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f 72 65 Info(A,B) _store
18a9b 54 79 70 65 49 6e 66 6f 28 41 29 0a 73 74 61 74 TypeInfo(A).stat
18a9c 69 63 20 76 6f 69 64 20 5f 73 74 6f 72 65 54 79 ic void _storeTy
18a9d 70 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d peInfo(Mem *pMem
18a9e 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d ){. int flags =
18a9f 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 pMem->flags;.
18aa0 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f if( flags & MEM_
18aa1 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d Null ){. pMem
18aa2 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
18aa3 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 NULL;. }. else
18aa4 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d if( flags & MEM
18aa5 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d _Int ){. pMem
18aa6 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
18aa7 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65 INTEGER;. }. e
18aa8 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 lse if( flags &
18aa9 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
18aaa 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
18aab 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 ITE_FLOAT;. }.
18aac 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 else if( flags
18aad 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 & MEM_Str ){.
18aae 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
18aaf 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c LITE_TEXT;. }el
18ab0 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 se{. pMem->ty
18ab1 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 pe = SQLITE_BLOB
18ab2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ;. }.}../*.** P
18ab3 72 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 63 roperties of opc
18ab4 6f 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 odes. The OPFLG
18ab5 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 _INITIALIZER mac
18ab6 72 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 ro is.** created
18ab7 20 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 by mkopcodeh.aw
18ab8 6b 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 k during compila
18ab9 74 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 6f tion. Data is o
18aba 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 btained.** from
18abb 74 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c the comments fol
18abc 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 lowing the "case
18abd 20 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 65 OP_xxxx:" state
18abe 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 69 73 ments in.** this
18abf 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 file. .*/.stat
18ac0 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 ic const unsigne
18ac1 64 20 63 68 61 72 20 6f 70 63 6f 64 65 50 72 6f d char opcodePro
18ac2 70 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f perty[] = OPFLG_
18ac3 49 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a INITIALIZER;../*
18ac4 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
18ac5 69 66 20 61 6e 20 6f 70 63 6f 64 65 20 68 61 73 if an opcode has
18ac6 20 61 6e 79 20 6f 66 20 74 68 65 20 4f 50 46 4c any of the OPFL
18ac7 47 5f 78 78 78 20 70 72 6f 70 65 72 74 69 65 73 G_xxx properties
18ac8 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 .** specified by
18ac9 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 mask..*/.SQLITE
18aca 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
18acb 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 ite3VdbeOpcodeHa
18acc 73 50 72 6f 70 65 72 74 79 28 69 6e 74 20 6f 70 sProperty(int op
18acd 63 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b 29 7b code, int mask){
18ace 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64 . assert( opcod
18acf 65 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c 28 69 e>0 && opcode<(i
18ad0 6e 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f 64 65 nt)sizeof(opcode
18ad1 50 72 6f 70 65 72 74 79 29 20 29 3b 0a 20 20 72 Property) );. r
18ad2 65 74 75 72 6e 20 28 6f 70 63 6f 64 65 50 72 6f eturn (opcodePro
18ad3 70 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26 6d 61 perty[opcode]&ma
18ad4 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a sk)!=0;.}../*.**
18ad5 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 75 Allocate VdbeCu
18ad6 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 rsor number iCur
18ad7 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e . Return a poin
18ad8 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74 75 ter to it. Retu
18ad9 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 rn NULL.** if we
18ada 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f run out of memo
18adb 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 64 ry..*/.static Vd
18adc 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 61 beCursor *alloca
18add 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62 65 teCursor(. Vdbe
18ade 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
18adf 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c /* The virtual
18ae0 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 6e machine */. in
18ae1 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 t iCur,
18ae2 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
18ae3 74 68 65 20 6e 65 77 20 56 64 62 65 43 75 72 73 the new VdbeCurs
18ae4 6f 72 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 2c or */. Op *pOp,
18ae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18ae6 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 */. int iDb,
18ae7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2a /* *
18ae8 2f 0a 20 20 69 6e 74 20 69 73 42 74 72 65 65 43 /. int isBtreeC
18ae9 75 72 73 6f 72 20 20 20 20 20 2f 2a 20 2a 2f 0a ursor /* */.
18aea 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 ){. /* Find the
18aeb 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 memory cell tha
18aec 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 t will be used t
18aed 6f 20 73 74 6f 72 65 20 74 68 65 20 62 6c 6f 62 o store the blob
18aee 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 of memory. **
18aef 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 required for thi
18af0 73 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 s VdbeCursor str
18af1 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 63 6f ucture. It is co
18af2 6e 76 65 6e 69 65 6e 74 20 74 6f 20 75 73 65 20 nvenient to use
18af3 61 20 0a 20 20 2a 2a 20 76 64 62 65 20 6d 65 6d a . ** vdbe mem
18af4 6f 72 79 20 63 65 6c 6c 20 74 6f 20 6d 61 6e 61 ory cell to mana
18af5 67 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ge the memory al
18af6 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 65 location require
18af7 64 20 66 6f 72 20 61 0a 20 20 2a 2a 20 56 64 62 d for a. ** Vdb
18af8 65 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 eCursor structur
18af9 65 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 e for the follow
18afa 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20 2a ing reasons:. *
18afb 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65 74 *. ** * Somet
18afc 69 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d 62 imes cursor numb
18afd 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 ers are used for
18afe 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 66 a couple of dif
18aff 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20 20 ferent. **
18b00 70 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76 64 purposes in a vd
18b01 62 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 20 be program. The
18b02 64 69 66 66 65 72 65 6e 74 20 75 73 65 73 20 6d different uses m
18b03 69 67 68 74 20 72 65 71 75 69 72 65 0a 20 20 2a ight require. *
18b04 2a 20 20 20 20 20 64 69 66 66 65 72 65 6e 74 20 * different
18b05 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e sized allocation
18b06 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 s. Memory cells
18b07 70 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c 65 provide growable
18b08 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63 61 . ** alloca
18b09 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
18b0a 20 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67 20 * When using
18b0b 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
18b0c 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72 79 NAGEMENT, memory
18b0d 20 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63 61 cell buffers ca
18b0e 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66 72 n. ** be fr
18b0f 65 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20 74 eed lazily via t
18b10 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 he sqlite3_relea
18b11 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49 2e se_memory() API.
18b12 20 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20 6d This. ** m
18b13 69 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75 6d inimizes the num
18b14 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63 61 ber of malloc ca
18b15 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 lls made by the
18b16 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a system.. **. *
18b17 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 * Memory cells f
18b18 6f 72 20 63 75 72 73 6f 72 73 20 61 72 65 20 61 or cursors are a
18b19 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65 20 llocated at the
18b1a 74 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72 65 top of the addre
18b1b 73 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20 4d ss. ** space. M
18b1c 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e emory cell (p->n
18b1d 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 Mem) corresponds
18b1e 20 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53 70 to cursor 0. Sp
18b1f 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75 72 ace for. ** cur
18b20 73 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65 64 sor 1 is managed
18b21 20 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 by memory cell
18b22 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74 63 (p->nMem-1), etc
18b23 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d .. */. Mem *pM
18b24 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 2d em = &p->aMem[p-
18b25 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20 20 >nMem-iCur];..
18b26 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 56 64 62 int nByte;. Vdb
18b27 65 43 75 72 73 6f 72 20 2a 70 43 78 20 3d 20 30 eCursor *pCx = 0
18b28 3b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 70 ;. /* If the op
18b29 63 6f 64 65 20 6f 66 20 70 4f 70 20 69 73 20 4f code of pOp is O
18b2a 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c P_SetNumColumns,
18b2b 20 74 68 65 6e 20 70 4f 70 2d 3e 70 32 20 63 6f then pOp->p2 co
18b2c 6e 74 61 69 6e 73 0a 20 20 2a 2a 20 74 68 65 20 ntains. ** the
18b2d 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 number of fields
18b2e 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73 20 in the records
18b2f 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 contained in the
18b30 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 69 table or. ** i
18b31 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 ndex being opene
18b32 64 2e 20 55 73 65 20 74 68 69 73 20 74 6f 20 72 d. Use this to r
18b33 65 73 65 72 76 65 20 73 70 61 63 65 20 66 6f 72 eserve space for
18b34 20 74 68 65 20 0a 20 20 2a 2a 20 56 64 62 65 43 the . ** VdbeC
18b35 75 72 73 6f 72 2e 61 54 79 70 65 5b 5d 20 61 72 ursor.aType[] ar
18b36 72 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 ray.. */. int
18b37 6e 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 69 66 nField = 0;. if
18b38 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f ( pOp->opcode==O
18b39 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 P_SetNumColumns
18b3a 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d || pOp->opcode==
18b3b 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
18b3c 20 29 7b 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d ){. nField =
18b3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 0a 20 20 pOp->p2;. }.
18b3e 6e 42 79 74 65 20 3d 20 0a 20 20 20 20 20 20 73 nByte = . s
18b3f 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 izeof(VdbeCursor
18b40 29 20 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74 ) + . (isBt
18b41 72 65 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 reeCursor?sqlite
18b42 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 3BtreeCursorSize
18b43 28 29 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32 ():0) + . 2
18b44 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 *nField*sizeof(u
18b45 33 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 32);.. assert(
18b46 69 43 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 iCur<p->nCursor
18b47 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 );. if( p->apCs
18b48 72 5b 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73 r[iCur] ){. s
18b49 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 qlite3VdbeFreeCu
18b4a 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 rsor(p, p->apCsr
18b4b 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e [iCur]);. p->
18b4c 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b apCsr[iCur] = 0;
18b4d 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 . }. if( SQLIT
18b4e 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 E_OK==sqlite3Vdb
18b4f 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e eMemGrow(pMem, n
18b50 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 Byte, 0) ){.
18b51 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d p->apCsr[iCur] =
18b52 20 70 43 78 20 3d 20 28 56 64 62 65 43 75 72 73 pCx = (VdbeCurs
18b53 6f 72 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 or*)pMem->z;.
18b54 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c memset(pMem->z,
18b55 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 0, nByte);.
18b56 70 43 78 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a pCx->iDb = iDb;.
18b57 20 20 20 20 70 43 78 2d 3e 6e 46 69 65 6c 64 20 pCx->nField
18b58 3d 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 69 66 = nField;. if
18b59 28 20 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 ( nField ){.
18b5a 20 20 70 43 78 2d 3e 61 54 79 70 65 20 3d 20 28 pCx->aType = (
18b5b 75 33 32 20 2a 29 26 70 4d 65 6d 2d 3e 7a 5b 73 u32 *)&pMem->z[s
18b5c 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 izeof(VdbeCursor
18b5d 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 )];. }. if
18b5e 28 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 ( isBtreeCursor
18b5f 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 43 ){. pCx->pC
18b60 75 72 73 6f 72 20 3d 20 28 42 74 43 75 72 73 6f ursor = (BtCurso
18b61 72 2a 29 0a 20 20 20 20 20 20 20 20 20 20 26 70 r*). &p
18b62 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 Mem->z[sizeof(Vd
18b63 62 65 43 75 72 73 6f 72 29 2b 32 2a 6e 46 69 65 beCursor)+2*nFie
18b64 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 5d 3b ld*sizeof(u32)];
18b65 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
18b66 75 72 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a urn pCx;.}../*.*
18b67 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 * Try to convert
18b68 20 61 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 a value into a
18b69 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e numeric represen
18b6a 74 61 74 69 6f 6e 20 69 66 20 77 65 20 63 61 6e tation if we can
18b6b 0a 2a 2a 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 .** do so withou
18b6c 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d t loss of inform
18b6d 61 74 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65 72 ation. In other
18b6e 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20 73 words, if the s
18b6f 74 72 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c tring.** looks l
18b70 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 63 6f ike a number, co
18b71 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 nvert it into a
18b72 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 20 64 number. If it d
18b73 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20 oes not.** look
18b74 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 6c like a number, l
18b75 65 61 76 65 20 69 74 20 61 6c 6f 6e 65 2e 0a 2a eave it alone..*
18b76 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 /.static void ap
18b77 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 plyNumericAffini
18b78 74 79 28 4d 65 6d 20 2a 70 52 65 63 29 7b 0a 20 ty(Mem *pRec){.
18b79 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 if( (pRec->flag
18b7a 73 20 26 20 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 s & (MEM_Real|ME
18b7b 4d 5f 49 6e 74 29 29 3d 3d 30 20 29 7b 0a 20 20 M_Int))==0 ){.
18b7c 20 20 69 6e 74 20 72 65 61 6c 6e 75 6d 3b 0a 20 int realnum;.
18b7d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
18b7e 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 52 mNulTerminate(pR
18b7f 65 63 29 3b 0a 20 20 20 20 69 66 28 20 28 70 52 ec);. if( (pR
18b80 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 ec->flags&MEM_St
18b81 72 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 73 r). && s
18b82 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 70 qlite3IsNumber(p
18b83 52 65 63 2d 3e 7a 2c 20 26 72 65 61 6c 6e 75 6d Rec->z, &realnum
18b84 2c 20 70 52 65 63 2d 3e 65 6e 63 29 20 29 7b 0a , pRec->enc) ){.
18b85 20 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b i64 value;
18b86 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
18b87 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
18b88 28 70 52 65 63 2c 20 53 51 4c 49 54 45 5f 55 54 (pRec, SQLITE_UT
18b89 46 38 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 F8);. if( !
18b8a 72 65 61 6c 6e 75 6d 20 26 26 20 73 71 6c 69 74 realnum && sqlit
18b8b 65 33 41 74 6f 69 36 34 28 70 52 65 63 2d 3e 7a e3Atoi64(pRec->z
18b8c 2c 20 26 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 , &value) ){.
18b8d 20 20 20 20 20 70 52 65 63 2d 3e 75 2e 69 20 3d pRec->u.i =
18b8e 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 value;.
18b8f 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
18b90 52 65 63 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 Rec, MEM_Int);.
18b91 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
18b92 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
18b93 65 6d 52 65 61 6c 69 66 79 28 70 52 65 63 29 3b emRealify(pRec);
18b94 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
18b95 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 }.}../*.** Proc
18b96 65 73 73 69 6e 67 20 69 73 20 64 65 74 65 72 6d essing is determ
18b97 69 6e 65 20 62 79 20 74 68 65 20 61 66 66 69 6e ine by the affin
18b98 69 74 79 20 70 61 72 61 6d 65 74 65 72 3a 0a 2a ity parameter:.*
18b99 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f *.** SQLITE_AFF_
18b9a 49 4e 54 45 47 45 52 3a 0a 2a 2a 20 53 51 4c 49 INTEGER:.** SQLI
18b9b 54 45 5f 41 46 46 5f 52 45 41 4c 3a 0a 2a 2a 20 TE_AFF_REAL:.**
18b9c 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 SQLITE_AFF_NUMER
18b9d 49 43 3a 0a 2a 2a 20 20 20 20 54 72 79 20 74 6f IC:.** Try to
18b9e 20 63 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f convert pRec to
18b9f 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 an integer repr
18ba0 65 73 65 6e 74 61 74 69 6f 6e 20 6f 72 20 61 20 esentation or a
18ba1 0a 2a 2a 20 20 20 20 66 6c 6f 61 74 69 6e 67 2d .** floating-
18ba2 70 6f 69 6e 74 20 72 65 70 72 65 73 65 6e 74 61 point representa
18ba3 74 69 6f 6e 20 69 66 20 61 6e 20 69 6e 74 65 67 tion if an integ
18ba4 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f er representatio
18ba5 6e 0a 2a 2a 20 20 20 20 69 73 20 6e 6f 74 20 70 n.** is not p
18ba6 6f 73 73 69 62 6c 65 2e 20 20 4e 6f 74 65 20 74 ossible. Note t
18ba7 68 61 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 hat the integer
18ba8 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 representation i
18ba9 73 0a 2a 2a 20 20 20 20 61 6c 77 61 79 73 20 70 s.** always p
18baa 72 65 66 65 72 72 65 64 2c 20 65 76 65 6e 20 69 referred, even i
18bab 66 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 69 f the affinity i
18bac 73 20 52 45 41 4c 2c 20 62 65 63 61 75 73 65 0a s REAL, because.
18bad 2a 2a 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72 ** an integer
18bae 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
18baf 69 73 20 6d 6f 72 65 20 73 70 61 63 65 20 65 66 is more space ef
18bb0 66 69 63 69 65 6e 74 20 6f 6e 20 64 69 73 6b 2e ficient on disk.
18bb1 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 .**.** SQLITE_AF
18bb2 46 5f 54 45 58 54 3a 0a 2a 2a 20 20 20 20 43 6f F_TEXT:.** Co
18bb3 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20 61 20 nvert pRec to a
18bb4 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 text representat
18bb5 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 ion..**.** SQLIT
18bb6 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 2a 2a 20 20 E_AFF_NONE:.**
18bb7 20 20 4e 6f 2d 6f 70 2e 20 20 70 52 65 63 20 69 No-op. pRec i
18bb8 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a s unchanged..*/.
18bb9 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c static void appl
18bba 79 41 66 66 69 6e 69 74 79 28 0a 20 20 4d 65 6d yAffinity(. Mem
18bbb 20 2a 70 52 65 63 2c 20 20 20 20 20 20 20 20 20 *pRec,
18bbc 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 74 6f /* The value to
18bbd 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 apply affinity
18bbe 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 to */. char aff
18bbf 69 6e 69 74 79 2c 20 20 20 20 20 20 2f 2a 20 54 inity, /* T
18bc0 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 he affinity to b
18bc1 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 75 e applied */. u
18bc2 38 20 65 6e 63 20 20 20 20 20 20 20 20 20 20 20 8 enc
18bc3 20 20 20 2f 2a 20 55 73 65 20 74 68 69 73 20 74 /* Use this t
18bc4 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a ext encoding */.
18bc5 29 7b 0a 20 20 69 66 28 20 61 66 66 69 6e 69 74 ){. if( affinit
18bc6 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 y==SQLITE_AFF_TE
18bc7 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e 6c XT ){. /* Onl
18bc8 79 20 61 74 74 65 6d 70 74 20 74 68 65 20 63 6f y attempt the co
18bc9 6e 76 65 72 73 69 6f 6e 20 74 6f 20 54 45 58 54 nversion to TEXT
18bca 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 if there is an
18bcb 69 6e 74 65 67 65 72 20 6f 72 20 72 65 61 6c 0a integer or real.
18bcc 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 ** represent
18bcd 61 74 69 6f 6e 20 28 62 6c 6f 62 20 61 6e 64 20 ation (blob and
18bce 4e 55 4c 4c 20 64 6f 20 6e 6f 74 20 67 65 74 20 NULL do not get
18bcf 63 6f 6e 76 65 72 74 65 64 29 20 62 75 74 20 6e converted) but n
18bd0 6f 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 o string. **
18bd1 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a representation..
18bd2 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 30 */. if( 0
18bd3 3d 3d 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d ==(pRec->flags&M
18bd4 45 4d 5f 53 74 72 29 20 26 26 20 28 70 52 65 63 EM_Str) && (pRec
18bd5 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 52 65 61 ->flags&(MEM_Rea
18bd6 6c 7c 4d 45 4d 5f 49 6e 74 29 29 20 29 7b 0a 20 l|MEM_Int)) ){.
18bd7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
18bd8 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70 52 65 MemStringify(pRe
18bd9 63 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 c, enc);. }.
18bda 20 20 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 pRec->flags &
18bdb 3d 20 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d = ~(MEM_Real|MEM
18bdc 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 _Int);. }else i
18bdd 66 28 20 61 66 66 69 6e 69 74 79 21 3d 53 51 4c f( affinity!=SQL
18bde 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a ITE_AFF_NONE ){.
18bdf 20 20 20 20 61 73 73 65 72 74 28 20 61 66 66 69 assert( affi
18be0 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 nity==SQLITE_AFF
18be1 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 69 _INTEGER || affi
18be2 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 nity==SQLITE_AFF
18be3 5f 52 45 41 4c 0a 20 20 20 20 20 20 20 20 20 20 _REAL.
18be4 20 20 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d || affinity==
18be5 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 SQLITE_AFF_NUMER
18be6 49 43 20 29 3b 0a 20 20 20 20 61 70 70 6c 79 4e IC );. applyN
18be7 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 umericAffinity(p
18be8 52 65 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52 Rec);. if( pR
18be9 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f ec->flags & MEM_
18bea 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 Real ){. sq
18beb 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 lite3VdbeInteger
18bec 41 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a Affinity(pRec);.
18bed 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
18bee 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 ** Try to conver
18bef 74 20 74 68 65 20 74 79 70 65 20 6f 66 20 61 20 t the type of a
18bf0 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
18bf1 74 20 6f 72 20 61 20 72 65 73 75 6c 74 20 63 6f t or a result co
18bf2 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e lumn.** into a n
18bf3 75 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 umeric represent
18bf4 61 74 69 6f 6e 2e 20 20 55 73 65 20 65 69 74 68 ation. Use eith
18bf5 65 72 20 49 4e 54 45 47 45 52 20 6f 72 20 52 45 er INTEGER or RE
18bf6 41 4c 20 77 68 69 63 68 65 76 65 72 0a 2a 2a 20 AL whichever.**
18bf7 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 is appropriate.
18bf8 20 42 75 74 20 6f 6e 6c 79 20 64 6f 20 74 68 65 But only do the
18bf9 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 20 69 conversion if i
18bfa 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 t is possible wi
18bfb 74 68 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20 6f 66 thout.** loss of
18bfc 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 information and
18bfd 20 72 65 74 75 72 6e 20 74 68 65 20 72 65 76 69 return the revi
18bfe 73 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 sed type of the
18bff 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
18c00 54 68 69 73 20 69 73 20 61 6e 20 45 58 50 45 52 This is an EXPER
18c01 49 4d 45 4e 54 41 4c 20 61 70 69 20 61 6e 64 20 IMENTAL api and
18c02 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 is subject to ch
18c03 61 6e 67 65 20 6f 72 20 72 65 6d 6f 76 61 6c 2e ange or removal.
18c04 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
18c05 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt sqlite3_value
18c06 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 _numeric_type(sq
18c07 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
18c08 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 l){. Mem *pMem
18c09 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 = (Mem*)pVal;.
18c0a 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 applyNumericAffi
18c0b 6e 69 74 79 28 70 4d 65 6d 29 3b 0a 20 20 73 74 nity(pMem);. st
18c0c 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 4d 65 6d oreTypeInfo(pMem
18c0d 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 , 0);. return p
18c0e 4d 65 6d 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a Mem->type;.}../*
18c0f 0a 2a 2a 20 45 78 70 6f 72 74 65 64 20 76 65 72 .** Exported ver
18c10 73 69 6f 6e 20 6f 66 20 61 70 70 6c 79 41 66 66 sion of applyAff
18c11 69 6e 69 74 79 28 29 2e 20 54 68 69 73 20 6f 6e inity(). This on
18c12 65 20 77 6f 72 6b 73 20 6f 6e 20 73 71 6c 69 74 e works on sqlit
18c13 65 33 5f 76 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e e3_value*, .** n
18c14 6f 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ot the internal
18c15 4d 65 6d 2a 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 Mem* type..*/.SQ
18c16 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
18c17 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 d sqlite3ValueAp
18c18 70 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20 73 plyAffinity(. s
18c19 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
18c1a 61 6c 2c 20 0a 20 20 75 38 20 61 66 66 69 6e 69 al, . u8 affini
18c1b 74 79 2c 20 0a 20 20 75 38 20 65 6e 63 0a 29 7b ty, . u8 enc.){
18c1c 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 . applyAffinity
18c1d 28 28 4d 65 6d 20 2a 29 70 56 61 6c 2c 20 61 66 ((Mem *)pVal, af
18c1e 66 69 6e 69 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a finity, enc);.}.
18c1f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
18c20 45 42 55 47 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 EBUG./*.** Write
18c21 20 61 20 6e 69 63 65 20 73 74 72 69 6e 67 20 72 a nice string r
18c22 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
18c23 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
18c24 20 63 65 6c 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e cell pMem.** in
18c25 74 6f 20 62 75 66 66 65 72 20 7a 42 75 66 2c 20 to buffer zBuf,
18c26 6c 65 6e 67 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a length nBuf..*/.
18c27 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
18c28 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
18c29 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65 emPrettyPrint(Me
18c2a 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a m *pMem, char *z
18c2b 42 75 66 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 Buf){. char *zC
18c2c 73 72 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 sr = zBuf;. int
18c2d 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 f = pMem->flags
18c2e 3b 0a 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ;.. static cons
18c2f 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 65 6e t char *const en
18c30 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 22 28 58 29 cnames[] = {"(X)
18c31 22 2c 20 22 28 38 29 22 2c 20 22 28 31 36 4c 45 ", "(8)", "(16LE
18c32 29 22 2c 20 22 28 31 36 42 45 29 22 7d 3b 0a 0a )", "(16BE)"};..
18c33 20 20 69 66 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 if( f&MEM_Blob
18c34 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
18c35 20 20 20 63 68 61 72 20 63 3b 0a 20 20 20 20 69 char c;. i
18c36 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29 f( f & MEM_Dyn )
18c37 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 7a 27 3b {. c = 'z';
18c38 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
18c39 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c f & (MEM_Static|
18c3a 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 MEM_Ephem))==0 )
18c3b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
18c3c 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 f & MEM_Static )
18c3d 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 74 27 3b {. c = 't';
18c3e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 . assert( (
18c3f 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d f & (MEM_Dyn|MEM
18c40 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 _Ephem))==0 );.
18c41 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 }else if( f &
18c42 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 MEM_Ephem ){.
18c43 20 20 20 20 63 20 3d 20 27 65 27 3b 0a 20 20 20 c = 'e';.
18c44 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
18c45 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f (MEM_Static|MEM_
18c46 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 Dyn))==0 );.
18c47 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d }else{. c =
18c48 20 27 73 27 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 's';. }..
18c49 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
18c4a 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 63 f(100, zCsr, "%c
18c4b 22 2c 20 63 29 3b 0a 20 20 20 20 7a 43 73 72 20 ", c);. zCsr
18c4c 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 29 3b += strlen(zCsr);
18c4d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
18c4e 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c rintf(100, zCsr,
18c4f 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29 "%d[", pMem->n)
18c50 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 ;. zCsr += st
18c51 72 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 rlen(zCsr);.
18c52 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 for(i=0; i<16 &&
18c53 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 i<pMem->n; i++)
18c54 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
18c55 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 snprintf(100, zC
18c56 73 72 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e sr, "%02X", ((in
18c57 74 29 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 t)pMem->z[i] & 0
18c58 78 46 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 xFF));. zCs
18c59 72 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 r += strlen(zCsr
18c5a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
18c5b 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c (i=0; i<16 && i<
18c5c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 pMem->n; i++){.
18c5d 20 20 20 20 20 63 68 61 72 20 7a 20 3d 20 70 4d char z = pM
18c5e 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 em->z[i];.
18c5f 69 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e 31 32 if( z<32 || z>12
18c60 36 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 27 2e 6 ) *zCsr++ = '.
18c61 27 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 2a 7a ';. else *z
18c62 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 20 7d Csr++ = z;. }
18c63 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e .. sqlite3_sn
18c64 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 printf(100, zCsr
18c65 2c 20 22 5d 25 73 22 2c 20 65 6e 63 6e 61 6d 65 , "]%s", encname
18c66 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 s[pMem->enc]);.
18c67 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72 6c 65 zCsr += strle
18c68 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 n(zCsr);. if(
18c69 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b f & MEM_Zero ){
18c6a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
18c6b 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 nprintf(100, zCs
18c6c 72 2c 22 2b 25 6c 6c 64 7a 22 2c 70 4d 65 6d 2d r,"+%lldz",pMem-
18c6d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7a 43 73 >u.i);. zCs
18c6e 72 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 r += strlen(zCsr
18c6f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 43 );. }. *zC
18c70 73 72 20 3d 20 27 5c 30 27 3b 0a 20 20 7d 65 6c sr = '\0';. }el
18c71 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 se if( f & MEM_S
18c72 74 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 2c tr ){. int j,
18c73 20 6b 3b 0a 20 20 20 20 7a 42 75 66 5b 30 5d 20 k;. zBuf[0]
18c74 3d 20 27 20 27 3b 0a 20 20 20 20 69 66 28 20 66 = ' ';. if( f
18c75 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 & MEM_Dyn ){.
18c76 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 7a zBuf[1] = 'z
18c77 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ';. assert(
18c78 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 (f & (MEM_Stati
18c79 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 c|MEM_Ephem))==0
18c7a 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 );. }else if
18c7b 28 20 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 ( f & MEM_Static
18c7c 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 ){. zBuf[1
18c7d 5d 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61 ] = 't';. a
18c7e 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d ssert( (f & (MEM
18c7f 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 _Dyn|MEM_Ephem))
18c80 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 ==0 );. }else
18c81 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 if( f & MEM_Eph
18c82 65 6d 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 em ){. zBuf
18c83 5b 31 5d 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 [1] = 'e';.
18c84 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d assert( (f & (M
18c85 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 EM_Static|MEM_Dy
18c86 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 n))==0 );. }e
18c87 6c 73 65 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b lse{. zBuf[
18c88 31 5d 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d 0a 1] = 's';. }.
18c89 20 20 20 20 6b 20 3d 20 32 3b 0a 20 20 20 20 73 k = 2;. s
18c8a 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
18c8b 31 30 30 2c 20 26 7a 42 75 66 5b 6b 5d 2c 20 22 100, &zBuf[k], "
18c8c 25 64 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 %d", pMem->n);.
18c8d 20 20 20 6b 20 2b 3d 20 73 74 72 6c 65 6e 28 26 k += strlen(&
18c8e 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 zBuf[k]);. zB
18c8f 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a 20 uf[k++] = '[';.
18c90 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35 for(j=0; j<15
18c91 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a && j<pMem->n; j
18c92 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 63 20 ++){. u8 c
18c93 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 = pMem->z[j];.
18c94 20 20 20 20 69 66 28 20 63 3e 3d 30 78 32 30 20 if( c>=0x20
18c95 26 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 20 20 && c<0x7f ){.
18c96 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d zBuf[k++] =
18c97 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b c;. }else{
18c98 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b . zBuf[k+
18c99 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 +] = '.';.
18c9a 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 }. }. zBuf
18c9b 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 20 [k++] = ']';.
18c9c 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
18c9d 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c 20 f(100,&zBuf[k],
18c9e 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 encnames[pMem->e
18c9f 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 nc]);. k += s
18ca0 74 72 6c 65 6e 28 26 7a 42 75 66 5b 6b 5d 29 3b trlen(&zBuf[k]);
18ca1 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d . zBuf[k++] =
18ca2 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0;. }.}.#endif
18ca3 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
18ca4 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e DEBUG./*.** Prin
18ca5 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 t the value of a
18ca6 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 72 register for tr
18ca7 61 63 69 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a acing purposes:.
18ca8 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
18ca9 65 6d 54 72 61 63 65 50 72 69 6e 74 28 46 49 4c emTracePrint(FIL
18caa 45 20 2a 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b E *out, Mem *p){
18cab 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 . if( p->flags
18cac 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
18cad 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
18cae 20 4e 55 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 NULL");. }else
18caf 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 if( (p->flags &
18cb0 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 (MEM_Int|MEM_St
18cb1 72 29 29 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 r))==(MEM_Int|ME
18cb2 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 66 70 M_Str) ){. fp
18cb3 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 73 69 3a rintf(out, " si:
18cb4 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a %lld", p->u.i);.
18cb5 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 }else if( p->f
18cb6 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 lags & MEM_Int )
18cb7 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 {. fprintf(ou
18cb8 74 2c 20 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d t, " i:%lld", p-
18cb9 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20 69 >u.i);. }else i
18cba 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 f( p->flags & ME
18cbb 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70 M_Real ){. fp
18cbc 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25 rintf(out, " r:%
18cbd 67 22 2c 20 70 2d 3e 72 29 3b 0a 20 20 7d 65 6c g", p->r);. }el
18cbe 73 65 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 se{. char zBu
18cbf 66 5b 32 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 f[200];. sqli
18cc0 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 te3VdbeMemPretty
18cc1 50 72 69 6e 74 28 70 2c 20 7a 42 75 66 29 3b 0a Print(p, zBuf);.
18cc2 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
18cc3 20 22 20 22 29 3b 0a 20 20 20 20 66 70 72 69 6e " ");. fprin
18cc4 74 66 28 6f 75 74 2c 20 22 25 73 22 2c 20 7a 42 tf(out, "%s", zB
18cc5 75 66 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 uf);. }.}.stati
18cc6 63 20 76 6f 69 64 20 72 65 67 69 73 74 65 72 54 c void registerT
18cc7 72 61 63 65 28 46 49 4c 45 20 2a 6f 75 74 2c 20 race(FILE *out,
18cc8 69 6e 74 20 69 52 65 67 2c 20 4d 65 6d 20 2a 70 int iReg, Mem *p
18cc9 29 7b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 ){. fprintf(out
18cca 2c 20 22 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20 , "REG[%d] = ",
18ccb 69 52 65 67 29 3b 0a 20 20 6d 65 6d 54 72 61 63 iReg);. memTrac
18ccc 65 50 72 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0a ePrint(out, p);.
18ccd 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
18cce 5c 6e 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a \n");.}.#endif..
18ccf 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
18cd0 42 55 47 0a 23 20 20 64 65 66 69 6e 65 20 52 45 BUG.# define RE
18cd1 47 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d GISTER_TRACE(R,M
18cd2 29 20 69 66 28 70 2d 3e 74 72 61 63 65 29 72 65 ) if(p->trace)re
18cd3 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 gisterTrace(p->t
18cd4 72 61 63 65 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a race,R,M).#else.
18cd5 23 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 # define REGIST
18cd6 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 0a 23 65 ER_TRACE(R,M).#e
18cd7 6e 64 69 66 0a 0a 0a 23 69 66 64 65 66 20 56 44 ndif...#ifdef VD
18cd8 42 45 5f 50 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a BE_PROFILE../* .
18cd9 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 ** hwtime.h cont
18cda 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 ains inline asse
18cdb 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 mbler code for i
18cdc 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 mplementing .**
18cdd 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 high-performance
18cde 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 timing routines
18cdf 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..*/./**********
18ce0 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 **** Include hwt
18ce1 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 ime.h in the mid
18ce2 64 6c 65 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a dle of vdbe.c **
18ce3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ce4 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
18ce5 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
18ce6 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a hwtime.h *******
18ce7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ce8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ce9 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
18cea 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 May 27.**.** The
18ceb 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
18cec 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
18ced 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
18cee 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
18cef 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
18cf0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
18cf1 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
18cf2 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
18cf3 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
18cf4 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
18cf5 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
18cf6 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
18cf7 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
18cf8 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
18cf9 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
18cfa 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
18cfb 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
18cfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18cfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18cfe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18cff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
18d01 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
18d02 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 ins inline asm c
18d03 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 ode for retrievi
18d04 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d ng "high-perform
18d05 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 ance".** counter
18d06 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 s for x86 class
18d07 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a CPUs..**.** $Id:
18d08 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 hwtime.h,v 1.3
18d09 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 2008/08/01 14:33
18d0a 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a :15 shane Exp $.
18d0b 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 */.#ifndef _HWTI
18d0c 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 ME_H_.#define _H
18d0d 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 WTIME_H_../*.**
18d0e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
18d0f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 utine only works
18d10 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 on pentium-clas
18d11 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f s (or newer) pro
18d12 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 cessors..** It u
18d13 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 ses the RDTSC op
18d14 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 code to read the
18d15 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c cycle count val
18d16 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a ue out of the.**
18d17 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 processor and r
18d18 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 eturns that valu
18d19 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 e. This can be
18d1a 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 used for high-re
18d1b 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a s.** profiling..
18d1c 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 */.#if (defined(
18d1d 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 __GNUC__) || def
18d1e 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 ined(_MSC_VER))
18d1f 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 && \. (defi
18d20 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 ned(i386) || def
18d21 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c ined(__i386__) |
18d22 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 | defined(_M_IX8
18d23 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 6)).. #if defin
18d24 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 ed(__GNUC__)..
18d25 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
18d26 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
18d27 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
18d28 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
18d29 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 lo, hi;. __a
18d2a 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
18d2b 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 _ ("rdtsc" : "=a
18d2c 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 " (lo), "=d" (hi
18d2d 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 ));. return
18d2e 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 (sqlite_uint64)h
18d2f 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 i << 32 | lo;.
18d30 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e }.. #elif defin
18d31 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 ed(_MSC_VER)..
18d32 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 __declspec(naked
18d33 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 ) __inline sqlit
18d34 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c e_uint64 __cdecl
18d35 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
18d36 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d oid){. __asm
18d37 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 {. rdtsc
18d38 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 . ret
18d39 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 ; return valu
18d3a 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 e at EDX:EAX.
18d3b 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 }. }.. #endi
18d3c 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 f..#elif (define
18d3d 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 d(__GNUC__) && d
18d3e 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f efined(__x86_64_
18d3f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f _)).. __inline_
18d40 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
18d41 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
18d42 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 id){. unsig
18d43 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 ned long val;.
18d44 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f __asm__ __vo
18d45 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 latile__ ("rdtsc
18d46 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b " : "=A" (val));
18d47 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 . return va
18d48 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 l;. }. .#elif (
18d49 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
18d4a 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 ) && defined(__p
18d4b 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 pc__)).. __inli
18d4c 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 ne__ sqlite_uint
18d4d 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
18d4e 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e (void){. un
18d4f 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
18d50 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 retval;. u
18d51 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e nsigned long jun
18d52 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f k;. __asm__
18d53 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 __volatile__ ("
18d54 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a \n\. 1:
18d55 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 mftbu %1
18d56 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
18d57 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c mftb %L
18d58 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 0\n\.
18d59 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 mftbu %
18d5a 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 0\n\.
18d5b 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 cmpw %
18d5c 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 0,%1\n\.
18d5d 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 bne
18d5e 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 1b".
18d5f 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 : "=r" (
18d60 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a retval), "=r" (j
18d61 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 unk));. ret
18d62 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a urn retval;. }.
18d63 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 .#else.. #error
18d64 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 Need implementa
18d65 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 tion of sqlite3H
18d66 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 wtime() for your
18d67 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a platform... /*
18d68 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 . ** To compile
18d69 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 without impleme
18d6a 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 nting sqlite3Hwt
18d6b 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 ime() for your p
18d6c 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f latform,. ** yo
18d6d 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 u can remove the
18d6e 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e above #error an
18d6f 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 d use the follow
18d70 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 ing. ** stub fu
18d71 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c nction. You wil
18d72 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 l lose timing su
18d73 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 pport for many.
18d74 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 ** of the debug
18d75 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 ging and testing
18d76 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 utilities, but
18d77 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a it should at. *
18d78 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 * least compile
18d79 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 and run.. */.SQ
18d7a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 LITE_PRIVATE s
18d7b 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
18d7c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
18d7d 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 { return ((sqlit
18d7e 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a e_uint64)0); }..
18d7f 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f #endif..#endif /
18d80 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 * !defined(_HWTI
18d81 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a ME_H_) */../****
18d82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
18d83 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a f hwtime.h *****
18d84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18d85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18d86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
18d87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
18d88 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
18d89 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e eft off in vdbe.
18d8a 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
18d8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 *********/..#end
18d8c 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 if../*.** The CH
18d8d 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 ECK_FOR_INTERRUP
18d8e 54 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 T macro defined
18d8f 68 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 here looks to se
18d90 65 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 e if the.** sqli
18d91 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 te3_interrupt()
18d92 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e routine has been
18d93 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 20 called. If it
18d94 68 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a has been, then.*
18d95 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 * processing of
18d96 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d the VDBE program
18d97 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 2e is interrupted.
18d98 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 .**.** This macr
18d99 6f 20 61 64 64 65 64 20 74 6f 20 65 76 65 72 79 o added to every
18d9a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 instruction tha
18d9b 74 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e t does a jump in
18d9c 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 order to.** imp
18d9d 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 lement a loop.
18d9e 54 68 69 73 20 74 65 73 74 20 75 73 65 64 20 74 This test used t
18d9f 6f 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73 69 o be on every si
18da0 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e ngle instruction
18da1 2c 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d 65 ,.** but that me
18da2 61 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73 74 ant we more test
18da3 69 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65 64 ing that we need
18da4 65 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 73 ed. By only tes
18da5 74 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67 ting the.** flag
18da6 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 on jump instruc
18da7 74 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 20 tions, we get a
18da8 28 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 6d (small) speed im
18da9 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 provement..*/.#d
18daa 65 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f efine CHECK_FOR_
18dab 49 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20 69 INTERRUPT \. i
18dac 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 f( db->u1.isInte
18dad 72 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20 61 rrupted ) goto a
18dae 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 bort_due_to_inte
18daf 72 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 53 rrupt;..#ifdef S
18db0 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 QLITE_DEBUG.stat
18db1 69 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73 74 ic int fileExist
18db2 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 s(sqlite3 *db, c
18db3 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
18db4 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 ){. int res = 0
18db5 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
18db6 49 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53 ITE_OK;.#ifdef S
18db7 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 QLITE_TEST. /*
18db8 49 66 20 77 65 20 61 72 65 20 63 75 72 72 65 6e If we are curren
18db9 74 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20 65 tly testing IO e
18dba 72 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e rrors, then do n
18dbb 6f 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 73 ot call OsAccess
18dbc 28 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20 () to. ** test
18dbd 66 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63 65 for the presence
18dbe 20 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 20 of zFile. This
18dbf 69 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 49 is because any I
18dc0 4f 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20 2a O error that. *
18dc1 2a 20 6f 63 63 75 72 73 20 68 65 72 65 20 77 69 * occurs here wi
18dc2 6c 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 74 ll not be report
18dc3 65 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65 20 ed, causing the
18dc4 74 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20 test to fail..
18dc5 2a 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 */. extern int
18dc6 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
18dc7 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20 _pending;. if(
18dc8 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
18dc9 5f 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65 _pending<=0 ).#e
18dca 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 ndif. rc = sq
18dcb 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 lite3OsAccess(db
18dcc 2d 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53 ->pVfs, zFile, S
18dcd 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 QLITE_ACCESS_EXI
18dce 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 65 STS, &res);. re
18dcf 74 75 72 6e 20 28 72 65 73 20 26 26 20 72 63 3d turn (res && rc=
18dd0 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 =SQLITE_OK);.}.#
18dd1 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 endif../*.** Exe
18dd2 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f 66 20 cute as much of
18dd3 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61 a VDBE program a
18dd4 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20 72 65 s we can then re
18dd5 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 turn..**.** sqli
18dd6 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 te3VdbeMakeReady
18dd7 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 () must be calle
18dd8 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f d before this ro
18dd9 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 utine in order t
18dda 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 70 o.** close the p
18ddb 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20 66 69 rogram with a fi
18ddc 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20 nal OP_Halt and
18ddd 74 6f 20 73 65 74 20 75 70 20 74 68 65 20 63 61 to set up the ca
18dde 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74 llbacks.** and t
18ddf 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
18de0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
18de1 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 6f Whenever a row o
18de2 72 20 72 65 73 75 6c 74 20 64 61 74 61 20 69 73 r result data is
18de3 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 available, this
18de4 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 69 routine will ei
18de5 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 ther.** invoke t
18de6 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 61 he result callba
18de7 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20 ck (if there is
18de8 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e 20 77 one) or return w
18de9 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f ith.** SQLITE_RO
18dea 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61 W..**.** If an a
18deb 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 ttempt is made t
18dec 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20 o open a locked
18ded 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74 database, then t
18dee 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 his routine.** w
18def 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b ill either invok
18df0 65 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 e the busy callb
18df1 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 ack (if there is
18df2 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c one) or it will
18df3 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .** return SQLIT
18df4 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 E_BUSY..**.** If
18df5 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
18df6 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 , an error messa
18df7 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f ge is written to
18df8 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
18df9 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 .** from sqlite3
18dfa 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d _malloc() and p-
18dfb 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 64 65 >zErrMsg is made
18dfc 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 to point to tha
18dfd 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 t memory..** The
18dfe 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 error code is s
18dff 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 61 tored in p->rc a
18e00 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nd this routine
18e01 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 returns SQLITE_E
18e02 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 RROR..**.** If t
18e03 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 72 he callback ever
18e04 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
18e05 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 o, then the prog
18e06 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d ram exits.** imm
18e07 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65 72 65 ediately. There
18e08 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f will be no erro
18e09 72 20 6d 65 73 73 61 67 65 20 62 75 74 20 74 68 r message but th
18e0a 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 73 e p->rc field is
18e0b 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 54 .** set to SQLIT
18e0c 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 69 73 E_ABORT and this
18e0d 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 routine will re
18e0e 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
18e0f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 R..**.** A memor
18e10 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 y allocation err
18e11 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72 63 20 or causes p->rc
18e12 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c to be set to SQL
18e13 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68 ITE_NOMEM and th
18e14 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f is.** routine to
18e15 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
18e16 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 RROR..**.** Othe
18e17 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73 20 72 r fatal errors r
18e18 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
18e19 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 OR..**.** After
18e1a 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 this routine has
18e1b 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74 finished, sqlit
18e1c 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29 e3VdbeFinalize()
18e1d 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73 should be.** us
18e1e 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 ed to clean up t
18e1f 68 65 20 6d 65 73 73 20 74 68 61 74 20 77 61 73 he mess that was
18e20 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f left behind..*/
18e21 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18e22 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 int sqlite3VdbeE
18e23 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70 20 20 xec(. Vdbe *p
18e24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18e25 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f /* The VDBE */
18e26 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 .){. int pc;
18e27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18e28 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 /* The program
18e29 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 counter */. Op
18e2a 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 *pOp;
18e2b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
18e2c 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a nt operation */.
18e2d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
18e2e 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 E_OK; /*
18e2f 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 Value to return
18e30 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
18e31 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 20 20 = p->db;
18e32 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
18e33 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 */. u8 encoding
18e34 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20 = ENC(db);
18e35 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
18e36 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65 encoding */. Me
18e37 6d 20 2a 70 49 6e 31 2c 20 2a 70 49 6e 32 2c 20 m *pIn1, *pIn2,
18e38 2a 70 49 6e 33 3b 20 20 20 2f 2a 20 49 6e 70 75 *pIn3; /* Inpu
18e39 74 20 6f 70 65 72 61 6e 64 73 20 2a 2f 0a 20 20 t operands */.
18e3a 4d 65 6d 20 2a 70 4f 75 74 3b 20 20 20 20 20 20 Mem *pOut;
18e3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 /* Ou
18e3c 74 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a tput operand */.
18e3d 20 20 75 38 20 6f 70 50 72 6f 70 65 72 74 79 3b u8 opProperty;
18e3e 0a 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 . int iCompare
18e3f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
18e40 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 Result of last
18e41 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 OP_Compare opera
18e42 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 tion */. int *a
18e43 50 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 Permute = 0;
18e44 20 20 20 20 20 2f 2a 20 50 65 72 6d 75 61 74 69 /* Permuati
18e45 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f on of columns fo
18e46 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a r OP_Compare */.
18e47 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
18e48 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b ILE. u64 start;
18e49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18e4a 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f /* CPU clock co
18e4b 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 unt at start of
18e4c 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 opcode */. int
18e4d 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20 20 origPc;
18e4e 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 /* Progra
18e4f 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61 m counter at sta
18e50 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a rt of opcode */.
18e51 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
18e52 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 QLITE_OMIT_PROGR
18e53 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 ESS_CALLBACK. i
18e54 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 nt nProgressOps
18e55 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63 = 0; /* Opc
18e56 6f 64 65 73 20 65 78 65 63 75 74 65 64 20 73 69 odes executed si
18e57 6e 63 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c nce progress cal
18e58 6c 62 61 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 lback. */.#endif
18e59 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
18e5a 64 20 61 54 65 6d 70 52 65 63 5b 31 36 5d 3b 20 d aTempRec[16];
18e5b 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 /* Space to hold
18e5c 20 61 20 74 72 61 6e 73 69 65 6e 74 20 55 6e 70 a transient Unp
18e5d 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 2f 0a 0a ackedRecord */..
18e5e 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 assert( p->mag
18e5f 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 ic==VDBE_MAGIC_R
18e60 55 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 UN ); /* sqlite
18e61 33 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65 3_step() verifie
18e62 73 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65 s this */. asse
18e63 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 rt( db->magic==S
18e64 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 QLITE_MAGIC_BUSY
18e65 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
18e66 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 eeMutexArrayEnte
18e67 72 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 r(&p->aMutex);.
18e68 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 if( p->rc==SQLI
18e69 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 TE_NOMEM ){.
18e6a 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 /* This happens
18e6b 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e if a malloc() in
18e6c 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 side a call to s
18e6d 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
18e6e 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 xt() or. ** s
18e6f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
18e70 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20 xt16() failed.
18e71 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d */. goto no_m
18e72 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 em;. }. assert
18e73 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f ( p->rc==SQLITE_
18e74 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c OK || p->rc==SQL
18e75 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20 70 2d ITE_BUSY );. p-
18e76 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b >rc = SQLITE_OK;
18e77 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 78 . assert( p->ex
18e78 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 2d plain==0 );. p-
18e79 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b >pResultSet = 0;
18e7a 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c . db->busyHandl
18e7b 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20 er.nBusy = 0;.
18e7c 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 CHECK_FOR_INTERR
18e7d 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 UPT;. sqlite3Vd
18e7e 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70 29 3b beIOTraceSql(p);
18e7f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
18e80 45 42 55 47 0a 20 20 73 71 6c 69 74 65 33 42 65 EBUG. sqlite3Be
18e81 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
18e82 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 63 3d 3d );. if( p->pc==
18e83 30 20 0a 20 20 20 26 26 20 28 28 70 2d 3e 64 62 0 . && ((p->db
18e84 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
18e85 5f 56 64 62 65 4c 69 73 74 69 6e 67 29 20 7c 7c _VdbeListing) ||
18e86 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20 fileExists(db,
18e87 22 76 64 62 65 5f 65 78 70 6c 61 69 6e 22 29 29 "vdbe_explain"))
18e88 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b . ){. int i;
18e89 0a 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 42 . printf("VDB
18e8a 45 20 50 72 6f 67 72 61 6d 20 4c 69 73 74 69 6e E Program Listin
18e8b 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 71 6c 69 g:\n");. sqli
18e8c 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 te3VdbePrintSql(
18e8d 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b p);. for(i=0;
18e8e 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b i<p->nOp; i++){
18e8f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
18e90 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f 75 74 bePrintOp(stdout
18e91 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 , i, &p->aOp[i])
18e92 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
18e93 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c ( fileExists(db,
18e94 20 22 76 64 62 65 5f 74 72 61 63 65 22 29 20 29 "vdbe_trace") )
18e95 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65 20 3d {. p->trace =
18e96 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20 20 73 stdout;. }. s
18e97 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
18e98 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69 66 0a alloc();.#endif.
18e99 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 3b 20 for(pc=p->pc;
18e9a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70 rc==SQLITE_OK; p
18e9b 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 c++){. assert
18e9c 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c 70 2d ( pc>=0 && pc<p-
18e9d 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 28 20 >nOp );. if(
18e9e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
18e9f 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
18ea0 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
18ea1 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63 20 3d ILE. origPc =
18ea2 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74 20 3d pc;. start =
18ea3 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
18ea4 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4f 70 ;.#endif. pOp
18ea5 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 63 5d 3b 0a = &p->aOp[pc];.
18ea6 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c . /* Only all
18ea7 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51 ow tracing if SQ
18ea8 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 LITE_DEBUG is de
18ea9 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 fined.. */.#i
18eaa 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
18eab 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 G. if( p->tra
18eac 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ce ){. if(
18ead 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 pc==0 ){.
18eae 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78 printf("VDBE Ex
18eaf 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e ecution Trace:\n
18eb0 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ");. sqli
18eb1 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 te3VdbePrintSql(
18eb2 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
18eb3 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
18eb4 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 ntOp(p->trace, p
18eb5 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20 c, pOp);. }.
18eb6 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d if( p->trace=
18eb7 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20 =0 && pc==0 ){.
18eb8 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 sqlite3Begi
18eb9 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
18eba 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45 . if( fileE
18ebb 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f xists(db, "vdbe_
18ebc 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20 sqltrace") ){.
18ebd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
18ebe 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 ePrintSql(p);.
18ebf 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
18ec0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
18ec1 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 oc();. }.#end
18ec2 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f if. .. /
18ec3 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
18ec4 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d f we need to sim
18ec5 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75 ulate an interru
18ec6 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 pt. This only h
18ec7 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66 appens. ** if
18ec8 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69 we have a speci
18ec9 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20 al test build..
18eca 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c */.#ifdef SQL
18ecb 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28 ITE_TEST. if(
18ecc 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
18ecd 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20 pt_count>0 ){.
18ece 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 sqlite3_inte
18ecf 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 rrupt_count--;.
18ed0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
18ed1 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 _interrupt_count
18ed2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
18ed3 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
18ed4 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 (db);. }.
18ed5 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e }.#endif..#ifn
18ed6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18ed7 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 PROGRESS_CALLBAC
18ed8 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 K. /* Call th
18ed9 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
18eda 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e ack if it is con
18edb 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20 figured and the
18edc 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a required number.
18edd 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f ** of VDBE o
18ede 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65 ps have been exe
18edf 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69 cuted (either si
18ee0 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74 nce this invocat
18ee1 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71 ion of. ** sq
18ee2 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20 lite3VdbeExec()
18ee3 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69 or since last ti
18ee4 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 me the progress
18ee5 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c callback was cal
18ee6 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20 led).. ** If
18ee7 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c the progress cal
18ee8 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f lback returns no
18ee9 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65 n-zero, exit the
18eea 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
18eeb 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72 with. ** a r
18eec 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 eturn code SQLIT
18eed 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a E_ABORT.. */.
18eee 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f if( db->xPro
18eef 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69 gress ){. i
18ef0 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 f( db->nProgress
18ef1 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70 Ops==nProgressOp
18ef2 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 s ){. int
18ef3 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66 prc;. if
18ef4 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
18ef5 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
18ef6 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
18ef7 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d e;. prc =
18ef8 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62 db->xProgress(db
18ef9 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b ->pProgressArg);
18efa 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
18efb 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
18efc 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
18efd 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 e_to_misuse;.
18efe 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20 if( prc!=0
18eff 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
18f00 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 = SQLITE_INTERRU
18f01 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f PT;. go
18f02 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 to vdbe_error_ha
18f03 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 lt;. }.
18f04 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
18f05 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a ps = 0;. }.
18f06 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
18f07 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 ps++;. }.#end
18f08 69 66 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f if.. /* Do co
18f09 6d 6d 6f 6e 20 73 65 74 75 70 20 70 72 6f 63 65 mmon setup proce
18f0a 73 73 69 6e 67 20 66 6f 72 20 61 6e 79 20 6f 70 ssing for any op
18f0b 63 6f 64 65 20 74 68 61 74 20 69 73 20 6d 61 72 code that is mar
18f0c 6b 65 64 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 ked. ** with
18f0d 74 68 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c the "out2-prerel
18f0e 65 61 73 65 22 20 74 61 67 2e 20 20 53 75 63 68 ease" tag. Such
18f0f 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 61 20 opcodes have a
18f10 73 69 6e 67 6c 65 0a 20 20 20 20 2a 2a 20 6f 75 single. ** ou
18f11 74 70 75 74 20 77 68 69 63 68 20 69 73 20 73 70 tput which is sp
18f12 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 50 ecified by the P
18f13 32 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 2 parameter. Th
18f14 65 20 50 32 20 72 65 67 69 73 74 65 72 0a 20 20 e P2 register.
18f15 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 ** is initiali
18f16 7a 65 64 20 74 6f 20 61 20 4e 55 4c 4c 2e 0a 20 zed to a NULL..
18f17 20 20 20 2a 2f 0a 20 20 20 20 6f 70 50 72 6f 70 */. opProp
18f18 65 72 74 79 20 3d 20 6f 70 63 6f 64 65 50 72 6f erty = opcodePro
18f19 70 65 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64 perty[pOp->opcod
18f1a 65 5d 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 50 e];. if( (opP
18f1b 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f roperty & OPFLG_
18f1c 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 29 OUT2_PRERELEASE)
18f1d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 !=0 ){. ass
18f1e 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 ert( pOp->p2>0 )
18f1f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
18f20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p2<=p->nMem
18f21 20 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 20 3d );. pOut =
18f22 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
18f23 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2];. sqlite
18f24 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 3VdbeMemReleaseE
18f25 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20 xternal(pOut);.
18f26 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 pOut->flags
18f27 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
18f28 20 7d 65 6c 73 65 0a 20 0a 20 20 20 20 2f 2a 20 }else. . /*
18f29 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20 Do common setup
18f2a 66 6f 72 20 6f 70 63 6f 64 65 73 20 6d 61 72 6b for opcodes mark
18f2b 65 64 20 77 69 74 68 20 6f 6e 65 20 6f 66 20 74 ed with one of t
18f2c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 he following.
18f2d 20 2a 2a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 ** combinations
18f2e 20 6f 66 20 70 72 6f 70 65 72 74 69 65 73 2e 0a of properties..
18f2f 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
18f30 20 20 20 20 20 20 20 20 69 6e 31 0a 20 20 20 20 in1.
18f31 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 ** in1
18f32 20 69 6e 32 0a 20 20 20 20 2a 2a 20 20 20 20 20 in2. **
18f33 20 20 20 20 20 20 69 6e 31 20 69 6e 32 20 6f 75 in1 in2 ou
18f34 74 33 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 t3. **
18f35 20 20 20 20 69 6e 31 20 69 6e 33 0a 20 20 20 20 in1 in3.
18f36 2a 2a 0a 20 20 20 20 2a 2a 20 56 61 72 69 61 62 **. ** Variab
18f37 6c 65 73 20 70 49 6e 31 2c 20 70 49 6e 32 2c 20 les pIn1, pIn2,
18f38 61 6e 64 20 70 49 6e 33 20 61 72 65 20 6d 61 64 and pIn3 are mad
18f39 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 70 e to point to ap
18f3a 70 72 6f 70 72 69 61 74 65 0a 20 20 20 20 2a 2a propriate. **
18f3b 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 69 registers for i
18f3c 6e 70 75 74 73 2e 20 20 56 61 72 69 61 62 6c 65 nputs. Variable
18f3d 20 70 4f 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 pOut points to
18f3e 74 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 the output regis
18f3f 74 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ter.. */.
18f40 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 if( (opProperty
18f41 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 3d 30 20 & OPFLG_IN1)!=0
18f42 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
18f43 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 pOp->p1>0 );.
18f44 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
18f45 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p1<=p->nMem );.
18f46 20 20 20 20 20 20 70 49 6e 31 20 3d 20 26 70 2d pIn1 = &p-
18f47 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
18f48 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 REGISTER_T
18f49 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 RACE(pOp->p1, pI
18f4a 6e 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 n1);. if( (
18f4b 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
18f4c 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 LG_IN2)!=0 ){.
18f4d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
18f4e 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 p->p2>0 );.
18f4f 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
18f50 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p2<=p->nMem );.
18f51 20 20 20 20 20 20 20 70 49 6e 32 20 3d 20 26 70 pIn2 = &p
18f52 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b ->aMem[pOp->p2];
18f53 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 54 45 . REGISTE
18f54 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c R_TRACE(pOp->p2,
18f55 20 70 49 6e 32 29 3b 0a 20 20 20 20 20 20 20 20 pIn2);.
18f56 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 if( (opProperty
18f57 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d 30 & OPFLG_OUT3)!=0
18f58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 ){. as
18f59 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 sert( pOp->p3>0
18f5a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 );. ass
18f5b 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d ert( pOp->p3<=p-
18f5c 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 >nMem );.
18f5d 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d pOut = &p->aM
18f5e 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 em[pOp->p3];.
18f5f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
18f60 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 se if( (opProper
18f61 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21 ty & OPFLG_IN3)!
18f62 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 =0 ){. as
18f63 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 sert( pOp->p3>0
18f64 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
18f65 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e t( pOp->p3<=p->n
18f66 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 Mem );. p
18f67 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In3 = &p->aMem[p
18f68 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 Op->p3];.
18f69 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
18f6a 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a pOp->p3, pIn3);.
18f6b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
18f6c 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 e if( (opPropert
18f6d 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21 3d y & OPFLG_IN2)!=
18f6e 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 0 ){. asser
18f6f 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a t( pOp->p2>0 );.
18f70 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
18f71 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p2<=p->nMem )
18f72 3b 0a 20 20 20 20 20 20 70 49 6e 32 20 3d 20 26 ;. pIn2 = &
18f73 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d p->aMem[pOp->p2]
18f74 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52 ;. REGISTER
18f75 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 _TRACE(pOp->p2,
18f76 70 49 6e 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 pIn2);. }else
18f77 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 if( (opProperty
18f78 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21 3d 30 & OPFLG_IN3)!=0
18f79 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
18f7a 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 ( pOp->p3>0 );.
18f7b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
18f7c 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
18f7d 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 70 . pIn3 = &p
18f7e 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
18f7f 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f . REGISTER_
18f80 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 TRACE(pOp->p3, p
18f81 49 6e 33 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 In3);. }..
18f82 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 switch( pOp->op
18f83 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a 2a code ){../******
18f84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18f88 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 74 20 *******.** What
18f89 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d 61 73 follows is a mas
18f8a 73 69 76 65 20 73 77 69 74 63 68 20 73 74 61 74 sive switch stat
18f8b 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61 63 68 ement where each
18f8c 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e 74 73 case implements
18f8d 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 69 a.** separate i
18f8e 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 nstruction in th
18f8f 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e e virtual machin
18f90 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c 6f 77 e. If we follow
18f91 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 69 6e the usual.** in
18f92 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 65 6e dentation conven
18f93 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61 73 65 tions, each case
18f94 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64 65 6e should be inden
18f95 74 65 64 20 62 79 20 36 20 73 70 61 63 65 73 2e ted by 6 spaces.
18f96 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 69 73 But.** that is
18f97 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74 65 64 a lot of wasted
18f98 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 6c 65 space on the le
18f99 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f 20 74 ft margin. So t
18f9a 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 0a 2a he code within.*
18f9b 2a 20 74 68 65 20 73 77 69 74 63 68 20 73 74 61 * the switch sta
18f9c 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72 65 61 tement will brea
18f9d 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74 69 6f k with conventio
18f9e 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68 2d 6c n and be flush-l
18f9f 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 eft. Another.**
18fa0 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 69 6d big comment (sim
18fa1 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 ilar to this one
18fa2 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 65 20 ) will mark the
18fa3 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 point in the cod
18fa4 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20 74 72 e where.** we tr
18fa5 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 74 6f ansition back to
18fa6 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 normal indentat
18fa7 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ion..**.** The f
18fa8 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65 61 63 ormatting of eac
18fa9 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f 72 74 h case is import
18faa 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65 66 69 ant. The makefi
18fab 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a 2a 2a le for SQLite.**
18fac 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 43 generates two C
18fad 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65 73 2e files "opcodes.
18fae 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65 73 2e h" and "opcodes.
18faf 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 74 c" by scanning t
18fb0 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f 6f 6b his.** file look
18fb1 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20 74 68 ing for lines th
18fb2 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 22 63 at begin with "c
18fb3 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65 20 6f ase OP_". The o
18fb4 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73 0a 2a pcodes.h files.*
18fb5 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c 65 64 * will be filled
18fb6 20 77 69 74 68 20 23 64 65 66 69 6e 65 73 20 74 with #defines t
18fb7 68 61 74 20 67 69 76 65 20 75 6e 69 71 75 65 20 hat give unique
18fb8 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 74 integer values t
18fb9 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f 64 65 o each.** opcode
18fba 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 73 and the opcodes
18fbb 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c 6c 65 .c file is fille
18fbc 64 20 77 69 74 68 20 61 6e 20 61 72 72 61 79 20 d with an array
18fbd 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65 72 65 of strings where
18fbe 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e 67 20 .** each string
18fbf 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20 is the symbolic
18fc0 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 72 name for the cor
18fc1 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63 6f 64 responding opcod
18fc2 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 61 e. If the.** ca
18fc3 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 se statement is
18fc4 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 63 6f followed by a co
18fc5 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 mment of the for
18fc6 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20 2e 2e m "/# same as ..
18fc7 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20 63 6f . #/".** that co
18fc8 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f mment is used to
18fc9 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 70 determine the p
18fca 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65 20 articular value
18fcb 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 0a 2a of the opcode..*
18fcc 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79 77 6f *.** Other keywo
18fcd 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 rds in the comme
18fce 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 nt that follows
18fcf 65 61 63 68 20 63 61 73 65 20 61 72 65 20 75 73 each case are us
18fd0 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74 72 75 ed to.** constru
18fd1 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49 4e 49 ct the OPFLG_INI
18fd2 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65 20 74 TIALIZER value t
18fd3 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 hat initializes
18fd4 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d opcodeProperty[]
18fd5 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20 69 6e ..** Keywords in
18fd6 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e 32 2c clude: in1, in2,
18fd7 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65 72 65 in3, out2_prere
18fd8 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f 75 74 lease, out2, out
18fd9 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 6d 3. See.** the m
18fda 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72 kopcodeh.awk scr
18fdb 69 70 74 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ipt for addition
18fdc 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
18fdd 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74 61 74 **.** Documentat
18fde 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45 20 6f ion about VDBE o
18fdf 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65 72 61 pcodes is genera
18fe0 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 ted by scanning
18fe1 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66 6f 72 this file.** for
18fe2 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74 20 63 lines of that c
18fe3 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65 3a 22 ontain "Opcode:"
18fe4 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61 6e 64 . That line and
18fe5 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a all subsequent.
18fe6 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e 65 73 ** comment lines
18fe7 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68 65 are used in the
18fe8 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 74 generation of t
18fe9 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c 20 64 he opcode.html d
18fea 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 ocumentation.**
18feb 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55 4d 4d file..**.** SUMM
18fec 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 46 ARY:.**.** F
18fed 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69 6d 70 ormatting is imp
18fee 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69 70 74 ortant to script
18fef 73 20 74 68 61 74 20 73 63 61 6e 20 74 68 69 73 s that scan this
18ff0 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20 44 6f file..** Do
18ff1 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72 6f not deviate fro
18ff2 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 m the formatting
18ff3 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74 6c 79 style currently
18ff4 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 2a 2a in use..**.****
18ff5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ff6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ff7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ff8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ff9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 4f *********/../* O
18ffa 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a 20 50 pcode: Goto * P
18ffb 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6e 2 * * *.**.** An
18ffc 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 6a unconditional j
18ffd 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 ump to address P
18ffe 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 69 2..** The next i
18fff 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 63 75 nstruction execu
19000 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a 2a 20 ted will be .**
19001 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 65 78 the one at index
19002 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 65 67 P2 from the beg
19003 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 inning of.** the
19004 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 61 73 program..*/.cas
19005 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20 20 20 e OP_Goto: {
19006 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 /* jump
19007 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f */. CHECK_FOR_
19008 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 63 20 INTERRUPT;. pc
19009 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1900a 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1900b 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50 31 20 code: Gosub P1
1900c 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 P2 * * *.**.** W
1900d 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 rite the current
1900e 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20 72 65 address onto re
1900f 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61 6e 64 gister P1.** and
19010 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 61 64 then jump to ad
19011 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63 61 73 dress P2..*/.cas
19012 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 20 20 e OP_Gosub: {
19013 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 /* jump
19014 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f */. assert( pO
19015 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 p->p1>0 );. ass
19016 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d ert( pOp->p1<=p-
19017 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 20 >nMem );. pIn1
19018 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
19019 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 p1];. assert( (
1901a 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1901b 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 M_Dyn)==0 );. p
1901c 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d In1->flags = MEM
1901d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e _Int;. pIn1->u.
1901e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54 i = pc;. REGIST
1901f 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 ER_TRACE(pOp->p1
19020 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20 , pIn1);. pc =
19021 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 62 pOp->p2 - 1;. b
19022 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
19023 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31 20 2a de: Return P1 *
19024 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d * * *.**.** Jum
19025 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e p to the next in
19026 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65 72 20 struction after
19027 74 68 65 20 61 64 64 72 65 73 73 20 69 6e 20 72 the address in r
19028 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 egister P1..*/.c
19029 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20 7b ase OP_Return: {
1902a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e /* in
1902b 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 1 */. assert( p
1902c 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1902d 5f 49 6e 74 20 29 3b 0a 20 20 70 63 20 3d 20 70 _Int );. pc = p
1902e 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 In1->u.i;. brea
1902f 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
19030 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a Yield P1 * * *
19031 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 *.**.** Swap th
19032 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 e program counte
19033 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 r with the value
19034 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e in register P1.
19035 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c .*/.case OP_Yiel
19036 64 3a 20 7b 0a 20 20 69 6e 74 20 70 63 44 65 73 d: {. int pcDes
19037 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 t;. assert( pOp
19038 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 ->p1>0 );. asse
19039 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e rt( pOp->p1<=p->
1903a 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 20 3d nMem );. pIn1 =
1903b 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1903c 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 1];. assert( (p
1903d 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1903e 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49 _Dyn)==0 );. pI
1903f 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f n1->flags = MEM_
19040 49 6e 74 3b 0a 20 20 70 63 44 65 73 74 20 3d 20 Int;. pcDest =
19041 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e pIn1->u.i;. pIn
19042 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 1->u.i = pc;. R
19043 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
19044 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 p->p1, pIn1);.
19045 70 63 20 3d 20 70 63 44 65 73 74 3b 0a 20 20 62 pc = pcDest;. b
19046 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 reak;.}.../* Opc
19047 6f 64 65 3a 20 20 48 61 6c 74 20 50 31 20 50 32 ode: Halt P1 P2
19048 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 * P4 *.**.** Ex
19049 69 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 it immediately.
1904a 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 All open cursor
1904b 73 2c 20 46 69 66 6f 73 2c 20 65 74 63 20 61 72 s, Fifos, etc ar
1904c 65 20 63 6c 6f 73 65 64 0a 2a 2a 20 61 75 74 6f e closed.** auto
1904d 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a matically..**.**
1904e 20 50 31 20 69 73 20 74 68 65 20 72 65 73 75 6c P1 is the resul
1904f 74 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 t code returned
19050 62 79 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 by sqlite3_exec(
19051 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 ), sqlite3_reset
19052 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 (),.** or sqlite
19053 33 5f 66 69 6e 61 6c 69 7a 65 28 29 2e 20 20 46 3_finalize(). F
19054 6f 72 20 61 20 6e 6f 72 6d 61 6c 20 68 61 6c 74 or a normal halt
19055 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20 62 65 , this should be
19056 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2e 0a SQLITE_OK (0)..
19057 2a 2a 20 46 6f 72 20 65 72 72 6f 72 73 2c 20 69 ** For errors, i
19058 74 20 63 61 6e 20 62 65 20 73 6f 6d 65 20 6f 74 t can be some ot
19059 68 65 72 20 76 61 6c 75 65 2e 20 20 49 66 20 50 her value. If P
1905a 31 21 3d 30 20 74 68 65 6e 20 50 32 20 77 69 6c 1!=0 then P2 wil
1905b 6c 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 l determine.** w
1905c 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f hether or not to
1905d 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 63 75 rollback the cu
1905e 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f rrent transactio
1905f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72 6f 6c 6c 62 n. Do not rollb
19060 61 63 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d 4f 45 ack.** if P2==OE
19061 5f 46 61 69 6c 2e 20 44 6f 20 74 68 65 20 72 6f _Fail. Do the ro
19062 6c 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d 4f 45 llback if P2==OE
19063 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 20 50 _Rollback. If P
19064 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a 2a 20 2==OE_Abort,.**
19065 74 68 65 6e 20 62 61 63 6b 20 6f 75 74 20 61 6c then back out al
19066 6c 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 68 l changes that h
19067 61 76 65 20 6f 63 63 75 72 72 65 64 20 64 75 72 ave occurred dur
19068 69 6e 67 20 74 68 69 73 20 65 78 65 63 75 74 69 ing this executi
19069 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 on of the.** VDB
1906a 45 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 72 6f E, but do not ro
1906b 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 llback the trans
1906c 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 action. .**.** I
1906d 66 20 50 34 20 69 73 20 6e 6f 74 20 6e 75 6c 6c f P4 is not null
1906e 20 74 68 65 6e 20 69 74 20 69 73 20 61 6e 20 65 then it is an e
1906f 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 rror message str
19070 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 ing..**.** There
19071 20 69 73 20 61 6e 20 69 6d 70 6c 69 65 64 20 22 is an implied "
19072 48 61 6c 74 20 30 20 30 20 30 22 20 69 6e 73 74 Halt 0 0 0" inst
19073 72 75 63 74 69 6f 6e 20 69 6e 73 65 72 74 65 64 ruction inserted
19074 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 at the very end
19075 20 6f 66 0a 2a 2a 20 65 76 65 72 79 20 70 72 6f of.** every pro
19076 67 72 61 6d 2e 20 20 53 6f 20 61 20 6a 75 6d 70 gram. So a jump
19077 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 69 past the last i
19078 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 nstruction of th
19079 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 73 20 e program.** is
1907a 74 68 65 20 73 61 6d 65 20 61 73 20 65 78 65 63 the same as exec
1907b 75 74 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f 0a 63 uting Halt..*/.c
1907c 61 73 65 20 4f 50 5f 48 61 6c 74 3a 20 7b 0a 20 ase OP_Halt: {.
1907d 20 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31 p->rc = pOp->p1
1907e 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a ;. p->pc = pc;.
1907f 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e p->errorAction
19080 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 66 = pOp->p2;. if
19081 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 ( pOp->p4.z ){.
19082 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
19083 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
19084 20 64 62 2c 20 22 25 73 22 2c 20 70 4f 70 2d 3e db, "%s", pOp->
19085 70 34 2e 7a 29 3b 0a 20 20 7d 0a 20 20 72 63 20 p4.z);. }. rc
19086 3d 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c = sqlite3VdbeHal
19087 74 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 t(p);. assert(
19088 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
19089 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b || rc==SQLITE_OK
1908a 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 );. if( rc==SQ
1908b 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 LITE_BUSY ){.
1908c 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 p->rc = rc = SQ
1908d 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c LITE_BUSY;. }el
1908e 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 2d 3e se{. rc = p->
1908f 72 63 20 3f 20 53 51 4c 49 54 45 5f 45 52 52 4f rc ? SQLITE_ERRO
19090 52 20 3a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b R : SQLITE_DONE;
19091 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 . }. goto vdbe
19092 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f _return;.}../* O
19093 70 63 6f 64 65 3a 20 49 6e 74 65 67 65 72 20 50 pcode: Integer P
19094 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
19095 20 54 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65 The 32-bit inte
19096 67 65 72 20 76 61 6c 75 65 20 50 31 20 69 73 20 ger value P1 is
19097 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 72 65 67 written into reg
19098 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 ister P2..*/.cas
19099 65 20 4f 50 5f 49 6e 74 65 67 65 72 3a 20 7b 20 e OP_Integer: {
1909a 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1909b 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
1909c 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pOut->flags = ME
1909d 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 M_Int;. pOut->u
1909e 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 .i = pOp->p1;.
1909f 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
190a0 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 50 32 20 ode: Int64 * P2
190a1 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
190a2 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
190a3 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 a 64-bit integer
190a4 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 value..** Write
190a5 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f that value into
190a6 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f register P2..*/
190a7 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 34 3a 20 .case OP_Int64:
190a8 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f { /* o
190a9 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
190aa 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d /. assert( pOp-
190ab 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 3b 0a 20 >p4.pI64!=0 );.
190ac 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
190ad 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e EM_Int;. pOut->
190ae 75 2e 69 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 u.i = *pOp->p4.p
190af 49 36 34 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a I64;. break;.}.
190b0 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 6c ./* Opcode: Real
190b1 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a * P2 * P4 *.**.
190b2 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
190b3 65 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 66 er to a 64-bit f
190b4 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
190b5 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 lue..** Write th
190b6 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 at value into re
190b7 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 gister P2..*/.ca
190b8 73 65 20 4f 50 5f 52 65 61 6c 3a 20 7b 20 20 20 se OP_Real: {
190b9 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
190ba 20 61 73 20 54 4b 5f 46 4c 4f 41 54 2c 20 6f 75 as TK_FLOAT, ou
190bb 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
190bc 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d . pOut->flags =
190bd 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 61 73 73 MEM_Real;. ass
190be 65 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 4e ert( !sqlite3IsN
190bf 61 4e 28 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 aN(*pOp->p4.pRea
190c0 6c 29 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 72 20 l) );. pOut->r
190c1 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c = *pOp->p4.pReal
190c2 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
190c3 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 38 Opcode: String8
190c4 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a * P2 * P4 *.**.
190c5 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 ** P4 points to
190c6 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 a nul terminated
190c7 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 20 54 UTF-8 string. T
190c8 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72 his opcode is tr
190c9 61 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a 20 69 6e ansformed .** in
190ca 74 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e 67 20 to an OP_String
190cb 62 65 66 6f 72 65 20 69 74 20 69 73 20 65 78 65 before it is exe
190cc 63 75 74 65 64 20 66 6f 72 20 74 68 65 20 66 69 cuted for the fi
190cd 72 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a 63 61 73 rst time..*/.cas
190ce 65 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20 7b 20 e OP_String8: {
190cf 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
190d0 61 73 20 54 4b 5f 53 54 52 49 4e 47 2c 20 6f 75 as TK_STRING, ou
190d1 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f t2-prerelease */
190d2 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
190d3 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f 70 p4.z!=0 );. pOp
190d4 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 53 74 ->opcode = OP_St
190d5 72 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 ring;. pOp->p1
190d6 3d 20 73 74 72 6c 65 6e 28 70 4f 70 2d 3e 70 34 = strlen(pOp->p4
190d7 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 .z);..#ifndef SQ
190d8 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
190d9 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67 21 3d if( encoding!=
190da 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 SQLITE_UTF8 ){.
190db 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
190dc 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f mSetStr(pOut, pO
190dd 70 2d 3e 70 34 2e 7a 2c 20 2d 31 2c 20 53 51 4c p->p4.z, -1, SQL
190de 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
190df 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 69 66 _STATIC);. if
190e0 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c ( SQLITE_OK!=sql
190e1 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e ite3VdbeChangeEn
190e2 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 6e 63 coding(pOut, enc
190e3 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 6e 6f oding) ) goto no
190e4 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 28 20 53 51 _mem;. if( SQ
190e5 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 LITE_OK!=sqlite3
190e6 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 VdbeMemMakeWrite
190e7 61 62 6c 65 28 70 4f 75 74 29 20 29 20 67 6f 74 able(pOut) ) got
190e8 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 70 4f o no_mem;. pO
190e9 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b ut->zMalloc = 0;
190ea 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 . pOut->flags
190eb 20 7c 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b 0a |= MEM_Static;.
190ec 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 pOut->flags
190ed 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 &= ~MEM_Dyn;.
190ee 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 if( pOp->p4type
190ef 3d 3d 50 34 5f 44 59 4e 41 4d 49 43 20 29 7b 0a ==P4_DYNAMIC ){.
190f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
190f1 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 70 34 2e ree(db, pOp->p4.
190f2 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f z);. }. pO
190f3 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 p->p4type = P4_D
190f4 59 4e 41 4d 49 43 3b 0a 20 20 20 20 70 4f 70 2d YNAMIC;. pOp-
190f5 3e 70 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e 7a 3b >p4.z = pOut->z;
190f6 0a 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 . pOp->p1 = p
190f7 4f 75 74 2d 3e 6e 3b 0a 20 20 20 20 69 66 28 20 Out->n;. if(
190f8 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d pOp->p1>db->aLim
190f9 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
190fa 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 LENGTH] ){.
190fb 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
190fc 20 20 20 7d 0a 20 20 20 20 55 50 44 41 54 45 5f }. UPDATE_
190fd 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 MAX_BLOBSIZE(pOu
190fe 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 t);. break;.
190ff 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 }.#endif. if(
19100 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d pOp->p1>db->aLim
19101 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
19102 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 LENGTH] ){. g
19103 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d oto too_big;. }
19104 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 . /* Fall throu
19105 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 gh to the next c
19106 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 20 2a ase, OP_String *
19107 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 65 /.}. ./* Opcode
19108 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32 20 2a : String P1 P2 *
19109 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 P4 *.**.** The
1910a 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50 34 20 string value P4
1910b 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28 62 79 of length P1 (by
1910c 74 65 73 29 20 69 73 20 73 74 6f 72 65 64 20 69 tes) is stored i
1910d 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a n register P2..*
1910e 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 /.case OP_String
1910f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 : { /*
19110 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
19111 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
19112 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 ->p4.z!=0 );. p
19113 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Out->flags = MEM
19114 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c _Str|MEM_Static|
19115 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 MEM_Term;. pOut
19116 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b ->z = pOp->p4.z;
19117 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f 70 . pOut->n = pOp
19118 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e ->p1;. pOut->en
19119 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 c = encoding;.
1911a 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1911b 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 IZE(pOut);. bre
1911c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1911d 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 2a 20 : Null * P2 * *
1911e 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 *.**.** Write a
1911f 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 73 74 NULL into regist
19120 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f er P2..*/.case O
19121 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 P_Null: {
19122 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
19123 65 6c 65 61 73 65 20 2a 2f 0a 20 20 62 72 65 61 elease */. brea
19124 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 k;.}...#ifndef S
19125 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f QLITE_OMIT_BLOB_
19126 4c 49 54 45 52 41 4c 0a 2f 2a 20 4f 70 63 6f 64 LITERAL./* Opcod
19127 65 3a 20 42 6c 6f 62 20 50 31 20 50 32 20 2a 20 e: Blob P1 P2 *
19128 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e P4.**.** P4 poin
19129 74 73 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 ts to a blob of
1912a 64 61 74 61 20 50 31 20 62 79 74 65 73 20 6c 6f data P1 bytes lo
1912b 6e 67 2e 20 20 53 74 6f 72 65 20 74 68 69 73 0a ng. Store this.
1912c 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 65 67 69 73 ** blob in regis
1912d 74 65 72 20 50 32 2e 20 54 68 69 73 20 69 6e 73 ter P2. This ins
1912e 74 72 75 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 truction is not
1912f 63 6f 64 65 64 20 64 69 72 65 63 74 6c 79 0a 2a coded directly.*
19130 2a 20 62 79 20 74 68 65 20 63 6f 6d 70 69 6c 65 * by the compile
19131 72 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 r. Instead, the
19132 63 6f 6d 70 69 6c 65 72 20 6c 61 79 65 72 20 73 compiler layer s
19133 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6e 20 4f pecifies.** an O
19134 50 5f 48 65 78 42 6c 6f 62 20 6f 70 63 6f 64 65 P_HexBlob opcode
19135 2c 20 77 69 74 68 20 74 68 65 20 68 65 78 20 73 , with the hex s
19136 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 tring representa
19137 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 62 tion of.** the b
19138 6c 6f 62 20 61 73 20 50 34 2e 20 54 68 69 73 20 lob as P4. This
19139 6f 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 opcode is transf
1913a 6f 72 6d 65 64 20 74 6f 20 61 6e 20 4f 50 5f 42 ormed to an OP_B
1913b 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 lob.** the first
1913c 20 74 69 6d 65 20 69 74 20 69 73 20 65 78 65 63 time it is exec
1913d 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 uted..*/.case OP
1913e 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 _Blob: {
1913f 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
19140 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 prerelease */.
19141 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20 assert( pOp->p1
19142 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 <= SQLITE_MAX_LE
19143 4e 47 54 48 20 29 3b 0a 20 20 73 71 6c 69 74 65 NGTH );. sqlite
19144 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 3VdbeMemSetStr(p
19145 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 Out, pOp->p4.z,
19146 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b 0a pOp->p1, 0, 0);.
19147 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e pOut->enc = en
19148 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 coding;. UPDATE
19149 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f _MAX_BLOBSIZE(pO
1914a 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ut);. break;.}.
1914b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1914c 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 _OMIT_BLOB_LITER
1914d 41 4c 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 AL */../* Opcode
1914e 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20 50 32 : Variable P1 P2
1914f 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 * * *.**.** The
19150 20 76 61 6c 75 65 20 6f 66 20 76 61 72 69 61 62 value of variab
19151 6c 65 20 50 31 20 69 73 20 77 72 69 74 74 65 6e le P1 is written
19152 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
19153 32 2e 20 41 20 76 61 72 69 61 62 6c 65 20 69 73 2. A variable is
19154 0a 2a 2a 20 61 6e 20 75 6e 6b 6e 6f 77 6e 20 69 .** an unknown i
19155 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 n the original S
19156 51 4c 20 73 74 72 69 6e 67 20 61 73 20 68 61 6e QL string as han
19157 64 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 ded to sqlite3_c
19158 6f 6d 70 69 6c 65 28 29 2e 0a 2a 2a 20 41 6e 79 ompile()..** Any
19159 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 20 74 occurrence of t
1915a 68 65 20 27 3f 27 20 63 68 61 72 61 63 74 65 72 he '?' character
1915b 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c in the original
1915c 20 53 51 4c 20 69 73 20 63 6f 6e 73 69 64 65 72 SQL is consider
1915d 65 64 0a 2a 2a 20 61 20 76 61 72 69 61 62 6c 65 ed.** a variable
1915e 2e 20 20 56 61 72 69 61 62 6c 65 73 20 69 6e 20 . Variables in
1915f 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20 61 the SQL string a
19160 72 65 20 6e 75 6d 62 65 72 20 66 72 6f 6d 20 6c re number from l
19161 65 66 74 20 74 6f 0a 2a 2a 20 72 69 67 68 74 20 eft to.** right
19162 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 31 beginning with 1
19163 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66 . The values of
19164 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 73 variables are s
19165 65 74 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 et using the.**
19166 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 20 41 sqlite3_bind() A
19167 50 49 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 PI..*/.case OP_V
19168 61 72 69 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 ariable: {
19169 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1916a 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 release */. int
1916b 20 6a 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 j = pOp->p1 - 1
1916c 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 ;. Mem *pVar;.
1916d 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 assert( j>=0 &&
1916e 20 6a 3c 70 2d 3e 6e 56 61 72 20 29 3b 0a 0a 20 j<p->nVar );..
1916f 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 pVar = &p->aVar
19170 5b 6a 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 [j];. if( sqlit
19171 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 e3VdbeMemTooBig(
19172 70 56 61 72 29 20 29 7b 0a 20 20 20 20 67 6f 74 pVar) ){. got
19173 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 o too_big;. }.
19174 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19175 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c hallowCopy(pOut,
19176 20 26 70 2d 3e 61 56 61 72 5b 6a 5d 2c 20 4d 45 &p->aVar[j], ME
19177 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 55 50 44 M_Static);. UPD
19178 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
19179 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b (pOut);. break;
1917a 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d .}../* Opcode: M
1917b 6f 76 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a ove P1 P2 P3 * *
1917c 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 .**.** Move the
1917d 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 values in regist
1917e 65 72 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 6f er P1..P1+P3-1 o
1917f 76 65 72 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 ver into.** regi
19180 73 74 65 72 73 20 50 32 2e 2e 50 32 2b 50 33 2d sters P2..P2+P3-
19181 31 2e 20 20 52 65 67 69 73 74 65 72 73 20 50 31 1. Registers P1
19182 2e 2e 50 31 2b 50 31 2d 31 20 61 72 65 0a 2a 2a ..P1+P1-1 are.**
19183 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20 61 20 left holding a
19184 4e 55 4c 4c 2e 20 20 49 74 20 69 73 20 61 6e 20 NULL. It is an
19185 65 72 72 6f 72 20 66 6f 72 20 72 65 67 69 73 74 error for regist
19186 65 72 20 72 61 6e 67 65 73 0a 2a 2a 20 50 31 2e er ranges.** P1.
19187 2e 50 31 2b 50 33 2d 31 20 61 6e 64 20 50 32 2e .P1+P3-1 and P2.
19188 2e 50 32 2b 50 33 2d 31 20 74 6f 20 6f 76 65 72 .P2+P3-1 to over
19189 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f lap..*/.case OP_
1918a 4d 6f 76 65 3a 20 7b 0a 20 20 63 68 61 72 20 2a Move: {. char *
1918b 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 6e 74 20 6e zMalloc;. int n
1918c 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e = pOp->p3;. in
1918d 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a t p1 = pOp->p1;.
1918e 20 20 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e int p2 = pOp->
1918f 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e p2;. assert( n>
19190 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
19191 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 1>0 );. assert(
19192 20 70 31 2b 6e 3c 70 2d 3e 6e 4d 65 6d 20 29 3b p1+n<p->nMem );
19193 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d . pIn1 = &p->aM
19194 65 6d 5b 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 em[p1];. assert
19195 28 20 70 32 3e 30 20 29 3b 0a 20 20 61 73 73 65 ( p2>0 );. asse
19196 72 74 28 20 70 32 2b 6e 3c 70 2d 3e 6e 4d 65 6d rt( p2+n<p->nMem
19197 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d );. pOut = &p-
19198 3e 61 4d 65 6d 5b 70 32 5d 3b 0a 20 20 61 73 73 >aMem[p2];. ass
19199 65 72 74 28 20 70 31 2b 6e 3c 3d 70 32 20 7c 7c ert( p1+n<=p2 ||
1919a 20 70 32 2b 6e 3c 3d 70 31 20 29 3b 0a 20 20 77 p2+n<=p1 );. w
1919b 68 69 6c 65 28 20 6e 2d 2d 20 29 7b 0a 20 20 20 hile( n-- ){.
1919c 20 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4f 75 74 2d zMalloc = pOut-
1919d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 70 4f >zMalloc;. pO
1919e 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b ut->zMalloc = 0;
1919f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
191a0 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 70 49 MemMove(pOut, pI
191a1 6e 31 29 3b 0a 20 20 20 20 70 49 6e 31 2d 3e 7a n1);. pIn1->z
191a2 4d 61 6c 6c 6f 63 20 3d 20 7a 4d 61 6c 6c 6f 63 Malloc = zMalloc
191a3 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 ;. REGISTER_T
191a4 52 41 43 45 28 70 32 2b 2b 2c 20 70 4f 75 74 29 RACE(p2++, pOut)
191a5 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 ;. pIn1++;.
191a6 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a 20 20 pOut++;. }.
191a7 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
191a8 6f 64 65 3a 20 43 6f 70 79 20 50 31 20 50 32 20 ode: Copy P1 P2
191a9 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 * * *.**.** Make
191aa 20 61 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 a copy of regis
191ab 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 ter P1 into regi
191ac 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 ster P2..**.** T
191ad 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
191ae 6d 61 6b 65 73 20 61 20 64 65 65 70 20 63 6f 70 makes a deep cop
191af 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 y of the value.
191b0 20 41 20 64 75 70 6c 69 63 61 74 65 0a 2a 2a 20 A duplicate.**
191b1 69 73 20 6d 61 64 65 20 6f 66 20 61 6e 79 20 73 is made of any s
191b2 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 63 6f tring or blob co
191b3 6e 73 74 61 6e 74 2e 20 20 53 65 65 20 61 6c 73 nstant. See als
191b4 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 63 o OP_SCopy..*/.c
191b5 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20 7b 0a 20 ase OP_Copy: {.
191b6 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
191b7 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
191b8 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p1<=p->nMem
191b9 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d );. pIn1 = &p-
191ba 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
191bb 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
191bc 32 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 2>0 );. assert(
191bd 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 pOp->p2<=p->nMe
191be 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 m );. pOut = &p
191bf 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b ->aMem[pOp->p2];
191c0 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 . assert( pOut!
191c1 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 =pIn1 );. sqlit
191c2 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
191c3 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c Copy(pOut, pIn1,
191c4 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 MEM_Ephem);. D
191c5 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f eephemeralize(pO
191c6 75 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f ut);. REGISTER_
191c7 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 TRACE(pOp->p2, p
191c8 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
191c9 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f ../* Opcode: SCo
191ca 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a py P1 P2 * * *.*
191cb 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c *.** Make a shal
191cc 6c 6f 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69 low copy of regi
191cd 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 ster P1 into reg
191ce 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ister P2..**.**
191cf 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
191d0 20 6d 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 makes a shallow
191d1 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c copy of the val
191d2 75 65 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 ue. If the valu
191d3 65 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 e.** is a string
191d4 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 or blob, then t
191d5 68 65 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 he copy is only
191d6 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
191d7 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 .** original and
191d8 20 68 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72 hence if the or
191d9 69 67 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 iginal changes s
191da 6f 20 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e o will the copy.
191db 0a 2a 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68 .** Worse, if th
191dc 65 20 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65 e original is de
191dd 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63 allocated, the c
191de 6f 70 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 opy becomes inva
191df 6c 69 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 lid..** Thus the
191e0 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 program must gu
191e1 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 arantee that the
191e2 20 6f 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e original will n
191e3 6f 74 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 ot change.** dur
191e4 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 ing the lifetime
191e5 20 6f 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 of the copy. U
191e6 73 65 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 se OP_Copy to ma
191e7 6b 65 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a ke a complete.**
191e8 20 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f copy..*/.case O
191e9 50 5f 53 43 6f 70 79 3a 20 7b 0a 20 20 61 73 73 P_SCopy: {. ass
191ea 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 ert( pOp->p1>0 )
191eb 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
191ec 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p1<=p->nMem );.
191ed 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 pIn1 = &p->aMe
191ee 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 52 45 m[pOp->p1];. RE
191ef 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
191f0 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 61 ->p1, pIn1);. a
191f1 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 ssert( pOp->p2>0
191f2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
191f3 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p2<=p->nMem )
191f4 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 ;. pOut = &p->a
191f5 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
191f6 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 assert( pOut!=pI
191f7 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 n1 );. sqlite3V
191f8 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 dbeMemShallowCop
191f9 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 y(pOut, pIn1, ME
191fa 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45 47 49 M_Ephem);. REGI
191fb 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
191fc 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65 p2, pOut);. bre
191fd 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
191fe 3a 20 52 65 73 75 6c 74 52 6f 77 20 50 31 20 50 : ResultRow P1 P
191ff 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 2 * * *.**.** Th
19200 65 20 72 65 67 69 73 74 65 72 73 20 50 31 20 74 e registers P1 t
19201 68 72 6f 75 67 68 20 50 31 2b 50 32 2d 31 20 63 hrough P1+P2-1 c
19202 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65 20 ontain a single
19203 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 6c 74 row of.** result
19204 73 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 63 s. This opcode c
19205 61 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 65 auses the sqlite
19206 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74 6f 3_step() call to
19207 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 69 terminate.** wi
19208 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f 57 th an SQLITE_ROW
19209 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 return code and
1920a 20 69 74 20 73 65 74 73 20 75 70 20 74 68 65 20 it sets up the
1920b 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a 20 sqlite3_stmt.**
1920c 73 74 72 75 63 74 75 72 65 20 74 6f 20 70 72 6f structure to pro
1920d 76 69 64 65 20 61 63 63 65 73 73 20 74 6f 20 74 vide access to t
1920e 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65 73 he top P1 values
1920f 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a as the result.*
19210 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 4f * row..*/.case O
19211 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0a 20 P_ResultRow: {.
19212 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e Mem *pMem;. in
19213 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t i;. assert( p
19214 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f ->nResColumn==pO
19215 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65 72 p->p2 );. asser
19216 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a t( pOp->p1>0 );.
19217 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
19218 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 1+pOp->p2<=p->nM
19219 65 6d 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 em );.. /* Inva
1921a 6c 69 64 61 74 65 20 61 6c 6c 20 65 70 68 65 6d lidate all ephem
1921b 65 72 61 6c 20 63 75 72 73 6f 72 20 72 6f 77 20 eral cursor row
1921c 63 61 63 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63 caches */. p->c
1921d 61 63 68 65 43 74 72 20 3d 20 28 70 2d 3e 63 61 acheCtr = (p->ca
1921e 63 68 65 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a cheCtr + 2)|1;..
1921f 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 /* Make sure t
19220 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 he results of th
19221 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 61 72 e current row ar
19222 65 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 e \000 terminate
19223 64 0a 20 20 2a 2a 20 61 6e 64 20 68 61 76 65 20 d. ** and have
19224 61 6e 20 61 73 73 69 67 6e 65 64 20 74 79 70 65 an assigned type
19225 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 61 . The results a
19226 72 65 20 64 65 2d 65 70 68 65 6d 65 72 61 6c 69 re de-ephemerali
19227 7a 65 64 20 61 73 0a 20 20 2a 2a 20 61 73 20 73 zed as. ** as s
19228 69 64 65 20 65 66 66 65 63 74 2e 0a 20 20 2a 2f ide effect.. */
19229 0a 20 20 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 . pMem = p->pRe
1922a 73 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d sultSet = &p->aM
1922b 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 em[pOp->p1];. f
1922c 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 70 2d 3e 70 or(i=0; i<pOp->p
1922d 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 2; i++){. sql
1922e 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
1922f 72 6d 69 6e 61 74 65 28 26 70 4d 65 6d 5b 69 5d rminate(&pMem[i]
19230 29 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 );. storeType
19231 49 6e 66 6f 28 26 70 4d 65 6d 5b 69 5d 2c 20 65 Info(&pMem[i], e
19232 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 ncoding);. RE
19233 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
19234 2d 3e 70 31 2b 69 2c 20 26 70 4d 65 6d 5b 69 5d ->p1+i, &pMem[i]
19235 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d );. }. if( db-
19236 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 >mallocFailed )
19237 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 goto no_mem;..
19238 2f 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 /* Return SQLITE
19239 5f 52 4f 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 6e _ROW. */. p->n
1923a 43 61 6c 6c 62 61 63 6b 2b 2b 3b 0a 20 20 70 2d Callback++;. p-
1923b 3e 70 63 20 3d 20 70 63 20 2b 20 31 3b 0a 20 20 >pc = pc + 1;.
1923c 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b rc = SQLITE_ROW;
1923d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 . goto vdbe_ret
1923e 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 urn;.}../* Opcod
1923f 65 3a 20 43 6f 6e 63 61 74 20 50 31 20 50 32 20 e: Concat P1 P2
19240 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 P3 * *.**.** Add
19241 20 74 68 65 20 74 65 78 74 20 69 6e 20 72 65 67 the text in reg
19242 69 73 74 65 72 20 50 31 20 6f 6e 74 6f 20 74 68 ister P1 onto th
19243 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74 65 78 e end of the tex
19244 74 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 t in.** register
19245 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68 P2 and store th
19246 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 e result in regi
19247 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 ster P3..** If e
19248 69 74 68 65 72 20 74 68 65 20 50 31 20 6f 72 20 ither the P1 or
19249 50 32 20 74 65 78 74 20 61 72 65 20 4e 55 4c 4c P2 text are NULL
1924a 20 74 68 65 6e 20 73 74 6f 72 65 20 4e 55 4c 4c then store NULL
1924b 20 69 6e 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 in P3..**.**
1924c 50 33 20 3d 20 50 32 20 7c 7c 20 50 31 0a 2a 2a P3 = P2 || P1.**
1924d 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 .** It is illega
1924e 6c 20 66 6f 72 20 50 31 20 61 6e 64 20 50 33 20 l for P1 and P3
1924f 74 6f 20 62 65 20 74 68 65 20 73 61 6d 65 20 72 to be the same r
19250 65 67 69 73 74 65 72 2e 20 53 6f 6d 65 74 69 6d egister. Sometim
19251 65 73 2c 0a 2a 2a 20 69 66 20 50 33 20 69 73 20 es,.** if P3 is
19252 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 74 65 the same registe
19253 72 20 61 73 20 50 32 2c 20 74 68 65 20 69 6d 70 r as P2, the imp
19254 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 lementation is a
19255 62 6c 65 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 ble.** to avoid
19256 61 20 6d 65 6d 63 70 79 28 29 2e 0a 2a 2f 0a 63 a memcpy()..*/.c
19257 61 73 65 20 4f 50 5f 43 6f 6e 63 61 74 3a 20 7b ase OP_Concat: {
19258 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
19259 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 2c me as TK_CONCAT,
1925a 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 in1, in2, out3
1925b 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 3b 0a */. i64 nByte;.
1925c 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21 . assert( pIn1!
1925d 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28 =pOut );. if( (
1925e 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 pIn1->flags | pI
1925f 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d n2->flags) & MEM
19260 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c _Null ){. sql
19261 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
19262 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 ll(pOut);. br
19263 65 61 6b 3b 0a 20 20 7d 0a 20 20 45 78 70 61 6e eak;. }. Expan
19264 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 53 dBlob(pIn1);. S
19265 74 72 69 6e 67 69 66 79 28 70 49 6e 31 2c 20 65 tringify(pIn1, e
19266 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 45 78 70 61 ncoding);. Expa
19267 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 ndBlob(pIn2);.
19268 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32 2c 20 Stringify(pIn2,
19269 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 6e 42 79 encoding);. nBy
1926a 74 65 20 3d 20 70 49 6e 31 2d 3e 6e 20 2b 20 70 te = pIn1->n + p
1926b 49 6e 32 2d 3e 6e 3b 0a 20 20 69 66 28 20 6e 42 In2->n;. if( nB
1926c 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 yte>db->aLimit[S
1926d 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
1926e 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 TH] ){. goto
1926f 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 4d too_big;. }. M
19270 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
19271 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 ut, MEM_Str);.
19272 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
19273 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e 42 79 emGrow(pOut, nBy
19274 74 65 2b 32 2c 20 70 4f 75 74 3d 3d 70 49 6e 32 te+2, pOut==pIn2
19275 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f ) ){. goto no
19276 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 _mem;. }. if(
19277 70 4f 75 74 21 3d 70 49 6e 32 20 29 7b 0a 20 20 pOut!=pIn2 ){.
19278 20 20 6d 65 6d 63 70 79 28 70 4f 75 74 2d 3e 7a memcpy(pOut->z
19279 2c 20 70 49 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d , pIn2->z, pIn2-
1927a 3e 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 >n);. }. memcp
1927b 79 28 26 70 4f 75 74 2d 3e 7a 5b 70 49 6e 32 2d y(&pOut->z[pIn2-
1927c 3e 6e 5d 2c 20 70 49 6e 31 2d 3e 7a 2c 20 70 49 >n], pIn1->z, pI
1927d 6e 31 2d 3e 6e 29 3b 0a 20 20 70 4f 75 74 2d 3e n1->n);. pOut->
1927e 7a 5b 6e 42 79 74 65 5d 20 3d 20 30 3b 0a 20 20 z[nByte] = 0;.
1927f 70 4f 75 74 2d 3e 7a 5b 6e 42 79 74 65 2b 31 5d pOut->z[nByte+1]
19280 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c = 0;. pOut->fl
19281 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
19282 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 6e 42 79 . pOut->n = nBy
19283 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 te;. pOut->enc
19284 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 = encoding;. UP
19285 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
19286 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b E(pOut);. break
19287 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
19288 41 64 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a Add P1 P2 P3 * *
19289 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 76 .**.** Add the v
1928a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1928b 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c 75 65 P1 to the value
1928c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a in register P2.
1928d 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 ** and store the
1928e 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 result in regis
1928f 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 ter P3..** If ei
19290 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 ther input is NU
19291 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 LL, the result i
19292 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 s NULL..*/./* Op
19293 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20 50 code: Multiply P
19294 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
19295 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 74 68 *.** Multiply th
19296 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
19297 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 ter P1 by the va
19298 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
19299 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 P2.** and store
1929a 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 the result in re
1929b 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 gister P3..** If
1929c 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 either input is
1929d 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c NULL, the resul
1929e 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a t is NULL..*/./*
1929f 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 61 63 Opcode: Subtrac
192a0 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a t P1 P2 P3 * *.*
192a1 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74 68 *.** Subtract th
192a2 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
192a3 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65 20 ter P1 from the
192a4 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
192a5 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 r P2.** and stor
192a6 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 e the result in
192a7 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 register P3..**
192a8 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 If either input
192a9 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 is NULL, the res
192aa 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a ult is NULL..*/.
192ab 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 69 64 /* Opcode: Divid
192ac 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a e P1 P2 P3 * *.*
192ad 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 65 20 *.** Divide the
192ae 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
192af 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 r P1 by the valu
192b0 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 e in register P2
192b1 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 .** and store th
192b2 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 e result in regi
192b3 73 74 65 72 20 50 33 2e 20 20 49 66 20 74 68 65 ster P3. If the
192b4 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
192b5 65 72 20 50 32 0a 2a 2a 20 69 73 20 7a 65 72 6f er P2.** is zero
192b6 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c , then the resul
192b7 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 t is NULL..** If
192b8 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 either input is
192b9 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c NULL, the resul
192ba 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a t is NULL..*/./*
192bb 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 69 6e 64 Opcode: Remaind
192bc 65 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a er P1 P2 P3 * *.
192bd 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 **.** Compute th
192be 65 20 72 65 6d 61 69 6e 64 65 72 20 61 66 74 65 e remainder afte
192bf 72 20 69 6e 74 65 67 65 72 20 64 69 76 69 73 69 r integer divisi
192c0 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 on of the value
192c1 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 in.** register P
192c2 31 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 1 by the value i
192c3 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e n register P2 an
192c4 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 d store the resu
192c5 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a 20 49 66 lt in P3. .** If
192c6 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
192c7 67 69 73 74 65 72 20 50 32 20 69 73 20 7a 65 72 gister P2 is zer
192c8 6f 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 o the result is
192c9 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69 74 68 NULL..** If eith
192ca 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 er operand is NU
192cb 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 LL, the result i
192cc 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 s NULL..*/.case
192cd 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 OP_Add:
192ce 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
192cf 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c 20 69 6e e as TK_PLUS, in
192d0 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 1, in2, out3 */.
192d1 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 case OP_Subtract
192d2 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f : /
192d3 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e * same as TK_MIN
192d4 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 US, in1, in2, ou
192d5 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 t3 */.case OP_Mu
192d6 6c 74 69 70 6c 79 3a 20 20 20 20 20 20 20 20 20 ltiply:
192d7 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
192d8 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c 20 69 6e TK_STAR, in1, in
192d9 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 2, out3 */.case
192da 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 20 20 20 OP_Divide:
192db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
192dc 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 2c 20 69 e as TK_SLASH, i
192dd 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
192de 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 69 6e 64 .case OP_Remaind
192df 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 er: {
192e0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 /* same as TK_RE
192e1 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 M, in1, in2, out
192e2 33 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3 */. int flags
192e3 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 ;. applyNumeric
192e4 41 66 66 69 6e 69 74 79 28 70 49 6e 31 29 3b 0a Affinity(pIn1);.
192e5 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 applyNumericAf
192e6 66 69 6e 69 74 79 28 70 49 6e 32 29 3b 0a 20 20 finity(pIn2);.
192e7 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e 66 6c flags = pIn1->fl
192e8 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 ags | pIn2->flag
192e9 73 3b 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20 s;. if( (flags
192ea 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 & MEM_Null)!=0 )
192eb 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 goto arithmetic
192ec 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b _result_is_null;
192ed 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c . if( (pIn1->fl
192ee 61 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c 61 67 ags & pIn2->flag
192ef 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45 s & MEM_Int)==ME
192f0 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 69 36 34 M_Int ){. i64
192f1 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20 70 a, b;. a = p
192f2 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 62 20 In1->u.i;. b
192f3 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 = pIn2->u.i;.
192f4 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 switch( pOp->op
192f5 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 63 61 code ){. ca
192f6 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 se OP_Add:
192f7 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20 20 20 b += a;
192f8 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
192f9 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 se OP_Subtract:
192fa 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20 20 20 b -= a;
192fb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
192fc 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 se OP_Multiply:
192fd 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20 20 20 b *= a;
192fe 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
192ff 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a se OP_Divide: {.
19300 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d 30 if( a==0
19301 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 ) goto arithmet
19302 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c ic_result_is_nul
19303 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69 l;. /* Di
19304 76 69 64 69 6e 67 20 74 68 65 20 6c 61 72 67 65 viding the large
19305 73 74 20 70 6f 73 73 69 62 6c 65 20 6e 65 67 61 st possible nega
19306 74 69 76 65 20 36 34 2d 62 69 74 20 69 6e 74 65 tive 64-bit inte
19307 67 65 72 20 28 31 3c 3c 36 33 29 20 62 79 20 0a ger (1<<63) by .
19308 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20 72 65 ** -1 re
19309 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 turns an integer
1930a 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 73 74 too large to st
1930b 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69 74 20 ore in a 64-bit
1930c 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a 20 20 data-type. On.
1930d 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20 61 72 ** some ar
1930e 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 65 chitectures, the
1930f 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f 77 73 value overflows
19310 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f 6e 20 to (1<<63). On
19311 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20 20 20 others,.
19312 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73 20 69 ** a SIGFPE is i
19313 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c 6c 6f ssued. The follo
19314 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 6e wing statement n
19315 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73 0a 20 ormalizes this.
19316 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61 76 69 ** behavi
19317 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 61 or so that all a
19318 72 63 68 69 74 65 63 74 75 72 65 73 20 62 65 68 rchitectures beh
19319 61 76 65 20 61 73 20 69 66 20 69 6e 74 65 67 65 ave as if intege
1931a 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 76 r . ** ov
1931b 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65 64 2e erflow occurred.
1931c 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
1931d 20 20 20 20 69 66 28 20 61 3d 3d 2d 31 20 26 26 if( a==-1 &&
1931e 20 62 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 b==SMALLEST_INT
1931f 36 34 20 29 20 61 20 3d 20 31 3b 0a 20 20 20 20 64 ) a = 1;.
19320 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20 20 20 20 b /= a;.
19321 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
19322 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 }. default
19323 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 : {. if(
19324 61 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 a==0 ) goto arit
19325 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 hmetic_result_is
19326 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 _null;. i
19327 66 28 20 61 3d 3d 2d 31 20 29 20 61 20 3d 20 31 f( a==-1 ) a = 1
19328 3b 0a 20 20 20 20 20 20 20 20 62 20 25 3d 20 61 ;. b %= a
19329 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
1932a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1932b 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 62 pOut->u.i = b
1932c 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 ;. MemSetType
1932d 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1932e 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 nt);. }else{.
1932f 20 20 64 6f 75 62 6c 65 20 61 2c 20 62 3b 0a 20 double a, b;.
19330 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33 56 64 a = sqlite3Vd
19331 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 beRealValue(pIn1
19332 29 3b 0a 20 20 20 20 62 20 3d 20 73 71 6c 69 74 );. b = sqlit
19333 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
19334 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69 74 63 pIn2);. switc
19335 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 h( pOp->opcode )
19336 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f {. case OP_
19337 41 64 64 3a 20 20 20 20 20 20 20 20 20 62 20 2b Add: b +
19338 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b = a; break
19339 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f ;. case OP_
1933a 53 75 62 74 72 61 63 74 3a 20 20 20 20 62 20 2d Subtract: b -
1933b 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b = a; break
1933c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f ;. case OP_
1933d 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 62 20 2a Multiply: b *
1933e 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b = a; break
1933f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f ;. case OP_
19340 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 Divide: {.
19341 20 20 69 66 28 20 61 3d 3d 30 2e 30 20 29 20 67 if( a==0.0 ) g
19342 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 oto arithmetic_r
19343 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 esult_is_null;.
19344 20 20 20 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20 b /= a;.
19345 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
19346 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 }. defa
19347 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 ult: {. i
19348 36 34 20 69 61 20 3d 20 28 69 36 34 29 61 3b 0a 64 ia = (i64)a;.
19349 20 20 20 20 20 20 20 20 69 36 34 20 69 62 20 3d i64 ib =
1934a 20 28 69 36 34 29 62 3b 0a 20 20 20 20 20 20 20 (i64)b;.
1934b 20 69 66 28 20 69 61 3d 3d 30 20 29 20 67 6f 74 if( ia==0 ) got
1934c 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 o arithmetic_res
1934d 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 ult_is_null;.
1934e 20 20 20 20 20 69 66 28 20 69 61 3d 3d 2d 31 20 if( ia==-1
1934f 29 20 69 61 20 3d 20 31 3b 0a 20 20 20 20 20 20 ) ia = 1;.
19350 20 20 62 20 3d 20 69 62 20 25 20 69 61 3b 0a 20 b = ib % ia;.
19351 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
19352 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
19353 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e if( sqlite3IsNaN
19354 28 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 (b) ){. got
19355 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 o arithmetic_res
19356 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 ult_is_null;.
19357 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e 72 20 3d }. pOut->r =
19358 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 b;. MemSetTy
19359 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1935a 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 _Real);. if(
1935b 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 (flags & MEM_Rea
1935c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 l)==0 ){. s
1935d 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 qlite3VdbeIntege
1935e 72 41 66 66 69 6e 69 74 79 28 70 4f 75 74 29 3b rAffinity(pOut);
1935f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 . }. }. bre
19360 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f ak;..arithmetic_
19361 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a result_is_null:.
19362 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19363 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 SetNull(pOut);.
19364 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
19365 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 code: CollSeq *
19366 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 * P4.**.** P4 is
19367 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
19368 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e 20 CollSeq struct.
19369 49 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c If the next call
1936a 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63 74 to a user funct
1936b 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 ion.** or aggreg
1936c 61 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 ate calls sqlite
1936d 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 3GetFuncCollSeq(
1936e 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f ), this collatio
1936f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a n sequence will.
19370 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 ** be returned.
19371 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
19372 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e the built-in min
19373 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 (), max() and nu
19374 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 llif().** functi
19375 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 ons..**.** The i
19376 6e 74 65 72 66 61 63 65 20 75 73 65 64 20 62 79 nterface used by
19377 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
19378 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f 72 65 ion of the afore
19379 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 mentioned functi
1937a 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 ons.** to retrie
1937b 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e ve the collation
1937c 20 73 65 71 75 65 6e 63 65 20 73 65 74 20 62 79 sequence set by
1937d 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 this opcode is
1937e 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a not available.**
1937f 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 publicly, only
19380 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e to user function
19381 73 20 64 65 66 69 6e 65 64 20 69 6e 20 66 75 6e s defined in fun
19382 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f c.c..*/.case OP_
19383 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73 CollSeq: {. ass
19384 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
19385 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a ==P4_COLLSEQ );.
19386 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
19387 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20 pcode: Function
19388 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
19389 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73 *.** Invoke a us
1938a 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 34 20 er function (P4
1938b 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1938c 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 75 63 a Function struc
1938d 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 64 65 66 ture that.** def
1938e 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f ines the functio
1938f 6e 29 20 77 69 74 68 20 50 35 20 61 72 67 75 6d n) with P5 argum
19390 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 ents taken from
19391 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 0a register P2 and.
19392 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 20 20 ** successors.
19393 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 The result of th
19394 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 74 e function is st
19395 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
19396 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72 P3..** Register
19397 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 P3 must not be
19398 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 one of the funct
19399 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a ion inputs..**.*
1939a 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62 69 74 * P1 is a 32-bit
1939b 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 bitmask indicat
1939c 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e ing whether or n
1939d 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74 ot each argument
1939e 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63 to the .** func
1939f 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d 69 tion was determi
193a0 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 ned to be consta
193a1 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 nt at compile ti
193a2 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72 73 74 me. If the first
193a3 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61 73 .** argument was
193a4 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 62 constant then b
193a5 69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73 65 it 0 of P1 is se
193a6 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 t. This is used
193a7 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 to determine.**
193a8 77 68 65 74 68 65 72 20 6d 65 74 61 20 64 61 74 whether meta dat
193a9 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
193aa 68 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f h a user functio
193ab 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e 67 n argument using
193ac 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the.** sqlite3_
193ad 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 50 set_auxdata() AP
193ae 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20 I may be safely
193af 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 74 retained until t
193b0 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63 he next.** invoc
193b1 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70 ation of this op
193b2 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 code..**.** See
193b3 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61 6e also: AggStep an
193b4 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61 d AggFinal.*/.ca
193b5 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20 se OP_Function:
193b6 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d {. int i;. Mem
193b7 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 *pArg;. sqlite
193b8 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 3_context ctx;.
193b9 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
193ba 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 20 *apVal;. int n
193bb 3d 20 70 4f 70 2d 3e 70 35 3b 0a 0a 20 20 61 70 = pOp->p5;.. ap
193bc 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a Val = p->apArg;.
193bd 20 20 61 73 73 65 72 74 28 20 61 70 56 61 6c 20 assert( apVal
193be 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20 20 61 73 || n==0 );.. as
193bf 73 65 72 74 28 20 6e 3d 3d 30 20 7c 7c 20 28 70 sert( n==0 || (p
193c0 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d Op->p2>0 && pOp-
193c1 3e 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d 29 20 >p2+n<=p->nMem)
193c2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
193c3 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20 ->p3<pOp->p2 ||
193c4 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32 pOp->p3>=pOp->p2
193c5 2b 6e 20 29 3b 0a 20 20 70 41 72 67 20 3d 20 26 +n );. pArg = &
193c6 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d p->aMem[pOp->p2]
193c7 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e ;. for(i=0; i<n
193c8 3b 20 69 2b 2b 2c 20 70 41 72 67 2b 2b 29 7b 0a ; i++, pArg++){.
193c9 20 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 apVal[i] = p
193ca 41 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 Arg;. storeTy
193cb 70 65 49 6e 66 6f 28 70 41 72 67 2c 20 65 6e 63 peInfo(pArg, enc
193cc 6f 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 47 49 oding);. REGI
193cd 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
193ce 70 32 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a 0a p2, pArg);. }..
193cf 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
193d0 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 4type==P4_FUNCDE
193d1 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65 F || pOp->p4type
193d2 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43 20 29 3b ==P4_VDBEFUNC );
193d3 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 . if( pOp->p4ty
193d4 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 29 pe==P4_FUNCDEF )
193d5 7b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20 {. ctx.pFunc
193d6 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b = pOp->p4.pFunc;
193d7 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75 . ctx.pVdbeFu
193d8 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b nc = 0;. }else{
193d9 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75 . ctx.pVdbeFu
193da 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a 29 nc = (VdbeFunc*)
193db 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e pOp->p4.pVdbeFun
193dc 63 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 c;. ctx.pFunc
193dd 20 3d 20 63 74 78 2e 70 56 64 62 65 46 75 6e 63 = ctx.pVdbeFunc
193de 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 ->pFunc;. }..
193df 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
193e0 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 0 && pOp->p3<=p-
193e1 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20 >nMem );. pOut
193e2 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
193e3 70 33 5d 3b 0a 20 20 63 74 78 2e 73 2e 66 6c 61 p3];. ctx.s.fla
193e4 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
193e5 20 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a ctx.s.db = db;.
193e6 20 20 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 ctx.s.xDel = 0
193e7 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f ;. ctx.s.zMallo
193e8 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 c = 0;.. /* The
193e9 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 output cell may
193ea 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 already have a
193eb 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 buffer allocated
193ec 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 . Move. ** the
193ed 70 6f 69 6e 74 65 72 20 74 6f 20 63 74 78 2e 73 pointer to ctx.s
193ee 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 20 so in case the
193ef 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 63 61 user-function ca
193f0 6e 20 75 73 65 0a 20 20 2a 2a 20 74 68 65 20 61 n use. ** the a
193f1 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 lready allocated
193f2 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 buffer instead
193f3 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 of allocating a
193f4 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 new one.. */.
193f5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f sqlite3VdbeMemMo
193f6 76 65 28 26 63 74 78 2e 73 2c 20 70 4f 75 74 29 ve(&ctx.s, pOut)
193f7 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c ;. MemSetTypeFl
193f8 61 67 28 26 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e ag(&ctx.s, MEM_N
193f9 75 6c 6c 29 3b 0a 0a 20 20 63 74 78 2e 69 73 45 ull);.. ctx.isE
193fa 72 72 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 rror = 0;. if(
193fb 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 ctx.pFunc->flags
193fc 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e & SQLITE_FUNC_N
193fd 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 EEDCOLL ){. a
193fe 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f ssert( pOp>p->aO
193ff 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 p );. assert(
19400 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d pOp[-1].p4type=
19401 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 =P4_COLLSEQ );.
19402 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d assert( pOp[-
19403 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 1].opcode==OP_Co
19404 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 78 llSeq );. ctx
19405 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d .pColl = pOp[-1]
19406 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 .p4.pColl;. }.
19407 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
19408 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
19409 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1940a 73 75 73 65 3b 0a 20 20 28 2a 63 74 78 2e 70 46 suse;. (*ctx.pF
1940b 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 63 74 78 unc->xFunc)(&ctx
1940c 2c 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 , n, apVal);. i
1940d 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1940e 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73 71 On(db) ){. sq
1940f 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19410 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 20 ase(&ctx.s);.
19411 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
19412 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 to_misuse;. }.
19413 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
19414 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 ailed ){. /*
19415 45 76 65 6e 20 74 68 6f 75 67 68 20 61 20 6d 61 Even though a ma
19416 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 lloc() has faile
19417 64 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 d, the implement
19418 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 20 ation of the.
19419 20 2a 2a 20 75 73 65 72 20 66 75 6e 63 74 69 6f ** user functio
1941a 6e 20 6d 61 79 20 68 61 76 65 20 63 61 6c 6c 65 n may have calle
1941b 64 20 61 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 d an sqlite3_res
1941c 75 6c 74 5f 58 58 58 28 29 20 66 75 6e 63 74 69 ult_XXX() functi
1941d 6f 6e 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 74 on. ** to ret
1941e 75 72 6e 20 61 20 76 61 6c 75 65 2e 20 54 68 65 urn a value. The
1941f 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 following call
19420 72 65 6c 65 61 73 65 73 20 61 6e 79 20 72 65 73 releases any res
19421 6f 75 72 63 65 73 0a 20 20 20 20 2a 2a 20 61 73 ources. ** as
19422 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 75 sociated with su
19423 63 68 20 61 20 76 61 6c 75 65 2e 0a 20 20 20 20 ch a value..
19424 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 **. ** Note:
19425 4d 61 79 62 65 20 4d 65 6d 52 65 6c 65 61 73 65 Maybe MemRelease
19426 28 29 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c () should be cal
19427 6c 65 64 20 69 66 20 73 71 6c 69 74 65 33 53 61 led if sqlite3Sa
19428 66 65 74 79 4f 6e 28 29 0a 20 20 20 20 2a 2a 20 fetyOn(). **
19429 66 61 69 6c 73 20 61 6c 73 6f 20 28 74 68 65 20 fails also (the
1942a 69 66 28 2e 2e 2e 29 20 73 74 61 74 65 6d 65 6e if(...) statemen
1942b 74 20 61 62 6f 76 65 29 2e 20 42 75 74 20 69 66 t above). But if
1942c 20 70 65 6f 70 6c 65 20 61 72 65 0a 20 20 20 20 people are.
1942d 2a 2a 20 6d 69 73 75 73 69 6e 67 20 73 71 6c 69 ** misusing sqli
1942e 74 65 2c 20 74 68 65 79 20 68 61 76 65 20 62 69 te, they have bi
1942f 67 67 65 72 20 70 72 6f 62 6c 65 6d 73 20 74 68 gger problems th
19430 61 6e 20 61 20 6c 65 61 6b 65 64 20 76 61 6c 75 an a leaked valu
19431 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 e.. */. sq
19432 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19433 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 20 ase(&ctx.s);.
19434 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
19435 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 61 }.. /* If any a
19436 75 78 69 6c 69 61 72 79 20 64 61 74 61 20 66 75 uxiliary data fu
19437 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 nctions have bee
19438 6e 20 63 61 6c 6c 65 64 20 62 79 20 74 68 69 73 n called by this
19439 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c 0a user function,.
1943a 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 ** immediately
1943b 20 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 call the destru
1943c 63 74 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f 6e ctor for any non
1943d 2d 73 74 61 74 69 63 20 76 61 6c 75 65 73 2e 0a -static values..
1943e 20 20 2a 2f 0a 20 20 69 66 28 20 63 74 78 2e 70 */. if( ctx.p
1943f 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 VdbeFunc ){.
19440 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
19441 65 41 75 78 44 61 74 61 28 63 74 78 2e 70 56 64 eAuxData(ctx.pVd
19442 62 65 46 75 6e 63 2c 20 70 4f 70 2d 3e 70 31 29 beFunc, pOp->p1)
19443 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 56 ;. pOp->p4.pV
19444 64 62 65 46 75 6e 63 20 3d 20 63 74 78 2e 70 56 dbeFunc = ctx.pV
19445 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70 dbeFunc;. pOp
19446 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56 44 ->p4type = P4_VD
19447 42 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f BEFUNC;. }.. /
19448 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f * If the functio
19449 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72 n returned an er
1944a 72 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 ror, throw an ex
1944b 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 ception */. if(
1944c 20 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a ctx.isError ){.
1944d 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
1944e 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1944f 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 , db, "%s", sqli
19450 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 te3_value_text(&
19451 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 ctx.s));. rc
19452 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 = ctx.isError;.
19453 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 }.. /* Copy th
19454 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 e result of the
19455 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72 65 function into re
19456 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 73 gister P3 */. s
19457 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
19458 45 6e 63 6f 64 69 6e 67 28 26 63 74 78 2e 73 2c Encoding(&ctx.s,
19459 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 encoding);. sq
1945a 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
1945b 28 70 4f 75 74 2c 20 26 63 74 78 2e 73 29 3b 0a (pOut, &ctx.s);.
1945c 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1945d 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f 75 74 29 eMemTooBig(pOut)
1945e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f ){. goto too
1945f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52 45 47 49 _big;. }. REGI
19460 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
19461 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 44 p3, pOut);. UPD
19462 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
19463 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b (pOut);. break;
19464 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 .}../* Opcode: B
19465 69 74 41 6e 64 20 50 31 20 50 32 20 50 33 20 2a itAnd P1 P2 P3 *
19466 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 *.**.** Take th
19467 65 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20 6f e bit-wise AND o
19468 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 f the values in
19469 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 register P1 and
1946a 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 P2 and.** store
1946b 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 the result in re
1946c 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 gister P3..** If
1946d 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 either input is
1946e 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c NULL, the resul
1946f 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a t is NULL..*/./*
19470 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20 50 Opcode: BitOr P
19471 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
19472 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d 77 * Take the bit-w
19473 69 73 65 20 4f 52 20 6f 66 20 74 68 65 20 76 61 ise OR of the va
19474 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 lues in register
19475 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a P1 and P2 and.*
19476 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 * store the resu
19477 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 lt in register P
19478 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 3..** If either
19479 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 input is NULL, t
1947a 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c he result is NUL
1947b 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a L..*/./* Opcode:
1947c 20 53 68 69 66 74 4c 65 66 74 20 50 31 20 50 32 ShiftLeft P1 P2
1947d 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 P3 * *.**.** Sh
1947e 69 66 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 ift the integer
1947f 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
19480 72 20 50 32 20 74 6f 20 74 68 65 20 6c 65 66 74 r P2 to the left
19481 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 by the.** numbe
19482 72 20 6f 66 20 62 69 74 73 20 73 70 65 63 69 66 r of bits specif
19483 69 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 67 ied by the integ
19484 65 72 20 69 6e 20 72 65 67 69 73 65 72 20 50 31 er in regiser P1
19485 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 72 ..** Store the r
19486 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 esult in registe
19487 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 r P3..** If eith
19488 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
19489 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 , the result is
1948a 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f NULL..*/./* Opco
1948b 64 65 3a 20 53 68 69 66 74 52 69 67 68 74 20 50 de: ShiftRight P
1948c 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1948d 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65 * Shift the inte
1948e 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67 ger value in reg
1948f 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20 ister P2 to the
19490 72 69 67 68 74 20 62 79 20 74 68 65 0a 2a 2a 20 right by the.**
19491 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 number of bits s
19492 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 pecified by the
19493 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 integer in regis
19494 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 ter P1..** Store
19495 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
19496 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
19497 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
19498 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
19499 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 lt is NULL..*/.c
1949a 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 ase OP_BitAnd:
1949b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1949c 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 * same as TK_BIT
1949d 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f AND, in1, in2, o
1949e 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 ut3 */.case OP_B
1949f 69 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20 20 itOr:
194a0 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
194a1 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69 6e 31 2c s TK_BITOR, in1,
194a2 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 in2, out3 */.ca
194a3 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a se OP_ShiftLeft:
194a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
194a5 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 same as TK_LSHI
194a6 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 FT, in1, in2, ou
194a7 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68 t3 */.case OP_Sh
194a8 69 66 74 52 69 67 68 74 3a 20 7b 20 20 20 20 20 iftRight: {
194a9 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
194aa 20 54 4b 5f 52 53 48 49 46 54 2c 20 69 6e 31 2c TK_RSHIFT, in1,
194ab 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 in2, out3 */.
194ac 69 36 34 20 61 2c 20 62 3b 0a 0a 20 20 69 66 28 i64 a, b;.. if(
194ad 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 (pIn1->flags |
194ae 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d pIn2->flags) & M
194af 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 EM_Null ){. s
194b0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
194b1 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 Null(pOut);.
194b2 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 61 20 3d break;. }. a =
194b3 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
194b4 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 62 20 alue(pIn2);. b
194b5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 = sqlite3VdbeInt
194b6 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 73 Value(pIn1);. s
194b7 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f witch( pOp->opco
194b8 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f de ){. case O
194b9 50 5f 42 69 74 41 6e 64 3a 20 20 20 20 20 20 61 P_BitAnd: a
194ba 20 26 3d 20 62 3b 20 20 20 20 20 62 72 65 61 6b &= b; break
194bb 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 ;. case OP_Bi
194bc 74 4f 72 3a 20 20 20 20 20 20 20 61 20 7c 3d 20 tOr: a |=
194bd 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 b; break;.
194be 20 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c case OP_ShiftL
194bf 65 66 74 3a 20 20 20 61 20 3c 3c 3d 20 62 3b 20 eft: a <<= b;
194c0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 break;. de
194c1 66 61 75 6c 74 3a 20 20 61 73 73 65 72 74 28 20 fault: assert(
194c2 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
194c3 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 ShiftRight );.
194c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
194c5 20 20 20 20 20 20 20 61 20 3e 3e 3d 20 62 3b 20 a >>= b;
194c6 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 break;. }.
194c7 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 61 3b 0a 20 pOut->u.i = a;.
194c8 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
194c9 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a pOut, MEM_Int);.
194ca 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
194cb 70 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 20 50 pcode: AddImm P
194cc 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 1 P2 * * *.** .*
194cd 2a 20 41 64 64 20 74 68 65 20 63 6f 6e 73 74 61 * Add the consta
194ce 6e 74 20 50 32 20 74 6f 20 74 68 65 20 76 61 6c nt P2 to the val
194cf 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
194d0 31 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 1..** The result
194d1 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69 6e is always an in
194d2 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 teger..**.** To
194d3 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73 74 force any regist
194d4 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 er to be an inte
194d5 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30 2e ger, just add 0.
194d6 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 49 .*/.case OP_AddI
194d7 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 mm: {
194d8 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 71 6c /* in1 */. sql
194d9 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
194da 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 erify(pIn1);. p
194db 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d In1->u.i += pOp-
194dc 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a >p2;. break;.}.
194dd 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 72 63 ./* Opcode: Forc
194de 65 49 6e 74 20 50 31 20 50 32 20 50 33 20 2a 20 eInt P1 P2 P3 *
194df 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 *.**.** Convert
194e0 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
194e1 72 20 50 31 20 69 6e 74 6f 20 61 6e 20 69 6e 74 r P1 into an int
194e2 65 67 65 72 2e 20 20 49 66 20 74 68 65 20 76 61 eger. If the va
194e3 6c 75 65 20 0a 2a 2a 20 69 6e 20 50 31 20 69 73 lue .** in P1 is
194e4 20 6e 6f 74 20 6e 75 6d 65 72 69 63 20 28 6d 65 not numeric (me
194e5 61 6e 69 6e 67 20 74 68 61 74 20 69 73 20 69 73 aning that is is
194e6 20 61 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72 a NULL or a str
194e7 69 6e 67 20 74 68 61 74 0a 2a 2a 20 64 6f 65 73 ing that.** does
194e8 20 6e 6f 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 not look like a
194e9 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f n integer or flo
194ea 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 ating point numb
194eb 65 72 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 er) then.** jump
194ec 20 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65 20 to P2. If the
194ed 76 61 6c 75 65 20 69 6e 20 50 31 20 69 73 20 6e value in P1 is n
194ee 75 6d 65 72 69 63 20 74 68 65 6e 0a 2a 2a 20 63 umeric then.** c
194ef 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20 74 onvert it into t
194f0 68 65 20 6c 65 61 73 74 20 69 6e 74 65 67 65 72 he least integer
194f1 20 74 68 61 74 20 69 73 20 67 72 65 61 74 65 72 that is greater
194f2 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
194f3 6f 20 69 74 73 0a 2a 2a 20 63 75 72 72 65 6e 74 o its.** current
194f4 20 76 61 6c 75 65 20 69 66 20 50 33 3d 3d 30 2c value if P3==0,
194f5 20 6f 72 20 74 6f 20 74 68 65 20 6c 65 61 73 74 or to the least
194f6 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 integer that is
194f7 20 73 74 72 69 63 74 6c 79 0a 2a 2a 20 67 72 65 strictly.** gre
194f8 61 74 65 72 20 74 68 61 6e 20 69 74 73 20 63 75 ater than its cu
194f9 72 72 65 6e 74 20 76 61 6c 75 65 20 69 66 20 50 rrent value if P
194fa 33 3d 3d 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 3==1..*/.case OP
194fb 5f 46 6f 72 63 65 49 6e 74 3a 20 7b 20 20 20 20 _ForceInt: {
194fc 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
194fd 20 69 6e 31 20 2a 2f 0a 20 20 69 36 34 20 76 3b in1 */. i64 v;
194fe 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 . applyAffinity
194ff 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 (pIn1, SQLITE_AF
19500 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 F_NUMERIC, encod
19501 69 6e 67 29 3b 0a 20 20 69 66 28 20 28 70 49 6e ing);. if( (pIn
19502 31 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 1->flags & (MEM_
19503 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d Int|MEM_Real))==
19504 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 0 ){. pc = pO
19505 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 62 p->p2 - 1;. b
19506 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 reak;. }. if(
19507 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
19508 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 76 20 3d M_Int ){. v =
19509 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 20 28 70 4f pIn1->u.i + (pO
1950a 70 2d 3e 70 33 21 3d 30 29 3b 0a 20 20 7d 65 6c p->p3!=0);. }el
1950b 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
1950c 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1950d 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 76 20 M_Real );. v
1950e 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 = (sqlite3_int64
1950f 29 70 49 6e 31 2d 3e 72 3b 0a 20 20 20 20 69 66 )pIn1->r;. if
19510 28 20 70 49 6e 31 2d 3e 72 3e 28 64 6f 75 62 6c ( pIn1->r>(doubl
19511 65 29 76 20 29 20 76 2b 2b 3b 0a 20 20 20 20 69 e)v ) v++;. i
19512 66 28 20 70 4f 70 2d 3e 70 33 20 26 26 20 70 49 f( pOp->p3 && pI
19513 6e 31 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65 29 76 n1->r==(double)v
19514 20 29 20 76 2b 2b 3b 0a 20 20 7d 0a 20 20 70 49 ) v++;. }. pI
19515 6e 31 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 4d n1->u.i = v;. M
19516 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 emSetTypeFlag(pI
19517 6e 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 n1, MEM_Int);.
19518 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
19519 6f 64 65 3a 20 4d 75 73 74 42 65 49 6e 74 20 50 ode: MustBeInt P
1951a 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 1 P2 * * *.** .*
1951b 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 * Force the valu
1951c 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1951d 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 to be an intege
1951e 72 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 r. If the value
1951f 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 6e 6f 74 .** in P1 is not
19520 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20 an integer and
19521 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 cannot be conver
19522 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 ted into an inte
19523 67 65 72 0a 2a 2a 20 77 69 74 68 6f 75 74 20 64 ger.** without d
19524 61 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e 20 6a ata loss, then j
19525 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ump immediately
19526 74 6f 20 50 32 2c 20 6f 72 20 69 66 20 50 32 3d to P2, or if P2=
19527 3d 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20 53 =0.** raise an S
19528 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 65 QLITE_MISMATCH e
19529 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 xception..*/.cas
1952a 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 3a 20 e OP_MustBeInt:
1952b 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1952c 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 jump, in1 */. a
1952d 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e pplyAffinity(pIn
1952e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 1, SQLITE_AFF_NU
1952f 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 29 MERIC, encoding)
19530 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 ;. if( (pIn1->f
19531 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d lags & MEM_Int)=
19532 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f =0 ){. if( pO
19533 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 p->p2==0 ){.
19534 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 rc = SQLITE_MI
19535 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67 6f SMATCH;. go
19536 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
19537 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 error;. }else
19538 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 {. pc = pOp
19539 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a ->p2 - 1;. }.
1953a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65 6d }else{. Mem
1953b 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e 31 SetTypeFlag(pIn1
1953c 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a , MEM_Int);. }.
1953d 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1953e 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69 6e pcode: RealAffin
1953f 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a ity P1 * * * *.*
19540 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65 72 *.** If register
19541 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 P1 holds an int
19542 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74 20 eger convert it
19543 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e to a real value.
19544 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
19545 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 de is used when
19546 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f 72 extracting infor
19547 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 6f mation from a co
19548 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 lumn that.** has
19549 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e 20 REAL affinity.
1954a 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61 6c Such column val
1954b 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 ues may still be
1954c 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69 6e stored as.** in
1954d 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61 63 tegers, for spac
1954e 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62 75 e efficiency, bu
1954f 74 20 61 66 74 65 72 20 65 78 74 72 61 63 74 69 t after extracti
19550 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d 0a on we want them.
19551 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 20 ** to have only
19552 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f a real value..*/
19553 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 .case OP_RealAff
19554 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20 20 inity: {
19555 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 /* in1
19556 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e */. if( pIn1->
19557 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 flags & MEM_Int
19558 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
19559 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e beMemRealify(pIn
1955a 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1);. }. break;
1955b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
1955c 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 TE_OMIT_CAST./*
1955d 4f 70 63 6f 64 65 3a 20 54 6f 54 65 78 74 20 50 Opcode: ToText P
1955e 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
1955f 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 Force the value
19560 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 in register P1 t
19561 6f 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20 49 66 o be text..** If
19562 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 the value is nu
19563 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 meric, convert i
19564 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73 t to a string us
19565 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 ing the.** equiv
19566 61 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74 66 28 alent of printf(
19567 29 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65 73 20 ). Blob values
19568 61 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e are unchanged an
19569 64 0a 2a 2a 20 61 72 65 20 61 66 74 65 72 77 61 d.** are afterwa
1956a 72 64 73 20 73 69 6d 70 6c 79 20 69 6e 74 65 72 rds simply inter
1956b 70 72 65 74 65 64 20 61 73 20 74 65 78 74 2e 0a preted as text..
1956c 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c **.** A NULL val
1956d 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 ue is not change
1956e 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e d by this routin
1956f 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e e. It remains N
19570 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
19571 54 6f 54 65 78 74 3a 20 7b 20 20 20 20 20 20 20 ToText: {
19572 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
19573 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 54 me as TK_TO_TEXT
19574 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 , in1 */. if( p
19575 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
19576 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 _Null ) break;.
19577 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74 72 assert( MEM_Str
19578 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 ==(MEM_Blob>>3)
19579 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 );. pIn1->flags
1957a 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 |= (pIn1->flags
1957b 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 &MEM_Blob)>>3;.
1957c 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 applyAffinity(p
1957d 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f In1, SQLITE_AFF_
1957e 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b TEXT, encoding);
1957f 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c . rc = ExpandBl
19580 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 ob(pIn1);. asse
19581 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 rt( pIn1->flags
19582 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d & MEM_Str || db-
19583 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
19584 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 . pIn1->flags &
19585 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f = ~(MEM_Int|MEM_
19586 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 3b 0a Real|MEM_Blob);.
19587 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
19588 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 BSIZE(pIn1);. b
19589 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1958a 64 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 2a 20 de: ToBlob P1 *
1958b 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 * * *.**.** Forc
1958c 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1958d 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 egister P1 to be
1958e 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74 a BLOB..** If t
1958f 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d 65 he value is nume
19590 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 ric, convert it
19591 74 6f 20 61 20 73 74 72 69 6e 67 20 66 69 72 73 to a string firs
19592 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 61 72 t..** Strings ar
19593 65 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 65 72 e simply reinter
19594 70 72 65 74 65 64 20 61 73 20 62 6c 6f 62 73 20 preted as blobs
19595 77 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 0a 2a with no change.*
19596 2a 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79 * to the underly
19597 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 ing data..**.**
19598 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 A NULL value is
19599 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 not changed by t
1959a 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 his routine. It
1959b 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a remains NULL..*
1959c 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 /.case OP_ToBlob
1959d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1959e 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1959f 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20 TK_TO_BLOB, in1
195a0 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 */. if( pIn1->f
195a1 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
195a2 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20 28 ) break;. if( (
195a3 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
195a4 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20 20 M_Blob)==0 ){.
195a5 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 applyAffinity(
195a6 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 pIn1, SQLITE_AFF
195a7 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 _TEXT, encoding)
195a8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 ;. assert( pI
195a9 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
195aa 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f Str || db->mallo
195ab 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 20 cFailed );. }.
195ac 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
195ad 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b pIn1, MEM_Blob);
195ae 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c . UPDATE_MAX_BL
195af 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 OBSIZE(pIn1);.
195b0 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
195b1 6f 64 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 ode: ToNumeric P
195b2 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
195b3 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 Force the value
195b4 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 in register P1 t
195b5 6f 20 62 65 20 6e 75 6d 65 72 69 63 20 28 65 69 o be numeric (ei
195b6 74 68 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 ther an.** integ
195b7 65 72 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67 er or a floating
195b8 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a -point number.).
195b9 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 ** If the value
195ba 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c is text or blob,
195bb 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 try to convert
195bc 69 74 20 74 6f 20 61 6e 20 75 73 69 6e 67 20 74 it to an using t
195bd 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 he.** equivalent
195be 20 6f 66 20 61 74 6f 69 28 29 20 6f 72 20 61 74 of atoi() or at
195bf 6f 66 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 of() and store 0
195c0 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 if no such conv
195c1 65 72 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f ersion .** is po
195c2 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 ssible..**.** A
195c3 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f NULL value is no
195c4 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 t changed by thi
195c5 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 s routine. It r
195c6 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a emains NULL..*/.
195c7 63 61 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 case OP_ToNumeri
195c8 63 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 c: {
195c9 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
195ca 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20 TK_TO_NUMERIC,
195cb 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 in1 */. if( (pI
195cc 6e 31 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d n1->flags & (MEM
195cd 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 _Null|MEM_Int|ME
195ce 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 29 7b 0a 20 M_Real))==0 ){.
195cf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
195d0 6d 4e 75 6d 65 72 69 66 79 28 70 49 6e 31 29 3b mNumerify(pIn1);
195d1 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
195d2 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
195d3 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f _OMIT_CAST */../
195d4 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 49 6e 74 20 * Opcode: ToInt
195d5 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
195d6 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 Force the value
195d7 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
195d8 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 be an integer.
195d9 49 66 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 If.** The value
195da 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 20 72 is currently a r
195db 65 61 6c 20 6e 75 6d 62 65 72 2c 20 64 72 6f 70 eal number, drop
195dc 20 69 74 73 20 66 72 61 63 74 69 6f 6e 61 6c 20 its fractional
195dd 70 61 72 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 part..** If the
195de 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 value is text or
195df 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f blob, try to co
195e0 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 nvert it to an i
195e1 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 nteger using the
195e2 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f .** equivalent o
195e3 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f f atoi() and sto
195e4 72 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20 re 0 if no such
195e5 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f conversion is po
195e6 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 ssible..**.** A
195e7 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f NULL value is no
195e8 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 t changed by thi
195e9 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 s routine. It r
195ea 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a emains NULL..*/.
195eb 63 61 73 65 20 4f 50 5f 54 6f 49 6e 74 3a 20 7b case OP_ToInt: {
195ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
195ed 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
195ee 54 4f 5f 49 4e 54 2c 20 69 6e 31 20 2a 2f 0a 20 TO_INT, in1 */.
195ef 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
195f0 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 s & MEM_Null)==0
195f1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
195f2 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
195f3 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 (pIn1);. }. br
195f4 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 eak;.}..#ifndef
195f5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 SQLITE_OMIT_CAST
195f6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 ./* Opcode: ToRe
195f7 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a al P1 * * * *.**
195f8 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 .** Force the va
195f9 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
195fa 50 31 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 P1 to be a float
195fb 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 ing point number
195fc 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75 ..** If The valu
195fd 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 e is currently a
195fe 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 n integer, conve
195ff 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 rt it..** If the
19600 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f value is text o
19601 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 r blob, try to c
19602 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 onvert it to an
19603 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 integer using th
19604 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 e.** equivalent
19605 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 of atoi() and st
19606 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 ore 0.0 if no su
19607 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 ch conversion is
19608 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a possible..**.**
19609 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 A NULL value is
1960a 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 not changed by
1960b 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 this routine. I
1960c 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a t remains NULL..
1960d 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 */.case OP_ToRea
1960e 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 l: {
1960f 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
19610 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 TK_TO_REAL, in1
19611 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d */. if( (pIn1-
19612 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
19613 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c l)==0 ){. sql
19614 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 ite3VdbeMemReali
19615 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 fy(pIn1);. }.
19616 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
19617 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 /* SQLITE_OMIT_C
19618 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 AST */../* Opcod
19619 65 3a 20 4c 74 20 50 31 20 50 32 20 50 33 20 50 e: Lt P1 P2 P3 P
1961a 34 20 50 35 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 4 P5.**.** Compa
1961b 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e re the values in
1961c 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 register P1 and
1961d 20 50 33 2e 20 20 49 66 20 72 65 67 28 50 33 29 P3. If reg(P3)
1961e 3c 72 65 67 28 50 31 29 20 74 68 65 6e 0a 2a 2a <reg(P1) then.**
1961f 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 jump to address
19620 20 50 32 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 P2. .**.** If
19621 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 the SQLITE_JUMPI
19622 46 4e 55 4c 4c 20 62 69 74 20 6f 66 20 50 35 20 FNULL bit of P5
19623 69 73 20 73 65 74 20 61 6e 64 20 65 69 74 68 65 is set and eithe
19624 72 20 72 65 67 28 50 31 29 20 6f 72 0a 2a 2a 20 r reg(P1) or.**
19625 72 65 67 28 50 33 29 20 69 73 20 4e 55 4c 4c 20 reg(P3) is NULL
19626 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 then take the ju
19627 6d 70 2e 20 20 49 66 20 74 68 65 20 53 51 4c 49 mp. If the SQLI
19628 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 0a 2a TE_JUMPIFNULL .*
19629 2a 20 62 69 74 20 69 73 20 63 6c 65 61 72 20 74 * bit is clear t
1962a 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 20 69 66 hen fall thru if
1962b 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 either operand
1962c 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 is NULL..**.** T
1962d 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 he SQLITE_AFF_MA
1962e 53 4b 20 70 6f 72 74 69 6f 6e 20 6f 66 20 50 35 SK portion of P5
1962f 20 6d 75 73 74 20 62 65 20 61 6e 20 61 66 66 69 must be an affi
19630 6e 69 74 79 20 63 68 61 72 61 63 74 65 72 20 2d nity character -
19631 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 .** SQLITE_AFF_T
19632 45 58 54 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f EXT, SQLITE_AFF_
19633 49 4e 54 45 47 45 52 2c 20 61 6e 64 20 73 6f 20 INTEGER, and so
19634 66 6f 72 74 68 2e 20 41 6e 20 61 74 74 65 6d 70 forth. An attemp
19635 74 20 69 73 20 6d 61 64 65 20 0a 2a 2a 20 74 6f t is made .** to
19636 20 63 6f 65 72 63 65 20 62 6f 74 68 20 69 6e 70 coerce both inp
19637 75 74 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f uts according to
19638 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 62 this affinity b
19639 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 63 6f 6d efore the.** com
1963a 70 61 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 2e parison is made.
1963b 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41 If the SQLITE_A
1963c 46 46 5f 4d 41 53 4b 20 69 73 20 30 78 30 30 2c FF_MASK is 0x00,
1963d 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 0a 2a 2a then numeric.**
1963e 20 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 affinity is use
1963f 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 d. Note that the
19640 20 61 66 66 69 6e 69 74 79 20 63 6f 6e 76 65 72 affinity conver
19641 73 69 6f 6e 73 20 61 72 65 20 73 74 6f 72 65 64 sions are stored
19642 0a 2a 2a 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 .** back into th
19643 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 e input register
19644 73 20 50 31 20 61 6e 64 20 50 33 2e 20 20 53 6f s P1 and P3. So
19645 20 74 68 69 73 20 6f 70 63 6f 64 65 20 63 61 6e this opcode can
19646 20 63 61 75 73 65 0a 2a 2a 20 70 65 72 73 69 73 cause.** persis
19647 74 65 6e 74 20 63 68 61 6e 67 65 73 20 74 6f 20 tent changes to
19648 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 registers P1 and
19649 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 P3..**.** Once
1964a 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 any conversions
1964b 68 61 76 65 20 74 61 6b 65 6e 20 70 6c 61 63 65 have taken place
1964c 2c 20 61 6e 64 20 6e 65 69 74 68 65 72 20 76 61 , and neither va
1964d 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a 2a lue is NULL, .**
1964e 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 the values are
1964f 63 6f 6d 70 61 72 65 64 2e 20 49 66 20 62 6f 74 compared. If bot
19650 68 20 76 61 6c 75 65 73 20 61 72 65 20 62 6c 6f h values are blo
19651 62 73 20 74 68 65 6e 20 6d 65 6d 63 6d 70 28 29 bs then memcmp()
19652 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 64 is.** used to d
19653 65 74 65 72 6d 69 6e 65 20 74 68 65 20 72 65 73 etermine the res
19654 75 6c 74 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 ults of the comp
19655 61 72 69 73 6f 6e 2e 20 20 49 66 20 62 6f 74 68 arison. If both
19656 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 74 values.** are t
19657 65 78 74 2c 20 74 68 65 6e 20 74 68 65 20 61 70 ext, then the ap
19658 70 72 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 propriate collat
19659 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 ing function spe
1965a 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 50 34 20 cified in.** P4
1965b 69 73 20 20 75 73 65 64 20 74 6f 20 64 6f 20 74 is used to do t
1965c 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 he comparison.
1965d 49 66 20 50 34 20 69 73 20 6e 6f 74 20 73 70 65 If P4 is not spe
1965e 63 69 66 69 65 64 20 74 68 65 6e 0a 2a 2a 20 6d cified then.** m
1965f 65 6d 63 6d 70 28 29 20 69 73 20 75 73 65 64 20 emcmp() is used
19660 74 6f 20 63 6f 6d 70 61 72 65 20 74 65 78 74 20 to compare text
19661 73 74 72 69 6e 67 2e 20 20 49 66 20 62 6f 74 68 string. If both
19662 20 76 61 6c 75 65 73 20 61 72 65 0a 2a 2a 20 6e values are.** n
19663 75 6d 65 72 69 63 2c 20 74 68 65 6e 20 61 20 6e umeric, then a n
19664 75 6d 65 72 69 63 20 63 6f 6d 70 61 72 69 73 6f umeric compariso
19665 6e 20 69 73 20 75 73 65 64 2e 20 49 66 20 74 68 n is used. If th
19666 65 20 74 77 6f 20 76 61 6c 75 65 73 0a 2a 2a 20 e two values.**
19667 61 72 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 are of different
19668 20 74 79 70 65 73 2c 20 74 68 65 6e 20 6e 75 6d types, then num
19669 62 65 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65 bers are conside
1966a 72 65 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a red less than.**
1966b 20 73 74 72 69 6e 67 73 20 61 6e 64 20 73 74 72 strings and str
1966c 69 6e 67 73 20 61 72 65 20 63 6f 6e 73 69 64 65 ings are conside
1966d 72 65 64 20 6c 65 73 73 20 74 68 61 6e 20 62 6c red less than bl
1966e 6f 62 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 obs..**.** If th
1966f 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 e SQLITE_STOREP2
19670 20 62 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 bit of P5 is se
19671 74 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a t, then do not j
19672 75 6d 70 2e 20 20 49 6e 73 74 65 61 64 2c 0a 2a ump. Instead,.*
19673 2a 20 73 74 6f 72 65 20 61 20 62 6f 6f 6c 65 61 * store a boolea
19674 6e 20 72 65 73 75 6c 74 20 28 65 69 74 68 65 72 n result (either
19675 20 30 2c 20 6f 72 20 31 2c 20 6f 72 20 4e 55 4c 0, or 1, or NUL
19676 4c 29 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 L) in register P
19677 32 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 2..*/./* Opcode:
19678 20 4e 65 20 50 31 20 50 32 20 50 33 20 50 34 20 Ne P1 P2 P3 P4
19679 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f P5.**.** This wo
1967a 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 rks just like th
1967b 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 e Lt opcode exce
1967c 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 pt that the jump
1967d 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 is taken if.**
1967e 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 the operands in
1967f 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 registers P1 and
19680 20 50 33 20 61 72 65 20 6e 6f 74 20 65 71 75 61 P3 are not equa
19681 6c 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f l. See the Lt o
19682 70 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 61 64 64 pcode for.** add
19683 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
19684 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ion..*/./* Opcod
19685 65 3a 20 45 71 20 50 31 20 50 32 20 50 33 20 50 e: Eq P1 P2 P3 P
19686 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 4 P5.**.** This
19687 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 works just like
19688 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 the Lt opcode ex
19689 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 cept that the ju
1968a 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a mp is taken if.*
1968b 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69 * the operands i
1968c 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 n registers P1 a
1968d 6e 64 20 50 33 20 61 72 65 20 65 71 75 61 6c 2e nd P3 are equal.
1968e 0a 2a 2a 20 53 65 65 20 74 68 65 20 4c 74 20 6f .** See the Lt o
1968f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 pcode for additi
19690 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
19691 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
19692 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 Le P1 P2 P3 P4 P
19693 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 5.**.** This wor
19694 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 ks just like the
19695 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 Lt opcode excep
19696 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 t that the jump
19697 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 is taken if.** t
19698 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
19699 67 69 73 74 65 72 20 50 33 20 69 73 20 6c 65 73 gister P3 is les
1969a 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 s than or equal
1969b 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f to the content o
1969c 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 f.** register P1
1969d 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 . See the Lt op
1969e 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f code for additio
1969f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
196a0 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 .*/./* Opcode: G
196a1 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 t P1 P2 P3 P4 P5
196a2 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b .**.** This work
196a3 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 s just like the
196a4 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 Lt opcode except
196a5 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 that the jump i
196a6 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 s taken if.** th
196a7 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 e content of reg
196a8 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61 ister P3 is grea
196a9 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 6f 6e ter than the con
196aa 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 tent of.** regis
196ab 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 ter P1. See the
196ac 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 Lt opcode for a
196ad 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
196ae 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 ation..*/./* Opc
196af 6f 64 65 3a 20 47 65 20 50 31 20 50 32 20 50 33 ode: Ge P1 P2 P3
196b0 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 P4 P5.**.** Thi
196b1 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b s works just lik
196b2 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 e the Lt opcode
196b3 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 except that the
196b4 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 jump is taken if
196b5 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 .** the content
196b6 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 69 of register P3 i
196b7 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f s greater than o
196b8 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 63 r equal to the c
196b9 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 ontent of.** reg
196ba 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 ister P1. See t
196bb 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 he Lt opcode for
196bc 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
196bd 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 rmation..*/.case
196be 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20 20 OP_Eq:
196bf 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
196c0 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 6e TK_EQ, jump, in
196c1 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 1, in3 */.case O
196c2 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20 20 20 P_Ne:
196c3 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
196c4 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c K_NE, jump, in1,
196c5 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f in3 */.case OP_
196c6 4c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 Lt:
196c7 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
196c8 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 LT, jump, in1, i
196c9 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 n3 */.case OP_Le
196ca 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 :
196cb 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 /* same as TK_LE
196cc 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 , jump, in1, in3
196cd 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20 */.case OP_Gt:
196ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
196cf 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20 same as TK_GT,
196d0 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a jump, in1, in3 *
196d1 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20 /.case OP_Ge: {
196d2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
196d3 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a 75 ame as TK_GE, ju
196d4 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a mp, in1, in3 */.
196d5 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 69 int flags;. i
196d6 6e 74 20 72 65 73 3b 0a 20 20 63 68 61 72 20 61 nt res;. char a
196d7 66 66 69 6e 69 74 79 3b 0a 0a 20 20 66 6c 61 67 ffinity;.. flag
196d8 73 20 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 7c s = pIn1->flags|
196d9 70 49 6e 33 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 pIn3->flags;..
196da 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 if( flags&MEM_Nu
196db 6c 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 ll ){. /* If
196dc 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 either operand i
196dd 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 s NULL then the
196de 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 result is always
196df 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 54 68 NULL.. ** Th
196e0 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 e jump is taken
196e1 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 if the SQLITE_JU
196e2 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 69 73 20 MPIFNULL bit is
196e3 73 65 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 set.. */.
196e4 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 if( pOp->p5 & SQ
196e5 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a LITE_STOREP2 ){.
196e6 20 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d pOut = &p-
196e7 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a >aMem[pOp->p2];.
196e8 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 MemSetType
196e9 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e Flag(pOut, MEM_N
196ea 75 6c 6c 29 3b 0a 20 20 20 20 20 20 52 45 47 49 ull);. REGI
196eb 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
196ec 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 7d p2, pOut);. }
196ed 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 else if( pOp->p5
196ee 20 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 & SQLITE_JUMPIF
196ef 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 70 63 NULL ){. pc
196f0 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 = pOp->p2-1;.
196f1 20 20 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
196f2 20 7d 0a 0a 20 20 61 66 66 69 6e 69 74 79 20 3d }.. affinity =
196f3 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 pOp->p5 & SQLIT
196f4 45 5f 41 46 46 5f 4d 41 53 4b 3b 0a 20 20 69 66 E_AFF_MASK;. if
196f5 28 20 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 ( affinity ){.
196f6 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 applyAffinity(
196f7 70 49 6e 31 2c 20 61 66 66 69 6e 69 74 79 2c 20 pIn1, affinity,
196f8 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 encoding);. a
196f9 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e pplyAffinity(pIn
196fa 33 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 3, affinity, enc
196fb 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 oding);. }.. a
196fc 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
196fd 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c pe==P4_COLLSEQ |
196fe 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d | pOp->p4.pColl=
196ff 3d 30 20 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c =0 );. ExpandBl
19700 6f 62 28 70 49 6e 31 29 3b 0a 20 20 45 78 70 61 ob(pIn1);. Expa
19701 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 ndBlob(pIn3);.
19702 72 65 73 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d res = sqlite3Mem
19703 43 6f 6d 70 61 72 65 28 70 49 6e 33 2c 20 70 49 Compare(pIn3, pI
19704 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c n1, pOp->p4.pCol
19705 6c 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f l);. switch( pO
19706 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 p->opcode ){.
19707 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 case OP_Eq:
19708 72 65 73 20 3d 20 72 65 73 3d 3d 30 3b 20 20 20 res = res==0;
19709 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
1970a 65 20 4f 50 5f 4e 65 3a 20 20 20 20 72 65 73 20 e OP_Ne: res
1970b 3d 20 72 65 73 21 3d 30 3b 20 20 20 20 20 62 72 = res!=0; br
1970c 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 eak;. case OP
1970d 5f 4c 74 3a 20 20 20 20 72 65 73 20 3d 20 72 65 _Lt: res = re
1970e 73 3c 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b s<0; break;
1970f 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a . case OP_Le:
19710 20 20 20 20 72 65 73 20 3d 20 72 65 73 3c 3d 30 res = res<=0
19711 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 ; break;.
19712 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 case OP_Gt:
19713 72 65 73 20 3d 20 72 65 73 3e 30 3b 20 20 20 20 res = res>0;
19714 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 break;. def
19715 61 75 6c 74 3a 20 20 20 20 20 20 20 72 65 73 20 ault: res
19716 3d 20 72 65 73 3e 3d 30 3b 20 20 20 20 20 62 72 = res>=0; br
19717 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 eak;. }.. if(
19718 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 pOp->p5 & SQLITE
19719 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 _STOREP2 ){.
1971a 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pOut = &p->aMem[
1971b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 4d 65 pOp->p2];. Me
1971c 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
1971d 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 t, MEM_Int);.
1971e 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 72 65 73 pOut->u.i = res
1971f 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 ;. REGISTER_T
19720 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f RACE(pOp->p2, pO
19721 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ut);. }else if(
19722 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d res ){. pc =
19723 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a pOp->p2-1;. }.
19724 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
19725 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69 pcode: Permutati
19726 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a on * * * P4 *.**
19727 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d .** Set the perm
19728 75 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 uation used by t
19729 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 he OP_Compare op
1972a 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68 65 erator to be the
1972b 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e 74 array.** of int
1972c 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a 0a egers in P4..**.
1972d 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74 69 ** The permutati
1972e 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 on is only valid
1972f 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 until the next
19730 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 OP_Permutation,
19731 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20 4f OP_Compare,.** O
19732 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52 65 P_Halt, or OP_Re
19733 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63 61 sultRow. Typica
19734 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d 75 lly the OP_Permu
19735 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 63 tation should oc
19736 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 cur.** immediate
19737 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 ly prior to the
19738 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a 63 OP_Compare..*/.c
19739 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 ase OP_Permutati
1973a 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 on: {. assert(
1973b 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1973c 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61 73 INTARRAY );. as
1973d 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61 69 sert( pOp->p4.ai
1973e 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20 3d );. aPermute =
1973f 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20 62 pOp->p4.ai;. b
19740 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
19741 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20 50 de: Compare P1 P
19742 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
19743 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74 6f Compare to vecto
19744 72 73 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 rs of registers
19745 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67 28 in reg(P1)..reg(
19746 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74 68 P1+P3-1) (all th
19747 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20 61 is.** one "A") a
19748 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72 nd in reg(P2)..r
19749 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22 eg(P2+P3-1) ("B"
1974a 29 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 73 ). Save the res
1974b 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f ult of.** the co
1974c 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65 mparison for use
1974d 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f by the next OP_
1974e 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a 2a Jump instruct..*
1974f 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 79 *.** P4 is a Key
19750 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 Info structure t
19751 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c 6c hat defines coll
19752 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 ating sequences
19753 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 65 and sort.** orde
19754 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 rs for the compa
19755 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 6d rison. The perm
19756 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 20 utation applies
19757 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 to registers.**
19758 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 6e only. The KeyIn
19759 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 fo elements are
1975a 75 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c 6c used sequentiall
1975b 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d y..**.** The com
1975c 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f 72 parison is a sor
1975d 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 6f t comparison, so
1975e 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 65 NULLs compare e
1975f 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 61 qual,.** NULLs a
19760 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 6d re less than num
19761 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 72 bers, numbers ar
19762 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 69 e less than stri
19763 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 69 ngs,.** and stri
19764 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 ngs are less tha
19765 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 65 n blobs..*/.case
19766 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a 20 OP_Compare: {.
19767 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 33 int n = pOp->p3
19768 3b 0a 20 20 69 6e 74 20 69 2c 20 70 31 2c 20 70 ;. int i, p1, p
19769 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 2;. const KeyIn
1976a 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 fo *pKeyInfo = p
1976b 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b Op->p4.pKeyInfo;
1976c 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 . assert( n>0 )
1976d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 ;. assert( pKey
1976e 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 70 31 20 Info!=0 );. p1
1976f 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 = pOp->p1;. ass
19770 65 72 74 28 20 70 31 3e 30 20 26 26 20 70 31 2b ert( p1>0 && p1+
19771 6e 2d 31 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 n-1<p->nMem );.
19772 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 p2 = pOp->p2;.
19773 20 61 73 73 65 72 74 28 20 70 32 3e 30 20 26 26 assert( p2>0 &&
19774 20 70 32 2b 6e 2d 31 3c 70 2d 3e 6e 4d 65 6d 20 p2+n-1<p->nMem
19775 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c );. for(i=0; i<
19776 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 n; i++){. int
19777 20 69 64 78 20 3d 20 61 50 65 72 6d 75 74 65 20 idx = aPermute
19778 3f 20 61 50 65 72 6d 75 74 65 5b 69 5d 20 3a 20 ? aPermute[i] :
19779 69 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a i;. CollSeq *
1977a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c pColl; /* Col
1977b 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
1977c 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 20 74 to use on this t
1977d 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 erm */. int b
1977e 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Rev; /*
1977f 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e True for DESCEN
19780 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20 DING sort order
19781 2a 2f 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f */. REGISTER_
19782 54 52 41 43 45 28 70 31 2b 69 64 78 2c 20 26 70 TRACE(p1+idx, &p
19783 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d 29 3b ->aMem[p1+idx]);
19784 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 . REGISTER_TR
19785 41 43 45 28 70 32 2b 69 64 78 2c 20 26 70 2d 3e ACE(p2+idx, &p->
19786 61 4d 65 6d 5b 70 32 2b 69 64 78 5d 29 3b 0a 20 aMem[p2+idx]);.
19787 20 20 20 61 73 73 65 72 74 28 20 69 3c 70 4b 65 assert( i<pKe
19788 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 29 3b yInfo->nField );
19789 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4b 65 . pColl = pKe
1978a 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 3b yInfo->aColl[i];
1978b 0a 20 20 20 20 62 52 65 76 20 3d 20 70 4b 65 79 . bRev = pKey
1978c 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 Info->aSortOrder
1978d 5b 69 5d 3b 0a 20 20 20 20 69 43 6f 6d 70 61 72 [i];. iCompar
1978e 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f e = sqlite3MemCo
1978f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65 6d 5b 70 mpare(&p->aMem[p
19790 31 2b 69 64 78 5d 2c 20 26 70 2d 3e 61 4d 65 6d 1+idx], &p->aMem
19791 5b 70 32 2b 69 64 78 5d 2c 20 70 43 6f 6c 6c 29 [p2+idx], pColl)
19792 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6d 70 61 ;. if( iCompa
19793 72 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 re ){. if(
19794 62 52 65 76 20 29 20 69 43 6f 6d 70 61 72 65 20 bRev ) iCompare
19795 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a 20 20 20 = -iCompare;.
19796 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
19797 20 20 7d 0a 20 20 61 50 65 72 6d 75 74 65 20 3d }. aPermute =
19798 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0;. break;.}..
19799 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75 6d 70 20 /* Opcode: Jump
1979a 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1979b 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 69 ** Jump to the i
1979c 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 20 61 64 nstruction at ad
1979d 64 72 65 73 73 20 50 31 2c 20 50 32 2c 20 6f 72 dress P1, P2, or
1979e 20 50 33 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e P3 depending on
1979f 20 77 68 65 74 68 65 72 0a 2a 2a 20 69 6e 20 74 whether.** in t
197a0 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 4f he most recent O
197a1 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73 74 72 75 P_Compare instru
197a2 63 74 69 6f 6e 20 74 68 65 20 50 31 20 76 65 63 ction the P1 vec
197a3 74 6f 72 20 77 61 73 20 6c 65 73 73 20 74 68 61 tor was less tha
197a4 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f n.** equal to, o
197a5 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 r greater than t
197a6 68 65 20 50 32 20 76 65 63 74 6f 72 2c 20 72 65 he P2 vector, re
197a7 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 63 spectively..*/.c
197a8 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20 7b 20 20 ase OP_Jump: {
197a9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 /* ju
197aa 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69 43 6f 6d mp */. if( iCom
197ab 70 61 72 65 3c 30 20 29 7b 0a 20 20 20 20 70 63 pare<0 ){. pc
197ac 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a = pOp->p1 - 1;.
197ad 20 20 7d 65 6c 73 65 20 69 66 28 20 69 43 6f 6d }else if( iCom
197ae 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 20 70 pare==0 ){. p
197af 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
197b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 63 . }else{. pc
197b1 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 31 3b 0a = pOp->p3 - 1;.
197b2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
197b3 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e 64 20 50 /* Opcode: And P
197b4 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
197b5 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 * Take the logic
197b6 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 20 76 61 al AND of the va
197b7 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 lues in register
197b8 73 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a s P1 and P2 and.
197b9 2a 2a 20 77 72 69 74 65 20 74 68 65 20 72 65 73 ** write the res
197ba 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ult into registe
197bb 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 r P3..**.** If e
197bc 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 69 ither P1 or P2 i
197bd 73 20 30 20 28 66 61 6c 73 65 29 20 74 68 65 6e s 0 (false) then
197be 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 30 the result is 0
197bf 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 even if.** the
197c0 6f 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e other input is N
197c1 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 ULL. A NULL and
197c2 20 74 72 75 65 20 6f 72 20 74 77 6f 20 4e 55 4c true or two NUL
197c3 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20 4e 55 4c Ls give.** a NUL
197c4 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 2f 2a 20 L output..*/./*
197c5 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31 20 50 32 Opcode: Or P1 P2
197c6 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 P3 * *.**.** Ta
197c7 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c 20 4f ke the logical O
197c8 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 R of the values
197c9 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61 in register P1 a
197ca 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f nd P2 and.** sto
197cb 72 65 20 74 68 65 20 61 6e 73 77 65 72 20 69 6e re the answer in
197cc 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a register P3..**
197cd 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 31 .** If either P1
197ce 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e 7a 65 72 or P2 is nonzer
197cf 6f 20 28 74 72 75 65 29 20 74 68 65 6e 20 74 68 o (true) then th
197d0 65 20 72 65 73 75 6c 74 20 69 73 20 31 20 28 74 e result is 1 (t
197d1 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20 69 66 20 rue).** even if
197d2 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74 20 the other input
197d3 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c is NULL. A NULL
197d4 20 61 6e 64 20 66 61 6c 73 65 20 6f 72 20 74 77 and false or tw
197d5 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69 76 65 20 o NULLs.** give
197d6 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a a NULL output..*
197d7 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 3a 20 20 /.case OP_And:
197d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
197d9 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 2c 20 69 ame as TK_AND, i
197da 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
197db 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 7b 20 20 .case OP_Or: {
197dc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
197dd 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 69 6e 31 me as TK_OR, in1
197de 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 , in2, out3 */.
197df 20 69 6e 74 20 76 31 2c 20 76 32 3b 20 20 20 20 int v1, v2;
197e0 2f 2a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d /* 0==FALSE, 1==
197e1 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e TRUE, 2==UNKNOWN
197e2 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20 69 or NULL */.. i
197e3 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 f( pIn1->flags &
197e4 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
197e5 20 76 31 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 v1 = 2;. }else
197e6 7b 0a 20 20 20 20 76 31 20 3d 20 73 71 6c 69 74 {. v1 = sqlit
197e7 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 e3VdbeIntValue(p
197e8 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 In1)!=0;. }. i
197e9 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 f( pIn2->flags &
197ea 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
197eb 20 76 32 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 v2 = 2;. }else
197ec 7b 0a 20 20 20 20 76 32 20 3d 20 73 71 6c 69 74 {. v2 = sqlit
197ed 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 e3VdbeIntValue(p
197ee 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 In2)!=0;. }. i
197ef 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
197f0 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 20 73 74 OP_And ){. st
197f1 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
197f2 6e 65 64 20 63 68 61 72 20 61 6e 64 5f 6c 6f 67 ned char and_log
197f3 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c 20 30 ic[] = { 0, 0, 0
197f4 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 32 2c , 0, 1, 2, 0, 2,
197f5 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 3d 20 61 2 };. v1 = a
197f6 6e 64 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b 76 32 nd_logic[v1*3+v2
197f7 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
197f8 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
197f9 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f 6c 6f igned char or_lo
197fa 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 gic[] = { 0, 1,
197fb 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20 31 2, 1, 1, 1, 2, 1
197fc 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 3d 20 , 2 };. v1 =
197fd 6f 72 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b 76 32 or_logic[v1*3+v2
197fe 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76 31 3d ];. }. if( v1=
197ff 3d 32 20 29 7b 0a 20 20 20 20 4d 65 6d 53 65 74 =2 ){. MemSet
19800 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
19801 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 65 6c 73 EM_Null);. }els
19802 65 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 e{. pOut->u.i
19803 20 3d 20 76 31 3b 0a 20 20 20 20 4d 65 6d 53 65 = v1;. MemSe
19804 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
19805 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 MEM_Int);. }.
19806 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
19807 6f 64 65 3a 20 4e 6f 74 20 50 31 20 2a 20 2a 20 ode: Not P1 * *
19808 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 * *.**.** Interp
19809 72 65 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e ret the value in
1980a 20 72 65 67 69 73 74 65 72 20 50 31 20 61 73 20 register P1 as
1980b 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e a boolean value.
1980c 20 20 52 65 70 6c 61 63 65 20 69 74 0a 2a 2a 20 Replace it.**
1980d 77 69 74 68 20 69 74 73 20 63 6f 6d 70 6c 65 6d with its complem
1980e 65 6e 74 2e 20 20 49 66 20 74 68 65 20 76 61 6c ent. If the val
1980f 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
19810 31 20 69 73 20 4e 55 4c 4c 20 69 74 73 20 76 61 1 is NULL its va
19811 6c 75 65 0a 2a 2a 20 69 73 20 75 6e 63 68 61 6e lue.** is unchan
19812 67 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ged..*/.case OP_
19813 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 Not: {
19814 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
19815 20 54 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a TK_NOT, in1 */.
19816 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 if( pIn1->flag
19817 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 s & MEM_Null ) b
19818 72 65 61 6b 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74 reak; /* Do not
19819 68 69 6e 67 20 74 6f 20 4e 55 4c 4c 73 20 2a 2f hing to NULLs */
1981a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1981b 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 mIntegerify(pIn1
1981c 29 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d );. pIn1->u.i =
1981d 20 21 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 61 !pIn1->u.i;. a
1981e 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 ssert( pIn1->fla
1981f 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 gs&MEM_Int );.
19820 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
19821 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31 20 2a ode: BitNot P1 *
19822 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 * * *.**.** Int
19823 65 72 70 72 65 74 20 74 68 65 20 63 6f 6e 74 65 erpret the conte
19824 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 nt of register P
19825 31 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 1 as an integer.
19826 20 20 52 65 70 6c 61 63 65 20 69 74 0a 2a 2a 20 Replace it.**
19827 77 69 74 68 20 69 74 73 20 6f 6e 65 73 2d 63 6f with its ones-co
19828 6d 70 6c 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 mplement. If th
19829 65 20 76 61 6c 75 65 20 69 73 20 6f 72 69 67 69 e value is origi
1982a 6e 61 6c 6c 79 20 4e 55 4c 4c 2c 20 6c 65 61 76 nally NULL, leav
1982b 65 0a 2a 2a 20 69 74 20 75 6e 63 68 61 6e 67 65 e.** it unchange
1982c 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 d..*/.case OP_Bi
1982d 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 tNot: {
1982e 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1982f 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a 2f K_BITNOT, in1 */
19830 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 . if( pIn1->fla
19831 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 gs & MEM_Null )
19832 62 72 65 61 6b 3b 20 20 2f 2a 20 44 6f 20 6e 6f break; /* Do no
19833 74 68 69 6e 67 20 74 6f 20 4e 55 4c 4c 73 20 2a thing to NULLs *
19834 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d /. sqlite3VdbeM
19835 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e emIntegerify(pIn
19836 31 29 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 1);. pIn1->u.i
19837 3d 20 7e 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 = ~pIn1->u.i;.
19838 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c assert( pIn1->fl
19839 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 ags&MEM_Int );.
1983a 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1983b 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32 20 50 code: If P1 P2 P
1983c 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
1983d 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61 to P2 if the va
1983e 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1983f 50 31 20 69 73 20 74 72 75 65 2e 20 20 54 68 65 P1 is true. The
19840 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 value is.** is
19841 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 65 20 considered true
19842 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72 69 63 if it is numeric
19843 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 and non-zero.
19844 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 If the value.**
19845 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 in P1 is NULL th
19846 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 en take the jump
19847 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e 0a if P3 is true..
19848 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 */./* Opcode: If
19849 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a Not P1 P2 P3 * *
1984a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 .**.** Jump to P
1984b 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 2 if the value i
1984c 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 n register P1 is
1984d 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76 61 6c False. The val
1984e 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 ue is.** is cons
1984f 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20 69 idered true if i
19850 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69 63 20 t has a numeric
19851 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e 20 20 value of zero.
19852 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 If the value.**
19853 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 in P1 is NULL th
19854 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 en take the jump
19855 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e 0a if P3 is true..
19856 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a 20 20 */.case OP_If:
19857 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19858 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 63 * jump, in1 */.c
19859 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b 20 ase OP_IfNot: {
1985a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 /* ju
1985b 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74 mp, in1 */. int
1985c 20 63 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e c;. if( pIn1->
1985d 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1985e 20 29 7b 0a 20 20 20 20 63 20 3d 20 70 4f 70 2d ){. c = pOp-
1985f 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 >p3;. }else{.#i
19860 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
19861 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
19862 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 c = sqlite3V
19863 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 dbeIntValue(pIn1
19864 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 63 20 3d );.#else. c =
19865 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c sqlite3VdbeReal
19866 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30 Value(pIn1)!=0.0
19867 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 ;.#endif. if(
19868 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
19869 5f 49 66 4e 6f 74 20 29 20 63 20 3d 20 21 63 3b _IfNot ) c = !c;
1986a 0a 20 20 7d 0a 20 20 69 66 28 20 63 20 29 7b 0a . }. if( c ){.
1986b 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1986c 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b -1;. }. break;
1986d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
1986e 73 4e 75 6c 6c 20 50 31 20 50 32 20 50 33 20 2a sNull P1 P2 P3 *
1986f 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f *.**.** Jump to
19870 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 P2 if the value
19871 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
19872 69 73 20 4e 55 4c 4c 2e 20 20 49 66 20 50 33 20 is NULL. If P3
19873 69 73 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 is greater.** th
19874 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 63 68 an zero, then ch
19875 65 63 6b 20 61 6c 6c 20 76 61 6c 75 65 73 20 72 eck all values r
19876 65 67 28 50 31 29 2c 20 72 65 67 28 50 31 2b 31 eg(P1), reg(P1+1
19877 29 2c 20 0a 2a 2a 20 72 65 67 28 50 31 2b 32 29 ), .** reg(P1+2)
19878 2c 20 2e 2e 2e 2c 20 72 65 67 28 50 31 2b 50 33 , ..., reg(P1+P3
19879 2d 31 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f -1)..*/.case OP_
1987a 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 IsNull: {
1987b 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1987c 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70 2c TK_ISNULL, jump,
1987d 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 6e 20 in1 */. int n
1987e 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 = pOp->p3;. ass
1987f 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 ert( pOp->p3==0
19880 7c 7c 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a || pOp->p1>0 );.
19881 20 20 64 6f 7b 0a 20 20 20 20 69 66 28 20 28 70 do{. if( (p
19882 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
19883 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 _Null)!=0 ){.
19884 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
19885 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b - 1;. break
19886 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 31 ;. }. pIn1
19887 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 2d 2d ++;. }while( --
19888 6e 20 3e 20 30 20 29 3b 0a 20 20 62 72 65 61 6b n > 0 );. break
19889 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1988a 4e 6f 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 NotNull P1 P2 *
1988b 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 * *.**.** Jump t
1988c 6f 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 o P2 if the valu
1988d 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1988e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 0a is not NULL. .
1988f 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e 75 */.case OP_NotNu
19890 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 ll: {
19891 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e /* same as TK_N
19892 4f 54 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e OTNULL, jump, in
19893 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 1 */. if( (pIn1
19894 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
19895 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 ll)==0 ){. pc
19896 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
19897 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
19898 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 4e 75 /* Opcode: SetNu
19899 6d 43 6f 6c 75 6d 6e 73 20 2a 20 50 32 20 2a 20 mColumns * P2 *
1989a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f * *.**.** This o
1989b 70 63 6f 64 65 20 73 65 74 73 20 74 68 65 20 6e pcode sets the n
1989c 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
1989d 20 66 6f 72 20 74 68 65 20 63 75 72 73 6f 72 20 for the cursor
1989e 6f 70 65 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a opened by the.**
1989f 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 following instr
198a0 75 63 74 69 6f 6e 20 74 6f 20 50 32 2e 0a 2a 2a uction to P2..**
198a1 0a 2a 2a 20 41 6e 20 4f 50 5f 53 65 74 4e 75 6d .** An OP_SetNum
198a2 43 6f 6c 75 6d 6e 73 20 69 73 20 6f 6e 6c 79 20 Columns is only
198a3 75 73 65 66 75 6c 20 69 66 20 69 74 20 6f 63 63 useful if it occ
198a4 75 72 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 urs immediately
198a5 62 65 66 6f 72 65 20 0a 2a 2a 20 6f 6e 65 20 6f before .** one o
198a6 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
198a7 6f 70 63 6f 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 opcodes:.**.**
198a8 20 20 20 4f 70 65 6e 52 65 61 64 0a 2a 2a 20 20 OpenRead.**
198a9 20 20 20 4f 70 65 6e 57 72 69 74 65 0a 2a 2a 20 OpenWrite.**
198aa 20 20 20 20 4f 70 65 6e 50 73 65 75 64 6f 0a 2a OpenPseudo.*
198ab 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 5f 43 *.** If the OP_C
198ac 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 20 69 73 20 olumn opcode is
198ad 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 6f to be executed o
198ae 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68 65 6e n a cursor, then
198af 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f 64 65 20 .** this opcode
198b0 6d 75 73 74 20 62 65 20 70 72 65 73 65 6e 74 20 must be present
198b1 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 65 66 6f immediately befo
198b2 72 65 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 re the opcode th
198b3 61 74 0a 2a 2a 20 6f 70 65 6e 73 20 74 68 65 20 at.** opens the
198b4 63 75 72 73 6f 72 2e 0a 2a 2f 0a 63 61 73 65 20 cursor..*/.case
198b5 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 OP_SetNumColumns
198b6 3a 20 7b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a : {. break;.}..
198b7 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 6d /* Opcode: Colum
198b8 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a n P1 P2 P3 P4 *.
198b9 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 **.** Interpret
198ba 74 68 65 20 64 61 74 61 20 74 68 61 74 20 63 75 the data that cu
198bb 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 74 rsor P1 points t
198bc 6f 20 61 73 20 61 20 73 74 72 75 63 74 75 72 65 o as a structure
198bd 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 built using.**
198be 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 the MakeRecord i
198bf 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 65 nstruction. (Se
198c0 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 e the MakeRecord
198c1 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 opcode for addi
198c2 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d tional.** inform
198c3 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
198c4 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 61 format of the da
198c5 74 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 68 ta.) Extract th
198c6 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a 2a e P2-th column.*
198c7 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 6f * from this reco
198c8 72 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 rd. If there ar
198c9 65 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b e less that (P2+
198ca 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e 1) .** values in
198cb 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78 74 the record, ext
198cc 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a ract a NULL..**.
198cd 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78 74 ** The value ext
198ce 72 61 63 74 65 64 20 69 73 20 73 74 6f 72 65 64 racted is stored
198cf 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
198d0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f .**.** If the co
198d1 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65 lumn contains fe
198d2 77 65 72 20 74 68 61 6e 20 50 32 20 66 69 65 6c wer than P2 fiel
198d3 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63 74 ds, then extract
198d4 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a a NULL. Or,.**
198d5 20 69 66 20 74 68 65 20 50 34 20 61 72 67 75 6d if the P4 argum
198d6 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d 20 ent is a P4_MEM
198d7 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 use the value of
198d8 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74 the P4 argument
198d9 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c as.** the resul
198da 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f t..*/.case OP_Co
198db 6c 75 6d 6e 3a 20 7b 0a 20 20 69 6e 74 20 70 61 lumn: {. int pa
198dc 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 yloadSize; /*
198dd 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
198de 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f in the record */
198df 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d . int p1 = pOp-
198e0 3e 70 31 3b 20 20 2f 2a 20 50 31 20 76 61 6c 75 >p1; /* P1 valu
198e1 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 e of the opcode
198e2 2a 2f 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 4f */. int p2 = pO
198e3 70 2d 3e 70 32 3b 20 20 2f 2a 20 63 6f 6c 75 6d p->p2; /* colum
198e4 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72 n number to retr
198e5 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 75 ieve */. VdbeCu
198e6 72 73 6f 72 20 2a 70 43 20 3d 20 30 3b 2f 2a 20 rsor *pC = 0;/*
198e7 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 The VDBE cursor
198e8 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 3b */. char *zRec;
198e9 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
198ea 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 er to complete r
198eb 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 ecord-data */.
198ec 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
198ed 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 /* The BTree
198ee 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 20 cursor */. u32
198ef 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f *aType; /
198f0 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73 * aType[i] holds
198f1 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 the numeric typ
198f2 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f e of the i-th co
198f3 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61 lumn */. u32 *a
198f4 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 Offset; /*
198f5 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 aOffset[i] is of
198f6 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 fset to start of
198f7 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 data for i-th c
198f8 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e olumn */. int n
198f9 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a Field; /*
198fa 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 number of field
198fb 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 s in the record
198fc 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 */. int len;
198fd 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c /* The l
198fe 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 72 ength of the ser
198ff 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72 ialized data for
19900 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 the column */.
19901 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
19902 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
19903 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ter */. char *z
19904 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 Data; /* P
19905 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 art of the recor
19906 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 d being decoded
19907 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b */. Mem *pDest;
19908 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 /* Where
19909 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 78 to write the ex
1990a 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f tracted value */
1990b 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 . Mem sMem;
1990c 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f /* For sto
1990d 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 ring the record
1990e 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f being decoded */
1990f 0a 0a 20 20 73 4d 65 6d 2e 66 6c 61 67 73 20 3d .. sMem.flags =
19910 20 30 3b 0a 20 20 73 4d 65 6d 2e 64 62 20 3d 20 0;. sMem.db =
19911 30 3b 0a 20 20 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 0;. sMem.zMallo
19912 63 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 c = 0;. assert(
19913 20 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 p1<p->nCursor )
19914 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
19915 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 >p3>0 && pOp->p3
19916 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
19917 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b Dest = &p->aMem[
19918 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53 pOp->p3];. MemS
19919 65 74 54 79 70 65 46 6c 61 67 28 70 44 65 73 74 etTypeFlag(pDest
1991a 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 , MEM_Null);..
1991b 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 /* This block se
1991c 74 73 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 ts the variable
1991d 70 61 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 payloadSize to b
1991e 65 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 e the total numb
1991f 65 72 20 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 er of. ** bytes
19920 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0a in the record..
19921 20 20 2a 2a 0a 20 20 2a 2a 20 7a 52 65 63 20 69 **. ** zRec i
19922 73 20 73 65 74 20 74 6f 20 62 65 20 74 68 65 20 s set to be the
19923 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 complete text of
19924 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69 the record if i
19925 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a t is available..
19926 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 ** The complet
19927 65 20 72 65 63 6f 72 64 20 74 65 78 74 20 69 73 e record text is
19928 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c always availabl
19929 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62 e for pseudo-tab
1992a 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 les. ** If the
1992b 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 record is stored
1992c 20 69 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68 in a cursor, th
1992d 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 e complete recor
1992e 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68 d text. ** migh
1992f 74 20 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69 t be available i
19930 6e 20 74 68 65 20 20 70 43 2d 3e 61 52 6f 77 20 n the pC->aRow
19931 63 61 63 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 cache. Or it mi
19932 67 68 74 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a ght not be.. **
19933 20 49 66 20 74 68 65 20 64 61 74 61 20 69 73 20 If the data is
19934 75 6e 61 76 61 69 6c 61 62 6c 65 2c 20 20 7a 52 unavailable, zR
19935 65 63 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c ec is set to NUL
19936 4c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 L.. **. ** We
19937 61 6c 73 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 also compute the
19938 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
19939 6e 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ns in the record
1993a 2e 20 20 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a . For cursors,.
1993b 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 ** the number
1993c 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 of columns is st
1993d 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 62 65 ored in the Vdbe
1993e 43 75 72 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c Cursor.nField el
1993f 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 ement.. */. pC
19940 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 31 5d 3b = p->apCsr[p1];
19941 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 . assert( pC!=0
19942 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );.#ifndef SQLI
19943 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
19944 41 42 4c 45 0a 20 20 61 73 73 65 72 74 28 20 70 ABLE. assert( p
19945 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d C->pVtabCursor==
19946 30 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 0 );.#endif. if
19947 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 ( pC->pCursor!=0
19948 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 ){. /* The r
19949 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 ecord is stored
1994a 69 6e 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 in a B-Tree */.
1994b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1994c 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 dbeCursorMoveto(
1994d 70 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 pC);. if( rc
1994e 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1994f 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7a _to_error;. z
19950 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 70 43 72 Rec = 0;. pCr
19951 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 sr = pC->pCursor
19952 3b 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 6e 75 ;. if( pC->nu
19953 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 70 llRow ){. p
19954 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a ayloadSize = 0;.
19955 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 }else if( pC
19956 2d 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 ->cacheStatus==p
19957 2d 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 ->cacheCtr ){.
19958 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 payloadSize
19959 3d 20 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a = pC->payloadSiz
1995a 65 3b 0a 20 20 20 20 20 20 7a 52 65 63 20 3d 20 e;. zRec =
1995b 28 63 68 61 72 2a 29 70 43 2d 3e 61 52 6f 77 3b (char*)pC->aRow;
1995c 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
1995d 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 C->isIndex ){.
1995e 20 20 20 20 69 36 34 20 70 61 79 6c 6f 61 64 53 i64 payloadS
1995f 69 7a 65 36 34 3b 0a 20 20 20 20 20 20 73 71 6c ize64;. sql
19960 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
19961 28 70 43 72 73 72 2c 20 26 70 61 79 6c 6f 61 64 (pCrsr, &payload
19962 53 69 7a 65 36 34 29 3b 0a 20 20 20 20 20 20 70 Size64);. p
19963 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61 79 ayloadSize = pay
19964 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20 loadSize64;.
19965 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
19966 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a ite3BtreeDataSiz
19967 65 28 70 43 72 73 72 2c 20 28 75 33 32 20 2a 29 e(pCrsr, (u32 *)
19968 26 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 &payloadSize);.
19969 20 20 20 7d 0a 20 20 20 20 6e 46 69 65 6c 64 20 }. nField
1996a 3d 20 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 = pC->nField;.
1996b 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
1996c 74 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 t( pC->pseudoTab
1996d 6c 65 20 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 le );. /* The
1996e 20 72 65 63 6f 72 64 20 69 73 20 74 68 65 20 73 record is the s
1996f 6f 6c 65 20 65 6e 74 72 79 20 6f 66 20 61 20 70 ole entry of a p
19970 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 seudo-table */.
19971 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d payloadSize =
19972 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20 20 20 20 pC->nData;.
19973 7a 52 65 63 20 3d 20 70 43 2d 3e 70 44 61 74 61 zRec = pC->pData
19974 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 ;. pC->cacheS
19975 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
19976 41 4c 45 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ALE;. assert(
19977 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 payloadSize==0
19978 7c 7c 20 7a 52 65 63 21 3d 30 20 29 3b 0a 20 20 || zRec!=0 );.
19979 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 2d 3e 6e nField = pC->n
1997a 46 69 65 6c 64 3b 0a 20 20 20 20 70 43 72 73 72 Field;. pCrsr
1997b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 0;. }.. /*
1997c 49 66 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 69 If payloadSize i
1997d 73 20 30 2c 20 74 68 65 6e 20 6a 75 73 74 20 73 s 0, then just s
1997e 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 tore a NULL */.
1997f 20 69 66 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 if( payloadSize
19980 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==0 ){. asser
19981 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 t( pDest->flags&
19982 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 MEM_Null );.
19983 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f goto op_column_o
19984 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 61 ut;. }. if( pa
19985 79 6c 6f 61 64 53 69 7a 65 3e 64 62 2d 3e 61 4c yloadSize>db->aL
19986 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
19987 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
19988 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
19989 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 32 }.. assert( p2
1998a 3c 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a <nField );.. /*
1998b 20 52 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 Read and parse
1998c 74 68 65 20 74 61 62 6c 65 20 68 65 61 64 65 72 the table header
1998d 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 . Store the res
1998e 75 6c 74 73 20 6f 66 20 74 68 65 20 70 61 72 73 ults of the pars
1998f 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 e. ** into the
19990 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 63 61 record header ca
19991 63 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 che fields of th
19992 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 e cursor.. */.
19993 20 61 54 79 70 65 20 3d 20 70 43 2d 3e 61 54 79 aType = pC->aTy
19994 70 65 3b 0a 20 20 69 66 28 20 70 43 2d 3e 63 61 pe;. if( pC->ca
19995 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 cheStatus==p->ca
19996 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20 61 4f cheCtr ){. aO
19997 66 66 73 65 74 20 3d 20 70 43 2d 3e 61 4f 66 66 ffset = pC->aOff
19998 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 set;. }else{.
19999 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 u8 *zIdx;
1999a 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f /* Index into
1999b 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 header */. u
1999c 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 8 *zEndHdr;
1999d 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 /* Pointer to fi
1999e 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 rst byte after t
1999f 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 he header */.
199a0 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 int offset;
199a1 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f /* Offset into
199a2 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 20 the data */.
199a3 20 69 6e 74 20 73 7a 48 64 72 53 7a 3b 20 20 20 int szHdrSz;
199a4 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
199a5 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 69 65 header size fie
199a6 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 20 72 ld at start of r
199a7 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 ecord */. int
199a8 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20 2f 2a avail; /*
199a9 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
199aa 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61 of available da
199ab 74 61 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 ta */.. asser
199ac 74 28 61 54 79 70 65 29 3b 0a 20 20 20 20 70 43 t(aType);. pC
199ad 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61 4f 66 66 ->aOffset = aOff
199ae 73 65 74 20 3d 20 26 61 54 79 70 65 5b 6e 46 69 set = &aType[nFi
199af 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d 3e 70 61 eld];. pC->pa
199b0 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61 79 6c yloadSize = payl
199b1 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 70 43 2d oadSize;. pC-
199b2 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 70 >cacheStatus = p
199b3 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20 20 20 ->cacheCtr;..
199b4 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 /* Figure out h
199b5 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 61 72 ow many bytes ar
199b6 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 e in the header
199b7 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52 65 63 20 */. if( zRec
199b8 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 61 20 3d ){. zData =
199b9 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c 73 65 zRec;. }else
199ba 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 2d 3e {. if( pC->
199bb 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 isIndex ){.
199bc 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72 zData = (char
199bd 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 *)sqlite3BtreeKe
199be 79 46 65 74 63 68 28 70 43 72 73 72 2c 20 26 61 yFetch(pCrsr, &a
199bf 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d 65 6c vail);. }el
199c0 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 se{. zDat
199c1 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 a = (char*)sqlit
199c2 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
199c3 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c 29 3b (pCrsr, &avail);
199c4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f . }. /
199c5 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28 29 2f * If KeyFetch()/
199c6 44 61 74 61 46 65 74 63 68 28 29 20 6d 61 6e 61 DataFetch() mana
199c7 67 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 65 ged to get the e
199c8 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c 0a 20 ntire payload,.
199c9 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74 68 65 ** save the
199ca 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68 65 20 payload in the
199cb 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 pC->aRow cache.
199cc 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 That will save
199cd 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a us from. **
199ce 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 having to make
199cf 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 additional calls
199d0 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f to fetch the co
199d1 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 ntent portion of
199d2 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 . ** the re
199d3 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 cord.. */.
199d4 20 20 20 20 20 69 66 28 20 61 76 61 69 6c 3e 3d if( avail>=
199d5 70 61 79 6c 6f 61 64 53 69 7a 65 20 29 7b 0a 20 payloadSize ){.
199d6 20 20 20 20 20 20 20 7a 52 65 63 20 3d 20 7a 44 zRec = zD
199d7 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70 43 2d ata;. pC-
199d8 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 7a 44 61 >aRow = (u8*)zDa
199d9 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ta;. }else{
199da 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61 52 6f . pC->aRo
199db 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 w = 0;. }.
199dc 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 }. /* The
199dd 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 following assert
199de 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c 6c 20 is true in all
199df 63 61 73 65 73 20 61 63 63 65 70 74 20 77 68 65 cases accept whe
199e0 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 n. ** the dat
199e1 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 abase file has b
199e2 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20 65 78 een corrupted ex
199e3 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a ternally.. **
199e4 20 20 20 20 61 73 73 65 72 74 28 20 7a 52 65 63 assert( zRec
199e5 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e 3d 70 61 !=0 || avail>=pa
199e6 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 61 76 61 yloadSize || ava
199e7 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20 20 20 il>=9 ); */.
199e8 73 7a 48 64 72 53 7a 20 3d 20 67 65 74 56 61 72 szHdrSz = getVar
199e9 69 6e 74 33 32 28 28 75 38 2a 29 7a 44 61 74 61 int32((u8*)zData
199ea 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 , offset);..
199eb 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 /* The KeyFetch(
199ec 29 20 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 ) or DataFetch()
199ed 20 61 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 above are fast
199ee 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 and will get the
199ef 20 65 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 entire. ** r
199f0 65 63 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 ecord header in
199f1 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 most cases. But
199f2 20 74 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 they will fail
199f3 74 6f 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c to get the compl
199f4 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 ete. ** recor
199f5 64 20 68 65 61 64 65 72 20 69 66 20 74 68 65 20 d header if the
199f6 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f record header do
199f7 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 es not fit on a
199f8 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 single page.
199f9 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 ** in the B-Tree
199fa 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 . When that hap
199fb 70 65 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 pens, use sqlite
199fc 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 3VdbeMemFromBtre
199fd 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 e() to. ** ac
199fe 71 75 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 quire the comple
199ff 74 65 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a te header text..
19a00 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
19a01 7a 52 65 63 20 26 26 20 61 76 61 69 6c 3c 6f 66 zRec && avail<of
19a02 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 73 4d fset ){. sM
19a03 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 em.flags = 0;.
19a04 20 20 20 20 73 4d 65 6d 2e 64 62 20 3d 20 30 3b sMem.db = 0;
19a05 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
19a06 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
19a07 72 65 65 28 70 43 72 73 72 2c 20 30 2c 20 6f 66 ree(pCrsr, 0, of
19a08 66 73 65 74 2c 20 70 43 2d 3e 69 73 49 6e 64 65 fset, pC->isInde
19a09 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20 20 20 x, &sMem);.
19a0a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
19a0b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
19a0c 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 to op_column_out
19a0d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
19a0e 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a 3b 0a zData = sMem.z;.
19a0f 20 20 20 20 7d 0a 20 20 20 20 7a 45 6e 64 48 64 }. zEndHd
19a10 72 20 3d 20 28 75 38 20 2a 29 26 7a 44 61 74 61 r = (u8 *)&zData
19a11 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 7a 49 [offset];. zI
19a12 64 78 20 3d 20 28 75 38 20 2a 29 26 7a 44 61 74 dx = (u8 *)&zDat
19a13 61 5b 73 7a 48 64 72 53 7a 5d 3b 0a 0a 20 20 20 a[szHdrSz];..
19a14 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68 65 61 /* Scan the hea
19a15 64 65 72 20 61 6e 64 20 75 73 65 20 69 74 20 74 der and use it t
19a16 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 61 54 o fill in the aT
19a17 79 70 65 5b 5d 20 61 6e 64 20 61 4f 66 66 73 65 ype[] and aOffse
19a18 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 t[]. ** array
19a19 73 2e 20 20 61 54 79 70 65 5b 69 5d 20 77 69 6c s. aType[i] wil
19a1a 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 79 l contain the ty
19a1b 70 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 74 pe integer for t
19a1c 68 65 20 69 2d 74 68 0a 20 20 20 20 2a 2a 20 63 he i-th. ** c
19a1d 6f 6c 75 6d 6e 20 61 6e 64 20 61 4f 66 66 73 65 olumn and aOffse
19a1e 74 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 t[i] will contai
19a1f 6e 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f n the offset fro
19a20 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a m the beginning.
19a21 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 ** of the re
19a22 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72 cord to the star
19a23 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f t of the data fo
19a24 72 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d r the i-th colum
19a25 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 n. */. for
19a26 28 69 3d 30 3b 20 69 3c 6e 46 69 65 6c 64 3b 20 (i=0; i<nField;
19a27 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 i++){. if(
19a28 7a 49 64 78 3c 7a 45 6e 64 48 64 72 20 29 7b 0a zIdx<zEndHdr ){.
19a29 20 20 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b aOffset[
19a2a 69 5d 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 20 i] = offset;.
19a2b 20 20 20 20 20 7a 49 64 78 20 2b 3d 20 67 65 74 zIdx += get
19a2c 56 61 72 69 6e 74 33 32 28 7a 49 64 78 2c 20 61 Varint32(zIdx, a
19a2d 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 Type[i]);.
19a2e 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 offset += sqli
19a2f 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
19a30 65 4c 65 6e 28 61 54 79 70 65 5b 69 5d 29 3b 0a eLen(aType[i]);.
19a31 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
19a32 20 20 20 20 20 2f 2a 20 49 66 20 69 20 69 73 20 /* If i is
19a33 6c 65 73 73 20 74 68 61 74 20 6e 46 69 65 6c 64 less that nField
19a34 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65 , then there are
19a35 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69 6e 20 less fields in
19a36 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 this. **
19a37 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65 74 4e record than SetN
19a38 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69 63 61 umColumns indica
19a39 74 65 64 20 74 68 65 72 65 20 61 72 65 20 63 6f ted there are co
19a3a 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20 20 20 lumns in the.
19a3b 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 53 ** table. S
19a3c 65 74 20 74 68 65 20 6f 66 66 73 65 74 20 66 6f et the offset fo
19a3d 72 20 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 r any extra colu
19a3e 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 mns not present
19a3f 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 in. ** th
19a40 65 20 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 e record to 0. T
19a41 68 69 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 his tells code b
19a42 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 elow to store a
19a43 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 NULL. **
19a44 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73 65 72 instead of deser
19a45 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 ializing a value
19a46 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 from the record
19a47 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
19a48 20 20 20 20 20 61 4f 66 66 73 65 74 5b 69 5d 20 aOffset[i]
19a49 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
19a4a 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 }. sqlite3Vd
19a4b 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 73 4d beMemRelease(&sM
19a4c 65 6d 29 3b 0a 20 20 20 20 73 4d 65 6d 2e 66 6c em);. sMem.fl
19a4d 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
19a4e 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 . /* If we ha
19a4f 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68 65 61 ve read more hea
19a50 64 65 72 20 64 61 74 61 20 74 68 61 6e 20 77 61 der data than wa
19a51 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 s contained in t
19a52 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20 20 2a he header,. *
19a53 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e 64 20 * or if the end
19a54 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c of the last fiel
19a55 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 d appears to be
19a56 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 past the end of
19a57 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 the. ** recor
19a58 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65 6e 64 d, or if the end
19a59 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 65 of the last fie
19a5a 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 ld appears to be
19a5b 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e 64 0a before the end.
19a5c 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 ** of the re
19a5d 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c 20 66 cord (when all f
19a5e 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29 2c 20 ields present),
19a5f 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62 65 20 then we must be
19a60 64 65 61 6c 69 6e 67 20 0a 20 20 20 20 2a 2a 20 dealing . **
19a61 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 64 with a corrupt d
19a62 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a atabase.. */.
19a63 20 20 20 20 69 66 28 20 7a 49 64 78 3e 7a 45 6e if( zIdx>zEn
19a64 64 48 64 72 20 7c 7c 20 6f 66 66 73 65 74 3e 70 dHdr || offset>p
19a65 61 79 6c 6f 61 64 53 69 7a 65 20 0a 20 20 20 20 ayloadSize .
19a66 20 7c 7c 20 28 7a 49 64 78 3d 3d 7a 45 6e 64 48 || (zIdx==zEndH
19a67 64 72 20 26 26 20 6f 66 66 73 65 74 21 3d 70 61 dr && offset!=pa
19a68 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a 20 20 yloadSize) ){.
19a69 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
19a6a 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
19a6b 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 goto op_colu
19a6c 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 mn_out;. }.
19a6d 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 }.. /* Get the
19a6e 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69 column informati
19a6f 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70 on. If aOffset[p
19a70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 2] is non-zero,
19a71 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65 73 65 72 then . ** deser
19a72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 65 ialize the value
19a73 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 from the record
19a74 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70 32 5d . If aOffset[p2]
19a75 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74 is zero,. ** t
19a76 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f hen there are no
19a77 74 20 65 6e 6f 75 67 68 20 66 69 65 6c 64 73 20 t enough fields
19a78 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f in the record to
19a79 20 73 61 74 69 73 66 79 20 74 68 65 0a 20 20 2a satisfy the. *
19a7a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 6e 20 74 * request. In t
19a7b 68 69 73 20 63 61 73 65 2c 20 73 65 74 20 74 68 his case, set th
19a7c 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 6f 72 20 e value NULL or
19a7d 74 6f 20 50 34 20 69 66 20 50 34 20 69 73 0a 20 to P4 if P4 is.
19a7e 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ** a pointer to
19a7f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 2e 0a 20 a Mem object..
19a80 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66 66 73 65 */. if( aOffse
19a81 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20 61 73 73 t[p2] ){. ass
19a82 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
19a83 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 OK );. if( zR
19a84 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ec ){. sqli
19a85 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
19a86 65 45 78 74 65 72 6e 61 6c 28 70 44 65 73 74 29 eExternal(pDest)
19a87 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
19a88 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 dbeSerialGet((u8
19a89 20 2a 29 26 7a 52 65 63 5b 61 4f 66 66 73 65 74 *)&zRec[aOffset
19a8a 5b 70 32 5d 5d 2c 20 61 54 79 70 65 5b 70 32 5d [p2]], aType[p2]
19a8b 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 , pDest);. }e
19a8c 6c 73 65 7b 0a 20 20 20 20 20 20 6c 65 6e 20 3d lse{. len =
19a8d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
19a8e 61 6c 54 79 70 65 4c 65 6e 28 61 54 79 70 65 5b alTypeLen(aType[
19a8f 70 32 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 p2]);. sqli
19a90 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 te3VdbeMemMove(&
19a91 73 4d 65 6d 2c 20 70 44 65 73 74 29 3b 0a 20 20 sMem, pDest);.
19a92 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
19a93 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 VdbeMemFromBtree
19a94 28 70 43 72 73 72 2c 20 61 4f 66 66 73 65 74 5b (pCrsr, aOffset[
19a95 70 32 5d 2c 20 6c 65 6e 2c 20 70 43 2d 3e 69 73 p2], len, pC->is
19a96 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 Index, &sMem);.
19a97 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
19a98 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
19a99 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e goto op_column
19a9a 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 _out;. }.
19a9b 20 20 20 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d zData = sMem
19a9c 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 .z;. sqlite
19a9d 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 3VdbeSerialGet((
19a9e 75 38 2a 29 7a 44 61 74 61 2c 20 61 54 79 70 65 u8*)zData, aType
19a9f 5b 70 32 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 [p2], pDest);.
19aa0 20 20 7d 0a 20 20 20 20 70 44 65 73 74 2d 3e 65 }. pDest->e
19aa1 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 nc = encoding;.
19aa2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 }else{. if(
19aa3 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
19aa4 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c MEM ){. sql
19aa5 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c ite3VdbeMemShall
19aa6 6f 77 43 6f 70 79 28 70 44 65 73 74 2c 20 70 4f owCopy(pDest, pO
19aa7 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f p->p4.pMem, MEM_
19aa8 53 74 61 74 69 63 29 3b 0a 20 20 20 20 7d 65 6c Static);. }el
19aa9 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
19aaa 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d ( pDest->flags&M
19aab 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 7d EM_Null );. }
19aac 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 . }.. /* If we
19aad 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c dynamically all
19aae 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f 20 ocated space to
19aaf 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28 69 hold the data (i
19ab0 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 n the. ** sqlit
19ab1 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 e3VdbeMemFromBtr
19ab2 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 29 ee() call above)
19ab3 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 63 then transfer c
19ab4 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a 20 ontrol of that.
19ab5 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 ** dynamically
19ab6 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 allocated space
19ab7 6f 76 65 72 20 74 6f 20 74 68 65 20 70 44 65 73 over to the pDes
19ab8 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a t structure.. *
19ab9 2a 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 * This prevents
19aba 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 a memory copy..
19abb 20 2a 2f 0a 20 20 69 66 28 20 73 4d 65 6d 2e 7a */. if( sMem.z
19abc 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 61 73 Malloc ){. as
19abd 73 65 72 74 28 20 73 4d 65 6d 2e 7a 3d 3d 73 4d sert( sMem.z==sM
19abe 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 em.zMalloc );.
19abf 20 20 61 73 73 65 72 74 28 20 21 28 70 44 65 73 assert( !(pDes
19ac0 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 t->flags & MEM_D
19ac1 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 yn) );. asser
19ac2 74 28 20 21 28 70 44 65 73 74 2d 3e 66 6c 61 67 t( !(pDest->flag
19ac3 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 s & (MEM_Blob|ME
19ac4 4d 5f 53 74 72 29 29 20 7c 7c 20 70 44 65 73 74 M_Str)) || pDest
19ac5 2d 3e 7a 3d 3d 73 4d 65 6d 2e 7a 20 29 3b 0a 20 ->z==sMem.z );.
19ac6 20 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 pDest->flags
19ac7 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d &= ~(MEM_Ephem|M
19ac8 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 EM_Static);.
19ac9 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 pDest->flags |=
19aca 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 70 44 MEM_Term;. pD
19acb 65 73 74 2d 3e 7a 20 3d 20 73 4d 65 6d 2e 7a 3b est->z = sMem.z;
19acc 0a 20 20 20 20 70 44 65 73 74 2d 3e 7a 4d 61 6c . pDest->zMal
19acd 6c 6f 63 20 3d 20 73 4d 65 6d 2e 7a 4d 61 6c 6c loc = sMem.zMall
19ace 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 oc;. }.. rc =
19acf 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 sqlite3VdbeMemMa
19ad0 6b 65 57 72 69 74 65 61 62 6c 65 28 70 44 65 73 keWriteable(pDes
19ad1 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f t);..op_column_o
19ad2 75 74 3a 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 ut:. UPDATE_MAX
19ad3 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 73 74 29 _BLOBSIZE(pDest)
19ad4 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 ;. REGISTER_TRA
19ad5 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44 65 73 CE(pOp->p3, pDes
19ad6 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
19ad7 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e /* Opcode: Affin
19ad8 69 74 79 20 50 31 20 50 32 20 2a 20 50 34 20 2a ity P1 P2 * P4 *
19ad9 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20 61 66 66 .**.** Apply aff
19ada 69 6e 69 74 69 65 73 20 74 6f 20 61 20 72 61 6e inities to a ran
19adb 67 65 20 6f 66 20 50 32 20 72 65 67 69 73 74 65 ge of P2 registe
19adc 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 rs starting with
19add 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 P1..**.** P4 is
19ade 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 a string that i
19adf 73 20 50 32 20 63 68 61 72 61 63 74 65 72 73 20 s P2 characters
19ae0 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68 20 63 68 long. The nth ch
19ae1 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a aracter of the.*
19ae2 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 * string indicat
19ae3 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 es the column af
19ae4 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 finity that shou
19ae5 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 ld be used for t
19ae6 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 he nth.** memory
19ae7 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72 61 6e cell in the ran
19ae8 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 ge..*/.case OP_A
19ae9 66 66 69 6e 69 74 79 3a 20 7b 0a 20 20 63 68 61 ffinity: {. cha
19aea 72 20 2a 7a 41 66 66 69 6e 69 74 79 20 3d 20 70 r *zAffinity = p
19aeb 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 4d 65 6d 20 Op->p4.z;. Mem
19aec 2a 70 44 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d *pData0 = &p->aM
19aed 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 4d em[pOp->p1];. M
19aee 65 6d 20 2a 70 4c 61 73 74 20 3d 20 26 70 44 61 em *pLast = &pDa
19aef 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a ta0[pOp->p2-1];.
19af0 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a 0a 20 20 Mem *pRec;..
19af1 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b for(pRec=pData0;
19af2 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 pRec<=pLast; pR
19af3 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78 70 61 6e ec++){. Expan
19af4 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 dBlob(pRec);.
19af5 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 applyAffinity(p
19af6 52 65 63 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70 Rec, zAffinity[p
19af7 52 65 63 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63 Rec-pData0], enc
19af8 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 62 72 oding);. }. br
19af9 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
19afa 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64 20 50 31 e: MakeRecord P1
19afb 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
19afc 2a 20 43 6f 6e 76 65 72 74 20 50 32 20 72 65 67 * Convert P2 reg
19afd 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 isters beginning
19afe 20 77 69 74 68 20 50 31 20 69 6e 74 6f 20 61 20 with P1 into a
19aff 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 single entry.**
19b00 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 suitable for use
19b01 20 61 73 20 61 20 64 61 74 61 20 72 65 63 6f 72 as a data recor
19b02 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 d in a database
19b03 74 61 62 6c 65 20 6f 72 20 61 73 20 61 20 6b 65 table or as a ke
19b04 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e 64 65 78 y.** in an index
19b05 2e 20 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f . The details o
19b06 66 20 74 68 65 20 66 6f 72 6d 61 74 20 61 72 65 f the format are
19b07 20 69 72 72 65 6c 65 76 61 6e 74 20 61 73 20 6c irrelevant as l
19b08 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65 20 4f 50 ong as.** the OP
19b09 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 20 63 _Column opcode c
19b0a 61 6e 20 64 65 63 6f 64 65 20 74 68 65 20 72 65 an decode the re
19b0b 63 6f 72 64 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 cord later..** R
19b0c 65 66 65 72 20 74 6f 20 73 6f 75 72 63 65 20 63 efer to source c
19b0d 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 ode comments for
19b0e 20 74 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 the details of
19b0f 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20 66 6f the record.** fo
19b10 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d rmat..**.** P4 m
19b11 61 79 20 62 65 20 61 20 73 74 72 69 6e 67 20 74 ay be a string t
19b12 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61 63 hat is P2 charac
19b13 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 ters long. The
19b14 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66 nth character of
19b15 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 the.** string i
19b16 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c ndicates the col
19b17 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61 umn affinity tha
19b18 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 t should be used
19b19 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 for the nth.**
19b1a 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 field of the ind
19b1b 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 ex key..**.** Th
19b1c 65 20 6d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 e mapping from c
19b1d 68 61 72 61 63 74 65 72 20 74 6f 20 61 66 66 69 haracter to affi
19b1e 6e 69 74 79 20 69 73 20 67 69 76 65 6e 20 62 79 nity is given by
19b1f 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f the SQLITE_AFF_
19b20 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 65 66 69 6e .** macros defin
19b21 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ed in sqliteInt.
19b22 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 h..**.** If P4 i
19b23 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 s NULL then all
19b24 69 6e 64 65 78 20 66 69 65 6c 64 73 20 68 61 76 index fields hav
19b25 65 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 4e e the affinity N
19b26 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ONE..*/.case OP_
19b27 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b 0a 20 20 MakeRecord: {.
19b28 2f 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 /* Assuming the
19b29 72 65 63 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 record contains
19b2a 4e 20 66 69 65 6c 64 73 2c 20 74 68 65 20 72 65 N fields, the re
19b2b 63 6f 72 64 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b cord format look
19b2c 73 0a 20 20 2a 2a 20 6c 69 6b 65 20 74 68 69 73 s. ** like this
19b2d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d :. **. ** ----
19b2e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
19b2f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
19b30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
19b31 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
19b32 2d 2d 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64 72 2d ----. ** | hdr-
19b33 73 69 7a 65 20 7c 20 74 79 70 65 20 30 20 7c 20 size | type 0 |
19b34 74 79 70 65 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 type 1 | ... | t
19b35 79 70 65 20 4e 2d 31 20 7c 20 64 61 74 61 30 20 ype N-1 | data0
19b36 7c 20 2e 2e 2e 20 7c 20 64 61 74 61 20 4e 2d 31 | ... | data N-1
19b37 20 7c 20 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d | . ** -------
19b38 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
19b39 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
19b3a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
19b3b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
19b3c 2d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 -. **. ** Data
19b3d 28 30 29 20 69 73 20 74 61 6b 65 6e 20 66 72 6f (0) is taken fro
19b3e 6d 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 m register P1.
19b3f 44 61 74 61 28 31 29 20 63 6f 6d 65 73 20 66 72 Data(1) comes fr
19b40 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 2b 31 om register P1+1
19b41 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f . ** and so fro
19b42 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 th.. **. ** Ea
19b43 63 68 20 74 79 70 65 20 66 69 65 6c 64 20 69 73 ch type field is
19b44 20 61 20 76 61 72 69 6e 74 20 72 65 70 72 65 73 a varint repres
19b45 65 6e 74 69 6e 67 20 74 68 65 20 73 65 72 69 61 enting the seria
19b46 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 0a 20 l type of the .
19b47 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ** correspondin
19b48 67 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 28 g data element (
19b49 73 65 65 20 73 71 6c 69 74 65 33 56 64 62 65 53 see sqlite3VdbeS
19b4a 65 72 69 61 6c 54 79 70 65 28 29 29 2e 20 54 68 erialType()). Th
19b4b 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69 7a 65 20 e. ** hdr-size
19b4c 66 69 65 6c 64 20 69 73 20 61 6c 73 6f 20 61 20 field is also a
19b4d 76 61 72 69 6e 74 20 77 68 69 63 68 20 69 73 20 varint which is
19b4e 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f 6d 20 the offset from
19b4f 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 the beginning.
19b50 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 ** of the record
19b51 20 74 6f 20 64 61 74 61 30 2e 0a 20 20 2a 2f 0a to data0.. */.
19b52 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 u8 *zNewRecord
19b53 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75 ; /* A bu
19b54 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 ffer to hold the
19b55 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65 data for the ne
19b56 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 w record */. Me
19b57 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 m *pRec;
19b58 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 /* The new
19b59 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20 record */. u64
19b5a 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 20 20 20 nData = 0;
19b5b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
19b5c 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 bytes of data sp
19b5d 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 ace */. int nHd
19b5e 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 r = 0;
19b5f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
19b60 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 es of header spa
19b61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 ce */. i64 nByt
19b62 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f e = 0; /
19b63 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71 * Data space req
19b64 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 uired for this r
19b65 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e ecord */. int n
19b66 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 Zero = 0;
19b67 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a /* Number of z
19b68 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 ero bytes at the
19b69 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f end of the reco
19b6a 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 rd */. int nVar
19b6b 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f int; /
19b6c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
19b6d 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f s in a varint */
19b6e 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 . u32 serial_ty
19b6f 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 pe; /* Typ
19b70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d e field */. Mem
19b71 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 *pData0;
19b72 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65 /* First fie
19b73 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 ld to be combine
19b74 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 d into the recor
19b75 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 d */. Mem *pLas
19b76 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
19b77 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 Last field of t
19b78 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 he record */. i
19b79 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 nt nField;
19b7a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
19b7b 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 of fields in the
19b7c 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 record */. cha
19b7d 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 r *zAffinity;
19b7e 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e /* The affin
19b7f 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 ity string for t
19b80 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 he record */. i
19b81 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 nt file_format;
19b82 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f /* File fo
19b83 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20 rmat to use for
19b84 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e encoding */. in
19b85 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
19b86 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73 /* Space us
19b87 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 ed in zNewRecord
19b88 5b 5d 20 2a 2f 0a 0a 20 20 6e 46 69 65 6c 64 20 [] */.. nField
19b89 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 7a 41 66 = pOp->p1;. zAf
19b8a 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 finity = pOp->p4
19b8b 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 46 .z;. assert( nF
19b8c 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ield>0 && pOp->p
19b8d 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 6e 2>0 && pOp->p2+n
19b8e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 Field<=p->nMem )
19b8f 3b 0a 20 20 70 44 61 74 61 30 20 3d 20 26 70 2d ;. pData0 = &p-
19b90 3e 61 4d 65 6d 5b 6e 46 69 65 6c 64 5d 3b 0a 20 >aMem[nField];.
19b91 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 nField = pOp->p
19b92 32 3b 0a 20 20 70 4c 61 73 74 20 3d 20 26 70 44 2;. pLast = &pD
19b93 61 74 61 30 5b 6e 46 69 65 6c 64 2d 31 5d 3b 0a ata0[nField-1];.
19b94 20 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 file_format =
19b95 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 p->minWriteFileF
19b96 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f ormat;.. /* Loo
19b97 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c p through the el
19b98 65 6d 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c ements that will
19b99 20 6d 61 6b 65 20 75 70 20 74 68 65 20 72 65 63 make up the rec
19b9a 6f 72 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20 ord to figure.
19b9b 2a 2a 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 ** out how much
19b9c 73 70 61 63 65 20 69 73 20 72 65 71 75 69 72 65 space is require
19b9d 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 d for the new re
19b9e 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 cord.. */. for
19b9f 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 (pRec=pData0; pR
19ba0 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b ec<=pLast; pRec+
19ba1 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b +){. int len;
19ba2 0a 20 20 20 20 69 66 28 20 7a 41 66 66 69 6e 69 . if( zAffini
19ba3 74 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c ty ){. appl
19ba4 79 41 66 66 69 6e 69 74 79 28 70 52 65 63 2c 20 yAffinity(pRec,
19ba5 7a 41 66 66 69 6e 69 74 79 5b 70 52 65 63 2d 70 zAffinity[pRec-p
19ba6 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 Data0], encoding
19ba7 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
19ba8 20 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d pRec->flags&MEM
19ba9 5f 5a 65 72 6f 20 26 26 20 70 52 65 63 2d 3e 6e _Zero && pRec->n
19baa 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 >0 ){. sqli
19bab 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
19bac 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20 Blob(pRec);.
19bad 7d 0a 20 20 20 20 73 65 72 69 61 6c 5f 74 79 70 }. serial_typ
19bae 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 e = sqlite3VdbeS
19baf 65 72 69 61 6c 54 79 70 65 28 70 52 65 63 2c 20 erialType(pRec,
19bb0 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 file_format);.
19bb1 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 len = sqlite3V
19bb2 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e dbeSerialTypeLen
19bb3 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 (serial_type);.
19bb4 20 20 20 6e 44 61 74 61 20 2b 3d 20 6c 65 6e 3b nData += len;
19bb5 0a 20 20 20 20 6e 48 64 72 20 2b 3d 20 73 71 6c . nHdr += sql
19bb6 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 73 65 ite3VarintLen(se
19bb7 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 rial_type);.
19bb8 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 if( pRec->flags
19bb9 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 & MEM_Zero ){.
19bba 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 /* Only pure
19bbb 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f zero-filled BLO
19bbc 42 73 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20 Bs can be input
19bbd 74 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a to this Opcode..
19bbe 20 20 20 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e ** We do n
19bbf 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 ot allow blobs w
19bc0 69 74 68 20 61 20 70 72 65 66 69 78 20 61 6e 64 ith a prefix and
19bc1 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 a zero-filled t
19bc2 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 5a ail. */. nZ
19bc3 65 72 6f 20 2b 3d 20 70 52 65 63 2d 3e 75 2e 69 ero += pRec->u.i
19bc4 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
19bc5 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 len ){. nZe
19bc6 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 ro = 0;. }.
19bc7 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 }.. /* Add the
19bc8 69 6e 69 74 69 61 6c 20 68 65 61 64 65 72 20 76 initial header v
19bc9 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 61 6c 20 arint and total
19bca 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 6e 48 the size */. nH
19bcb 64 72 20 2b 3d 20 6e 56 61 72 69 6e 74 20 3d 20 dr += nVarint =
19bcc 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e sqlite3VarintLen
19bcd 28 6e 48 64 72 29 3b 0a 20 20 69 66 28 20 6e 56 (nHdr);. if( nV
19bce 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56 61 72 arint<sqlite3Var
19bcf 69 6e 74 4c 65 6e 28 6e 48 64 72 29 20 29 7b 0a intLen(nHdr) ){.
19bd0 20 20 20 20 6e 48 64 72 2b 2b 3b 0a 20 20 7d 0a nHdr++;. }.
19bd1 20 20 6e 42 79 74 65 20 3d 20 6e 48 64 72 2b 6e nByte = nHdr+n
19bd2 44 61 74 61 2d 6e 5a 65 72 6f 3b 0a 20 20 69 66 Data-nZero;. if
19bd3 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d ( nByte>db->aLim
19bd4 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
19bd5 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 LENGTH] ){. g
19bd6 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d oto too_big;. }
19bd7 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
19bd8 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 the output regi
19bd9 73 74 65 72 20 68 61 73 20 61 20 62 75 66 66 65 ster has a buffe
19bda 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 r large enough t
19bdb 6f 20 73 74 6f 72 65 20 0a 20 20 2a 2a 20 74 68 o store . ** th
19bdc 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 54 68 e new record. Th
19bdd 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 e output registe
19bde 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73 20 6e r (pOp->p3) is n
19bdf 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 20 20 ot allowed to.
19be0 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 ** be one of the
19be1 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73 input registers
19be2 20 28 62 65 63 61 75 73 65 20 74 68 65 20 66 6f (because the fo
19be3 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 0a llowing call to.
19be4 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 ** sqlite3Vdbe
19be5 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c 64 20 MemGrow() could
19be6 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61 6c 75 clobber the valu
19be7 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 75 e before it is u
19be8 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 sed).. */. ass
19be9 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 ert( pOp->p3<pOp
19bea 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e ->p1 || pOp->p3>
19beb 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 =pOp->p1+pOp->p2
19bec 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d );. pOut = &p-
19bed 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
19bee 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
19bef 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e eMemGrow(pOut, n
19bf0 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 Byte, 0) ){.
19bf1 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d goto no_mem;. }
19bf2 0a 20 20 7a 4e 65 77 52 65 63 6f 72 64 20 3d 20 . zNewRecord =
19bf3 28 75 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a 0a (u8 *)pOut->z;..
19bf4 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 /* Write the r
19bf5 65 63 6f 72 64 20 2a 2f 0a 20 20 69 20 3d 20 70 ecord */. i = p
19bf6 75 74 56 61 72 69 6e 74 33 32 28 7a 4e 65 77 52 utVarint32(zNewR
19bf7 65 63 6f 72 64 2c 20 6e 48 64 72 29 3b 0a 20 20 ecord, nHdr);.
19bf8 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b for(pRec=pData0;
19bf9 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 pRec<=pLast; pR
19bfa 65 63 2b 2b 29 7b 0a 20 20 20 20 73 65 72 69 61 ec++){. seria
19bfb 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 l_type = sqlite3
19bfc 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 VdbeSerialType(p
19bfd 52 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 Rec, file_format
19bfe 29 3b 0a 20 20 20 20 69 20 2b 3d 20 70 75 74 56 );. i += putV
19bff 61 72 69 6e 74 33 32 28 26 7a 4e 65 77 52 65 63 arint32(&zNewRec
19c00 6f 72 64 5b 69 5d 2c 20 73 65 72 69 61 6c 5f 74 ord[i], serial_t
19c01 79 70 65 29 3b 20 20 20 20 20 20 2f 2a 20 73 65 ype); /* se
19c02 72 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d rial type */. }
19c03 0a 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 . for(pRec=pDat
19c04 61 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b a0; pRec<=pLast;
19c05 20 70 52 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 pRec++){ /* se
19c06 72 69 61 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 rial data */.
19c07 20 69 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 i += sqlite3Vdb
19c08 65 53 65 72 69 61 6c 50 75 74 28 26 7a 4e 65 77 eSerialPut(&zNew
19c09 52 65 63 6f 72 64 5b 69 5d 2c 20 6e 42 79 74 65 Record[i], nByte
19c0a 2d 69 2c 20 70 52 65 63 2c 20 66 69 6c 65 5f 66 -i, pRec, file_f
19c0b 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 73 ormat);. }. as
19c0c 73 65 72 74 28 20 69 3d 3d 6e 42 79 74 65 20 29 sert( i==nByte )
19c0d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 ;.. assert( pOp
19c0e 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p3>0 && pOp->p
19c0f 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
19c10 70 4f 75 74 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b pOut->n = nByte;
19c11 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d . pOut->flags =
19c12 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f MEM_Blob | MEM_
19c13 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78 44 65 Dyn;. pOut->xDe
19c14 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 5a 65 l = 0;. if( nZe
19c15 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e ro ){. pOut->
19c16 75 2e 69 20 3d 20 6e 5a 65 72 6f 3b 0a 20 20 20 u.i = nZero;.
19c17 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 pOut->flags |=
19c18 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 MEM_Zero;. }.
19c19 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pOut->enc = SQLI
19c1a 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 TE_UTF8; /* In
19c1b 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73 case the blob is
19c1c 20 65 76 65 72 20 63 6f 6e 76 65 72 74 65 64 20 ever converted
19c1d 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 52 45 47 to text */. REG
19c1e 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
19c1f 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 >p3, pOut);. UP
19c20 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
19c21 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b E(pOut);. break
19c22 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
19c23 53 74 61 74 65 6d 65 6e 74 20 50 31 20 2a 20 2a Statement P1 * *
19c24 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e * *.**.** Begin
19c25 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 73 an individual s
19c26 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
19c27 74 69 6f 6e 20 77 68 69 63 68 20 69 73 20 70 61 tion which is pa
19c28 72 74 20 6f 66 20 61 20 6c 61 72 67 65 72 0a 2a rt of a larger.*
19c29 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 * transaction.
19c2a 54 68 69 73 20 69 73 20 6e 65 65 64 65 64 20 73 This is needed s
19c2b 6f 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 o that the state
19c2c 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 ment.** can be r
19c2d 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 olled back after
19c2e 20 61 6e 20 65 72 72 6f 72 20 77 69 74 68 6f 75 an error withou
19c2f 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c t having to roll
19c30 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 65 6e 74 back the.** ent
19c31 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ire transaction.
19c32 20 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 The statement
19c33 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c transaction will
19c34 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a automatically.*
19c35 2a 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68 * commit when th
19c36 65 20 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a e VDBE halts..**
19c37 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 .** If the datab
19c38 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 ase connection i
19c39 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 s currently in a
19c3a 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 28 utocommit mode (
19c3b 74 68 61 74 20 0a 2a 2a 20 69 73 20 74 6f 20 73 that .** is to s
19c3c 61 79 2c 20 69 66 20 69 74 20 69 73 20 69 6e 20 ay, if it is in
19c3d 62 65 74 77 65 65 6e 20 42 45 47 49 4e 20 61 6e between BEGIN an
19c3e 64 20 43 4f 4d 4d 49 54 29 0a 2a 2a 20 61 6e 64 d COMMIT).** and
19c3f 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f if there are no
19c40 20 6f 74 68 65 72 20 61 63 74 69 76 65 20 73 74 other active st
19c41 61 74 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 atements on the
19c42 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a same database.**
19c43 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 connection, the
19c44 6e 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e n this operation
19c45 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 4e 6f is a no-op. No
19c46 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
19c47 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 65 65 action.** is nee
19c48 64 65 64 20 73 69 6e 63 65 20 61 6e 79 20 65 72 ded since any er
19c49 72 6f 72 20 63 61 6e 20 75 73 65 20 74 68 65 20 ror can use the
19c4a 6e 6f 72 6d 61 6c 20 52 4f 4c 4c 42 41 43 4b 20 normal ROLLBACK
19c4b 70 72 6f 63 65 73 73 20 74 6f 0a 2a 2a 20 75 6e process to.** un
19c4c 64 6f 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a do changes..**.*
19c4d 2a 20 49 66 20 61 20 73 74 61 74 65 6d 65 6e 74 * If a statement
19c4e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
19c4f 73 74 61 72 74 65 64 2c 20 74 68 65 6e 20 61 20 started, then a
19c50 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 statement journa
19c51 6c 20 66 69 6c 65 0a 2a 2a 20 77 69 6c 6c 20 62 l file.** will b
19c52 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 e allocated and
19c53 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a initialized..**.
19c54 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 ** The statement
19c55 20 69 73 20 62 65 67 75 6e 20 6f 6e 20 74 68 65 is begun on the
19c56 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
19c57 69 74 68 20 69 6e 64 65 78 20 50 31 2e 20 20 54 ith index P1. T
19c58 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 he main.** datab
19c59 61 73 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 ase file has an
19c5a 69 6e 64 65 78 20 6f 66 20 30 20 61 6e 64 20 74 index of 0 and t
19c5b 68 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 he file used for
19c5c 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
19c5d 73 0a 2a 2a 20 68 61 73 20 61 6e 20 69 6e 64 65 s.** has an inde
19c5e 78 20 6f 66 20 31 2e 0a 2a 2f 0a 63 61 73 65 20 x of 1..*/.case
19c5f 4f 50 5f 53 74 61 74 65 6d 65 6e 74 3a 20 7b 0a OP_Statement: {.
19c60 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f if( db->autoCo
19c61 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 mmit==0 || db->a
19c62 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 ctiveVdbeCnt>1 )
19c63 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4f {. int i = pO
19c64 70 2d 3e 70 31 3b 0a 20 20 20 20 42 74 72 65 65 p->p1;. Btree
19c65 20 2a 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 *pBt;. asser
19c66 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d t( i>=0 && i<db-
19c67 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 >nDb );. asse
19c68 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 rt( db->aDb[i].p
19c69 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 Bt!=0 );. pBt
19c6a 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
19c6b 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 t;. assert( s
19c6c 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 qlite3BtreeIsInT
19c6d 72 61 6e 73 28 70 42 74 29 20 29 3b 0a 20 20 20 rans(pBt) );.
19c6e 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 assert( (p->btr
19c6f 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29 eeMask & (1<<i))
19c70 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 !=0 );. if( !
19c71 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e sqlite3BtreeIsIn
19c72 53 74 6d 74 28 70 42 74 29 20 29 7b 0a 20 20 20 Stmt(pBt) ){.
19c73 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
19c74 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 70 42 treeBeginStmt(pB
19c75 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 70 65 t);. p->ope
19c76 6e 65 64 53 74 61 74 65 6d 65 6e 74 20 3d 20 31 nedStatement = 1
19c77 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
19c78 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
19c79 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 e: AutoCommit P1
19c7a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
19c7b 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 Set the database
19c7c 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 auto-commit fla
19c7d 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29 g to P1 (1 or 0)
19c7e 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c . If P2 is true,
19c7f 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e roll.** back an
19c80 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 y currently acti
19c81 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63 ve btree transac
19c82 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20 tions. If there
19c83 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a are any active.*
19c84 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f * VMs (apart fro
19c85 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 m this one), the
19c86 6e 20 74 68 65 20 43 4f 4d 4d 49 54 20 6f 72 20 n the COMMIT or
19c87 52 4f 4c 4c 42 41 43 4b 20 73 74 61 74 65 6d 65 ROLLBACK stateme
19c88 6e 74 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 nt fails..**.**
19c89 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
19c8a 20 63 61 75 73 65 73 20 74 68 65 20 56 4d 20 74 causes the VM t
19c8b 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 o halt..*/.case
19c8c 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a 20 7b OP_AutoCommit: {
19c8d 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 41 75 . int desiredAu
19c8e 74 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70 2d 3e toCommit = pOp->
19c8f 70 31 3b 0a 20 20 69 6e 74 20 72 6f 6c 6c 62 61 p1;. int rollba
19c90 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 ck = pOp->p2;.
19c91 69 6e 74 20 74 75 72 6e 4f 6e 41 43 20 3d 20 64 int turnOnAC = d
19c92 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
19c93 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d && !db->autoCom
19c94 6d 69 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 mit;.. assert(
19c95 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 desiredAutoCommi
19c96 74 3d 3d 31 20 7c 7c 20 64 65 73 69 72 65 64 41 t==1 || desiredA
19c97 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a utoCommit==0 );.
19c98 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 assert( desire
19c99 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c dAutoCommit==1 |
19c9a 7c 20 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20 29 3b | rollback==0 );
19c9b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e .. assert( db->
19c9c 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20 activeVdbeCnt>0
19c9d 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73 74 20 ); /* At least
19c9e 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73 20 61 this one VM is a
19c9f 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66 28 20 ctive */.. if(
19ca0 74 75 72 6e 4f 6e 41 43 20 26 26 20 72 6f 6c 6c turnOnAC && roll
19ca1 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63 74 69 back && db->acti
19ca2 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 veVdbeCnt>1 ){.
19ca3 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e /* If this in
19ca4 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d struction implem
19ca5 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 ents a ROLLBACK
19ca6 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 and other VMs ar
19ca7 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72 e. ** still r
19ca8 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20 74 72 unning, and a tr
19ca9 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
19caa 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 ive, return an e
19cab 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 0a rror indicating.
19cac 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 ** that the
19cad 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63 other VMs must c
19cae 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 20 0a omplete first. .
19caf 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
19cb0 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
19cb1 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 zErrMsg, db, "ca
19cb2 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 72 nnot rollback tr
19cb3 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 ansaction - ".
19cb4 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 "SQL state
19cb5 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 ments in progres
19cb6 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 s");. rc = SQ
19cb7 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c LITE_BUSY;. }el
19cb8 73 65 20 69 66 28 20 74 75 72 6e 4f 6e 41 43 20 se if( turnOnAC
19cb9 26 26 20 21 72 6f 6c 6c 62 61 63 6b 20 26 26 20 && !rollback &&
19cba 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 db->writeVdbeCnt
19cbb 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 >1 ){. /* If
19cbc 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
19cbd 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 43 4f implements a CO
19cbe 4d 4d 49 54 20 61 6e 64 20 6f 74 68 65 72 20 56 MMIT and other V
19cbf 4d 73 20 61 72 65 20 77 72 69 74 69 6e 67 0a 20 Ms are writing.
19cc0 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20 ** return an
19cc1 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 error indicating
19cc2 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20 that the other
19cc3 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74 VMs must complet
19cc4 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20 2a 2f e first. . */
19cc5 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
19cc6 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
19cc7 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 63 g, db, "cannot c
19cc8 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74 69 6f ommit transactio
19cc9 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20 22 53 n - ". "S
19cca 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e QL statements in
19ccb 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 20 progress");.
19ccc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
19ccd 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 Y;. }else if( d
19cce 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
19ccf 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 !=db->autoCommit
19cd0 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d ){. if( pOp-
19cd1 3e 70 32 20 29 7b 0a 20 20 20 20 20 20 61 73 73 >p2 ){. ass
19cd2 65 72 74 28 20 64 65 73 69 72 65 64 41 75 74 6f ert( desiredAuto
19cd3 43 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a 20 20 20 Commit==1 );.
19cd4 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 sqlite3Rollba
19cd5 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 ckAll(db);.
19cd6 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 db->autoCommit
19cd7 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 1;. }else{.
19cd8 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f db->autoCo
19cd9 6d 6d 69 74 20 3d 20 64 65 73 69 72 65 64 41 75 mmit = desiredAu
19cda 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 toCommit;.
19cdb 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48 if( sqlite3VdbeH
19cdc 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 alt(p)==SQLITE_B
19cdd 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 70 USY ){. p
19cde 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 ->pc = pc;.
19cdf 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 db->autoCommi
19ce0 74 20 3d 20 31 2d 64 65 73 69 72 65 64 41 75 74 t = 1-desiredAut
19ce1 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 20 oCommit;.
19ce2 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 p->rc = rc = SQ
19ce3 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 LITE_BUSY;.
19ce4 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 goto vdbe_ret
19ce5 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 urn;. }.
19ce6 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 }. if( p->rc
19ce7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
19ce8 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
19ce9 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 _DONE;. }else
19cea 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
19ceb 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d ITE_ERROR;. }
19cec 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 . goto vdbe_r
19ced 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a eturn;. }else{.
19cee 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
19cef 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
19cf0 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21 , db,. (!
19cf1 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 desiredAutoCommi
19cf2 74 29 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72 74 t)?"cannot start
19cf3 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 a transaction w
19cf4 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 ithin a transact
19cf5 69 6f 6e 22 3a 28 0a 20 20 20 20 20 20 20 20 28 ion":(. (
19cf6 72 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f rollback)?"canno
19cf7 74 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 t rollback - no
19cf8 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
19cf9 63 74 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 ctive":.
19cfa 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e "cann
19cfb 6f 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 ot commit - no t
19cfc 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
19cfd 74 69 76 65 22 29 29 3b 0a 20 20 20 20 20 20 20 tive"));.
19cfe 20 20 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
19cff 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 TE_ERROR;. }.
19d00 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
19d01 6f 64 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e ode: Transaction
19d02 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
19d03 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 ** Begin a trans
19d04 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61 action. The tra
19d05 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68 nsaction ends wh
19d06 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52 en a Commit or R
19d07 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64 ollback.** opcod
19d08 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 e is encountered
19d09 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 . Depending on
19d0a 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 the ON CONFLICT
19d0b 73 65 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20 setting, the.**
19d0c 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 transaction migh
19d0d 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64 t also be rolled
19d0e 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f back if an erro
19d0f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 r is encountered
19d10 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 ..**.** P1 is th
19d11 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 e index of the d
19d12 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 atabase file on
19d13 77 68 69 63 68 20 74 68 65 20 74 72 61 6e 73 61 which the transa
19d14 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72 ction is.** star
19d15 74 65 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73 ted. Index 0 is
19d16 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
19d17 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65 se file and inde
19d18 78 20 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 x 1 is the.** fi
19d19 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 le used for temp
19d1a 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49 orary tables. I
19d1b 6e 64 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d ndices of 2 or m
19d1c 6f 72 65 20 61 72 65 20 75 73 65 64 20 66 6f 72 ore are used for
19d1d 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 .** attached dat
19d1e 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 abases..**.** If
19d1f 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c P2 is non-zero,
19d20 20 74 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 then a write-tr
19d21 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 ansaction is sta
19d22 72 74 65 64 2e 20 20 41 20 52 45 53 45 52 56 45 rted. A RESERVE
19d23 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 D lock is.** obt
19d24 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 ained on the dat
19d25 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 abase file when
19d26 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 a write-transact
19d27 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 ion is started.
19d28 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f No.** other pro
19d29 63 65 73 73 20 63 61 6e 20 73 74 61 72 74 20 61 cess can start a
19d2a 6e 6f 74 68 65 72 20 77 72 69 74 65 20 74 72 61 nother write tra
19d2b 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 nsaction while t
19d2c 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 his transaction
19d2d 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20 is.** underway.
19d2e 20 53 74 61 72 74 69 6e 67 20 61 20 77 72 69 74 Starting a writ
19d2f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c e transaction al
19d30 73 6f 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c so creates a rol
19d31 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41 lback journal. A
19d32 0a 2a 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61 .** write transa
19d33 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 ction must be st
19d34 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 arted before any
19d35 20 63 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20 changes can be
19d36 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64 made to the.** d
19d37 61 74 61 62 61 73 65 2e 20 20 49 66 20 50 32 20 atabase. If P2
19d38 69 73 20 32 20 6f 72 20 67 72 65 61 74 65 72 20 is 2 or greater
19d39 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 then an EXCLUSIV
19d3a 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f E lock is also o
19d3b 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 btained.** on th
19d3c 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e file..**.** If
19d3d 20 50 32 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 P2 is zero, the
19d3e 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 n a read-lock is
19d3f 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 obtained on the
19d40 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
19d41 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 6e 73 */.case OP_Trans
19d42 61 63 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e 74 20 action: {. int
19d43 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 42 i = pOp->p1;. B
19d44 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61 73 tree *pBt;.. as
19d45 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
19d46 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 db->nDb );. ass
19d47 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 ert( (p->btreeMa
19d48 73 6b 20 26 20 28 31 3c 3c 69 29 29 21 3d 30 20 sk & (1<<i))!=0
19d49 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 );. pBt = db->a
19d4a 44 62 5b 69 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 Db[i].pBt;.. if
19d4b 28 20 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 ( pBt ){. rc
19d4c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 = sqlite3BtreeBe
19d4d 67 69 6e 54 72 61 6e 73 28 70 42 74 2c 20 70 4f ginTrans(pBt, pO
19d4e 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69 66 28 20 p->p2);. if(
19d4f 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
19d50 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 3d ){. p->pc =
19d51 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 pc;. p->rc
19d52 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 = rc = SQLITE_B
19d53 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 USY;. goto
19d54 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 vdbe_return;.
19d55 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 }. if( rc!=S
19d56 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d QLITE_OK && rc!=
19d57 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 SQLITE_READONLY
19d58 2f 2a 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 /* && rc!=SQLITE
19d59 5f 42 55 53 59 20 2a 2f 20 29 7b 0a 20 20 20 20 _BUSY */ ){.
19d5a 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
19d5b 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d _to_error;. }
19d5c 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
19d5d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 ./* Opcode: Read
19d5e 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 Cookie P1 P2 P3
19d5f 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 * *.**.** Read c
19d60 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 ookie number P3
19d61 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 from database P1
19d62 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e and write it in
19d63 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
19d64 2a 2a 20 50 33 3d 3d 30 20 69 73 20 74 68 65 20 ** P3==0 is the
19d65 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 schema version.
19d66 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 64 61 P3==1 is the da
19d67 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a tabase format..*
19d68 2a 20 50 33 3d 3d 32 20 69 73 20 74 68 65 20 72 * P3==2 is the r
19d69 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 ecommended pager
19d6a 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 cache size, and
19d6b 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d so forth. P1==
19d6c 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 0 is.** the main
19d6d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
19d6e 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 nd P1==1 is the
19d6f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 database file us
19d70 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 ed to store.** t
19d71 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e emporary tables.
19d72 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 .**.** If P1 is
19d73 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 negative, then t
19d74 68 69 73 20 69 73 20 61 20 72 65 71 75 65 73 74 his is a request
19d75 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 69 7a to read the siz
19d76 65 20 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 e of a.** databa
19d77 73 65 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 50 ses free-list. P
19d78 33 20 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 3 must be set to
19d79 20 31 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 1 in this case.
19d7a 20 54 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 64 The actual.** d
19d7b 61 74 61 62 61 73 65 20 61 63 63 65 73 73 65 64 atabase accessed
19d7c 20 69 73 20 28 28 50 31 2b 31 29 2a 2d 31 29 2e is ((P1+1)*-1).
19d7d 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 20 For example, a
19d7e 50 31 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 P1 parameter of
19d7f 2d 31 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 -1.** correspond
19d80 73 20 74 6f 20 64 61 74 61 62 61 73 65 20 30 20 s to database 0
19d81 28 22 6d 61 69 6e 22 29 2c 20 61 20 50 31 20 6f ("main"), a P1 o
19d82 66 20 2d 32 20 69 73 20 64 61 74 61 62 61 73 65 f -2 is database
19d83 20 31 20 28 22 74 65 6d 70 22 29 2e 0a 2a 2a 0a 1 ("temp")..**.
19d84 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 ** There must be
19d85 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 a read-lock on
19d86 74 68 65 20 64 61 74 61 62 61 73 65 20 28 65 69 the database (ei
19d87 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 ther a transacti
19d88 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74 on.** must be st
19d89 61 72 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d arted or there m
19d8a 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 ust be an open c
19d8b 75 72 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a ursor) before.**
19d8c 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 executing this
19d8d 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a instruction..*/.
19d8e 63 61 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b case OP_ReadCook
19d8f 69 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 ie: {
19d90 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
19d91 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 elease */. int
19d92 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 iMeta;. int iDb
19d93 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e = pOp->p1;. in
19d94 74 20 69 43 6f 6f 6b 69 65 20 3d 20 70 4f 70 2d t iCookie = pOp-
19d95 3e 70 33 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 >p3;.. assert(
19d96 70 4f 70 2d 3e 70 33 3c 53 51 4c 49 54 45 5f 4e pOp->p3<SQLITE_N
19d97 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 _BTREE_META );.
19d98 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 if( iDb<0 ){.
19d99 20 20 69 44 62 20 3d 20 28 2d 31 2a 28 69 44 62 iDb = (-1*(iDb
19d9a 2b 31 29 29 3b 0a 20 20 20 20 69 43 6f 6f 6b 69 +1));. iCooki
19d9b 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 61 e *= -1;. }. a
19d9c 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 ssert( iDb>=0 &&
19d9d 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a iDb<db->nDb );.
19d9e 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 assert( db->aD
19d9f 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b b[iDb].pBt!=0 );
19da0 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 . assert( (p->b
19da1 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 treeMask & (1<<i
19da2 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 2f 2a 20 Db))!=0 );. /*
19da3 54 68 65 20 69 6e 64 65 78 69 6e 67 20 6f 66 20 The indexing of
19da4 6d 65 74 61 20 76 61 6c 75 65 73 20 61 74 20 74 meta values at t
19da5 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 he schema layer
19da6 69 73 20 6f 66 66 20 62 79 20 6f 6e 65 20 66 72 is off by one fr
19da7 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 om. ** the inde
19da8 78 69 6e 67 20 69 6e 20 74 68 65 20 62 74 72 65 xing in the btre
19da9 65 20 6c 61 79 65 72 2e 20 20 54 68 65 20 62 74 e layer. The bt
19daa 72 65 65 20 63 6f 6e 73 69 64 65 72 73 20 6d 65 ree considers me
19dab 74 61 5b 30 5d 20 74 6f 0a 20 20 2a 2a 20 62 65 ta[0] to. ** be
19dac 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 the number of f
19dad 72 65 65 20 70 61 67 65 73 20 69 6e 20 74 68 65 ree pages in the
19dae 20 64 61 74 61 62 61 73 65 20 28 61 20 72 65 61 database (a rea
19daf 64 2d 6f 6e 6c 79 20 76 61 6c 75 65 29 0a 20 20 d-only value).
19db0 2a 2a 20 61 6e 64 20 6d 65 74 61 5b 31 5d 20 74 ** and meta[1] t
19db1 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d 61 20 o be the schema
19db2 63 6f 6f 6b 69 65 2e 20 20 54 68 65 20 73 63 68 cookie. The sch
19db3 65 6d 61 20 6c 61 79 65 72 20 63 6f 6e 73 69 64 ema layer consid
19db4 65 72 73 0a 20 20 2a 2a 20 6d 65 74 61 5b 31 5d ers. ** meta[1]
19db5 20 74 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d to be the schem
19db6 61 20 63 6f 6f 6b 69 65 2e 20 20 53 6f 20 77 65 a cookie. So we
19db7 20 68 61 76 65 20 74 6f 20 73 68 69 66 74 20 74 have to shift t
19db8 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 62 79 he index. ** by
19db9 20 6f 6e 65 20 69 6e 20 74 68 65 20 66 6f 6c 6c one in the foll
19dba 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 2e owing statement.
19dbb 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c . */. rc = sql
19dbc 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 ite3BtreeGetMeta
19dbd 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 (db->aDb[iDb].pB
19dbe 74 2c 20 31 20 2b 20 69 43 6f 6f 6b 69 65 2c 20 t, 1 + iCookie,
19dbf 28 75 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a (u32 *)&iMeta);.
19dc0 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 69 4d pOut->u.i = iM
19dc1 65 74 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 eta;. MemSetTyp
19dc2 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
19dc3 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Int);. break;.}
19dc4 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 ../* Opcode: Set
19dc5 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 Cookie P1 P2 P3
19dc6 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 * *.**.** Write
19dc7 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
19dc8 65 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 egister P3 (inte
19dc9 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e rpreted as an in
19dca 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 teger).** into c
19dcb 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 ookie number P2
19dcc 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 0a of database P1..
19dcd 2a 2a 20 50 32 3d 3d 30 20 69 73 20 74 68 65 20 ** P2==0 is the
19dce 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 schema version.
19dcf 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20 64 61 P2==1 is the da
19dd0 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a tabase format..*
19dd1 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20 72 * P2==2 is the r
19dd2 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 ecommended pager
19dd3 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 cache size, and
19dd4 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d so forth. P1==
19dd5 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 0 is.** the main
19dd6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
19dd7 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 nd P1==1 is the
19dd8 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 database file us
19dd9 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 ed to store.** t
19dda 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e emporary tables.
19ddb 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 .**.** A transac
19ddc 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 tion must be sta
19ddd 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 rted before exec
19dde 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 uting this opcod
19ddf 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 e..*/.case OP_Se
19de0 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 tCookie: {
19de1 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 44 62 20 /* in3 */. Db
19de2 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20 *pDb;. assert(
19de3 70 4f 70 2d 3e 70 32 3c 53 51 4c 49 54 45 5f 4e pOp->p2<SQLITE_N
19de4 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 _BTREE_META );.
19de5 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
19de6 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 >=0 && pOp->p1<d
19de7 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 b->nDb );. asse
19de8 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 rt( (p->btreeMas
19de9 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 k & (1<<pOp->p1)
19dea 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 )!=0 );. pDb =
19deb 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 &db->aDb[pOp->p1
19dec 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 ];. assert( pDb
19ded 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 73 71 ->pBt!=0 );. sq
19dee 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 lite3VdbeMemInte
19def 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 gerify(pIn3);.
19df0 2f 2a 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 /* See note abou
19df1 74 20 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67 t index shifting
19df2 20 6f 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 on OP_ReadCooki
19df3 65 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 e */. rc = sqli
19df4 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 te3BtreeUpdateMe
19df5 74 61 28 70 44 62 2d 3e 70 42 74 2c 20 31 2b 70 ta(pDb->pBt, 1+p
19df6 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e Op->p2, (int)pIn
19df7 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70 3->u.i);. if( p
19df8 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 Op->p2==0 ){.
19df9 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 73 63 68 /* When the sch
19dfa 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 ema cookie chang
19dfb 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e es, record the n
19dfc 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 6e ew cookie intern
19dfd 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 70 44 62 2d ally */. pDb-
19dfe 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 >pSchema->schema
19dff 5f 63 6f 6f 6b 69 65 20 3d 20 70 49 6e 33 2d 3e _cookie = pIn3->
19e00 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 u.i;. db->fla
19e01 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 gs |= SQLITE_Int
19e02 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 65 ernChanges;. }e
19e03 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d lse if( pOp->p2=
19e04 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 63 =1 ){. /* Rec
19e05 6f 72 64 20 63 68 61 6e 67 65 73 20 69 6e 20 74 ord changes in t
19e06 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 2a he file format *
19e07 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 /. pDb->pSche
19e08 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 ma->file_format
19e09 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d = pIn3->u.i;. }
19e0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d . if( pOp->p1==
19e0b 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61 1 ){. /* Inva
19e0c 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 70 61 lidate all prepa
19e0d 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 77 red statements w
19e0e 68 65 6e 65 76 65 72 20 74 68 65 20 54 45 4d 50 henever the TEMP
19e0f 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a database. **
19e10 20 73 63 68 65 6d 61 20 69 73 20 63 68 61 6e 67 schema is chang
19e11 65 64 2e 20 20 54 69 63 6b 65 74 20 23 31 36 34 ed. Ticket #164
19e12 34 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4 */. sqlite3
19e13 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 ExpirePreparedSt
19e14 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 atements(db);.
19e15 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
19e16 20 4f 70 63 6f 64 65 3a 20 56 65 72 69 66 79 43 Opcode: VerifyC
19e17 6f 6f 6b 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a ookie P1 P2 *.**
19e18 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61 .** Check the va
19e19 6c 75 65 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61 lue of global da
19e1a 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 65 72 tabase parameter
19e1b 20 6e 75 6d 62 65 72 20 30 20 28 74 68 65 0a 2a number 0 (the.*
19e1c 2a 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e * schema version
19e1d 29 20 61 6e 64 20 6d 61 6b 65 20 73 75 72 65 20 ) and make sure
19e1e 69 74 20 69 73 20 65 71 75 61 6c 20 74 6f 20 50 it is equal to P
19e1f 32 2e 20 20 0a 2a 2a 20 50 31 20 69 73 20 74 68 2. .** P1 is th
19e20 65 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 e database numbe
19e21 72 20 77 68 69 63 68 20 69 73 20 30 20 66 6f 72 r which is 0 for
19e22 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
19e23 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 31 se file.** and 1
19e24 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 68 6f for the file ho
19e25 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 lding temporary
19e26 74 61 62 6c 65 73 20 61 6e 64 20 73 6f 6d 65 20 tables and some
19e27 68 69 67 68 65 72 20 6e 75 6d 62 65 72 0a 2a 2a higher number.**
19e28 20 66 6f 72 20 61 75 78 69 6c 69 61 72 79 20 64 for auxiliary d
19e29 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 atabases..**.**
19e2a 54 68 65 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 The cookie chang
19e2b 65 73 20 69 74 73 20 76 61 6c 75 65 20 77 68 65 es its value whe
19e2c 6e 65 76 65 72 20 74 68 65 20 64 61 74 61 62 61 never the databa
19e2d 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 se schema change
19e2e 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 s..** This opera
19e2f 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
19e30 64 65 74 65 63 74 20 77 68 65 6e 20 74 68 61 74 detect when that
19e31 20 74 68 65 20 63 6f 6f 6b 69 65 20 68 61 73 20 the cookie has
19e32 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 changed.** and t
19e33 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 hat the current
19e34 70 72 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f process needs to
19e35 20 72 65 72 65 61 64 20 74 68 65 20 73 63 68 65 reread the sche
19e36 6d 61 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 72 ma..**.** Either
19e37 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e a transaction n
19e38 65 65 64 73 20 74 6f 20 68 61 76 65 20 62 65 65 eeds to have bee
19e39 6e 20 73 74 61 72 74 65 64 20 6f 72 20 61 6e 20 n started or an
19e3a 4f 50 5f 4f 70 65 6e 20 6e 65 65 64 73 0a 2a 2a OP_Open needs.**
19e3b 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 to be executed
19e3c 28 74 6f 20 65 73 74 61 62 6c 69 73 68 20 61 20 (to establish a
19e3d 72 65 61 64 20 6c 6f 63 6b 29 20 62 65 66 6f 72 read lock) befor
19e3e 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 e this opcode is
19e3f 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a .** invoked..*/.
19e40 63 61 73 65 20 4f 50 5f 56 65 72 69 66 79 43 6f case OP_VerifyCo
19e41 6f 6b 69 65 3a 20 7b 0a 20 20 69 6e 74 20 69 4d okie: {. int iM
19e42 65 74 61 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 eta;. Btree *pB
19e43 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 t;. assert( pOp
19e44 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
19e45 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 p1<db->nDb );.
19e46 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 assert( (p->btre
19e47 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d eMask & (1<<pOp-
19e48 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 42 >p1))!=0 );. pB
19e49 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d t = db->aDb[pOp-
19e4a 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20 >p1].pBt;. if(
19e4b 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 pBt ){. rc =
19e4c 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d sqlite3BtreeGetM
19e4d 65 74 61 28 70 42 74 2c 20 31 2c 20 28 75 33 32 eta(pBt, 1, (u32
19e4e 20 2a 29 26 69 4d 65 74 61 29 3b 0a 20 20 7d 65 *)&iMeta);. }e
19e4f 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 lse{. rc = SQ
19e50 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 4d 65 LITE_OK;. iMe
19e51 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 ta = 0;. }. if
19e52 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
19e53 26 26 20 69 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 && iMeta!=pOp->p
19e54 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 2 ){. sqlite3
19e55 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
19e56 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
19e57 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 ErrMsg = sqlite3
19e58 44 62 53 74 72 44 75 70 28 64 62 2c 20 22 64 61 DbStrDup(db, "da
19e59 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 tabase schema ha
19e5a 73 20 63 68 61 6e 67 65 64 22 29 3b 0a 20 20 20 s changed");.
19e5b 20 2f 2a 20 49 66 20 74 68 65 20 73 63 68 65 6d /* If the schem
19e5c 61 2d 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 a-cookie from th
19e5d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
19e5e 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f 6f 6b matches the cook
19e5f 69 65 20 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 ie . ** store
19e60 64 20 77 69 74 68 20 74 68 65 20 69 6e 2d 6d 65 d with the in-me
19e61 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 mory representat
19e62 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 68 65 6d ion of the schem
19e63 61 2c 20 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f 74 a, do. ** not
19e64 20 72 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65 reload the sche
19e65 6d 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ma from the data
19e66 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a base file.. *
19e67 2a 0a 20 20 20 20 2a 2a 20 49 66 20 76 69 72 74 *. ** If virt
19e68 75 61 6c 2d 74 61 62 6c 65 73 20 61 72 65 20 69 ual-tables are i
19e69 6e 20 75 73 65 2c 20 74 68 69 73 20 69 73 20 6e n use, this is n
19e6a 6f 74 20 6a 75 73 74 20 61 6e 20 6f 70 74 69 6d ot just an optim
19e6b 69 7a 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 ization.. **
19e6c 4f 66 74 65 6e 2c 20 76 2d 74 61 62 6c 65 73 20 Often, v-tables
19e6d 73 74 6f 72 65 20 74 68 65 69 72 20 64 61 74 61 store their data
19e6e 20 69 6e 20 6f 74 68 65 72 20 53 51 4c 69 74 65 in other SQLite
19e6f 20 74 61 62 6c 65 73 2c 20 77 68 69 63 68 0a 20 tables, which.
19e70 20 20 20 2a 2a 20 61 72 65 20 71 75 65 72 69 65 ** are querie
19e71 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 78 4e d from within xN
19e72 65 78 74 28 29 20 61 6e 64 20 6f 74 68 65 72 20 ext() and other
19e73 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 73 20 v-table methods
19e74 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 70 72 65 using. ** pre
19e75 70 61 72 65 64 20 71 75 65 72 69 65 73 2e 20 49 pared queries. I
19e76 66 20 73 75 63 68 20 61 20 71 75 65 72 79 20 69 f such a query i
19e77 73 20 6f 75 74 2d 6f 66 2d 64 61 74 65 2c 20 77 s out-of-date, w
19e78 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f e do not want to
19e79 0a 20 20 20 20 2a 2a 20 64 69 73 63 61 72 64 20 . ** discard
19e7a 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
19e7b 65 6d 61 2c 20 61 73 20 74 68 65 20 75 73 65 72 ema, as the user
19e7c 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 69 code implementi
19e7d 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 2d ng the. ** v-
19e7e 74 61 62 6c 65 20 77 6f 75 6c 64 20 68 61 76 65 table would have
19e7f 20 74 6f 20 62 65 20 72 65 61 64 79 20 66 6f 72 to be ready for
19e80 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 the sqlite3_vta
19e81 62 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 b structure itse
19e82 6c 66 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 lf. ** to be
19e83 69 6e 76 61 6c 69 64 61 74 65 64 20 77 68 65 6e invalidated when
19e84 65 76 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 ever sqlite3_ste
19e85 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 66 72 p() is called fr
19e86 6f 6d 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a om within . *
19e87 2a 20 61 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 * a v-table meth
19e88 6f 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 od.. */. i
19e89 66 28 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e f( db->aDb[pOp->
19e8a 70 31 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 p1].pSchema->sch
19e8b 65 6d 61 5f 63 6f 6f 6b 69 65 21 3d 69 4d 65 74 ema_cookie!=iMet
19e8c 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 a ){. sqlit
19e8d 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 e3ResetInternalS
19e8e 63 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 chema(db, pOp->p
19e8f 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 1);. }.. s
19e90 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 qlite3ExpirePrep
19e91 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 aredStatements(d
19e92 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c b);. rc = SQL
19e93 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a ITE_SCHEMA;. }.
19e94 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
19e95 70 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 pcode: OpenRead
19e96 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
19e97 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 *.** Open a read
19e98 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 -only cursor for
19e99 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 the database ta
19e9a 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 ble whose root p
19e9b 61 67 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 age is.** P2 in
19e9c 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e a database file.
19e9d 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 The database f
19e9e 69 6c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 ile is determine
19e9f 64 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d d by P3. .** P3=
19ea0 3d 30 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 =0 means the mai
19ea1 6e 20 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d n database, P3==
19ea2 31 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 1 means the data
19ea3 62 61 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a base used for .*
19ea4 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c * temporary tabl
19ea5 65 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 es, and P3>1 mea
19ea6 6e 73 20 75 73 65 64 20 74 68 65 20 63 6f 72 72 ns used the corr
19ea7 65 73 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 esponding attach
19ea8 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 ed.** database.
19ea9 20 47 69 76 65 20 74 68 65 20 6e 65 77 20 63 75 Give the new cu
19eaa 72 73 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 rsor an identifi
19eab 65 72 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50 er of P1. The P
19eac 31 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 1.** values need
19ead 20 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f not be contiguo
19eae 75 73 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61 us but all P1 va
19eaf 6c 75 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 lues should be s
19eb0 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a mall integers..*
19eb1 2a 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 * It is an error
19eb2 20 66 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 for P1 to be ne
19eb3 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 gative..**.** If
19eb4 20 50 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20 P5!=0 then use
19eb5 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
19eb6 65 67 69 73 74 65 72 20 50 32 20 61 73 20 74 68 egister P2 as th
19eb7 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 e root page, not
19eb8 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 .** the value of
19eb9 20 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a P2 itself..**.*
19eba 2a 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 * There will be
19ebb 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 a read lock on t
19ebc 68 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e he database when
19ebd 65 76 65 72 20 74 68 65 72 65 20 69 73 20 61 6e ever there is an
19ebe 0a 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e .** open cursor.
19ebf 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 If the databas
19ec0 65 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 e was unlocked p
19ec1 72 69 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73 rior to this ins
19ec2 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e truction.** then
19ec3 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 a read lock is
19ec4 61 63 71 75 69 72 65 64 20 61 73 20 70 61 72 74 acquired as part
19ec5 20 6f 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 of this instruc
19ec6 74 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a tion. A read.**
19ec7 20 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 lock allows oth
19ec8 65 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 er processes to
19ec9 72 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 read the databas
19eca 65 20 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a e but prohibits.
19ecb 2a 2a 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f ** any other pro
19ecc 63 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 cess from modify
19ecd 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 ing the database
19ece 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b . The read lock
19ecf 20 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 is.** released
19ed0 77 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 when all cursors
19ed1 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 are closed. If
19ed2 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
19ed3 6e 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f n attempts.** to
19ed4 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b get a read lock
19ed5 20 62 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20 but fails, the
19ed6 73 63 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65 script terminate
19ed7 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c s with an.** SQL
19ed8 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63 ITE_BUSY error c
19ed9 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 ode..**.** The P
19eda 34 20 76 61 6c 75 65 20 69 73 20 61 20 70 6f 69 4 value is a poi
19edb 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 nter to a KeyInf
19edc 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 o structure that
19edd 20 64 65 66 69 6e 65 73 20 74 68 65 0a 2a 2a 20 defines the.**
19ede 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c content and coll
19edf 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f ating sequence o
19ee0 66 20 69 6e 64 69 63 65 73 2e 20 20 50 34 20 69 f indices. P4 i
19ee1 73 20 4e 55 4c 4c 20 66 6f 72 20 63 75 72 73 6f s NULL for curso
19ee2 72 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 6e rs.** that are n
19ee3 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 ot pointing to i
19ee4 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 ndices..**.** Se
19ee5 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 e also OpenWrite
19ee6 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
19ee7 4f 70 65 6e 57 72 69 74 65 20 50 31 20 50 32 20 OpenWrite P1 P2
19ee8 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f P3 P4 P5.**.** O
19ee9 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 pen a read/write
19eea 20 63 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 cursor named P1
19eeb 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 on the table or
19eec 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f index whose roo
19eed 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e t.** page is P2.
19eee 20 20 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73 Or if P5!=0 us
19eef 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
19ef0 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 register P2 to
19ef1 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 find the.** root
19ef2 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 page..**.** The
19ef3 20 50 34 20 76 61 6c 75 65 20 69 73 20 61 20 70 P4 value is a p
19ef4 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 ointer to a KeyI
19ef5 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 nfo structure th
19ef6 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 0a 2a at defines the.*
19ef7 2a 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f * content and co
19ef8 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
19ef9 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 50 34 of indices. P4
19efa 20 69 73 20 4e 55 4c 4c 20 66 6f 72 20 63 75 72 is NULL for cur
19efb 73 6f 72 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 sors.** that are
19efc 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f not pointing to
19efd 20 69 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 indices..**.**
19efe 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
19eff 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 works just like
19f00 20 4f 70 65 6e 52 65 61 64 20 65 78 63 65 70 74 OpenRead except
19f01 20 74 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74 that it opens t
19f02 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 he cursor.** in
19f03 72 65 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e read/write mode.
19f04 20 20 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61 For a given ta
19f05 62 6c 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62 ble, there can b
19f06 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 e one or more re
19f07 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f ad-only.** curso
19f08 72 73 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 rs or a single r
19f09 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 ead/write cursor
19f0a 20 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a but not both..*
19f0b 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 *.** See also Op
19f0c 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 enRead..*/.case
19f0d 4f 50 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 OP_OpenRead:.cas
19f0e 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 e OP_OpenWrite:
19f0f 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d {. int i = pOp-
19f10 3e 70 31 3b 0a 20 20 69 6e 74 20 70 32 20 3d 20 >p1;. int p2 =
19f11 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 6e 74 20 69 pOp->p2;. int i
19f12 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 Db = pOp->p3;.
19f13 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 42 74 int wrFlag;. Bt
19f14 72 65 65 20 2a 70 58 3b 0a 20 20 56 64 62 65 43 ree *pX;. VdbeC
19f15 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 44 ursor *pCur;. D
19f16 62 20 2a 70 44 62 3b 0a 20 20 0a 20 20 61 73 73 b *pDb;. . ass
19f17 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 ert( iDb>=0 && i
19f18 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 Db<db->nDb );.
19f19 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 assert( (p->btre
19f1a 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 62 29 eMask & (1<<iDb)
19f1b 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 )!=0 );. pDb =
19f1c 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 &db->aDb[iDb];.
19f1d 20 70 58 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a pX = pDb->pBt;.
19f1e 20 20 61 73 73 65 72 74 28 20 70 58 21 3d 30 20 assert( pX!=0
19f1f 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 );. if( pOp->op
19f20 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 code==OP_OpenWri
19f21 74 65 20 29 7b 0a 20 20 20 20 77 72 46 6c 61 67 te ){. wrFlag
19f22 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 70 44 = 1;. if( pD
19f23 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 b->pSchema->file
19f24 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e _format < p->min
19f25 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 WriteFileFormat
19f26 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57 ){. p->minW
19f27 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d riteFileFormat =
19f28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 pDb->pSchema->f
19f29 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 20 20 ile_format;.
19f2a 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 }. }else{. w
19f2b 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 rFlag = 0;. }.
19f2c 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a if( pOp->p5 ){.
19f2d 20 20 20 20 61 73 73 65 72 74 28 20 70 32 3e 30 assert( p2>0
19f2e 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
19f2f 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p2<=p->nMem );.
19f30 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d pIn2 = &p->aM
19f31 65 6d 5b 70 32 5d 3b 0a 20 20 20 20 73 71 6c 69 em[p2];. sqli
19f32 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
19f33 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 20 20 rify(pIn2);.
19f34 70 32 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a p2 = pIn2->u.i;.
19f35 20 20 20 20 69 66 28 20 70 32 3c 32 20 29 20 7b if( p2<2 ) {
19f36 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
19f37 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
19f38 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 . goto abor
19f39 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
19f3a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 }. }. asse
19f3b 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43 rt( i>=0 );. pC
19f3c 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 ur = allocateCur
19f3d 73 6f 72 28 70 2c 20 69 2c 20 26 70 4f 70 5b 2d sor(p, i, &pOp[-
19f3e 31 5d 2c 20 69 44 62 2c 20 31 29 3b 0a 20 20 69 1], iDb, 1);. i
19f3f 66 28 20 70 43 75 72 3d 3d 30 20 29 20 67 6f 74 f( pCur==0 ) got
19f40 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 75 72 o no_mem;. pCur
19f41 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 ->nullRow = 1;.
19f42 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
19f43 65 65 43 75 72 73 6f 72 28 70 58 2c 20 70 32 2c eeCursor(pX, p2,
19f44 20 77 72 46 6c 61 67 2c 20 70 4f 70 2d 3e 70 34 wrFlag, pOp->p4
19f45 2e 70 2c 20 70 43 75 72 2d 3e 70 43 75 72 73 6f .p, pCur->pCurso
19f46 72 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 r);. if( pOp->p
19f47 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4type==P4_KEYINF
19f48 4f 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 O ){. pCur->p
19f49 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 KeyInfo = pOp->p
19f4a 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 4.pKeyInfo;.
19f4b 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e pCur->pKeyInfo->
19f4c 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 enc = ENC(p->db)
19f4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
19f4e 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 Cur->pKeyInfo =
19f4f 30 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 0;. }. switch(
19f50 20 72 63 20 29 7b 0a 20 20 20 20 63 61 73 65 20 rc ){. case
19f51 53 51 4c 49 54 45 5f 42 55 53 59 3a 20 7b 0a 20 SQLITE_BUSY: {.
19f52 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b p->pc = pc;
19f53 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 . p->rc = r
19f54 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
19f55 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 . goto vdbe
19f56 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 _return;. }.
19f57 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4f case SQLITE_O
19f58 4b 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 K: {. int f
19f59 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 42 74 lags = sqlite3Bt
19f5a 72 65 65 46 6c 61 67 73 28 70 43 75 72 2d 3e 70 reeFlags(pCur->p
19f5b 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 2f Cursor);. /
19f5c 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e * Sanity checkin
19f5d 67 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6c 6f 77 g. Only the low
19f5e 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f 66 20 er four bits of
19f5f 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 73 the flags byte s
19f60 68 6f 75 6c 64 0a 20 20 20 20 20 20 2a 2a 20 62 hould. ** b
19f61 65 20 75 73 65 64 2e 20 20 42 69 74 20 33 20 28 e used. Bit 3 (
19f62 6d 61 73 6b 20 30 78 30 38 29 20 69 73 20 75 6e mask 0x08) is un
19f63 70 72 65 64 69 63 74 61 62 6c 65 2e 20 20 54 68 predictable. Th
19f64 65 20 6c 6f 77 65 72 20 33 20 62 69 74 73 0a 20 e lower 3 bits.
19f65 20 20 20 20 20 2a 2a 20 28 6d 61 73 6b 20 30 78 ** (mask 0x
19f66 30 37 29 20 73 68 6f 75 6c 64 20 62 65 20 65 69 07) should be ei
19f67 74 68 65 72 20 35 20 28 69 6e 74 6b 65 79 2b 6c ther 5 (intkey+l
19f68 65 61 66 64 61 74 61 20 66 6f 72 20 74 61 62 6c eafdata for tabl
19f69 65 73 29 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 es) or. **
19f6a 32 20 28 7a 65 72 6f 64 61 74 61 20 66 6f 72 20 2 (zerodata for
19f6b 69 6e 64 69 63 65 73 29 2e 20 20 49 66 20 74 68 indices). If th
19f6c 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 ese conditions a
19f6d 72 65 20 6e 6f 74 20 6d 65 74 20 69 74 20 63 61 re not met it ca
19f6e 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 n. ** only
19f6f 6d 65 61 6e 20 74 68 61 74 20 77 65 20 61 72 65 mean that we are
19f70 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 dealing with a
19f71 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
19f72 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2f 0a 20 file. */.
19f73 20 20 20 20 20 69 66 28 20 28 66 6c 61 67 73 20 if( (flags
19f74 26 20 30 78 66 30 29 21 3d 30 20 7c 7c 20 28 28 & 0xf0)!=0 || ((
19f75 66 6c 61 67 73 20 26 20 30 78 30 37 29 21 3d 35 flags & 0x07)!=5
19f76 20 26 26 20 28 66 6c 61 67 73 20 26 20 30 78 30 && (flags & 0x0
19f77 37 29 21 3d 32 29 20 29 7b 0a 20 20 20 20 20 20 7)!=2) ){.
19f78 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
19f79 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
19f7a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
19f7b 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
19f7c 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75 72 2d }. pCur-
19f7d 3e 69 73 54 61 62 6c 65 20 3d 20 28 66 6c 61 67 >isTable = (flag
19f7e 73 20 26 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 s & BTREE_INTKEY
19f7f 29 21 3d 30 3b 0a 20 20 20 20 20 20 70 43 75 72 )!=0;. pCur
19f80 2d 3e 69 73 49 6e 64 65 78 20 3d 20 28 66 6c 61 ->isIndex = (fla
19f81 67 73 20 26 20 42 54 52 45 45 5f 5a 45 52 4f 44 gs & BTREE_ZEROD
19f82 41 54 41 29 21 3d 30 3b 0a 20 20 20 20 20 20 2f ATA)!=0;. /
19f83 2a 20 49 66 20 50 34 3d 3d 30 20 69 74 20 6d 65 * If P4==0 it me
19f84 61 6e 73 20 77 65 20 61 72 65 20 65 78 70 65 63 ans we are expec
19f85 74 65 64 20 74 6f 20 6f 70 65 6e 20 61 20 74 61 ted to open a ta
19f86 62 6c 65 2e 20 20 49 66 20 50 34 21 3d 30 20 74 ble. If P4!=0 t
19f87 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 hen. ** we
19f88 65 78 70 65 63 74 20 74 6f 20 62 65 20 6f 70 65 expect to be ope
19f89 6e 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 20 20 ning an index.
19f8a 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 77 If this is not w
19f8b 68 61 74 20 68 61 70 70 65 6e 65 64 2c 0a 20 20 hat happened,.
19f8c 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 ** then the
19f8d 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 database is corr
19f8e 75 70 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 upt. */.
19f8f 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e 69 73 if( (pCur->is
19f90 54 61 62 6c 65 20 26 26 20 70 4f 70 2d 3e 70 34 Table && pOp->p4
19f91 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f type==P4_KEYINFO
19f92 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 43 75 ). || (pCu
19f93 72 2d 3e 69 73 49 6e 64 65 78 20 26 26 20 70 4f r->isIndex && pO
19f94 70 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 p->p4type!=P4_KE
19f95 59 49 4e 46 4f 29 20 29 7b 0a 20 20 20 20 20 20 YINFO) ){.
19f96 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f rc = SQLITE_CO
19f97 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
19f98 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
19f99 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
19f9a 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
19f9b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
19f9c 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20 7b SQLITE_EMPTY: {
19f9d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 54 . pCur->isT
19f9e 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 able = pOp->p4ty
19f9f 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a pe!=P4_KEYINFO;.
19fa0 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e pCur->isIn
19fa1 64 65 78 20 3d 20 21 70 43 75 72 2d 3e 69 73 54 dex = !pCur->isT
19fa2 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 43 75 72 able;. pCur
19fa3 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 ->pCursor = 0;.
19fa4 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
19fa5 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b _OK;. break
19fa6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
19fa7 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 67 6f 74 ult: {. got
19fa8 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
19fa9 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rror;. }. }.
19faa 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
19fab 70 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d pcode: OpenEphem
19fac 65 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20 eral P1 P2 * P4
19fad 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e *.**.** Open a n
19fae 65 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 ew cursor P1 to
19faf 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c a transient tabl
19fb0 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 e..** The cursor
19fb1 20 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 is always opene
19fb2 64 20 72 65 61 64 2f 77 72 69 74 65 20 65 76 65 d read/write eve
19fb3 6e 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 n if .** the mai
19fb4 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 n database is re
19fb5 61 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72 ad-only. The tr
19fb6 61 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74 75 ansient or virtu
19fb7 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 al.** table is d
19fb8 65 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69 63 eleted automatic
19fb9 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63 75 ally when the cu
19fba 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a rsor is closed..
19fbb 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 **.** P2 is the
19fbc 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
19fbd 73 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c s in the virtual
19fbe 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 table..** The c
19fbf 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 ursor points to
19fc0 61 20 42 54 72 65 65 20 74 61 62 6c 65 20 69 66 a BTree table if
19fc1 20 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20 P4==0 and to a
19fc2 42 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69 BTree index.** i
19fc3 66 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20 20 f P4 is not 0.
19fc4 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c If P4 is not NUL
19fc5 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 L, it points to
19fc6 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 a KeyInfo struct
19fc7 75 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66 69 ure.** that defi
19fc8 6e 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f nes the format o
19fc9 66 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69 6e f keys in the in
19fca 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 dex..**.** This
19fcb 6f 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65 20 opcode was once
19fcc 63 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e called OpenTemp.
19fcd 20 20 42 75 74 20 74 68 61 74 20 63 72 65 61 74 But that creat
19fce 65 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 ed.** confusion
19fcf 62 65 63 61 75 73 65 20 74 68 65 20 74 65 72 6d because the term
19fd0 20 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d "temp table", m
19fd1 69 67 68 74 20 72 65 66 65 72 20 65 69 74 68 65 ight refer eithe
19fd2 72 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20 74 r.** to a TEMP t
19fd3 61 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c 20 able at the SQL
19fd4 6c 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74 level, or to a t
19fd5 61 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a able opened by.*
19fd6 2a 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 * this opcode.
19fd7 54 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 Then this opcode
19fd8 20 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69 was call OpenVi
19fd9 72 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 rtual. But.** t
19fda 68 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e 66 hat created conf
19fdb 75 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20 77 usion with the w
19fdc 68 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61 62 hole virtual-tab
19fdd 6c 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 le idea..*/.case
19fde 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 OP_OpenEphemera
19fdf 6c 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 l: {. int i = p
19fe0 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 Op->p1;. VdbeCu
19fe1 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 73 74 61 rsor *pCx;. sta
19fe2 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 tic const int op
19fe3 65 6e 46 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 enFlags = .
19fe4 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
19fe5 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53 DWRITE |. S
19fe6 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
19fe7 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 E |. SQLITE
19fe8 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 _OPEN_EXCLUSIVE
19fe9 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f |. SQLITE_O
19fea 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
19feb 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 E |. SQLITE
19fec 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f _OPEN_TRANSIENT_
19fed 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 DB;.. assert( i
19fee 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 61 >=0 );. pCx = a
19fef 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c llocateCursor(p,
19ff0 20 69 2c 20 70 4f 70 2c 20 2d 31 2c 20 31 29 3b i, pOp, -1, 1);
19ff1 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 20 . if( pCx==0 )
19ff2 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 goto no_mem;. p
19ff3 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b Cx->nullRow = 1;
19ff4 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
19ff5 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 treeFactory(db,
19ff6 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45 46 0, 1, SQLITE_DEF
19ff7 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f AULT_TEMP_CACHE_
19ff8 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73 2c SIZE, openFlags,
19ff9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
19ffa 20 20 20 20 20 20 20 20 20 20 20 20 26 70 43 78 &pCx
19ffb 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 ->pBt);. if( rc
19ffc 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
19ffd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
19ffe 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 treeBeginTrans(p
19fff 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a 20 20 7d Cx->pBt, 1);. }
1a000 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1a001 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 49 E_OK ){. /* I
1a002 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 69 6e f a transient in
1a003 64 65 78 20 69 73 20 72 65 71 75 69 72 65 64 2c dex is required,
1a004 20 63 72 65 61 74 65 20 69 74 20 62 79 20 63 61 create it by ca
1a005 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 73 71 6c lling. ** sql
1a006 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 ite3BtreeCreateT
1a007 61 62 6c 65 28 29 20 77 69 74 68 20 74 68 65 20 able() with the
1a008 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 66 BTREE_ZERODATA f
1a009 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 20 20 2a lag before. *
1a00a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e 20 49 66 * opening it. If
1a00b 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 a transient tab
1a00c 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 le is required,
1a00d 6a 75 73 74 20 75 73 65 20 74 68 65 0a 20 20 20 just use the.
1a00e 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ** automaticall
1a00f 79 20 63 72 65 61 74 65 64 20 74 61 62 6c 65 20 y created table
1a010 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 31 with root-page 1
1a011 20 28 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c (an INTKEY tabl
1a012 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 e).. */. i
1a013 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 f( pOp->p4.pKeyI
1a014 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 nfo ){. int
1a015 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 61 73 73 pgno;. ass
1a016 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1a017 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 3b 0a ==P4_KEYINFO );.
1a018 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1a019 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 e3BtreeCreateTab
1a01a 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 le(pCx->pBt, &pg
1a01b 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 no, BTREE_ZERODA
1a01c 54 41 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 TA); . if(
1a01d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1a01e 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1a01f 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f pgno==MASTER_RO
1a020 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20 20 20 20 OT+1 );.
1a021 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1a022 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 42 74 eCursor(pCx->pBt
1a023 2c 20 70 67 6e 6f 2c 20 31 2c 20 0a 20 20 20 20 , pgno, 1, .
1a024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a025 20 20 20 20 20 20 20 20 20 20 20 20 28 4b 65 79 (Key
1a026 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c Info*)pOp->p4.z,
1a027 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a pCx->pCursor);.
1a028 20 20 20 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 pCx->pKe
1a029 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e yInfo = pOp->p4.
1a02a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 pKeyInfo;.
1a02b 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d pCx->pKeyInfo-
1a02c 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 >enc = ENC(p->db
1a02d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1a02e 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 pCx->isTable =
1a02f 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
1a030 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1a031 42 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 2d BtreeCursor(pCx-
1a032 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f >pBt, MASTER_ROO
1a033 54 2c 20 31 2c 20 30 2c 20 70 43 78 2d 3e 70 43 T, 1, 0, pCx->pC
1a034 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 70 43 ursor);. pC
1a035 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a x->isTable = 1;.
1a036 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 43 78 2d }. }. pCx-
1a037 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 43 78 2d >isIndex = !pCx-
1a038 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 >isTable;. brea
1a039 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1a03a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 31 20 50 OpenPseudo P1 P
1a03b 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 2 * * *.**.** Op
1a03c 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 en a new cursor
1a03d 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 that points to a
1a03e 20 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61 74 fake table that
1a03f 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 contains a sing
1a040 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 le.** row of dat
1a041 61 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 a. Any attempt
1a042 74 6f 20 77 72 69 74 65 20 61 20 73 65 63 6f 6e to write a secon
1a043 64 20 72 6f 77 20 6f 66 20 64 61 74 61 20 63 61 d row of data ca
1a044 75 73 65 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 uses the.** firs
1a045 74 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 t row to be dele
1a046 74 65 64 2e 20 20 41 6c 6c 20 64 61 74 61 20 69 ted. All data i
1a047 73 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 s deleted when t
1a048 68 65 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 he cursor is.**
1a049 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 closed..**.** A
1a04a 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 72 65 pseudo-table cre
1a04b 61 74 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 ated by this opc
1a04c 6f 64 65 20 69 73 20 75 73 65 66 75 6c 20 66 6f ode is useful fo
1a04d 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a r holding the.**
1a04e 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c NEW or OLD tabl
1a04f 65 73 20 69 6e 20 61 20 74 72 69 67 67 65 72 2e es in a trigger.
1a050 20 20 41 6c 73 6f 20 75 73 65 64 20 74 6f 20 68 Also used to h
1a051 6f 6c 64 20 74 68 65 20 61 20 73 69 6e 67 6c 65 old the a single
1a052 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74 20 66 .** row output f
1a053 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20 73 rom the sorter s
1a054 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77 20 63 o that the row c
1a055 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73 65 64 an be decomposed
1a056 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 into.** individ
1a057 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69 6e ual columns usin
1a058 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 g the OP_Column
1a059 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 opcode..**.** Wh
1a05a 65 6e 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 en OP_Insert is
1a05b 65 78 65 63 75 74 65 64 20 74 6f 20 69 6e 73 65 executed to inse
1a05c 72 74 20 61 20 72 6f 77 20 69 6e 20 74 6f 20 74 rt a row in to t
1a05d 68 65 20 70 73 65 75 64 6f 20 74 61 62 6c 65 2c he pseudo table,
1a05e 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d 74 .** the pseudo-t
1a05f 61 62 6c 65 20 63 75 72 73 6f 72 20 6d 61 79 20 able cursor may
1a060 6f 72 20 6d 61 79 20 6e 6f 74 20 6d 61 6b 65 20 or may not make
1a061 69 74 27 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66 it's own copy of
1a062 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c the.** original
1a063 20 72 6f 77 20 64 61 74 61 2e 20 49 66 20 50 32 row data. If P2
1a064 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 is 0, then the
1a065 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 77 69 6c pseudo-table wil
1a066 6c 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 6f 72 l copy the.** or
1a067 69 67 69 6e 61 6c 20 72 6f 77 20 64 61 74 61 2e iginal row data.
1a068 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70 6f Otherwise, a po
1a069 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 72 69 inter to the ori
1a06a 67 69 6e 61 6c 20 6d 65 6d 6f 72 79 20 63 65 6c ginal memory cel
1a06b 6c 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 2e 20 l.** is stored.
1a06c 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 In this case, th
1a06d 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 6d e vdbe program m
1a06e 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 ust ensure that
1a06f 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 the .** memory c
1a070 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ell containing t
1a071 68 65 20 72 6f 77 20 64 61 74 61 20 69 73 20 6e he row data is n
1a072 6f 74 20 6f 76 65 72 77 72 69 74 74 65 6e 20 75 ot overwritten u
1a073 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 70 73 65 75 ntil the.** pseu
1a074 64 6f 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 do table is clos
1a075 65 64 20 28 6f 72 20 61 20 6e 65 77 20 72 6f 77 ed (or a new row
1a076 20 69 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74 is inserted int
1a077 6f 20 69 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f o it)..*/.case O
1a078 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a P_OpenPseudo: {.
1a079 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 int i = pOp->p
1a07a 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 1;. VdbeCursor
1a07b 2a 70 43 78 3b 0a 20 20 61 73 73 65 72 74 28 20 *pCx;. assert(
1a07c 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 i>=0 );. pCx =
1a07d 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 allocateCursor(p
1a07e 2c 20 69 2c 20 26 70 4f 70 5b 2d 31 5d 2c 20 2d , i, &pOp[-1], -
1a07f 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 78 1, 0);. if( pCx
1a080 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ==0 ) goto no_me
1a081 6d 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f m;. pCx->nullRo
1a082 77 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 70 73 w = 1;. pCx->ps
1a083 65 75 64 6f 54 61 62 6c 65 20 3d 20 31 3b 0a 20 eudoTable = 1;.
1a084 20 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 pCx->ephemPseud
1a085 6f 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 32 oTable = pOp->p2
1a086 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 ;. pCx->isTable
1a087 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 69 73 49 = 1;. pCx->isI
1a088 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 72 65 61 ndex = 0;. brea
1a089 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1a08a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a 20 Close P1 * * *
1a08b 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 *.**.** Close a
1a08c 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c cursor previousl
1a08d 79 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e 20 y opened as P1.
1a08e 20 49 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a 2a If P1 is not.**
1a08f 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 2c currently open,
1a090 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
1a091 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f n is a no-op..*/
1a092 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a 20 .case OP_Close:
1a093 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d {. int i = pOp-
1a094 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 >p1;. assert( i
1a095 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 >=0 && i<p->nCur
1a096 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 sor );. sqlite3
1a097 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 VdbeFreeCursor(p
1a098 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 3b 0a , p->apCsr[i]);.
1a099 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20 p->apCsr[i] =
1a09a 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 0;. break;.}../
1a09b 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 47 65 * Opcode: MoveGe
1a09c 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1a09d 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 *.** If cursor P
1a09e 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 1 refers to an S
1a09f 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 QL table (B-Tree
1a0a0 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 that uses integ
1a0a1 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 er keys), .** us
1a0a2 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 e the integer va
1a0a3 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1a0a4 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 P3 as a key. If
1a0a5 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 cursor P1 refers
1a0a6 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 .** to an SQL i
1a0a7 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 ndex, then P3 is
1a0a8 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e the first in an
1a0a9 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 array of P4 reg
1a0aa 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 isters .** that
1a0ab 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 are used as an u
1a0ac 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 npacked index ke
1a0ad 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 y. .**.** Reposi
1a0ae 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 tion cursor P1 s
1a0af 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 o that it point
1a0b0 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 s to the smalles
1a0b1 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a t entry that .**
1a0b2 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
1a0b3 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
1a0b4 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 key value. If t
1a0b5 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f here are no reco
1a0b6 72 64 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 rds .** greater
1a0b7 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
1a0b8 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 the key and P2
1a0b9 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 is not zero, the
1a0ba 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1a0bb 0a 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 66 65 .** A special fe
1a0bc 61 74 75 72 65 20 6f 66 20 74 68 69 73 20 6f 70 ature of this op
1a0bd 63 6f 64 65 20 28 61 6e 64 20 64 69 66 66 65 72 code (and differ
1a0be 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 ent from the.**
1a0bf 72 65 6c 61 74 65 64 20 4f 50 5f 4d 6f 76 65 47 related OP_MoveG
1a0c0 74 2c 20 4f 50 5f 4d 6f 76 65 4c 74 2c 20 61 6e t, OP_MoveLt, an
1a0c1 64 20 4f 50 5f 4d 6f 76 65 4c 65 29 20 69 73 20 d OP_MoveLe) is
1a0c2 74 68 61 74 20 69 66 20 50 32 20 69 73 0a 2a 2a that if P2 is.**
1a0c3 20 7a 65 72 6f 20 61 6e 64 20 50 31 20 69 73 20 zero and P1 is
1a0c4 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 61 20 an SQL table (a
1a0c5 62 2d 74 72 65 65 20 77 69 74 68 20 69 6e 74 65 b-tree with inte
1a0c6 67 65 72 20 6b 65 79 73 29 20 74 68 65 6e 0a 2a ger keys) then.*
1a0c7 2a 20 74 68 65 20 73 65 65 6b 20 69 73 20 64 65 * the seek is de
1a0c8 66 65 72 72 65 64 20 75 6e 74 69 6c 20 69 74 20 ferred until it
1a0c9 69 73 20 61 63 74 75 61 6c 6c 79 20 6e 65 65 64 is actually need
1a0ca 65 64 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65 ed. It might be
1a0cb 0a 2a 2a 20 74 68 65 20 63 61 73 65 20 74 68 61 .** the case tha
1a0cc 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 t the cursor is
1a0cd 6e 65 76 65 72 20 61 63 63 65 73 73 65 64 2e 20 never accessed.
1a0ce 20 42 79 20 64 65 66 65 72 72 69 6e 67 20 74 68 By deferring th
1a0cf 65 0a 2a 2a 20 73 65 65 6b 2c 20 77 65 20 61 76 e.** seek, we av
1a0d0 6f 69 64 20 75 6e 6e 65 63 65 73 73 61 72 79 20 oid unnecessary
1a0d1 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 seeks..**.** See
1a0d2 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f also: Found, No
1a0d3 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 tFound, Distinct
1a0d4 2c 20 4d 6f 76 65 4c 74 2c 20 4d 6f 76 65 47 74 , MoveLt, MoveGt
1a0d5 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f , MoveLe.*/./* O
1a0d6 70 63 6f 64 65 3a 20 4d 6f 76 65 47 74 20 50 31 pcode: MoveGt P1
1a0d7 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1a0d8 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 * If cursor P1 r
1a0d9 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 efers to an SQL
1a0da 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 table (B-Tree th
1a0db 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 at uses integer
1a0dc 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 keys), .** use t
1a0dd 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 he integer value
1a0de 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 in register P3
1a0df 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 as a key. If cur
1a0e0 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a sor P1 refers .*
1a0e1 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 * to an SQL inde
1a0e2 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 x, then P3 is th
1a0e3 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 e first in an ar
1a0e4 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 ray of P4 regist
1a0e5 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 ers .** that are
1a0e6 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 used as an unpa
1a0e7 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 cked index key.
1a0e8 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f .**.** Repositio
1a0e9 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 n cursor P1 so t
1a0ea 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 hat it points t
1a0eb 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 o the smallest e
1a0ec 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 ntry that .** is
1a0ed 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
1a0ee 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 e key value. If
1a0ef 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 there are no rec
1a0f0 6f 72 64 73 20 67 72 65 61 74 65 72 20 74 68 61 ords greater tha
1a0f1 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e n .** the key an
1a0f2 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f d P2 is not zero
1a0f3 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 , then jump to P
1a0f4 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 2..**.** See als
1a0f5 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 o: Found, NotFou
1a0f6 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 4d 6f nd, Distinct, Mo
1a0f7 76 65 4c 74 2c 20 4d 6f 76 65 47 65 2c 20 4d 6f veLt, MoveGe, Mo
1a0f8 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 veLe.*/./* Opcod
1a0f9 65 3a 20 4d 6f 76 65 4c 74 20 50 31 20 50 32 20 e: MoveLt P1 P2
1a0fa 50 33 20 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 P3 P4 * .**.** I
1a0fb 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 f cursor P1 refe
1a0fc 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 rs to an SQL tab
1a0fd 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 le (B-Tree that
1a0fe 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 uses integer key
1a0ff 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 s), .** use the
1a100 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e integer value in
1a101 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 register P3 as
1a102 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 a key. If cursor
1a103 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 P1 refers .** t
1a104 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 o an SQL index,
1a105 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 then P3 is the f
1a106 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 irst in an array
1a107 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 of P4 registers
1a108 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 .** that are us
1a109 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 ed as an unpacke
1a10a 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a d index key. .**
1a10b 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 .** Reposition c
1a10c 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 ursor P1 so that
1a10d 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 it points to t
1a10e 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 he largest entry
1a10f 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 that .** is les
1a110 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 s than the key v
1a111 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 alue. If there a
1a112 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c 65 re no records le
1a113 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20 ss than .** the
1a114 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f key and P2 is no
1a115 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d t zero, then jum
1a116 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 p to P2..**.** S
1a117 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 ee also: Found,
1a118 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e NotFound, Distin
1a119 63 74 2c 20 4d 6f 76 65 47 74 2c 20 4d 6f 76 65 ct, MoveGt, Move
1a11a 47 65 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a Ge, MoveLe.*/./*
1a11b 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 4c 65 20 Opcode: MoveLe
1a11c 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a P1 P2 P3 P4 *.**
1a11d 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 .** If cursor P1
1a11e 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 refers to an SQ
1a11f 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 L table (B-Tree
1a120 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 that uses intege
1a121 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 r keys), .** use
1a122 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c the integer val
1a123 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1a124 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 3 as a key. If c
1a125 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 ursor P1 refers
1a126 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e .** to an SQL in
1a127 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 dex, then P3 is
1a128 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 the first in an
1a129 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 array of P4 regi
1a12a 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 sters .** that a
1a12b 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e re used as an un
1a12c 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 packed index key
1a12d 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 . .**.** Reposit
1a12e 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f ion cursor P1 so
1a12f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
1a130 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 to the largest e
1a131 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 ntry that .** is
1a132 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
1a133 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76 ual to the key v
1a134 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 alue. If there a
1a135 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a re no records .*
1a136 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 * less than or e
1a137 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 qual to the key
1a138 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 and P2 is not ze
1a139 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f ro, then jump to
1a13a 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 P2..**.** See a
1a13b 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 lso: Found, NotF
1a13c 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 ound, Distinct,
1a13d 4d 6f 76 65 47 74 2c 20 4d 6f 76 65 47 65 2c 20 MoveGt, MoveGe,
1a13e 4d 6f 76 65 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f MoveLt.*/.case O
1a13f 50 5f 4d 6f 76 65 4c 74 3a 20 20 20 20 20 20 20 P_MoveLt:
1a140 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1a141 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 4c 65 /.case OP_MoveLe
1a142 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d : /* jum
1a143 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f p, in3 */.case O
1a144 50 5f 4d 6f 76 65 47 65 3a 20 20 20 20 20 20 20 P_MoveGe:
1a145 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1a146 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 47 74 /.case OP_MoveGt
1a147 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d : { /* jum
1a148 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 p, in3 */. int
1a149 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 i = pOp->p1;. V
1a14a 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a dbeCursor *pC;..
1a14b 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1a14c 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1a14d 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 ;. pC = p->apCs
1a14e 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 r[i];. assert(
1a14f 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 pC!=0 );. if( p
1a150 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b C->pCursor!=0 ){
1a151 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 6f 63 . int res, oc
1a152 3b 0a 20 20 20 20 6f 63 20 3d 20 70 4f 70 2d 3e ;. oc = pOp->
1a153 6f 70 63 6f 64 65 3b 0a 20 20 20 20 70 43 2d 3e opcode;. pC->
1a154 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 nullRow = 0;.
1a155 20 69 66 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 if( pC->isTable
1a156 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4b ){. i64 iK
1a157 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 ey = sqlite3Vdbe
1a158 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b 0a IntValue(pIn3);.
1a159 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 if( pOp->p
1a15a 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2==0 ){.
1a15b 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 assert( pOp->opc
1a15c 6f 64 65 3d 3d 4f 50 5f 4d 6f 76 65 47 65 20 29 ode==OP_MoveGe )
1a15d 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6d 6f ;. pC->mo
1a15e 76 65 74 6f 54 61 72 67 65 74 20 3d 20 69 4b 65 vetoTarget = iKe
1a15f 79 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 72 y;. pC->r
1a160 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b owidIsValid = 0;
1a161 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 64 65 66 . pC->def
1a162 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 31 3b erredMoveto = 1;
1a163 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1a164 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
1a165 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
1a166 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
1a167 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75 ->pCursor, 0, (u
1a168 36 34 29 69 4b 65 79 2c 20 30 2c 20 26 72 65 73 64)iKey, 0, &res
1a169 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1a16a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1a16b 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 goto abort
1a16c 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1a16d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 2d }. pC-
1a16e 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 69 4b 65 >lastRowid = iKe
1a16f 79 3b 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 y;. pC->row
1a170 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 73 3d idIsValid = res=
1a171 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 =0;. }else{.
1a172 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 UnpackedRec
1a173 6f 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e 74 ord r;. int
1a174 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 nField = pOp->p
1a175 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 4.i;. asser
1a176 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1a177 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 P4_INT32 );.
1a178 20 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c 64 assert( nField
1a179 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 2e 70 4b >0 );. r.pK
1a17a 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 eyInfo = pC->pKe
1a17b 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 72 2e 6e yInfo;. r.n
1a17c 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b 0a Field = nField;.
1a17d 20 20 20 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 if( oc==OP
1a17e 5f 4d 6f 76 65 47 74 20 7c 7c 20 6f 63 3d 3d 4f _MoveGt || oc==O
1a17f 50 5f 4d 6f 76 65 4c 65 20 29 7b 0a 20 20 20 20 P_MoveLe ){.
1a180 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e r.flags = UN
1a181 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a PACKED_INCRKEY;.
1a182 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1a183 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 30 r.flags = 0
1a184 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1a185 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 r.aMem = &p->aMe
1a186 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 m[pOp->p3];.
1a187 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1a188 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
1a189 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 d(pC->pCursor, &
1a18a 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b 0a r, 0, 0, &res);.
1a18b 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1a18c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1a18d 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1a18e 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1a18f 20 20 7d 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f }. pC->ro
1a190 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1a191 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e 64 65 }. pC->de
1a192 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 ferredMoveto = 0
1a193 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 ;. pC->cacheS
1a194 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1a195 41 4c 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 ALE;.#ifdef SQLI
1a196 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 TE_TEST. sqli
1a197 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 te3_search_count
1a198 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 ++;.#endif. i
1a199 66 28 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 47 65 f( oc==OP_MoveGe
1a19a 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 47 || oc==OP_MoveG
1a19b 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 t ){. if( r
1a19c 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 es<0 ){.
1a19d 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1a19e 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73 6f eNext(pC->pCurso
1a19f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 r, &res);.
1a1a0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1a1a1 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 _OK ) goto abort
1a1a2 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1a1a3 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 pC->rowid
1a1a4 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 IsValid = 0;.
1a1a5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1a1a6 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 res = 0;.
1a1a7 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1a1a8 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d 3d assert( oc==
1a1a9 4f 50 5f 4d 6f 76 65 4c 74 20 7c 7c 20 6f 63 3d OP_MoveLt || oc=
1a1aa 3d 4f 50 5f 4d 6f 76 65 4c 65 20 29 3b 0a 20 20 =OP_MoveLe );.
1a1ab 20 20 20 20 69 66 28 20 72 65 73 3e 3d 30 20 29 if( res>=0 )
1a1ac 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
1a1ad 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 qlite3BtreePrevi
1a1ae 6f 75 73 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c ous(pC->pCursor,
1a1af 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 &res);.
1a1b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1a1b1 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 K ) goto abort_d
1a1b2 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1a1b3 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 pC->rowidIs
1a1b4 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 Valid = 0;.
1a1b5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1a1b6 2f 2a 20 72 65 73 20 6d 69 67 68 74 20 62 65 20 /* res might be
1a1b7 6e 65 67 61 74 69 76 65 20 62 65 63 61 75 73 65 negative because
1a1b8 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d the table is em
1a1b9 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 pty. Check to.
1a1ba 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20 69 66 ** see if
1a1bb 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 this is the cas
1a1bc 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
1a1bd 20 20 20 20 20 20 72 65 73 20 3d 20 73 71 6c 69 res = sqli
1a1be 74 65 33 42 74 72 65 65 45 6f 66 28 70 43 2d 3e te3BtreeEof(pC->
1a1bf 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 pCursor);.
1a1c0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 }. }. asse
1a1c1 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b rt( pOp->p2>0 );
1a1c2 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a . if( res ){.
1a1c3 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1a1c4 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 p2 - 1;. }.
1a1c5 7d 65 6c 73 65 20 69 66 28 20 21 70 43 2d 3e 70 }else if( !pC->p
1a1c6 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 seudoTable ){.
1a1c7 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e /* This happen
1a1c8 73 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e s when attemptin
1a1c9 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 73 71 g to open the sq
1a1ca 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 74 61 62 lite3_master tab
1a1cb 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 20 72 65 le. ** for re
1a1cc 61 64 20 61 63 63 65 73 73 20 72 65 74 75 72 6e ad access return
1a1cd 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20 s SQLITE_EMPTY.
1a1ce 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 77 In this case alw
1a1cf 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20 ays. ** take
1a1d0 74 68 65 20 6a 75 6d 70 20 28 73 69 6e 63 65 20 the jump (since
1a1d1 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 there are no rec
1a1d2 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c ords in the tabl
1a1d3 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 e).. */. p
1a1d4 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1a1d5 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1a1d6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75 6e ./* Opcode: Foun
1a1d7 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a d P1 P2 P3 * *.*
1a1d8 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 *.** Register P3
1a1d9 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f holds a blob co
1a1da 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b nstructed by Mak
1a1db 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73 20 eRecord. P1 is
1a1dc 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 an index..** If
1a1dd 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61 an entry that ma
1a1de 74 63 68 65 73 20 74 68 65 20 76 61 6c 75 65 20 tches the value
1a1df 69 6e 20 72 65 67 69 73 74 65 72 20 70 33 20 65 in register p3 e
1a1e0 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 65 6e xists in P1 then
1a1e1 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 .** jump to P2.
1a1e2 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 If the P3 value
1a1e3 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 does not match
1a1e4 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31 0a any entry in P1.
1a1e5 2a 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 ** then fall thr
1a1e6 75 2e 20 20 54 68 65 20 50 31 20 63 75 72 73 6f u. The P1 curso
1a1e7 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 r is left pointi
1a1e8 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 69 ng at the matchi
1a1e9 6e 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20 69 ng entry.** if i
1a1ea 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 t exists..**.**
1a1eb 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1a1ec 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c is used to impl
1a1ed 65 6d 65 6e 74 20 74 68 65 20 49 4e 20 6f 70 65 ement the IN ope
1a1ee 72 61 74 6f 72 20 77 68 65 72 65 20 74 68 65 0a rator where the.
1a1ef 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 ** left-hand sid
1a1f0 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74 e is a SELECT st
1a1f1 61 74 65 6d 65 6e 74 2e 20 20 50 31 20 6d 61 79 atement. P1 may
1a1f2 20 62 65 20 61 20 74 72 75 65 20 69 6e 64 65 78 be a true index
1a1f3 2c 20 6f 72 20 69 74 0a 2a 2a 20 6d 61 79 20 62 , or it.** may b
1a1f4 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69 6e e a temporary in
1a1f5 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 74 dex that holds t
1a1f6 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 he results of th
1a1f7 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 e SELECT.** stat
1a1f8 65 6d 65 6e 74 2e 20 20 20 54 68 69 73 20 69 6e ement. This in
1a1f9 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c 73 struction is als
1a1fa 6f 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d o used to implem
1a1fb 65 6e 74 20 74 68 65 0a 2a 2a 20 44 49 53 54 49 ent the.** DISTI
1a1fc 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 6e 20 53 NCT keyword in S
1a1fd 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 ELECT statements
1a1fe 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 ..**.** This ins
1a1ff 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 20 truction checks
1a200 69 66 20 69 6e 64 65 78 20 50 31 20 63 6f 6e 74 if index P1 cont
1a201 61 69 6e 73 20 61 20 72 65 63 6f 72 64 20 66 6f ains a record fo
1a202 72 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20 r which .** the
1a203 66 69 72 73 74 20 4e 20 73 65 72 69 61 6c 69 7a first N serializ
1a204 65 64 20 76 61 6c 75 65 73 20 65 78 61 63 74 6c ed values exactl
1a205 79 20 6d 61 74 63 68 20 74 68 65 20 4e 20 73 65 y match the N se
1a206 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73 0a rialized values.
1a207 2a 2a 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ** in the record
1a208 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2c in register P3,
1a209 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 where N is the
1a20a 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
1a20b 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 74 68 65 values in.** the
1a20c 20 50 33 20 72 65 63 6f 72 64 20 28 74 68 65 20 P3 record (the
1a20d 50 33 20 72 65 63 6f 72 64 20 69 73 20 61 20 70 P3 record is a p
1a20e 72 65 66 69 78 20 6f 66 20 74 68 65 20 50 31 20 refix of the P1
1a20f 72 65 63 6f 72 64 29 2e 20 0a 2a 2a 0a 2a 2a 20 record). .**.**
1a210 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 See also: NotFou
1a211 6e 64 2c 20 49 73 55 6e 69 71 75 65 2c 20 4e 6f nd, IsUnique, No
1a212 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20 4f 70 tExists.*/./* Op
1a213 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20 50 code: NotFound P
1a214 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1a215 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 68 6f * Register P3 ho
1a216 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 lds a blob const
1a217 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 ructed by MakeRe
1a218 63 6f 72 64 2e 20 20 50 31 20 69 73 0a 2a 2a 20 cord. P1 is.**
1a219 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20 6e 6f an index. If no
1a21a 20 65 6e 74 72 79 20 65 78 69 73 74 73 20 69 6e entry exists in
1a21b 20 50 31 20 74 68 61 74 20 6d 61 74 63 68 65 73 P1 that matches
1a21c 20 74 68 65 20 62 6c 6f 62 20 74 68 65 6e 20 6a the blob then j
1a21d 75 6d 70 0a 2a 2a 20 74 6f 20 50 32 2e 20 20 49 ump.** to P2. I
1a21e 66 20 61 6e 20 65 6e 74 72 79 20 64 6f 65 73 20 f an entry does
1a21f 65 78 69 73 74 69 6e 67 2c 20 66 61 6c 6c 20 74 existing, fall t
1a220 68 72 6f 75 67 68 2e 20 20 54 68 65 20 63 75 72 hrough. The cur
1a221 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a 2a 20 70 sor is left.** p
1a222 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 65 ointing to the e
1a223 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65 ntry that matche
1a224 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 s..**.** See als
1a225 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 o: Found, NotExi
1a226 73 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f sts, IsUnique.*/
1a227 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e .case OP_NotFoun
1a228 64 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 d: /* jump
1a229 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 , in3 */.case OP
1a22a 5f 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20 20 _Found: {
1a22b 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f /* jump, in3 */
1a22c 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e . int i = pOp->
1a22d 70 31 3b 0a 20 20 69 6e 74 20 61 6c 72 65 61 64 p1;. int alread
1a22e 79 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 56 yExists = 0;. V
1a22f 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1a230 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 assert( i>=0 &&
1a231 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b i<p->nCursor );
1a232 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 . assert( p->ap
1a233 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 Csr[i]!=0 );. i
1a234 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 f( (pC = p->apCs
1a235 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21 3d r[i])->pCursor!=
1a236 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 0 ){. int res
1a237 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 ;. UnpackedRe
1a238 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a 0a cord *pIdxKey;..
1a239 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e assert( pC->
1a23a 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 isTable==0 );.
1a23b 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e assert( pIn3->
1a23c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 flags & MEM_Blob
1a23d 20 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 );. pIdxKey
1a23e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 = sqlite3VdbeRec
1a23f 6f 72 64 55 6e 70 61 63 6b 28 70 43 2d 3e 70 4b ordUnpack(pC->pK
1a240 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e 2c eyInfo, pIn3->n,
1a241 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20 20 pIn3->z,.
1a242 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a244 61 54 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66 aTempRec, sizeof
1a245 28 61 54 65 6d 70 52 65 63 29 29 3b 0a 20 20 20 (aTempRec));.
1a246 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 20 if( pIdxKey==0
1a247 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f ){. goto no
1a248 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _mem;. }.
1a249 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1a24a 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20 =OP_Found ){.
1a24b 20 20 20 70 49 64 78 4b 65 79 2d 3e 66 6c 61 67 pIdxKey->flag
1a24c 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 s |= UNPACKED_PR
1a24d 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20 20 EFIX_MATCH;.
1a24e 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
1a24f 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
1a250 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f acked(pC->pCurso
1a251 72 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 r, pIdxKey, 0, 0
1a252 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 73 71 6c , &res);. sql
1a253 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e ite3VdbeDeleteUn
1a254 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 64 packedRecord(pId
1a255 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 72 xKey);. if( r
1a256 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1a257 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1a258 20 7d 0a 20 20 20 20 61 6c 72 65 61 64 79 45 78 }. alreadyEx
1a259 69 73 74 73 20 3d 20 28 72 65 73 3d 3d 30 29 3b ists = (res==0);
1a25a 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72 65 . pC->deferre
1a25b 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 dMoveto = 0;.
1a25c 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 pC->cacheStatus
1a25d 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
1a25e 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f }. if( pOp->o
1a25f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 pcode==OP_Found
1a260 29 7b 0a 20 20 20 20 69 66 28 20 61 6c 72 65 61 ){. if( alrea
1a261 64 79 45 78 69 73 74 73 20 29 20 70 63 20 3d 20 dyExists ) pc =
1a262 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d pOp->p2 - 1;. }
1a263 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 21 61 else{. if( !a
1a264 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20 70 lreadyExists ) p
1a265 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1a266 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1a267 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55 6e ./* Opcode: IsUn
1a268 69 71 75 65 20 50 31 20 50 32 20 50 33 20 50 34 ique P1 P2 P3 P4
1a269 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20 *.**.** The P3
1a26a 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e register contain
1a26b 73 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 63 s an integer rec
1a26c 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 43 61 6c ord number. Cal
1a26d 6c 20 74 68 69 73 0a 2a 2a 20 72 65 63 6f 72 64 l this.** record
1a26e 20 6e 75 6d 62 65 72 20 52 2e 20 20 54 68 65 20 number R. The
1a26f 50 34 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74 P4 register cont
1a270 61 69 6e 73 20 61 6e 20 69 6e 64 65 78 20 6b 65 ains an index ke
1a271 79 20 63 72 65 61 74 65 64 0a 2a 2a 20 75 73 69 y created.** usi
1a272 6e 67 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 ng MakeRecord.
1a273 43 61 6c 6c 20 69 74 20 4b 2e 0a 2a 2a 0a 2a 2a Call it K..**.**
1a274 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e P1 is an index.
1a275 20 20 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 64 So it has no d
1a276 61 74 61 20 61 6e 64 20 69 74 73 20 6b 65 79 20 ata and its key
1a277 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a consists of a.**
1a278 20 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74 65 record generate
1a279 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 6f d by OP_MakeReco
1a27a 72 64 20 77 68 65 72 65 20 74 68 65 20 6c 61 73 rd where the las
1a27b 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 0a t field is the .
1a27c 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 ** rowid of the
1a27d 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 69 entry that the i
1a27e 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e 0a ndex refers to..
1a27f 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 ** .** This inst
1a280 72 75 63 74 69 6f 6e 20 61 73 6b 73 20 69 66 20 ruction asks if
1a281 74 68 65 72 65 20 69 73 20 61 6e 20 65 6e 74 72 there is an entr
1a282 79 20 69 6e 20 50 31 20 77 68 65 72 65 20 74 68 y in P1 where th
1a283 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6d 61 74 63 e.** fields matc
1a284 68 65 73 20 4b 20 62 75 74 20 74 68 65 20 72 6f hes K but the ro
1a285 77 69 64 20 69 73 20 64 69 66 66 65 72 65 6e 74 wid is different
1a286 20 66 72 6f 6d 20 52 2e 0a 2a 2a 20 49 66 20 74 from R..** If t
1a287 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 here is no such
1a288 65 6e 74 72 79 2c 20 74 68 65 6e 20 74 68 65 72 entry, then ther
1a289 65 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 e is an immediat
1a28a 65 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e e.** jump to P2.
1a28b 20 20 49 66 20 61 6e 79 20 65 6e 74 72 79 20 64 If any entry d
1a28c 6f 65 73 20 65 78 69 73 74 20 77 68 65 72 65 20 oes exist where
1a28d 74 68 65 20 69 6e 64 65 78 20 73 74 72 69 6e 67 the index string
1a28e 0a 2a 2a 20 6d 61 74 63 68 65 73 20 4b 20 62 75 .** matches K bu
1a28f 74 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d t the record num
1a290 62 65 72 20 69 73 20 6e 6f 74 20 52 2c 20 74 68 ber is not R, th
1a291 65 6e 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a en the record.**
1a292 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 number for that
1a293 20 65 6e 74 72 79 20 69 73 20 77 72 69 74 74 65 entry is writte
1a294 6e 20 69 6e 74 6f 20 50 33 20 61 6e 64 20 63 6f n into P3 and co
1a295 6e 74 72 6f 6c 0a 2a 2a 20 66 61 6c 6c 73 20 74 ntrol.** falls t
1a296 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 hrough to the ne
1a297 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a xt instruction..
1a298 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1a299 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 NotFound, NotExi
1a29a 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 sts, Found.*/.ca
1a29b 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20 se OP_IsUnique:
1a29c 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1a29d 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69 , in3 */. int i
1a29e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 = pOp->p1;. Vd
1a29f 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 beCursor *pCx;.
1a2a0 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 BtCursor *pCrsr
1a2a1 3b 0a 20 20 4d 65 6d 20 2a 70 4b 3b 0a 20 20 69 ;. Mem *pK;. i
1a2a2 36 34 20 52 3b 0a 0a 20 20 2f 2a 20 50 6f 70 20 64 R;.. /* Pop
1a2a3 74 68 65 20 76 61 6c 75 65 20 52 20 6f 66 66 20 the value R off
1a2a4 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 the top of the s
1a2a5 74 61 63 6b 0a 20 20 2a 2f 0a 20 20 61 73 73 65 tack. */. asse
1a2a6 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d rt( pOp->p4type=
1a2a7 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 =P4_INT32 );. a
1a2a8 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 69 ssert( pOp->p4.i
1a2a9 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 3c >0 && pOp->p4.i<
1a2aa 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4b =p->nMem );. pK
1a2ab 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1a2ac 3e 70 34 2e 69 5d 3b 0a 20 20 73 71 6c 69 74 65 >p4.i];. sqlite
1a2ad 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 3VdbeMemIntegeri
1a2ae 66 79 28 70 49 6e 33 29 3b 0a 20 20 52 20 3d 20 fy(pIn3);. R =
1a2af 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 61 73 73 pIn3->u.i;. ass
1a2b0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 ert( i>=0 && i<p
1a2b1 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 ->nCursor );. p
1a2b2 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d Cx = p->apCsr[i]
1a2b3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 78 21 ;. assert( pCx!
1a2b4 3d 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 =0 );. pCrsr =
1a2b5 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 pCx->pCursor;.
1a2b6 69 66 28 20 70 43 72 73 72 21 3d 30 20 29 7b 0a if( pCrsr!=0 ){.
1a2b7 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1a2b8 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 20 i64 v;
1a2b9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1a2ba 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 he record number
1a2bb 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 4b 20 that matches K
1a2bc 2a 2f 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 */. UnpackedR
1a2bd 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 20 ecord *pIdxKey;
1a2be 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 76 65 /* Unpacked ve
1a2bf 72 73 69 6f 6e 20 6f 66 20 50 34 20 2a 2f 0a 0a rsion of P4 */..
1a2c0 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 /* Make sure
1a2c1 20 4b 20 69 73 20 61 20 73 74 72 69 6e 67 20 61 K is a string a
1a2c2 6e 64 20 6d 61 6b 65 20 7a 4b 65 79 20 70 6f 69 nd make zKey poi
1a2c3 6e 74 20 74 6f 20 4b 0a 20 20 20 20 2a 2f 0a 20 nt to K. */.
1a2c4 20 20 20 61 73 73 65 72 74 28 20 70 4b 2d 3e 66 assert( pK->f
1a2c5 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 lags & MEM_Blob
1a2c6 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d );. pIdxKey =
1a2c7 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f sqlite3VdbeReco
1a2c8 72 64 55 6e 70 61 63 6b 28 70 43 78 2d 3e 70 4b rdUnpack(pCx->pK
1a2c9 65 79 49 6e 66 6f 2c 20 70 4b 2d 3e 6e 2c 20 70 eyInfo, pK->n, p
1a2ca 4b 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 K->z,.
1a2cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a2cc 20 20 20 20 20 20 20 20 20 20 20 20 61 54 65 6d aTem
1a2cd 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 61 54 65 pRec, sizeof(aTe
1a2ce 6d 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28 mpRec));. if(
1a2cf 20 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 pIdxKey==0 ){.
1a2d0 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d goto no_mem
1a2d1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64 78 ;. }. pIdx
1a2d2 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55 4e Key->flags |= UN
1a2d3 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f PACKED_IGNORE_RO
1a2d4 57 49 44 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 61 WID;.. /* Sea
1a2d5 72 63 68 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 rch for an entry
1a2d6 20 69 6e 20 50 31 20 77 68 65 72 65 20 61 6c 6c in P1 where all
1a2d7 20 62 75 74 20 74 68 65 20 6c 61 73 74 20 72 6f but the last ro
1a2d8 77 69 64 20 6d 61 74 63 68 20 4b 0a 20 20 20 20 wid match K.
1a2d9 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e ** If there is n
1a2da 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 6a 75 o such entry, ju
1a2db 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 mp immediately t
1a2dc 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 o P2.. */.
1a2dd 20 61 73 73 65 72 74 28 20 70 43 78 2d 3e 64 65 assert( pCx->de
1a2de 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 ferredMoveto==0
1a2df 29 3b 0a 20 20 20 20 70 43 78 2d 3e 63 61 63 68 );. pCx->cach
1a2e0 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1a2e1 53 54 41 4c 45 3b 0a 20 20 20 20 72 63 20 3d 20 STALE;. rc =
1a2e2 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1a2e3 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 72 73 72 toUnpacked(pCrsr
1a2e4 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 2c , pIdxKey, 0, 0,
1a2e5 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 &res);. if(
1a2e6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1a2e7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1a2e8 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 beDeleteUnpacked
1a2e9 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b Record(pIdxKey);
1a2ea 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 . goto abor
1a2eb 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1a2ec 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 65 }. if( re
1a2ed 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 s<0 ){. rc
1a2ee 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 = sqlite3BtreeNe
1a2ef 78 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b xt(pCrsr, &res);
1a2f0 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29 . if( res )
1a2f1 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 {. pc = p
1a2f2 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1a2f3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 sqlite3VdbeD
1a2f4 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 eleteUnpackedRec
1a2f5 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 ord(pIdxKey);.
1a2f6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1a2f7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 }. }. r
1a2f8 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 c = sqlite3VdbeI
1a2f9 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 70 43 78 dxKeyCompare(pCx
1a2fa 2c 20 70 49 64 78 4b 65 79 2c 20 26 72 65 73 29 , pIdxKey, &res)
1a2fb 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ; . sqlite3Vd
1a2fc 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 beDeleteUnpacked
1a2fd 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b Record(pIdxKey);
1a2fe 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1a2ff 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 ITE_OK ) goto ab
1a300 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1a301 3b 0a 20 20 20 20 69 66 28 20 72 65 73 3e 30 20 ;. if( res>0
1a302 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f ){. pc = pO
1a303 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 p->p2 - 1;.
1a304 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 break;. }..
1a305 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f /* At this po
1a306 69 6e 74 2c 20 70 43 72 73 72 20 69 73 20 70 6f int, pCrsr is po
1a307 69 6e 74 69 6e 67 20 74 6f 20 61 6e 20 65 6e 74 inting to an ent
1a308 72 79 20 69 6e 20 50 31 20 77 68 65 72 65 20 61 ry in P1 where a
1a309 6c 6c 20 62 75 74 0a 20 20 20 20 2a 2a 20 74 68 ll but. ** th
1a30a 65 20 66 69 6e 61 6c 20 65 6e 74 72 79 20 28 74 e final entry (t
1a30b 68 65 20 72 6f 77 69 64 29 20 6d 61 74 63 68 65 he rowid) matche
1a30c 73 20 4b 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 s K. Check to s
1a30d 65 65 20 69 66 20 74 68 65 0a 20 20 20 20 2a 2a ee if the. **
1a30e 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 63 6f 6c final rowid col
1a30f 75 6d 6e 20 69 73 20 64 69 66 66 65 72 65 6e 74 umn is different
1a310 20 66 72 6f 6d 20 52 2e 20 20 49 66 20 69 74 20 from R. If it
1a311 65 71 75 61 6c 73 20 52 20 74 68 65 6e 20 6a 75 equals R then ju
1a312 6d 70 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 mp. ** immedi
1a313 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 20 20 20 ately to P2..
1a314 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
1a315 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 ite3VdbeIdxRowid
1a316 28 70 43 72 73 72 2c 20 26 76 29 3b 0a 20 20 20 (pCrsr, &v);.
1a317 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1a318 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f OK ){. goto
1a319 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1a31a 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ror;. }. i
1a31b 66 28 20 76 3d 3d 52 20 29 7b 0a 20 20 20 20 20 f( v==R ){.
1a31c 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1a31d 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 1;. break;.
1a31e 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 }.. /* Th
1a31f 65 20 66 69 6e 61 6c 20 76 61 72 69 6e 74 20 6f e final varint o
1a320 66 20 74 68 65 20 6b 65 79 20 69 73 20 64 69 66 f the key is dif
1a321 66 65 72 65 6e 74 20 66 72 6f 6d 20 52 2e 20 20 ferent from R.
1a322 53 74 6f 72 65 20 69 74 20 62 61 63 6b 0a 20 20 Store it back.
1a323 20 20 2a 2a 20 69 6e 74 6f 20 72 65 67 69 73 74 ** into regist
1a324 65 72 20 52 33 2e 20 20 28 54 68 65 20 72 65 63 er R3. (The rec
1a325 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e ord number of an
1a326 20 65 6e 74 72 79 20 74 68 61 74 20 76 69 6f 6c entry that viol
1a327 61 74 65 73 0a 20 20 20 20 2a 2a 20 61 20 55 4e ates. ** a UN
1a328 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e IQUE constraint.
1a329 29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e ). */. pIn
1a32a 33 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20 3->u.i = v;.
1a32b 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c assert( pIn3->fl
1a32c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 ags&MEM_Int );.
1a32d 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1a32e 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 78 69 * Opcode: NotExi
1a32f 73 74 73 20 50 31 20 50 32 20 50 33 20 2a 20 2a sts P1 P2 P3 * *
1a330 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 63 .**.** Use the c
1a331 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 ontent of regist
1a332 65 72 20 50 33 20 61 73 20 61 20 69 6e 74 65 67 er P3 as a integ
1a333 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20 72 65 er key. If a re
1a334 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20 74 68 cord .** with th
1a335 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 at key does not
1a336 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65 20 6f exist in table o
1a337 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d 70 20 f P1, then jump
1a338 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20 74 68 to P2. .** If th
1a339 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20 65 78 e record does ex
1a33a 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 ist, then fall t
1a33b 68 72 75 2e 20 20 54 68 65 20 63 75 72 73 6f 72 hru. The cursor
1a33c 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70 6f 69 is left .** poi
1a33d 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 65 63 nting to the rec
1a33e 6f 72 64 20 69 66 20 69 74 20 65 78 69 73 74 73 ord if it exists
1a33f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 ..**.** The diff
1a340 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 erence between t
1a341 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e his operation an
1a342 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74 68 d NotFound is th
1a343 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61 at this.** opera
1a344 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 tion assumes the
1a345 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 65 67 key is an integ
1a346 65 72 20 61 6e 64 20 74 68 61 74 20 50 31 20 69 er and that P1 i
1a347 73 20 61 20 74 61 62 6c 65 20 77 68 65 72 65 61 s a table wherea
1a348 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61 73 s.** NotFound as
1a349 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61 20 62 sumes key is a b
1a34a 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 lob constructed
1a34b 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64 20 from MakeRecord
1a34c 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 and.** P1 is an
1a34d 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 index..**.** See
1a34e 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f also: Found, No
1a34f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 tFound, IsUnique
1a350 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 45 .*/.case OP_NotE
1a351 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20 20 20 xists: {
1a352 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a /* jump, in3 */.
1a353 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 int i = pOp->p
1a354 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 1;. VdbeCursor
1a355 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 *pC;. BtCursor
1a356 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 *pCrsr;. assert
1a357 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e ( i>=0 && i<p->n
1a358 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 Cursor );. asse
1a359 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 rt( p->apCsr[i]!
1a35a 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 =0 );. if( (pCr
1a35b 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70 sr = (pC = p->ap
1a35c 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 Csr[i])->pCursor
1a35d 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 )!=0 ){. int
1a35e 72 65 73 3b 0a 20 20 20 20 75 36 34 20 69 4b 65 res;. u64 iKe
1a35f 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 y;. assert( p
1a360 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In3->flags & MEM
1a361 5f 49 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65 _Int );. asse
1a362 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 2d rt( p->apCsr[i]-
1a363 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 >isTable );.
1a364 69 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 iKey = intToKey(
1a365 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 pIn3->u.i);.
1a366 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1a367 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1a368 70 43 72 73 72 2c 20 30 2c 20 69 4b 65 79 2c 20 pCrsr, 0, iKey,
1a369 30 2c 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d 0,&res);. pC-
1a36a 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 49 6e >lastRowid = pIn
1a36b 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 70 43 2d 3e 3->u.i;. pC->
1a36c 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 72 rowidIsValid = r
1a36d 65 73 3d 3d 30 3b 0a 20 20 20 20 70 43 2d 3e 6e es==0;. pC->n
1a36e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 ullRow = 0;.
1a36f 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1a370 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1a371 20 20 20 2f 2a 20 72 65 73 20 6d 69 67 68 74 20 /* res might
1a372 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 be uninitialized
1a373 20 69 66 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if rc!=SQLITE_O
1a374 4b 2e 20 20 42 75 74 20 69 66 20 72 63 21 3d 53 K. But if rc!=S
1a375 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 2a 2a 20 QLITE_OK. **
1a376 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 61 62 processing is ab
1a377 6f 75 74 20 74 6f 20 61 62 6f 72 74 20 73 6f 20 out to abort so
1a378 77 65 20 72 65 61 6c 6c 79 20 64 6f 20 6e 6f 74 we really do not
1a379 20 63 61 72 65 20 77 68 65 74 68 65 72 20 6f 72 care whether or
1a37a 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 not. ** the
1a37b 66 6f 6c 6c 6f 77 69 6e 67 20 6a 75 6d 70 20 69 following jump i
1a37c 73 20 74 61 6b 65 6e 2e 20 20 28 49 6e 20 6f 74 s taken. (In ot
1a37d 68 65 72 20 77 6f 72 64 73 2c 20 64 6f 20 6e 6f her words, do no
1a37e 74 20 73 74 72 65 73 73 20 6f 76 65 72 0a 20 20 t stress over.
1a37f 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 74 ** the error t
1a380 68 61 74 20 76 61 6c 67 72 69 6e 64 20 73 6f 6d hat valgrind som
1a381 65 74 69 6d 65 73 20 73 68 6f 77 73 20 6f 6e 20 etimes shows on
1a382 74 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65 the next stateme
1a383 6e 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 72 nt when. ** r
1a384 75 6e 6e 69 6e 67 20 69 6f 65 72 72 2e 74 65 73 unning ioerr.tes
1a385 74 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 66 61 t and similar fa
1a386 69 6c 75 72 65 2d 72 65 63 6f 76 65 72 79 20 74 ilure-recovery t
1a387 65 73 74 20 73 63 72 69 70 74 73 2e 29 20 2a 2f est scripts.) */
1a388 0a 20 20 20 20 69 66 28 20 72 65 73 21 3d 30 20 . if( res!=0
1a389 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f ){. pc = pO
1a38a 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 p->p2 - 1;.
1a38b 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 6f 77 assert( pC->row
1a38c 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a idIsValid==0 );.
1a38d 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1a38e 28 20 21 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 ( !pC->pseudoTab
1a38f 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 le ){. /* Thi
1a390 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 s happens when a
1a391 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 70 65 n attempt to ope
1a392 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 20 n a read cursor
1a393 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 on the . ** s
1a394 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
1a395 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 le returns SQLIT
1a396 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f 0a E_EMPTY.. */.
1a397 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e assert( pC->
1a398 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 isTable );. p
1a399 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1a39a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1a39b 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 >rowidIsValid==0
1a39c 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b );. }. break;
1a39d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .}../* Opcode: S
1a39e 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a 20 equence P1 P2 *
1a39f 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74 * *.**.** Find t
1a3a0 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c he next availabl
1a3a1 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 e sequence numbe
1a3a2 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e r for cursor P1.
1a3a3 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 65 .** Write the se
1a3a4 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e quence number in
1a3a5 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1a3a6 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 20 ** The sequence
1a3a7 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 75 number on the cu
1a3a8 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 6e rsor is incremen
1a3a9 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a 2a ted after this.*
1a3aa 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 * instruction.
1a3ab 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 75 .*/.case OP_Sequ
1a3ac 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20 ence: {
1a3ad 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1a3ae 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 ease */. int i
1a3af 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 = pOp->p1;. ass
1a3b0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 ert( i>=0 && i<p
1a3b1 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
1a3b2 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b ssert( p->apCsr[
1a3b3 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d i]!=0 );. pOut-
1a3b4 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b >u.i = p->apCsr[
1a3b5 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a i]->seqCount++;.
1a3b6 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1a3b7 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1a3b8 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a . break;.}.../*
1a3b9 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 Opcode: NewRowi
1a3ba 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a d P1 P2 P3 * *.*
1a3bb 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69 *.** Get a new i
1a3bc 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 nteger record nu
1a3bd 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77 mber (a.k.a "row
1a3be 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65 id") used as the
1a3bf 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e key to a table.
1a3c0 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e .** The record n
1a3c1 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65 umber is not pre
1a3c2 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20 viously used as
1a3c3 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74 a key in the dat
1a3c4 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74 abase.** table t
1a3c5 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f hat cursor P1 po
1a3c6 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 ints to. The ne
1a3c7 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 w record number
1a3c8 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72 is written.** wr
1a3c9 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 itten to registe
1a3ca 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 r P2..**.** If P
1a3cb 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61 3>0 then P3 is a
1a3cc 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 68 register that h
1a3cd 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 olds the largest
1a3ce 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 67 previously.** g
1a3cf 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 enerated record
1a3d0 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65 77 20 number. No new
1a3d1 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 record numbers a
1a3d2 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 re allowed to be
1a3d3 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 less.** than th
1a3d4 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65 6e 20 is value. When
1a3d5 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 68 this value reach
1a3d6 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 es its maximum,
1a3d7 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a a SQLITE_FULL.**
1a3d8 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 error is genera
1a3d9 74 65 64 2e 20 20 54 68 65 20 50 33 20 72 65 67 ted. The P3 reg
1a3da 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65 64 ister is updated
1a3db 20 77 69 74 68 20 74 68 65 20 67 65 6e 65 72 61 with the genera
1a3dc 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20 6e 75 ted.** record nu
1a3dd 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33 20 6d mber. This P3 m
1a3de 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 echanism is used
1a3df 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 to help impleme
1a3e0 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e nt the.** AUTOIN
1a3e1 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e CREMENT feature.
1a3e2 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 .*/.case OP_NewR
1a3e3 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 owid: {
1a3e4 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1a3e5 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20 ease */. int i
1a3e6 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 34 = pOp->p1;. i64
1a3e7 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75 v = 0;. VdbeCu
1a3e8 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 rsor *pC;. asse
1a3e9 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1a3ea 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 >nCursor );. as
1a3eb 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 sert( p->apCsr[i
1a3ec 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 ]!=0 );. if( (p
1a3ed 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 C = p->apCsr[i])
1a3ee 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b 0a ->pCursor==0 ){.
1a3ef 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f 20 /* The zero
1a3f0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 initialization a
1a3f1 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 74 bove is all that
1a3f2 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 is needed */.
1a3f3 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 }else{. /* Th
1a3f4 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 20 e next rowid or
1a3f5 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 64 record number (d
1a3f6 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 66 ifferent terms f
1a3f7 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 or the same.
1a3f8 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74 ** thing) is obt
1a3f9 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73 ained in a two-s
1a3fa 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 tep algorithm..
1a3fb 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72 **. ** Fir
1a3fc 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f st we attempt to
1a3fd 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 find the larges
1a3fe 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 t existing rowid
1a3ff 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 20 and add one.
1a400 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 75 ** to that. Bu
1a401 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 74 t if the largest
1a402 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20 existing rowid
1a403 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 6d is already the m
1a404 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 6f aximum. ** po
1a405 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c 20 sitive integer,
1a406 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c 20 we have to fall
1a407 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 through to the s
1a408 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 6f econd. ** pro
1a409 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f 72 babilistic algor
1a40a 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ithm. **.
1a40b 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6c ** The second al
1a40c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 65 gorithm is to se
1a40d 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 20 lect a rowid at
1a40e 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 69 random and see i
1a40f 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 65 f. ** it alre
1a410 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 68 ady exists in th
1a411 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20 e table. If it
1a412 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 does not exist,
1a413 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 73 we have. ** s
1a414 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 68 ucceeded. If th
1a415 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 64 e random rowid d
1a416 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 65 oes exist, we se
1a417 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a 20 lect a new one.
1a418 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 67 ** and try ag
1a419 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 30 20 ain, up to 1000
1a41a 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 times.. **.
1a41b 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 ** For a table
1a41c 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 6e 20 with less than
1a41d 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 69 65 2 billion entrie
1a41e 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69 s, the probabili
1a41f 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e 6f 74 ty. ** of not
1a420 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 73 65 finding a unuse
1a421 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f 75 74 d rowid is about
1a422 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 69 73 1.0e-300. This
1a423 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 6e 6f is a . ** no
1a424 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69 n-zero probabili
1a425 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 73 74 ty, but it is st
1a426 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c 79 20 ill vanishingly
1a427 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 6c 64 small and should
1a428 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 63 61 . ** never ca
1a429 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e 20 20 use a problem.
1a42a 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 6d 75 You are much, mu
1a42b 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74 ch more likely t
1a42c 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a 20 o have a. **
1a42d 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 72 65 hardware failure
1a42e 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 20 61 than for this a
1a42f 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 69 6c lgorithm to fail
1a430 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1a431 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 6e 20 The analysis in
1a432 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72 the previous par
1a433 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 20 74 agraph assumes t
1a434 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 20 67 hat you have a g
1a435 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 ood. ** sourc
1a436 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 e of random numb
1a437 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 72 61 ers. Is a libra
1a438 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 ry function like
1a439 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 20 2a lrand48(). *
1a43a 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f 20 20 * good enough?
1a43b 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e 6f 74 Maybe. Maybe not
1a43c 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f 20 6b . It's hard to k
1a43d 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 65 72 now whether ther
1a43e 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 62 e. ** might b
1a43f 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 69 73 e subtle bugs is
1a440 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61 some implementa
1a441 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 34 38 tions of lrand48
1a442 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 63 () that. ** c
1a443 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c ould cause probl
1a444 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 75 6e ems. To avoid un
1a445 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c 69 74 certainty, SQLit
1a446 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e 20 0a e uses its own .
1a447 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 6e 75 ** random nu
1a448 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62 mber generator b
1a449 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 34 20 ased on the RC4
1a44a 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a algorithm.. *
1a44b 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 6f 6d *. ** To prom
1a44c 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f 66 20 ote locality of
1a44d 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 72 65 reference for re
1a44e 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 74 73 petitive inserts
1a44f 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 , the. ** fir
1a450 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 73 20 st few attempts
1a451 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20 72 61 at choosing a ra
1a452 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b 20 ndom rowid pick
1a453 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c 69 values just a li
1a454 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67 ttle. ** larg
1a455 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 76 er than the prev
1a456 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68 69 ious rowid. Thi
1a457 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77 6e s has been shown
1a458 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79 0a experimentally.
1a459 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c 65 ** to double
1a45a 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74 68 the speed of th
1a45b 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f 6e e COPY operation
1a45c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 .. */. int
1a45d 20 72 65 73 2c 20 72 78 3d 53 51 4c 49 54 45 5f res, rx=SQLITE_
1a45e 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69 36 34 OK, cnt;. i64
1a45f 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20 30 3b x;. cnt = 0;
1a460 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 65 . if( (sqlite
1a461 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 3BtreeFlags(pC->
1a462 70 43 75 72 73 6f 72 29 26 28 42 54 52 45 45 5f pCursor)&(BTREE_
1a463 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a 45 52 INTKEY|BTREE_ZER
1a464 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20 20 20 ODATA)) !=.
1a465 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b 45 BTREE_INTKE
1a466 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 Y ){. rc =
1a467 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1a468 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 KPT;. goto
1a469 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1a46a 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 or;. }. as
1a46b 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 42 74 sert( (sqlite3Bt
1a46c 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 43 75 reeFlags(pC->pCu
1a46d 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f 49 4e rsor) & BTREE_IN
1a46e 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20 20 20 TKEY)!=0 );.
1a46f 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 assert( (sqlite3
1a470 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 BtreeFlags(pC->p
1a471 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f Cursor) & BTREE_
1a472 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29 3b 0a ZERODATA)==0 );.
1a473 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33 .#ifdef SQLITE_3
1a474 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64 2BIT_ROWID.# d
1a475 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 efine MAX_ROWID
1a476 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65 0x7fffffff.#else
1a477 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d . /* Some com
1a478 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 pilers complain
1a479 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20 about constants
1a47a 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66 of the form 0x7f
1a47b 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a ffffffffffffff..
1a47c 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f ** Others co
1a47d 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37 mplain about 0x7
1a47e 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 ffffffffffffffff
1a47f 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 LL. The followi
1a480 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20 ng macro seems.
1a481 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65 ** to provide
1a482 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68 the constant wh
1a483 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63 ile making all c
1a484 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a ompilers happy..
1a485 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e */.# defin
1a486 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36 e MAX_ROWID (i6
1a487 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66 4)( (((u64)0x7ff
1a488 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75 fffff)<<32) | (u
1a489 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a 64)0xffffffff ).
1a48a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 #endif.. if(
1a48b 21 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f !pC->useRandomRo
1a48c 77 69 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 wid ){. if(
1a48d 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 pC->nextRowidVa
1a48e 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 76 lid ){. v
1a48f 20 3d 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 = pC->nextRowid
1a490 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1a491 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1a492 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d te3BtreeLast(pC-
1a493 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b >pCursor, &res);
1a494 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1a495 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1a496 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f goto abo
1a497 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1a498 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1a499 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 if( res ){.
1a49a 20 20 20 20 20 20 20 20 76 20 3d 20 31 3b 0a 20 v = 1;.
1a49b 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1a49c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
1a49d 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d 3e treeKeySize(pC->
1a49e 70 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20 pCursor, &v);.
1a49f 20 20 20 20 20 20 20 20 76 20 3d 20 6b 65 79 54 v = keyT
1a4a0 6f 49 6e 74 28 76 29 3b 0a 20 20 20 20 20 20 20 oInt(v);.
1a4a1 20 20 20 69 66 28 20 76 3d 3d 4d 41 58 5f 52 4f if( v==MAX_RO
1a4a2 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 WID ){.
1a4a3 20 20 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d pC->useRandom
1a4a4 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 Rowid = 1;.
1a4a5 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1a4a6 20 20 20 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 v++;.
1a4a7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1a4a8 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e }. }..#ifn
1a4a9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1a4aa 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 AUTOINCREMENT.
1a4ab 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 if( pOp->p3
1a4ac 29 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 2a ){. Mem *
1a4ad 70 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 61 73 pMem;. as
1a4ae 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 sert( pOp->p3>0
1a4af 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e && pOp->p3<=p->n
1a4b0 4d 65 6d 20 29 3b 20 2f 2a 20 50 33 20 69 73 20 Mem ); /* P3 is
1a4b1 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 a valid memory c
1a4b2 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 ell */. p
1a4b3 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Mem = &p->aMem[p
1a4b4 4f 70 2d 3e 70 33 5d 3b 0a 09 52 45 47 49 53 54 Op->p3];..REGIST
1a4b5 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 ER_TRACE(pOp->p3
1a4b6 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 , pMem);.
1a4b7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1a4b8 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b ntegerify(pMem);
1a4b9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1a4ba 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 (pMem->flags &
1a4bb 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 20 MEM_Int)!=0 );
1a4bc 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 73 /* mem(P3) holds
1a4bd 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 an integer */.
1a4be 20 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d if( pMem-
1a4bf 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 >u.i==MAX_ROWID
1a4c0 7c 7c 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d || pC->useRandom
1a4c1 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 Rowid ){.
1a4c2 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 rc = SQLITE_F
1a4c3 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 67 ULL;. g
1a4c4 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1a4c5 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 _error;.
1a4c6 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3c }. if( v<
1a4c7 70 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 pMem->u.i+1 ){.
1a4c8 20 20 20 20 20 20 20 20 20 76 20 3d 20 70 4d 65 v = pMe
1a4c9 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20 m->u.i + 1;.
1a4ca 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d }. pM
1a4cb 65 6d 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 em->u.i = v;.
1a4cc 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
1a4cd 20 20 20 69 66 28 20 76 3c 4d 41 58 5f 52 4f 57 if( v<MAX_ROW
1a4ce 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 ID ){. pC
1a4cf 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64 ->nextRowidValid
1a4d0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 = 1;. pC
1a4d1 2d 3e 6e 65 78 74 52 6f 77 69 64 20 3d 20 76 2b ->nextRowid = v+
1a4d2 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 1;. }else{.
1a4d3 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65 78 74 pC->next
1a4d4 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a RowidValid = 0;.
1a4d5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1a4d6 20 20 69 66 28 20 70 43 2d 3e 75 73 65 52 61 6e if( pC->useRan
1a4d7 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 domRowid ){.
1a4d8 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1a4d9 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 51 4c 49 3==0 ); /* SQLI
1a4da 54 45 5f 46 55 4c 4c 20 6d 75 73 74 20 68 61 76 TE_FULL must hav
1a4db 65 20 6f 63 63 75 72 72 65 64 20 70 72 69 6f 72 e occurred prior
1a4dc 20 74 6f 20 74 68 69 73 20 2a 2f 0a 20 20 20 20 to this */.
1a4dd 20 20 76 20 3d 20 64 62 2d 3e 70 72 69 6f 72 4e v = db->priorN
1a4de 65 77 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 63 ewRowid;. c
1a4df 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f nt = 0;. do
1a4e0 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6e {. if( cn
1a4e1 74 3d 3d 30 20 26 26 20 28 76 26 30 78 66 66 66 t==0 && (v&0xfff
1a4e2 66 66 66 29 3d 3d 76 20 29 7b 0a 20 20 20 20 20 fff)==v ){.
1a4e3 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20 v++;.
1a4e4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1a4e5 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f sqlite3_rando
1a4e6 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 76 29 2c mness(sizeof(v),
1a4e7 20 26 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 &v);.
1a4e8 69 66 28 20 63 6e 74 3c 35 20 29 20 76 20 26 3d if( cnt<5 ) v &=
1a4e9 20 30 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20 0xffffff;.
1a4ea 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1a4eb 20 76 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 v==0 ) continue
1a4ec 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 69 6e ;. x = in
1a4ed 74 54 6f 4b 65 79 28 76 29 3b 0a 20 20 20 20 20 tToKey(v);.
1a4ee 20 20 20 72 78 20 3d 20 73 71 6c 69 74 65 33 42 rx = sqlite3B
1a4ef 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
1a4f0 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 ed(pC->pCursor,
1a4f1 30 2c 20 28 75 36 34 29 78 2c 20 30 2c 20 26 72 0, (u64)x, 0, &r
1a4f2 65 73 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 es);. cnt
1a4f3 2b 2b 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 ++;. }while
1a4f4 28 20 63 6e 74 3c 31 30 30 20 26 26 20 72 78 3d ( cnt<100 && rx=
1a4f5 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 =SQLITE_OK && re
1a4f6 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62 s==0 );. db
1a4f7 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 20 ->priorNewRowid
1a4f8 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 72 = v;. if( r
1a4f9 78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 x==SQLITE_OK &&
1a4fa 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 res==0 ){.
1a4fb 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 rc = SQLITE_FU
1a4fc 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f LL;. goto
1a4fd 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1a4fe 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ror;. }.
1a4ff 20 7d 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 }. pC->rowid
1a500 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 IsValid = 0;.
1a501 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 pC->deferredMov
1a502 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d eto = 0;. pC-
1a503 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 >cacheStatus = C
1a504 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a ACHE_STALE;. }.
1a505 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1a506 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1a507 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 . pOut->u.i = v
1a508 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1a509 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 20 Opcode: Insert
1a50a 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1a50b 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e *.** Write an en
1a50c 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 61 62 try into the tab
1a50d 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 31 2e le of cursor P1.
1a50e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73 A new entry is
1a50f 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 20 69 .** created if i
1a510 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61 64 t doesn't alread
1a511 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 20 64 y exist or the d
1a512 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 73 74 ata for an exist
1a513 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20 ing.** entry is
1a514 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 54 68 overwritten. Th
1a515 65 20 64 61 74 61 20 69 73 20 74 68 65 20 76 61 e data is the va
1a516 6c 75 65 20 73 74 6f 72 65 64 20 72 65 67 69 73 lue stored regis
1a517 74 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 ter.** number P2
1a518 2e 20 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f . The key is sto
1a519 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1a51a 50 33 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74 P3. The key must
1a51b 0a 2a 2a 20 62 65 20 61 6e 20 69 6e 74 65 67 65 .** be an intege
1a51c 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
1a51d 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 OPFLAG_NCHANGE f
1a51e 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 lag of P5 is set
1a51f 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 , then the row c
1a520 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a hange count is.*
1a521 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f * incremented (o
1a522 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 20 20 therwise not).
1a523 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41 If the OPFLAG_LA
1a524 53 54 52 4f 57 49 44 20 66 6c 61 67 20 6f 66 20 STROWID flag of
1a525 50 35 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 P5 is set,.** th
1a526 65 6e 20 72 6f 77 69 64 20 69 73 20 73 74 6f 72 en rowid is stor
1a527 65 64 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e ed for subsequen
1a528 74 20 72 65 74 75 72 6e 20 62 79 20 74 68 65 0a t return by the.
1a529 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f ** sqlite3_last_
1a52a 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 66 insert_rowid() f
1a52b 75 6e 63 74 69 6f 6e 20 28 6f 74 68 65 72 77 69 unction (otherwi
1a52c 73 65 20 69 74 20 69 73 20 75 6e 6d 6f 64 69 66 se it is unmodif
1a52d 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 ied)..**.** Para
1a52e 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69 meter P4 may poi
1a52f 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63 nt to a string c
1a530 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 ontaining the ta
1a531 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 ble-name, or.**
1a532 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20 may be NULL. If
1a533 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 it is not NULL,
1a534 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d then the update-
1a535 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65 hook .** (sqlite
1a536 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 3.xUpdateCallbac
1a537 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f k) is invoked fo
1a538 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73 llowing a succes
1a539 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a sful insert..**.
1a53a 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f ** (WARNING/TODO
1a53b 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65 : If P1 is a pse
1a53c 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50 udo-cursor and P
1a53d 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 2 is dynamically
1a53e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 .** allocated, t
1a53f 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 hen ownership of
1a540 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72 P2 is transferr
1a541 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f ed to the pseudo
1a542 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 -cursor.** and r
1a543 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d egister P2 becom
1a544 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49 es ephemeral. I
1a545 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
1a546 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20 changed, the.**
1a547 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 value of registe
1a548 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63 r P2 will then c
1a549 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72 hange. Make sur
1a54a 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a e this does not.
1a54b 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f ** cause any pro
1a54c 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 blems.).**.** Th
1a54d 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f is instruction o
1a54e 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62 nly works on tab
1a54f 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 les. The equiva
1a550 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e lent instruction
1a551 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20 .** for indices
1a552 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e is OP_IdxInsert.
1a553 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65 .*/.case OP_Inse
1a554 72 74 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 44 61 rt: {. Mem *pDa
1a555 74 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ta = &p->aMem[pO
1a556 70 2d 3e 70 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70 p->p2];. Mem *p
1a557 4b 65 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Key = &p->aMem[p
1a558 4f 70 2d 3e 70 33 5d 3b 0a 0a 20 20 69 36 34 20 Op->p3];.. i64
1a559 69 4b 65 79 3b 20 20 20 2f 2a 20 54 68 65 20 69 iKey; /* The i
1a55a 6e 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20 nteger ROWID or
1a55b 6b 65 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f key for the reco
1a55c 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 rd to be inserte
1a55d 64 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 d */. int i = p
1a55e 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 Op->p1;. VdbeCu
1a55f 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65 rsor *pC;. asse
1a560 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1a561 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 >nCursor );. pC
1a562 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a = p->apCsr[i];.
1a563 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 assert( pC!=0
1a564 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d );. assert( pC-
1a565 3e 70 43 75 72 73 6f 72 21 3d 30 20 7c 7c 20 70 >pCursor!=0 || p
1a566 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 C->pseudoTable )
1a567 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 ;. assert( pKey
1a568 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1a569 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t );. assert( p
1a56a 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1a56b 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1a56c 4f 70 2d 3e 70 32 2c 20 70 44 61 74 61 29 3b 0a Op->p2, pData);.
1a56d 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1a56e 28 70 4f 70 2d 3e 70 33 2c 20 70 4b 65 79 29 3b (pOp->p3, pKey);
1a56f 0a 0a 20 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f .. iKey = intTo
1a570 4b 65 79 28 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a Key(pKey->u.i);.
1a571 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 if( pOp->p5 &
1a572 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 OPFLAG_NCHANGE )
1a573 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 p->nChange++;.
1a574 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f if( pOp->p5 & O
1a575 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 PFLAG_LASTROWID
1a576 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 ) db->lastRowid
1a577 3d 20 70 4b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69 = pKey->u.i;. i
1a578 66 28 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 f( pC->nextRowid
1a579 56 61 6c 69 64 20 26 26 20 70 4b 65 79 2d 3e 75 Valid && pKey->u
1a57a 2e 69 3e 3d 70 43 2d 3e 6e 65 78 74 52 6f 77 69 .i>=pC->nextRowi
1a57b 64 20 29 7b 0a 20 20 20 20 70 43 2d 3e 6e 65 78 d ){. pC->nex
1a57c 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b tRowidValid = 0;
1a57d 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 61 74 61 . }. if( pData
1a57e 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1a57f 6c 6c 20 29 7b 0a 20 20 20 20 70 44 61 74 61 2d ll ){. pData-
1a580 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 70 44 61 74 >z = 0;. pDat
1a581 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 a->n = 0;. }els
1a582 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
1a583 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 28 4d Data->flags & (M
1a584 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 EM_Blob|MEM_Str)
1a585 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 );. }. if( pC
1a586 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 7b ->pseudoTable ){
1a587 0a 20 20 20 20 69 66 28 20 21 70 43 2d 3e 65 70 . if( !pC->ep
1a588 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20 29 hemPseudoTable )
1a589 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
1a58a 62 46 72 65 65 28 64 62 2c 20 70 43 2d 3e 70 44 bFree(db, pC->pD
1a58b 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ata);. }.
1a58c 70 43 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b pC->iKey = iKey;
1a58d 0a 20 20 20 20 70 43 2d 3e 6e 44 61 74 61 20 3d . pC->nData =
1a58e 20 70 44 61 74 61 2d 3e 6e 3b 0a 20 20 20 20 69 pData->n;. i
1a58f 66 28 20 70 44 61 74 61 2d 3e 7a 3d 3d 70 44 61 f( pData->z==pDa
1a590 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20 7c 7c 20 70 ta->zMalloc || p
1a591 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 C->ephemPseudoTa
1a592 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 2d ble ){. pC-
1a593 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 2d 3e >pData = pData->
1a594 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 z;. if( !pC
1a595 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 ->ephemPseudoTab
1a596 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 le ){. pD
1a597 61 74 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d ata->flags &= ~M
1a598 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 20 20 20 20 EM_Dyn;.
1a599 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 pData->flags |=
1a59a 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 MEM_Ephem;.
1a59b 20 20 20 70 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f pData->zMallo
1a59c 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 0;. }.
1a59d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1a59e 70 43 2d 3e 70 44 61 74 61 20 3d 20 73 71 6c 69 pC->pData = sqli
1a59f 74 65 33 4d 61 6c 6c 6f 63 28 20 70 43 2d 3e 6e te3Malloc( pC->n
1a5a0 44 61 74 61 2b 32 20 29 3b 0a 20 20 20 20 20 20 Data+2 );.
1a5a1 69 66 28 20 21 70 43 2d 3e 70 44 61 74 61 20 29 if( !pC->pData )
1a5a2 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1a5a3 20 20 20 20 6d 65 6d 63 70 79 28 70 43 2d 3e 70 memcpy(pC->p
1a5a4 44 61 74 61 2c 20 70 44 61 74 61 2d 3e 7a 2c 20 Data, pData->z,
1a5a5 70 43 2d 3e 6e 44 61 74 61 29 3b 0a 20 20 20 20 pC->nData);.
1a5a6 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e pC->pData[pC->
1a5a7 6e 44 61 74 61 5d 20 3d 20 30 3b 0a 20 20 20 20 nData] = 0;.
1a5a8 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e pC->pData[pC->
1a5a9 6e 44 61 74 61 2b 31 5d 20 3d 20 30 3b 0a 20 20 nData+1] = 0;.
1a5aa 20 20 7d 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c }. pC->null
1a5ab 52 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 Row = 0;. }else
1a5ac 7b 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b {. int nZero;
1a5ad 0a 20 20 20 20 69 66 28 20 70 44 61 74 61 2d 3e . if( pData->
1a5ae 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f flags & MEM_Zero
1a5af 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20 ){. nZero
1a5b0 3d 20 70 44 61 74 61 2d 3e 75 2e 69 3b 0a 20 20 = pData->u.i;.
1a5b1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e }else{. n
1a5b2 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a Zero = 0;. }.
1a5b3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1a5b4 42 74 72 65 65 49 6e 73 65 72 74 28 70 43 2d 3e BtreeInsert(pC->
1a5b5 70 43 75 72 73 6f 72 2c 20 30 2c 20 69 4b 65 79 pCursor, 0, iKey
1a5b6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1a5b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44 pD
1a5b8 61 74 61 2d 3e 7a 2c 20 70 44 61 74 61 2d 3e 6e ata->z, pData->n
1a5b9 2c 20 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20 , nZero,.
1a5ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5bb 20 20 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f pOp->p5 & O
1a5bc 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 PFLAG_APPEND);.
1a5bd 20 7d 0a 20 20 0a 20 20 70 43 2d 3e 72 6f 77 69 }. . pC->rowi
1a5be 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1a5bf 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1a5c0 74 6f 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 63 61 to = 0;. pC->ca
1a5c1 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1a5c2 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 E_STALE;.. /* I
1a5c3 6e 76 6f 6b 65 20 74 68 65 20 75 70 64 61 74 65 nvoke the update
1a5c4 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75 69 72 65 -hook if require
1a5c5 64 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d d. */. if( rc==
1a5c6 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d SQLITE_OK && db-
1a5c7 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b >xUpdateCallback
1a5c8 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b && pOp->p4.z ){
1a5c9 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
1a5ca 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 70 *zDb = db->aDb[p
1a5cb 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 C->iDb].zName;.
1a5cc 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1a5cd 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b Tbl = pOp->p4.z;
1a5ce 0a 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 28 28 . int op = ((
1a5cf 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 pOp->p5 & OPFLAG
1a5d0 5f 49 53 55 50 44 41 54 45 29 20 3f 20 53 51 4c _ISUPDATE) ? SQL
1a5d1 49 54 45 5f 55 50 44 41 54 45 20 3a 20 53 51 4c ITE_UPDATE : SQL
1a5d2 49 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20 20 20 ITE_INSERT);.
1a5d3 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 assert( pC->isT
1a5d4 61 62 6c 65 20 29 3b 0a 20 20 20 20 64 62 2d 3e able );. db->
1a5d5 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 xUpdateCallback(
1a5d6 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 db->pUpdateArg,
1a5d7 6f 70 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 op, zDb, zTbl, i
1a5d8 4b 65 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 Key);. assert
1a5d9 28 20 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a ( pC->iDb>=0 );.
1a5da 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1a5db 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74 /* Opcode: Delet
1a5dc 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a e P1 P2 * P4 *.*
1a5dd 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 *.** Delete the
1a5de 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 68 20 record at which
1a5df 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 the P1 cursor is
1a5e0 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1a5e1 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ing..**.** The c
1a5e2 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65 ursor will be le
1a5e3 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65 ft pointing at e
1a5e4 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 20 6f ither the next o
1a5e5 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a r the previous.*
1a5e6 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20 * record in the
1a5e7 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73 20 table. If it is
1a5e8 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
1a5e9 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64 the next record
1a5ea 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 , then.** the ne
1a5eb 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74 xt Next instruct
1a5ec 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f ion will be a no
1a5ed 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20 69 -op. Hence it i
1a5ee 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a s OK to delete.*
1a5ef 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 * a record from
1a5f0 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c within an Next l
1a5f1 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 oop..**.** If th
1a5f2 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 e OPFLAG_NCHANGE
1a5f3 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73 flag of P2 is s
1a5f4 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 et, then the row
1a5f5 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 change count is
1a5f6 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 .** incremented
1a5f7 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e (otherwise not).
1a5f8 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e .**.** P1 must n
1a5f9 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61 62 ot be pseudo-tab
1a5fa 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20 62 le. It has to b
1a5fb 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 77 e a real table w
1a5fc 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 ith.** multiple
1a5fd 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 rows..**.** If P
1a5fe 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 4 is not NULL, t
1a5ff 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e 61 hen it is the na
1a600 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1a601 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f that P1 is.** po
1a602 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 inting to. The
1a603 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c update hook will
1a604 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20 be invoked, if
1a605 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66 it exists..** If
1a606 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 P4 is not NULL
1a607 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 72 73 then the P1 curs
1a608 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 or must have bee
1a609 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 n positioned.**
1a60a 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e using OP_NotFoun
1a60b 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b d prior to invok
1a60c 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e ing this opcode.
1a60d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65 .*/.case OP_Dele
1a60e 74 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 te: {. int i =
1a60f 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 34 20 69 pOp->p1;. i64 i
1a610 4b 65 79 3b 0a 20 20 56 64 62 65 43 75 72 73 6f Key;. VdbeCurso
1a611 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65 72 74 r *pC;.. assert
1a612 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e ( i>=0 && i<p->n
1a613 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d Cursor );. pC =
1a614 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 p->apCsr[i];.
1a615 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b assert( pC!=0 );
1a616 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 . assert( pC->p
1a617 43 75 72 73 6f 72 21 3d 30 20 29 3b 20 20 2f 2a Cursor!=0 ); /*
1a618 20 4f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 Only valid for
1a619 72 65 61 6c 20 74 61 62 6c 65 73 2c 20 6e 6f 20 real tables, no
1a61a 70 73 65 75 64 6f 74 61 62 6c 65 73 20 2a 2f 0a pseudotables */.
1a61b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 75 70 64 . /* If the upd
1a61c 61 74 65 2d 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 ate-hook will be
1a61d 20 69 6e 76 6f 6b 65 64 2c 20 73 65 74 20 69 4b invoked, set iK
1a61e 65 79 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 ey to the rowid
1a61f 6f 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 of the. ** row
1a620 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 being deleted..
1a621 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 */. if( db->xU
1a622 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 pdateCallback &&
1a623 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 pOp->p4.z ){.
1a624 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 assert( pC->is
1a625 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 Table );. ass
1a626 65 72 74 28 20 70 43 2d 3e 72 6f 77 69 64 49 73 ert( pC->rowidIs
1a627 56 61 6c 69 64 20 29 3b 20 20 2f 2a 20 6c 61 73 Valid ); /* las
1a628 74 52 6f 77 69 64 20 73 65 74 20 62 79 20 70 72 tRowid set by pr
1a629 65 76 69 6f 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 evious OP_NotFou
1a62a 6e 64 20 2a 2f 0a 20 20 20 20 69 4b 65 79 20 3d nd */. iKey =
1a62b 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a pC->lastRowid;.
1a62c 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 }.. rc = sqli
1a62d 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 te3VdbeCursorMov
1a62e 65 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72 eto(pC);. if( r
1a62f 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 c ) goto abort_d
1a630 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 72 ue_to_error;. r
1a631 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1a632 44 65 6c 65 74 65 28 70 43 2d 3e 70 43 75 72 73 Delete(pC->pCurs
1a633 6f 72 29 3b 0a 20 20 70 43 2d 3e 6e 65 78 74 52 or);. pC->nextR
1a634 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a 20 owidValid = 0;.
1a635 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 pC->cacheStatus
1a636 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
1a637 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 . /* Invoke the
1a638 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 update-hook if
1a639 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 required. */. i
1a63a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1a63b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 && db->xUpdateC
1a63c 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e allback && pOp->
1a63d 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 p4.z ){. cons
1a63e 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 t char *zDb = db
1a63f 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a ->aDb[pC->iDb].z
1a640 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 Name;. const
1a641 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 char *zTbl = pOp
1a642 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e ->p4.z;. db->
1a643 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 xUpdateCallback(
1a644 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 db->pUpdateArg,
1a645 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a SQLITE_DELETE, z
1a646 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b Db, zTbl, iKey);
1a647 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d . assert( pC-
1a648 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 >iDb>=0 );. }.
1a649 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f if( pOp->p2 & O
1a64a 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 PFLAG_NCHANGE )
1a64b 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 p->nChange++;.
1a64c 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1a64d 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 20 ode: ResetCount
1a64e 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 P1 * *.**.** Thi
1a64f 73 20 6f 70 63 6f 64 65 20 72 65 73 65 74 73 20 s opcode resets
1a650 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c the VMs internal
1a651 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1a652 74 6f 20 30 2e 20 49 66 20 50 31 20 69 73 20 74 to 0. If P1 is t
1a653 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 rue,.** then the
1a654 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 68 value of the ch
1a655 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 ange counter is
1a656 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 64 61 copied to the da
1a657 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a tabase handle.**
1a658 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1a659 28 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62 (returned by sub
1a65a 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f sequent calls to
1a65b 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
1a65c 28 29 29 0a 2a 2a 20 62 65 66 6f 72 65 20 69 74 ()).** before it
1a65d 20 69 73 20 72 65 73 65 74 2e 20 54 68 69 73 20 is reset. This
1a65e 69 73 20 75 73 65 64 20 62 79 20 74 72 69 67 67 is used by trigg
1a65f 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a er programs..*/.
1a660 63 61 73 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 case OP_ResetCou
1a661 6e 74 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d nt: {. if( pOp-
1a662 3e 70 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 >p1 ){. sqlit
1a663 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 e3VdbeSetChanges
1a664 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 (db, p->nChange)
1a665 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 43 68 61 6e ;. }. p->nChan
1a666 67 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b ge = 0;. break;
1a667 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .}../* Opcode: R
1a668 6f 77 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a owData P1 P2 * *
1a669 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 *.**.** Write i
1a66a 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 nto register P2
1a66b 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 the complete row
1a66c 20 64 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 data for cursor
1a66d 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 P1..** There is
1a66e 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 no interpretati
1a66f 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 on of the data.
1a670 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20 .** It is just
1a671 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 copied onto the
1a672 50 32 20 72 65 67 69 73 74 65 72 20 65 78 61 63 P2 register exac
1a673 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 tly as .** it is
1a674 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 found in the da
1a675 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1a676 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72 ** If the P1 cur
1a677 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e sor must be poin
1a678 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 ting to a valid
1a679 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 row (not a NULL
1a67a 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 row).** of a rea
1a67b 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 l table, not a p
1a67c 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a seudo-table..*/.
1a67d 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 /* Opcode: RowKe
1a67e 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a y P1 P2 * * *.**
1a67f 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 .** Write into r
1a680 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63 egister P2 the c
1a681 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 omplete row key
1a682 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a for cursor P1..*
1a683 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e * There is no in
1a684 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 terpretation of
1a685 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 the data. .** T
1a686 68 65 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64 he key is copied
1a687 20 6f 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67 onto the P3 reg
1a688 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 ister exactly as
1a689 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 .** it is found
1a68a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1a68b 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
1a68c 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 the P1 cursor mu
1a68d 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 st be pointing t
1a68e 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e o a valid row (n
1a68f 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a ot a NULL row).*
1a690 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c * of a real tabl
1a691 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d e, not a pseudo-
1a692 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f table..*/.case O
1a693 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f P_RowKey:.case O
1a694 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 20 20 69 P_RowData: {. i
1a695 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a nt i = pOp->p1;.
1a696 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1a697 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 ;. BtCursor *pC
1a698 72 73 72 3b 0a 20 20 75 33 32 20 6e 3b 0a 0a 20 rsr;. u32 n;..
1a699 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pOut = &p->aMem
1a69a 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a [pOp->p2];.. /*
1a69b 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b 65 Note that RowKe
1a69c 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61 72 y and RowData ar
1a69d 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c 79 e really exactly
1a69e 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72 75 the same instru
1a69f 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 ction */. asser
1a6a0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e t( i>=0 && i<p->
1a6a1 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 nCursor );. pC
1a6a2 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 = p->apCsr[i];.
1a6a3 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 assert( pC->isT
1a6a4 61 62 6c 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 able || pOp->opc
1a6a5 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 ode==OP_RowKey )
1a6a6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e ;. assert( pC->
1a6a7 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e isIndex || pOp->
1a6a8 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 opcode==OP_RowDa
1a6a9 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ta );. assert(
1a6aa 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 pC!=0 );. asser
1a6ab 74 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d t( pC->nullRow==
1a6ac 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1a6ad 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 3d 3d C->pseudoTable==
1a6ae 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1a6af 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b C->pCursor!=0 );
1a6b0 0a 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 . pCrsr = pC->p
1a6b1 43 75 72 73 6f 72 3b 0a 20 20 72 63 20 3d 20 73 Cursor;. rc = s
1a6b2 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 qlite3VdbeCursor
1a6b3 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 69 66 Moveto(pC);. if
1a6b4 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 ( rc ) goto abor
1a6b5 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1a6b6 20 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 if( pC->isInde
1a6b7 78 20 29 7b 0a 20 20 20 20 69 36 34 20 6e 36 34 x ){. i64 n64
1a6b8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 ;. assert( !p
1a6b9 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1a6ba 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 sqlite3BtreeKe
1a6bb 79 53 69 7a 65 28 70 43 72 73 72 2c 20 26 6e 36 ySize(pCrsr, &n6
1a6bc 34 29 3b 0a 20 20 20 20 69 66 28 20 6e 36 34 3e 4);. if( n64>
1a6bd 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1a6be 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
1a6bf 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f ){. goto to
1a6c0 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 o_big;. }.
1a6c1 20 6e 20 3d 20 6e 36 34 3b 0a 20 20 7d 65 6c 73 n = n64;. }els
1a6c2 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 e{. sqlite3Bt
1a6c3 72 65 65 44 61 74 61 53 69 7a 65 28 70 43 72 73 reeDataSize(pCrs
1a6c4 72 2c 20 26 6e 29 3b 0a 20 20 20 20 69 66 28 20 r, &n);. if(
1a6c5 28 69 6e 74 29 6e 3e 64 62 2d 3e 61 4c 69 6d 69 (int)n>db->aLimi
1a6c6 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1a6c7 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 ENGTH] ){.
1a6c8 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 goto too_big;.
1a6c9 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 }. }. if( sq
1a6ca 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
1a6cb 28 70 4f 75 74 2c 20 6e 2c 20 30 29 20 29 7b 0a (pOut, n, 0) ){.
1a6cc 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1a6cd 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d . }. pOut->n =
1a6ce 20 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 n;. MemSetType
1a6cf 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42 Flag(pOut, MEM_B
1a6d0 6c 6f 62 29 3b 0a 20 20 69 66 28 20 70 43 2d 3e lob);. if( pC->
1a6d1 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 72 isIndex ){. r
1a6d2 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1a6d3 4b 65 79 28 70 43 72 73 72 2c 20 30 2c 20 6e 2c Key(pCrsr, 0, n,
1a6d4 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 65 6c pOut->z);. }el
1a6d5 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c se{. rc = sql
1a6d6 69 74 65 33 42 74 72 65 65 44 61 74 61 28 70 43 ite3BtreeData(pC
1a6d7 72 73 72 2c 20 30 2c 20 6e 2c 20 70 4f 75 74 2d rsr, 0, n, pOut-
1a6d8 3e 7a 29 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d >z);. }. pOut-
1a6d9 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 >enc = SQLITE_UT
1a6da 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 F8; /* In case
1a6db 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76 65 72 the blob is ever
1a6dc 20 63 61 73 74 20 74 6f 20 74 65 78 74 20 2a 2f cast to text */
1a6dd 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c . UPDATE_MAX_BL
1a6de 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 OBSIZE(pOut);.
1a6df 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1a6e0 6f 64 65 3a 20 52 6f 77 69 64 20 50 31 20 50 32 ode: Rowid P1 P2
1a6e1 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f * * *.**.** Sto
1a6e2 72 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 re in register P
1a6e3 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 2 an integer whi
1a6e4 63 68 20 69 73 20 74 68 65 20 6b 65 79 20 6f 66 ch is the key of
1a6e5 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 the table entry
1a6e6 20 74 68 61 74 0a 2a 2a 20 50 31 20 69 73 20 63 that.** P1 is c
1a6e7 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 urrently point t
1a6e8 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f o..*/.case OP_Ro
1a6e9 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 wid: {
1a6ea 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1a6eb 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 rerelease */. i
1a6ec 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a nt i = pOp->p1;.
1a6ed 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1a6ee 3b 0a 20 20 69 36 34 20 76 3b 0a 0a 20 20 61 73 ;. i64 v;.. as
1a6ef 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
1a6f0 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1a6f1 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d pC = p->apCsr[i]
1a6f2 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d ;. assert( pC!=
1a6f3 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 0 );. rc = sqli
1a6f4 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 te3VdbeCursorMov
1a6f5 65 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72 eto(pC);. if( r
1a6f6 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 c ) goto abort_d
1a6f7 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 69 ue_to_error;. i
1a6f8 66 28 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 f( pC->rowidIsVa
1a6f9 6c 69 64 20 29 7b 0a 20 20 20 20 76 20 3d 20 70 lid ){. v = p
1a6fa 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 C->lastRowid;.
1a6fb 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 70 73 }else if( pC->ps
1a6fc 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 eudoTable ){.
1a6fd 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28 70 43 v = keyToInt(pC
1a6fe 2d 3e 69 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 ->iKey);. }else
1a6ff 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 if( pC->nullRow
1a700 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 61 76 65 ){. /* Leave
1a701 20 74 68 65 20 72 6f 77 69 64 20 73 65 74 20 74 the rowid set t
1a702 6f 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 o a NULL */.
1a703 62 72 65 61 6b 3b 0a 20 20 7d 65 6c 73 65 7b 0a break;. }else{.
1a704 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e assert( pC->
1a705 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 pCursor!=0 );.
1a706 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 sqlite3BtreeKe
1a707 79 53 69 7a 65 28 70 43 2d 3e 70 43 75 72 73 6f ySize(pC->pCurso
1a708 72 2c 20 26 76 29 3b 0a 20 20 20 20 76 20 3d 20 r, &v);. v =
1a709 6b 65 79 54 6f 49 6e 74 28 76 29 3b 0a 20 20 7d keyToInt(v);. }
1a70a 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 . pOut->u.i = v
1a70b 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c ;. MemSetTypeFl
1a70c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 ag(pOut, MEM_Int
1a70d 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1a70e 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 52 6f * Opcode: NullRo
1a70f 77 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a w P1 * * * *.**.
1a710 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 ** Move the curs
1a711 6f 72 20 50 31 20 74 6f 20 61 20 6e 75 6c 6c 20 or P1 to a null
1a712 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43 6f 6c row. Any OP_Col
1a713 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a umn operations.*
1a714 2a 20 74 68 61 74 20 6f 63 63 75 72 20 77 68 69 * that occur whi
1a715 6c 65 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 le the cursor is
1a716 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72 6f 77 on the null row
1a717 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 will always.**
1a718 77 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a 2a 2f write a NULL..*/
1a719 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 .case OP_NullRow
1a71a 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f : {. int i = pO
1a71b 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 p->p1;. VdbeCur
1a71c 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65 sor *pC;.. asse
1a71d 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d rt( i>=0 && i<p-
1a71e 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 >nCursor );. pC
1a71f 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a = p->apCsr[i];.
1a720 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 assert( pC!=0
1a721 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 );. pC->nullRow
1a722 20 3d 20 31 3b 0a 20 20 70 43 2d 3e 72 6f 77 69 = 1;. pC->rowi
1a723 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1a724 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 20 if( pC->pCursor
1a725 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
1a726 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 reeClearCursor(p
1a727 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d C->pCursor);. }
1a728 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1a729 4f 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20 Opcode: Last P1
1a72a 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 P2 * * *.**.** T
1a72b 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 he next use of t
1a72c 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 he Rowid or Colu
1a72d 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 mn or Next instr
1a72e 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a uction for P1 .*
1a72f 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 * will refer to
1a730 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 the last entry i
1a731 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 n the database t
1a732 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a able or index..*
1a733 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f * If the table o
1a734 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 r index is empty
1a735 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 and P2>0, then
1a736 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 jump immediately
1a737 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 to P2..** If P2
1a738 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 is 0 or if the
1a739 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 table or index i
1a73a 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c s not empty, fal
1a73b 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 l through.** to
1a73c 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e the following in
1a73d 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 struction..*/.ca
1a73e 73 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20 se OP_Last: {
1a73f 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1a740 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 int i = pOp->p
1a741 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 1;. VdbeCursor
1a742 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 *pC;. BtCursor
1a743 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 *pCrsr;. int re
1a744 73 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e s;.. assert( i>
1a745 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 =0 && i<p->nCurs
1a746 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e or );. pC = p->
1a747 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 apCsr[i];. asse
1a748 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 70 rt( pC!=0 );. p
1a749 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 Crsr = pC->pCurs
1a74a 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 or;. assert( pC
1a74b 72 73 72 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d rsr!=0 );. rc =
1a74c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 sqlite3BtreeLas
1a74d 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a t(pCrsr, &res);.
1a74e 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 pC->nullRow =
1a74f 72 65 73 3b 0a 20 20 70 43 2d 3e 64 65 66 65 72 res;. pC->defer
1a750 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 redMoveto = 0;.
1a751 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 pC->cacheStatus
1a752 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
1a753 20 20 69 66 28 20 72 65 73 20 26 26 20 70 4f 70 if( res && pOp
1a754 2d 3e 70 32 3e 30 20 29 7b 0a 20 20 20 20 70 63 ->p2>0 ){. pc
1a755 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1a756 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1a757 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72 74 ./* Opcode: Sort
1a758 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1a759 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 64 ** This opcode d
1a75a 6f 65 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 oes exactly the
1a75b 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 4f 50 same thing as OP
1a75c 5f 52 65 77 69 6e 64 20 65 78 63 65 70 74 20 74 _Rewind except t
1a75d 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63 72 65 6d hat.** it increm
1a75e 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63 75 6d 65 ents an undocume
1a75f 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76 61 72 69 nted global vari
1a760 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 able used for te
1a761 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72 sting..**.** Sor
1a762 74 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 ting is accompli
1a763 73 68 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 shed by writing
1a764 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 73 records into a s
1a765 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 0a 2a 2a orting index,.**
1a766 20 74 68 65 6e 20 72 65 77 69 6e 64 69 6e 67 20 then rewinding
1a767 74 68 61 74 20 69 6e 64 65 78 20 61 6e 64 20 70 that index and p
1a768 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b 20 66 laying it back f
1a769 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f rom beginning to
1a76a 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75 73 65 .** end. We use
1a76b 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 the OP_Sort opc
1a76c 6f 64 65 20 69 6e 73 74 65 61 64 20 6f 66 20 4f ode instead of O
1a76d 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f 20 74 P_Rewind to do t
1a76e 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69 6e 67 20 he.** rewinding
1a76f 73 6f 20 74 68 61 74 20 74 68 65 20 67 6c 6f 62 so that the glob
1a770 61 6c 20 76 61 72 69 61 62 6c 65 20 77 69 6c 6c al variable will
1a771 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 be incremented
1a772 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73 73 69 6f and.** regressio
1a773 6e 20 74 65 73 74 73 20 63 61 6e 20 64 65 74 65 n tests can dete
1a774 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 rmine whether or
1a775 20 6e 6f 74 20 74 68 65 20 6f 70 74 69 6d 69 7a not the optimiz
1a776 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72 65 63 74 er is.** correct
1a777 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67 20 6f 75 ly optimizing ou
1a778 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 65 t sorts..*/.case
1a779 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 20 20 20 OP_Sort: {
1a77a 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 /* jump */.#i
1a77b 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1a77c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f . sqlite3_sort_
1a77d 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 count++;. sqlit
1a77e 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d e3_search_count-
1a77f 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 61 -;.#endif. p->a
1a780 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f 53 Counter[SQLITE_S
1a781 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 2d 31 TMTSTATUS_SORT-1
1a782 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 ]++;. /* Fall t
1a783 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 52 hrough into OP_R
1a784 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f 70 ewind */.}./* Op
1a785 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50 31 20 code: Rewind P1
1a786 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 P2 * * *.**.** T
1a787 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 he next use of t
1a788 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 he Rowid or Colu
1a789 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 mn or Next instr
1a78a 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a uction for P1 .*
1a78b 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 * will refer to
1a78c 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 the first entry
1a78d 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1a78e 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a table or index..
1a78f 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 ** If the table
1a790 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 or index is empt
1a791 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e y and P2>0, then
1a792 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c jump immediatel
1a793 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 y to P2..** If P
1a794 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 2 is 0 or if the
1a795 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1a796 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 is not empty, fa
1a797 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f ll through.** to
1a798 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
1a799 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 nstruction..*/.c
1a79a 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20 7b ase OP_Rewind: {
1a79b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 /* jump
1a79c 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 */. int i = pOp
1a79d 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 ->p1;. VdbeCurs
1a79e 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 or *pC;. BtCurs
1a79f 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 or *pCrsr;. int
1a7a0 20 72 65 73 3b 0a 0a 20 20 61 73 73 65 72 74 28 res;.. assert(
1a7a1 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 i>=0 && i<p->nC
1a7a2 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 ursor );. pC =
1a7a3 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 p->apCsr[i];. a
1a7a4 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a ssert( pC!=0 );.
1a7a5 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 70 if( (pCrsr = p
1a7a6 43 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 C->pCursor)!=0 )
1a7a7 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1a7a8 65 33 42 74 72 65 65 46 69 72 73 74 28 70 43 72 e3BtreeFirst(pCr
1a7a9 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 70 sr, &res);. p
1a7aa 43 2d 3e 61 74 46 69 72 73 74 20 3d 20 72 65 73 C->atFirst = res
1a7ab 3d 3d 30 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 ==0;. pC->def
1a7ac 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b erredMoveto = 0;
1a7ad 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 . pC->cacheSt
1a7ae 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1a7af 4c 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 LE;. }else{.
1a7b0 20 72 65 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 res = 1;. }.
1a7b1 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 72 65 pC->nullRow = re
1a7b2 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 s;. assert( pOp
1a7b3 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p2>0 && pOp->p
1a7b4 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 2<p->nOp );. if
1a7b5 28 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 ( res ){. pc
1a7b6 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1a7b7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1a7b8 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20 50 * Opcode: Next P
1a7b9 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1a7ba 20 41 64 76 61 6e 63 65 20 63 75 72 73 6f 72 20 Advance cursor
1a7bb 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f P1 so that it po
1a7bc 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 65 78 74 ints to the next
1a7bd 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 key/data pair i
1a7be 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f n its.** table o
1a7bf 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 r index. If the
1a7c0 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6b re are no more k
1a7c1 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74 ey/value pairs t
1a7c2 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 hen fall through
1a7c3 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f .** to the follo
1a7c4 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e wing instruction
1a7c5 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63 75 . But if the cu
1a7c6 72 73 6f 72 20 61 64 76 61 6e 63 65 20 77 61 73 rsor advance was
1a7c7 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 successful,.**
1a7c8 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 jump immediately
1a7c9 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 to P2..**.** Th
1a7ca 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 e P1 cursor must
1a7cb 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74 be for a real t
1a7cc 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 able, not a pseu
1a7cd 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 do-table..**.**
1a7ce 53 65 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a 2a See also: Prev.*
1a7cf 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 65 /./* Opcode: Pre
1a7d0 76 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a v P1 P2 * * *.**
1a7d1 0a 2a 2a 20 42 61 63 6b 20 75 70 20 63 75 72 73 .** Back up curs
1a7d2 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 or P1 so that it
1a7d3 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 points to the p
1a7d4 72 65 76 69 6f 75 73 20 6b 65 79 2f 64 61 74 61 revious key/data
1a7d5 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 pair in its.**
1a7d6 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 table or index.
1a7d7 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
1a7d8 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76 61 6c previous key/val
1a7d9 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 ue pairs then fa
1a7da 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f ll through.** to
1a7db 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
1a7dc 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 nstruction. But
1a7dd 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 62 if the cursor b
1a7de 61 63 6b 75 70 20 77 61 73 20 73 75 63 63 65 73 ackup was succes
1a7df 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d sful,.** jump im
1a7e0 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e mediately to P2.
1a7e1 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 .**.** The P1 cu
1a7e2 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 rsor must be for
1a7e3 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e a real table, n
1a7e4 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c ot a pseudo-tabl
1a7e5 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72 e..*/.case OP_Pr
1a7e6 65 76 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ev: /*
1a7e7 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50 5f jump */.case OP_
1a7e8 4e 65 78 74 3a 20 7b 20 20 20 20 20 20 20 20 2f Next: { /
1a7e9 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 56 64 62 65 * jump */. Vdbe
1a7ea 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 Cursor *pC;. Bt
1a7eb 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1a7ec 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 43 48 45 int res;.. CHE
1a7ed 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 CK_FOR_INTERRUPT
1a7ee 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1a7ef 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1a7f0 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1a7f1 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b pC = p->apCsr[
1a7f2 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 pOp->p1];. if(
1a7f3 70 43 3d 3d 30 20 29 7b 0a 20 20 20 20 62 72 65 pC==0 ){. bre
1a7f4 61 6b 3b 20 20 2f 2a 20 53 65 65 20 74 69 63 6b ak; /* See tick
1a7f5 65 74 20 23 32 32 37 33 20 2a 2f 0a 20 20 7d 0a et #2273 */. }.
1a7f6 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 pCrsr = pC->pC
1a7f7 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 ursor;. assert(
1a7f8 20 70 43 72 73 72 20 29 3b 0a 20 20 72 65 73 20 pCrsr );. res
1a7f9 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 = 1;. assert( p
1a7fa 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1a7fb 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 70 o==0 );. rc = p
1a7fc 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e Op->opcode==OP_N
1a7fd 65 78 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72 ext ? sqlite3Btr
1a7fe 65 65 4e 65 78 74 28 70 43 72 73 72 2c 20 26 72 eeNext(pCrsr, &r
1a7ff 65 73 29 20 3a 0a 20 20 20 20 20 20 20 20 20 20 es) :.
1a800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a801 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1a802 50 72 65 76 69 6f 75 73 28 70 43 72 73 72 2c 20 Previous(pCrsr,
1a803 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c &res);. pC->nul
1a804 6c 52 6f 77 20 3d 20 72 65 73 3b 0a 20 20 70 43 lRow = res;. pC
1a805 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1a806 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69 CACHE_STALE;. i
1a807 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 f( res==0 ){.
1a808 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1a809 31 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 1;. if( pOp->
1a80a 70 35 20 29 20 70 2d 3e 61 43 6f 75 6e 74 65 72 p5 ) p->aCounter
1a80b 5b 70 4f 70 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 [pOp->p5-1]++;.#
1a80c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1a80d 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 T. sqlite3_se
1a80e 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 arch_count++;.#e
1a80f 6e 64 69 66 0a 20 20 7d 0a 20 20 70 43 2d 3e 72 ndif. }. pC->r
1a810 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b owidIsValid = 0;
1a811 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1a812 4f 70 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72 Opcode: IdxInser
1a813 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a t P1 P2 P3 * *.*
1a814 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 32 *.** Register P2
1a815 20 68 6f 6c 64 73 20 61 20 53 51 4c 20 69 6e 64 holds a SQL ind
1a816 65 78 20 6b 65 79 20 6d 61 64 65 20 75 73 69 6e ex key made usin
1a817 67 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 49 64 78 g the.** MakeIdx
1a818 52 65 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 Rec instructions
1a819 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 . This opcode w
1a81a 72 69 74 65 73 20 74 68 61 74 20 6b 65 79 0a 2a rites that key.*
1a81b 2a 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78 * into the index
1a81c 20 50 31 2e 20 20 44 61 74 61 20 66 6f 72 20 74 P1. Data for t
1a81d 68 65 20 65 6e 74 72 79 20 69 73 20 6e 69 6c 2e he entry is nil.
1a81e 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20 66 .**.** P3 is a f
1a81f 6c 61 67 20 74 68 61 74 20 70 72 6f 76 69 64 65 lag that provide
1a820 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68 65 20 s a hint to the
1a821 62 2d 74 72 65 65 20 6c 61 79 65 72 20 74 68 61 b-tree layer tha
1a822 74 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65 72 74 t this.** insert
1a823 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 is likely to be
1a824 20 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a an append..**.*
1a825 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 * This instructi
1a826 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f on only works fo
1a827 72 20 69 6e 64 69 63 65 73 2e 20 20 54 68 65 20 r indices. The
1a828 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72 equivalent instr
1a829 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61 uction.** for ta
1a82a 62 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73 65 72 bles is OP_Inser
1a82b 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 t..*/.case OP_Id
1a82c 78 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 20 20 xInsert: {
1a82d 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 69 6e /* in2 */. in
1a82e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 t i = pOp->p1;.
1a82f 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1a830 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 . BtCursor *pCr
1a831 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e sr;. assert( i>
1a832 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 =0 && i<p->nCurs
1a833 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 or );. assert(
1a834 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 p->apCsr[i]!=0 )
1a835 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 32 ;. assert( pIn2
1a836 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c ->flags & MEM_Bl
1a837 6f 62 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 ob );. if( (pCr
1a838 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70 sr = (pC = p->ap
1a839 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 Csr[i])->pCursor
1a83a 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 )!=0 ){. asse
1a83b 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 3d rt( pC->isTable=
1a83c 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 45 =0 );. rc = E
1a83d 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b xpandBlob(pIn2);
1a83e 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1a83f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1a840 69 6e 74 20 6e 4b 65 79 20 3d 20 70 49 6e 32 2d int nKey = pIn2-
1a841 3e 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 >n;. const
1a842 63 68 61 72 20 2a 7a 4b 65 79 20 3d 20 70 49 6e char *zKey = pIn
1a843 32 2d 3e 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 2->z;. rc =
1a844 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 sqlite3BtreeIns
1a845 65 72 74 28 70 43 72 73 72 2c 20 7a 4b 65 79 2c ert(pCrsr, zKey,
1a846 20 6e 4b 65 79 2c 20 22 22 2c 20 30 2c 20 30 2c nKey, "", 0, 0,
1a847 20 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 20 20 20 pOp->p3);.
1a848 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66 assert( pC->def
1a849 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 erredMoveto==0 )
1a84a 3b 0a 20 20 20 20 20 20 70 43 2d 3e 63 61 63 68 ;. pC->cach
1a84b 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1a84c 53 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d STALE;. }. }
1a84d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1a84e 4f 70 63 6f 64 65 3a 20 49 64 78 44 65 6c 65 74 Opcode: IdxDelet
1a84f 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a e P1 P2 P3 * *.*
1a850 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 *.** The content
1a851 20 6f 66 20 50 33 20 72 65 67 69 73 74 65 72 73 of P3 registers
1a852 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 starting at reg
1a853 69 73 74 65 72 20 50 32 20 66 6f 72 6d 0a 2a 2a ister P2 form.**
1a854 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 an unpacked ind
1a855 65 78 20 6b 65 79 2e 20 54 68 69 73 20 6f 70 63 ex key. This opc
1a856 6f 64 65 20 72 65 6d 6f 76 65 73 20 74 68 61 74 ode removes that
1a857 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 entry from the
1a858 0a 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e 65 64 .** index opened
1a859 20 62 79 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a by cursor P1..*
1a85a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 44 65 6c /.case OP_IdxDel
1a85b 65 74 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d ete: {. int i =
1a85c 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 pOp->p1;. Vdbe
1a85d 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 Cursor *pC;. Bt
1a85e 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1a85f 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1a860 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
1a861 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 pOp->p2>0 && pOp
1a862 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3c 3d 70 2d ->p2+pOp->p3<=p-
1a863 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 >nMem );. asser
1a864 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e t( i>=0 && i<p->
1a865 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 nCursor );. ass
1a866 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d ert( p->apCsr[i]
1a867 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 !=0 );. if( (pC
1a868 72 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 rsr = (pC = p->a
1a869 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f pCsr[i])->pCurso
1a86a 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 r)!=0 ){. int
1a86b 20 72 65 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b res;. Unpack
1a86c 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 edRecord r;.
1a86d 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d r.pKeyInfo = pC-
1a86e 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72 >pKeyInfo;. r
1a86f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 .nField = pOp->p
1a870 33 3b 0a 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 3;. r.flags =
1a871 20 30 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d 0;. r.aMem =
1a872 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1a873 32 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 2];. rc = sql
1a874 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 ite3BtreeMovetoU
1a875 6e 70 61 63 6b 65 64 28 70 43 72 73 72 2c 20 26 npacked(pCrsr, &
1a876 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b 0a r, 0, 0, &res);.
1a877 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1a878 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 20 TE_OK && res==0
1a879 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1a87a 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 lite3BtreeDelete
1a87b 28 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20 (pCrsr);. }.
1a87c 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 assert( pC->d
1a87d 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 eferredMoveto==0
1a87e 20 29 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 );. pC->cach
1a87f 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1a880 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 STALE;. }. bre
1a881 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1a882 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 : IdxRowid P1 P2
1a883 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 * * *.**.** Wri
1a884 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 te into register
1a885 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 P2 an integer w
1a886 68 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 hich is the last
1a887 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 entry in the re
1a888 63 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 cord at.** the e
1a889 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 nd of the index
1a88a 6b 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 key pointed to b
1a88b 79 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 y cursor P1. Th
1a88c 69 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c is integer shoul
1a88d 64 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 d be.** the rowi
1a88e 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 d of the table e
1a88f 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 ntry to which th
1a890 69 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 is index entry p
1a891 6f 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 oints..**.** See
1a892 20 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 also: Rowid, Ma
1a893 6b 65 49 64 78 52 65 63 2e 0a 2a 2f 0a 63 61 73 keIdxRec..*/.cas
1a894 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b e OP_IdxRowid: {
1a895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a896 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1a897 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f */. int i = pO
1a898 70 2d 3e 70 31 3b 0a 20 20 42 74 43 75 72 73 6f p->p1;. BtCurso
1a899 72 20 2a 70 43 72 73 72 3b 0a 20 20 56 64 62 65 r *pCrsr;. Vdbe
1a89a 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 Cursor *pC;.. a
1a89b 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
1a89c 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1a89d 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 assert( p->apCs
1a89e 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 r[i]!=0 );. if(
1a89f 20 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20 (pCrsr = (pC =
1a8a0 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 p->apCsr[i])->pC
1a8a1 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 ursor)!=0 ){.
1a8a2 20 69 36 34 20 72 6f 77 69 64 3b 0a 0a 20 20 20 i64 rowid;..
1a8a3 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66 assert( pC->def
1a8a4 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 erredMoveto==0 )
1a8a5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
1a8a6 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a ->isTable==0 );.
1a8a7 20 20 20 20 69 66 28 20 21 70 43 2d 3e 6e 75 6c if( !pC->nul
1a8a8 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 63 lRow ){. rc
1a8a9 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 = sqlite3VdbeId
1a8aa 78 52 6f 77 69 64 28 70 43 72 73 72 2c 20 26 72 xRowid(pCrsr, &r
1a8ab 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 owid);. if(
1a8ac 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1a8ad 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 {. goto a
1a8ae 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1a8af 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 r;. }.
1a8b0 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1a8b1 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a pOut, MEM_Int);.
1a8b2 20 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 pOut->u.i
1a8b3 3d 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 = rowid;. }.
1a8b4 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1a8b5 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 47 45 20 * Opcode: IdxGE
1a8b6 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1a8b7 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 *.** The P4 regi
1a8b8 73 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 ster values begi
1a8b9 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f nning with P3 fo
1a8ba 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 rm an unpacked i
1a8bb 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 ndex .** key tha
1a8bc 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 t omits the ROWI
1a8bd 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 D. Compare this
1a8be 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e key value again
1a8bf 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a st the index .**
1a8c0 20 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 that P1 is curr
1a8c1 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 ently pointing t
1a8c2 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 o, ignoring the
1a8c3 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 ROWID on the P1
1a8c4 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 index..**.** If
1a8c5 74 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 the P1 index ent
1a8c6 72 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68 ry is greater th
1a8c7 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
1a8c8 68 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20 he key value.**
1a8c9 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e then jump to P2.
1a8ca 20 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c Otherwise fall
1a8cb 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
1a8cc 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e next instruction
1a8cd 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 ..**.** If P5 is
1a8ce 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 non-zero then t
1a8cf 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 he key value is
1a8d0 69 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 increased by an
1a8d1 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f epsilon .** prio
1a8d2 72 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 r to the compari
1a8d3 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 20 son. This make
1a8d4 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 the opcode work
1a8d5 6c 69 6b 65 20 49 64 78 47 54 20 65 78 63 65 70 like IdxGT excep
1a8d6 74 0a 2a 2a 20 74 68 61 74 20 69 66 20 74 68 65 t.** that if the
1a8d7 20 6b 65 79 20 66 72 6f 6d 20 72 65 67 69 73 74 key from regist
1a8d8 65 72 20 50 33 20 69 73 20 61 20 70 72 65 66 69 er P3 is a prefi
1a8d9 78 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 20 x of the key in
1a8da 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 the cursor,.** t
1a8db 68 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c he result is fal
1a8dc 73 65 20 77 68 65 72 65 61 73 20 69 74 20 77 6f se whereas it wo
1a8dd 75 6c 64 20 62 65 20 74 72 75 65 20 77 69 74 68 uld be true with
1a8de 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70 IdxGT..*/./* Op
1a8df 63 6f 64 65 3a 20 49 64 78 4c 54 20 50 31 20 50 code: IdxLT P1 P
1a8e0 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 2 P3 * P5.**.**
1a8e1 54 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20 The P4 register
1a8e2 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 values beginning
1a8e3 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e with P3 form an
1a8e4 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 unpacked index
1a8e5 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 .** key that omi
1a8e6 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 ts the ROWID. C
1a8e7 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 ompare this key
1a8e8 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 value against th
1a8e9 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 e index .** that
1a8ea 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 P1 is currently
1a8eb 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 pointing to, ig
1a8ec 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 noring the ROWID
1a8ed 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 on the P1 index
1a8ee 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 ..**.** If the P
1a8ef 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 1 index entry is
1a8f0 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b less than the k
1a8f1 65 79 20 76 61 6c 75 65 20 74 68 65 6e 20 6a 75 ey value then ju
1a8f2 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68 mp to P2..** Oth
1a8f3 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f erwise fall thro
1a8f4 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ugh to the next
1a8f5 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a instruction..**.
1a8f6 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d ** If P5 is non-
1a8f7 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 zero then the ke
1a8f8 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 y value is incre
1a8f9 61 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c ased by an epsil
1a8fa 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 on prior .** to
1a8fb 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 the comparison.
1a8fc 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 20 This makes the
1a8fd 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 opcode work like
1a8fe 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 IdxLE..*/.case
1a8ff 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 20 OP_IdxLT:
1a900 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 /* jump, in3
1a901 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 45 */.case OP_IdxGE
1a902 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1a903 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 mp, in3 */. int
1a904 20 69 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 i= pOp->p1;. V
1a905 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a dbeCursor *pC;..
1a906 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1a907 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 & i<p->nCursor )
1a908 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ;. assert( p->a
1a909 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 pCsr[i]!=0 );.
1a90a 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 if( (pC = p->apC
1a90b 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21 sr[i])->pCursor!
1a90c 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 =0 ){. int re
1a90d 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 s;. UnpackedR
1a90e 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 61 73 73 ecord r;. ass
1a90f 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 ert( pC->deferre
1a910 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 dMoveto==0 );.
1a911 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1a912 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 5==0 || pOp->p5=
1a913 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =1 );. assert
1a914 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1a915 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 72 4_INT32 );. r
1a916 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e .pKeyInfo = pC->
1a917 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72 2e pKeyInfo;. r.
1a918 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 nField = pOp->p4
1a919 2e 69 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d .i;. if( pOp-
1a91a 3e 70 35 20 29 7b 0a 20 20 20 20 20 20 72 2e 66 >p5 ){. r.f
1a91b 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f lags = UNPACKED_
1a91c 49 4e 43 52 4b 45 59 20 7c 20 55 4e 50 41 43 4b INCRKEY | UNPACK
1a91d 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b ED_IGNORE_ROWID;
1a91e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1a91f 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 r.flags = UNPA
1a920 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 CKED_IGNORE_ROWI
1a921 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 2e 61 D;. }. r.a
1a922 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Mem = &p->aMem[p
1a923 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 72 63 20 Op->p3];. rc
1a924 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 = sqlite3VdbeIdx
1a925 4b 65 79 43 6f 6d 70 61 72 65 28 70 43 2c 20 26 KeyCompare(pC, &
1a926 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 r, &res);. if
1a927 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f ( pOp->opcode==O
1a928 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 20 20 P_IdxLT ){.
1a929 20 72 65 73 20 3d 20 2d 72 65 73 3b 0a 20 20 20 res = -res;.
1a92a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
1a92b 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 sert( pOp->opcod
1a92c 65 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20 e==OP_IdxGE );.
1a92d 20 20 20 20 20 72 65 73 2b 2b 3b 0a 20 20 20 20 res++;.
1a92e 7d 0a 20 20 20 20 69 66 28 20 72 65 73 3e 30 20 }. if( res>0
1a92f 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f ){. pc = pO
1a930 70 2d 3e 70 32 20 2d 20 31 20 3b 0a 20 20 20 20 p->p2 - 1 ;.
1a931 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d }. }. break;.}
1a932 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 73 ../* Opcode: Des
1a933 74 72 6f 79 20 50 31 20 50 32 20 50 33 20 2a 20 troy P1 P2 P3 *
1a934 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 *.**.** Delete a
1a935 6e 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 n entire databas
1a936 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 e table or index
1a937 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 whose root page
1a938 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1a939 0a 2a 2a 20 66 69 6c 65 20 69 73 20 67 69 76 65 .** file is give
1a93a 6e 20 62 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 n by P1..**.** T
1a93b 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 he table being d
1a93c 65 73 74 72 6f 79 65 64 20 69 73 20 69 6e 20 74 estroyed is in t
1a93d 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1a93e 20 66 69 6c 65 20 69 66 20 50 33 3d 3d 30 2e 20 file if P3==0.
1a93f 20 49 66 0a 2a 2a 20 50 33 3d 3d 31 20 74 68 65 If.** P3==1 the
1a940 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 n the table to b
1a941 65 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 e clear is in th
1a942 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 e auxiliary data
1a943 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 base file.** tha
1a944 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f t is used to sto
1a945 72 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 re tables create
1a946 20 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 using CREATE TE
1a947 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a MPORARY TABLE..*
1a948 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 *.** If AUTOVACU
1a949 55 4d 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68 UM is enabled th
1a94a 65 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c en it is possibl
1a94b 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 72 e that another r
1a94c 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 oot page.** migh
1a94d 74 20 62 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 t be moved into
1a94e 74 68 65 20 6e 65 77 6c 79 20 64 65 6c 65 74 65 the newly delete
1a94f 64 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 6f d root page in o
1a950 72 64 65 72 20 74 6f 20 6b 65 65 70 20 61 6c 6c rder to keep all
1a951 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 63 .** root pages c
1a952 6f 6e 74 69 67 75 6f 75 73 20 61 74 20 74 68 65 ontiguous at the
1a953 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
1a954 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 e database. The
1a955 20 66 6f 72 6d 65 72 0a 2a 2a 20 76 61 6c 75 65 former.** value
1a956 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 70 61 67 of the root pag
1a957 65 20 74 68 61 74 20 6d 6f 76 65 64 20 2d 20 69 e that moved - i
1a958 74 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 ts value before
1a959 74 68 65 20 6d 6f 76 65 20 6f 63 63 75 72 72 65 the move occurre
1a95a 64 20 2d 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 d -.** is stored
1a95b 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e in register P2.
1a95c 20 20 49 66 20 6e 6f 20 70 61 67 65 20 0a 2a 2a If no page .**
1a95d 20 6d 6f 76 65 6d 65 6e 74 20 77 61 73 20 72 65 movement was re
1a95e 71 75 69 72 65 64 20 28 62 65 63 61 75 73 65 20 quired (because
1a95f 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
1a960 64 72 6f 70 70 65 64 20 77 61 73 20 61 6c 72 65 dropped was alre
1a961 61 64 79 20 0a 2a 2a 20 74 68 65 20 6c 61 73 74 ady .** the last
1a962 20 6f 6e 65 20 69 6e 20 74 68 65 20 64 61 74 61 one in the data
1a963 62 61 73 65 29 20 74 68 65 6e 20 61 20 7a 65 72 base) then a zer
1a964 6f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 o is stored in r
1a965 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 49 egister P2..** I
1a966 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 f AUTOVACUUM is
1a967 64 69 73 61 62 6c 65 64 20 74 68 65 6e 20 61 20 disabled then a
1a968 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20 69 zero is stored i
1a969 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a n register P2..*
1a96a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 *.** See also: C
1a96b 6c 65 61 72 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f lear.*/.case OP_
1a96c 44 65 73 74 72 6f 79 3a 20 7b 20 20 20 20 20 2f Destroy: { /
1a96d 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1a96e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f 76 65 e */. int iMove
1a96f 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74 3b 0a 23 d;. int iCnt;.#
1a970 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1a971 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1a972 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 Vdbe *pVdbe;.
1a973 20 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72 iCnt = 0;. for
1a974 28 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 62 65 (pVdbe=db->pVdbe
1a975 3b 20 70 56 64 62 65 3b 20 70 56 64 62 65 3d 70 ; pVdbe; pVdbe=p
1a976 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 Vdbe->pNext){.
1a977 20 20 69 66 28 20 70 56 64 62 65 2d 3e 6d 61 67 if( pVdbe->mag
1a978 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 ic==VDBE_MAGIC_R
1a979 55 4e 20 26 26 20 70 56 64 62 65 2d 3e 69 6e 56 UN && pVdbe->inV
1a97a 74 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 70 tabMethod<2 && p
1a97b 56 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 Vdbe->pc>=0 ){.
1a97c 20 20 20 20 20 69 43 6e 74 2b 2b 3b 0a 20 20 20 iCnt++;.
1a97d 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 }. }.#else. i
1a97e 43 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 Cnt = db->active
1a97f 56 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a VdbeCnt;.#endif.
1a980 20 20 69 66 28 20 69 43 6e 74 3e 31 20 29 7b 0a if( iCnt>1 ){.
1a981 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1a982 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 70 2d 3e 65 LOCKED;. p->e
1a983 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f rrorAction = OE_
1a984 41 62 6f 72 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a Abort;. }else{.
1a985 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f int iDb = pO
1a986 70 2d 3e 70 33 3b 0a 20 20 20 20 61 73 73 65 72 p->p3;. asser
1a987 74 28 20 69 43 6e 74 3d 3d 31 20 29 3b 0a 20 20 t( iCnt==1 );.
1a988 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1a989 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 reeMask & (1<<iD
1a98a 62 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 b))!=0 );. rc
1a98b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 = sqlite3BtreeD
1a98c 72 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 ropTable(db->aDb
1a98d 5b 69 44 62 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e [iDb].pBt, pOp->
1a98e 70 31 2c 20 26 69 4d 6f 76 65 64 29 3b 0a 20 20 p1, &iMoved);.
1a98f 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1a990 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1a991 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d . pOut->u.i =
1a992 20 69 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 iMoved;.#ifndef
1a993 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1a994 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 OVACUUM. if(
1a995 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1a996 20 69 4d 6f 76 65 64 21 3d 30 20 29 7b 0a 20 20 iMoved!=0 ){.
1a997 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 sqlite3RootP
1a998 61 67 65 4d 6f 76 65 64 28 26 64 62 2d 3e 61 44 ageMoved(&db->aD
1a999 62 5b 69 44 62 5d 2c 20 69 4d 6f 76 65 64 2c 20 b[iDb], iMoved,
1a99a 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a pOp->p1);. }.
1a99b 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 #endif. }. bre
1a99c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1a99d 3a 20 43 6c 65 61 72 20 50 31 20 50 32 20 50 33 : Clear P1 P2 P3
1a99e 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c .**.** Delete al
1a99f 6c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 l contents of th
1a9a0 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 e database table
1a9a1 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 or index whose
1a9a2 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20 root page.** in
1a9a3 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1a9a4 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 e is given by P1
1a9a5 2e 20 20 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44 . But, unlike D
1a9a6 65 73 74 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a estroy, do not.*
1a9a7 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 * remove the tab
1a9a8 6c 65 20 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d le or index from
1a9a9 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1a9aa 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 le..**.** The ta
1a9ab 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 20 ble being clear
1a9ac 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 is in the main d
1a9ad 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 atabase file if
1a9ae 50 32 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 P2==0. If.** P2
1a9af 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 ==1 then the tab
1a9b0 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 le to be clear i
1a9b1 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 s in the auxilia
1a9b2 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ry database file
1a9b3 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 .** that is used
1a9b4 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 to store tables
1a9b5 20 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 create using CR
1a9b6 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 EATE TEMPORARY T
1a9b7 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ABLE..**.** If t
1a9b8 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 6e he P3 value is n
1a9b9 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 on-zero, then th
1a9ba 65 20 74 61 62 6c 65 20 72 65 66 65 72 65 64 20 e table refered
1a9bb 74 6f 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a to must be an.**
1a9bc 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61 intkey table (a
1a9bd 6e 20 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 n SQL table, not
1a9be 20 61 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 an index). In t
1a9bf 68 69 73 20 63 61 73 65 20 74 68 65 20 72 6f 77 his case the row
1a9c0 20 63 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e change .** coun
1a9c1 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 t is incremented
1a9c2 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f by the number o
1a9c3 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 f rows in the ta
1a9c4 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 ble being cleare
1a9c5 64 2e 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 d. .** If P3 is
1a9c6 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 greater than zer
1a9c7 6f 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 o, then the valu
1a9c8 65 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 e stored in regi
1a9c9 73 74 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c ster P3 is.** al
1a9ca 73 6f 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 so incremented b
1a9cb 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 y the number of
1a9cc 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c rows in the tabl
1a9cd 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e e being cleared.
1a9ce 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1a9cf 20 44 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 Destroy.*/.case
1a9d0 20 4f 50 5f 43 6c 65 61 72 3a 20 7b 0a 20 20 69 OP_Clear: {. i
1a9d1 6e 74 20 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a nt nChange = 0;.
1a9d2 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1a9d3 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f reeMask & (1<<pO
1a9d4 70 2d 3e 70 32 29 29 21 3d 30 20 29 3b 0a 20 20 p->p2))!=0 );.
1a9d5 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1a9d6 65 43 6c 65 61 72 54 61 62 6c 65 28 0a 20 20 20 eClearTable(.
1a9d7 20 20 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e db->aDb[pOp->
1a9d8 70 32 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 p2].pBt, pOp->p1
1a9d9 2c 20 28 70 4f 70 2d 3e 70 33 20 3f 20 26 6e 43 , (pOp->p3 ? &nC
1a9da 68 61 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a hange : 0). );.
1a9db 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b if( pOp->p3 ){
1a9dc 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 . p->nChange
1a9dd 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 += nChange;.
1a9de 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 7b if( pOp->p3>0 ){
1a9df 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 70 . p->aMem[p
1a9e0 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 6e Op->p3].u.i += n
1a9e1 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d 0a 20 20 Change;. }.
1a9e2 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1a9e3 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 54 Opcode: CreateT
1a9e4 61 62 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a able P1 P2 * * *
1a9e5 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 .**.** Allocate
1a9e6 61 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 74 a new table in t
1a9e7 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1a9e8 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f file if P1==0 o
1a9e9 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 r in the.** auxi
1a9ea 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 liary database f
1a9eb 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 ile if P1==1 or
1a9ec 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 in an attached d
1a9ed 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 atabase if.** P1
1a9ee 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 >1. Write the r
1a9ef 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 oot page number
1a9f0 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 of the new table
1a9f1 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 into.** registe
1a9f2 72 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 r P2.**.** The d
1a9f3 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
1a9f4 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 6e n a table and an
1a9f5 20 69 6e 64 65 78 20 69 73 20 74 68 69 73 3a 20 index is this:
1a9f6 20 41 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a A table must.**
1a9f7 20 68 61 76 65 20 61 20 34 2d 62 79 74 65 20 69 have a 4-byte i
1a9f8 6e 74 65 67 65 72 20 6b 65 79 20 61 6e 64 20 63 nteger key and c
1a9f9 61 6e 20 68 61 76 65 20 61 72 62 69 74 72 61 72 an have arbitrar
1a9fa 79 20 64 61 74 61 2e 20 20 41 6e 20 69 6e 64 65 y data. An inde
1a9fb 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72 62 69 x.** has an arbi
1a9fc 74 72 61 72 79 20 6b 65 79 20 62 75 74 20 6e 6f trary key but no
1a9fd 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 data..**.** See
1a9fe 20 61 6c 73 6f 3a 20 43 72 65 61 74 65 49 6e 64 also: CreateInd
1a9ff 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a ex.*/./* Opcode:
1aa00 20 43 72 65 61 74 65 49 6e 64 65 78 20 50 31 20 CreateIndex P1
1aa01 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 P2 * * *.**.** A
1aa02 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 69 6e llocate a new in
1aa03 64 65 78 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 dex in the main
1aa04 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 database file if
1aa05 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 P1==0 or in the
1aa06 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 .** auxiliary da
1aa07 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 tabase file if P
1aa08 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 1==1 or in an at
1aa09 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 tached database
1aa0a 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 if.** P1>1. Wri
1aa0b 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 te the root page
1aa0c 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e number of the n
1aa0d 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a ew table into.**
1aa0e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a register P2..**
1aa0f 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 6e 74 .** See document
1aa10 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 65 61 ation on OP_Crea
1aa11 74 65 54 61 62 6c 65 20 66 6f 72 20 61 64 64 69 teTable for addi
1aa12 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
1aa13 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 on..*/.case OP_C
1aa14 72 65 61 74 65 49 6e 64 65 78 3a 20 20 20 20 20 reateIndex:
1aa15 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1aa16 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 63 61 73 rerelease */.cas
1aa17 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 e OP_CreateTable
1aa18 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 : { /*
1aa19 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1aa1a 2a 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 */. int pgno;.
1aa1b 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62 int flags;. Db
1aa1c 20 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74 28 *pDb;. assert(
1aa1d 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1aa1e 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 Op->p1<db->nDb )
1aa1f 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e ;. assert( (p->
1aa20 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c btreeMask & (1<<
1aa21 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a pOp->p1))!=0 );.
1aa22 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 pDb = &db->aDb
1aa23 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1aa24 65 72 74 28 20 70 44 62 2d 3e 70 42 74 21 3d 30 ert( pDb->pBt!=0
1aa25 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f );. if( pOp->o
1aa26 70 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 pcode==OP_Create
1aa27 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 Table ){. /*
1aa28 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e flags = BTREE_IN
1aa29 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 66 6c 61 TKEY; */. fla
1aa2a 67 73 20 3d 20 42 54 52 45 45 5f 4c 45 41 46 44 gs = BTREE_LEAFD
1aa2b 41 54 41 7c 42 54 52 45 45 5f 49 4e 54 4b 45 59 ATA|BTREE_INTKEY
1aa2c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 ;. }else{. f
1aa2d 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 lags = BTREE_ZER
1aa2e 4f 44 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 ODATA;. }. rc
1aa2f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 = sqlite3BtreeCr
1aa30 65 61 74 65 54 61 62 6c 65 28 70 44 62 2d 3e 70 eateTable(pDb->p
1aa31 42 74 2c 20 26 70 67 6e 6f 2c 20 66 6c 61 67 73 Bt, &pgno, flags
1aa32 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1aa33 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 4f ITE_OK ){. pO
1aa34 75 74 2d 3e 75 2e 69 20 3d 20 70 67 6e 6f 3b 0a ut->u.i = pgno;.
1aa35 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
1aa36 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 ag(pOut, MEM_Int
1aa37 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1aa38 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 }../* Opcode: Pa
1aa39 72 73 65 53 63 68 65 6d 61 20 50 31 20 50 32 20 rseSchema P1 P2
1aa3a 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 * P4 *.**.** Rea
1aa3b 64 20 61 6e 64 20 70 61 72 73 65 20 61 6c 6c 20 d and parse all
1aa3c 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 entries from the
1aa3d 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 SQLITE_MASTER t
1aa3e 61 62 6c 65 20 6f 66 20 64 61 74 61 62 61 73 65 able of database
1aa3f 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 P1.** that matc
1aa40 68 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 h the WHERE clau
1aa41 73 65 20 50 34 2e 20 20 50 32 20 69 73 20 74 68 se P4. P2 is th
1aa42 65 20 22 66 6f 72 63 65 22 20 66 6c 61 67 2e 20 e "force" flag.
1aa43 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 74 Always do.** t
1aa44 68 65 20 70 61 72 73 69 6e 67 20 69 66 20 50 32 he parsing if P2
1aa45 20 69 73 20 74 72 75 65 2e 20 20 49 66 20 50 32 is true. If P2
1aa46 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 is false, then
1aa47 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
1aa48 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 a.** no-op if th
1aa49 65 20 73 63 68 65 6d 61 20 69 73 20 6e 6f 74 20 e schema is not
1aa4a 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64 65 64 currently loaded
1aa4b 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
1aa4c 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73 20 66 s, if P2.** is f
1aa4d 61 6c 73 65 2c 20 74 68 65 20 53 51 4c 49 54 45 alse, the SQLITE
1aa4e 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 69 73 _MASTER table is
1aa4f 20 6f 6e 6c 79 20 70 61 72 73 65 64 20 69 66 20 only parsed if
1aa50 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 0a the rest of the.
1aa51 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72 ** schema is alr
1aa52 65 61 64 79 20 6c 6f 61 64 65 64 20 69 6e 74 6f eady loaded into
1aa53 20 74 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c the symbol tabl
1aa54 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 e..**.** This op
1aa55 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 code invokes the
1aa56 20 70 61 72 73 65 72 20 74 6f 20 63 72 65 61 74 parser to creat
1aa57 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 e a new virtual
1aa58 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e machine,.** then
1aa59 20 72 75 6e 73 20 74 68 65 20 6e 65 77 20 76 69 runs the new vi
1aa5a 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 rtual machine.
1aa5b 49 74 20 69 73 20 74 68 75 73 20 61 20 72 65 2d It is thus a re-
1aa5c 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a entrant opcode..
1aa5d 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 73 65 */.case OP_Parse
1aa5e 53 63 68 65 6d 61 3a 20 7b 0a 20 20 63 68 61 72 Schema: {. char
1aa5f 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 69 44 *zSql;. int iD
1aa60 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 63 b = pOp->p1;. c
1aa61 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 onst char *zMast
1aa62 65 72 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 69 er;. InitData i
1aa63 6e 69 74 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 nitData;.. asse
1aa64 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 rt( iDb>=0 && iD
1aa65 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69 b<db->nDb );. i
1aa66 66 28 20 21 70 4f 70 2d 3e 70 32 20 26 26 20 21 f( !pOp->p2 && !
1aa67 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 DbHasProperty(db
1aa68 2c 20 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 , iDb, DB_Schema
1aa69 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 62 Loaded) ){. b
1aa6a 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 reak;. }. zMas
1aa6b 74 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 ter = SCHEMA_TAB
1aa6c 4c 45 28 69 44 62 29 3b 0a 20 20 69 6e 69 74 44 LE(iDb);. initD
1aa6d 61 74 61 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 ata.db = db;. i
1aa6e 6e 69 74 44 61 74 61 2e 69 44 62 20 3d 20 70 4f nitData.iDb = pO
1aa6f 70 2d 3e 70 31 3b 0a 20 20 69 6e 69 74 44 61 74 p->p1;. initDat
1aa70 61 2e 70 7a 45 72 72 4d 73 67 20 3d 20 26 70 2d a.pzErrMsg = &p-
1aa71 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 7a 53 71 6c >zErrMsg;. zSql
1aa72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
1aa73 66 28 64 62 2c 0a 20 20 20 20 20 22 53 45 4c 45 f(db,. "SELE
1aa74 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 CT name, rootpag
1aa75 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 e, sql FROM '%q'
1aa76 2e 25 73 20 57 48 45 52 45 20 25 73 22 2c 0a 20 .%s WHERE %s",.
1aa77 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d db->aDb[iDb]
1aa78 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c .zName, zMaster,
1aa79 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 69 pOp->p4.z);. i
1aa7a 66 28 20 7a 53 71 6c 3d 3d 30 20 29 20 67 6f 74 f( zSql==0 ) got
1aa7b 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 28 76 6f 69 o no_mem;. (voi
1aa7c 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f d)sqlite3SafetyO
1aa7d 66 66 28 64 62 29 3b 0a 20 20 61 73 73 65 72 74 ff(db);. assert
1aa7e 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d ( db->init.busy=
1aa7f 3d 30 20 29 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 =0 );. db->init
1aa80 2e 62 75 73 79 20 3d 20 31 3b 0a 20 20 69 6e 69 .busy = 1;. ini
1aa81 74 44 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 tData.rc = SQLIT
1aa82 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
1aa83 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 !db->mallocFaile
1aa84 64 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 d );. rc = sqli
1aa85 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 te3_exec(db, zSq
1aa86 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 l, sqlite3InitCa
1aa87 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74 44 61 74 llback, &initDat
1aa88 61 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d a, 0);. if( rc=
1aa89 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 =SQLITE_OK ) rc
1aa8a 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 = initData.rc;.
1aa8b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1aa8c 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 64 62 2d 3e b, zSql);. db->
1aa8d 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a 20 init.busy = 0;.
1aa8e 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1aa8f 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 69 66 fetyOn(db);. if
1aa90 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
1aa91 45 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e EM ){. goto n
1aa92 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 o_mem;. }. bre
1aa93 61 6b 3b 20 20 0a 7d 0a 0a 23 69 66 20 21 64 65 ak; .}..#if !de
1aa94 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1aa95 54 5f 41 4e 41 4c 59 5a 45 29 20 26 26 20 21 64 T_ANALYZE) && !d
1aa96 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1aa97 49 54 5f 50 41 52 53 45 52 29 0a 2f 2a 20 4f 70 IT_PARSER)./* Op
1aa98 63 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 code: LoadAnalys
1aa99 69 73 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a is P1 * * * *.**
1aa9a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 73 71 6c .** Read the sql
1aa9b 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 ite_stat1 table
1aa9c 66 6f 72 20 64 61 74 61 62 61 73 65 20 50 31 20 for database P1
1aa9d 61 6e 64 20 6c 6f 61 64 20 74 68 65 20 63 6f 6e and load the con
1aa9e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 tent.** of that
1aa9f 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69 table into the i
1aaa0 6e 74 65 72 6e 61 6c 20 69 6e 64 65 78 20 68 61 nternal index ha
1aaa1 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 sh table. This
1aaa2 77 69 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 74 68 will cause.** th
1aaa3 65 20 61 6e 61 6c 79 73 69 73 20 74 6f 20 62 65 e analysis to be
1aaa4 20 75 73 65 64 20 77 68 65 6e 20 70 72 65 70 61 used when prepa
1aaa5 72 69 6e 67 20 61 6c 6c 20 73 75 62 73 65 71 75 ring all subsequ
1aaa6 65 6e 74 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a ent queries..*/.
1aaa7 63 61 73 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c case OP_LoadAnal
1aaa8 79 73 69 73 3a 20 7b 0a 20 20 69 6e 74 20 69 44 ysis: {. int iD
1aaa9 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 b = pOp->p1;. a
1aaaa 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 ssert( iDb>=0 &&
1aaab 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a iDb<db->nDb );.
1aaac 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 6e rc = sqlite3An
1aaad 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 69 alysisLoad(db, i
1aaae 44 62 29 3b 0a 20 20 62 72 65 61 6b 3b 20 20 0a Db);. break; .
1aaaf 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 }.#endif /* !def
1aab0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1aab1 5f 41 4e 41 4c 59 5a 45 29 20 26 26 20 21 64 65 _ANALYZE) && !de
1aab2 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1aab3 54 5f 50 41 52 53 45 52 29 20 20 2a 2f 0a 0a 2f T_PARSER) */../
1aab4 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 61 * Opcode: DropTa
1aab5 62 6c 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a ble P1 * * P4 *.
1aab6 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 **.** Remove the
1aab7 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 internal (in-me
1aab8 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63 mory) data struc
1aab9 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72 tures that descr
1aaba 69 62 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 ibe.** the table
1aabb 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 named P4 in dat
1aabc 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 abase P1. This
1aabd 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 is called after
1aabe 61 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 64 72 a table.** is dr
1aabf 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74 opped in order t
1aac0 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72 o keep the inter
1aac1 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 nal representati
1aac2 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 on of the.** sch
1aac3 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 ema consistent w
1aac4 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64 ith what is on d
1aac5 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f isk..*/.case OP_
1aac6 44 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20 20 73 DropTable: {. s
1aac7 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 qlite3UnlinkAndD
1aac8 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 eleteTable(db, p
1aac9 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e Op->p1, pOp->p4.
1aaca 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a z);. break;.}..
1aacb 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 49 /* Opcode: DropI
1aacc 6e 64 65 78 20 50 31 20 2a 20 2a 20 50 34 20 2a ndex P1 * * P4 *
1aacd 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 .**.** Remove th
1aace 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d e internal (in-m
1aacf 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 emory) data stru
1aad0 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 ctures that desc
1aad1 72 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e 64 65 ribe.** the inde
1aad2 78 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 x named P4 in da
1aad3 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 tabase P1. This
1aad4 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 is called after
1aad5 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20 an index.** is
1aad6 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 dropped in order
1aad7 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 to keep the int
1aad8 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
1aad9 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 tion of the.** s
1aada 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 chema consistent
1aadb 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e with what is on
1aadc 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f disk..*/.case O
1aadd 50 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b 0a 20 P_DropIndex: {.
1aade 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e sqlite3UnlinkAn
1aadf 64 44 65 6c 65 74 65 49 6e 64 65 78 28 64 62 2c dDeleteIndex(db,
1aae0 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 pOp->p1, pOp->p
1aae1 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 4.z);. break;.}
1aae2 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f ../* Opcode: Dro
1aae3 70 54 72 69 67 67 65 72 20 50 31 20 2a 20 2a 20 pTrigger P1 * *
1aae4 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 P4 *.**.** Remov
1aae5 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 e the internal (
1aae6 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 in-memory) data
1aae7 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 structures that
1aae8 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 describe.** the
1aae9 74 72 69 67 67 65 72 20 6e 61 6d 65 64 20 50 34 trigger named P4
1aaea 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e in database P1.
1aaeb 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 This is called
1aaec 20 61 66 74 65 72 20 61 20 74 72 69 67 67 65 72 after a trigger
1aaed 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 .** is dropped i
1aaee 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
1aaef 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 the internal rep
1aaf0 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
1aaf1 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e he.** schema con
1aaf2 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 sistent with wha
1aaf3 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f t is on disk..*/
1aaf4 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 72 69 .case OP_DropTri
1aaf5 67 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69 74 65 gger: {. sqlite
1aaf6 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
1aaf7 54 72 69 67 67 65 72 28 64 62 2c 20 70 4f 70 2d Trigger(db, pOp-
1aaf8 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b >p1, pOp->p4.z);
1aaf9 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 . break;.}...#i
1aafa 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1aafb 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
1aafc 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 K./* Opcode: Int
1aafd 65 67 72 69 74 79 43 6b 20 50 31 20 50 32 20 50 egrityCk P1 P2 P
1aafe 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44 6f 20 3 * P5.**.** Do
1aaff 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 74 an analysis of t
1ab00 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 he currently ope
1ab01 6e 20 64 61 74 61 62 61 73 65 2e 20 20 53 74 6f n database. Sto
1ab02 72 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 re in.** registe
1ab03 72 20 50 31 20 74 68 65 20 74 65 78 74 20 6f 66 r P1 the text of
1ab04 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1ab05 65 20 64 65 73 63 72 69 62 69 6e 67 20 61 6e 79 e describing any
1ab06 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 49 66 problems..** If
1ab07 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 72 65 no problems are
1ab08 20 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20 61 20 found, store a
1ab09 4e 55 4c 4c 20 69 6e 20 72 65 67 69 73 74 65 72 NULL in register
1ab0a 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 P1..**.** The r
1ab0b 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e 74 61 egister P3 conta
1ab0c 69 6e 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 ins the maximum
1ab0d 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 77 65 number of allowe
1ab0e 64 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41 74 20 d errors..** At
1ab0f 6d 6f 73 74 20 72 65 67 28 50 33 29 20 65 72 72 most reg(P3) err
1ab10 6f 72 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f ors will be repo
1ab11 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 rted..** In othe
1ab12 72 20 77 6f 72 64 73 2c 20 74 68 65 20 61 6e 61 r words, the ana
1ab13 6c 79 73 69 73 20 73 74 6f 70 73 20 61 73 20 73 lysis stops as s
1ab14 6f 6f 6e 20 61 73 20 72 65 67 28 50 31 29 20 65 oon as reg(P1) e
1ab15 72 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20 73 65 rrors are .** se
1ab16 65 6e 2e 20 20 52 65 67 28 50 31 29 20 69 73 20 en. Reg(P1) is
1ab17 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 updated with the
1ab18 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 number of error
1ab19 73 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a 2a 0a s remaining..**.
1ab1a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 ** The root page
1ab1b 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c 6c 20 numbers of all
1ab1c 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61 tables in the da
1ab1d 74 61 62 61 73 65 20 61 72 65 20 69 6e 74 65 67 tabase are integ
1ab1e 65 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 er.** stored in
1ab1f 72 65 67 28 50 31 29 2c 20 72 65 67 28 50 31 2b reg(P1), reg(P1+
1ab20 31 29 2c 20 72 65 67 28 50 31 2b 32 29 2c 20 2e 1), reg(P1+2), .
1ab21 2e 2e 2e 20 20 54 68 65 72 65 20 61 72 65 20 50 ... There are P
1ab22 32 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f 74 61 2 tables.** tota
1ab23 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 l..**.** If P5 i
1ab24 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 20 s not zero, the
1ab25 63 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f 6e check is done on
1ab26 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 the auxiliary d
1ab27 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 2c atabase.** file,
1ab28 20 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64 61 not the main da
1ab29 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1ab2a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 ** This opcode i
1ab2b 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d s used to implem
1ab2c 65 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69 74 ent the integrit
1ab2d 79 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e 0a y_check pragma..
1ab2e 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67 */.case OP_Integ
1ab2f 72 69 74 79 43 6b 3a 20 7b 0a 20 20 69 6e 74 20 rityCk: {. int
1ab30 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e nRoot; /* N
1ab31 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 umber of tables
1ab32 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 to check. (Numb
1ab33 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 er of root pages
1ab34 2e 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f .) */. int *aRo
1ab35 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61 79 ot; /* Array
1ab36 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d of rootpage num
1ab37 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73 20 bers for tables
1ab38 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f to be checked */
1ab39 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 . int j;
1ab3a 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1ab3b 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 er */. int nErr
1ab3c 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
1ab3d 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70 6f r of errors repo
1ab3e 72 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a rted */. char *
1ab3f 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 z; /* Tex
1ab40 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72 t of the error r
1ab41 65 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 2a eport */. Mem *
1ab42 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 pnErr; /* Re
1ab43 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20 74 gister keeping t
1ab44 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 rack of errors r
1ab45 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 20 20 0a 20 emaining */. .
1ab46 20 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 32 nRoot = pOp->p2
1ab47 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 52 6f 6f ;. assert( nRoo
1ab48 74 3e 30 20 29 3b 0a 20 20 61 52 6f 6f 74 20 3d t>0 );. aRoot =
1ab49 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1ab4a 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 Raw(db, sizeof(i
1ab4b 6e 74 29 2a 28 6e 52 6f 6f 74 2b 31 29 20 29 3b nt)*(nRoot+1) );
1ab4c 0a 20 20 69 66 28 20 61 52 6f 6f 74 3d 3d 30 20 . if( aRoot==0
1ab4d 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 ) goto no_mem;.
1ab4e 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1ab4f 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 >0 && pOp->p3<=p
1ab50 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 6e 45 72 ->nMem );. pnEr
1ab51 72 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 r = &p->aMem[pOp
1ab52 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p3];. assert(
1ab53 20 28 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 (pnErr->flags &
1ab54 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a MEM_Int)!=0 );.
1ab55 20 20 61 73 73 65 72 74 28 20 28 70 6e 45 72 72 assert( (pnErr
1ab56 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 ->flags & (MEM_S
1ab57 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 tr|MEM_Blob))==0
1ab58 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d );. pIn1 = &p-
1ab59 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
1ab5a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 52 6f for(j=0; j<nRo
1ab5b 6f 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 61 52 ot; j++){. aR
1ab5c 6f 6f 74 5b 6a 5d 20 3d 20 73 71 6c 69 74 65 33 oot[j] = sqlite3
1ab5d 56 64 62 65 49 6e 74 56 61 6c 75 65 28 26 70 49 VdbeIntValue(&pI
1ab5e 6e 31 5b 6a 5d 29 3b 0a 20 20 7d 0a 20 20 61 52 n1[j]);. }. aR
1ab5f 6f 6f 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 61 73 oot[j] = 0;. as
1ab60 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64 62 sert( pOp->p5<db
1ab61 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1ab62 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1ab63 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29 29 & (1<<pOp->p5))
1ab64 21 3d 30 20 29 3b 0a 20 20 7a 20 3d 20 73 71 6c !=0 );. z = sql
1ab65 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 ite3BtreeIntegri
1ab66 74 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b tyCheck(db->aDb[
1ab67 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 61 52 pOp->p5].pBt, aR
1ab68 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 0a 20 20 20 20 oot, nRoot,.
1ab69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ab6a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6e 45 pnE
1ab6b 72 72 2d 3e 75 2e 69 2c 20 26 6e 45 72 72 29 3b rr->u.i, &nErr);
1ab6c 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1ab6d 28 64 62 2c 20 61 52 6f 6f 74 29 3b 0a 20 20 70 (db, aRoot);. p
1ab6e 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d 20 6e 45 72 nErr->u.i -= nEr
1ab6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 r;. sqlite3Vdbe
1ab70 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 MemSetNull(pIn1)
1ab71 3b 0a 20 20 69 66 28 20 6e 45 72 72 3d 3d 30 20 ;. if( nErr==0
1ab72 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a ){. assert( z
1ab73 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 ==0 );. }else i
1ab74 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 f( z==0 ){. g
1ab75 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 65 oto no_mem;. }e
1ab76 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
1ab77 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 49 VdbeMemSetStr(pI
1ab78 6e 31 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 n1, z, -1, SQLIT
1ab79 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 65 33 5f E_UTF8, sqlite3_
1ab7a 66 72 65 65 29 3b 0a 20 20 7d 0a 20 20 55 50 44 free);. }. UPD
1ab7b 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1ab7c 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65 (pIn1);. sqlite
1ab7d 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
1ab7e 69 6e 67 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 ing(pIn1, encodi
1ab7f 6e 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ng);. break;.}.
1ab80 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1ab81 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
1ab82 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 CHECK */../* Opc
1ab83 6f 64 65 3a 20 46 69 66 6f 57 72 69 74 65 20 50 ode: FifoWrite P
1ab84 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
1ab85 57 72 69 74 65 20 74 68 65 20 69 6e 74 65 67 65 Write the intege
1ab86 72 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 r from register
1ab87 50 31 20 69 6e 74 6f 20 74 68 65 20 46 69 66 6f P1 into the Fifo
1ab88 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 69 66 ..*/.case OP_Fif
1ab89 6f 57 72 69 74 65 3a 20 7b 20 20 20 20 20 20 20 oWrite: {
1ab8a 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 70 2d 3e /* in1 */. p->
1ab8b 73 46 69 66 6f 2e 64 62 20 3d 20 64 62 3b 0a 20 sFifo.db = db;.
1ab8c 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
1ab8d 46 69 66 6f 50 75 73 68 28 26 70 2d 3e 73 46 69 FifoPush(&p->sFi
1ab8e 66 6f 2c 20 73 71 6c 69 74 65 33 56 64 62 65 49 fo, sqlite3VdbeI
1ab8f 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 3d 3d ntValue(pIn1))==
1ab90 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a SQLITE_NOMEM ){.
1ab91 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1ab92 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1ab93 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 69 66 6f ./* Opcode: Fifo
1ab94 52 65 61 64 20 50 31 20 50 32 20 2a 20 2a 20 2a Read P1 P2 * * *
1ab95 0a 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 .**.** Attempt t
1ab96 6f 20 72 65 61 64 20 61 20 73 69 6e 67 6c 65 20 o read a single
1ab97 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68 65 integer from the
1ab98 20 46 69 66 6f 2e 20 20 53 74 6f 72 65 20 74 68 Fifo. Store th
1ab99 61 74 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 6e at.** integer in
1ab9a 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a register P1..**
1ab9b 20 0a 2a 2a 20 49 66 20 74 68 65 20 46 69 66 6f .** If the Fifo
1ab9c 20 69 73 20 65 6d 70 74 79 20 6a 75 6d 70 20 74 is empty jump t
1ab9d 6f 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 o P2..*/.case OP
1ab9e 5f 46 69 66 6f 52 65 61 64 3a 20 7b 20 20 20 20 _FifoRead: {
1ab9f 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1aba0 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 CHECK_FOR_INTE
1aba1 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28 RRUPT;. assert(
1aba2 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f pOp->p1>0 && pO
1aba3 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p1<=p->nMem )
1aba4 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 ;. pOut = &p->a
1aba5 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1aba6 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
1aba7 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 Out, MEM_Int);.
1aba8 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
1aba9 46 69 66 6f 50 6f 70 28 26 70 2d 3e 73 46 69 66 FifoPop(&p->sFif
1abaa 6f 2c 20 26 70 4f 75 74 2d 3e 75 2e 69 29 3d 3d o, &pOut->u.i)==
1abab 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 SQLITE_DONE ){.
1abac 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1abad 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b - 1;. }. break
1abae 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
1abaf 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1abb0 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 ./* Opcode: Cont
1abb1 65 78 74 50 75 73 68 20 2a 20 2a 20 2a 20 0a 2a extPush * * * .*
1abb2 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75 *.** Save the cu
1abb3 72 72 65 6e 74 20 56 64 62 65 20 63 6f 6e 74 65 rrent Vdbe conte
1abb4 78 74 20 73 75 63 68 20 74 68 61 74 20 69 74 20 xt such that it
1abb5 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 can be restored
1abb6 62 79 20 61 20 43 6f 6e 74 65 78 74 50 6f 70 0a by a ContextPop.
1abb7 2a 2a 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 63 ** opcode. The c
1abb8 6f 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 ontext stores th
1abb9 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f e last insert ro
1abba 77 20 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 w id, the last s
1abbb 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 0a tatement change.
1abbc 2a 2a 20 63 6f 75 6e 74 2c 20 61 6e 64 20 74 68 ** count, and th
1abbd 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d e current statem
1abbe 65 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 ent change count
1abbf 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e ..*/.case OP_Con
1abc0 74 65 78 74 50 75 73 68 3a 20 7b 0a 20 20 69 6e textPush: {. in
1abc1 74 20 69 20 3d 20 70 2d 3e 63 6f 6e 74 65 78 74 t i = p->context
1abc2 53 74 61 63 6b 54 6f 70 2b 2b 3b 0a 20 20 43 6f StackTop++;. Co
1abc3 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 3b ntext *pContext;
1abc4 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 .. assert( i>=0
1abc5 20 29 3b 0a 20 20 2f 2a 20 46 49 58 20 4d 45 3a );. /* FIX ME:
1abc6 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 This should be
1abc7 61 6c 6c 6f 63 61 74 65 64 20 61 73 20 70 61 72 allocated as par
1abc8 74 20 6f 66 20 74 68 65 20 76 64 62 65 20 61 74 t of the vdbe at
1abc9 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 2a 2f compile-time */
1abca 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 63 6f 6e . if( i>=p->con
1abcb 74 65 78 74 53 74 61 63 6b 44 65 70 74 68 20 29 textStackDepth )
1abcc 7b 0a 20 20 20 20 70 2d 3e 63 6f 6e 74 65 78 74 {. p->context
1abcd 53 74 61 63 6b 44 65 70 74 68 20 3d 20 69 2b 31 StackDepth = i+1
1abce 3b 0a 20 20 20 20 70 2d 3e 63 6f 6e 74 65 78 74 ;. p->context
1abcf 53 74 61 63 6b 20 3d 20 73 71 6c 69 74 65 33 44 Stack = sqlite3D
1abd0 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64 bReallocOrFree(d
1abd1 62 2c 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 b, p->contextSta
1abd2 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ck,.
1abd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1abd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 si
1abd5 7a 65 6f 66 28 43 6f 6e 74 65 78 74 29 2a 28 69 zeof(Context)*(i
1abd6 2b 31 29 29 3b 0a 20 20 20 20 69 66 28 20 70 2d +1));. if( p-
1abd7 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 3d 3d 30 >contextStack==0
1abd8 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1abd9 20 20 7d 0a 20 20 70 43 6f 6e 74 65 78 74 20 3d }. pContext =
1abda 20 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 &p->contextStac
1abdb 6b 5b 69 5d 3b 0a 20 20 70 43 6f 6e 74 65 78 74 k[i];. pContext
1abdc 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62 ->lastRowid = db
1abdd 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 70 ->lastRowid;. p
1abde 43 6f 6e 74 65 78 74 2d 3e 6e 43 68 61 6e 67 65 Context->nChange
1abdf 20 3d 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 = p->nChange;.
1abe0 20 70 43 6f 6e 74 65 78 74 2d 3e 73 46 69 66 6f pContext->sFifo
1abe1 20 3d 20 70 2d 3e 73 46 69 66 6f 3b 0a 20 20 73 = p->sFifo;. s
1abe2 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f 49 6e qlite3VdbeFifoIn
1abe3 69 74 28 26 70 2d 3e 73 46 69 66 6f 2c 20 64 62 it(&p->sFifo, db
1abe4 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1abe5 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 65 78 * Opcode: Contex
1abe6 74 50 6f 70 20 2a 20 2a 20 2a 20 0a 2a 2a 0a 2a tPop * * * .**.*
1abe7 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 56 64 * Restore the Vd
1abe8 62 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 74 68 be context to th
1abe9 65 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69 e state it was i
1abea 6e 20 77 68 65 6e 20 63 6f 6e 74 65 78 74 50 75 n when contextPu
1abeb 73 68 20 77 61 73 20 6c 61 73 74 0a 2a 2a 20 65 sh was last.** e
1abec 78 65 63 75 74 65 64 2e 20 54 68 65 20 63 6f 6e xecuted. The con
1abed 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 20 text stores the
1abee 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 20 last insert row
1abef 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 id, the last sta
1abf0 74 65 6d 65 6e 74 0a 2a 2a 20 63 68 61 6e 67 65 tement.** change
1abf1 20 63 6f 75 6e 74 2c 20 61 6e 64 20 74 68 65 20 count, and the
1abf2 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e current statemen
1abf3 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2e 0a t change count..
1abf4 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 74 65 */.case OP_Conte
1abf5 78 74 50 6f 70 3a 20 7b 0a 20 20 43 6f 6e 74 65 xtPop: {. Conte
1abf6 78 74 20 2a 70 43 6f 6e 74 65 78 74 20 3d 20 26 xt *pContext = &
1abf7 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b p->contextStack[
1abf8 2d 2d 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 --p->contextStac
1abf9 6b 54 6f 70 5d 3b 0a 20 20 61 73 73 65 72 74 28 kTop];. assert(
1abfa 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b p->contextStack
1abfb 54 6f 70 3e 3d 30 20 29 3b 0a 20 20 64 62 2d 3e Top>=0 );. db->
1abfc 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 43 6f 6e lastRowid = pCon
1abfd 74 65 78 74 2d 3e 6c 61 73 74 52 6f 77 69 64 3b text->lastRowid;
1abfe 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 . p->nChange =
1abff 70 43 6f 6e 74 65 78 74 2d 3e 6e 43 68 61 6e 67 pContext->nChang
1ac00 65 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 e;. sqlite3Vdbe
1ac01 46 69 66 6f 43 6c 65 61 72 28 26 70 2d 3e 73 46 FifoClear(&p->sF
1ac02 69 66 6f 29 3b 0a 20 20 70 2d 3e 73 46 69 66 6f ifo);. p->sFifo
1ac03 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 73 46 69 = pContext->sFi
1ac04 66 6f 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 fo;. break;.}.#
1ac05 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 endif /* #ifndef
1ac06 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 SQLITE_OMIT_TRI
1ac07 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 GGER */..#ifndef
1ac08 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1ac09 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 OINCREMENT./* Op
1ac0a 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 50 31 20 code: MemMax P1
1ac0b 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 P2 * * *.**.** S
1ac0c 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 et the value of
1ac0d 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 register P1 to t
1ac0e 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 69 74 he maximum of it
1ac0f 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 0a s current value.
1ac10 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 ** and the value
1ac11 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e in register P2.
1ac12 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 .**.** This inst
1ac13 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 20 61 ruction throws a
1ac14 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 6d n error if the m
1ac15 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 6e 6f emory cell is no
1ac16 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 61 t initially.** a
1ac17 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 n integer..*/.ca
1ac18 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 7b 20 se OP_MemMax: {
1ac19 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20 69 /* in1, i
1ac1a 6e 32 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 n2 */. sqlite3V
1ac1b 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
1ac1c 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65 (pIn1);. sqlite
1ac1d 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 3VdbeMemIntegeri
1ac1e 66 79 28 70 49 6e 32 29 3b 0a 20 20 69 66 28 20 fy(pIn2);. if(
1ac1f 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e 32 2d 3e pIn1->u.i<pIn2->
1ac20 75 2e 69 29 7b 0a 20 20 20 20 70 49 6e 31 2d 3e u.i){. pIn1->
1ac21 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b u.i = pIn2->u.i;
1ac22 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1ac23 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1ac24 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
1ac25 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 ENT */../* Opcod
1ac26 65 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a e: IfPos P1 P2 *
1ac27 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 * *.**.** If th
1ac28 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 e value of regis
1ac29 74 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67 ter P1 is 1 or g
1ac2a 72 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20 reater, jump to
1ac2b 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 P2..**.** It is
1ac2c 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 illegal to use t
1ac2d 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1ac2e 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 on a register th
1ac2f 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 at does.** not c
1ac30 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 ontain an intege
1ac31 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e r. An assertion
1ac32 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 fault will resu
1ac33 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a lt if you try..*
1ac34 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a /.case OP_IfPos:
1ac35 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1ac36 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 p, in1 */. asse
1ac37 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 rt( pIn1->flags&
1ac38 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 MEM_Int );. if(
1ac39 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a pIn1->u.i>0 ){.
1ac3a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1ac3b 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
1ac3c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1ac3d 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20 : IfNeg P1 P2 *
1ac3e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 * *.**.** If the
1ac3f 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 value of regist
1ac40 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68 er P1 is less th
1ac41 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f an zero, jump to
1ac42 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 P2. .**.** It i
1ac43 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 s illegal to use
1ac44 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
1ac45 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 n on a register
1ac46 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 that does.** not
1ac47 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 contain an inte
1ac48 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 ger. An asserti
1ac49 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 on fault will re
1ac4a 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e sult if you try.
1ac4b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65 .*/.case OP_IfNe
1ac4c 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a g: { /* j
1ac4d 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 ump, in1 */. as
1ac4e 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 sert( pIn1->flag
1ac4f 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 s&MEM_Int );. i
1ac50 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29 f( pIn1->u.i<0 )
1ac51 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d {. pc = pOp-
1ac52 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1ac53 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1ac54 64 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32 de: IfZero P1 P2
1ac55 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 * * *.**.** If
1ac56 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 the value of reg
1ac57 69 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63 ister P1 is exac
1ac58 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50 tly 0, jump to P
1ac59 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2. .**.** It is
1ac5a 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 illegal to use t
1ac5b 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1ac5c 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 on a register th
1ac5d 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 at does.** not c
1ac5e 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 ontain an intege
1ac5f 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e r. An assertion
1ac60 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 fault will resu
1ac61 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a lt if you try..*
1ac62 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f /.case OP_IfZero
1ac63 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1ac64 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 mp, in1 */. ass
1ac65 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 ert( pIn1->flags
1ac66 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 &MEM_Int );. if
1ac67 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29 ( pIn1->u.i==0 )
1ac68 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d {. pc = pOp-
1ac69 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1ac6a 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1ac6b 64 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32 de: AggStep * P2
1ac6c 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1ac6d 45 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70 Execute the step
1ac6e 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e function for an
1ac6f 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65 aggregate. The
1ac70 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 .** function has
1ac71 20 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 P5 arguments.
1ac72 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
1ac73 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a to the FuncDef.
1ac74 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 ** structure tha
1ac75 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 t specifies the
1ac76 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72 function. Use r
1ac77 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73 egister.** P3 as
1ac78 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 the accumulator
1ac79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61 ..**.** The P5 a
1ac7a 72 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b rguments are tak
1ac7b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 en from register
1ac7c 20 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73 P2 and its.** s
1ac7d 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61 uccessors..*/.ca
1ac7e 73 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b se OP_AggStep: {
1ac7f 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e . int n = pOp->
1ac80 70 35 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d p5;. int i;. M
1ac81 65 6d 20 2a 70 4d 65 6d 2c 20 2a 70 52 65 63 3b em *pMem, *pRec;
1ac82 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
1ac83 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 xt ctx;. sqlite
1ac84 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 3_value **apVal;
1ac85 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 .. assert( n>=0
1ac86 20 29 3b 0a 20 20 70 52 65 63 20 3d 20 26 70 2d );. pRec = &p-
1ac87 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a >aMem[pOp->p2];.
1ac88 20 20 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 apVal = p->apA
1ac89 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20 61 70 rg;. assert( ap
1ac8a 56 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 20 Val || n==0 );.
1ac8b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 for(i=0; i<n; i
1ac8c 2b 2b 2c 20 70 52 65 63 2b 2b 29 7b 0a 20 20 20 ++, pRec++){.
1ac8d 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 52 65 63 apVal[i] = pRec
1ac8e 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 ;. storeTypeI
1ac8f 6e 66 6f 28 70 52 65 63 2c 20 65 6e 63 6f 64 69 nfo(pRec, encodi
1ac90 6e 67 29 3b 0a 20 20 7d 0a 20 20 63 74 78 2e 70 ng);. }. ctx.p
1ac91 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Func = pOp->p4.p
1ac92 46 75 6e 63 3b 0a 20 20 61 73 73 65 72 74 28 20 Func;. assert(
1ac93 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 pOp->p3>0 && pOp
1ac94 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
1ac95 0a 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d . ctx.pMem = pM
1ac96 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f em = &p->aMem[pO
1ac97 70 2d 3e 70 33 5d 3b 0a 20 20 70 4d 65 6d 2d 3e p->p3];. pMem->
1ac98 6e 2b 2b 3b 0a 20 20 63 74 78 2e 73 2e 66 6c 61 n++;. ctx.s.fla
1ac99 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1ac9a 20 63 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 ctx.s.z = 0;.
1ac9b 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 ctx.s.zMalloc =
1ac9c 30 3b 0a 20 20 63 74 78 2e 73 2e 78 44 65 6c 20 0;. ctx.s.xDel
1ac9d 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 64 62 20 = 0;. ctx.s.db
1ac9e 3d 20 64 62 3b 0a 20 20 63 74 78 2e 69 73 45 72 = db;. ctx.isEr
1ac9f 72 6f 72 20 3d 20 30 3b 0a 20 20 63 74 78 2e 70 ror = 0;. ctx.p
1aca0 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 Coll = 0;. if(
1aca1 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 ctx.pFunc->flags
1aca2 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e & SQLITE_FUNC_N
1aca3 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 EEDCOLL ){. a
1aca4 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f ssert( pOp>p->aO
1aca5 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 p );. assert(
1aca6 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d pOp[-1].p4type=
1aca7 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 =P4_COLLSEQ );.
1aca8 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d assert( pOp[-
1aca9 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 1].opcode==OP_Co
1acaa 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 78 llSeq );. ctx
1acab 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d .pColl = pOp[-1]
1acac 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 .p4.pColl;. }.
1acad 20 28 63 74 78 2e 70 46 75 6e 63 2d 3e 78 53 74 (ctx.pFunc->xSt
1acae 65 70 29 28 26 63 74 78 2c 20 6e 2c 20 61 70 56 ep)(&ctx, n, apV
1acaf 61 6c 29 3b 0a 20 20 69 66 28 20 63 74 78 2e 69 al);. if( ctx.i
1acb0 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73 71 sError ){. sq
1acb1 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1acb2 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1acb3 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 "%s", sqlite3_va
1acb4 6c 75 65 5f 74 65 78 74 28 26 63 74 78 2e 73 29 lue_text(&ctx.s)
1acb5 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 78 2e );. rc = ctx.
1acb6 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 73 isError;. }. s
1acb7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1acb8 65 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 ease(&ctx.s);.
1acb9 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1acba 6f 64 65 3a 20 41 67 67 46 69 6e 61 6c 20 50 31 ode: AggFinal P1
1acbb 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 * P4 *.**.**
1acbc 20 45 78 65 63 75 74 65 20 74 68 65 20 66 69 6e Execute the fin
1acbd 61 6c 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 alizer function
1acbe 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 for an aggregate
1acbf 2e 20 20 50 31 20 69 73 0a 2a 2a 20 74 68 65 20 . P1 is.** the
1acc0 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 memory location
1acc1 74 68 61 74 20 69 73 20 74 68 65 20 61 63 63 75 that is the accu
1acc2 6d 75 6c 61 74 6f 72 20 66 6f 72 20 74 68 65 20 mulator for the
1acc3 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a aggregate..**.**
1acc4 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 P2 is the numbe
1acc5 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 r of arguments t
1acc6 68 61 74 20 74 68 65 20 73 74 65 70 20 66 75 6e hat the step fun
1acc7 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 64 0a ction takes and.
1acc8 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1acc9 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 er to the FuncDe
1acca 66 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63 74 f for this funct
1accb 69 6f 6e 2e 20 20 54 68 65 20 50 32 0a 2a 2a 20 ion. The P2.**
1accc 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 argument is not
1accd 75 73 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 used by this opc
1acce 6f 64 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 ode. It is only
1accf 20 74 68 65 72 65 20 74 6f 20 64 69 73 61 6d 62 there to disamb
1acd0 69 67 75 61 74 65 0a 2a 2a 20 66 75 6e 63 74 69 iguate.** functi
1acd1 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 74 61 6b ons that can tak
1acd2 65 20 76 61 72 79 69 6e 67 20 6e 75 6d 62 65 72 e varying number
1acd3 73 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 s of arguments.
1acd4 20 54 68 65 0a 2a 2a 20 50 34 20 61 72 67 75 6d The.** P4 argum
1acd5 65 6e 74 20 69 73 20 6f 6e 6c 79 20 6e 65 65 64 ent is only need
1acd6 65 64 20 66 6f 72 20 74 68 65 20 64 65 67 65 6e ed for the degen
1acd7 65 72 61 74 65 20 63 61 73 65 20 77 68 65 72 65 erate case where
1acd8 0a 2a 2a 20 74 68 65 20 73 74 65 70 20 66 75 6e .** the step fun
1acd9 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 70 72 ction was not pr
1acda 65 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2e eviously called.
1acdb 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 46 .*/.case OP_AggF
1acdc 69 6e 61 6c 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 inal: {. Mem *p
1acdd 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 Mem;. assert( p
1acde 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d Op->p1>0 && pOp-
1acdf 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p1<=p->nMem );.
1ace0 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 pMem = &p->aMe
1ace1 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 m[pOp->p1];. as
1ace2 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
1ace3 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c gs & ~(MEM_Null|
1ace4 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a MEM_Agg))==0 );.
1ace5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1ace6 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 4d beMemFinalize(pM
1ace7 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e em, pOp->p4.pFun
1ace8 63 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 c);. if( rc==SQ
1ace9 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b 0a 20 20 LITE_ERROR ){.
1acea 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1aceb 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1acec 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 db, "%s", sqlite
1aced 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 4d 65 3_value_text(pMe
1acee 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 m));. }. sqlit
1acef 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
1acf0 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63 6f 64 ding(pMem, encod
1acf1 69 6e 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d ing);. UPDATE_M
1acf2 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4d 65 6d AX_BLOBSIZE(pMem
1acf3 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
1acf4 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4d VdbeMemTooBig(pM
1acf5 65 6d 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 em) ){. goto
1acf6 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62 too_big;. }. b
1acf7 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64 reak;.}...#if !d
1acf8 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1acf9 49 54 5f 56 41 43 55 55 4d 29 20 26 26 20 21 64 IT_VACUUM) && !d
1acfa 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1acfb 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a 20 4f 70 IT_ATTACH)./* Op
1acfc 63 6f 64 65 3a 20 56 61 63 75 75 6d 20 2a 20 2a code: Vacuum * *
1acfd 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 56 61 63 * * *.**.** Vac
1acfe 75 75 6d 20 74 68 65 20 65 6e 74 69 72 65 20 64 uum the entire d
1acff 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 6f atabase. This o
1ad00 70 63 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65 pcode will cause
1ad01 20 6f 74 68 65 72 20 76 69 72 74 75 61 6c 0a 2a other virtual.*
1ad02 2a 20 6d 61 63 68 69 6e 65 73 20 74 6f 20 62 65 * machines to be
1ad03 20 63 72 65 61 74 65 64 20 61 6e 64 20 72 75 6e created and run
1ad04 2e 20 20 49 74 20 6d 61 79 20 6e 6f 74 20 62 65 . It may not be
1ad05 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
1ad06 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 hin.** a transac
1ad07 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 tion..*/.case OP
1ad08 5f 56 61 63 75 75 6d 3a 20 7b 0a 20 20 69 66 28 _Vacuum: {. if(
1ad09 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1ad0a 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1ad0b 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1ad0c 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 ; . rc = sqlite
1ad0d 33 52 75 6e 56 61 63 75 75 6d 28 26 70 2d 3e 7a 3RunVacuum(&p->z
1ad0e 45 72 72 4d 73 67 2c 20 64 62 29 3b 0a 20 20 69 ErrMsg, db);. i
1ad0f 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1ad10 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 On(db) ) goto ab
1ad11 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1ad12 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 e;. break;.}.#e
1ad13 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e ndif..#if !defin
1ad14 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ed(SQLITE_OMIT_A
1ad15 55 54 4f 56 41 43 55 55 4d 29 0a 2f 2a 20 4f 70 UTOVACUUM)./* Op
1ad16 63 6f 64 65 3a 20 49 6e 63 72 56 61 63 75 75 6d code: IncrVacuum
1ad17 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1ad18 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 73 69 6e ** Perform a sin
1ad19 67 6c 65 20 73 74 65 70 20 6f 66 20 74 68 65 20 gle step of the
1ad1a 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
1ad1b 75 6d 20 70 72 6f 63 65 64 75 72 65 20 6f 6e 0a um procedure on.
1ad1c 2a 2a 20 74 68 65 20 50 31 20 64 61 74 61 62 61 ** the P1 databa
1ad1d 73 65 2e 20 49 66 20 74 68 65 20 76 61 63 75 75 se. If the vacuu
1ad1e 6d 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 m has finished,
1ad1f 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 jump to instruct
1ad20 69 6f 6e 0a 2a 2a 20 50 32 2e 20 4f 74 68 65 72 ion.** P2. Other
1ad21 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75 wise, fall throu
1ad22 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 gh to the next i
1ad23 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 nstruction..*/.c
1ad24 61 73 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 ase OP_IncrVacuu
1ad25 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a m: { /* j
1ad26 75 6d 70 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a ump */. Btree *
1ad27 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 pBt;.. assert(
1ad28 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1ad29 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b p->p1<db->nDb );
1ad2a 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 . assert( (p->b
1ad2b 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 treeMask & (1<<p
1ad2c 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 Op->p1))!=0 );.
1ad2d 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 pBt = db->aDb[p
1ad2e 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 72 Op->p1].pBt;. r
1ad2f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1ad30 49 6e 63 72 56 61 63 75 75 6d 28 70 42 74 29 3b IncrVacuum(pBt);
1ad31 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1ad32 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 70 63 E_DONE ){. pc
1ad33 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1ad34 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1ad35 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b OK;. }. break;
1ad36 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 .}.#endif../* Op
1ad37 63 6f 64 65 3a 20 45 78 70 69 72 65 20 50 31 20 code: Expire P1
1ad38 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 * * * *.**.** Ca
1ad39 75 73 65 20 70 72 65 63 6f 6d 70 69 6c 65 64 20 use precompiled
1ad3a 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 62 65 statements to be
1ad3b 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 41 6e come expired. An
1ad3c 20 65 78 70 69 72 65 64 20 73 74 61 74 65 6d 65 expired stateme
1ad3d 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 68 nt.** fails with
1ad3e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f an error code o
1ad3f 66 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 f SQLITE_SCHEMA
1ad40 69 66 20 69 74 20 69 73 20 65 76 65 72 20 65 78 if it is ever ex
1ad41 65 63 75 74 65 64 20 0a 2a 2a 20 28 76 69 61 20 ecuted .** (via
1ad42 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 29 2e sqlite3_step()).
1ad43 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31 20 69 73 .** .** If P1 is
1ad44 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20 53 51 4c 0, then all SQL
1ad45 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65 63 6f statements beco
1ad46 6d 65 20 65 78 70 69 72 65 64 2e 20 49 66 20 50 me expired. If P
1ad47 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 1 is non-zero,.*
1ad48 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 20 * then only the
1ad49 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 currently execut
1ad4a 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 69 73 ing statement is
1ad4b 20 61 66 66 65 63 74 65 64 2e 20 0a 2a 2f 0a 63 affected. .*/.c
1ad4c 61 73 65 20 4f 50 5f 45 78 70 69 72 65 3a 20 7b ase OP_Expire: {
1ad4d 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e 70 31 20 . if( !pOp->p1
1ad4e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
1ad4f 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 pirePreparedStat
1ad50 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 65 ements(db);. }e
1ad51 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 lse{. p->expi
1ad52 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 red = 1;. }. b
1ad53 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 reak;.}..#ifndef
1ad54 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
1ad55 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20 4f 70 63 RED_CACHE./* Opc
1ad56 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63 6b 20 50 ode: TableLock P
1ad57 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1ad58 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b ** Obtain a lock
1ad59 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 on a particular
1ad5a 20 74 61 62 6c 65 2e 20 54 68 69 73 20 69 6e 73 table. This ins
1ad5b 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 truction is only
1ad5c 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68 used when.** th
1ad5d 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 e shared-cache f
1ad5e 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c 65 eature is enable
1ad5f 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 d. .**.** If P1
1ad60 69 73 20 20 74 68 65 20 69 6e 64 65 78 20 6f 66 is the index of
1ad61 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e the database in
1ad62 20 73 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20 6f sqlite3.aDb[] o
1ad63 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a f the database.*
1ad64 2a 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 6c * on which the l
1ad65 6f 63 6b 20 69 73 20 61 63 71 75 69 72 65 64 2e ock is acquired.
1ad66 20 20 41 20 72 65 61 64 6c 6f 63 6b 20 69 73 20 A readlock is
1ad67 6f 62 74 61 69 6e 65 64 20 69 66 20 50 33 3d 3d obtained if P3==
1ad68 30 20 6f 72 0a 2a 2a 20 61 20 77 72 69 74 65 20 0 or.** a write
1ad69 6c 6f 63 6b 20 69 66 20 50 33 3d 3d 31 2e 0a 2a lock if P3==1..*
1ad6a 2a 0a 2a 2a 20 50 32 20 63 6f 6e 74 61 69 6e 73 *.** P2 contains
1ad6b 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f the root-page o
1ad6c 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c f the table to l
1ad6d 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 63 6f ock..**.** P4 co
1ad6e 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 ntains a pointer
1ad6f 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 to the name of
1ad70 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
1ad71 6c 6f 63 6b 65 64 2e 20 54 68 69 73 20 69 73 20 locked. This is
1ad72 6f 6e 6c 79 0a 2a 2a 20 75 73 65 64 20 74 6f 20 only.** used to
1ad73 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f generate an erro
1ad74 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68 65 r message if the
1ad75 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 lock cannot be
1ad76 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 63 61 73 obtained..*/.cas
1ad77 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 3a 20 e OP_TableLock:
1ad78 7b 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 {. int p1 = pOp
1ad79 2d 3e 70 31 3b 20 0a 20 20 75 38 20 69 73 57 72 ->p1; . u8 isWr
1ad7a 69 74 65 4c 6f 63 6b 20 3d 20 70 4f 70 2d 3e 70 iteLock = pOp->p
1ad7b 33 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31 3e 3;. assert( p1>
1ad7c 3d 30 20 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 =0 && p1<db->nDb
1ad7d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
1ad7e 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1ad7f 3c 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 61 <<p1))!=0 );. a
1ad80 73 73 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f ssert( isWriteLo
1ad81 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 ck==0 || isWrite
1ad82 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 72 63 20 Lock==1 );. rc
1ad83 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f = sqlite3BtreeLo
1ad84 63 6b 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b ckTable(db->aDb[
1ad85 70 31 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 p1].pBt, pOp->p2
1ad86 2c 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a , isWriteLock);.
1ad87 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1ad88 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 63 _LOCKED ){. c
1ad89 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 onst char *z = p
1ad8a 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 73 71 Op->p4.z;. sq
1ad8b 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1ad8c 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1ad8d 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 "database table
1ad8e 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20 is locked: %s",
1ad8f 7a 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b z);. }. break;
1ad90 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1ad91 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1ad92 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
1ad93 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1ad94 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 RTUALTABLE./* Op
1ad95 63 6f 64 65 3a 20 56 42 65 67 69 6e 20 2a 20 2a code: VBegin * *
1ad96 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 * P4 *.**.** P4
1ad97 20 6d 61 79 20 62 65 20 61 20 70 6f 69 6e 74 65 may be a pointe
1ad98 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f r to an sqlite3_
1ad99 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 20 vtab structure.
1ad9a 49 66 20 73 6f 2c 20 63 61 6c 6c 20 74 68 65 20 If so, call the
1ad9b 0a 2a 2a 20 78 42 65 67 69 6e 20 6d 65 74 68 6f .** xBegin metho
1ad9c 64 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 d for that table
1ad9d 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 2c 20 77 68 ..**.** Also, wh
1ad9e 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 50 34 20 ether or not P4
1ad9f 69 73 20 73 65 74 2c 20 63 68 65 63 6b 20 74 68 is set, check th
1ada0 61 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20 62 at this is not b
1ada1 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d eing called from
1ada2 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 63 61 6c .** within a cal
1ada3 6c 62 61 63 6b 20 74 6f 20 61 20 76 69 72 74 75 lback to a virtu
1ada4 61 6c 20 74 61 62 6c 65 20 78 53 79 6e 63 28 29 al table xSync()
1ada5 20 6d 65 74 68 6f 64 2e 20 49 66 20 69 74 20 69 method. If it i
1ada6 73 2c 20 73 65 74 20 74 68 65 0a 2a 2a 20 65 72 s, set the.** er
1ada7 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 ror code to SQLI
1ada8 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 TE_LOCKED..*/.ca
1ada9 73 65 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a se OP_VBegin: {.
1adaa 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1adab 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e pVtab = pOp->p4.
1adac 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73 71 pVtab;. rc = sq
1adad 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 64 lite3VtabBegin(d
1adae 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 69 66 28 b, pVtab);. if(
1adaf 20 70 56 74 61 62 20 29 7b 0a 20 20 20 20 73 71 pVtab ){. sq
1adb0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1adb1 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 p->zErrMsg);.
1adb2 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 p->zErrMsg = pV
1adb3 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 tab->zErrMsg;.
1adb4 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 pVtab->zErrMsg
1adb5 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 = 0;. }. brea
1adb6 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 k;.}.#endif /* S
1adb7 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1adb8 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e ALTABLE */..#ifn
1adb9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1adba 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 VIRTUALTABLE./*
1adbb 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 74 65 20 Opcode: VCreate
1adbc 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a P1 * * P4 *.**.*
1adbd 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65 * P4 is the name
1adbe 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61 of a virtual ta
1adbf 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 ble in database
1adc0 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 78 43 72 P1. Call the xCr
1adc1 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 66 eate method.** f
1adc2 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a or that table..*
1adc3 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 65 61 74 /.case OP_VCreat
1adc4 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 71 6c 69 e: {. rc = sqli
1adc5 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 te3VtabCallCreat
1adc6 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 e(db, pOp->p1, p
1adc7 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d 3e 7a 45 Op->p4.z, &p->zE
1adc8 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 61 6b 3b rrMsg);. break;
1adc9 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1adca 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1adcb 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 TABLE */..#ifnde
1adcc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1adcd 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 RTUALTABLE./* Op
1adce 63 6f 64 65 3a 20 56 44 65 73 74 72 6f 79 20 50 code: VDestroy P
1adcf 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 1 * * P4 *.**.**
1add0 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65 20 P4 is the name
1add1 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 of a virtual tab
1add2 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 le in database P
1add3 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 78 44 65 1. Call the xDe
1add4 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 stroy method.**
1add5 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a of that table..*
1add6 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 73 74 72 /.case OP_VDestr
1add7 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e 56 74 61 oy: {. p->inVta
1add8 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a 20 20 72 bMethod = 2;. r
1add9 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 c = sqlite3VtabC
1adda 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 2c 20 70 allDestroy(db, p
1addb 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e Op->p1, pOp->p4.
1addc 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d z);. p->inVtabM
1addd 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 62 72 65 ethod = 0;. bre
1adde 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1addf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1ade0 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1ade1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1ade2 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1ade3 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 6e 20 50 Opcode: VOpen P
1ade4 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 1 * * P4 *.**.**
1ade5 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
1ade6 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 to a virtual ta
1ade7 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 ble object, an s
1ade8 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
1ade9 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 69 73 20 cture..** P1 is
1adea 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e a cursor number.
1adeb 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 6f 70 This opcode op
1adec 65 6e 73 20 61 20 63 75 72 73 6f 72 20 74 6f 20 ens a cursor to
1aded 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 the virtual.** t
1adee 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 73 20 able and stores
1adef 74 68 61 74 20 63 75 72 73 6f 72 20 69 6e 20 50 that cursor in P
1adf0 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4f 1..*/.case OP_VO
1adf1 70 65 6e 3a 20 7b 0a 20 20 56 64 62 65 43 75 72 pen: {. VdbeCur
1adf2 73 6f 72 20 2a 70 43 75 72 20 3d 20 30 3b 0a 20 sor *pCur = 0;.
1adf3 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 sqlite3_vtab_cu
1adf4 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f rsor *pVtabCurso
1adf5 72 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 r = 0;.. sqlite
1adf6 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 3_vtab *pVtab =
1adf7 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 pOp->p4.pVtab;.
1adf8 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
1adf9 2a 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 *pModule = (sqli
1adfa 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 te3_module *)pVt
1adfb 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 ab->pModule;..
1adfc 61 73 73 65 72 74 28 70 56 74 61 62 20 26 26 20 assert(pVtab &&
1adfd 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20 pModule);. if(
1adfe 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1adff 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1ae00 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1ae01 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d . rc = pModule-
1ae02 3e 78 4f 70 65 6e 28 70 56 74 61 62 2c 20 26 70 >xOpen(pVtab, &p
1ae03 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73 VtabCursor);. s
1ae04 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1ae05 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1ae06 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 p->zErrMsg = pVt
1ae07 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 ab->zErrMsg;. p
1ae08 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 Vtab->zErrMsg =
1ae09 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 0;. if( sqlite3
1ae0a 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 SafetyOn(db) ) g
1ae0b 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1ae0c 5f 6d 69 73 75 73 65 3b 0a 20 20 69 66 28 20 53 _misuse;. if( S
1ae0d 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a QLITE_OK==rc ){.
1ae0e 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a /* Initializ
1ae0f 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 e sqlite3_vtab_c
1ae10 75 72 73 6f 72 20 62 61 73 65 20 63 6c 61 73 73 ursor base class
1ae11 20 2a 2f 0a 20 20 20 20 70 56 74 61 62 43 75 72 */. pVtabCur
1ae12 73 6f 72 2d 3e 70 56 74 61 62 20 3d 20 70 56 74 sor->pVtab = pVt
1ae13 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 ab;.. /* Init
1ae14 69 61 6c 69 73 65 20 76 64 62 65 20 63 75 72 73 ialise vdbe curs
1ae15 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 or object */.
1ae16 20 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 pCur = allocate
1ae17 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 Cursor(p, pOp->p
1ae18 31 2c 20 26 70 4f 70 5b 2d 31 5d 2c 20 2d 31 2c 1, &pOp[-1], -1,
1ae19 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 0);. if( pCu
1ae1a 72 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d r ){. pCur-
1ae1b 3e 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 >pVtabCursor = p
1ae1c 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 VtabCursor;.
1ae1d 20 20 70 43 75 72 2d 3e 70 4d 6f 64 75 6c 65 20 pCur->pModule
1ae1e 3d 20 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 = pVtabCursor->p
1ae1f 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 Vtab->pModule;.
1ae20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1ae21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1ae22 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4d 6f 64 = 1;. pMod
1ae23 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74 61 ule->xClose(pVta
1ae24 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a bCursor);. }.
1ae25 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1ae26 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1ae27 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1ae28 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
1ae29 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1ae2a 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 LTABLE./* Opcode
1ae2b 3a 20 56 46 69 6c 74 65 72 20 50 31 20 50 32 20 : VFilter P1 P2
1ae2c 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 P3 P4 *.**.** P1
1ae2d 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 is a cursor ope
1ae2e 6e 65 64 20 75 73 69 6e 67 20 56 4f 70 65 6e 2e ned using VOpen.
1ae2f 20 20 50 32 20 69 73 20 61 6e 20 61 64 64 72 65 P2 is an addre
1ae30 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 ss to jump to if
1ae31 0a 2a 2a 20 74 68 65 20 66 69 6c 74 65 72 65 64 .** the filtered
1ae32 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 result set is e
1ae33 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 mpty..**.** P4 i
1ae34 73 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 s either NULL or
1ae35 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 77 a string that w
1ae36 61 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 as generated by
1ae37 74 68 65 20 78 42 65 73 74 49 6e 64 65 78 0a 2a the xBestIndex.*
1ae38 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 * method of the
1ae39 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 69 6e 74 module. The int
1ae3a 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 erpretation of t
1ae3b 68 65 20 50 34 20 73 74 72 69 6e 67 20 69 73 20 he P4 string is
1ae3c 6c 65 66 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6d left.** to the m
1ae3d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 odule implementa
1ae3e 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tion..**.** This
1ae3f 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 opcode invokes
1ae40 74 68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 the xFilter meth
1ae41 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61 od on the virtua
1ae42 6c 20 74 61 62 6c 65 20 73 70 65 63 69 66 69 65 l table specifie
1ae43 64 0a 2a 2a 20 62 79 20 50 31 2e 20 20 54 68 65 d.** by P1. The
1ae44 20 69 6e 74 65 67 65 72 20 71 75 65 72 79 20 70 integer query p
1ae45 6c 61 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f lan parameter to
1ae46 20 78 46 69 6c 74 65 72 20 69 73 20 73 74 6f 72 xFilter is stor
1ae47 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a ed in register.*
1ae48 2a 20 50 33 2e 20 52 65 67 69 73 74 65 72 20 50 * P3. Register P
1ae49 33 2b 31 20 73 74 6f 72 65 73 20 74 68 65 20 61 3+1 stores the a
1ae4a 72 67 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f rgc parameter to
1ae4b 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 68 be passed to th
1ae4c 65 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d 65 74 e.** xFilter met
1ae4d 68 6f 64 2e 20 52 65 67 69 73 74 65 72 73 20 50 hod. Registers P
1ae4e 33 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63 20 61 3+2..P3+1+argc a
1ae4f 72 65 20 74 68 65 20 61 72 67 63 0a 2a 2a 20 61 re the argc.** a
1ae50 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 dditional parame
1ae51 74 65 72 73 20 77 68 69 63 68 20 61 72 65 20 70 ters which are p
1ae52 61 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46 69 6c assed to.** xFil
1ae53 74 65 72 20 61 73 20 61 72 67 76 2e 20 52 65 67 ter as argv. Reg
1ae54 69 73 74 65 72 20 50 33 2b 32 20 62 65 63 6f 6d ister P3+2 becom
1ae55 65 73 20 61 72 67 76 5b 30 5d 20 77 68 65 6e 20 es argv[0] when
1ae56 70 61 73 73 65 64 20 74 6f 20 78 46 69 6c 74 65 passed to xFilte
1ae57 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20 r..**.** A jump
1ae58 69 73 20 6d 61 64 65 20 74 6f 20 50 32 20 69 66 is made to P2 if
1ae59 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1ae5a 61 66 74 65 72 20 66 69 6c 74 65 72 69 6e 67 20 after filtering
1ae5b 77 6f 75 6c 64 20 62 65 20 65 6d 70 74 79 2e 0a would be empty..
1ae5c 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 46 69 6c 74 */.case OP_VFilt
1ae5d 65 72 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 er: { /* jump
1ae5e 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 */. int nArg;.
1ae5f 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 63 int iQuery;. c
1ae60 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
1ae61 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 ule *pModule;.
1ae62 4d 65 6d 20 2a 70 51 75 65 72 79 20 3d 20 26 70 Mem *pQuery = &p
1ae63 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1ae64 0a 20 20 4d 65 6d 20 2a 70 41 72 67 63 20 3d 20 . Mem *pArgc =
1ae65 26 70 51 75 65 72 79 5b 31 5d 3b 0a 20 20 73 71 &pQuery[1];. sq
1ae66 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1ae67 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a r *pVtabCursor;.
1ae68 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1ae69 70 56 74 61 62 3b 0a 0a 20 20 56 64 62 65 43 75 pVtab;.. VdbeCu
1ae6a 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e rsor *pCur = p->
1ae6b 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1ae6c 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1ae6d 45 28 70 4f 70 2d 3e 70 33 2c 20 70 51 75 65 72 E(pOp->p3, pQuer
1ae6e 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 y);. assert( pC
1ae6f 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 ur->pVtabCursor
1ae70 29 3b 0a 20 20 70 56 74 61 62 43 75 72 73 6f 72 );. pVtabCursor
1ae71 20 3d 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 = pCur->pVtabCu
1ae72 72 73 6f 72 3b 0a 20 20 70 56 74 61 62 20 3d 20 rsor;. pVtab =
1ae73 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 pVtabCursor->pVt
1ae74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 ab;. pModule =
1ae75 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a pVtab->pModule;.
1ae76 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20 69 . /* Grab the i
1ae77 6e 64 65 78 20 6e 75 6d 62 65 72 20 61 6e 64 20 ndex number and
1ae78 61 72 67 63 20 70 61 72 61 6d 65 74 65 72 73 20 argc parameters
1ae79 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 51 */. assert( (pQ
1ae7a 75 65 72 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f uery->flags&MEM_
1ae7b 49 6e 74 29 21 3d 30 20 26 26 20 70 41 72 67 63 Int)!=0 && pArgc
1ae7c 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 ->flags==MEM_Int
1ae7d 20 29 3b 0a 20 20 6e 41 72 67 20 3d 20 70 41 72 );. nArg = pAr
1ae7e 67 63 2d 3e 75 2e 69 3b 0a 20 20 69 51 75 65 72 gc->u.i;. iQuer
1ae7f 79 20 3d 20 70 51 75 65 72 79 2d 3e 75 2e 69 3b y = pQuery->u.i;
1ae80 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 .. /* Invoke th
1ae81 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 e xFilter method
1ae82 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 */. {. int
1ae83 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 res = 0;. int
1ae84 20 69 3b 0a 20 20 20 20 4d 65 6d 20 2a 2a 61 70 i;. Mem **ap
1ae85 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a Arg = p->apArg;.
1ae86 20 20 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 for(i = 0; i
1ae87 3c 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nArg; i++){.
1ae88 20 20 20 61 70 41 72 67 5b 69 5d 20 3d 20 26 70 apArg[i] = &p
1ae89 41 72 67 63 5b 69 2b 31 5d 3b 0a 20 20 20 20 20 Argc[i+1];.
1ae8a 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 61 storeTypeInfo(a
1ae8b 70 41 72 67 5b 69 5d 2c 20 30 29 3b 0a 20 20 20 pArg[i], 0);.
1ae8c 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }.. if( sqli
1ae8d 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
1ae8e 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1ae8f 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 e_to_misuse;.
1ae90 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b sqlite3VtabLock
1ae91 28 70 56 74 61 62 29 3b 0a 20 20 20 20 70 2d 3e (pVtab);. p->
1ae92 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 inVtabMethod = 1
1ae93 3b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 ;. rc = pModu
1ae94 6c 65 2d 3e 78 46 69 6c 74 65 72 28 70 56 74 61 le->xFilter(pVta
1ae95 62 43 75 72 73 6f 72 2c 20 69 51 75 65 72 79 2c bCursor, iQuery,
1ae96 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 6e 41 72 67 pOp->p4.z, nArg
1ae97 2c 20 61 70 41 72 67 29 3b 0a 20 20 20 20 70 2d , apArg);. p-
1ae98 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 >inVtabMethod =
1ae99 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 0;. sqlite3Db
1ae9a 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 Free(db, p->zErr
1ae9b 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 Msg);. p->zEr
1ae9c 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 rMsg = pVtab->zE
1ae9d 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62 rrMsg;. pVtab
1ae9e 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1ae9f 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e sqlite3VtabUn
1aea0 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b lock(db, pVtab);
1aea1 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1aea2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1aea3 72 65 73 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 res = pModule->x
1aea4 45 6f 66 28 70 56 74 61 62 43 75 72 73 6f 72 29 Eof(pVtabCursor)
1aea5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1aea6 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1aea7 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1aea8 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1aea9 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a . if( res ){.
1aeaa 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1aeab 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 p2 - 1;. }.
1aeac 7d 0a 20 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f }. pCur->nullRo
1aead 77 20 3d 20 30 3b 0a 0a 20 20 62 72 65 61 6b 3b w = 0;.. break;
1aeae 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1aeaf 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1aeb0 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 TABLE */..#ifnde
1aeb1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1aeb2 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 RTUALTABLE./* Op
1aeb3 63 6f 64 65 3a 20 56 52 6f 77 69 64 20 50 31 20 code: VRowid P1
1aeb4 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 P2 * * *.**.** S
1aeb5 74 6f 72 65 20 69 6e 74 6f 20 72 65 67 69 73 74 tore into regist
1aeb6 65 72 20 50 32 20 20 74 68 65 20 72 6f 77 69 64 er P2 the rowid
1aeb7 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 of.** the virtu
1aeb8 61 6c 2d 74 61 62 6c 65 20 74 68 61 74 20 74 68 al-table that th
1aeb9 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 70 e P1 cursor is p
1aeba 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2f 0a 63 ointing to..*/.c
1aebb 61 73 65 20 4f 50 5f 56 52 6f 77 69 64 3a 20 7b ase OP_VRowid: {
1aebc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1aebd 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1aebe 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 */. sqlite3_vta
1aebf 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 b *pVtab;. cons
1aec0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
1aec1 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 73 71 6c *pModule;. sql
1aec2 69 74 65 5f 69 6e 74 36 34 20 69 52 6f 77 3b 0a ite_int64 iRow;.
1aec3 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1aec4 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f ur = p->apCsr[pO
1aec5 70 2d 3e 70 31 5d 3b 0a 0a 20 20 61 73 73 65 72 p->p1];.. asser
1aec6 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 t( pCur->pVtabCu
1aec7 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 70 43 rsor );. if( pC
1aec8 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 ur->nullRow ){.
1aec9 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 break;. }.
1aeca 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 pVtab = pCur->pV
1aecb 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1aecc 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 ;. pModule = pV
1aecd 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 tab->pModule;.
1aece 61 73 73 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d assert( pModule-
1aecf 3e 78 52 6f 77 69 64 20 29 3b 0a 20 20 69 66 28 >xRowid );. if(
1aed0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1aed1 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1aed2 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1aed3 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 ;. rc = pModule
1aed4 2d 3e 78 52 6f 77 69 64 28 70 43 75 72 2d 3e 70 ->xRowid(pCur->p
1aed5 56 74 61 62 43 75 72 73 6f 72 2c 20 26 69 52 6f VtabCursor, &iRo
1aed6 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 w);. sqlite3DbF
1aed7 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1aed8 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 sg);. p->zErrMs
1aed9 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d g = pVtab->zErrM
1aeda 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 sg;. pVtab->zEr
1aedb 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 rMsg = 0;. if(
1aedc 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1aedd 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1aede 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1aedf 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1aee0 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1aee1 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 69 . pOut->u.i = i
1aee2 52 6f 77 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a Row;. break;.}.
1aee3 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1aee4 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1aee5 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 LE */..#ifndef S
1aee6 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1aee7 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 ALTABLE./* Opcod
1aee8 65 3a 20 56 43 6f 6c 75 6d 6e 20 50 31 20 50 32 e: VColumn P1 P2
1aee9 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 P3 * *.**.** St
1aeea 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ore the value of
1aeeb 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d the P2-th colum
1aeec 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20 n of.** the row
1aeed 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 of the virtual-t
1aeee 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 0a 2a able that the .*
1aeef 2a 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 70 * P1 cursor is p
1aef0 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 74 6f 20 ointing to into
1aef1 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a register P3..*/.
1aef2 63 61 73 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a case OP_VColumn:
1aef3 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 {. sqlite3_vta
1aef4 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 b *pVtab;. cons
1aef5 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
1aef6 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d *pModule;. Mem
1aef7 20 2a 70 44 65 73 74 3b 0a 20 20 73 71 6c 69 74 *pDest;. sqlit
1aef8 65 33 5f 63 6f 6e 74 65 78 74 20 73 43 6f 6e 74 e3_context sCont
1aef9 65 78 74 3b 0a 0a 20 20 56 64 62 65 43 75 72 73 ext;.. VdbeCurs
1aefa 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 or *pCur = p->ap
1aefb 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1aefc 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56 assert( pCur->pV
1aefd 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 tabCursor );. a
1aefe 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1aeff 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e && pOp->p3<=p->
1af00 6e 4d 65 6d 20 29 3b 0a 20 20 70 44 65 73 74 20 nMem );. pDest
1af01 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1af02 70 33 5d 3b 0a 20 20 69 66 28 20 70 43 75 72 2d p3];. if( pCur-
1af03 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 >nullRow ){.
1af04 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1af05 74 4e 75 6c 6c 28 70 44 65 73 74 29 3b 0a 20 20 tNull(pDest);.
1af06 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 break;. }. p
1af07 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 74 Vtab = pCur->pVt
1af08 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b abCursor->pVtab;
1af09 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74 . pModule = pVt
1af0a 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 ab->pModule;. a
1af0b 73 73 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d 3e ssert( pModule->
1af0c 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d xColumn );. mem
1af0d 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c 20 30 set(&sContext, 0
1af0e 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74 65 78 , sizeof(sContex
1af0f 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f t));.. /* The o
1af10 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61 utput cell may a
1af11 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62 75 lready have a bu
1af12 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20 ffer allocated.
1af13 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 Move. ** the cu
1af14 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 74 rrent contents t
1af15 6f 20 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 o sContext.s so
1af16 69 6e 20 63 61 73 65 20 74 68 65 20 75 73 65 72 in case the user
1af17 2d 66 75 6e 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 -function . **
1af18 63 61 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65 can use the alre
1af19 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 ady allocated bu
1af1a 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 ffer instead of
1af1b 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 0a 20 20 allocating a .
1af1c 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f ** new one.. */
1af1d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1af1e 6d 4d 6f 76 65 28 26 73 43 6f 6e 74 65 78 74 2e mMove(&sContext.
1af1f 73 2c 20 70 44 65 73 74 29 3b 0a 20 20 4d 65 6d s, pDest);. Mem
1af20 53 65 74 54 79 70 65 46 6c 61 67 28 26 73 43 6f SetTypeFlag(&sCo
1af21 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c ntext.s, MEM_Nul
1af22 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 l);.. if( sqlit
1af23 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 e3SafetyOff(db)
1af24 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1af25 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 _to_misuse;. rc
1af26 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c = pModule->xCol
1af27 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 43 umn(pCur->pVtabC
1af28 75 72 73 6f 72 2c 20 26 73 43 6f 6e 74 65 78 74 ursor, &sContext
1af29 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 73 71 , pOp->p2);. sq
1af2a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1af2b 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 p->zErrMsg);. p
1af2c 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 ->zErrMsg = pVta
1af2d 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 b->zErrMsg;. pV
1af2e 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 tab->zErrMsg = 0
1af2f 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 ;.. /* Copy the
1af30 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66 result of the f
1af31 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 50 unction to the P
1af32 33 20 72 65 67 69 73 74 65 72 2e 20 57 65 0a 20 3 register. We.
1af33 20 2a 2a 20 64 6f 20 74 68 69 73 20 72 65 67 61 ** do this rega
1af34 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 rdless of whethe
1af35 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f r or not an erro
1af36 72 20 6f 63 63 75 72 65 64 20 74 6f 20 65 6e 73 r occured to ens
1af37 75 72 65 20 61 6e 79 0a 20 20 2a 2a 20 64 79 6e ure any. ** dyn
1af38 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 amic allocation
1af39 69 6e 20 73 43 6f 6e 74 65 78 74 2e 73 20 28 61 in sContext.s (a
1af3a 20 4d 65 6d 20 73 74 72 75 63 74 29 20 69 73 20 Mem struct) is
1af3b 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a released.. */.
1af3c 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1af3d 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 73 43 6f ngeEncoding(&sCo
1af3e 6e 74 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e ntext.s, encodin
1af3f 67 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 g);. REGISTER_T
1af40 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44 RACE(pOp->p3, pD
1af41 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 est);. sqlite3V
1af42 64 62 65 4d 65 6d 4d 6f 76 65 28 70 44 65 73 74 dbeMemMove(pDest
1af43 2c 20 26 73 43 6f 6e 74 65 78 74 2e 73 29 3b 0a , &sContext.s);.
1af44 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1af45 42 53 49 5a 45 28 70 44 65 73 74 29 3b 0a 0a 20 BSIZE(pDest);..
1af46 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1af47 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 tyOn(db) ){.
1af48 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1af49 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 o_misuse;. }.
1af4a 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
1af4b 65 6d 54 6f 6f 42 69 67 28 70 44 65 73 74 29 20 emTooBig(pDest)
1af4c 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f ){. goto too_
1af4d 62 69 67 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b big;. }. break
1af4e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1af4f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1af50 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 LTABLE */..#ifnd
1af51 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1af52 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f IRTUALTABLE./* O
1af53 70 63 6f 64 65 3a 20 56 4e 65 78 74 20 50 31 20 pcode: VNext P1
1af54 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 P2 * * *.**.** A
1af55 64 76 61 6e 63 65 20 76 69 72 74 75 61 6c 20 74 dvance virtual t
1af56 61 62 6c 65 20 50 31 20 74 6f 20 74 68 65 20 6e able P1 to the n
1af57 65 78 74 20 72 6f 77 20 69 6e 20 69 74 73 20 72 ext row in its r
1af58 65 73 75 6c 74 20 73 65 74 20 61 6e 64 0a 2a 2a esult set and.**
1af59 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 jump to instruc
1af5a 74 69 6f 6e 20 50 32 2e 20 20 4f 72 2c 20 69 66 tion P2. Or, if
1af5b 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
1af5c 6c 65 20 68 61 73 20 72 65 61 63 68 65 64 0a 2a le has reached.*
1af5d 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 69 74 73 * the end of its
1af5e 20 72 65 73 75 6c 74 20 73 65 74 2c 20 74 68 65 result set, the
1af5f 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 n fall through t
1af60 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 o the next instr
1af61 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 uction..*/.case
1af62 4f 50 5f 56 4e 65 78 74 3a 20 7b 20 20 20 2f 2a OP_VNext: { /*
1af63 20 6a 75 6d 70 20 2a 2f 0a 20 20 73 71 6c 69 74 jump */. sqlit
1af64 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1af65 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
1af66 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
1af67 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a . int res = 0;.
1af68 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1af69 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 Cur = p->apCsr[p
1af6a 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1af6b 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 t( pCur->pVtabCu
1af6c 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 70 43 rsor );. if( pC
1af6d 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 ur->nullRow ){.
1af6e 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 break;. }.
1af6f 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 pVtab = pCur->pV
1af70 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1af71 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 ;. pModule = pV
1af72 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 tab->pModule;.
1af73 61 73 73 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d assert( pModule-
1af74 3e 78 4e 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 >xNext );.. /*
1af75 49 6e 76 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 Invoke the xNext
1af76 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 () method of the
1af77 20 6d 6f 64 75 6c 65 2e 20 54 68 65 72 65 20 69 module. There i
1af78 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 74 68 65 s no way for the
1af79 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 . ** underlying
1af7a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
1af7b 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 to return an err
1af7c 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 or if one occurs
1af7d 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 during. ** xNe
1af7e 78 74 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 xt(). Instead, i
1af7f 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
1af80 73 2c 20 74 72 75 65 20 69 73 20 72 65 74 75 72 s, true is retur
1af81 6e 65 64 20 28 69 6e 64 69 63 61 74 69 6e 67 20 ned (indicating
1af82 74 68 61 74 20 0a 20 20 2a 2a 20 64 61 74 61 20 that . ** data
1af83 69 73 20 61 76 61 69 6c 61 62 6c 65 29 20 61 6e is available) an
1af84 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 d the error code
1af85 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 78 returned when x
1af86 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 Column or. ** s
1af87 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 ome other method
1af88 20 69 73 20 6e 65 78 74 20 69 6e 76 6f 6b 65 64 is next invoked
1af89 20 6f 6e 20 74 68 65 20 73 61 76 65 20 76 69 72 on the save vir
1af8a 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73 6f tual table curso
1af8b 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 r.. */. if( sq
1af8c 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1af8d 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1af8e 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1af8f 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b sqlite3VtabLock
1af90 28 70 56 74 61 62 29 3b 0a 20 20 70 2d 3e 69 6e (pVtab);. p->in
1af91 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a VtabMethod = 1;.
1af92 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e rc = pModule->
1af93 78 4e 65 78 74 28 70 43 75 72 2d 3e 70 56 74 61 xNext(pCur->pVta
1af94 62 43 75 72 73 6f 72 29 3b 0a 20 20 70 2d 3e 69 bCursor);. p->i
1af95 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b nVtabMethod = 0;
1af96 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1af97 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1af98 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d ;. p->zErrMsg =
1af99 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b pVtab->zErrMsg;
1af9a 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 . pVtab->zErrMs
1af9b 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 g = 0;. sqlite3
1af9c 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 VtabUnlock(db, p
1af9d 56 74 61 62 29 3b 0a 20 20 69 66 28 20 72 63 3d Vtab);. if( rc=
1af9e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1af9f 20 20 72 65 73 20 3d 20 70 4d 6f 64 75 6c 65 2d res = pModule-
1afa0 3e 78 45 6f 66 28 70 43 75 72 2d 3e 70 56 74 61 >xEof(pCur->pVta
1afa1 62 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 bCursor);. }.
1afa2 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1afa3 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 yOn(db) ) goto a
1afa4 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1afa5 73 65 3b 0a 0a 20 20 69 66 28 20 21 72 65 73 20 se;.. if( !res
1afa6 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ){. /* If the
1afa7 72 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d 70 re is data, jump
1afa8 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 70 63 to P2 */. pc
1afa9 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1afaa 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1afab 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1afac 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1afad 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
1afae 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1afaf 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 LTABLE./* Opcode
1afb0 3a 20 56 52 65 6e 61 6d 65 20 50 31 20 2a 20 2a : VRename P1 * *
1afb1 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 P4 *.**.** P4 i
1afb2 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1afb3 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f virtual table o
1afb4 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 bject, an sqlite
1afb5 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 3_vtab structure
1afb6 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 ..** This opcode
1afb7 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 invokes the cor
1afb8 72 65 73 70 6f 6e 64 69 6e 67 20 78 52 65 6e 61 responding xRena
1afb9 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 76 me method. The v
1afba 61 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 67 69 73 alue.** in regis
1afbb 74 65 72 20 50 31 20 69 73 20 70 61 73 73 65 64 ter P1 is passed
1afbc 20 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61 72 as the zName ar
1afbd 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78 52 gument to the xR
1afbe 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f ename method..*/
1afbf 0a 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d 65 .case OP_VRename
1afc0 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 : {. sqlite3_vt
1afc1 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d ab *pVtab = pOp-
1afc2 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 4d 65 6d >p4.pVtab;. Mem
1afc3 20 2a 70 4e 61 6d 65 20 3d 20 26 70 2d 3e 61 4d *pName = &p->aM
1afc4 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 em[pOp->p1];. a
1afc5 73 73 65 72 74 28 20 70 56 74 61 62 2d 3e 70 4d ssert( pVtab->pM
1afc6 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 odule->xRename )
1afc7 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 ;. REGISTER_TRA
1afc8 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 4e 61 6d CE(pOp->p1, pNam
1afc9 65 29 3b 0a 0a 20 20 53 74 72 69 6e 67 69 66 79 e);.. Stringify
1afca 28 70 4e 61 6d 65 2c 20 65 6e 63 6f 64 69 6e 67 (pName, encoding
1afcb 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 );.. if( sqlite
1afcc 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1afcd 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1afce 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 73 71 6c to_misuse;. sql
1afcf 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 56 74 ite3VtabLock(pVt
1afd0 61 62 29 3b 0a 20 20 72 63 20 3d 20 70 56 74 61 ab);. rc = pVta
1afd1 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e b->pModule->xRen
1afd2 61 6d 65 28 70 56 74 61 62 2c 20 70 4e 61 6d 65 ame(pVtab, pName
1afd3 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 ->z);. sqlite3D
1afd4 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
1afd5 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 rMsg);. p->zErr
1afd6 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 Msg = pVtab->zEr
1afd7 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a rMsg;. pVtab->z
1afd8 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71 ErrMsg = 0;. sq
1afd9 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 lite3VtabUnlock(
1afda 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 69 66 db, pVtab);. if
1afdb 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1afdc 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f n(db) ) goto abo
1afdd 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1afde 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 ;.. break;.}.#e
1afdf 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
1afe0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1afe1 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 LTABLE./* Opcode
1afe2 3a 20 56 55 70 64 61 74 65 20 50 31 20 50 32 20 : VUpdate P1 P2
1afe3 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 P3 P4 *.**.** P4
1afe4 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1afe5 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
1afe6 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 object, an sqli
1afe7 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 te3_vtab structu
1afe8 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f re..** This opco
1afe9 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 de invokes the c
1afea 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 55 70 orresponding xUp
1afeb 64 61 74 65 20 6d 65 74 68 6f 64 2e 20 50 32 20 date method. P2
1afec 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 63 6f values.** are co
1afed 6e 74 69 67 75 6f 75 73 20 6d 65 6d 6f 72 79 20 ntiguous memory
1afee 63 65 6c 6c 73 20 73 74 61 72 74 69 6e 67 20 61 cells starting a
1afef 74 20 50 33 20 74 6f 20 70 61 73 73 20 74 6f 20 t P3 to pass to
1aff0 74 68 65 20 78 55 70 64 61 74 65 20 0a 2a 2a 20 the xUpdate .**
1aff1 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 invocation. The
1aff2 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1aff3 72 20 28 50 33 2b 50 32 2d 31 29 20 63 6f 72 72 r (P3+P2-1) corr
1aff4 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a esponds to the .
1aff5 2a 2a 20 70 32 74 68 20 65 6c 65 6d 65 6e 74 20 ** p2th element
1aff6 6f 66 20 74 68 65 20 61 72 67 76 20 61 72 72 61 of the argv arra
1aff7 79 20 70 61 73 73 65 64 20 74 6f 20 78 55 70 64 y passed to xUpd
1aff8 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ate..**.** The x
1aff9 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20 77 69 Update method wi
1affa 6c 6c 20 64 6f 20 61 20 44 45 4c 45 54 45 20 6f ll do a DELETE o
1affb 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20 62 r an INSERT or b
1affc 6f 74 68 2e 0a 2a 2a 20 54 68 65 20 61 72 67 76 oth..** The argv
1affd 5b 30 5d 20 65 6c 65 6d 65 6e 74 20 28 77 68 69 [0] element (whi
1affe 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 ch corresponds t
1afff 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 33 o memory cell P3
1b000 29 0a 2a 2a 20 69 73 20 74 68 65 20 72 6f 77 69 ).** is the rowi
1b001 64 20 6f 66 20 61 20 72 6f 77 20 74 6f 20 64 65 d of a row to de
1b002 6c 65 74 65 2e 20 20 49 66 20 61 72 67 76 5b 30 lete. If argv[0
1b003 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 6e ] is NULL then n
1b004 6f 20 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 6f o .** deletion o
1b005 63 63 75 72 73 2e 20 20 54 68 65 20 61 72 67 76 ccurs. The argv
1b006 5b 31 5d 20 65 6c 65 6d 65 6e 74 20 69 73 20 74 [1] element is t
1b007 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 he rowid of the
1b008 6e 65 77 20 0a 2a 2a 20 72 6f 77 2e 20 20 54 68 new .** row. Th
1b009 69 73 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 74 is can be NULL t
1b00a 6f 20 68 61 76 65 20 74 68 65 20 76 69 72 74 75 o have the virtu
1b00b 61 6c 20 74 61 62 6c 65 20 73 65 6c 65 63 74 20 al table select
1b00c 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 69 the new .** rowi
1b00d 64 20 66 6f 72 20 69 74 73 65 6c 66 2e 20 20 54 d for itself. T
1b00e 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 65 6c he subsequent el
1b00f 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 72 ements in the ar
1b010 72 61 79 20 61 72 65 20 0a 2a 2a 20 74 68 65 20 ray are .** the
1b011 76 61 6c 75 65 73 20 6f 66 20 63 6f 6c 75 6d 6e values of column
1b012 73 20 69 6e 20 74 68 65 20 6e 65 77 20 72 6f 77 s in the new row
1b013 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d 31 ..**.** If P2==1
1b014 20 74 68 65 6e 20 6e 6f 20 69 6e 73 65 72 74 20 then no insert
1b015 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 61 is performed. a
1b016 72 67 76 5b 30 5d 20 69 73 20 74 68 65 20 72 6f rgv[0] is the ro
1b017 77 69 64 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 wid of.** a row
1b018 74 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a to delete..**.**
1b019 20 50 31 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e P1 is a boolean
1b01a 20 66 6c 61 67 2e 20 49 66 20 69 74 20 69 73 20 flag. If it is
1b01b 73 65 74 20 74 6f 20 74 72 75 65 20 61 6e 64 20 set to true and
1b01c 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c 6c the xUpdate call
1b01d 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66 75 .** is successfu
1b01e 6c 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 l, then the valu
1b01f 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 e returned by sq
1b020 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 lite3_last_inser
1b021 74 5f 72 6f 77 69 64 28 29 20 0a 2a 2a 20 69 73 t_rowid() .** is
1b022 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 set to the valu
1b023 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66 e of the rowid f
1b024 6f 72 20 74 68 65 20 72 6f 77 20 6a 75 73 74 20 or the row just
1b025 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 63 61 73 inserted..*/.cas
1b026 65 20 4f 50 5f 56 55 70 64 61 74 65 3a 20 7b 0a e OP_VUpdate: {.
1b027 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1b028 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e pVtab = pOp->p4.
1b029 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 pVtab;. sqlite3
1b02a 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 _module *pModule
1b02b 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 = (sqlite3_modu
1b02c 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 le *)pVtab->pMod
1b02d 75 6c 65 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20 ule;. int nArg
1b02e 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 = pOp->p2;. ass
1b02f 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1b030 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a 20 20 69 ==P4_VTAB );. i
1b031 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 f( pModule->xUpd
1b032 61 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 ate==0 ){. sq
1b033 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1b034 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1b035 22 72 65 61 64 2d 6f 6e 6c 79 20 74 61 62 6c 65 "read-only table
1b036 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ");. rc = SQL
1b037 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c ITE_ERROR;. }el
1b038 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 se{. int i;.
1b039 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 sqlite_int64
1b03a 72 6f 77 69 64 3b 0a 20 20 20 20 4d 65 6d 20 2a rowid;. Mem *
1b03b 2a 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 *apArg = p->apAr
1b03c 67 3b 0a 20 20 20 20 4d 65 6d 20 2a 70 58 20 3d g;. Mem *pX =
1b03d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1b03e 33 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 3];. for(i=0;
1b03f 20 69 3c 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 i<nArg; i++){.
1b040 20 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e storeTypeIn
1b041 66 6f 28 70 58 2c 20 30 29 3b 0a 20 20 20 20 20 fo(pX, 0);.
1b042 20 61 70 41 72 67 5b 69 5d 20 3d 20 70 58 3b 0a apArg[i] = pX;.
1b043 20 20 20 20 20 20 70 58 2b 2b 3b 0a 20 20 20 20 pX++;.
1b044 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1b045 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1b046 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1b047 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 73 to_misuse;. s
1b048 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 qlite3VtabLock(p
1b049 56 74 61 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 Vtab);. rc =
1b04a 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 pModule->xUpdate
1b04b 28 70 56 74 61 62 2c 20 6e 41 72 67 2c 20 61 70 (pVtab, nArg, ap
1b04c 41 72 67 2c 20 26 72 6f 77 69 64 29 3b 0a 20 20 Arg, &rowid);.
1b04d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1b04e 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b db, p->zErrMsg);
1b04f 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 . p->zErrMsg
1b050 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 = pVtab->zErrMsg
1b051 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 ;. pVtab->zEr
1b052 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 73 71 rMsg = 0;. sq
1b053 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 lite3VtabUnlock(
1b054 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20 db, pVtab);.
1b055 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1b056 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 yOn(db) ) goto a
1b057 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1b058 73 65 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d se;. if( pOp-
1b059 3e 70 31 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 >p1 && rc==SQLIT
1b05a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 E_OK ){. as
1b05b 73 65 72 74 28 20 6e 41 72 67 3e 31 20 26 26 20 sert( nArg>1 &&
1b05c 61 70 41 72 67 5b 30 5d 20 26 26 20 28 61 70 41 apArg[0] && (apA
1b05d 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d 45 4d rg[0]->flags&MEM
1b05e 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 20 20 _Null) );.
1b05f 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 db->lastRowid =
1b060 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 rowid;. }.
1b061 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 p->nChange++;.
1b062 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1b063 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1b064 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1b065 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 20 53 51 */..#ifndef SQ
1b066 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f LITE_OMIT_PAGER_
1b067 50 52 41 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64 PRAGMAS./* Opcod
1b068 65 3a 20 50 61 67 65 63 6f 75 6e 74 20 50 31 20 e: Pagecount P1
1b069 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 P2 * * *.**.** W
1b06a 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 rite the current
1b06b 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1b06c 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 20 in database P1
1b06d 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 to memory cell P
1b06e 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 2..*/.case OP_Pa
1b06f 67 65 63 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 gecount: {
1b070 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1b071 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e erelease */. in
1b072 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 t p1 = pOp->p1;
1b073 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 . int nPage;.
1b074 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
1b075 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
1b076 72 28 64 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 r(db->aDb[p1].pB
1b077 74 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 t);.. rc = sqli
1b078 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
1b079 74 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 t(pPager, &nPage
1b07a 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1b07b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 4f ITE_OK ){. pO
1b07c 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ut->flags = MEM_
1b07d 49 6e 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 Int;. pOut->u
1b07e 2e 69 20 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a .i = nPage;. }.
1b07f 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1b080 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
1b081 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 20 E_OMIT_TRACE./*
1b082 4f 70 63 6f 64 65 3a 20 54 72 61 63 65 20 2a 20 Opcode: Trace *
1b083 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 * * P4 *.**.** I
1b084 66 20 74 72 61 63 69 6e 67 20 69 73 20 65 6e 61 f tracing is ena
1b085 62 6c 65 64 20 28 62 79 20 74 68 65 20 73 71 6c bled (by the sql
1b086 69 74 65 33 5f 74 72 61 63 65 28 29 29 20 69 6e ite3_trace()) in
1b087 74 65 72 66 61 63 65 2c 20 74 68 65 6e 0a 2a 2a terface, then.**
1b088 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e the UTF-8 strin
1b089 67 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 50 g contained in P
1b08a 34 20 69 73 20 65 6d 69 74 74 65 64 20 6f 6e 20 4 is emitted on
1b08b 74 68 65 20 74 72 61 63 65 20 63 61 6c 6c 62 61 the trace callba
1b08c 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 ck..*/.case OP_T
1b08d 72 61 63 65 3a 20 7b 0a 20 20 69 66 28 20 70 4f race: {. if( pO
1b08e 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 69 p->p4.z ){. i
1b08f 66 28 20 64 62 2d 3e 78 54 72 61 63 65 20 29 7b f( db->xTrace ){
1b090 0a 20 20 20 20 20 20 64 62 2d 3e 78 54 72 61 63 . db->xTrac
1b091 65 28 64 62 2d 3e 70 54 72 61 63 65 41 72 67 2c e(db->pTraceArg,
1b092 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 pOp->p4.z);.
1b093 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
1b094 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 28 _DEBUG. if( (
1b095 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 db->flags & SQLI
1b096 54 45 5f 53 71 6c 54 72 61 63 65 29 21 3d 30 20 TE_SqlTrace)!=0
1b097 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1b098 44 65 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c DebugPrintf("SQL
1b099 2d 74 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 70 -trace: %s\n", p
1b09a 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d Op->p4.z);. }
1b09b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1b09c 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 E_DEBUG */. }.
1b09d 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1b09e 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f .../* Opcode: No
1b09f 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a op * * * * *.**.
1b0a0 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 ** Do nothing.
1b0a1 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1b0a2 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c is often useful
1b0a3 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 as a jump.** de
1b0a4 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a stination..*/./*
1b0a5 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78 .** The magic Ex
1b0a6 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65 plain opcode are
1b0a7 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77 only inserted w
1b0a8 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28 hen explain==2 (
1b0a9 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73 which.** is to s
1b0aa 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c ay when the EXPL
1b0ab 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73 AIN QUERY PLAN s
1b0ac 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a yntax is used.).
1b0ad 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 ** This opcode r
1b0ae 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69 ecords informati
1b0af 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69 on from the opti
1b0b0 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68 mizer. It is th
1b0b1 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 e.** the same as
1b0b2 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 a no-op. This
1b0b3 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70 opcodesnever app
1b0b4 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56 ears in a real V
1b0b5 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 M program..*/.de
1b0b6 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 fault: {
1b0b7 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61 /* This is rea
1b0b8 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 lly OP_Noop and
1b0b9 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 OP_Explain */.
1b0ba 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a break;.}../*****
1b0bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0bf 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 ********.** The
1b0c0 63 61 73 65 73 20 6f 66 20 74 68 65 20 73 77 69 cases of the swi
1b0c1 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62 tch statement ab
1b0c2 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68 ove this line sh
1b0c3 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65 ould all be inde
1b0c4 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61 nted.** by 6 spa
1b0c5 63 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65 ces. But the le
1b0c6 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73 ft-most 6 spaces
1b0c7 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 have been remov
1b0c8 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68 ed to improve th
1b0c9 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79 e.** readability
1b0ca 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69 . From this poi
1b0cb 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20 nt on down, the
1b0cc 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 normal indentati
1b0cd 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20 on rules are.**
1b0ce 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a restored..******
1b0cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0d3 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a *******/. }..
1b0d4 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1b0d5 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 ILE. {.
1b0d6 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71 u64 elapsed = sq
1b0d7 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20 lite3Hwtime() -
1b0d8 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 start;. pOp
1b0d9 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 ->cycles += elap
1b0da 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e sed;. pOp->
1b0db 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 cnt++;.#if 0.
1b0dc 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
1b0dd 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 out, "%10llu ",
1b0de 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20 elapsed);.
1b0df 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1b0e0 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69 ntOp(stdout, ori
1b0e1 67 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72 69 gPc, &p->aOp[ori
1b0e2 67 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 gPc]);.#endif.
1b0e3 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
1b0e4 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 /* The following
1b0e5 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74 68 69 code adds nothi
1b0e6 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c ng to the actual
1b0e7 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20 functionality.
1b0e8 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f ** of the pro
1b0e9 67 72 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c gram. It is onl
1b0ea 79 20 68 65 72 65 20 66 6f 72 20 74 65 73 74 69 y here for testi
1b0eb 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 ng and debugging
1b0ec 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 .. ** On the
1b0ed 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 74 20 64 other hand, it d
1b0ee 6f 65 73 20 62 75 72 6e 20 43 50 55 20 63 79 63 oes burn CPU cyc
1b0ef 6c 65 73 20 65 76 65 72 79 20 74 69 6d 65 20 74 les every time t
1b0f0 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 68 hrough. ** th
1b0f1 65 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70 e evaluator loop
1b0f2 2e 20 20 53 6f 20 77 65 20 63 61 6e 20 6c 65 61 . So we can lea
1b0f3 76 65 20 69 74 20 6f 75 74 20 77 68 65 6e 20 4e ve it out when N
1b0f4 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 DEBUG is defined
1b0f5 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 .. */.#ifndef
1b0f6 20 4e 44 45 42 55 47 0a 20 20 20 20 61 73 73 65 NDEBUG. asse
1b0f7 72 74 28 20 70 63 3e 3d 2d 31 20 26 26 20 70 63 rt( pc>=-1 && pc
1b0f8 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64 <p->nOp );..#ifd
1b0f9 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1b0fa 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 if( p->trace
1b0fb 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 ){. if( rc
1b0fc 21 3d 30 20 29 20 66 70 72 69 6e 74 66 28 70 2d !=0 ) fprintf(p-
1b0fd 3e 74 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22 >trace,"rc=%d\n"
1b0fe 2c 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ,rc);. if(
1b0ff 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
1b100 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 LG_OUT2_PRERELEA
1b101 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 SE ){. re
1b102 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 gisterTrace(p->t
1b103 72 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 race, pOp->p2, p
1b104 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Out);. }.
1b105 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 if( opProper
1b106 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 20 ty & OPFLG_OUT3
1b107 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 73 ){. regis
1b108 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 terTrace(p->trac
1b109 65 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 e, pOp->p3, pOut
1b10a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1b10b 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 .#endif /* SQLI
1b10c 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 TE_DEBUG */.#end
1b10d 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f if /* NDEBUG */
1b10e 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 . } /* The end
1b10f 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 of the for(;;)
1b110 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 loop the loops t
1b111 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a hrough opcodes *
1b112 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 /.. /* If we re
1b113 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 ach this point,
1b114 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 it means that ex
1b115 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 ecution is finis
1b116 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e hed with. ** an
1b117 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b error of some k
1b118 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 ind.. */.vdbe_e
1b119 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 rror_halt:. ass
1b11a 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e ert( rc );. p->
1b11b 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 rc = rc;. sqlit
1b11c 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 e3VdbeHalt(p);.
1b11d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1b11e 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 IOERR_NOMEM ) db
1b11f 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
1b120 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 1;. rc = SQLIT
1b121 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 E_ERROR;.. /* T
1b122 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 his is the only
1b123 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20 way out of this
1b124 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68 procedure. We h
1b125 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 ave to. ** rele
1b126 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 20 ase the mutexes
1b127 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 77 on btrees that w
1b128 65 72 65 20 61 63 71 75 69 72 65 64 20 61 74 20 ere acquired at
1b129 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f the. ** top. */
1b12a 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 .vdbe_return:.
1b12b 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
1b12c 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e xArrayLeave(&p->
1b12d 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 aMutex);. retur
1b12e 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 n rc;.. /* Jump
1b12f 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 74 to here if a st
1b130 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72 ring or blob lar
1b131 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f ger than SQLITE_
1b132 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 MAX_LENGTH. **
1b133 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a is encountered..
1b134 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 */.too_big:.
1b135 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 sqlite3SetString
1b136 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 (&p->zErrMsg, db
1b137 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f , "string or blo
1b138 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72 b too big");. r
1b139 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 c = SQLITE_TOOBI
1b13a 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 G;. goto vdbe_e
1b13b 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a rror_halt;.. /*
1b13c 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 Jump to here if
1b13d 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c a malloc() fail
1b13e 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a s.. */.no_mem:.
1b13f 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
1b140 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 ed = 1;. sqlite
1b141 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1b142 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74 ErrMsg, db, "out
1b143 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 of memory");.
1b144 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
1b145 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 M;. goto vdbe_e
1b146 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a rror_halt;.. /*
1b147 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f Jump to here fo
1b148 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 r an SQLITE_MISU
1b149 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 SE error.. */.a
1b14a 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1b14b 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 se:. rc = SQLIT
1b14c 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 E_MISUSE;. /* F
1b14d 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62 all thru into ab
1b14e 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1b14f 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 */.. /* Jump t
1b150 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f o here for any o
1b151 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 ther kind of fat
1b152 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 al error. The "
1b153 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a rc" variable. *
1b154 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 * should hold th
1b155 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a e error number..
1b156 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 */.abort_due_t
1b157 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 o_error:. asser
1b158 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 t( p->zErrMsg==0
1b159 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 );. if( db->ma
1b15a 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 llocFailed ) rc
1b15b 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1b15c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1b15d 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a _IOERR_NOMEM ){.
1b15e 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
1b15f 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1b160 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 , db, "%s", sqli
1b161 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a te3ErrStr(rc));.
1b162 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f }. goto vdbe_
1b163 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f error_halt;.. /
1b164 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 * Jump to here i
1b165 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e f the sqlite3_in
1b166 74 65 72 72 75 70 74 28 29 20 41 50 49 20 73 65 terrupt() API se
1b167 74 73 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 ts the interrupt
1b168 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f . ** flag.. */
1b169 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e .abort_due_to_in
1b16a 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72 terrupt:. asser
1b16b 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 t( db->u1.isInte
1b16c 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20 rrupted );. rc
1b16d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 = SQLITE_INTERRU
1b16e 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 PT;. p->rc = rc
1b16f 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 ;. sqlite3SetSt
1b170 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1b171 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 , db, "%s", sqli
1b172 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a te3ErrStr(rc));.
1b173 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f goto vdbe_erro
1b174 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a r_halt;.}../****
1b175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1b176 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a f vdbe.c *******
1b177 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b178 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b179 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
1b17a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
1b17b 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 file vdbeblob.c
1b17c 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1b17d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b17e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
1b17f 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2007 May 1.**.*
1b180 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
1b181 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
1b182 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
1b183 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1b184 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1b185 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1b186 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1b187 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1b188 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1b189 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1b18a 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1b18b 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
1b18c 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
1b18d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
1b18e 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
1b18f 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
1b190 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
1b191 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1b192 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b193 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b194 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b195 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
1b196 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
1b197 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f ins code used to
1b198 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 implement incre
1b199 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e mental BLOB I/O.
1b19a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 .**.** $Id: vdbe
1b19b 62 6c 6f 62 2e 63 2c 76 20 31 2e 32 36 20 32 30 blob.c,v 1.26 20
1b19c 30 38 2f 31 30 2f 30 32 20 31 34 3a 34 39 3a 30 08/10/02 14:49:0
1b19d 32 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 2 danielk1977 Ex
1b19e 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 p $.*/...#ifndef
1b19f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
1b1a0 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c RBLOB../*.** Val
1b1a1 69 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a id sqlite3_blob*
1b1a2 20 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74 handles point t
1b1a3 6f 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75 63 o Incrblob struc
1b1a4 74 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 tures..*/.typede
1b1a5 66 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f f struct Incrblo
1b1a6 62 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75 b Incrblob;.stru
1b1a7 63 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20 ct Incrblob {.
1b1a8 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 int flags;
1b1a9 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 /* Copy
1b1aa 6f 66 20 22 66 6c 61 67 73 22 20 70 61 73 73 65 of "flags" passe
1b1ab 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f d to sqlite3_blo
1b1ac 62 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e b_open() */. in
1b1ad 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 t nByte;
1b1ae 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1b1af 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62 open blob, in b
1b1b0 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f ytes */. int iO
1b1b1 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
1b1b2 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 /* Byte offset
1b1b3 20 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72 73 of blob in curs
1b1b4 6f 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 or data */. BtC
1b1b5 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 ursor *pCsr;
1b1b6 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 /* Cursor p
1b1b7 6f 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 20 ointing at blob
1b1b8 72 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 row */. sqlite3
1b1b9 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 _stmt *pStmt;
1b1ba 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 6f /* Statement ho
1b1bb 6c 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 65 lding cursor ope
1b1bc 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a n */. sqlite3 *
1b1bd 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f db; /
1b1be 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 * The associated
1b1bf 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a database */.};.
1b1c0 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c ./*.** Open a bl
1b1c1 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 ob handle..*/.SQ
1b1c2 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1b1c3 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a ite3_blob_open(.
1b1c4 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 sqlite3* db,
1b1c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1b1c6 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1b1c7 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 tion */. const
1b1c8 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 char *zDb,
1b1c9 20 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68 65 /* The attache
1b1ca 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 d database conta
1b1cb 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a ining the blob *
1b1cc 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1b1cd 7a 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54 zTable, /* T
1b1ce 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e he table contain
1b1cf 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a ing the blob */.
1b1d0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 const char *zC
1b1d1 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65 olumn, /* The
1b1d2 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 column containi
1b1d3 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 ng the blob */.
1b1d4 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 sqlite_int64 iR
1b1d5 6f 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ow, /* The
1b1d6 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 row containing t
1b1d7 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74 he glob */. int
1b1d8 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
1b1d9 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20 /* True ->
1b1da 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 read/write acces
1b1db 73 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61 64 s, false -> read
1b1dc 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74 -only */. sqlit
1b1dd 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 e3_blob **ppBlob
1b1de 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 /* Handle for
1b1df 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 62 accessing the b
1b1e0 6c 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65 72 lob returned her
1b1e1 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41 e */.){. int nA
1b1e2 74 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e ttempt = 0;. in
1b1e3 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 t iCol;
1b1e4 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1b1e5 66 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77 f zColumn in row
1b1e6 2d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a -record */.. /*
1b1e7 20 54 68 69 73 20 56 44 42 45 20 70 72 6f 67 72 This VDBE progr
1b1e8 61 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65 65 am seeks a btree
1b1e9 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 cursor to the i
1b1ea 64 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a 20 dentified . **
1b1eb 64 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74 db/table/row ent
1b1ec 72 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66 ry. The reason f
1b1ed 6f 72 20 75 73 69 6e 67 20 61 20 76 64 62 65 20 or using a vdbe
1b1ee 70 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64 0a program instead.
1b1ef 20 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67 20 ** of writing
1b1f0 63 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65 20 code to use the
1b1f1 62 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69 72 b-tree layer dir
1b1f2 65 63 74 6c 79 20 69 73 20 74 68 61 74 20 74 68 ectly is that th
1b1f3 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 e. ** vdbe prog
1b1f4 72 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61 64 ram will take ad
1b1f5 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 76 vantage of the v
1b1f6 61 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74 69 arious transacti
1b1f7 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 on,. ** locking
1b1f8 20 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64 6c and error handl
1b1f9 69 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74 75 ing infrastructu
1b1fa 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 re built into th
1b1fb 65 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a e vdbe.. **. *
1b1fc 2a 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67 20 * After seeking
1b1fd 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 20 the cursor, the
1b1fe 76 64 62 65 20 65 78 65 63 75 74 65 73 20 61 6e vdbe executes an
1b1ff 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20 OP_ResultRow..
1b200 20 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e 61 ** Code externa
1b201 6c 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68 l to the Vdbe th
1b202 65 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 65 en "borrows" the
1b203 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 b-tree cursor a
1b204 6e 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 20 nd. ** uses it
1b205 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1b206 20 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c blob_read(), bl
1b207 6f 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 0a ob_write() and .
1b208 20 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28 ** blob_bytes(
1b209 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a ) functions.. *
1b20a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 *. ** The sqlit
1b20b 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20 e3_blob_close()
1b20c 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a function finaliz
1b20d 65 73 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 es the vdbe prog
1b20e 72 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20 ram,. ** which
1b20f 63 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72 65 closes the b-tre
1b210 65 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70 6f e cursor and (po
1b211 73 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20 ssibly) commits
1b212 74 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 the . ** transa
1b213 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 ction.. */. st
1b214 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f atic const VdbeO
1b215 70 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d pList openBlob[]
1b216 20 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61 = {. {OP_Tra
1b217 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 nsaction, 0, 0,
1b218 30 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53 74 0}, /* 0: St
1b219 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f art a transactio
1b21a 6e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72 n */. {OP_Ver
1b21b 69 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c ifyCookie, 0, 0,
1b21c 20 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68 0}, /* 1: Ch
1b21d 65 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20 63 eck the schema c
1b21e 6f 6f 6b 69 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a ookie */.. /*
1b21f 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c One of the foll
1b220 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 74 72 75 owing two instru
1b221 63 74 69 6f 6e 73 20 69 73 20 72 65 70 6c 61 63 ctions is replac
1b222 65 64 20 62 79 20 61 6e 0a 20 20 20 20 2a 2a 20 ed by an. **
1b223 4f 50 5f 4e 6f 6f 70 20 62 65 66 6f 72 65 20 65 OP_Noop before e
1b224 78 65 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a xection.. */.
1b225 20 20 20 20 7b 4f 50 5f 53 65 74 4e 75 6d 43 6f {OP_SetNumCo
1b226 6c 75 6d 6e 73 2c 20 30 2c 20 30 2c 20 30 7d 2c lumns, 0, 0, 0},
1b227 20 20 20 2f 2a 20 32 3a 20 4e 75 6d 20 63 6f 6c /* 2: Num col
1b228 73 20 66 6f 72 20 63 75 72 73 6f 72 20 2a 2f 0a s for cursor */.
1b229 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64 {OP_OpenRead
1b22a 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 , 0, 0, 0},
1b22b 20 20 20 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75 /* 3: Open cu
1b22c 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64 69 rsor 0 for readi
1b22d 6e 67 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 53 65 ng */. {OP_Se
1b22e 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 tNumColumns, 0,
1b22f 30 2c 20 30 7d 2c 20 20 20 2f 2a 20 34 3a 20 4e 0, 0}, /* 4: N
1b230 75 6d 20 63 6f 6c 73 20 66 6f 72 20 63 75 72 73 um cols for curs
1b231 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 or */. {OP_Op
1b232 65 6e 57 72 69 74 65 2c 20 30 2c 20 30 2c 20 30 enWrite, 0, 0, 0
1b233 7d 2c 20 20 20 20 20 20 20 2f 2a 20 35 3a 20 4f }, /* 5: O
1b234 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 pen cursor 0 for
1b235 20 72 65 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a read/write */..
1b236 20 20 20 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65 {OP_Variable
1b237 2c 20 31 2c 20 31 2c 20 30 7d 2c 20 20 20 20 20 , 1, 1, 0},
1b238 20 20 20 2f 2a 20 36 3a 20 50 75 73 68 20 74 68 /* 6: Push th
1b239 65 20 72 6f 77 69 64 20 74 6f 20 74 68 65 20 73 e rowid to the s
1b23a 74 61 63 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f tack */. {OP_
1b23b 4e 6f 74 45 78 69 73 74 73 2c 20 30 2c 20 31 30 NotExists, 0, 10
1b23c 2c 20 31 7d 2c 20 20 20 20 20 20 2f 2a 20 37 3a , 1}, /* 7:
1b23d 20 53 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 Seek the cursor
1b23e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75 */. {OP_Colu
1b23f 6d 6e 2c 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20 mn, 0, 0, 1},
1b240 20 20 20 20 20 20 20 2f 2a 20 38 20 20 2a 2f 0a /* 8 */.
1b241 20 20 20 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f {OP_ResultRo
1b242 77 2c 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20 w, 1, 0, 0},
1b243 20 20 20 2f 2a 20 39 20 20 2a 2f 0a 20 20 20 20 /* 9 */.
1b244 7b 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c {OP_Close, 0, 0,
1b245 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 0}, /
1b246 2a 20 31 30 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 * 10 */. {OP
1b247 5f 48 61 6c 74 2c 20 30 2c 20 30 2c 20 30 7d 2c _Halt, 0, 0, 0},
1b248 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
1b249 31 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 56 64 62 1 */. };.. Vdb
1b24a 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 e *v = 0;. int
1b24b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1b24c 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d char zErr[128]
1b24d 3b 0a 0a 20 20 7a 45 72 72 5b 30 5d 20 3d 20 30 ;.. zErr[0] = 0
1b24e 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
1b24f 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 x_enter(db->mute
1b250 78 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 50 x);. do {. P
1b251 61 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20 20 arse sParse;.
1b252 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a 20 Table *pTab;..
1b253 20 20 20 6d 65 6d 73 65 74 28 26 73 50 61 72 73 memset(&sPars
1b254 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 e, 0, sizeof(Par
1b255 73 65 29 29 3b 0a 20 20 20 20 73 50 61 72 73 65 se));. sParse
1b256 2e 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69 .db = db;.. i
1b257 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1b258 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 On(db) ){.
1b259 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1b25a 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ave(db->mutex);.
1b25b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1b25c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 ITE_MISUSE;.
1b25d 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 }.. sqlite3Bt
1b25e 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b reeEnterAll(db);
1b25f 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 . pTab = sqli
1b260 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 26 te3LocateTable(&
1b261 73 50 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 6c sParse, 0, zTabl
1b262 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 e, zDb);. if(
1b263 20 70 54 61 62 20 26 26 20 49 73 56 69 72 74 75 pTab && IsVirtu
1b264 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 al(pTab) ){.
1b265 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 pTab = 0;.
1b266 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1b267 67 28 26 73 50 61 72 73 65 2c 20 22 63 61 6e 6e g(&sParse, "cann
1b268 6f 74 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 ot open virtual
1b269 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 table: %s", zTab
1b26a 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 le);. }.#ifnd
1b26b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1b26c 49 45 57 0a 20 20 20 20 69 66 28 20 70 54 61 62 IEW. if( pTab
1b26d 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 && pTab->pSelec
1b26e 74 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 t ){. pTab
1b26f 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 = 0;. sqlit
1b270 65 33 45 72 72 6f 72 4d 73 67 28 26 73 50 61 72 e3ErrorMsg(&sPar
1b271 73 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e se, "cannot open
1b272 20 76 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 view: %s", zTab
1b273 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 le);. }.#endi
1b274 66 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20 f. if( !pTab
1b275 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 50 61 ){. if( sPa
1b276 72 73 65 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 rse.zErrMsg ){.
1b277 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
1b278 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
1b279 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 25 73 22 Err), zErr, "%s"
1b27a 2c 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 , sParse.zErrMsg
1b27b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1b27c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1b27d 62 2c 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 b, sParse.zErrMs
1b27e 67 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 g);. rc = S
1b27f 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1b280 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
1b281 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 SafetyOff(db);.
1b282 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1b283 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 eLeaveAll(db);.
1b284 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1b285 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a pen_out;. }..
1b286 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63 /* Now searc
1b287 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20 65 h pTab for the e
1b288 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a xact column. */.
1b289 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 for(iCol=0;
1b28a 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f iCol < pTab->nCo
1b28b 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20 l; iCol++) {.
1b28c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 if( sqlite3St
1b28d 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c rICmp(pTab->aCol
1b28e 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 [iCol].zName, zC
1b28f 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 olumn)==0 ){.
1b290 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1b291 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
1b292 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43 ( iCol==pTab->nC
1b293 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ol ){. sqli
1b294 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
1b295 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c eof(zErr), zErr,
1b296 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e "no such column
1b297 3a 20 5c 22 25 73 5c 22 22 2c 20 7a 43 6f 6c 75 : \"%s\"", zColu
1b298 6d 6e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 mn);. rc =
1b299 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1b29a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 (void)sqlite
1b29b 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 3SafetyOff(db);.
1b29c 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1b29d 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a eeLeaveAll(db);.
1b29e 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f goto blob_
1b29f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a open_out;. }.
1b2a0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 76 . /* If the v
1b2a1 61 6c 75 65 20 69 73 20 62 65 69 6e 67 20 6f 70 alue is being op
1b2a2 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 ened for writing
1b2a3 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 , check that the
1b2a4 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 . ** column i
1b2a5 73 20 6e 6f 74 20 69 6e 64 65 78 65 64 2e 20 49 s not indexed. I
1b2a6 74 20 69 73 20 61 67 61 69 6e 73 74 20 74 68 65 t is against the
1b2a7 20 72 75 6c 65 73 20 74 6f 20 6f 70 65 6e 20 61 rules to open a
1b2a8 6e 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 65 64 n. ** indexed
1b2a9 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 column for writ
1b2aa 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ing.. */.
1b2ab 69 66 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 if( flags ){.
1b2ac 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a Index *pIdx;.
1b2ad 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 for(pIdx=p
1b2ae 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 Tab->pIndex; pId
1b2af 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e x; pIdx=pIdx->pN
1b2b0 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 6e ext){. in
1b2b1 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 t j;. for
1b2b2 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 (j=0; j<pIdx->nC
1b2b3 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 olumn; j++){.
1b2b4 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d if( pIdx-
1b2b5 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69 43 >aiColumn[j]==iC
1b2b6 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ol ){.
1b2b7 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1b2b8 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c tf(sizeof(zErr),
1b2b9 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 zErr,.
1b2ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2bb 20 20 20 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e "cannot open
1b2bc 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 indexed column
1b2bd 66 6f 72 20 77 72 69 74 69 6e 67 22 29 3b 0a 20 for writing");.
1b2be 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
1b2bf 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1b2c0 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 (void)
1b2c1 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1b2c2 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (db);.
1b2c3 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1b2c4 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 aveAll(db);.
1b2c5 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f goto blo
1b2c6 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 b_open_out;.
1b2c7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1b2c8 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1b2c9 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 . v = sqlite3
1b2ca 56 64 62 65 43 72 65 61 74 65 28 64 62 29 3b 0a VdbeCreate(db);.
1b2cb 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 if( v ){.
1b2cc 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c int iDb = sql
1b2cd 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 ite3SchemaToInde
1b2ce 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 x(db, pTab->pSch
1b2cf 65 6d 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ema);. sqli
1b2d0 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 te3VdbeAddOpList
1b2d1 28 76 2c 20 73 69 7a 65 6f 66 28 6f 70 65 6e 42 (v, sizeof(openB
1b2d2 6c 6f 62 29 2f 73 69 7a 65 6f 66 28 56 64 62 65 lob)/sizeof(Vdbe
1b2d3 4f 70 4c 69 73 74 29 2c 20 6f 70 65 6e 42 6c 6f OpList), openBlo
1b2d4 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f b);.. /* Co
1b2d5 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54 nfigure the OP_T
1b2d6 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 ransaction */.
1b2d7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1b2d8 68 61 6e 67 65 50 31 28 76 2c 20 30 2c 20 69 44 hangeP1(v, 0, iD
1b2d9 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 b);. sqlite
1b2da 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 3VdbeChangeP2(v,
1b2db 20 30 2c 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 0, (flags ? 1 :
1b2dc 20 30 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 0));.. /*
1b2dd 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 Configure the OP
1b2de 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a 2f _VerifyCookie */
1b2df 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1b2e0 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 31 2c beChangeP1(v, 1,
1b2e1 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c iDb);. sql
1b2e2 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 ite3VdbeChangeP2
1b2e3 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 63 (v, 1, pTab->pSc
1b2e4 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f hema->schema_coo
1b2e5 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 kie);.. /*
1b2e6 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75 74 65 Make sure a mute
1b2e7 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 x is held on the
1b2e8 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63 table to be acc
1b2e9 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20 20 73 essed */. s
1b2ea 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 qlite3VdbeUsesBt
1b2eb 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a 0a 20 ree(v, iDb); ..
1b2ec 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 /* Remove e
1b2ed 69 74 68 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 ither the OP_Ope
1b2ee 6e 57 72 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 nWrite or OpenRe
1b2ef 61 64 2e 20 53 65 74 20 74 68 65 20 50 32 20 0a ad. Set the P2 .
1b2f0 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 ** paramet
1b2f1 65 72 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 er of the other
1b2f2 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 0a to pTab->tnum. .
1b2f3 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
1b2f4 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1b2f5 54 6f 4e 6f 6f 70 28 76 2c 20 28 66 6c 61 67 73 ToNoop(v, (flags
1b2f6 20 3f 20 33 20 3a 20 35 29 2c 20 31 29 3b 0a 20 ? 3 : 5), 1);.
1b2f7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1b2f8 43 68 61 6e 67 65 50 32 28 76 2c 20 28 66 6c 61 ChangeP2(v, (fla
1b2f9 67 73 20 3f 20 35 20 3a 20 33 29 2c 20 70 54 61 gs ? 5 : 3), pTa
1b2fa 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 b->tnum);.
1b2fb 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1b2fc 65 50 33 28 76 2c 20 28 66 6c 61 67 73 20 3f 20 eP3(v, (flags ?
1b2fd 35 20 3a 20 33 29 2c 20 69 44 62 29 3b 0a 0a 20 5 : 3), iDb);..
1b2fe 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 /* Configur
1b2ff 65 20 74 68 65 20 4f 50 5f 53 65 74 4e 75 6d 43 e the OP_SetNumC
1b300 6f 6c 75 6d 6e 73 2e 20 43 6f 6e 66 69 67 75 72 olumns. Configur
1b301 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 0a e the cursor to.
1b302 20 20 20 20 20 20 2a 2a 20 74 68 69 6e 6b 20 74 ** think t
1b303 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 68 61 hat the table ha
1b304 73 20 6f 6e 65 20 6d 6f 72 65 20 63 6f 6c 75 6d s one more colum
1b305 6e 20 74 68 61 6e 20 69 74 20 72 65 61 6c 6c 79 n than it really
1b306 0a 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 2e 20 . ** does.
1b307 41 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 An OP_Column to
1b308 72 65 74 72 69 65 76 65 20 74 68 69 73 20 69 6d retrieve this im
1b309 61 67 69 6e 61 72 79 20 63 6f 6c 75 6d 6e 20 77 aginary column w
1b30a 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6c 77 ill. ** alw
1b30b 61 79 73 20 72 65 74 75 72 6e 20 61 6e 20 53 51 ays return an SQ
1b30c 4c 20 4e 55 4c 4c 2e 20 54 68 69 73 20 69 73 20 L NULL. This is
1b30d 75 73 65 66 75 6c 20 62 65 63 61 75 73 65 20 69 useful because i
1b30e 74 20 6d 65 61 6e 73 0a 20 20 20 20 20 20 2a 2a t means. **
1b30f 20 77 65 20 63 61 6e 20 69 6e 76 6f 6b 65 20 4f we can invoke O
1b310 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c P_Column to fill
1b311 20 69 6e 20 74 68 65 20 76 64 62 65 20 63 75 72 in the vdbe cur
1b312 73 6f 72 73 20 74 79 70 65 20 0a 20 20 20 20 20 sors type .
1b313 20 2a 2a 20 61 6e 64 20 6f 66 66 73 65 74 20 63 ** and offset c
1b314 61 63 68 65 20 77 69 74 68 6f 75 74 20 63 61 75 ache without cau
1b315 73 69 6e 67 20 61 6e 79 20 49 4f 2e 0a 20 20 20 sing any IO..
1b316 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 */. sqli
1b317 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
1b318 76 2c 20 66 6c 61 67 73 20 3f 20 34 20 3a 20 32 v, flags ? 4 : 2
1b319 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b , pTab->nCol+1);
1b31a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1b31b 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 38 2c beChangeP2(v, 8,
1b31c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 pTab->nCol);.
1b31d 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c if( !db->mal
1b31e 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1b31f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1b320 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31 2c 20 MakeReady(v, 1,
1b321 31 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 1, 1, 0);.
1b322 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20 20 20 }. }. .
1b323 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1b324 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 72 63 eAll(db);. rc
1b325 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 = sqlite3Safety
1b326 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69 66 28 Off(db);. if(
1b327 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
1b328 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
1b329 65 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f ed ){. goto
1b32a 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a blob_open_out;.
1b32b 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 }.. sqlit
1b32c 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 28 73 e3_bind_int64((s
1b32d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 2c qlite3_stmt *)v,
1b32e 20 31 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 1, iRow);. r
1b32f 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 c = sqlite3_step
1b330 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a ((sqlite3_stmt *
1b331 29 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 )v);. if( rc!
1b332 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 =SQLITE_ROW ){.
1b333 20 20 20 20 20 6e 41 74 74 65 6d 70 74 2b 2b 3b nAttempt++;
1b334 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1b335 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71 te3_finalize((sq
1b336 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b lite3_stmt *)v);
1b337 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1b338 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
1b339 45 72 72 29 2c 20 7a 45 72 72 2c 20 73 71 6c 69 Err), zErr, sqli
1b33a 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b te3_errmsg(db));
1b33b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 . v = 0;.
1b33c 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e }. } while( n
1b33d 41 74 74 65 6d 70 74 3c 35 20 26 26 20 72 63 3d Attempt<5 && rc=
1b33e 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 =SQLITE_SCHEMA )
1b33f 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
1b340 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f ITE_ROW ){. /
1b341 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 * The row-record
1b342 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 has been opened
1b343 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 43 successfully. C
1b344 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20 heck that the.
1b345 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 ** column in q
1b346 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 uestion contains
1b347 20 74 65 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e text or a blob.
1b348 20 49 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a If it contains.
1b349 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 69 74 20 ** text, it
1b34a 69 73 20 75 70 20 74 6f 20 74 68 65 20 63 61 6c is up to the cal
1b34b 6c 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 65 ler to get the e
1b34c 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 2e 0a 20 ncoding right..
1b34d 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c */. Incrbl
1b34e 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75 ob *pBlob;. u
1b34f 33 32 20 74 79 70 65 20 3d 20 76 2d 3e 61 70 43 32 type = v->apC
1b350 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f sr[0]->aType[iCo
1b351 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 74 79 70 l];.. if( typ
1b352 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 20 73 71 e<12 ){. sq
1b353 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
1b354 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 izeof(zErr), zEr
1b355 72 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 r, "cannot open
1b356 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25 73 value of type %s
1b357 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79 70 ",. typ
1b358 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79 70 e==0?"null": typ
1b359 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69 6e e==7?"real": "in
1b35a 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b 0a teger". );.
1b35b 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1b35c 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 67 E_ERROR;. g
1b35d 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 oto blob_open_ou
1b35e 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c t;. }. pBl
1b35f 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a ob = (Incrblob *
1b360 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 )sqlite3DbMalloc
1b361 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
1b362 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20 20 Incrblob));.
1b363 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1b364 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 iled ){. sq
1b365 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1b366 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67 6f pBlob);. go
1b367 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 to blob_open_out
1b368 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c 6f ;. }. pBlo
1b369 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 b->flags = flags
1b36a 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43 73 ;. pBlob->pCs
1b36b 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30 5d r = v->apCsr[0]
1b36c 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 73 ->pCursor;. s
1b36d 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1b36e 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 Cursor(pBlob->pC
1b36f 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 sr);. sqlite3
1b370 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c BtreeCacheOverfl
1b371 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b ow(pBlob->pCsr);
1b372 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1b373 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42 6c eLeaveCursor(pBl
1b374 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 70 ob->pCsr);. p
1b375 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28 73 Blob->pStmt = (s
1b376 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 3b qlite3_stmt *)v;
1b377 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66 66 . pBlob->iOff
1b378 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30 set = v->apCsr[0
1b379 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c 5d ]->aOffset[iCol]
1b37a 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42 79 ;. pBlob->nBy
1b37b 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 te = sqlite3Vdbe
1b37c 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79 SerialTypeLen(ty
1b37d 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e pe);. pBlob->
1b37e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70 70 db = db;. *pp
1b37f 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33 5f Blob = (sqlite3_
1b380 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 blob *)pBlob;.
1b381 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
1b382 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 ;. }else if( rc
1b383 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1b384 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1b385 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 ntf(sizeof(zErr)
1b386 2c 20 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 , zErr, "no such
1b387 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22 2c 20 69 rowid: %lld", i
1b388 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 Row);. rc = S
1b389 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
1b38a 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3a ..blob_open_out:
1b38b 0a 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a . zErr[sizeof(z
1b38c 45 72 72 29 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a Err)-1] = '\0';.
1b38d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1b38e 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f _OK || db->mallo
1b38f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 cFailed ){. s
1b390 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
1b391 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 (sqlite3_stmt *)
1b392 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 v);. }. sqlite
1b393 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 3Error(db, rc, (
1b394 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 30 3a rc==SQLITE_OK?0:
1b395 7a 45 72 72 29 29 3b 0a 20 20 72 63 20 3d 20 73 zErr));. rc = s
1b396 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 qlite3ApiExit(db
1b397 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 , rc);. sqlite3
1b398 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d _mutex_leave(db-
1b399 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 >mutex);. retur
1b39a 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
1b39b 6c 6f 73 65 20 61 20 62 6c 6f 62 20 68 61 6e 64 lose a blob hand
1b39c 6c 65 20 74 68 61 74 20 77 61 73 20 70 72 65 76 le that was prev
1b39d 69 6f 75 73 6c 79 20 63 72 65 61 74 65 64 20 75 iously created u
1b39e 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f sing.** sqlite3_
1b39f 62 6c 6f 62 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a blob_open()..*/.
1b3a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1b3a1 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 qlite3_blob_clos
1b3a2 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a e(sqlite3_blob *
1b3a3 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c pBlob){. Incrbl
1b3a4 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f ob *p = (Incrblo
1b3a5 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 b *)pBlob;. int
1b3a6 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c rc;.. rc = sql
1b3a7 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d ite3_finalize(p-
1b3a8 3e 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 >pStmt);. sqlit
1b3a9 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 e3DbFree(p->db,
1b3aa 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
1b3ab 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 .}../*.** Perfor
1b3ac 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 m a read or writ
1b3ad 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 61 e operation on a
1b3ae 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 20 blob.*/.static
1b3af 69 6e 74 20 62 6c 6f 62 52 65 61 64 57 72 69 74 int blobReadWrit
1b3b0 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f e(. sqlite3_blo
1b3b1 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f 69 b *pBlob, . voi
1b3b2 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 d *z, . int n,
1b3b3 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 2c 20 . int iOffset,
1b3b4 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 28 . int (*xCall)(
1b3b5 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c 20 BtCursor*, u32,
1b3b6 75 33 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 u32, void*).){.
1b3b7 20 69 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 62 int rc;. Incrb
1b3b8 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c lob *p = (Incrbl
1b3b9 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 64 ob *)pBlob;. Vd
1b3ba 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 be *v;. sqlite3
1b3bb 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 0a *db = p->db; .
1b3bc 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
1b3bd 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 _enter(db->mutex
1b3be 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65 2a 29 );. v = (Vdbe*)
1b3bf 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69 66 28 p->pStmt;.. if(
1b3c0 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65 74 3c n<0 || iOffset<
1b3c1 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b 6e 29 0 || (iOffset+n)
1b3c2 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 20 20 >p->nByte ){.
1b3c3 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73 20 6f /* Request is o
1b3c4 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52 65 74 ut of range. Ret
1b3c5 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20 urn a transient
1b3c6 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20 72 63 error. */. rc
1b3c7 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
1b3c8 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1b3c9 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 r(db, SQLITE_ERR
1b3ca 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c 73 65 OR, 0);. } else
1b3cb 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 if( v==0 ){.
1b3cc 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 /* If there is
1b3cd 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e no statement han
1b3ce 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 62 6c dle, then the bl
1b3cf 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a 20 20 ob-handle has.
1b3d0 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 ** already bee
1b3d1 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 52 n invalidated. R
1b3d2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f eturn SQLITE_ABO
1b3d3 52 54 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e RT in this case.
1b3d4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
1b3d5 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 SQLITE_ABORT;.
1b3d6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 }else{. /* C
1b3d7 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72 65 65 all either Btree
1b3d8 44 61 74 61 28 29 20 6f 72 20 42 74 72 65 65 50 Data() or BtreeP
1b3d9 75 74 44 61 74 61 28 29 2e 20 49 66 20 53 51 4c utData(). If SQL
1b3da 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20 20 20 ITE_ABORT is.
1b3db 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 63 6c ** returned, cl
1b3dc 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61 74 65 ean-up the state
1b3dd 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 20 20 ment handle..
1b3de 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
1b3df 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b 0a 20 db == v->db );.
1b3e0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 sqlite3BtreeE
1b3e1 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e 70 43 nterCursor(p->pC
1b3e2 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20 78 43 sr);. rc = xC
1b3e3 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69 4f 66 all(p->pCsr, iOf
1b3e4 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65 74 2c fset+p->iOffset,
1b3e5 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69 n, z);. sqli
1b3e6 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 te3BtreeLeaveCur
1b3e7 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 sor(p->pCsr);.
1b3e8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1b3e9 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20 20 20 _ABORT ){.
1b3ea 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c sqlite3VdbeFinal
1b3eb 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20 70 2d ize(v);. p-
1b3ec 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 >pStmt = 0;.
1b3ed 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d }else{. db-
1b3ee 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 >errCode = rc;.
1b3ef 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72 63 3b v->rc = rc;
1b3f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20 . }. }. rc
1b3f1 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 = sqlite3ApiExit
1b3f2 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 (db, rc);. sqli
1b3f3 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1b3f4 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 db->mutex);. re
1b3f5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1b3f6 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d * Read data from
1b3f7 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a a blob handle..
1b3f8 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1b3f9 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 t sqlite3_blob_r
1b3fa 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ead(sqlite3_blob
1b3fb 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20 2a 7a *pBlob, void *z
1b3fc 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 , int n, int iOf
1b3fd 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 fset){. return
1b3fe 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 70 42 blobReadWrite(pB
1b3ff 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 66 73 lob, z, n, iOffs
1b400 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 et, sqlite3Btree
1b401 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Data);.}../*.**
1b402 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 61 20 Write data to a
1b403 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a blob handle..*/.
1b404 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1b405 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 qlite3_blob_writ
1b406 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a e(sqlite3_blob *
1b407 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 6f 69 pBlob, const voi
1b408 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 d *z, int n, int
1b409 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 iOffset){. ret
1b40a 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 urn blobReadWrit
1b40b 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64 20 2a e(pBlob, (void *
1b40c 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74 2c 20 )z, n, iOffset,
1b40d 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 sqlite3BtreePutD
1b40e 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 ata);.}../*.** Q
1b40f 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61 6e 64 uery a blob hand
1b410 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a 65 20 le for the size
1b411 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a 2a 0a of the data..**.
1b412 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f 62 2e ** The Incrblob.
1b413 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73 20 66 nByte field is f
1b414 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c 69 66 ixed for the lif
1b415 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49 6e 63 etime of the Inc
1b416 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f 20 6d rblob.** so no m
1b417 75 74 65 78 20 69 73 20 72 65 71 75 69 72 65 64 utex is required
1b418 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a 2f 0a for access..*/.
1b419 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1b41a 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 qlite3_blob_byte
1b41b 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a s(sqlite3_blob *
1b41c 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c pBlob){. Incrbl
1b41d 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f ob *p = (Incrblo
1b41e 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 65 74 b *)pBlob;. ret
1b41f 75 72 6e 20 70 2d 3e 6e 42 79 74 65 3b 0a 7d 0a urn p->nByte;.}.
1b420 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 .#endif /* #ifnd
1b421 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
1b422 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a NCRBLOB */../***
1b423 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1b424 6f 66 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a of vdbeblob.c **
1b425 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b426 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b427 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1b428 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1b429 6e 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 n file journal.c
1b42a 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1b42b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b42c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1b42d 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 32 * 2007 August 22
1b42e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1b42f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1b430 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1b431 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1b432 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1b433 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1b434 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1b435 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1b436 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1b437 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1b438 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1b439 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1b43a 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1b43b 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1b43c 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1b43d 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1b43e 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1b43f 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1b440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b441 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b442 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b443 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1b444 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
1b445 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 38 20 journal.c,v 1.8
1b446 32 30 30 38 2f 30 35 2f 30 31 20 31 38 3a 30 31 2008/05/01 18:01
1b447 3a 34 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :47 drh Exp $.*/
1b448 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1b449 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
1b44a 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ITE../*.** This
1b44b 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 file implements
1b44c 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64 20 6f a special kind o
1b44d 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f f sqlite3_file o
1b44e 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20 62 79 bject used.** by
1b44f 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65 61 74 SQLite to creat
1b450 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 e journal files
1b451 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 if the atomic-wr
1b452 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ite optimization
1b453 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a .** is enabled..
1b454 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74 69 6e **.** The distin
1b455 63 74 69 76 65 20 63 68 61 72 61 63 74 65 72 69 ctive characteri
1b456 73 74 69 63 20 6f 66 20 74 68 69 73 20 73 71 6c stic of this sql
1b457 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74 68 61 ite3_file is tha
1b458 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 6c 20 t the.** actual
1b459 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69 73 20 on disk file is
1b45a 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79 2e 20 created lazily.
1b45b 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20 69 73 When the file is
1b45c 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74 68 65 created,.** the
1b45d 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66 69 65 caller specifie
1b45e 73 20 61 20 62 75 66 66 65 72 20 73 69 7a 65 20 s a buffer size
1b45f 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
1b460 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20 62 65 buffer to.** be
1b461 20 75 73 65 64 20 74 6f 20 73 65 72 76 69 63 65 used to service
1b462 20 72 65 61 64 28 29 20 61 6e 64 20 77 72 69 74 read() and writ
1b463 65 28 29 20 72 65 71 75 65 73 74 73 2e 20 54 68 e() requests. Th
1b464 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a 2a 2a e actual file.**
1b465 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 on disk is not
1b466 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70 75 6c created or popul
1b467 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74 68 65 ated until eithe
1b468 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 54 68 r:.**.** 1) Th
1b469 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 e in-memory repr
1b46a 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f 77 73 esentation grows
1b46b 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20 74 too large for t
1b46c 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a he allocated .**
1b46d 20 20 20 20 20 20 62 75 66 66 65 72 2c 20 6f 72 buffer, or
1b46e 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 78 53 79 .** 2) The xSy
1b46f 6e 63 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63 nc() method is c
1b470 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a alled..*/..../*.
1b471 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 ** A JournalFile
1b472 20 6f 62 6a 65 63 74 20 69 73 20 61 20 73 75 62 object is a sub
1b473 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 class of sqlite3
1b474 5f 66 69 6c 65 20 75 73 65 64 20 62 79 0a 2a 2a _file used by.**
1b475 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 as an open file
1b476 20 68 61 6e 64 6c 65 20 66 6f 72 20 6a 6f 75 72 handle for jour
1b477 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 nal files..*/.st
1b478 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 ruct JournalFile
1b479 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f {. sqlite3_io_
1b47a 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 methods *pMethod
1b47b 3b 20 20 20 20 2f 2a 20 49 2f 4f 20 6d 65 74 68 ; /* I/O meth
1b47c 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e 61 6c 20 66 ods on journal f
1b47d 69 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 iles */. int nB
1b47e 75 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 uf;
1b47f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
1b480 65 20 6f 66 20 7a 42 75 66 5b 5d 20 69 6e 20 62 e of zBuf[] in b
1b481 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a ytes */. char *
1b482 7a 42 75 66 3b 20 20 20 20 20 20 20 20 20 20 20 zBuf;
1b483 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 /* Spa
1b484 63 65 20 74 6f 20 62 75 66 66 65 72 20 6a 6f 75 ce to buffer jou
1b485 72 6e 61 6c 20 77 72 69 74 65 73 20 2a 2f 0a 20 rnal writes */.
1b486 20 69 6e 74 20 69 53 69 7a 65 3b 20 20 20 20 20 int iSize;
1b487 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b488 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 7a 42 /* Amount of zB
1b489 75 66 5b 5d 20 63 75 72 72 65 6e 74 6c 79 20 75 uf[] currently u
1b48a 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 sed */. int fla
1b48b 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
1b48c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 /* xOpe
1b48d 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c n flags */. sql
1b48e 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 ite3_vfs *pVfs;
1b48f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b490 54 68 65 20 22 72 65 61 6c 22 20 75 6e 64 65 72 The "real" under
1b491 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0a 20 20 73 lying VFS */. s
1b492 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 52 65 qlite3_file *pRe
1b493 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f al; /
1b494 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e 64 * The "real" und
1b495 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 64 65 73 erlying file des
1b496 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 63 6f 6e criptor */. con
1b497 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 st char *zJourna
1b498 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 l; /*
1b499 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 Name of the jour
1b49a 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 74 nal file */.};.t
1b49b 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4a 6f ypedef struct Jo
1b49c 75 72 6e 61 6c 46 69 6c 65 20 4a 6f 75 72 6e 61 urnalFile Journa
1b49d 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 lFile;../*.** If
1b49e 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 it does not alr
1b49f 65 61 64 79 20 65 78 69 73 74 73 2c 20 63 72 65 eady exists, cre
1b4a0 61 74 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 ate and populate
1b4a1 20 74 68 65 20 6f 6e 2d 64 69 73 6b 20 66 69 6c the on-disk fil
1b4a2 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f 75 72 6e 61 e .** for Journa
1b4a3 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a 73 74 61 74 lFile p..*/.stat
1b4a4 69 63 20 69 6e 74 20 63 72 65 61 74 65 46 69 6c ic int createFil
1b4a5 65 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 e(JournalFile *p
1b4a6 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1b4a7 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 LITE_OK;. if( !
1b4a8 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 p->pReal ){.
1b4a9 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 52 sqlite3_file *pR
1b4aa 65 61 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 eal = (sqlite3_f
1b4ab 69 6c 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 ile *)&p[1];.
1b4ac 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
1b4ad 70 65 6e 28 70 2d 3e 70 56 66 73 2c 20 70 2d 3e pen(p->pVfs, p->
1b4ae 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52 65 61 6c 2c zJournal, pReal,
1b4af 20 70 2d 3e 66 6c 61 67 73 2c 20 30 29 3b 0a 20 p->flags, 0);.
1b4b0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1b4b1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d E_OK ){. p-
1b4b2 3e 70 52 65 61 6c 20 3d 20 70 52 65 61 6c 3b 0a >pReal = pReal;.
1b4b3 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53 69 if( p->iSi
1b4b4 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ze>0 ){.
1b4b5 61 73 73 65 72 74 28 70 2d 3e 69 53 69 7a 65 3c assert(p->iSize<
1b4b6 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20 20 20 20 20 =p->nBuf);.
1b4b7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1b4b8 73 57 72 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c sWrite(p->pReal,
1b4b9 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e 69 53 69 p->zBuf, p->iSi
1b4ba 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a ze, 0);. }.
1b4bb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1b4bc 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1b4bd 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a Close the file..
1b4be 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1b4bf 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f nlClose(sqlite3_
1b4c0 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4a file *pJfd){. J
1b4c1 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 ournalFile *p =
1b4c2 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 (JournalFile *)p
1b4c3 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 Jfd;. if( p->pR
1b4c4 65 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 eal ){. sqlit
1b4c5 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e 70 52 65 e3OsClose(p->pRe
1b4c6 61 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 al);. }. sqlit
1b4c7 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42 75 66 29 e3_free(p->zBuf)
1b4c8 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1b4c9 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 E_OK;.}../*.** R
1b4ca 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 74 68 ead data from th
1b4cb 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 e file..*/.stati
1b4cc 63 20 69 6e 74 20 6a 72 6e 6c 52 65 61 64 28 0a c int jrnlRead(.
1b4cd 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1b4ce 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 pJfd, /* The
1b4cf 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f journal file fro
1b4d0 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61 64 20 m which to read
1b4d1 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c */. void *zBuf,
1b4d2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1b4d3 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 20 68 ut the results h
1b4d4 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d ere */. int iAm
1b4d5 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
1b4d6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1b4d7 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 es to read */.
1b4d8 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 sqlite_int64 iOf
1b4d9 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 st /* Begin
1b4da 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 reading at this
1b4db 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 69 offset */.){. i
1b4dc 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1b4dd 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 K;. JournalFile
1b4de 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 *p = (JournalFi
1b4df 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 le *)pJfd;. if(
1b4e0 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 p->pReal ){.
1b4e1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
1b4e2 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 ead(p->pReal, zB
1b4e3 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 uf, iAmt, iOfst)
1b4e4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
1b4e5 73 73 65 72 74 28 20 69 41 6d 74 2b 69 4f 66 73 ssert( iAmt+iOfs
1b4e6 74 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a 20 t<=p->iSize );.
1b4e7 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 memcpy(zBuf,
1b4e8 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c &p->zBuf[iOfst],
1b4e9 20 69 41 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65 iAmt);. }. re
1b4ea 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1b4eb 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 * Write data to
1b4ec 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1b4ed 74 69 63 20 69 6e 74 20 6a 72 6e 6c 57 72 69 74 tic int jrnlWrit
1b4ee 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c e(. sqlite3_fil
1b4ef 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 e *pJfd, /* T
1b4f0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1b4f1 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 into which to wr
1b4f2 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 ite */. const v
1b4f3 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 oid *zBuf,
1b4f4 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20 /* Take data to
1b4f5 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 be written from
1b4f6 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 here */. int iA
1b4f7 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
1b4f8 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1b4f9 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a tes to write */.
1b4fa 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 sqlite_int64 i
1b4fb 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 Ofst /* Begi
1b4fc 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 n writing at thi
1b4fd 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 s offset into th
1b4fe 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 e file */.){. i
1b4ff 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1b500 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 K;. JournalFile
1b501 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 *p = (JournalFi
1b502 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 le *)pJfd;. if(
1b503 20 21 70 2d 3e 70 52 65 61 6c 20 26 26 20 28 69 !p->pReal && (i
1b504 4f 66 73 74 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42 Ofst+iAmt)>p->nB
1b505 75 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 uf ){. rc = c
1b506 72 65 61 74 65 46 69 6c 65 28 70 29 3b 0a 20 20 reateFile(p);.
1b507 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 }. if( rc==SQLI
1b508 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
1b509 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 p->pReal ){.
1b50a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1b50b 73 57 72 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c sWrite(p->pReal,
1b50c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 zBuf, iAmt, iOf
1b50d 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a st);. }else{.
1b50e 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d memcpy(&p-
1b50f 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 7a 42 >zBuf[iOfst], zB
1b510 75 66 2c 20 69 41 6d 74 29 3b 0a 20 20 20 20 20 uf, iAmt);.
1b511 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 28 69 if( p->iSize<(i
1b512 4f 66 73 74 2b 69 41 6d 74 29 20 29 7b 0a 20 20 Ofst+iAmt) ){.
1b513 20 20 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d p->iSize =
1b514 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 3b 0a 20 (iOfst+iAmt);.
1b515 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1b516 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1b517 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 ./*.** Truncate
1b518 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1b519 74 69 63 20 69 6e 74 20 6a 72 6e 6c 54 72 75 6e tic int jrnlTrun
1b51a 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c cate(sqlite3_fil
1b51b 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f e *pJfd, sqlite_
1b51c 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 69 int64 size){. i
1b51d 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1b51e 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 K;. JournalFile
1b51f 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 *p = (JournalFi
1b520 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 le *)pJfd;. if(
1b521 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 p->pReal ){.
1b522 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
1b523 72 75 6e 63 61 74 65 28 70 2d 3e 70 52 65 61 6c runcate(p->pReal
1b524 2c 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 , size);. }else
1b525 20 69 66 28 20 73 69 7a 65 3c 70 2d 3e 69 53 69 if( size<p->iSi
1b526 7a 65 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 ze ){. p->iSi
1b527 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 7d 0a 20 ze = size;. }.
1b528 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1b529 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69 *.** Sync the fi
1b52a 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
1b52b 74 20 6a 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 t jrnlSync(sqlit
1b52c 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 69 e3_file *pJfd, i
1b52d 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 nt flags){. int
1b52e 20 72 63 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 rc;. JournalFi
1b52f 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c le *p = (Journal
1b530 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 File *)pJfd;. i
1b531 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 f( p->pReal ){.
1b532 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1b533 73 53 79 6e 63 28 70 2d 3e 70 52 65 61 6c 2c 20 sSync(p->pReal,
1b534 66 6c 61 67 73 29 3b 0a 20 20 7d 65 6c 73 65 7b flags);. }else{
1b535 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1b536 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 _OK;. }. retur
1b537 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 n rc;.}../*.** Q
1b538 75 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 uery the size of
1b539 20 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 the file in byt
1b53a 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
1b53b 74 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 t jrnlFileSize(s
1b53c 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1b53d 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 d, sqlite_int64
1b53e 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 *pSize){. int r
1b53f 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1b540 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 JournalFile *p
1b541 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a = (JournalFile *
1b542 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e )pJfd;. if( p->
1b543 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 pReal ){. rc
1b544 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
1b545 69 7a 65 28 70 2d 3e 70 52 65 61 6c 2c 20 70 53 ize(p->pReal, pS
1b546 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ize);. }else{.
1b547 20 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c *pSize = (sql
1b548 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 69 53 ite_int64) p->iS
1b549 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ize;. }. retur
1b54a 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
1b54b 61 62 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 able of methods
1b54c 66 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 for JournalFile
1b54d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a sqlite3_file obj
1b54e 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ect..*/.static s
1b54f 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f truct sqlite3_io
1b550 5f 6d 65 74 68 6f 64 73 20 4a 6f 75 72 6e 61 6c _methods Journal
1b551 46 69 6c 65 4d 65 74 68 6f 64 73 20 3d 20 7b 0a FileMethods = {.
1b552 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
1b553 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
1b554 20 20 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 jrnlClose,
1b555 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 /* xClose */.
1b556 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20 2f jrnlRead, /
1b557 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6a 72 6e * xRead */. jrn
1b558 6c 57 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 lWrite, /* x
1b559 57 72 69 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54 Write */. jrnlT
1b55a 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 72 runcate, /* xTr
1b55b 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c uncate */. jrnl
1b55c 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 Sync, /* xS
1b55d 79 6e 63 20 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c ync */. jrnlFil
1b55e 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 eSize, /* xFile
1b55f 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 Size */. 0,
1b560 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
1b561 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 k */. 0,
1b562 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
1b563 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1b564 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 /* xCheckRe
1b565 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 servedLock */.
1b566 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1b567 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a * xFileControl *
1b568 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 /. 0,
1b569 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a /* xSectorSiz
1b56a 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 e */. 0
1b56b 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 /* xDevice
1b56c 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 Characteristics
1b56d 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 */.};../* .** Op
1b56e 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c en a journal fil
1b56f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1b570 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1b571 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 73 JournalOpen(. s
1b572 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1b573 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 , /* The
1b574 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20 VFS to use for
1b575 61 63 74 75 61 6c 20 66 69 6c 65 20 49 2f 4f 20 actual file I/O
1b576 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1b577 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 *zName,
1b578 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a /* Name of the j
1b579 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 ournal file */.
1b57a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
1b57b 4a 66 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 Jfd, /* P
1b57c 72 65 61 6c 6c 6f 63 61 74 65 64 2c 20 62 6c 61 reallocated, bla
1b57d 6e 6b 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 2a nk file handle *
1b57e 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
1b57f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1b580 2a 20 4f 70 65 6e 69 6e 67 20 66 6c 61 67 73 20 * Opening flags
1b581 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 20 20 20 */. int nBuf
1b582 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b583 2f 2a 20 42 79 74 65 73 20 62 75 66 66 65 72 65 /* Bytes buffere
1b584 64 20 62 65 66 6f 72 65 20 6f 70 65 6e 69 6e 67 d before opening
1b585 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a the file */.){.
1b586 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 JournalFile *p
1b587 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 = (JournalFile
1b588 2a 29 70 4a 66 64 3b 0a 20 20 6d 65 6d 73 65 74 *)pJfd;. memset
1b589 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4a 6f (p, 0, sqlite3Jo
1b58a 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 29 urnalSize(pVfs))
1b58b 3b 0a 20 20 69 66 28 20 6e 42 75 66 3e 30 20 29 ;. if( nBuf>0 )
1b58c 7b 0a 20 20 20 20 70 2d 3e 7a 42 75 66 20 3d 20 {. p->zBuf =
1b58d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
1b58e 6f 28 6e 42 75 66 29 3b 0a 20 20 20 20 69 66 28 o(nBuf);. if(
1b58f 20 21 70 2d 3e 7a 42 75 66 20 29 7b 0a 20 20 20 !p->zBuf ){.
1b590 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1b591 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
1b592 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
1b593 6e 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 n sqlite3OsOpen(
1b594 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 pVfs, zName, pJf
1b595 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 d, flags, 0);.
1b596 7d 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d }. p->pMethod =
1b597 20 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 &JournalFileMet
1b598 68 6f 64 73 3b 0a 20 20 70 2d 3e 6e 42 75 66 20 hods;. p->nBuf
1b599 3d 20 6e 42 75 66 3b 0a 20 20 70 2d 3e 66 6c 61 = nBuf;. p->fla
1b59a 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 2d gs = flags;. p-
1b59b 3e 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d >zJournal = zNam
1b59c 65 3b 0a 20 20 70 2d 3e 70 56 66 73 20 3d 20 70 e;. p->pVfs = p
1b59d 56 66 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 Vfs;. return SQ
1b59e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1b59f 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e * If the argumen
1b5a0 74 20 70 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 t p points to a
1b5a1 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 74 72 75 JournalFile stru
1b5a2 63 74 75 72 65 2c 20 61 6e 64 20 74 68 65 20 75 cture, and the u
1b5a3 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c nderlying.** fil
1b5a4 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 e has not yet be
1b5a5 65 6e 20 63 72 65 61 74 65 64 2c 20 63 72 65 61 en created, crea
1b5a6 74 65 20 69 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 te it now..*/.SQ
1b5a7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1b5a8 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 sqlite3JournalC
1b5a9 72 65 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 reate(sqlite3_fi
1b5aa 6c 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d le *p){. if( p-
1b5ab 3e 70 4d 65 74 68 6f 64 73 21 3d 26 4a 6f 75 72 >pMethods!=&Jour
1b5ac 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 20 29 nalFileMethods )
1b5ad 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1b5ae 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 ITE_OK;. }. re
1b5af 74 75 72 6e 20 63 72 65 61 74 65 46 69 6c 65 28 turn createFile(
1b5b0 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 (JournalFile *)p
1b5b1 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 );.}../* .** Ret
1b5b2 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1b5b3 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 f bytes required
1b5b4 20 74 6f 20 73 74 6f 72 65 20 61 20 4a 6f 75 72 to store a Jour
1b5b5 6e 61 6c 46 69 6c 65 20 74 68 61 74 20 75 73 65 nalFile that use
1b5b6 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f s vfs.** pVfs to
1b5b7 20 63 72 65 61 74 65 20 74 68 65 20 75 6e 64 65 create the unde
1b5b8 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 rlying on-disk f
1b5b9 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f iles..*/.SQLITE_
1b5ba 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1b5bb 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73 te3JournalSize(s
1b5bc 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1b5bd 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 56 66 ){. return (pVf
1b5be 73 2d 3e 73 7a 4f 73 46 69 6c 65 2b 73 69 7a 65 s->szOsFile+size
1b5bf 6f 66 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 29 29 of(JournalFile))
1b5c0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a ;.}.#endif../***
1b5c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1b5c2 6f 66 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a of journal.c ***
1b5c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1b5c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1b5c7 6e 20 66 69 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61 n file memjourna
1b5c8 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a l.c ************
1b5c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1b5cb 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 37 * 2008 October 7
1b5cc 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1b5cd 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1b5ce 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1b5cf 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1b5d0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1b5d1 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1b5d2 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1b5d3 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1b5d4 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1b5d5 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1b5d6 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1b5d7 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1b5d8 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1b5d9 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1b5da 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1b5db 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1b5dc 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1b5dd 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1b5de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1b5e2 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
1b5e3 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 contains code us
1b5e4 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 e to implement a
1b5e5 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c n in-memory roll
1b5e6 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a back journal..**
1b5e7 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 The in-memory r
1b5e8 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
1b5e9 69 73 20 75 73 65 64 20 74 6f 20 6a 6f 75 72 6e is used to journ
1b5ea 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 al transactions
1b5eb 66 6f 72 0a 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a for.** ":memory:
1b5ec 22 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20 " databases and
1b5ed 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c when the journal
1b5ee 5f 6d 6f 64 65 3d 4d 45 4d 4f 52 59 20 70 72 61 _mode=MEMORY pra
1b5ef 67 6d 61 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a gma is used..**.
1b5f0 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 6d 65 6d ** @(#) $Id: mem
1b5f1 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 35 20 journal.c,v 1.5
1b5f2 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 2008/11/19 16:52
1b5f3 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :44 danielk1977
1b5f4 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 Exp $.*/../* For
1b5f5 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 ward references
1b5f6 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 to internal stru
1b5f7 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 ctures */.typede
1b5f8 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 f struct MemJour
1b5f9 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a nal MemJournal;.
1b5fa 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 typedef struct F
1b5fb 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 ilePoint FilePoi
1b5fc 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 nt;.typedef stru
1b5fd 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c ct FileChunk Fil
1b5fe 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 eChunk;../* Spac
1b5ff 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f e to hold the ro
1b600 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 llback journal i
1b601 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 s allocated in i
1b602 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 ncrements of.**
1b603 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 2e this many bytes.
1b604 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 .*/.#define JOUR
1b605 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 31 30 NAL_CHUNKSIZE 10
1b606 32 34 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 24../* Macro to
1b607 66 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d find the minimum
1b608 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 of two numeric
1b609 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 values..*/.#ifnd
1b60a 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 ef MIN.# define
1b60b 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 MIN(x,y) ((x)<(y
1b60c 29 3f 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 )?(x):(y)).#endi
1b60d 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c f../*.** The rol
1b60e 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 lback journal is
1b60f 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c composed of a l
1b610 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 inked list of th
1b611 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a ese structures..
1b612 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 43 68 */.struct FileCh
1b613 75 6e 6b 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e unk {. FileChun
1b614 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 k *pNext;
1b615 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
1b616 63 68 75 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75 chunk in the jou
1b617 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68 rnal */. u8 zCh
1b618 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e unk[JOURNAL_CHUN
1b619 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e KSIZE]; /* Con
1b61a 74 65 6e 74 20 6f 66 20 74 68 69 73 20 63 68 75 tent of this chu
1b61b 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 nk */.};../*.**
1b61c 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
1b61d 68 69 73 20 6f 62 6a 65 63 74 20 73 65 72 76 65 his object serve
1b61e 73 20 61 73 20 61 20 63 75 72 73 6f 72 20 69 6e s as a cursor in
1b61f 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 to the rollback
1b620 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 journal..** The
1b621 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 65 69 cursor can be ei
1b622 74 68 65 72 20 66 6f 72 20 72 65 61 64 69 6e 67 ther for reading
1b623 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a or writing..*/.
1b624 73 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74 struct FilePoint
1b625 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 {. sqlite3_int
1b626 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 64 iOffset;
1b627 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 /* Offset f
1b628 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
1b629 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f g of the file */
1b62a 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 . FileChunk *pC
1b62b 68 75 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 hunk;
1b62c 20 20 20 2f 2a 20 53 70 65 63 69 66 69 63 20 63 /* Specific c
1b62d 68 75 6e 6b 20 69 6e 74 6f 20 77 68 69 63 68 20 hunk into which
1b62e 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f cursor points */
1b62f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .};../*.** This
1b630 73 75 62 63 6c 61 73 73 20 69 73 20 61 20 73 75 subclass is a su
1b631 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 bclass of sqlite
1b632 33 5f 66 69 6c 65 2e 20 20 45 61 63 68 20 6f 70 3_file. Each op
1b633 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61 en memory-journa
1b634 6c 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 73 74 61 l.** is an insta
1b635 6e 63 65 20 6f 66 20 74 68 69 73 20 63 6c 61 73 nce of this clas
1b636 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d s..*/.struct Mem
1b637 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69 Journal {. sqli
1b638 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
1b639 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50 pMethod; /* P
1b63a 61 72 65 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53 arent class. MUS
1b63b 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 T BE FIRST */.
1b63c 46 69 6c 65 43 68 75 6e 6b 20 2a 70 46 69 72 73 FileChunk *pFirs
1b63d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
1b63e 2f 2a 20 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65 /* Head of in-me
1b63f 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20 mory chunk-list
1b640 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 65 */. FilePoint e
1b641 6e 64 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 ndpoint;
1b642 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
1b643 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 to the end of th
1b644 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 e file */. File
1b645 50 6f 69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b Point readpoint;
1b646 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1b647 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e ointer to the en
1b648 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 78 52 d of the last xR
1b649 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ead() */.};../*.
1b64a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f ** Read data fro
1b64b 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 m the file..*/.s
1b64c 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e tatic int memjrn
1b64d 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 lRead(. sqlite3
1b64e 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 _file *pJfd,
1b64f 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
1b650 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 ile from which t
1b651 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 o read */. void
1b652 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 *zBuf,
1b653 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 /* Put the re
1b654 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 sults here */.
1b655 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 int iAmt,
1b656 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1b657 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 of bytes to rea
1b658 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e d */. sqlite_in
1b659 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a t64 iOfst /*
1b65a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 Begin reading a
1b65b 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f t this offset */
1b65c 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c .){. MemJournal
1b65d 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 *p = (MemJourna
1b65e 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a l *)pJfd;. u8 *
1b65f 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69 zOut = zBuf;. i
1b660 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b nt nRead = iAmt;
1b661 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 . int iChunkOff
1b662 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b set;. FileChunk
1b663 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 61 73 73 *pChunk;.. ass
1b664 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d 74 3c ert( iOfst+iAmt<
1b665 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 =p->endpoint.iOf
1b666 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 20 70 fset );.. if( p
1b667 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 ->readpoint.iOff
1b668 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69 4f set!=iOfst || iO
1b669 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 fst==0 ){. sq
1b66a 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 lite3_int64 iOff
1b66b 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 43 = 0;. for(pC
1b66c 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b 20 hunk=p->pFirst;
1b66d 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 20 . pChunk
1b66e 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c && (iOff+JOURNAL
1b66f 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 _CHUNKSIZE)<=iOf
1b670 73 74 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75 st;. pChu
1b671 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 nk=pChunk->pNext
1b672 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 4f . ){. iO
1b673 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 ff += JOURNAL_CH
1b674 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 UNKSIZE;. }.
1b675 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 68 75 }else{. pChu
1b676 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e nk = p->readpoin
1b677 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 t.pChunk;. }..
1b678 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 iChunkOffset =
1b679 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c 5f 43 (iOfst%JOURNAL_C
1b67a 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 6f 20 HUNKSIZE);. do
1b67b 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61 63 65 {. int iSpace
1b67c 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b = JOURNAL_CHUNK
1b67d 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 SIZE - iChunkOff
1b67e 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f set;. int nCo
1b67f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64 2c 20 py = MIN(nRead,
1b680 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 (JOURNAL_CHUNKSI
1b681 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 ZE - iChunkOffse
1b682 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 t));. memcpy(
1b683 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a zOut, &pChunk->z
1b684 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 Chunk[iChunkOffs
1b685 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 et], nCopy);.
1b686 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a zOut += nCopy;.
1b687 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69 53 70 nRead -= iSp
1b688 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e 6b 4f ace;. iChunkO
1b689 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d 20 77 ffset = 0;. } w
1b68a 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30 20 26 hile( nRead>=0 &
1b68b 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b & (pChunk=pChunk
1b68c 2d 3e 70 4e 65 78 74 29 20 26 26 20 6e 52 65 61 ->pNext) && nRea
1b68d 64 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 61 64 d>0 );. p->read
1b68e 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 3d 20 point.iOffset =
1b68f 69 4f 66 73 74 2b 69 41 6d 74 3b 0a 20 20 70 2d iOfst+iAmt;. p-
1b690 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e >readpoint.pChun
1b691 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a 0a 20 20 72 k = pChunk;.. r
1b692 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1b693 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 .}../*.** Write
1b694 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 data to the file
1b695 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1b696 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20 memjrnlWrite(.
1b697 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1b698 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f fd, /* The jo
1b699 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 urnal file into
1b69a 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a which to write *
1b69b 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
1b69c 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 zBuf, /* Ta
1b69d 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 ke data to be wr
1b69e 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 itten from here
1b69f 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 */. int iAmt,
1b6a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1b6a1 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
1b6a2 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c o write */. sql
1b6a3 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 ite_int64 iOfst
1b6a4 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69 /* Begin wri
1b6a5 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 ting at this off
1b6a6 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c set into the fil
1b6a7 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 e */.){. MemJou
1b6a8 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f rnal *p = (MemJo
1b6a9 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 urnal *)pJfd;.
1b6aa 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 69 41 6d int nWrite = iAm
1b6ab 74 3b 0a 20 20 75 38 20 2a 7a 57 72 69 74 65 20 t;. u8 *zWrite
1b6ac 3d 20 28 75 38 20 2a 29 7a 42 75 66 3b 0a 0a 20 = (u8 *)zBuf;..
1b6ad 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 /* An in-memory
1b6ae 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 journal file sh
1b6af 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 65 72 20 62 ould only ever b
1b6b0 65 20 61 70 70 65 6e 64 65 64 20 74 6f 2e 20 52 e appended to. R
1b6b1 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 63 63 65 73 andom. ** acces
1b6b2 73 20 77 72 69 74 65 73 20 61 72 65 20 6e 6f 74 s writes are not
1b6b3 20 72 65 71 75 69 72 65 64 20 62 79 20 73 71 6c required by sql
1b6b4 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ite.. */. asse
1b6b5 72 74 28 69 4f 66 73 74 3d 3d 70 2d 3e 65 6e 64 rt(iOfst==p->end
1b6b6 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 29 3b 0a point.iOffset);.
1b6b7 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
1b6b8 45 52 28 69 4f 66 73 74 29 3b 0a 0a 20 20 77 68 ER(iOfst);.. wh
1b6b9 69 6c 65 28 20 6e 57 72 69 74 65 3e 30 20 29 7b ile( nWrite>0 ){
1b6ba 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a . FileChunk *
1b6bb 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70 pChunk = p->endp
1b6bc 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20 oint.pChunk;.
1b6bd 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 int iChunkOffse
1b6be 74 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e t = p->endpoint.
1b6bf 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f iOffset%JOURNAL_
1b6c0 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 69 CHUNKSIZE;. i
1b6c1 6e 74 20 69 53 70 61 63 65 20 3d 20 4d 49 4e 28 nt iSpace = MIN(
1b6c2 6e 57 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f nWrite, JOURNAL_
1b6c3 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 CHUNKSIZE - iChu
1b6c4 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 nkOffset);..
1b6c5 69 66 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 if( iChunkOffset
1b6c6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ==0 ){. /*
1b6c7 4e 65 77 20 63 68 75 6e 6b 20 69 73 20 72 65 71 New chunk is req
1b6c8 75 69 72 65 64 20 74 6f 20 65 78 74 65 6e 64 20 uired to extend
1b6c9 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 the file. */.
1b6ca 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e FileChunk *pN
1b6cb 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c ew = sqlite3_mal
1b6cc 6c 6f 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 43 loc(sizeof(FileC
1b6cd 68 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 66 hunk));. if
1b6ce 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 ( !pNew ){.
1b6cf 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1b6d0 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 _IOERR_NOMEM;.
1b6d1 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 }. pNew
1b6d2 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 ->pNext = 0;.
1b6d3 20 20 20 69 66 28 20 70 43 68 75 6e 6b 20 29 7b if( pChunk ){
1b6d4 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1b6d5 20 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 p->pFirst );.
1b6d6 20 20 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e pChunk->pN
1b6d7 65 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 ext = pNew;.
1b6d8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1b6d9 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70 46 69 assert( !p->pFi
1b6da 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70 rst );. p
1b6db 2d 3e 70 46 69 72 73 74 20 3d 20 70 4e 65 77 3b ->pFirst = pNew;
1b6dc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1b6dd 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e ->endpoint.pChun
1b6de 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a k = pNew;. }.
1b6df 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e . memcpy(&p->
1b6e0 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d endpoint.pChunk-
1b6e1 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 >zChunk[iChunkOf
1b6e2 66 73 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69 fset], zWrite, i
1b6e3 53 70 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 69 Space);. zWri
1b6e4 74 65 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 te += iSpace;.
1b6e5 20 20 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 61 nWrite -= iSpa
1b6e6 63 65 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f ce;. p->endpo
1b6e7 69 6e 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69 int.iOffset += i
1b6e8 53 70 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 Space;. }.. re
1b6e9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1b6ea 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 }../*.** Truncat
1b6eb 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 e the file..*/.s
1b6ec 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e tatic int memjrn
1b6ed 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 lTruncate(sqlite
1b6ee 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 3_file *pJfd, sq
1b6ef 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 lite_int64 size)
1b6f0 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a {. MemJournal *
1b6f1 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 p = (MemJournal
1b6f2 2a 29 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68 *)pJfd;. FileCh
1b6f3 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 unk *pChunk;. a
1b6f4 73 73 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a ssert(size==0);.
1b6f5 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
1b6f6 45 52 28 73 69 7a 65 29 3b 0a 20 20 70 43 68 75 ER(size);. pChu
1b6f7 6e 6b 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b 0a nk = p->pFirst;.
1b6f8 20 20 77 68 69 6c 65 28 20 70 43 68 75 6e 6b 20 while( pChunk
1b6f9 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b ){. FileChunk
1b6fa 20 2a 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b 3b *pTmp = pChunk;
1b6fb 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 43 . pChunk = pC
1b6fc 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 hunk->pNext;.
1b6fd 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 sqlite3_free(pT
1b6fe 6d 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 mp);. }. sqlit
1b6ff 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e e3MemJournalOpen
1b700 28 70 4a 66 64 29 3b 0a 20 20 72 65 74 75 72 6e (pJfd);. return
1b701 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1b702 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 *.** Close the f
1b703 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
1b704 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28 nt memjrnlClose(
1b705 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1b706 66 64 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 fd){. memjrnlTr
1b707 75 6e 63 61 74 65 28 70 4a 66 64 2c 20 30 29 3b uncate(pJfd, 0);
1b708 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1b709 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 _OK;.}.../*.** S
1b70a 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f ync the file..*/
1b70b 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a .static int memj
1b70c 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f rnlSync(sqlite3_
1b70d 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 file *NotUsed, i
1b70e 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 nt NotUsed2){.
1b70f 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1b710 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
1b711 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 ed2);. return S
1b712 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1b713 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69 7a ** Query the siz
1b714 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e e of the file in
1b715 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 bytes..*/.stati
1b716 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69 6c c int memjrnlFil
1b717 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 eSize(sqlite3_fi
1b718 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 le *pJfd, sqlite
1b719 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a _int64 *pSize){.
1b71a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 MemJournal *p
1b71b 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 = (MemJournal *)
1b71c 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20 3d pJfd;. *pSize =
1b71d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 20 (sqlite_int64)
1b71e 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 p->endpoint.iOff
1b71f 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 set;. return SQ
1b720 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1b721 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68 6f * Table of metho
1b722 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e 61 ds for MemJourna
1b723 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f l sqlite3_file o
1b724 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 bject..*/.static
1b725 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
1b726 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a 6f io_methods MemJo
1b727 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 7b urnalMethods = {
1b728 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 . 1,
1b729 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e /* iVersion
1b72a 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c 6f */. memjrnlClo
1b72b 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 se, /* xClos
1b72c 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52 65 e */. memjrnlRe
1b72d 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61 ad, /* xRea
1b72e 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57 72 d */. memjrnlWr
1b72f 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 ite, /* xWri
1b730 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 54 te */. memjrnlT
1b731 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 72 runcate, /* xTr
1b732 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a uncate */. memj
1b733 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a rnlSync, /*
1b734 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d 6a xSync */. memj
1b735 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a rnlFileSize, /*
1b736 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 xFileSize */.
1b737 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1b738 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 /* xLock */.
1b739 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1b73a 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a /* xUnlock */.
1b73b 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
1b73c 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 /* xCheckRes
1b73d 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 ervedLock */. 0
1b73e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1b73f 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c /* xFileControl
1b740 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1b741 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 /* xSect
1b742 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 20 orSize */. 0
1b743 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1b744 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 xDeviceCharacte
1b745 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f ristics */.};../
1b746 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 * .** Open a jou
1b747 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 rnal file..*/.SQ
1b748 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1b749 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 d sqlite3MemJour
1b74a 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f nalOpen(sqlite3_
1b74b 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4d file *pJfd){. M
1b74c 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 emJournal *p = (
1b74d 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 MemJournal *)pJf
1b74e 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 d;. memset(p, 0
1b74f 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 , sqlite3MemJour
1b750 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70 2d nalSize());. p-
1b751 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d 4a >pMethod = &MemJ
1b752 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d ournalMethods;.}
1b753 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1b754 72 75 65 20 69 66 20 74 68 65 20 66 69 6c 65 2d rue if the file-
1b755 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73 handle passed as
1b756 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 an argument is
1b757 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 .** an in-memory
1b758 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c journal .*/.SQL
1b759 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1b75a 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 sqlite3IsMemJour
1b75b 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 nal(sqlite3_file
1b75c 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75 72 *pJfd){. retur
1b75d 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 n pJfd->pMethods
1b75e 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 ==&MemJournalMet
1b75f 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 hods;.}../* .**
1b760 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1b761 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 r of bytes requi
1b762 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 4d red to store a M
1b763 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20 75 emJournal that u
1b764 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20 ses vfs.** pVfs
1b765 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 6e to create the un
1b766 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b derlying on-disk
1b767 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 files..*/.SQLIT
1b768 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1b769 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 lite3MemJournalS
1b76a 69 7a 65 28 29 7b 0a 20 20 72 65 74 75 72 6e 20 ize(){. return
1b76b 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61 sizeof(MemJourna
1b76c 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a l);.}../********
1b76d 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
1b76e 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a mjournal.c *****
1b76f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b771 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1b772 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1b773 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a e walker.c *****
1b774 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b775 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b776 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1b777 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a 8 August 16.**.*
1b778 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
1b779 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
1b77a 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
1b77b 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1b77c 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1b77d 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1b77e 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1b77f 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1b780 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1b781 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1b782 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1b783 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
1b784 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
1b785 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
1b786 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
1b787 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
1b788 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
1b789 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1b78a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b78b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b78c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b78d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1b78e 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1b78f 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 routines used f
1b790 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 or walking the p
1b791 61 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a arser tree for.*
1b792 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 * an SQL stateme
1b793 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77 nt..**.** $Id: w
1b794 61 6c 6b 65 72 2e 63 2c 76 20 31 2e 31 20 32 30 alker.c,v 1.1 20
1b795 30 38 2f 30 38 2f 32 30 20 31 36 3a 33 35 3a 31 08/08/20 16:35:1
1b796 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0 drh Exp $.*/..
1b797 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 ./*.** Walk an e
1b798 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 xpression tree.
1b799 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c Invoke the call
1b79a 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 back once for ea
1b79b 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 ch node.** of th
1b79c 65 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 e expression, wh
1b79d 69 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 ile decending.
1b79e 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c (In other words,
1b79f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a the callback.**
1b7a0 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f is invoked befo
1b7a1 72 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c re visiting chil
1b7a2 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 dren.).**.** The
1b7a3 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 return value fr
1b7a4 6f 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 om the callback
1b7a5 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 should be one of
1b7a6 20 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f the WRC_*.** co
1b7a7 6e 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69 nstants to speci
1b7a8 66 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 fy how to procee
1b7a9 64 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e d with the walk.
1b7aa 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f .**.** WRC_Co
1b7ab 6e 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 ntinue Cont
1b7ac 69 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20 inue descending
1b7ad 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a down the tree..*
1b7ae 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e *.** WRC_Prun
1b7af 65 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 e Do not
1b7b0 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 descend into ch
1b7b1 69 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 ild nodes. But
1b7b2 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 allow.**
1b7b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
1b7b4 65 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e e walk to contin
1b7b5 75 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20 ue with sibling
1b7b6 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 nodes..**.**
1b7b7 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 WRC_Abort
1b7b8 20 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c Do no more cal
1b7b9 6c 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 lbacks. Unwind
1b7ba 74 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a the stack and.**
1b7bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b7bc 20 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65 return the
1b7bd 20 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 top-level walk
1b7be 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 call..**.** The
1b7bf 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f return value fro
1b7c0 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 m this routine i
1b7c1 73 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 s WRC_Abort to a
1b7c2 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 bandon the tree
1b7c3 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f walk.** and WRC_
1b7c4 43 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 Continue to cont
1b7c5 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f inue..*/.SQLITE_
1b7c6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1b7c7 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b te3WalkExpr(Walk
1b7c8 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 er *pWalker, Exp
1b7c9 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 r *pExpr){. int
1b7ca 20 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72 rc;. if( pExpr
1b7cb 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 ==0 ) return WRC
1b7cc 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 _Continue;. rc
1b7cd 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72 = pWalker->xExpr
1b7ce 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 Callback(pWalker
1b7cf 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 , pExpr);. if(
1b7d0 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65 rc==WRC_Continue
1b7d1 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ){. if( sqli
1b7d2 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1b7d3 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 ker, pExpr->pLef
1b7d4 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f t) ) return WRC_
1b7d5 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 Abort;. if( s
1b7d6 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 qlite3WalkExpr(p
1b7d7 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 Walker, pExpr->p
1b7d8 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 Right) ) return
1b7d9 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 WRC_Abort;. i
1b7da 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 f( sqlite3WalkEx
1b7db 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 prList(pWalker,
1b7dc 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 20 29 20 pExpr->pList) )
1b7dd 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1b7de 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1b7df 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 3WalkSelect(pWal
1b7e0 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c ker, pExpr->pSel
1b7e1 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ect) ){. re
1b7e2 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1b7e3 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1b7e4 72 6e 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72 rn rc & WRC_Abor
1b7e5 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c t;.}../*.** Call
1b7e6 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
1b7e7 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 () for every exp
1b7e8 72 65 73 73 69 6f 6e 20 69 6e 20 6c 69 73 74 20 ression in list
1b7e9 70 20 6f 72 20 75 6e 74 69 6c 0a 2a 2a 20 61 6e p or until.** an
1b7ea 20 61 62 6f 72 74 20 72 65 71 75 65 73 74 20 69 abort request i
1b7eb 73 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 s seen..*/.SQLIT
1b7ec 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1b7ed 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
1b7ee 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 t(Walker *pWalke
1b7ef 72 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 29 7b r, ExprList *p){
1b7f0 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 57 . int i, rc = W
1b7f1 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 73 RC_Continue;. s
1b7f2 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
1b7f3 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 tem *pItem;. if
1b7f4 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 ( p ){. for(i
1b7f5 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d =p->nExpr, pItem
1b7f6 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c =p->a; i>0; i--,
1b7f7 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1b7f8 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1b7f9 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 49 Expr(pWalker, pI
1b7fa 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 72 65 tem->pExpr) ) re
1b7fb 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1b7fc 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1b7fd 72 6e 20 72 63 20 26 20 57 52 43 5f 43 6f 6e 74 rn rc & WRC_Cont
1b7fe 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 inue;.}../*.** W
1b7ff 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69 alk all expressi
1b800 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ons associated w
1b801 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 ith SELECT state
1b802 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e ment p. Do.** n
1b803 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 ot invoke the SE
1b804 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e LECT callback on
1b805 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63 p, but do (of c
1b806 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a ourse) invoke.**
1b807 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61 any expr callba
1b808 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63 cks and SELECT c
1b809 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f allbacks that co
1b80a 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69 me from subqueri
1b80b 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 es..** Return WR
1b80c 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 C_Abort or WRC_C
1b80d 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 ontinue..*/.SQLI
1b80e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1b80f 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
1b810 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 Expr(Walker *pWa
1b811 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 lker, Select *p)
1b812 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 {. if( sqlite3W
1b813 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1b814 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 ker, p->pEList)
1b815 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1b816 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 rt;. if( sqlite
1b817 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 3WalkExpr(pWalke
1b818 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20 r, p->pWhere) )
1b819 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1b81a 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 ;. if( sqlite3W
1b81b 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c alkExprList(pWal
1b81c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 ker, p->pGroupBy
1b81d 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1b81e 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 bort;. if( sqli
1b81f 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1b820 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 ker, p->pHaving)
1b821 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1b822 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 ort;. if( sqlit
1b823 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 e3WalkExprList(p
1b824 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 Walker, p->pOrde
1b825 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 rBy) ) return WR
1b826 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 C_Abort;. if( s
1b827 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 qlite3WalkExpr(p
1b828 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 Walker, p->pLimi
1b829 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f t) ) return WRC_
1b82a 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c Abort;. if( sql
1b82b 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1b82c 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 lker, p->pOffset
1b82d 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1b82e 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 bort;. return W
1b82f 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a RC_Continue;.}..
1b830 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 /*.** Walk the p
1b831 61 72 73 65 20 74 72 65 65 73 20 61 73 73 6f 63 arse trees assoc
1b832 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73 iated with all s
1b833 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 ubqueries in the
1b834 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 .** FROM clause
1b835 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d of SELECT statem
1b836 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 ent p. Do not i
1b837 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74 nvoke the select
1b838 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 .** callback on
1b839 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 p, but do invoke
1b83a 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d it on each FROM
1b83b 20 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79 clause subquery
1b83c 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 .** and on any s
1b83d 75 62 71 75 65 72 69 65 73 20 66 75 72 74 68 65 ubqueries furthe
1b83e 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 r down in the tr
1b83f 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 ee. Return .**
1b840 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 WRC_Abort or WRC
1b841 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 _Continue;.*/.SQ
1b842 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1b843 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
1b844 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 ctFrom(Walker *p
1b845 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a Walker, Select *
1b846 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 p){. SrcList *p
1b847 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 Src;. int i;.
1b848 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
1b849 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 tem *pItem;.. p
1b84a 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 Src = p->pSrc;.
1b84b 20 69 66 28 20 70 53 72 63 20 29 7b 0a 20 20 20 if( pSrc ){.
1b84c 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 for(i=pSrc->nSr
1b84d 63 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61 c, pItem=pSrc->a
1b84e 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 ; i>0; i--, pIte
1b84f 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 m++){. if(
1b850 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1b851 74 28 70 57 61 6c 6b 65 72 2c 20 70 49 74 65 6d t(pWalker, pItem
1b852 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 ->pSelect) ){.
1b853 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1b854 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a _Abort;. }.
1b855 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1b856 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b rn WRC_Continue;
1b857 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 .} ../*.** Call
1b858 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1b859 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 72 ) for every expr
1b85a 65 73 73 69 6f 6e 20 69 6e 20 53 65 6c 65 63 74 ession in Select
1b85b 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 2a 2a statement p..**
1b85c 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 57 Invoke sqlite3W
1b85d 61 6c 6b 53 65 6c 65 63 74 28 29 20 66 6f 72 20 alkSelect() for
1b85e 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 subqueries in th
1b85f 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e e FROM clause an
1b860 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 d.** on the comp
1b861 6f 75 6e 64 20 73 65 6c 65 63 74 20 63 68 61 69 ound select chai
1b862 6e 2c 20 70 2d 3e 70 50 72 69 6f 72 2e 0a 2a 2a n, p->pPrior..**
1b863 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 43 .** Return WRC_C
1b864 6f 6e 74 69 6e 75 65 20 75 6e 64 65 72 20 6e 6f ontinue under no
1b865 72 6d 61 6c 20 63 6f 6e 64 69 74 69 6f 6e 73 2e rmal conditions.
1b866 20 20 52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f Return WRC_Abo
1b867 72 74 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 69 rt if.** there i
1b868 73 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75 65 s an abort reque
1b869 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 st..**.** If the
1b86a 20 57 61 6c 6b 65 72 20 64 6f 65 73 20 6e 6f 74 Walker does not
1b86b 20 68 61 76 65 20 61 6e 20 78 53 65 6c 65 63 74 have an xSelect
1b86c 43 61 6c 6c 62 61 63 6b 28 29 20 74 68 65 6e 20 Callback() then
1b86d 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1b86e 69 73 20 61 20 6e 6f 2d 6f 70 20 72 65 74 75 72 is a no-op retur
1b86f 6e 69 6e 67 20 57 52 43 5f 43 6f 6e 74 69 6e 75 ning WRC_Continu
1b870 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1b871 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1b872 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 WalkSelect(Walke
1b873 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 r *pWalker, Sele
1b874 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 ct *p){. int rc
1b875 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 ;. if( p==0 ||
1b876 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74 pWalker->xSelect
1b877 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65 Callback==0 ) re
1b878 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
1b879 65 3b 0a 20 20 72 63 20 3d 20 57 52 43 5f 43 6f e;. rc = WRC_Co
1b87a 6e 74 69 6e 75 65 3b 0a 20 20 77 68 69 6c 65 28 ntinue;. while(
1b87b 20 70 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 p ){. rc =
1b87c 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74 pWalker->xSelect
1b87d 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 Callback(pWalker
1b87e 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , p);. if( rc
1b87f 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 ) break;. if
1b880 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c ( sqlite3WalkSel
1b881 65 63 74 45 78 70 72 28 70 57 61 6c 6b 65 72 2c ectExpr(pWalker,
1b882 20 70 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 p) ) return WRC
1b883 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 _Abort;. if(
1b884 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
1b885 74 46 72 6f 6d 28 70 57 61 6c 6b 65 72 2c 20 70 tFrom(pWalker, p
1b886 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1b887 62 6f 72 74 3b 0a 20 20 20 20 70 20 3d 20 70 2d bort;. p = p-
1b888 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 >pPrior;. }. r
1b889 65 74 75 72 6e 20 72 63 20 26 20 57 52 43 5f 41 eturn rc & WRC_A
1b88a 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a bort;.}../******
1b88b 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
1b88c 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a walker.c *******
1b88d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b88e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b88f 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1b890 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1b891 69 6c 65 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a ile resolve.c **
1b892 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b893 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b894 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1b895 30 30 38 20 41 75 67 75 73 74 20 31 38 0a 2a 2a 008 August 18.**
1b896 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1b897 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1b898 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1b899 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1b89a 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1b89b 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1b89c 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1b89d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1b89e 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1b89f 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1b8a0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1b8a1 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1b8a2 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1b8a3 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1b8a4 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1b8a5 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1b8a6 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1b8a7 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1b8a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b8a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b8aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b8ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
1b8ac 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
1b8ad 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 tains routines u
1b8ae 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 sed for walking
1b8af 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20 the parser tree
1b8b0 61 6e 64 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 61 and.** resolve a
1b8b1 6c 6c 20 69 64 65 6e 74 69 66 69 65 72 73 20 62 ll identifiers b
1b8b2 79 20 61 73 73 6f 63 69 61 74 69 6e 67 20 74 68 y associating th
1b8b3 65 6d 20 77 69 74 68 20 61 20 70 61 72 74 69 63 em with a partic
1b8b4 75 6c 61 72 0a 2a 2a 20 74 61 62 6c 65 20 61 6e ular.** table an
1b8b5 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 d column..**.**
1b8b6 24 49 64 3a 20 72 65 73 6f 6c 76 65 2e 63 2c 76 $Id: resolve.c,v
1b8b7 20 31 2e 31 31 20 32 30 30 38 2f 31 31 2f 31 37 1.11 2008/11/17
1b8b8 20 31 39 3a 31 38 3a 35 35 20 64 61 6e 69 65 6c 19:18:55 daniel
1b8b9 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
1b8ba 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68 65 20 70 /*.** Turn the p
1b8bb 45 78 70 72 20 65 78 70 72 65 73 73 69 6f 6e 20 Expr expression
1b8bc 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f into an alias fo
1b8bd 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f r the iCol-th co
1b8be 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 lumn of the.** r
1b8bf 65 73 75 6c 74 20 73 65 74 20 69 6e 20 70 45 4c esult set in pEL
1b8c0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ist..**.** If th
1b8c1 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c e result set col
1b8c2 75 6d 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 20 umn is a simple
1b8c3 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 column reference
1b8c4 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
1b8c5 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20 ine.** makes an
1b8c6 65 78 61 63 74 20 63 6f 70 79 2e 20 20 42 75 74 exact copy. But
1b8c7 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b for any other k
1b8c8 69 6e 64 20 6f 66 20 65 78 70 72 65 73 73 69 6f ind of expressio
1b8c9 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 n, this.** routi
1b8ca 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f ne make a copy o
1b8cb 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
1b8cc 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 61 column as the a
1b8cd 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a rgument to the.*
1b8ce 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 * TK_AS operator
1b8cf 2e 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 . The TK_AS ope
1b8d0 72 61 74 6f 72 20 63 61 75 73 65 73 20 74 68 65 rator causes the
1b8d1 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 expression to b
1b8d2 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6a e.** evaluated j
1b8d3 75 73 74 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 ust once and the
1b8d4 6e 20 72 65 75 73 65 64 20 66 6f 72 20 65 61 63 n reused for eac
1b8d5 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54 h alias..**.** T
1b8d6 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 73 75 he reason for su
1b8d7 70 70 72 65 73 73 69 6e 67 20 74 68 65 20 54 4b ppressing the TK
1b8d8 5f 41 53 20 74 65 72 6d 20 77 68 65 6e 20 74 68 _AS term when th
1b8d9 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
1b8da 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75 a simple.** colu
1b8db 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 mn reference is
1b8dc 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 so that the colu
1b8dd 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 77 69 6c mn reference wil
1b8de 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 l be recognized
1b8df 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20 62 79 20 as.** usable by
1b8e0 69 6e 64 69 63 65 73 20 77 69 74 68 69 6e 20 74 indices within t
1b8e1 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
1b8e2 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 processing logic
1b8e3 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20 . .**.** Hack:
1b8e4 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 The TK_AS operat
1b8e5 6f 72 20 69 73 20 69 6e 68 69 62 69 74 65 64 20 or is inhibited
1b8e6 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27 if zType[0]=='G'
1b8e7 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a . This means.**
1b8e8 20 74 68 61 74 20 69 6e 20 61 20 47 52 4f 55 50 that in a GROUP
1b8e9 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 20 BY clause, the
1b8ea 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76 expression is ev
1b8eb 61 6c 75 61 74 65 64 20 74 77 69 63 65 2e 20 20 aluated twice.
1b8ec 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 Hence:.**.**
1b8ed 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 SELECT random()
1b8ee 25 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a %5 AS x, count(*
1b8ef 29 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 ) FROM tab GROUP
1b8f0 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65 BY x.**.** Is e
1b8f1 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a quivalent to:.**
1b8f2 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72 .** SELECT r
1b8f3 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 andom()%5 AS x,
1b8f4 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 count(*) FROM ta
1b8f5 62 20 47 52 4f 55 50 20 42 59 20 72 61 6e 64 6f b GROUP BY rando
1b8f6 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 m()%5.**.** The
1b8f7 72 65 73 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d result of random
1b8f8 28 29 25 35 20 69 6e 20 74 68 65 20 47 52 4f 55 ()%5 in the GROU
1b8f9 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 70 P BY clause is p
1b8fa 72 6f 62 61 62 6c 79 20 64 69 66 66 65 72 65 6e robably differen
1b8fb 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 t.** from the re
1b8fc 73 75 6c 74 20 69 6e 20 74 68 65 20 72 65 73 75 sult in the resu
1b8fd 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d 69 67 68 lt-set. We migh
1b8fe 74 20 66 69 78 20 74 68 69 73 20 73 6f 6d 65 64 t fix this somed
1b8ff 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20 ay. Or.** then
1b900 61 67 61 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 again, we might
1b901 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 not....*/.static
1b902 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 41 6c 69 void resolveAli
1b903 61 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 as(. Parse *pPa
1b904 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 rse, /*
1b905 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
1b906 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
1b907 45 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 41 EList, /* A
1b908 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
1b909 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 int iCol,
1b90a 20 20 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c /* A col
1b90b 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c umn in the resul
1b90c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73 t set. 0..pELis
1b90d 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20 t->nExpr-1 */.
1b90e 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 Expr *pExpr,
1b90f 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 /* Transf
1b910 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f 20 61 6e orm this into an
1b911 20 61 6c 69 61 73 20 74 6f 20 74 68 65 20 72 65 alias to the re
1b912 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f sult set */. co
1b913 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 nst char *zType
1b914 20 20 20 20 20 2f 2a 20 22 47 52 4f 55 50 22 20 /* "GROUP"
1b915 6f 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 22 or "ORDER" or ""
1b916 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 */.){. Expr *p
1b917 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 20 20 Orig;
1b918 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 68 20 63 /* The iCol-th c
1b919 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 olumn of the res
1b91a 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 ult set */. Exp
1b91b 72 20 2a 70 44 75 70 3b 20 20 20 20 20 20 20 20 r *pDup;
1b91c 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 /* Copy of p
1b91d 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 Orig */. sqlite
1b91e 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 3 *db;
1b91f 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1b920 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a connection */..
1b921 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d assert( iCol>=
1b922 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74 0 && iCol<pEList
1b923 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 70 4f 72 ->nExpr );. pOr
1b924 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 ig = pEList->a[i
1b925 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 Col].pExpr;. as
1b926 73 65 72 74 28 20 70 4f 72 69 67 21 3d 30 20 29 sert( pOrig!=0 )
1b927 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 69 ;. assert( pOri
1b928 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 52 65 g->flags & EP_Re
1b929 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 62 20 3d solved );. db =
1b92a 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 pParse->db;. p
1b92b 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 Dup = sqlite3Exp
1b92c 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67 29 3b rDup(db, pOrig);
1b92d 0a 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29 . if( pDup==0 )
1b92e 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 return;. if( p
1b92f 44 75 70 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 Dup->op!=TK_COLU
1b930 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d 21 3d MN && zType[0]!=
1b931 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75 70 20 'G' ){. pDup
1b932 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 = sqlite3PExpr(p
1b933 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 44 Parse, TK_AS, pD
1b934 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 up, 0, 0);. i
1b935 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74 f( pDup==0 ) ret
1b936 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 4c urn;. if( pEL
1b937 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c ist->a[iCol].iAl
1b938 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ias==0 ){.
1b939 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e pEList->a[iCol].
1b93a 69 41 6c 69 61 73 20 3d 20 2b 2b 70 50 61 72 73 iAlias = ++pPars
1b93b 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 20 20 7d e->nAlias;. }
1b93c 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61 62 6c . pDup->iTabl
1b93d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 e = pEList->a[iC
1b93e 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a ol].iAlias;. }.
1b93f 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 if( pExpr->fla
1b940 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 gs & EP_ExpColla
1b941 74 65 20 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e te ){. pDup->
1b942 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 pColl = pExpr->p
1b943 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e Coll;. pDup->
1b944 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 flags |= EP_ExpC
1b945 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 20 73 71 ollate;. }. sq
1b946 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 lite3ExprClear(d
1b947 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d b, pExpr);. mem
1b948 63 70 79 28 70 45 78 70 72 2c 20 70 44 75 70 2c cpy(pExpr, pDup,
1b949 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29 29 sizeof(*pExpr))
1b94a 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1b94b 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a e(db, pDup);.}..
1b94c 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 /*.** Given the
1b94d 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e name of a column
1b94e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 2e 59 of the form X.Y
1b94f 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 .Z or Y.Z or jus
1b950 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 t Z, look up.**
1b951 74 68 61 74 20 6e 61 6d 65 20 69 6e 20 74 68 65 that name in the
1b952 20 73 65 74 20 6f 66 20 73 6f 75 72 63 65 20 74 set of source t
1b953 61 62 6c 65 73 20 69 6e 20 70 53 72 63 4c 69 73 ables in pSrcLis
1b954 74 20 61 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 t and make the p
1b955 45 78 70 72 20 0a 2a 2a 20 65 78 70 72 65 73 73 Expr .** express
1b956 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72 20 62 ion node refer b
1b957 61 63 6b 20 74 6f 20 74 68 61 74 20 73 6f 75 72 ack to that sour
1b958 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 ce column. The
1b959 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 following change
1b95a 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f s.** are made to
1b95b 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 pExpr:.**.**
1b95c 20 70 45 78 70 72 2d 3e 69 44 62 20 20 20 20 20 pExpr->iDb
1b95d 20 20 20 20 20 20 53 65 74 20 74 68 65 20 69 6e Set the in
1b95e 64 65 78 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d dex in db->aDb[]
1b95f 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1b960 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 X.**
1b961 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 (e
1b962 76 65 6e 20 69 66 20 58 20 69 73 20 69 6d 70 6c ven if X is impl
1b963 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70 ied)..** pExp
1b964 72 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20 r->iTable
1b965 20 53 65 74 20 74 6f 20 74 68 65 20 63 75 72 73 Set to the curs
1b966 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 or number for th
1b967 65 20 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 e table obtained
1b968 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1b969 20 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d from
1b96a 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 pSrcList..**
1b96b 20 70 45 78 70 72 2d 3e 70 54 61 62 20 20 20 20 pExpr->pTab
1b96c 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 Points to
1b96d 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 the Table struct
1b96e 75 72 65 20 6f 66 20 58 2e 59 20 28 65 76 65 6e ure of X.Y (even
1b96f 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 if.**
1b970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 58 X
1b971 20 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 69 6d and/or Y are im
1b972 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45 plied.).** pE
1b973 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 xpr->iColumn
1b974 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 6f Set to the co
1b975 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 lumn number with
1b976 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a in the table..**
1b977 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 pExpr->op
1b978 20 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 Set to
1b979 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 TK_COLUMN..**
1b97a 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 20 20 pExpr->pLeft
1b97b 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 Any expres
1b97c 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 sion this points
1b97d 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 0a 2a to is deleted.*
1b97e 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 * pExpr->pRig
1b97f 68 74 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 ht Any ex
1b980 70 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f pression this po
1b981 69 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 ints to is delet
1b982 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 44 ed..**.** The pD
1b983 62 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 6e 61 bToken is the na
1b984 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
1b985 73 65 20 28 74 68 65 20 22 58 22 29 2e 20 20 54 se (the "X"). T
1b986 68 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 his value may be
1b987 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 .** NULL meaning
1b988 20 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 that name is of
1b989 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 the form Y.Z or
1b98a 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62 Z. Any availab
1b98b 6c 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 le database.** c
1b98c 61 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68 65 an be used. The
1b98d 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20 pTableToken is
1b98e 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1b98f 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 2e table (the "Y").
1b990 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20 This.** value
1b991 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 70 can be NULL if p
1b992 44 62 54 6f 6b 65 6e 20 69 73 20 61 6c 73 6f 20 DbToken is also
1b993 4e 55 4c 4c 2e 20 20 49 66 20 70 54 61 62 6c 65 NULL. If pTable
1b994 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 69 74 Token is NULL it
1b995 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74 .** means that t
1b996 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e he form of the n
1b997 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68 61 ame is Z and tha
1b998 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61 t columns from a
1b999 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20 ny table.** can
1b99a 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 be used..**.** I
1b99b 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f f the name canno
1b99c 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75 6e t be resolved un
1b99d 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61 ambiguously, lea
1b99e 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ve an error mess
1b99f 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 age.** in pParse
1b9a0 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d and return non-
1b9a1 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 zero. Return ze
1b9a2 72 6f 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a ro on success..*
1b9a3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f /.static int loo
1b9a4 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 kupName(. Parse
1b9a5 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
1b9a6 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 /* The parsing c
1b9a7 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 ontext */. Toke
1b9a8 6e 20 2a 70 44 62 54 6f 6b 65 6e 2c 20 20 20 20 n *pDbToken,
1b9a9 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1b9aa 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
1b9ab 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 ing table, or NU
1b9ac 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 LL */. Token *p
1b9ad 54 61 62 6c 65 54 6f 6b 65 6e 2c 20 20 2f 2a 20 TableToken, /*
1b9ae 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f Name of table co
1b9af 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c ntaining column,
1b9b0 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f or NULL */. To
1b9b1 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54 6f 6b 65 ken *pColumnToke
1b9b2 6e 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 n, /* Name of th
1b9b3 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e e column. */. N
1b9b4 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c ameContext *pNC,
1b9b5 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 /* The name
1b9b6 63 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20 context used to
1b9b7 72 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 resolve the name
1b9b8 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 */. Expr *pExp
1b9b9 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 r /* Ma
1b9ba 6b 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64 ke this EXPR nod
1b9bb 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 e point to the s
1b9bc 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a elected column *
1b9bd 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 62 /.){. char *zDb
1b9be 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e = 0; /* N
1b9bf 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
1b9c0 61 73 65 2e 20 20 54 68 65 20 22 58 22 20 69 6e ase. The "X" in
1b9c1 20 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72 X.Y.Z */. char
1b9c2 20 2a 7a 54 61 62 20 3d 20 30 3b 20 20 20 20 20 *zTab = 0;
1b9c3 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1b9c4 74 61 62 6c 65 2e 20 20 54 68 65 20 22 59 22 20 table. The "Y"
1b9c5 69 6e 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 in X.Y.Z or Y.Z
1b9c6 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 */. char *zCol
1b9c7 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d = 0; /* Nam
1b9c8 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e e of the column.
1b9c9 20 20 54 68 65 20 22 5a 22 20 2a 2f 0a 20 20 69 The "Z" */. i
1b9ca 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 nt i, j;
1b9cb 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1b9cc 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e ters */. int cn
1b9cd 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 t = 0;
1b9ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1b9cf 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e umber of matchin
1b9d0 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a g column names *
1b9d1 2f 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d /. int cntTab =
1b9d2 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1b9d3 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1b9d4 6f 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c of matching tabl
1b9d5 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c e names */. sql
1b9d6 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
1b9d7 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f e->db; /
1b9d8 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
1b9d9 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 onnection */. s
1b9da 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
1b9db 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 em *pItem;
1b9dc 20 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70 /* Use for loop
1b9dd 69 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73 ing over pSrcLis
1b9de 74 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72 t items */. str
1b9df 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
1b9e0 20 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f *pMatch = 0; /
1b9e1 2a 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70 * The matching p
1b9e2 53 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a SrcList item */.
1b9e3 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 NameContext *p
1b9e4 54 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20 TopNC = pNC;
1b9e5 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d /* First nam
1b9e6 65 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 econtext in the
1b9e7 6c 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61 list */. Schema
1b9e8 20 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20 *pSchema = 0;
1b9e9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1b9ea 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 chema of the exp
1b9eb 72 65 73 73 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 ression */.. as
1b9ec 73 65 72 74 28 20 70 43 6f 6c 75 6d 6e 54 6f 6b sert( pColumnTok
1b9ed 65 6e 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b en && pColumnTok
1b9ee 65 6e 2d 3e 7a 20 29 3b 20 2f 2a 20 54 68 65 20 en->z ); /* The
1b9ef 5a 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f Z in X.Y.Z canno
1b9f0 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20 t be NULL */..
1b9f1 2f 2a 20 44 65 71 75 6f 74 65 20 61 6e 64 20 7a /* Dequote and z
1b9f2 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20 74 68 ero-terminate th
1b9f3 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 7a 44 62 e names */. zDb
1b9f4 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 = sqlite3NameFr
1b9f5 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 62 54 omToken(db, pDbT
1b9f6 6f 6b 65 6e 29 3b 0a 20 20 7a 54 61 62 20 3d 20 oken);. zTab =
1b9f7 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
1b9f8 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 54 oken(db, pTableT
1b9f9 6f 6b 65 6e 29 3b 0a 20 20 7a 43 6f 6c 20 3d 20 oken);. zCol =
1b9fa 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
1b9fb 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 75 6d 6e oken(db, pColumn
1b9fc 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 64 62 Token);. if( db
1b9fd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1b9fe 7b 0a 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 {. goto looku
1b9ff 70 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 7d 0a 0a pname_end;. }..
1ba00 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 /* Initialize
1ba01 74 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d the node to no-m
1ba02 61 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d atch */. pExpr-
1ba03 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 >iTable = -1;.
1ba04 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b pExpr->pTab = 0;
1ba05 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20 .. /* Start at
1ba06 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 the inner-most c
1ba07 6f 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 ontext and move
1ba08 6f 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20 outward until a
1ba09 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a match is found *
1ba0a 2f 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 /. while( pNC &
1ba0b 26 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 & cnt==0 ){.
1ba0c 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
1ba0d 3b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 ;. SrcList *p
1ba0e 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 SrcList = pNC->p
1ba0f 53 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 SrcList;.. if
1ba10 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 ( pSrcList ){.
1ba11 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 for(i=0, pIt
1ba12 65 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 em=pSrcList->a;
1ba13 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 i<pSrcList->nSrc
1ba14 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
1ba15 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a . Table *
1ba16 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e pTab;. in
1ba17 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43 t iDb;. C
1ba18 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a olumn *pCol;. .
1ba19 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 pTab = p
1ba1a 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 Item->pTab;.
1ba1b 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 assert( pTab
1ba1c 21 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 !=0 && pTab->zNa
1ba1d 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 me!=0 );.
1ba1e 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
1ba1f 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 hemaToIndex(db,
1ba20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
1ba21 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1ba22 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a pTab->nCol>0 );.
1ba23 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 if( zTab
1ba24 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ){. if
1ba25 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 ( pItem->zAlias
1ba26 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 ){. c
1ba27 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 har *zTabName =
1ba28 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 pItem->zAlias;.
1ba29 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 if( s
1ba2a 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 qlite3StrICmp(zT
1ba2b 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 abName, zTab)!=0
1ba2c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
1ba2d 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1ba2e 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a char *
1ba2f 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d zTabName = pTab-
1ba30 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 >zName;.
1ba31 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65 if( zTabName
1ba32 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 ==0 || sqlite3St
1ba33 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 rICmp(zTabName,
1ba34 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69 zTab)!=0 ) conti
1ba35 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nue;.
1ba36 20 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 if( zDb!=0 && s
1ba37 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 qlite3StrICmp(db
1ba38 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
1ba39 2c 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 , zDb)!=0 ){.
1ba3a 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 conti
1ba3b 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nue;.
1ba3c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1ba3d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1ba3e 20 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b if( 0==(cntTab+
1ba3f 2b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 +) ){.
1ba40 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
1ba41 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a pItem->iCursor;.
1ba42 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1ba43 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 >pTab = pTab;.
1ba44 20 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 pSchema
1ba45 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b = pTab->pSchema;
1ba46 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 . pMatc
1ba47 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 h = pItem;.
1ba48 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 }. for
1ba49 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d (j=0, pCol=pTab-
1ba4a 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e >aCol; j<pTab->n
1ba4b 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b Col; j++, pCol++
1ba4c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
1ba4d 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
1ba4e 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f pCol->zName, zCo
1ba4f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 l)==0 ){.
1ba50 20 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 IdList *pUs
1ba51 69 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ing;.
1ba52 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 cnt++;.
1ba53 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
1ba54 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 e = pItem->iCurs
1ba55 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 or;.
1ba56 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 pExpr->pTab = pT
1ba57 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ab;.
1ba58 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a pMatch = pItem;.
1ba59 20 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68 pSch
1ba5a 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 ema = pTab->pSch
1ba5b 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ema;.
1ba5c 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 /* Substitute t
1ba5d 68 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e he rowid (column
1ba5e 20 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 -1) for the INT
1ba5f 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
1ba60 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
1ba61 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d pExpr->iColumn =
1ba62 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 j==pTab->iPKey
1ba63 3f 20 2d 31 20 3a 20 6a 3b 0a 20 20 20 20 20 20 ? -1 : j;.
1ba64 20 20 20 20 20 20 69 66 28 20 69 3c 70 53 72 63 if( i<pSrc
1ba65 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a List->nSrc-1 ){.
1ba66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
1ba67 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 ( pItem[1].joint
1ba68 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c ype & JT_NATURAL
1ba69 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1ba6a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d /* If this m
1ba6b 61 74 63 68 20 6f 63 63 75 72 72 65 64 20 69 6e atch occurred in
1ba6c 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 the left table
1ba6d 6f 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 of a natural joi
1ba6e 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n,.
1ba6f 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 ** then skip
1ba70 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 the right table
1ba71 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 to avoid a dupli
1ba72 63 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 cate match */.
1ba73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 pI
1ba74 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 tem++;.
1ba75 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 i++;.
1ba76 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 }else
1ba77 69 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70 49 if( (pUsing = pI
1ba78 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d tem[1].pUsing)!=
1ba79 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
1ba7a 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 /* If this
1ba7b 6d 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 match occurs on
1ba7c 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 a column that is
1ba7d 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c in the USING cl
1ba7e 61 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 20 ause.
1ba7f 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 ** of a joi
1ba80 6e 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61 72 n, skip the sear
1ba81 63 68 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 ch of the right
1ba82 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 table of the joi
1ba83 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n.
1ba84 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 ** to avoid a
1ba85 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 duplicate match
1ba86 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 there. */.
1ba87 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b int k;
1ba88 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1ba89 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 for(k=0; k<pUsi
1ba8a 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 ng->nId; k++){.
1ba8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ba8c 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
1ba8d 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d Cmp(pUsing->a[k]
1ba8e 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 .zName, zCol)==0
1ba8f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1ba90 20 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b pItem++;
1ba91 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1ba92 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 i++;.
1ba93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 br
1ba94 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 eak;.
1ba95 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1ba96 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1ba97 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1ba98 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1ba99 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1ba9a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1ba9b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1ba9c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1ba9d 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 OMIT_TRIGGER.
1ba9e 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e /* If we have n
1ba9f 6f 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f 6c ot already resol
1baa0 76 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 ved the name, th
1baa1 65 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a en maybe . **
1baa2 20 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f it is a new.* o
1baa3 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 r old.* trigger
1baa4 61 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65 6e argument referen
1baa5 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 ce. */. if
1baa6 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 ( zDb==0 && zTab
1baa7 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 !=0 && cnt==0 &&
1baa8 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 pParse->trigSta
1baa9 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 54 ck!=0 ){. T
1baaa 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54 72 riggerStack *pTr
1baab 69 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50 61 iggerStack = pPa
1baac 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a rse->trigStack;.
1baad 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 Table *pTa
1baae 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 33 32 b = 0;. u32
1baaf 20 2a 70 69 43 6f 6c 4d 61 73 6b 3b 0a 20 20 20 *piColMask;.
1bab0 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53 if( pTriggerS
1bab1 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20 21 3d 20 tack->newIdx !=
1bab2 2d 31 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 -1 && sqlite3Str
1bab3 49 43 6d 70 28 22 6e 65 77 22 2c 20 7a 54 61 62 ICmp("new", zTab
1bab4 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 ) == 0 ){.
1bab5 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1bab6 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d = pTriggerStack-
1bab7 3e 6e 65 77 49 64 78 3b 0a 20 20 20 20 20 20 20 >newIdx;.
1bab8 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65 assert( pTrigge
1bab9 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a rStack->pTab );.
1baba 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 pTab = p
1babb 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 TriggerStack->pT
1babc 61 62 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f ab;. piCo
1babd 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67 lMask = &(pTrigg
1babe 65 72 53 74 61 63 6b 2d 3e 6e 65 77 43 6f 6c 4d erStack->newColM
1babf 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ask);. }els
1bac0 65 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74 e if( pTriggerSt
1bac1 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 21 3d 20 2d ack->oldIdx != -
1bac2 31 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 1 && sqlite3StrI
1bac3 43 6d 70 28 22 6f 6c 64 22 2c 20 7a 54 61 62 29 Cmp("old", zTab)
1bac4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
1bac5 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 Expr->iTable = p
1bac6 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c TriggerStack->ol
1bac7 64 49 64 78 3b 0a 20 20 20 20 20 20 20 20 61 73 dIdx;. as
1bac8 73 65 72 74 28 20 70 54 72 69 67 67 65 72 53 74 sert( pTriggerSt
1bac9 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a 20 20 20 ack->pTab );.
1baca 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 72 69 pTab = pTri
1bacb 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 3b ggerStack->pTab;
1bacc 0a 20 20 20 20 20 20 20 20 70 69 43 6f 6c 4d 61 . piColMa
1bacd 73 6b 20 3d 20 26 28 70 54 72 69 67 67 65 72 53 sk = &(pTriggerS
1bace 74 61 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d 61 73 6b tack->oldColMask
1bacf 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
1bad0 20 20 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20 if( pTab ){ .
1bad1 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b int iCol;
1bad2 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 . Column
1bad3 2a 70 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 *pCol = pTab->aC
1bad4 6f 6c 3b 0a 0a 20 20 20 20 20 20 20 20 70 53 63 ol;.. pSc
1bad5 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 hema = pTab->pSc
1bad6 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e hema;. cn
1bad7 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 tTab++;.
1bad8 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c for(iCol=0; iCol
1bad9 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 < pTab->nCol; i
1bada 43 6f 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 20 7b Col++, pCol++) {
1badb 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 . if( s
1badc 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 qlite3StrICmp(pC
1badd 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 ol->zName, zCol)
1bade 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1badf 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 cnt++;.
1bae0 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f pExpr->iCo
1bae1 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3d 3d 70 54 61 lumn = iCol==pTa
1bae2 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20 b->iPKey ? -1 :
1bae3 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 iCol;.
1bae4 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 pExpr->pTab =
1bae5 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 pTab;.
1bae6 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b if( iCol>=0 ){
1bae7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 . t
1bae8 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 estcase( iCol==3
1bae9 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 1 );.
1baea 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f testcase( iCo
1baeb 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 l==32 );.
1baec 20 20 20 20 20 20 20 2a 70 69 43 6f 6c 4d 61 73 *piColMas
1baed 6b 20 7c 3d 20 28 28 75 33 32 29 31 3c 3c 69 43 k |= ((u32)1<<iC
1baee 6f 6c 29 20 7c 20 28 69 43 6f 6c 3e 3d 33 32 3f ol) | (iCol>=32?
1baef 30 78 66 66 66 66 66 66 66 66 3a 30 29 3b 0a 20 0xffffffff:0);.
1baf0 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1baf1 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
1baf2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1baf3 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1baf4 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 }.#endif /* !d
1baf5 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1baf6 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a IT_TRIGGER) */..
1baf7 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 /*. ** Pe
1baf8 72 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 rhaps the name i
1baf9 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
1bafa 20 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a the ROWID. *
1bafb 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 /. if( cnt==0
1bafc 20 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 && cntTab==1 &&
1bafd 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 sqlite3IsRowid(
1bafe 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 zCol) ){. c
1baff 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 nt = 1;. pE
1bb00 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d xpr->iColumn = -
1bb01 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 1;. pExpr->
1bb02 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 affinity = SQLIT
1bb03 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 E_AFF_INTEGER;.
1bb04 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 }.. /*.
1bb05 20 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 ** If the input
1bb06 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 is of the form
1bb07 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e Z (not Y.Z or X.
1bb08 59 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 Y.Z) then the na
1bb09 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 me Z. ** migh
1bb0a 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 t refer to an re
1bb0b 73 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 sult-set alias.
1bb0c 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 This happens, f
1bb0d 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e or example, when
1bb0e 0a 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 . ** we are r
1bb0f 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 esolving names i
1bb10 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 n the WHERE clau
1bb11 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 se of the follow
1bb12 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 ing command:.
1bb13 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 **. ** S
1bb14 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 ELECT a+b AS x F
1bb15 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 ROM table WHERE
1bb16 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 x<10;. **.
1bb17 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b ** In cases lik
1bb18 65 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 e this, replace
1bb19 70 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 pExpr with a cop
1bb1a 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 y of the express
1bb1b 69 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 ion that. **
1bb1c 66 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 forms the result
1bb1d 20 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 set entry ("a+b
1bb1e 22 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 " in the example
1bb1f 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d ) and return imm
1bb20 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a ediately.. **
1bb21 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 Note that the e
1bb22 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 xpression in the
1bb23 20 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 result set shou
1bb24 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 ld have already
1bb25 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f been. ** reso
1bb26 6c 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 lved by the time
1bb27 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
1bb28 65 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 e is resolved..
1bb29 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e */. if( cn
1bb2a 74 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 t==0 && (pEList
1bb2b 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d = pNC->pEList)!=
1bb2c 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 0 && zTab==0 ){.
1bb2d 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
1bb2e 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 <pEList->nExpr;
1bb2f 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 j++){. ch
1bb30 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 ar *zAs = pEList
1bb31 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 ->a[j].zName;.
1bb32 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 if( zAs!=0
1bb33 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1bb34 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 mp(zAs, zCol)==0
1bb35 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 ){. Ex
1bb36 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 pr *pOrig;.
1bb37 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 assert( pEx
1bb38 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 pr->pLeft==0 &&
1bb39 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 pExpr->pRight==0
1bb3a 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 );. as
1bb3b 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 69 sert( pExpr->pLi
1bb3c 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 st==0 );.
1bb3d 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
1bb3e 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a ->pSelect==0 );.
1bb3f 20 20 20 20 20 20 20 20 20 20 70 4f 72 69 67 20 pOrig
1bb40 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 = pEList->a[j].p
1bb41 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 Expr;.
1bb42 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 if( !pNC->allowA
1bb43 67 67 20 26 26 20 45 78 70 72 48 61 73 50 72 6f gg && ExprHasPro
1bb44 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45 50 5f perty(pOrig, EP_
1bb45 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 Agg) ){.
1bb46 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1bb47 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 Msg(pParse, "mis
1bb48 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64 20 61 use of aliased a
1bb49 67 67 72 65 67 61 74 65 20 25 73 22 2c 20 7a 41 ggregate %s", zA
1bb4a 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 s);.
1bb4b 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1bb4c 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 , zCol);.
1bb4d 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 return 2;.
1bb4e 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1bb4f 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 resolveAlia
1bb50 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 s(pParse, pEList
1bb51 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29 3b , j, pExpr, "");
1bb52 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d . cnt =
1bb53 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 1;. pM
1bb54 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 atch = 0;.
1bb55 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62 assert( zTab
1bb56 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b ==0 && zDb==0 );
1bb57 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1bb58 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32 lookupname_end_2
1bb59 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1bb5a 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20 } . }..
1bb5b 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68 /* Advance to th
1bb5c 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74 e next name cont
1bb5d 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77 ext. The loop w
1bb5e 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69 ill exit when ei
1bb5f 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68 ther. ** we h
1bb60 61 76 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74 ave a match (cnt
1bb61 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72 >0) or when we r
1bb62 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63 un out of name c
1bb63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a ontexts.. */.
1bb64 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 if( cnt==0 )
1bb65 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e {. pNC = pN
1bb66 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a C->pNext;. }.
1bb67 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 }.. /*. ** I
1bb68 66 20 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55 f X and Y are NU
1bb69 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 LL (in other wor
1bb6a 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63 ds if only the c
1bb6b 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a olumn name Z is.
1bb6c 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61 ** supplied) a
1bb6d 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 nd the value of
1bb6e 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e Z is enclosed in
1bb6f 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20 double-quotes,
1bb70 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61 then. ** Z is a
1bb71 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 string literal
1bb72 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61 if it doesn't ma
1bb73 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e tch any column n
1bb74 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20 ames. In that.
1bb75 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65 ** case, we nee
1bb76 64 20 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68 d to return righ
1bb77 74 20 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d t away and not m
1bb78 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 ake any changes
1bb79 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20 to. ** pExpr..
1bb7a 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65 **. ** Because
1bb7b 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 no reference wa
1bb7c 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20 s made to outer
1bb7d 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e contexts, the pN
1bb7e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65 C->nRef. ** fie
1bb7f 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e lds are not chan
1bb80 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65 ged in any conte
1bb81 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 xt.. */. if( c
1bb82 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 nt==0 && zTab==0
1bb83 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e && pColumnToken
1bb84 2d 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29 7b 0a 20 ->z[0]=='"' ){.
1bb85 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1bb86 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 (db, zCol);.
1bb87 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 pExpr->op = TK_S
1bb88 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 78 70 72 TRING;. pExpr
1bb89 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 ->pTab = 0;.
1bb8a 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
1bb8b 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 /*. ** cnt==0
1bb8c 6d 65 61 6e 73 20 74 68 65 72 65 20 77 61 73 20 means there was
1bb8d 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e not match. cnt>
1bb8e 31 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 65 1 means there we
1bb8f 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d re two or. ** m
1bb90 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 20 45 69 ore matches. Ei
1bb91 74 68 65 72 20 77 61 79 2c 20 77 65 20 68 61 76 ther way, we hav
1bb92 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f e an error.. */
1bb93 0a 20 20 69 66 28 20 63 6e 74 21 3d 31 20 29 7b . if( cnt!=1 ){
1bb94 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
1bb95 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45 72 72 20 *zErr;. zErr
1bb96 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 73 = cnt==0 ? "no s
1bb97 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 61 uch column" : "a
1bb98 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 mbiguous column
1bb99 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66 28 20 7a name";. if( z
1bb9a 44 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 Db ){. sqli
1bb9b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1bb9c 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 2e 25 se, "%s: %s.%s.%
1bb9d 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62 2c 20 7a s", zErr, zDb, z
1bb9e 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 Tab, zCol);.
1bb9f 7d 65 6c 73 65 20 69 66 28 20 7a 54 61 62 20 29 }else if( zTab )
1bba0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1bba1 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1bba2 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 45 72 "%s: %s.%s", zEr
1bba3 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a r, zTab, zCol);.
1bba4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1bba5 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1bba6 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 (pParse, "%s: %s
1bba7 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a ", zErr, zCol);.
1bba8 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 70 4e 43 }. pTopNC
1bba9 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 ->nErr++;. }..
1bbaa 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 /* If a column
1bbab 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 69 6e 20 from a table in
1bbac 70 53 72 63 4c 69 73 74 20 69 73 20 72 65 66 65 pSrcList is refe
1bbad 72 65 6e 63 65 64 2c 20 74 68 65 6e 20 72 65 63 renced, then rec
1bbae 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73 20 66 61 ord. ** this fa
1bbaf 63 74 20 69 6e 20 74 68 65 20 70 53 72 63 4c 69 ct in the pSrcLi
1bbb0 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 62 st.a[].colUsed b
1bbb1 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20 itmask. Column
1bbb2 30 20 63 61 75 73 65 73 0a 20 20 2a 2a 20 62 69 0 causes. ** bi
1bbb3 74 20 30 20 74 6f 20 62 65 20 73 65 74 2e 20 20 t 0 to be set.
1bbb4 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 20 62 69 Column 1 sets bi
1bbb5 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 t 1. And so for
1bbb6 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 2a 2a th. If the. **
1bbb7 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 column number i
1bbb8 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 s greater than t
1bbb9 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 he number of bit
1bbba 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 73 6b s in the bitmask
1bbbb 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 20 74 . ** then set t
1bbbc 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69 he high-order bi
1bbbd 74 20 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b t of the bitmask
1bbbe 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 45 78 .. */. if( pEx
1bbbf 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 pr->iColumn>=0 &
1bbc0 26 20 70 4d 61 74 63 68 21 3d 30 20 29 7b 0a 20 & pMatch!=0 ){.
1bbc1 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 72 int n = pExpr
1bbc2 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74 ->iColumn;. t
1bbc3 65 73 74 63 61 73 65 28 20 6e 3d 3d 42 4d 53 2d estcase( n==BMS-
1bbc4 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 3d 1 );. if( n>=
1bbc5 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 6e 20 3d BMS ){. n =
1bbc6 20 42 4d 53 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 BMS-1;. }.
1bbc7 20 20 61 73 73 65 72 74 28 20 70 4d 61 74 63 68 assert( pMatch
1bbc8 2d 3e 69 43 75 72 73 6f 72 3d 3d 70 45 78 70 72 ->iCursor==pExpr
1bbc9 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 ->iTable );.
1bbca 70 4d 61 74 63 68 2d 3e 63 6f 6c 55 73 65 64 20 pMatch->colUsed
1bbcb 7c 3d 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c |= ((Bitmask)1)<
1bbcc 3c 6e 3b 0a 20 20 7d 0a 0a 6c 6f 6f 6b 75 70 6e <n;. }..lookupn
1bbcd 61 6d 65 5f 65 6e 64 3a 0a 20 20 2f 2a 20 43 6c ame_end:. /* Cl
1bbce 65 61 6e 20 75 70 20 61 6e 64 20 72 65 74 75 72 ean up and retur
1bbcf 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 n. */. sqlite3
1bbd0 44 62 46 72 65 65 28 64 62 2c 20 7a 44 62 29 3b DbFree(db, zDb);
1bbd1 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1bbd2 28 64 62 2c 20 7a 54 61 62 29 3b 0a 20 20 73 71 (db, zTab);. sq
1bbd3 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1bbd4 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 db, pExpr->pLeft
1bbd5 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 );. pExpr->pLef
1bbd6 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 t = 0;. sqlite3
1bbd7 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1bbd8 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 Expr->pRight);.
1bbd9 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d pExpr->pRight =
1bbda 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e 6f 70 20 0;. pExpr->op
1bbdb 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 6c 6f 6f = TK_COLUMN;.loo
1bbdc 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32 3a 0a 20 kupname_end_2:.
1bbdd 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1bbde 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 69 66 28 20 b, zCol);. if(
1bbdf 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 20 61 73 cnt==1 ){. as
1bbe0 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a sert( pNC!=0 );.
1bbe1 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 52 sqlite3AuthR
1bbe2 65 61 64 28 70 50 61 72 73 65 2c 20 70 45 78 70 ead(pParse, pExp
1bbe3 72 2c 20 70 53 63 68 65 6d 61 2c 20 70 4e 43 2d r, pSchema, pNC-
1bbe4 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20 20 20 20 >pSrcList);.
1bbe5 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 /* Increment the
1bbe6 20 6e 52 65 66 20 76 61 6c 75 65 20 6f 6e 20 61 nRef value on a
1bbe7 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 ll name contexts
1bbe8 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75 70 20 74 from TopNC up t
1bbe9 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 69 o. ** the poi
1bbea 6e 74 20 77 68 65 72 65 20 74 68 65 20 6e 61 6d nt where the nam
1bbeb 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f 0a 20 20 e matched. */.
1bbec 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 for(;;){.
1bbed 20 61 73 73 65 72 74 28 20 70 54 6f 70 4e 43 21 assert( pTopNC!
1bbee 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 54 6f 70 =0 );. pTop
1bbef 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 NC->nRef++;.
1bbf0 20 20 69 66 28 20 70 54 6f 70 4e 43 3d 3d 70 4e if( pTopNC==pN
1bbf1 43 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 C ) break;.
1bbf2 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f 70 4e 43 pTopNC = pTopNC
1bbf3 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 ->pNext;. }.
1bbf4 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1bbf5 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 else {. retu
1bbf6 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a rn 1;. }.}../*.
1bbf7 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1bbf8 69 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 is callback for
1bbf9 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1bbfa 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 )..**.** Resolve
1bbfb 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 73 20 symbolic names
1bbfc 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6f into TK_COLUMN o
1bbfd 70 65 72 61 74 6f 72 73 20 66 6f 72 20 74 68 65 perators for the
1bbfe 20 63 75 72 72 65 6e 74 0a 2a 2a 20 6e 6f 64 65 current.** node
1bbff 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 in the expressi
1bc00 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e on tree. Return
1bc01 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 0 to continue t
1bc02 68 65 20 73 65 61 72 63 68 20 64 6f 77 6e 0a 2a he search down.*
1bc03 2a 20 74 68 65 20 74 72 65 65 20 6f 72 20 32 20 * the tree or 2
1bc04 74 6f 20 61 62 6f 72 74 20 74 68 65 20 74 72 65 to abort the tre
1bc05 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 e walk..**.** Th
1bc06 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 is routine also
1bc07 64 6f 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b does error check
1bc08 69 6e 67 20 61 6e 64 20 6e 61 6d 65 20 72 65 73 ing and name res
1bc09 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 66 olution for.** f
1bc0a 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 2e 20 20 unction names.
1bc0b 54 68 65 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 The operator for
1bc0c 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
1bc0d 69 6f 6e 73 20 69 73 20 63 68 61 6e 67 65 64 0a ions is changed.
1bc0e 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e ** to TK_AGG_FUN
1bc0f 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 CTION..*/.static
1bc10 20 69 6e 74 20 72 65 73 6f 6c 76 65 45 78 70 72 int resolveExpr
1bc11 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 Step(Walker *pWa
1bc12 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 lker, Expr *pExp
1bc13 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 r){. NameContex
1bc14 74 20 2a 70 4e 43 3b 0a 20 20 50 61 72 73 65 20 t *pNC;. Parse
1bc15 2a 70 50 61 72 73 65 3b 0a 0a 20 20 70 4e 43 20 *pParse;.. pNC
1bc16 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 = pWalker->u.pNC
1bc17 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 ;. assert( pNC!
1bc18 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65 20 3d =0 );. pParse =
1bc19 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 pNC->pParse;.
1bc1a 61 73 73 65 72 74 28 20 70 50 61 72 73 65 3d 3d assert( pParse==
1bc1b 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 20 pWalker->pParse
1bc1c 29 3b 0a 0a 20 20 69 66 28 20 45 78 70 72 48 61 );.. if( ExprHa
1bc1d 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 sAnyProperty(pEx
1bc1e 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 pr, EP_Resolved)
1bc1f 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 ) return WRC_Pr
1bc20 75 6e 65 3b 0a 20 20 45 78 70 72 53 65 74 50 72 une;. ExprSetPr
1bc21 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1bc22 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23 69 66 6e _Resolved);.#ifn
1bc23 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 def NDEBUG. if(
1bc24 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 20 26 pNC->pSrcList &
1bc25 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d & pNC->pSrcList-
1bc26 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a 20 20 20 >nAlloc>0 ){.
1bc27 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 SrcList *pSrcLi
1bc28 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 st = pNC->pSrcLi
1bc29 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 st;. int i;.
1bc2a 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e for(i=0; i<pN
1bc2b 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 C->pSrcList->nSr
1bc2c 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 c; i++){. a
1bc2d 73 73 65 72 74 28 20 70 53 72 63 4c 69 73 74 2d ssert( pSrcList-
1bc2e 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3e 3d 30 >a[i].iCursor>=0
1bc2f 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b && pSrcList->a[
1bc30 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50 61 72 73 i].iCursor<pPars
1bc31 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20 20 7d 0a e->nTab);. }.
1bc32 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 77 69 }.#endif. swi
1bc33 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 tch( pExpr->op )
1bc34 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 {..#if defined(S
1bc35 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 QLITE_ENABLE_UPD
1bc36 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 ATE_DELETE_LIMIT
1bc37 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
1bc38 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
1bc39 52 59 29 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 RY). /* The s
1bc3a 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f 72 20 pecial operator
1bc3b 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 20 75 73 65 TK_ROW means use
1bc3c 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 the rowid for t
1bc3d 68 65 20 66 69 72 73 74 0a 20 20 20 20 2a 2a 20 he first. **
1bc3e 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 46 52 column in the FR
1bc3f 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 69 73 OM clause. This
1bc40 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 is used by the
1bc41 4c 49 4d 49 54 20 61 6e 64 20 4f 52 44 45 52 20 LIMIT and ORDER
1bc42 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 BY. ** clause
1bc43 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 6e 20 55 processing on U
1bc44 50 44 41 54 45 20 61 6e 64 20 44 45 4c 45 54 45 PDATE and DELETE
1bc45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 statements..
1bc46 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f */. case TK_
1bc47 52 4f 57 3a 20 7b 0a 20 20 20 20 20 20 53 72 63 ROW: {. Src
1bc48 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d List *pSrcList =
1bc49 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a pNC->pSrcList;.
1bc4a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 struct Src
1bc4b 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1bc4c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1bc4d 70 53 72 63 4c 69 73 74 20 26 26 20 70 53 72 63 pSrcList && pSrc
1bc4e 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b List->nSrc==1 );
1bc4f 0a 20 20 20 20 20 20 70 49 74 65 6d 20 3d 20 70 . pItem = p
1bc50 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0a 20 20 20 SrcList->a; .
1bc51 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
1bc52 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 K_COLUMN;.
1bc53 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 49 pExpr->pTab = pI
1bc54 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 tem->pTab;.
1bc55 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d pExpr->iTable =
1bc56 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b pItem->iCursor;
1bc57 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 . pExpr->iC
1bc58 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 olumn = -1;.
1bc59 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 pExpr->affinit
1bc5a 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 y = SQLITE_AFF_I
1bc5b 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 62 72 NTEGER;. br
1bc5c 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 eak;. }.#endi
1bc5d 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c f /* defined(SQL
1bc5e 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 ITE_ENABLE_UPDAT
1bc5f 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 E_DELETE_LIMIT)
1bc60 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
1bc61 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
1bc62 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 20 6c ) */.. /* A l
1bc63 6f 6e 65 20 69 64 65 6e 74 69 66 69 65 72 20 69 one identifier i
1bc64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 s the name of a
1bc65 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 column.. */.
1bc66 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 20 7b case TK_ID: {
1bc67 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d . lookupNam
1bc68 65 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 e(pParse, 0, 0,
1bc69 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2c 20 70 &pExpr->token, p
1bc6a 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 NC, pExpr);.
1bc6b 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
1bc6c 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 ne;. }. .
1bc6d 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e 61 6d 65 /* A table name
1bc6e 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 and column name
1bc6f 3a 20 20 20 20 20 49 44 2e 49 44 0a 20 20 20 20 : ID.ID.
1bc70 2a 2a 20 4f 72 20 61 20 64 61 74 61 62 61 73 65 ** Or a database
1bc71 2c 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 , table and colu
1bc72 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 44 0a 20 20 mn: ID.ID.ID.
1bc73 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b */. case TK
1bc74 5f 44 4f 54 3a 20 7b 0a 20 20 20 20 20 20 54 6f _DOT: {. To
1bc75 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 3b 0a 20 20 ken *pColumn;.
1bc76 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c Token *pTabl
1bc77 65 3b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a e;. Token *
1bc78 70 44 62 3b 0a 20 20 20 20 20 20 45 78 70 72 20 pDb;. Expr
1bc79 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20 20 20 20 *pRight;..
1bc7a 2f 2a 20 69 66 28 20 70 53 72 63 4c 69 73 74 3d /* if( pSrcList=
1bc7b 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a 2f 0a 20 =0 ) break; */.
1bc7c 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 pRight = pE
1bc7d 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 xpr->pRight;.
1bc7e 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 6f if( pRight->o
1bc7f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 p==TK_ID ){.
1bc80 20 20 20 20 70 44 62 20 3d 20 30 3b 0a 20 20 20 pDb = 0;.
1bc81 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20 26 70 pTable = &p
1bc82 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b Expr->pLeft->tok
1bc83 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c en;. pCol
1bc84 75 6d 6e 20 3d 20 26 70 52 69 67 68 74 2d 3e 74 umn = &pRight->t
1bc85 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 oken;. }els
1bc86 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 e{. asser
1bc87 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 t( pRight->op==T
1bc88 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 20 20 20 K_DOT );.
1bc89 20 70 44 62 20 3d 20 26 70 45 78 70 72 2d 3e 70 pDb = &pExpr->p
1bc8a 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 Left->token;.
1bc8b 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20 26 70 pTable = &p
1bc8c 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d 3e 74 6f Right->pLeft->to
1bc8d 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 43 6f ken;. pCo
1bc8e 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74 2d 3e lumn = &pRight->
1bc8f 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 pRight->token;.
1bc90 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6c 6f 6f }. loo
1bc91 6b 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 kupName(pParse,
1bc92 70 44 62 2c 20 70 54 61 62 6c 65 2c 20 70 43 6f pDb, pTable, pCo
1bc93 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72 lumn, pNC, pExpr
1bc94 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1bc95 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d WRC_Prune;. }
1bc96 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 .. /* Resolve
1bc97 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a function names.
1bc98 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 */. case
1bc99 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 TK_CONST_FUNC:.
1bc9a 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 case TK_FUNCT
1bc9b 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 ION: {. Exp
1bc9c 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 rList *pList = p
1bc9d 45 78 70 72 2d 3e 70 4c 69 73 74 3b 20 20 20 20 Expr->pList;
1bc9e 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 /* The argument
1bc9f 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e list */. in
1bca0 74 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c t n = pList ? pL
1bca1 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 ist->nExpr : 0;
1bca2 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 /* Number of ar
1bca3 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 guments */.
1bca4 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75 6e int no_such_fun
1bca5 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 c = 0; /*
1bca6 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68 20 True if no such
1bca7 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 20 function exists
1bca8 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72 6f */. int wro
1bca9 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30 3b ng_num_args = 0;
1bcaa 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1bcab 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 wrong number of
1bcac 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 arguments */.
1bcad 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d 20 int is_agg =
1bcae 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
1bcaf 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e 20 * True if is an
1bcb0 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
1bcb1 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 on */. int
1bcb2 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 auth;
1bcb3 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 6f /* Autho
1bcb4 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65 20 rization to use
1bcb5 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a the function */.
1bcb6 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20 int nId;
1bcb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bcb8 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
1bcb9 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75 6e haracters in fun
1bcba 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 ction name */.
1bcbb 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
1bcbc 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 zId;
1bcbd 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 /* The function
1bcbe 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 name. */. F
1bcbf 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 uncDef *pDef;
1bcc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1bcc1 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
1bcc2 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a the function */.
1bcc3 20 20 20 20 20 20 69 6e 74 20 65 6e 63 20 3d 20 int enc =
1bcc4 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b ENC(pParse->db);
1bcc5 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
1bcc6 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a 20 e encoding */..
1bcc7 20 20 20 20 20 7a 49 64 20 3d 20 28 63 68 61 72 zId = (char
1bcc8 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a *)pExpr->token.z
1bcc9 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 70 45 ;. nId = pE
1bcca 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 xpr->token.n;.
1bccb 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 pDef = sqlit
1bccc 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 e3FindFunction(p
1bccd 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 Parse->db, zId,
1bcce 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20 30 29 3b nId, n, enc, 0);
1bccf 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d . if( pDef=
1bcd0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 =0 ){. pD
1bcd1 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 ef = sqlite3Find
1bcd2 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d Function(pParse-
1bcd3 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 2d >db, zId, nId, -
1bcd4 31 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 1, enc, 0);.
1bcd5 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 if( pDef==0
1bcd6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 5f ){. no_
1bcd7 73 75 63 68 5f 66 75 6e 63 20 3d 20 31 3b 0a 20 such_func = 1;.
1bcd8 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1bcd9 20 20 20 20 20 20 20 20 77 72 6f 6e 67 5f 6e 75 wrong_nu
1bcda 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20 20 20 20 m_args = 1;.
1bcdb 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
1bcdc 65 7b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67 e{. is_ag
1bcdd 67 20 3d 20 70 44 65 66 2d 3e 78 46 75 6e 63 3d g = pDef->xFunc=
1bcde 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e =0;. }.#ifn
1bcdf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1bce0 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 AUTHORIZATION.
1bce1 20 20 20 20 69 66 28 20 70 44 65 66 20 29 7b 0a if( pDef ){.
1bce2 20 20 20 20 20 20 20 20 61 75 74 68 20 3d 20 73 auth = s
1bce3 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
1bce4 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 46 pParse, SQLITE_F
1bce5 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70 44 65 66 UNCTION, 0, pDef
1bce6 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 ->zName, 0);.
1bce7 20 20 20 20 20 69 66 28 20 61 75 74 68 21 3d 53 if( auth!=S
1bce8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1bce9 20 20 20 20 20 20 69 66 28 20 61 75 74 68 3d 3d if( auth==
1bcea 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 SQLITE_DENY ){.
1bceb 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1bcec 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1bced 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a e, "not authoriz
1bcee 65 64 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 ed to use functi
1bcef 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20 20 20 20 on: %s",.
1bcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bcf1 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44 65 pDe
1bcf2 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 f->zName);.
1bcf3 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 pNC->nErr
1bcf4 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ++;. }.
1bcf5 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
1bcf6 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 >op = TK_NULL;.
1bcf7 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
1bcf8 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 WRC_Prune;.
1bcf9 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e }. }.#en
1bcfa 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 69 73 dif. if( is
1bcfb 5f 61 67 67 20 26 26 20 21 70 4e 43 2d 3e 61 6c _agg && !pNC->al
1bcfc 6c 6f 77 41 67 67 20 29 7b 0a 20 20 20 20 20 20 lowAgg ){.
1bcfd 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1bcfe 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 g(pParse, "misus
1bcff 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 20 66 e of aggregate f
1bd00 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c unction %.*s()",
1bd01 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 20 20 20 20 nId,zId);.
1bd02 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a pNC->nErr++;.
1bd03 20 20 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d is_agg =
1bd04 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 0;. }else
1bd05 69 66 28 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 if( no_such_func
1bd06 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1bd07 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1bd08 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 66 75 6e se, "no such fun
1bd09 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c 20 6e 49 ction: %.*s", nI
1bd0a 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 d, zId);.
1bd0b 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 pNC->nErr++;.
1bd0c 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 }else if( wr
1bd0d 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 29 7b 0a ong_num_args ){.
1bd0e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1bd0f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 rrorMsg(pParse,"
1bd10 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 wrong number of
1bd11 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 66 75 6e arguments to fun
1bd12 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 0a 20 ction %.*s()",.
1bd13 20 20 20 20 20 20 20 20 20 20 20 20 6e 49 64 2c nId,
1bd14 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 zId);. p
1bd15 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 NC->nErr++;.
1bd16 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 }. if( is
1bd17 5f 61 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 _agg ){.
1bd18 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 pExpr->op = TK_A
1bd19 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 20 GG_FUNCTION;.
1bd1a 20 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 pNC->hasAgg
1bd1b 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 1;. }.
1bd1c 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 if( is_agg )
1bd1d 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d pNC->allowAgg =
1bd1e 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 0;. sqlite
1bd1f 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 3WalkExprList(pW
1bd20 61 6c 6b 65 72 2c 20 70 4c 69 73 74 29 3b 0a 20 alker, pList);.
1bd21 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 if( is_agg
1bd22 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 ) pNC->allowAgg
1bd23 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a 20 46 49 = 1;. /* FI
1bd24 58 20 4d 45 3a 20 20 43 6f 6d 70 75 74 65 20 70 X ME: Compute p
1bd25 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 62 Expr->affinity b
1bd26 61 73 65 64 20 6f 6e 20 74 68 65 20 65 78 70 65 ased on the expe
1bd27 63 74 65 64 20 72 65 74 75 72 6e 0a 20 20 20 20 cted return.
1bd28 20 20 2a 2a 20 74 79 70 65 20 6f 66 20 74 68 65 ** type of the
1bd29 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20 20 20 20 function .
1bd2a 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
1bd2b 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 WRC_Prune;.
1bd2c 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1bd2d 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 _OMIT_SUBQUERY.
1bd2e 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 case TK_SELEC
1bd2f 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 T:. case TK_E
1bd30 58 49 53 54 53 3a 0a 23 65 6e 64 69 66 0a 20 20 XISTS:.#endif.
1bd31 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a case TK_IN: {.
1bd32 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
1bd33 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 >pSelect ){.
1bd34 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 70 int nRef = p
1bd35 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64 65 NC->nRef;.#ifnde
1bd36 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 f SQLITE_OMIT_CH
1bd37 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ECK. if(
1bd38 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a pNC->isCheck ){.
1bd39 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1bd3a 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1bd3b 2c 22 73 75 62 71 75 65 72 69 65 73 20 70 72 6f ,"subqueries pro
1bd3c 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b hibited in CHECK
1bd3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a constraints");.
1bd3e 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }.#endif
1bd3f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1bd40 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b WalkSelect(pWalk
1bd41 65 72 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 er, pExpr->pSele
1bd42 63 74 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 ct);. ass
1bd43 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 66 3e 3d ert( pNC->nRef>=
1bd44 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 20 20 20 nRef );.
1bd45 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 2d 3e 6e if( nRef!=pNC->n
1bd46 52 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Ref ){.
1bd47 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 ExprSetProperty
1bd48 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72 53 65 (pExpr, EP_VarSe
1bd49 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 7d lect);. }
1bd4a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
1bd4b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e reak;. }.#ifn
1bd4c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1bd4d 43 48 45 43 4b 0a 20 20 20 20 63 61 73 65 20 54 CHECK. case T
1bd4e 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 K_VARIABLE: {.
1bd4f 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 43 if( pNC->isC
1bd50 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 heck ){.
1bd51 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1bd52 70 50 61 72 73 65 2c 22 70 61 72 61 6d 65 74 65 pParse,"paramete
1bd53 72 73 20 70 72 6f 68 69 62 69 74 65 64 20 69 6e rs prohibited in
1bd54 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e CHECK constrain
1bd55 74 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ts");. }.
1bd56 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1bd57 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 .#endif. }. re
1bd58 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45 turn (pParse->nE
1bd59 72 72 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 rr || pParse->db
1bd5a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 ->mallocFailed)
1bd5b 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 52 ? WRC_Abort : WR
1bd5c 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f C_Continue;.}../
1bd5d 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73 20 61 *.** pEList is a
1bd5e 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 list of express
1bd5f 69 6f 6e 73 20 77 68 69 63 68 20 61 72 65 20 72 ions which are r
1bd60 65 61 6c 6c 79 20 74 68 65 20 72 65 73 75 6c 74 eally the result
1bd61 20 73 65 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61 set of the.** a
1bd62 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1bd63 74 2e 20 20 70 45 20 69 73 20 61 20 74 65 72 6d t. pE is a term
1bd64 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 in an ORDER BY
1bd65 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 or GROUP BY clau
1bd66 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 se..** This rout
1bd67 69 6e 65 20 63 68 65 63 6b 73 20 74 6f 20 73 65 ine checks to se
1bd68 65 20 69 66 20 70 45 20 69 73 20 61 20 73 69 6d e if pE is a sim
1bd69 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 20 77 ple identifier w
1bd6a 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 hich corresponds
1bd6b 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 2d 6e 61 .** to the AS-na
1bd6c 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65 me of one of the
1bd6d 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 65 78 terms of the ex
1bd6e 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 pression list.
1bd6f 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20 74 68 69 If it is,.** thi
1bd70 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
1bd71 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77 an integer betw
1bd72 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 68 65 72 een 1 and N wher
1bd73 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 e N is the numbe
1bd74 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 r of.** elements
1bd75 20 69 6e 20 70 45 4c 69 73 74 2c 20 63 6f 72 72 in pEList, corr
1bd76 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 esponding to the
1bd77 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79 2e matching entry.
1bd78 20 20 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a If there is.**
1bd79 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72 20 69 66 no match, or if
1bd7a 20 70 45 20 69 73 20 6e 6f 74 20 61 20 73 69 6d pE is not a sim
1bd7b 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2c 20 ple identifier,
1bd7c 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
1bd7d 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a e.** return 0..*
1bd7e 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68 61 73 20 *.** pEList has
1bd7f 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20 been resolved.
1bd80 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a 2f 0a 73 pE has not..*/.s
1bd81 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 tatic int resolv
1bd82 65 41 73 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 eAsName(. Parse
1bd83 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a *pParse, /*
1bd84 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
1bd85 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 for error messa
1bd86 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ges */. ExprLis
1bd87 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f 2a 20 4c t *pEList, /* L
1bd88 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f ist of expressio
1bd89 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 ns to scan */.
1bd8a 45 78 70 72 20 2a 70 45 20 20 20 20 20 20 20 20 Expr *pE
1bd8b 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e /* Expression
1bd8c 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 we are trying t
1bd8d 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 o match */.){.
1bd8e 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
1bd8f 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1bd90 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 70 45 2d er */.. if( pE-
1bd91 3e 6f 70 3d 3d 54 4b 5f 49 44 20 7c 7c 20 28 70 >op==TK_ID || (p
1bd92 45 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 E->op==TK_STRING
1bd93 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e 2e 7a 5b && pE->token.z[
1bd94 30 5d 21 3d 27 5c 27 27 29 20 29 7b 0a 20 20 20 0]!='\'') ){.
1bd95 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1bd96 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 63 Parse->db;. c
1bd97 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 73 71 6c 69 har *zCol = sqli
1bd98 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
1bd99 28 64 62 2c 20 26 70 45 2d 3e 74 6f 6b 65 6e 29 (db, &pE->token)
1bd9a 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f 6c 3d 3d ;. if( zCol==
1bd9b 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
1bd9c 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n -1;. }.
1bd9d 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 for(i=0; i<pELis
1bd9e 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a t->nExpr; i++){.
1bd9f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20 char *zAs
1bda0 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a = pEList->a[i].z
1bda1 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 20 Name;. if(
1bda2 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65 zAs!=0 && sqlite
1bda3 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 3StrICmp(zAs, zC
1bda4 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ol)==0 ){.
1bda5 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1bda6 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 db, zCol);.
1bda7 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 return i+1;.
1bda8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1bda9 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1bdaa 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 b, zCol);. }.
1bdab 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
1bdac 2a 2a 20 70 45 20 69 73 20 61 20 70 6f 69 6e 74 ** pE is a point
1bdad 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73 er to an express
1bdae 69 6f 6e 20 77 68 69 63 68 20 69 73 20 61 20 73 ion which is a s
1bdaf 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 74 68 ingle term in th
1bdb0 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 6f 66 e.** ORDER BY of
1bdb1 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 a compound SELE
1bdb2 43 54 2e 20 20 54 68 65 20 65 78 70 72 65 73 73 CT. The express
1bdb3 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ion has not been
1bdb4 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c 76 65 .** name resolve
1bdb5 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 d..**.** At the
1bdb6 70 6f 69 6e 74 20 74 68 69 73 20 72 6f 75 74 69 point this routi
1bdb7 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 ne is called, we
1bdb8 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 already know th
1bdb9 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 at the.** ORDER
1bdba 42 59 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 61 BY term is not a
1bdbb 6e 20 69 6e 74 65 67 65 72 20 69 6e 64 65 78 20 n integer index
1bdbc 69 6e 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20 into the result
1bdbd 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a 20 63 61 set. That.** ca
1bdbe 73 65 20 69 73 20 68 61 6e 64 6c 65 64 20 62 79 se is handled by
1bdbf 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 the calling rou
1bdc0 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 74 65 tine..**.** Atte
1bdc1 6d 70 74 20 74 6f 20 6d 61 74 63 68 20 70 45 20 mpt to match pE
1bdc2 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 against result s
1bdc3 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 et columns in th
1bdc4 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a 2a 20 53 e left-most.** S
1bdc5 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
1bdc6 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 Return the ind
1bdc7 65 78 20 69 20 6f 66 20 74 68 65 20 6d 61 74 63 ex i of the matc
1bdc8 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 hing column,.**
1bdc9 61 73 20 61 6e 20 69 6e 64 69 63 61 74 69 6f 6e as an indication
1bdca 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 74 to the caller t
1bdcb 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 73 6f hat it should so
1bdcc 72 74 20 62 79 20 74 68 65 20 69 2d 74 68 20 63 rt by the i-th c
1bdcd 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65 20 6c 65 olumn..** The le
1bdce 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 ft-most column i
1bdcf 73 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 s 1. In other w
1bdd0 6f 72 64 73 2c 20 74 68 65 20 76 61 6c 75 65 20 ords, the value
1bdd1 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 0a returned is the.
1bdd2 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20 ** same integer
1bdd3 76 61 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 value that would
1bdd4 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 65 20 be used in the
1bdd5 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f SQL statement to
1bdd6 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 65 indicate.** the
1bdd7 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 column..**.** I
1bdd8 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 f there is no ma
1bdd9 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 20 tch, return 0.
1bdda 52 65 74 75 72 6e 20 2d 31 20 69 66 20 61 6e 20 Return -1 if an
1bddb 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f error occurs..*/
1bddc 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f .static int reso
1bddd 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f lveOrderByTermTo
1bdde 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 ExprList(. Pars
1bddf 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f e *pParse, /
1bde0 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
1bde1 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 t for error mess
1bde2 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 ages */. Select
1bde3 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20 *pSelect, /*
1bde4 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 The SELECT state
1bde5 6d 65 6e 74 20 77 69 74 68 20 74 68 65 20 4f 52 ment with the OR
1bde6 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f DER BY clause */
1bde7 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20 . Expr *pE
1bde8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 /* The spe
1bde9 63 69 66 69 63 20 4f 52 44 45 52 20 42 59 20 74 cific ORDER BY t
1bdea 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 erm */.){. int
1bdeb 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f i; /
1bdec 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
1bded 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
1bdee 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 20 63 6f List; /* The co
1bdef 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 72 65 73 lumns of the res
1bdf0 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 4e 61 6d ult set */. Nam
1bdf1 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 20 20 20 eContext nc;
1bdf2 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 /* Name context
1bdf3 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 20 70 45 for resolving pE
1bdf4 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
1bdf5 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 qlite3ExprIsInte
1bdf6 67 65 72 28 70 45 2c 20 26 69 29 3d 3d 30 20 29 ger(pE, &i)==0 )
1bdf7 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 ;. pEList = pSe
1bdf8 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 lect->pEList;..
1bdf9 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 /* Resolve all
1bdfa 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 4f 52 44 names in the ORD
1bdfb 45 52 20 42 59 20 74 65 72 6d 20 65 78 70 72 65 ER BY term expre
1bdfc 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d ssion. */. mem
1bdfd 73 65 74 28 26 6e 63 2c 20 30 2c 20 73 69 7a 65 set(&nc, 0, size
1bdfe 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63 2e 70 50 of(nc));. nc.pP
1bdff 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 arse = pParse;.
1be00 20 6e 63 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 nc.pSrcList = p
1be01 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 Select->pSrc;.
1be02 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70 45 4c 69 nc.pEList = pELi
1be03 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f 77 41 67 st;. nc.allowAg
1be04 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e 45 72 72 g = 1;. nc.nErr
1be05 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 = 0;. if( sqli
1be06 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
1be07 6d 65 73 28 26 6e 63 2c 20 70 45 29 20 29 7b 0a mes(&nc, pE) ){.
1be08 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1be09 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 Clear(pParse);.
1be0a 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1be0b 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 6d 61 .. /* Try to ma
1be0c 74 63 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 tch the ORDER BY
1be0d 20 65 78 70 72 65 73 73 69 6f 6e 20 61 67 61 69 expression agai
1be0e 6e 73 74 20 61 6e 20 65 78 70 72 65 73 73 69 6f nst an expressio
1be0f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 n. ** in the re
1be10 73 75 6c 74 20 73 65 74 2e 20 20 52 65 74 75 72 sult set. Retur
1be11 6e 20 61 6e 20 31 2d 62 61 73 65 64 20 69 6e 64 n an 1-based ind
1be12 65 78 20 6f 66 20 74 68 65 20 6d 61 74 63 68 69 ex of the matchi
1be13 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73 ng. ** result-s
1be14 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a 2f 0a 20 et entry.. */.
1be15 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 for(i=0; i<pELi
1be16 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b st->nExpr; i++){
1be17 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1be18 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45 4c 69 ExprCompare(pELi
1be19 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 st->a[i].pExpr,
1be1a 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 pE) ){. ret
1be1b 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 7d 0a 20 urn i+1;. }.
1be1c 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 6d }.. /* If no m
1be1d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 atch, return 0.
1be1e 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d */. return 0;.}
1be1f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1be20 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 an ORDER BY or
1be21 47 52 4f 55 50 20 42 59 20 74 65 72 6d 20 6f 75 GROUP BY term ou
1be22 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72 72 6f 72 t-of-range error
1be23 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1be24 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e resolveOutOfRan
1be25 67 65 45 72 72 6f 72 28 0a 20 20 50 61 72 73 65 geError(. Parse
1be26 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
1be27 20 20 2f 2a 20 54 68 65 20 65 72 72 6f 72 20 63 /* The error c
1be28 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77 68 69 63 ontext into whic
1be29 68 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 h to write the e
1be2a 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rror */. const
1be2b 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 20 20 char *zType,
1be2c 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 /* "ORDER" or "
1be2d 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69 6e 74 20 GROUP" */. int
1be2e 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i,
1be2f 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 /* The index
1be30 28 31 2d 62 61 73 65 64 29 20 6f 66 20 74 68 65 (1-based) of the
1be31 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e term out of ran
1be32 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 20 20 ge */. int mx
1be33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1be34 2a 20 4c 61 72 67 65 73 74 20 70 65 72 6d 69 73 * Largest permis
1be35 73 69 62 6c 65 20 76 61 6c 75 65 20 6f 66 20 69 sible value of i
1be36 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 */.){. sqlite3
1be37 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1be38 20 0a 20 20 20 20 22 25 72 20 25 73 20 42 59 20 . "%r %s BY
1be39 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67 term out of rang
1be3a 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 20 22 0a e - should be ".
1be3b 20 20 20 20 22 62 65 74 77 65 65 6e 20 31 20 61 "between 1 a
1be3c 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54 79 70 65 nd %d", i, zType
1be3d 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 , mx);.}../*.**
1be3e 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f 52 44 45 Analyze the ORDE
1be3f 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20 61 R BY clause in a
1be40 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
1be41 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20 4d 6f statement. Mo
1be42 64 69 66 79 0a 2a 2a 20 65 61 63 68 20 74 65 72 dify.** each ter
1be43 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 m of the ORDER B
1be44 59 20 63 6c 61 75 73 65 20 69 73 20 61 20 63 6f Y clause is a co
1be45 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72 20 62 nstant integer b
1be46 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61 6e 64 20 etween 1.** and
1be47 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 N where N is the
1be48 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1be49 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 6f 75 ns in the compou
1be4a 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a nd SELECT..**.**
1be4b 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 ORDER BY terms
1be4c 74 68 61 74 20 61 72 65 20 61 6c 72 65 61 64 79 that are already
1be4d 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77 an integer betw
1be4e 65 65 6e 20 31 20 61 6e 64 20 4e 20 61 72 65 0a een 1 and N are.
1be4f 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64 2e 20 20 ** unmodified.
1be50 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 ORDER BY terms t
1be51 68 61 74 20 61 72 65 20 69 6e 74 65 67 65 72 73 hat are integers
1be52 20 6f 75 74 73 69 64 65 20 74 68 65 20 72 61 6e outside the ran
1be53 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68 72 6f 75 ge of.** 1 throu
1be54 67 68 20 4e 20 67 65 6e 65 72 61 74 65 20 61 6e gh N generate an
1be55 20 65 72 72 6f 72 2e 20 20 4f 52 44 45 52 20 42 error. ORDER B
1be56 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 Y terms that are
1be57 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 expressions.**
1be58 61 72 65 20 6d 61 74 63 68 65 64 20 61 67 61 69 are matched agai
1be59 6e 73 74 20 72 65 73 75 6c 74 20 73 65 74 20 65 nst result set e
1be5a 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 63 6f xpressions of co
1be5b 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 0a 2a 2a mpound SELECT.**
1be5c 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 beginning with
1be5d 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 53 45 the left-most SE
1be5e 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b 69 6e 67 LECT and working
1be5f 20 74 6f 77 61 72 64 20 74 68 65 20 72 69 67 68 toward the righ
1be60 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20 66 69 72 t..** At the fir
1be61 73 74 20 6d 61 74 63 68 2c 20 74 68 65 20 4f 52 st match, the OR
1be62 44 45 52 20 42 59 20 65 78 70 72 65 73 73 69 6f DER BY expressio
1be63 6e 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 n is transformed
1be64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 69 6e 74 into.** the int
1be65 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 eger column numb
1be66 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e er..**.** Return
1be67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
1be68 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a 2f 0a 73 rrors seen..*/.s
1be69 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 tatic int resolv
1be6a 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 eCompoundOrderBy
1be6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1be6c 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 e, /* Par
1be6d 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c sing context. L
1be6e 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61 eave error messa
1be6f 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 ges here */. Se
1be70 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20 lect *pSelect
1be71 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 /* The SELEC
1be72 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 T statement cont
1be73 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 44 45 52 aining the ORDER
1be74 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 BY */.){. int
1be75 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 i;. ExprList *p
1be76 4f 72 64 65 72 42 79 3b 0a 20 20 45 78 70 72 4c OrderBy;. ExprL
1be77 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 ist *pEList;. s
1be78 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e qlite3 *db;. in
1be79 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a t moreToDo = 1;.
1be7a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 53 . pOrderBy = pS
1be7b 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 42 79 3b elect->pOrderBy;
1be7c 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d . if( pOrderBy=
1be7d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
1be7e 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 db = pParse->db
1be7f 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 ;.#if SQLITE_MAX
1be80 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f _COLUMN. if( pO
1be81 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 rderBy->nExpr>db
1be82 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
1be83 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b LIMIT_COLUMN] ){
1be84 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1be85 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f rMsg(pParse, "to
1be86 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 o many terms in
1be87 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 22 ORDER BY clause"
1be88 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b );. return 1;
1be89 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 66 6f . }.#endif. fo
1be8a 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 r(i=0; i<pOrderB
1be8b 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a y->nExpr; i++){.
1be8c 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b pOrderBy->a[
1be8d 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d i].done = 0;. }
1be8e 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 . pSelect->pNex
1be8f 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 t = 0;. while(
1be90 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 pSelect->pPrior
1be91 29 7b 0a 20 20 20 20 70 53 65 6c 65 63 74 2d 3e ){. pSelect->
1be92 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 20 3d 20 pPrior->pNext =
1be93 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70 53 65 pSelect;. pSe
1be94 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e lect = pSelect->
1be95 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 77 68 pPrior;. }. wh
1be96 69 6c 65 28 20 70 53 65 6c 65 63 74 20 26 26 20 ile( pSelect &&
1be97 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 20 20 20 moreToDo ){.
1be98 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1be99 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 item *pItem;.
1be9a 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 3b 0a 20 moreToDo = 0;.
1be9b 20 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c pEList = pSel
1be9c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 ect->pEList;.
1be9d 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 assert( pEList!
1be9e 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d =0 );. for(i=
1be9f 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 0, pItem=pOrderB
1bea0 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 y->a; i<pOrderBy
1bea1 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 ->nExpr; i++, pI
1bea2 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e tem++){. in
1bea3 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 t iCol = -1;.
1bea4 20 20 20 45 78 70 72 20 2a 70 45 2c 20 2a 70 44 Expr *pE, *pD
1bea5 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 up;. if( pI
1bea6 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 6f 6e 74 tem->done ) cont
1bea7 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 45 20 3d inue;. pE =
1bea8 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 pItem->pExpr;.
1bea9 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1beaa 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 ExprIsInteger(pE
1beab 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 , &iCol) ){.
1beac 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 7c if( iCol<0 |
1bead 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e | iCol>pEList->n
1beae 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 Expr ){.
1beaf 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 resolveOutOfRa
1beb0 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c ngeError(pParse,
1beb1 20 22 4f 52 44 45 52 22 2c 20 69 2b 31 2c 20 70 "ORDER", i+1, p
1beb2 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 EList->nExpr);.
1beb3 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
1beb4 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 1;. }.
1beb5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1beb6 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 iCol = resolve
1beb7 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 AsName(pParse, p
1beb8 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 EList, pE);.
1beb9 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 30 20 if( iCol==0
1beba 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 44 75 ){. pDu
1bebb 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 p = sqlite3ExprD
1bebc 75 70 28 64 62 2c 20 70 45 29 3b 0a 20 20 20 20 up(db, pE);.
1bebd 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d if( !db->m
1bebe 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1bebf 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 asser
1bec0 74 28 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 t(pDup);.
1bec1 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f iCol = reso
1bec2 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f lveOrderByTermTo
1bec3 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c ExprList(pParse,
1bec4 20 70 53 65 6c 65 63 74 2c 20 70 44 75 70 29 3b pSelect, pDup);
1bec5 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1bec6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
1bec7 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75 prDelete(db, pDu
1bec8 70 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 p);. }.
1bec9 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 if( iCol<0
1beca 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
1becb 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 turn 1;.
1becc 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
1becd 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 if( iCol>0 ){.
1bece 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 CollSeq *p
1becf 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43 6f 6c 6c Coll = pE->pColl
1bed0 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c ;. int fl
1bed1 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61 67 73 20 ags = pE->flags
1bed2 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b & EP_ExpCollate;
1bed3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1bed4 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1bed5 45 29 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 E);. pIte
1bed6 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 20 3d 20 m->pExpr = pE =
1bed7 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 sqlite3Expr(db,
1bed8 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 TK_INTEGER, 0, 0
1bed9 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 0);. if
1beda 28 20 70 45 3d 3d 30 20 29 20 72 65 74 75 72 6e ( pE==0 ) return
1bedb 20 31 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 1;. pE->
1bedc 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 pColl = pColl;.
1bedd 20 20 20 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 pE->flags
1bede 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 |= EP_IntValue
1bedf 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 | flags;.
1bee0 20 70 45 2d 3e 69 54 61 62 6c 65 20 3d 20 69 43 pE->iTable = iC
1bee1 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 ol;. pIte
1bee2 6d 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a m->iCol = iCol;.
1bee3 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 64 pItem->d
1bee4 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d one = 1;. }
1bee5 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 6f else{. mo
1bee6 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 20 20 20 20 reToDo = 1;.
1bee7 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 }. }. pS
1bee8 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d elect = pSelect-
1bee9 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 66 6f >pNext;. }. fo
1beea 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 r(i=0; i<pOrderB
1beeb 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a y->nExpr; i++){.
1beec 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 if( pOrderBy
1beed 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29 ->a[i].done==0 )
1beee 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1beef 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1bef0 22 25 72 20 4f 52 44 45 52 20 42 59 20 74 65 72 "%r ORDER BY ter
1bef1 6d 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 m does not match
1bef2 20 61 6e 79 20 22 0a 20 20 20 20 20 20 20 20 20 any ".
1bef3 20 20 20 22 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 "column in th
1bef4 65 20 72 65 73 75 6c 74 20 73 65 74 22 2c 20 69 e result set", i
1bef5 2b 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 +1);. retur
1bef6 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
1bef7 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
1bef8 0a 2a 2a 20 43 68 65 63 6b 20 65 76 65 72 79 20 .** Check every
1bef9 74 65 72 6d 20 69 6e 20 74 68 65 20 4f 52 44 45 term in the ORDE
1befa 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 R BY or GROUP BY
1befb 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79 clause pOrderBy
1befc 20 6f 66 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43 of.** the SELEC
1befd 54 20 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c T statement pSel
1befe 65 63 74 2e 20 20 49 66 20 61 6e 79 20 74 65 72 ect. If any ter
1beff 6d 20 69 73 20 72 65 66 65 72 65 6e 63 65 20 74 m is reference t
1bf00 6f 20 61 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 o a.** result se
1bf01 74 20 65 78 70 72 65 73 73 69 6f 6e 20 28 61 73 t expression (as
1bf02 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 determined by t
1bf03 68 65 20 45 78 70 72 4c 69 73 74 2e 61 2e 69 43 he ExprList.a.iC
1bf04 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a 20 74 68 65 ol field).** the
1bf05 6e 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 74 n convert that t
1bf06 65 72 6d 20 69 6e 74 6f 20 61 20 63 6f 70 79 20 erm into a copy
1bf07 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e of the correspon
1bf08 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65 74 0a ding result set.
1bf09 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a ** column..**.**
1bf0a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73 20 61 If any errors a
1bf0b 72 65 20 64 65 74 65 63 74 65 64 2c 20 61 64 64 re detected, add
1bf0c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1bf0d 65 20 74 6f 20 70 50 61 72 73 65 20 61 6e 64 0a e to pParse and.
1bf0e 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 ** return non-ze
1bf0f 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 72 6f ro. Return zero
1bf10 20 69 66 20 6e 6f 20 65 72 72 6f 72 73 20 61 72 if no errors ar
1bf11 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 e seen..*/.SQLIT
1bf12 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1bf13 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 lite3ResolveOrde
1bf14 72 47 72 6f 75 70 42 79 28 0a 20 20 50 61 72 73 rGroupBy(. Pars
1bf15 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
1bf16 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
1bf17 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65 72 72 text. Leave err
1bf18 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 or messages here
1bf19 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 */. Select *pS
1bf1a 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 elect, /* T
1bf1b 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
1bf1c 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ent containing t
1bf1d 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 he clause */. E
1bf1e 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 xprList *pOrderB
1bf1f 79 2c 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 y, /* The ORDE
1bf20 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 R BY or GROUP BY
1bf21 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 70 72 clause to be pr
1bf22 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20 63 6f 6e ocessed */. con
1bf23 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 st char *zType
1bf24 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 /* "ORDER" or
1bf25 20 22 47 52 4f 55 50 22 20 2a 2f 0a 29 7b 0a 20 "GROUP" */.){.
1bf26 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 int i;. sqlite
1bf27 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1bf28 64 62 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a db;. ExprList *
1bf29 70 45 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74 pEList;. struct
1bf2a 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1bf2b 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20 70 4f pItem;.. if( pO
1bf2c 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 70 50 61 rderBy==0 || pPa
1bf2d 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 rse->db->mallocF
1bf2e 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 ailed ) return 0
1bf2f 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 ;.#if SQLITE_MAX
1bf30 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f _COLUMN. if( pO
1bf31 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 rderBy->nExpr>db
1bf32 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
1bf33 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b LIMIT_COLUMN] ){
1bf34 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1bf35 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f rMsg(pParse, "to
1bf36 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 o many terms in
1bf37 25 73 20 42 59 20 63 6c 61 75 73 65 22 2c 20 7a %s BY clause", z
1bf38 54 79 70 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Type);. retur
1bf39 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a n 1;. }.#endif.
1bf3a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 pEList = pSele
1bf3b 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 ct->pEList;. as
1bf3c 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 sert( pEList!=0
1bf3d 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 ); /* sqlite3Se
1bf3e 6c 65 63 74 4e 65 77 28 29 20 67 75 61 72 61 6e lectNew() guaran
1bf3f 74 65 65 73 20 74 68 69 73 20 2a 2f 0a 20 20 66 tees this */. f
1bf40 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f or(i=0, pItem=pO
1bf41 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 rderBy->a; i<pOr
1bf42 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b derBy->nExpr; i+
1bf43 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 +, pItem++){.
1bf44 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c if( pItem->iCol
1bf45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 ){. if( pI
1bf46 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45 4c 69 73 74 tem->iCol>pEList
1bf47 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 ->nExpr ){.
1bf48 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 resolveOutOfR
1bf49 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 angeError(pParse
1bf4a 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 70 45 , zType, i+1, pE
1bf4b 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 List->nExpr);.
1bf4c 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
1bf4d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
1bf4e 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61 72 73 solveAlias(pPars
1bf4f 65 2c 20 70 45 4c 69 73 74 2c 20 70 49 74 65 6d e, pEList, pItem
1bf50 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49 74 65 6d 2d ->iCol-1, pItem-
1bf51 3e 70 45 78 70 72 2c 20 7a 54 79 70 65 29 3b 0a >pExpr, zType);.
1bf52 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1bf53 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 rn 0;.}../*.** p
1bf54 4f 72 64 65 72 42 79 20 69 73 20 61 6e 20 4f 52 OrderBy is an OR
1bf55 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 DER BY or GROUP
1bf56 42 59 20 63 6c 61 75 73 65 20 69 6e 20 53 45 4c BY clause in SEL
1bf57 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 53 ECT statement pS
1bf58 65 6c 65 63 74 2e 0a 2a 2a 20 54 68 65 20 4e 61 elect..** The Na
1bf59 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 me context of th
1bf5a 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 e SELECT stateme
1bf5b 6e 74 20 69 73 20 70 4e 43 2e 20 20 7a 54 79 70 nt is pNC. zTyp
1bf5c 65 20 69 73 20 65 69 74 68 65 72 0a 2a 2a 20 22 e is either.** "
1bf5d 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 ORDER" or "GROUP
1bf5e 22 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 " depending on w
1bf5f 68 69 63 68 20 74 79 70 65 20 6f 66 20 63 6c 61 hich type of cla
1bf60 75 73 65 20 70 4f 72 64 65 72 42 79 20 69 73 2e use pOrderBy is.
1bf61 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
1bf62 69 6e 65 20 72 65 73 6f 6c 76 65 73 20 65 61 63 ine resolves eac
1bf63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6c h term of the cl
1bf64 61 75 73 65 20 69 6e 74 6f 20 61 6e 20 65 78 70 ause into an exp
1bf65 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 ression..** If t
1bf66 68 65 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d he order-by term
1bf67 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 49 is an integer I
1bf68 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e between 1 and N
1bf69 20 28 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 (where N is the
1bf6a 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f .** number of co
1bf6b 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 lumns in the res
1bf6c 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 ult set of the S
1bf6d 45 4c 45 43 54 29 20 74 68 65 6e 20 74 68 65 20 ELECT) then the
1bf6e 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e expression.** in
1bf6f 20 74 68 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 the resolution
1bf70 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 is a copy of the
1bf71 20 49 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 I-th result-set
1bf72 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 66 expression. If
1bf73 0a 2a 2a 20 74 68 65 20 6f 72 64 65 72 2d 62 79 .** the order-by
1bf74 20 74 65 72 6d 20 69 73 20 61 6e 20 69 64 65 6e term is an iden
1bf75 74 69 66 79 20 74 68 61 74 20 63 6f 72 72 65 73 tify that corres
1bf76 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 41 53 2d ponds to the AS-
1bf77 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61 20 72 65 73 name of.** a res
1bf78 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 ult-set expressi
1bf79 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 65 72 on, then the ter
1bf7a 6d 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 61 20 m resolves to a
1bf7b 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 72 copy of the.** r
1bf7c 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 esult-set expres
1bf7d 73 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 73 65 sion. Otherwise
1bf7e 2c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e , the expression
1bf7f 20 69 73 20 72 65 73 6f 6c 76 65 64 20 69 6e 0a is resolved in.
1bf80 2a 2a 20 74 68 65 20 75 73 75 61 6c 20 77 61 79 ** the usual way
1bf81 20 2d 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 - using sqlite3
1bf82 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
1bf83 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ()..**.** This r
1bf84 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 outine returns t
1bf85 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 he number of err
1bf86 6f 72 73 2e 20 20 49 66 20 65 72 72 6f 72 73 20 ors. If errors
1bf87 6f 63 63 75 72 2c 20 74 68 65 6e 0a 2a 2a 20 61 occur, then.** a
1bf88 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 n appropriate er
1bf89 72 6f 72 20 6d 65 73 73 61 67 65 20 6d 69 67 68 ror message migh
1bf8a 74 20 62 65 20 6c 65 66 74 20 69 6e 20 70 50 61 t be left in pPa
1bf8b 72 73 65 2e 20 20 28 4f 4f 4d 20 65 72 72 6f 72 rse. (OOM error
1bf8c 73 0a 2a 2a 20 65 78 63 65 70 74 65 64 2e 29 0a s.** excepted.).
1bf8d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
1bf8e 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 solveOrderGroupB
1bf8f 79 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 y(. NameContext
1bf90 20 2a 70 4e 43 2c 20 20 20 20 20 2f 2a 20 54 68 *pNC, /* Th
1bf91 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f e name context o
1bf92 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 f the SELECT sta
1bf93 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 tement */. Sele
1bf94 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 ct *pSelect,
1bf95 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 /* The SELECT
1bf96 73 74 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e statement holdin
1bf97 67 20 70 4f 72 64 65 72 42 79 20 2a 2f 0a 20 20 g pOrderBy */.
1bf98 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 ExprList *pOrder
1bf99 42 79 2c 20 20 20 2f 2a 20 41 6e 20 4f 52 44 45 By, /* An ORDE
1bf9a 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 R BY or GROUP BY
1bf9b 20 63 6c 61 75 73 65 20 74 6f 20 72 65 73 6f 6c clause to resol
1bf9c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ve */. const ch
1bf9d 61 72 20 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a ar *zType /*
1bf9e 20 45 69 74 68 65 72 20 22 4f 52 44 45 52 22 20 Either "ORDER"
1bf9f 6f 72 20 22 47 52 4f 55 50 22 2c 20 61 73 20 61 or "GROUP", as a
1bfa0 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 29 7b ppropriate */.){
1bfa1 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
1bfa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bfa3 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1bfa4 72 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b r */. int iCol;
1bfa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bfa6 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 /* Column
1bfa7 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 74 72 75 number */. stru
1bfa8 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
1bfa9 20 2a 70 49 74 65 6d 3b 20 20 20 2f 2a 20 41 20 *pItem; /* A
1bfaa 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 term of the ORDE
1bfab 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 R BY clause */.
1bfac 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 Parse *pParse;
1bfad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bfae 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
1bfaf 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 xt */. int nRes
1bfb0 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ult;
1bfb1 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1bfb2 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65 of terms in the
1bfb3 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a result set */..
1bfb4 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d if( pOrderBy==
1bfb5 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
1bfb6 6e 52 65 73 75 6c 74 20 3d 20 70 53 65 6c 65 63 nResult = pSelec
1bfb7 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 t->pEList->nExpr
1bfb8 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43 ;. pParse = pNC
1bfb9 2d 3e 70 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 ->pParse;. for(
1bfba 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 i=0, pItem=pOrde
1bfbb 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 rBy->a; i<pOrder
1bfbc 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 By->nExpr; i++,
1bfbd 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 pItem++){. Ex
1bfbe 70 72 20 2a 70 45 20 3d 20 70 49 74 65 6d 2d 3e pr *pE = pItem->
1bfbf 70 45 78 70 72 3b 0a 20 20 20 20 69 43 6f 6c 20 pExpr;. iCol
1bfc0 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 = resolveAsName(
1bfc1 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2d pParse, pSelect-
1bfc2 3e 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20 >pEList, pE);.
1bfc3 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a if( iCol<0 ){.
1bfc4 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20 return 1;
1bfc5 20 2f 2a 20 4f 4f 4d 20 65 72 72 6f 72 20 2a 2f /* OOM error */
1bfc6 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 . }. if( i
1bfc7 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f Col>0 ){. /
1bfc8 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d 65 20 * If an AS-name
1bfc9 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 match is found,
1bfca 6d 61 72 6b 20 74 68 69 73 20 4f 52 44 45 52 20 mark this ORDER
1bfcb 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62 65 69 BY column as bei
1bfcc 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 6f ng. ** a co
1bfcd 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 py of the iCol-t
1bfce 68 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c h result-set col
1bfcf 75 6d 6e 2e 20 20 54 68 65 20 73 75 62 73 65 71 umn. The subseq
1bfd0 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 uent call to.
1bfd1 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 65 73 ** sqlite3Res
1bfd2 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 olveOrderGroupBy
1bfd3 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20 () will convert
1bfd4 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 the expression t
1bfd5 6f 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 o a. ** cop
1bfd6 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 y of the iCol-th
1bfd7 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 result-set expr
1bfd8 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 ession. */.
1bfd9 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 69 pItem->iCol = i
1bfda 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 Col;. conti
1bfdb 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 nue;. }. i
1bfdc 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 f( sqlite3ExprIs
1bfdd 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f Integer(pE, &iCo
1bfde 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 l) ){. /* T
1bfdf 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d he ORDER BY term
1bfe0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 is an integer c
1bfe1 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c onstant. Again,
1bfe2 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a set the column.
1bfe3 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 ** number
1bfe4 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 52 so that sqlite3R
1bfe5 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
1bfe6 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 By() will conver
1bfe7 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f t the. ** o
1bfe8 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 rder-by term to
1bfe9 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 a copy of the re
1bfea 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
1bfeb 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ion */. if(
1bfec 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 iCol<1 ){.
1bfed 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 resolveOutOfR
1bfee 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 angeError(pParse
1bfef 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 , zType, i+1, nR
1bff0 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 esult);.
1bff1 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 return 1;.
1bff2 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 }. pItem->i
1bff3 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 Col = iCol;.
1bff4 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
1bff5 7d 0a 0a 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 }.. /* Otherw
1bff6 69 73 65 2c 20 74 72 65 61 74 20 74 68 65 20 4f ise, treat the O
1bff7 52 44 45 52 20 42 59 20 74 65 72 6d 20 61 73 20 RDER BY term as
1bff8 61 6e 20 6f 72 64 69 6e 61 72 79 20 65 78 70 72 an ordinary expr
1bff9 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 70 49 ession */. pI
1bffa 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 30 3b 0a 20 tem->iCol = 0;.
1bffb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 if( sqlite3Re
1bffc 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70 solveExprNames(p
1bffd 4e 43 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 NC, pE) ){.
1bffe 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
1bfff 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 . }. return sq
1c000 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 lite3ResolveOrde
1c001 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 65 2c rGroupBy(pParse,
1c002 20 70 53 65 6c 65 63 74 2c 20 70 4f 72 64 65 72 pSelect, pOrder
1c003 42 79 2c 20 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f By, zType);.}../
1c004 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d *.** Resolve nam
1c005 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 es in the SELECT
1c006 20 73 74 61 74 65 6d 65 6e 74 20 70 20 61 6e 64 statement p and
1c007 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 65 73 63 all of its desc
1c008 65 6e 64 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 endents..*/.stat
1c009 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 53 65 ic int resolveSe
1c00a 6c 65 63 74 53 74 65 70 28 57 61 6c 6b 65 72 20 lectStep(Walker
1c00b 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 *pWalker, Select
1c00c 20 2a 70 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 *p){. NameCont
1c00d 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b 20 20 ext *pOuterNC;
1c00e 2f 2a 20 43 6f 6e 74 65 78 74 20 74 68 61 74 20 /* Context that
1c00f 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 53 45 contains this SE
1c010 4c 45 43 54 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f LECT */. NameCo
1c011 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 ntext sNC;
1c012 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 /* Name contex
1c013 74 20 6f 66 20 74 68 69 73 20 53 45 4c 45 43 54 t of this SELECT
1c014 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70 */. int isComp
1c015 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 2f 2a ound; /*
1c016 20 54 72 75 65 20 69 66 20 70 20 69 73 20 61 20 True if p is a
1c017 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 compound select
1c018 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6d 70 6f 75 */. int nCompou
1c019 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 nd; /*
1c01a 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70 6f 75 Number of compou
1c01b 6e 64 20 74 65 72 6d 73 20 70 72 6f 63 65 73 73 nd terms process
1c01c 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 50 ed so far */. P
1c01d 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
1c01e 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
1c01f 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 g context */. E
1c020 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b xprList *pEList;
1c021 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
1c022 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 set expression
1c023 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b list */. int i;
1c024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c025 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1c026 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 r */. ExprList
1c027 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f *pGroupBy; /
1c028 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59 20 63 * The GROUP BY c
1c029 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 lause */. Selec
1c02a 74 20 2a 70 4c 65 66 74 6d 6f 73 74 3b 20 20 20 t *pLeftmost;
1c02b 20 20 20 2f 2a 20 4c 65 66 74 2d 6d 6f 73 74 20 /* Left-most
1c02c 6f 66 20 53 45 4c 45 43 54 20 6f 66 20 61 20 63 of SELECT of a c
1c02d 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 73 71 6c ompound */. sql
1c02e 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
1c02f 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1c030 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
1c031 20 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d .. assert( p!=
1c032 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65 0 );. if( p->se
1c033 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f lFlags & SF_Reso
1c034 6c 76 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 lved ){. retu
1c035 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
1c036 7d 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d 20 70 }. pOuterNC = p
1c037 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 Walker->u.pNC;.
1c038 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 pParse = pWalke
1c039 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 64 62 20 r->pParse;. db
1c03a 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 = pParse->db;..
1c03b 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 20 73 71 6c /* Normally sql
1c03c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 ite3SelectExpand
1c03d 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 () will be calle
1c03e 64 20 66 69 72 73 74 20 61 6e 64 20 77 69 6c 6c d first and will
1c03f 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 have. ** alrea
1c040 64 79 20 65 78 70 61 6e 64 65 64 20 74 68 69 73 dy expanded this
1c041 20 53 45 4c 45 43 54 2e 20 20 48 6f 77 65 76 65 SELECT. Howeve
1c042 72 2c 20 69 66 20 74 68 69 73 20 69 73 20 61 20 r, if this is a
1c043 73 75 62 71 75 65 72 79 20 77 69 74 68 69 6e 0a subquery within.
1c044 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 ** an expressi
1c045 6f 6e 2c 20 73 71 6c 69 74 65 33 52 65 73 6f 6c on, sqlite3Resol
1c046 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 77 69 veExprNames() wi
1c047 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 ll be called wit
1c048 68 6f 75 74 20 61 0a 20 20 2a 2a 20 70 72 69 6f hout a. ** prio
1c049 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 r call to sqlite
1c04a 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 2e 3SelectExpand().
1c04b 20 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 When that happ
1c04c 65 6e 73 2c 20 6c 65 74 0a 20 20 2a 2a 20 73 71 ens, let. ** sq
1c04d 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 lite3SelectPrep(
1c04e 29 20 64 6f 20 61 6c 6c 20 6f 66 20 74 68 65 20 ) do all of the
1c04f 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 74 processing for t
1c050 68 69 73 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2a his SELECT.. **
1c051 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 sqlite3SelectPr
1c052 65 70 28 29 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 ep() will invoke
1c053 20 62 6f 74 68 20 73 71 6c 69 74 65 33 53 65 6c both sqlite3Sel
1c054 65 63 74 45 78 70 61 6e 64 28 29 20 61 6e 64 0a ectExpand() and.
1c055 20 20 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e ** this routin
1c056 65 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 e in the correct
1c057 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 order.. */. i
1c058 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 f( (p->selFlags
1c059 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 3d 3d & SF_Expanded)==
1c05a 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1c05b 53 65 6c 65 63 74 50 72 65 70 28 70 50 61 72 73 SelectPrep(pPars
1c05c 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b e, p, pOuterNC);
1c05d 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 50 61 . return (pPa
1c05e 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d rse->nErr || db-
1c05f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f >mallocFailed) ?
1c060 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 52 43 WRC_Abort : WRC
1c061 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 69 _Prune;. }.. i
1c062 73 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d 3e 70 sCompound = p->p
1c063 50 72 69 6f 72 21 3d 30 3b 0a 20 20 6e 43 6f 6d Prior!=0;. nCom
1c064 70 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 70 4c 65 pound = 0;. pLe
1c065 66 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20 77 68 ftmost = p;. wh
1c066 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 61 73 ile( p ){. as
1c067 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 sert( (p->selFla
1c068 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64 gs & SF_Expanded
1c069 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 )!=0 );. asse
1c06a 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 rt( (p->selFlags
1c06b 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 29 3d & SF_Resolved)=
1c06c 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 73 65 6c =0 );. p->sel
1c06d 46 6c 61 67 73 20 7c 3d 20 53 46 5f 52 65 73 6f Flags |= SF_Reso
1c06e 6c 76 65 64 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 lved;.. /* Re
1c06f 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65 73 solve the expres
1c070 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 4c 49 4d sions in the LIM
1c071 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20 63 6c IT and OFFSET cl
1c072 61 75 73 65 73 2e 20 54 68 65 73 65 0a 20 20 20 auses. These.
1c073 20 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f ** are not allo
1c074 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f 20 wed to refer to
1c075 61 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20 70 61 any names, so pa
1c076 73 73 20 61 6e 20 65 6d 70 74 79 20 4e 61 6d 65 ss an empty Name
1c077 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a Context.. */.
1c078 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c memset(&sNC,
1c079 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 0, sizeof(sNC))
1c07a 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 ;. sNC.pParse
1c07b 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 69 = pParse;. i
1c07c 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 f( sqlite3Resolv
1c07d 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c eExprNames(&sNC,
1c07e 20 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c 0a 20 p->pLimit) ||.
1c07f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
1c080 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 solveExprNames(&
1c081 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 sNC, p->pOffset)
1c082 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1c083 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
1c084 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 53 65 74 20 }. . /* Set
1c085 75 70 20 74 68 65 20 6c 6f 63 61 6c 20 6e 61 6d up the local nam
1c086 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 70 61 73 e-context to pas
1c087 73 20 74 6f 20 73 71 6c 69 74 65 33 52 65 73 6f s to sqlite3Reso
1c088 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 74 lveExprNames() t
1c089 6f 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76 65 o. ** resolve
1c08a 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 the result-set
1c08b 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e expression list.
1c08c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e . */. sNC.
1c08d 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 allowAgg = 1;.
1c08e 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d sNC.pSrcList =
1c08f 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 73 4e p->pSrc;. sN
1c090 43 2e 70 4e 65 78 74 20 3d 20 70 4f 75 74 65 72 C.pNext = pOuter
1c091 4e 43 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 NC;. . /* Re
1c092 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 solve names in t
1c093 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 2a he result set. *
1c094 2f 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 /. pEList = p
1c095 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 ->pEList;. as
1c096 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 sert( pEList!=0
1c097 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 );. for(i=0;
1c098 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b i<pEList->nExpr;
1c099 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 i++){. Exp
1c09a 72 20 2a 70 58 20 3d 20 70 45 4c 69 73 74 2d 3e r *pX = pEList->
1c09b 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 a[i].pExpr;.
1c09c 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 if( sqlite3Res
1c09d 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 olveExprNames(&s
1c09e 4e 43 2c 20 70 58 29 20 29 7b 0a 20 20 20 20 20 NC, pX) ){.
1c09f 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1c0a0 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ort;. }.
1c0a1 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 63 }. . /* Rec
1c0a2 75 72 73 69 76 65 6c 79 20 72 65 73 6f 6c 76 65 ursively resolve
1c0a3 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 73 75 names in all su
1c0a4 62 71 75 65 72 69 65 73 0a 20 20 20 20 2a 2f 0a bqueries. */.
1c0a5 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1c0a6 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b ->pSrc->nSrc; i+
1c0a7 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 +){. struct
1c0a8 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
1c0a9 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d Item = &p->pSrc-
1c0aa 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 >a[i];. if(
1c0ab 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 pItem->pSelect
1c0ac 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 ){. const
1c0ad 20 63 68 61 72 20 2a 7a 53 61 76 65 64 43 6f 6e char *zSavedCon
1c0ae 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a text = pParse->z
1c0af 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 AuthContext;.
1c0b0 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e if( pItem->
1c0b1 7a 4e 61 6d 65 20 29 20 70 50 61 72 73 65 2d 3e zName ) pParse->
1c0b2 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 zAuthContext = p
1c0b3 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 Item->zName;.
1c0b4 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f sqlite3Reso
1c0b5 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70 lveSelectNames(p
1c0b6 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53 Parse, pItem->pS
1c0b7 65 6c 65 63 74 2c 20 26 73 4e 43 29 3b 0a 20 20 elect, &sNC);.
1c0b8 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 pParse->zA
1c0b9 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61 uthContext = zSa
1c0ba 76 65 64 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 vedContext;.
1c0bb 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e if( pParse->
1c0bc 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c nErr || db->mall
1c0bd 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 ocFailed ) retur
1c0be 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
1c0bf 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
1c0c0 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
1c0c1 65 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66 e no aggregate f
1c0c2 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 unctions in the
1c0c3 72 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64 20 result-set, and
1c0c4 6e 6f 20 47 52 4f 55 50 20 42 59 20 0a 20 20 20 no GROUP BY .
1c0c5 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 ** expression,
1c0c6 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67 do not allow agg
1c0c7 72 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20 6f regates in any o
1c0c8 66 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 f the other expr
1c0c9 65 73 73 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a essions.. */.
1c0ca 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e assert( (p->
1c0cb 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 selFlags & SF_Ag
1c0cc 67 72 65 67 61 74 65 29 3d 3d 30 20 29 3b 0a 20 gregate)==0 );.
1c0cd 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d pGroupBy = p-
1c0ce 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 69 >pGroupBy;. i
1c0cf 66 28 20 70 47 72 6f 75 70 42 79 20 7c 7c 20 73 f( pGroupBy || s
1c0d0 4e 43 2e 68 61 73 41 67 67 20 29 7b 0a 20 20 20 NC.hasAgg ){.
1c0d1 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c p->selFlags |
1c0d2 3d 20 53 46 5f 41 67 67 72 65 67 61 74 65 3b 0a = SF_Aggregate;.
1c0d3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1c0d4 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 sNC.allowAgg =
1c0d5 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 0;. }. .
1c0d6 2f 2a 20 49 66 20 61 20 48 41 56 49 4e 47 20 63 /* If a HAVING c
1c0d7 6c 61 75 73 65 20 69 73 20 70 72 65 73 65 6e 74 lause is present
1c0d8 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 , then there mus
1c0d9 74 20 62 65 20 61 20 47 52 4f 55 50 20 42 59 20 t be a GROUP BY
1c0da 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 clause.. */.
1c0db 20 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e if( p->pHavin
1c0dc 67 20 26 26 20 21 70 47 72 6f 75 70 42 79 20 29 g && !pGroupBy )
1c0dd 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1c0de 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1c0df 22 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 "a GROUP BY clau
1c0e0 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20 62 se is required b
1c0e1 65 66 6f 72 65 20 48 41 56 49 4e 47 22 29 3b 0a efore HAVING");.
1c0e2 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
1c0e3 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 _Abort;. }.
1c0e4 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 . /* Add the
1c0e5 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 expression list
1c0e6 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 to the name-cont
1c0e7 65 78 74 20 62 65 66 6f 72 65 20 70 61 72 73 69 ext before parsi
1c0e8 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74 ng the. ** ot
1c0e9 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 her expressions
1c0ea 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 in the SELECT st
1c0eb 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 atement. This is
1c0ec 20 73 6f 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 so that. **
1c0ed 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 expressions in t
1c0ee 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
1c0ef 28 65 74 63 2e 29 20 63 61 6e 20 72 65 66 65 72 (etc.) can refer
1c0f0 20 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 to expressions
1c0f1 62 79 0a 20 20 20 20 2a 2a 20 61 6c 69 61 73 65 by. ** aliase
1c0f2 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
1c0f3 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 set.. **.
1c0f4 2a 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a 20 ** Minor point:
1c0f5 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 If this is the c
1c0f6 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78 ase, then the ex
1c0f7 70 72 65 73 73 69 6f 6e 20 77 69 6c 6c 20 62 65 pression will be
1c0f8 0a 20 20 20 20 2a 2a 20 72 65 2d 65 76 61 6c 75 . ** re-evalu
1c0f9 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 65 ated for each re
1c0fa 66 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 0a 20 ference to it..
1c0fb 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 45 */. sNC.pE
1c0fc 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 List = p->pEList
1c0fd 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1c0fe 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
1c0ff 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57 68 65 72 s(&sNC, p->pWher
1c100 65 29 20 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c e) ||. sql
1c101 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
1c102 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 48 ames(&sNC, p->pH
1c103 61 76 69 6e 67 29 0a 20 20 20 20 29 7b 0a 20 20 aving). ){.
1c104 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
1c105 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 bort;. }..
1c106 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 /* The ORDER BY
1c107 20 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63 6c and GROUP BY cl
1c108 61 75 73 65 73 20 6d 61 79 20 6e 6f 74 20 72 65 auses may not re
1c109 66 65 72 20 74 6f 20 74 65 72 6d 73 20 69 6e 0a fer to terms in.
1c10a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 ** outer que
1c10b 72 69 65 73 20 0a 20 20 20 20 2a 2f 0a 20 20 20 ries . */.
1c10c 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a sNC.pNext = 0;.
1c10d 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 sNC.allowAgg
1c10e 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 = 1;.. /* Pr
1c10f 6f 63 65 73 73 20 74 68 65 20 4f 52 44 45 52 20 ocess the ORDER
1c110 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 73 69 BY clause for si
1c111 6e 67 6c 65 74 6f 6e 20 53 45 4c 45 43 54 20 73 ngleton SELECT s
1c112 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a tatements.. *
1c113 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 * The ORDER BY c
1c114 6c 61 75 73 65 20 66 6f 72 20 63 6f 6d 70 6f 75 lause for compou
1c115 6e 64 73 20 53 45 4c 45 43 54 20 73 74 61 74 65 nds SELECT state
1c116 6d 65 6e 74 73 20 69 73 20 68 61 6e 64 6c 65 64 ments is handled
1c117 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 61 . ** below, a
1c118 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 fter all of the
1c119 72 65 73 75 6c 74 2d 73 65 74 73 20 66 6f 72 20 result-sets for
1c11a 61 6c 6c 20 6f 66 20 74 68 65 20 65 6c 65 6d 65 all of the eleme
1c11b 6e 74 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 nts of. ** th
1c11c 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 e compound have
1c11d 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 been resolved..
1c11e 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 */. if( !i
1c11f 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 73 sCompound && res
1c120 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 olveOrderGroupBy
1c121 28 26 73 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f 72 (&sNC, p, p->pOr
1c122 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 20 derBy, "ORDER")
1c123 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1c124 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d WRC_Abort;. }
1c125 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c . if( db->mal
1c126 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1c127 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1c128 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 ort;. }. .
1c129 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 /* Resolve the
1c12a 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
1c12b 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 . At the same t
1c12c 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 0a ime, make sure .
1c12d 20 20 20 20 2a 2a 20 74 68 65 20 47 52 4f 55 50 ** the GROUP
1c12e 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20 BY clause does
1c12f 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 67 67 72 not contain aggr
1c130 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e egate functions.
1c131 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1c132 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 pGroupBy ){.
1c133 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
1c134 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 t_item *pItem;.
1c135 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 72 65 . if( re
1c136 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 solveOrderGroupB
1c137 79 28 26 73 4e 43 2c 20 70 2c 20 70 47 72 6f 75 y(&sNC, p, pGrou
1c138 70 42 79 2c 20 22 47 52 4f 55 50 22 29 20 7c 7c pBy, "GROUP") ||
1c139 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1c13a 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 d ){. ret
1c13b 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
1c13c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 }. for
1c13d 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f (i=0, pItem=pGro
1c13e 75 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75 upBy->a; i<pGrou
1c13f 70 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c pBy->nExpr; i++,
1c140 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1c141 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 if( ExprHasPr
1c142 6f 70 65 72 74 79 28 70 49 74 65 6d 2d 3e 70 45 operty(pItem->pE
1c143 78 70 72 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a xpr, EP_Agg) ){.
1c144 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1c145 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1c146 2c 20 22 61 67 67 72 65 67 61 74 65 20 66 75 6e , "aggregate fun
1c147 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 ctions are not a
1c148 6c 6c 6f 77 65 64 20 69 6e 20 22 0a 20 20 20 20 llowed in ".
1c149 20 20 20 20 20 20 20 20 20 20 22 74 68 65 20 47 "the G
1c14a 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 22 29 ROUP BY clause")
1c14b 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
1c14c 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1c14d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1c14e 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 }.. /* Ad
1c14f 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 vance to the nex
1c150 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6f t term of the co
1c151 6d 70 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20 mpound. */.
1c152 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b p = p->pPrior;
1c153 0a 20 20 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b 2b . nCompound++
1c154 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f ;. }.. /* Reso
1c155 6c 76 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 lve the ORDER BY
1c156 20 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 on a compound S
1c157 45 4c 45 43 54 20 61 66 74 65 72 20 61 6c 6c 20 ELECT after all
1c158 74 65 72 6d 73 20 6f 66 0a 20 20 2a 2a 20 74 68 terms of. ** th
1c159 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 e compound have
1c15a 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 been resolved..
1c15b 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d 70 */. if( isComp
1c15c 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 43 ound && resolveC
1c15d 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28 70 ompoundOrderBy(p
1c15e 50 61 72 73 65 2c 20 70 4c 65 66 74 6d 6f 73 74 Parse, pLeftmost
1c15f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
1c160 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a WRC_Abort;. }..
1c161 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
1c162 6e 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ne;.}../*.** Thi
1c163 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 s routine walks
1c164 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 an expression tr
1c165 65 65 20 61 6e 64 20 72 65 73 6f 6c 76 65 73 20 ee and resolves
1c166 72 65 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a 2a references to.**
1c167 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 61 table columns a
1c168 6e 64 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f nd result-set co
1c169 6c 75 6d 6e 73 2e 20 20 41 74 20 74 68 65 20 73 lumns. At the s
1c16a 61 6d 65 20 74 69 6d 65 2c 20 64 6f 20 65 72 72 ame time, do err
1c16b 6f 72 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 6f or.** checking o
1c16c 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 61 67 65 n function usage
1c16d 20 61 6e 64 20 73 65 74 20 61 20 66 6c 61 67 20 and set a flag
1c16e 69 66 20 61 6e 79 20 61 67 67 72 65 67 61 74 65 if any aggregate
1c16f 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 functions.** ar
1c170 65 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f e seen..**.** To
1c171 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20 63 resolve table c
1c172 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 olumns reference
1c173 73 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e 6f s we look for no
1c174 64 65 73 20 28 6f 72 20 73 75 62 74 72 65 65 73 des (or subtrees
1c175 29 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 72 ) of the .** for
1c176 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f m X.Y.Z or Y.Z o
1c177 72 20 6a 75 73 74 20 5a 20 77 68 65 72 65 0a 2a r just Z where.*
1c178 2a 0a 2a 2a 20 20 20 20 20 20 58 3a 20 20 20 54 *.** X: T
1c179 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 he name of a dat
1c17a 61 62 61 73 65 2e 20 20 45 78 3a 20 20 22 6d 61 abase. Ex: "ma
1c17b 69 6e 22 20 6f 72 20 22 74 65 6d 70 22 20 6f 72 in" or "temp" or
1c17c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 .** th
1c17d 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 e symbolic name
1c17e 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6e 20 41 assigned to an A
1c17f 54 54 41 43 48 2d 65 64 20 64 61 74 61 62 61 73 TTACH-ed databas
1c180 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 3a e..**.** Y:
1c181 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 The name of a
1c182 20 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f 4d table in a FROM
1c183 20 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e 20 clause. Or in
1c184 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 20 20 20 a trigger.**
1c185 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74 68 one of th
1c186 65 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73 20 e special names
1c187 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e 0a "old" or "new"..
1c188 2a 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a 20 20 20 **.** Z:
1c189 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f The name of a co
1c18a 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 59 2e lumn in table Y.
1c18b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f 64 65 20 .**.** The node
1c18c 61 74 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 at the root of t
1c18d 68 65 20 73 75 62 74 72 65 65 20 69 73 20 6d 6f he subtree is mo
1c18e 64 69 66 69 65 64 20 61 73 20 66 6f 6c 6c 6f 77 dified as follow
1c18f 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 78 70 72 s:.**.** Expr
1c190 2e 6f 70 20 20 20 20 20 20 20 20 43 68 61 6e 67 .op Chang
1c191 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 0a ed to TK_COLUMN.
1c192 2a 2a 20 20 20 20 45 78 70 72 2e 70 54 61 62 20 ** Expr.pTab
1c193 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 Points to t
1c194 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 he Table object
1c195 66 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 20 45 78 for X.Y.** Ex
1c196 70 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 54 68 65 pr.iColumn The
1c197 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 6e column index in
1c198 20 58 2e 59 2e 20 20 2d 31 20 66 6f 72 20 74 68 X.Y. -1 for th
1c199 65 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 45 e rowid..** E
1c19a 78 70 72 2e 69 54 61 62 6c 65 20 20 20 20 54 68 xpr.iTable Th
1c19b 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 e VDBE cursor nu
1c19c 6d 62 65 72 20 66 6f 72 20 58 2e 59 0a 2a 2a 0a mber for X.Y.**.
1c19d 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 **.** To resolve
1c19e 20 72 65 73 75 6c 74 2d 73 65 74 20 72 65 66 65 result-set refe
1c19f 72 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f 72 rences, look for
1c1a0 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 expression node
1c1a1 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d s of the.** form
1c1a2 20 5a 20 28 77 69 74 68 20 6e 6f 20 58 20 61 6e Z (with no X an
1c1a3 64 20 59 20 70 72 65 66 69 78 29 20 77 68 65 72 d Y prefix) wher
1c1a4 65 20 74 68 65 20 5a 20 6d 61 74 63 68 65 73 20 e the Z matches
1c1a5 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a the right-hand.*
1c1a6 2a 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53 20 * size of an AS
1c1a7 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72 65 clause in the re
1c1a8 73 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53 45 sult-set of a SE
1c1a9 4c 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78 70 LECT. The Z exp
1c1aa 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 72 65 ression.** is re
1c1ab 70 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70 79 placed by a copy
1c1ac 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61 6e of the left-han
1c1ad 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 d side of the re
1c1ae 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
1c1af 69 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e 61 ion..** Table-na
1c1b0 6d 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 me and function
1c1b1 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75 72 resolution occur
1c1b2 73 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69 74 s on the substit
1c1b3 75 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 0a uted expression.
1c1b4 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72 20 65 78 ** tree. For ex
1c1b5 61 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a ample, in:.**.**
1c1b6 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 SELECT a+b
1c1b7 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79 20 AS x, c+d AS y
1c1b8 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 59 FROM t1 ORDER BY
1c1b9 20 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 78 x;.**.** The "x
1c1ba 22 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6f 72 " term of the or
1c1bb 64 65 72 20 62 79 20 69 73 20 72 65 70 6c 61 63 der by is replac
1c1bc 65 64 20 62 79 20 22 61 2b 62 22 20 74 6f 20 72 ed by "a+b" to r
1c1bd 65 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ender:.**.**
1c1be 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 SELECT a+b AS
1c1bf 78 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d x, c+d AS y FROM
1c1c0 20 74 31 20 4f 52 44 45 52 20 42 59 20 61 2b 62 t1 ORDER BY a+b
1c1c1 3b 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e ;.**.** Function
1c1c2 20 63 61 6c 6c 73 20 61 72 65 20 63 68 65 63 6b calls are check
1c1c3 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ed to make sure
1c1c4 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f that the functio
1c1c5 6e 20 69 73 20 0a 2a 2a 20 64 65 66 69 6e 65 64 n is .** defined
1c1c6 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 6f and that the co
1c1c7 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 rrect number of
1c1c8 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 73 70 arguments are sp
1c1c9 65 63 69 66 69 65 64 2e 0a 2a 2a 20 49 66 20 74 ecified..** If t
1c1ca 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 he function is a
1c1cb 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 n aggregate func
1c1cc 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 tion, then the p
1c1cd 4e 43 2d 3e 68 61 73 41 67 67 20 69 73 0a 2a 2a NC->hasAgg is.**
1c1ce 20 73 65 74 20 61 6e 64 20 74 68 65 20 6f 70 63 set and the opc
1c1cf 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64 20 66 ode is changed f
1c1d0 72 6f 6d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 rom TK_FUNCTION
1c1d1 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 to TK_AGG_FUNCTI
1c1d2 4f 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 78 70 ON..** If an exp
1c1d3 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 ression contains
1c1d4 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
1c1d5 69 6f 6e 73 20 74 68 65 6e 20 74 68 65 20 45 50 ions then the EP
1c1d6 5f 41 67 67 0a 2a 2a 20 70 72 6f 70 65 72 74 79 _Agg.** property
1c1d7 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 on the expressi
1c1d8 6f 6e 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a on is set..**.**
1c1d9 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 An error messag
1c1da 65 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50 61 e is left in pPa
1c1db 72 73 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 rse if anything
1c1dc 69 73 20 61 6d 69 73 73 2e 20 20 54 68 65 20 6e is amiss. The n
1c1dd 75 6d 62 65 72 0a 2a 2a 20 69 66 20 65 72 72 6f umber.** if erro
1c1de 72 73 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a rs is returned..
1c1df 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1c1e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 E int sqlite3Res
1c1e1 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 20 0a olveExprNames( .
1c1e2 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 NameContext *p
1c1e3 4e 43 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d NC, /* Nam
1c1e4 65 73 70 61 63 65 20 74 6f 20 72 65 73 6f 6c 76 espace to resolv
1c1e5 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e e expressions in
1c1e6 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 . */. Expr *pEx
1c1e7 70 72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f pr /
1c1e8 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e * The expression
1c1e9 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e to be analyzed.
1c1ea 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 73 61 76 */.){. int sav
1c1eb 65 64 48 61 73 41 67 67 3b 0a 20 20 57 61 6c 6b edHasAgg;. Walk
1c1ec 65 72 20 77 3b 0a 0a 20 20 69 66 28 20 70 45 78 er w;.. if( pEx
1c1ed 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 pr==0 ) return 0
1c1ee 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 ;.#if SQLITE_MAX
1c1ef 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 _EXPR_DEPTH>0.
1c1f0 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 {. Parse *pPa
1c1f1 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 rse = pNC->pPars
1c1f2 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 e;. if( sqlit
1c1f3 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 e3ExprCheckHeigh
1c1f4 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d t(pParse, pExpr-
1c1f5 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70 50 >nHeight+pNC->pP
1c1f6 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 20 29 arse->nHeight) )
1c1f7 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
1c1f8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 ;. }. pPar
1c1f9 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70 se->nHeight += p
1c1fa 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 Expr->nHeight;.
1c1fb 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 61 76 65 }.#endif. save
1c1fc 64 48 61 73 41 67 67 20 3d 20 70 4e 43 2d 3e 68 dHasAgg = pNC->h
1c1fd 61 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 61 asAgg;. pNC->ha
1c1fe 73 41 67 67 20 3d 20 30 3b 0a 20 20 77 2e 78 45 sAgg = 0;. w.xE
1c1ff 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 xprCallback = re
1c200 73 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 solveExprStep;.
1c201 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 w.xSelectCallba
1c202 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 ck = resolveSele
1c203 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 ctStep;. w.pPar
1c204 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
1c205 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e ;. w.u.pNC = pN
1c206 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b C;. sqlite3Walk
1c207 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b Expr(&w, pExpr);
1c208 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f .#if SQLITE_MAX_
1c209 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 70 EXPR_DEPTH>0. p
1c20a 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 NC->pParse->nHei
1c20b 67 68 74 20 2d 3d 20 70 45 78 70 72 2d 3e 6e 48 ght -= pExpr->nH
1c20c 65 69 67 68 74 3b 0a 23 65 6e 64 69 66 0a 20 20 eight;.#endif.
1c20d 69 66 28 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 20 if( pNC->nErr>0
1c20e 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 ){. ExprSetPr
1c20f 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1c210 5f 45 72 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 _Error);. }. i
1c211 66 28 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 29 f( pNC->hasAgg )
1c212 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f {. ExprSetPro
1c213 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
1c214 41 67 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Agg);. }else if
1c215 28 20 73 61 76 65 64 48 61 73 41 67 67 20 29 7b ( savedHasAgg ){
1c216 0a 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 . pNC->hasAgg
1c217 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
1c218 72 6e 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 rn ExprHasProper
1c219 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72 ty(pExpr, EP_Err
1c21a 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 or);.}.../*.** R
1c21b 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 esolve all names
1c21c 20 69 6e 20 61 6c 6c 20 65 78 70 72 65 73 73 69 in all expressi
1c21d 6f 6e 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20 ons of a SELECT
1c21e 61 6e 64 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 and in all.** de
1c21f 63 65 6e 64 65 6e 74 73 20 6f 66 20 74 68 65 20 cendents of the
1c220 53 45 4c 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e SELECT, includin
1c221 67 20 63 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20 g compounds off
1c222 6f 66 20 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a of p->pPrior,.**
1c223 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 65 subqueries in e
1c224 78 70 72 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20 xpressions, and
1c225 73 75 62 71 75 65 72 69 65 73 20 75 73 65 64 20 subqueries used
1c226 61 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a as FROM clause.*
1c227 2a 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 * terms..**.** S
1c228 65 65 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 ee sqlite3Resolv
1c229 65 45 78 70 72 4e 61 6d 65 73 28 29 20 66 6f 72 eExprNames() for
1c22a 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f a description o
1c22b 66 20 74 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a f the kinds of.*
1c22c 2a 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e * transformation
1c22d 73 20 74 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a s that occur..**
1c22e 0a 2a 2a 20 41 6c 6c 20 53 45 4c 45 43 54 20 73 .** All SELECT s
1c22f 74 61 74 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64 tatements should
1c230 20 68 61 76 65 20 62 65 65 6e 20 65 78 70 61 6e have been expan
1c231 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c ded using.** sql
1c232 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 ite3SelectExpand
1c233 28 29 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f () prior to invo
1c234 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e king this routin
1c235 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1c236 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1c237 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 3ResolveSelectNa
1c238 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 mes(. Parse *pP
1c239 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a arse, /*
1c23a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 The parser cont
1c23b 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 ext */. Select
1c23c 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
1c23d 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 /* The SELECT st
1c23e 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f atement being co
1c23f 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f ded. */. NameCo
1c240 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 ntext *pOuterNC
1c241 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 /* Name context
1c242 20 66 6f 72 20 70 61 72 65 6e 74 20 53 45 4c 45 for parent SELE
1c243 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a CT statement */.
1c244 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a ){. Walker w;..
1c245 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 assert( p!=0 )
1c246 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 ;. w.xExprCallb
1c247 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 ack = resolveExp
1c248 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 rStep;. w.xSele
1c249 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 ctCallback = res
1c24a 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a olveSelectStep;.
1c24b 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 w.pParse = pPa
1c24c 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d rse;. w.u.pNC =
1c24d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c pOuterNC;. sql
1c24e 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 ite3WalkSelect(&
1c24f 77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a w, p);.}../*****
1c250 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1c251 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a resolve.c *****
1c252 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c253 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c254 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1c255 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
1c256 66 69 6c 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a file expr.c ****
1c257 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c258 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c259 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
1c25a 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
1c25b 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
1c25c 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
1c25d 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
1c25e 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
1c25f 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
1c260 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
1c261 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1c262 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1c263 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1c264 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1c265 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1c266 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1c267 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1c268 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1c269 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1c26a 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1c26b 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
1c26c 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
1c26d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c26e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c26f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c271 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
1c272 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 ntains routines
1c273 75 73 65 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69 used for analyzi
1c274 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 ng expressions a
1c275 6e 64 0a 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61 nd.** for genera
1c276 74 69 6e 67 20 56 44 42 45 20 63 6f 64 65 20 74 ting VDBE code t
1c277 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 65 78 hat evaluates ex
1c278 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c pressions in SQL
1c279 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ite..**.** $Id:
1c27a 65 78 70 72 2e 63 2c 76 20 31 2e 34 30 34 20 32 expr.c,v 1.404 2
1c27b 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 3a 008/11/19 16:52:
1c27c 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 44 danielk1977 E
1c27d 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 xp $.*/../*.** R
1c27e 65 74 75 72 6e 20 74 68 65 20 27 61 66 66 69 6e eturn the 'affin
1c27f 69 74 79 27 20 6f 66 20 74 68 65 20 65 78 70 72 ity' of the expr
1c280 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 66 20 ession pExpr if
1c281 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 any..**.** If pE
1c282 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c xpr is a column,
1c283 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
1c284 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 a column via an
1c285 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 'AS' alias,.** o
1c286 72 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 77 r a sub-select w
1c287 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 ith a column as
1c288 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
1c289 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 , then the .** a
1c28a 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 20 ffinity of that
1c28b 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e column is return
1c28c 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 ed. Otherwise, 0
1c28d 78 30 30 20 69 73 20 72 65 74 75 72 6e 65 64 2c x00 is returned,
1c28e 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 6e .** indicating n
1c28f 6f 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 74 o affinity for t
1c290 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a he expression..*
1c291 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 *.** i.e. the WH
1c292 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 72 65 ERE clause expre
1c293 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 sssions in the f
1c294 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 ollowing stateme
1c295 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 nts all.** have
1c296 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a an affinity:.**.
1c297 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 ** CREATE TABLE
1c298 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 t1(a);.** SELECT
1c299 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 * FROM t1 WHERE
1c29a 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 a;.** SELECT a
1c29b 41 53 20 62 20 46 52 4f 4d 20 74 31 20 57 48 45 AS b FROM t1 WHE
1c29c 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 RE b;.** SELECT
1c29d 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 * FROM t1 WHERE
1c29e 28 73 65 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 (select a from t
1c29f 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 1);.*/.SQLITE_PR
1c2a0 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 IVATE char sqlit
1c2a1 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 e3ExprAffinity(E
1c2a2 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 xpr *pExpr){. i
1c2a3 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f nt op = pExpr->o
1c2a4 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f p;. if( op==TK_
1c2a5 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 72 65 SELECT ){. re
1c2a6 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 turn sqlite3Expr
1c2a7 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e Affinity(pExpr->
1c2a8 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d pSelect->pEList-
1c2a9 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 >a[0].pExpr);.
1c2aa 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1c2ab 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28 _OMIT_CAST. if(
1c2ac 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a op==TK_CAST ){.
1c2ad 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
1c2ae 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 26 e3AffinityType(&
1c2af 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 pExpr->token);.
1c2b0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 }.#endif. if(
1c2b1 28 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 (op==TK_AGG_COLU
1c2b2 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c MN || op==TK_COL
1c2b3 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 UMN || op==TK_RE
1c2b4 47 49 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 GISTER) . && p
1c2b5 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 Expr->pTab!=0.
1c2b6 29 7b 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b ){. /* op==TK
1c2b7 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 45 78 _REGISTER && pEx
1c2b8 70 72 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 pr->pTab!=0 happ
1c2b9 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 ens when pExpr w
1c2ba 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 as originally.
1c2bb 20 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e ** a TK_COLUMN
1c2bc 20 62 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 but was previou
1c2bd 73 6c 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e sly evaluated an
1c2be 64 20 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 d cached in a re
1c2bf 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e gister */. in
1c2c0 74 20 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f t j = pExpr->iCo
1c2c1 6c 75 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c lumn;. if( j<
1c2c2 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
1c2c3 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 E_AFF_INTEGER;.
1c2c4 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
1c2c5 2d 3e 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 ->pTab && j<pExp
1c2c6 72 2d 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b r->pTab->nCol );
1c2c7 0a 20 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 . return pExp
1c2c8 72 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d r->pTab->aCol[j]
1c2c9 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 .affinity;. }.
1c2ca 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 return pExpr->a
1c2cb 66 66 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a ffinity;.}../*.*
1c2cc 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 * Set the collat
1c2cd 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 ing sequence for
1c2ce 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 expression pExp
1c2cf 72 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c r to be the coll
1c2d0 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 ating.** sequenc
1c2d1 65 20 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 e named by pToke
1c2d2 6e 2e 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f n. Return a po
1c2d3 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 inter to the rev
1c2d4 69 73 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e ised expression.
1c2d5 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e .** The collatin
1c2d6 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 g sequence is ma
1c2d7 72 6b 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 rked as "explici
1c2d8 74 22 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f t" using the EP_
1c2d9 45 78 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c ExpCollate.** fl
1c2da 61 67 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 ag. An explicit
1c2db 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
1c2dc 6e 63 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 nce will overrid
1c2dd 65 20 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f e implicit.** co
1c2de 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
1c2df 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
1c2e0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
1c2e1 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 e3ExprSetColl(Pa
1c2e2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
1c2e3 72 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 r *pExpr, Token
1c2e4 2a 70 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 *pCollName){. c
1c2e5 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 har *zColl = 0;
1c2e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
1c2e7 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 quoted name of c
1c2e8 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
1c2e9 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a e */. CollSeq *
1c2ea 70 43 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 pColl;. sqlite3
1c2eb 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
1c2ec 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c b;. zColl = sql
1c2ed 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 ite3NameFromToke
1c2ee 6e 28 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 n(db, pCollName)
1c2ef 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 26 26 ;. if( pExpr &&
1c2f0 20 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 zColl ){. pC
1c2f1 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 oll = sqlite3Loc
1c2f2 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 ateCollSeq(pPars
1c2f3 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20 e, zColl, -1);.
1c2f4 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a if( pColl ){.
1c2f5 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f pExpr->pCo
1c2f6 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 ll = pColl;.
1c2f7 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c pExpr->flags |
1c2f8 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b = EP_ExpCollate;
1c2f9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
1c2fa 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1c2fb 43 6f 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 Coll);. return
1c2fc 70 45 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pExpr;.}../*.**
1c2fd 52 65 74 75 72 6e 20 74 68 65 20 64 65 66 61 75 Return the defau
1c2fe 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 lt collation seq
1c2ff 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78 uence for the ex
1c300 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 pression pExpr.
1c301 49 66 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e If.** there is n
1c302 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 o default collat
1c303 69 6f 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e ion type, return
1c304 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 0..*/.SQLITE_PR
1c305 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
1c306 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 qlite3ExprCollSe
1c307 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c q(Parse *pParse,
1c308 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 Expr *pExpr){.
1c309 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 CollSeq *pColl
1c30a 3d 20 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d = 0;. Expr *p =
1c30b 20 70 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 pExpr;. while(
1c30c 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 p ){. int op
1c30d 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 2d ;. pColl = p-
1c30e 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 20 >pColl;. if(
1c30f 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 pColl ) break;.
1c310 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 op = p->op;.
1c311 20 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 if( (op==TK_A
1c312 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d GG_COLUMN || op=
1c313 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 =TK_COLUMN || op
1c314 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20 26 ==TK_REGISTER) &
1c315 26 20 70 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a & p->pTab!=0 ){.
1c316 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f /* op==TK_
1c317 52 45 47 49 53 54 45 52 20 26 26 20 70 2d 3e 70 REGISTER && p->p
1c318 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20 77 Tab!=0 happens w
1c319 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f 72 hen pExpr was or
1c31a 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a iginally. *
1c31b 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 * a TK_COLUMN bu
1c31c 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 t was previously
1c31d 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63 evaluated and c
1c31e 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 73 ached in a regis
1c31f 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e ter */. con
1c320 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a st char *zColl;.
1c321 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d int j = p-
1c322 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 >iColumn;.
1c323 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 if( j>=0 ){.
1c324 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 sqlite3 *db
1c325 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
1c326 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d zColl = p-
1c327 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a >pTab->aCol[j].z
1c328 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 Coll;. pC
1c329 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e oll = sqlite3Fin
1c32a 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 dCollSeq(db, ENC
1c32b 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c (db), zColl, -1,
1c32c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78 0);. pEx
1c32d 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c pr->pColl = pCol
1c32e 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 l;. }.
1c32f 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1c330 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 if( op!=TK_CAS
1c331 54 20 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 T && op!=TK_UPLU
1c332 53 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b S ){. break
1c333 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 ;. }. p =
1c334 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 p->pLeft;. }.
1c335 69 66 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b if( sqlite3Check
1c336 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 CollSeq(pParse,
1c337 70 43 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 pColl) ){ . p
1c338 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Coll = 0;. }.
1c339 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a return pColl;.}.
1c33a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 ./*.** pExpr is
1c33b 61 6e 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 an operand of a
1c33c 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 comparison opera
1c33d 74 6f 72 2e 20 20 61 66 66 32 20 69 73 20 74 68 tor. aff2 is th
1c33e 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 e.** type affini
1c33f 74 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 ty of the other
1c340 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 operand. This r
1c341 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 outine returns t
1c342 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e he.** type affin
1c343 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 ity that should
1c344 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 be used for the
1c345 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 comparison opera
1c346 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 tor..*/.SQLITE_P
1c347 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 RIVATE char sqli
1c348 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 te3CompareAffini
1c349 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 ty(Expr *pExpr,
1c34a 63 68 61 72 20 61 66 66 32 29 7b 0a 20 20 63 68 char aff2){. ch
1c34b 61 72 20 61 66 66 31 20 3d 20 73 71 6c 69 74 65 ar aff1 = sqlite
1c34c 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 3ExprAffinity(pE
1c34d 78 70 72 29 3b 0a 20 20 69 66 28 20 61 66 66 31 xpr);. if( aff1
1c34e 20 26 26 20 61 66 66 32 20 29 7b 0a 20 20 20 20 && aff2 ){.
1c34f 2f 2a 20 42 6f 74 68 20 73 69 64 65 73 20 6f 66 /* Both sides of
1c350 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
1c351 61 72 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 are columns. If
1c352 6f 6e 65 20 68 61 73 20 6e 75 6d 65 72 69 63 0a one has numeric.
1c353 20 20 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c ** affinity,
1c354 20 75 73 65 20 74 68 61 74 2e 20 4f 74 68 65 72 use that. Other
1c355 77 69 73 65 20 75 73 65 20 6e 6f 20 61 66 66 69 wise use no affi
1c356 6e 69 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 nity.. */.
1c357 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 if( sqlite3IsNu
1c358 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 mericAffinity(af
1c359 66 31 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 f1) || sqlite3Is
1c35a 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 NumericAffinity(
1c35b 61 66 66 32 29 20 29 7b 0a 20 20 20 20 20 20 72 aff2) ){. r
1c35c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 eturn SQLITE_AFF
1c35d 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 _NUMERIC;. }e
1c35e 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
1c35f 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e n SQLITE_AFF_NON
1c360 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 E;. }. }else
1c361 20 69 66 28 20 21 61 66 66 31 20 26 26 20 21 61 if( !aff1 && !a
1c362 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 ff2 ){. /* Ne
1c363 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 ither side of th
1c364 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 e comparison is
1c365 61 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 a column. Compa
1c366 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 re the. ** re
1c367 73 75 6c 74 73 20 64 69 72 65 63 74 6c 79 2e 0a sults directly..
1c368 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 */. retur
1c369 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e n SQLITE_AFF_NON
1c36a 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 E;. }else{.
1c36b 2f 2a 20 4f 6e 65 20 73 69 64 65 20 69 73 20 61 /* One side is a
1c36c 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 column, the oth
1c36d 65 72 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 er is not. Use t
1c36e 68 65 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e he columns affin
1c36f 69 74 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 ity. */. asse
1c370 72 74 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 rt( aff1==0 || a
1c371 66 66 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 ff2==0 );. re
1c372 74 75 72 6e 20 28 61 66 66 31 20 2b 20 61 66 66 turn (aff1 + aff
1c373 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 2);. }.}../*.**
1c374 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 pExpr is a comp
1c375 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e arison operator.
1c376 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 Return the typ
1c377 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 e affinity that
1c378 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 should.** be app
1c379 6c 69 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 lied to both ope
1c37a 72 61 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 64 rands prior to d
1c37b 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69 oing the compari
1c37c 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 son..*/.static c
1c37d 68 61 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 har comparisonAf
1c37e 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 finity(Expr *pEx
1c37f 70 72 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b pr){. char aff;
1c380 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 . assert( pExpr
1c381 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 ->op==TK_EQ || p
1c382 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 Expr->op==TK_IN
1c383 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b || pExpr->op==TK
1c384 5f 4c 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 _LT ||.
1c385 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 pExpr->op==TK_G
1c386 54 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d T || pExpr->op==
1c387 54 4b 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e TK_GE || pExpr->
1c388 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 op==TK_LE ||.
1c389 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 pExpr->op
1c38a 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 ==TK_NE );. ass
1c38b 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 ert( pExpr->pLef
1c38c 74 20 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c t );. aff = sql
1c38d 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 ite3ExprAffinity
1c38e 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a (pExpr->pLeft);.
1c38f 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 if( pExpr->pRi
1c390 67 68 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d ght ){. aff =
1c391 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 sqlite3CompareA
1c392 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 ffinity(pExpr->p
1c393 52 69 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d Right, aff);. }
1c394 0a 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 . else if( pExp
1c395 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 r->pSelect ){.
1c396 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 aff = sqlite3C
1c397 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 ompareAffinity(p
1c398 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 Expr->pSelect->p
1c399 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 EList->a[0].pExp
1c39a 72 2c 20 61 66 66 29 3b 0a 20 20 7d 0a 20 20 65 r, aff);. }. e
1c39b 6c 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a lse if( !aff ){.
1c39c 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 aff = SQLITE
1c39d 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 _AFF_NONE;. }.
1c39e 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a return aff;.}..
1c39f 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 /*.** pExpr is a
1c3a0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 comparison expr
1c3a1 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c ession, eg. '=',
1c3a2 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 '<', IN(...) et
1c3a3 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 c..** idx_affini
1c3a4 74 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 ty is the affini
1c3a5 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 ty of an indexed
1c3a6 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 column. Return
1c3a7 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 true.** if the i
1c3a8 6e 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 ndex with affini
1c3a9 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 ty idx_affinity
1c3aa 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 may be used to i
1c3ab 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 mplement.** the
1c3ac 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 comparison in pE
1c3ad 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 xpr..*/.SQLITE_P
1c3ae 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1c3af 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f e3IndexAffinityO
1c3b0 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 k(Expr *pExpr, c
1c3b1 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 har idx_affinity
1c3b2 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 ){. char aff =
1c3b3 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 comparisonAffini
1c3b4 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 ty(pExpr);. swi
1c3b5 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 tch( aff ){.
1c3b6 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f case SQLITE_AFF_
1c3b7 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 NONE:. retu
1c3b8 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 rn 1;. case S
1c3b9 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a QLITE_AFF_TEXT:.
1c3ba 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 return idx
1c3bb 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 _affinity==SQLIT
1c3bc 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 E_AFF_TEXT;.
1c3bd 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 default:. r
1c3be 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e eturn sqlite3IsN
1c3bf 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 umericAffinity(i
1c3c0 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 dx_affinity);.
1c3c1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
1c3c2 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 n the P5 value t
1c3c3 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 hat should be us
1c3c4 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 ed for a binary
1c3c5 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 comparison.** op
1c3c6 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f code (OP_Eq, OP_
1c3c7 47 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f Ge etc.) used to
1c3c8 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 compare pExpr1
1c3c9 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 and pExpr2..*/.s
1c3ca 74 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 tatic u8 binaryC
1c3cb 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 ompareP5(Expr *p
1c3cc 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 Expr1, Expr *pEx
1c3cd 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e pr2, int jumpIfN
1c3ce 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d ull){. u8 aff =
1c3cf 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 (char)sqlite3Ex
1c3d0 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 prAffinity(pExpr
1c3d1 32 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 2);. aff = sqli
1c3d2 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 te3CompareAffini
1c3d3 74 79 28 70 45 78 70 72 31 2c 20 61 66 66 29 20 ty(pExpr1, aff)
1c3d4 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a 20 20 | jumpIfNull;.
1c3d5 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f return aff;.}../
1c3d6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
1c3d7 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6c inter to the col
1c3d8 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
1c3d9 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 that should be u
1c3da 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 6e 61 sed by.** a bina
1c3db 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 ry comparison op
1c3dc 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 6e 67 erator comparing
1c3dd 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 pLeft and pRigh
1c3de 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 t..**.** If the
1c3df 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72 65 73 left hand expres
1c3e0 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 sion has a colla
1c3e1 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 79 ting sequence ty
1c3e2 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a 2a pe, then it is.*
1c3e3 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77 69 73 * used. Otherwis
1c3e4 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 e the collation
1c3e5 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 sequence for the
1c3e6 20 72 69 67 68 74 20 68 61 6e 64 20 65 78 70 72 right hand expr
1c3e7 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 73 65 ession.** is use
1c3e8 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75 6c d, or the defaul
1c3e9 74 20 28 42 49 4e 41 52 59 29 20 69 66 20 6e 65 t (BINARY) if ne
1c3ea 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e ither expression
1c3eb 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67 has a collating
1c3ec 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 .** type..**.**
1c3ed 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74 20 Argument pRight
1c3ee 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29 20 (but not pLeft)
1c3ef 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f may be a null po
1c3f0 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20 63 inter. In this c
1c3f1 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 6e 6f ase,.** it is no
1c3f2 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a 2f t considered..*/
1c3f3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1c3f4 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
1c3f5 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c BinaryCompareCol
1c3f6 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20 2a 70 lSeq(. Parse *p
1c3f7 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 2a Parse, . Expr *
1c3f8 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 20 2a pLeft, . Expr *
1c3f9 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f 6c 6c pRight.){. Coll
1c3fa 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 61 73 Seq *pColl;. as
1c3fb 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20 sert( pLeft );.
1c3fc 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67 if( pLeft->flag
1c3fd 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 s & EP_ExpCollat
1c3fe 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 e ){. assert(
1c3ff 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 29 3b pLeft->pColl );
1c400 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4c 65 . pColl = pLe
1c401 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c ft->pColl;. }el
1c402 73 65 20 69 66 28 20 70 52 69 67 68 74 20 26 26 se if( pRight &&
1c403 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 pRight->flags &
1c404 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 EP_ExpCollate )
1c405 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52 {. assert( pR
1c406 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20 ight->pColl );.
1c407 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68 pColl = pRigh
1c408 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c 73 t->pColl;. }els
1c409 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 e{. pColl = s
1c40a 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 qlite3ExprCollSe
1c40b 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 29 q(pParse, pLeft)
1c40c 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c ;. if( !pColl
1c40d 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 ){. pColl
1c40e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c = sqlite3ExprCol
1c40f 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 52 69 lSeq(pParse, pRi
1c410 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ght);. }. }.
1c411 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a return pColl;.
1c412 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
1c413 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 66 e the operands f
1c414 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 or a comparison
1c415 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 66 6f operation. Befo
1c416 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 re.** generating
1c417 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 65 61 the code for ea
1c418 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 74 20 ch operand, set
1c419 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a 2a 2a the EP_AnyAff.**
1c41a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 78 70 flag on the exp
1c41b 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 20 ression so that
1c41c 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 it will be able
1c41d 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 61 63 to used a.** cac
1c41e 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 hed column value
1c41f 20 74 68 61 74 20 68 61 73 20 70 72 65 76 69 6f that has previo
1c420 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 20 61 usly undergone a
1c421 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 63 68 n.** affinity ch
1c422 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ange..*/.static
1c423 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 72 65 void codeCompare
1c424 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 72 73 Operands(. Pars
1c425 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a e *pParse, /*
1c426 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 Parsing and cod
1c427 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e e generating con
1c428 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a text */. Expr *
1c429 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 pLeft, /* T
1c42a 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 he left operand
1c42b 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 4c 65 */. int *pRegLe
1c42c 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 73 74 ft, /* Regist
1c42d 65 72 20 77 68 65 72 65 20 6c 65 66 74 20 6f 70 er where left op
1c42e 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65 64 20 erand is stored
1c42f 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 65 4c */. int *pFreeL
1c430 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 20 74 eft, /* Free t
1c431 68 69 73 20 72 65 67 69 73 74 65 72 20 77 68 65 his register whe
1c432 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 70 72 n done */. Expr
1c433 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a *pRight, /*
1c434 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 The right opera
1c435 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 nd */. int *pRe
1c436 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 65 67 gRight, /* Reg
1c437 69 73 74 65 72 20 77 68 65 72 65 20 72 69 67 68 ister where righ
1c438 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f t operand is sto
1c439 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 red */. int *pF
1c43a 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20 57 72 reeRight /* Wr
1c43b 69 74 65 20 74 65 6d 70 20 72 65 67 69 73 74 65 ite temp registe
1c43c 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70 65 72 r for right oper
1c43d 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 7b 0a and there */.){.
1c43e 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d 3e while( pLeft->
1c43f 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 op==TK_UPLUS ) p
1c440 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 4c Left = pLeft->pL
1c441 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e 66 6c eft;. pLeft->fl
1c442 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 66 66 ags |= EP_AnyAff
1c443 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 3d 20 ;. *pRegLeft =
1c444 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
1c445 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c 65 66 emp(pParse, pLef
1c446 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b 0a 20 t, pFreeLeft);.
1c447 20 77 68 69 6c 65 28 20 70 52 69 67 68 74 2d 3e while( pRight->
1c448 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 op==TK_UPLUS ) p
1c449 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 2d 3e Right = pRight->
1c44a 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 74 2d pLeft;. pRight-
1c44b 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 >flags |= EP_Any
1c44c 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 67 68 Aff;. *pRegRigh
1c44d 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 t = sqlite3ExprC
1c44e 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
1c44f 70 52 69 67 68 74 2c 20 70 46 72 65 65 52 69 67 pRight, pFreeRig
1c450 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 ht);.}../*.** Ge
1c451 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 nerate code for
1c452 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 a comparison ope
1c453 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 rator..*/.static
1c454 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 72 65 int codeCompare
1c455 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1c456 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 e, /* The par
1c457 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 20 67 sing (and code g
1c458 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e 74 65 enerating) conte
1c459 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c xt */. Expr *pL
1c45a 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 eft, /* The
1c45b 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f left operand */
1c45c 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c . Expr *pRight,
1c45d 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 /* The righ
1c45e 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 t operand */. i
1c45f 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 20 20 nt opcode,
1c460 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 69 73 /* The comparis
1c461 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 on opcode */. i
1c462 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e 32 2c nt in1, int in2,
1c463 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
1c464 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 2a 2f ding operands */
1c465 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20 20 20 . int dest,
1c466 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 /* Jump her
1c467 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f 0a 20 e if true. */.
1c468 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 int jumpIfNull
1c469 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 6a /* If true, j
1c46a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20 6f 70 ump if either op
1c46b 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f erand is NULL */
1c46c 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a 20 20 .){. int p5;.
1c46d 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f 6c 6c int addr;. Coll
1c46e 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 20 3d Seq *p4;.. p4 =
1c46f 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f sqlite3BinaryCo
1c470 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 mpareCollSeq(pPa
1c471 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 rse, pLeft, pRig
1c472 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 6e 61 ht);. p5 = bina
1c473 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c 65 66 ryCompareP5(pLef
1c474 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d 70 49 t, pRight, jumpI
1c475 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 20 3d fNull);. addr =
1c476 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1c477 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 p4(pParse->pVdbe
1c478 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c 20 64 , opcode, in2, d
1c479 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 20 20 est, in1,.
1c47a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c47b 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34 2c 20 (void*)p4,
1c47c 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 73 P4_COLLSEQ);. s
1c47d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1c47e 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 P5(pParse->pVdbe
1c47f 2c 20 70 35 29 3b 0a 20 20 69 66 28 20 28 70 35 , p5);. if( (p5
1c480 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 & SQLITE_AFF_MA
1c481 53 4b 29 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f SK)!=SQLITE_AFF_
1c482 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 73 71 6c 69 NONE ){. sqli
1c483 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 te3ExprCacheAffi
1c484 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 nityChange(pPars
1c485 65 2c 20 69 6e 31 2c 20 31 29 3b 0a 20 20 20 20 e, in1, 1);.
1c486 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
1c487 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 AffinityChange(p
1c488 50 61 72 73 65 2c 20 69 6e 32 2c 20 31 29 3b 0a Parse, in2, 1);.
1c489 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 }. return add
1c48a 72 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 r;.}..#if SQLITE
1c48b 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e _MAX_EXPR_DEPTH>
1c48c 30 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 0./*.** Check th
1c48d 61 74 20 61 72 67 75 6d 65 6e 74 20 6e 48 65 69 at argument nHei
1c48e 67 68 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e ght is less than
1c48f 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
1c490 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 65 78 70 72 maximum.** expr
1c491 65 73 73 69 6f 6e 20 64 65 70 74 68 20 61 6c 6c ession depth all
1c492 6f 77 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e owed. If it is n
1c493 6f 74 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 ot, leave an err
1c494 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 0a 2a 2a or message in.**
1c495 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 pParse..*/.SQLI
1c496 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1c497 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 qlite3ExprCheckH
1c498 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 eight(Parse *pPa
1c499 72 73 65 2c 20 69 6e 74 20 6e 48 65 69 67 68 74 rse, int nHeight
1c49a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1c49b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6d LITE_OK;. int m
1c49c 78 48 65 69 67 68 74 20 3d 20 70 50 61 72 73 65 xHeight = pParse
1c49d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ->db->aLimit[SQL
1c49e 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 ITE_LIMIT_EXPR_D
1c49f 45 50 54 48 5d 3b 0a 20 20 69 66 28 20 6e 48 65 EPTH];. if( nHe
1c4a0 69 67 68 74 3e 6d 78 48 65 69 67 68 74 20 29 7b ight>mxHeight ){
1c4a1 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1c4a2 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 rMsg(pParse, .
1c4a3 20 20 20 20 20 22 45 78 70 72 65 73 73 69 6f 6e "Expression
1c4a4 20 74 72 65 65 20 69 73 20 74 6f 6f 20 6c 61 72 tree is too lar
1c4a5 67 65 20 28 6d 61 78 69 6d 75 6d 20 64 65 70 74 ge (maximum dept
1c4a6 68 20 25 64 29 22 2c 20 6d 78 48 65 69 67 68 74 h %d)", mxHeight
1c4a7 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 63 20 3d . );. rc =
1c4a8 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1c4a9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1c4aa 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 }../* The follow
1c4ab 69 6e 67 20 74 68 72 65 65 20 66 75 6e 63 74 69 ing three functi
1c4ac 6f 6e 73 2c 20 68 65 69 67 68 74 4f 66 45 78 70 ons, heightOfExp
1c4ad 72 28 29 2c 20 68 65 69 67 68 74 4f 66 45 78 70 r(), heightOfExp
1c4ae 72 4c 69 73 74 28 29 0a 2a 2a 20 61 6e 64 20 68 rList().** and h
1c4af 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 29 2c eightOfSelect(),
1c4b0 20 61 72 65 20 75 73 65 64 20 74 6f 20 64 65 74 are used to det
1c4b1 65 72 6d 69 6e 65 20 74 68 65 20 6d 61 78 69 6d ermine the maxim
1c4b2 75 6d 20 68 65 69 67 68 74 0a 2a 2a 20 6f 66 20 um height.** of
1c4b3 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 any expression t
1c4b4 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 ree referenced b
1c4b5 79 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 y the structure
1c4b6 70 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a passed as the.**
1c4b7 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
1c4b8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d .**.** If this m
1c4b9 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 69 73 aximum height is
1c4ba 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
1c4bb 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
1c4bc 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 pointed.** to by
1c4bd 20 70 6e 48 65 69 67 68 74 2c 20 74 68 65 20 73 pnHeight, the s
1c4be 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2c econd parameter,
1c4bf 20 74 68 65 6e 20 73 65 74 20 2a 70 6e 48 65 69 then set *pnHei
1c4c0 67 68 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 76 ght to that.** v
1c4c1 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 alue..*/.static
1c4c2 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78 70 void heightOfExp
1c4c3 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a r(Expr *p, int *
1c4c4 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 pnHeight){. if(
1c4c5 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p ){. if( p-
1c4c6 3e 6e 48 65 69 67 68 74 3e 2a 70 6e 48 65 69 67 >nHeight>*pnHeig
1c4c7 68 74 20 29 7b 0a 20 20 20 20 20 20 2a 70 6e 48 ht ){. *pnH
1c4c8 65 69 67 68 74 20 3d 20 70 2d 3e 6e 48 65 69 67 eight = p->nHeig
1c4c9 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a ht;. }. }.}.
1c4ca 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 static void heig
1c4cb 68 74 4f 66 45 78 70 72 4c 69 73 74 28 45 78 70 htOfExprList(Exp
1c4cc 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 2a 70 rList *p, int *p
1c4cd 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 nHeight){. if(
1c4ce 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a p ){. int i;.
1c4cf 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1c4d0 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 ->nExpr; i++){.
1c4d1 20 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 heightOfExp
1c4d2 72 28 70 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c r(p->a[i].pExpr,
1c4d3 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 pnHeight);.
1c4d4 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 }. }.}.static v
1c4d5 6f 69 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65 oid heightOfSele
1c4d6 63 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e ct(Select *p, in
1c4d7 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 t *pnHeight){.
1c4d8 69 66 28 20 70 20 29 7b 0a 20 20 20 20 68 65 69 if( p ){. hei
1c4d9 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 57 68 ghtOfExpr(p->pWh
1c4da 65 72 65 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a ere, pnHeight);.
1c4db 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 heightOfExpr
1c4dc 28 70 2d 3e 70 48 61 76 69 6e 67 2c 20 70 6e 48 (p->pHaving, pnH
1c4dd 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 eight);. heig
1c4de 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4c 69 6d htOfExpr(p->pLim
1c4df 69 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 it, pnHeight);.
1c4e0 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 heightOfExpr(
1c4e1 70 2d 3e 70 4f 66 66 73 65 74 2c 20 70 6e 48 65 p->pOffset, pnHe
1c4e2 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 ight);. heigh
1c4e3 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 tOfExprList(p->p
1c4e4 45 4c 69 73 74 2c 20 70 6e 48 65 69 67 68 74 29 EList, pnHeight)
1c4e5 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 ;. heightOfEx
1c4e6 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f 75 70 prList(p->pGroup
1c4e7 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 By, pnHeight);.
1c4e8 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c heightOfExprL
1c4e9 69 73 74 28 70 2d 3e 70 4f 72 64 65 72 42 79 2c ist(p->pOrderBy,
1c4ea 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 pnHeight);.
1c4eb 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 heightOfSelect(p
1c4ec 2d 3e 70 50 72 69 6f 72 2c 20 70 6e 48 65 69 67 ->pPrior, pnHeig
1c4ed 68 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ht);. }.}../*.*
1c4ee 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e 6e * Set the Expr.n
1c4ef 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65 20 Height variable
1c4f0 69 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 in the structure
1c4f1 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 0a 2a passed as an .*
1c4f2 2a 20 61 72 67 75 6d 65 6e 74 2e 20 41 6e 20 65 * argument. An e
1c4f3 78 70 72 65 73 73 69 6f 6e 20 77 69 74 68 20 6e xpression with n
1c4f4 6f 20 63 68 69 6c 64 72 65 6e 2c 20 45 78 70 72 o children, Expr
1c4f5 2e 70 4c 69 73 74 20 6f 72 20 0a 2a 2a 20 45 78 .pList or .** Ex
1c4f6 70 72 2e 70 53 65 6c 65 63 74 20 6d 65 6d 62 65 pr.pSelect membe
1c4f7 72 20 68 61 73 20 61 20 68 65 69 67 68 74 20 6f r has a height o
1c4f8 66 20 31 2e 20 41 6e 79 20 6f 74 68 65 72 20 65 f 1. Any other e
1c4f9 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 68 61 73 xpression.** has
1c4fa 20 61 20 68 65 69 67 68 74 20 65 71 75 61 6c 20 a height equal
1c4fb 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 to the maximum h
1c4fc 65 69 67 68 74 20 6f 66 20 61 6e 79 20 6f 74 68 eight of any oth
1c4fd 65 72 20 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 er .** reference
1c4fe 64 20 45 78 70 72 20 70 6c 75 73 20 6f 6e 65 2e d Expr plus one.
1c4ff 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1c500 65 78 70 72 53 65 74 48 65 69 67 68 74 28 45 78 exprSetHeight(Ex
1c501 70 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 48 pr *p){. int nH
1c502 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65 69 eight = 0;. hei
1c503 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4c 65 ghtOfExpr(p->pLe
1c504 66 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 ft, &nHeight);.
1c505 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d heightOfExpr(p-
1c506 3e 70 52 69 67 68 74 2c 20 26 6e 48 65 69 67 68 >pRight, &nHeigh
1c507 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 t);. heightOfEx
1c508 70 72 4c 69 73 74 28 70 2d 3e 70 4c 69 73 74 2c prList(p->pList,
1c509 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 &nHeight);. he
1c50a 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e ightOfSelect(p->
1c50b 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 67 68 pSelect, &nHeigh
1c50c 74 29 3b 0a 20 20 70 2d 3e 6e 48 65 69 67 68 74 t);. p->nHeight
1c50d 20 3d 20 6e 48 65 69 67 68 74 20 2b 20 31 3b 0a = nHeight + 1;.
1c50e 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
1c50f 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61 Expr.nHeight va
1c510 72 69 61 62 6c 65 20 75 73 69 6e 67 20 74 68 65 riable using the
1c511 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 29 exprSetHeight()
1c512 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 0a 2a 2a function. If.**
1c513 20 74 68 65 20 68 65 69 67 68 74 20 69 73 20 67 the height is g
1c514 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
1c515 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 maximum allowed
1c516 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 74 68 expression depth
1c517 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 ,.** leave an er
1c518 72 6f 72 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a ror in pParse..*
1c519 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1c51a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
1c51b 72 53 65 74 48 65 69 67 68 74 28 50 61 72 73 65 rSetHeight(Parse
1c51c 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
1c51d 70 29 7b 0a 20 20 65 78 70 72 53 65 74 48 65 69 p){. exprSetHei
1c51e 67 68 74 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ght(p);. sqlite
1c51f 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 3ExprCheckHeight
1c520 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48 65 69 (pParse, p->nHei
1c521 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ght);.}../*.** R
1c522 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 eturn the maximu
1c523 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e 79 20 m height of any
1c524 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 expression tree
1c525 72 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20 62 79 referenced.** by
1c526 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 61 74 the select stat
1c527 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73 20 ement passed as
1c528 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a an argument..*/.
1c529 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1c52a 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 nt sqlite3Select
1c52b 45 78 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 ExprHeight(Selec
1c52c 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65 t *p){. int nHe
1c52d 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65 69 67 ight = 0;. heig
1c52e 68 74 4f 66 53 65 6c 65 63 74 28 70 2c 20 26 6e htOfSelect(p, &n
1c52f 48 65 69 67 68 74 29 3b 0a 20 20 72 65 74 75 72 Height);. retur
1c530 6e 20 6e 48 65 69 67 68 74 3b 0a 7d 0a 23 65 6c n nHeight;.}.#el
1c531 73 65 0a 20 20 23 64 65 66 69 6e 65 20 65 78 70 se. #define exp
1c532 72 53 65 74 48 65 69 67 68 74 28 79 29 0a 23 65 rSetHeight(y).#e
1c533 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d ndif /* SQLITE_M
1c534 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 20 AX_EXPR_DEPTH>0
1c535 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 */../*.** Constr
1c536 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 uct a new expres
1c537 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 sion node and re
1c538 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
1c539 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a o it. Memory.**
1c53a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 65 20 69 for this node i
1c53b 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
1c53c 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
1c53d 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 . The calling f
1c53e 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 72 65 unction.** is re
1c53f 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61 sponsible for ma
1c540 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20 6e 6f king sure the no
1c541 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 67 65 de eventually ge
1c542 74 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 53 51 4c ts freed..*/.SQL
1c543 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
1c544 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 0a 20 *sqlite3Expr(.
1c545 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
1c546 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 /* Hand
1c547 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44 62 le for sqlite3Db
1c548 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61 MallocZero() (ma
1c549 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 20 y be null) */.
1c54a 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 int op,
1c54b 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 /* Expre
1c54c 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a ssion opcode */.
1c54d 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 Expr *pLeft,
1c54e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 /* Lef
1c54f 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 t operand */. E
1c550 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 xpr *pRight,
1c551 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 /* Right
1c552 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e operand */. con
1c553 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e st Token *pToken
1c554 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
1c555 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 token */.){. E
1c556 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 xpr *pNew;. pNe
1c557 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
1c558 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 locZero(db, size
1c559 6f 66 28 45 78 70 72 29 29 3b 0a 20 20 69 66 28 of(Expr));. if(
1c55a 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 pNew==0 ){.
1c55b 2f 2a 20 57 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 /* When malloc f
1c55c 61 69 6c 73 2c 20 64 65 6c 65 74 65 20 70 4c 65 ails, delete pLe
1c55d 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e 20 45 ft and pRight. E
1c55e 78 70 72 65 73 73 69 6f 6e 73 20 70 61 73 73 65 xpressions passe
1c55f 64 20 74 6f 20 0a 20 20 20 20 2a 2a 20 74 68 69 d to . ** thi
1c560 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 s function must
1c561 61 6c 77 61 79 73 20 62 65 20 61 6c 6c 6f 63 61 always be alloca
1c562 74 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33 ted with sqlite3
1c563 45 78 70 72 28 29 20 66 6f 72 20 74 68 69 73 20 Expr() for this
1c564 0a 20 20 20 20 2a 2a 20 72 65 61 73 6f 6e 2e 20 . ** reason.
1c565 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
1c566 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1c567 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 , pLeft);. sq
1c568 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1c569 64 62 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 db, pRight);.
1c56a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
1c56b 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 6f 70 3b 0a pNew->op = op;.
1c56c 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 pNew->pLeft =
1c56d 70 4c 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 pLeft;. pNew->p
1c56e 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a Right = pRight;.
1c56f 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d pNew->iAgg = -
1c570 31 3b 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 1;. pNew->span.
1c571 7a 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 69 z = (u8*)"";. i
1c572 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 f( pToken ){.
1c573 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d assert( pToken-
1c574 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 70 >dyn==0 );. p
1c575 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 70 4e 65 77 New->span = pNew
1c576 2d 3e 74 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b 65 ->token = *pToke
1c577 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 n;. }else if( p
1c578 4c 65 66 74 20 29 7b 0a 20 20 20 20 69 66 28 20 Left ){. if(
1c579 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 pRight ){.
1c57a 69 66 28 20 70 52 69 67 68 74 2d 3e 73 70 61 6e if( pRight->span
1c57b 2e 64 79 6e 3d 3d 30 20 26 26 20 70 4c 65 66 74 .dyn==0 && pLeft
1c57c 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d 30 20 29 7b ->span.dyn==0 ){
1c57d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1c57e 45 78 70 72 53 70 61 6e 28 70 4e 65 77 2c 20 26 ExprSpan(pNew, &
1c57f 70 4c 65 66 74 2d 3e 73 70 61 6e 2c 20 26 70 52 pLeft->span, &pR
1c580 69 67 68 74 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 ight->span);.
1c581 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 }. if( p
1c582 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 Right->flags & E
1c583 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a P_ExpCollate ){.
1c584 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c pNew->fl
1c585 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c ags |= EP_ExpCol
1c586 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 4e late;. pN
1c587 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67 ew->pColl = pRig
1c588 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 ht->pColl;.
1c589 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
1c58a 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 pLeft->flags &
1c58b 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b EP_ExpCollate ){
1c58c 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 . pNew->fla
1c58d 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c gs |= EP_ExpColl
1c58e 61 74 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d ate;. pNew-
1c58f 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e >pColl = pLeft->
1c590 70 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d pColl;. }. }
1c591 0a 0a 20 20 65 78 70 72 53 65 74 48 65 69 67 68 .. exprSetHeigh
1c592 74 28 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 t(pNew);. retur
1c593 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
1c594 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 Works like sqli
1c595 74 65 33 45 78 70 72 28 29 20 65 78 63 65 70 74 te3Expr() except
1c596 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61 that it takes a
1c597 6e 20 65 78 74 72 61 20 50 61 72 73 65 2a 0a 2a n extra Parse*.*
1c598 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 6e * argument and n
1c599 6f 74 69 66 69 65 73 20 74 68 65 20 61 73 73 6f otifies the asso
1c59a 63 69 61 74 65 64 20 63 6f 6e 6e 65 63 74 69 6f ciated connectio
1c59b 6e 20 6f 62 6a 65 63 74 20 69 66 20 6d 61 6c 6c n object if mall
1c59c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c oc fails..*/.SQL
1c59d 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
1c59e 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 0a *sqlite3PExpr(.
1c59f 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
1c5a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 /* Par
1c5a1 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
1c5a2 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 int op,
1c5a3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
1c5a4 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a ression opcode *
1c5a5 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c /. Expr *pLeft,
1c5a6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1c5a7 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 eft operand */.
1c5a8 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 Expr *pRight,
1c5a9 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 /* Righ
1c5aa 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 t operand */. c
1c5ab 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b onst Token *pTok
1c5ac 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 en /* Argume
1c5ad 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 nt token */.){.
1c5ae 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 Expr *p = sqlit
1c5af 65 33 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64 e3Expr(pParse->d
1c5b0 62 2c 20 6f 70 2c 20 70 4c 65 66 74 2c 20 70 52 b, op, pLeft, pR
1c5b1 69 67 68 74 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 ight, pToken);.
1c5b2 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 if( p ){. sq
1c5b3 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 lite3ExprCheckHe
1c5b4 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e ight(pParse, p->
1c5b5 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 nHeight);. }.
1c5b6 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
1c5b7 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 61 20 ** When doing a
1c5b8 6e 65 73 74 65 64 20 70 61 72 73 65 2c 20 79 6f nested parse, yo
1c5b9 75 20 63 61 6e 20 69 6e 63 6c 75 64 65 20 74 65 u can include te
1c5ba 72 6d 73 20 69 6e 20 61 6e 20 65 78 70 72 65 73 rms in an expres
1c5bb 73 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 6c 6f 6f sion.** that loo
1c5bc 6b 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 20 23 k like this: #
1c5bd 31 20 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20 1 #2 ... These
1c5be 74 65 72 6d 73 20 72 65 66 65 72 20 74 6f 20 72 terms refer to r
1c5bf 65 67 69 73 74 65 72 73 0a 2a 2a 20 69 6e 20 74 egisters.** in t
1c5c0 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 he virtual machi
1c5c1 6e 65 2e 20 20 23 4e 20 69 73 20 74 68 65 20 4e ne. #N is the N
1c5c2 2d 74 68 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a -th register..**
1c5c3 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1c5c4 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
1c5c5 65 20 70 61 72 73 65 72 20 74 6f 20 64 65 61 6c e parser to deal
1c5c6 20 77 69 74 68 20 6f 6e 20 6f 66 20 74 68 6f 73 with on of thos
1c5c7 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 49 74 20 69 e terms..** It i
1c5c8 6d 6d 65 64 69 61 74 65 6c 79 20 67 65 6e 65 72 mmediately gener
1c5c9 61 74 65 73 20 63 6f 64 65 20 74 6f 20 73 74 6f ates code to sto
1c5ca 72 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 re the value in
1c5cb 61 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f a memory locatio
1c5cc 6e 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e n..** The return
1c5cd 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 s an expression
1c5ce 74 68 61 74 20 77 69 6c 6c 20 63 6f 64 65 20 74 that will code t
1c5cf 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 76 61 o extract the va
1c5d0 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 61 74 lue from.** that
1c5d1 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e memory location
1c5d2 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 as needed..*/.S
1c5d3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
1c5d4 70 72 20 2a 73 71 6c 69 74 65 33 52 65 67 69 73 pr *sqlite3Regis
1c5d5 74 65 72 45 78 70 72 28 50 61 72 73 65 20 2a 70 terExpr(Parse *p
1c5d6 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 Parse, Token *pT
1c5d7 6f 6b 65 6e 29 7b 0a 20 20 56 64 62 65 20 2a 76 oken){. Vdbe *v
1c5d8 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
1c5d9 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 69 ;. Expr *p;. i
1c5da 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 f( pParse->neste
1c5db 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 d==0 ){. sqli
1c5dc 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1c5dd 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c 22 se, "near \"%T\"
1c5de 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 22 2c : syntax error",
1c5df 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 72 65 pToken);. re
1c5e0 74 75 72 6e 20 73 71 6c 69 74 65 33 50 45 78 70 turn sqlite3PExp
1c5e1 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c r(pParse, TK_NUL
1c5e2 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d L, 0, 0, 0);. }
1c5e3 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 . if( v==0 ) re
1c5e4 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 73 71 turn 0;. p = sq
1c5e5 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 lite3PExpr(pPars
1c5e6 65 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 e, TK_REGISTER,
1c5e7 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 0, 0, pToken);.
1c5e8 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 if( p==0 ){.
1c5e9 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4d return 0; /* M
1c5ea 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a alloc failed */.
1c5eb 20 20 7d 0a 20 20 70 2d 3e 69 54 61 62 6c 65 20 }. p->iTable
1c5ec 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70 = atoi((char*)&p
1c5ed 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20 Token->z[1]);.
1c5ee 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
1c5ef 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72 ** Join two expr
1c5f0 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e essions using an
1c5f1 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20 AND operator.
1c5f2 49 66 20 65 69 74 68 65 72 20 65 78 70 72 65 73 If either expres
1c5f3 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c sion is.** NULL,
1c5f4 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 then just retur
1c5f5 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 n the other expr
1c5f6 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ession..*/.SQLIT
1c5f7 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
1c5f8 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 sqlite3ExprAnd(s
1c5f9 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 qlite3 *db, Expr
1c5fa 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70 *pLeft, Expr *p
1c5fb 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c Right){. if( pL
1c5fc 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 eft==0 ){. re
1c5fd 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d turn pRight;. }
1c5fe 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 3d else if( pRight=
1c5ff 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
1c600 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b pLeft;. }else{
1c601 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
1c602 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 41 te3Expr(db, TK_A
1c603 4e 44 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 ND, pLeft, pRigh
1c604 74 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a t, 0);. }.}../*
1c605 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 .** Set the Expr
1c606 2e 73 70 61 6e 20 66 69 65 6c 64 20 6f 66 20 74 .span field of t
1c607 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 he given express
1c608 69 6f 6e 20 74 6f 20 73 70 61 6e 20 61 6c 6c 0a ion to span all.
1c609 2a 2a 20 74 65 78 74 20 62 65 74 77 65 65 6e 20 ** text between
1c60a 74 68 65 20 74 77 6f 20 67 69 76 65 6e 20 74 6f the two given to
1c60b 6b 65 6e 73 2e 20 20 42 6f 74 68 20 74 6f 6b 65 kens. Both toke
1c60c 6e 73 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 ns must be point
1c60d 69 6e 67 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 ing.** at the sa
1c60e 6d 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 me string..*/.SQ
1c60f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1c610 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 d sqlite3ExprSpa
1c611 6e 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54 n(Expr *pExpr, T
1c612 6f 6b 65 6e 20 2a 70 4c 65 66 74 2c 20 54 6f 6b oken *pLeft, Tok
1c613 65 6e 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 en *pRight){. a
1c614 73 73 65 72 74 28 20 70 52 69 67 68 74 21 3d 30 ssert( pRight!=0
1c615 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c );. assert( pL
1c616 65 66 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 eft!=0 );. if(
1c617 70 45 78 70 72 20 29 7b 0a 20 20 20 20 70 45 78 pExpr ){. pEx
1c618 70 72 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 4c 65 pr->span.z = pLe
1c619 66 74 2d 3e 7a 3b 0a 20 20 20 20 70 45 78 70 72 ft->z;. pExpr
1c61a 2d 3e 73 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68 ->span.n = pRigh
1c61b 74 2d 3e 6e 20 2b 20 28 70 52 69 67 68 74 2d 3e t->n + (pRight->
1c61c 7a 20 2d 20 70 4c 65 66 74 2d 3e 7a 29 3b 0a 20 z - pLeft->z);.
1c61d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 }.}../*.** Cons
1c61e 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 truct a new expr
1c61f 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 ession node for
1c620 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 a function with
1c621 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75 multiple.** argu
1c622 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ments..*/.SQLITE
1c623 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
1c624 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 qlite3ExprFuncti
1c625 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 on(Parse *pParse
1c626 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 , ExprList *pLis
1c627 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e t, Token *pToken
1c628 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b ){. Expr *pNew;
1c629 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1c62a 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 pParse->db;. a
1c62b 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b ssert( pToken );
1c62c 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
1c62d 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
1c62e 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 29 20 29 , sizeof(Expr) )
1c62f 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
1c630 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
1c631 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c prListDelete(db,
1c632 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69 pList); /* Avoi
1c633 64 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 d leaking memory
1c634 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 when malloc fai
1c635 6c 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e ls */. return
1c636 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 0;. }. pNew->
1c637 6f 70 20 3d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e op = TK_FUNCTION
1c638 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 73 74 20 ;. pNew->pList
1c639 3d 20 70 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 = pList;. asser
1c63a 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d t( pToken->dyn==
1c63b 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 74 6f 6b 0 );. pNew->tok
1c63c 65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 en = *pToken;.
1c63d 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 70 4e 65 pNew->span = pNe
1c63e 77 2d 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 73 71 6c w->token;.. sql
1c63f 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 ite3ExprSetHeigh
1c640 74 28 70 50 61 72 73 65 2c 20 70 4e 65 77 29 3b t(pParse, pNew);
1c641 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
1c642 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 }../*.** Assign
1c643 61 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 a variable numbe
1c644 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 r to an expressi
1c645 6f 6e 20 74 68 61 74 20 65 6e 63 6f 64 65 73 20 on that encodes
1c646 61 20 77 69 6c 64 63 61 72 64 0a 2a 2a 20 69 6e a wildcard.** in
1c647 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 the original SQ
1c648 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a L statement. .*
1c649 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 63 *.** Wildcards c
1c64a 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 onsisting of a s
1c64b 69 6e 67 6c 65 20 22 3f 22 20 61 72 65 20 61 73 ingle "?" are as
1c64c 73 69 67 6e 65 64 20 74 68 65 20 6e 65 78 74 20 signed the next
1c64d 73 65 71 75 65 6e 74 69 61 6c 0a 2a 2a 20 76 61 sequential.** va
1c64e 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a riable number..*
1c64f 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f *.** Wildcards o
1c650 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e f the form "?nnn
1c651 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 " are assigned t
1c652 68 65 20 6e 75 6d 62 65 72 20 22 6e 6e 6e 22 2e he number "nnn".
1c653 20 20 57 65 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 We make.** sur
1c654 65 20 22 6e 6e 6e 22 20 69 73 20 6e 6f 74 20 74 e "nnn" is not t
1c655 6f 6f 20 62 65 20 74 6f 20 61 76 6f 69 64 20 61 oo be to avoid a
1c656 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 denial of servi
1c657 63 65 20 61 74 74 61 63 6b 20 77 68 65 6e 0a 2a ce attack when.*
1c658 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d * the SQL statem
1c659 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 ent comes from a
1c65a 6e 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 n external sourc
1c65b 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 e..**.** Wildcar
1c65c 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 ds of the form "
1c65d 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 22 20 :aaa" or "$aaa"
1c65e 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 are assigned the
1c65f 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 same number.**
1c660 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 as the previous
1c661 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
1c662 73 61 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 same wildcard.
1c663 4f 72 20 69 66 20 74 68 69 73 20 69 73 20 74 68 Or if this is th
1c664 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 e first.** insta
1c665 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 nce of the wildc
1c666 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 ard, the next se
1c667 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 quenial variable
1c668 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 number is.** as
1c669 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 signed..*/.SQLIT
1c66a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1c66b 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e qlite3ExprAssign
1c66c 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 VarNumber(Parse
1c66d 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
1c66e 45 78 70 72 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a Expr){. Token *
1c66f 70 54 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65 pToken;. sqlite
1c670 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1c671 64 62 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 db;.. if( pExpr
1c672 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
1c673 70 54 6f 6b 65 6e 20 3d 20 26 70 45 78 70 72 2d pToken = &pExpr-
1c674 3e 74 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 >token;. assert
1c675 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 3d 31 20 29 ( pToken->n>=1 )
1c676 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b ;. assert( pTok
1c677 65 6e 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 61 73 en->z!=0 );. as
1c678 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b sert( pToken->z[
1c679 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 0]!=0 );. if( p
1c67a 54 6f 6b 65 6e 2d 3e 6e 3d 3d 31 20 29 7b 0a 20 Token->n==1 ){.
1c67b 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f /* Wildcard o
1c67c 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20 f the form "?".
1c67d 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74 Assign the next
1c67e 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 variable number
1c67f 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 */. pExpr->i
1c680 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65 Table = ++pParse
1c681 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c 73 65 20 ->nVar;. }else
1c682 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d if( pToken->z[0]
1c683 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20 =='?' ){. /*
1c684 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 Wildcard of the
1c685 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f form "?nnn". Co
1c686 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61 nvert "nnn" to a
1c687 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a 20 20 n integer and.
1c688 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73 20 74 ** use it as t
1c689 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 he variable numb
1c68a 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b er */. int i;
1c68b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 . pExpr->iTab
1c68c 6c 65 20 3d 20 69 20 3d 20 61 74 6f 69 28 28 63 le = i = atoi((c
1c68d 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e 7a 5b har*)&pToken->z[
1c68e 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 1]);. testcas
1c68f 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20 74 e( i==0 );. t
1c690 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20 29 3b estcase( i==1 );
1c691 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 . testcase( i
1c692 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ==db->aLimit[SQL
1c693 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 ITE_LIMIT_VARIAB
1c694 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 29 3b 0a LE_NUMBER]-1 );.
1c695 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d testcase( i=
1c696 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 =db->aLimit[SQLI
1c697 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c TE_LIMIT_VARIABL
1c698 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20 20 20 E_NUMBER] );.
1c699 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 64 62 if( i<1 || i>db
1c69a 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
1c69b 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
1c69c 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 20 20 UMBER] ){.
1c69d 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1c69e 70 50 61 72 73 65 2c 20 22 76 61 72 69 61 62 6c pParse, "variabl
1c69f 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 62 65 e number must be
1c6a0 20 62 65 74 77 65 65 6e 20 3f 31 20 61 6e 64 20 between ?1 and
1c6a1 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 ?%d",.
1c6a2 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1c6a3 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 E_LIMIT_VARIABLE
1c6a4 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20 20 7d _NUMBER]);. }
1c6a5 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 72 73 . if( i>pPars
1c6a6 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 20 e->nVar ){.
1c6a7 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 pParse->nVar =
1c6a8 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 i;. }. }else
1c6a9 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 {. /* Wildcar
1c6aa 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 ds of the form "
1c6ab 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 22 2e :aaa" or "$aaa".
1c6ac 20 20 52 65 75 73 65 20 74 68 65 20 73 61 6d 65 Reuse the same
1c6ad 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a variable. **
1c6ae 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65 20 70 number as the p
1c6af 72 69 6f 72 20 61 70 70 65 61 72 61 6e 63 65 20 rior appearance
1c6b0 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 of the same name
1c6b1 2c 20 6f 72 20 69 66 20 74 68 65 20 6e 61 6d 65 , or if the name
1c6b2 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 65 76 65 . ** has neve
1c6b3 72 20 61 70 70 65 61 72 65 64 20 62 65 66 6f 72 r appeared befor
1c6b4 65 2c 20 72 65 75 73 65 20 74 68 65 20 73 61 6d e, reuse the sam
1c6b5 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 e variable numbe
1c6b6 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 r. */. int
1c6b7 20 69 2c 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 70 i, n;. n = p
1c6b8 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20 20 20 20 66 6f Token->n;. fo
1c6b9 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d r(i=0; i<pParse-
1c6ba 3e 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b 29 7b >nVarExpr; i++){
1c6bb 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 3b . Expr *pE;
1c6bc 0a 20 20 20 20 20 20 69 66 28 20 28 70 45 20 3d . if( (pE =
1c6bd 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 pParse->apVarEx
1c6be 70 72 5b 69 5d 29 21 3d 30 0a 20 20 20 20 20 20 pr[i])!=0.
1c6bf 20 20 20 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e && pE->token
1c6c0 2e 6e 3d 3d 6e 0a 20 20 20 20 20 20 20 20 20 20 .n==n.
1c6c1 26 26 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 74 6f && memcmp(pE->to
1c6c2 6b 65 6e 2e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 7a ken.z, pToken->z
1c6c3 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 , n)==0 ){.
1c6c4 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1c6c5 20 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b 0a 20 = pE->iTable;.
1c6c6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1c6c7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1c6c8 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e if( i>=pParse->n
1c6c9 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20 20 20 VarExpr ){.
1c6ca 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d pExpr->iTable =
1c6cb 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b ++pParse->nVar;
1c6cc 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 . if( pPars
1c6cd 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 50 61 e->nVarExpr>=pPa
1c6ce 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c rse->nVarExprAll
1c6cf 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 oc-1 ){.
1c6d0 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 pParse->nVarExpr
1c6d1 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 65 2d Alloc += pParse-
1c6d2 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b >nVarExprAlloc +
1c6d3 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70 50 61 10;. pPa
1c6d4 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 20 3d rse->apVarExpr =
1c6d5 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
1c6d6 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 ite3DbReallocOrF
1c6d7 72 65 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 ree(.
1c6d8 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 20 20 db,.
1c6d9 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 pParse->apV
1c6da 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 arExpr,.
1c6db 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 pParse->nV
1c6dc 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 7a 65 arExprAlloc*size
1c6dd 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56 61 72 of(pParse->apVar
1c6de 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 20 20 Expr[0]).
1c6df 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a );. }.
1c6e0 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d if( !db->m
1c6e1 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1c6e2 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1c6e3 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
1c6e4 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 !=0 );. p
1c6e5 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
1c6e6 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 [pParse->nVarExp
1c6e7 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a 20 20 r++] = pExpr;.
1c6e8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 }. }. }
1c6e9 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e . if( !pParse->
1c6ea 6e 45 72 72 20 26 26 20 70 50 61 72 73 65 2d 3e nErr && pParse->
1c6eb 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b nVar>db->aLimit[
1c6ec 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 SQLITE_LIMIT_VAR
1c6ed 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b IABLE_NUMBER] ){
1c6ee 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1c6ef 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f rMsg(pParse, "to
1c6f0 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 69 61 o many SQL varia
1c6f1 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f bles");. }.}../
1c6f2 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 65 78 *.** Clear an ex
1c6f3 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 pression structu
1c6f4 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74 re without delet
1c6f5 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 72 ing the structur
1c6f6 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 75 62 e itself..** Sub
1c6f7 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c structure is del
1c6f8 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eted..*/.SQLITE_
1c6f9 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1c6fa 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 ite3ExprClear(sq
1c6fb 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 lite3 *db, Expr
1c6fc 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 70 *p){. if( p->sp
1c6fd 61 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33 an.dyn ) sqlite3
1c6fe 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 61 72 DbFree(db, (char
1c6ff 2a 29 70 2d 3e 73 70 61 6e 2e 7a 29 3b 0a 20 20 *)p->span.z);.
1c700 69 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 64 79 6e if( p->token.dyn
1c701 20 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 ) sqlite3DbFree
1c702 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 74 (db, (char*)p->t
1c703 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 73 71 6c 69 74 oken.z);. sqlit
1c704 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
1c705 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 73 71 p->pLeft);. sq
1c706 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1c707 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a db, p->pRight);.
1c708 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 sqlite3ExprLis
1c709 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 tDelete(db, p->p
1c70a 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 List);. sqlite3
1c70b 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c SelectDelete(db,
1c70c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 7d 0a p->pSelect);.}.
1c70d 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76 65 ./*.** Recursive
1c70e 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78 70 ly delete an exp
1c70f 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a 2f ression tree..*/
1c710 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1c711 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
1c712 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a Delete(sqlite3 *
1c713 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 db, Expr *p){.
1c714 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
1c715 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 n;. sqlite3Expr
1c716 43 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a 20 20 Clear(db, p);.
1c717 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1c718 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 , p);.}../*.** T
1c719 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 20 66 69 he Expr.token fi
1c71a 65 6c 64 20 6d 69 67 68 74 20 62 65 20 61 20 73 eld might be a s
1c71b 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 74 68 tring literal th
1c71c 61 74 20 69 73 20 71 75 6f 74 65 64 2e 0a 2a 2a at is quoted..**
1c71d 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65 20 74 If so, remove t
1c71e 68 65 20 71 75 6f 74 61 74 69 6f 6e 20 6d 61 72 he quotation mar
1c71f 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ks..*/.SQLITE_PR
1c720 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1c721 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 73 71 e3DequoteExpr(sq
1c722 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 lite3 *db, Expr
1c723 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 70 72 48 *p){. if( ExprH
1c724 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c asAnyProperty(p,
1c725 20 45 50 5f 44 65 71 75 6f 74 65 64 29 20 29 7b EP_Dequoted) ){
1c726 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
1c727 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 . ExprSetProper
1c728 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f 74 65 ty(p, EP_Dequote
1c729 64 29 3b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6b d);. if( p->tok
1c72a 65 6e 2e 64 79 6e 3d 3d 30 20 29 7b 0a 20 20 20 en.dyn==0 ){.
1c72b 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 sqlite3TokenCop
1c72c 79 28 64 62 2c 20 26 70 2d 3e 74 6f 6b 65 6e 2c y(db, &p->token,
1c72d 20 26 70 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d &p->token);. }
1c72e 0a 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 . sqlite3Dequot
1c72f 65 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f 6b 65 e((char*)p->toke
1c730 6e 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n.z);.}../*.** T
1c731 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 72 6f he following gro
1c732 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 6d up of routines m
1c733 61 6b 65 20 64 65 65 70 20 63 6f 70 69 65 73 20 ake deep copies
1c734 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a of expressions,.
1c735 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 ** expression li
1c736 73 74 73 2c 20 49 44 20 6c 69 73 74 73 2c 20 61 sts, ID lists, a
1c737 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d nd select statem
1c738 65 6e 74 73 2e 20 20 54 68 65 20 63 6f 70 69 65 ents. The copie
1c739 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64 65 6c 65 s can.** be dele
1c73a 74 65 64 20 28 62 79 20 62 65 69 6e 67 20 70 61 ted (by being pa
1c73b 73 73 65 64 20 74 6f 20 74 68 65 69 72 20 72 65 ssed to their re
1c73c 73 70 65 63 74 69 76 65 20 2e 2e 2e 44 65 6c 65 spective ...Dele
1c73d 74 65 28 29 20 72 6f 75 74 69 6e 65 73 29 0a 2a te() routines).*
1c73e 2a 20 77 69 74 68 6f 75 74 20 65 66 66 65 63 74 * without effect
1c73f 69 6e 67 20 74 68 65 20 6f 72 69 67 69 6e 61 6c ing the original
1c740 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 70 s..**.** The exp
1c741 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 49 44 ression list, ID
1c742 2c 20 61 6e 64 20 73 6f 75 72 63 65 20 6c 69 73 , and source lis
1c743 74 73 20 72 65 74 75 72 6e 20 62 79 20 73 71 6c ts return by sql
1c744 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 ite3ExprListDup(
1c745 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 49 64 4c ),.** sqlite3IdL
1c746 69 73 74 44 75 70 28 29 2c 20 61 6e 64 20 73 71 istDup(), and sq
1c747 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 lite3SrcListDup(
1c748 29 20 63 61 6e 20 6e 6f 74 20 62 65 20 66 75 72 ) can not be fur
1c749 74 68 65 72 20 65 78 70 61 6e 64 65 64 20 0a 2a ther expanded .*
1c74a 2a 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 * by subsequent
1c74b 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 2a calls to sqlite*
1c74c 4c 69 73 74 41 70 70 65 6e 64 28 29 20 72 6f 75 ListAppend() rou
1c74d 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 tines..**.** Any
1c74e 20 74 61 62 6c 65 73 20 74 68 61 74 20 74 68 65 tables that the
1c74f 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 70 SrcList might p
1c750 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e 6f 74 20 oint to are not
1c751 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a 2f 0a 53 duplicated..*/.S
1c752 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
1c753 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44 pr *sqlite3ExprD
1c754 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 up(sqlite3 *db,
1c755 45 78 70 72 20 2a 70 29 7b 0a 20 20 45 78 70 72 Expr *p){. Expr
1c756 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 3d *pNew;. if( p=
1c757 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
1c758 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 pNew = sqlite3D
1c759 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 bMallocRaw(db, s
1c75a 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 izeof(*p) );. i
1c75b 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 f( pNew==0 ) ret
1c75c 75 72 6e 20 30 3b 0a 20 20 6d 65 6d 63 70 79 28 urn 0;. memcpy(
1c75d 70 4e 65 77 2c 20 70 2c 20 73 69 7a 65 6f 66 28 pNew, p, sizeof(
1c75e 2a 70 4e 65 77 29 29 3b 0a 20 20 69 66 28 20 70 *pNew));. if( p
1c75f 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 30 20 29 7b 0a ->token.z!=0 ){.
1c760 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e pNew->token.
1c761 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 z = (u8*)sqlite3
1c762 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 DbStrNDup(db, (c
1c763 68 61 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 2c har*)p->token.z,
1c764 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 p->token.n);.
1c765 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 64 79 pNew->token.dy
1c766 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a n = 1;. }else{.
1c767 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 assert( pNew
1c768 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 29 3b 0a ->token.z==0 );.
1c769 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e }. pNew->span
1c76a 2e 7a 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e .z = 0;. pNew->
1c76b 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 pLeft = sqlite3E
1c76c 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c xprDup(db, p->pL
1c76d 65 66 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 eft);. pNew->pR
1c76e 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 ight = sqlite3Ex
1c76f 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52 69 prDup(db, p->pRi
1c770 67 68 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c ght);. pNew->pL
1c771 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ist = sqlite3Exp
1c772 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e rListDup(db, p->
1c773 70 4c 69 73 74 29 3b 0a 20 20 70 4e 65 77 2d 3e pList);. pNew->
1c774 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 pSelect = sqlite
1c775 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 3SelectDup(db, p
1c776 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 72 65 ->pSelect);. re
1c777 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c turn pNew;.}.SQL
1c778 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1c779 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 sqlite3TokenCop
1c77a 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 y(sqlite3 *db, T
1c77b 6f 6b 65 6e 20 2a 70 54 6f 2c 20 54 6f 6b 65 6e oken *pTo, Token
1c77c 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66 28 20 *pFrom){. if(
1c77d 70 54 6f 2d 3e 64 79 6e 20 29 20 73 71 6c 69 74 pTo->dyn ) sqlit
1c77e 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 e3DbFree(db, (ch
1c77f 61 72 2a 29 70 54 6f 2d 3e 7a 29 3b 0a 20 20 69 ar*)pTo->z);. i
1c780 66 28 20 70 46 72 6f 6d 2d 3e 7a 20 29 7b 0a 20 f( pFrom->z ){.
1c781 20 20 20 70 54 6f 2d 3e 6e 20 3d 20 70 46 72 6f pTo->n = pFro
1c782 6d 2d 3e 6e 3b 0a 20 20 20 20 70 54 6f 2d 3e 7a m->n;. pTo->z
1c783 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 44 = (u8*)sqlite3D
1c784 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 bStrNDup(db, (ch
1c785 61 72 2a 29 70 46 72 6f 6d 2d 3e 7a 2c 20 70 46 ar*)pFrom->z, pF
1c786 72 6f 6d 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 6f rom->n);. pTo
1c787 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c ->dyn = 1;. }el
1c788 73 65 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 20 3d se{. pTo->z =
1c789 20 30 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 0;. }.}.SQLITE
1c78a 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 _PRIVATE ExprLis
1c78b 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 t *sqlite3ExprLi
1c78c 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 stDup(sqlite3 *d
1c78d 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 29 7b b, ExprList *p){
1c78e 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 . ExprList *pNe
1c78f 77 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 w;. struct Expr
1c790 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1c791 2c 20 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 , *pOldItem;. i
1c792 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 nt i;. if( p==0
1c793 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 ) return 0;. p
1c794 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
1c795 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a allocRaw(db, siz
1c796 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 eof(*pNew) );.
1c797 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 if( pNew==0 ) re
1c798 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e turn 0;. pNew->
1c799 69 45 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 iECursor = 0;.
1c79a 70 4e 65 77 2d 3e 6e 45 78 70 72 20 3d 20 70 4e pNew->nExpr = pN
1c79b 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e ew->nAlloc = p->
1c79c 6e 45 78 70 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 nExpr;. pNew->a
1c79d 20 3d 20 70 49 74 65 6d 20 3d 20 73 71 6c 69 74 = pItem = sqlit
1c79e 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
1c79f 2c 20 20 70 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 , p->nExpr*size
1c7a0 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 of(p->a[0]) );.
1c7a1 20 69 66 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b if( pItem==0 ){
1c7a2 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1c7a3 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 ee(db, pNew);.
1c7a4 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 return 0;. }
1c7a5 0a 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d . pOldItem = p-
1c7a6 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 >a;. for(i=0; i
1c7a7 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 <p->nExpr; i++,
1c7a8 70 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 pItem++, pOldIte
1c7a9 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a m++){. Expr *
1c7aa 70 4e 65 77 45 78 70 72 2c 20 2a 70 4f 6c 64 45 pNewExpr, *pOldE
1c7ab 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e xpr;. pItem->
1c7ac 70 45 78 70 72 20 3d 20 70 4e 65 77 45 78 70 72 pExpr = pNewExpr
1c7ad 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 = sqlite3ExprDu
1c7ae 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72 20 3d p(db, pOldExpr =
1c7af 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72 pOldItem->pExpr
1c7b0 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 45 );. if( pOldE
1c7b1 78 70 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 26 xpr->span.z!=0 &
1c7b2 26 20 70 4e 65 77 45 78 70 72 20 29 7b 0a 20 20 & pNewExpr ){.
1c7b3 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 6d 61 /* Always ma
1c7b4 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ke a copy of the
1c7b5 20 73 70 61 6e 20 66 6f 72 20 74 6f 70 2d 6c 65 span for top-le
1c7b6 76 65 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 vel expressions
1c7b7 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 in the. **
1c7b8 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e expression list.
1c7b9 20 20 54 68 65 20 6c 6f 67 69 63 20 69 6e 20 53 The logic in S
1c7ba 45 4c 45 43 54 20 70 72 6f 63 65 73 73 69 6e 67 ELECT processing
1c7bb 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 that determines
1c7bc 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 61 . ** the na
1c7bd 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 mes of columns i
1c7be 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
1c7bf 20 6e 65 65 64 73 20 74 68 69 73 20 69 6e 66 6f needs this info
1c7c0 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 rmation */.
1c7c1 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 sqlite3TokenCop
1c7c2 79 28 64 62 2c 20 26 70 4e 65 77 45 78 70 72 2d y(db, &pNewExpr-
1c7c3 3e 73 70 61 6e 2c 20 26 70 4f 6c 64 45 78 70 72 >span, &pOldExpr
1c7c4 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 7d 0a 20 ->span);. }.
1c7c5 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 45 assert( pNewE
1c7c6 78 70 72 3d 3d 30 20 7c 7c 20 70 4e 65 77 45 78 xpr==0 || pNewEx
1c7c7 70 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 0a 20 pr->span.z!=0 .
1c7c8 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 4f || pO
1c7c9 6c 64 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 3d 3d ldExpr->span.z==
1c7ca 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 0. ||
1c7cb 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1c7cc 64 20 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e d );. pItem->
1c7cd 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 zName = sqlite3D
1c7ce 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 bStrDup(db, pOld
1c7cf 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 Item->zName);.
1c7d0 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 pItem->sortOrd
1c7d1 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 73 er = pOldItem->s
1c7d2 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 70 49 ortOrder;. pI
1c7d3 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20 tem->done = 0;.
1c7d4 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d pItem->iCol =
1c7d5 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b pOldItem->iCol;
1c7d6 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 . pItem->iAli
1c7d7 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 as = pOldItem->i
1c7d8 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 72 65 74 Alias;. }. ret
1c7d9 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
1c7da 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 2c 20 74 ** If cursors, t
1c7db 72 69 67 67 65 72 73 2c 20 76 69 65 77 73 20 61 riggers, views a
1c7dc 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 61 72 nd subqueries ar
1c7dd 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 20 66 72 e all omitted fr
1c7de 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2c om.** the build,
1c7df 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 then none of th
1c7e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
1c7e1 69 6e 65 73 2c 20 65 78 63 65 70 74 20 66 6f 72 ines, except for
1c7e2 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 .** sqlite3Sele
1c7e3 63 74 44 75 70 28 29 2c 20 63 61 6e 20 62 65 20 ctDup(), can be
1c7e4 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 65 33 53 called. sqlite3S
1c7e5 65 6c 65 63 74 44 75 70 28 29 20 69 73 20 73 6f electDup() is so
1c7e6 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65 metimes.** calle
1c7e7 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 61 72 d with a NULL ar
1c7e8 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21 gument..*/.#if !
1c7e9 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1c7ea 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 MIT_VIEW) || !de
1c7eb 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1c7ec 54 5f 54 52 49 47 47 45 52 29 20 5c 0a 20 7c 7c T_TRIGGER) \. ||
1c7ed 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1c7ee 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a _OMIT_SUBQUERY).
1c7ef 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
1c7f0 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 rcList *sqlite3S
1c7f1 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 rcListDup(sqlite
1c7f2 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 3 *db, SrcList *
1c7f3 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 p){. SrcList *p
1c7f4 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 New;. int i;.
1c7f5 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 28 int nByte;. if(
1c7f6 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
1c7f7 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 ;. nByte = size
1c7f8 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72 of(*p) + (p->nSr
1c7f9 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e c>0 ? sizeof(p->
1c7fa 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63 a[0]) * (p->nSrc
1c7fb 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77 -1) : 0);. pNew
1c7fc 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1c7fd 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 20 ocRaw(db, nByte
1c7fe 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 );. if( pNew==0
1c7ff 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 ) return 0;. p
1c800 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77 New->nSrc = pNew
1c801 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53 ->nAlloc = p->nS
1c802 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 rc;. for(i=0; i
1c803 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a <p->nSrc; i++){.
1c804 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 struct SrcLi
1c805 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 st_item *pNewIte
1c806 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b m = &pNew->a[i];
1c807 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c . struct SrcL
1c808 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 ist_item *pOldIt
1c809 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 em = &p->a[i];.
1c80a 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a Table *pTab;.
1c80b 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44 pNewItem->zD
1c80c 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 atabase = sqlite
1c80d 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 3DbStrDup(db, pO
1c80e 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 ldItem->zDatabas
1c80f 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d e);. pNewItem
1c810 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 ->zName = sqlite
1c811 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 3DbStrDup(db, pO
1c812 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a ldItem->zName);.
1c813 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41 pNewItem->zA
1c814 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 lias = sqlite3Db
1c815 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 StrDup(db, pOldI
1c816 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 tem->zAlias);.
1c817 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e pNewItem->join
1c818 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d type = pOldItem-
1c819 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 70 >jointype;. p
1c81a 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 NewItem->iCursor
1c81b 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75 = pOldItem->iCu
1c81c 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49 74 rsor;. pNewIt
1c81d 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 em->isPopulated
1c81e 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f = pOldItem->isPo
1c81f 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e 65 pulated;. pNe
1c820 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 wItem->zIndex =
1c821 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1c822 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49 db, pOldItem->zI
1c823 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 77 49 ndex);. pNewI
1c824 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 tem->notIndexed
1c825 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49 = pOldItem->notI
1c826 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e 65 77 ndexed;. pNew
1c827 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 Item->pIndex = p
1c828 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b OldItem->pIndex;
1c829 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e 65 77 . pTab = pNew
1c82a 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c Item->pTab = pOl
1c82b 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 dItem->pTab;.
1c82c 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 if( pTab ){.
1c82d 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b pTab->nRef++;
1c82e 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 49 . }. pNewI
1c82f 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 tem->pSelect = s
1c830 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 qlite3SelectDup(
1c831 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 db, pOldItem->pS
1c832 65 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e 65 77 elect);. pNew
1c833 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69 Item->pOn = sqli
1c834 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
1c835 4f 6c 64 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 OldItem->pOn);.
1c836 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73 pNewItem->pUs
1c837 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49 64 4c ing = sqlite3IdL
1c838 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 istDup(db, pOldI
1c839 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 tem->pUsing);.
1c83a 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 pNewItem->colU
1c83b 73 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e sed = pOldItem->
1c83c 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72 colUsed;. }. r
1c83d 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 eturn pNew;.}.SQ
1c83e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c LITE_PRIVATE IdL
1c83f 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 ist *sqlite3IdLi
1c840 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 stDup(sqlite3 *d
1c841 62 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20 b, IdList *p){.
1c842 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 IdList *pNew;.
1c843 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d int i;. if( p=
1c844 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
1c845 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 pNew = sqlite3D
1c846 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 bMallocRaw(db, s
1c847 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a izeof(*pNew) );.
1c848 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 if( pNew==0 )
1c849 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 return 0;. pNew
1c84a 2d 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 ->nId = pNew->nA
1c84b 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 lloc = p->nId;.
1c84c 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 pNew->a = sqlit
1c84d 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
1c84e 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 , p->nId*sizeof(
1c84f 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 p->a[0]) );. if
1c850 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a ( pNew->a==0 ){.
1c851 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1c852 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 e(db, pNew);.
1c853 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
1c854 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
1c855 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 Id; i++){. st
1c856 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d ruct IdList_item
1c857 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e *pNewItem = &pN
1c858 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 ew->a[i];. st
1c859 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d ruct IdList_item
1c85a 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d *pOldItem = &p-
1c85b 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49 >a[i];. pNewI
1c85c 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c tem->zName = sql
1c85d 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
1c85e 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 pOldItem->zName
1c85f 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d );. pNewItem-
1c860 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d >idx = pOldItem-
1c861 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 >idx;. }. retu
1c862 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 rn pNew;.}.SQLIT
1c863 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 E_PRIVATE Select
1c864 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 *sqlite3SelectD
1c865 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 up(sqlite3 *db,
1c866 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 65 Select *p){. Se
1c867 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 69 66 lect *pNew;. if
1c868 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
1c869 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 0;. pNew = sqli
1c86a 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
1c86b 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b b, sizeof(*p) );
1c86c 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 . if( pNew==0 )
1c86d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 return 0;. pNe
1c86e 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 w->pEList = sqli
1c86f 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 te3ExprListDup(d
1c870 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 b, p->pEList);.
1c871 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 pNew->pSrc = sq
1c872 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 lite3SrcListDup(
1c873 64 62 2c 20 70 2d 3e 70 53 72 63 29 3b 0a 20 20 db, p->pSrc);.
1c874 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d 20 73 pNew->pWhere = s
1c875 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
1c876 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 , p->pWhere);.
1c877 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d pNew->pGroupBy =
1c878 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
1c879 44 75 70 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 Dup(db, p->pGrou
1c87a 70 42 79 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 pBy);. pNew->pH
1c87b 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 aving = sqlite3E
1c87c 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 48 xprDup(db, p->pH
1c87d 61 76 69 6e 67 29 3b 0a 20 20 70 4e 65 77 2d 3e aving);. pNew->
1c87e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74 pOrderBy = sqlit
1c87f 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 e3ExprListDup(db
1c880 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a , p->pOrderBy);.
1c881 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d 3e pNew->op = p->
1c882 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72 69 op;. pNew->pPri
1c883 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 or = sqlite3Sele
1c884 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 50 72 ctDup(db, p->pPr
1c885 69 6f 72 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c ior);. pNew->pL
1c886 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 45 78 imit = sqlite3Ex
1c887 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 69 prDup(db, p->pLi
1c888 6d 69 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f mit);. pNew->pO
1c889 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45 ffset = sqlite3E
1c88a 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f xprDup(db, p->pO
1c88b 66 66 73 65 74 29 3b 0a 20 20 70 4e 65 77 2d 3e ffset);. pNew->
1c88c 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 70 4e iLimit = 0;. pN
1c88d 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b ew->iOffset = 0;
1c88e 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 . pNew->selFlag
1c88f 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 s = p->selFlags
1c890 26 20 7e 53 46 5f 55 73 65 73 45 70 68 65 6d 65 & ~SF_UsesEpheme
1c891 72 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69 ral;. pNew->pRi
1c892 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 70 ghtmost = 0;. p
1c893 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 New->addrOpenEph
1c894 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 m[0] = -1;. pNe
1c895 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b w->addrOpenEphm[
1c896 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 1] = -1;. pNew-
1c897 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d >addrOpenEphm[2]
1c898 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 = -1;. return
1c899 70 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51 pNew;.}.#else.SQ
1c89a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c LITE_PRIVATE Sel
1c89b 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 ect *sqlite3Sele
1c89c 63 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 ctDup(sqlite3 *d
1c89d 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 b, Select *p){.
1c89e 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 3b assert( p==0 );
1c89f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 . return 0;.}.#
1c8a0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 endif.../*.** Ad
1c8a1 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 d a new element
1c8a2 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61 6e to the end of an
1c8a3 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
1c8a4 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a 2a . If pList is.*
1c8a5 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c 4c * initially NULL
1c8a6 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 , then create a
1c8a7 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c new expression l
1c8a8 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ist..*/.SQLITE_P
1c8a9 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 RIVATE ExprList
1c8aa 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 *sqlite3ExprList
1c8ab 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73 65 20 Append(. Parse
1c8ac 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
1c8ad 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
1c8ae 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 text */. ExprLi
1c8af 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 st *pList,
1c8b0 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 /* List to whi
1c8b1 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d 69 ch to append. Mi
1c8b2 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 ght be NULL */.
1c8b3 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 Expr *pExpr,
1c8b4 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 /* Expr
1c8b5 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 70 ession to be app
1c8b6 65 6e 64 65 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e ended */. Token
1c8b7 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20 *pName
1c8b8 20 20 20 2f 2a 20 41 53 20 6b 65 79 77 6f 72 64 /* AS keyword
1c8b9 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 for the express
1c8ba 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 ion */.){. sqli
1c8bb 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1c8bc 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69 73 ->db;. if( pLis
1c8bd 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 t==0 ){. pLis
1c8be 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c t = sqlite3DbMal
1c8bf 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 locZero(db, size
1c8c0 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b 0a of(ExprList) );.
1c8c1 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 if( pList==0
1c8c2 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e ){. goto n
1c8c3 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 o_mem;. }.
1c8c4 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e assert( pList->
1c8c5 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 7d nAlloc==0 );. }
1c8c6 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 41 . if( pList->nA
1c8c7 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 78 lloc<=pList->nEx
1c8c8 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 pr ){. struct
1c8c9 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1c8ca 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 a;. int n = p
1c8cb 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b List->nAlloc*2 +
1c8cc 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c 69 4;. a = sqli
1c8cd 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c te3DbRealloc(db,
1c8ce 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 7a pList->a, n*siz
1c8cf 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 eof(pList->a[0])
1c8d0 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30 20 );. if( a==0
1c8d1 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f ){. goto no
1c8d2 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _mem;. }.
1c8d3 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 20 pList->a = a;.
1c8d4 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 pList->nAlloc
1c8d5 3d 20 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 = n;. }. asser
1c8d6 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 29 t( pList->a!=0 )
1c8d7 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 7c 7c ;. if( pExpr ||
1c8d8 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 73 74 pName ){. st
1c8d9 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
1c8da 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 em *pItem = &pLi
1c8db 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 st->a[pList->nEx
1c8dc 70 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 pr++];. memse
1c8dd 74 28 70 49 74 65 6d 2c 20 30 2c 20 73 69 7a 65 t(pItem, 0, size
1c8de 6f 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20 20 20 of(*pItem));.
1c8df 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 pItem->zName =
1c8e0 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
1c8e1 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b oken(db, pName);
1c8e2 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 . pItem->pExp
1c8e3 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 70 r = pExpr;. p
1c8e4 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30 Item->iAlias = 0
1c8e5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
1c8e6 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 List;..no_mem:
1c8e7 20 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c . /* Avoid l
1c8e8 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66 eaking memory if
1c8e9 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c malloc has fail
1c8ea 65 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ed. */. sqlite3
1c8eb 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1c8ec 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 Expr);. sqlite3
1c8ed 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
1c8ee 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 b, pList);. ret
1c8ef 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
1c8f0 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
1c8f1 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20 63 6f n list pEList co
1c8f2 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e ntains more than
1c8f3 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74 73 iLimit elements
1c8f4 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 ,.** leave an er
1c8f5 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 ror message in p
1c8f6 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 Parse..*/.SQLITE
1c8f7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1c8f8 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 lite3ExprListChe
1c8f9 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 72 73 ckLength(. Pars
1c8fa 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 78 70 e *pParse,. Exp
1c8fb 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a 20 rList *pEList,.
1c8fc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62 const char *zOb
1c8fd 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d 78 ject.){. int mx
1c8fe 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 = pParse->db->a
1c8ff 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1c900 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65 IT_COLUMN];. te
1c901 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26 stcase( pEList &
1c902 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d & pEList->nExpr=
1c903 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 61 73 =mx );. testcas
1c904 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c e( pEList && pEL
1c905 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31 ist->nExpr==mx+1
1c906 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 );. if( pEList
1c907 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 && pEList->nExp
1c908 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 r>mx ){. sqli
1c909 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1c90a 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f se, "too many co
1c90b 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a 4f lumns in %s", zO
1c90c 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f bject);. }.}../
1c90d 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 *.** Delete an e
1c90e 6e 74 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e ntire expression
1c90f 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 list..*/.SQLITE
1c910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1c911 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
1c912 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ete(sqlite3 *db,
1c913 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
1c914 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 ){. int i;. st
1c915 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
1c916 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 em *pItem;. if(
1c917 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 pList==0 ) retu
1c918 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c rn;. assert( pL
1c919 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70 4c ist->a!=0 || (pL
1c91a 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26 26 ist->nExpr==0 &&
1c91b 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d pList->nAlloc==
1c91c 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 0) );. assert(
1c91d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c pList->nExpr<=pL
1c91e 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 ist->nAlloc );.
1c91f 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 for(pItem=pList
1c920 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 ->a, i=0; i<pLis
1c921 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 t->nExpr; i++, p
1c922 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c Item++){. sql
1c923 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
1c924 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 b, pItem->pExpr)
1c925 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
1c926 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a ree(db, pItem->z
1c927 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c Name);. }. sql
1c928 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1c929 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 List->a);. sqli
1c92a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c te3DbFree(db, pL
1c92b 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ist);.}../*.** T
1c92c 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
1c92d 65 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63 e Walker callbac
1c92e 6b 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e 70 69 ks. Walker.u.pi
1c92f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a is a pointer.**
1c930 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 to an integer.
1c931 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
1c932 61 72 65 20 63 68 65 63 6b 69 6e 67 20 61 6e 20 are checking an
1c933 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 65 expression to se
1c934 65 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 20 e.** if it is a
1c935 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74 20 2a constant. Set *
1c936 57 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f 20 30 Walker.u.pi to 0
1c937 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 if the expressi
1c938 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e on is.** not con
1c939 73 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 stant..**.** The
1c93a 73 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 se callback rout
1c93b 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f ines are used to
1c93c 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 implement the f
1c93d 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
1c93e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 sqlite3ExprI
1c93f 73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a 20 20 sConstant().**
1c940 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 sqlite3ExprIs
1c941 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 ConstantNotJoin(
1c942 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
1c943 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 ExprIsConstantOr
1c944 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f Function().**.*/
1c945 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72 .static int expr
1c946 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 NodeIsConstant(W
1c947 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 alker *pWalker,
1c948 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 0a 20 Expr *pExpr){..
1c949 20 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72 2d 3e /* If pWalker->
1c94a 75 2e 69 20 69 73 20 33 20 74 68 65 6e 20 61 6e u.i is 3 then an
1c94b 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 65 78 y term of the ex
1c94c 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f pression that co
1c94d 6d 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 mes from. ** th
1c94e 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c e ON or USING cl
1c94f 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 20 auses of a join
1c950 64 69 73 71 75 61 6c 69 66 69 65 73 20 74 68 65 disqualifies the
1c951 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a expression. **
1c952 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f 6e 73 from being cons
1c953 69 64 65 72 65 64 20 63 6f 6e 73 74 61 6e 74 2e idered constant.
1c954 20 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c 6b 65 */. if( pWalke
1c955 72 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45 78 70 r->u.i==3 && Exp
1c956 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
1c957 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f pExpr, EP_FromJo
1c958 69 6e 29 20 29 7b 0a 20 20 20 20 70 57 61 6c 6b in) ){. pWalk
1c959 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 er->u.i = 0;.
1c95a 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1c95b 74 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 t;. }.. switch
1c95c 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 ( pExpr->op ){.
1c95d 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66 /* Consider f
1c95e 75 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 63 unctions to be c
1c95f 6f 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c 20 74 onstant if all t
1c960 68 65 69 72 20 61 72 67 75 6d 65 6e 74 73 20 61 heir arguments a
1c961 72 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 re constant.
1c962 2a 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e ** and pWalker->
1c963 75 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 u.i==2 */. ca
1c964 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a se TK_FUNCTION:.
1c965 20 20 20 20 20 20 69 66 28 20 70 57 61 6c 6b 65 if( pWalke
1c966 72 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 74 75 r->u.i==2 ) retu
1c967 72 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46 rn 0;. /* F
1c968 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 all through */.
1c969 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20 case TK_ID:.
1c96a 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d case TK_COLUM
1c96b 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 44 N:. case TK_D
1c96c 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f OT:. case TK_
1c96d 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 AGG_FUNCTION:.
1c96e 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f case TK_AGG_CO
1c96f 4c 55 4d 4e 3a 0a 23 69 66 6e 64 65 66 20 53 51 LUMN:.#ifndef SQ
1c970 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
1c971 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 RY. case TK_S
1c972 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 ELECT:. case
1c973 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 TK_EXISTS:.
1c974 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 testcase( pExpr
1c975 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 ->op==TK_SELECT
1c976 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1c977 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b e( pExpr->op==TK
1c978 5f 45 58 49 53 54 53 20 29 3b 0a 23 65 6e 64 69 _EXISTS );.#endi
1c979 66 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 f. testcase
1c97a 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
1c97b 49 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ID );. test
1c97c 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
1c97d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 =TK_COLUMN );.
1c97e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
1c97f 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 xpr->op==TK_DOT
1c980 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1c981 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b e( pExpr->op==TK
1c982 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 3b _AGG_FUNCTION );
1c983 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1c984 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 pExpr->op==TK_A
1c985 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 GG_COLUMN );.
1c986 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 pWalker->u.i
1c987 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 = 0;. retur
1c988 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
1c989 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 default:.
1c98a 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 return WRC_Conti
1c98b 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 nue;. }.}.stati
1c98c 63 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f 64 65 c int selectNode
1c98d 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 IsConstant(Walke
1c98e 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 r *pWalker, Sele
1c98f 63 74 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 ct *NotUsed){.
1c990 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1c991 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 70 57 61 (NotUsed);. pWa
1c992 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 lker->u.i = 0;.
1c993 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1c994 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 t;.}.static int
1c995 65 78 70 72 49 73 43 6f 6e 73 74 28 45 78 70 72 exprIsConst(Expr
1c996 20 2a 70 2c 20 69 6e 74 20 69 6e 69 74 46 6c 61 *p, int initFla
1c997 67 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a g){. Walker w;.
1c998 20 20 77 2e 75 2e 69 20 3d 20 69 6e 69 74 46 6c w.u.i = initFl
1c999 61 67 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c ag;. w.xExprCal
1c99a 6c 62 61 63 6b 20 3d 20 65 78 70 72 4e 6f 64 65 lback = exprNode
1c99b 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 77 2e IsConstant;. w.
1c99c 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 xSelectCallback
1c99d 3d 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f = selectNodeIsCo
1c99e 6e 73 74 61 6e 74 3b 0a 20 20 73 71 6c 69 74 65 nstant;. sqlite
1c99f 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 29 3WalkExpr(&w, p)
1c9a0 3b 0a 20 20 72 65 74 75 72 6e 20 77 2e 75 2e 69 ;. return w.u.i
1c9a1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 ;.}../*.** Walk
1c9a2 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 an expression tr
1c9a3 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 ee. Return 1 if
1c9a4 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1c9a5 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 is constant.** a
1c9a6 6e 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c nd 0 if it invol
1c9a7 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 ves variables or
1c9a8 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 2e function calls.
1c9a9 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 .**.** For the p
1c9aa 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 urposes of this
1c9ab 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 function, a doub
1c9ac 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 le-quoted string
1c9ad 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 (ex: "abc").**
1c9ae 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 is considered a
1c9af 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 73 variable but a s
1c9b0 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 ingle-quoted str
1c9b1 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 20 ing (ex: 'abc')
1c9b2 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 is.** a constant
1c9b3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1c9b4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
1c9b5 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 xprIsConstant(Ex
1c9b6 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e pr *p){. return
1c9b7 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 exprIsConst(p,
1c9b8 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 1);.}../*.** Wal
1c9b9 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 k an expression
1c9ba 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 tree. Return 1
1c9bb 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f if the expressio
1c9bc 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a n is constant.**
1c9bd 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 6f 72 that does no or
1c9be 69 67 69 6e 61 74 65 20 66 72 6f 6d 20 74 68 65 iginate from the
1c9bf 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 ON or USING cla
1c9c0 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 2e 0a uses of a join..
1c9c1 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 69 ** Return 0 if i
1c9c2 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 t involves varia
1c9c3 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e bles or function
1c9c4 20 63 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73 20 calls or terms
1c9c5 66 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f 72 from.** an ON or
1c9c6 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a USING clause..*
1c9c7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1c9c8 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
1c9c9 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 IsConstantNotJoi
1c9ca 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 n(Expr *p){. re
1c9cb 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 turn exprIsConst
1c9cc 28 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a (p, 3);.}../*.**
1c9cd 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 Walk an express
1c9ce 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 ion tree. Retur
1c9cf 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 n 1 if the expre
1c9d0 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e ssion is constan
1c9d1 74 0a 2a 2a 20 6f 72 20 61 20 66 75 6e 63 74 69 t.** or a functi
1c9d2 6f 6e 20 63 61 6c 6c 20 77 69 74 68 20 63 6f 6e on call with con
1c9d3 73 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 73 2e stant arguments.
1c9d4 20 20 52 65 74 75 72 6e 20 61 6e 64 20 30 20 69 Return and 0 i
1c9d5 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 61 f there.** are a
1c9d6 6e 79 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a ny variables..**
1c9d7 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 .** For the purp
1c9d8 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66 75 6e oses of this fun
1c9d9 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d ction, a double-
1c9da 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 quoted string (e
1c9db 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73 20 x: "abc").** is
1c9dc 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61 72 considered a var
1c9dd 69 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e 67 iable but a sing
1c9de 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 le-quoted string
1c9df 20 28 65 78 3a 20 27 61 62 63 27 29 20 69 73 0a (ex: 'abc') is.
1c9e0 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a ** a constant..*
1c9e1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1c9e2 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
1c9e3 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 IsConstantOrFunc
1c9e4 74 69 6f 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 tion(Expr *p){.
1c9e5 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f return exprIsCo
1c9e6 6e 73 74 28 70 2c 20 32 29 3b 0a 7d 0a 0a 2f 2a nst(p, 2);.}../*
1c9e7 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 .** If the expre
1c9e8 73 73 69 6f 6e 20 70 20 63 6f 64 65 73 20 61 20 ssion p codes a
1c9e9 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72 constant integer
1c9ea 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 20 65 that is small e
1c9eb 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 69 74 20 nough.** to fit
1c9ec 69 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 in a 32-bit inte
1c9ed 67 65 72 2c 20 72 65 74 75 72 6e 20 31 20 61 6e ger, return 1 an
1c9ee 64 20 70 75 74 20 74 68 65 20 76 61 6c 75 65 20 d put the value
1c9ef 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a of the integer.*
1c9f0 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e 20 20 49 * in *pValue. I
1c9f1 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1c9f2 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 is not an integ
1c9f3 65 72 20 6f 72 20 69 66 20 69 74 20 69 73 20 74 er or if it is t
1c9f4 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 66 69 74 oo big.** to fit
1c9f5 20 69 6e 20 61 20 73 69 67 6e 65 64 20 33 32 2d in a signed 32-
1c9f6 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 bit integer, ret
1c9f7 75 72 6e 20 30 20 61 6e 64 20 6c 65 61 76 65 20 urn 0 and leave
1c9f8 2a 70 56 61 6c 75 65 20 75 6e 63 68 61 6e 67 65 *pValue unchange
1c9f9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1c9fa 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1c9fb 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78 ExprIsInteger(Ex
1c9fc 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 56 61 6c pr *p, int *pVal
1c9fd 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ue){. int rc =
1c9fe 30 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 0;. if( p->flag
1c9ff 73 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 s & EP_IntValue
1ca00 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65 20 3d ){. *pValue =
1ca01 20 70 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 p->iTable;.
1ca02 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 return 1;. }.
1ca03 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b switch( p->op ){
1ca04 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 . case TK_INT
1ca05 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 63 EGER: {. rc
1ca06 20 3d 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 = sqlite3GetInt
1ca07 33 32 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f 6b 32((char*)p->tok
1ca08 65 6e 2e 7a 2c 20 70 56 61 6c 75 65 29 3b 0a 20 en.z, pValue);.
1ca09 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1ca0a 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 }. case TK_UP
1ca0b 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 LUS: {. rc
1ca0c 3d 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 = sqlite3ExprIsI
1ca0d 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c nteger(p->pLeft,
1ca0e 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 pValue);.
1ca0f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1ca10 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a case TK_UMINUS:
1ca11 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76 3b 0a {. int v;.
1ca12 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1ca13 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 3ExprIsInteger(p
1ca14 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20 29 7b 0a ->pLeft, &v) ){.
1ca15 20 20 20 20 20 20 20 20 2a 70 56 61 6c 75 65 20 *pValue
1ca16 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 72 63 = -v;. rc
1ca17 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 1;. }.
1ca18 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1ca19 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 62 72 . default: br
1ca1a 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 eak;. }. if( r
1ca1b 63 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d c ){. p->op =
1ca1c 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 TK_INTEGER;.
1ca1d 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f p->flags |= EP_
1ca1e 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 70 2d IntValue;. p-
1ca1f 3e 69 54 61 62 6c 65 20 3d 20 2a 70 56 61 6c 75 >iTable = *pValu
1ca20 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
1ca21 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
1ca22 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 urn TRUE if the
1ca23 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73 20 given string is
1ca24 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e 20 a row-id column
1ca25 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f name..*/.SQLITE_
1ca26 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1ca27 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 te3IsRowid(const
1ca28 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 char *z){. if(
1ca29 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
1ca2a 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d 30 z, "_ROWID_")==0
1ca2b 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 ) return 1;. i
1ca2c 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
1ca2d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d 30 p(z, "ROWID")==0
1ca2e 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 ) return 1;. i
1ca2f 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
1ca30 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20 29 p(z, "OID")==0 )
1ca31 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 return 1;. ret
1ca32 75 72 6e 20 30 3b 0a 7d 0a 0a 23 69 66 64 65 66 urn 0;.}..#ifdef
1ca33 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 SQLITE_TEST. i
1ca34 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c nt sqlite3_enabl
1ca35 65 5f 69 6e 5f 6f 70 74 20 3d 20 31 3b 0a 23 65 e_in_opt = 1;.#e
1ca36 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
1ca37 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 69 6e 5f lite3_enable_in_
1ca38 6f 70 74 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a opt 1.#endif../*
1ca39 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
1ca3a 69 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 if the IN operat
1ca3b 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 or optimization
1ca3c 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 0a 2a is enabled and.*
1ca3d 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 * the SELECT sta
1ca3e 74 65 6d 65 6e 74 20 70 20 65 78 69 73 74 73 20 tement p exists
1ca3f 61 6e 64 20 69 73 20 6f 66 20 74 68 65 0a 2a 2a and is of the.**
1ca40 20 73 69 6d 70 6c 65 20 66 6f 72 6d 3a 0a 2a 2a simple form:.**
1ca41 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c .** SELECT <
1ca42 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 column> FROM <ta
1ca43 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ble>.**.** If th
1ca44 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 is is the case,
1ca45 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69 62 it may be possib
1ca46 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78 69 le to use an exi
1ca47 73 74 69 6e 67 20 74 61 62 6c 65 0a 2a 2a 20 6f sting table.** o
1ca48 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61 64 20 r index instead
1ca49 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e of generating an
1ca4a 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 epheremal table
1ca4b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
1ca4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 ITE_OMIT_SUBQUER
1ca4d 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 43 Y.static int isC
1ca4e 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 andidateForInOpt
1ca4f 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 (Select *p){. S
1ca50 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 rcList *pSrc;.
1ca51 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
1ca52 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b ;. Table *pTab;
1ca53 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f . if( !sqlite3_
1ca54 65 6e 61 62 6c 65 5f 69 6e 5f 6f 70 74 20 29 20 enable_in_opt )
1ca55 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 49 4e 20 return 0; /* IN
1ca56 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 75 73 optimization mus
1ca57 74 20 62 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a t be enabled */.
1ca58 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
1ca59 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 urn 0;
1ca5a 20 20 20 20 20 20 20 20 20 2f 2a 20 72 69 67 68 /* righ
1ca5b 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 49 t-hand side of I
1ca5c 4e 20 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 N is SELECT */.
1ca5d 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 if( p->pPrior )
1ca5e 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 return 0;
1ca5f 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61 /* Not a
1ca60 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
1ca61 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c */. if( p->sel
1ca62 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 Flags & (SF_Dist
1ca63 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 inct|SF_Aggregat
1ca64 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 e) ){. retu
1ca65 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 rn 0; /* No DIST
1ca66 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 61 6e 64 INCT keyword and
1ca67 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66 75 no aggregate fu
1ca68 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 nctions */. }.
1ca69 20 69 66 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 if( p->pGroupBy
1ca6a 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
1ca6b 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e /* Has n
1ca6c 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 o GROUP BY claus
1ca6d 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4c e */. if( p->pL
1ca6e 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20 30 3b imit ) return 0;
1ca6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ca70 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c Has no LIMIT cl
1ca71 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d ause */. if( p-
1ca72 3e 70 4f 66 66 73 65 74 20 29 20 72 65 74 75 72 >pOffset ) retur
1ca73 6e 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 70 57 n 0;. if( p->pW
1ca74 68 65 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b here ) return 0;
1ca75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ca76 20 48 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c Has no WHERE cl
1ca77 61 75 73 65 20 2a 2f 0a 20 20 70 53 72 63 20 3d ause */. pSrc =
1ca78 20 70 2d 3e 70 53 72 63 3b 0a 20 20 69 66 28 20 p->pSrc;. if(
1ca79 70 53 72 63 3d 3d 30 20 29 20 72 65 74 75 72 6e pSrc==0 ) return
1ca7a 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1ca7b 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74 /* A single t
1ca7c 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d able in the FROM
1ca7d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 clause */. if(
1ca7e 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 pSrc->nSrc!=1 )
1ca7f 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
1ca80 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c pSrc->a[0].pSel
1ca81 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 ect ) return 0;
1ca82 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 /* FROM clau
1ca83 73 65 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71 se is not a subq
1ca84 75 65 72 79 20 2a 2f 0a 20 20 70 54 61 62 20 3d uery */. pTab =
1ca85 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 pSrc->a[0].pTab
1ca86 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 ;. if( pTab==0
1ca87 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 ) return 0;. if
1ca88 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 ( pTab->pSelect
1ca89 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
1ca8a 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 /* FROM cla
1ca8b 75 73 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65 use is not a vie
1ca8c 77 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 w */. if( IsVir
1ca8d 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 tual(pTab) ) ret
1ca8e 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a urn 0; /*
1ca8f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74 FROM clause not
1ca90 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
1ca91 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 */. pEList = p
1ca92 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 ->pEList;. if(
1ca93 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 pEList->nExpr!=1
1ca94 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
1ca95 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e /* One column
1ca96 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1ca97 65 74 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69 et */. if( pELi
1ca98 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e st->a[0].pExpr->
1ca99 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 op!=TK_COLUMN )
1ca9a 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73 return 0; /* Res
1ca9b 75 6c 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 ult is a column
1ca9c 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d */. return 1;.}
1ca9d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1ca9e 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 E_OMIT_SUBQUERY
1ca9f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 */../*.** This f
1caa0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
1caa1 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 by the implement
1caa2 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 ation of the IN
1caa3 28 2e 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a (...) operator..
1caa4 2a 2a 20 49 74 27 73 20 6a 6f 62 20 69 73 20 74 ** It's job is t
1caa5 6f 20 66 69 6e 64 20 6f 72 20 63 72 65 61 74 65 o find or create
1caa6 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 a b-tree struct
1caa7 75 72 65 20 74 68 61 74 20 6d 61 79 20 62 65 20 ure that may be
1caa8 75 73 65 64 0a 2a 2a 20 65 69 74 68 65 72 20 74 used.** either t
1caa9 6f 20 74 65 73 74 20 66 6f 72 20 6d 65 6d 62 65 o test for membe
1caaa 72 73 68 69 70 20 6f 66 20 74 68 65 20 28 2e 2e rship of the (..
1caab 2e 29 20 73 65 74 20 6f 72 20 74 6f 20 69 74 65 .) set or to ite
1caac 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 rate through.**
1caad 69 74 73 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 its members, ski
1caae 70 70 69 6e 67 20 64 75 70 6c 69 63 61 74 65 73 pping duplicates
1caaf 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 ..**.** The curs
1cab0 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 or opened on the
1cab1 20 73 74 72 75 63 74 75 72 65 20 28 64 61 74 61 structure (data
1cab2 62 61 73 65 20 74 61 62 6c 65 2c 20 64 61 74 61 base table, data
1cab3 62 61 73 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6f base index .** o
1cab4 72 20 65 70 68 65 72 6d 61 6c 20 74 61 62 6c 65 r ephermal table
1cab5 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 ) is stored in p
1cab6 58 2d 3e 69 54 61 62 6c 65 20 62 65 66 6f 72 65 X->iTable before
1cab7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
1cab8 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 eturns..** The r
1cab9 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e eturned value in
1caba 64 69 63 61 74 65 73 20 74 68 65 20 73 74 72 75 dicates the stru
1cabb 63 74 75 72 65 20 74 79 70 65 2c 20 61 73 20 66 cture type, as f
1cabc 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
1cabd 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 2d IN_INDEX_ROWID -
1cabe 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 The cursor was
1cabf 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 61 opened on a data
1cac0 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 base table..**
1cac1 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 IN_INDEX_INDEX
1cac2 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 - The cursor was
1cac3 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 opened on a dat
1cac4 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 abase index..**
1cac5 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 2d IN_INDEX_EPH -
1cac6 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 The cursor wa
1cac7 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73 70 s opened on a sp
1cac8 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64 20 ecially created
1cac9 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 and.**
1caca 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c 61 popula
1cacb 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74 61 ted epheremal ta
1cacc 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 ble..**.** An ex
1cacd 69 73 74 69 6e 67 20 73 74 72 75 63 74 75 72 65 isting structure
1cace 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 may only be use
1cacf 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 d if the SELECT
1cad0 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 is of the simple
1cad1 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 .** form:.**.**
1cad2 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75 SELECT <colu
1cad3 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e mn> FROM <table>
1cad4 0a 2a 2a 0a 2a 2a 20 49 66 20 70 72 4e 6f 74 46 .**.** If prNotF
1cad5 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 ound parameter i
1cad6 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 73 74 s 0, then the st
1cad7 72 75 63 74 75 72 65 20 77 69 6c 6c 20 62 65 20 ructure will be
1cad8 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 0a used to iterate.
1cad9 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 73 ** through the s
1cada 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 et members, skip
1cadb 70 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63 61 ping any duplica
1cadc 74 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 tes. In this cas
1cadd 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d 61 e an.** epherema
1cade 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 l table must be
1cadf 75 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65 20 used unless the
1cae0 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d 6e selected <column
1cae1 3e 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a > is guaranteed.
1cae2 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65 20 ** to be unique
1cae3 2d 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 - either because
1cae4 20 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47 45 it is an INTEGE
1cae5 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 R PRIMARY KEY or
1cae6 20 69 74 0a 2a 2a 20 69 73 20 75 6e 69 71 75 65 it.** is unique
1cae7 20 62 79 20 76 69 72 74 75 65 20 6f 66 20 61 20 by virtue of a
1cae8 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72 20 69 6d constraint or im
1cae9 70 6c 69 63 69 74 20 69 6e 64 65 78 2e 0a 2a 2a plicit index..**
1caea 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f 74 .** If the prNot
1caeb 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20 Found parameter
1caec 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74 is not 0, then t
1caed 68 65 20 73 74 72 75 63 74 75 72 65 20 77 69 6c he structure wil
1caee 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f l be used .** fo
1caef 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d 62 65 r fast set membe
1caf0 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 6e 20 rship tests. In
1caf1 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 70 68 this case an eph
1caf2 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 eremal table mus
1caf3 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 6e t .** be used un
1caf4 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 less <column> is
1caf5 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d an INTEGER PRIM
1caf6 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 6e ARY KEY or an in
1caf7 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 66 dex can .** be f
1caf8 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 6d ound with <colum
1caf9 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 2d 6d n> as its left-m
1cafa 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a ost column..**.*
1cafb 2a 20 57 68 65 6e 20 74 68 65 20 73 74 72 75 63 * When the struc
1cafc 74 75 72 65 20 69 73 20 62 65 69 6e 67 20 75 73 ture is being us
1cafd 65 64 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65 ed for set membe
1cafe 72 73 68 69 70 20 74 65 73 74 73 2c 20 74 68 65 rship tests, the
1caff 20 75 73 65 72 0a 2a 2a 20 6e 65 65 64 73 20 74 user.** needs t
1cb00 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f o know whether o
1cb01 72 20 6e 6f 74 20 74 68 65 20 73 74 72 75 63 74 r not the struct
1cb02 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 ure contains an
1cb03 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c SQL NULL .** val
1cb04 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 ue in order to c
1cb05 6f 72 72 65 63 74 6c 79 20 65 76 61 6c 75 61 74 orrectly evaluat
1cb06 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 69 e expressions li
1cb07 6b 65 20 22 58 20 49 4e 20 28 59 2c 20 5a 29 22 ke "X IN (Y, Z)"
1cb08 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 ..** If there is
1cb09 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 a chance that t
1cb0a 68 65 20 73 74 72 75 63 74 75 72 65 20 6d 61 79 he structure may
1cb0b 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20 contain a NULL
1cb0c 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72 75 6e 74 value at.** runt
1cb0d 69 6d 65 2c 20 74 68 65 6e 20 61 20 72 65 67 69 ime, then a regi
1cb0e 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 ster is allocate
1cb0f 64 20 61 6e 64 20 74 68 65 20 72 65 67 69 73 74 d and the regist
1cb10 65 72 20 6e 75 6d 62 65 72 20 77 72 69 74 74 65 er number writte
1cb11 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f 74 46 6f n.** to *prNotFo
1cb12 75 6e 64 2e 20 49 66 20 74 68 65 72 65 20 69 73 und. If there is
1cb13 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20 no chance that
1cb14 74 68 65 20 73 74 72 75 63 74 75 72 65 20 63 6f the structure co
1cb15 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c ntains a.** NULL
1cb16 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 72 value, then *pr
1cb17 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66 74 NotFound is left
1cb18 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a unchanged..**.*
1cb19 2a 20 49 66 20 61 20 72 65 67 69 73 74 65 72 20 * If a register
1cb1a 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 is allocated and
1cb1b 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73 74 its location st
1cb1c 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46 6f ored in *prNotFo
1cb1d 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74 73 und, then.** its
1cb1e 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 69 initial value i
1cb1f 73 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73 s NULL. If the s
1cb20 74 72 75 63 74 75 72 65 20 64 6f 65 73 20 6e 6f tructure does no
1cb21 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e t remain constan
1cb22 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72 t.** for the dur
1cb23 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65 ation of the que
1cb24 72 79 20 28 69 2e 65 2e 20 74 68 65 20 73 65 74 ry (i.e. the set
1cb25 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64 is a correlated
1cb26 20 73 75 62 2d 73 65 6c 65 63 74 29 2c 20 0a 2a sub-select), .*
1cb27 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 * the value of t
1cb28 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 67 he allocated reg
1cb29 69 73 74 65 72 20 69 73 20 72 65 73 65 74 20 74 ister is reset t
1cb2a 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69 6d 65 o NULL each time
1cb2b 20 74 68 65 20 0a 2a 2a 20 73 74 72 75 63 74 75 the .** structu
1cb2c 72 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74 65 re is repopulate
1cb2d 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 74 d. This allows t
1cb2e 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 he caller to use
1cb2f 20 76 64 62 65 20 63 6f 64 65 20 0a 2a 2a 20 65 vdbe code .** e
1cb30 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65 quivalent to the
1cb31 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
1cb32 2a 20 20 20 69 66 28 20 72 65 67 69 73 74 65 72 * if( register
1cb33 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20 ==NULL ){.**
1cb34 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 has_null = <tes
1cb35 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74 t if data struct
1cb36 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c ure contains nul
1cb37 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 74 l>.** regist
1cb38 65 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a er = 1.** }.**
1cb39 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 .** in order to
1cb3a 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68 avoid running th
1cb3b 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 e <test if data
1cb3c 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
1cb3d 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74 ns null>.** test
1cb3e 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e more often than
1cb3f 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a is necessary..*
1cb40 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1cb41 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 _OMIT_SUBQUERY.S
1cb42 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1cb43 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 t sqlite3FindInI
1cb44 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 ndex(Parse *pPar
1cb45 73 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 6e se, Expr *pX, in
1cb46 74 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a t *prNotFound){.
1cb47 20 20 53 65 6c 65 63 74 20 2a 70 3b 0a 20 20 69 Select *p;. i
1cb48 6e 74 20 65 54 79 70 65 20 3d 20 30 3b 0a 20 20 nt eType = 0;.
1cb49 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 73 int iTab = pPars
1cb4a 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 6e 74 e->nTab++;. int
1cb4b 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d 20 mustBeUnique =
1cb4c 21 70 72 4e 6f 74 46 6f 75 6e 64 3b 0a 0a 20 20 !prNotFound;..
1cb4d 2f 2a 20 54 68 65 20 66 6f 6c 6c 77 69 6e 67 20 /* The follwing
1cb4e 69 66 28 2e 2e 2e 29 20 65 78 70 72 65 73 73 69 if(...) expressi
1cb4f 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74 68 on is true if th
1cb50 65 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74 e SELECT is of t
1cb51 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 he . ** simple
1cb52 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 form:. **. **
1cb53 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75 SELECT <colu
1cb54 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e mn> FROM <table>
1cb55 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 . **. ** If th
1cb56 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 is is the case,
1cb57 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69 62 it may be possib
1cb58 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78 69 le to use an exi
1cb59 73 74 69 6e 67 20 74 61 62 6c 65 0a 20 20 2a 2a sting table. **
1cb5a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61 or index instea
1cb5b 64 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20 d of generating
1cb5c 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 an epheremal tab
1cb5d 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 70 le.. */. p = p
1cb5e 58 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 69 66 X->pSelect;. if
1cb5f 28 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 ( isCandidateFor
1cb60 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20 InOpt(p) ){.
1cb61 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
1cb62 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 49 6e arse->db;. In
1cb63 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 45 dex *pIdx;. E
1cb64 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 2d 3e xpr *pExpr = p->
1cb65 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 pEList->a[0].pEx
1cb66 70 72 3b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c pr;. int iCol
1cb67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d = pExpr->iColum
1cb68 6e 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d n;. Vdbe *v =
1cb69 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
1cb6a 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 2f 2a pParse);.. /*
1cb6b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1cb6c 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 s only called fr
1cb6d 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73 2e 20 49 om two places. I
1cb6e 6e 20 62 6f 74 68 20 63 61 73 65 73 20 74 68 65 n both cases the
1cb6f 20 76 64 62 65 0a 20 20 20 20 2a 2a 20 68 61 73 vdbe. ** has
1cb70 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c already been al
1cb71 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61 73 73 75 located. So assu
1cb72 6d 65 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 me sqlite3GetVdb
1cb73 65 28 29 20 69 73 20 61 6c 77 61 79 73 0a 20 20 e() is always.
1cb74 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 ** successful
1cb75 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 here.. */.
1cb76 20 61 73 73 65 72 74 28 76 29 3b 0a 20 20 20 20 assert(v);.
1cb77 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 if( iCol<0 ){.
1cb78 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b int iMem = +
1cb79 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
1cb7a 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a int iAddr;.
1cb7b 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 Table *pTa
1cb7c 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 b = p->pSrc->a[0
1cb7d 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 69 6e ].pTab;. in
1cb7e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 t iDb = sqlite3S
1cb7f 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
1cb80 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b pTab->pSchema);
1cb81 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1cb82 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 beUsesBtree(v, i
1cb83 44 62 29 3b 0a 0a 20 20 20 20 20 20 69 41 64 64 Db);.. iAdd
1cb84 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 r = sqlite3VdbeA
1cb85 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 ddOp1(v, OP_If,
1cb86 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c iMem);. sql
1cb87 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1cb88 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c , OP_Integer, 1,
1cb89 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 20 20 73 iMem);.. s
1cb8a 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 qlite3OpenTable(
1cb8b 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20 69 44 pParse, iTab, iD
1cb8c 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e b, pTab, OP_Open
1cb8d 52 65 61 64 29 3b 0a 20 20 20 20 20 20 65 54 79 Read);. eTy
1cb8e 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f pe = IN_INDEX_RO
1cb8f 57 49 44 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 WID;.. sqli
1cb90 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
1cb91 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20 7d v, iAddr);. }
1cb92 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 else{. /* T
1cb93 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 he collation seq
1cb94 75 65 6e 63 65 20 75 73 65 64 20 62 79 20 74 68 uence used by th
1cb95 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 66 e comparison. If
1cb96 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 6f 20 an index is to
1cb97 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65 . ** be use
1cb98 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 d in place of a
1cb99 74 65 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20 6d temp-table, it m
1cb9a 75 73 74 20 62 65 20 6f 72 64 65 72 65 64 20 61 ust be ordered a
1cb9b 63 63 6f 72 64 69 6e 67 0a 20 20 20 20 20 20 2a ccording. *
1cb9c 2a 20 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61 74 * to this collat
1cb9d 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 20 20 ion sequence..
1cb9e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f 6c */. Col
1cb9f 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73 71 6c lSeq *pReq = sql
1cba0 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 ite3BinaryCompar
1cba1 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c eCollSeq(pParse,
1cba2 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 pX->pLeft, pExp
1cba3 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 r);.. /* Ch
1cba4 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 66 66 eck that the aff
1cba5 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c 6c 20 inity that will
1cba6 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 66 6f be used to perfo
1cba7 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a rm the . **
1cba8 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 comparison is t
1cba9 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 61 he same as the a
1cbaa 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63 ffinity of the c
1cbab 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20 20 olumn. If.
1cbac 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20 69 74 ** it is not, it
1cbad 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1cbae 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e 64 65 to use any inde
1cbaf 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 x.. */.
1cbb0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 Table *pTab =
1cbb1 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 p->pSrc->a[0].pT
1cbb2 61 62 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 ab;. char a
1cbb3 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 ff = comparisonA
1cbb4 66 66 69 6e 69 74 79 28 70 58 29 3b 0a 20 20 20 ffinity(pX);.
1cbb5 20 20 20 69 6e 74 20 61 66 66 69 6e 69 74 79 5f int affinity_
1cbb6 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f 6c ok = (pTab->aCol
1cbb7 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79 3d [iCol].affinity=
1cbb8 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49 54 =aff||aff==SQLIT
1cbb9 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 20 E_AFF_NONE);..
1cbba 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 for(pIdx=pTa
1cbbb 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 20 b->pIndex; pIdx
1cbbc 26 26 20 65 54 79 70 65 3d 3d 30 20 26 26 20 61 && eType==0 && a
1cbbd 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64 78 ffinity_ok; pIdx
1cbbe 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 =pIdx->pNext){.
1cbbf 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64 78 if( (pIdx
1cbc0 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 ->aiColumn[0]==i
1cbc1 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 26 26 Col). &&
1cbc2 20 28 70 52 65 71 3d 3d 73 71 6c 69 74 65 33 46 (pReq==sqlite3F
1cbc3 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 indCollSeq(db, E
1cbc4 4e 43 28 64 62 29 2c 20 70 49 64 78 2d 3e 61 7a NC(db), pIdx->az
1cbc5 43 6f 6c 6c 5b 30 5d 2c 20 2d 31 2c 20 30 29 29 Coll[0], -1, 0))
1cbc6 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 21 6d . && (!m
1cbc7 75 73 74 42 65 55 6e 69 71 75 65 20 7c 7c 20 28 ustBeUnique || (
1cbc8 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 pIdx->nColumn==1
1cbc9 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f && pIdx->onErro
1cbca 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20 20 r!=OE_None)).
1cbcb 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
1cbcc 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 int iDb;.
1cbcd 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 int iMem =
1cbce 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
1cbcf 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 41 int iA
1cbd0 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20 63 ddr;. c
1cbd1 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 20 har *pKey;. .
1cbd2 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d 20 28 pKey = (
1cbd3 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 49 6e char *)sqlite3In
1cbd4 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 dexKeyinfo(pPars
1cbd5 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 e, pIdx);.
1cbd6 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 iDb = sqlite
1cbd7 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 3SchemaToIndex(d
1cbd8 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 b, pIdx->pSchema
1cbd9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
1cbda 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 ite3VdbeUsesBtre
1cbdb 65 28 76 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 e(v, iDb);..
1cbdc 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 iAddr = sq
1cbdd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
1cbde 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b v, OP_If, iMem);
1cbdf 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1cbe0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1cbe1 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 OP_Integer, 1, i
1cbe2 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 Mem);. .
1cbe3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1cbe4 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e 75 dOp2(v, OP_SetNu
1cbe5 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70 49 64 mColumns, 0, pId
1cbe6 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 x->nColumn);.
1cbe7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1cbe8 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f beAddOp4(v, OP_O
1cbe9 70 65 6e 52 65 61 64 2c 20 69 54 61 62 2c 20 70 penRead, iTab, p
1cbea 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a Idx->tnum, iDb,.
1cbeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cbec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1cbed 4b 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 Key,P4_KEYINFO_H
1cbee 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 20 ANDOFF);.
1cbef 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 VdbeComment((
1cbf0 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a v, "%s", pIdx->z
1cbf1 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 20 20 Name));.
1cbf2 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 eType = IN_IND
1cbf3 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20 20 20 EX_INDEX;..
1cbf4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1cbf5 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 64 JumpHere(v, iAdd
1cbf6 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 r);. if
1cbf7 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26 26 20 ( prNotFound &&
1cbf8 21 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c !pTab->aCol[iCol
1cbf9 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 ].notNull ){.
1cbfa 20 20 20 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 *prNotF
1cbfb 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d ound = ++pParse-
1cbfc 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 >nMem;.
1cbfd 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1cbfe 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
1cbff 20 20 69 66 28 20 65 54 79 70 65 3d 3d 30 20 29 if( eType==0 )
1cc00 7b 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61 {. int rMayHa
1cc01 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 veNull = 0;.
1cc02 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 eType = IN_INDEX
1cc03 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 72 _EPH;. if( pr
1cc04 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 NotFound ){.
1cc05 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 *prNotFound =
1cc06 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 2b rMayHaveNull = +
1cc07 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
1cc08 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 2d }else if( pX-
1cc09 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c >pLeft->iColumn<
1cc0a 30 20 26 26 20 70 58 2d 3e 70 53 65 6c 65 63 74 0 && pX->pSelect
1cc0b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 54 79 ==0 ){. eTy
1cc0c 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f pe = IN_INDEX_RO
1cc0d 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 WID;. }. s
1cc0e 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c qlite3CodeSubsel
1cc0f 65 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c 20 ect(pParse, pX,
1cc10 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65 54 rMayHaveNull, eT
1cc11 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f ype==IN_INDEX_RO
1cc12 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 WID);. }else{.
1cc13 20 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d 20 pX->iTable =
1cc14 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 iTab;. }. retu
1cc15 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e 64 rn eType;.}.#end
1cc16 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 if../*.** Genera
1cc17 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61 6c te code for scal
1cc18 61 72 20 73 75 62 71 75 65 72 69 65 73 20 75 73 ar subqueries us
1cc19 65 64 20 61 73 20 61 6e 20 65 78 70 72 65 73 73 ed as an express
1cc1a 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f 70 ion.** and IN op
1cc1b 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70 6c erators. Exampl
1cc1c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 53 es:.**.** (S
1cc1d 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 ELECT a FROM b)
1cc1e 20 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62 71 -- subq
1cc1f 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49 53 uery.** EXIS
1cc20 54 53 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f TS (SELECT a FRO
1cc21 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54 53 M b) -- EXISTS
1cc22 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 subquery.**
1cc23 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20 20 x IN (4,5,11)
1cc24 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 49 -- I
1cc25 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 N operator with
1cc26 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68 61 list on right-ha
1cc27 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20 78 nd side.** x
1cc28 20 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46 52 IN (SELECT a FR
1cc29 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e 20 OM b) -- IN
1cc2a 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 73 75 operator with su
1cc2b 62 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72 69 bquery on the ri
1cc2c 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 45 ght.**.** The pE
1cc2d 78 70 72 20 70 61 72 61 6d 65 74 65 72 20 64 65 xpr parameter de
1cc2e 73 63 72 69 62 65 73 20 74 68 65 20 65 78 70 72 scribes the expr
1cc2f 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74 ession that cont
1cc30 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20 6f ains the IN.** o
1cc31 70 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71 75 perator or subqu
1cc32 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 ery..**.** If pa
1cc33 72 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64 20 rameter isRowid
1cc34 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
1cc35 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 n expression pEx
1cc36 70 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 pr is guaranteed
1cc37 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68 65 .** to be of the
1cc38 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20 49 form "<rowid> I
1cc39 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 68 N (?, ?, ?)", wh
1cc3a 65 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20 61 ere <rowid> is a
1cc3b 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 6f reference.** to
1cc3c 20 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b 65 some integer ke
1cc3d 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61 y column of a ta
1cc3e 62 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 ble B-Tree. In t
1cc3f 68 69 73 20 63 61 73 65 2c 20 75 73 65 20 61 6e his case, use an
1cc40 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 .** intkey B-Tre
1cc41 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 e to store the s
1cc42 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61 et of IN(...) va
1cc43 6c 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 lues instead of
1cc44 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73 6c the usual.** (sl
1cc45 6f 77 65 72 29 20 76 61 72 69 61 62 6c 65 20 6c ower) variable l
1cc46 65 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72 65 ength keys B-Tre
1cc47 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 e..*/.#ifndef SQ
1cc48 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
1cc49 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 RY.SQLITE_PRIVAT
1cc4a 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
1cc4b 64 65 53 75 62 73 65 6c 65 63 74 28 0a 20 20 50 deSubselect(. P
1cc4c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 arse *pParse, .
1cc4d 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 0a 20 Expr *pExpr, .
1cc4e 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c int rMayHaveNul
1cc4f 6c 2c 0a 20 20 69 6e 74 20 69 73 52 6f 77 69 64 l,. int isRowid
1cc50 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 64 .){. int testAd
1cc51 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 dr = 0;
1cc52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1cc53 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61 One-time test a
1cc54 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64 62 65 ddress */. Vdbe
1cc55 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 *v = sqlite3Get
1cc56 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 Vdbe(pParse);.
1cc57 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 if( v==0 ) retur
1cc58 6e 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 n;... /* This c
1cc59 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e 20 ode must be run
1cc5a 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79 20 in its entirety
1cc5b 65 76 65 72 79 20 74 69 6d 65 20 69 74 20 69 73 every time it is
1cc5c 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a encountered. *
1cc5d 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 * if any of the
1cc5e 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 following is tru
1cc5f 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 e:. **. **
1cc60 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e * The right-han
1cc61 64 20 73 69 64 65 20 69 73 20 61 20 63 6f 72 72 d side is a corr
1cc62 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 0a elated subquery.
1cc63 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 ** * The r
1cc64 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 ight-hand side i
1cc65 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 s an expression
1cc66 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 list containing
1cc67 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20 variables. **
1cc68 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69 * We are insi
1cc69 64 65 20 61 20 74 72 69 67 67 65 72 0a 20 20 2a de a trigger. *
1cc6a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 *. ** If all of
1cc6b 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 66 the above are f
1cc6c 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61 alse, then we ca
1cc6d 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 n run this code
1cc6e 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 just once. ** s
1cc6f 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 2c ave the results,
1cc70 20 61 6e 64 20 72 65 75 73 65 20 74 68 65 20 73 and reuse the s
1cc71 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 75 ame result on su
1cc72 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 bsequent invocat
1cc73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ions.. */. if(
1cc74 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 !ExprHasAnyProp
1cc75 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 erty(pExpr, EP_V
1cc76 61 72 53 65 6c 65 63 74 29 20 26 26 20 21 70 50 arSelect) && !pP
1cc77 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 arse->trigStack
1cc78 29 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d ){. int mem =
1cc79 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
1cc7a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1cc7b 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c AddOp1(v, OP_If,
1cc7c 20 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 41 mem);. testA
1cc7d 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
1cc7e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
1cc7f 74 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b 0a teger, 1, mem);.
1cc80 20 20 20 20 61 73 73 65 72 74 28 20 74 65 73 74 assert( test
1cc81 41 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 65 Addr>0 || pParse
1cc82 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1cc83 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 ed );. }.. swi
1cc84 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 tch( pExpr->op )
1cc85 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e {. case TK_IN
1cc86 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 61 : {. char a
1cc87 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 4b ffinity;. K
1cc88 65 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 0a eyInfo keyInfo;.
1cc89 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20 int addr;
1cc8a 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 /* Addres
1cc8b 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 s of OP_OpenEphe
1cc8c 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f meral instructio
1cc8d 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 n */. Expr
1cc8e 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e *pLeft = pExpr->
1cc8f 70 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 66 pLeft;.. if
1cc90 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 ( rMayHaveNull )
1cc91 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1cc92 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1cc93 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 48 P_Null, 0, rMayH
1cc94 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 aveNull);.
1cc95 7d 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74 }.. affinit
1cc96 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 y = sqlite3ExprA
1cc97 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b 0a ffinity(pLeft);.
1cc98 0a 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68 65 . /* Whethe
1cc99 72 20 74 68 69 73 20 69 73 20 61 6e 20 27 78 20 r this is an 'x
1cc9a 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 6f IN(SELECT...)' o
1cc9b 72 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 72 r an 'x IN(<expr
1cc9c 6c 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a 2a list>)'. **
1cc9d 20 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20 69 expression it i
1cc9e 73 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73 61 s handled the sa
1cc9f 6d 65 20 77 61 79 2e 20 41 20 76 69 72 74 75 61 me way. A virtua
1cca0 6c 20 74 61 62 6c 65 20 69 73 20 0a 20 20 20 20 l table is .
1cca1 20 20 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68 ** filled with
1cca2 20 73 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69 6e single-field in
1cca3 64 65 78 20 6b 65 79 73 20 72 65 70 72 65 73 65 dex keys represe
1cca4 6e 74 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 nting the result
1cca5 73 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 s. ** from
1cca6 74 68 65 20 53 45 4c 45 43 54 20 6f 72 20 74 68 the SELECT or th
1cca7 65 20 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20 20 e <exprlist>..
1cca8 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
1cca9 49 66 20 74 68 65 20 27 78 27 20 65 78 70 72 65 If the 'x' expre
1ccaa 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d ssion is a colum
1ccab 6e 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 20 n value, or the
1ccac 53 45 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20 20 SELECT....
1ccad 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 ** statement ret
1ccae 75 72 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 urns a column va
1ccaf 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 61 66 lue, then the af
1ccb0 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 0a 20 finity of that.
1ccb1 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 ** column i
1ccb2 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 s used to build
1ccb3 74 68 65 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 the index keys.
1ccb4 49 66 20 62 6f 74 68 20 27 78 27 20 61 6e 64 20 If both 'x' and
1ccb5 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 53 45 4c the. ** SEL
1ccb6 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 ECT... statement
1ccb7 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 are columns, th
1ccb8 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e en numeric affin
1ccb9 69 74 79 20 69 73 20 75 73 65 64 0a 20 20 20 20 ity is used.
1ccba 20 20 2a 2a 20 69 66 20 65 69 74 68 65 72 20 63 ** if either c
1ccbb 6f 6c 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52 49 olumn has NUMERI
1ccbc 43 20 6f 72 20 49 4e 54 45 47 45 52 20 61 66 66 C or INTEGER aff
1ccbd 69 6e 69 74 79 2e 20 49 66 20 6e 65 69 74 68 65 inity. If neithe
1ccbe 72 0a 20 20 20 20 20 20 2a 2a 20 27 78 27 20 6e r. ** 'x' n
1ccbf 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e or the SELECT...
1ccc0 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 statement are c
1ccc1 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d olumns, then num
1ccc2 65 72 69 63 20 61 66 66 69 6e 69 74 79 0a 20 20 eric affinity.
1ccc3 20 20 20 20 2a 2a 20 69 73 20 75 73 65 64 2e 0a ** is used..
1ccc4 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 */. p
1ccc5 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 Expr->iTable = p
1ccc6 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 Parse->nTab++;.
1ccc7 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 addr = sqli
1ccc8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1ccc9 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 OP_OpenEphemera
1ccca 6c 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 l, pExpr->iTable
1cccb 2c 20 21 69 73 52 6f 77 69 64 29 3b 0a 20 20 20 , !isRowid);.
1cccc 20 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e memset(&keyIn
1cccd 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 fo, 0, sizeof(ke
1ccce 79 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b yInfo));. k
1cccf 65 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20 eyInfo.nField =
1ccd0 31 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 45 1;.. if( pE
1ccd1 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a xpr->pSelect ){.
1ccd2 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 /* Case
1ccd3 31 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 1: expr IN (
1ccd4 53 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 SELECT ...).
1ccd5 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
1ccd6 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
1ccd7 74 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 73 to write the res
1ccd8 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 ults of the sele
1ccd9 63 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 ct into the temp
1ccda 6f 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a orary. **
1ccdb 20 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 table allocated
1ccdc 20 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 and opened abov
1ccdd 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
1ccde 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 SelectDest
1ccdf 20 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 dest;. E
1cce0 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b xprList *pEList;
1cce1 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 .. assert
1cce2 28 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 ( !isRowid );.
1cce3 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c sqlite3Sel
1cce4 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 ectDestInit(&des
1cce5 74 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 t, SRT_Set, pExp
1cce6 72 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 r->iTable);.
1cce7 20 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 dest.affinit
1cce8 79 20 3d 20 28 69 6e 74 29 61 66 66 69 6e 69 74 y = (int)affinit
1cce9 79 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 y;. asser
1ccea 74 28 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c t( (pExpr->iTabl
1cceb 65 26 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70 e&0x0000FFFF)==p
1ccec 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a Expr->iTable );.
1cced 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
1ccee 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 te3Select(pParse
1ccef 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 , pExpr->pSelect
1ccf0 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20 , &dest) ){.
1ccf1 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
1ccf2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1ccf3 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e pEList = pExpr->
1ccf4 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b pSelect->pEList;
1ccf5 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 4c . if( pEL
1ccf6 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e ist && pEList->n
1ccf7 45 78 70 72 3e 30 20 29 7b 20 0a 20 20 20 20 20 Expr>0 ){ .
1ccf8 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f keyInfo.aCo
1ccf9 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 ll[0] = sqlite3B
1ccfa 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c inaryCompareColl
1ccfb 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 Seq(pParse, pExp
1ccfc 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 r->pLeft,.
1ccfd 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e pEList->
1ccfe 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 a[0].pExpr);.
1ccff 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1cd00 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 70 4c se if( pExpr->pL
1cd01 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ist ){. /
1cd02 2a 20 43 61 73 65 20 32 3a 20 20 20 20 20 65 78 * Case 2: ex
1cd03 70 72 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29 pr IN (exprlist)
1cd04 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
1cd05 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 ** For each
1cd06 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c expression, buil
1cd07 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66 d an index key f
1cd08 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69 rom the evaluati
1cd09 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a on and. *
1cd0a 2a 20 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68 * store it in th
1cd0b 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c e temporary tabl
1cd0c 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 e. If <expr> is
1cd0d 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75 a column, then u
1cd0e 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 se. ** th
1cd0f 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e at columns affin
1cd10 69 74 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e ity when buildin
1cd11 67 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 g index keys. If
1cd12 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20 <expr> is not.
1cd13 20 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75 ** a colu
1cd14 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20 mn, use numeric
1cd15 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20 affinity..
1cd16 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 */. int
1cd17 20 69 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 i;. Expr
1cd18 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 List *pList = pE
1cd19 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 xpr->pList;.
1cd1a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
1cd1b 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
1cd1c 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c . int r1,
1cd1d 20 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20 r2, r3;..
1cd1e 20 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20 if( !affinity
1cd1f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 ){. aff
1cd20 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 inity = SQLITE_A
1cd21 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 FF_NONE;.
1cd22 20 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 49 6e }. keyIn
1cd23 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 fo.aColl[0] = sq
1cd24 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 lite3ExprCollSeq
1cd25 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
1cd26 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 pLeft);..
1cd27 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 /* Loop through
1cd28 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e each expression
1cd29 20 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20 in <exprlist>.
1cd2a 2a 2f 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 */. r1 =
1cd2b 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 sqlite3GetTempRe
1cd2c 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 g(pParse);.
1cd2d 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 r2 = sqlite3G
1cd2e 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 etTempReg(pParse
1cd2f 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1cd30 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1cd31 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b OP_Null, 0, r2);
1cd32 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 70 . for(i=p
1cd33 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 List->nExpr, pIt
1cd34 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 em=pList->a; i>0
1cd35 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i--, pItem++){
1cd36 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 . Expr
1cd37 2a 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45 *pE2 = pItem->pE
1cd38 78 70 72 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 xpr;..
1cd39 2f 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 /* If the expres
1cd3a 73 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 sion is not cons
1cd3b 74 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c tant then we wil
1cd3c 6c 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 l need to.
1cd3d 20 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 ** disable t
1cd3e 68 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73 he test that was
1cd3f 20 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 generated above
1cd40 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 that makes sure
1cd41 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 . ** th
1cd42 69 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 is code only exe
1cd43 63 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 cutes once. Bec
1cd44 61 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 ause for a non-c
1cd45 6f 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 onstant.
1cd46 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 ** expression
1cd47 77 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e we need to rerun
1cd48 20 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 this code each
1cd49 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 time..
1cd4a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 */. if(
1cd4b 20 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71 testAddr && !sq
1cd4c 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
1cd4d 61 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 ant(pE2) ){.
1cd4e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1cd4f 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 dbeChangeToNoop(
1cd50 76 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 v, testAddr-1, 2
1cd51 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 );. t
1cd52 65 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 estAddr = 0;.
1cd53 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
1cd54 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 /* Evaluate
1cd55 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 the expression a
1cd56 6e 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 nd insert it int
1cd57 6f 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 o the temp table
1cd58 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 50 */. pP
1cd59 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1cd5a 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 20 Cache++;.
1cd5b 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 45 r3 = sqlite3E
1cd5c 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 xprCodeTarget(pP
1cd5d 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a arse, pE2, r1);.
1cd5e 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1cd5f 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c ( pParse->disabl
1cd60 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 eColCache>0 );.
1cd61 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d pParse-
1cd62 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 >disableColCache
1cd63 2d 2d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 69 --;.. i
1cd64 66 28 20 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 f( isRowid ){.
1cd65 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1cd66 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1cd67 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 33 2c P_MustBeInt, r3,
1cd68 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
1cd69 65 6e 74 41 64 64 72 28 76 29 2b 32 29 3b 0a 20 entAddr(v)+2);.
1cd6a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1cd6b 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
1cd6c 4f 50 5f 49 6e 73 65 72 74 2c 20 70 45 78 70 72 OP_Insert, pExpr
1cd6d 2d 3e 69 54 61 62 6c 65 2c 20 72 32 2c 20 72 33 ->iTable, r2, r3
1cd6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
1cd6f 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
1cd70 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1cd71 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 4(v, OP_MakeReco
1cd72 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c 20 26 rd, r3, 1, r2, &
1cd73 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 affinity, 1);.
1cd74 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1cd75 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 3ExprCacheAffini
1cd76 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c tyChange(pParse,
1cd77 20 72 33 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 r3, 1);.
1cd78 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1cd79 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 AddOp2(v, OP_Idx
1cd7a 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 Insert, pExpr->i
1cd7b 54 61 62 6c 65 2c 20 72 32 29 3b 0a 20 20 20 20 Table, r2);.
1cd7c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1cd7d 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1cd7e 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
1cd7f 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 pParse, r1);.
1cd80 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 sqlite3Rele
1cd81 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
1cd82 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a e, r2);. }.
1cd83 20 20 20 20 20 20 69 66 28 20 21 69 73 52 6f 77 if( !isRow
1cd84 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 id ){. sq
1cd85 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1cd86 34 28 76 2c 20 61 64 64 72 2c 20 28 76 6f 69 64 4(v, addr, (void
1cd87 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20 50 34 5f *)&keyInfo, P4_
1cd88 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20 KEYINFO);.
1cd89 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1cd8a 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54 }.. case T
1cd8b 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 K_EXISTS:. ca
1cd8c 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a se TK_SELECT: {.
1cd8d 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 /* This ha
1cd8e 73 20 74 6f 20 62 65 20 61 20 73 63 61 6c 61 72 s to be a scalar
1cd8f 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 72 61 SELECT. Genera
1cd90 74 65 20 63 6f 64 65 20 74 6f 20 70 75 74 20 74 te code to put t
1cd91 68 65 0a 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 he. ** valu
1cd92 65 20 6f 66 20 74 68 69 73 20 73 65 6c 65 63 74 e of this select
1cd93 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c in a memory cel
1cd94 6c 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68 65 l and record the
1cd95 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 2a 2a number. **
1cd96 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 of the memory c
1cd97 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d 6e 2e 0a ell in iColumn..
1cd98 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
1cd99 74 61 74 69 63 20 63 6f 6e 73 74 20 54 6f 6b 65 tatic const Toke
1cd9a 6e 20 6f 6e 65 20 3d 20 7b 20 28 75 38 2a 29 22 n one = { (u8*)"
1cd9b 31 22 2c 20 30 2c 20 31 20 7d 3b 0a 20 20 20 20 1", 0, 1 };.
1cd9c 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 0a Select *pSel;.
1cd9d 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 SelectDest
1cd9e 20 64 65 73 74 3b 0a 0a 20 20 20 20 20 20 70 53 dest;.. pS
1cd9f 65 6c 20 3d 20 70 45 78 70 72 2d 3e 70 53 65 6c el = pExpr->pSel
1cda0 65 63 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ect;. sqlit
1cda1 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 e3SelectDestInit
1cda2 28 26 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61 (&dest, 0, ++pPa
1cda3 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 rse->nMem);.
1cda4 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d if( pExpr->op=
1cda5 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 =TK_SELECT ){.
1cda6 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 dest.eDest
1cda7 20 3d 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 = SRT_Mem;.
1cda8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1cda9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c ddOp2(v, OP_Null
1cdaa 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 , 0, dest.iParm)
1cdab 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f ;. VdbeCo
1cdac 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 mment((v, "Init
1cdad 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 22 subquery result"
1cdae 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ));. }else{
1cdaf 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 65 44 . dest.eD
1cdb0 65 73 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73 est = SRT_Exists
1cdb1 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1cdb2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1cdb3 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65 P_Integer, 0, de
1cdb4 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 st.iParm);.
1cdb5 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 VdbeComment((
1cdb6 76 2c 20 22 49 6e 69 74 20 45 58 49 53 54 53 20 v, "Init EXISTS
1cdb7 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 result"));.
1cdb8 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1cdb9 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 ExprDelete(pPars
1cdba 65 2d 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69 e->db, pSel->pLi
1cdbb 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c mit);. pSel
1cdbc 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 ->pLimit = sqlit
1cdbd 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 e3PExpr(pParse,
1cdbe 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 TK_INTEGER, 0, 0
1cdbf 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69 , &one);. i
1cdc0 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 f( sqlite3Select
1cdc1 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26 (pParse, pSel, &
1cdc2 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 dest) ){.
1cdc3 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
1cdc4 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 . pExpr->iC
1cdc5 6f 6c 75 6d 6e 20 3d 20 64 65 73 74 2e 69 50 61 olumn = dest.iPa
1cdc6 72 6d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b rm;. break;
1cdc7 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
1cdc8 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20 ( testAddr ){.
1cdc9 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1cdca 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64 64 pHere(v, testAdd
1cdcb 72 2d 31 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 r-1);. }.. ret
1cdcc 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a urn;.}.#endif /*
1cdcd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 SQLITE_OMIT_SUB
1cdce 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 QUERY */../*.**
1cdcf 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62 Duplicate an 8-b
1cdd0 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61 yte value.*/.sta
1cdd1 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62 79 tic char *dup8by
1cdd2 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e tes(Vdbe *v, con
1cdd3 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20 st char *in){.
1cdd4 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69 char *out = sqli
1cdd5 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 te3DbMallocRaw(s
1cdd6 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c qlite3VdbeDb(v),
1cdd7 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29 8);. if( out )
1cdd8 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74 {. memcpy(out
1cdd9 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20 , in, 8);. }.
1cdda 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f return out;.}../
1cddb 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e *.** Generate an
1cddc 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 instruction tha
1cddd 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66 t will put the f
1cdde 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a loating point.**
1cddf 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65 64 value described
1cde0 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e by z[0..n-1] in
1cde1 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d to register iMem
1cde2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 ..**.** The z[]
1cde3 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 string will prob
1cde4 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f ably not be zero
1cde5 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 -terminated. Bu
1cde6 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 t the .** z[n] c
1cde7 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72 haracter is guar
1cde8 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d anteed to be som
1cde9 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 ething that does
1cdea 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b not look.** lik
1cdeb 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 e the continuati
1cdec 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 on of the number
1cded 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1cdee 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a codeReal(Vdbe *
1cdef 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a v, const char *z
1cdf0 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6e 65 67 , int n, int neg
1cdf1 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 ateFlag, int iMe
1cdf2 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 20 m){. assert( z
1cdf3 7c 7c 20 76 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 || v==0 || sqlit
1cdf4 65 33 56 64 62 65 44 62 28 76 29 2d 3e 6d 61 6c e3VdbeDb(v)->mal
1cdf5 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 locFailed );. a
1cdf6 73 73 65 72 74 28 20 21 7a 20 7c 7c 20 21 69 73 ssert( !z || !is
1cdf7 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 digit(z[n]) );.
1cdf8 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1cdf9 52 28 6e 29 3b 0a 20 20 69 66 28 20 7a 20 29 7b R(n);. if( z ){
1cdfa 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75 . double valu
1cdfb 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b e;. char *zV;
1cdfc 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 . sqlite3AtoF
1cdfd 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 (z, &value);.
1cdfe 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 if( sqlite3IsNa
1cdff 4e 28 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 N(value) ){.
1ce00 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1ce01 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 Op2(v, OP_Null,
1ce02 30 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 65 0, iMem);. }e
1ce03 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 6e lse{. if( n
1ce04 65 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75 egateFlag ) valu
1ce05 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 e = -value;.
1ce06 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73 zV = dup8bytes
1ce07 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75 (v, (char*)&valu
1ce08 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 e);. sqlite
1ce09 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
1ce0a 50 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c P_Real, 0, iMem,
1ce0b 20 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 0, zV, P4_REAL)
1ce0c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a ;. }. }.}...
1ce0d 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 /*.** Generate a
1ce0e 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 n instruction th
1ce0f 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 at will put the
1ce10 69 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65 integer describe
1ce11 20 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e by.** text z[0.
1ce12 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 .n-1] into regis
1ce13 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 ter iMem..**.**
1ce14 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 The z[] string w
1ce15 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 ill probably not
1ce16 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 be zero-termina
1ce17 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a ted. But the .*
1ce18 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 * z[n] character
1ce19 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
1ce1a 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 o be something t
1ce1b 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f hat does not loo
1ce1c 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f k.** like the co
1ce1d 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 ntinuation of th
1ce1e 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 e number..*/.sta
1ce1f 74 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74 tic void codeInt
1ce20 65 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78 eger(Vdbe *v, Ex
1ce21 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e pr *pExpr, int n
1ce22 65 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d egFlag, int iMem
1ce23 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 ){. const char
1ce24 2a 7a 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d *z;. if( pExpr-
1ce25 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 >flags & EP_IntV
1ce26 61 6c 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 alue ){. int
1ce27 69 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c i = pExpr->iTabl
1ce28 65 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46 6c e;. if( negFl
1ce29 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20 ag ) i = -i;.
1ce2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1ce2b 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
1ce2c 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 65 , i, iMem);. }e
1ce2d 6c 73 65 20 69 66 28 20 28 7a 20 3d 20 28 63 68 lse if( (z = (ch
1ce2e 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e ar*)pExpr->token
1ce2f 2e 7a 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e .z)!=0 ){. in
1ce30 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d t i;. int n =
1ce31 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b pExpr->token.n;
1ce32 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 . assert( !is
1ce33 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 digit(z[n]) );.
1ce34 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 65 if( sqlite3Ge
1ce35 74 49 6e 74 33 32 28 7a 2c 20 26 69 29 20 29 7b tInt32(z, &i) ){
1ce36 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c . if( negFl
1ce37 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20 ag ) i = -i;.
1ce38 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1ce39 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
1ce3a 65 72 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 er, i, iMem);.
1ce3b 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 }else if( sqli
1ce3c 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 te3FitsIn64Bits(
1ce3d 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20 z, negFlag) ){.
1ce3e 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a i64 value;.
1ce3f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a char *zV;.
1ce40 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f sqlite3Ato
1ce41 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a i64(z, &value);.
1ce42 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 if( negFla
1ce43 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c g ) value = -val
1ce44 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64 ue;. zV = d
1ce45 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 up8bytes(v, (cha
1ce46 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 r*)&value);.
1ce47 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1ce48 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c Op4(v, OP_Int64,
1ce49 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 0, iMem, 0, zV,
1ce4a 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20 P4_INT64);.
1ce4b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64 }else{. cod
1ce4c 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 2c 20 6e eReal(v, z, n, n
1ce4d 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 egFlag, iMem);.
1ce4e 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a }. }.}.../*.
1ce4f 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
1ce50 20 74 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61 that will extra
1ce51 63 74 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 ct the iColumn-t
1ce52 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a 2a 2a h column from.**
1ce53 20 74 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20 table pTab and
1ce54 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e store the column
1ce55 20 76 61 6c 75 65 20 69 6e 20 61 20 72 65 67 69 value in a regi
1ce56 73 74 65 72 2e 20 20 41 6e 20 65 66 66 6f 72 74 ster. An effort
1ce57 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 73 .** is made to s
1ce58 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 tore the column
1ce59 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1ce5a 72 20 69 52 65 67 2c 20 62 75 74 20 74 68 69 73 r iReg, but this
1ce5b 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 61 72 61 is.** not guara
1ce5c 6e 74 65 65 64 2e 20 20 54 68 65 20 6c 6f 63 61 nteed. The loca
1ce5d 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 75 tion of the colu
1ce5e 6d 6e 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 mn value is retu
1ce5f 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 rned..**.** Ther
1ce60 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 e must be an ope
1ce61 6e 20 63 75 72 73 6f 72 20 74 6f 20 70 54 61 62 n cursor to pTab
1ce62 20 69 6e 20 69 54 61 62 6c 65 20 77 68 65 6e 20 in iTable when
1ce63 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1ce64 69 73 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 is called. If i
1ce65 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e 20 63 6f Column<0 then co
1ce66 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 de is generated
1ce67 74 68 61 74 20 65 78 74 72 61 63 74 73 20 74 68 that extracts th
1ce68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 e rowid..**.** T
1ce69 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 his routine migh
1ce6a 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 75 t attempt to reu
1ce6b 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 se the value of
1ce6c 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a the column that.
1ce6d 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ** has already b
1ce6e 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 een loaded into
1ce6f 61 20 72 65 67 69 73 74 65 72 2e 20 20 54 68 65 a register. The
1ce70 20 76 61 6c 75 65 20 77 69 6c 6c 20 61 6c 77 61 value will alwa
1ce71 79 73 0a 2a 2a 20 62 65 20 75 73 65 64 20 69 66 ys.** be used if
1ce72 20 69 74 20 68 61 73 20 6e 6f 74 20 75 6e 64 65 it has not unde
1ce73 72 67 6f 6e 65 20 61 6e 79 20 61 66 66 69 6e 69 rgone any affini
1ce74 74 79 20 63 68 61 6e 67 65 73 2e 20 20 42 75 74 ty changes. But
1ce75 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66 69 6e 69 if.** an affini
1ce76 74 79 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 ty change has oc
1ce77 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 curred, then the
1ce78 20 63 61 63 68 65 64 20 76 61 6c 75 65 20 77 69 cached value wi
1ce79 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 75 73 ll only be.** us
1ce7a 65 64 20 69 66 20 61 6c 6c 6f 77 41 66 66 43 68 ed if allowAffCh
1ce7b 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 53 ng is true..*/.S
1ce7c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1ce7d 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
1ce7e 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 20 50 61 eGetColumn(. Pa
1ce7f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f rse *pParse, /
1ce80 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
1ce81 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
1ce82 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 ntext */. Table
1ce83 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 44 *pTab, /* D
1ce84 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 escription of th
1ce85 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 72 e table we are r
1ce86 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a 2f 0a 20 eading from */.
1ce87 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 int iColumn,
1ce88 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 /* Index of th
1ce89 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a e table column *
1ce8a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 /. int iTable,
1ce8b 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 /* The curs
1ce8c 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 or pointing to t
1ce8d 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e he table */. in
1ce8e 74 20 69 52 65 67 2c 20 20 20 20 20 20 20 20 2f t iReg, /
1ce8f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 * Store results
1ce90 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 61 6c here */. int al
1ce91 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a 20 54 72 lowAffChng /* Tr
1ce92 75 65 20 69 66 20 70 72 69 6f 72 20 61 66 66 69 ue if prior affi
1ce93 6e 69 74 79 20 63 68 61 6e 67 65 73 20 61 72 65 nity changes are
1ce94 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 OK */.){. Vdbe
1ce95 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 *v = pParse->pV
1ce96 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 dbe;. int i;.
1ce97 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 struct yColCache
1ce98 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c *p;.. for(i=0,
1ce99 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 p=pParse->aColC
1ce9a 61 63 68 65 3b 20 69 3c 70 50 61 72 73 65 2d 3e ache; i<pParse->
1ce9b 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 2c 20 nColCache; i++,
1ce9c 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p++){. if( p-
1ce9d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 >iTable==iTable
1ce9e 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 && p->iColumn==i
1ce9f 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20 Column.
1cea0 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68 61 && (!p->affCha
1cea1 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66 43 nge || allowAffC
1cea2 68 6e 67 29 20 29 7b 0a 23 69 66 20 30 0a 20 20 hng) ){.#if 0.
1cea3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1cea4 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f 6f 70 ddOp0(v, OP_Noop
1cea5 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d );. VdbeCom
1cea6 6d 65 6e 74 28 28 76 2c 20 22 4f 50 54 3a 20 74 ment((v, "OPT: t
1cea7 61 62 25 64 2e 63 6f 6c 25 64 20 2d 3e 20 72 25 ab%d.col%d -> r%
1cea8 64 22 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c d", iTable, iCol
1cea9 75 6d 6e 2c 20 70 2d 3e 69 52 65 67 29 29 3b 0a umn, p->iReg));.
1ceaa 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74 #endif. ret
1ceab 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 urn p->iReg;.
1ceac 20 7d 0a 20 20 7d 20 20 0a 20 20 61 73 73 65 72 }. } . asser
1cead 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 t( v!=0 );. if(
1ceae 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 iColumn<0 ){.
1ceaf 20 20 69 6e 74 20 6f 70 20 3d 20 28 70 54 61 62 int op = (pTab
1ceb0 20 26 26 20 49 73 56 69 72 74 75 61 6c 28 70 54 && IsVirtual(pT
1ceb1 61 62 29 29 20 3f 20 4f 50 5f 56 52 6f 77 69 64 ab)) ? OP_VRowid
1ceb2 20 3a 20 4f 50 5f 52 6f 77 69 64 3b 0a 20 20 20 : OP_Rowid;.
1ceb3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1ceb4 70 32 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 p2(v, op, iTable
1ceb5 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 , iReg);. }else
1ceb6 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a if( pTab==0 ){.
1ceb7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1ceb8 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 ddOp3(v, OP_Colu
1ceb9 6d 6e 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c mn, iTable, iCol
1ceba 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 umn, iReg);. }e
1cebb 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20 lse{. int op
1cebc 3d 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 = IsVirtual(pTab
1cebd 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a ) ? OP_VColumn :
1cebe 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 OP_Column;.
1cebf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1cec0 33 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c 3(v, op, iTable,
1cec1 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b iColumn, iReg);
1cec2 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 . sqlite3Colu
1cec3 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 mnDefault(v, pTa
1cec4 62 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 23 69 66 b, iColumn);.#if
1cec5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1cec6 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
1cec7 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 61 43 if( pTab->aC
1cec8 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 ol[iColumn].affi
1cec9 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 nity==SQLITE_AFF
1ceca 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20 20 20 73 _REAL ){. s
1cecb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
1cecc 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e (v, OP_RealAffin
1cecd 69 74 79 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 ity, iReg);.
1cece 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 }.#endif. }. i
1cecf 66 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 f( pParse->disab
1ced0 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 7b leColCache==0 ){
1ced1 0a 20 20 20 20 69 20 3d 20 70 50 61 72 73 65 2d . i = pParse-
1ced2 3e 69 43 6f 6c 43 61 63 68 65 3b 0a 20 20 20 20 >iColCache;.
1ced3 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 43 6f p = &pParse->aCo
1ced4 6c 43 61 63 68 65 5b 69 5d 3b 0a 20 20 20 20 70 lCache[i];. p
1ced5 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c ->iTable = iTabl
1ced6 65 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d e;. p->iColum
1ced7 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 n = iColumn;.
1ced8 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b p->iReg = iReg;
1ced9 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 . p->affChang
1ceda 65 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a e = 0;. i++;.
1cedb 20 20 20 20 69 66 28 20 69 3e 3d 41 72 72 61 79 if( i>=Array
1cedc 53 69 7a 65 28 70 50 61 72 73 65 2d 3e 61 43 6f Size(pParse->aCo
1cedd 6c 43 61 63 68 65 29 20 29 20 69 20 3d 20 30 3b lCache) ) i = 0;
1cede 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 72 73 . if( i>pPars
1cedf 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 20 29 20 70 e->nColCache ) p
1cee0 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 Parse->nColCache
1cee1 20 3d 20 69 3b 0a 20 20 20 20 70 50 61 72 73 65 = i;. pParse
1cee2 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 69 3b ->iColCache = i;
1cee3 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52 . }. return iR
1cee4 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 eg;.}../*.** Cle
1cee5 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 ar all column ca
1cee6 63 68 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f che entries asso
1cee7 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
1cee8 76 64 62 65 0a 2a 2a 20 63 75 72 73 6f 72 20 77 vdbe.** cursor w
1cee9 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 ith cursor numbe
1ceea 72 20 69 54 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c r iTable..*/.SQL
1ceeb 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1ceec 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 sqlite3ExprClea
1ceed 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 rColumnCache(Par
1ceee 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
1ceef 69 54 61 62 6c 65 29 7b 0a 20 20 69 66 28 20 69 iTable){. if( i
1cef0 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 70 Table<0 ){. p
1cef1 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 Parse->nColCache
1cef2 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 65 = 0;. pParse
1cef3 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 30 3b ->iColCache = 0;
1cef4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
1cef5 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t i;. for(i=0
1cef6 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c ; i<pParse->nCol
1cef7 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 Cache; i++){.
1cef8 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 if( pParse->a
1cef9 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 54 61 62 ColCache[i].iTab
1cefa 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20 le==iTable ){.
1cefb 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1cefc 69 3d 3d 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 i==pParse->nColC
1cefd 61 63 68 65 2d 31 20 29 3b 0a 20 20 20 20 20 20 ache-1 );.
1cefe 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 pParse->aColCa
1ceff 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d che[i] = pParse-
1cf00 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 50 61 >aColCache[--pPa
1cf01 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 5d 3b rse->nColCache];
1cf02 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d . pParse-
1cf03 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 61 >iColCache = pPa
1cf04 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 0a rse->nColCache;.
1cf05 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1cf06 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 }.}../*.** Recor
1cf07 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 d the fact that
1cf08 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e an affinity chan
1cf09 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 ge has occurred
1cf0a 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a 20 72 65 67 on iCount.** reg
1cf0b 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 isters starting
1cf0c 77 69 74 68 20 69 53 74 61 72 74 2e 0a 2a 2f 0a with iStart..*/.
1cf0d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1cf0e 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
1cf0f 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e acheAffinityChan
1cf10 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ge(Parse *pParse
1cf11 2c 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 6e , int iStart, in
1cf12 74 20 69 43 6f 75 6e 74 29 7b 0a 20 20 69 6e 74 t iCount){. int
1cf13 20 69 45 6e 64 20 3d 20 69 53 74 61 72 74 20 2b iEnd = iStart +
1cf14 20 69 43 6f 75 6e 74 20 2d 20 31 3b 0a 20 20 69 iCount - 1;. i
1cf15 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
1cf16 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 i<pParse->nColC
1cf17 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 ache; i++){.
1cf18 69 6e 74 20 72 20 3d 20 70 50 61 72 73 65 2d 3e int r = pParse->
1cf19 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 aColCache[i].iRe
1cf1a 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 53 g;. if( r>=iS
1cf1b 74 61 72 74 20 26 26 20 72 3c 3d 69 45 6e 64 20 tart && r<=iEnd
1cf1c 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d ){. pParse-
1cf1d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 61 66 >aColCache[i].af
1cf1e 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 20 20 fChange = 1;.
1cf1f 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
1cf20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
1cf21 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 66 72 move content fr
1cf22 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46 72 om registers iFr
1cf23 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d om...iFrom+nReg-
1cf24 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 1.** over to iTo
1cf25 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 4b 65 ..iTo+nReg-1. Ke
1cf26 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 ep the column ca
1cf27 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 2e 0a che up-to-date..
1cf28 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1cf29 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
1cf2a 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65 prCodeMove(Parse
1cf2b 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 *pParse, int iF
1cf2c 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e rom, int iTo, in
1cf2d 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 t nReg){. int i
1cf2e 3b 0a 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69 ;. if( iFrom==i
1cf2f 54 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 To ) return;. s
1cf30 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1cf31 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 (pParse->pVdbe,
1cf32 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20 OP_Move, iFrom,
1cf33 69 54 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f iTo, nReg);. fo
1cf34 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d r(i=0; i<pParse-
1cf35 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29 >nColCache; i++)
1cf36 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 70 50 {. int x = pP
1cf37 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b arse->aColCache[
1cf38 69 5d 2e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 i].iReg;. if(
1cf39 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69 x>=iFrom && x<i
1cf3a 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20 From+nReg ){.
1cf3b 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 pParse->aColC
1cf3c 61 63 68 65 5b 69 5d 2e 69 52 65 67 20 2b 3d 20 ache[i].iReg +=
1cf3d 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d iTo-iFrom;. }
1cf3e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 . }.}../*.** Ge
1cf3f 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63 nerate code to c
1cf40 6f 70 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d opy content from
1cf41 20 72 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d registers iFrom
1cf42 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a ...iFrom+nReg-1.
1cf43 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e ** over to iTo..
1cf44 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 iTo+nReg-1..*/.S
1cf45 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1cf46 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f id sqlite3ExprCo
1cf47 64 65 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 deCopy(Parse *pP
1cf48 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c arse, int iFrom,
1cf49 20 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 int iTo, int nR
1cf4a 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 eg){. int i;.
1cf4b 69 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29 if( iFrom==iTo )
1cf4c 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 return;. for(i
1cf4d 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b 29 =0; i<nReg; i++)
1cf4e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1cf4f 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e eAddOp2(pParse->
1cf50 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c 20 pVdbe, OP_Copy,
1cf51 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b iFrom+i, iTo+i);
1cf52 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
1cf53 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 79 turn true if any
1cf54 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 register in the
1cf55 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 54 range iFrom..iT
1cf56 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a 2a o (inclusive).**
1cf57 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74 is used as part
1cf58 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 of the column c
1cf59 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ache..*/.static
1cf5a 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e int usedAsColumn
1cf5b 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 Cache(Parse *pPa
1cf5c 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 rse, int iFrom,
1cf5d 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 20 int iTo){. int
1cf5e 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
1cf5f 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 pParse->nColCach
1cf60 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 e; i++){. int
1cf61 20 72 20 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f r = pParse->aCo
1cf62 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 3b 0a lCache[i].iReg;.
1cf63 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d if( r>=iFrom
1cf64 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74 && r<=iTo ) ret
1cf65 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 urn 1;. }. ret
1cf66 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
1cf67 54 68 65 72 65 73 20 69 73 20 61 20 76 61 6c 75 Theres is a valu
1cf68 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 43 e in register iC
1cf69 75 72 72 65 6e 74 2e 20 20 57 65 20 75 6c 74 69 urrent. We ulti
1cf6a 6d 61 74 65 6c 79 20 77 61 6e 74 0a 2a 2a 20 74 mately want.** t
1cf6b 68 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 69 he value to be i
1cf6c 6e 20 72 65 67 69 73 74 65 72 20 69 54 61 72 67 n register iTarg
1cf6d 65 74 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65 et. It might be
1cf6e 20 74 68 61 74 0a 2a 2a 20 69 43 75 72 72 65 6e that.** iCurren
1cf6f 74 20 61 6e 64 20 69 54 61 72 67 65 74 20 61 72 t and iTarget ar
1cf70 65 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 e the same regis
1cf71 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 72 ter..**.** We ar
1cf72 65 20 67 6f 69 6e 67 20 74 6f 20 6d 6f 64 69 66 e going to modif
1cf73 79 20 74 68 65 20 76 61 6c 75 65 2c 20 73 6f 20 y the value, so
1cf74 77 65 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 we need to make
1cf75 73 75 72 65 20 69 74 0a 2a 2a 20 69 73 20 6e 6f sure it.** is no
1cf76 74 20 61 20 63 61 63 68 65 64 20 72 65 67 69 73 t a cached regis
1cf77 74 65 72 2e 20 20 49 66 20 69 43 75 72 72 65 6e ter. If iCurren
1cf78 74 20 69 73 20 61 20 63 61 63 68 65 64 20 72 65 t is a cached re
1cf79 67 69 73 74 65 72 2c 0a 2a 2a 20 74 68 65 6e 20 gister,.** then
1cf7a 74 72 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 try to move the
1cf7b 76 61 6c 75 65 20 6f 76 65 72 20 74 6f 20 69 54 value over to iT
1cf7c 61 72 67 65 74 2e 20 20 49 66 20 69 54 61 72 67 arget. If iTarg
1cf7d 65 74 20 69 73 20 61 0a 2a 2a 20 63 61 63 68 65 et is a.** cache
1cf7e 64 20 72 65 67 69 73 74 65 72 2c 20 74 68 65 6e d register, then
1cf7f 20 63 6c 65 61 72 20 74 68 65 20 63 6f 72 72 65 clear the corre
1cf80 73 70 6f 6e 64 69 6e 67 20 63 61 63 68 65 20 6c sponding cache l
1cf81 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ine..**.** Retur
1cf82 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 74 n the register t
1cf83 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 65 6e hat the value en
1cf84 64 73 20 75 70 20 69 6e 2e 0a 2a 2f 0a 53 51 4c ds up in..*/.SQL
1cf85 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1cf86 73 71 6c 69 74 65 33 45 78 70 72 57 72 69 74 61 sqlite3ExprWrita
1cf87 62 6c 65 52 65 67 69 73 74 65 72 28 50 61 72 73 bleRegister(Pars
1cf88 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
1cf89 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 69 54 61 Current, int iTa
1cf8a 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a rget){. int i;.
1cf8b 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 assert( pParse
1cf8c 2d 3e 70 56 64 62 65 21 3d 30 20 29 3b 0a 20 20 ->pVdbe!=0 );.
1cf8d 69 66 28 20 21 75 73 65 64 41 73 43 6f 6c 75 6d if( !usedAsColum
1cf8e 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 69 nCache(pParse, i
1cf8f 43 75 72 72 65 6e 74 2c 20 69 43 75 72 72 65 6e Current, iCurren
1cf90 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e t) ){. return
1cf91 20 69 43 75 72 72 65 6e 74 3b 0a 20 20 7d 0a 20 iCurrent;. }.
1cf92 20 69 66 28 20 69 43 75 72 72 65 6e 74 21 3d 69 if( iCurrent!=i
1cf93 54 61 72 67 65 74 20 29 7b 0a 20 20 20 20 73 71 Target ){. sq
1cf94 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1cf95 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f pParse->pVdbe, O
1cf96 50 5f 53 43 6f 70 79 2c 20 69 43 75 72 72 65 6e P_SCopy, iCurren
1cf97 74 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 7d t, iTarget);. }
1cf98 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 . for(i=0; i<pP
1cf99 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b arse->nColCache;
1cf9a 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 i++){. if( p
1cf9b 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
1cf9c 5b 69 5d 2e 69 52 65 67 3d 3d 69 54 61 72 67 65 [i].iReg==iTarge
1cf9d 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 t ){. pPars
1cf9e 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 20 e->aColCache[i]
1cf9f 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 = pParse->aColCa
1cfa0 63 68 65 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e 43 che[--pParse->nC
1cfa1 6f 6c 43 61 63 68 65 5d 3b 0a 20 20 20 20 20 20 olCache];.
1cfa2 70 50 61 72 73 65 2d 3e 69 43 6f 6c 43 61 63 68 pParse->iColCach
1cfa3 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 43 6f 6c e = pParse->nCol
1cfa4 43 61 63 68 65 3b 0a 20 20 20 20 7d 0a 20 20 7d Cache;. }. }
1cfa5 0a 20 20 72 65 74 75 72 6e 20 69 54 61 72 67 65 . return iTarge
1cfa6 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 t;.}../*.** If t
1cfa7 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 he last instruct
1cfa8 69 6f 6e 20 63 6f 64 65 64 20 69 73 20 61 6e 20 ion coded is an
1cfa9 65 70 68 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f ephemeral copy o
1cfaa 66 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20 f any of.** the
1cfab 72 65 67 69 73 74 65 72 73 20 69 6e 20 74 68 65 registers in the
1cfac 20 6e 52 65 67 20 72 65 67 69 73 74 65 72 73 20 nReg registers
1cfad 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 69 beginning with i
1cfae 52 65 67 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e Reg, then.** con
1cfaf 76 65 72 74 20 74 68 65 20 6c 61 73 74 20 69 6e vert the last in
1cfb0 73 74 72 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f struction from O
1cfb1 50 5f 53 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f P_SCopy to OP_Co
1cfb2 70 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 py..*/.SQLITE_PR
1cfb3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1cfb4 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 e3ExprHardCopy(P
1cfb5 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
1cfb6 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 t iReg, int nReg
1cfb7 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 ){. int addr;.
1cfb8 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 VdbeOp *pOp;.
1cfb9 56 64 62 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20 Vdbe *v;.. v =
1cfba 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 pParse->pVdbe;.
1cfbb 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 addr = sqlite3V
1cfbc 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 dbeCurrentAddr(v
1cfbd 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 );. pOp = sqlit
1cfbe 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 61 e3VdbeGetOp(v, a
1cfbf 64 64 72 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 ddr-1);. assert
1cfc0 28 20 70 4f 70 20 7c 7c 20 70 50 61 72 73 65 2d ( pOp || pParse-
1cfc1 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1cfc2 64 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 20 26 d );. if( pOp &
1cfc3 26 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f & pOp->opcode==O
1cfc4 50 5f 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e P_SCopy && pOp->
1cfc5 70 31 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d p1>=iReg && pOp-
1cfc6 3e 70 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b >p1<iReg+nReg ){
1cfc7 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 . pOp->opcode
1cfc8 20 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a = OP_Copy;. }.
1cfc9 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
1cfca 65 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 e code to store
1cfcb 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1cfcc 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 iAlias-th alias
1cfcd 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 in register.**
1cfce 74 61 72 67 65 74 2e 20 20 54 68 65 20 66 69 72 target. The fir
1cfcf 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 st time this is
1cfd0 63 61 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73 called, pExpr is
1cfd1 20 65 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f evaluated to co
1cfd2 6d 70 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c mpute.** the val
1cfd3 75 65 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e ue of the alias.
1cfd4 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 The value is s
1cfd5 74 6f 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69 tored in an auxi
1cfd6 6c 69 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a liary register.*
1cfd7 2a 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 * and the number
1cfd8 20 6f 66 20 74 68 61 74 20 72 65 67 69 73 74 65 of that registe
1cfd9 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 r is returned.
1cfda 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 On subsequent ca
1cfdb 6c 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 lls,.** the regi
1cfdc 73 74 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72 ster number is r
1cfdd 65 74 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20 eturned without
1cfde 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63 generating any c
1cfdf 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ode..**.** Note
1cfe0 74 68 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f that in order fo
1cfe1 72 20 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 r this to work,
1cfe2 63 6f 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e code must be gen
1cfe3 65 72 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a erated in the.**
1cfe4 20 73 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74 same order that
1cfe5 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e it is executed.
1cfe6 0a 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 .**.** Aliases a
1cfe7 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 re numbered star
1cfe8 74 69 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f ting with 1. So
1cfe9 20 69 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68 iAlias is in th
1cfea 65 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 e range.** of 1
1cfeb 74 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 to pParse->nAlia
1cfec 73 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a s inclusive. .*
1cfed 2a 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c *.** pParse->aAl
1cfee 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 ias[iAlias-1] re
1cfef 63 6f 72 64 73 20 74 68 65 20 72 65 67 69 73 74 cords the regist
1cff0 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 er number where
1cff1 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 the value.** of
1cff2 74 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c the iAlias-th al
1cff3 69 61 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20 ias is stored.
1cff4 49 66 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 If zero, that me
1cff5 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ans that the.**
1cff6 61 6c 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65 alias has not ye
1cff7 74 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e t been computed.
1cff8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
1cff9 6f 64 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a odeAlias(Parse *
1cffa 70 50 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 pParse, int iAli
1cffb 61 73 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c as, Expr *pExpr,
1cffc 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 int target){.
1cffd 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
1cffe 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 arse->db;. int
1cfff 69 52 65 67 3b 0a 20 20 69 66 28 20 70 50 61 72 iReg;. if( pPar
1d000 73 65 2d 3e 61 41 6c 69 61 73 3d 3d 30 20 29 7b se->aAlias==0 ){
1d001 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c . pParse->aAl
1d002 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 4d ias = sqlite3DbM
1d003 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20 allocZero(db, .
1d004 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d006 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 sizeof(pParse->a
1d007 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65 Alias[0])*pParse
1d008 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20 ->nAlias );.
1d009 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1d00a 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b iled ) return 0;
1d00b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 . }. assert( i
1d00c 41 6c 69 61 73 3e 30 20 26 26 20 69 41 6c 69 61 Alias>0 && iAlia
1d00d 73 3c 3d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 s<=pParse->nAlia
1d00e 73 20 29 3b 0a 20 20 69 52 65 67 20 3d 20 70 50 s );. iReg = pP
1d00f 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c arse->aAlias[iAl
1d010 69 61 73 2d 31 5d 3b 0a 20 20 69 66 28 20 69 52 ias-1];. if( iR
1d011 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 eg==0 ){. if(
1d012 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 pParse->disable
1d013 43 6f 6c 43 61 63 68 65 20 29 7b 0a 20 20 20 20 ColCache ){.
1d014 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33 iReg = sqlite3
1d015 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 ExprCodeTarget(p
1d016 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 Parse, pExpr, ta
1d017 72 67 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 rget);. }else
1d018 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 2b {. iReg = +
1d019 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
1d01a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1d01b 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 Code(pParse, pEx
1d01c 70 72 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20 pr, iReg);.
1d01d 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b pParse->aAlias[
1d01e 69 41 6c 69 61 73 2d 31 5d 20 3d 20 69 52 65 67 iAlias-1] = iReg
1d01f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1d020 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a turn iReg;.}../*
1d021 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
1d022 65 20 69 6e 74 6f 20 74 68 65 20 63 75 72 72 65 e into the curre
1d023 6e 74 20 56 64 62 65 20 74 6f 20 65 76 61 6c 75 nt Vdbe to evalu
1d024 61 74 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a ate the given.**
1d025 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 41 74 expression. At
1d026 74 65 6d 70 74 20 74 6f 20 73 74 6f 72 65 20 74 tempt to store t
1d027 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 72 65 he results in re
1d028 67 69 73 74 65 72 20 22 74 61 72 67 65 74 22 2e gister "target".
1d029 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 72 .** Return the r
1d02a 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 65 egister where re
1d02b 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 sults are stored
1d02c 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 69 ..**.** With thi
1d02d 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 72 65 s routine, there
1d02e 20 69 73 20 6e 6f 20 67 75 61 72 61 6e 74 65 65 is no guarantee
1d02f 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 77 69 that results wi
1d030 6c 6c 0a 2a 2a 20 62 65 20 73 74 6f 72 65 64 20 ll.** be stored
1d031 69 6e 20 74 61 72 67 65 74 2e 20 20 54 68 65 20 in target. The
1d032 72 65 73 75 6c 74 20 6d 69 67 68 74 20 62 65 20 result might be
1d033 73 74 6f 72 65 64 20 69 6e 20 73 6f 6d 65 20 6f stored in some o
1d034 74 68 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 ther.** register
1d035 20 69 66 20 69 74 20 69 73 20 63 6f 6e 76 65 6e if it is conven
1d036 69 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 20 20 ient to do so.
1d037 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 The calling func
1d038 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 63 68 65 tion.** must che
1d039 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f ck the return co
1d03a 64 65 20 61 6e 64 20 6d 6f 76 65 20 74 68 65 20 de and move the
1d03b 72 65 73 75 6c 74 73 20 74 6f 20 74 68 65 20 64 results to the d
1d03c 65 73 69 72 65 64 0a 2a 2a 20 72 65 67 69 73 74 esired.** regist
1d03d 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
1d03e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1d03f 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 3ExprCodeTarget(
1d040 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
1d041 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
1d042 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62 65 20 target){. Vdbe
1d043 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 *v = pParse->pVd
1d044 62 65 3b 20 20 2f 2a 20 54 68 65 20 56 4d 20 75 be; /* The VM u
1d045 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
1d046 6e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 n */. int op;
1d047 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d048 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64 65 20 62 /* The opcode b
1d049 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 eing coded */.
1d04a 69 6e 74 20 69 6e 52 65 67 20 3d 20 74 61 72 67 int inReg = targ
1d04b 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 et; /* Res
1d04c 75 6c 74 73 20 73 74 6f 72 65 64 20 69 6e 20 72 ults stored in r
1d04d 65 67 69 73 74 65 72 20 69 6e 52 65 67 20 2a 2f egister inReg */
1d04e 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 . int regFree1
1d04f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
1d050 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 If non-zero free
1d051 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 this temporary
1d052 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e register */. in
1d053 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b 20 t regFree2 = 0;
1d054 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f /* If no
1d055 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 n-zero free this
1d056 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 temporary regis
1d057 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 31 2c ter */. int r1,
1d058 20 72 32 2c 20 72 33 2c 20 72 34 3b 20 20 20 20 r2, r3, r4;
1d059 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 72 65 /* Various re
1d05a 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20 2a gister numbers *
1d05b 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b /. sqlite3 *db;
1d05c 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d .. db = pParse-
1d05d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 76 >db;. assert( v
1d05e 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f !=0 || db->mallo
1d05f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 cFailed );. ass
1d060 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 26 26 ert( target>0 &&
1d061 20 74 61 72 67 65 74 3c 3d 70 50 61 72 73 65 2d target<=pParse-
1d062 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 66 28 20 76 >nMem );. if( v
1d063 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1d064 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 . if( pExpr==0
1d065 29 7b 0a 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e ){. op = TK_N
1d066 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ULL;. }else{.
1d067 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 op = pExpr->op
1d068 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 ;. }. switch(
1d069 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 op ){. case T
1d06a 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a K_AGG_COLUMN: {.
1d06b 20 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 AggInfo *p
1d06c 41 67 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d AggInfo = pExpr-
1d06d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 >pAggInfo;.
1d06e 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f struct AggInfo_
1d06f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 col *pCol = &pAg
1d070 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 gInfo->aCol[pExp
1d071 72 2d 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 r->iAgg];.
1d072 69 66 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 if( !pAggInfo->d
1d073 69 72 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 irectMode ){.
1d074 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f assert( pCo
1d075 6c 2d 3e 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 l->iMem>0 );.
1d076 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 43 6f inReg = pCo
1d077 6c 2d 3e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 l->iMem;.
1d078 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 break;. }e
1d079 6c 73 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f lse if( pAggInfo
1d07a 2d 3e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 ->useSortingIdx
1d07b 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1d07c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
1d07d 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 OP_Column, pAggI
1d07e 6e 66 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c nfo->sortingIdx,
1d07f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1d080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1d081 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 Col->iSorterColu
1d082 6d 6e 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 mn, target);.
1d083 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1d084 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 }. /* Oth
1d085 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 erwise, fall thr
1d086 75 20 69 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f u into the TK_CO
1d087 4c 55 4d 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20 LUMN case */.
1d088 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 }. case TK_C
1d089 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69 OLUMN: {. i
1d08a 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 f( pExpr->iTable
1d08b 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a <0 ){. /*
1d08c 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 This only happe
1d08d 6e 73 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 ns when coding c
1d08e 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 heck constraints
1d08f 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 */. asse
1d090 72 74 28 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 rt( pParse->ckBa
1d091 73 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 se>0 );.
1d092 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 inReg = pExpr->i
1d093 43 6f 6c 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d Column + pParse-
1d094 3e 63 6b 42 61 73 65 3b 0a 20 20 20 20 20 20 7d >ckBase;. }
1d095 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65 else{. te
1d096 73 74 63 61 73 65 28 20 28 70 45 78 70 72 2d 3e stcase( (pExpr->
1d097 66 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 flags & EP_AnyAf
1d098 66 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 f)!=0 );.
1d099 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 inReg = sqlite3
1d09a 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d ExprCodeGetColum
1d09b 6e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d n(pParse, pExpr-
1d09c 3e 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20 >pTab,.
1d09d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d09e 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1d09f 43 6f 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 Column, pExpr->i
1d0a0 54 61 62 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20 Table, target,.
1d0a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d0a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d0a3 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 pExpr->flags & E
1d0a4 50 5f 41 6e 79 41 66 66 29 3b 0a 20 20 20 20 20 P_AnyAff);.
1d0a5 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1d0a6 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
1d0a7 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 K_INTEGER: {.
1d0a8 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 codeInteger(v
1d0a9 2c 20 70 45 78 70 72 2c 20 30 2c 20 74 61 72 67 , pExpr, 0, targ
1d0aa 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b et);. break
1d0ab 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1d0ac 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 TK_FLOAT: {.
1d0ad 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 28 codeReal(v, (
1d0ae 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b char*)pExpr->tok
1d0af 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b en.z, pExpr->tok
1d0b0 65 6e 2e 6e 2c 20 30 2c 20 74 61 72 67 65 74 29 en.n, 0, target)
1d0b1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1d0b2 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
1d0b3 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 _STRING: {.
1d0b4 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 45 sqlite3DequoteE
1d0b5 78 70 72 28 64 62 2c 20 70 45 78 70 72 29 3b 0a xpr(db, pExpr);.
1d0b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d0b7 65 41 64 64 4f 70 34 28 76 2c 4f 50 5f 53 74 72 eAddOp4(v,OP_Str
1d0b8 69 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c ing8, 0, target,
1d0b9 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 0,.
1d0ba 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 (cha
1d0bb 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e r*)pExpr->token.
1d0bc 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e z, pExpr->token.
1d0bd 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b n);. break;
1d0be 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1d0bf 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 TK_NULL: {.
1d0c0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1d0c1 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 p2(v, OP_Null, 0
1d0c2 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
1d0c3 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 break;. }.#i
1d0c4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1d0c5 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 T_BLOB_LITERAL.
1d0c6 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a case TK_BLOB:
1d0c7 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a {. int n;.
1d0c8 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1d0c9 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68 61 72 20 *z;. char
1d0ca 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 20 61 73 *zBlob;. as
1d0cb 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b sert( pExpr->tok
1d0cc 65 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 20 20 en.n>=3 );.
1d0cd 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
1d0ce 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20 token.z[0]=='x'
1d0cf 7c 7c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e || pExpr->token.
1d0d0 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 z[0]=='X' );.
1d0d1 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
1d0d2 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c ->token.z[1]=='\
1d0d3 27 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 '' );. asse
1d0d4 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e rt( pExpr->token
1d0d5 2e 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e .z[pExpr->token.
1d0d6 6e 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 n-1]=='\'' );.
1d0d7 20 20 20 20 6e 20 3d 20 70 45 78 70 72 2d 3e 74 n = pExpr->t
1d0d8 6f 6b 65 6e 2e 6e 20 2d 20 33 3b 0a 20 20 20 20 oken.n - 3;.
1d0d9 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 45 78 z = (char*)pEx
1d0da 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b 20 32 3b pr->token.z + 2;
1d0db 0a 20 20 20 20 20 20 7a 42 6c 6f 62 20 3d 20 73 . zBlob = s
1d0dc 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 qlite3HexToBlob(
1d0dd 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 sqlite3VdbeDb(v)
1d0de 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 , z, n);. s
1d0df 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
1d0e0 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 (v, OP_Blob, n/2
1d0e1 2c 20 74 61 72 67 65 74 2c 20 30 2c 20 7a 42 6c , target, 0, zBl
1d0e2 6f 62 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b ob, P4_DYNAMIC);
1d0e3 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1d0e4 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 }.#endif. c
1d0e5 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a ase TK_VARIABLE:
1d0e6 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 {. sqlite3
1d0e7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1d0e8 5f 56 61 72 69 61 62 6c 65 2c 20 70 45 78 70 72 _Variable, pExpr
1d0e9 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 65 74 ->iTable, target
1d0ea 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 );. if( pEx
1d0eb 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 31 20 29 7b pr->token.n>1 ){
1d0ec 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1d0ed 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 VdbeChangeP4(v,
1d0ee 2d 31 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 -1, (char*)pExpr
1d0ef 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 ->token.z, pExpr
1d0f0 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 ->token.n);.
1d0f1 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
1d0f2 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1d0f3 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b 0a 20 TK_REGISTER: {.
1d0f4 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 inReg = pEx
1d0f5 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 pr->iTable;.
1d0f6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1d0f7 20 20 20 63 61 73 65 20 54 4b 5f 41 53 3a 20 7b case TK_AS: {
1d0f8 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 63 . inReg = c
1d0f9 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c odeAlias(pParse,
1d0fa 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 pExpr->iTable,
1d0fb 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 pExpr->pLeft, ta
1d0fc 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 rget);. bre
1d0fd 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 ak;. }.#ifnde
1d0fe 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 f SQLITE_OMIT_CA
1d0ff 53 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 ST. case TK_C
1d100 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 AST: {. /*
1d101 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 Expressions of t
1d102 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54 28 he form: CAST(
1d103 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29 20 pLeft AS token)
1d104 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66 */. int aff
1d105 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 20 20 69 , to_op;. i
1d106 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 nReg = sqlite3Ex
1d107 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 prCodeTarget(pPa
1d108 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1d109 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 t, target);.
1d10a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 41 aff = sqlite3A
1d10b 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45 78 ffinityType(&pEx
1d10c 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 pr->token);.
1d10d 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 2d 20 to_op = aff -
1d10e 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 SQLITE_AFF_TEXT
1d10f 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 20 20 + OP_ToText;.
1d110 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
1d111 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 7c ==OP_ToText |
1d112 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
1d113 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 20 20 F_TEXT );.
1d114 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
1d115 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 7c ==OP_ToBlob |
1d116 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
1d117 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 20 20 F_NONE );.
1d118 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
1d119 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 7c ==OP_ToNumeric |
1d11a 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
1d11b 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 F_NUMERIC );.
1d11c 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
1d11d 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 7c ==OP_ToInt |
1d11e 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
1d11f 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 F_INTEGER );.
1d120 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 assert( to_op
1d121 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 7c ==OP_ToReal |
1d122 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 | aff!=SQLITE_AF
1d123 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 20 20 F_REAL );.
1d124 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f testcase( to_
1d125 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 29 3b op==OP_ToText );
1d126 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1d127 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f to_op==OP_ToBlo
1d128 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 b );. testc
1d129 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 ase( to_op==OP_T
1d12a 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 20 20 oNumeric );.
1d12b 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f testcase( to_o
1d12c 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b 0a 20 p==OP_ToInt );.
1d12d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 testcase( t
1d12e 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 o_op==OP_ToReal
1d12f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e 52 );. if( inR
1d130 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a 20 20 eg!=target ){.
1d131 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d132 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 eAddOp2(v, OP_SC
1d133 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 opy, inReg, targ
1d134 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 et);. inR
1d135 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 eg = target;.
1d136 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
1d137 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
1d138 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b 0a 20 to_op, inReg);.
1d139 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 75 testcase( u
1d13a 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 sedAsColumnCache
1d13b 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 (pParse, inReg,
1d13c 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 20 20 inReg) );.
1d13d 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
1d13e 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 AffinityChange(p
1d13f 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 31 29 Parse, inReg, 1)
1d140 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1d141 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.#endif /* S
1d142 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 QLITE_OMIT_CAST
1d143 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c */. case TK_L
1d144 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c T:. case TK_L
1d145 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 E:. case TK_G
1d146 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 T:. case TK_G
1d147 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e E:. case TK_N
1d148 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 E:. case TK_E
1d149 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 Q: {. asser
1d14a 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 t( TK_LT==OP_Lt
1d14b 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1d14c 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b TK_LE==OP_Le );
1d14d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
1d14e 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 K_GT==OP_Gt );.
1d14f 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1d150 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 GE==OP_Ge );.
1d151 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 assert( TK_EQ
1d152 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 ==OP_Eq );.
1d153 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d assert( TK_NE==
1d154 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74 OP_Ne );. t
1d155 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1d156 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LT );. test
1d157 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 case( op==TK_LE
1d158 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1d159 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a e( op==TK_GT );.
1d15a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1d15b 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 op==TK_GE );.
1d15c 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1d15d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 =TK_EQ );.
1d15e 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1d15f 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 _NE );. cod
1d160 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 eCompareOperands
1d161 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
1d162 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 pLeft, &r1, ®
1d163 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 Free1,.
1d164 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d165 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
1d166 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 pRight, &r2, &re
1d167 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63 gFree2);. c
1d168 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 odeCompare(pPars
1d169 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
1d16a 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 pExpr->pRight,
1d16b 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 op,.
1d16c 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 69 6e r1, r2, in
1d16d 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 Reg, SQLITE_STOR
1d16e 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 EP2);. test
1d16f 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
1d170 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
1d171 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
1d172 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
1d173 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1d174 54 4b 5f 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 TK_AND:. case
1d175 20 54 4b 5f 4f 52 3a 0a 20 20 20 20 63 61 73 65 TK_OR:. case
1d176 20 54 4b 5f 50 4c 55 53 3a 0a 20 20 20 20 63 61 TK_PLUS:. ca
1d177 73 65 20 54 4b 5f 53 54 41 52 3a 0a 20 20 20 20 se TK_STAR:.
1d178 63 61 73 65 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 case TK_MINUS:.
1d179 20 20 20 63 61 73 65 20 54 4b 5f 52 45 4d 3a 0a case TK_REM:.
1d17a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 41 case TK_BITA
1d17b 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ND:. case TK_
1d17c 42 49 54 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 BITOR:. case
1d17d 54 4b 5f 53 4c 41 53 48 3a 0a 20 20 20 20 63 61 TK_SLASH:. ca
1d17e 73 65 20 54 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 se TK_LSHIFT:.
1d17f 20 20 63 61 73 65 20 54 4b 5f 52 53 48 49 46 54 case TK_RSHIFT
1d180 3a 20 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 : . case TK_C
1d181 4f 4e 43 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 ONCAT: {. a
1d182 73 73 65 72 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f ssert( TK_AND==O
1d183 50 5f 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 P_And );. a
1d184 73 73 65 72 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 ssert( TK_OR==OP
1d185 5f 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73 _Or );. ass
1d186 65 72 74 28 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 ert( TK_PLUS==OP
1d187 5f 41 64 64 20 29 3b 0a 20 20 20 20 20 20 61 73 _Add );. as
1d188 73 65 72 74 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d sert( TK_MINUS==
1d189 4f 50 5f 53 75 62 74 72 61 63 74 20 29 3b 0a 20 OP_Subtract );.
1d18a 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1d18b 52 45 4d 3d 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 REM==OP_Remainde
1d18c 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 r );. asser
1d18d 74 28 20 54 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 t( TK_BITAND==OP
1d18e 5f 42 69 74 41 6e 64 20 29 3b 0a 20 20 20 20 20 _BitAnd );.
1d18f 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4f assert( TK_BITO
1d190 52 3d 3d 4f 50 5f 42 69 74 4f 72 20 29 3b 0a 20 R==OP_BitOr );.
1d191 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1d192 53 4c 41 53 48 3d 3d 4f 50 5f 44 69 76 69 64 65 SLASH==OP_Divide
1d193 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1d194 28 20 54 4b 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f ( TK_LSHIFT==OP_
1d195 53 68 69 66 74 4c 65 66 74 20 29 3b 0a 20 20 20 ShiftLeft );.
1d196 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 52 53 assert( TK_RS
1d197 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 52 69 HIFT==OP_ShiftRi
1d198 67 68 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 ght );. ass
1d199 65 72 74 28 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d ert( TK_CONCAT==
1d19a 4f 50 5f 43 6f 6e 63 61 74 20 29 3b 0a 20 20 20 OP_Concat );.
1d19b 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1d19c 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 20 20 =TK_AND );.
1d19d 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
1d19e 4b 5f 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 65 K_OR );. te
1d19f 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 stcase( op==TK_P
1d1a0 4c 55 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 LUS );. tes
1d1a1 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 tcase( op==TK_MI
1d1a2 4e 55 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 NUS );. tes
1d1a3 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 tcase( op==TK_RE
1d1a4 4d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 M );. testc
1d1a5 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 ase( op==TK_BITA
1d1a6 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ND );. test
1d1a7 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 case( op==TK_BIT
1d1a8 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 OR );. test
1d1a9 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 case( op==TK_SLA
1d1aa 53 48 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 SH );. test
1d1ab 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 case( op==TK_LSH
1d1ac 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 IFT );. tes
1d1ad 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 tcase( op==TK_RS
1d1ae 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74 65 HIFT );. te
1d1af 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 stcase( op==TK_C
1d1b0 4f 4e 43 41 54 20 29 3b 0a 20 20 20 20 20 20 72 ONCAT );. r
1d1b1 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 1 = sqlite3ExprC
1d1b2 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
1d1b3 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 pExpr->pLeft, &r
1d1b4 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 egFree1);.
1d1b5 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r2 = sqlite3Expr
1d1b6 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c CodeTemp(pParse,
1d1b7 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 pExpr->pRight,
1d1b8 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 ®Free2);.
1d1b9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1d1ba 4f 70 33 28 76 2c 20 6f 70 2c 20 72 32 2c 20 72 Op3(v, op, r2, r
1d1bb 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 1, target);.
1d1bc 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
1d1bd 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree1==0 );.
1d1be 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
1d1bf 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee2==0 );.
1d1c0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1d1c1 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a case TK_UMINUS:
1d1c2 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 {. Expr *p
1d1c3 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c Left = pExpr->pL
1d1c4 65 66 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 eft;. asser
1d1c5 74 28 20 70 4c 65 66 74 20 29 3b 0a 20 20 20 20 t( pLeft );.
1d1c6 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d if( pLeft->op=
1d1c7 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 4c 65 =TK_FLOAT || pLe
1d1c8 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 ft->op==TK_INTEG
1d1c9 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 ER ){. if
1d1ca 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f ( pLeft->op==TK_
1d1cb 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 FLOAT ){.
1d1cc 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 28 codeReal(v, (
1d1cd 63 68 61 72 2a 29 70 4c 65 66 74 2d 3e 74 6f 6b char*)pLeft->tok
1d1ce 65 6e 2e 7a 2c 20 70 4c 65 66 74 2d 3e 74 6f 6b en.z, pLeft->tok
1d1cf 65 6e 2e 6e 2c 20 31 2c 20 74 61 72 67 65 74 29 en.n, 1, target)
1d1d0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
1d1d1 0a 20 20 20 20 20 20 20 20 20 20 63 6f 64 65 49 . codeI
1d1d2 6e 74 65 67 65 72 28 76 2c 20 70 4c 65 66 74 2c nteger(v, pLeft,
1d1d3 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 1, target);.
1d1d4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1d1d5 73 65 7b 0a 20 20 20 20 20 20 20 20 72 65 67 46 se{. regF
1d1d6 72 65 65 31 20 3d 20 72 31 20 3d 20 73 71 6c 69 ree1 = r1 = sqli
1d1d7 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
1d1d8 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 arse);. s
1d1d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1d1da 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
1d1db 30 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 0, r1);.
1d1dc 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r2 = sqlite3Expr
1d1dd 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c CodeTemp(pParse,
1d1de 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 pExpr->pLeft, &
1d1df 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 regFree2);.
1d1e0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1d1e1 64 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 dOp3(v, OP_Subtr
1d1e2 61 63 74 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 act, r2, r1, tar
1d1e3 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 74 65 get);. te
1d1e4 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 stcase( regFree2
1d1e5 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ==0 );. }.
1d1e6 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 inReg = tar
1d1e7 67 65 74 3b 0a 20 20 20 20 20 20 62 72 65 61 6b get;. break
1d1e8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1d1e9 20 54 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20 20 TK_BITNOT:.
1d1ea 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 case TK_NOT: {.
1d1eb 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
1d1ec 42 49 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f BITNOT==OP_BitNo
1d1ed 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 t );. asser
1d1ee 74 28 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f t( TK_NOT==OP_No
1d1ef 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 t );. testc
1d1f0 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e ase( op==TK_BITN
1d1f1 4f 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 OT );. test
1d1f2 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 case( op==TK_NOT
1d1f3 20 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 );. inReg
1d1f4 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
1d1f5 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 eTarget(pParse,
1d1f6 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 pExpr->pLeft, ta
1d1f7 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65 73 rget);. tes
1d1f8 74 63 61 73 65 28 20 69 6e 52 65 67 3d 3d 74 61 tcase( inReg==ta
1d1f9 72 67 65 74 20 29 3b 0a 20 20 20 20 20 20 74 65 rget );. te
1d1fa 73 74 63 61 73 65 28 20 75 73 65 64 41 73 43 6f stcase( usedAsCo
1d1fb 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65 lumnCache(pParse
1d1fc 2c 20 69 6e 52 65 67 2c 20 69 6e 52 65 67 29 20 , inReg, inReg)
1d1fd 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d );. inReg =
1d1fe 20 73 71 6c 69 74 65 33 45 78 70 72 57 72 69 74 sqlite3ExprWrit
1d1ff 61 62 6c 65 52 65 67 69 73 74 65 72 28 70 50 61 ableRegister(pPa
1d200 72 73 65 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 rse, inReg, targ
1d201 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 et);. sqlit
1d202 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
1d203 6f 70 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 op, inReg);.
1d204 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1d205 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c case TK_ISNUL
1d206 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e L:. case TK_N
1d207 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 OTNULL: {.
1d208 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 int addr;.
1d209 61 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c assert( TK_ISNUL
1d20a 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a L==OP_IsNull );.
1d20b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1d20c 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 _NOTNULL==OP_Not
1d20d 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 Null );. te
1d20e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 stcase( op==TK_I
1d20f 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 SNULL );. t
1d210 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1d211 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 NOTNULL );.
1d212 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1d213 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
1d214 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 1, target);.
1d215 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 r1 = sqlite3
1d216 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 ExprCodeTemp(pPa
1d217 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
1d218 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 t, ®Free1);.
1d219 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
1d21a 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
1d21b 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 addr = sqlit
1d21c 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
1d21d 6f 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 op, r1);. s
1d21e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1d21f 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 (v, OP_AddImm, t
1d220 61 72 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 arget, -1);.
1d221 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
1d222 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a pHere(v, addr);.
1d223 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1d224 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 }. case TK_A
1d225 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 GG_FUNCTION: {.
1d226 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 AggInfo *pI
1d227 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 nfo = pExpr->pAg
1d228 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 gInfo;. if(
1d229 20 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 pInfo==0 ){.
1d22a 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1d22b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 rMsg(pParse, "mi
1d22c 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 suse of aggregat
1d22d 65 3a 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20 e: %T",.
1d22e 20 20 20 20 26 70 45 78 70 72 2d 3e 73 70 61 6e &pExpr->span
1d22f 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1d230 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 inReg =
1d231 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78 pInfo->aFunc[pEx
1d232 70 72 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0a pr->iAgg].iMem;.
1d233 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
1d234 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1d235 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e ase TK_CONST_FUN
1d236 43 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 C:. case TK_F
1d237 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 UNCTION: {.
1d238 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
1d239 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b = pExpr->pList;
1d23a 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72 . int nExpr
1d23b 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 = pList ? pList
1d23c 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a 20 20 20 ->nExpr : 0;.
1d23d 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 FuncDef *pDef
1d23e 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b ;. int nId;
1d23f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
1d240 72 20 2a 7a 49 64 3b 0a 20 20 20 20 20 20 69 6e r *zId;. in
1d241 74 20 63 6f 6e 73 74 4d 61 73 6b 20 3d 20 30 3b t constMask = 0;
1d242 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
1d243 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 u8 enc = ENC
1d244 28 64 62 29 3b 0a 20 20 20 20 20 20 43 6f 6c 6c (db);. Coll
1d245 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a Seq *pColl = 0;.
1d246 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1d247 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 op==TK_CONST_FU
1d248 4e 43 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 NC );. test
1d249 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 46 55 4e case( op==TK_FUN
1d24a 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 7a CTION );. z
1d24b 49 64 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 Id = (char*)pExp
1d24c 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 r->token.z;.
1d24d 20 20 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74 nId = pExpr->t
1d24e 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44 oken.n;. pD
1d24f 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 ef = sqlite3Find
1d250 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49 64 Function(db, zId
1d251 2c 20 6e 49 64 2c 20 6e 45 78 70 72 2c 20 65 6e , nId, nExpr, en
1d252 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 c, 0);. ass
1d253 65 72 74 28 20 70 44 65 66 21 3d 30 20 29 3b 0a ert( pDef!=0 );.
1d254 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 20 if( pList
1d255 29 7b 0a 20 20 20 20 20 20 20 20 6e 45 78 70 72 ){. nExpr
1d256 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b = pList->nExpr;
1d257 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 . r1 = sq
1d258 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 lite3GetTempRang
1d259 65 28 70 50 61 72 73 65 2c 20 6e 45 78 70 72 29 e(pParse, nExpr)
1d25a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1d25b 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 3ExprCodeExprLis
1d25c 74 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c t(pParse, pList,
1d25d 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d r1, 1);. }
1d25e 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 45 else{. nE
1d25f 78 70 72 20 3d 20 72 31 20 3d 20 30 3b 0a 20 20 xpr = r1 = 0;.
1d260 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
1d261 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1d262 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 LTABLE. /*
1d263 50 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 Possibly overloa
1d264 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 d the function i
1d265 66 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 f the first argu
1d266 6d 65 6e 74 20 69 73 0a 20 20 20 20 20 20 2a 2a ment is. **
1d267 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
1d268 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a column.. *
1d269 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 *. ** For i
1d26a 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 nfix functions (
1d26b 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 LIKE, GLOB, REGE
1d26c 58 50 2c 20 61 6e 64 20 4d 41 54 43 48 29 20 75 XP, and MATCH) u
1d26d 73 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 se the. **
1d26e 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c second argument,
1d26f 20 6e 6f 74 20 74 68 65 20 66 69 72 73 74 2c 20 not the first,
1d270 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 as the argument
1d271 74 6f 20 74 65 73 74 20 74 6f 0a 20 20 20 20 20 to test to.
1d272 20 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 ** see if it is
1d273 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 a column in a v
1d274 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 irtual table. T
1d275 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 63 61 his is done beca
1d276 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 use. ** the
1d277 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 left operand of
1d278 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 infix functions
1d279 20 28 74 68 65 20 6f 70 65 72 61 6e 64 20 77 65 (the operand we
1d27a 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a want to. *
1d27b 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f * control overlo
1d27c 61 64 69 6e 67 29 20 65 6e 64 73 20 75 70 20 61 ading) ends up a
1d27d 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 s the second arg
1d27e 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 ument to the.
1d27f 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 ** function.
1d280 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 The expression
1d281 22 41 20 67 6c 6f 62 20 42 22 20 69 73 20 65 71 "A glob B" is eq
1d282 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 uivalent to .
1d283 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 ** "glob(B,A)
1d284 2e 20 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73 . We want to us
1d285 65 20 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c e the A in "A gl
1d286 6f 62 20 42 22 20 74 6f 20 74 65 73 74 0a 20 20 ob B" to test.
1d287 20 20 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 ** for funct
1d288 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e ion overloading.
1d289 20 20 42 75 74 20 77 65 20 75 73 65 20 74 68 65 But we use the
1d28a 20 42 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 B term in "glob
1d28b 28 42 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f (B,A)".. */
1d28c 0a 20 20 20 20 20 20 69 66 28 20 6e 45 78 70 72 . if( nExpr
1d28d 3e 3d 32 20 26 26 20 28 70 45 78 70 72 2d 3e 66 >=2 && (pExpr->f
1d28e 6c 61 67 73 20 26 20 45 50 5f 49 6e 66 69 78 46 lags & EP_InfixF
1d28f 75 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 unc) ){.
1d290 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 pDef = sqlite3Vt
1d291 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 abOverloadFuncti
1d292 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e 45 78 on(db, pDef, nEx
1d293 70 72 2c 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e pr, pList->a[1].
1d294 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 pExpr);. }e
1d295 6c 73 65 20 69 66 28 20 6e 45 78 70 72 3e 30 20 lse if( nExpr>0
1d296 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 ){. pDef
1d297 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 = sqlite3VtabOve
1d298 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 rloadFunction(db
1d299 2c 20 70 44 65 66 2c 20 6e 45 78 70 72 2c 20 70 , pDef, nExpr, p
1d29a 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 List->a[0].pExpr
1d29b 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 );. }.#endi
1d29c 66 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b f. for(i=0;
1d29d 20 69 3c 6e 45 78 70 72 20 26 26 20 69 3c 33 32 i<nExpr && i<32
1d29e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
1d29f 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 if( sqlite3ExprI
1d2a0 73 43 6f 6e 73 74 61 6e 74 28 70 4c 69 73 74 2d sConstant(pList-
1d2a1 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a >a[i].pExpr) ){.
1d2a2 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 4d constM
1d2a3 61 73 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 ask |= (1<<i);.
1d2a4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1d2a5 20 69 66 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 if( (pDef->flag
1d2a6 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f s & SQLITE_FUNC_
1d2a7 4e 45 45 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 NEEDCOLL)!=0 &&
1d2a8 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 !pColl ){.
1d2a9 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 pColl = sqli
1d2aa 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 te3ExprCollSeq(p
1d2ab 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b Parse, pList->a[
1d2ac 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 i].pExpr);.
1d2ad 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1d2ae 20 20 20 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 if( pDef->fla
1d2af 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 gs & SQLITE_FUNC
1d2b0 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 _NEEDCOLL ){.
1d2b1 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 if( !pColl
1d2b2 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 ) pColl = db->pD
1d2b3 66 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 fltColl; .
1d2b4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1d2b5 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 Op4(v, OP_CollSe
1d2b6 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 q, 0, 0, 0, (cha
1d2b7 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f r *)pColl, P4_CO
1d2b8 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a LLSEQ);. }.
1d2b9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d2ba 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 eAddOp4(v, OP_Fu
1d2bb 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 nction, constMas
1d2bc 6b 2c 20 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 k, r1, target,.
1d2bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d2be 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 44 (char*)pD
1d2bf 65 66 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b ef, P4_FUNCDEF);
1d2c0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d2c1 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 6e 45 beChangeP5(v, nE
1d2c2 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 xpr);. if(
1d2c3 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 nExpr ){.
1d2c4 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
1d2c5 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c empRange(pParse,
1d2c6 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a 20 20 20 r1, nExpr);.
1d2c7 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
1d2c8 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e e3ExprCacheAffin
1d2c9 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 ityChange(pParse
1d2ca 2c 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a 20 20 , r1, nExpr);.
1d2cb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1d2cc 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1d2cd 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 OMIT_SUBQUERY.
1d2ce 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 case TK_EXISTS
1d2cf 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 :. case TK_SE
1d2d0 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 LECT: {. te
1d2d1 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 stcase( op==TK_E
1d2d2 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74 XISTS );. t
1d2d3 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1d2d4 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 SELECT );.
1d2d5 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 if( pExpr->iColu
1d2d6 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 mn==0 ){.
1d2d7 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 sqlite3CodeSubs
1d2d8 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 45 elect(pParse, pE
1d2d9 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 xpr, 0, 0);.
1d2da 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 67 20 }. inReg
1d2db 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e = pExpr->iColumn
1d2dc 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1d2dd 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
1d2de 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 _IN: {. int
1d2df 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 30 3b 0a rNotFound = 0;.
1d2e0 20 20 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61 int rMayHa
1d2e1 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 veNull = 0;.
1d2e2 20 20 69 6e 74 20 6a 32 2c 20 6a 33 2c 20 6a 34 int j2, j3, j4
1d2e3 2c 20 6a 35 3b 0a 20 20 20 20 20 20 63 68 61 72 , j5;. char
1d2e4 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 affinity;.
1d2e5 20 69 6e 74 20 65 54 79 70 65 3b 0a 0a 20 20 20 int eType;..
1d2e6 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 VdbeNoopComme
1d2e7 6e 74 28 28 76 2c 20 22 62 65 67 69 6e 20 49 4e nt((v, "begin IN
1d2e8 20 65 78 70 72 20 72 25 64 22 2c 20 74 61 72 67 expr r%d", targ
1d2e9 65 74 29 29 3b 0a 20 20 20 20 20 20 65 54 79 70 et));. eTyp
1d2ea 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 e = sqlite3FindI
1d2eb 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 nIndex(pParse, p
1d2ec 45 78 70 72 2c 20 26 72 4d 61 79 48 61 76 65 4e Expr, &rMayHaveN
1d2ed 75 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ull);. if(
1d2ee 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 7b 0a rMayHaveNull ){.
1d2ef 20 20 20 20 20 20 20 20 72 4e 6f 74 46 6f 75 6e rNotFoun
1d2f0 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d d = ++pParse->nM
1d2f1 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 em;. }..
1d2f2 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 /* Figure out
1d2f3 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f the affinity to
1d2f4 20 75 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 use to create a
1d2f5 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 key from the re
1d2f6 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f sults. ** o
1d2f7 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
1d2f8 2e 20 61 66 66 69 6e 69 74 79 53 74 72 20 73 74 . affinityStr st
1d2f9 6f 72 65 73 20 61 20 73 74 61 74 69 63 20 73 74 ores a static st
1d2fa 72 69 6e 67 20 73 75 69 74 61 62 6c 65 20 66 6f ring suitable fo
1d2fb 72 0a 20 20 20 20 20 20 2a 2a 20 50 34 20 6f 66 r. ** P4 of
1d2fc 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2e 0a OP_MakeRecord..
1d2fd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 */. a
1d2fe 66 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 ffinity = compar
1d2ff 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 isonAffinity(pEx
1d300 70 72 29 3b 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 pr);... /*
1d301 43 6f 64 65 20 74 68 65 20 3c 65 78 70 72 3e 20 Code the <expr>
1d302 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 4e 20 from "<expr> IN
1d303 28 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65 6d 70 (...)". The temp
1d304 6f 72 61 72 79 20 74 61 62 6c 65 0a 20 20 20 20 orary table.
1d305 20 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 ** pExpr->iTab
1d306 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
1d307 76 61 6c 75 65 73 20 74 68 61 74 20 6d 61 6b 65 values that make
1d308 20 75 70 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 up the (...) se
1d309 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 t.. */.
1d30a 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c pParse->disabl
1d30b 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 eColCache++;.
1d30c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
1d30d 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 de(pParse, pExpr
1d30e 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 ->pLeft, target)
1d30f 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e ;. pParse->
1d310 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d disableColCache-
1d311 2d 3b 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 71 -;. j2 = sq
1d312 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
1d313 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 74 61 v, OP_IsNull, ta
1d314 72 67 65 74 29 3b 0a 20 20 20 20 20 20 69 66 28 rget);. if(
1d315 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 eType==IN_INDEX
1d316 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 _ROWID ){.
1d317 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 j3 = sqlite3Vd
1d318 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d beAddOp1(v, OP_M
1d319 75 73 74 42 65 49 6e 74 2c 20 74 61 72 67 65 74 ustBeInt, target
1d31a 29 3b 0a 20 20 20 20 20 20 20 20 6a 34 20 3d 20 );. j4 =
1d31b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1d31c 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 3(v, OP_NotExist
1d31d 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 s, pExpr->iTable
1d31e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 0, target);.
1d31f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d320 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e eAddOp2(v, OP_In
1d321 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 teger, 1, target
1d322 29 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 );. j5 =
1d323 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1d324 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 0(v, OP_Goto);.
1d325 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1d326 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 beJumpHere(v, j3
1d327 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1d328 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
1d329 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 73 , j4);. s
1d32a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1d32b 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
1d32c 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 0, target);.
1d32d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1d32e 20 72 32 20 3d 20 72 65 67 46 72 65 65 32 20 3d r2 = regFree2 =
1d32f 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
1d330 65 67 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 eg(pParse);..
1d331 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 /* Create a
1d332 20 72 65 63 6f 72 64 20 61 6e 64 20 74 65 73 74 record and test
1d333 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65 72 73 for set members
1d334 68 69 70 2e 20 49 66 20 74 68 65 20 73 65 74 20 hip. If the set
1d335 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 20 contains.
1d336 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 2c 20 74 ** the value, t
1d337 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 hen jump to the
1d338 65 6e 64 20 6f 66 20 74 68 65 20 74 65 73 74 20 end of the test
1d339 63 6f 64 65 2e 20 54 68 65 20 74 61 72 67 65 74 code. The target
1d33a 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 67 69 . ** regi
1d33b 73 74 65 72 20 73 74 69 6c 6c 20 63 6f 6e 74 61 ster still conta
1d33c 69 6e 73 20 74 68 65 20 74 72 75 65 20 28 31 29 ins the true (1)
1d33d 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 74 value written t
1d33e 6f 20 69 74 20 65 61 72 6c 69 65 72 2e 0a 20 20 o it earlier..
1d33f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1d340 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1d341 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 p4(v, OP_MakeRec
1d342 6f 72 64 2c 20 74 61 72 67 65 74 2c 20 31 2c 20 ord, target, 1,
1d343 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 r2, &affinity, 1
1d344 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1d345 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1d346 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 OP_Integer, 1, t
1d347 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 arget);.
1d348 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 j5 = sqlite3Vdbe
1d349 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 AddOp3(v, OP_Fou
1d34a 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c nd, pExpr->iTabl
1d34b 65 2c 20 30 2c 20 72 32 29 3b 0a 0a 20 20 20 20 e, 0, r2);..
1d34c 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 /* If the se
1d34d 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 t membership tes
1d34e 74 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 t fails, then th
1d34f 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 e result of the
1d350 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 78 20 49 . ** "x I
1d351 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73 N (...)" express
1d352 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 74 68 ion must be eith
1d353 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 er 0 or NULL. If
1d354 20 74 68 65 20 73 65 74 0a 20 20 20 20 20 20 20 the set.
1d355 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 ** contains no
1d356 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 NULL values, the
1d357 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 n the result is
1d358 30 2e 20 49 66 20 74 68 65 20 73 65 74 20 0a 20 0. If the set .
1d359 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 ** contai
1d35a 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 4e ns one or more N
1d35b 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e ULL values, then
1d35c 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 the result of t
1d35d 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 78 he. ** ex
1d35e 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6c 73 6f pression is also
1d35f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a NULL.. *
1d360 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 4e /. if( rN
1d361 6f 74 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a 20 20 otFound==0 ){.
1d362 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
1d363 62 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 69 branch runs if i
1d364 74 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 63 6f t is known at co
1d365 6d 70 69 6c 65 20 74 69 6d 65 20 28 6e 6f 77 29 mpile time (now)
1d366 20 74 68 61 74 20 0a 20 20 20 20 20 20 20 20 20 that .
1d367 20 2a 2a 20 74 68 65 20 73 65 74 20 63 6f 6e 74 ** the set cont
1d368 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c ains no NULL val
1d369 75 65 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e ues. This happen
1d36a 73 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a s as the result.
1d36b 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 ** of
1d36c 61 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e a "NOT NULL" con
1d36d 73 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 64 straint in the d
1d36e 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 atabase schema.
1d36f 4e 6f 20 6e 65 65 64 0a 20 20 20 20 20 20 20 20 No need.
1d370 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 ** to test the
1d371 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 data structure
1d372 61 74 20 72 75 6e 74 69 6d 65 20 69 6e 20 74 68 at runtime in th
1d373 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 is case..
1d374 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
1d375 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1d376 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
1d377 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 0, target);.
1d378 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d379 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c /* This bl
1d37a 6f 63 6b 20 70 6f 70 75 6c 61 74 65 73 20 74 68 ock populates th
1d37b 65 20 72 4e 6f 74 46 6f 75 6e 64 20 72 65 67 69 e rNotFound regi
1d37c 73 74 65 72 20 77 69 74 68 20 65 69 74 68 65 72 ster with either
1d37d 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20 20 NULL.
1d37e 2a 2a 20 6f 72 20 30 20 28 61 6e 20 69 6e 74 65 ** or 0 (an inte
1d37f 67 65 72 20 76 61 6c 75 65 29 2e 20 49 66 20 74 ger value). If t
1d380 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 he data structur
1d381 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 0a 20 e contains one.
1d382 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d ** or m
1d383 6f 72 65 20 4e 55 4c 4c 73 2c 20 74 68 65 6e 20 ore NULLs, then
1d384 73 65 74 20 72 4e 6f 74 46 6f 75 6e 64 20 74 6f set rNotFound to
1d385 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 NULL. Otherwise
1d386 2c 20 73 65 74 20 69 74 0a 20 20 20 20 20 20 20 , set it.
1d387 20 20 20 2a 2a 20 74 6f 20 30 2e 20 49 66 20 72 ** to 0. If r
1d388 65 67 69 73 74 65 72 20 72 4d 61 79 48 61 76 65 egister rMayHave
1d389 4e 75 6c 6c 20 69 73 20 61 6c 72 65 61 64 79 20 Null is already
1d38a 73 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c 75 set to some valu
1d38b 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f e. ** o
1d38c 74 68 65 72 20 74 68 61 6e 20 4e 55 4c 4c 2c 20 ther than NULL,
1d38d 74 68 65 6e 20 74 68 65 20 74 65 73 74 20 68 61 then the test ha
1d38e 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 s already been r
1d38f 75 6e 20 61 6e 64 20 0a 20 20 20 20 20 20 20 20 un and .
1d390 20 20 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64 20 69 ** rNotFound i
1d391 73 20 61 6c 72 65 61 64 79 20 70 6f 70 75 6c 61 s already popula
1d392 74 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a ted.. *
1d393 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 /. stat
1d394 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6e 75 ic const char nu
1d395 6c 6c 52 65 63 6f 72 64 5b 5d 20 3d 20 7b 20 30 llRecord[] = { 0
1d396 78 30 32 2c 20 30 78 30 30 20 7d 3b 0a 20 20 20 x02, 0x00 };.
1d397 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 j3 = sqli
1d398 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
1d399 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 4d 61 OP_NotNull, rMa
1d39a 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 yHaveNull);.
1d39b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d39c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 eAddOp2(v, OP_Nu
1d39d 6c 6c 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 ll, 0, rNotFound
1d39e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
1d39f 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
1d3a0 2c 20 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20 72 4d , OP_Blob, 2, rM
1d3a1 61 79 48 61 76 65 4e 75 6c 6c 2c 20 30 2c 20 0a ayHaveNull, 0, .
1d3a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d3a3 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 75 6c nul
1d3a4 6c 52 65 63 6f 72 64 2c 20 50 34 5f 53 54 41 54 lRecord, P4_STAT
1d3a5 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a IC);. j
1d3a6 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 4 = sqlite3VdbeA
1d3a7 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e ddOp3(v, OP_Foun
1d3a8 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 d, pExpr->iTable
1d3a9 2c 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c , 0, rMayHaveNul
1d3aa 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 l);. sq
1d3ab 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1d3ac 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 v, OP_Integer, 0
1d3ad 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 , rNotFound);.
1d3ae 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1d3af 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a dbeJumpHere(v, j
1d3b0 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 4);. sq
1d3b1 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
1d3b2 65 28 76 2c 20 6a 33 29 3b 0a 0a 20 20 20 20 20 e(v, j3);..
1d3b3 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 /* Copy the
1d3b4 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 value of regist
1d3b5 65 72 20 72 4e 6f 74 46 6f 75 6e 64 20 28 77 68 er rNotFound (wh
1d3b6 69 63 68 20 69 73 20 65 69 74 68 65 72 20 4e 55 ich is either NU
1d3b7 4c 4c 20 6f 72 20 30 29 0a 20 20 20 20 20 20 20 LL or 0).
1d3b8 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 74 ** into the t
1d3b9 61 72 67 65 74 20 72 65 67 69 73 74 65 72 2e 20 arget register.
1d3ba 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 This will be the
1d3bb 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 result of the.
1d3bc 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 ** expr
1d3bd 65 73 73 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 ession..
1d3be 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 */. s
1d3bf 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1d3c0 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 4e 6f (v, OP_Copy, rNo
1d3c1 74 46 6f 75 6e 64 2c 20 74 61 72 67 65 74 29 3b tFound, target);
1d3c2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1d3c3 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1d3c4 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
1d3c5 6a 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 j2);. sqlit
1d3c6 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
1d3c7 2c 20 6a 35 29 3b 0a 20 20 20 20 20 20 56 64 62 , j5);. Vdb
1d3c8 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e eComment((v, "en
1d3c9 64 20 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20 d IN expr r%d",
1d3ca 74 61 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20 target));.
1d3cb 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e break;. }.#en
1d3cc 64 69 66 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a dif. /*. *
1d3cd 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 * x BETWEEN y
1d3ce 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 AND z. **.
1d3cf 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75 ** This is equ
1d3d0 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a ivalent to. *
1d3d1 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 *. ** x>=y
1d3d2 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a AND x<=z. **
1d3d3 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74 6f . ** X is sto
1d3d4 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c red in pExpr->pL
1d3d5 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 eft.. ** Y is
1d3d6 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 stored in pExpr
1d3d7 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 ->pList->a[0].pE
1d3d8 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73 xpr.. ** Z is
1d3d9 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 stored in pExpr
1d3da 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 ->pList->a[1].pE
1d3db 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 xpr.. */.
1d3dc 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a case TK_BETWEEN:
1d3dd 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 {. Expr *p
1d3de 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c Left = pExpr->pL
1d3df 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 eft;. struc
1d3e0 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
1d3e1 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72 2d *pLItem = pExpr-
1d3e2 3e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 >pList->a;.
1d3e3 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 Expr *pRight =
1d3e4 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a pLItem->pExpr;..
1d3e5 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 codeCompar
1d3e6 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 eOperands(pParse
1d3e7 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 , pLeft, &r1, &r
1d3e8 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 egFree1,.
1d3e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d3ea 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 pRigh
1d3eb 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 t, &r2, ®Free
1d3ec 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 2);. testca
1d3ed 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
1d3ee 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1d3ef 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 e( regFree2==0 )
1d3f0 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73 71 6c ;. r3 = sql
1d3f1 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
1d3f2 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72 34 Parse);. r4
1d3f3 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
1d3f4 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
1d3f5 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 codeCompare(
1d3f6 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 pParse, pLeft, p
1d3f7 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a 20 20 Right, OP_Ge,.
1d3f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d3f9 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51 4c 49 r1, r2, r3, SQLI
1d3fa 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 TE_STOREP2);.
1d3fb 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20 20 pLItem++;.
1d3fc 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c 49 74 pRight = pLIt
1d3fd 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 em->pExpr;.
1d3fe 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
1d3ff 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 empReg(pParse, r
1d400 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 egFree2);.
1d401 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r2 = sqlite3Expr
1d402 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c CodeTemp(pParse,
1d403 20 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 pRight, ®Fre
1d404 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 e2);. testc
1d405 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
1d406 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f );. codeCo
1d407 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c mpare(pParse, pL
1d408 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f eft, pRight, OP_
1d409 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34 2c 20 Le, r1, r2, r4,
1d40a 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b SQLITE_STOREP2);
1d40b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d40c 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 beAddOp3(v, OP_A
1d40d 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61 72 67 nd, r3, r4, targ
1d40e 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 et);. sqlit
1d40f 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
1d410 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a 20 20 (pParse, r3);.
1d411 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 sqlite3Relea
1d412 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 seTempReg(pParse
1d413 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62 72 65 , r4);. bre
1d414 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
1d415 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 se TK_UPLUS: {.
1d416 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c inReg = sql
1d417 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 ite3ExprCodeTarg
1d418 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 et(pParse, pExpr
1d419 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 ->pLeft, target)
1d41a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1d41b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 }.. /*.
1d41c 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20 20 20 ** Form A:.
1d41d 2a 2a 20 20 20 43 41 53 45 20 78 20 57 48 45 4e ** CASE x WHEN
1d41e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e e1 THEN r1 WHEN
1d41f 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20 e2 THEN r2 ...
1d420 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20 WHEN eN THEN rN
1d421 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a ELSE y END. *
1d422 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 42 3a *. ** Form B:
1d423 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 . ** CASE W
1d424 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 HEN e1 THEN r1 W
1d425 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e HEN e2 THEN r2 .
1d426 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 .. WHEN eN THEN
1d427 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 rN ELSE y END.
1d428 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d **. ** Form
1d429 20 41 20 69 73 20 63 61 6e 20 62 65 20 74 72 61 A is can be tra
1d42a 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 74 68 nsformed into th
1d42b 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 e equivalent for
1d42c 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a m B as follows:.
1d42d 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 48 ** CASE WH
1d42e 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 31 20 EN x=e1 THEN r1
1d42f 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 20 72 WHEN x=e2 THEN r
1d430 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20 2 .... **
1d431 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20 54 48 WHEN x=eN TH
1d432 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 EN rN ELSE y END
1d433 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 . **. ** X
1d434 20 28 69 66 20 69 74 20 65 78 69 73 74 73 29 20 (if it exists)
1d435 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65 is in pExpr->pLe
1d436 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 ft.. ** Y is
1d437 69 6e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 in pExpr->pRight
1d438 2e 20 20 54 68 65 20 59 20 69 73 20 61 6c 73 6f . The Y is also
1d439 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 74 optional. If t
1d43a 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20 2a here is no. *
1d43b 2a 20 45 4c 53 45 20 63 6c 61 75 73 65 20 61 6e * ELSE clause an
1d43c 64 20 6e 6f 20 6f 74 68 65 72 20 74 65 72 6d 20 d no other term
1d43d 6d 61 74 63 68 65 73 2c 20 74 68 65 6e 20 74 68 matches, then th
1d43e 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a e result of the.
1d43f 20 20 20 20 2a 2a 20 65 78 70 72 73 73 69 6f 6e ** exprssion
1d440 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a is NULL.. **
1d441 20 45 69 20 69 73 20 69 6e 20 70 45 78 70 72 2d Ei is in pExpr-
1d442 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d 20 61 >pList->a[i*2] a
1d443 6e 64 20 52 69 20 69 73 20 70 45 78 70 72 2d 3e nd Ri is pExpr->
1d444 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31 5d 2e pList->a[i*2+1].
1d445 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1d446 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
1d447 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 expression is t
1d448 68 65 20 52 69 20 66 6f 72 20 74 68 65 20 66 69 he Ri for the fi
1d449 72 73 74 20 6d 61 74 63 68 69 6e 67 20 45 69 2c rst matching Ei,
1d44a 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 . ** or if th
1d44b 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 69 ere is no matchi
1d44c 6e 67 20 45 69 2c 20 74 68 65 20 45 4c 53 45 20 ng Ei, the ELSE
1d44d 74 65 72 6d 20 59 2c 20 6f 72 20 69 66 20 74 68 term Y, or if th
1d44e 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f ere is. ** no
1d44f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55 4c 4c ELSE term, NULL
1d450 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 .. */. cas
1d451 65 20 54 4b 5f 43 41 53 45 3a 20 7b 0a 20 20 20 e TK_CASE: {.
1d452 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 65 6c 3b int endLabel;
1d453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d454 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 /* GOTO lab
1d455 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 20 43 41 el for end of CA
1d456 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20 20 20 20 SE stmt */.
1d457 20 69 6e 74 20 6e 65 78 74 43 61 73 65 3b 20 20 int nextCase;
1d458 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d459 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 6c /* GOTO label
1d45a 20 66 6f 72 20 6e 65 78 74 20 57 48 45 4e 20 63 for next WHEN c
1d45b 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 69 lause */. i
1d45c 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 nt nExpr;
1d45d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d45e 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72 20 6f 66 /* 2x number of
1d45f 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 WHEN terms */.
1d460 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 int i;
1d461 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d462 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
1d463 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 ounter */.
1d464 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
1d465 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1d466 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 57 48 45 /* List of WHE
1d467 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 N terms */.
1d468 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
1d469 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65 6c 65 6d _item *aListelem
1d46a 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 57 ; /* Array of W
1d46b 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 HEN terms */.
1d46c 20 20 20 45 78 70 72 20 6f 70 43 6f 6d 70 61 72 Expr opCompar
1d46d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1d46e 20 20 20 20 20 2f 2a 20 54 68 65 20 58 3d 3d 45 /* The X==E
1d46f 69 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a i expression */.
1d470 20 20 20 20 20 20 45 78 70 72 20 63 61 63 68 65 Expr cache
1d471 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 X;
1d472 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 /* Cache
1d473 64 20 65 78 70 72 65 73 73 69 6f 6e 20 58 20 2a d expression X *
1d474 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 /. Expr *pX
1d475 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1d476 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1d477 20 58 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f X expression */
1d478 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 54 65 . Expr *pTe
1d479 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 st;
1d47a 20 20 20 20 20 20 20 20 20 2f 2a 20 58 3d 3d 45 /* X==E
1d47b 69 20 28 66 6f 72 6d 20 41 29 20 6f 72 20 6a 75 i (form A) or ju
1d47c 73 74 20 45 69 20 28 66 6f 72 6d 20 42 29 20 2a st Ei (form B) *
1d47d 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 /.. assert(
1d47e 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 3b 0a 20 pExpr->pList);.
1d47f 20 20 20 20 20 61 73 73 65 72 74 28 28 70 45 78 assert((pEx
1d480 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 pr->pList->nExpr
1d481 20 25 20 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20 % 2) == 0);.
1d482 20 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d assert(pExpr-
1d483 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3e 20 >pList->nExpr >
1d484 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 73 74 0);. pEList
1d485 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b = pExpr->pList;
1d486 0a 20 20 20 20 20 20 61 4c 69 73 74 65 6c 65 6d . aListelem
1d487 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 = pEList->a;.
1d488 20 20 20 20 6e 45 78 70 72 20 3d 20 70 45 4c 69 nExpr = pELi
1d489 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 st->nExpr;.
1d48a 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69 endLabel = sqli
1d48b 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c te3VdbeMakeLabel
1d48c 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 (v);. if( (
1d48d 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 pX = pExpr->pLef
1d48e 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 t)!=0 ){.
1d48f 20 63 61 63 68 65 58 20 3d 20 2a 70 58 3b 0a 20 cacheX = *pX;.
1d490 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
1d491 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 pX->op==TK_COLU
1d492 4d 4e 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b MN || pX->op==TK
1d493 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 _REGISTER );.
1d494 20 20 20 20 20 63 61 63 68 65 58 2e 69 54 61 62 cacheX.iTab
1d495 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 le = sqlite3Expr
1d496 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c CodeTemp(pParse,
1d497 20 70 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b pX, ®Free1);
1d498 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
1d499 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 e( regFree1==0 )
1d49a 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58 ;. cacheX
1d49b 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 .op = TK_REGISTE
1d49c 52 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d R;. opCom
1d49d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b pare.op = TK_EQ;
1d49e 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 . opCompa
1d49f 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61 63 68 re.pLeft = &cach
1d4a0 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54 65 73 eX;. pTes
1d4a1 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65 3b 0a t = &opCompare;.
1d4a2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 }. pP
1d4a3 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1d4a4 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 66 Cache++;. f
1d4a5 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b or(i=0; i<nExpr;
1d4a6 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20 20 i=i+2){.
1d4a7 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20 20 20 if( pX ){.
1d4a8 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 opCompare.p
1d4a9 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65 Right = aListele
1d4aa 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 m[i].pExpr;.
1d4ab 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1d4ac 20 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69 pTest = aLi
1d4ad 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b stelem[i].pExpr;
1d4ae 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1d4af 20 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71 nextCase = sq
1d4b0 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
1d4b1 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74 el(v);. t
1d4b2 65 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e estcase( pTest->
1d4b3 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c op==TK_COLUMN ||
1d4b4 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 52 pTest->op==TK_R
1d4b5 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 EGISTER );.
1d4b6 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 sqlite3ExprIf
1d4b7 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 False(pParse, pT
1d4b8 65 73 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53 est, nextCase, S
1d4b9 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
1d4ba 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 );. testc
1d4bb 61 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 ase( aListelem[i
1d4bc 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 +1].pExpr->op==T
1d4bd 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 K_COLUMN );.
1d4be 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c testcase( aL
1d4bf 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 istelem[i+1].pEx
1d4c0 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 pr->op==TK_REGIS
1d4c1 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 TER );. s
1d4c2 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
1d4c3 50 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d Parse, aListelem
1d4c4 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72 [i+1].pExpr, tar
1d4c5 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 get);. sq
1d4c6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1d4c7 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 v, OP_Goto, 0, e
1d4c8 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 ndLabel);.
1d4c9 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 sqlite3VdbeRes
1d4ca 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78 olveLabel(v, nex
1d4cb 74 43 61 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a tCase);. }.
1d4cc 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
1d4cd 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 >pRight ){.
1d4ce 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
1d4cf 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 de(pParse, pExpr
1d4d0 2d 3e 70 52 69 67 68 74 2c 20 74 61 72 67 65 74 ->pRight, target
1d4d1 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1d4d2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1d4d3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1d4d4 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 Null, 0, target)
1d4d5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1d4d6 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c sqlite3VdbeResol
1d4d7 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61 veLabel(v, endLa
1d4d8 62 65 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 bel);. asse
1d4d9 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 rt( pParse->disa
1d4da 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b bleColCache>0 );
1d4db 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 . pParse->d
1d4dc 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d isableColCache--
1d4dd 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1d4de 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
1d4df 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1d4e0 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 41 49 . case TK_RAI
1d4e1 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 SE: {. if(
1d4e2 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 !pParse->trigSta
1d4e3 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ck ){. sq
1d4e4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1d4e5 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 arse,.
1d4e6 20 20 20 20 20 20 20 20 20 20 20 20 20 22 52 41 "RA
1d4e7 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79 20 62 ISE() may only b
1d4e8 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 e used within a
1d4e9 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 22 trigger-program"
1d4ea 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
1d4eb 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 n 0;. }.
1d4ec 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 if( pExpr->iC
1d4ed 6f 6c 75 6d 6e 21 3d 4f 45 5f 49 67 6e 6f 72 65 olumn!=OE_Ignore
1d4ee 20 29 7b 0a 20 20 20 20 20 20 20 20 20 61 73 73 ){. ass
1d4ef 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c ert( pExpr->iCol
1d4f0 75 6d 6e 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b umn==OE_Rollback
1d4f1 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ||.
1d4f2 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c pExpr->iCol
1d4f3 75 6d 6e 20 3d 3d 20 4f 45 5f 41 62 6f 72 74 20 umn == OE_Abort
1d4f4 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ||.
1d4f5 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 pExpr->iColu
1d4f6 6d 6e 20 3d 3d 20 4f 45 5f 46 61 69 6c 20 29 3b mn == OE_Fail );
1d4f7 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 . sqlite
1d4f8 33 44 65 71 75 6f 74 65 45 78 70 72 28 64 62 2c 3DequoteExpr(db,
1d4f9 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 pExpr);.
1d4fa 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1d4fb 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 Op4(v, OP_Halt,
1d4fc 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
1d4fd 54 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d T, pExpr->iColum
1d4fe 6e 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 n, 0,.
1d4ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 (c
1d500 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 har*)pExpr->toke
1d501 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 n.z, pExpr->toke
1d502 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c n.n);. } el
1d503 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 61 73 se {. as
1d504 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f sert( pExpr->iCo
1d505 6c 75 6d 6e 20 3d 3d 20 4f 45 5f 49 67 6e 6f 72 lumn == OE_Ignor
1d506 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 e );. sq
1d507 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1d508 76 2c 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 v, OP_ContextPop
1d509 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 , 0, 0);.
1d50a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1d50b 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 Op2(v, OP_Goto,
1d50c 30 2c 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 0, pParse->trigS
1d50d 74 61 63 6b 2d 3e 69 67 6e 6f 72 65 4a 75 6d 70 tack->ignoreJump
1d50e 29 3b 0a 20 20 20 20 20 20 20 20 20 56 64 62 65 );. Vdbe
1d50f 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 61 69 Comment((v, "rai
1d510 73 65 28 49 47 4e 4f 52 45 29 22 29 29 3b 0a 20 se(IGNORE)"));.
1d511 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
1d512 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ak;. }.#endif
1d513 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 . }. sqlite3Re
1d514 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
1d515 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a rse, regFree1);.
1d516 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
1d517 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
1d518 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 65 74 regFree2);. ret
1d519 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a urn inReg;.}../*
1d51a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
1d51b 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6e e to evaluate an
1d51c 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 expression and
1d51d 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
1d51e 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 69 s.** into a regi
1d51f 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68 ster. Return th
1d520 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 e register numbe
1d521 72 20 77 68 65 72 65 20 74 68 65 20 72 65 73 75 r where the resu
1d522 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 lts.** are store
1d523 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
1d524 72 65 67 69 73 74 65 72 20 69 73 20 61 20 74 65 register is a te
1d525 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 mporary register
1d526 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 65 61 that can be dea
1d527 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68 65 llocated,.** the
1d528 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 6d 62 n write its numb
1d529 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20 20 er into *pReg.
1d52a 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 72 65 If the result re
1d52b 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a 2a gister is not.**
1d52c 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 74 68 a temporary, th
1d52d 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 6f 20 en set *pReg to
1d52e 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f zero..*/.SQLITE_
1d52f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1d530 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
1d531 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
1d532 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
1d533 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 72 31 *pReg){. int r1
1d534 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
1d535 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
1d536 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 int r2 = sqlite3
1d537 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 ExprCodeTarget(p
1d538 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72 31 Parse, pExpr, r1
1d539 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 31 20 );. if( r2==r1
1d53a 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 72 ){. *pReg = r
1d53b 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
1d53c 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
1d53d 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 mpReg(pParse, r1
1d53e 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 30 );. *pReg = 0
1d53f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1d540 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 2;.}../*.** Gene
1d541 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 rate code that w
1d542 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 78 70 ill evaluate exp
1d543 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61 6e ression pExpr an
1d544 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 d store the.** r
1d545 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 esults in regist
1d546 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 65 20 er target. The
1d547 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 61 72 results are guar
1d548 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 61 72 anteed to appear
1d549 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20 .** in register
1d54a 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 target..*/.SQLIT
1d54b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1d54c 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 50 61 lite3ExprCode(Pa
1d54d 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
1d54e 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 r *pExpr, int ta
1d54f 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e 52 rget){. int inR
1d550 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 74 eg;.. assert( t
1d551 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 65 arget>0 && targe
1d552 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 t<=pParse->nMem
1d553 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c );. inReg = sql
1d554 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 ite3ExprCodeTarg
1d555 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 et(pParse, pExpr
1d556 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73 , target);. ass
1d557 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56 64 ert( pParse->pVd
1d558 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 be || pParse->db
1d559 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1d55a 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74 ;. if( inReg!=t
1d55b 61 72 67 65 74 20 26 26 20 70 50 61 72 73 65 2d arget && pParse-
1d55c 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73 71 >pVdbe ){. sq
1d55d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
1d55e 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f pParse->pVdbe, O
1d55f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 P_SCopy, inReg,
1d560 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20 72 target);. }. r
1d561 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d 0a eturn target;.}.
1d562 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
1d563 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 74 code that evalut
1d564 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 es the given exp
1d565 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 74 73 ression and puts
1d566 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 the result.** i
1d567 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65 n register targe
1d568 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 t..**.** Also ma
1d569 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ke a copy of the
1d56a 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 73 75 expression resu
1d56b 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65 72 lts into another
1d56c 20 22 63 61 63 68 65 22 20 72 65 67 69 73 74 65 "cache" registe
1d56d 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79 20 r.** and modify
1d56e 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 the expression s
1d56f 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 o that the next
1d570 74 69 6d 65 20 69 74 20 69 73 20 65 76 61 6c 75 time it is evalu
1d571 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65 73 ated,.** the res
1d572 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66 ult is a copy of
1d573 20 74 68 65 20 63 61 63 68 65 20 72 65 67 69 73 the cache regis
1d574 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ter..**.** This
1d575 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
1d576 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 for expressions
1d577 74 68 61 74 20 61 72 65 20 75 73 65 64 20 6d 75 that are used mu
1d578 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 ltiple .** times
1d579 2e 20 20 54 68 65 79 20 61 72 65 20 65 76 61 6c . They are eval
1d57a 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64 20 74 uated once and t
1d57b 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 he results of th
1d57c 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 e expression.**
1d57d 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 are reused..*/.S
1d57e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1d57f 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
1d580 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65 20 eAndCache(Parse
1d581 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
1d582 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 Expr, int target
1d583 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 ){. Vdbe *v = p
1d584 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 Parse->pVdbe;.
1d585 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e 52 int inReg;. inR
1d586 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 eg = sqlite3Expr
1d587 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 Code(pParse, pEx
1d588 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 pr, target);. a
1d589 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 ssert( target>0
1d58a 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e );. if( pExpr->
1d58b 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 op!=TK_REGISTER
1d58c 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69 4d 65 ){ . int iMe
1d58d 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20 2b 2b m;. iMem = ++
1d58e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
1d58f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1d590 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 Op2(v, OP_Copy,
1d591 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 inReg, iMem);.
1d592 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1d593 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45 78 70 = iMem;. pExp
1d594 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 r->op = TK_REGIS
1d595 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 TER;. }. retur
1d596 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a n inReg;.}../*.*
1d597 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
1d598 20 70 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e pExpr is an con
1d599 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e stant expression
1d59a 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 that is appropr
1d59b 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 74 iate.** for fact
1d59c 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c oring out of a l
1d59d 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74 oop. Appropriat
1d59e 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 e expressions ar
1d59f 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 e:.**.** * A
1d5a0 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 ny expression th
1d5a1 61 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 at evaluates to
1d5a2 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 6f two or more opco
1d5a3 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 des..**.** *
1d5a4 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 2c Any OP_Integer,
1d5a5 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 72 OP_Real, OP_Str
1d5a6 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 ing, OP_Blob, OP
1d5a7 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 _Null, .**
1d5a8 20 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 or OP_Variable
1d5a9 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 that does not ne
1d5aa 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20 ed to be placed
1d5ab 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 73 in a .** s
1d5ac 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65 72 pecific register
1d5ad 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 ..**.** There is
1d5ae 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 63 no point in fac
1d5af 74 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 6c toring out singl
1d5b0 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f e-instruction co
1d5b1 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73 nstant.** expres
1d5b2 73 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 sions that need
1d5b3 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 to be placed in
1d5b4 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 a particular reg
1d5b5 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 63 ister. .** We c
1d5b6 6f 75 6c 64 20 66 61 63 74 6f 72 20 74 68 65 6d ould factor them
1d5b7 20 6f 75 74 2c 20 62 75 74 20 74 68 65 6e 20 77 out, but then w
1d5b8 65 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 61 e would end up a
1d5b9 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 dding an.** OP_S
1d5ba 43 6f 70 79 20 69 6e 73 74 72 75 63 74 69 6f 6e Copy instruction
1d5bb 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c to move the val
1d5bc 75 65 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 72 ue into the corr
1d5bd 65 63 74 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 ect register.**
1d5be 6c 61 74 65 72 2e 20 20 57 65 20 6d 69 67 68 74 later. We might
1d5bf 20 61 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 73 as well just us
1d5c0 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69 e the original i
1d5c1 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a 2a nstruction and.*
1d5c2 2a 20 61 76 6f 69 64 20 74 68 65 20 4f 50 5f 53 * avoid the OP_S
1d5c3 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Copy..*/.static
1d5c4 69 6e 74 20 69 73 41 70 70 72 6f 70 72 69 61 74 int isAppropriat
1d5c5 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78 eForFactoring(Ex
1d5c6 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 73 pr *p){. if( !s
1d5c7 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
1d5c8 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 tantNotJoin(p) )
1d5c9 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 {. return 0;
1d5ca 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e /* Only constan
1d5cb 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 t expressions ar
1d5cc 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f e appropriate fo
1d5cd 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 r factoring */.
1d5ce 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 }. if( (p->fla
1d5cf 67 73 20 26 20 45 50 5f 46 69 78 65 64 44 65 73 gs & EP_FixedDes
1d5d0 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 t)==0 ){. ret
1d5d1 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 63 urn 1; /* Any c
1d5d2 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f 75 74 20 onstant without
1d5d3 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 a fixed destinat
1d5d4 69 6f 6e 20 69 73 20 61 70 70 72 6f 70 72 69 61 ion is appropria
1d5d5 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c te */. }. whil
1d5d6 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c e( p->op==TK_UPL
1d5d7 55 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 66 US ) p = p->pLef
1d5d8 74 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e t;. switch( p->
1d5d9 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 op ){.#ifndef SQ
1d5da 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c LITE_OMIT_BLOB_L
1d5db 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 ITERAL. case
1d5dc 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a TK_BLOB:.#endif.
1d5dd 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 case TK_VARI
1d5de 41 42 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 ABLE:. case T
1d5df 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 K_INTEGER:. c
1d5e0 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 ase TK_FLOAT:.
1d5e1 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a case TK_NULL:.
1d5e2 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 case TK_STRI
1d5e3 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 NG: {. test
1d5e4 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f case( p->op==TK_
1d5e5 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 65 BLOB );. te
1d5e6 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 stcase( p->op==T
1d5e7 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 20 K_VARIABLE );.
1d5e8 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d testcase( p-
1d5e9 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 >op==TK_INTEGER
1d5ea 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1d5eb 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f e( p->op==TK_FLO
1d5ec 41 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 AT );. test
1d5ed 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f case( p->op==TK_
1d5ee 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 NULL );. te
1d5ef 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 stcase( p->op==T
1d5f0 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 20 K_STRING );.
1d5f1 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 74 /* Single-inst
1d5f2 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 ruction constant
1d5f3 73 20 77 69 74 68 20 61 20 66 69 78 65 64 20 64 s with a fixed d
1d5f4 65 73 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a 20 estination are.
1d5f5 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 64 ** better d
1d5f6 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 one in-line. If
1d5f7 20 77 65 20 66 61 63 74 6f 72 20 74 68 65 6d 2c we factor them,
1d5f8 20 74 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 20 they will just
1d5f9 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 20 end. ** up
1d5fa 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f 50 generating an OP
1d5fb 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 74 _SCopy to move t
1d5fc 68 65 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 he value to the
1d5fd 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 destination.
1d5fe 20 20 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 2a ** register. *
1d5ff 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 /. return 0
1d600 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1d601 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 TK_UMINUS: {.
1d602 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 if( p->pLef
1d603 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 t->op==TK_FLOAT
1d604 7c 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d || p->pLeft->op=
1d605 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 =TK_INTEGER ){.
1d606 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 return 0
1d607 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ;. }.
1d608 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1d609 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
1d60a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1d60b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b . }. return 1;
1d60c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 .}../*.** If pEx
1d60d 70 72 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 pr is a constant
1d60e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 expression that
1d60f 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 is appropriate
1d610 66 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69 6e 67 for.** factoring
1d611 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 out of a loop,
1d612 74 68 65 6e 20 65 76 61 6c 75 61 74 65 20 74 68 then evaluate th
1d613 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 e expression.**
1d614 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 20 into a register
1d615 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 68 65 20 and convert the
1d616 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 expression into
1d617 61 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 2a 2a a TK_REGISTER.**
1d618 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a expression..*/.
1d619 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c 43 static int evalC
1d61a 6f 6e 73 74 45 78 70 72 28 57 61 6c 6b 65 72 20 onstExpr(Walker
1d61b 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a *pWalker, Expr *
1d61c 70 45 78 70 72 29 7b 0a 20 20 50 61 72 73 65 20 pExpr){. Parse
1d61d 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 *pParse = pWalke
1d61e 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 77 69 r->pParse;. swi
1d61f 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 tch( pExpr->op )
1d620 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 {. case TK_RE
1d621 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 GISTER: {.
1d622 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
1d623 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 case TK_FUNC
1d624 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 TION:. case T
1d625 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a K_AGG_FUNCTION:.
1d626 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 case TK_CONS
1d627 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20 T_FUNC: {.
1d628 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73 /* The arguments
1d629 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 68 to a function h
1d62a 61 76 65 20 61 20 66 69 78 65 64 20 64 65 73 74 ave a fixed dest
1d62b 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a ination.. *
1d62c 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 69 73 * Mark them this
1d62d 20 77 61 79 20 74 6f 20 61 76 6f 69 64 20 67 65 way to avoid ge
1d62e 6e 65 72 61 74 65 64 20 75 6e 6e 65 65 64 65 64 nerated unneeded
1d62f 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 20 20 OP_SCopy.
1d630 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e ** instructions.
1d631 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 . */.
1d632 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
1d633 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b = pExpr->pList;
1d634 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 . if( pList
1d635 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
1d636 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 i = pList->nExpr
1d637 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 ;. struct
1d638 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
1d639 70 49 74 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61 pItem = pList->a
1d63a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 ;. for(;
1d63b 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b i>0; i--, pItem+
1d63c 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
1d63d 28 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 29 ( pItem->pExpr )
1d63e 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 66 pItem->pExpr->f
1d63f 6c 61 67 73 20 7c 3d 20 45 50 5f 46 69 78 65 64 lags |= EP_Fixed
1d640 44 65 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a Dest;. }.
1d641 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
1d642 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
1d643 20 69 66 28 20 69 73 41 70 70 72 6f 70 72 69 61 if( isAppropria
1d644 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 70 teForFactoring(p
1d645 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 Expr) ){. int
1d646 20 72 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e r1 = ++pParse->
1d647 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e 74 20 72 32 nMem;. int r2
1d648 3b 0a 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 ;. r2 = sqlit
1d649 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 e3ExprCodeTarget
1d64a 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
1d64b 72 31 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21 r1);. if( r1!
1d64c 3d 72 32 20 29 20 73 71 6c 69 74 65 33 52 65 6c =r2 ) sqlite3Rel
1d64d 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
1d64e 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 70 45 78 se, r1);. pEx
1d64f 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 pr->op = TK_REGI
1d650 53 54 45 52 3b 0a 20 20 20 20 70 45 78 70 72 2d STER;. pExpr-
1d651 3e 69 54 61 62 6c 65 20 3d 20 72 32 3b 0a 20 20 >iTable = r2;.
1d652 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
1d653 6e 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ne;. }. return
1d654 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d WRC_Continue;.}
1d655 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 65 76 61 6c 75 ../*.** Preevalu
1d656 61 74 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 ate constant sub
1d657 65 78 70 72 65 73 73 69 6f 6e 73 20 77 69 74 68 expressions with
1d658 69 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f in pExpr and sto
1d659 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 re the.** result
1d65a 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 2e 20 s in registers.
1d65b 20 4d 6f 64 69 66 79 20 70 45 78 70 72 20 73 6f Modify pExpr so
1d65c 20 74 68 61 74 20 74 68 65 20 63 6f 6e 73 74 61 that the consta
1d65d 6e 74 20 73 75 62 65 78 70 72 65 73 69 6f 6e 73 nt subexpresions
1d65e 0a 2a 2a 20 61 72 65 20 54 4b 5f 52 45 47 49 53 .** are TK_REGIS
1d65f 54 45 52 20 6f 70 63 6f 64 65 73 20 74 68 61 74 TER opcodes that
1d660 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 72 refer to the pr
1d661 65 63 6f 6d 70 75 74 65 64 20 76 61 6c 75 65 73 ecomputed values
1d662 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1d663 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1d664 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 ExprCodeConstant
1d665 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c s(Parse *pParse,
1d666 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 Expr *pExpr){.
1d667 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 Walker w;. w.x
1d668 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 ExprCallback = e
1d669 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b 0a 20 20 valConstExpr;.
1d66a 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 w.xSelectCallbac
1d66b 6b 20 3d 20 30 3b 0a 20 20 77 2e 70 50 61 72 73 k = 0;. w.pPars
1d66c 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71 e = pParse;. sq
1d66d 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 lite3WalkExpr(&w
1d66e 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 0a 2f 2a , pExpr);.}.../*
1d66f 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
1d670 65 20 74 68 61 74 20 70 75 73 68 65 73 20 74 68 e that pushes th
1d671 65 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79 e value of every
1d672 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
1d673 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 given.** express
1d674 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f 20 61 20 ion list into a
1d675 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69 sequence of regi
1d676 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 sters beginning
1d677 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a at target..**.**
1d678 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1d679 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 65 er of elements e
1d67a 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c valuated..*/.SQL
1d67b 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1d67c 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 sqlite3ExprCodeE
1d67d 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65 xprList(. Parse
1d67e 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a *pParse, /*
1d67f 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
1d680 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
1d681 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 pList, /* The
1d682 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 expression list
1d683 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 to be coded */.
1d684 20 69 6e 74 20 74 61 72 67 65 74 2c 20 20 20 20 int target,
1d685 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 /* Where to
1d686 77 72 69 74 65 20 72 65 73 75 6c 74 73 20 2a 2f write results */
1d687 0a 20 20 69 6e 74 20 64 6f 48 61 72 64 43 6f 70 . int doHardCop
1d688 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61 20 y /* Make a
1d689 68 61 72 64 20 63 6f 70 79 20 6f 66 20 65 76 65 hard copy of eve
1d68a 72 79 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b ry element */.){
1d68b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 . struct ExprLi
1d68c 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
1d68d 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 61 73 int i, n;. as
1d68e 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 sert( pList!=0 )
1d68f 3b 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67 ;. assert( targ
1d690 65 74 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 70 4c et>0 );. n = pL
1d691 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 66 6f ist->nExpr;. fo
1d692 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 r(pItem=pList->a
1d693 2c 20 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c , i=0; i<n; i++,
1d694 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 pItem++){. i
1d695 66 28 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 f( pItem->iAlias
1d696 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 ){. int iR
1d697 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 eg = codeAlias(p
1d698 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 69 41 Parse, pItem->iA
1d699 6c 69 61 73 2c 20 70 49 74 65 6d 2d 3e 70 45 78 lias, pItem->pEx
1d69a 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 pr, target+i);.
1d69b 20 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 Vdbe *v = s
1d69c 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
1d69d 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28 arse);. if(
1d69e 20 69 52 65 67 21 3d 74 61 72 67 65 74 2b 69 20 iReg!=target+i
1d69f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1d6a0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1d6a1 4f 50 5f 53 43 6f 70 79 2c 20 69 52 65 67 2c 20 OP_SCopy, iReg,
1d6a2 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 target+i);.
1d6a3 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1d6a4 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
1d6a5 6f 64 65 28 70 50 61 72 73 65 2c 20 70 49 74 65 ode(pParse, pIte
1d6a6 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74 m->pExpr, target
1d6a7 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 +i);. }. i
1d6a8 66 28 20 64 6f 48 61 72 64 43 6f 70 79 20 29 7b f( doHardCopy ){
1d6a9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
1d6aa 70 72 48 61 72 64 43 6f 70 79 28 70 50 61 72 73 prHardCopy(pPars
1d6ab 65 2c 20 74 61 72 67 65 74 2c 20 6e 29 3b 0a 20 e, target, n);.
1d6ac 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1d6ad 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 n n;.}../*.** Ge
1d6ae 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 nerate code for
1d6af 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 a boolean expres
1d6b0 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 sion such that a
1d6b1 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a jump is made.**
1d6b2 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 to the label "d
1d6b3 65 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 est" if the expr
1d6b4 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 62 ession is true b
1d6b5 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 ut execution.**
1d6b6 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 continues straig
1d6b7 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65 ht thru if the e
1d6b8 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c xpression is fal
1d6b9 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
1d6ba 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c expression eval
1d6bb 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e uates to NULL (n
1d6bc 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 either true nor
1d6bd 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a 2a 2a 20 false), then.**
1d6be 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 take the jump if
1d6bf 20 74 68 65 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 the jumpIfNull
1d6c0 66 6c 61 67 20 69 73 20 53 51 4c 49 54 45 5f 4a flag is SQLITE_J
1d6c1 55 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a UMPIFNULL..**.**
1d6c2 20 54 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e This code depen
1d6c3 64 73 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74 ds on the fact t
1d6c4 68 61 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65 hat certain toke
1d6c5 6e 20 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b n values (ex: TK
1d6c6 5f 45 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20 _EQ).** are the
1d6c7 73 61 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76 same as opcode v
1d6c8 61 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71 alues (ex: OP_Eq
1d6c9 29 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 ) that implement
1d6ca 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
1d6cb 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e ng.** operation.
1d6cc 20 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e Special commen
1d6cd 74 73 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64 ts in vdbe.c and
1d6ce 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 the mkopcodeh.a
1d6cf 77 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20 wk script in.**
1d6d0 74 68 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73 the make process
1d6d1 20 63 61 75 73 65 20 74 68 65 73 65 20 76 61 6c cause these val
1d6d2 75 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41 ues to align. A
1d6d3 73 73 65 72 74 28 29 73 20 69 6e 20 74 68 65 20 ssert()s in the
1d6d4 63 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65 code.** below ve
1d6d5 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 rify that the nu
1d6d6 6d 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65 mbers are aligne
1d6d7 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a d correctly..*/.
1d6d8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1d6d9 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
1d6da 66 54 72 75 65 28 50 61 72 73 65 20 2a 70 50 61 fTrue(Parse *pPa
1d6db 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
1d6dc 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 , int dest, int
1d6dd 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 jumpIfNull){. V
1d6de 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d dbe *v = pParse-
1d6df 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 >pVdbe;. int op
1d6e0 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 = 0;. int regF
1d6e1 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 ree1 = 0;. int
1d6e2 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 regFree2 = 0;.
1d6e3 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 int r1, r2;.. a
1d6e4 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c ssert( jumpIfNul
1d6e5 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 l==SQLITE_JUMPIF
1d6e6 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 NULL || jumpIfNu
1d6e7 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 76 ll==0 );. if( v
1d6e8 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30 20 ==0 || pExpr==0
1d6e9 29 20 72 65 74 75 72 6e 3b 0a 20 20 6f 70 20 3d ) return;. op =
1d6ea 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 73 77 pExpr->op;. sw
1d6eb 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 itch( op ){.
1d6ec 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 case TK_AND: {.
1d6ed 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 73 71 int d2 = sq
1d6ee 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
1d6ef 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 74 65 73 el(v);. tes
1d6f0 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c tcase( jumpIfNul
1d6f1 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 l==0 );. te
1d6f2 73 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e stcase( pParse->
1d6f3 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d disableColCache=
1d6f4 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 =0 );. sqli
1d6f5 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 te3ExprIfFalse(p
1d6f6 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
1d6f7 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75 eft, d2,jumpIfNu
1d6f8 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 ll^SQLITE_JUMPIF
1d6f9 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 70 50 61 NULL);. pPa
1d6fa 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 rse->disableColC
1d6fb 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71 ache++;. sq
1d6fc 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 lite3ExprIfTrue(
1d6fd 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1d6fe 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d Right, dest, jum
1d6ff 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 pIfNull);.
1d700 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
1d701 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e disableColCache>
1d702 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 0 );. pPars
1d703 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 e->disableColCac
1d704 68 65 2d 2d 3b 0a 20 20 20 20 20 20 73 71 6c 69 he--;. sqli
1d705 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
1d706 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 bel(v, d2);.
1d707 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1d708 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 20 7b case TK_OR: {
1d709 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1d70a 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 jumpIfNull==0 )
1d70b 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1d70c 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c ( pParse->disabl
1d70d 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 3b 0a eColCache==0 );.
1d70e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1d70f 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 rIfTrue(pParse,
1d710 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 pExpr->pLeft, de
1d711 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b st, jumpIfNull);
1d712 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 . pParse->d
1d713 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b isableColCache++
1d714 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
1d715 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 xprIfTrue(pParse
1d716 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c , pExpr->pRight,
1d717 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c dest, jumpIfNul
1d718 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 l);. assert
1d719 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c ( pParse->disabl
1d71a 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 eColCache>0 );.
1d71b 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 pParse->dis
1d71c 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a ableColCache--;.
1d71d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1d71e 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e }. case TK_N
1d71f 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 OT: {. test
1d720 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c case( jumpIfNull
1d721 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ==0 );. sql
1d722 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 ite3ExprIfFalse(
1d723 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1d724 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 Left, dest, jump
1d725 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 IfNull);. b
1d726 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1d727 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 case TK_LT:.
1d728 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 case TK_LE:.
1d729 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 case TK_GT:.
1d72a 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 case TK_GE:.
1d72b 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 case TK_NE:.
1d72c 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 case TK_EQ: {.
1d72d 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c assert( TK_L
1d72e 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 T==OP_Lt );.
1d72f 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d assert( TK_LE=
1d730 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 =OP_Le );.
1d731 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f assert( TK_GT==O
1d732 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73 P_Gt );. as
1d733 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f sert( TK_GE==OP_
1d734 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Ge );. asse
1d735 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 rt( TK_EQ==OP_Eq
1d736 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1d737 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 ( TK_NE==OP_Ne )
1d738 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1d739 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 ( op==TK_LT );.
1d73a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
1d73b 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 p==TK_LE );.
1d73c 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
1d73d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 TK_GT );. t
1d73e 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
1d73f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 GE );. test
1d740 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 case( op==TK_EQ
1d741 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1d742 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a e( op==TK_NE );.
1d743 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1d744 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b jumpIfNull==0 );
1d745 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 . codeCompa
1d746 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 reOperands(pPars
1d747 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
1d748 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c &r1, ®Free1,
1d749 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1d74a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d74b 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 pExpr->pRight
1d74c 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 , &r2, ®Free2
1d74d 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d );. codeCom
1d74e 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 pare(pParse, pEx
1d74f 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 pr->pLeft, pExpr
1d750 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 ->pRight, op,.
1d751 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d752 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 r1, r2, dest, ju
1d753 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 mpIfNull);.
1d754 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
1d755 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee1==0 );.
1d756 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
1d757 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 e2==0 );. b
1d758 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1d759 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a case TK_ISNULL:.
1d75a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e case TK_NOTN
1d75b 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ULL: {. ass
1d75c 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d ert( TK_ISNULL==
1d75d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 OP_IsNull );.
1d75e 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f assert( TK_NO
1d75f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c TNULL==OP_NotNul
1d760 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 l );. testc
1d761 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 ase( op==TK_ISNU
1d762 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LL );. test
1d763 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 case( op==TK_NOT
1d764 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 NULL );. r1
1d765 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
1d766 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
1d767 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 Expr->pLeft, &re
1d768 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 gFree1);. s
1d769 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1d76a 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 (v, op, r1, dest
1d76b 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1d76c 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 e( regFree1==0 )
1d76d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1d76e 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
1d76f 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 _BETWEEN: {.
1d770 20 20 2f 2a 20 20 20 20 78 20 42 45 54 57 45 45 /* x BETWEE
1d771 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 N y AND z.
1d772 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 65 **. ** Is e
1d773 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 quivalent to .
1d774 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
1d775 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a x>=y AND x<=z
1d776 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
1d777 2a 2a 20 43 6f 64 65 20 69 74 20 61 73 20 73 75 ** Code it as su
1d778 63 68 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20 ch, taking care
1d779 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e to do the common
1d77a 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 subexpression.
1d77b 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 ** elementa
1d77c 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 tion of x..
1d77d 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 65 */. Expr e
1d77e 78 70 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 xprAnd;. Ex
1d77f 70 72 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 pr compLeft;.
1d780 20 20 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68 Expr compRigh
1d781 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 t;. Expr ex
1d782 70 72 58 3b 0a 0a 20 20 20 20 20 20 65 78 70 72 prX;.. expr
1d783 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 X = *pExpr->pLef
1d784 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 t;. exprAnd
1d785 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 .op = TK_AND;.
1d786 20 20 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 exprAnd.pLef
1d787 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 t = &compLeft;.
1d788 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 52 69 exprAnd.pRi
1d789 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 ght = &compRight
1d78a 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 ;. compLeft
1d78b 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 .op = TK_GE;.
1d78c 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 compLeft.pLef
1d78d 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 t = &exprX;.
1d78e 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 compLeft.pRigh
1d78f 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 t = pExpr->pList
1d790 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 ->a[0].pExpr;.
1d791 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 compRight.op
1d792 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 = TK_LE;.
1d793 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20 compRight.pLeft
1d794 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 = &exprX;.
1d795 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74 compRight.pRight
1d796 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d = pExpr->pList-
1d797 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 >a[1].pExpr;.
1d798 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20 exprX.iTable
1d799 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
1d79a 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65 eTemp(pParse, &e
1d79b 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29 xprX, ®Free1)
1d79c 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1d79d 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
1d79e 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20 . exprX.op
1d79f 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 = TK_REGISTER;.
1d7a0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a testcase( j
1d7a1 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a umpIfNull==0 );.
1d7a2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
1d7a3 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 rIfTrue(pParse,
1d7a4 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 &exprAnd, dest,
1d7a5 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 jumpIfNull);.
1d7a6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1d7a7 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
1d7a8 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 r1 = sqlite
1d7a9 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
1d7aa 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 arse, pExpr, &re
1d7ab 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 gFree1);. s
1d7ac 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1d7ad 28 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64 (v, OP_If, r1, d
1d7ae 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 est, jumpIfNull!
1d7af 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 =0);. testc
1d7b0 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 ase( regFree1==0
1d7b1 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1d7b2 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d se( jumpIfNull==
1d7b3 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
1d7b4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
1d7b5 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
1d7b6 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 Reg(pParse, regF
1d7b7 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 ree1);. sqlite3
1d7b8 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
1d7b9 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 Parse, regFree2)
1d7ba 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e ; .}../*.** Gen
1d7bb 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 erate code for a
1d7bc 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 boolean express
1d7bd 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20 ion such that a
1d7be 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 jump is made.**
1d7bf 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65 to the label "de
1d7c0 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65 st" if the expre
1d7c1 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 20 62 ssion is false b
1d7c2 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 ut execution.**
1d7c3 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 continues straig
1d7c4 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65 ht thru if the e
1d7c5 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 xpression is tru
1d7c6 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
1d7c7 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 expression evalu
1d7c8 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 ates to NULL (ne
1d7c9 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66 ither true nor f
1d7ca 61 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 alse) then.** ju
1d7cb 6d 70 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c mp if jumpIfNull
1d7cc 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 is SQLITE_JUMPI
1d7cd 46 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68 FNULL or fall th
1d7ce 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49 66 4e rough if jumpIfN
1d7cf 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a ull.** is 0..*/.
1d7d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1d7d1 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
1d7d2 66 46 61 6c 73 65 28 50 61 72 73 65 20 2a 70 50 fFalse(Parse *pP
1d7d3 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 arse, Expr *pExp
1d7d4 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 r, int dest, int
1d7d5 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 jumpIfNull){.
1d7d6 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 Vdbe *v = pParse
1d7d7 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f ->pVdbe;. int o
1d7d8 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 p = 0;. int reg
1d7d9 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 Free1 = 0;. int
1d7da 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 regFree2 = 0;.
1d7db 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 int r1, r2;..
1d7dc 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 assert( jumpIfNu
1d7dd 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 ll==SQLITE_JUMPI
1d7de 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e FNULL || jumpIfN
1d7df 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 ull==0 );. if(
1d7e0 76 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30 v==0 || pExpr==0
1d7e1 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a ) return;.. /*
1d7e2 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 The value of pE
1d7e3 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61 xpr->op and op a
1d7e4 72 65 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f re related as fo
1d7e5 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a llows:. **. **
1d7e6 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 pExpr->op
1d7e7 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20 op.
1d7e8 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d ** ------
1d7e9 2d 2d 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d --- ---
1d7ea 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 -------. **
1d7eb 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 TK_ISNULL
1d7ec 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c OP_NotNull
1d7ed 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e . ** TK_N
1d7ee 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f OTNULL O
1d7ef 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 P_IsNull. **
1d7f0 20 20 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 TK_NE
1d7f1 20 20 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a OP_Eq. *
1d7f2 2a 20 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20 * TK_EQ
1d7f3 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65 OP_Ne
1d7f4 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47 . ** TK_G
1d7f5 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f T O
1d7f6 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 P_Le. **
1d7f7 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 TK_LE
1d7f8 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20 OP_Gt. **
1d7f9 20 20 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20 TK_GE
1d7fa 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a OP_Lt. *
1d7fb 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20 * TK_LT
1d7fc 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65 OP_Ge
1d7fd 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f . **. ** For o
1d7fe 74 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70 ther values of p
1d7ff 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 Expr->op, op is
1d800 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e undefined and un
1d801 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76 used.. ** The v
1d802 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20 alue of TK_ and
1d803 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 OP_ constants ar
1d804 65 20 61 72 72 61 6e 67 65 64 20 73 75 63 68 20 e arranged such
1d805 74 68 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e that we. ** can
1d806 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70 compute the map
1d807 70 69 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67 ping above using
1d808 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 the following e
1d809 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 xpression.. **
1d80a 41 73 73 65 72 74 28 29 73 20 76 65 72 69 66 79 Assert()s verify
1d80b 20 74 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74 that the comput
1d80c 61 74 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74 ation is correct
1d80d 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 .. */. op = ((
1d80e 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 pExpr->op+(TK_IS
1d80f 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f NULL&1))^1)-(TK_
1d810 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a ISNULL&1);.. /*
1d811 20 56 65 72 69 66 79 20 63 6f 72 72 65 63 74 20 Verify correct
1d812 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f alignment of TK_
1d813 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e and OP_ constan
1d814 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ts. */. assert
1d815 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f ( pExpr->op!=TK_
1d816 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 ISNULL || op==OP
1d817 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 _NotNull );. as
1d818 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 sert( pExpr->op!
1d819 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f =TK_NOTNULL || o
1d81a 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a p==OP_IsNull );.
1d81b 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
1d81c 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 >op!=TK_NE || op
1d81d 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73 ==OP_Eq );. ass
1d81e 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d ert( pExpr->op!=
1d81f 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f TK_EQ || op==OP_
1d820 4e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Ne );. assert(
1d821 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 pExpr->op!=TK_LT
1d822 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b || op==OP_Ge );
1d823 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 . assert( pExpr
1d824 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f ->op!=TK_LE || o
1d825 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73 p==OP_Gt );. as
1d826 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 sert( pExpr->op!
1d827 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 =TK_GT || op==OP
1d828 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 _Le );. assert(
1d829 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 pExpr->op!=TK_G
1d82a 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 E || op==OP_Lt )
1d82b 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 ;.. switch( pEx
1d82c 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 pr->op ){. ca
1d82d 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 se TK_AND: {.
1d82e 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d testcase( jum
1d82f 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
1d830 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 50 testcase( pP
1d831 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c arse->disableCol
1d832 43 61 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 Cache==0 );.
1d833 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 sqlite3ExprIfF
1d834 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 alse(pParse, pEx
1d835 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c pr->pLeft, dest,
1d836 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
1d837 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 pParse->disa
1d838 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 bleColCache++;.
1d839 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
1d83a 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 IfFalse(pParse,
1d83b 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 pExpr->pRight, d
1d83c 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 est, jumpIfNull)
1d83d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1d83e 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 pParse->disableC
1d83f 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 olCache>0 );.
1d840 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 pParse->disab
1d841 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 leColCache--;.
1d842 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1d843 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a . case TK_OR:
1d844 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20 {. int d2
1d845 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b = sqlite3VdbeMak
1d846 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 eLabel(v);.
1d847 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 testcase( jumpI
1d848 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 fNull==0 );.
1d849 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72 testcase( pPar
1d84a 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 se->disableColCa
1d84b 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 che==0 );.
1d84c 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 sqlite3ExprIfTru
1d84d 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
1d84e 3e 70 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70 >pLeft, d2, jump
1d84f 49 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 IfNull^SQLITE_JU
1d850 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 MPIFNULL);.
1d851 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 pParse->disable
1d852 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 ColCache++;.
1d853 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 sqlite3ExprIfF
1d854 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 alse(pParse, pEx
1d855 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 pr->pRight, dest
1d856 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 , jumpIfNull);.
1d857 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
1d858 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 rse->disableColC
1d859 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 ache>0 );.
1d85a 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 pParse->disableC
1d85b 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 olCache--;.
1d85c 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f sqlite3VdbeReso
1d85d 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b lveLabel(v, d2);
1d85e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1d85f 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
1d860 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c NOT: {. sql
1d861 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70 ite3ExprIfTrue(p
1d862 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c Parse, pExpr->pL
1d863 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 eft, dest, jumpI
1d864 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 fNull);. br
1d865 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1d866 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 ase TK_LT:. c
1d867 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 ase TK_LE:. c
1d868 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 ase TK_GT:. c
1d869 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 ase TK_GE:. c
1d86a 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 ase TK_NE:. c
1d86b 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 ase TK_EQ: {.
1d86c 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
1d86d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 =TK_LT );.
1d86e 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
1d86f 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _LE );. tes
1d870 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 tcase( op==TK_GT
1d871 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1d872 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b se( op==TK_GE );
1d873 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1d874 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 op==TK_EQ );.
1d875 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
1d876 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 ==TK_NE );.
1d877 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 testcase( jumpI
1d878 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 fNull==0 );.
1d879 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 codeCompareOpe
1d87a 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 rands(pParse, pE
1d87b 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c xpr->pLeft, &r1,
1d87c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 ®Free1,.
1d87d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d87e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
1d87f 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 xpr->pRight, &r2
1d880 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
1d881 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 codeCompare(
1d882 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
1d883 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 Left, pExpr->pRi
1d884 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 ght, op,.
1d885 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 r1, r
1d886 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 2, dest, jumpIfN
1d887 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ull);. test
1d888 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
1d889 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
1d88a 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
1d88b 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
1d88c 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1d88d 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 TK_ISNULL:. c
1d88e 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 ase TK_NOTNULL:
1d88f 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
1d890 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 ( op==TK_ISNULL
1d891 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
1d892 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c e( op==TK_NOTNUL
1d893 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 L );. r1 =
1d894 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
1d895 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 emp(pParse, pExp
1d896 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 r->pLeft, ®Fr
1d897 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ee1);. sqli
1d898 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1d899 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a op, r1, dest);.
1d89a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1d89b 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 regFree1==0 );.
1d89c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1d89d 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 }. case TK_BE
1d89e 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f TWEEN: {. /
1d89f 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 * x BETWEEN y
1d8a0 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a AND z. **.
1d8a1 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75 69 ** Is equi
1d8a2 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 valent to .
1d8a3 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 **. **
1d8a4 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 x>=y AND x<=z.
1d8a5 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
1d8a6 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68 2c Code it as such,
1d8a7 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f 20 taking care to
1d8a8 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 do the common su
1d8a9 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 bexpression.
1d8aa 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f ** elementatio
1d8ab 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f n of x.. */
1d8ac 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 . Expr expr
1d8ad 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72 20 And;. Expr
1d8ae 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 compLeft;.
1d8af 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a Expr compRight;.
1d8b0 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 58 Expr exprX
1d8b1 3b 0a 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d ;.. exprX =
1d8b2 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a *pExpr->pLeft;.
1d8b3 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 exprAnd.op
1d8b4 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 = TK_AND;.
1d8b5 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d exprAnd.pLeft =
1d8b6 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 &compLeft;.
1d8b7 20 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 exprAnd.pRight
1d8b8 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 = &compRight;.
1d8b9 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 compLeft.op
1d8ba 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 = TK_GE;.
1d8bb 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d compLeft.pLeft =
1d8bc 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 &exprX;. c
1d8bd 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d ompLeft.pRight =
1d8be 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 pExpr->pList->a
1d8bf 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 [0].pExpr;.
1d8c0 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 compRight.op =
1d8c1 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d TK_LE;. com
1d8c2 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 pRight.pLeft = &
1d8c3 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d exprX;. com
1d8c4 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20 pRight.pRight =
1d8c5 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b pExpr->pList->a[
1d8c6 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 1].pExpr;.
1d8c7 65 78 70 72 58 2e 69 54 61 62 6c 65 20 3d 20 73 exprX.iTable = s
1d8c8 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
1d8c9 6d 70 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 mp(pParse, &expr
1d8ca 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 X, ®Free1);.
1d8cb 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
1d8cc 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
1d8cd 20 20 20 20 65 78 70 72 58 2e 6f 70 20 3d 20 54 exprX.op = T
1d8ce 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 K_REGISTER;.
1d8cf 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 testcase( jump
1d8d0 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 IfNull==0 );.
1d8d1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 sqlite3ExprIf
1d8d2 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 26 65 False(pParse, &e
1d8d3 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75 xprAnd, dest, ju
1d8d4 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 mpIfNull);.
1d8d5 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1d8d6 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
1d8d7 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 r1 = sqlite3E
1d8d8 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 xprCodeTemp(pPar
1d8d9 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46 se, pExpr, ®F
1d8da 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c ree1);. sql
1d8db 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1d8dc 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31 2c 20 , OP_IfNot, r1,
1d8dd 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c dest, jumpIfNull
1d8de 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 !=0);. test
1d8df 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
1d8e0 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
1d8e1 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d ase( jumpIfNull=
1d8e2 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 =0 );. brea
1d8e3 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 k;. }. }. s
1d8e4 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
1d8e5 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 pReg(pParse, reg
1d8e6 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 Free1);. sqlite
1d8e7 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
1d8e8 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 pParse, regFree2
1d8e9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 );.}../*.** Do a
1d8ea 20 64 65 65 70 20 63 6f 6d 70 61 72 69 73 6f 6e deep comparison
1d8eb 20 6f 66 20 74 77 6f 20 65 78 70 72 65 73 73 69 of two expressi
1d8ec 6f 6e 20 74 72 65 65 73 2e 20 20 52 65 74 75 72 on trees. Retur
1d8ed 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f n TRUE (non-zero
1d8ee 29 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65 ).** if they are
1d8ef 20 69 64 65 6e 74 69 63 61 6c 20 61 6e 64 20 72 identical and r
1d8f0 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74 eturn FALSE if t
1d8f1 68 65 79 20 64 69 66 66 65 72 20 69 6e 20 61 6e hey differ in an
1d8f2 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d y way..**.** Som
1d8f3 65 74 69 6d 65 73 20 74 68 69 73 20 72 6f 75 74 etimes this rout
1d8f4 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ine will return
1d8f5 46 41 4c 53 45 20 65 76 65 6e 20 69 66 20 74 68 FALSE even if th
1d8f6 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e e two expression
1d8f7 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 72 65 20 s.** really are
1d8f8 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 49 66 20 equivalent. If
1d8f9 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f 76 65 20 we cannot prove
1d8fa 74 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 that the express
1d8fb 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 64 65 6e ions are.** iden
1d8fc 74 69 63 61 6c 2c 20 77 65 20 72 65 74 75 72 6e tical, we return
1d8fd 20 46 41 4c 53 45 20 6a 75 73 74 20 74 6f 20 62 FALSE just to b
1d8fe 65 20 73 61 66 65 2e 20 20 53 6f 20 69 66 20 74 e safe. So if t
1d8ff 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 his routine.** r
1d900 65 74 75 72 6e 73 20 66 61 6c 73 65 2c 20 74 68 eturns false, th
1d901 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 20 72 65 en you do not re
1d902 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 20 63 65 ally know for ce
1d903 72 74 61 69 6e 20 69 66 20 74 68 65 20 74 77 6f rtain if the two
1d904 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 .** expressions
1d905 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 42 are the same. B
1d906 75 74 20 69 66 20 79 6f 75 20 67 65 74 20 61 20 ut if you get a
1d907 54 52 55 45 20 72 65 74 75 72 6e 2c 20 74 68 65 TRUE return, the
1d908 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 62 65 20 n you.** can be
1d909 73 75 72 65 20 74 68 65 20 65 78 70 72 65 73 73 sure the express
1d90a 69 6f 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d ions are the sam
1d90b 65 2e 20 20 49 6e 20 74 68 65 20 70 6c 61 63 65 e. In the place
1d90c 73 20 77 68 65 72 65 0a 2a 2a 20 74 68 69 73 20 s where.** this
1d90d 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 2c routine is used,
1d90e 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 68 75 72 it does not hur
1d90f 74 20 74 6f 20 67 65 74 20 61 6e 20 65 78 74 72 t to get an extr
1d910 61 20 46 41 4c 53 45 20 2d 20 74 68 61 74 0a 2a a FALSE - that.*
1d911 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 72 65 73 * just might res
1d912 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c 69 67 ult in some slig
1d913 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f 64 65 htly slower code
1d914 2e 20 20 42 75 74 20 72 65 74 75 72 6e 69 6e 67 . But returning
1d915 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 .** an incorrect
1d916 20 54 52 55 45 20 63 6f 75 6c 64 20 6c 65 61 64 TRUE could lead
1d917 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f to a malfunctio
1d918 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1d919 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1d91a 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78 70 72 ExprCompare(Expr
1d91b 20 2a 70 41 2c 20 45 78 70 72 20 2a 70 42 29 7b *pA, Expr *pB){
1d91c 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
1d91d 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a pA==0||pB==0 ){.
1d91e 20 20 20 20 72 65 74 75 72 6e 20 70 42 3d 3d 70 return pB==p
1d91f 41 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 2d A;. }. if( pA-
1d920 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72 65 >op!=pB->op ) re
1d921 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 70 turn 0;. if( (p
1d922 41 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 A->flags & EP_Di
1d923 73 74 69 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c stinct)!=(pB->fl
1d924 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 ags & EP_Distinc
1d925 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 t) ) return 0;.
1d926 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 if( !sqlite3Exp
1d927 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 rCompare(pA->pLe
1d928 66 74 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 ft, pB->pLeft) )
1d929 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
1d92a 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d !sqlite3ExprCom
1d92b 70 61 72 65 28 70 41 2d 3e 70 52 69 67 68 74 2c pare(pA->pRight,
1d92c 20 70 42 2d 3e 70 52 69 67 68 74 29 20 29 20 72 pB->pRight) ) r
1d92d 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 eturn 0;. if( p
1d92e 41 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 A->pList ){.
1d92f 69 66 28 20 70 42 2d 3e 70 4c 69 73 74 3d 3d 30 if( pB->pList==0
1d930 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
1d931 20 69 66 28 20 70 41 2d 3e 70 4c 69 73 74 2d 3e if( pA->pList->
1d932 6e 45 78 70 72 21 3d 70 42 2d 3e 70 4c 69 73 74 nExpr!=pB->pList
1d933 2d 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e ->nExpr ) return
1d934 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 0;. for(i=0;
1d935 20 69 3c 70 41 2d 3e 70 4c 69 73 74 2d 3e 6e 45 i<pA->pList->nE
1d936 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
1d937 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 if( !sqlite3Exp
1d938 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 69 rCompare(pA->pLi
1d939 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 st->a[i].pExpr,
1d93a 70 42 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e pB->pList->a[i].
1d93b 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 pExpr) ){.
1d93c 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
1d93d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 }. }. }els
1d93e 65 20 69 66 28 20 70 42 2d 3e 70 4c 69 73 74 20 e if( pB->pList
1d93f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
1d940 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 2d 3e 70 . }. if( pA->p
1d941 53 65 6c 65 63 74 20 7c 7c 20 70 42 2d 3e 70 53 Select || pB->pS
1d942 65 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 elect ) return 0
1d943 3b 0a 20 20 69 66 28 20 70 41 2d 3e 69 54 61 62 ;. if( pA->iTab
1d944 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c 65 20 7c le!=pB->iTable |
1d945 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 | pA->iColumn!=p
1d946 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20 72 65 74 B->iColumn ) ret
1d947 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 2d urn 0;. if( pA-
1d948 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 >op!=TK_COLUMN &
1d949 26 20 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 20 29 7b & pA->token.z ){
1d94a 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b . if( pB->tok
1d94b 65 6e 2e 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e en.z==0 ) return
1d94c 20 30 3b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 0;. if( pB->
1d94d 74 6f 6b 65 6e 2e 6e 21 3d 70 41 2d 3e 74 6f 6b token.n!=pA->tok
1d94e 65 6e 2e 6e 20 29 20 72 65 74 75 72 6e 20 30 3b en.n ) return 0;
1d94f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1d950 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 StrNICmp((char*)
1d951 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 2c 28 63 68 61 pA->token.z,(cha
1d952 72 2a 29 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 2c 70 r*)pB->token.z,p
1d953 42 2d 3e 74 6f 6b 65 6e 2e 6e 29 21 3d 30 20 29 B->token.n)!=0 )
1d954 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
1d955 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1d956 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn 1;.}.../*.*
1d957 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d * Add a new elem
1d958 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 ent to the pAggI
1d959 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61 nfo->aCol[] arra
1d95a 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 y. Return the i
1d95b 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e ndex of.** the n
1d95c 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 ew element. Ret
1d95d 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e urn a negative n
1d95e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 umber if malloc
1d95f 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 fails..*/.static
1d960 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 43 int addAggInfoC
1d961 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64 olumn(sqlite3 *d
1d962 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 b, AggInfo *pInf
1d963 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 o){. int i;. p
1d964 49 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c Info->aCol = sql
1d965 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 ite3ArrayAllocat
1d966 65 28 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 20 e(. db,.
1d967 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c pInfo->aCol
1d968 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 ,. sizeof(
1d969 70 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c pInfo->aCol[0]),
1d96a 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 . 3,.
1d96b 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d &pInfo->nColum
1d96c 6e 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f n,. &pInfo
1d96d 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a ->nColumnAlloc,.
1d96e 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 &i. );.
1d96f 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 return i;.}
1d970 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 ../*.** Add a ne
1d971 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 w element to the
1d972 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 pAggInfo->aFunc
1d973 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 [] array. Retur
1d974 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a n the index of.*
1d975 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e * the new elemen
1d976 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 t. Return a neg
1d977 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 ative number if
1d978 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f malloc fails..*/
1d979 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 .static int addA
1d97a 67 67 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74 ggInfoFunc(sqlit
1d97b 65 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20 e3 *db, AggInfo
1d97c 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69 *pInfo){. int i
1d97d 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 ;. pInfo->aFunc
1d97e 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 = sqlite3ArrayA
1d97f 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20 llocate(.
1d980 64 62 2c 20 0a 20 20 20 20 20 20 20 70 49 6e 66 db, . pInf
1d981 6f 2d 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 20 o->aFunc,.
1d982 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 sizeof(pInfo->a
1d983 46 75 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20 Func[0]),.
1d984 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 3,. &pInf
1d985 6f 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20 o->nFunc,.
1d986 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c &pInfo->nFuncAl
1d987 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20 loc,. &i.
1d988 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a );. return i;.
1d989 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 } ../*.** Thi
1d98a 73 20 69 73 20 74 68 65 20 78 45 78 70 72 43 61 s is the xExprCa
1d98b 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65 llback for a tre
1d98c 65 20 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73 e walker. It is
1d98d 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c used to.** impl
1d98e 65 6d 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70 ement sqlite3Exp
1d98f 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 rAnalyzeAggregat
1d990 65 73 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74 es(). See sqlit
1d991 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 e3ExprAnalyzeAgg
1d992 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61 regates.** for a
1d993 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1d994 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ation..*/.static
1d995 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 int analyzeAggr
1d996 65 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57 egate(Walker *pW
1d997 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 alker, Expr *pEx
1d998 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 pr){. int i;.
1d999 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 NameContext *pNC
1d99a 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e = pWalker->u.pN
1d99b 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 C;. Parse *pPar
1d99c 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
1d99d 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 ;. SrcList *pSr
1d99e 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 cList = pNC->pSr
1d99f 63 4c 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f cList;. AggInfo
1d9a0 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43 *pAggInfo = pNC
1d9a1 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73 ->pAggInfo;.. s
1d9a2 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 witch( pExpr->op
1d9a3 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ){. case TK_
1d9a4 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 AGG_COLUMN:.
1d9a5 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 case TK_COLUMN:
1d9a6 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
1d9a7 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
1d9a8 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 AGG_COLUMN );.
1d9a9 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
1d9aa 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 xpr->op==TK_COLU
1d9ab 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43 MN );. /* C
1d9ac 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
1d9ad 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20 he column is in
1d9ae 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 one of the table
1d9af 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20 s in the FROM.
1d9b0 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66 ** clause of
1d9b1 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71 the aggregate q
1d9b2 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66 uery */. if
1d9b3 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 ( pSrcList ){.
1d9b4 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 struct Src
1d9b5 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1d9b6 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 0a = pSrcList->a;.
1d9b7 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b for(i=0;
1d9b8 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 i<pSrcList->nSr
1d9b9 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 c; i++, pItem++)
1d9ba 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75 {. stru
1d9bb 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a ct AggInfo_col *
1d9bc 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 pCol;.
1d9bd 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c if( pExpr->iTabl
1d9be 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f e==pItem->iCurso
1d9bf 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 r ){.
1d9c0 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 /* If we reach
1d9c1 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d this point, it m
1d9c2 65 61 6e 73 20 74 68 61 74 20 70 45 78 70 72 20 eans that pExpr
1d9c3 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c refers to a tabl
1d9c4 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a e. **
1d9c5 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 that is in the
1d9c6 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 FROM clause of t
1d9c7 68 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65 he aggregate que
1d9c8 72 79 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20 ry. .
1d9c9 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20 **.
1d9ca 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 ** Make an entr
1d9cb 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e y for the column
1d9cc 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 in pAggInfo->aC
1d9cd 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20 ol[] if there.
1d9ce 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 ** is
1d9cf 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65 not an entry the
1d9d0 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 re already..
1d9d1 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1d9d2 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 int k;.
1d9d3 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d pCol =
1d9d4 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b pAggInfo->aCol;
1d9d5 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 . for
1d9d6 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f (k=0; k<pAggInfo
1d9d7 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20 ->nColumn; k++,
1d9d8 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 pCol++){.
1d9d9 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d if( pCol-
1d9da 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e >iTable==pExpr->
1d9db 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 iTable &&.
1d9dc 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c pCol
1d9dd 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 ->iColumn==pExpr
1d9de 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 ->iColumn ){.
1d9df 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 bre
1d9e0 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ak;.
1d9e1 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
1d9e2 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 }. if
1d9e3 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e ( (k>=pAggInfo->
1d9e4 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20 nColumn).
1d9e5 20 20 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64 && (k = ad
1d9e6 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70 dAggInfoColumn(p
1d9e7 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 Parse->db, pAggI
1d9e8 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20 nfo))>=0 .
1d9e9 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
1d9ea 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 pCol = &p
1d9eb 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d AggInfo->aCol[k]
1d9ec 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1d9ed 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78 pCol->pTab = pEx
1d9ee 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 pr->pTab;.
1d9ef 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54 pCol->iT
1d9f0 61 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54 able = pExpr->iT
1d9f1 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 able;.
1d9f2 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d pCol->iColum
1d9f3 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 n = pExpr->iColu
1d9f4 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 mn;.
1d9f5 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b pCol->iMem = +
1d9f6 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
1d9f7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f pCo
1d9f8 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e l->iSorterColumn
1d9f9 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 = -1;.
1d9fa 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72 pCol->pExpr
1d9fb 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 = pExpr;.
1d9fc 20 20 20 20 20 20 20 20 69 66 28 20 70 41 67 67 if( pAgg
1d9fd 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29 Info->pGroupBy )
1d9fe 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1d9ff 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20 int j, n;.
1da00 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 Expr
1da01 4c 69 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67 List *pGB = pAgg
1da02 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a Info->pGroupBy;.
1da03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da04 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1da05 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47 item *pTerm = pG
1da06 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20 B->a;.
1da07 20 20 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e n = pGB->n
1da08 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 Expr;.
1da09 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
1da0a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b <n; j++, pTerm++
1da0b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1da0c 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 Expr *pE =
1da0d 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 pTerm->pExpr;.
1da0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da0f 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43 if( pE->op==TK_C
1da10 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61 OLUMN && pE->iTa
1da11 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 ble==pExpr->iTab
1da12 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 le &&.
1da13 20 20 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e pE->
1da14 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e iColumn==pExpr->
1da15 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 iColumn ){.
1da16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1da17 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 Col->iSorterColu
1da18 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 mn = j;.
1da19 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
1da1a 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 k;.
1da1b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1da1c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1da1d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1da1e 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d if( pCol-
1da1f 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30 >iSorterColumn<0
1da20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1da21 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 pCol->iSorte
1da22 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e rColumn = pAggIn
1da23 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 fo->nSortingColu
1da24 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 mn++;.
1da25 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1da26 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
1da27 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20 /* There is now
1da28 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78 an entry for pEx
1da29 70 72 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e pr in pAggInfo->
1da2a 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20 aCol[] (either.
1da2b 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 ** be
1da2c 63 61 75 73 65 20 69 74 20 77 61 73 20 74 68 65 cause it was the
1da2d 72 65 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63 re before or bec
1da2e 61 75 73 65 20 77 65 20 6a 75 73 74 20 63 72 65 ause we just cre
1da2f 61 74 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20 ated it)..
1da30 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74 ** Convert
1da31 20 74 68 65 20 70 45 78 70 72 20 74 6f 20 62 65 the pExpr to be
1da32 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e a TK_AGG_COLUMN
1da33 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68 referring to th
1da34 61 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a at. *
1da35 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c * pAggInfo->aCol
1da36 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20 [] entry..
1da37 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1da38 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 pExpr->pAgg
1da39 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b Info = pAggInfo;
1da3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 . pEx
1da3b 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f pr->op = TK_AGG_
1da3c 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20 COLUMN;.
1da3d 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20 pExpr->iAgg
1da3e 3d 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = k;.
1da3f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1da40 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78 } /* endif pEx
1da41 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 pr->iTable==pIte
1da42 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20 m->iCursor */.
1da43 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c } /* end l
1da44 6f 6f 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73 oop over pSrcLis
1da45 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 t */. }.
1da46 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 return WRC_Pr
1da47 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 une;. }. c
1da48 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 ase TK_AGG_FUNCT
1da49 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 ION: {. /*
1da4a 54 68 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d The pNC->nDepth=
1da4b 3d 30 20 74 65 73 74 20 63 61 75 73 65 73 20 61 =0 test causes a
1da4c 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
1da4d 6e 73 20 69 6e 20 73 75 62 71 75 65 72 69 65 73 ns in subqueries
1da4e 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 . ** to be
1da4f 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 ignored */.
1da50 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 if( pNC->nDepth
1da51 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ==0 ){. /
1da52 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
1da53 66 20 70 45 78 70 72 20 69 73 20 61 20 64 75 70 f pExpr is a dup
1da54 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65 licate of anothe
1da55 72 20 61 67 67 72 65 67 61 74 65 20 0a 20 20 20 r aggregate .
1da56 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e ** function
1da57 20 74 68 61 74 20 69 73 20 61 6c 72 65 61 64 79 that is already
1da58 20 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f in the pAggInfo
1da59 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 20 structure.
1da5a 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74 */. st
1da5b 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e ruct AggInfo_fun
1da5c 63 20 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49 c *pItem = pAggI
1da5d 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20 nfo->aFunc;.
1da5e 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1da5f 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 AggInfo->nFunc;
1da60 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i++, pItem++){.
1da61 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
1da62 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 ite3ExprCompare(
1da63 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45 pItem->pExpr, pE
1da64 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 xpr) ){.
1da65 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1da66 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1da67 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d . if( i>=
1da68 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 pAggInfo->nFunc
1da69 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
1da6a 70 45 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61 pExpr is origina
1da6b 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65 l. Make a new e
1da6c 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f ntry in pAggInfo
1da6d 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20 ->aFunc[].
1da6e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
1da6f 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 u8 enc = ENC(pP
1da70 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 arse->db);.
1da71 20 20 20 20 20 69 20 3d 20 61 64 64 41 67 67 49 i = addAggI
1da72 6e 66 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e nfoFunc(pParse->
1da73 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20 db, pAggInfo);.
1da74 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d if( i>=
1da75 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
1da76 20 70 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e pItem = &pAggIn
1da77 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20 fo->aFunc[i];.
1da78 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d pItem-
1da79 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a >pExpr = pExpr;.
1da7a 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 pIte
1da7b 6d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 m->iMem = ++pPar
1da7c 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 se->nMem;.
1da7d 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 46 75 pItem->pFu
1da7e 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 nc = sqlite3Find
1da7f 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d Function(pParse-
1da80 3e 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 >db,.
1da81 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 (char*)p
1da82 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 Expr->token.z, p
1da83 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 0a 20 Expr->token.n,.
1da84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1da85 20 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 20 3f pExpr->pList ?
1da86 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e pExpr->pList->n
1da87 45 78 70 72 20 3a 20 30 2c 20 65 6e 63 2c 20 30 Expr : 0, enc, 0
1da88 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
1da89 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 f( pExpr->flags
1da8a 26 20 45 50 5f 44 69 73 74 69 6e 63 74 20 29 7b & EP_Distinct ){
1da8b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 . p
1da8c 49 74 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 Item->iDistinct
1da8d 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b = pParse->nTab++
1da8e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 ;. }e
1da8f 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
1da90 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69 pItem->iDisti
1da91 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 nct = -1;.
1da92 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1da93 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1da94 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 70 45 /* Make pE
1da95 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 xpr point to the
1da96 20 61 70 70 72 6f 70 72 69 61 74 65 20 70 41 67 appropriate pAg
1da97 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 65 gInfo->aFunc[] e
1da98 6e 74 72 79 0a 20 20 20 20 20 20 20 20 2a 2f 0a ntry. */.
1da99 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1da9a 41 67 67 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 Agg = i;.
1da9b 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f pExpr->pAggInfo
1da9c 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 = pAggInfo;.
1da9d 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
1da9e 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 7d 0a 20 Prune;. }.
1da9f 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1daa0 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a n WRC_Continue;.
1daa1 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61 }.static int ana
1daa2 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 49 6e lyzeAggregatesIn
1daa3 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 Select(Walker *p
1daa4 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a Walker, Select *
1daa5 70 53 65 6c 65 63 74 29 7b 0a 20 20 4e 61 6d 65 pSelect){. Name
1daa6 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 Context *pNC = p
1daa7 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 Walker->u.pNC;.
1daa8 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 if( pNC->nDepth
1daa9 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e ==0 ){. pNC->
1daaa 6e 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 73 71 nDepth++;. sq
1daab 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
1daac 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c 65 63 74 pWalker, pSelect
1daad 29 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70 );. pNC->nDep
1daae 74 68 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e th--;. return
1daaf 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 65 WRC_Prune;. }e
1dab0 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
1dab1 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 WRC_Continue;.
1dab2 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 }.}../*.** Analy
1dab3 7a 65 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 ze the given exp
1dab4 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69 6e 67 20 ression looking
1dab5 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75 for aggregate fu
1dab6 6e 63 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 nctions and.** f
1dab7 6f 72 20 76 61 72 69 61 62 6c 65 73 20 74 68 61 or variables tha
1dab8 74 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64 64 t need to be add
1dab9 65 64 20 74 6f 20 74 68 65 20 70 50 61 72 73 65 ed to the pParse
1daba 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 2e 0a ->aAgg[] array..
1dabb 2a 2a 20 4d 61 6b 65 20 61 64 64 69 74 69 6f 6e ** Make addition
1dabc 61 6c 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 al entries to th
1dabd 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d e pParse->aAgg[]
1dabe 20 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73 array as necess
1dabf 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ary..**.** This
1dac0 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f routine should o
1dac1 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 61 66 nly be called af
1dac2 74 65 72 20 74 68 65 20 65 78 70 72 65 73 73 69 ter the expressi
1dac3 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 on has been.** a
1dac4 6e 61 6c 79 7a 65 64 20 62 79 20 73 71 6c 69 74 nalyzed by sqlit
1dac5 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
1dac6 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f es()..*/.SQLITE_
1dac7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1dac8 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 ite3ExprAnalyzeA
1dac9 67 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f ggregates(NameCo
1daca 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72 ntext *pNC, Expr
1dacb 20 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b *pExpr){. Walk
1dacc 65 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 er w;. w.xExprC
1dacd 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a allback = analyz
1dace 65 41 67 67 72 65 67 61 74 65 3b 0a 20 20 77 2e eAggregate;. w.
1dacf 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 xSelectCallback
1dad0 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 = analyzeAggrega
1dad1 74 65 73 49 6e 53 65 6c 65 63 74 3b 0a 20 20 77 tesInSelect;. w
1dad2 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 .u.pNC = pNC;.
1dad3 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1dad4 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f &w, pExpr);.}../
1dad5 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 *.** Call sqlite
1dad6 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 3ExprAnalyzeAggr
1dad7 65 67 61 74 65 73 28 29 20 66 6f 72 20 65 76 65 egates() for eve
1dad8 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e ry expression in
1dad9 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f an.** expressio
1dada 6e 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 20 n list. Return
1dadb 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 the number of er
1dadc 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 rors..**.** If a
1dadd 6e 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e 64 n error is found
1dade 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 69 , the analysis i
1dadf 73 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f 0a s cut short..*/.
1dae0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1dae1 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 oid sqlite3ExprA
1dae2 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61 nalyzeAggList(Na
1dae3 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 meContext *pNC,
1dae4 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 ExprList *pList)
1dae5 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c {. struct ExprL
1dae6 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
1dae7 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
1dae8 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 pList ){. for
1dae9 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c (pItem=pList->a,
1daea 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e i=0; i<pList->n
1daeb 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
1daec 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
1daed 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 e3ExprAnalyzeAgg
1daee 72 65 67 61 74 65 73 28 70 4e 43 2c 20 70 49 74 regates(pNC, pIt
1daef 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 em->pExpr);.
1daf0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 }. }.}../*.** A
1daf1 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61 6c 6c llocate or deall
1daf2 6f 63 61 74 65 20 74 65 6d 70 6f 72 61 72 79 20 ocate temporary
1daf3 75 73 65 20 72 65 67 69 73 74 65 72 73 20 64 75 use registers du
1daf4 72 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61 ring code genera
1daf5 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
1daf6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1daf7 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 61 te3GetTempReg(Pa
1daf8 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 rse *pParse){.
1daf9 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d if( pParse->nTem
1dafa 70 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 72 pReg==0 ){. r
1dafb 65 74 75 72 6e 20 2b 2b 70 50 61 72 73 65 2d 3e eturn ++pParse->
1dafc 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 nMem;. }. retu
1dafd 72 6e 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 rn pParse->aTemp
1dafe 52 65 67 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e 54 Reg[--pParse->nT
1daff 65 6d 70 52 65 67 5d 3b 0a 7d 0a 53 51 4c 49 54 empReg];.}.SQLIT
1db00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1db01 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
1db02 70 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72 pReg(Parse *pPar
1db03 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 se, int iReg){.
1db04 20 69 66 28 20 69 52 65 67 20 26 26 20 70 50 61 if( iReg && pPa
1db05 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c 41 72 rse->nTempReg<Ar
1db06 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d 3e raySize(pParse->
1db07 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 20 20 aTempReg) ){.
1db08 20 73 71 6c 69 74 65 33 45 78 70 72 57 72 69 74 sqlite3ExprWrit
1db09 61 62 6c 65 52 65 67 69 73 74 65 72 28 70 50 61 ableRegister(pPa
1db0a 72 73 65 2c 20 69 52 65 67 2c 20 69 52 65 67 29 rse, iReg, iReg)
1db0b 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54 ;. pParse->aT
1db0c 65 6d 70 52 65 67 5b 70 50 61 72 73 65 2d 3e 6e empReg[pParse->n
1db0d 54 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 69 52 65 TempReg++] = iRe
1db0e 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 g;. }.}../*.**
1db0f 41 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61 6c Allocate or deal
1db10 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f locate a block o
1db11 66 20 6e 52 65 67 20 63 6f 6e 73 65 63 75 74 69 f nReg consecuti
1db12 76 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2f 0a ve registers.*/.
1db13 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1db14 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d nt sqlite3GetTem
1db15 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50 pRange(Parse *pP
1db16 61 72 73 65 2c 20 69 6e 74 20 6e 52 65 67 29 7b arse, int nReg){
1db17 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 . int i, n;. i
1db18 20 3d 20 70 50 61 72 73 65 2d 3e 69 52 61 6e 67 = pParse->iRang
1db19 65 52 65 67 3b 0a 20 20 6e 20 3d 20 70 50 61 72 eReg;. n = pPar
1db1a 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 3b 0a 20 se->nRangeReg;.
1db1b 20 69 66 28 20 6e 52 65 67 3c 3d 6e 20 26 26 20 if( nReg<=n &&
1db1c 21 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 !usedAsColumnCac
1db1d 68 65 28 70 50 61 72 73 65 2c 20 69 2c 20 69 2b he(pParse, i, i+
1db1e 6e 2d 31 29 20 29 7b 0a 20 20 20 20 70 50 61 72 n-1) ){. pPar
1db1f 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 20 2b 3d se->iRangeReg +=
1db20 20 6e 52 65 67 3b 0a 20 20 20 20 70 50 61 72 73 nReg;. pPars
1db21 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 2d 3d 20 e->nRangeReg -=
1db22 6e 52 65 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 nReg;. }else{.
1db23 20 20 20 69 20 3d 20 70 50 61 72 73 65 2d 3e 6e i = pParse->n
1db24 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61 72 73 Mem+1;. pPars
1db25 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b e->nMem += nReg;
1db26 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b . }. return i;
1db27 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
1db28 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
1db29 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 50 leaseTempRange(P
1db2a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
1db2b 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 t iReg, int nReg
1db2c 29 7b 0a 20 20 69 66 28 20 6e 52 65 67 3e 70 50 ){. if( nReg>pP
1db2d 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 arse->nRangeReg
1db2e 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e ){. pParse->n
1db2f 52 61 6e 67 65 52 65 67 20 3d 20 6e 52 65 67 3b RangeReg = nReg;
1db30 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52 61 . pParse->iRa
1db31 6e 67 65 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 ngeReg = iReg;.
1db32 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
1db33 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 65 78 70 ***** End of exp
1db34 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a r.c ************
1db35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db37 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1db38 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1db39 20 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a alter.c *******
1db3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db3c 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 ****/./*.** 2005
1db3d 20 46 65 62 72 75 61 72 79 20 31 35 0a 2a 2a 0a February 15.**.
1db3e 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
1db3f 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
1db40 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
1db41 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
1db42 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1db43 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1db44 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1db45 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1db46 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1db47 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1db48 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1db49 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1db4a 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
1db4b 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1db4c 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
1db4d 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
1db4e 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
1db4f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1db50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1db53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
1db54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
1db55 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 s C code routine
1db56 73 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 67 s that used to g
1db57 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64 enerate VDBE cod
1db58 65 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d e.** that implem
1db59 65 6e 74 73 20 74 68 65 20 41 4c 54 45 52 20 54 ents the ALTER T
1db5a 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a ABLE command..**
1db5b 0a 2a 2a 20 24 49 64 3a 20 61 6c 74 65 72 2e 63 .** $Id: alter.c
1db5c 2c 76 20 31 2e 35 30 20 32 30 30 38 2f 31 31 2f ,v 1.50 2008/11/
1db5d 31 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 19 09:05:27 dani
1db5e 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
1db5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 ../*.** The code
1db60 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 6e in this file on
1db61 6c 79 20 65 78 69 73 74 73 20 69 66 20 77 65 20 ly exists if we
1db62 61 72 65 20 6e 6f 74 20 6f 6d 69 74 74 69 6e 67 are not omitting
1db63 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41 the.** ALTER TA
1db64 42 4c 45 20 6c 6f 67 69 63 20 66 72 6f 6d 20 74 BLE logic from t
1db65 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 he build..*/.#if
1db66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1db67 5f 41 4c 54 45 52 54 41 42 4c 45 0a 0a 0a 2f 2a _ALTERTABLE.../*
1db68 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1db69 6e 20 69 73 20 75 73 65 64 20 62 79 20 53 51 4c n is used by SQL
1db6a 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d generated to im
1db6b 70 6c 65 6d 65 6e 74 20 74 68 65 20 0a 2a 2a 20 plement the .**
1db6c 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d ALTER TABLE comm
1db6d 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 and. The first a
1db6e 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 74 rgument is the t
1db6f 65 78 74 20 6f 66 20 61 20 43 52 45 41 54 45 20 ext of a CREATE
1db70 54 41 42 4c 45 20 6f 72 0a 2a 2a 20 43 52 45 41 TABLE or.** CREA
1db71 54 45 20 49 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 TE INDEX command
1db72 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73 20 . The second is
1db73 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 a table name. Th
1db74 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 e table name in
1db75 0a 2a 2a 20 74 68 65 20 43 52 45 41 54 45 20 54 .** the CREATE T
1db76 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20 49 ABLE or CREATE I
1db77 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 69 NDEX statement i
1db78 73 20 72 65 70 6c 61 63 65 64 20 77 69 74 68 20 s replaced with
1db79 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 the third.** arg
1db7a 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72 65 ument and the re
1db7b 73 75 6c 74 20 72 65 74 75 72 6e 65 64 2e 20 45 sult returned. E
1db7c 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 73 xamples:.**.** s
1db7d 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 qlite_rename_tab
1db7e 6c 65 28 27 43 52 45 41 54 45 20 54 41 42 4c 45 le('CREATE TABLE
1db7f 20 61 62 63 28 61 2c 20 62 2c 20 63 29 27 2c 20 abc(a, b, c)',
1db80 27 64 65 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 'def').** ->
1db81 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 20 64 'CREATE TABLE d
1db82 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2a 0a ef(a, b, c)'.**.
1db83 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 ** sqlite_rename
1db84 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45 20 49 _table('CREATE I
1db85 4e 44 45 58 20 69 20 4f 4e 20 61 62 63 28 61 29 NDEX i ON abc(a)
1db86 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20 20 20 ', 'def').**
1db87 20 2d 3e 20 27 43 52 45 41 54 45 20 49 4e 44 45 -> 'CREATE INDE
1db88 58 20 69 20 4f 4e 20 64 65 66 28 61 2c 20 62 2c X i ON def(a, b,
1db89 20 63 29 27 0a 2a 2f 0a 73 74 61 74 69 63 20 76 c)'.*/.static v
1db8a 6f 69 64 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 oid renameTableF
1db8b 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
1db8c 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
1db8d 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a . int NotUsed,.
1db8e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
1db8f 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69 **argv.){. unsi
1db90 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 gned char const
1db91 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f *zSql = sqlite3_
1db92 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
1db93 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 0]);. unsigned
1db94 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62 char const *zTab
1db95 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 leName = sqlite3
1db96 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
1db97 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b [1]);.. int tok
1db98 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d en;. Token tnam
1db99 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 e;. unsigned ch
1db9a 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d ar const *zCsr =
1db9b 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e zSql;. int len
1db9c 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52 = 0;. char *zR
1db9d 65 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 20 2a et;.. sqlite3 *
1db9e 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
1db9f 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
1dba0 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53 ontext);.. UNUS
1dba1 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
1dba2 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 Used);.. /* The
1dba3 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 principle used
1dba4 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 to locate the ta
1dba5 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 ble name in the
1dba6 43 52 45 41 54 45 20 54 41 42 4c 45 20 0a 20 20 CREATE TABLE .
1dba7 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 ** statement is
1dba8 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 6e that the table n
1dba9 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73 74 ame is the first
1dbaa 20 6e 6f 6e 2d 73 70 61 63 65 20 74 6f 6b 65 6e non-space token
1dbab 20 74 68 61 74 0a 20 20 2a 2a 20 69 73 20 69 6d that. ** is im
1dbac 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 mediately follow
1dbad 65 64 20 62 79 20 61 20 54 4b 5f 4c 50 20 6f 72 ed by a TK_LP or
1dbae 20 54 4b 5f 55 53 49 4e 47 20 74 6f 6b 65 6e 2e TK_USING token.
1dbaf 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53 71 6c . */. if( zSql
1dbb0 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 ){. do {.
1dbb1 20 20 20 69 66 28 20 21 2a 7a 43 73 72 20 29 7b if( !*zCsr ){
1dbb2 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 61 6e 20 . /* Ran
1dbb3 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 62 65 66 out of input bef
1dbb4 6f 72 65 20 66 69 6e 64 69 6e 67 20 61 6e 20 6f ore finding an o
1dbb5 70 65 6e 69 6e 67 20 62 72 61 63 6b 65 74 2e 20 pening bracket.
1dbb6 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a Return NULL. */.
1dbb7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a return;.
1dbb8 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f }.. /
1dbb9 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 * Store the toke
1dbba 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e n that zCsr poin
1dbbb 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 ts to in tname.
1dbbc 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a */. tname.z
1dbbd 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 = zCsr;. t
1dbbe 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 name.n = len;..
1dbbf 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 /* Advance
1dbc0 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74 zCsr to the next
1dbc1 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 token. Store th
1dbc2 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e at token type in
1dbc3 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 'token',.
1dbc4 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 ** and its lengt
1dbc5 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 h in 'len' (to b
1dbc6 65 20 75 73 65 64 20 6e 65 78 74 20 69 74 65 72 e used next iter
1dbc7 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f ation of this lo
1dbc8 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 op).. */.
1dbc9 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 do {.
1dbca 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 zCsr += len;.
1dbcb 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 len = sqli
1dbcc 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 te3GetToken(zCsr
1dbcd 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 , &token);.
1dbce 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d } while( token=
1dbcf 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 20 =TK_SPACE );.
1dbd0 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30 assert( len>0
1dbd1 20 29 3b 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 );. } while(
1dbd2 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20 26 26 token!=TK_LP &&
1dbd3 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49 4e 47 token!=TK_USING
1dbd4 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74 20 3d 20 );.. zRet =
1dbd5 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1dbd6 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73 b, "%.*s\"%w\"%s
1dbd7 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 ", tname.z - zSq
1dbd8 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20 l, zSql, .
1dbd9 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 zTableName, tna
1dbda 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 me.z+tname.n);.
1dbdb 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
1dbdc 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
1dbdd 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 zRet, -1, SQLITE
1dbde 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d _DYNAMIC);. }.}
1dbdf 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1dbe0 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a _OMIT_TRIGGER./*
1dbe1 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1dbe2 73 20 75 73 65 64 20 62 79 20 53 51 4c 20 67 65 s used by SQL ge
1dbe3 6e 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 nerated to imple
1dbe4 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c 54 45 ment the.** ALTE
1dbe5 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e R TABLE command.
1dbe6 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
1dbe7 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 20 ent is the text
1dbe8 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49 47 of a CREATE TRIG
1dbe9 47 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e GER .** statemen
1dbea 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73 t. The second is
1dbeb 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 a table name. T
1dbec 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e he table name in
1dbed 20 74 68 65 20 43 52 45 41 54 45 20 0a 2a 2a 20 the CREATE .**
1dbee 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e TRIGGER statemen
1dbef 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 69 t is replaced wi
1dbf0 74 68 20 74 68 65 20 74 68 69 72 64 20 61 72 67 th the third arg
1dbf1 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72 65 ument and the re
1dbf2 73 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72 6e 65 sult .** returne
1dbf3 64 2e 20 54 68 69 73 20 69 73 20 61 6e 61 6c 61 d. This is anala
1dbf4 67 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65 54 61 gous to renameTa
1dbf5 62 6c 65 46 75 6e 63 28 29 20 61 62 6f 76 65 2c bleFunc() above,
1dbf6 20 65 78 63 65 70 74 20 66 6f 72 20 43 52 45 41 except for CREA
1dbf7 54 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c 20 6e TE.** TRIGGER, n
1dbf8 6f 74 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 ot CREATE INDEX
1dbf9 61 6e 64 20 43 52 45 41 54 45 20 54 41 42 4c 45 and CREATE TABLE
1dbfa 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1dbfb 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46 75 renameTriggerFu
1dbfc 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
1dbfd 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
1dbfe 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
1dbff 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1dc00 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67 *argv.){. unsig
1dc01 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a ned char const *
1dc02 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 zSql = sqlite3_v
1dc03 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
1dc04 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 ]);. unsigned c
1dc05 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c har const *zTabl
1dc06 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f eName = sqlite3_
1dc07 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
1dc08 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 1]);.. int toke
1dc09 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 n;. Token tname
1dc0a 3b 0a 20 20 69 6e 74 20 64 69 73 74 20 3d 20 33 ;. int dist = 3
1dc0b 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
1dc0c 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d 20 r const *zCsr =
1dc0d 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 zSql;. int len
1dc0e 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65 = 0;. char *zRe
1dc0f 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 t;. sqlite3 *db
1dc10 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 = sqlite3_conte
1dc11 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e xt_db_handle(con
1dc12 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45 44 text);.. UNUSED
1dc13 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
1dc14 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 ed);.. /* The p
1dc15 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 74 6f rinciple used to
1dc16 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c locate the tabl
1dc17 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52 e name in the CR
1dc18 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 20 20 EATE TRIGGER .
1dc19 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 ** statement is
1dc1a 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 6e that the table n
1dc1b 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73 74 ame is the first
1dc1c 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 69 token that is i
1dc1d 6d 6d 65 64 69 61 74 65 64 6c 79 0a 20 20 2a 2a mmediatedly. **
1dc1e 20 70 72 65 63 65 64 65 64 20 62 79 20 65 69 74 preceded by eit
1dc1f 68 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f her TK_ON or TK_
1dc20 44 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69 61 74 DOT and immediat
1dc21 65 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 edly followed by
1dc22 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f one. ** of TK_
1dc23 57 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e 20 6f WHEN, TK_BEGIN o
1dc24 72 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a 20 r TK_FOR.. */.
1dc25 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 if( zSql ){.
1dc26 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69 66 28 do {.. if(
1dc27 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 !*zCsr ){.
1dc28 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 /* Ran out of
1dc29 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69 input before fi
1dc2a 6e 64 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 nding the table
1dc2b 6e 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e 55 4c name. Return NUL
1dc2c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 L. */. re
1dc2d 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 turn;. }..
1dc2e 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 /* Store th
1dc2f 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73 e token that zCs
1dc30 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74 r points to in t
1dc31 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74 name. */. t
1dc32 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72 3b 0a 20 name.z = zCsr;.
1dc33 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c tname.n = l
1dc34 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 64 en;.. /* Ad
1dc35 76 61 6e 63 65 20 7a 43 73 72 20 74 6f 20 74 68 vance zCsr to th
1dc36 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53 74 e next token. St
1dc37 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 6e 20 74 ore that token t
1dc38 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a ype in 'token',.
1dc39 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 74 73 ** and its
1dc3a 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e 27 length in 'len'
1dc3b 20 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65 78 (to be used nex
1dc3c 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 t iteration of t
1dc3d 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 his loop)..
1dc3e 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 */. do {.
1dc3f 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 6c zCsr += l
1dc40 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 en;. len
1dc41 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 = sqlite3GetToke
1dc42 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29 3b n(zCsr, &token);
1dc43 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 74 . }while( t
1dc44 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29 oken==TK_SPACE )
1dc45 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1dc46 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 20 20 len>0 );..
1dc47 2f 2a 20 56 61 72 69 61 62 6c 65 20 27 64 69 73 /* Variable 'dis
1dc48 74 27 20 73 74 6f 72 65 73 20 74 68 65 20 6e 75 t' stores the nu
1dc49 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 72 mber of tokens r
1dc4a 65 61 64 20 73 69 6e 63 65 20 74 68 65 20 6d 6f ead since the mo
1dc4b 73 74 0a 20 20 20 20 20 20 2a 2a 20 72 65 63 65 st. ** rece
1dc4c 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b 5f nt TK_DOT or TK_
1dc4d 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 ON. This means t
1dc4e 68 61 74 20 77 68 65 6e 20 61 20 57 48 45 4e 2c hat when a WHEN,
1dc4f 20 46 4f 52 20 6f 72 20 42 45 47 49 4e 20 0a 20 FOR or BEGIN .
1dc50 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 73 ** token is
1dc51 20 72 65 61 64 20 61 6e 64 20 27 64 69 73 74 27 read and 'dist'
1dc52 20 65 71 75 61 6c 73 20 32 2c 20 74 68 65 20 63 equals 2, the c
1dc53 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 64 20 ondition stated
1dc54 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 above. ** t
1dc55 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20 20 20 20 o be met..
1dc56 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 **. ** Note
1dc57 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f 74 20 that ON cannot
1dc58 62 65 20 61 20 64 61 74 61 62 61 73 65 2c 20 74 be a database, t
1dc59 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 6e able or column n
1dc5a 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20 2a 2a ame, so. **
1dc5b 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
1dc5c 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 d to worry about
1dc5d 20 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a 20 20 syntax like .
1dc5e 20 20 20 20 2a 2a 20 22 43 52 45 41 54 45 20 54 ** "CREATE T
1dc5f 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 4f 4e RIGGER ... ON ON
1dc60 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 20 65 .ON BEGIN ..." e
1dc61 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 tc.. */.
1dc62 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20 20 20 dist++;.
1dc63 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 44 if( token==TK_D
1dc64 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 4b 5f OT || token==TK_
1dc65 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69 ON ){. di
1dc66 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a st = 0;. }.
1dc67 20 20 20 20 7d 20 77 68 69 6c 65 28 20 64 69 73 } while( dis
1dc68 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e 21 3d t!=2 || (token!=
1dc69 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b 65 6e TK_WHEN && token
1dc6a 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f 6b 65 !=TK_FOR && toke
1dc6b 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29 3b 0a n!=TK_BEGIN) );.
1dc6c 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 . /* Variable
1dc6d 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e 74 61 tname now conta
1dc6e 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 ins the token th
1dc6f 61 74 20 69 73 20 74 68 65 20 6f 6c 64 20 74 61 at is the old ta
1dc70 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 ble-name. **
1dc71 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 52 in the CREATE TR
1dc72 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e IGGER statement.
1dc73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52 65 74 . */. zRet
1dc74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
1dc75 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c f(db, "%.*s\"%w\
1dc76 22 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 "%s", tname.z -
1dc77 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 zSql, zSql, .
1dc78 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 zTableName,
1dc79 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 tname.z+tname.n)
1dc7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
1dc7b 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
1dc7c 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c t, zRet, -1, SQL
1dc7d 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 ITE_DYNAMIC);.
1dc7e 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 }.}.#endif /*
1dc7f 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 !SQLITE_OMIT_TRI
1dc80 47 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 GGER */../*.** R
1dc81 65 67 69 73 74 65 72 20 62 75 69 6c 74 2d 69 6e egister built-in
1dc82 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 functions used
1dc83 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e to help implemen
1dc84 74 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2f t ALTER TABLE.*/
1dc85 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1dc86 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 void sqlite3Alte
1dc87 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 rFunctions(sqlit
1dc88 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 e3 *db){. sqlit
1dc89 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c e3CreateFunc(db,
1dc8a 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f "sqlite_rename_
1dc8b 74 61 62 6c 65 22 2c 20 32 2c 20 53 51 4c 49 54 table", 2, SQLIT
1dc8c 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 E_UTF8, 0,.
1dc8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dc8e 20 20 20 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 renameTableF
1dc8f 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 69 66 6e unc, 0, 0);.#ifn
1dc90 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1dc91 54 52 49 47 47 45 52 0a 20 20 73 71 6c 69 74 65 TRIGGER. sqlite
1dc92 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 3CreateFunc(db,
1dc93 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 "sqlite_rename_t
1dc94 72 69 67 67 65 72 22 2c 20 32 2c 20 53 51 4c 49 rigger", 2, SQLI
1dc95 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 TE_UTF8, 0,.
1dc96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dc97 20 20 20 20 20 72 65 6e 61 6d 65 54 72 69 67 67 renameTrigg
1dc98 65 72 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 erFunc, 0, 0);.#
1dc99 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 endif.}../*.** G
1dc9a 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78 74 enerate the text
1dc9b 20 6f 66 20 61 20 57 48 45 52 45 20 65 78 70 72 of a WHERE expr
1dc9c 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63 61 6e ession which can
1dc9d 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c 65 be used to sele
1dc9e 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 6f 72 ct all.** tempor
1dc9f 61 72 79 20 74 72 69 67 67 65 72 73 20 6f 6e 20 ary triggers on
1dca0 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f 6d 20 table pTab from
1dca1 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f the sqlite_temp_
1dca2 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 66 master table. If
1dca3 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 68 .** table pTab h
1dca4 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 79 20 as no temporary
1dca5 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69 73 20 triggers, or is
1dca6 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20 69 6e itself stored in
1dca7 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 the .** tempora
1dca8 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e 55 4c ry database, NUL
1dca9 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a L is returned..*
1dcaa 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 77 /.static char *w
1dcab 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 hereTempTriggers
1dcac 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1dcad 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 Table *pTab){.
1dcae 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 0a Trigger *pTrig;.
1dcaf 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d char *zWhere =
1dcb0 20 30 3b 0a 20 20 63 68 61 72 20 2a 74 6d 70 20 0;. char *tmp
1dcb1 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63 68 = 0;. const Sch
1dcb2 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 6d 61 ema *pTempSchema
1dcb3 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 = pParse->db->a
1dcb4 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20 2f Db[1].pSchema; /
1dcb5 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65 6d 61 * Temp db schema
1dcb6 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 */.. /* If the
1dcb7 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c 6f table is not lo
1dcb8 63 61 74 65 64 20 69 6e 20 74 68 65 20 74 65 6d cated in the tem
1dcb9 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68 20 63 p-db (in which c
1dcba 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20 2a ase NULL is . *
1dcbb 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f 70 * returned, loop
1dcbc 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 62 through the tab
1dcbd 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72 69 67 les list of trig
1dcbe 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68 20 74 gers. For each t
1dcbf 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68 61 74 rigger. ** that
1dcc0 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 is not part of
1dcc1 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63 68 65 the temp-db sche
1dcc2 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75 73 65 ma, add a clause
1dcc3 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 0a 20 to the WHERE .
1dcc4 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 62 ** expression b
1dcc5 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 69 6e eing built up in
1dcc6 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 zWhere.. */.
1dcc7 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65 6d if( pTab->pSchem
1dcc8 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61 20 29 a!=pTempSchema )
1dcc9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 {. sqlite3 *d
1dcca 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1dccb 20 20 20 20 66 6f 72 28 20 70 54 72 69 67 3d 70 for( pTrig=p
1dccc 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20 70 Tab->pTrigger; p
1dccd 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72 69 Trig; pTrig=pTri
1dcce 67 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 g->pNext ){.
1dccf 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 53 63 if( pTrig->pSc
1dcd0 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65 6d hema==pTempSchem
1dcd1 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 a ){. if(
1dcd2 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 20 !zWhere ){.
1dcd3 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73 zWhere = s
1dcd4 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
1dcd5 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 70 54 72 , "name=%Q", pTr
1dcd6 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20 ig->name);.
1dcd7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1dcd8 20 20 20 20 74 6d 70 20 3d 20 7a 57 68 65 72 65 tmp = zWhere
1dcd9 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 57 68 65 ;. zWhe
1dcda 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 re = sqlite3MPri
1dcdb 6e 74 66 28 64 62 2c 20 22 25 73 20 4f 52 20 6e ntf(db, "%s OR n
1dcdc 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65 72 65 2c ame=%Q", zWhere,
1dcdd 20 70 54 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 pTrig->name);.
1dcde 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1dcdf 44 62 46 72 65 65 28 64 62 2c 20 74 6d 70 29 3b DbFree(db, tmp);
1dce0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1dce1 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
1dce2 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a eturn zWhere;.}.
1dce3 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
1dce4 63 6f 64 65 20 74 6f 20 64 72 6f 70 20 61 6e 64 code to drop and
1dce5 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 reload the inte
1dce6 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 rnal representat
1dce7 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a 2a 2a 20 ion of table.**
1dce8 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20 64 61 pTab from the da
1dce9 74 61 62 61 73 65 2c 20 69 6e 63 6c 75 64 69 6e tabase, includin
1dcea 67 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 74 g triggers and t
1dceb 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72 emporary trigger
1dcec 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a s..** Argument z
1dced 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 Name is the name
1dcee 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e of the table in
1dcef 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
1dcf0 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68 65 20 74 hema at.** the t
1dcf1 69 6d 65 20 74 68 65 20 67 65 6e 65 72 61 74 65 ime the generate
1dcf2 64 20 63 6f 64 65 20 69 73 20 65 78 65 63 75 74 d code is execut
1dcf3 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 ed. This can be
1dcf4 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a different from.*
1dcf5 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 69 66 * pTab->zName if
1dcf6 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
1dcf7 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 74 s being called t
1dcf8 6f 20 63 6f 64 65 20 70 61 72 74 20 6f 66 20 61 o code part of a
1dcf9 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20 54 41 42 n .** "ALTER TAB
1dcfa 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22 20 73 74 LE RENAME TO" st
1dcfb 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 atement..*/.stat
1dcfc 69 63 20 76 6f 69 64 20 72 65 6c 6f 61 64 54 61 ic void reloadTa
1dcfd 62 6c 65 53 63 68 65 6d 61 28 50 61 72 73 65 20 bleSchema(Parse
1dcfe 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a *pParse, Table *
1dcff 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 pTab, const char
1dd00 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 65 *zName){. Vdbe
1dd01 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 *v;. char *zWh
1dd02 65 72 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 ere;. int iDb;
1dd03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dd04 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 /* Index of da
1dd05 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e tabase containin
1dd06 67 20 70 54 61 62 20 2a 2f 0a 23 69 66 6e 64 65 g pTab */.#ifnde
1dd07 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
1dd08 49 47 47 45 52 0a 20 20 54 72 69 67 67 65 72 20 IGGER. Trigger
1dd09 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69 66 0a 0a *pTrig;.#endif..
1dd0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 v = sqlite3Get
1dd0b 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 Vdbe(pParse);.
1dd0c 69 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b if( !v ) return;
1dd0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1dd0e 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d e3BtreeHoldsAllM
1dd0f 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 utexes(pParse->d
1dd10 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 b) );. iDb = sq
1dd11 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 lite3SchemaToInd
1dd12 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 ex(pParse->db, p
1dd13 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 Tab->pSchema);.
1dd14 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
1dd15 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
1dd16 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a TE_OMIT_TRIGGER.
1dd17 20 20 2f 2a 20 44 72 6f 70 20 61 6e 79 20 74 61 /* Drop any ta
1dd18 62 6c 65 20 74 72 69 67 67 65 72 73 20 66 72 6f ble triggers fro
1dd19 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 m the internal s
1dd1a 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 66 6f 72 28 chema. */. for(
1dd1b 70 54 72 69 67 3d 70 54 61 62 2d 3e 70 54 72 69 pTrig=pTab->pTri
1dd1c 67 67 65 72 3b 20 70 54 72 69 67 3b 20 70 54 72 gger; pTrig; pTr
1dd1d 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78 74 29 ig=pTrig->pNext)
1dd1e 7b 0a 20 20 20 20 69 6e 74 20 69 54 72 69 67 44 {. int iTrigD
1dd1f 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
1dd20 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d aToIndex(pParse-
1dd21 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53 63 68 >db, pTrig->pSch
1dd22 65 6d 61 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ema);. assert
1dd23 28 20 69 54 72 69 67 44 62 3d 3d 69 44 62 20 7c ( iTrigDb==iDb |
1dd24 7c 20 69 54 72 69 67 44 62 3d 3d 31 20 29 3b 0a | iTrigDb==1 );.
1dd25 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1dd26 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 ddOp4(v, OP_Drop
1dd27 54 72 69 67 67 65 72 2c 20 69 54 72 69 67 44 62 Trigger, iTrigDb
1dd28 2c 20 30 2c 20 30 2c 20 70 54 72 69 67 2d 3e 6e , 0, 0, pTrig->n
1dd29 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e ame, 0);. }.#en
1dd2a 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20 74 dif.. /* Drop t
1dd2b 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 he table and ind
1dd2c 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 ex from the inte
1dd2d 72 6e 61 6c 20 73 63 68 65 6d 61 20 2a 2f 0a 20 rnal schema */.
1dd2e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1dd2f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62 p4(v, OP_DropTab
1dd30 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 le, iDb, 0, 0, p
1dd31 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a Tab->zName, 0);.
1dd32 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65 . /* Reload the
1dd33 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 20 61 6e table, index an
1dd34 64 20 70 65 72 6d 61 6e 65 6e 74 20 74 72 69 67 d permanent trig
1dd35 67 65 72 20 73 63 68 65 6d 61 73 2e 20 2a 2f 0a ger schemas. */.
1dd36 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74 zWhere = sqlit
1dd37 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 e3MPrintf(pParse
1dd38 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d ->db, "tbl_name=
1dd39 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 %Q", zName);. i
1dd3a 66 28 20 21 7a 57 68 65 72 65 20 29 20 72 65 74 f( !zWhere ) ret
1dd3b 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 urn;. sqlite3Vd
1dd3c 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 beAddOp4(v, OP_P
1dd3d 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c arseSchema, iDb,
1dd3e 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 0, 0, zWhere, P
1dd3f 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 0a 23 69 66 4_DYNAMIC);..#if
1dd40 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1dd41 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 4e 6f _TRIGGER. /* No
1dd42 77 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 w, if the table
1dd43 69 73 20 6e 6f 74 20 73 74 6f 72 65 64 20 69 6e is not stored in
1dd44 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 the temp databa
1dd45 73 65 2c 20 72 65 6c 6f 61 64 20 61 6e 79 20 74 se, reload any t
1dd46 65 6d 70 20 0a 20 20 2a 2a 20 74 72 69 67 67 65 emp . ** trigge
1dd47 72 73 2e 20 44 6f 6e 27 74 20 75 73 65 20 49 4e rs. Don't use IN
1dd48 28 2e 2e 2e 29 20 69 6e 20 63 61 73 65 20 53 51 (...) in case SQ
1dd49 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
1dd4a 52 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20 0a RY is defined. .
1dd4b 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65 */. if( (zWhe
1dd4c 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67 re=whereTempTrig
1dd4d 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 gers(pParse, pTa
1dd4e 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 b))!=0 ){. sq
1dd4f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
1dd50 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d v, OP_ParseSchem
1dd51 61 2c 20 31 2c 20 30 2c 20 30 2c 20 7a 57 68 65 a, 1, 0, 0, zWhe
1dd52 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b re, P4_DYNAMIC);
1dd53 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f . }.#endif.}../
1dd54 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
1dd55 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 de to implement
1dd56 74 68 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45 the "ALTER TABLE
1dd57 20 78 78 78 20 52 45 4e 41 4d 45 20 54 4f 20 79 xxx RENAME TO y
1dd58 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e yy" .** command.
1dd59 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
1dd5a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1dd5b 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 AlterRenameTable
1dd5c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
1dd5d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e, /*
1dd5e 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e Parser context.
1dd5f 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 */. SrcList *p
1dd60 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 Src,
1dd61 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 /* The table to
1dd62 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b rename. */. Tok
1dd63 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 en *pName
1dd64 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 /* The ne
1dd65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f w table name. */
1dd66 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 .){. int iDb;
1dd67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dd68 2f 2a 20 44 61 74 61 62 61 73 65 20 74 68 61 74 /* Database that
1dd69 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61 contains the ta
1dd6a 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ble */. char *z
1dd6b 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
1dd6c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 /* Name of da
1dd6d 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20 tabase iDb */.
1dd6e 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 Table *pTab;
1dd6f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 /* Tab
1dd70 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 le being renamed
1dd71 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d */. char *zNam
1dd72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
1dd73 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 /* NULL-terminat
1dd74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 4e ed version of pN
1dd75 61 6d 65 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65 ame */ . sqlite
1dd76 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1dd77 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20 db; /* Database
1dd78 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
1dd79 69 6e 74 20 6e 54 61 62 4e 61 6d 65 3b 20 20 20 int nTabName;
1dd7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1dd7b 62 65 72 20 6f 66 20 55 54 46 2d 38 20 63 68 61 ber of UTF-8 cha
1dd7c 72 61 63 74 65 72 73 20 69 6e 20 7a 54 61 62 4e racters in zTabN
1dd7d 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ame */. const c
1dd7e 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 3b 20 20 har *zTabName;
1dd7f 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e /* Original n
1dd80 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1dd81 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 0a 23 */. Vdbe *v;.#
1dd82 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1dd83 49 54 5f 54 52 49 47 47 45 52 0a 20 20 63 68 61 IT_TRIGGER. cha
1dd84 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b 20 20 r *zWhere = 0;
1dd85 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 /* Where
1dd86 63 6c 61 75 73 65 20 74 6f 20 6c 6f 63 61 74 65 clause to locate
1dd87 20 74 65 6d 70 20 74 72 69 67 67 65 72 73 20 2a temp triggers *
1dd88 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 /.#endif. int i
1dd89 73 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 20 3d sVirtualRename =
1dd8a 20 30 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 0; /* True if
1dd8b 74 68 69 73 20 69 73 20 61 20 76 2d 74 61 62 6c this is a v-tabl
1dd8c 65 20 77 69 74 68 20 61 6e 20 78 52 65 6e 61 6d e with an xRenam
1dd8d 65 28 29 20 2a 2f 0a 20 20 0a 20 20 69 66 28 20 e() */. . if(
1dd8e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1dd8f 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e ) goto exit_ren
1dd90 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 61 73 73 ame_table;. ass
1dd91 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3d ert( pSrc->nSrc=
1dd92 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =1 );. assert(
1dd93 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
1dd94 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 sAllMutexes(pPar
1dd95 73 65 2d 3e 64 62 29 20 29 3b 0a 0a 20 20 70 54 se->db) );.. pT
1dd96 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 ab = sqlite3Loca
1dd97 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 teTable(pParse,
1dd98 30 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 0, pSrc->a[0].zN
1dd99 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e ame, pSrc->a[0].
1dd9a 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 zDatabase);. if
1dd9b 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 ( !pTab ) goto e
1dd9c 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 xit_rename_table
1dd9d 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 ;. iDb = sqlite
1dd9e 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 3SchemaToIndex(p
1dd9f 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d Parse->db, pTab-
1dda0 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 >pSchema);. zDb
1dda1 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
1dda2 7a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 47 65 74 zName;.. /* Get
1dda3 20 61 20 4e 55 4c 4c 20 74 65 72 6d 69 6e 61 74 a NULL terminat
1dda4 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 ed version of th
1dda5 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 e new table name
1dda6 2e 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 . */. zName = s
1dda7 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
1dda8 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a ken(db, pName);.
1dda9 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 20 67 if( !zName ) g
1ddaa 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f oto exit_rename_
1ddab 74 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 table;.. /* Che
1ddac 63 6b 20 74 68 61 74 20 61 20 74 61 62 6c 65 20 ck that a table
1ddad 6f 72 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 27 or index named '
1ddae 7a 4e 61 6d 65 27 20 64 6f 65 73 20 6e 6f 74 20 zName' does not
1ddaf 61 6c 72 65 61 64 79 20 65 78 69 73 74 0a 20 20 already exist.
1ddb0 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 ** in database i
1ddb1 44 62 2e 20 49 66 20 73 6f 2c 20 74 68 69 73 20 Db. If so, this
1ddb2 69 73 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a is an error.. *
1ddb3 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 /. if( sqlite3F
1ddb4 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 indTable(db, zNa
1ddb5 6d 65 2c 20 7a 44 62 29 20 7c 7c 20 73 71 6c 69 me, zDb) || sqli
1ddb6 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c te3FindIndex(db,
1ddb7 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a zName, zDb) ){.
1ddb8 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1ddb9 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 Msg(pParse, .
1ddba 20 20 20 20 20 22 74 68 65 72 65 20 69 73 20 61 "there is a
1ddbb 6c 72 65 61 64 79 20 61 6e 6f 74 68 65 72 20 74 lready another t
1ddbc 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 69 able or index wi
1ddbd 74 68 20 74 68 69 73 20 6e 61 6d 65 3a 20 25 73 th this name: %s
1ddbe 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 ", zName);. g
1ddbf 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f oto exit_rename_
1ddc0 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a table;. }.. /*
1ddc1 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 Make sure it is
1ddc2 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 74 61 not a system ta
1ddc3 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 ble being altere
1ddc4 64 2c 20 6f 72 20 61 20 72 65 73 65 72 76 65 64 d, or a reserved
1ddc5 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 68 61 74 20 name. ** that
1ddc6 74 68 65 20 74 61 62 6c 65 20 69 73 20 62 65 69 the table is bei
1ddc7 6e 67 20 72 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 ng renamed to..
1ddc8 20 2a 2f 0a 20 20 69 66 28 20 73 74 72 6c 65 6e */. if( strlen
1ddc9 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3e 36 20 (pTab->zName)>6
1ddca 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 && 0==sqlite3Str
1ddcb 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d NICmp(pTab->zNam
1ddcc 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 e, "sqlite_", 7)
1ddcd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1ddce 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1ddcf 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f "table %s may no
1ddd0 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70 t be altered", p
1ddd1 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 Tab->zName);.
1ddd2 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d goto exit_renam
1ddd3 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 e_table;. }. i
1ddd4 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 f( SQLITE_OK!=sq
1ddd5 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 lite3CheckObject
1ddd6 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 Name(pParse, zNa
1ddd7 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 me) ){. goto
1ddd8 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c exit_rename_tabl
1ddd9 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 e;. }..#ifndef
1ddda 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
1dddb 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 . if( pTab->pSe
1dddc 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 lect ){. sqli
1dddd 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1ddde 73 65 2c 20 22 76 69 65 77 20 25 73 20 6d 61 79 se, "view %s may
1dddf 20 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22 not be altered"
1dde0 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a , pTab->zName);.
1dde1 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 goto exit_re
1dde2 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a name_table;. }.
1dde3 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
1dde4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 SQLITE_OMIT_AUTH
1dde5 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 ORIZATION. /* I
1dde6 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72 nvoke the author
1dde7 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b ization callback
1dde8 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 . */. if( sqlit
1dde9 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
1ddea 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 se, SQLITE_ALTER
1ddeb 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 _TABLE, zDb, pTa
1ddec 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a b->zName, 0) ){.
1dded 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 goto exit_re
1ddee 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a name_table;. }.
1ddef 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
1ddf0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1ddf1 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 73 UALTABLE. if( s
1ddf2 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c qlite3ViewGetCol
1ddf3 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c umnNames(pParse,
1ddf4 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f pTab) ){. go
1ddf5 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 to exit_rename_t
1ddf6 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 able;. }. if(
1ddf7 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 IsVirtual(pTab)
1ddf8 26 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 && pTab->pMod->p
1ddf9 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 Module->xRename
1ddfa 29 7b 0a 20 20 20 20 69 73 56 69 72 74 75 61 6c ){. isVirtual
1ddfb 52 65 6e 61 6d 65 20 3d 20 31 3b 0a 20 20 7d 0a Rename = 1;. }.
1ddfc 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 #endif.. /* Beg
1ddfd 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e in a transaction
1ddfe 20 61 6e 64 20 63 6f 64 65 20 74 68 65 20 56 65 and code the Ve
1ddff 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72 20 64 rifyCookie for d
1de00 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a 20 20 atabase iDb. .
1de01 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79 20 74 ** Then modify t
1de02 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 he schema cookie
1de03 20 28 73 69 6e 63 65 20 74 68 65 20 41 4c 54 45 (since the ALTE
1de04 52 20 54 41 42 4c 45 20 6d 6f 64 69 66 69 65 73 R TABLE modifies
1de05 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 the. ** schema
1de06 29 2e 20 4f 70 65 6e 20 61 20 73 74 61 74 65 6d ). Open a statem
1de07 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1de08 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
1de09 61 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 a virtual. ** t
1de0a 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d able.. */. v =
1de0b 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
1de0c 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 pParse);. if( v
1de0d 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ==0 ){. goto
1de0e 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c exit_rename_tabl
1de0f 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 e;. }. sqlite3
1de10 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 BeginWriteOperat
1de11 69 6f 6e 28 70 50 61 72 73 65 2c 20 69 73 56 69 ion(pParse, isVi
1de12 72 74 75 61 6c 52 65 6e 61 6d 65 2c 20 69 44 62 rtualRename, iDb
1de13 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61 6e );. sqlite3Chan
1de14 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c geCookie(pParse,
1de15 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 iDb);.. /* If
1de16 74 68 69 73 20 69 73 20 61 20 76 69 72 74 75 61 this is a virtua
1de17 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b 65 20 l table, invoke
1de18 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20 66 75 the xRename() fu
1de19 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a 20 6f nction if. ** o
1de1a 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e 20 54 ne is defined. T
1de1b 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63 61 6c he xRename() cal
1de1c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64 69 66 lback will modif
1de1d 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a y the names. **
1de1e 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72 63 65 of any resource
1de1f 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76 2d s used by the v-
1de20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 table implementa
1de21 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 tion (including
1de22 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c 69 74 other. ** SQLit
1de23 65 20 74 61 62 6c 65 73 29 20 74 68 61 74 20 61 e tables) that a
1de24 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 re identified by
1de25 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1de26 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a virtual table..
1de27 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
1de28 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1de29 54 41 42 4c 45 0a 20 20 69 66 28 20 69 73 56 69 TABLE. if( isVi
1de2a 72 74 75 61 6c 52 65 6e 61 6d 65 20 29 7b 0a 20 rtualRename ){.
1de2b 20 20 20 69 6e 74 20 69 20 3d 20 2b 2b 70 50 61 int i = ++pPa
1de2c 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 rse->nMem;. s
1de2d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
1de2e 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 (v, OP_String8,
1de2f 30 2c 20 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 0, i, 0, zName,
1de30 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 0);. sqlite3V
1de31 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
1de32 56 52 65 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 30 VRename, i, 0, 0
1de33 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 ,(const char*)pT
1de34 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f 56 54 ab->pVtab, P4_VT
1de35 41 42 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a AB);. }.#endif.
1de36 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f 75 74 . /* figure out
1de37 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d 38 20 how many UTF-8
1de38 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 69 characters are i
1de39 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54 61 n zName */. zTa
1de3a 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e bName = pTab->zN
1de3b 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d 65 20 ame;. nTabName
1de3c 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 = sqlite3Utf8Cha
1de3d 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c 20 2d rLen(zTabName, -
1de3e 31 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 1);.. /* Modify
1de3f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 the sqlite_mast
1de40 65 72 20 74 61 62 6c 65 20 74 6f 20 75 73 65 20 er table to use
1de41 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 the new table na
1de42 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 me. */. sqlite3
1de43 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 NestedParse(pPar
1de44 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54 se,. "UPDAT
1de45 45 20 25 51 2e 25 73 20 53 45 54 20 22 0a 23 69 E %Q.%s SET ".#i
1de46 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
1de47 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 20 _TRIGGER.
1de48 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 "sql = sqlite
1de49 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73 71 _rename_table(sq
1de4a 6c 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73 65 0a l, %Q), ".#else.
1de4b 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d "sql =
1de4c 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 20 20 CASE ".
1de4d 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 20 3d "WHEN type =
1de4e 20 27 74 72 69 67 67 65 72 27 20 54 48 45 4e 20 'trigger' THEN
1de4f 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 sqlite_rename_tr
1de50 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 22 0a igger(sql, %Q)".
1de51 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c 53 "ELS
1de52 45 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f E sqlite_rename_
1de53 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 20 45 table(sql, %Q) E
1de54 4e 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20 20 20 ND, ".#endif.
1de55 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 "tbl_name
1de56 20 3d 20 25 51 2c 20 22 0a 20 20 20 20 20 20 20 = %Q, ".
1de57 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 45 20 "name = CASE
1de58 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 ". "W
1de59 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c 65 27 HEN type='table'
1de5a 20 54 48 45 4e 20 25 51 20 22 0a 20 20 20 20 20 THEN %Q ".
1de5b 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e 61 6d "WHEN nam
1de5c 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f 61 e LIKE 'sqlite_a
1de5d 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e 44 20 utoindex%%' AND
1de5e 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54 48 45 type='index' THE
1de5f 4e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 N ".
1de60 20 22 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e "'sqlite_autoin
1de61 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c 20 73 dex_' || %Q || s
1de62 75 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b 31 38 ubstr(name,%d+18
1de63 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 ) ".
1de64 22 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44 20 22 "ELSE name END "
1de65 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74 62 . "WHERE tb
1de66 6c 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20 22 0a l_name=%Q AND ".
1de67 20 20 20 20 20 20 20 20 20 20 22 28 74 79 70 65 "(type
1de68 3d 27 74 61 62 6c 65 27 20 4f 52 20 74 79 70 65 ='table' OR type
1de69 3d 27 69 6e 64 65 78 27 20 4f 52 20 74 79 70 65 ='index' OR type
1de6a 3d 27 74 72 69 67 67 65 72 27 29 3b 22 2c 20 0a ='trigger');", .
1de6b 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d zDb, SCHEM
1de6c 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e A_TABLE(iDb), zN
1de6d 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d ame, zName, zNam
1de6e 65 2c 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 e, .#ifndef SQLI
1de6f 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a TE_OMIT_TRIGGER.
1de70 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e zName,.#en
1de71 64 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c dif. zName,
1de72 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e nTabName, zTabN
1de73 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65 ame. );..#ifnde
1de74 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1de75 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a TOINCREMENT. /*
1de76 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 If the sqlite_s
1de77 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65 78 equence table ex
1de78 69 73 74 73 20 69 6e 20 74 68 69 73 20 64 61 74 ists in this dat
1de79 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64 61 abase, then upda
1de7a 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 74 68 te . ** it with
1de7b 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e the new table n
1de7c 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ame.. */. if(
1de7d 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 sqlite3FindTable
1de7e 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 (db, "sqlite_seq
1de7f 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a uence", zDb) ){.
1de80 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 sqlite3Neste
1de81 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 dParse(pParse,.
1de82 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c "UPDATE \
1de83 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 65 71 "%w\".sqlite_seq
1de84 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 20 3d uence set name =
1de85 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65 20 3d %Q WHERE name =
1de86 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20 7a 44 %Q",. zD
1de87 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e b, zName, pTab->
1de88 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 zName);. }.#end
1de89 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
1de8a 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a TE_OMIT_TRIGGER.
1de8b 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
1de8c 65 20 54 45 4d 50 20 74 72 69 67 67 65 72 73 20 e TEMP triggers
1de8d 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20 6d on this table, m
1de8e 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74 65 odify the sqlite
1de8f 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20 20 2a _temp_master. *
1de90 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74 20 64 * table. Don't d
1de91 6f 20 74 68 69 73 20 69 66 20 74 68 65 20 74 61 o this if the ta
1de92 62 6c 65 20 62 65 69 6e 67 20 41 4c 54 45 52 65 ble being ALTERe
1de93 64 20 69 73 20 69 74 73 65 6c 66 20 6c 6f 63 61 d is itself loca
1de94 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 ted in. ** the
1de95 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 0a 20 temp database..
1de96 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65 72 */. if( (zWher
1de97 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 e=whereTempTrigg
1de98 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 ers(pParse, pTab
1de99 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c ))!=0 ){. sql
1de9a 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
1de9b 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
1de9c 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65 5f "UPDATE sqlite_
1de9d 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54 20 temp_master SET
1de9e 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 73 ". "s
1de9f 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 ql = sqlite_rena
1dea0 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c 20 me_trigger(sql,
1dea1 25 51 29 2c 20 22 0a 20 20 20 20 20 20 20 20 20 %Q), ".
1dea2 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 "tbl_name = %
1dea3 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 Q ".
1dea4 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a 4e 61 "WHERE %s;", zNa
1dea5 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 me, zName, zWher
1dea6 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 e);. sqlite3D
1dea7 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65 bFree(db, zWhere
1dea8 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 );. }.#endif..
1dea9 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72 65 6c /* Drop and rel
1deaa 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c oad the internal
1deab 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 20 2a table schema. *
1deac 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 /. reloadTableS
1dead 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70 54 chema(pParse, pT
1deae 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78 69 ab, zName);..exi
1deaf 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3a 0a t_rename_table:.
1deb0 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 sqlite3SrcList
1deb1 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 Delete(db, pSrc)
1deb2 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1deb3 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a e(db, zName);.}.
1deb4 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
1deb5 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
1deb6 61 66 74 65 72 20 61 6e 20 22 41 4c 54 45 52 20 after an "ALTER
1deb7 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 TABLE ... ADD" s
1deb8 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 tatement.** has
1deb9 62 65 65 6e 20 70 61 72 73 65 64 2e 20 41 72 67 been parsed. Arg
1deba 75 6d 65 6e 74 20 70 43 6f 6c 44 65 66 20 63 6f ument pColDef co
1debb 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 ntains the text
1debc 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 6f of the new.** co
1debd 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e lumn definition.
1debe 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 .**.** The Table
1debf 20 73 74 72 75 63 74 75 72 65 20 70 50 61 72 73 structure pPars
1dec0 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 77 61 73 e->pNewTable was
1dec1 20 65 78 74 65 6e 64 65 64 20 74 6f 20 69 6e 63 extended to inc
1dec2 6c 75 64 65 0a 2a 2a 20 74 68 65 20 6e 65 77 20 lude.** the new
1dec3 63 6f 6c 75 6d 6e 20 64 75 72 69 6e 67 20 70 61 column during pa
1dec4 72 73 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rsing..*/.SQLITE
1dec5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1dec6 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 lite3AlterFinish
1dec7 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 AddColumn(Parse
1dec8 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a *pParse, Token *
1dec9 70 43 6f 6c 44 65 66 29 7b 0a 20 20 54 61 62 6c pColDef){. Tabl
1deca 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 e *pNew;
1decb 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 /* Copy of
1decc 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
1decd 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 le */. Table *p
1dece 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 Tab;
1decf 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 /* Table being
1ded0 20 61 6c 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e altered */. in
1ded1 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 t iDb;
1ded2 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
1ded3 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 ase number */.
1ded4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b const char *zDb;
1ded5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
1ded6 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 abase name */.
1ded7 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 const char *zTab
1ded8 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 ; /* Tab
1ded9 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 le name */. cha
1deda 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 r *zCol;
1dedb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d 74 /* Null-t
1dedc 65 72 6d 69 6e 61 74 65 64 20 63 6f 6c 75 6d 6e erminated column
1dedd 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 definition */.
1dede 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20 20 Column *pCol;
1dedf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1dee0 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a e new column */.
1dee1 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 Expr *pDflt;
1dee2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
1dee3 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 efault value for
1dee4 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 the new column
1dee5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
1dee6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1dee7 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
1dee8 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0a 0a 20 onnection; */..
1dee9 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 db = pParse->db
1deea 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
1deeb 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c nErr || db->mall
1deec 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 ocFailed ) retur
1deed 6e 3b 0a 20 20 70 4e 65 77 20 3d 20 70 50 61 72 n;. pNew = pPar
1deee 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 se->pNewTable;.
1deef 20 61 73 73 65 72 74 28 20 70 4e 65 77 20 29 3b assert( pNew );
1def0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1def1 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c te3BtreeHoldsAll
1def2 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20 Mutexes(db) );.
1def3 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
1def4 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 hemaToIndex(db,
1def5 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pNew->pSchema);.
1def6 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b zDb = db->aDb[
1def7 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7a 54 iDb].zName;. zT
1def8 61 62 20 3d 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 ab = pNew->zName
1def9 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 4e 65 77 ;. pCol = &pNew
1defa 2d 3e 61 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43 6f ->aCol[pNew->nCo
1defb 6c 2d 31 5d 3b 0a 20 20 70 44 66 6c 74 20 3d 20 l-1];. pDflt =
1defc 70 43 6f 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20 70 pCol->pDflt;. p
1defd 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e Tab = sqlite3Fin
1defe 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c dTable(db, zTab,
1deff 20 7a 44 62 29 3b 0a 20 20 61 73 73 65 72 74 28 zDb);. assert(
1df00 20 70 54 61 62 20 29 3b 0a 0a 23 69 66 6e 64 65 pTab );..#ifnde
1df01 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1df02 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a THORIZATION. /*
1df03 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 Invoke the auth
1df04 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 orization callba
1df05 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c ck. */. if( sql
1df06 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
1df07 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 arse, SQLITE_ALT
1df08 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 ER_TABLE, zDb, p
1df09 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 Tab->zName, 0) )
1df0a 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
1df0b 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 }.#endif.. /* I
1df0c 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 f the default va
1df0d 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 lue for the new
1df0e 63 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65 63 69 column was speci
1df0f 66 69 65 64 20 77 69 74 68 20 61 20 0a 20 20 2a fied with a . *
1df10 2a 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c 20 * literal NULL,
1df11 74 68 65 6e 20 73 65 74 20 70 44 66 6c 74 20 74 then set pDflt t
1df12 6f 20 30 2e 20 54 68 69 73 20 73 69 6d 70 6c 69 o 0. This simpli
1df13 66 69 65 73 20 63 68 65 63 6b 69 6e 67 0a 20 20 fies checking.
1df14 2a 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20 4e 55 ** for an SQL NU
1df15 4c 4c 20 64 65 66 61 75 6c 74 20 62 65 6c 6f 77 LL default below
1df16 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 .. */. if( pDf
1df17 6c 74 20 26 26 20 70 44 66 6c 74 2d 3e 6f 70 3d lt && pDflt->op=
1df18 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 =TK_NULL ){.
1df19 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a pDflt = 0;. }..
1df1a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 /* Check that
1df1b 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 the new column i
1df1c 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 s not specified
1df1d 61 73 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f as PRIMARY KEY o
1df1e 72 20 55 4e 49 51 55 45 2e 0a 20 20 2a 2a 20 49 r UNIQUE.. ** I
1df1f 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 f there is a NOT
1df20 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 NULL constraint
1df21 2c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75 , then the defau
1df22 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
1df23 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75 73 . ** column mus
1df24 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 20 t not be NULL..
1df25 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e */. if( pCol->
1df26 69 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20 isPrimKey ){.
1df27 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1df28 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 (pParse, "Cannot
1df29 20 61 64 64 20 61 20 50 52 49 4d 41 52 59 20 4b add a PRIMARY K
1df2a 45 59 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20 EY column");.
1df2b 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
1df2c 66 28 20 70 4e 65 77 2d 3e 70 49 6e 64 65 78 20 f( pNew->pIndex
1df2d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1df2e 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1df2f 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 55 4e 49 Cannot add a UNI
1df30 51 55 45 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 QUE column");.
1df31 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
1df32 69 66 28 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c if( pCol->notNul
1df33 6c 20 26 26 20 21 70 44 66 6c 74 20 29 7b 0a 20 l && !pDflt ){.
1df34 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1df35 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 sg(pParse, .
1df36 20 20 20 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 "Cannot add
1df37 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 6d a NOT NULL colum
1df38 6e 20 77 69 74 68 20 64 65 66 61 75 6c 74 20 76 n with default v
1df39 61 6c 75 65 20 4e 55 4c 4c 22 29 3b 0a 20 20 20 alue NULL");.
1df3a 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 return;. }..
1df3b 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 64 65 /* Ensure the de
1df3c 66 61 75 6c 74 20 65 78 70 72 65 73 73 69 6f 6e fault expression
1df3d 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 is something th
1df3e 61 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 at sqlite3ValueF
1df3f 72 6f 6d 45 78 70 72 28 29 0a 20 20 2a 2a 20 63 romExpr(). ** c
1df40 61 6e 20 68 61 6e 64 6c 65 20 28 69 2e 65 2e 20 an handle (i.e.
1df41 6e 6f 74 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 not CURRENT_TIME
1df42 20 65 74 63 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 etc.). */. if
1df43 28 20 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 ( pDflt ){. s
1df44 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1df45 61 6c 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 al;. if( sqli
1df46 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 te3ValueFromExpr
1df47 28 64 62 2c 20 70 44 66 6c 74 2c 20 53 51 4c 49 (db, pDflt, SQLI
1df48 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
1df49 41 46 46 5f 4e 4f 4e 45 2c 20 26 70 56 61 6c 29 AFF_NONE, &pVal)
1df4a 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 ){. db->ma
1df4b 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
1df4c 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
1df4d 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 56 61 }. if( !pVa
1df4e 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 l ){. sqlit
1df4f 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1df50 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 e, "Cannot add a
1df51 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e column with non
1df52 2d 63 6f 6e 73 74 61 6e 74 20 64 65 66 61 75 6c -constant defaul
1df53 74 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 t");. retur
1df54 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c n;. }. sql
1df55 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 ite3ValueFree(pV
1df56 61 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d al);. }.. /* M
1df57 6f 64 69 66 79 20 74 68 65 20 43 52 45 41 54 45 odify the CREATE
1df58 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
1df59 2e 20 2a 2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 71 . */. zCol = sq
1df5a 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 lite3DbStrNDup(d
1df5b 62 2c 20 28 63 68 61 72 2a 29 70 43 6f 6c 44 65 b, (char*)pColDe
1df5c 66 2d 3e 7a 2c 20 70 43 6f 6c 44 65 66 2d 3e 6e f->z, pColDef->n
1df5d 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6c 20 29 7b );. if( zCol ){
1df5e 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 6e 64 20 . char *zEnd
1df5f 3d 20 26 7a 43 6f 6c 5b 70 43 6f 6c 44 65 66 2d = &zCol[pColDef-
1df60 3e 6e 2d 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 >n-1];. while
1df61 28 20 28 7a 45 6e 64 3e 7a 43 6f 6c 20 26 26 20 ( (zEnd>zCol &&
1df62 2a 7a 45 6e 64 3d 3d 27 3b 27 29 20 7c 7c 20 69 *zEnd==';') || i
1df63 73 73 70 61 63 65 28 2a 28 75 6e 73 69 67 6e 65 sspace(*(unsigne
1df64 64 20 63 68 61 72 20 2a 29 7a 45 6e 64 29 20 29 d char *)zEnd) )
1df65 7b 0a 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d 20 {. *zEnd--
1df66 3d 20 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20 20 = '\0';. }.
1df67 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 sqlite3NestedP
1df68 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 arse(pParse, .
1df69 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 "UPDATE \"
1df6a 25 77 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20 %w\".%s SET ".
1df6b 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 "sql = s
1df6c 75 62 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 20 ubstr(sql,1,%d)
1df6d 7c 7c 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c || ', ' || %Q ||
1df6e 20 73 75 62 73 74 72 28 73 71 6c 2c 25 64 29 20 substr(sql,%d)
1df6f 22 0a 20 20 20 20 20 20 20 20 22 57 48 45 52 45 ". "WHERE
1df70 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 type = 'table'
1df71 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20 AND name = %Q",
1df72 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 . zDb, SCHE
1df73 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 MA_TABLE(iDb), p
1df74 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 New->addColOffse
1df75 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61 t, zCol, pNew->a
1df76 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20 ddColOffset+1,.
1df77 20 20 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b zTab. );
1df78 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1df79 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 ee(db, zCol);.
1df7a 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 }.. /* If the d
1df7b 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 efault value of
1df7c 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 the new column i
1df7d 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 s NULL, then set
1df7e 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 66 the file. ** f
1df7f 6f 72 6d 61 74 20 74 6f 20 32 2e 20 49 66 20 74 ormat to 2. If t
1df80 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 he default value
1df81 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 of the new colu
1df82 6d 6e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 0a mn is not NULL,.
1df83 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 66 6f ** the file fo
1df84 72 6d 61 74 20 62 65 63 6f 6d 65 73 20 33 2e 0a rmat becomes 3..
1df85 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d 69 */. sqlite3Mi
1df86 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 nimumFileFormat(
1df87 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 44 66 pParse, iDb, pDf
1df88 6c 74 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20 20 lt ? 3 : 2);..
1df89 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 73 63 /* Reload the sc
1df8a 68 65 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64 69 hema of the modi
1df8b 66 69 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 fied table. */.
1df8c 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 reloadTableSche
1df8d 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c ma(pParse, pTab,
1df8e 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d pTab->zName);.}
1df8f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
1df90 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
1df91 62 79 20 74 68 65 20 70 61 72 73 65 72 20 61 66 by the parser af
1df92 74 65 72 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 ter the table-na
1df93 6d 65 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54 me in.** an "ALT
1df94 45 52 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 2d ER TABLE <table-
1df95 6e 61 6d 65 3e 20 41 44 44 22 20 73 74 61 74 65 name> ADD" state
1df96 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2e 20 ment is parsed.
1df97 41 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53 72 Argument .** pSr
1df98 63 20 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e 61 c is the full-na
1df99 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1df9a 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a 2a being altered..*
1df9b 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1df9c 65 20 6d 61 6b 65 73 20 61 20 28 70 61 72 74 69 e makes a (parti
1df9d 61 6c 29 20 63 6f 70 79 20 6f 66 20 74 68 65 20 al) copy of the
1df9e 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a Table structure.
1df9f 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 ** for the table
1dfa0 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20 61 being altered a
1dfa1 6e 64 20 73 65 74 73 20 50 61 72 73 65 2e 70 4e nd sets Parse.pN
1dfa2 65 77 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e 74 ewTable to point
1dfa3 0a 2a 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74 69 .** to it. Routi
1dfa4 6e 65 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 nes called by th
1dfa5 65 20 70 61 72 73 65 72 20 61 73 20 74 68 65 20 e parser as the
1dfa6 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f column definitio
1dfa7 6e 0a 2a 2a 20 69 73 20 70 61 72 73 65 64 20 28 n.** is parsed (
1dfa8 69 2e 65 2e 20 73 71 6c 69 74 65 33 41 64 64 43 i.e. sqlite3AddC
1dfa9 6f 6c 75 6d 6e 28 29 29 20 61 64 64 20 74 68 65 olumn()) add the
1dfaa 20 6e 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74 61 new Column data
1dfab 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70 79 to .** the copy
1dfac 2e 20 54 68 65 20 63 6f 70 79 20 6f 66 20 74 68 . The copy of th
1dfad 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 e Table structur
1dfae 65 20 69 73 20 64 65 6c 65 74 65 64 20 62 79 20 e is deleted by
1dfaf 74 6f 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61 tokenize.c .** a
1dfb0 66 74 65 72 20 70 61 72 73 69 6e 67 20 69 73 20 fter parsing is
1dfb1 66 69 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 finished..**.**
1dfb2 52 6f 75 74 69 6e 65 20 73 71 6c 69 74 65 33 41 Routine sqlite3A
1dfb3 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c lterFinishAddCol
1dfb4 75 6d 6e 28 29 20 77 69 6c 6c 20 62 65 20 63 61 umn() will be ca
1dfb5 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 lled to complete
1dfb6 0a 2a 2a 20 63 6f 64 69 6e 67 20 74 68 65 20 22 .** coding the "
1dfb7 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 ALTER TABLE ...
1dfb8 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a ADD" statement..
1dfb9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1dfba 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c E void sqlite3Al
1dfbb 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d terBeginAddColum
1dfbc 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c n(Parse *pParse,
1dfbd 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b SrcList *pSrc){
1dfbe 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0a . Table *pNew;.
1dfbf 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 Table *pTab;.
1dfc0 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 Vdbe *v;. int
1dfc1 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 iDb;. int i;.
1dfc2 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73 71 int nAlloc;. sq
1dfc3 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
1dfc4 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c 6f se->db;.. /* Lo
1dfc5 6f 6b 20 75 70 20 74 68 65 20 74 61 62 6c 65 20 ok up the table
1dfc6 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 2a being altered. *
1dfc7 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 /. assert( pPar
1dfc8 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 se->pNewTable==0
1dfc9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
1dfca 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 lite3BtreeHoldsA
1dfcb 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b llMutexes(db) );
1dfcc 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
1dfcd 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65 cFailed ) goto e
1dfce 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f xit_begin_add_co
1dfcf 6c 75 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20 73 lumn;. pTab = s
1dfd0 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c qlite3LocateTabl
1dfd1 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 e(pParse, 0, pSr
1dfd2 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 c->a[0].zName, p
1dfd3 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 Src->a[0].zDatab
1dfd4 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 ase);. if( !pTa
1dfd5 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 b ) goto exit_be
1dfd6 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a gin_add_column;.
1dfd7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1dfd8 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1dfd9 45 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 E. if( IsVirtua
1dfda 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 l(pTab) ){. s
1dfdb 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1dfdc 50 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 Parse, "virtual
1dfdd 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 tables may not b
1dfde 65 20 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 20 e altered");.
1dfdf 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e goto exit_begin
1dfe0 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d _add_column;. }
1dfe1 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 .#endif.. /* Ma
1dfe2 6b 65 20 73 75 72 65 20 74 68 69 73 20 69 73 20 ke sure this is
1dfe3 6e 6f 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 not an attempt t
1dfe4 6f 20 41 4c 54 45 52 20 61 20 76 69 65 77 2e 20 o ALTER a view.
1dfe5 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 */. if( pTab->p
1dfe6 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 Select ){. sq
1dfe7 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1dfe8 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 arse, "Cannot ad
1dfe9 64 20 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 20 d a column to a
1dfea 76 69 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 6f view");. goto
1dfeb 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f exit_begin_add_
1dfec 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61 column;. }.. a
1dfed 73 73 65 72 74 28 20 70 54 61 62 2d 3e 61 64 64 ssert( pTab->add
1dfee 43 6f 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a 20 ColOffset>0 );.
1dfef 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
1dff0 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 hemaToIndex(db,
1dff1 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
1dff2 0a 20 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 79 . /* Put a copy
1dff3 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 of the Table st
1dff4 72 75 63 74 20 69 6e 20 50 61 72 73 65 2e 70 4e ruct in Parse.pN
1dff5 65 77 54 61 62 6c 65 20 66 6f 72 20 74 68 65 0a ewTable for the.
1dff6 20 20 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 43 ** sqlite3AddC
1dff7 6f 6c 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f 6e olumn() function
1dff8 20 61 6e 64 20 66 72 69 65 6e 64 73 20 74 6f 20 and friends to
1dff9 6d 6f 64 69 66 79 2e 0a 20 20 2a 2f 0a 20 20 70 modify.. */. p
1dffa 4e 65 77 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 New = (Table*)sq
1dffb 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
1dffc 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 o(db, sizeof(Tab
1dffd 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 le));. if( !pNe
1dffe 77 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 w ) goto exit_be
1dfff 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a gin_add_column;.
1e000 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 pParse->pNewTa
1e001 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 70 4e ble = pNew;. pN
1e002 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 ew->nRef = 1;.
1e003 70 4e 65 77 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 pNew->db = db;.
1e004 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54 pNew->nCol = pT
1e005 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73 65 ab->nCol;. asse
1e006 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30 rt( pNew->nCol>0
1e007 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 28 );. nAlloc = (
1e008 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f ((pNew->nCol-1)/
1e009 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65 72 8)*8)+8;. asser
1e00a 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d t( nAlloc>=pNew-
1e00b 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 25 >nCol && nAlloc%
1e00c 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d 70 8==0 && nAlloc-p
1e00d 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a 20 New->nCol<8 );.
1e00e 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28 43 pNew->aCol = (C
1e00f 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44 62 olumn*)sqlite3Db
1e010 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 MallocZero(db, s
1e011 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e 41 izeof(Column)*nA
1e012 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 7a lloc);. pNew->z
1e013 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 Name = sqlite3Db
1e014 53 74 72 44 75 70 28 64 62 2c 20 70 54 61 62 2d StrDup(db, pTab-
1e015 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 >zName);. if( !
1e016 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c 7c 20 21 70 pNew->aCol || !p
1e017 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 New->zName ){.
1e018 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
1e019 65 64 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f ed = 1;. goto
1e01a 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f exit_begin_add_
1e01b 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20 6d 65 column;. }. me
1e01c 6d 63 70 79 28 70 4e 65 77 2d 3e 61 43 6f 6c 2c mcpy(pNew->aCol,
1e01d 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 73 69 7a pTab->aCol, siz
1e01e 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 70 4e 65 77 eof(Column)*pNew
1e01f 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28 69 ->nCol);. for(i
1e020 3d 30 3b 20 69 3c 70 4e 65 77 2d 3e 6e 43 6f 6c =0; i<pNew->nCol
1e021 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 43 6f 6c 75 ; i++){. Colu
1e022 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 4e 65 77 mn *pCol = &pNew
1e023 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 70 ->aCol[i];. p
1e024 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c Col->zName = sql
1e025 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
1e026 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pCol->zName);.
1e027 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d pCol->zColl =
1e028 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 0;. pCol->zT
1e029 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f ype = 0;. pCo
1e02a 6c 2d 3e 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 l->pDflt = 0;.
1e02b 7d 0a 20 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d }. pNew->pSchem
1e02c 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d a = db->aDb[iDb]
1e02d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 4e 65 77 .pSchema;. pNew
1e02e 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d ->addColOffset =
1e02f 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 pTab->addColOff
1e030 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 set;. pNew->nRe
1e031 66 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 42 65 67 f = 1;.. /* Beg
1e032 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e in a transaction
1e033 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 74 and increment t
1e034 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 he schema cookie
1e035 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 . */. sqlite3B
1e036 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 eginWriteOperati
1e037 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 on(pParse, 0, iD
1e038 62 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 b);. v = sqlite
1e039 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
1e03a 3b 0a 20 20 69 66 28 20 21 76 20 29 20 67 6f 74 ;. if( !v ) got
1e03b 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 o exit_begin_add
1e03c 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 73 71 6c 69 74 _column;. sqlit
1e03d 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 e3ChangeCookie(p
1e03e 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 65 78 Parse, iDb);..ex
1e03f 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c it_begin_add_col
1e040 75 6d 6e 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 umn:. sqlite3Sr
1e041 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 cListDelete(db,
1e042 70 53 72 63 29 3b 0a 20 20 72 65 74 75 72 6e 3b pSrc);. return;
1e043 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 .}.#endif /* SQ
1e044 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 LITE_ALTER_TABLE
1e045 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
1e046 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6c 74 65 **** End of alte
1e047 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a r.c ************
1e048 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e049 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e04a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1e04b 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1e04c 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a analyze.c ******
1e04d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e04e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e04f 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 ***/./*.** 2005
1e050 4a 75 6c 79 20 38 0a 2a 2a 0a 2a 2a 20 54 68 65 July 8.**.** The
1e051 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
1e052 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
1e053 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
1e054 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
1e055 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
1e056 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1e057 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1e058 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1e059 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1e05a 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1e05b 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1e05c 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1e05d 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1e05e 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1e05f 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
1e060 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
1e061 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
1e062 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e063 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e064 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e065 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e066 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
1e067 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
1e068 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1e069 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d the ANALYZE com
1e06a 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 mand..**.** @(#)
1e06b 20 24 49 64 3a 20 61 6e 61 6c 79 7a 65 2e 63 2c $Id: analyze.c,
1e06c 76 20 31 2e 34 36 20 32 30 30 38 2f 31 31 2f 31 v 1.46 2008/11/1
1e06d 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 9 16:52:44 danie
1e06e 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
1e06f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1e070 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a 0a MIT_ANALYZE../*.
1e071 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1e072 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 generates code t
1e073 68 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73 71 hat opens the sq
1e074 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 lite_stat1 table
1e075 20 6f 6e 20 63 75 72 73 6f 72 0a 2a 2a 20 69 53 on cursor.** iS
1e076 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 tatCur..**.** If
1e077 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
1e078 31 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e 6f 1 tables does no
1e079 74 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 t previously exi
1e07a 73 74 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 st, it is create
1e07b 64 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 d..** If it does
1e07c 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 previously exis
1e07d 74 2c 20 61 6c 6c 20 65 6e 74 69 72 65 73 20 61 t, all entires a
1e07e 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
1e07f 61 62 6c 65 20 7a 57 68 65 72 65 0a 2a 2a 20 61 able zWhere.** a
1e080 72 65 20 72 65 6d 6f 76 65 64 2e 20 20 49 66 20 re removed. If
1e081 7a 57 68 65 72 65 3d 3d 30 20 74 68 65 6e 20 61 zWhere==0 then a
1e082 6c 6c 20 65 6e 74 72 69 65 73 20 61 72 65 20 72 ll entries are r
1e083 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 emoved..*/.stati
1e084 63 20 76 6f 69 64 20 6f 70 65 6e 53 74 61 74 54 c void openStatT
1e085 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 able(. Parse *p
1e086 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 Parse,
1e087 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
1e088 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c xt */. int iDb,
1e089 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e08a 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
1e08b 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 69 we are looking i
1e08c 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 n */. int iStat
1e08d 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f Cur, /
1e08e 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 * Open the sqlit
1e08f 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 6f 6e e_stat1 table on
1e090 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a this cursor */.
1e091 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57 const char *zW
1e092 68 65 72 65 20 20 20 20 20 20 2f 2a 20 44 65 6c here /* Del
1e093 65 74 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f ete entries asso
1e094 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ciated with this
1e095 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 table */.){. s
1e096 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1e097 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 rse->db;. Db *p
1e098 44 62 3b 0a 20 20 69 6e 74 20 69 52 6f 6f 74 50 Db;. int iRootP
1e099 61 67 65 3b 0a 20 20 69 6e 74 20 63 72 65 61 74 age;. int creat
1e09a 65 53 74 61 74 31 20 3d 20 30 3b 0a 20 20 54 61 eStat1 = 0;. Ta
1e09b 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20 20 56 64 ble *pStat;. Vd
1e09c 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 be *v = sqlite3G
1e09d 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
1e09e 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 . if( v==0 ) re
1e09f 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 turn;. assert(
1e0a0 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
1e0a1 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 sAllMutexes(db)
1e0a2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
1e0a3 69 74 65 33 56 64 62 65 44 62 28 76 29 3d 3d 64 ite3VdbeDb(v)==d
1e0a4 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 b );. pDb = &db
1e0a5 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66 ->aDb[iDb];. if
1e0a6 28 20 28 70 53 74 61 74 20 3d 20 73 71 6c 69 74 ( (pStat = sqlit
1e0a7 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 e3FindTable(db,
1e0a8 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 "sqlite_stat1",
1e0a9 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d 3d 30 20 pDb->zName))==0
1e0aa 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 71 ){. /* The sq
1e0ab 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 lite_stat1 table
1e0ac 73 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 s does not exist
1e0ad 2e 20 20 43 72 65 61 74 65 20 69 74 2e 20 20 0a . Create it. .
1e0ae 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 ** Note that
1e0af 20 61 20 73 69 64 65 2d 65 66 66 65 63 74 20 6f a side-effect o
1e0b0 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 f the CREATE TAB
1e0b1 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 LE statement is
1e0b2 74 6f 20 6c 65 61 76 65 0a 20 20 20 20 2a 2a 20 to leave. **
1e0b3 74 68 65 20 72 6f 6f 74 70 61 67 65 20 6f 66 20 the rootpage of
1e0b4 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e the new table in
1e0b5 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65 register pParse
1e0b6 2d 3e 72 65 67 52 6f 6f 74 2e 20 20 54 68 69 73 ->regRoot. This
1e0b7 20 69 73 0a 20 20 20 20 2a 2a 20 69 6d 70 6f 72 is. ** impor
1e0b8 74 61 6e 74 20 62 65 63 61 75 73 65 20 74 68 65 tant because the
1e0b9 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64 OpenWrite opcod
1e0ba 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20 e below will be
1e0bb 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 needing it. */.
1e0bc 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 sqlite3Nested
1e0bd 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 Parse(pParse,.
1e0be 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c "CREATE TABL
1e0bf 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 E %Q.sqlite_stat
1e0c0 31 28 74 62 6c 2c 69 64 78 2c 73 74 61 74 29 22 1(tbl,idx,stat)"
1e0c1 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 ,. pDb->zNa
1e0c2 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 52 me. );. iR
1e0c3 6f 6f 74 50 61 67 65 20 3d 20 70 50 61 72 73 65 ootPage = pParse
1e0c4 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20 20 20 63 ->regRoot;. c
1e0c5 72 65 61 74 65 53 74 61 74 31 20 3d 20 31 3b 20 reateStat1 = 1;
1e0c6 20 2f 2a 20 43 61 75 73 65 20 72 6f 6f 74 70 61 /* Cause rootpa
1e0c7 67 65 20 74 6f 20 62 65 20 74 61 6b 65 6e 20 66 ge to be taken f
1e0c8 72 6f 6d 20 74 6f 70 20 6f 66 20 73 74 61 63 6b rom top of stack
1e0c9 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 */. }else if(
1e0ca 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 20 2f 2a zWhere ){. /*
1e0cb 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 The sqlite_stat
1e0cc 31 20 74 61 62 6c 65 20 65 78 69 73 74 73 2e 20 1 table exists.
1e0cd 20 44 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72 Delete all entr
1e0ce 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ies associated w
1e0cf 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 ith. ** the t
1e0d0 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 2a 2f 0a able zWhere. */.
1e0d1 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 sqlite3Neste
1e0d2 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 dParse(pParse,.
1e0d3 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 "DELETE FR
1e0d4 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 OM %Q.sqlite_sta
1e0d5 74 31 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22 t1 WHERE tbl=%Q"
1e0d6 2c 0a 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e ,. pDb->zN
1e0d7 61 6d 65 2c 20 7a 57 68 65 72 65 0a 20 20 20 20 ame, zWhere.
1e0d8 29 3b 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 65 );. iRootPage
1e0d9 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a = pStat->tnum;.
1e0da 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1e0db 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 The sqlite_stat1
1e0dc 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 table already e
1e0dd 78 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20 61 xists. Delete a
1e0de 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20 20 ll rows. */.
1e0df 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 53 74 61 iRootPage = pSta
1e0e0 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c t->tnum;. sql
1e0e1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1e0e2 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 53 74 61 , OP_Clear, pSta
1e0e3 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 t->tnum, iDb);.
1e0e4 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 }.. /* Open th
1e0e5 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 e sqlite_stat1 t
1e0e6 61 62 6c 65 20 66 6f 72 20 77 72 69 74 69 6e 67 able for writing
1e0e7 2e 20 55 6e 6c 65 73 73 20 69 74 20 77 61 73 20 . Unless it was
1e0e8 63 72 65 61 74 65 64 0a 20 20 2a 2a 20 62 79 20 created. ** by
1e0e9 74 68 69 73 20 76 64 62 65 20 70 72 6f 67 72 61 this vdbe progra
1e0ea 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f 72 20 77 m, lock it for w
1e0eb 72 69 74 69 6e 67 20 61 74 20 74 68 65 20 73 68 riting at the sh
1e0ec 61 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65 6c ared-cache level
1e0ed 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 . . ** If this
1e0ee 76 64 62 65 20 64 69 64 20 63 72 65 61 74 65 20 vdbe did create
1e0ef 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 the sqlite_stat1
1e0f0 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 69 74 20 table, then it
1e0f1 6d 75 73 74 20 68 61 76 65 20 0a 20 20 2a 2a 20 must have . **
1e0f2 61 6c 72 65 61 64 79 20 6f 62 74 61 69 6e 65 64 already obtained
1e0f3 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 2c 20 a schema-lock,
1e0f4 6d 61 6b 69 6e 67 20 74 68 65 20 77 72 69 74 65 making the write
1e0f5 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61 6e 74 2e -lock redundant.
1e0f6 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63 72 65 . */. if( !cre
1e0f7 61 74 65 53 74 61 74 31 20 29 7b 0a 20 20 20 20 ateStat1 ){.
1e0f8 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b sqlite3TableLock
1e0f9 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 69 52 (pParse, iDb, iR
1e0fa 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22 73 71 6c ootPage, 1, "sql
1e0fb 69 74 65 5f 73 74 61 74 31 22 29 3b 0a 20 20 7d ite_stat1");. }
1e0fc 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
1e0fd 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e 75 dOp2(v, OP_SetNu
1e0fe 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 33 29 3b mColumns, 0, 3);
1e0ff 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
1e100 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 dOp3(v, OP_OpenW
1e101 72 69 74 65 2c 20 69 53 74 61 74 43 75 72 2c 20 rite, iStatCur,
1e102 69 52 6f 6f 74 50 61 67 65 2c 20 69 44 62 29 3b iRootPage, iDb);
1e103 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
1e104 61 6e 67 65 50 35 28 76 2c 20 63 72 65 61 74 65 angeP5(v, create
1e105 53 74 61 74 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Stat1);.}../*.**
1e106 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
1e107 6f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 o do an analysis
1e108 20 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 of all indices
1e109 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a associated with.
1e10a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c ** a single tabl
1e10b 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
1e10c 64 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c d analyzeOneTabl
1e10d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
1e10e 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 se, /* Parser
1e10f 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 context */. Tab
1e110 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a le *pTab, /*
1e111 20 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e 64 Table whose ind
1e112 69 63 65 73 20 61 72 65 20 74 6f 20 62 65 20 61 ices are to be a
1e113 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 nalyzed */. int
1e114 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f 2a iStatCur, /*
1e115 20 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43 75 Index of VdbeCu
1e116 72 73 6f 72 20 74 68 61 74 20 77 72 69 74 65 73 rsor that writes
1e117 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 the sqlite_stat
1e118 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 1 table */. int
1e119 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f 2a iMem /*
1e11a 20 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 Available memor
1e11b 79 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67 69 y locations begi
1e11c 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 49 n here */.){. I
1e11d 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 ndex *pIdx;
1e11e 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20 62 /* An index to b
1e11f 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f eing analyzed */
1e120 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 . int iIdxCur;
1e121 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
1e122 56 64 62 65 43 75 72 73 6f 72 20 66 6f 72 20 69 VdbeCursor for i
1e123 6e 64 65 78 20 62 65 69 6e 67 20 61 6e 61 6c 79 ndex being analy
1e124 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f zed */. int nCo
1e125 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d l; /* Num
1e126 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
1e127 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 n the index */.
1e128 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 Vdbe *v;
1e129 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c /* The virtual
1e12a 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20 62 machine being b
1e12b 75 69 6c 74 20 75 70 20 2a 2f 0a 20 20 69 6e 74 uilt up */. int
1e12c 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a i; /*
1e12d 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
1e12e 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f 70 . int topOfLoop
1e12f 3b 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f ; /* The top o
1e130 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 f the loop */.
1e131 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 int endOfLoop;
1e132 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 74 /* The end of t
1e133 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 he loop */. int
1e134 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a addr; /*
1e135 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 The address of
1e136 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a an instruction *
1e137 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 /. int iDb;
1e138 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1e139 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
1e13a 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 0a 20 20 ning pTab */..
1e13b 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 v = sqlite3GetVd
1e13c 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 be(pParse);. if
1e13d 28 20 76 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d ( v==0 || pTab==
1e13e 30 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 0 || pTab->pInde
1e13f 78 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 44 x==0 ){. /* D
1e140 6f 20 6e 6f 20 61 6e 61 6c 79 73 69 73 20 66 6f o no analysis fo
1e141 72 20 74 61 62 6c 65 73 20 74 68 61 74 20 68 61 r tables that ha
1e142 76 65 20 6e 6f 20 69 6e 64 69 63 65 73 20 2a 2f ve no indices */
1e143 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
1e144 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1e145 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d e3BtreeHoldsAllM
1e146 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 utexes(pParse->d
1e147 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 b) );. iDb = sq
1e148 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 lite3SchemaToInd
1e149 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 ex(pParse->db, p
1e14a 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 Tab->pSchema);.
1e14b 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 assert( iDb>=0
1e14c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
1e14d 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 E_OMIT_AUTHORIZA
1e14e 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69 74 TION. if( sqlit
1e14f 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 e3AuthCheck(pPar
1e150 73 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 se, SQLITE_ANALY
1e151 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c ZE, pTab->zName,
1e152 20 30 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 0,. pParse
1e153 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a ->db->aDb[iDb].z
1e154 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 Name ) ){. re
1e155 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 turn;. }.#endif
1e156 0a 0a 20 20 2f 2a 20 45 73 74 61 62 6c 69 73 68 .. /* Establish
1e157 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 a read-lock on
1e158 74 68 65 20 74 61 62 6c 65 20 61 74 20 74 68 65 the table at the
1e159 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 shared-cache le
1e15a 76 65 6c 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 vel. */. sqlite
1e15b 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 3TableLock(pPars
1e15c 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e e, iDb, pTab->tn
1e15d 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 um, 0, pTab->zNa
1e15e 6d 65 29 3b 0a 0a 20 20 69 49 64 78 43 75 72 20 me);.. iIdxCur
1e15f 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a = pParse->nTab;.
1e160 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d for(pIdx=pTab-
1e161 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 >pIndex; pIdx; p
1e162 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 Idx=pIdx->pNext)
1e163 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 {. KeyInfo *p
1e164 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 Key = sqlite3Ind
1e165 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 exKeyinfo(pParse
1e166 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 69 6e 74 , pIdx);. int
1e167 20 72 65 67 46 69 65 6c 64 73 3b 20 20 20 20 2f regFields; /
1e168 2a 20 52 65 67 69 73 74 65 72 20 62 6c 6f 63 6b * Register block
1e169 20 66 6f 72 20 62 75 69 6c 64 69 6e 67 20 72 65 for building re
1e16a 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 69 6e 74 cords */. int
1e16b 20 72 65 67 52 65 63 3b 20 20 20 20 20 20 20 2f regRec; /
1e16c 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 * Register holdi
1e16d 6e 67 20 63 6f 6d 70 6c 65 74 65 64 20 72 65 63 ng completed rec
1e16e 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 ord */. int r
1e16f 65 67 54 65 6d 70 3b 20 20 20 20 20 20 2f 2a 20 egTemp; /*
1e170 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65 Temporary use re
1e171 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e gister */. in
1e172 74 20 72 65 67 43 6f 6c 3b 20 20 20 20 20 20 20 t regCol;
1e173 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 61 20 /* Content of a
1e174 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20 column from the
1e175 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6e 61 6c table being anal
1e176 79 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 yzed */. int
1e177 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 2f 2a regRowid; /*
1e178 20 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20 69 Rowid for the i
1e179 6e 73 65 72 74 65 64 20 72 65 63 6f 72 64 20 2a nserted record *
1e17a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 46 32 3b /. int regF2;
1e17b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20 .. /* Open a
1e17c 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 6e cursor to the in
1e17d 64 65 78 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a dex to be analyz
1e17e 65 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 ed. */. as
1e17f 73 65 72 74 28 20 69 44 62 3d 3d 73 71 6c 69 74 sert( iDb==sqlit
1e180 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
1e181 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 64 78 pParse->db, pIdx
1e182 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 20 ->pSchema) );.
1e183 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e nCol = pIdx->n
1e184 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 Column;. sqli
1e185 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1e186 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e OP_SetNumColumn
1e187 73 2c 20 30 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 s, 0, nCol+1);.
1e188 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1e189 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 dOp4(v, OP_OpenR
1e18a 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70 49 ead, iIdxCur, pI
1e18b 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 dx->tnum, iDb,.
1e18c 20 20 20 20 20 20 20 28 63 68 61 72 20 2a 29 70 (char *)p
1e18d 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f Key, P4_KEYINFO_
1e18e 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 HANDOFF);. Vd
1e18f 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 beComment((v, "%
1e190 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 s", pIdx->zName)
1e191 29 3b 0a 20 20 20 20 72 65 67 46 69 65 6c 64 73 );. regFields
1e192 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32 3b 0a = iMem+nCol*2;.
1e193 20 20 20 20 72 65 67 54 65 6d 70 20 3d 20 72 65 regTemp = re
1e194 67 52 6f 77 69 64 20 3d 20 72 65 67 43 6f 6c 20 gRowid = regCol
1e195 3d 20 72 65 67 46 69 65 6c 64 73 2b 33 3b 0a 20 = regFields+3;.
1e196 20 20 20 72 65 67 52 65 63 20 3d 20 72 65 67 43 regRec = regC
1e197 6f 6c 2b 31 3b 0a 20 20 20 20 69 66 28 20 72 65 ol+1;. if( re
1e198 67 52 65 63 3e 70 50 61 72 73 65 2d 3e 6e 4d 65 gRec>pParse->nMe
1e199 6d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 m ){. pPars
1e19a 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67 52 65 63 e->nMem = regRec
1e19b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1e19c 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 61 72 65 Memory cells are
1e19d 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 used as follows
1e19e 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 :. **. **
1e19f 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a 20 20 20 mem[iMem]:
1e1a0 20 20 20 20 20 20 20 20 20 20 54 68 65 20 74 6f The to
1e1a1 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f tal number of ro
1e1a2 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ws in the table.
1e1a3 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 . ** mem[i
1e1a4 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20 20 20 20 Mem+1]:
1e1a5 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73 74 Number of dist
1e1a6 69 6e 63 74 20 76 61 6c 75 65 73 20 69 6e 20 63 inct values in c
1e1a7 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 20 olumn 1. **
1e1a8 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20 .... **
1e1a9 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 5d 3a 20 mem[iMem+nCol]:
1e1aa 20 20 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 Number of
1e1ab 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 distinct values
1e1ac 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20 in column N.
1e1ad 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b ** mem[iMem+
1e1ae 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20 20 4c 61 nCol+1] La
1e1af 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75 st observed valu
1e1b0 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31 0a 20 20 e of column 1.
1e1b1 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 20 20 ** ....
1e1b2 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 6e ** mem[iMem+n
1e1b3 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20 4c 61 73 Col+nCol]: Las
1e1b4 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75 65 t observed value
1e1b5 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20 of column N.
1e1b6 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c 73 **. ** Cells
1e1b7 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69 4d iMem through iM
1e1b8 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69 74 em+nCol are init
1e1b9 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 20 54 ialized to 0. T
1e1ba 68 65 20 6f 74 68 65 72 73 0a 20 20 20 20 2a 2a he others. **
1e1bb 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 are initialized
1e1bc 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f to NULL.. */
1e1bd 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1e1be 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 =nCol; i++){.
1e1bf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1e1c0 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
1e1c1 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b 0a er, 0, iMem+i);.
1e1c2 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d }. for(i=
1e1c3 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0; i<nCol; i++){
1e1c4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1e1c5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
1e1c6 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f ull, 0, iMem+nCo
1e1c7 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 l+i+1);. }..
1e1c8 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 61 6e 61 /* Do the ana
1e1c9 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 lysis.. */.
1e1ca 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 endOfLoop = sq
1e1cb 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
1e1cc 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 el(v);. sqlit
1e1cd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1e1ce 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78 43 OP_Rewind, iIdxC
1e1cf 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a ur, endOfLoop);.
1e1d0 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d 20 topOfLoop =
1e1d1 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
1e1d2 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 ntAddr(v);. s
1e1d3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1e1d4 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 (v, OP_AddImm, i
1e1d5 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 66 6f 72 Mem, 1);. for
1e1d6 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b (i=0; i<nCol; i+
1e1d7 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 +){. sqlite
1e1d8 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
1e1d9 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 P_Column, iIdxCu
1e1da 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 3b 0a 20 r, i, regCol);.
1e1db 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1e1dc 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c AddOp3(v, OP_Ne,
1e1dd 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65 6d regCol, 0, iMem
1e1de 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 +nCol+i+1);.
1e1df 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20 61 /**** TODO: a
1e1e0 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 dd collating seq
1e1e1 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20 20 uence *****/.
1e1e2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1e1e3 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 angeP5(v, SQLITE
1e1e4 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 _JUMPIFNULL);.
1e1e5 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
1e1e6 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1e1e7 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c 6f Goto, 0, endOfLo
1e1e8 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 op);. for(i=0
1e1e9 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a ; i<nCol; i++){.
1e1ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1e1eb 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 6f 70 eJumpHere(v, top
1e1ec 4f 66 4c 6f 6f 70 20 2b 20 32 2a 28 69 20 2b 20 OfLoop + 2*(i +
1e1ed 31 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 1));. sqlit
1e1ee 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1e1ef 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d 2b OP_AddImm, iMem+
1e1f0 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 i+1, 1);. s
1e1f1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1e1f2 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 (v, OP_Column, i
1e1f3 49 64 78 43 75 72 2c 20 69 2c 20 69 4d 65 6d 2b IdxCur, i, iMem+
1e1f4 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d nCol+i+1);. }
1e1f5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1e1f6 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 ResolveLabel(v,
1e1f7 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 endOfLoop);.
1e1f8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1e1f9 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 49 2(v, OP_Next, iI
1e1fa 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f 70 dxCur, topOfLoop
1e1fb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1e1fc 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 beAddOp1(v, OP_C
1e1fd 6c 6f 73 65 2c 20 69 49 64 78 43 75 72 29 3b 0a lose, iIdxCur);.
1e1fe 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 . /* Store th
1e1ff 65 20 72 65 73 75 6c 74 73 2e 20 20 0a 20 20 20 e results. .
1e200 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 **. ** The r
1e201 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67 6c esult is a singl
1e202 65 20 72 6f 77 20 6f 66 20 74 68 65 20 73 71 6c e row of the sql
1e203 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e ite_stat1 table.
1e204 20 20 54 68 65 20 66 69 72 73 74 0a 20 20 20 20 The first.
1e205 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20 61 ** two columns a
1e206 72 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 re the names of
1e207 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e the table and in
1e208 64 65 78 2e 20 20 54 68 65 20 74 68 69 72 64 20 dex. The third
1e209 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69 73 column. ** is
1e20a 20 61 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f 73 a string compos
1e20b 65 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 ed of a list of
1e20c 69 6e 74 65 67 65 72 20 73 74 61 74 69 73 74 69 integer statisti
1e20d 63 73 20 61 62 6f 75 74 20 74 68 65 0a 20 20 20 cs about the.
1e20e 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65 20 ** index. The
1e20f 66 69 72 73 74 20 69 6e 74 65 67 65 72 20 69 6e first integer in
1e210 20 74 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 the list is the
1e211 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
1e212 20 65 6e 74 69 72 65 73 0a 20 20 20 20 2a 2a 20 entires. **
1e213 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20 54 in the index. T
1e214 68 65 72 65 20 69 73 20 6f 6e 65 20 61 64 64 69 here is one addi
1e215 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20 69 tional integer i
1e216 6e 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 65 n the list for e
1e217 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d ach. ** colum
1e218 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 n of the table.
1e219 20 54 68 69 73 20 61 64 64 69 74 69 6f 6e 61 6c This additional
1e21a 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 67 75 integer is a gu
1e21b 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79 0a ess of how many.
1e21c 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20 74 ** rows of t
1e21d 68 65 20 74 61 62 6c 65 20 74 68 65 20 69 6e 64 he table the ind
1e21e 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e 20 ex will select.
1e21f 20 49 66 20 44 20 69 73 20 74 68 65 20 63 6f 75 If D is the cou
1e220 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 74 0a 20 nt of distinct.
1e221 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e 64 ** values and
1e222 20 4b 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 K is the total
1e223 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c 20 number of rows,
1e224 74 68 65 6e 20 74 68 65 20 69 6e 74 65 67 65 72 then the integer
1e225 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 20 20 20 is computed.
1e226 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a 20 ** as:. **.
1e227 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20 3d ** I =
1e228 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20 2a (K+D-1)/D. *
1e229 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d 30 *. ** If K==0
1e22a 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20 69 then no entry i
1e22b 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68 65 20 s made into the
1e22c 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 sqlite_stat1 tab
1e22d 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66 20 le. . ** If
1e22e 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20 61 K>0 then it is a
1e22f 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 20 74 lways the case t
1e230 68 65 20 44 3e 30 20 73 6f 20 64 69 76 69 73 69 he D>0 so divisi
1e231 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 20 20 2a on by zero. *
1e232 2a 20 69 73 20 6e 65 76 65 72 20 70 6f 73 73 69 * is never possi
1e233 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ble.. */.
1e234 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 addr = sqlite3Vd
1e235 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 beAddOp1(v, OP_I
1e236 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 fNot, iMem);.
1e237 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1e238 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 p4(v, OP_String8
1e239 2c 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2c 20 , 0, regFields,
1e23a 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 0, pTab->zName,
1e23b 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 0);. sqlite3V
1e23c 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
1e23d 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 46 String8, 0, regF
1e23e 69 65 6c 64 73 2b 31 2c 20 30 2c 20 70 49 64 78 ields+1, 0, pIdx
1e23f 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 ->zName, 0);.
1e240 20 72 65 67 46 32 20 3d 20 72 65 67 46 69 65 6c regF2 = regFiel
1e241 64 73 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 ds+2;. sqlite
1e242 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
1e243 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d 2c 20 72 P_SCopy, iMem, r
1e244 65 67 46 32 29 3b 0a 20 20 20 20 66 6f 72 28 69 egF2);. for(i
1e245 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 =0; i<nCol; i++)
1e246 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1e247 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
1e248 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 54 String8, 0, regT
1e249 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 30 29 3b emp, 0, " ", 0);
1e24a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1e24b 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 beAddOp3(v, OP_C
1e24c 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c 20 oncat, regTemp,
1e24d 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a 20 regF2, regF2);.
1e24e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1e24f 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64 AddOp3(v, OP_Add
1e250 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b 31 , iMem, iMem+i+1
1e251 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 , regTemp);.
1e252 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1e253 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d Op2(v, OP_AddImm
1e254 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b 0a , regTemp, -1);.
1e255 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1e256 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 69 eAddOp3(v, OP_Di
1e257 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 vide, iMem+i+1,
1e258 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d 70 regTemp, regTemp
1e259 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1e25a 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
1e25b 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70 29 _ToInt, regTemp)
1e25c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1e25d 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
1e25e 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c Concat, regTemp,
1e25f 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a regF2, regF2);.
1e260 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1e261 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
1e262 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 P_MakeRecord, re
1e263 67 46 69 65 6c 64 73 2c 20 33 2c 20 72 65 67 52 gFields, 3, regR
1e264 65 63 2c 20 22 61 61 61 22 2c 20 30 29 3b 0a 20 ec, "aaa", 0);.
1e265 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1e266 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f dOp2(v, OP_NewRo
1e267 77 69 64 2c 20 69 53 74 61 74 43 75 72 2c 20 72 wid, iStatCur, r
1e268 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 egRowid);. sq
1e269 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
1e26a 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 53 v, OP_Insert, iS
1e26b 74 61 74 43 75 72 2c 20 72 65 67 52 65 63 2c 20 tatCur, regRec,
1e26c 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 regRowid);. s
1e26d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1e26e 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 P5(v, OPFLAG_APP
1e26f 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 END);. sqlite
1e270 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
1e271 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f addr);. }.}../
1e272 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
1e273 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 61 75 de that will cau
1e274 73 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 se the most rece
1e275 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c 79 73 69 nt index analysi
1e276 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64 65 s to.** be laode
1e277 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 d into internal
1e278 68 61 73 68 20 74 61 62 6c 65 73 20 77 68 65 72 hash tables wher
1e279 65 20 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 e is can be used
1e27a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1e27b 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 50 61 loadAnalysis(Pa
1e27c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 rse *pParse, int
1e27d 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 iDb){. Vdbe *v
1e27e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
1e27f 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 e(pParse);. if(
1e280 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 v ){. sqlite
1e281 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
1e282 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 2c 20 P_LoadAnalysis,
1e283 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a iDb);. }.}../*.
1e284 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 ** Generate code
1e285 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e that will do an
1e286 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e 20 analysis of an
1e287 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 0a entire database.
1e288 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
1e289 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 50 nalyzeDatabase(P
1e28a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
1e28b 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 t iDb){. sqlite
1e28c 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
1e28d 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 db;. Schema *pS
1e28e 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b chema = db->aDb[
1e28f 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20 20 iDb].pSchema;
1e290 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64 61 /* Schema of da
1e291 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20 tabase iDb */.
1e292 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69 HashElem *k;. i
1e293 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 20 20 69 nt iStatCur;. i
1e294 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69 nt iMem;.. sqli
1e295 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 te3BeginWriteOpe
1e296 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 ration(pParse, 0
1e297 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43 , iDb);. iStatC
1e298 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 ur = pParse->nTa
1e299 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54 b++;. openStatT
1e29a 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 able(pParse, iDb
1e29b 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b 0a , iStatCur, 0);.
1e29c 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d iMem = pParse-
1e29d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28 6b >nMem+1;. for(k
1e29e 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 =sqliteHashFirst
1e29f 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 (&pSchema->tblHa
1e2a0 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 sh); k; k=sqlite
1e2a1 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20 HashNext(k)){.
1e2a2 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 Table *pTab =
1e2a3 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 (Table*)sqliteHa
1e2a4 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 61 shData(k);. a
1e2a5 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 nalyzeOneTable(p
1e2a6 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 Parse, pTab, iSt
1e2a7 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20 20 atCur, iMem);.
1e2a8 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 }. loadAnalysis
1e2a9 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 7d (pParse, iDb);.}
1e2aa 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1e2ab 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 code that will
1e2ac 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f do an analysis o
1e2ad 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 f a single table
1e2ae 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 in.** a databas
1e2af 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
1e2b0 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 50 d analyzeTable(P
1e2b1 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 arse *pParse, Ta
1e2b2 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e ble *pTab){. in
1e2b3 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 74 t iDb;. int iSt
1e2b4 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 74 atCur;.. assert
1e2b5 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 61 ( pTab!=0 );. a
1e2b6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1e2b7 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 reeHoldsAllMutex
1e2b8 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 es(pParse->db) )
1e2b9 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 ;. iDb = sqlite
1e2ba 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 3SchemaToIndex(p
1e2bb 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d Parse->db, pTab-
1e2bc 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c >pSchema);. sql
1e2bd 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 ite3BeginWriteOp
1e2be 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 eration(pParse,
1e2bf 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 0, iDb);. iStat
1e2c0 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 Cur = pParse->nT
1e2c1 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74 ab++;. openStat
1e2c2 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 Table(pParse, iD
1e2c3 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54 61 b, iStatCur, pTa
1e2c4 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e 61 b->zName);. ana
1e2c5 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50 61 lyzeOneTable(pPa
1e2c6 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61 74 rse, pTab, iStat
1e2c7 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 Cur, pParse->nMe
1e2c8 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e 61 6c m+1);. loadAnal
1e2c9 79 73 69 73 28 70 50 61 72 73 65 2c 20 69 44 62 ysis(pParse, iDb
1e2ca 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 );.}../*.** Gene
1e2cb 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 rate code for th
1e2cc 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e e ANALYZE comman
1e2cd 64 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63 d. The parser c
1e2ce 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e alls this routin
1e2cf 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72 65 63 e.** when it rec
1e2d0 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41 4c 59 ognizes an ANALY
1e2d1 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a ZE command..**.*
1e2d2 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a 45 * ANALYZE
1e2d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e2d4 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 31 -- 1
1e2d5 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 .** ANALY
1e2d6 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e 20 20 ZE <database>
1e2d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d --
1e2d8 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 2.** ANA
1e2d9 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61 73 65 LYZE ?<database
1e2da 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20 >.?<tablename>
1e2db 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 -- 3.**.** Form
1e2dc 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 1 causes all ind
1e2dd 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 ices in all atta
1e2de 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 74 ched databases t
1e2df 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a 2a o be analyzed..*
1e2e0 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79 7a 65 * Form 2 analyze
1e2e1 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 74 68 s all indices th
1e2e2 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 e single databas
1e2e3 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72 6d e named..** Form
1e2e4 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 3 analyzes all
1e2e5 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 indices associat
1e2e6 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 ed with the name
1e2e7 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 d table..*/.SQLI
1e2e8 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1e2e9 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50 sqlite3Analyze(P
1e2ea 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f arse *pParse, To
1e2eb 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b ken *pName1, Tok
1e2ec 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 73 en *pName2){. s
1e2ed 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1e2ee 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 rse->db;. int i
1e2ef 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 Db;. int i;. c
1e2f0 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20 20 har *z, *zDb;.
1e2f1 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 54 Table *pTab;. T
1e2f2 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 oken *pTableName
1e2f3 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 ;.. /* Read the
1e2f4 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
1e2f5 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
1e2f6 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 curs, leave an e
1e2f7 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a rror message. *
1e2f8 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 * and code in pP
1e2f9 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 arse and return
1e2fa 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 NULL. */. asser
1e2fb 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
1e2fc 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 oldsAllMutexes(p
1e2fd 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 Parse->db) );.
1e2fe 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
1e2ff 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 qlite3ReadSchema
1e300 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 (pParse) ){.
1e301 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 return;. }.. i
1e302 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b 0a f( pName1==0 ){.
1e303 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a 20 20 /* Form 1:
1e304 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74 68 69 Analyze everythi
1e305 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d ng */. for(i=
1e306 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
1e307 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 3d +){. if( i=
1e308 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 =1 ) continue;
1e309 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61 6c 79 7a /* Do not analyz
1e30a 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 e the TEMP datab
1e30b 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 61 6e 61 ase */. ana
1e30c 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61 lyzeDatabase(pPa
1e30d 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 rse, i);. }.
1e30e 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d 65 }else if( pName
1e30f 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e 2==0 || pName2->
1e310 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 n==0 ){. /* F
1e311 6f 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65 20 orm 2: Analyze
1e312 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 the database or
1e313 74 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a 20 table named */.
1e314 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 iDb = sqlite3
1e315 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 FindDb(db, pName
1e316 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e 1);. if( iDb>
1e317 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61 6c =0 ){. anal
1e318 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61 72 yzeDatabase(pPar
1e319 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 65 se, iDb);. }e
1e31a 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 lse{. z = s
1e31b 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
1e31c 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b ken(db, pName1);
1e31d 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a . if( z ){.
1e31e 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 73 pTab = s
1e31f 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c qlite3LocateTabl
1e320 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20 e(pParse, 0, z,
1e321 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 0);. sqli
1e322 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 te3DbFree(db, z)
1e323 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 ;. if( pT
1e324 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ab ){.
1e325 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 analyzeTable(pPa
1e326 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 rse, pTab);.
1e327 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1e328 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1e329 20 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61 6c /* Form 3: Anal
1e32a 79 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71 75 yze the fully qu
1e32b 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 alified table na
1e32c 6d 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 me */. iDb =
1e32d 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 sqlite3TwoPartNa
1e32e 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 me(pParse, pName
1e32f 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61 62 1, pName2, &pTab
1e330 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 leName);. if(
1e331 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 20 iDb>=0 ){.
1e332 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 zDb = db->aDb[i
1e333 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 Db].zName;.
1e334 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 z = sqlite3Name
1e335 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 FromToken(db, pT
1e336 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 20 ableName);.
1e337 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 if( z ){.
1e338 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 pTab = sqlite3
1e339 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 LocateTable(pPar
1e33a 73 65 2c 20 30 2c 20 7a 2c 20 7a 44 62 29 3b 0a se, 0, z, zDb);.
1e33b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1e33c 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 bFree(db, z);.
1e33d 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 if( pTab )
1e33e 7b 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c {. anal
1e33f 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c yzeTable(pParse,
1e340 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 pTab);.
1e341 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 }. }. }
1e342 20 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 . }.}../*.**
1e343 55 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e 66 Used to pass inf
1e344 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 ormation from th
1e345 65 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64 65 e analyzer reade
1e346 72 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 r through to the
1e347 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 .** callback rou
1e348 74 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tine..*/.typedef
1e349 20 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69 73 struct analysis
1e34a 49 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e 66 Info analysisInf
1e34b 6f 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79 73 o;.struct analys
1e34c 69 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 isInfo {. sqlit
1e34d 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20 e3 *db;. const
1e34e 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b char *zDatabase;
1e34f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .};../*.** This
1e350 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f callback is invo
1e351 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 ked once for eac
1e352 68 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65 61 h index when rea
1e353 64 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 ding the.** sqli
1e354 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e 20 te_stat1 table.
1e355 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67 76 .**.** argv
1e356 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74 68 [0] = name of th
1e357 65 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 61 e index.** a
1e358 72 67 76 5b 31 5d 20 3d 20 72 65 73 75 6c 74 73 rgv[1] = results
1e359 20 6f 66 20 61 6e 61 6c 79 73 69 73 20 2d 20 6f of analysis - o
1e35a 6e 20 69 6e 74 65 67 65 72 20 66 6f 72 20 65 61 n integer for ea
1e35b 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 ch column.*/.sta
1e35c 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 73 69 73 tic int analysis
1e35d 4c 6f 61 64 65 72 28 76 6f 69 64 20 2a 70 44 61 Loader(void *pDa
1e35e 74 61 2c 20 69 6e 74 20 61 72 67 63 2c 20 63 68 ta, int argc, ch
1e35f 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 20 ar **argv, char
1e360 2a 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 61 6e **NotUsed){. an
1e361 61 6c 79 73 69 73 49 6e 66 6f 20 2a 70 49 6e 66 alysisInfo *pInf
1e362 6f 20 3d 20 28 61 6e 61 6c 79 73 69 73 49 6e 66 o = (analysisInf
1e363 6f 2a 29 70 44 61 74 61 3b 0a 20 20 49 6e 64 65 o*)pData;. Inde
1e364 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 x *pIndex;. int
1e365 20 69 2c 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 i, c;. unsigne
1e366 64 20 69 6e 74 20 76 3b 0a 20 20 63 6f 6e 73 74 d int v;. const
1e367 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 char *z;.. ass
1e368 65 72 74 28 20 61 72 67 63 3d 3d 32 20 29 3b 0a ert( argc==2 );.
1e369 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
1e36a 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 67 ER2(NotUsed, arg
1e36b 63 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 76 3d c);.. if( argv=
1e36c 3d 30 20 7c 7c 20 61 72 67 76 5b 30 5d 3d 3d 30 =0 || argv[0]==0
1e36d 20 7c 7c 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29 || argv[1]==0 )
1e36e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
1e36f 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 }. pIndex = s
1e370 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 qlite3FindIndex(
1e371 70 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 5b pInfo->db, argv[
1e372 30 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 61 0], pInfo->zData
1e373 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49 6e base);. if( pIn
1e374 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 dex==0 ){. re
1e375 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 turn 0;. }. z
1e376 3d 20 61 72 67 76 5b 31 5d 3b 0a 20 20 66 6f 72 = argv[1];. for
1e377 28 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d 70 (i=0; *z && i<=p
1e378 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 Index->nColumn;
1e379 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 30 3b i++){. v = 0;
1e37a 0a 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a . while( (c=z
1e37b 5b 30 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d [0])>='0' && c<=
1e37c 27 39 27 20 29 7b 0a 20 20 20 20 20 20 76 20 3d '9' ){. v =
1e37d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b v*10 + c - '0';
1e37e 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 . z++;.
1e37f 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 }. pIndex->ai
1e380 52 6f 77 45 73 74 5b 69 5d 20 3d 20 76 3b 0a 20 RowEst[i] = v;.
1e381 20 20 20 69 66 28 20 2a 7a 3d 3d 27 20 27 20 29 if( *z==' ' )
1e382 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 z++;. }. retu
1e383 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c rn 0;.}../*.** L
1e384 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 oad the content
1e385 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 of the sqlite_st
1e386 61 74 31 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 at1 table into t
1e387 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 he index hash ta
1e388 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f bles..*/.SQLITE_
1e389 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1e38a 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 te3AnalysisLoad(
1e38b 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
1e38c 20 69 44 62 29 7b 0a 20 20 61 6e 61 6c 79 73 69 iDb){. analysi
1e38d 73 49 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20 20 48 sInfo sInfo;. H
1e38e 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 63 68 ashElem *i;. ch
1e38f 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 ar *zSql;. int
1e390 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 rc;.. assert( i
1e391 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d Db>=0 && iDb<db-
1e392 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1e393 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 ( db->aDb[iDb].p
1e394 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 Bt!=0 );. asser
1e395 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
1e396 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 oldsMutex(db->aD
1e397 62 5b 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a b[iDb].pBt) );..
1e398 20 20 2f 2a 20 43 6c 65 61 72 20 61 6e 79 20 70 /* Clear any p
1e399 72 69 6f 72 20 73 74 61 74 69 73 74 69 63 73 20 rior statistics
1e39a 2a 2f 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 */. for(i=sqlit
1e39b 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e eHashFirst(&db->
1e39c 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 aDb[iDb].pSchema
1e39d 2d 3e 69 64 78 48 61 73 68 29 3b 69 3b 69 3d 73 ->idxHash);i;i=s
1e39e 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 qliteHashNext(i)
1e39f 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 ){. Index *pI
1e3a0 64 78 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 dx = sqliteHashD
1e3a1 61 74 61 28 69 29 3b 0a 20 20 20 20 73 71 6c 69 ata(i);. sqli
1e3a2 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 te3DefaultRowEst
1e3a3 28 70 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f (pIdx);. }.. /
1e3a4 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 * Check to make
1e3a5 73 75 72 65 20 74 68 65 20 73 71 6c 69 74 65 5f sure the sqlite_
1e3a6 73 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 stat1 table exis
1e3a7 74 73 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e 64 tss */. sInfo.d
1e3a8 62 20 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f 2e b = db;. sInfo.
1e3a9 7a 44 61 74 61 62 61 73 65 20 3d 20 64 62 2d 3e zDatabase = db->
1e3aa 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a aDb[iDb].zName;.
1e3ab 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e if( sqlite3Fin
1e3ac 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 dTable(db, "sqli
1e3ad 74 65 5f 73 74 61 74 31 22 2c 20 73 49 6e 66 6f te_stat1", sInfo
1e3ae 2e 7a 44 61 74 61 62 61 73 65 29 3d 3d 30 20 29 .zDatabase)==0 )
1e3af 7b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 {. return SQ
1e3b0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
1e3b1 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77 20 .. /* Load new
1e3b2 73 74 61 74 69 73 74 69 63 73 20 6f 75 74 20 6f statistics out o
1e3b3 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 f the sqlite_sta
1e3b4 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 53 t1 table */. zS
1e3b5 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 ql = sqlite3MPri
1e3b6 6e 74 66 28 64 62 2c 20 22 53 45 4c 45 43 54 20 ntf(db, "SELECT
1e3b7 69 64 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 25 idx, stat FROM %
1e3b8 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c Q.sqlite_stat1",
1e3b9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1e3ba 20 20 20 20 20 20 20 20 20 73 49 6e 66 6f 2e 7a sInfo.z
1e3bb 44 61 74 61 62 61 73 65 29 3b 0a 20 20 28 76 6f Database);. (vo
1e3bc 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
1e3bd 4f 66 66 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 Off(db);. rc =
1e3be 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c sqlite3_exec(db,
1e3bf 20 7a 53 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c zSql, analysisL
1e3c0 6f 61 64 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 oader, &sInfo, 0
1e3c1 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 );. (void)sqlit
1e3c2 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a e3SafetyOn(db);.
1e3c3 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1e3c4 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 72 65 74 db, zSql);. ret
1e3c5 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 urn rc;.}...#end
1e3c6 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1e3c7 54 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a T_ANALYZE */../*
1e3c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1e3c9 64 20 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a d of analyze.c *
1e3ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e3cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e3cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1e3cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1e3ce 67 69 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e gin file attach.
1e3cf 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1e3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e3d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1e3d2 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 .** 2003 April 6
1e3d3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1e3d4 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1e3d5 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1e3d6 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1e3d7 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1e3d8 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1e3d9 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1e3da 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1e3db 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1e3dc 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1e3dd 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1e3de 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1e3df 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1e3e0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1e3e1 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1e3e2 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1e3e3 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1e3e4 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1e3e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e3e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e3e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e3e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1e3e9 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
1e3ea 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 tains code used
1e3eb 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1e3ec 20 41 54 54 41 43 48 20 61 6e 64 20 44 45 54 41 ATTACH and DETA
1e3ed 43 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a CH commands..**.
1e3ee 2a 2a 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63 ** $Id: attach.c
1e3ef 2c 76 20 31 2e 38 30 20 32 30 30 38 2f 31 31 2f ,v 1.80 2008/11/
1e3f0 31 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 19 09:05:27 dani
1e3f1 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f elk1977 Exp $.*/
1e3f2 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1e3f3 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a _OMIT_ATTACH./*.
1e3f4 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6e 20 65 78 ** Resolve an ex
1e3f5 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 61 pression that wa
1e3f6 73 20 70 61 72 74 20 6f 66 20 61 6e 20 41 54 54 s part of an ATT
1e3f7 41 43 48 20 6f 72 20 44 45 54 41 43 48 20 73 74 ACH or DETACH st
1e3f8 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a atement. This.**
1e3f9 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 is slightly dif
1e3fa 66 65 72 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f ferent from reso
1e3fb 6c 76 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53 lving a normal S
1e3fc 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 QL expression, b
1e3fd 65 63 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a ecause simple.**
1e3fe 20 69 64 65 6e 74 69 66 69 65 72 73 20 61 72 65 identifiers are
1e3ff 20 74 72 65 61 74 65 64 20 61 73 20 73 74 72 69 treated as stri
1e400 6e 67 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c ngs, not possibl
1e401 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f e column names o
1e402 72 20 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a r aliases..**.**
1e403 20 69 2e 65 2e 20 69 66 20 74 68 65 20 70 61 72 i.e. if the par
1e404 73 65 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 ser sees:.**.**
1e405 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42 ATTACH DATAB
1e406 41 53 45 20 61 62 63 20 41 53 20 64 65 66 0a 2a ASE abc AS def.*
1e407 2a 0a 2a 2a 20 69 74 20 74 72 65 61 74 73 20 74 *.** it treats t
1e408 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f he two expressio
1e409 6e 73 20 61 73 20 6c 69 74 65 72 61 6c 20 73 74 ns as literal st
1e40a 72 69 6e 67 73 20 27 61 62 63 27 20 61 6e 64 20 rings 'abc' and
1e40b 27 64 65 66 27 20 69 6e 73 74 65 61 64 20 6f 66 'def' instead of
1e40c 0a 2a 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 .** looking for
1e40d 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 columns of the s
1e40e 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 ame name..**.**
1e40f 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 This only applie
1e410 73 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f s to the root no
1e411 64 65 20 6f 66 20 70 45 78 70 72 2c 20 73 6f 20 de of pExpr, so
1e412 74 68 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a the statement:.*
1e413 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 *.** ATTACH
1e414 44 41 54 41 42 41 53 45 20 61 62 63 7c 7c 64 65 DATABASE abc||de
1e415 66 20 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a f AS 'db2'.**.**
1e416 20 77 69 6c 6c 20 66 61 69 6c 20 62 65 63 61 75 will fail becau
1e417 73 65 20 6e 65 69 74 68 65 72 20 61 62 63 20 6f se neither abc o
1e418 72 20 64 65 66 20 63 61 6e 20 62 65 20 72 65 73 r def can be res
1e419 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 olved..*/.static
1e41a 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 74 74 61 int resolveAtta
1e41b 63 68 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65 chExpr(NameConte
1e41c 78 74 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20 xt *pName, Expr
1e41d 2a 70 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20 *pExpr).{. int
1e41e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1e41f 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20 if( pExpr ){.
1e420 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 if( pExpr->op
1e421 21 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 !=TK_ID ){.
1e422 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73 rc = sqlite3Res
1e423 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e olveExprNames(pN
1e424 61 6d 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 ame, pExpr);.
1e425 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1e426 45 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74 65 33 E_OK && !sqlite3
1e427 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 ExprIsConstant(p
1e428 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 Expr) ){.
1e429 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1e42a 28 70 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20 (pName->pParse,
1e42b 22 69 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c "invalid name: \
1e42c 22 25 54 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e "%T\"", &pExpr->
1e42d 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 span);. r
1e42e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
1e42f 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 OR;. }.
1e430 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 }else{. pEx
1e431 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 pr->op = TK_STRI
1e432 4e 47 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 NG;. }. }.
1e433 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1e434 0a 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d .** An SQL user-
1e435 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 function registe
1e436 72 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f red to do the wo
1e437 72 6b 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 rk of an ATTACH
1e438 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a statement. The.*
1e439 2a 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 * three argument
1e43a 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f s to the functio
1e43b 6e 20 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 n come directly
1e43c 66 72 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73 from an attach s
1e43d 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 tatement:.**.**
1e43e 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42 ATTACH DATAB
1e43f 41 53 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a ASE x AS y KEY z
1e440 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 .**.** SELEC
1e441 54 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 T sqlite_attach(
1e442 78 2c 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 x, y, z).**.** I
1e443 66 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 f the optional "
1e444 4b 45 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73 KEY z" syntax is
1e445 20 6f 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c omitted, an SQL
1e446 20 4e 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 NULL is passed
1e447 61 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 as the.** third
1e448 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 argument..*/.sta
1e449 74 69 63 20 76 6f 69 64 20 61 74 74 61 63 68 46 tic void attachF
1e44a 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
1e44b 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
1e44c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a . int NotUsed,.
1e44d 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
1e44e 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 **argv.){. int
1e44f 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b i;. int rc = 0;
1e450 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1e451 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
1e452 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 _db_handle(conte
1e453 78 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 xt);. const cha
1e454 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 r *zName;. cons
1e455 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 t char *zFile;.
1e456 20 44 62 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61 Db *aNew;. cha
1e457 72 20 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a r *zErrDyn = 0;.
1e458 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d char zErr[128]
1e459 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ;.. UNUSED_PARA
1e45a 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
1e45b 0a 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 . zFile = (cons
1e45c 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 t char *)sqlite3
1e45d 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
1e45e 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 [0]);. zName =
1e45f 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 (const char *)sq
1e460 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1e461 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 (argv[1]);. if(
1e462 20 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c zFile==0 ) zFil
1e463 65 20 3d 20 22 22 3b 0a 20 20 69 66 28 20 7a 4e e = "";. if( zN
1e464 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d ame==0 ) zName =
1e465 20 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b "";.. /* Check
1e466 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 for the followi
1e467 6e 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a ng errors:. **.
1e468 20 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f 20 6d ** * Too m
1e469 61 6e 79 20 61 74 74 61 63 68 65 64 20 64 61 74 any attached dat
1e46a 61 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20 20 20 abases,. **
1e46b 20 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 63 * Transaction c
1e46c 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 urrently open.
1e46d 2a 2a 20 20 20 20 20 2a 20 53 70 65 63 69 66 69 ** * Specifi
1e46e 65 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 ed database name
1e46f 20 61 6c 72 65 61 64 79 20 62 65 69 6e 67 20 75 already being u
1e470 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 sed.. */. if(
1e471 64 62 2d 3e 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69 db->nDb>=db->aLi
1e472 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
1e473 5f 41 54 54 41 43 48 45 44 5d 2b 32 20 29 7b 0a _ATTACHED]+2 ){.
1e474 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1e475 69 6e 74 66 28 0a 20 20 20 20 20 20 73 69 7a 65 intf(. size
1e476 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 of(zErr), zErr,
1e477 22 74 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 "too many attach
1e478 65 64 20 64 61 74 61 62 61 73 65 73 20 2d 20 6d ed databases - m
1e479 61 78 20 25 64 22 2c 20 0a 20 20 20 20 20 20 64 ax %d", . d
1e47a 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
1e47b 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d _LIMIT_ATTACHED]
1e47c 0a 20 20 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f . );. goto
1e47d 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 attach_error;.
1e47e 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 }. if( !db->au
1e47f 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 toCommit ){.
1e480 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1e481 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a (sizeof(zErr), z
1e482 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Err,.
1e483 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f "canno
1e484 74 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73 t ATTACH databas
1e485 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 e within transac
1e486 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f tion");. goto
1e487 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 attach_error;.
1e488 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
1e489 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 db->nDb; i++){.
1e48a 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d char *z = db-
1e48b 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 >aDb[i].zName;.
1e48c 20 20 20 69 66 28 20 7a 20 26 26 20 7a 4e 61 6d if( z && zNam
1e48d 65 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 e && sqlite3StrI
1e48e 43 6d 70 28 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30 Cmp(z, zName)==0
1e48f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1e490 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
1e491 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 0a f(zErr), zErr, .
1e492 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e493 20 20 20 20 20 20 20 22 64 61 74 61 62 61 73 65 "database
1e494 20 25 73 20 69 73 20 61 6c 72 65 61 64 79 20 69 %s is already i
1e495 6e 20 75 73 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a n use", zName);.
1e496 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 goto attac
1e497 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 h_error;. }.
1e498 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 }.. /* Allocat
1e499 65 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 e the new entry
1e49a 69 6e 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d in the db->aDb[]
1e49b 20 61 72 72 61 79 20 61 6e 64 20 69 6e 69 74 69 array and initi
1e49c 61 6c 69 73 65 20 74 68 65 20 73 63 68 65 6d 61 alise the schema
1e49d 0a 20 20 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 . ** hash table
1e49e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 s.. */. if( db
1e49f 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 ->aDb==db->aDbSt
1e4a0 61 74 69 63 20 29 7b 0a 20 20 20 20 61 4e 65 77 atic ){. aNew
1e4a1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1e4a2 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 ocRaw(db, sizeof
1e4a3 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29 (db->aDb[0])*3 )
1e4a4 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d ;. if( aNew==
1e4a5 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 0 ) return;.
1e4a6 6d 65 6d 63 70 79 28 61 4e 65 77 2c 20 64 62 2d memcpy(aNew, db-
1e4a7 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d >aDb, sizeof(db-
1e4a8 3e 61 44 62 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d >aDb[0])*2);. }
1e4a9 65 6c 73 65 7b 0a 20 20 20 20 61 4e 65 77 20 3d else{. aNew =
1e4aa 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
1e4ab 63 28 64 62 2c 20 64 62 2d 3e 61 44 62 2c 20 73 c(db, db->aDb, s
1e4ac 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d izeof(db->aDb[0]
1e4ad 29 2a 28 64 62 2d 3e 6e 44 62 2b 31 29 20 29 3b )*(db->nDb+1) );
1e4ae 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 . if( aNew==0
1e4af 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 ) return;. }.
1e4b0 20 64 62 2d 3e 61 44 62 20 3d 20 61 4e 65 77 3b db->aDb = aNew;
1e4b1 0a 20 20 61 4e 65 77 20 3d 20 26 64 62 2d 3e 61 . aNew = &db->a
1e4b2 44 62 5b 64 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20 Db[db->nDb++];.
1e4b3 20 6d 65 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c memset(aNew, 0,
1e4b4 20 73 69 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b sizeof(*aNew));
1e4b5 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 .. /* Open the
1e4b6 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
1e4b7 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 73 f the btree is s
1e4b8 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e uccessfully open
1e4b9 65 64 2c 20 75 73 65 0a 20 20 2a 2a 20 69 74 20 ed, use. ** it
1e4ba 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 64 61 to obtain the da
1e4bb 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 41 tabase schema. A
1e4bc 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 t this point the
1e4bd 20 73 63 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a schema may. **
1e4be 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 or may not be i
1e4bf 6e 69 74 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f nitialised.. */
1e4c0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
1e4c1 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 treeFactory(db,
1e4c2 7a 46 69 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45 zFile, 0, SQLITE
1e4c3 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 _DEFAULT_CACHE_S
1e4c4 49 5a 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20 IZE,.
1e4c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e4c6 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20 db->openFlags |
1e4c7 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
1e4c8 5f 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20 _DB,.
1e4c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e4ca 26 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 69 &aNew->pBt);. i
1e4cb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1e4cc 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 ){. Pager *p
1e4cd 50 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d Pager;. aNew-
1e4ce 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 >pSchema = sqlit
1e4cf 65 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 e3SchemaGet(db,
1e4d0 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 aNew->pBt);.
1e4d1 69 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 if( !aNew->pSche
1e4d2 6d 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ma ){. rc =
1e4d3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1e4d4 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 }else if( aNe
1e4d5 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 w->pSchema->file
1e4d6 5f 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d _format && aNew-
1e4d7 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 >pSchema->enc!=E
1e4d8 4e 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 NC(db) ){.
1e4d9 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1e4da 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a (sizeof(zErr), z
1e4db 45 72 72 2c 20 0a 20 20 20 20 20 20 20 20 22 61 Err, . "a
1e4dc 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
1e4dd 73 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73 s must use the s
1e4de 61 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e ame text encodin
1e4df 67 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61 g as main databa
1e4e0 73 65 22 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f se");. goto
1e4e1 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 attach_error;.
1e4e2 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 20 }. pPager
1e4e3 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 = sqlite3BtreePa
1e4e4 67 65 72 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a ger(aNew->pBt);.
1e4e5 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
1e4e6 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 LockingMode(pPag
1e4e7 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b er, db->dfltLock
1e4e8 4d 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 Mode);. sqlit
1e4e9 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f e3PagerJournalMo
1e4ea 64 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 de(pPager, db->d
1e4eb 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b fltJournalMode);
1e4ec 0a 20 20 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 . }. aNew->zNa
1e4ed 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 me = sqlite3DbSt
1e4ee 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b rDup(db, zName);
1e4ef 0a 20 20 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f . aNew->safety_
1e4f0 6c 65 76 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 level = 3;..#if
1e4f1 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
1e4f2 0a 20 20 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 . {. extern
1e4f3 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 63 int sqlite3Codec
1e4f4 41 74 74 61 63 68 28 73 71 6c 69 74 65 33 2a 2c Attach(sqlite3*,
1e4f5 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 int, const void
1e4f6 2a 2c 20 69 6e 74 29 3b 0a 20 20 20 20 65 78 74 *, int);. ext
1e4f7 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ern void sqlite3
1e4f8 43 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69 CodecGetKey(sqli
1e4f9 74 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a te3*, int, void*
1e4fa 2a 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e *, int*);. in
1e4fb 74 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 t nKey;. char
1e4fc 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20 *zKey;. int
1e4fd 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 t = sqlite3_valu
1e4fe 65 5f 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3b e_type(argv[2]);
1e4ff 0a 20 20 20 20 73 77 69 74 63 68 28 20 74 20 29 . switch( t )
1e500 7b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c {. case SQL
1e501 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 ITE_INTEGER:.
1e502 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 case SQLITE_F
1e503 4c 4f 41 54 3a 0a 20 20 20 20 20 20 20 20 7a 45 LOAT:. zE
1e504 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 44 rrDyn = sqlite3D
1e505 62 53 74 72 44 75 70 28 64 62 2c 20 22 49 6e 76 bStrDup(db, "Inv
1e506 61 6c 69 64 20 6b 65 79 20 76 61 6c 75 65 22 29 alid key value")
1e507 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 ;. rc = S
1e508 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1e509 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1e50a 20 20 20 20 0a 20 20 20 20 20 20 63 61 73 65 20 . case
1e50b 53 51 4c 49 54 45 5f 54 45 58 54 3a 0a 20 20 20 SQLITE_TEXT:.
1e50c 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 case SQLITE_B
1e50d 4c 4f 42 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65 LOB:. nKe
1e50e 79 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 y = sqlite3_valu
1e50f 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d 29 e_bytes(argv[2])
1e510 3b 0a 20 20 20 20 20 20 20 20 7a 4b 65 79 20 3d ;. zKey =
1e511 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 (char *)sqlite3
1e512 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 _value_blob(argv
1e513 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 [2]);. sq
1e514 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 lite3CodecAttach
1e515 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 (db, db->nDb-1,
1e516 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 zKey, nKey);.
1e517 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 break;..
1e518 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e case SQLITE_N
1e519 55 4c 4c 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 ULL:. /*
1e51a 4e 6f 20 6b 65 79 20 73 70 65 63 69 66 69 65 64 No key specified
1e51b 2e 20 20 55 73 65 20 74 68 65 20 6b 65 79 20 66 . Use the key f
1e51c 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 rom the main dat
1e51d 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 abase */.
1e51e 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 sqlite3CodecGet
1e51f 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 Key(db, 0, (void
1e520 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 **)&zKey, &nKey)
1e521 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1e522 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c 3CodecAttach(db,
1e523 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 db->nDb-1, zKey
1e524 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 , nKey);.
1e525 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1e526 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 }.#endif.. /* I
1e527 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 6f f the file was o
1e528 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c pened successful
1e529 6c 79 2c 20 72 65 61 64 20 74 68 65 20 73 63 68 ly, read the sch
1e52a 65 6d 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20 ema for the new
1e52b 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 database.. ** I
1e52c 66 20 74 68 69 73 20 66 61 69 6c 73 2c 20 6f 72 f this fails, or
1e52d 20 69 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 if opening the
1e52e 66 69 6c 65 20 66 61 69 6c 65 64 2c 20 74 68 65 file failed, the
1e52f 6e 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 n close the file
1e530 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 and . ** remov
1e531 65 20 74 68 65 20 65 6e 74 72 79 20 66 72 6f 6d e the entry from
1e532 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 the db->aDb[] a
1e533 72 72 61 79 2e 20 69 2e 65 2e 20 70 75 74 20 65 rray. i.e. put e
1e534 76 65 72 79 74 68 69 6e 67 20 62 61 63 6b 20 74 verything back t
1e535 68 65 20 77 61 79 0a 20 20 2a 2a 20 77 65 20 66 he way. ** we f
1e536 6f 75 6e 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 ound it.. */.
1e537 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1e538 4b 20 29 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 K ){. (void)s
1e539 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1e53a 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 b);. sqlite3B
1e53b 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 treeEnterAll(db)
1e53c 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1e53d 65 33 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 e3Init(db, &zErr
1e53e 44 79 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Dyn);. sqlite
1e53f 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 3BtreeLeaveAll(d
1e540 62 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 b);. (void)sq
1e541 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1e542 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 b);. }. if( rc
1e543 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20 ){. int iDb
1e544 3d 20 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 = db->nDb - 1;.
1e545 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d assert( iDb>=
1e546 32 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 2 );. if( db-
1e547 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b >aDb[iDb].pBt ){
1e548 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 . sqlite3Bt
1e549 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 reeClose(db->aDb
1e54a 5b 69 44 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 [iDb].pBt);.
1e54b 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 db->aDb[iDb].p
1e54c 42 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 Bt = 0;. db
1e54d 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 ->aDb[iDb].pSche
1e54e 6d 61 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 ma = 0;. }.
1e54f 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e sqlite3ResetIn
1e550 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c ternalSchema(db,
1e551 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62 0);. db->nDb
1e552 20 3d 20 69 44 62 3b 0a 20 20 20 20 69 66 28 20 = iDb;. if(
1e553 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
1e554 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 || rc==SQLITE_I
1e555 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 OERR_NOMEM ){.
1e556 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
1e557 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 iled = 1;.
1e558 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1e559 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 (sizeof(zErr),zE
1e55a 72 72 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f rr, "out of memo
1e55b 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ry");. }else{
1e55c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1e55d 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
1e55e 45 72 72 29 2c 7a 45 72 72 2c 20 22 75 6e 61 62 Err),zErr, "unab
1e55f 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 le to open datab
1e560 61 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c 65 29 ase: %s", zFile)
1e561 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f ;. }. goto
1e562 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 attach_error;.
1e563 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 3b 0a }. . return;.
1e564 0a 61 74 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 .attach_error:.
1e565 20 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 /* Return an er
1e566 72 6f 72 20 69 66 20 77 65 20 67 65 74 20 68 65 ror if we get he
1e567 72 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45 72 72 re */. if( zErr
1e568 44 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Dyn ){. sqlit
1e569 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
1e56a 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79 6e context, zErrDyn
1e56b 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 , -1);. sqlit
1e56c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 e3DbFree(db, zEr
1e56d 72 44 79 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a rDyn);. }else{.
1e56e 20 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 zErr[sizeof(
1e56f 7a 45 72 72 29 2d 31 5d 20 3d 20 30 3b 0a 20 20 zErr)-1] = 0;.
1e570 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
1e571 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 _error(context,
1e572 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 zErr, -1);. }.
1e573 20 69 66 28 20 72 63 20 29 20 73 71 6c 69 74 65 if( rc ) sqlite
1e574 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 3_result_error_c
1e575 6f 64 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 ode(context, rc)
1e576 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 ;.}../*.** An SQ
1e577 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 L user-function
1e578 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f registered to do
1e579 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 the work of an
1e57a 44 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 DETACH statement
1e57b 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 . The.** three a
1e57c 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 rguments to the
1e57d 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 function come di
1e57e 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 rectly from a de
1e57f 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a tach statement:.
1e580 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 **.** DETACH
1e581 20 44 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a DATABASE x.**.*
1e582 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c * SELECT sql
1e583 69 74 65 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f ite_detach(x).*/
1e584 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 74 .static void det
1e585 61 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 achFunc(. sqlit
1e586 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
1e587 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 ext,. int NotUs
1e588 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ed,. sqlite3_va
1e589 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
1e58a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
1e58b 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 e = (const char
1e58c 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *)sqlite3_value_
1e58d 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 text(argv[0]);.
1e58e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 sqlite3 *db = s
1e58f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
1e590 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
1e591 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44 62 );. int i;. Db
1e592 20 2a 70 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 *pDb = 0;. cha
1e593 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 r zErr[128];..
1e594 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1e595 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 (NotUsed);.. if
1e596 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 ( zName==0 ) zNa
1e597 6d 65 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69 me = "";. for(i
1e598 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
1e599 2b 2b 29 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 ++){. pDb = &
1e59a 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 db->aDb[i];.
1e59b 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 if( pDb->pBt==0
1e59c 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1e59d 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
1e59e 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a mp(pDb->zName, z
1e59f 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b Name)==0 ) break
1e5a0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d ;. }.. if( i>=
1e5a1 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73 db->nDb ){. s
1e5a2 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1e5a3 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 sizeof(zErr),zEr
1e5a4 72 2c 20 22 6e 6f 20 73 75 63 68 20 64 61 74 61 r, "no such data
1e5a5 62 61 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 base: %s", zName
1e5a6 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 );. goto deta
1e5a7 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 ch_error;. }.
1e5a8 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 73 if( i<2 ){. s
1e5a9 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1e5aa 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 sizeof(zErr),zEr
1e5ab 72 2c 20 22 63 61 6e 6e 6f 74 20 64 65 74 61 63 r, "cannot detac
1e5ac 68 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20 h database %s",
1e5ad 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f zName);. goto
1e5ae 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 detach_error;.
1e5af 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 }. if( !db->au
1e5b0 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 toCommit ){.
1e5b1 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1e5b2 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a (sizeof(zErr), z
1e5b3 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Err,.
1e5b4 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f "canno
1e5b5 74 20 44 45 54 41 43 48 20 64 61 74 61 62 61 73 t DETACH databas
1e5b6 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 e within transac
1e5b7 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f tion");. goto
1e5b8 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 detach_error;.
1e5b9 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1e5ba 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 BtreeIsInReadTra
1e5bb 6e 73 28 70 44 62 2d 3e 70 42 74 29 20 29 7b 0a ns(pDb->pBt) ){.
1e5bc 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1e5bd 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 intf(sizeof(zErr
1e5be 29 2c 7a 45 72 72 2c 20 22 64 61 74 61 62 61 73 ),zErr, "databas
1e5bf 65 20 25 73 20 69 73 20 6c 6f 63 6b 65 64 22 2c e %s is locked",
1e5c0 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 zName);. got
1e5c1 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a o detach_error;.
1e5c2 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 }.. sqlite3Bt
1e5c3 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 reeClose(pDb->pB
1e5c4 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42 74 20 3d t);. pDb->pBt =
1e5c5 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 0;. pDb->pSche
1e5c6 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 ma = 0;. sqlite
1e5c7 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 3ResetInternalSc
1e5c8 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 72 hema(db, 0);. r
1e5c9 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63 68 5f 65 eturn;..detach_e
1e5ca 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 5f rror:. sqlite3_
1e5cb 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e result_error(con
1e5cc 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b text, zErr, -1);
1e5cd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 .}../*.** This p
1e5ce 72 6f 63 65 64 75 72 65 20 67 65 6e 65 72 61 74 rocedure generat
1e5cf 65 73 20 56 44 42 45 20 63 6f 64 65 20 66 6f 72 es VDBE code for
1e5d0 20 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f 63 61 a single invoca
1e5d1 74 69 6f 6e 20 6f 66 20 65 69 74 68 65 72 20 74 tion of either t
1e5d2 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 64 65 74 he.** sqlite_det
1e5d3 61 63 68 28 29 20 6f 72 20 73 71 6c 69 74 65 5f ach() or sqlite_
1e5d4 61 74 74 61 63 68 28 29 20 53 51 4c 20 75 73 65 attach() SQL use
1e5d5 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a r functions..*/.
1e5d6 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 static void code
1e5d7 41 74 74 61 63 68 28 0a 20 20 50 61 72 73 65 20 Attach(. Parse
1e5d8 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f *pParse, /
1e5d9 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e * The parser con
1e5da 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 74 79 text */. int ty
1e5db 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f pe, /
1e5dc 2a 20 45 69 74 68 65 72 20 53 51 4c 49 54 45 5f * Either SQLITE_
1e5dd 41 54 54 41 43 48 20 6f 72 20 53 51 4c 49 54 45 ATTACH or SQLITE
1e5de 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20 46 75 6e _DETACH */. Fun
1e5df 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 20 20 20 cDef *pFunc,
1e5e0 20 20 2f 2a 20 46 75 6e 63 44 65 66 20 77 72 61 /* FuncDef wra
1e5e1 70 70 65 72 20 66 6f 72 20 64 65 74 61 63 68 46 pper for detachF
1e5e2 75 6e 63 28 29 20 6f 72 20 61 74 74 61 63 68 46 unc() or attachF
1e5e3 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78 70 72 20 unc() */. Expr
1e5e4 2a 70 41 75 74 68 41 72 67 2c 20 20 20 20 20 20 *pAuthArg,
1e5e5 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f /* Expression to
1e5e6 20 70 61 73 73 20 74 6f 20 61 75 74 68 6f 72 69 pass to authori
1e5e7 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 zation callback
1e5e8 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46 69 6c 65 */. Expr *pFile
1e5e9 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d name, /* Nam
1e5ea 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 e of database fi
1e5eb 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 le */. Expr *pD
1e5ec 62 6e 61 6d 65 2c 20 20 20 20 20 20 20 2f 2a 20 bname, /*
1e5ed 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 Name of the data
1e5ee 62 61 73 65 20 74 6f 20 75 73 65 20 69 6e 74 65 base to use inte
1e5ef 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 78 70 72 rnally */. Expr
1e5f0 20 2a 70 4b 65 79 20 20 20 20 20 20 20 20 20 20 *pKey
1e5f1 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6b 65 79 /* Database key
1e5f2 20 66 6f 72 20 65 6e 63 72 79 70 74 69 6f 6e 20 for encryption
1e5f3 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a extension */.){.
1e5f4 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e 61 6d 65 int rc;. Name
1e5f5 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 3b 0a 20 Context sName;.
1e5f6 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 Vdbe *v;. sqli
1e5f7 74 65 33 2a 20 64 62 20 3d 20 70 50 61 72 73 65 te3* db = pParse
1e5f8 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 65 67 41 ->db;. int regA
1e5f9 72 67 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 rgs;..#ifndef SQ
1e5fa 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
1e5fb 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72 74 IZATION. assert
1e5fc 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
1e5fd 65 64 20 7c 7c 20 70 41 75 74 68 41 72 67 20 29 ed || pAuthArg )
1e5fe 3b 0a 20 20 69 66 28 20 70 41 75 74 68 41 72 67 ;. if( pAuthArg
1e5ff 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 ){. char *zA
1e600 75 74 68 41 72 67 20 3d 20 73 71 6c 69 74 65 33 uthArg = sqlite3
1e601 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 NameFromToken(db
1e602 2c 20 26 70 41 75 74 68 41 72 67 2d 3e 73 70 61 , &pAuthArg->spa
1e603 6e 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 41 75 n);. if( !zAu
1e604 74 68 41 72 67 20 29 7b 0a 20 20 20 20 20 20 67 thArg ){. g
1e605 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a oto attach_end;.
1e606 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 }. rc = s
1e607 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
1e608 70 50 61 72 73 65 2c 20 74 79 70 65 2c 20 7a 41 pParse, type, zA
1e609 75 74 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a 20 uthArg, 0, 0);.
1e60a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1e60b 28 64 62 2c 20 7a 41 75 74 68 41 72 67 29 3b 0a (db, zAuthArg);.
1e60c 20 20 20 20 69 66 28 72 63 21 3d 53 51 4c 49 54 if(rc!=SQLIT
1e60d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f E_OK ){. go
1e60e 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 to attach_end;.
1e60f 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 }. }.#endif
1e610 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 /* SQLITE_OMIT_A
1e611 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a UTHORIZATION */.
1e612 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 61 6d 65 . memset(&sName
1e613 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65 , 0, sizeof(Name
1e614 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e 61 Context));. sNa
1e615 6d 65 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 me.pParse = pPar
1e616 73 65 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20 20 se;.. if( .
1e617 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 SQLITE_OK!=(rc
1e618 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 = resolveAttach
1e619 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 46 69 Expr(&sName, pFi
1e61a 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 lename)) ||.
1e61b 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 SQLITE_OK!=(rc
1e61c 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 = resolveAttach
1e61d 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 44 62 Expr(&sName, pDb
1e61e 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 name)) ||.
1e61f 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
1e620 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 resolveAttachEx
1e621 70 72 28 26 73 4e 61 6d 65 2c 20 70 4b 65 79 29 pr(&sName, pKey)
1e622 29 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 72 73 ). ){. pPars
1e623 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 e->nErr++;. g
1e624 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a oto attach_end;.
1e625 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 }.. v = sqlit
1e626 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
1e627 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d 20 73 );. regArgs = s
1e628 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e qlite3GetTempRan
1e629 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b 0a 20 ge(pParse, 4);.
1e62a 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
1e62b 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e 61 (pParse, pFilena
1e62c 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a 20 20 me, regArgs);.
1e62d 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
1e62e 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65 2c pParse, pDbname,
1e62f 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20 20 73 regArgs+1);. s
1e630 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
1e631 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72 65 67 Parse, pKey, reg
1e632 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73 73 65 Args+2);.. asse
1e633 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c rt( v || db->mal
1e634 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 locFailed );. i
1e635 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 f( v ){. sqli
1e636 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
1e637 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 2c OP_Function, 0,
1e638 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75 6e 63 regArgs+3-pFunc
1e639 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67 73 2b ->nArg, regArgs+
1e63a 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 3);. sqlite3V
1e63b 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70 dbeChangeP5(v, p
1e63c 46 75 6e 63 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 Func->nArg);.
1e63d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1e63e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 geP4(v, -1, (cha
1e63f 72 20 2a 29 70 46 75 6e 63 2c 20 50 34 5f 46 55 r *)pFunc, P4_FU
1e640 4e 43 44 45 46 29 3b 0a 0a 20 20 20 20 2f 2a 20 NCDEF);.. /*
1e641 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70 69 72 Code an OP_Expir
1e642 65 2e 20 46 6f 72 20 61 6e 20 41 54 54 41 43 48 e. For an ATTACH
1e643 20 73 74 61 74 65 6d 65 6e 74 2c 20 73 65 74 20 statement, set
1e644 50 31 20 74 6f 20 74 72 75 65 20 28 65 78 70 69 P1 to true (expi
1e645 72 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 re this. ** s
1e646 74 61 74 65 6d 65 6e 74 20 6f 6e 6c 79 29 2e 20 tatement only).
1e647 46 6f 72 20 44 45 54 41 43 48 2c 20 73 65 74 20 For DETACH, set
1e648 69 74 20 74 6f 20 66 61 6c 73 65 20 28 65 78 70 it to false (exp
1e649 69 72 65 20 61 6c 6c 20 65 78 69 73 74 69 6e 67 ire all existing
1e64a 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e . ** statemen
1e64b 74 73 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ts).. */.
1e64c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1e64d 31 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 1(v, OP_Expire,
1e64e 28 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 41 54 (type==SQLITE_AT
1e64f 54 41 43 48 29 29 3b 0a 20 20 7d 0a 20 20 0a 61 TACH));. }. .a
1e650 74 74 61 63 68 5f 65 6e 64 3a 0a 20 20 73 71 6c ttach_end:. sql
1e651 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
1e652 62 2c 20 70 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 b, pFilename);.
1e653 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
1e654 74 65 28 64 62 2c 20 70 44 62 6e 61 6d 65 29 3b te(db, pDbname);
1e655 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 . sqlite3ExprDe
1e656 6c 65 74 65 28 64 62 2c 20 70 4b 65 79 29 3b 0a lete(db, pKey);.
1e657 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 }../*.** Called
1e658 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f by the parser to
1e659 20 63 6f 6d 70 69 6c 65 20 61 20 44 45 54 41 43 compile a DETAC
1e65a 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a H statement..**.
1e65b 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 70 44 ** DETACH pD
1e65c 62 6e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f bname.*/.SQLITE_
1e65d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1e65e 69 74 65 33 44 65 74 61 63 68 28 50 61 72 73 65 ite3Detach(Parse
1e65f 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
1e660 70 44 62 6e 61 6d 65 29 7b 0a 20 20 73 74 61 74 pDbname){. stat
1e661 69 63 20 46 75 6e 63 44 65 66 20 64 65 74 61 63 ic FuncDef detac
1e662 68 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20 20 31 h_func = {. 1
1e663 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1e664 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20 20 20 /* nArg */.
1e665 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 SQLITE_UTF8,
1e666 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a 2f /* iPrefEnc */
1e667 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
1e668 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 /* flags
1e669 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
1e66a 20 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73 65 /* pUse
1e66b 72 44 61 74 61 20 2a 2f 0a 20 20 20 20 30 2c 20 rData */. 0,
1e66c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e66d 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 64 * pNext */. d
1e66e 65 74 61 63 68 46 75 6e 63 2c 20 20 20 20 20 20 etachFunc,
1e66f 20 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20 20 20 /* xFunc */.
1e670 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1e671 20 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f 0a 20 /* xStep */.
1e672 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
1e673 20 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c 69 7a /* xFinaliz
1e674 65 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69 74 65 e */. "sqlite
1e675 5f 64 65 74 61 63 68 22 2c 20 20 2f 2a 20 7a 4e _detach", /* zN
1e676 61 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20 20 20 ame */. 0
1e677 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e678 70 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20 pHash */. };.
1e679 63 6f 64 65 41 74 74 61 63 68 28 70 50 61 72 73 codeAttach(pPars
1e67a 65 2c 20 53 51 4c 49 54 45 5f 44 45 54 41 43 48 e, SQLITE_DETACH
1e67b 2c 20 26 64 65 74 61 63 68 5f 66 75 6e 63 2c 20 , &detach_func,
1e67c 70 44 62 6e 61 6d 65 2c 20 30 2c 20 30 2c 20 70 pDbname, 0, 0, p
1e67d 44 62 6e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a Dbname);.}../*.*
1e67e 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20 * Called by the
1e67f 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c parser to compil
1e680 65 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 e an ATTACH stat
1e681 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 ement..**.**
1e682 20 41 54 54 41 43 48 20 70 20 41 53 20 70 44 62 ATTACH p AS pDb
1e683 6e 61 6d 65 20 4b 45 59 20 70 4b 65 79 0a 2a 2f name KEY pKey.*/
1e684 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1e685 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74 61 void sqlite3Atta
1e686 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ch(Parse *pParse
1e687 2c 20 45 78 70 72 20 2a 70 2c 20 45 78 70 72 20 , Expr *p, Expr
1e688 2a 70 44 62 6e 61 6d 65 2c 20 45 78 70 72 20 2a *pDbname, Expr *
1e689 70 4b 65 79 29 7b 0a 20 20 73 74 61 74 69 63 20 pKey){. static
1e68a 46 75 6e 63 44 65 66 20 61 74 74 61 63 68 5f 66 FuncDef attach_f
1e68b 75 6e 63 20 3d 20 7b 0a 20 20 20 20 33 2c 20 20 unc = {. 3,
1e68c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e68d 20 6e 41 72 67 20 2a 2f 0a 20 20 20 20 53 51 4c nArg */. SQL
1e68e 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 2f ITE_UTF8, /
1e68f 2a 20 69 50 72 65 66 45 6e 63 20 2a 2f 0a 20 20 * iPrefEnc */.
1e690 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
1e691 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a /* flags */.
1e692 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 0,
1e693 20 20 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61 /* pUserDa
1e694 74 61 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 ta */. 0,
1e695 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 /* p
1e696 4e 65 78 74 20 2a 2f 0a 20 20 20 20 61 74 74 61 Next */. atta
1e697 63 68 46 75 6e 63 2c 20 20 20 20 20 20 20 2f 2a chFunc, /*
1e698 20 78 46 75 6e 63 20 2a 2f 0a 20 20 20 20 30 2c xFunc */. 0,
1e699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e69a 2f 2a 20 78 53 74 65 70 20 2a 2f 0a 20 20 20 20 /* xStep */.
1e69b 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1e69c 20 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a /* xFinalize *
1e69d 2f 0a 20 20 20 20 22 73 71 6c 69 74 65 5f 61 74 /. "sqlite_at
1e69e 74 61 63 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 tach", /* zName
1e69f 20 2a 2f 0a 20 20 20 20 30 20 20 20 20 20 20 20 */. 0
1e6a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61 /* pHa
1e6a1 73 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 sh */. };. cod
1e6a2 65 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 eAttach(pParse,
1e6a3 53 51 4c 49 54 45 5f 41 54 54 41 43 48 2c 20 26 SQLITE_ATTACH, &
1e6a4 61 74 74 61 63 68 5f 66 75 6e 63 2c 20 70 2c 20 attach_func, p,
1e6a5 70 2c 20 70 44 62 6e 61 6d 65 2c 20 70 4b 65 79 p, pDbname, pKey
1e6a6 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
1e6a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 QLITE_OMIT_ATTAC
1e6a8 48 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 H */../*.** Init
1e6a9 69 61 6c 69 7a 65 20 61 20 44 62 46 69 78 65 72 ialize a DbFixer
1e6aa 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 structure. Thi
1e6ab 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 s routine must b
1e6ac 65 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 0a 2a e called prior.*
1e6ad 2a 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 * to passing the
1e6ae 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 6f 6e structure to on
1e6af 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 46 e of the sqliteF
1e6b0 69 78 41 41 41 41 28 29 20 72 6f 75 74 69 6e 65 ixAAAA() routine
1e6b1 73 20 62 65 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 s below..**.** T
1e6b2 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
1e6b3 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65 indicates whethe
1e6b4 72 20 6f 72 20 6e 6f 74 20 66 69 78 61 74 69 6f r or not fixatio
1e6b5 6e 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 20 n is required.
1e6b6 54 52 55 45 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 TRUE.** means we
1e6b7 20 64 6f 20 6e 65 65 64 20 74 6f 20 66 69 78 20 do need to fix
1e6b8 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65 66 the database ref
1e6b9 65 72 65 6e 63 65 73 2c 20 46 41 4c 53 45 20 6d erences, FALSE m
1e6ba 65 61 6e 73 20 77 65 20 64 6f 20 6e 6f 74 2e 0a eans we do not..
1e6bb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1e6bc 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 E int sqlite3Fix
1e6bd 49 6e 69 74 28 0a 20 20 44 62 46 69 78 65 72 20 Init(. DbFixer
1e6be 2a 70 46 69 78 2c 20 20 20 20 20 20 2f 2a 20 54 *pFix, /* T
1e6bf 68 65 20 66 69 78 65 72 20 74 6f 20 62 65 20 69 he fixer to be i
1e6c0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 nitialized */.
1e6c1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1e6c2 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 /* Error mes
1e6c3 73 61 67 65 73 20 77 69 6c 6c 20 62 65 20 77 72 sages will be wr
1e6c4 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 itten here */.
1e6c5 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 int iDb,
1e6c6 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 /* This is t
1e6c7 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 he database that
1e6c8 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 2a 2f must be used */
1e6c9 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1e6ca 54 79 70 65 2c 20 20 2f 2a 20 22 76 69 65 77 22 Type, /* "view"
1e6cb 2c 20 22 74 72 69 67 67 65 72 22 2c 20 6f 72 20 , "trigger", or
1e6cc 22 69 6e 64 65 78 22 20 2a 2f 0a 20 20 63 6f 6e "index" */. con
1e6cd 73 74 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 st Token *pName
1e6ce 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1e6cf 76 69 65 77 2c 20 74 72 69 67 67 65 72 2c 20 6f view, trigger, o
1e6d0 72 20 69 6e 64 65 78 20 2a 2f 0a 29 7b 0a 20 20 r index */.){.
1e6d1 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 sqlite3 *db;..
1e6d2 69 66 28 20 69 44 62 3c 30 20 7c 7c 20 69 44 62 if( iDb<0 || iDb
1e6d3 3d 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==1 ) return 0;.
1e6d4 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
1e6d5 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d b;. assert( db-
1e6d6 3e 6e 44 62 3e 69 44 62 20 29 3b 0a 20 20 70 46 >nDb>iDb );. pF
1e6d7 69 78 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 ix->pParse = pPa
1e6d8 72 73 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 44 62 rse;. pFix->zDb
1e6d9 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
1e6da 7a 4e 61 6d 65 3b 0a 20 20 70 46 69 78 2d 3e 7a zName;. pFix->z
1e6db 54 79 70 65 20 3d 20 7a 54 79 70 65 3b 0a 20 20 Type = zType;.
1e6dc 70 46 69 78 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e pFix->pName = pN
1e6dd 61 6d 65 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b ame;. return 1;
1e6de 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f .}../*.** The fo
1e6df 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 llowing set of r
1e6e0 6f 75 74 69 6e 65 73 20 77 61 6c 6b 20 74 68 72 outines walk thr
1e6e1 6f 75 67 68 20 74 68 65 20 70 61 72 73 65 20 74 ough the parse t
1e6e2 72 65 65 20 61 6e 64 20 61 73 73 69 67 6e 0a 2a ree and assign.*
1e6e3 2a 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74 * a specific dat
1e6e4 61 62 61 73 65 20 74 6f 20 61 6c 6c 20 74 61 62 abase to all tab
1e6e5 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68 le references wh
1e6e6 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ere the database
1e6e7 20 6e 61 6d 65 0a 2a 2a 20 77 61 73 20 6c 65 66 name.** was lef
1e6e8 74 20 75 6e 73 70 65 63 69 66 69 65 64 20 69 6e t unspecified in
1e6e9 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 the original SQ
1e6ea 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 L statement. Th
1e6eb 65 20 70 46 69 78 20 73 74 72 75 63 74 75 72 65 e pFix structure
1e6ec 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 .** must have be
1e6ed 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 en initialized b
1e6ee 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 y a prior call t
1e6ef 6f 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 o sqlite3FixInit
1e6f0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ()..**.** These
1e6f1 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 routines are use
1e6f2 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 d to make sure t
1e6f3 68 61 74 20 61 6e 20 69 6e 64 65 78 2c 20 74 72 hat an index, tr
1e6f4 69 67 67 65 72 2c 20 6f 72 0a 2a 2a 20 76 69 65 igger, or.** vie
1e6f5 77 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 w in one databas
1e6f6 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 66 65 72 e does not refer
1e6f7 20 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 61 to objects in a
1e6f8 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 different datab
1e6f9 61 73 65 2e 0a 2a 2a 20 28 45 78 63 65 70 74 69 ase..** (Excepti
1e6fa 6f 6e 3a 20 69 6e 64 69 63 65 73 2c 20 74 72 69 on: indices, tri
1e6fb 67 67 65 72 73 2c 20 61 6e 64 20 76 69 65 77 73 ggers, and views
1e6fc 20 69 6e 20 74 68 65 20 54 45 4d 50 20 64 61 74 in the TEMP dat
1e6fd 61 62 61 73 65 20 61 72 65 0a 2a 2a 20 61 6c 6c abase are.** all
1e6fe 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f owed to refer to
1e6ff 20 61 6e 79 74 68 69 6e 67 2e 29 20 20 49 66 20 anything.) If
1e700 61 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 65 a reference is e
1e701 78 70 6c 69 63 69 74 6c 79 20 6d 61 64 65 0a 2a xplicitly made.*
1e702 2a 20 74 6f 20 61 6e 20 6f 62 6a 65 63 74 20 69 * to an object i
1e703 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 61 n a different da
1e704 74 61 62 61 73 65 2c 20 61 6e 20 65 72 72 6f 72 tabase, an error
1e705 20 6d 65 73 73 61 67 65 20 69 73 20 61 64 64 65 message is adde
1e706 64 20 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2d 3e d to.** pParse->
1e707 7a 45 72 72 4d 73 67 20 61 6e 64 20 74 68 65 73 zErrMsg and thes
1e708 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 e routines retur
1e709 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 n non-zero. If
1e70a 65 76 65 72 79 74 68 69 6e 67 0a 2a 2a 20 63 68 everything.** ch
1e70b 65 63 6b 73 20 6f 75 74 2c 20 74 68 65 73 65 20 ecks out, these
1e70c 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
1e70d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 0..*/.SQLITE_PRI
1e70e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1e70f 46 69 78 53 72 63 4c 69 73 74 28 0a 20 20 44 62 FixSrcList(. Db
1e710 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 Fixer *pFix,
1e711 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 /* Context of
1e712 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f the fixation */
1e713 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 . SrcList *pLis
1e714 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 t /* The S
1e715 6f 75 72 63 65 20 6c 69 73 74 20 74 6f 20 63 68 ource list to ch
1e716 65 63 6b 20 61 6e 64 20 6d 6f 64 69 66 79 20 2a eck and modify *
1e717 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 /.){. int i;.
1e718 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b const char *zDb;
1e719 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 . struct SrcLis
1e71a 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a t_item *pItem;..
1e71b 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 if( pList==0 )
1e71c 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7a 44 62 return 0;. zDb
1e71d 20 3d 20 70 46 69 78 2d 3e 7a 44 62 3b 0a 20 20 = pFix->zDb;.
1e71e 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 for(i=0, pItem=p
1e71f 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 List->a; i<pList
1e720 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 ->nSrc; i++, pIt
1e721 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 em++){. if( p
1e722 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 3d Item->zDatabase=
1e723 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65 =0 ){. pIte
1e724 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 m->zDatabase = s
1e725 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 70 qlite3DbStrDup(p
1e726 46 69 78 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c Fix->pParse->db,
1e727 20 7a 44 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65 zDb);. }else
1e728 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
1e729 43 6d 70 28 70 49 74 65 6d 2d 3e 7a 44 61 74 61 Cmp(pItem->zData
1e72a 62 61 73 65 2c 7a 44 62 29 21 3d 30 20 29 7b 0a base,zDb)!=0 ){.
1e72b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1e72c 6f 72 4d 73 67 28 70 46 69 78 2d 3e 70 50 61 72 orMsg(pFix->pPar
1e72d 73 65 2c 0a 20 20 20 20 20 20 20 20 20 22 25 73 se,. "%s
1e72e 20 25 54 20 63 61 6e 6e 6f 74 20 72 65 66 65 72 %T cannot refer
1e72f 65 6e 63 65 20 6f 62 6a 65 63 74 73 20 69 6e 20 ence objects in
1e730 64 61 74 61 62 61 73 65 20 25 73 22 2c 0a 20 20 database %s",.
1e731 20 20 20 20 20 20 20 70 46 69 78 2d 3e 7a 54 79 pFix->zTy
1e732 70 65 2c 20 70 46 69 78 2d 3e 70 4e 61 6d 65 2c pe, pFix->pName,
1e733 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 pItem->zDatabas
1e734 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e e);. return
1e735 20 31 3b 0a 20 20 20 20 7d 0a 23 69 66 20 21 64 1;. }.#if !d
1e736 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1e737 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 IT_VIEW) || !def
1e738 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1e739 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20 69 66 _TRIGGER). if
1e73a 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 ( sqlite3FixSele
1e73b 63 74 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e ct(pFix, pItem->
1e73c 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72 pSelect) ) retur
1e73d 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 73 71 6c n 1;. if( sql
1e73e 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78 ite3FixExpr(pFix
1e73f 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 20 29 20 , pItem->pOn) )
1e740 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 return 1;.#endif
1e741 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
1e742 0a 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 .}.#if !defined(
1e743 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
1e744 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 ) || !defined(SQ
1e745 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
1e746 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 R).SQLITE_PRIVAT
1e747 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 E int sqlite3Fix
1e748 53 65 6c 65 63 74 28 0a 20 20 44 62 46 69 78 65 Select(. DbFixe
1e749 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f r *pFix, /
1e74a 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 * Context of the
1e74b 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 fixation */. S
1e74c 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 elect *pSelect
1e74d 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 /* The SELEC
1e74e 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 T statement to b
1e74f 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 e fixed to one d
1e750 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 atabase */.){.
1e751 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20 29 while( pSelect )
1e752 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
1e753 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 3FixExprList(pFi
1e754 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 x, pSelect->pELi
1e755 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 st) ){. ret
1e756 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 1;. }.
1e757 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53 if( sqlite3FixS
1e758 72 63 4c 69 73 74 28 70 46 69 78 2c 20 70 53 65 rcList(pFix, pSe
1e759 6c 65 63 74 2d 3e 70 53 72 63 29 20 29 7b 0a 20 lect->pSrc) ){.
1e75a 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
1e75b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
1e75c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78 ite3FixExpr(pFix
1e75d 2c 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72 , pSelect->pWher
1e75e 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 e) ){. retu
1e75f 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 1;. }.
1e760 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 if( sqlite3FixEx
1e761 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 pr(pFix, pSelect
1e762 2d 3e 70 48 61 76 69 6e 67 29 20 29 7b 0a 20 20 ->pHaving) ){.
1e763 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
1e764 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 }. pSelect
1e765 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f = pSelect->pPrio
1e766 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 r;. }. return
1e767 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 0;.}.SQLITE_PRIV
1e768 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
1e769 69 78 45 78 70 72 28 0a 20 20 44 62 46 69 78 65 ixExpr(. DbFixe
1e76a 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 r *pFix, /*
1e76b 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 Context of the f
1e76c 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 ixation */. Exp
1e76d 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20 r *pExpr
1e76e 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f /* The expressio
1e76f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f n to be fixed to
1e770 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f one database */
1e771 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 45 78 .){. while( pEx
1e772 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 pr ){. if( sq
1e773 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 lite3FixSelect(p
1e774 46 69 78 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c Fix, pExpr->pSel
1e775 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ect) ){. re
1e776 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
1e777 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 if( sqlite3Fix
1e778 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 ExprList(pFix, p
1e779 45 78 70 72 2d 3e 70 4c 69 73 74 29 20 29 7b 0a Expr->pList) ){.
1e77a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
1e77b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 }. if( sq
1e77c 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 lite3FixExpr(pFi
1e77d 78 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 x, pExpr->pRight
1e77e 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
1e77f 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 n 1;. }. p
1e780 45 78 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c Expr = pExpr->pL
1e781 65 66 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 eft;. }. retur
1e782 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 n 0;.}.SQLITE_PR
1e783 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1e784 33 46 69 78 45 78 70 72 4c 69 73 74 28 0a 20 20 3FixExprList(.
1e785 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 DbFixer *pFix,
1e786 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 /* Context of
1e787 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f the fixation */
1e788 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 . ExprList *pLi
1e789 73 74 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 st /* The exp
1e78a 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 ression to be fi
1e78b 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 xed to one datab
1e78c 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ase */.){. int
1e78d 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 i;. struct Expr
1e78e 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1e78f 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 ;. if( pList==0
1e790 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66 ) return 0;. f
1e791 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c or(i=0, pItem=pL
1e792 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d ist->a; i<pList-
1e793 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 >nExpr; i++, pIt
1e794 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 em++){. if( s
1e795 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 qlite3FixExpr(pF
1e796 69 78 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 ix, pItem->pExpr
1e797 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
1e798 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
1e799 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e return 0;.}.#en
1e79a 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
1e79b 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1e79c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1e79d 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 int sqlite3FixTr
1e79e 69 67 67 65 72 53 74 65 70 28 0a 20 20 44 62 46 iggerStep(. DbF
1e79f 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 ixer *pFix,
1e7a0 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 /* Context of th
1e7a1 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 e fixation */.
1e7a2 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 TriggerStep *pSt
1e7a3 65 70 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 ep /* The trigge
1e7a4 72 20 73 74 65 70 20 62 65 20 66 69 78 65 64 20 r step be fixed
1e7a5 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 to one database
1e7a6 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 */.){. while( p
1e7a7 53 74 65 70 20 29 7b 0a 20 20 20 20 69 66 28 20 Step ){. if(
1e7a8 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 sqlite3FixSelect
1e7a9 28 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 53 (pFix, pStep->pS
1e7aa 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 elect) ){.
1e7ab 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
1e7ac 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 if( sqlite3F
1e7ad 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 74 ixExpr(pFix, pSt
1e7ae 65 70 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 ep->pWhere) ){.
1e7af 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
1e7b0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
1e7b1 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 ite3FixExprList(
1e7b2 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 45 78 pFix, pStep->pEx
1e7b3 70 72 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 prList) ){.
1e7b4 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
1e7b5 0a 20 20 20 20 70 53 74 65 70 20 3d 20 70 53 74 . pStep = pSt
1e7b6 65 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 ep->pNext;. }.
1e7b7 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e return 0;.}.#en
1e7b8 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
1e7b9 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 74 74 61 **** End of atta
1e7ba 63 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ch.c ***********
1e7bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e7bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e7bd 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1e7be 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1e7bf 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a auth.c *********
1e7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e7c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e7c2 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 ***/./*.** 2003
1e7c3 4a 61 6e 75 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a January 11.**.**
1e7c4 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1e7c5 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1e7c6 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1e7c7 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1e7c8 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1e7c9 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1e7ca 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1e7cb 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1e7cc 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1e7cd 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1e7ce 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1e7cf 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1e7d0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1e7d1 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1e7d2 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1e7d3 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1e7d4 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1e7d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e7d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e7d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e7d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e7d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
1e7da 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1e7db 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 code used to imp
1e7dc 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74 lement the sqlit
1e7dd 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 e3_set_authorize
1e7de 72 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 69 r().** API. Thi
1e7df 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 61 6e s facility is an
1e7e0 20 6f 70 74 69 6f 6e 61 6c 20 66 65 61 74 75 72 optional featur
1e7e1 65 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 e of the library
1e7e2 2e 20 20 45 6d 62 65 64 64 65 64 0a 2a 2a 20 73 . Embedded.** s
1e7e3 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e ystems that do n
1e7e4 6f 74 20 6e 65 65 64 20 74 68 69 73 20 66 61 63 ot need this fac
1e7e5 69 6c 69 74 79 20 6d 61 79 20 6f 6d 69 74 20 69 ility may omit i
1e7e6 74 20 62 79 20 72 65 63 6f 6d 70 69 6c 69 6e 67 t by recompiling
1e7e7 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 .** the library
1e7e8 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 4f 4d with -DSQLITE_OM
1e7e9 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e IT_AUTHORIZATION
1e7ea 3d 31 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 75 =1.**.** $Id: au
1e7eb 74 68 2e 63 2c 76 20 31 2e 32 39 20 32 30 30 37 th.c,v 1.29 2007
1e7ec 2f 30 39 2f 31 38 20 31 35 3a 35 35 3a 30 37 20 /09/18 15:55:07
1e7ed 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
1e7ee 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 63 .** All of the c
1e7ef 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ode in this file
1e7f0 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 may be omitted
1e7f1 62 79 20 64 65 66 69 6e 69 6e 67 20 61 20 73 69 by defining a si
1e7f2 6e 67 6c 65 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a ngle.** macro..*
1e7f3 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1e7f4 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
1e7f5 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f ION../*.** Set o
1e7f6 72 20 63 6c 65 61 72 20 74 68 65 20 61 63 63 65 r clear the acce
1e7f7 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e ss authorization
1e7f8 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
1e7f9 20 54 68 65 20 61 63 63 65 73 73 20 61 75 74 68 The access auth
1e7fa 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 orization functi
1e7fb 6f 6e 20 69 73 20 62 65 20 63 61 6c 6c 65 64 20 on is be called
1e7fc 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 during the compi
1e7fd 6c 61 74 69 6f 6e 0a 2a 2a 20 70 68 61 73 65 20 lation.** phase
1e7fe 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 74 to verify that t
1e7ff 68 65 20 75 73 65 72 20 68 61 73 20 72 65 61 64 he user has read
1e800 20 61 6e 64 2f 6f 72 20 77 72 69 74 65 20 61 63 and/or write ac
1e801 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 cess permission
1e802 6f 6e 0a 2a 2a 20 76 61 72 69 6f 75 73 20 66 69 on.** various fi
1e803 65 6c 64 73 20 6f 66 20 74 68 65 20 64 61 74 61 elds of the data
1e804 62 61 73 65 2e 20 20 54 68 65 20 66 69 72 73 74 base. The first
1e805 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
1e806 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a auth function.*
1e807 2a 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 * is a copy of t
1e808 68 65 20 33 72 64 20 61 72 67 75 6d 65 6e 74 20 he 3rd argument
1e809 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e to this routine.
1e80a 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 The second arg
1e80b 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 ument.** to the
1e80c 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 auth function is
1e80d 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 63 6f one of these co
1e80e 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 nstants:.**.**
1e80f 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 SQLITE_CREA
1e810 54 45 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 TE_INDEX.**
1e811 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f SQLITE_CREATE_
1e812 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 TABLE.** S
1e813 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d QLITE_CREATE_TEM
1e814 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 P_INDEX.**
1e815 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
1e816 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 EMP_TABLE.**
1e817 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 SQLITE_CREATE
1e818 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a _TEMP_TRIGGER.**
1e819 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 SQLITE_CR
1e81a 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 0a 2a EATE_TEMP_VIEW.*
1e81b 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 * SQLITE_C
1e81c 52 45 41 54 45 5f 54 52 49 47 47 45 52 0a 2a 2a REATE_TRIGGER.**
1e81d 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 SQLITE_CR
1e81e 45 41 54 45 5f 56 49 45 57 0a 2a 2a 20 20 20 20 EATE_VIEW.**
1e81f 20 20 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 SQLITE_DELETE
1e820 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 .** SQLITE
1e821 5f 44 52 4f 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 _DROP_INDEX.**
1e822 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 SQLITE_DROP
1e823 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 _TABLE.**
1e824 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 SQLITE_DROP_TEMP
1e825 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 _INDEX.**
1e826 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 SQLITE_DROP_TEMP
1e827 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 _TABLE.**
1e828 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 SQLITE_DROP_TEMP
1e829 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 _TRIGGER.**
1e82a 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 SQLITE_DROP_TE
1e82b 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 MP_VIEW.**
1e82c 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 SQLITE_DROP_TRI
1e82d 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 GGER.** SQ
1e82e 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 0a 2a LITE_DROP_VIEW.*
1e82f 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 * SQLITE_I
1e830 4e 53 45 52 54 0a 2a 2a 20 20 20 20 20 20 20 53 NSERT.** S
1e831 51 4c 49 54 45 5f 50 52 41 47 4d 41 0a 2a 2a 20 QLITE_PRAGMA.**
1e832 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 45 41 SQLITE_REA
1e833 44 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 D.** SQLIT
1e834 45 5f 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 E_SELECT.**
1e835 20 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 SQLITE_TRANSAC
1e836 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 20 53 51 TION.** SQ
1e837 4c 49 54 45 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a LITE_UPDATE.**.*
1e838 2a 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 * The third and
1e839 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73 fourth arguments
1e83a 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e to the auth fun
1e83b 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 6e 61 ction are the na
1e83c 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 me of.** the tab
1e83d 6c 65 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d le and the colum
1e83e 6e 20 74 68 61 74 20 61 72 65 20 62 65 69 6e 67 n that are being
1e83f 20 61 63 63 65 73 73 65 64 2e 20 20 54 68 65 20 accessed. The
1e840 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a auth function.**
1e841 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 should return e
1e842 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 2c ither SQLITE_OK,
1e843 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c 20 6f 72 SQLITE_DENY, or
1e844 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2e 20 SQLITE_IGNORE.
1e845 20 49 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b If.** SQLITE_OK
1e846 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 is returned, it
1e847 20 6d 65 61 6e 73 20 74 68 61 74 20 61 63 63 65 means that acce
1e848 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 2e 20 20 ss is allowed.
1e849 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d SQLITE_DENY.** m
1e84a 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51 eans that the SQ
1e84b 4c 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c L statement will
1e84c 20 6e 65 76 65 72 2d 72 75 6e 20 2d 20 74 68 65 never-run - the
1e84d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
1e84e 63 61 6c 6c 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 call.** will ret
1e84f 75 72 6e 20 77 69 74 68 20 61 6e 20 65 72 72 6f urn with an erro
1e850 72 2e 20 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 r. SQLITE_IGNOR
1e851 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 E means that the
1e852 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a SQL statement.*
1e853 2a 20 73 68 6f 75 6c 64 20 72 75 6e 20 62 75 74 * should run but
1e854 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61 attempts to rea
1e855 64 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 d the specified
1e856 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 72 65 74 75 column will retu
1e857 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61 rn NULL.** and a
1e858 74 74 65 6d 70 74 73 20 74 6f 20 77 72 69 74 65 ttempts to write
1e859 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c the column will
1e85a 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a be ignored..**.
1e85b 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 65 20 61 ** Setting the a
1e85c 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 uth function to
1e85d 4e 55 4c 4c 20 64 69 73 61 62 6c 65 73 20 74 68 NULL disables th
1e85e 69 73 20 68 6f 6f 6b 2e 20 20 54 68 65 20 64 65 is hook. The de
1e85f 66 61 75 6c 74 0a 2a 2a 20 73 65 74 74 69 6e 67 fault.** setting
1e860 20 6f 66 20 74 68 65 20 61 75 74 68 20 66 75 6e of the auth fun
1e861 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a ction is NULL..*
1e862 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1e863 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 sqlite3_set_aut
1e864 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 horizer(. sqlit
1e865 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a e3 *db,. int (*
1e866 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 xAuth)(void*,int
1e867 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
1e868 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
1e869 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
1e86a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a ),. void *pArg.
1e86b 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ){. sqlite3_mut
1e86c 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 ex_enter(db->mut
1e86d 65 78 29 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68 ex);. db->xAuth
1e86e 20 3d 20 78 41 75 74 68 3b 0a 20 20 64 62 2d 3e = xAuth;. db->
1e86f 70 41 75 74 68 41 72 67 20 3d 20 70 41 72 67 3b pAuthArg = pArg;
1e870 0a 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 . sqlite3Expire
1e871 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e PreparedStatemen
1e872 74 73 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 ts(db);. sqlite
1e873 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 3_mutex_leave(db
1e874 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 ->mutex);. retu
1e875 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1e876 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 ./*.** Write an
1e877 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
1e878 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d to pParse->zErrM
1e879 73 67 20 74 68 61 74 20 65 78 70 6c 61 69 6e 73 sg that explains
1e87a 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 73 65 that the.** use
1e87b 72 2d 73 75 70 70 6c 69 65 64 20 61 75 74 68 6f r-supplied autho
1e87c 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f rization functio
1e87d 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6c n returned an il
1e87e 6c 65 67 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a legal value..*/.
1e87f 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
1e880 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43 teAuthBadReturnC
1e881 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 ode(Parse *pPars
1e882 65 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 73 71 e, int rc){. sq
1e883 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1e884 61 72 73 65 2c 20 22 69 6c 6c 65 67 61 6c 20 72 arse, "illegal r
1e885 65 74 75 72 6e 20 76 61 6c 75 65 20 28 25 64 29 eturn value (%d)
1e886 20 66 72 6f 6d 20 74 68 65 20 22 0a 20 20 20 20 from the ".
1e887 22 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 "authorization f
1e888 75 6e 63 74 69 6f 6e 20 2d 20 73 68 6f 75 6c 64 unction - should
1e889 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 be SQLITE_OK, S
1e88a 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 22 0a QLITE_IGNORE, ".
1e88b 20 20 20 20 22 6f 72 20 53 51 4c 49 54 45 5f 44 "or SQLITE_D
1e88c 45 4e 59 22 2c 20 72 63 29 3b 0a 20 20 70 50 61 ENY", rc);. pPa
1e88d 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 rse->rc = SQLITE
1e88e 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _ERROR;.}../*.**
1e88f 20 54 68 65 20 70 45 78 70 72 20 73 68 6f 75 6c The pExpr shoul
1e890 64 20 62 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e d be a TK_COLUMN
1e891 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 expression. Th
1e892 65 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 e table referred
1e893 20 74 6f 0a 2a 2a 20 69 73 20 69 6e 20 70 54 61 to.** is in pTa
1e894 62 4c 69 73 74 20 6f 72 20 65 6c 73 65 20 69 74 bList or else it
1e895 20 69 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f is the NEW or O
1e896 4c 44 20 74 61 62 6c 65 20 6f 66 20 61 20 74 72 LD table of a tr
1e897 69 67 67 65 72 2e 20 20 0a 2a 2a 20 43 68 65 63 igger. .** Chec
1e898 6b 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 69 k to see if it i
1e899 73 20 4f 4b 20 74 6f 20 72 65 61 64 20 74 68 69 s OK to read thi
1e89a 73 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c s particular col
1e89b 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 umn..**.** If th
1e89c 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 e auth function
1e89d 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 returns SQLITE_I
1e89e 47 4e 4f 52 45 2c 20 63 68 61 6e 67 65 20 74 68 GNORE, change th
1e89f 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 e TK_COLUMN .**
1e8a0 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 74 6f instruction into
1e8a1 20 61 20 54 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 a TK_NULL. If
1e8a2 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f the auth functio
1e8a3 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 n returns SQLITE
1e8a4 5f 44 45 4e 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 _DENY,.** then g
1e8a5 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 enerate an error
1e8a6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1e8a7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1e8a8 41 75 74 68 52 65 61 64 28 0a 20 20 50 61 72 73 AuthRead(. Pars
1e8a9 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
1e8aa 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 /* The parser
1e8ab 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
1e8ac 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 r *pExpr,
1e8ad 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 /* The expres
1e8ae 73 69 6f 6e 20 74 6f 20 63 68 65 63 6b 20 61 75 sion to check au
1e8af 74 68 6f 72 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a thorization on *
1e8b0 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 /. Schema *pSch
1e8b1 65 6d 61 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 ema, /* The
1e8b2 20 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 schema of the e
1e8b3 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 xpression */. S
1e8b4 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 rcList *pTabList
1e8b5 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c /* All tabl
1e8b6 65 20 74 68 61 74 20 70 45 78 70 72 20 6d 69 67 e that pExpr mig
1e8b7 68 74 20 72 65 66 65 72 20 74 6f 20 2a 2f 0a 29 ht refer to */.)
1e8b8 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
1e8b9 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
1e8ba 69 6e 74 20 72 63 3b 0a 20 20 54 61 62 6c 65 20 int rc;. Table
1e8bb 2a 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 *pTab = 0;
1e8bc 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 /* The table bei
1e8bd 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e ng read */. con
1e8be 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 st char *zCol;
1e8bf 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
1e8c0 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 e column of the
1e8c1 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 table */. int i
1e8c2 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Src;
1e8c3 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 54 61 /* Index in pTa
1e8c4 62 4c 69 73 74 2d 3e 61 5b 5d 20 6f 66 20 74 61 bList->a[] of ta
1e8c5 62 6c 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a ble being read *
1e8c6 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1e8c7 7a 44 42 61 73 65 3b 20 20 20 2f 2a 20 4e 61 6d zDBase; /* Nam
1e8c8 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 62 65 e of database be
1e8c9 69 6e 67 20 61 63 63 65 73 73 65 64 20 2a 2f 0a ing accessed */.
1e8ca 20 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a TriggerStack *
1e8cb 70 53 74 61 63 6b 3b 20 2f 2a 20 54 68 65 20 73 pStack; /* The s
1e8cc 74 61 63 6b 20 6f 66 20 63 75 72 72 65 6e 74 20 tack of current
1e8cd 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e triggers */. in
1e8ce 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 t iDb;
1e8cf 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 /* The index
1e8d0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1e8d1 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1e8d2 72 65 66 65 72 73 20 74 6f 20 2a 2f 0a 0a 20 20 refers to */..
1e8d3 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30 if( db->xAuth==0
1e8d4 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
1e8d5 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 pExpr->op!=TK_C
1e8d6 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 3b 0a OLUMN ) return;.
1e8d7 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
1e8d8 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 chemaToIndex(pPa
1e8d9 72 73 65 2d 3e 64 62 2c 20 70 53 63 68 65 6d 61 rse->db, pSchema
1e8da 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 );. if( iDb<0 )
1e8db 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 61 74 74 65 {. /* An atte
1e8dc 6d 70 74 20 74 6f 20 72 65 61 64 20 61 20 63 6f mpt to read a co
1e8dd 6c 75 6d 6e 20 6f 75 74 20 6f 66 20 61 20 73 75 lumn out of a su
1e8de 62 71 75 65 72 79 20 6f 72 20 6f 74 68 65 72 0a bquery or other.
1e8df 20 20 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 ** temporary
1e8e0 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 table. */. r
1e8e1 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 eturn;. }. for
1e8e2 28 69 53 72 63 3d 30 3b 20 70 54 61 62 4c 69 73 (iSrc=0; pTabLis
1e8e3 74 20 26 26 20 69 53 72 63 3c 70 54 61 62 4c 69 t && iSrc<pTabLi
1e8e4 73 74 2d 3e 6e 53 72 63 3b 20 69 53 72 63 2b 2b st->nSrc; iSrc++
1e8e5 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 ){. if( pExpr
1e8e6 2d 3e 69 54 61 62 6c 65 3d 3d 70 54 61 62 4c 69 ->iTable==pTabLi
1e8e7 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 69 43 75 72 st->a[iSrc].iCur
1e8e8 73 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d sor ) break;. }
1e8e9 0a 20 20 69 66 28 20 69 53 72 63 3e 3d 30 20 26 . if( iSrc>=0 &
1e8ea 26 20 70 54 61 62 4c 69 73 74 20 26 26 20 69 53 & pTabList && iS
1e8eb 72 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 rc<pTabList->nSr
1e8ec 63 20 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 c ){. pTab =
1e8ed 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 pTabList->a[iSrc
1e8ee 5d 2e 70 54 61 62 3b 0a 20 20 7d 65 6c 73 65 20 ].pTab;. }else
1e8ef 69 66 28 20 28 70 53 74 61 63 6b 20 3d 20 70 50 if( (pStack = pP
1e8f0 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 29 arse->trigStack)
1e8f1 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 !=0 ){. /* Th
1e8f2 69 73 20 6d 75 73 74 20 62 65 20 61 6e 20 61 74 is must be an at
1e8f3 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 tempt to read th
1e8f4 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 e NEW or OLD pse
1e8f5 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 20 20 2a udo-tables. *
1e8f6 2a 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 0a * of a trigger..
1e8f7 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
1e8f8 74 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 t( pExpr->iTable
1e8f9 3d 3d 70 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 ==pStack->newIdx
1e8fa 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c || pExpr->iTabl
1e8fb 65 3d 3d 70 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 e==pStack->oldId
1e8fc 78 20 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 x );. pTab =
1e8fd 70 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 pStack->pTab;.
1e8fe 7d 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 }. if( pTab==0
1e8ff 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
1e900 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d pExpr->iColumn>=
1e901 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
1e902 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c pExpr->iColumn<
1e903 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 pTab->nCol );.
1e904 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 zCol = pTab->a
1e905 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 Col[pExpr->iColu
1e906 6d 6e 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c mn].zName;. }el
1e907 73 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b se if( pTab->iPK
1e908 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 ey>=0 ){. ass
1e909 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 ert( pTab->iPKey
1e90a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 <pTab->nCol );.
1e90b 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e zCol = pTab->
1e90c 61 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 aCol[pTab->iPKey
1e90d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 ].zName;. }else
1e90e 7b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f {. zCol = "RO
1e90f 57 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65 WID";. }. asse
1e910 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 rt( iDb>=0 && iD
1e911 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a b<db->nDb );. z
1e912 44 42 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b DBase = db->aDb[
1e913 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 iDb].zName;. rc
1e914 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d = db->xAuth(db-
1e915 3e 70 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54 >pAuthArg, SQLIT
1e916 45 5f 52 45 41 44 2c 20 70 54 61 62 2d 3e 7a 4e E_READ, pTab->zN
1e917 61 6d 65 2c 20 7a 43 6f 6c 2c 20 7a 44 42 61 73 ame, zCol, zDBas
1e918 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 e, .
1e919 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 pParse->zAu
1e91a 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 thContext);. if
1e91b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e ( rc==SQLITE_IGN
1e91c 4f 52 45 20 29 7b 0a 20 20 20 20 70 45 78 70 72 ORE ){. pExpr
1e91d 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a ->op = TK_NULL;.
1e91e 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d }else if( rc==
1e91f 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 SQLITE_DENY ){.
1e920 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32 if( db->nDb>2
1e921 20 7c 7c 20 69 44 62 21 3d 30 20 29 7b 0a 20 20 || iDb!=0 ){.
1e922 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1e923 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63 Msg(pParse, "acc
1e924 65 73 73 20 74 6f 20 25 73 2e 25 73 2e 25 73 20 ess to %s.%s.%s
1e925 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20 is prohibited",
1e926 0a 20 20 20 20 20 20 20 20 20 7a 44 42 61 73 65 . zDBase
1e927 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a , pTab->zName, z
1e928 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Col);. }else{
1e929 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
1e92a 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1e92b 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20 access to %s.%s
1e92c 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 70 is prohibited",p
1e92d 54 61 62 2d 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29 Tab->zName,zCol)
1e92e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 ;. }. pPar
1e92f 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
1e930 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 AUTH;. }else if
1e931 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1e932 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 ){. sqliteAut
1e933 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 hBadReturnCode(p
1e934 50 61 72 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a Parse, rc);. }.
1e935 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 }../*.** Do an a
1e936 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 uthorization che
1e937 63 6b 20 75 73 69 6e 67 20 74 68 65 20 63 6f 64 ck using the cod
1e938 65 20 61 6e 64 20 61 72 67 75 6d 65 6e 74 73 20 e and arguments
1e939 67 69 76 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a given. Return.*
1e93a 2a 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f * either SQLITE_
1e93b 4f 4b 20 28 7a 65 72 6f 29 20 6f 72 20 53 51 4c OK (zero) or SQL
1e93c 49 54 45 5f 49 47 4e 4f 52 45 20 6f 72 20 53 51 ITE_IGNORE or SQ
1e93d 4c 49 54 45 5f 44 45 4e 59 2e 20 20 49 66 20 53 LITE_DENY. If S
1e93e 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 QLITE_DENY.** is
1e93f 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 returned, then
1e940 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 the error count
1e941 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 and error messag
1e942 65 20 69 6e 20 70 50 61 72 73 65 20 61 72 65 0a e in pParse are.
1e943 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 70 70 72 ** modified appr
1e944 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 opriately..*/.SQ
1e945 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1e946 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
1e947 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 k(. Parse *pPar
1e948 73 65 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c 0a se,. int code,.
1e949 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 const char *zA
1e94a 72 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 rg1,. const cha
1e94b 72 20 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73 r *zArg2,. cons
1e94c 74 20 63 68 61 72 20 2a 7a 41 72 67 33 0a 29 7b t char *zArg3.){
1e94d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1e94e 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 pParse->db;. i
1e94f 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e nt rc;.. /* Don
1e950 27 74 20 64 6f 20 61 6e 79 20 61 75 74 68 6f 72 't do any author
1e951 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 ization checks i
1e952 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
1e953 73 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a 20 s initialising.
1e954 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 ** or if the pa
1e955 72 73 65 72 20 69 73 20 62 65 69 6e 67 20 69 6e rser is being in
1e956 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 voked from withi
1e957 6e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 n sqlite3_declar
1e958 65 5f 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 e_vtab.. */. i
1e959 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 f( db->init.busy
1e95a 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 || IN_DECLARE_V
1e95b 54 41 42 20 29 7b 0a 20 20 20 20 72 65 74 75 72 TAB ){. retur
1e95c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
1e95d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 .. if( db->xAut
1e95e 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 h==0 ){. retu
1e95f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1e960 7d 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 }. rc = db->xAu
1e961 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c th(db->pAuthArg,
1e962 20 63 6f 64 65 2c 20 7a 41 72 67 31 2c 20 7a 41 code, zArg1, zA
1e963 72 67 32 2c 20 7a 41 72 67 33 2c 20 70 50 61 72 rg2, zArg3, pPar
1e964 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 se->zAuthContext
1e965 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1e966 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 ITE_DENY ){.
1e967 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1e968 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 pParse, "not aut
1e969 68 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 70 horized");. p
1e96a 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 Parse->rc = SQLI
1e96b 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 TE_AUTH;. }else
1e96c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1e96d 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 OK && rc!=SQLITE
1e96e 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 72 _IGNORE ){. r
1e96f 63 20 3d 20 53 51 4c 49 54 45 5f 44 45 4e 59 3b c = SQLITE_DENY;
1e970 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 . sqliteAuthB
1e971 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 adReturnCode(pPa
1e972 72 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 rse, rc);. }.
1e973 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1e974 0a 2a 2a 20 50 75 73 68 20 61 6e 20 61 75 74 68 .** Push an auth
1e975 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 orization contex
1e976 74 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72 t. After this r
1e977 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
1e978 2c 20 74 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61 , the.** zArg3 a
1e979 72 67 75 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f rgument to autho
1e97a 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 rization callbac
1e97b 6b 73 20 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 ks will be zCont
1e97c 65 78 74 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 ext until.** pop
1e97d 70 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 ped. Or if pPar
1e97e 73 65 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 se==0, this rout
1e97f 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
1e980 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1e981 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 E void sqlite3Au
1e982 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 0a 20 thContextPush(.
1e983 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a Parse *pParse,.
1e984 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 AuthContext *p
1e985 43 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 Context, . cons
1e986 74 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 t char *zContext
1e987 0a 29 7b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e .){. pContext->
1e988 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b pParse = pParse;
1e989 0a 20 20 69 66 28 20 70 50 61 72 73 65 20 29 7b . if( pParse ){
1e98a 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 7a . pContext->z
1e98b 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 AuthContext = pP
1e98c 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 arse->zAuthConte
1e98d 78 74 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e xt;. pParse->
1e98e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a zAuthContext = z
1e98f 43 6f 6e 74 65 78 74 3b 0a 20 20 7d 0a 7d 0a 0a Context;. }.}..
1e990 2f 2a 0a 2a 2a 20 50 6f 70 20 61 6e 20 61 75 74 /*.** Pop an aut
1e991 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 horization conte
1e992 78 74 20 74 68 61 74 20 77 61 73 20 70 72 65 76 xt that was prev
1e993 69 6f 75 73 6c 79 20 70 75 73 68 65 64 0a 2a 2a iously pushed.**
1e994 20 62 79 20 73 71 6c 69 74 65 33 41 75 74 68 43 by sqlite3AuthC
1e995 6f 6e 74 65 78 74 50 75 73 68 0a 2a 2f 0a 53 51 ontextPush.*/.SQ
1e996 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1e997 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e d sqlite3AuthCon
1e998 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 textPop(AuthCont
1e999 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 29 7b 0a ext *pContext){.
1e99a 20 20 69 66 28 20 70 43 6f 6e 74 65 78 74 2d 3e if( pContext->
1e99b 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 pParse ){. pC
1e99c 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 2d 3e ontext->pParse->
1e99d 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 zAuthContext = p
1e99e 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f Context->zAuthCo
1e99f 6e 74 65 78 74 3b 0a 20 20 20 20 70 43 6f 6e 74 ntext;. pCont
1e9a0 65 78 74 2d 3e 70 50 61 72 73 65 20 3d 20 30 3b ext->pParse = 0;
1e9a1 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f . }.}..#endif /
1e9a2 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 * SQLITE_OMIT_AU
1e9a3 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a THORIZATION */..
1e9a4 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1e9a5 45 6e 64 20 6f 66 20 61 75 74 68 2e 63 20 2a 2a End of auth.c **
1e9a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e9a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e9a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1e9a9 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1e9aa 42 65 67 69 6e 20 66 69 6c 65 20 62 75 69 6c 64 Begin file build
1e9ab 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1e9ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e9ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1e9ae 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
1e9af 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
1e9b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1e9b1 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1e9b2 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1e9b3 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1e9b4 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1e9b5 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1e9b6 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1e9b7 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1e9b8 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1e9b9 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1e9ba 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1e9bb 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1e9bc 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1e9bd 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1e9be 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1e9bf 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1e9c0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1e9c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e9c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e9c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e9c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1e9c5 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
1e9c6 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 ile contains C c
1e9c7 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 ode routines tha
1e9c8 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 t are called by
1e9c9 74 68 65 20 53 51 4c 69 74 65 20 70 61 72 73 65 the SQLite parse
1e9ca 72 0a 2a 2a 20 77 68 65 6e 20 73 79 6e 74 61 78 r.** when syntax
1e9cb 20 72 75 6c 65 73 20 61 72 65 20 72 65 64 75 63 rules are reduc
1e9cc 65 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 ed. The routine
1e9cd 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 68 s in this file h
1e9ce 61 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 66 6f 6c andle the.** fol
1e9cf 6c 6f 77 69 6e 67 20 6b 69 6e 64 73 20 6f 66 20 lowing kinds of
1e9d0 53 51 4c 20 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a SQL syntax:.**.*
1e9d1 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 * CREATE TAB
1e9d2 4c 45 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 54 LE.** DROP T
1e9d3 41 42 4c 45 0a 2a 2a 20 20 20 20 20 43 52 45 41 ABLE.** CREA
1e9d4 54 45 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 TE INDEX.**
1e9d5 44 52 4f 50 20 49 4e 44 45 58 0a 2a 2a 20 20 20 DROP INDEX.**
1e9d6 20 20 63 72 65 61 74 69 6e 67 20 49 44 20 6c 69 creating ID li
1e9d7 73 74 73 0a 2a 2a 20 20 20 20 20 42 45 47 49 4e sts.** BEGIN
1e9d8 20 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 TRANSACTION.**
1e9d9 20 20 20 20 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20 COMMIT.**
1e9da 20 20 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 0a 2a 2a ROLLBACK.**.**
1e9db 20 24 49 64 3a 20 62 75 69 6c 64 2e 63 2c 76 20 $Id: build.c,v
1e9dc 31 2e 35 30 33 20 32 30 30 38 2f 31 31 2f 31 37 1.503 2008/11/17
1e9dd 20 31 39 3a 31 38 3a 35 35 20 64 61 6e 69 65 6c 19:18:55 daniel
1e9de 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a k1977 Exp $.*/..
1e9df 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
1e9e0 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 ne is called whe
1e9e1 6e 20 61 20 6e 65 77 20 53 51 4c 20 73 74 61 74 n a new SQL stat
1e9e2 65 6d 65 6e 74 20 69 73 20 62 65 67 69 6e 6e 69 ement is beginni
1e9e3 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 70 61 72 73 ng to.** be pars
1e9e4 65 64 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 ed. Initialize
1e9e5 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63 the pParse struc
1e9e6 74 75 72 65 20 61 73 20 6e 65 65 64 65 64 2e 0a ture as needed..
1e9e7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1e9e8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 E void sqlite3Be
1e9e9 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 20 2a ginParse(Parse *
1e9ea 70 50 61 72 73 65 2c 20 69 6e 74 20 65 78 70 6c pParse, int expl
1e9eb 61 69 6e 46 6c 61 67 29 7b 0a 20 20 70 50 61 72 ainFlag){. pPar
1e9ec 73 65 2d 3e 65 78 70 6c 61 69 6e 20 3d 20 65 78 se->explain = ex
1e9ed 70 6c 61 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61 plainFlag;. pPa
1e9ee 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d rse->nVar = 0;.}
1e9ef 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1e9f0 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
1e9f1 48 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 HE./*.** The Tab
1e9f2 6c 65 4c 6f 63 6b 20 73 74 72 75 63 74 75 72 65 leLock structure
1e9f3 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 is only used by
1e9f4 20 74 68 65 20 73 71 6c 69 74 65 33 54 61 62 6c the sqlite3Tabl
1e9f5 65 4c 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 eLock() and.** c
1e9f6 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 odeTableLocks()
1e9f7 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 functions..*/.st
1e9f8 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b ruct TableLock {
1e9f9 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 . int iDb;
1e9fa 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
1e9fb 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 atabase containi
1e9fc 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 ng the table to
1e9fd 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 be locked */. i
1e9fe 6e 74 20 69 54 61 62 3b 20 20 20 20 20 20 20 20 nt iTab;
1e9ff 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 /* The root
1ea00 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c page of the tabl
1ea01 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a e to be locked *
1ea02 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f /. u8 isWriteLo
1ea03 63 6b 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ck; /* True
1ea04 20 66 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e for write lock.
1ea05 20 20 46 61 6c 73 65 20 66 6f 72 20 61 20 72 65 False for a re
1ea06 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e ad lock */. con
1ea07 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 st char *zName;
1ea08 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
1ea09 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a table */.};../*
1ea0a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66 .** Record the f
1ea0b 61 63 74 20 74 68 61 74 20 77 65 20 77 61 6e 74 act that we want
1ea0c 20 74 6f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65 to lock a table
1ea0d 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a at run-time. .
1ea0e 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 **.** The table
1ea0f 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 68 61 73 to be locked has
1ea10 20 72 6f 6f 74 20 70 61 67 65 20 69 54 61 62 20 root page iTab
1ea11 61 6e 64 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 and is found in
1ea12 64 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a database iDb..**
1ea13 20 41 20 72 65 61 64 20 6f 72 20 61 20 77 72 69 A read or a wri
1ea14 74 65 20 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74 te lock can be t
1ea15 61 6b 65 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f aken depending o
1ea16 6e 20 69 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a n isWritelock..*
1ea17 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1ea18 65 20 6a 75 73 74 20 72 65 63 6f 72 64 73 20 74 e just records t
1ea19 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 he fact that the
1ea1a 20 6c 6f 63 6b 20 69 73 20 64 65 73 69 72 65 64 lock is desired
1ea1b 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74 . The.** code t
1ea1c 6f 20 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20 o make the lock
1ea1d 6f 63 63 75 72 20 69 73 20 67 65 6e 65 72 61 74 occur is generat
1ea1e 65 64 20 62 79 20 61 20 6c 61 74 65 72 20 63 61 ed by a later ca
1ea1f 6c 6c 20 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 ll to.** codeTab
1ea20 6c 65 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20 leLocks() which
1ea21 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 73 71 occurs during sq
1ea22 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e lite3FinishCodin
1ea23 67 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 g()..*/.SQLITE_P
1ea24 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1ea25 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 te3TableLock(.
1ea26 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1ea27 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
1ea28 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ntext */. int i
1ea29 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a Db, /*
1ea2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 Index of the da
1ea2b 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e tabase containin
1ea2c 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c g the table to l
1ea2d 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 ock */. int iTa
1ea2e 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 b, /* R
1ea2f 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 oot page number
1ea30 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 of the table to
1ea31 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 be locked */. u
1ea32 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20 8 isWriteLock,
1ea33 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
1ea34 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 write lock */.
1ea35 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
1ea36 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 e /* Name of th
1ea37 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f e table to be lo
1ea38 63 6b 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 cked */.){. int
1ea39 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 i;. int nBytes
1ea3a 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 ;. TableLock *p
1ea3b 3b 0a 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 ;.. if( iDb<0 )
1ea3c 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
1ea3d 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }.. for(i=0; i<
1ea3e 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f pParse->nTableLo
1ea3f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 ck; i++){. p
1ea40 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c = &pParse->aTabl
1ea41 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 eLock[i];. if
1ea42 28 20 70 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26 ( p->iDb==iDb &&
1ea43 20 70 2d 3e 69 54 61 62 3d 3d 69 54 61 62 20 29 p->iTab==iTab )
1ea44 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 57 72 69 {. p->isWri
1ea45 74 65 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57 teLock = (p->isW
1ea46 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 riteLock || isWr
1ea47 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 iteLock);.
1ea48 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
1ea49 7d 0a 0a 20 20 6e 42 79 74 65 73 20 3d 20 73 69 }.. nBytes = si
1ea4a 7a 65 6f 66 28 54 61 62 6c 65 4c 6f 63 6b 29 20 zeof(TableLock)
1ea4b 2a 20 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c * (pParse->nTabl
1ea4c 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20 70 50 61 72 eLock+1);. pPar
1ea4d 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d se->aTableLock =
1ea4e 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 . sqlite3D
1ea4f 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 bReallocOrFree(p
1ea50 50 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 Parse->db, pPars
1ea51 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e e->aTableLock, n
1ea52 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 50 Bytes);. if( pP
1ea53 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b arse->aTableLock
1ea54 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61 ){. p = &pPa
1ea55 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b rse->aTableLock[
1ea56 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f pParse->nTableLo
1ea57 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44 ck++];. p->iD
1ea58 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e b = iDb;. p->
1ea59 69 54 61 62 20 3d 20 69 54 61 62 3b 0a 20 20 20 iTab = iTab;.
1ea5a 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 p->isWriteLock
1ea5b 3d 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 = isWriteLock;.
1ea5c 20 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e p->zName = zN
1ea5d 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ame;. }else{.
1ea5e 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 pParse->nTable
1ea5f 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 Lock = 0;. pP
1ea60 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 arse->db->malloc
1ea61 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a Failed = 1;. }.
1ea62 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e }../*.** Code an
1ea63 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e OP_TableLock in
1ea64 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 65 61 struction for ea
1ea65 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20 ch table locked
1ea66 62 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d by the.** statem
1ea67 65 6e 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20 ent (configured
1ea68 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 by calls to sqli
1ea69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e te3TableLock()).
1ea6a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1ea6b 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50 codeTableLocks(P
1ea6c 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 arse *pParse){.
1ea6d 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a int i;. Vdbe *
1ea6e 70 56 64 62 65 3b 20 0a 0a 20 20 69 66 28 20 30 pVdbe; .. if( 0
1ea6f 3d 3d 28 70 56 64 62 65 20 3d 20 73 71 6c 69 74 ==(pVdbe = sqlit
1ea70 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
1ea71 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e )) ){. return
1ea72 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 ;. }.. for(i=0
1ea73 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 ; i<pParse->nTab
1ea74 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 leLock; i++){.
1ea75 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d TableLock *p =
1ea76 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 &pParse->aTable
1ea77 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 Lock[i];. int
1ea78 20 70 31 20 3d 20 70 2d 3e 69 44 62 3b 0a 20 20 p1 = p->iDb;.
1ea79 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1ea7a 4f 70 34 28 70 56 64 62 65 2c 20 4f 50 5f 54 61 Op4(pVdbe, OP_Ta
1ea7b 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e bleLock, p1, p->
1ea7c 69 54 61 62 2c 20 70 2d 3e 69 73 57 72 69 74 65 iTab, p->isWrite
1ea7d 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 Lock,.
1ea7e 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 7a p->z
1ea7f 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 Name, P4_STATIC)
1ea80 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 ;. }.}.#else.
1ea81 23 64 65 66 69 6e 65 20 63 6f 64 65 54 61 62 6c #define codeTabl
1ea82 65 4c 6f 63 6b 73 28 78 29 0a 23 65 6e 64 69 66 eLocks(x).#endif
1ea83 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
1ea84 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 61 tine is called a
1ea85 66 74 65 72 20 61 20 73 69 6e 67 6c 65 20 53 51 fter a single SQ
1ea86 4c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 L statement has
1ea87 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61 been.** parsed a
1ea88 6e 64 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 nd a VDBE progra
1ea89 6d 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 61 m to execute tha
1ea8a 74 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 t statement has
1ea8b 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64 been.** prepared
1ea8c 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
1ea8d 70 75 74 73 20 74 68 65 20 66 69 6e 69 73 68 69 puts the finishi
1ea8e 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e 20 74 68 ng touches on th
1ea8f 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f 67 72 61 e.** VDBE progra
1ea90 6d 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65 m and resets the
1ea91 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 pParse structur
1ea92 65 20 66 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a e for the next.*
1ea93 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e * parse..**.** N
1ea94 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 20 65 ote that if an e
1ea95 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 69 rror occurred, i
1ea96 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 t might be the c
1ea97 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56 ase that.** no V
1ea98 44 42 45 20 63 6f 64 65 20 77 61 73 20 67 65 6e DBE code was gen
1ea99 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 erated..*/.SQLIT
1ea9a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1ea9b 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 qlite3FinishCodi
1ea9c 6e 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ng(Parse *pParse
1ea9d 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
1ea9e 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 ;. Vdbe *v;..
1ea9f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
1eaa0 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
1eaa1 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e cFailed ) return
1eaa2 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
1eaa3 6e 65 73 74 65 64 20 29 20 72 65 74 75 72 6e 3b nested ) return;
1eaa4 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e . if( pParse->n
1eaa5 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 Err ) return;..
1eaa6 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 67 65 6e /* Begin by gen
1eaa7 65 72 61 74 69 6e 67 20 73 6f 6d 65 20 74 65 72 erating some ter
1eaa8 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 20 61 74 mination code at
1eaa9 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a the end of the.
1eaaa 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 72 61 ** vdbe progra
1eaab 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c m. */. v = sql
1eaac 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 ite3GetVdbe(pPar
1eaad 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a se);. if( v ){.
1eaae 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1eaaf 64 64 4f 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74 ddOp0(v, OP_Halt
1eab0 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 );.. /* The c
1eab1 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61 ookie mask conta
1eab2 69 6e 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20 ins one bit for
1eab3 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69 each database fi
1eab4 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 le open.. **
1eab5 28 42 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61 (Bit 0 is for ma
1eab6 69 6e 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72 in, bit 1 is for
1eab7 20 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f temp, and so fo
1eab8 72 74 68 2e 29 20 20 42 69 74 73 20 61 72 65 0a rth.) Bits are.
1eab9 20 20 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65 ** set for e
1eaba 61 63 68 20 64 61 74 61 62 61 73 65 20 74 68 61 ach database tha
1eabb 74 20 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65 t is used. Gene
1eabc 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61 rate code to sta
1eabd 72 74 20 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e rt a. ** tran
1eabe 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20 saction on each
1eabf 75 73 65 64 20 64 61 74 61 62 61 73 65 20 61 6e used database an
1eac0 64 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 20 d to verify the
1eac1 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 schema cookie.
1eac2 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73 65 ** on each use
1eac3 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 d database..
1eac4 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 */. if( pPars
1eac5 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20 e->cookieGoto>0
1eac6 29 7b 0a 20 20 20 20 20 20 75 33 32 20 6d 61 73 ){. u32 mas
1eac7 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 k;. int iDb
1eac8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1eac9 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 dbeJumpHere(v, p
1eaca 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 Parse->cookieGot
1eacb 6f 2d 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 o-1);. for(
1eacc 69 44 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 iDb=0, mask=1; i
1eacd 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b Db<db->nDb; mask
1eace 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20 <<=1, iDb++){.
1eacf 20 20 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20 if( (mask
1ead0 26 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 & pParse->cookie
1ead1 4d 61 73 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 Mask)==0 ) conti
1ead2 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c nue;. sql
1ead3 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 ite3VdbeUsesBtre
1ead4 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 e(v, iDb);.
1ead5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1ead6 64 4f 70 32 28 76 2c 4f 50 5f 54 72 61 6e 73 61 dOp2(v,OP_Transa
1ead7 63 74 69 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73 ction, iDb, (mas
1ead8 6b 20 26 20 70 50 61 72 73 65 2d 3e 77 72 69 74 k & pParse->writ
1ead9 65 4d 61 73 6b 29 21 3d 30 29 3b 0a 20 20 20 20 eMask)!=0);.
1eada 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1eadb 64 64 4f 70 32 28 76 2c 4f 50 5f 56 65 72 69 66 ddOp2(v,OP_Verif
1eadc 79 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 70 50 yCookie, iDb, pP
1eadd 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 arse->cookieValu
1eade 65 5b 69 44 62 5d 29 3b 0a 20 20 20 20 20 20 7d e[iDb]);. }
1eadf 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1eae0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1eae1 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 E. {.
1eae2 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 int i;.
1eae3 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 for(i=0; i<pPar
1eae4 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 se->nVtabLock; i
1eae5 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 ++){. c
1eae6 68 61 72 20 2a 76 74 61 62 20 3d 20 28 63 68 61 har *vtab = (cha
1eae7 72 20 2a 29 70 50 61 72 73 65 2d 3e 61 70 56 74 r *)pParse->apVt
1eae8 61 62 4c 6f 63 6b 5b 69 5d 2d 3e 70 56 74 61 62 abLock[i]->pVtab
1eae9 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
1eaea 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
1eaeb 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c 20 30 OP_VBegin, 0, 0
1eaec 2c 20 30 2c 20 76 74 61 62 2c 20 50 34 5f 56 54 , 0, vtab, P4_VT
1eaed 41 42 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 AB);. }.
1eaee 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e pParse->n
1eaef 56 74 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 VtabLock = 0;.
1eaf0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
1eaf1 20 20 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 /* Once all
1eaf2 74 68 65 20 63 6f 6f 6b 69 65 73 20 68 61 76 65 the cookies have
1eaf3 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 20 61 been verified a
1eaf4 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 nd transactions
1eaf5 6f 70 65 6e 65 64 2c 20 0a 20 20 20 20 20 20 2a opened, . *
1eaf6 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 72 65 71 * obtain the req
1eaf7 75 69 72 65 64 20 74 61 62 6c 65 2d 6c 6f 63 6b uired table-lock
1eaf8 73 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d s. This is a no-
1eaf9 6f 70 20 75 6e 6c 65 73 73 20 74 68 65 20 0a 20 op unless the .
1eafa 20 20 20 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 ** shared-c
1eafb 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73 20 ache feature is
1eafc 65 6e 61 62 6c 65 64 2e 0a 20 20 20 20 20 20 2a enabled.. *
1eafd 2f 0a 20 20 20 20 20 20 63 6f 64 65 54 61 62 6c /. codeTabl
1eafe 65 4c 6f 63 6b 73 28 70 50 61 72 73 65 29 3b 0a eLocks(pParse);.
1eaff 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1eb00 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f eAddOp2(v, OP_Go
1eb01 74 6f 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e 63 to, 0, pParse->c
1eb02 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a 20 20 20 20 ookieGoto);.
1eb03 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1eb04 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 E_OMIT_TRACE.
1eb05 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 if( !db->init.b
1eb06 75 73 79 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 usy ){. /*
1eb07 43 68 61 6e 67 65 20 74 68 65 20 50 34 20 61 72 Change the P4 ar
1eb08 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 69 gument of the fi
1eb09 72 73 74 20 6f 70 63 6f 64 65 20 28 77 68 69 63 rst opcode (whic
1eb0a 68 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 h will always be
1eb0b 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 4f 50 5f . ** an OP_
1eb0c 54 72 61 63 65 29 20 74 6f 20 62 65 20 74 68 65 Trace) to be the
1eb0d 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f complete text o
1eb0e 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 53 51 f the current SQ
1eb0f 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 L statement..
1eb10 20 20 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 */. Vdbe
1eb11 4f 70 20 2a 70 4f 70 20 3d 20 73 71 6c 69 74 65 Op *pOp = sqlite
1eb12 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 30 29 3VdbeGetOp(v, 0)
1eb13 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 20 ;. if( pOp
1eb14 26 26 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d && pOp->opcode==
1eb15 4f 50 5f 54 72 61 63 65 20 29 7b 0a 20 20 20 20 OP_Trace ){.
1eb16 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1eb17 68 61 6e 67 65 50 34 28 76 2c 20 30 2c 20 70 50 hangeP4(v, 0, pP
1eb18 61 72 73 65 2d 3e 7a 53 71 6c 2c 20 70 50 61 72 arse->zSql, pPar
1eb19 73 65 2d 3e 7a 54 61 69 6c 2d 70 50 61 72 73 65 se->zTail-pParse
1eb1a 2d 3e 7a 53 71 6c 29 3b 0a 20 20 20 20 20 20 7d ->zSql);. }
1eb1b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.#endif /*
1eb1c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 SQLITE_OMIT_TRA
1eb1d 43 45 20 2a 2f 0a 20 20 7d 0a 0a 0a 20 20 2f 2a CE */. }... /*
1eb1e 20 47 65 74 20 74 68 65 20 56 44 42 45 20 70 72 Get the VDBE pr
1eb1f 6f 67 72 61 6d 20 72 65 61 64 79 20 66 6f 72 20 ogram ready for
1eb20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a 2f 0a 20 execution. */.
1eb21 20 69 66 28 20 76 20 26 26 20 70 50 61 72 73 65 if( v && pParse
1eb22 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 21 64 62 ->nErr==0 && !db
1eb23 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1eb24 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
1eb25 44 45 42 55 47 0a 20 20 20 20 46 49 4c 45 20 2a DEBUG. FILE *
1eb26 74 72 61 63 65 20 3d 20 28 64 62 2d 3e 66 6c 61 trace = (db->fla
1eb27 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65 gs & SQLITE_Vdbe
1eb28 54 72 61 63 65 29 21 3d 30 20 3f 20 73 74 64 6f Trace)!=0 ? stdo
1eb29 75 74 20 3a 20 30 3b 0a 20 20 20 20 73 71 6c 69 ut : 0;. sqli
1eb2a 74 65 33 56 64 62 65 54 72 61 63 65 28 76 2c 20 te3VdbeTrace(v,
1eb2b 74 72 61 63 65 29 3b 0a 23 65 6e 64 69 66 0a 20 trace);.#endif.
1eb2c 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 assert( pPars
1eb2d 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 e->disableColCac
1eb2e 68 65 3d 3d 30 20 29 3b 20 20 2f 2a 20 44 69 73 he==0 ); /* Dis
1eb2f 61 62 6c 65 73 20 61 6e 64 20 72 65 2d 65 6e 61 ables and re-ena
1eb30 62 6c 65 73 20 6d 61 74 63 68 20 2a 2f 0a 20 20 bles match */.
1eb31 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b sqlite3VdbeMak
1eb32 65 52 65 61 64 79 28 76 2c 20 70 50 61 72 73 65 eReady(v, pParse
1eb33 2d 3e 6e 56 61 72 2c 20 70 50 61 72 73 65 2d 3e ->nVar, pParse->
1eb34 6e 4d 65 6d 2b 33 2c 0a 20 20 20 20 20 20 20 20 nMem+3,.
1eb35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eb36 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 33 2c pParse->nTab+3,
1eb37 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e pParse->explain
1eb38 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 );. pParse->r
1eb39 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
1eb3a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c . pParse->col
1eb3b 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a 20 20 NamesSet = 0;.
1eb3c 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 73 65 }else if( pParse
1eb3d 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
1eb3e 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 ){. pParse->r
1eb3f 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1eb40 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e ;. }. pParse->
1eb41 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50 61 72 nTab = 0;. pPar
1eb42 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 20 20 se->nMem = 0;.
1eb43 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20 30 pParse->nSet = 0
1eb44 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 ;. pParse->nVar
1eb45 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e = 0;. pParse->
1eb46 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30 3b 0a cookieMask = 0;.
1eb47 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 pParse->cookie
1eb48 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a Goto = 0;.}../*.
1eb49 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65 ** Run the parse
1eb4a 72 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 r and code gener
1eb4b 61 74 6f 72 20 72 65 63 75 72 73 69 76 65 6c 79 ator recursively
1eb4c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e in order to gen
1eb4d 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f erate.** code fo
1eb4e 72 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d r the SQL statem
1eb4f 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20 74 ent given onto t
1eb50 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 50 he end of the pP
1eb51 61 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 arse context.**
1eb52 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 currently under
1eb53 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 57 construction. W
1eb54 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 69 hen the parser i
1eb55 73 20 72 75 6e 20 72 65 63 75 72 73 69 76 65 6c s run recursivel
1eb56 79 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c 20 74 y.** this way, t
1eb57 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 he final OP_Halt
1eb58 20 69 73 20 6e 6f 74 20 61 70 70 65 6e 64 65 64 is not appended
1eb59 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 69 74 69 and other initi
1eb5a 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 alization.** and
1eb5b 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73 74 finalization st
1eb5c 65 70 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 eps are omitted
1eb5d 62 65 63 61 75 73 65 20 74 68 6f 73 65 20 61 72 because those ar
1eb5e 65 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74 68 e handling by th
1eb5f 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20 70 e.** outermost p
1eb60 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 arser..**.** Not
1eb61 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 6e everything is n
1eb62 65 73 74 61 62 6c 65 2e 20 20 54 68 69 73 20 66 estable. This f
1eb63 61 63 69 6c 69 74 79 20 69 73 20 64 65 73 69 67 acility is desig
1eb64 6e 65 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a 2a ned to permit.**
1eb65 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
1eb66 20 61 6e 64 20 44 45 4c 45 54 45 20 6f 70 65 72 and DELETE oper
1eb67 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20 53 ations against S
1eb68 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20 55 QLITE_MASTER. U
1eb69 73 65 0a 2a 2a 20 63 61 72 65 20 69 66 20 79 6f se.** care if yo
1eb6a 75 20 64 65 63 69 64 65 20 74 6f 20 74 72 79 20 u decide to try
1eb6b 74 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 to use this rout
1eb6c 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 ine for some oth
1eb6d 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f 0a er purposes..*/.
1eb6e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1eb6f 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 oid sqlite3Neste
1eb70 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70 50 dParse(Parse *pP
1eb71 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 arse, const char
1eb72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
1eb73 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
1eb74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 63 char *zSql;. c
1eb75 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 har *zErrMsg = 0
1eb76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
1eb77 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 20 = pParse->db;.#
1eb78 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a 20 20 define SAVE_SZ
1eb79 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29 20 2d (sizeof(Parse) -
1eb7a 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73 65 2c offsetof(Parse,
1eb7b 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20 73 61 nVar)). char sa
1eb7c 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b 0a veBuf[SAVE_SZ];.
1eb7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e . if( pParse->n
1eb7e 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 Err ) return;.
1eb7f 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
1eb80 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20 2f 2a nested<10 ); /*
1eb81 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64 20 Nesting should
1eb82 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69 74 only be of limit
1eb83 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20 76 61 ed depth */. va
1eb84 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
1eb85 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 at);. zSql = sq
1eb86 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 lite3VMPrintf(db
1eb87 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
1eb88 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
1eb89 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 if( zSql==0 ){.
1eb8a 20 20 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a 20 return; /*
1eb8b 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 A malloc must ha
1eb8c 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 7d ve failed */. }
1eb8d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 . pParse->neste
1eb8e 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 73 61 d++;. memcpy(sa
1eb8f 76 65 42 75 66 2c 20 26 70 50 61 72 73 65 2d 3e veBuf, &pParse->
1eb90 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29 3b 0a nVar, SAVE_SZ);.
1eb91 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65 memset(&pParse
1eb92 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41 56 45 5f ->nVar, 0, SAVE_
1eb93 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 75 SZ);. sqlite3Ru
1eb94 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c 20 nParser(pParse,
1eb95 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b zSql, &zErrMsg);
1eb96 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1eb97 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 (db, zErrMsg);.
1eb98 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1eb99 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d 63 b, zSql);. memc
1eb9a 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56 61 72 py(&pParse->nVar
1eb9b 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56 45 5f , saveBuf, SAVE_
1eb9c 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e SZ);. pParse->n
1eb9d 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a ested--;.}../*.*
1eb9e 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d * Locate the in-
1eb9f 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 memory structure
1eba0 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
1eba1 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 a particular dat
1eba2 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67 abase.** table g
1eba3 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 iven the name of
1eba4 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 20 that table and
1eba5 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65 (optionally) the
1eba6 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 name of the.**
1eba7 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
1eba8 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 ing the table.
1eba9 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e Return NULL if n
1ebaa 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 ot found..**.**
1ebab 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20 If zDatabase is
1ebac 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 0, all databases
1ebad 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f are searched fo
1ebae 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 r the table and
1ebaf 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61 74 the.** first mat
1ebb0 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20 72 ching table is r
1ebb1 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68 eturned. (No ch
1ebb2 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c 69 ecking for dupli
1ebb3 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e 61 cate table.** na
1ebb4 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54 mes is done.) T
1ebb5 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20 he search order
1ebb6 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74 is TEMP first, t
1ebb7 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 hen MAIN, then a
1ebb8 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 ny.** auxiliary
1ebb9 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 20 databases added
1ebba 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48 using the ATTACH
1ebbb 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 command..**.**
1ebbc 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 See also sqlite3
1ebbd 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 0a 2a LocateTable()..*
1ebbe 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ebbf 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46 Table *sqlite3F
1ebc0 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33 indTable(sqlite3
1ebc1 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
1ebc2 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 *zName, const c
1ebc3 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 29 7b har *zDatabase){
1ebc4 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 30 3b . Table *p = 0;
1ebc5 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
1ebc6 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28 nName;. assert(
1ebc7 20 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 6e zName!=0 );. n
1ebc8 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 Name = sqlite3St
1ebc9 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 6d 65 29 20 rlen(db, zName)
1ebca 2b 20 31 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 + 1;. for(i=OMI
1ebcb 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e T_TEMPDB; i<db->
1ebcc 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 nDb; i++){. i
1ebcd 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 nt j = (i<2) ? i
1ebce 5e 31 20 3a 20 69 3b 20 20 20 2f 2a 20 53 65 61 ^1 : i; /* Sea
1ebcf 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 rch TEMP before
1ebd0 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 MAIN */. if(
1ebd1 7a 44 61 74 61 62 61 73 65 21 3d 30 20 26 26 20 zDatabase!=0 &&
1ebd2 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a sqlite3StrICmp(z
1ebd3 44 61 74 61 62 61 73 65 2c 20 64 62 2d 3e 61 44 Database, db->aD
1ebd4 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 6f b[j].zName) ) co
1ebd5 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 20 3d 20 ntinue;. p =
1ebd6 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 sqlite3HashFind(
1ebd7 26 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 &db->aDb[j].pSch
1ebd8 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e ema->tblHash, zN
1ebd9 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 ame, nName);.
1ebda 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a if( p ) break;.
1ebdb 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }. return p;.
1ebdc 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 }../*.** Locate
1ebdd 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 the in-memory st
1ebde 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 ructure that des
1ebdf 63 72 69 62 65 73 20 61 20 70 61 72 74 69 63 75 cribes a particu
1ebe0 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 lar database.**
1ebe1 74 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 table given the
1ebe2 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 name of that tab
1ebe3 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c le and (optional
1ebe4 6c 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 ly) the name of
1ebe5 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
1ebe6 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 containing the t
1ebe7 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 able. Return NU
1ebe8 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e LL if not found.
1ebe9 20 20 41 6c 73 6f 20 6c 65 61 76 65 20 61 6e 0a Also leave an.
1ebea 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 ** error message
1ebeb 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 in pParse->zErr
1ebec 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 Msg..**.** The d
1ebed 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
1ebee 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 n this routine a
1ebef 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 nd sqlite3FindTa
1ebf0 62 6c 65 28 29 20 69 73 20 74 68 61 74 20 74 68 ble() is that th
1ebf1 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6c 65 is.** routine le
1ebf2 61 76 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 aves an error me
1ebf3 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d ssage in pParse-
1ebf4 3e 7a 45 72 72 4d 73 67 20 77 68 65 72 65 0a 2a >zErrMsg where.*
1ebf5 2a 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 * sqlite3FindTab
1ebf6 6c 65 28 29 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a le() does not..*
1ebf7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ebf8 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c Table *sqlite3L
1ebf9 6f 63 61 74 65 54 61 62 6c 65 28 0a 20 20 50 61 ocateTable(. Pa
1ebfa 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1ebfb 20 20 20 20 20 2f 2a 20 63 6f 6e 74 65 78 74 20 /* context
1ebfc 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 70 6f in which to repo
1ebfd 72 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 rt errors */. i
1ebfe 6e 74 20 69 73 56 69 65 77 2c 20 20 20 20 20 20 nt isView,
1ebff 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
1ec00 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 56 looking for a V
1ec01 49 45 57 20 72 61 74 68 65 72 20 74 68 61 6e 20 IEW rather than
1ec02 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 6f 6e a TABLE */. con
1ec03 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
1ec04 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
1ec05 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 he table we are
1ec06 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 looking for */.
1ec07 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
1ec08 61 73 65 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 ase /* Name
1ec09 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e of the database.
1ec0a 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 Might be NULL
1ec0b 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 */.){. Table *p
1ec0c 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 ;.. /* Read the
1ec0d 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
1ec0e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
1ec0f 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 curs, leave an e
1ec10 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a rror message. *
1ec11 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 * and code in pP
1ec12 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 arse and return
1ec13 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 53 NULL. */. if( S
1ec14 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 QLITE_OK!=sqlite
1ec15 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 3ReadSchema(pPar
1ec16 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 se) ){. retur
1ec17 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 n 0;. }.. p =
1ec18 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 sqlite3FindTable
1ec19 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 (pParse->db, zNa
1ec1a 6d 65 2c 20 7a 44 62 61 73 65 29 3b 0a 20 20 69 me, zDbase);. i
1ec1b 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 63 f( p==0 ){. c
1ec1c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 73 67 20 onst char *zMsg
1ec1d 3d 20 69 73 56 69 65 77 20 3f 20 22 6e 6f 20 73 = isView ? "no s
1ec1e 75 63 68 20 76 69 65 77 22 20 3a 20 22 6e 6f 20 uch view" : "no
1ec1f 73 75 63 68 20 74 61 62 6c 65 22 3b 0a 20 20 20 such table";.
1ec20 20 69 66 28 20 7a 44 62 61 73 65 20 29 7b 0a 20 if( zDbase ){.
1ec21 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1ec22 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 rMsg(pParse, "%s
1ec23 3a 20 25 73 2e 25 73 22 2c 20 7a 4d 73 67 2c 20 : %s.%s", zMsg,
1ec24 7a 44 62 61 73 65 2c 20 7a 4e 61 6d 65 29 3b 0a zDbase, zName);.
1ec25 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ec26 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1ec27 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 (pParse, "%s: %s
1ec28 22 2c 20 7a 4d 73 67 2c 20 7a 4e 61 6d 65 29 3b ", zMsg, zName);
1ec29 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 . }. pPars
1ec2a 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d e->checkSchema =
1ec2b 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 1;. }. return
1ec2c 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 p;.}../*.** Loc
1ec2d 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 ate the in-memor
1ec2e 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 y structure that
1ec2f 20 64 65 73 63 72 69 62 65 73 20 0a 2a 2a 20 61 describes .** a
1ec30 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65 particular inde
1ec31 78 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 x given the name
1ec32 20 6f 66 20 74 68 61 74 20 69 6e 64 65 78 0a 2a of that index.*
1ec33 2a 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f * and the name o
1ec34 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 f the database t
1ec35 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
1ec36 20 69 6e 64 65 78 2e 0a 2a 2a 20 52 65 74 75 72 index..** Retur
1ec37 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f n NULL if not fo
1ec38 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44 und..**.** If zD
1ec39 61 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 6c atabase is 0, al
1ec3a 6c 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20 l databases are
1ec3b 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 65 searched for the
1ec3c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 74 68 .** table and th
1ec3d 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67 e first matching
1ec3e 20 69 6e 64 65 78 20 69 73 20 72 65 74 75 72 6e index is return
1ec3f 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e ed. (No checkin
1ec40 67 0a 2a 2a 20 66 6f 72 20 64 75 70 6c 69 63 61 g.** for duplica
1ec41 74 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 69 te index names i
1ec42 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 65 s done.) The se
1ec43 61 72 63 68 20 6f 72 64 65 72 20 69 73 0a 2a 2a arch order is.**
1ec44 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74 68 65 TEMP first, the
1ec45 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79 n MAIN, then any
1ec46 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 auxiliary datab
1ec47 61 73 65 73 20 61 64 64 65 64 0a 2a 2a 20 75 73 ases added.** us
1ec48 69 6e 67 20 74 68 65 20 41 54 54 41 43 48 20 63 ing the ATTACH c
1ec49 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 ommand..*/.SQLIT
1ec4a 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 E_PRIVATE Index
1ec4b 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 *sqlite3FindInde
1ec4c 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 x(sqlite3 *db, c
1ec4d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
1ec4e 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 , const char *zD
1ec4f 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 20 3d b){. Index *p =
1ec50 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 0;. int i;. i
1ec51 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 nt nName = sqlit
1ec52 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 e3Strlen(db, zNa
1ec53 6d 65 29 2b 31 3b 0a 20 20 66 6f 72 28 69 3d 4f me)+1;. for(i=O
1ec54 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 MIT_TEMPDB; i<db
1ec55 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
1ec56 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f int j = (i<2) ?
1ec57 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65 i^1 : i; /* Se
1ec58 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 arch TEMP before
1ec59 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 53 63 68 MAIN */. Sch
1ec5a 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 64 ema *pSchema = d
1ec5b 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d b->aDb[j].pSchem
1ec5c 61 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 26 a;. if( zDb &
1ec5d 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 & sqlite3StrICmp
1ec5e 28 7a 44 62 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d (zDb, db->aDb[j]
1ec5f 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e .zName) ) contin
1ec60 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ue;. assert(
1ec61 70 53 63 68 65 6d 61 20 7c 7c 20 28 6a 3d 3d 31 pSchema || (j==1
1ec62 20 26 26 20 21 64 62 2d 3e 61 44 62 5b 31 5d 2e && !db->aDb[1].
1ec63 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 pBt) );. if(
1ec64 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 pSchema ){.
1ec65 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 p = sqlite3Hash
1ec66 46 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e 69 Find(&pSchema->i
1ec67 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e dxHash, zName, n
1ec68 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Name);. }.
1ec69 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a if( p ) break;.
1ec6a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }. return p;.
1ec6b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d }../*.** Reclaim
1ec6c 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 the memory used
1ec6d 20 62 79 20 61 6e 20 69 6e 64 65 78 0a 2a 2f 0a by an index.*/.
1ec6e 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 static void free
1ec6f 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b Index(Index *p){
1ec70 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1ec71 20 70 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 3b 0a p->pTable->db;.
1ec72 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1ec73 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b db, p->zColAff);
1ec74 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1ec75 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a (db, p);.}../*.*
1ec76 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 67 69 76 * Remove the giv
1ec77 65 6e 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 en index from th
1ec78 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 e index hash tab
1ec79 6c 65 2c 20 61 6e 64 20 66 72 65 65 0a 2a 2a 20 le, and free.**
1ec7a 69 74 73 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 its memory struc
1ec7b 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tures..**.** The
1ec7c 20 69 6e 64 65 78 20 69 73 20 72 65 6d 6f 76 65 index is remove
1ec7d 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 d from the datab
1ec7e 61 73 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 ase hash tables
1ec7f 62 75 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 but.** it is not
1ec80 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74 unlinked from t
1ec81 68 65 20 54 61 62 6c 65 20 74 68 61 74 20 69 74 he Table that it
1ec82 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 55 6e 6c indexes..** Unl
1ec83 69 6e 6b 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 inking from the
1ec84 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 64 6f Table must be do
1ec85 6e 65 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ne by the callin
1ec86 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 g function..*/.s
1ec87 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
1ec88 65 44 65 6c 65 74 65 49 6e 64 65 78 28 49 6e 64 eDeleteIndex(Ind
1ec89 65 78 20 2a 70 29 7b 0a 20 20 49 6e 64 65 78 20 ex *p){. Index
1ec8a 2a 70 4f 6c 64 3b 0a 20 20 63 6f 6e 73 74 20 63 *pOld;. const c
1ec8b 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d 3e har *zName = p->
1ec8c 7a 4e 61 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20 3d zName;.. pOld =
1ec8d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 sqlite3HashInse
1ec8e 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d 3e rt(&p->pSchema->
1ec8f 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 idxHash, zName,
1ec90 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 2b 31 2c strlen(zName)+1,
1ec91 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0);. assert( p
1ec92 4f 6c 64 3d 3d 30 20 7c 7c 20 70 4f 6c 64 3d 3d Old==0 || pOld==
1ec93 70 20 29 3b 0a 20 20 66 72 65 65 49 6e 64 65 78 p );. freeIndex
1ec94 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f (p);.}../*.** Fo
1ec95 72 20 74 68 65 20 69 6e 64 65 78 20 63 61 6c 6c r the index call
1ec96 65 64 20 7a 49 64 78 4e 61 6d 65 20 77 68 69 63 ed zIdxName whic
1ec97 68 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 h is found in th
1ec98 65 20 64 61 74 61 62 61 73 65 20 69 44 62 2c 0a e database iDb,.
1ec99 2a 2a 20 75 6e 6c 69 6b 65 20 74 68 61 74 20 69 ** unlike that i
1ec9a 6e 64 65 78 20 66 72 6f 6d 20 69 74 73 20 54 61 ndex from its Ta
1ec9b 62 6c 65 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 ble then remove
1ec9c 74 68 65 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a the index from.*
1ec9d 2a 20 74 68 65 20 69 6e 64 65 78 20 68 61 73 68 * the index hash
1ec9e 20 74 61 62 6c 65 20 61 6e 64 20 66 72 65 65 20 table and free
1ec9f 61 6c 6c 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 all memory struc
1eca0 74 75 72 65 73 20 61 73 73 6f 63 69 61 74 65 64 tures associated
1eca1 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 69 6e 64 .** with the ind
1eca2 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ex..*/.SQLITE_PR
1eca3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1eca4 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 e3UnlinkAndDelet
1eca5 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a eIndex(sqlite3 *
1eca6 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e db, int iDb, con
1eca7 73 74 20 63 68 61 72 20 2a 7a 49 64 78 4e 61 6d st char *zIdxNam
1eca8 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e e){. Index *pIn
1eca9 64 65 78 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a dex;. int len;.
1ecaa 20 20 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 Hash *pHash =
1ecab 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 &db->aDb[iDb].pS
1ecac 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a chema->idxHash;.
1ecad 0a 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 . len = sqlite3
1ecae 53 74 72 6c 65 6e 28 64 62 2c 20 7a 49 64 78 4e Strlen(db, zIdxN
1ecaf 61 6d 65 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d ame);. pIndex =
1ecb0 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 sqlite3HashInse
1ecb1 72 74 28 70 48 61 73 68 2c 20 7a 49 64 78 4e 61 rt(pHash, zIdxNa
1ecb2 6d 65 2c 20 6c 65 6e 2b 31 2c 20 30 29 3b 0a 20 me, len+1, 0);.
1ecb3 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 if( pIndex ){.
1ecb4 20 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 70 if( pIndex->p
1ecb5 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 Table->pIndex==p
1ecb6 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 70 Index ){. p
1ecb7 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 Index->pTable->p
1ecb8 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e Index = pIndex->
1ecb9 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 pNext;. }else
1ecba 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 {. Index *p
1ecbb 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 70 49 ;. for(p=pI
1ecbc 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 ndex->pTable->pI
1ecbd 6e 64 65 78 3b 20 70 20 26 26 20 70 2d 3e 70 4e ndex; p && p->pN
1ecbe 65 78 74 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70 ext!=pIndex; p=p
1ecbf 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20 ->pNext){}.
1ecc0 20 69 66 28 20 70 20 26 26 20 70 2d 3e 70 4e 65 if( p && p->pNe
1ecc1 78 74 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 xt==pIndex ){.
1ecc2 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d p->pNext =
1ecc3 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a pIndex->pNext;.
1ecc4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1ecc5 20 20 66 72 65 65 49 6e 64 65 78 28 70 49 6e 64 freeIndex(pInd
1ecc6 65 78 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 ex);. }. db->f
1ecc7 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 lags |= SQLITE_I
1ecc8 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a nternChanges;.}.
1ecc9 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c ./*.** Erase all
1ecca 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 schema informat
1eccb 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d ion from the in-
1eccc 6d 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62 6c memory hash tabl
1eccd 65 73 20 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c es of.** a singl
1ecce 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 e database. Thi
1eccf 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
1ecd0 6c 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d led to reclaim m
1ecd1 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 emory.** before
1ecd2 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6c 6f the database clo
1ecd3 73 65 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f ses. It is also
1ecd4 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 called during a
1ecd5 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 rollback.** if
1ecd6 74 68 65 72 65 20 77 65 72 65 20 73 63 68 65 6d there were schem
1ecd7 61 20 63 68 61 6e 67 65 73 20 64 75 72 69 6e 67 a changes during
1ecd8 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1ecd9 20 6f 72 20 69 66 20 61 0a 2a 2a 20 73 63 68 65 or if a.** sche
1ecda 6d 61 2d 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74 ma-cookie mismat
1ecdb 63 68 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a ch occurs..**.**
1ecdc 20 49 66 20 69 44 62 3c 3d 30 20 74 68 65 6e 20 If iDb<=0 then
1ecdd 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e reset the intern
1ecde 61 6c 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73 al schema tables
1ecdf 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 for all databas
1ece0 65 0a 2a 2a 20 66 69 6c 65 73 2e 20 20 49 66 20 e.** files. If
1ece1 69 44 62 3e 3d 32 20 74 68 65 6e 20 72 65 73 65 iDb>=2 then rese
1ece2 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 t the internal s
1ece3 63 68 65 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74 chema for only t
1ece4 68 65 0a 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c he.** single fil
1ece5 65 20 69 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a e indicated..*/.
1ece6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1ece7 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 oid sqlite3Reset
1ece8 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 InternalSchema(s
1ece9 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
1ecea 69 44 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a iDb){. int i, j
1eceb 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e ;. assert( iDb>
1ecec 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 =0 && iDb<db->nD
1eced 62 20 29 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d b );.. if( iDb=
1ecee 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1ecef 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 3BtreeEnterAll(d
1ecf0 62 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d b);. }. for(i=
1ecf1 69 44 62 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 iDb; i<db->nDb;
1ecf2 69 2b 2b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 i++){. Db *pD
1ecf3 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b b = &db->aDb[i];
1ecf4 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 . if( pDb->pS
1ecf5 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 61 chema ){. a
1ecf6 73 73 65 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70 ssert(i==1 || (p
1ecf7 44 62 2d 3e 70 42 74 20 26 26 20 73 71 6c 69 74 Db->pBt && sqlit
1ecf8 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
1ecf9 78 28 70 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20 x(pDb->pBt)));.
1ecfa 20 20 20 20 20 73 71 6c 69 74 65 33 53 63 68 65 sqlite3Sche
1ecfb 6d 61 46 72 65 65 28 70 44 62 2d 3e 70 53 63 68 maFree(pDb->pSch
1ecfc 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ema);. }.
1ecfd 69 66 28 20 69 44 62 3e 30 20 29 20 72 65 74 75 if( iDb>0 ) retu
1ecfe 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rn;. }. assert
1ecff 28 20 69 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62 ( iDb==0 );. db
1ed00 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 ->flags &= ~SQLI
1ed01 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 TE_InternChanges
1ed02 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1ed03 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 0a 20 LeaveAll(db);..
1ed04 20 2f 2a 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f /* If one or mo
1ed05 72 65 20 6f 66 20 74 68 65 20 61 75 78 69 6c 69 re of the auxili
1ed06 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c ary database fil
1ed07 65 73 20 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 es has been clos
1ed08 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 ed,. ** then re
1ed09 6d 6f 76 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 move them from t
1ed0a 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 he auxiliary dat
1ed0b 61 62 61 73 65 20 6c 69 73 74 2e 20 20 57 65 20 abase list. We
1ed0c 74 61 6b 65 20 74 68 65 0a 20 20 2a 2a 20 6f 70 take the. ** op
1ed0d 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 64 6f 20 portunity to do
1ed0e 74 68 69 73 20 68 65 72 65 20 73 69 6e 63 65 20 this here since
1ed0f 77 65 20 68 61 76 65 20 6a 75 73 74 20 64 65 6c we have just del
1ed10 65 74 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 0a eted all of the.
1ed11 20 20 2a 2a 20 73 63 68 65 6d 61 20 68 61 73 68 ** schema hash
1ed12 20 74 61 62 6c 65 73 20 61 6e 64 20 74 68 65 72 tables and ther
1ed13 65 66 6f 72 65 20 64 6f 20 6e 6f 74 20 68 61 76 efore do not hav
1ed14 65 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 63 68 e to make any ch
1ed15 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20 61 6e anges. ** to an
1ed16 79 20 6f 66 20 74 68 6f 73 65 20 74 61 62 6c 65 y of those table
1ed17 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d s.. */. for(i=
1ed18 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
1ed19 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 +){. struct D
1ed1a 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 b *pDb = &db->aD
1ed1b 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 b[i];. if( pD
1ed1c 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 b->pBt==0 ){.
1ed1d 20 20 20 69 66 28 20 70 44 62 2d 3e 70 41 75 78 if( pDb->pAux
1ed1e 20 26 26 20 70 44 62 2d 3e 78 46 72 65 65 41 75 && pDb->xFreeAu
1ed1f 78 20 29 20 70 44 62 2d 3e 78 46 72 65 65 41 75 x ) pDb->xFreeAu
1ed20 78 28 70 44 62 2d 3e 70 41 75 78 29 3b 0a 20 20 x(pDb->pAux);.
1ed21 20 20 20 20 70 44 62 2d 3e 70 41 75 78 20 3d 20 pDb->pAux =
1ed22 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 0;. }. }. f
1ed23 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e or(i=j=2; i<db->
1ed24 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 nDb; i++){. s
1ed25 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20 truct Db *pDb =
1ed26 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 &db->aDb[i];.
1ed27 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 if( pDb->pBt==0
1ed28 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1ed29 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 62 2d 3DbFree(db, pDb-
1ed2a 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 >zName);. p
1ed2b 44 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 Db->zName = 0;.
1ed2c 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
1ed2d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69 }. if( j<i
1ed2e 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 ){. db->aD
1ed2f 62 5b 6a 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 b[j] = db->aDb[i
1ed30 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b ];. }. j++
1ed31 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 ;. }. memset(&
1ed32 64 62 2d 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28 db->aDb[j], 0, (
1ed33 64 62 2d 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f db->nDb-j)*sizeo
1ed34 66 28 64 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a f(db->aDb[j]));.
1ed35 20 20 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 db->nDb = j;.
1ed36 20 69 66 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 if( db->nDb<=2
1ed37 26 26 20 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e && db->aDb!=db->
1ed38 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20 20 aDbStatic ){.
1ed39 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 memcpy(db->aDbS
1ed3a 74 61 74 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 tatic, db->aDb,
1ed3b 32 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 2*sizeof(db->aDb
1ed3c 5b 30 5d 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 [0]));. sqlit
1ed3d 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d e3DbFree(db, db-
1ed3e 3e 61 44 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61 >aDb);. db->a
1ed3f 44 62 20 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 Db = db->aDbStat
1ed40 69 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a ic;. }.}../*.**
1ed41 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1ed42 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 63 called when a c
1ed43 6f 6d 6d 69 74 20 6f 63 63 75 72 73 2e 0a 2a 2f ommit occurs..*/
1ed44 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1ed45 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d void sqlite3Comm
1ed46 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 itInternalChange
1ed47 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a s(sqlite3 *db){.
1ed48 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e db->flags &= ~
1ed49 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
1ed4a 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 nges;.}../*.** C
1ed4b 6c 65 61 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 lear the column
1ed4c 6e 61 6d 65 73 20 66 72 6f 6d 20 61 20 74 61 62 names from a tab
1ed4d 6c 65 20 6f 72 20 76 69 65 77 2e 0a 2a 2f 0a 73 le or view..*/.s
1ed4e 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
1ed4f 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 eResetColumnName
1ed50 73 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 s(Table *pTable)
1ed51 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c {. int i;. Col
1ed52 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c umn *pCol;. sql
1ed53 69 74 65 33 20 2a 64 62 20 3d 20 70 54 61 62 6c ite3 *db = pTabl
1ed54 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 e->db;. assert(
1ed55 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20 pTable!=0 );.
1ed56 69 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62 if( (pCol = pTab
1ed57 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a le->aCol)!=0 ){.
1ed58 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
1ed59 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b Table->nCol; i++
1ed5a 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 , pCol++){.
1ed5b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1ed5c 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b b, pCol->zName);
1ed5d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
1ed5e 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f prDelete(db, pCo
1ed5f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 l->pDflt);.
1ed60 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1ed61 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b b, pCol->zType);
1ed62 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
1ed63 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a Free(db, pCol->z
1ed64 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Coll);. }.
1ed65 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1ed66 62 2c 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 b, pTable->aCol)
1ed67 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e ;. }. pTable->
1ed68 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62 aCol = 0;. pTab
1ed69 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a le->nCol = 0;.}.
1ed6a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 ./*.** Remove th
1ed6b 65 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 e memory data st
1ed6c 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 ructures associa
1ed6d 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 ted with the giv
1ed6e 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f en.** Table. No
1ed6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 changes are mad
1ed70 65 20 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69 e to disk by thi
1ed71 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a s routine..**.**
1ed72 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 This routine ju
1ed73 73 74 20 64 65 6c 65 74 65 73 20 74 68 65 20 64 st deletes the d
1ed74 61 74 61 20 73 74 72 75 63 74 75 72 65 2e 20 20 ata structure.
1ed75 49 74 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69 It does not unli
1ed76 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 nk.** the table
1ed77 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 66 data structure f
1ed78 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 rom the hash tab
1ed79 6c 65 2e 20 20 4e 6f 72 20 64 6f 65 73 20 69 74 le. Nor does it
1ed7a 20 72 65 6d 6f 76 65 0a 2a 2a 20 66 6f 72 65 69 remove.** forei
1ed7b 67 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74 68 65 gn keys from the
1ed7c 20 73 71 6c 69 74 65 2e 61 46 4b 65 79 20 68 61 sqlite.aFKey ha
1ed7d 73 68 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69 sh table. But i
1ed7e 74 20 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a t does destroy.*
1ed7f 2a 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 * memory structu
1ed80 72 65 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63 res of the indic
1ed81 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b es and foreign k
1ed82 65 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 eys associated w
1ed83 69 74 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c ith .** the tabl
1ed84 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1ed85 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1ed86 33 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 3DeleteTable(Tab
1ed87 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49 le *pTable){. I
1ed88 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70 ndex *pIndex, *p
1ed89 4e 65 78 74 3b 0a 20 20 46 4b 65 79 20 2a 70 46 Next;. FKey *pF
1ed8a 4b 65 79 2c 20 2a 70 4e 65 78 74 46 4b 65 79 3b Key, *pNextFKey;
1ed8b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1ed8c 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 . if( pTable==0
1ed8d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 ) return;. db
1ed8e 3d 20 70 54 61 62 6c 65 2d 3e 64 62 3b 0a 0a 20 = pTable->db;..
1ed8f 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 /* Do not delet
1ed90 65 20 74 68 65 20 74 61 62 6c 65 20 75 6e 74 69 e the table unti
1ed91 6c 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 l the reference
1ed92 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 count reaches ze
1ed93 72 6f 2e 20 2a 2f 0a 20 20 70 54 61 62 6c 65 2d ro. */. pTable-
1ed94 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 >nRef--;. if( p
1ed95 54 61 62 6c 65 2d 3e 6e 52 65 66 3e 30 20 29 7b Table->nRef>0 ){
1ed96 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
1ed97 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c . assert( pTabl
1ed98 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 e->nRef==0 );..
1ed99 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 /* Delete all i
1ed9a 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 ndices associate
1ed9b 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c d with this tabl
1ed9c 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 49 6e e. */. for(pIn
1ed9d 64 65 78 20 3d 20 70 54 61 62 6c 65 2d 3e 70 49 dex = pTable->pI
1ed9e 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 ndex; pIndex; pI
1ed9f 6e 64 65 78 3d 70 4e 65 78 74 29 7b 0a 20 20 20 ndex=pNext){.
1eda0 20 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 2d pNext = pIndex-
1eda1 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61 73 73 65 >pNext;. asse
1eda2 72 74 28 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 rt( pIndex->pSch
1eda3 65 6d 61 3d 3d 70 54 61 62 6c 65 2d 3e 70 53 63 ema==pTable->pSc
1eda4 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 hema );. sqli
1eda5 74 65 44 65 6c 65 74 65 49 6e 64 65 78 28 70 49 teDeleteIndex(pI
1eda6 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e ndex);. }..#ifn
1eda7 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1eda8 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 2f 2a FOREIGN_KEY. /*
1eda9 20 44 65 6c 65 74 65 20 61 6c 6c 20 66 6f 72 65 Delete all fore
1edaa 69 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 ign keys associa
1edab 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74 61 ted with this ta
1edac 62 6c 65 2e 20 20 54 68 65 20 6b 65 79 73 0a 20 ble. The keys.
1edad 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 76 65 20 ** should have
1edae 61 6c 72 65 61 64 79 20 62 65 65 6e 20 75 6e 6c already been unl
1edaf 69 6e 6b 65 64 20 66 72 6f 6d 20 74 68 65 20 70 inked from the p
1edb0 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 20 68 61 Schema->aFKey ha
1edb1 73 68 20 74 61 62 6c 65 20 0a 20 20 2a 2f 0a 20 sh table . */.
1edb2 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62 6c for(pFKey=pTabl
1edb3 65 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b e->pFKey; pFKey;
1edb4 20 70 46 4b 65 79 3d 70 4e 65 78 74 46 4b 65 79 pFKey=pNextFKey
1edb5 29 7b 0a 20 20 20 20 70 4e 65 78 74 46 4b 65 79 ){. pNextFKey
1edb6 20 3d 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 = pFKey->pNextF
1edb7 72 6f 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 rom;. assert(
1edb8 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 sqlite3HashFind
1edb9 28 26 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d (&pTable->pSchem
1edba 61 2d 3e 61 46 4b 65 79 2c 0a 20 20 20 20 20 20 a->aFKey,.
1edbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1edbc 20 20 20 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c pFKey->zTo,
1edbd 20 73 74 72 6c 65 6e 28 70 46 4b 65 79 2d 3e 7a strlen(pFKey->z
1edbe 54 6f 29 2b 31 29 21 3d 70 46 4b 65 79 20 29 3b To)+1)!=pFKey );
1edbf 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1edc0 65 65 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 20 ee(db, pFKey);.
1edc1 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
1edc2 44 65 6c 65 74 65 20 74 68 65 20 54 61 62 6c 65 Delete the Table
1edc3 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c structure itsel
1edc4 66 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 f.. */. sqlite
1edc5 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 ResetColumnNames
1edc6 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 (pTable);. sqli
1edc7 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 te3DbFree(db, pT
1edc8 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 able->zName);.
1edc9 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1edca 2c 20 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66 , pTable->zColAf
1edcb 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c f);. sqlite3Sel
1edcc 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 ectDelete(db, pT
1edcd 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a able->pSelect);.
1edce 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1edcf 4d 49 54 5f 43 48 45 43 4b 0a 20 20 73 71 6c 69 MIT_CHECK. sqli
1edd0 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1edd1 2c 20 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b , pTable->pCheck
1edd2 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 );.#endif. sqli
1edd3 74 65 33 56 74 61 62 43 6c 65 61 72 28 70 54 61 te3VtabClear(pTa
1edd4 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 ble);. sqlite3D
1edd5 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 bFree(db, pTable
1edd6 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 );.}../*.** Unli
1edd7 6e 6b 20 74 68 65 20 67 69 76 65 6e 20 74 61 62 nk the given tab
1edd8 6c 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 le from the hash
1edd9 20 74 61 62 6c 65 73 20 61 6e 64 20 74 68 65 20 tables and the
1edda 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 74 61 delete the.** ta
1eddb 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 77 69 ble structure wi
1eddc 74 68 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 th all its indic
1eddd 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b es and foreign k
1edde 65 79 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 eys..*/.SQLITE_P
1eddf 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1ede0 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 te3UnlinkAndDele
1ede1 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 teTable(sqlite3
1ede2 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f *db, int iDb, co
1ede3 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 nst char *zTabNa
1ede4 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b me){. Table *p;
1ede5 0a 20 20 46 4b 65 79 20 2a 70 46 31 2c 20 2a 70 . FKey *pF1, *p
1ede6 46 32 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a F2;. Db *pDb;..
1ede7 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 assert( db!=0
1ede8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 );. assert( iDb
1ede9 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e >=0 && iDb<db->n
1edea 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Db );. assert(
1edeb 7a 54 61 62 4e 61 6d 65 20 26 26 20 7a 54 61 62 zTabName && zTab
1edec 4e 61 6d 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62 Name[0] );. pDb
1eded 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d = &db->aDb[iDb]
1edee 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 ;. p = sqlite3H
1edef 61 73 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e ashInsert(&pDb->
1edf0 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 pSchema->tblHash
1edf1 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 73 74 72 6c , zTabName, strl
1edf2 65 6e 28 7a 54 61 62 4e 61 6d 65 29 2b 31 2c 30 en(zTabName)+1,0
1edf3 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 23 69 );. if( p ){.#i
1edf4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1edf5 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 T_FOREIGN_KEY.
1edf6 20 20 66 6f 72 28 70 46 31 3d 70 2d 3e 70 46 4b for(pF1=p->pFK
1edf7 65 79 3b 20 70 46 31 3b 20 70 46 31 3d 70 46 31 ey; pF1; pF1=pF1
1edf8 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 ->pNextFrom){.
1edf9 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73 74 int nTo = st
1edfa 72 6c 65 6e 28 70 46 31 2d 3e 7a 54 6f 29 20 2b rlen(pF1->zTo) +
1edfb 20 31 3b 0a 20 20 20 20 20 20 70 46 32 20 3d 20 1;. pF2 =
1edfc 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 sqlite3HashFind(
1edfd 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 61 &pDb->pSchema->a
1edfe 46 4b 65 79 2c 20 70 46 31 2d 3e 7a 54 6f 2c 20 FKey, pF1->zTo,
1edff 6e 54 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 nTo);. if(
1ee00 70 46 32 3d 3d 70 46 31 20 29 7b 0a 20 20 20 20 pF2==pF1 ){.
1ee01 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 sqlite3HashI
1ee02 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53 63 68 nsert(&pDb->pSch
1ee03 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46 31 2d ema->aFKey, pF1-
1ee04 3e 7a 54 6f 2c 20 6e 54 6f 2c 20 70 46 31 2d 3e >zTo, nTo, pF1->
1ee05 70 4e 65 78 74 54 6f 29 3b 0a 20 20 20 20 20 20 pNextTo);.
1ee06 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77 }else{. w
1ee07 68 69 6c 65 28 20 70 46 32 20 26 26 20 70 46 32 hile( pF2 && pF2
1ee08 2d 3e 70 4e 65 78 74 54 6f 21 3d 70 46 31 20 29 ->pNextTo!=pF1 )
1ee09 7b 20 70 46 32 3d 70 46 32 2d 3e 70 4e 65 78 74 { pF2=pF2->pNext
1ee0a 54 6f 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 To; }. if
1ee0b 28 20 70 46 32 20 29 7b 0a 20 20 20 20 20 20 20 ( pF2 ){.
1ee0c 20 20 20 70 46 32 2d 3e 70 4e 65 78 74 54 6f 20 pF2->pNextTo
1ee0d 3d 20 70 46 31 2d 3e 70 4e 65 78 74 54 6f 3b 0a = pF1->pNextTo;.
1ee0e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1ee0f 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
1ee10 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 sqlite3Delete
1ee11 54 61 62 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 Table(p);. }.
1ee12 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c db->flags |= SQL
1ee13 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 ITE_InternChange
1ee14 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 s;.}../*.** Give
1ee15 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 65 74 75 72 n a token, retur
1ee16 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 n a string that
1ee17 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 consists of the
1ee18 74 65 78 74 20 6f 66 20 74 68 61 74 0a 2a 2a 20 text of that.**
1ee19 74 6f 6b 65 6e 20 77 69 74 68 20 61 6e 79 20 71 token with any q
1ee1a 75 6f 74 61 74 69 6f 6e 73 20 72 65 6d 6f 76 65 uotations remove
1ee1b 64 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c d. Space to hol
1ee1c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 d the returned s
1ee1d 74 72 69 6e 67 0a 2a 2a 20 69 73 20 6f 62 74 61 tring.** is obta
1ee1e 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
1ee1f 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73 Malloc() and mus
1ee20 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68 t be freed by th
1ee21 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e e calling.** fun
1ee22 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b ction..**.** Tok
1ee23 65 6e 73 20 61 72 65 20 6f 66 74 65 6e 20 6a 75 ens are often ju
1ee24 73 74 20 70 6f 69 6e 74 65 72 73 20 69 6e 74 6f st pointers into
1ee25 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 the original SQ
1ee26 4c 20 74 65 78 74 20 61 6e 64 20 73 6f 0a 2a 2a L text and so.**
1ee27 20 61 72 65 20 6e 6f 74 20 5c 30 30 30 20 74 65 are not \000 te
1ee28 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 61 72 65 rminated and are
1ee29 20 6e 6f 74 20 70 65 72 73 69 73 74 65 6e 74 2e not persistent.
1ee2a 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 The returned s
1ee2b 74 72 69 6e 67 0a 2a 2a 20 69 73 20 5c 30 30 30 tring.** is \000
1ee2c 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 terminated and
1ee2d 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 2a is persistent..*
1ee2e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ee2f 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 char *sqlite3Na
1ee30 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 meFromToken(sqli
1ee31 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a te3 *db, Token *
1ee32 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a pName){. char *
1ee33 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 70 4e 61 zName;. if( pNa
1ee34 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 me ){. zName
1ee35 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 = sqlite3DbStrND
1ee36 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 4e up(db, (char*)pN
1ee37 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e ame->z, pName->n
1ee38 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 );. sqlite3De
1ee39 71 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 quote(zName);.
1ee3a 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4e 61 6d 65 }else{. zName
1ee3b 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 0;. }. retu
1ee3c 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a rn zName;.}../*.
1ee3d 2a 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 ** Open the sqli
1ee3e 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 te_master table
1ee3f 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 stored in databa
1ee40 73 65 20 6e 75 6d 62 65 72 20 69 44 62 20 66 6f se number iDb fo
1ee41 72 0a 2a 2a 20 77 72 69 74 69 6e 67 2e 20 54 68 r.** writing. Th
1ee42 65 20 74 61 62 6c 65 20 69 73 20 6f 70 65 6e 65 e table is opene
1ee43 64 20 75 73 69 6e 67 20 63 75 72 73 6f 72 20 30 d using cursor 0
1ee44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1ee45 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1ee46 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 OpenMasterTable(
1ee47 50 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69 44 Parse *p, int iD
1ee48 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 b){. Vdbe *v =
1ee49 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
1ee4a 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c );. sqlite3Tabl
1ee4b 65 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 4d 41 eLock(p, iDb, MA
1ee4c 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 53 43 STER_ROOT, 1, SC
1ee4d 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 29 HEMA_TABLE(iDb))
1ee4e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 ;. sqlite3VdbeA
1ee4f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e ddOp2(v, OP_SetN
1ee50 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 35 29 umColumns, 0, 5)
1ee51 3b 2f 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 ;/* sqlite_maste
1ee52 72 20 68 61 73 20 35 20 63 6f 6c 75 6d 6e 73 20 r has 5 columns
1ee53 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 */. sqlite3Vdbe
1ee54 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 AddOp3(v, OP_Ope
1ee55 6e 57 72 69 74 65 2c 20 30 2c 20 4d 41 53 54 45 nWrite, 0, MASTE
1ee56 52 5f 52 4f 4f 54 2c 20 69 44 62 29 3b 0a 7d 0a R_ROOT, iDb);.}.
1ee57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 6e ./*.** The token
1ee58 20 2a 70 4e 61 6d 65 20 63 6f 6e 74 61 69 6e 73 *pName contains
1ee59 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 the name of a d
1ee5a 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72 20 atabase (either
1ee5b 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 65 "main" or.** "te
1ee5c 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 mp" or the name
1ee5d 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 of an attached d
1ee5e 62 29 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 b). This routine
1ee5f 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 returns the.**
1ee60 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d index of the nam
1ee61 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 ed database in d
1ee62 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 20 b->aDb[], or -1
1ee63 69 66 20 74 68 65 20 6e 61 6d 65 64 20 64 62 20 if the named db
1ee64 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 .** does not exi
1ee65 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 st..*/.SQLITE_PR
1ee66 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1ee67 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 20 3FindDb(sqlite3
1ee68 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d *db, Token *pNam
1ee69 65 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 2d 31 e){. int i = -1
1ee6a 3b 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 ; /* Database
1ee6b 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 69 7a number */. siz
1ee6c 65 5f 74 20 6e 3b 20 20 20 20 20 20 2f 2a 20 4e e_t n; /* N
1ee6d 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
1ee6e 65 72 73 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 ers in the name
1ee6f 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 */. Db *pDb;
1ee70 20 20 20 20 2f 2a 20 41 20 64 61 74 61 62 61 73 /* A databas
1ee71 65 20 77 68 6f 73 65 20 6e 61 6d 65 20 73 70 61 e whose name spa
1ee72 63 65 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 ce is being sear
1ee73 63 68 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a ched */. char *
1ee74 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 zName; /* Name
1ee75 20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e we are searchin
1ee76 67 20 66 6f 72 20 2a 2f 0a 0a 20 20 7a 4e 61 6d g for */.. zNam
1ee77 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 e = sqlite3NameF
1ee78 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 romToken(db, pNa
1ee79 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 me);. if( zName
1ee7a 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c ){. n = strl
1ee7b 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66 en(zName);. f
1ee7c 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 29 or(i=(db->nDb-1)
1ee7d 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b 69 , pDb=&db->aDb[i
1ee7e 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 44 ]; i>=0; i--, pD
1ee7f 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 b--){. if(
1ee80 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c 7c (!OMIT_TEMPDB ||
1ee81 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73 74 i!=1 ) && n==st
1ee82 72 6c 65 6e 28 70 44 62 2d 3e 7a 4e 61 6d 65 29 rlen(pDb->zName)
1ee83 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 30 && . 0
1ee84 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ==sqlite3StrICmp
1ee85 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 (pDb->zName, zNa
1ee86 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62 me) ){. b
1ee87 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1ee88 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 }. sqlite3D
1ee89 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 bFree(db, zName)
1ee8a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 ;. }. return i
1ee8b 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74 61 62 6c ;.}../* The tabl
1ee8c 65 20 6f 72 20 76 69 65 77 20 6f 72 20 74 72 69 e or view or tri
1ee8d 67 67 65 72 20 6e 61 6d 65 20 69 73 20 70 61 73 gger name is pas
1ee8e 73 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74 sed to this rout
1ee8f 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a 2a ine via tokens.*
1ee90 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 * pName1 and pNa
1ee91 6d 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c me2. If the tabl
1ee92 65 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 e name was fully
1ee93 20 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 qualified, for
1ee94 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43 example:.**.** C
1ee95 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e REATE TABLE xxx.
1ee96 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a yyy (...);.** .*
1ee97 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 * Then pName1 is
1ee98 20 73 65 74 20 74 6f 20 22 78 78 78 22 20 61 6e set to "xxx" an
1ee99 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 d pName2 "yyy".
1ee9a 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e On the other han
1ee9b 64 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c d if.** the tabl
1ee9c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 e name is not fu
1ee9d 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69 lly qualified, i
1ee9e 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 .e.:.**.** CREAT
1ee9f 45 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 E TABLE yyy(...)
1eea0 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 ;.**.** Then pNa
1eea1 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 79 me1 is set to "y
1eea2 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 yy" and pName2 i
1eea3 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 s ""..**.** This
1eea4 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 routine sets th
1eea5 65 20 2a 70 70 55 6e 71 75 61 6c 20 70 6f 69 6e e *ppUnqual poin
1eea6 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 ter to point at
1eea7 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 the token (pName
1eea8 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65 32 29 20 1 or.** pName2)
1eea9 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 that stores the
1eeaa 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c unqualified tabl
1eeab 65 20 6e 61 6d 65 2e 20 20 54 68 65 20 69 6e 64 e name. The ind
1eeac 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 ex of the.** dat
1eead 61 62 61 73 65 20 22 78 78 78 22 20 69 73 20 72 abase "xxx" is r
1eeae 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 eturned..*/.SQLI
1eeaf 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1eeb0 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d qlite3TwoPartNam
1eeb1 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
1eeb2 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 se, /* Pars
1eeb3 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e ing and code gen
1eeb4 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 erating context
1eeb5 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d */. Token *pNam
1eeb6 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 e1, /* The
1eeb7 22 78 78 78 22 20 69 6e 20 74 68 65 20 6e 61 6d "xxx" in the nam
1eeb8 65 20 22 78 78 78 2e 79 79 79 22 20 6f 72 20 22 e "xxx.yyy" or "
1eeb9 78 78 78 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 xxx" */. Token
1eeba 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a *pName2, /*
1eebb 20 54 68 65 20 22 79 79 79 22 20 69 6e 20 74 68 The "yyy" in th
1eebc 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 e name "xxx.yyy"
1eebd 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70 55 */. Token **pU
1eebe 6e 71 75 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 nqual /* Wri
1eebf 74 65 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 te the unqualifi
1eec0 65 64 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 68 ed object name h
1eec1 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
1eec2 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 iDb;
1eec3 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
1eec4 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 ase holding the
1eec5 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 object */. sqli
1eec6 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1eec7 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 4e 61 ->db;.. if( pNa
1eec8 6d 65 32 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e me2 && pName2->n
1eec9 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 >0 ){. if( db
1eeca 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20 7b 0a ->init.busy ) {.
1eecb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1eecc 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 orMsg(pParse, "c
1eecd 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 22 orrupt database"
1eece 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d );. pParse-
1eecf 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 >nErr++;. r
1eed0 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a eturn -1;. }.
1eed1 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 *pUnqual = p
1eed2 4e 61 6d 65 32 3b 0a 20 20 20 20 69 44 62 20 3d Name2;. iDb =
1eed3 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64 sqlite3FindDb(d
1eed4 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 b, pName1);.
1eed5 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 if( iDb<0 ){.
1eed6 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1eed7 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e sg(pParse, "unkn
1eed8 6f 77 6e 20 64 61 74 61 62 61 73 65 20 25 54 22 own database %T"
1eed9 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 , pName1);.
1eeda 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b pParse->nErr++;
1eedb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 . return -1
1eedc 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1eedd 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d . assert( db-
1eede 3e 69 6e 69 74 2e 69 44 62 3d 3d 30 20 7c 7c 20 >init.iDb==0 ||
1eedf 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b db->init.busy );
1eee0 0a 20 20 20 20 69 44 62 20 3d 20 64 62 2d 3e 69 . iDb = db->i
1eee1 6e 69 74 2e 69 44 62 3b 0a 20 20 20 20 2a 70 55 nit.iDb;. *pU
1eee2 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 31 3b 0a nqual = pName1;.
1eee3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 44 62 }. return iDb
1eee4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
1eee5 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
1eee6 74 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 to check if the
1eee7 55 54 46 2d 38 20 73 74 72 69 6e 67 20 7a 4e 61 UTF-8 string zNa
1eee8 6d 65 20 69 73 20 61 20 6c 65 67 61 6c 0a 2a 2a me is a legal.**
1eee9 20 75 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d unqualified nam
1eeea 65 20 66 6f 72 20 61 20 6e 65 77 20 73 63 68 65 e for a new sche
1eeeb 6d 61 20 6f 62 6a 65 63 74 20 28 74 61 62 6c 65 ma object (table
1eeec 2c 20 69 6e 64 65 78 2c 20 76 69 65 77 20 6f 72 , index, view or
1eeed 0a 2a 2a 20 74 72 69 67 67 65 72 29 2e 20 41 6c .** trigger). Al
1eeee 6c 20 6e 61 6d 65 73 20 61 72 65 20 6c 65 67 61 l names are lega
1eeef 6c 20 65 78 63 65 70 74 20 74 68 6f 73 65 20 74 l except those t
1eef0 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 74 hat begin with t
1eef1 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 73 71 he string.** "sq
1eef2 6c 69 74 65 5f 22 20 28 69 6e 20 75 70 70 65 72 lite_" (in upper
1eef3 2c 20 6c 6f 77 65 72 20 6f 72 20 6d 69 78 65 64 , lower or mixed
1eef4 20 63 61 73 65 29 2e 20 54 68 69 73 20 70 6f 72 case). This por
1eef5 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65 tion of the name
1eef6 73 70 61 63 65 0a 2a 2a 20 69 73 20 72 65 73 65 space.** is rese
1eef7 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 rved for interna
1eef8 6c 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 l use..*/.SQLITE
1eef9 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1eefa 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e ite3CheckObjectN
1eefb 61 6d 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 ame(Parse *pPars
1eefc 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
1eefd 4e 61 6d 65 29 7b 0a 20 20 69 66 28 20 21 70 50 Name){. if( !pP
1eefe 61 72 73 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 arse->db->init.b
1eeff 75 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e usy && pParse->n
1ef00 65 73 74 65 64 3d 3d 30 20 0a 20 20 20 20 20 20 ested==0 .
1ef01 20 20 20 20 26 26 20 28 70 50 61 72 73 65 2d 3e && (pParse->
1ef02 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 db->flags & SQLI
1ef03 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 29 3d TE_WriteSchema)=
1ef04 3d 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 =0. &&
1ef05 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 0==sqlite3StrNIC
1ef06 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 mp(zName, "sqlit
1ef07 65 5f 22 2c 20 37 29 20 29 7b 0a 20 20 20 20 73 e_", 7) ){. s
1ef08 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1ef09 50 61 72 73 65 2c 20 22 6f 62 6a 65 63 74 20 6e Parse, "object n
1ef0a 61 6d 65 20 72 65 73 65 72 76 65 64 20 66 6f 72 ame reserved for
1ef0b 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 3a 20 25 internal use: %
1ef0c 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 s", zName);.
1ef0d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1ef0e 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ROR;. }. retur
1ef0f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1ef10 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 63 6f 6e 73 /*.** Begin cons
1ef11 74 72 75 63 74 69 6e 67 20 61 20 6e 65 77 20 74 tructing a new t
1ef12 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 able representat
1ef13 69 6f 6e 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 ion in memory.
1ef14 54 68 69 73 20 69 73 0a 2a 2a 20 74 68 65 20 66 This is.** the f
1ef15 69 72 73 74 20 6f 66 20 73 65 76 65 72 61 6c 20 irst of several
1ef16 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 action routines
1ef17 74 68 61 74 20 67 65 74 20 63 61 6c 6c 65 64 20 that get called
1ef18 69 6e 20 72 65 73 70 6f 6e 73 65 0a 2a 2a 20 74 in response.** t
1ef19 6f 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 o a CREATE TABLE
1ef1a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 statement. In
1ef1b 70 61 72 74 69 63 75 6c 61 72 2c 20 74 68 69 73 particular, this
1ef1c 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
1ef1d 65 64 0a 2a 2a 20 61 66 74 65 72 20 73 65 65 69 ed.** after seei
1ef1e 6e 67 20 74 6f 6b 65 6e 73 20 22 43 52 45 41 54 ng tokens "CREAT
1ef1f 45 22 20 61 6e 64 20 22 54 41 42 4c 45 22 20 61 E" and "TABLE" a
1ef20 6e 64 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d nd the table nam
1ef21 65 2e 20 54 68 65 20 69 73 54 65 6d 70 0a 2a 2a e. The isTemp.**
1ef22 20 66 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 flag is true if
1ef23 20 74 68 65 20 74 61 62 6c 65 20 73 68 6f 75 6c the table shoul
1ef24 64 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 d be stored in t
1ef25 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 he auxiliary dat
1ef26 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 6e abase.** file in
1ef27 73 74 65 61 64 20 6f 66 20 69 6e 20 74 68 65 20 stead of in the
1ef28 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1ef29 6c 65 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 le. This is nor
1ef2a 6d 61 6c 6c 79 20 74 68 65 20 63 61 73 65 0a 2a mally the case.*
1ef2b 2a 20 77 68 65 6e 20 74 68 65 20 22 54 45 4d 50 * when the "TEMP
1ef2c 22 20 6f 72 20 22 54 45 4d 50 4f 52 41 52 59 22 " or "TEMPORARY"
1ef2d 20 6b 65 79 77 6f 72 64 20 6f 63 63 75 72 73 20 keyword occurs
1ef2e 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 43 52 in between.** CR
1ef2f 45 41 54 45 20 61 6e 64 20 54 41 42 4c 45 2e 0a EATE and TABLE..
1ef30 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 74 61 **.** The new ta
1ef31 62 6c 65 20 72 65 63 6f 72 64 20 69 73 20 69 6e ble record is in
1ef32 69 74 69 61 6c 69 7a 65 64 20 61 6e 64 20 70 75 itialized and pu
1ef33 74 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 t in pParse->pNe
1ef34 77 54 61 62 6c 65 2e 0a 2a 2a 20 41 73 20 6d 6f wTable..** As mo
1ef35 72 65 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 re of the CREATE
1ef36 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
1ef37 20 69 73 20 70 61 72 73 65 64 2c 20 61 64 64 69 is parsed, addi
1ef38 74 69 6f 6e 61 6c 20 61 63 74 69 6f 6e 0a 2a 2a tional action.**
1ef39 20 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20 62 routines will b
1ef3a 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 64 64 20 e called to add
1ef3b 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e more information
1ef3c 20 74 6f 20 74 68 69 73 20 72 65 63 6f 72 64 2e to this record.
1ef3d 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f .** At the end o
1ef3e 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 f the CREATE TAB
1ef3f 4c 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 LE statement, th
1ef40 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c e sqlite3EndTabl
1ef41 65 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 e() routine.** i
1ef42 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 s called to comp
1ef43 6c 65 74 65 20 74 68 65 20 63 6f 6e 73 74 72 75 lete the constru
1ef44 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 ction of the new
1ef45 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a table record..*
1ef46 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ef47 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 void sqlite3Sta
1ef48 72 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 rtTable(. Parse
1ef49 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 *pParse, /* P
1ef4a 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f arser context */
1ef4b 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 . Token *pName1
1ef4c 2c 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 , /* First par
1ef4d 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 t of the name of
1ef4e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 the table or vi
1ef4f 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 ew */. Token *p
1ef50 4e 61 6d 65 32 2c 20 20 20 2f 2a 20 53 65 63 6f Name2, /* Seco
1ef51 6e 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e nd part of the n
1ef52 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1ef53 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e or view */. in
1ef54 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 2f t isTemp, /
1ef55 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 * True if this i
1ef56 73 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 2a s a TEMP table *
1ef57 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20 /. int isView,
1ef58 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1ef59 74 68 69 73 20 69 73 20 61 20 56 49 45 57 20 2a this is a VIEW *
1ef5a 2f 0a 20 20 69 6e 74 20 69 73 56 69 72 74 75 61 /. int isVirtua
1ef5b 6c 2c 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 l, /* True if
1ef5c 74 68 69 73 20 69 73 20 61 20 56 49 52 54 55 41 this is a VIRTUA
1ef5d 4c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 L table */. int
1ef5e 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20 2f 2a noErr /*
1ef5f 20 44 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20 74 Do nothing if t
1ef60 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 able already exi
1ef61 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c sts */.){. Tabl
1ef62 65 20 2a 70 54 61 62 6c 65 3b 0a 20 20 63 68 61 e *pTable;. cha
1ef63 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 2f 2a r *zName = 0; /*
1ef64 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 The name of the
1ef65 20 6e 65 77 20 74 61 62 6c 65 20 2a 2f 0a 20 20 new table */.
1ef66 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
1ef67 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65 arse->db;. Vdbe
1ef68 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 *v;. int iDb;
1ef69 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
1ef6a 61 73 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 72 ase number to cr
1ef6b 65 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 69 eate the table i
1ef6c 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e n */. Token *pN
1ef6d 61 6d 65 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 ame; /* Unqua
1ef6e 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 lified name of t
1ef6f 68 65 20 74 61 62 6c 65 20 74 6f 20 63 72 65 61 he table to crea
1ef70 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 te */.. /* The
1ef71 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6e 61 table or view na
1ef72 6d 65 20 74 6f 20 63 72 65 61 74 65 20 69 73 20 me to create is
1ef73 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 72 passed to this r
1ef74 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e outine via token
1ef75 73 0a 20 20 2a 2a 20 70 4e 61 6d 65 31 20 61 6e s. ** pName1 an
1ef76 64 20 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 d pName2. If the
1ef77 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 table name was
1ef78 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c fully qualified,
1ef79 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20 20 for example:.
1ef7a 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54 **. ** CREATE T
1ef7b 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e 2e ABLE xxx.yyy (..
1ef7c 2e 29 3b 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 54 .);. ** . ** T
1ef7d 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 hen pName1 is se
1ef7e 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64 20 70 t to "xxx" and p
1ef7f 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 Name2 "yyy". On
1ef80 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 20 69 the other hand i
1ef81 66 0a 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 f. ** the table
1ef82 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c name is not ful
1ef83 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e ly qualified, i.
1ef84 65 2e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 e.:. **. ** CR
1ef85 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e EATE TABLE yyy(.
1ef86 2e 2e 29 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ..);. **. ** T
1ef87 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 hen pName1 is se
1ef88 74 20 74 6f 20 22 79 79 79 22 20 61 6e 64 20 70 t to "yyy" and p
1ef89 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a 20 20 2a Name2 is "".. *
1ef8a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 *. ** The call
1ef8b 62 65 6c 6f 77 20 73 65 74 73 20 74 68 65 20 70 below sets the p
1ef8c 4e 61 6d 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 Name pointer to
1ef8d 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f 6b point at the tok
1ef8e 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 20 20 en (pName1 or.
1ef8f 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20 ** pName2) that
1ef90 73 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61 stores the unqua
1ef91 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d lified table nam
1ef92 65 2e 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 e. The variable
1ef93 69 44 62 20 69 73 0a 20 20 2a 2a 20 73 65 74 20 iDb is. ** set
1ef94 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 to the index of
1ef95 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 the database tha
1ef96 74 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 t the table or v
1ef97 69 65 77 20 69 73 20 74 6f 20 62 65 0a 20 20 2a iew is to be. *
1ef98 2a 20 63 72 65 61 74 65 64 20 69 6e 2e 0a 20 20 * created in..
1ef99 2a 2f 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 */. iDb = sqlit
1ef9a 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 e3TwoPartName(pP
1ef9b 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e arse, pName1, pN
1ef9c 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 ame2, &pName);.
1ef9d 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 74 if( iDb<0 ) ret
1ef9e 75 72 6e 3b 0a 20 20 69 66 28 20 21 4f 4d 49 54 urn;. if( !OMIT
1ef9f 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d _TEMPDB && isTem
1efa0 70 20 26 26 20 69 44 62 3e 31 20 29 7b 0a 20 20 p && iDb>1 ){.
1efa1 20 20 2f 2a 20 49 66 20 63 72 65 61 74 69 6e 67 /* If creating
1efa2 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2c 20 74 a temp table, t
1efa3 68 65 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20 he name may not
1efa4 62 65 20 71 75 61 6c 69 66 69 65 64 20 2a 2f 0a be qualified */.
1efa5 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1efa6 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d Msg(pParse, "tem
1efa7 70 6f 72 61 72 79 20 74 61 62 6c 65 20 6e 61 6d porary table nam
1efa8 65 20 6d 75 73 74 20 62 65 20 75 6e 71 75 61 6c e must be unqual
1efa9 69 66 69 65 64 22 29 3b 0a 20 20 20 20 72 65 74 ified");. ret
1efaa 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 urn;. }. if( !
1efab 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 OMIT_TEMPDB && i
1efac 73 54 65 6d 70 20 29 20 69 44 62 20 3d 20 31 3b sTemp ) iDb = 1;
1efad 0a 0a 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d .. pParse->sNam
1efae 65 54 6f 6b 65 6e 20 3d 20 2a 70 4e 61 6d 65 3b eToken = *pName;
1efaf 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 . zName = sqlit
1efb0 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
1efb1 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 db, pName);. if
1efb2 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 ( zName==0 ) ret
1efb3 75 72 6e 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 urn;. if( SQLIT
1efb4 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 E_OK!=sqlite3Che
1efb5 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 ckObjectName(pPa
1efb6 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 rse, zName) ){.
1efb7 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 goto begin_ta
1efb8 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 ble_error;. }.
1efb9 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 if( db->init.iD
1efba 62 3d 3d 31 20 29 20 69 73 54 65 6d 70 20 3d 20 b==1 ) isTemp =
1efbb 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 1;.#ifndef SQLIT
1efbc 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 E_OMIT_AUTHORIZA
1efbd 54 49 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 28 TION. assert( (
1efbe 69 73 54 65 6d 70 20 26 20 31 29 3d 3d 69 73 54 isTemp & 1)==isT
1efbf 65 6d 70 20 29 3b 0a 20 20 7b 0a 20 20 20 20 69 emp );. {. i
1efc0 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 68 61 nt code;. cha
1efc1 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 r *zDb = db->aDb
1efc2 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 [iDb].zName;.
1efc3 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 if( sqlite3Auth
1efc4 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 Check(pParse, SQ
1efc5 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 LITE_INSERT, SCH
1efc6 45 4d 41 5f 54 41 42 4c 45 28 69 73 54 65 6d 70 EMA_TABLE(isTemp
1efc7 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 ), 0, zDb) ){.
1efc8 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 goto begin_t
1efc9 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 able_error;.
1efca 7d 0a 20 20 20 20 69 66 28 20 69 73 56 69 65 77 }. if( isView
1efcb 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f ){. if( !O
1efcc 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 MIT_TEMPDB && is
1efcd 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 Temp ){.
1efce 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 code = SQLITE_CR
1efcf 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 3b 0a EATE_TEMP_VIEW;.
1efd0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1efd1 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 code = SQLI
1efd2 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 3b 0a TE_CREATE_VIEW;.
1efd3 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
1efd4 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d e{. if( !OM
1efd5 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 IT_TEMPDB && isT
1efd6 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63 emp ){. c
1efd7 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 ode = SQLITE_CRE
1efd8 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a ATE_TEMP_TABLE;.
1efd9 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1efda 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 code = SQLI
1efdb 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 3b TE_CREATE_TABLE;
1efdc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1efdd 20 20 20 69 66 28 20 21 69 73 56 69 72 74 75 61 if( !isVirtua
1efde 6c 20 26 26 20 73 71 6c 69 74 65 33 41 75 74 68 l && sqlite3Auth
1efdf 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f Check(pParse, co
1efe0 64 65 2c 20 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 de, zName, 0, zD
1efe1 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f b) ){. goto
1efe2 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
1efe3 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 or;. }. }.#e
1efe4 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 ndif.. /* Make
1efe5 73 75 72 65 20 74 68 65 20 6e 65 77 20 74 61 62 sure the new tab
1efe6 6c 65 20 6e 61 6d 65 20 64 6f 65 73 20 6e 6f 74 le name does not
1efe7 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 61 6e collide with an
1efe8 20 65 78 69 73 74 69 6e 67 0a 20 20 2a 2a 20 69 existing. ** i
1efe9 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 6e 61 ndex or table na
1efea 6d 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 me in the same d
1efeb 61 74 61 62 61 73 65 2e 20 20 49 73 73 75 65 20 atabase. Issue
1efec 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1efed 20 69 66 0a 20 20 2a 2a 20 69 74 20 64 6f 65 73 if. ** it does
1efee 2e 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e 20 . The exception
1efef 69 73 20 69 66 20 74 68 65 20 73 74 61 74 65 6d is if the statem
1eff0 65 6e 74 20 62 65 69 6e 67 20 70 61 72 73 65 64 ent being parsed
1eff1 20 77 61 73 20 70 61 73 73 65 64 0a 20 20 2a 2a was passed. **
1eff2 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 64 to an sqlite3_d
1eff3 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 63 61 eclare_vtab() ca
1eff4 6c 6c 2e 20 49 6e 20 74 68 61 74 20 63 61 73 65 ll. In that case
1eff5 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e only the column
1eff6 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 61 6e 64 20 names. ** and
1eff7 74 79 70 65 73 20 77 69 6c 6c 20 62 65 20 75 73 types will be us
1eff8 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20 ed, so there is
1eff9 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74 20 no need to test
1effa 66 6f 72 20 6e 61 6d 65 73 70 61 63 65 0a 20 20 for namespace.
1effb 2a 2a 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 20 ** collisions..
1effc 20 2a 2f 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 */. if( !IN_DE
1effd 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 CLARE_VTAB ){.
1effe 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
1efff 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 =sqlite3ReadSche
1f000 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 ma(pParse) ){.
1f001 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 goto begin_t
1f002 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 able_error;.
1f003 7d 0a 20 20 20 20 70 54 61 62 6c 65 20 3d 20 73 }. pTable = s
1f004 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 qlite3FindTable(
1f005 64 62 2c 20 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 db, zName, db->a
1f006 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a Db[iDb].zName);.
1f007 20 20 20 20 69 66 28 20 70 54 61 62 6c 65 20 29 if( pTable )
1f008 7b 0a 20 20 20 20 20 20 69 66 28 20 21 6e 6f 45 {. if( !noE
1f009 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 rr ){. sq
1f00a 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1f00b 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 54 20 arse, "table %T
1f00c 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 2c already exists",
1f00d 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d pName);. }
1f00e 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 . goto begi
1f00f 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 n_table_error;.
1f010 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
1f011 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 ite3FindIndex(db
1f012 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 26 , zName, 0)!=0 &
1f013 26 20 28 69 44 62 3d 3d 30 20 7c 7c 20 21 64 62 & (iDb==0 || !db
1f014 2d 3e 69 6e 69 74 2e 62 75 73 79 29 20 29 7b 0a ->init.busy) ){.
1f015 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1f016 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 orMsg(pParse, "t
1f017 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
1f018 61 6e 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 25 an index named %
1f019 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 s", zName);.
1f01a 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 goto begin_tab
1f01b 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a le_error;. }.
1f01c 20 20 7d 0a 0a 20 20 70 54 61 62 6c 65 20 3d 20 }.. pTable =
1f01d 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
1f01e 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 ero(db, sizeof(T
1f01f 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 70 54 able));. if( pT
1f020 61 62 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 64 able==0 ){. d
1f021 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1f022 3d 20 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d = 1;. pParse-
1f023 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d >rc = SQLITE_NOM
1f024 45 4d 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e EM;. pParse->
1f025 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f nErr++;. goto
1f026 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 begin_table_err
1f027 6f 72 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 or;. }. pTable
1f028 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b ->zName = zName;
1f029 0a 20 20 70 54 61 62 6c 65 2d 3e 69 50 4b 65 79 . pTable->iPKey
1f02a 20 3d 20 2d 31 3b 0a 20 20 70 54 61 62 6c 65 2d = -1;. pTable-
1f02b 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 >pSchema = db->a
1f02c 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b Db[iDb].pSchema;
1f02d 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 20 . pTable->nRef
1f02e 3d 20 31 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 64 = 1;. pTable->d
1f02f 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20 70 50 b = db;. if( pP
1f030 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 arse->pNewTable
1f031 29 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 ) sqlite3DeleteT
1f032 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e 65 able(pParse->pNe
1f033 77 54 61 62 6c 65 29 3b 0a 20 20 70 50 61 72 73 wTable);. pPars
1f034 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 e->pNewTable = p
1f035 54 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 Table;.. /* If
1f036 74 68 69 73 20 69 73 20 74 68 65 20 6d 61 67 69 this is the magi
1f037 63 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 c sqlite_sequenc
1f038 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 e table used by
1f039 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 2c 0a 20 autoincrement,.
1f03a 20 2a 2a 20 74 68 65 6e 20 72 65 63 6f 72 64 20 ** then record
1f03b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 a pointer to thi
1f03c 73 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 6d s table in the m
1f03d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 ain database str
1f03e 75 63 74 75 72 65 0a 20 20 2a 2a 20 73 6f 20 74 ucture. ** so t
1f03f 68 61 74 20 49 4e 53 45 52 54 20 63 61 6e 20 66 hat INSERT can f
1f040 69 6e 64 20 74 68 65 20 74 61 62 6c 65 20 65 61 ind the table ea
1f041 73 69 6c 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 sily.. */.#ifnd
1f042 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1f043 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 69 UTOINCREMENT. i
1f044 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 f( !pParse->nest
1f045 65 64 20 26 26 20 73 74 72 63 6d 70 28 7a 4e 61 ed && strcmp(zNa
1f046 6d 65 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 75 me, "sqlite_sequ
1f047 65 6e 63 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 ence")==0 ){.
1f048 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 pTable->pSchema
1f049 2d 3e 70 53 65 71 54 61 62 20 3d 20 70 54 61 62 ->pSeqTab = pTab
1f04a 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a le;. }.#endif..
1f04b 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 /* Begin gener
1f04c 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74 ating the code t
1f04d 68 61 74 20 77 69 6c 6c 20 69 6e 73 65 72 74 20 hat will insert
1f04e 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 the table record
1f04f 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 53 into. ** the S
1f050 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 QLITE_MASTER tab
1f051 6c 65 2e 20 20 4e 6f 74 65 20 69 6e 20 70 61 72 le. Note in par
1f052 74 69 63 75 6c 61 72 20 74 68 61 74 20 77 65 20 ticular that we
1f053 6d 75 73 74 20 67 6f 20 61 68 65 61 64 0a 20 20 must go ahead.
1f054 2a 2a 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20 ** and allocate
1f055 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 the record numbe
1f056 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 r for the table
1f057 65 6e 74 72 79 20 6e 6f 77 2e 20 20 42 65 66 6f entry now. Befo
1f058 72 65 20 61 6e 79 0a 20 20 2a 2a 20 50 52 49 4d re any. ** PRIM
1f059 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 ARY KEY or UNIQU
1f05a 45 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 70 E keywords are p
1f05b 61 72 73 65 64 2e 20 20 54 68 6f 73 65 20 6b 65 arsed. Those ke
1f05c 79 77 6f 72 64 73 20 77 69 6c 6c 20 63 61 75 73 ywords will caus
1f05d 65 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 74 e. ** indices t
1f05e 6f 20 62 65 20 63 72 65 61 74 65 64 20 61 6e 64 o be created and
1f05f 20 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72 the table recor
1f060 64 20 6d 75 73 74 20 63 6f 6d 65 20 62 65 66 6f d must come befo
1f061 72 65 20 74 68 65 20 0a 20 20 2a 2a 20 69 6e 64 re the . ** ind
1f062 69 63 65 73 2e 20 20 48 65 6e 63 65 2c 20 74 68 ices. Hence, th
1f063 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 e record number
1f064 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 6d 75 for the table mu
1f065 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a st be allocated.
1f066 20 20 2a 2a 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 ** now.. */.
1f067 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 if( !db->init.b
1f068 75 73 79 20 26 26 20 28 76 20 3d 20 73 71 6c 69 usy && (v = sqli
1f069 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
1f06a 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e e))!=0 ){. in
1f06b 74 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20 66 69 t j1;. int fi
1f06c 6c 65 46 6f 72 6d 61 74 3b 0a 20 20 20 20 69 6e leFormat;. in
1f06d 74 20 72 65 67 31 2c 20 72 65 67 32 2c 20 72 65 t reg1, reg2, re
1f06e 67 33 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 g3;. sqlite3B
1f06f 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 eginWriteOperati
1f070 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 on(pParse, 0, iD
1f071 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c b);..#ifndef SQL
1f072 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1f073 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 69 73 TABLE. if( is
1f074 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 Virtual ){.
1f075 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f076 70 30 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 p0(v, OP_VBegin)
1f077 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a ;. }.#endif..
1f078 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 /* If the fi
1f079 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 65 6e le format and en
1f07a 63 6f 64 69 6e 67 20 69 6e 20 74 68 65 20 64 61 coding in the da
1f07b 74 61 62 61 73 65 20 68 61 76 65 20 6e 6f 74 20 tabase have not
1f07c 62 65 65 6e 20 73 65 74 2c 20 0a 20 20 20 20 2a been set, . *
1f07d 2a 20 73 65 74 20 74 68 65 6d 20 6e 6f 77 2e 0a * set them now..
1f07e 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 31 20 */. reg1
1f07f 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 = pParse->regRow
1f080 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e id = ++pParse->n
1f081 4d 65 6d 3b 0a 20 20 20 20 72 65 67 32 20 3d 20 Mem;. reg2 =
1f082 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 20 pParse->regRoot
1f083 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
1f084 3b 0a 20 20 20 20 72 65 67 33 20 3d 20 2b 2b 70 ;. reg3 = ++p
1f085 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 Parse->nMem;.
1f086 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f087 70 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f p3(v, OP_ReadCoo
1f088 6b 69 65 2c 20 69 44 62 2c 20 72 65 67 33 2c 20 kie, iDb, reg3,
1f089 31 29 3b 20 20 20 2f 2a 20 66 69 6c 65 5f 66 6f 1); /* file_fo
1f08a 72 6d 61 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 rmat */. sqli
1f08b 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 te3VdbeUsesBtree
1f08c 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 6a 31 (v, iDb);. j1
1f08d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
1f08e 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72 dOp1(v, OP_If, r
1f08f 65 67 33 29 3b 0a 20 20 20 20 66 69 6c 65 46 6f eg3);. fileFo
1f090 72 6d 61 74 20 3d 20 28 64 62 2d 3e 66 6c 61 67 rmat = (db->flag
1f091 73 20 26 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 s & SQLITE_Legac
1f092 79 46 69 6c 65 46 6d 74 29 21 3d 30 20 3f 0a 20 yFileFmt)!=0 ?.
1f093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f094 20 31 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 1 : SQLITE_MAX_
1f095 46 49 4c 45 5f 46 4f 52 4d 41 54 3b 0a 20 20 20 FILE_FORMAT;.
1f096 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f097 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
1f098 2c 20 66 69 6c 65 46 6f 72 6d 61 74 2c 20 72 65 , fileFormat, re
1f099 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 g3);. sqlite3
1f09a 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
1f09b 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c _SetCookie, iDb,
1f09c 20 31 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 1, reg3);. s
1f09d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
1f09e 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
1f09f 45 4e 43 28 64 62 29 2c 20 72 65 67 33 29 3b 0a ENC(db), reg3);.
1f0a0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1f0a1 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 ddOp3(v, OP_SetC
1f0a2 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 34 2c 20 72 ookie, iDb, 4, r
1f0a3 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 eg3);. sqlite
1f0a4 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
1f0a5 20 6a 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 j1);.. /* Th
1f0a6 69 73 20 6a 75 73 74 20 63 72 65 61 74 65 73 20 is just creates
1f0a7 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72 a place-holder r
1f0a8 65 63 6f 72 64 20 69 6e 20 74 68 65 20 73 71 6c ecord in the sql
1f0a9 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
1f0aa 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 63 .. ** The rec
1f0ab 6f 72 64 20 63 72 65 61 74 65 64 20 64 6f 65 73 ord created does
1f0ac 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79 not contain any
1f0ad 74 68 69 6e 67 20 79 65 74 2e 20 20 49 74 20 77 thing yet. It w
1f0ae 69 6c 6c 20 62 65 20 72 65 70 6c 61 63 65 64 0a ill be replaced.
1f0af 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 72 65 ** by the re
1f0b0 61 6c 20 65 6e 74 72 79 20 69 6e 20 63 6f 64 65 al entry in code
1f0b1 20 67 65 6e 65 72 61 74 65 64 20 61 74 20 73 71 generated at sq
1f0b2 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 29 2e lite3EndTable().
1f0b3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1f0b4 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 he rowid for the
1f0b5 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 6c 65 new entry is le
1f0b6 66 74 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 ft on the top of
1f0b7 20 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 the stack..
1f0b8 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 76 61 6c ** The rowid val
1f0b9 75 65 20 69 73 20 6e 65 65 64 65 64 20 62 79 20 ue is needed by
1f0ba 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 71 the code that sq
1f0bb 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 20 77 69 lite3EndTable wi
1f0bc 6c 6c 0a 20 20 20 20 2a 2a 20 67 65 6e 65 72 61 ll. ** genera
1f0bd 74 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 20 21 te.. */.#if !
1f0be 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1f0bf 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 MIT_VIEW) || !de
1f0c0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1f0c1 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a T_VIRTUALTABLE).
1f0c2 20 20 20 20 69 66 28 20 69 73 56 69 65 77 20 7c if( isView |
1f0c3 7c 20 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20 | isVirtual ){.
1f0c4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f0c5 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
1f0c6 65 67 65 72 2c 20 30 2c 20 72 65 67 32 29 3b 0a eger, 0, reg2);.
1f0c7 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 }else.#endif
1f0c8 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c . {. sql
1f0c9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1f0ca 2c 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 , OP_CreateTable
1f0cb 2c 20 69 44 62 2c 20 72 65 67 32 29 3b 0a 20 20 , iDb, reg2);.
1f0cc 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f }. sqlite3O
1f0cd 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70 penMasterTable(p
1f0ce 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 Parse, iDb);.
1f0cf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f0d0 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 p2(v, OP_NewRowi
1f0d1 64 2c 20 30 2c 20 72 65 67 31 29 3b 0a 20 20 20 d, 0, reg1);.
1f0d2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f0d3 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 p2(v, OP_Null, 0
1f0d4 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c , reg3);. sql
1f0d5 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1f0d6 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c 20 , OP_Insert, 0,
1f0d7 72 65 67 33 2c 20 72 65 67 31 29 3b 0a 20 20 20 reg3, reg1);.
1f0d8 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1f0d9 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 geP5(v, OPFLAG_A
1f0da 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 PPEND);. sqli
1f0db 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c te3VdbeAddOp0(v,
1f0dc 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a OP_Close);. }.
1f0dd 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e 6f . /* Normal (no
1f0de 6e 2d 65 72 72 6f 72 29 20 72 65 74 75 72 6e 2e n-error) return.
1f0df 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 20 */. return;..
1f0e0 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 /* If an error
1f0e1 6f 63 63 75 72 73 2c 20 77 65 20 6a 75 6d 70 20 occurs, we jump
1f0e2 68 65 72 65 20 2a 2f 0a 62 65 67 69 6e 5f 74 61 here */.begin_ta
1f0e3 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c ble_error:. sql
1f0e4 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1f0e5 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b Name);. return;
1f0e6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d .}../*.** This m
1f0e7 61 63 72 6f 20 69 73 20 75 73 65 64 20 74 6f 20 acro is used to
1f0e8 63 6f 6d 70 61 72 65 20 74 77 6f 20 73 74 72 69 compare two stri
1f0e9 6e 67 73 20 69 6e 20 61 20 63 61 73 65 2d 69 6e ngs in a case-in
1f0ea 73 65 6e 73 69 74 69 76 65 20 6d 61 6e 6e 65 72 sensitive manner
1f0eb 2e 0a 2a 2a 20 49 74 20 69 73 20 73 6c 69 67 68 ..** It is sligh
1f0ec 74 6c 79 20 66 61 73 74 65 72 20 74 68 61 6e 20 tly faster than
1f0ed 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 53 calling sqlite3S
1f0ee 74 72 49 43 6d 70 28 29 20 64 69 72 65 63 74 6c trICmp() directl
1f0ef 79 2c 20 62 75 74 0a 2a 2a 20 70 72 6f 64 75 63 y, but.** produc
1f0f0 65 73 20 6c 61 72 67 65 72 20 63 6f 64 65 2e 0a es larger code..
1f0f1 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 54 **.** WARNING: T
1f0f2 68 69 73 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 his macro is not
1f0f3 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 compatible with
1f0f4 20 74 68 65 20 73 74 72 63 6d 70 28 29 20 66 61 the strcmp() fa
1f0f5 6d 69 6c 79 2e 20 49 74 0a 2a 2a 20 72 65 74 75 mily. It.** retu
1f0f6 72 6e 73 20 74 72 75 65 20 69 66 20 74 68 65 20 rns true if the
1f0f7 74 77 6f 20 73 74 72 69 6e 67 73 20 61 72 65 20 two strings are
1f0f8 65 71 75 61 6c 2c 20 6f 74 68 65 72 77 69 73 65 equal, otherwise
1f0f9 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 false..*/.#defi
1f0fa 6e 65 20 53 54 52 49 43 4d 50 28 78 2c 20 79 29 ne STRICMP(x, y)
1f0fb 20 28 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 (\.sqlite3Upper
1f0fc 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e ToLower[*(unsign
1f0fd 65 64 20 63 68 61 72 20 2a 29 28 78 29 5d 3d 3d ed char *)(x)]==
1f0fe 20 20 20 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 \.sqlite3Uppe
1f0ff 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 rToLower[*(unsig
1f100 6e 65 64 20 63 68 61 72 20 2a 29 28 79 29 5d 20 ned char *)(y)]
1f101 20 20 20 20 5c 0a 26 26 20 73 71 6c 69 74 65 33 \.&& sqlite3
1f102 53 74 72 49 43 6d 70 28 28 78 29 2b 31 2c 28 79 StrICmp((x)+1,(y
1f103 29 2b 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a 2a )+1)==0 )../*.**
1f104 20 41 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d Add a new colum
1f105 6e 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 n to the table c
1f106 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 urrently being c
1f107 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2a 0a 2a onstructed..**.*
1f108 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c * The parser cal
1f109 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ls this routine
1f10a 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63 6f once for each co
1f10b 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e lumn declaration
1f10c 0a 2a 2a 20 69 6e 20 61 20 43 52 45 41 54 45 20 .** in a CREATE
1f10d 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e TABLE statement.
1f10e 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 sqlite3StartTa
1f10f 62 6c 65 28 29 20 67 65 74 73 20 63 61 6c 6c 65 ble() gets calle
1f110 64 0a 2a 2a 20 66 69 72 73 74 20 74 6f 20 67 65 d.** first to ge
1f111 74 20 74 68 69 6e 67 73 20 67 6f 69 6e 67 2e 20 t things going.
1f112 20 54 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 Then this routi
1f113 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 ne is called for
1f114 20 65 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e each.** column.
1f115 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1f116 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
1f117 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a ddColumn(Parse *
1f118 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 pParse, Token *p
1f119 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a Name){. Table *
1f11a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 p;. int i;. ch
1f11b 61 72 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20 ar *z;. Column
1f11c 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 *pCol;. sqlite3
1f11d 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
1f11e 62 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 b;. if( (p = pP
1f11f 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 arse->pNewTable)
1f120 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 ==0 ) return;.#i
1f121 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c f SQLITE_MAX_COL
1f122 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 6e 43 6f UMN. if( p->nCo
1f123 6c 2b 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 l+1>db->aLimit[S
1f124 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 QLITE_LIMIT_COLU
1f125 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 MN] ){. sqlit
1f126 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1f127 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c e, "too many col
1f128 75 6d 6e 73 20 6f 6e 20 25 73 22 2c 20 70 2d 3e umns on %s", p->
1f129 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 zName);. retu
1f12a 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 rn;. }.#endif.
1f12b 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 z = sqlite3Name
1f12c 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 FromToken(pParse
1f12d 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 ->db, pName);.
1f12e 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 if( z==0 ) retur
1f12f 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c n;. for(i=0; i<
1f130 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 p->nCol; i++){.
1f131 20 20 20 69 66 28 20 53 54 52 49 43 4d 50 28 7a if( STRICMP(z
1f132 2c 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 , p->aCol[i].zNa
1f133 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c me) ){. sql
1f134 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1f135 72 73 65 2c 20 22 64 75 70 6c 69 63 61 74 65 20 rse, "duplicate
1f136 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25 73 22 column name: %s"
1f137 2c 20 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , z);. sqli
1f138 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 te3DbFree(db, z)
1f139 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
1f13a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
1f13b 28 70 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 29 3d (p->nCol & 0x7)=
1f13c 3d 30 20 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e =0 ){. Column
1f13d 20 2a 61 4e 65 77 3b 0a 20 20 20 20 61 4e 65 77 *aNew;. aNew
1f13e 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
1f13f 6c 6f 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 70 loc(pParse->db,p
1f140 2d 3e 61 43 6f 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b ->aCol,(p->nCol+
1f141 38 29 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 8)*sizeof(p->aCo
1f142 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 l[0]));. if(
1f143 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 aNew==0 ){.
1f144 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1f145 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 b, z);. ret
1f146 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 urn;. }. p
1f147 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65 77 3b 0a 20 ->aCol = aNew;.
1f148 20 7d 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e }. pCol = &p->
1f149 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 aCol[p->nCol];.
1f14a 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c 20 30 2c memset(pCol, 0,
1f14b 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b sizeof(p->aCol[
1f14c 30 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 0]));. pCol->zN
1f14d 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20 2f 2a 20 ame = z;. . /*
1f14e 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 74 If there is no t
1f14f 79 70 65 20 73 70 65 63 69 66 69 65 64 2c 20 63 ype specified, c
1f150 6f 6c 75 6d 6e 73 20 68 61 76 65 20 74 68 65 20 olumns have the
1f151 64 65 66 61 75 6c 74 20 61 66 66 69 6e 69 74 79 default affinity
1f152 0a 20 20 2a 2a 20 27 4e 4f 4e 45 27 2e 20 49 66 . ** 'NONE'. If
1f153 20 74 68 65 72 65 20 69 73 20 61 20 74 79 70 65 there is a type
1f154 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e specified, then
1f155 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d sqlite3AddColum
1f156 6e 54 79 70 65 28 29 20 77 69 6c 6c 0a 20 20 2a nType() will. *
1f157 2a 20 62 65 20 63 61 6c 6c 65 64 20 6e 65 78 74 * be called next
1f158 20 74 6f 20 73 65 74 20 70 43 6f 6c 2d 3e 61 66 to set pCol->af
1f159 66 69 6e 69 74 79 20 63 6f 72 72 65 63 74 6c 79 finity correctly
1f15a 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 2d 3e 61 .. */. pCol->a
1f15b 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 ffinity = SQLITE
1f15c 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e _AFF_NONE;. p->
1f15d 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a nCol++;.}../*.**
1f15e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1f15f 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 called by the p
1f160 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 arser while in t
1f161 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 he middle of.**
1f162 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 parsing a CREATE
1f163 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 TABLE statement
1f164 2e 20 20 41 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 . A "NOT NULL"
1f165 63 6f 6e 73 74 72 61 69 6e 74 20 68 61 73 0a 2a constraint has.*
1f166 2a 20 62 65 65 6e 20 73 65 65 6e 20 6f 6e 20 61 * been seen on a
1f167 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 69 73 20 72 column. This r
1f168 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 outine sets the
1f169 6e 6f 74 4e 75 6c 6c 20 66 6c 61 67 20 6f 6e 0a notNull flag on.
1f16a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 75 ** the column cu
1f16b 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f rrently under co
1f16c 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 nstruction..*/.S
1f16d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1f16e 69 64 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 id sqlite3AddNot
1f16f 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 Null(Parse *pPar
1f170 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72 6f 72 29 se, int onError)
1f171 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 {. Table *p;.
1f172 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 28 70 20 int i;. if( (p
1f173 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 = pParse->pNewTa
1f174 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e ble)==0 ) return
1f175 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d ;. i = p->nCol-
1f176 31 3b 0a 20 20 69 66 28 20 69 3e 3d 30 20 29 20 1;. if( i>=0 )
1f177 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 p->aCol[i].notNu
1f178 6c 6c 20 3d 20 6f 6e 45 72 72 6f 72 3b 0a 7d 0a ll = onError;.}.
1f179 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 65 20 ./*.** Scan the
1f17a 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6e 61 6d 65 column type name
1f17b 20 7a 54 79 70 65 20 28 6c 65 6e 67 74 68 20 6e zType (length n
1f17c 54 79 70 65 29 20 61 6e 64 20 72 65 74 75 72 6e Type) and return
1f17d 20 74 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 the.** associat
1f17e 65 64 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 ed affinity type
1f17f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1f180 74 69 6e 65 20 64 6f 65 73 20 61 20 63 61 73 65 tine does a case
1f181 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 65 61 -independent sea
1f182 72 63 68 20 6f 66 20 7a 54 79 70 65 20 66 6f 72 rch of zType for
1f183 20 74 68 65 20 0a 2a 2a 20 73 75 62 73 74 72 69 the .** substri
1f184 6e 67 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f ngs in the follo
1f185 77 69 6e 67 20 74 61 62 6c 65 2e 20 49 66 20 6f wing table. If o
1f186 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 ne of the substr
1f187 69 6e 67 73 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 ings is.** found
1f188 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 , the correspond
1f189 69 6e 67 20 61 66 66 69 6e 69 74 79 20 69 73 20 ing affinity is
1f18a 72 65 74 75 72 6e 65 64 2e 20 49 66 20 7a 54 79 returned. If zTy
1f18b 70 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 6d pe contains.** m
1f18c 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 ore than one of
1f18d 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 2c 20 the substrings,
1f18e 65 6e 74 72 69 65 73 20 74 6f 77 61 72 64 20 74 entries toward t
1f18f 68 65 20 74 6f 70 20 6f 66 20 0a 2a 2a 20 74 68 he top of .** th
1f190 65 20 74 61 62 6c 65 20 74 61 6b 65 20 70 72 69 e table take pri
1f191 6f 72 69 74 79 2e 20 46 6f 72 20 65 78 61 6d 70 ority. For examp
1f192 6c 65 2c 20 69 66 20 7a 54 79 70 65 20 69 73 20 le, if zType is
1f193 27 42 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a 20 53 'BLOBINT', .** S
1f194 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
1f195 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a R is returned..*
1f196 2a 0a 2a 2a 20 53 75 62 73 74 72 69 6e 67 20 20 *.** Substring
1f197 20 20 20 7c 20 41 66 66 69 6e 69 74 79 0a 2a 2a | Affinity.**
1f198 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
1f199 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1f19a 2d 0a 2a 2a 20 27 49 4e 54 27 20 20 20 20 20 20 -.** 'INT'
1f19b 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f | SQLITE_AFF_
1f19c 49 4e 54 45 47 45 52 0a 2a 2a 20 27 43 48 41 52 INTEGER.** 'CHAR
1f19d 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 ' | SQLIT
1f19e 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 43 E_AFF_TEXT.** 'C
1f19f 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 LOB' | SQ
1f1a0 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a LITE_AFF_TEXT.**
1f1a1 20 27 54 45 58 54 27 20 20 20 20 20 20 20 20 7c 'TEXT' |
1f1a2 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 SQLITE_AFF_TEXT
1f1a3 0a 2a 2a 20 27 42 4c 4f 42 27 20 20 20 20 20 20 .** 'BLOB'
1f1a4 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e | SQLITE_AFF_N
1f1a5 4f 4e 45 0a 2a 2a 20 27 52 45 41 4c 27 20 20 20 ONE.** 'REAL'
1f1a6 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 | SQLITE_AF
1f1a7 46 5f 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f 41 27 F_REAL.** 'FLOA'
1f1a8 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 | SQLITE
1f1a9 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 44 4f _AFF_REAL.** 'DO
1f1aa 55 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c UB' | SQL
1f1ab 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 0a ITE_AFF_REAL.**.
1f1ac 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 ** If none of th
1f1ad 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20 e substrings in
1f1ae 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c 65 20 the above table
1f1af 61 72 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20 53 51 are found,.** SQ
1f1b0 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
1f1b1 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
1f1b2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f1b3 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 char sqlite3Affi
1f1b4 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20 54 nityType(const T
1f1b5 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 20 oken *pType){.
1f1b6 75 33 32 20 68 20 3d 20 30 3b 0a 20 20 63 68 61 u32 h = 0;. cha
1f1b7 72 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 r aff = SQLITE_A
1f1b8 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 63 6f FF_NUMERIC;. co
1f1b9 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
1f1ba 72 20 2a 7a 49 6e 20 3d 20 70 54 79 70 65 2d 3e r *zIn = pType->
1f1bb 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 z;. const unsig
1f1bc 6e 65 64 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d ned char *zEnd =
1f1bd 20 26 70 54 79 70 65 2d 3e 7a 5b 70 54 79 70 65 &pType->z[pType
1f1be 2d 3e 6e 5d 3b 0a 0a 20 20 77 68 69 6c 65 28 20 ->n];.. while(
1f1bf 7a 49 6e 21 3d 7a 45 6e 64 20 29 7b 0a 20 20 20 zIn!=zEnd ){.
1f1c0 20 68 20 3d 20 28 68 3c 3c 38 29 20 2b 20 73 71 h = (h<<8) + sq
1f1c1 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
1f1c2 72 5b 2a 7a 49 6e 5d 3b 0a 20 20 20 20 7a 49 6e r[*zIn];. zIn
1f1c3 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3d 3d 28 ++;. if( h==(
1f1c4 28 27 63 27 3c 3c 32 34 29 2b 28 27 68 27 3c 3c ('c'<<24)+('h'<<
1f1c5 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 72 27 16)+('a'<<8)+'r'
1f1c6 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 ) ){
1f1c7 20 2f 2a 20 43 48 41 52 20 2a 2f 0a 20 20 20 20 /* CHAR */.
1f1c8 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 aff = SQLITE_A
1f1c9 46 46 5f 54 45 58 54 3b 20 0a 20 20 20 20 7d 65 FF_TEXT; . }e
1f1ca 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 63 27 lse if( h==(('c'
1f1cb 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b <<24)+('l'<<16)+
1f1cc 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 29 7b ('o'<<8)+'b') ){
1f1cd 20 20 20 20 20 20 20 2f 2a 20 43 4c 4f 42 20 2a /* CLOB *
1f1ce 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 /. aff = SQ
1f1cf 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 LITE_AFF_TEXT;.
1f1d0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d }else if( h==
1f1d1 28 28 27 74 27 3c 3c 32 34 29 2b 28 27 65 27 3c (('t'<<24)+('e'<
1f1d2 3c 31 36 29 2b 28 27 78 27 3c 3c 38 29 2b 27 74 <16)+('x'<<8)+'t
1f1d3 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 54 ') ){ /* T
1f1d4 45 58 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 EXT */. aff
1f1d5 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 = SQLITE_AFF_TE
1f1d6 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 XT;. }else if
1f1d7 28 20 68 3d 3d 28 28 27 62 27 3c 3c 32 34 29 2b ( h==(('b'<<24)+
1f1d8 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c ('l'<<16)+('o'<<
1f1d9 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 8)+'b')
1f1da 20 2f 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 /* BLOB */.
1f1db 20 20 20 20 26 26 20 28 61 66 66 3d 3d 53 51 4c && (aff==SQL
1f1dc 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 ITE_AFF_NUMERIC
1f1dd 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 || aff==SQLITE_A
1f1de 46 46 5f 52 45 41 4c 29 20 29 7b 0a 20 20 20 20 FF_REAL) ){.
1f1df 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 aff = SQLITE_A
1f1e0 46 46 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64 65 66 FF_NONE;.#ifndef
1f1e1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
1f1e2 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 ATING_POINT.
1f1e3 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 }else if( h==(('
1f1e4 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 r'<<24)+('e'<<16
1f1e5 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c 27 29 20 )+('a'<<8)+'l')
1f1e6 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 4c /* REAL
1f1e7 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 */. && a
1f1e8 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ff==SQLITE_AFF_N
1f1e9 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 UMERIC ){.
1f1ea 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 aff = SQLITE_AFF
1f1eb 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 _REAL;. }else
1f1ec 20 69 66 28 20 68 3d 3d 28 28 27 66 27 3c 3c 32 if( h==(('f'<<2
1f1ed 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 4)+('l'<<16)+('o
1f1ee 27 3c 3c 38 29 2b 27 61 27 29 20 20 20 20 20 20 '<<8)+'a')
1f1ef 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f 0a 20 /* FLOA */.
1f1f0 20 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 && aff==S
1f1f1 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
1f1f2 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d C ){. aff =
1f1f3 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
1f1f4 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1f1f5 68 3d 3d 28 28 27 64 27 3c 3c 32 34 29 2b 28 27 h==(('d'<<24)+('
1f1f6 6f 27 3c 3c 31 36 29 2b 28 27 75 27 3c 3c 38 29 o'<<16)+('u'<<8)
1f1f7 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 20 2f +'b') /
1f1f8 2a 20 44 4f 55 42 20 2a 2f 0a 20 20 20 20 20 20 * DOUB */.
1f1f9 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 && aff==SQLITE
1f1fa 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a _AFF_NUMERIC ){.
1f1fb 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 aff = SQLI
1f1fc 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 23 65 6e TE_AFF_REAL;.#en
1f1fd 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 dif. }else if
1f1fe 28 20 28 68 26 30 78 30 30 46 46 46 46 46 46 29 ( (h&0x00FFFFFF)
1f1ff 3d 3d 28 28 27 69 27 3c 3c 31 36 29 2b 28 27 6e ==(('i'<<16)+('n
1f200 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 '<<8)+'t') ){
1f201 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20 20 20 20 20 /* INT */.
1f202 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 aff = SQLITE_AF
1f203 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 F_INTEGER;.
1f204 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1f205 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b }.. return aff;
1f206 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
1f207 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
1f208 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77 by the parser w
1f209 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 hile in the midd
1f20a 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 le of.** parsing
1f20b 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 a CREATE TABLE
1f20c 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 statement. The
1f20d 70 46 69 72 73 74 20 74 6f 6b 65 6e 20 69 73 20 pFirst token is
1f20e 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b the first.** tok
1f20f 65 6e 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e en in the sequen
1f210 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20 74 68 61 ce of tokens tha
1f211 74 20 64 65 73 63 72 69 62 65 20 74 68 65 20 74 t describe the t
1f212 79 70 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f ype of the.** co
1f213 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 lumn currently u
1f214 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f nder constructio
1f215 6e 2e 20 20 20 70 4c 61 73 74 20 69 73 20 74 68 n. pLast is th
1f216 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 e last token.**
1f217 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65 2e in the sequence.
1f218 20 20 55 73 65 20 74 68 69 73 20 69 6e 66 6f 72 Use this infor
1f219 6d 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 73 74 72 mation to constr
1f21a 75 63 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 uct a string.**
1f21b 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
1f21c 65 20 74 79 70 65 6e 61 6d 65 20 6f 66 20 74 68 e typename of th
1f21d 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 73 74 6f e column and sto
1f21e 72 65 20 74 68 61 74 20 73 74 72 69 6e 67 0a 2a re that string.*
1f21f 2a 20 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f 20 0a * in zType..*/ .
1f220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1f221 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f oid sqlite3AddCo
1f222 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 20 2a lumnType(Parse *
1f223 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 pParse, Token *p
1f224 54 79 70 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a Type){. Table *
1f225 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43 6f p;. int i;. Co
1f226 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 lumn *pCol;. sq
1f227 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 lite3 *db;.. if
1f228 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 ( (p = pParse->p
1f229 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 NewTable)==0 ) r
1f22a 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e eturn;. i = p->
1f22b 6e 43 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69 3c nCol-1;. if( i<
1f22c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 0 ) return;. pC
1f22d 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 69 5d ol = &p->aCol[i]
1f22e 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d ;. db = pParse-
1f22f 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 >db;. sqlite3Db
1f230 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a Free(db, pCol->z
1f231 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a Type);. pCol->z
1f232 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 Type = sqlite3Na
1f233 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 meFromToken(db,
1f234 70 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e pType);. pCol->
1f235 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 affinity = sqlit
1f236 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 e3AffinityType(p
1f237 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Type);.}../*.**
1f238 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 The expression i
1f239 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 s the default va
1f23a 6c 75 65 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 lue for the most
1f23b 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 recently added
1f23c 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 column.** of the
1f23d 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 table currently
1f23e 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 under construct
1f23f 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 61 75 ion..**.** Defau
1f240 6c 74 20 76 61 6c 75 65 20 65 78 70 72 65 73 73 lt value express
1f241 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 63 6f 6e ions must be con
1f242 73 74 61 6e 74 2e 20 20 52 61 69 73 65 20 61 6e stant. Raise an
1f243 20 65 78 63 65 70 74 69 6f 6e 20 69 66 20 74 68 exception if th
1f244 69 73 0a 2a 2a 20 69 73 20 6e 6f 74 20 74 68 65 is.** is not the
1f245 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 case..**.** Thi
1f246 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
1f247 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 led by the parse
1f248 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d r while in the m
1f249 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 iddle of.** pars
1f24a 69 6e 67 20 61 20 43 52 45 41 54 45 20 54 41 42 ing a CREATE TAB
1f24b 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f LE statement..*/
1f24c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f24d 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 void sqlite3AddD
1f24e 65 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 efaultValue(Pars
1f24f 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
1f250 2a 70 45 78 70 72 29 7b 0a 20 20 54 61 62 6c 65 *pExpr){. Table
1f251 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 *p;. Column *p
1f252 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a Col;. sqlite3 *
1f253 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
1f254 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 . if( (p = pPar
1f255 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 21 3d se->pNewTable)!=
1f256 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 20 3d 20 0 ){. pCol =
1f257 26 28 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f &(p->aCol[p->nCo
1f258 6c 2d 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 l-1]);. if( !
1f259 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e sqlite3ExprIsCon
1f25a 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 stantOrFunction(
1f25b 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 pExpr) ){.
1f25c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1f25d 70 50 61 72 73 65 2c 20 22 64 65 66 61 75 6c 74 pParse, "default
1f25e 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e value of column
1f25f 20 5b 25 73 5d 20 69 73 20 6e 6f 74 20 63 6f 6e [%s] is not con
1f260 73 74 61 6e 74 22 2c 0a 20 20 20 20 20 20 20 20 stant",.
1f261 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a pCol->zName);.
1f262 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1f263 20 45 78 70 72 20 2a 70 43 6f 70 79 3b 0a 20 20 Expr *pCopy;.
1f264 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
1f265 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e elete(db, pCol->
1f266 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43 pDflt);. pC
1f267 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 70 43 6f 70 ol->pDflt = pCop
1f268 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 y = sqlite3ExprD
1f269 75 70 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 up(db, pExpr);.
1f26a 20 20 20 20 20 69 66 28 20 70 43 6f 70 79 20 29 if( pCopy )
1f26b 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1f26c 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 3TokenCopy(db, &
1f26d 70 43 6f 70 79 2d 3e 73 70 61 6e 2c 20 26 70 45 pCopy->span, &pE
1f26e 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 xpr->span);.
1f26f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
1f270 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1f271 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 7d 0a e(db, pExpr);.}.
1f272 0a 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65 ./*.** Designate
1f273 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
1f274 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 for the table.
1f275 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 pList is a list
1f276 20 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 of names .** of
1f277 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f columns that fo
1f278 72 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b rm the primary k
1f279 65 79 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 ey. If pList is
1f27a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a NULL, then the.
1f27b 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 ** most recently
1f27c 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 added column of
1f27d 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 the table is th
1f27e 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a e primary key..*
1f27f 2a 0a 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e *.** A table can
1f280 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e have at most on
1f281 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 e primary key.
1f282 49 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 If the table alr
1f283 65 61 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72 eady has.** a pr
1f284 69 6d 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74 imary key (and t
1f285 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e his is the secon
1f286 64 20 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74 d primary key) t
1f287 68 65 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a hen create an.**
1f288 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 error..**.** If
1f289 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
1f28a 20 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 is on a single
1f28b 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74 column whose dat
1f28c 61 74 79 70 65 20 69 73 20 49 4e 54 45 47 45 52 atype is INTEGER
1f28d 2c 0a 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c ,.** then we wil
1f28e 6c 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 61 l try to use tha
1f28f 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 t column as the
1f290 72 6f 77 69 64 2e 20 20 53 65 74 20 74 68 65 20 rowid. Set the
1f291 54 61 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 Table.iPKey.** f
1f292 69 65 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c ield of the tabl
1f293 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 e under construc
1f294 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69 tion to be the i
1f295 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49 ndex of the.** I
1f296 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
1f297 45 59 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c EY column. Tabl
1f298 65 2e 69 50 4b 65 79 20 69 73 20 73 65 74 20 74 e.iPKey is set t
1f299 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 o -1 if there is
1f29a 0a 2a 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 .** no INTEGER P
1f29b 52 49 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a RIMARY KEY..**.*
1f29c 2a 20 49 66 20 74 68 65 20 6b 65 79 20 69 73 20 * If the key is
1f29d 6e 6f 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50 not an INTEGER P
1f29e 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e RIMARY KEY, then
1f29f 20 63 72 65 61 74 65 20 61 20 75 6e 69 71 75 65 create a unique
1f2a0 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68 .** index for th
1f2a1 65 20 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78 e key. No index
1f2a2 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 is created for
1f2a3 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY
1f2a4 4b 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f KEYs..*/.SQLITE_
1f2a5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1f2a6 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 ite3AddPrimaryKe
1f2a7 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 y(. Parse *pPar
1f2a8 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e se, /* Parsin
1f2a9 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 g context */. E
1f2aa 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 xprList *pList,
1f2ab 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c /* List of fiel
1f2ac 64 20 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e d names to be in
1f2ad 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f dexed */. int o
1f2ae 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 nError, /*
1f2af 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 What to do with
1f2b0 61 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e a uniqueness con
1f2b1 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61 flict */. int a
1f2b2 75 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 utoInc, /*
1f2b3 54 72 75 65 20 69 66 20 74 68 65 20 41 55 54 4f True if the AUTO
1f2b4 49 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 INCREMENT keywor
1f2b5 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a d is present */.
1f2b6 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 int sortOrder
1f2b7 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f /* SQLITE_SO
1f2b8 5f 41 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53 _ASC or SQLITE_S
1f2b9 4f 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 O_DESC */.){. T
1f2ba 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 able *pTab = pPa
1f2bb 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a rse->pNewTable;.
1f2bc 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 char *zType =
1f2bd 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 0;. int iCol =
1f2be 2d 31 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61 -1, i;. if( pTa
1f2bf 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 b==0 || IN_DECLA
1f2c0 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70 RE_VTAB ) goto p
1f2c1 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b rimary_key_exit;
1f2c2 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 . if( pTab->tab
1f2c3 46 6c 61 67 73 20 26 20 54 46 5f 48 61 73 50 72 Flags & TF_HasPr
1f2c4 69 6d 61 72 79 4b 65 79 20 29 7b 0a 20 20 20 20 imaryKey ){.
1f2c5 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1f2c6 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 pParse, . "
1f2c7 74 61 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73 table \"%s\" has
1f2c8 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70 more than one p
1f2c9 72 69 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61 rimary key", pTa
1f2ca 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 b->zName);. g
1f2cb 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f oto primary_key_
1f2cc 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 exit;. }. pTab
1f2cd 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 ->tabFlags |= TF
1f2ce 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a _HasPrimaryKey;.
1f2cf 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 if( pList==0 )
1f2d0 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 {. iCol = pTa
1f2d1 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 b->nCol - 1;.
1f2d2 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c pTab->aCol[iCol
1f2d3 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b ].isPrimKey = 1;
1f2d4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f . }else{. fo
1f2d5 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e r(i=0; i<pList->
1f2d6 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
1f2d7 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 for(iCol=0; i
1f2d8 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 Col<pTab->nCol;
1f2d9 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 iCol++){.
1f2da 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
1f2db 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e Cmp(pList->a[i].
1f2dc 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f zName, pTab->aCo
1f2dd 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d l[iCol].zName)==
1f2de 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 0 ){. b
1f2df 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
1f2e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
1f2e1 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f ( iCol<pTab->nCo
1f2e2 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 l ){. pTa
1f2e3 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 b->aCol[iCol].is
1f2e4 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20 PrimKey = 1;.
1f2e5 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1f2e6 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e f( pList->nExpr>
1f2e7 31 20 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 1 ) iCol = -1;.
1f2e8 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 }. if( iCol>=0
1f2e9 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e && iCol<pTab->n
1f2ea 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 Col ){. zType
1f2eb 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 = pTab->aCol[iC
1f2ec 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20 ol].zType;. }.
1f2ed 20 69 66 28 20 7a 54 79 70 65 20 26 26 20 73 71 if( zType && sq
1f2ee 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79 lite3StrICmp(zTy
1f2ef 70 65 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d pe, "INTEGER")==
1f2f0 30 0a 20 20 20 20 20 20 20 20 26 26 20 73 6f 72 0. && sor
1f2f1 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53 tOrder==SQLITE_S
1f2f2 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20 70 54 61 O_ASC ){. pTa
1f2f3 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b b->iPKey = iCol;
1f2f4 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f . pTab->keyCo
1f2f5 6e 66 20 3d 20 6f 6e 45 72 72 6f 72 3b 0a 20 20 nf = onError;.
1f2f6 20 20 61 73 73 65 72 74 28 20 61 75 74 6f 49 6e assert( autoIn
1f2f7 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e 63 3d c==0 || autoInc=
1f2f8 3d 31 20 29 3b 0a 20 20 20 20 70 54 61 62 2d 3e =1 );. pTab->
1f2f9 74 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 74 6f tabFlags |= auto
1f2fa 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 72 65 Inc*TF_Autoincre
1f2fb 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ment;. }else if
1f2fc 28 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 69 66 ( autoInc ){.#if
1f2fd 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1f2fe 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 _AUTOINCREMENT.
1f2ff 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1f300 73 67 28 70 50 61 72 73 65 2c 20 22 41 55 54 4f sg(pParse, "AUTO
1f301 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f 6e 6c INCREMENT is onl
1f302 79 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 6e 20 y allowed on an
1f303 22 0a 20 20 20 20 20 20 20 22 49 4e 54 45 47 45 ". "INTEGE
1f304 52 20 50 52 49 4d 41 52 59 20 4b 45 59 22 29 3b R PRIMARY KEY");
1f305 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b .#endif. }else{
1f306 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 . sqlite3Crea
1f307 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 teIndex(pParse,
1f308 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c 20 0, 0, 0, pList,
1f309 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c 20 73 onError, 0, 0, s
1f30a 6f 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a 20 20 ortOrder, 0);.
1f30b 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 7d pList = 0;. }
1f30c 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 ..primary_key_ex
1f30d 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 it:. sqlite3Exp
1f30e 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 rListDelete(pPar
1f30f 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a se->db, pList);.
1f310 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a return;.}../*.
1f311 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 45 ** Add a new CHE
1f312 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f CK constraint to
1f313 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 the table curre
1f314 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 ntly under const
1f315 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 ruction..*/.SQLI
1f316 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1f317 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 sqlite3AddCheckC
1f318 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 72 onstraint(. Par
1f319 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f se *pParse, /
1f31a 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
1f31b 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 68 t */. Expr *pCh
1f31c 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 20 eckExpr /* The
1f31d 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f 6e check expression
1f31e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 */.){. sqlite3
1f31f 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
1f320 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 b;.#ifndef SQLIT
1f321 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54 E_OMIT_CHECK. T
1f322 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 able *pTab = pPa
1f323 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a rse->pNewTable;.
1f324 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21 49 if( pTab && !I
1f325 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 N_DECLARE_VTAB )
1f326 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 43 48 45 {. /* The CHE
1f327 43 4b 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 CK expression mu
1f328 73 74 20 62 65 20 64 75 70 6c 69 63 61 74 65 64 st be duplicated
1f329 20 73 6f 20 74 68 61 74 20 74 6f 6b 65 6e 73 20 so that tokens
1f32a 72 65 66 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20 refer. ** to
1f32b 6d 61 6c 6c 6f 63 65 64 20 73 70 61 63 65 20 61 malloced space a
1f32c 6e 64 20 6e 6f 74 20 74 68 65 20 28 65 70 68 65 nd not the (ephe
1f32d 6d 65 72 61 6c 29 20 74 65 78 74 20 6f 66 20 74 meral) text of t
1f32e 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 0a he CREATE TABLE.
1f32f 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ** statement
1f330 20 2a 2f 0a 20 20 20 20 70 54 61 62 2d 3e 70 43 */. pTab->pC
1f331 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 78 heck = sqlite3Ex
1f332 70 72 41 6e 64 28 64 62 2c 20 70 54 61 62 2d 3e prAnd(db, pTab->
1f333 70 43 68 65 63 6b 2c 20 0a 20 20 20 20 20 20 20 pCheck, .
1f334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f335 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1f336 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 43 e3ExprDup(db, pC
1f337 68 65 63 6b 45 78 70 72 29 29 3b 0a 20 20 7d 0a heckExpr));. }.
1f338 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 #endif. sqlite3
1f339 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1f33a 43 68 65 63 6b 45 78 70 72 29 3b 0a 7d 0a 0a 2f CheckExpr);.}../
1f33b 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 6c *.** Set the col
1f33c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 lation function
1f33d 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 of the most rece
1f33e 6e 74 6c 79 20 70 61 72 73 65 64 20 74 61 62 6c ntly parsed tabl
1f33f 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 74 e column.** to t
1f340 68 65 20 43 6f 6c 6c 53 65 71 20 67 69 76 65 6e he CollSeq given
1f341 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f342 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1f343 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 AddCollateType(P
1f344 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f arse *pParse, To
1f345 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 ken *pToken){.
1f346 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 Table *p;. int
1f347 69 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c i;. char *zColl
1f348 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1f349 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20 * Dequoted name
1f34a 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 of collation seq
1f34b 75 65 6e 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 uence */. sqlit
1f34c 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 28 e3 *db;.. if( (
1f34d 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 p = pParse->pNew
1f34e 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 Table)==0 ) retu
1f34f 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f rn;. i = p->nCo
1f350 6c 2d 31 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 l-1;. db = pPar
1f351 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 se->db;. zColl
1f352 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f = sqlite3NameFro
1f353 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65 mToken(db, pToke
1f354 6e 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c n);. if( !zColl
1f355 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 ) return;.. if
1f356 28 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 ( sqlite3LocateC
1f357 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a ollSeq(pParse, z
1f358 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20 Coll, -1) ){.
1f359 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 Index *pIdx;.
1f35a 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f p->aCol[i].zCo
1f35b 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 0a 20 ll = zColl;. .
1f35c 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c /* If the col
1f35d 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65 64 20 umn is declared
1f35e 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49 4d 41 as "<name> PRIMA
1f35f 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45 20 3c RY KEY COLLATE <
1f360 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a 20 74 type>",. ** t
1f361 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d 61 79 hen an index may
1f362 20 68 61 76 65 20 62 65 65 6e 20 63 72 65 61 74 have been creat
1f363 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c 75 6d ed on this colum
1f364 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20 20 20 n before the.
1f365 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 ** collation ty
1f366 70 65 20 77 61 73 20 61 64 64 65 64 2e 20 43 6f pe was added. Co
1f367 72 72 65 63 74 20 74 68 69 73 20 69 66 20 69 74 rrect this if it
1f368 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 is the case..
1f369 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 49 64 */. for(pId
1f36a 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 x=p->pIndex; pId
1f36b 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e x; pIdx=pIdx->pN
1f36c 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 ext){. asse
1f36d 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d rt( pIdx->nColum
1f36e 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 n==1 );. if
1f36f 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e ( pIdx->aiColumn
1f370 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 [0]==i ){.
1f371 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 pIdx->azColl[0
1f372 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a ] = p->aCol[i].z
1f373 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Coll;. }.
1f374 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1f375 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1f376 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 7d b, zColl);. }.}
1f377 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
1f378 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
1f379 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 e collation sequ
1f37a 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62 61 73 ence for databas
1f37b 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a 2a 2a e native text.**
1f37c 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e 74 69 encoding identi
1f37d 66 69 65 64 20 62 79 20 74 68 65 20 73 74 72 69 fied by the stri
1f37e 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 ng zName, length
1f37f 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 nName..**.** If
1f380 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63 the requested c
1f381 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
1f382 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 e is not availab
1f383 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61 69 6c le, or not avail
1f384 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 able.** in the d
1f385 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 65 atabase native e
1f386 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63 6f 6c ncoding, the col
1f387 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 69 lation factory i
1f388 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 s invoked to.**
1f389 72 65 71 75 65 73 74 20 69 74 2e 20 49 66 20 74 request it. If t
1f38a 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 he collation fac
1f38b 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20 73 75 tory does not su
1f38c 70 70 6c 79 20 73 75 63 68 20 61 20 73 65 71 75 pply such a sequ
1f38d 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 ence,.** and the
1f38e 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 76 61 sequence is ava
1f38f 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65 ilable in anothe
1f390 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2c r text encoding,
1f391 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a 2a 2a then that is.**
1f392 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61 returned instea
1f393 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 76 d..**.** If no v
1f394 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 72 ersions of the r
1f395 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 equested collati
1f396 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61 72 65 ons sequence are
1f397 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 0a 2a available, or.*
1f398 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f 72 20 * another error
1f399 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 occurs, NULL is
1f39a 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 returned and an
1f39b 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 error message wr
1f39c 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 70 50 itten into.** pP
1f39d 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 arse..**.** This
1f39e 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 77 72 routine is a wr
1f39f 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c apper around sql
1f3a0 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 ite3FindCollSeq(
1f3a1 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 ). This routine
1f3a2 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 .** invokes the
1f3a3 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 collation factor
1f3a4 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 63 y if the named c
1f3a5 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 ollation cannot
1f3a6 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e 64 20 be found.** and
1f3a7 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72 generates an err
1f3a8 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 53 or message..*/.S
1f3a9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f QLITE_PRIVATE Co
1f3aa 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f llSeq *sqlite3Lo
1f3ab 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 cateCollSeq(Pars
1f3ac 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 e *pParse, const
1f3ad 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e char *zName, in
1f3ae 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 73 71 6c 69 t nName){. sqli
1f3af 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1f3b0 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63 20 3d ->db;. u8 enc =
1f3b1 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38 20 69 ENC(db);. u8 i
1f3b2 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e 69 6e nitbusy = db->in
1f3b3 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c 6c 53 it.busy;. CollS
1f3b4 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 70 43 eq *pColl;.. pC
1f3b5 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e oll = sqlite3Fin
1f3b6 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63 dCollSeq(db, enc
1f3b7 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 , zName, nName,
1f3b8 69 6e 69 74 62 75 73 79 29 3b 0a 20 20 69 66 28 initbusy);. if(
1f3b9 20 21 69 6e 69 74 62 75 73 79 20 26 26 20 28 21 !initbusy && (!
1f3ba 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c 6c 2d pColl || !pColl-
1f3bb 3e 78 43 6d 70 29 20 29 7b 0a 20 20 20 20 70 43 >xCmp) ){. pC
1f3bc 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 oll = sqlite3Get
1f3bd 43 6f 6c 6c 53 65 71 28 64 62 2c 20 70 43 6f 6c CollSeq(db, pCol
1f3be 6c 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 l, zName, nName)
1f3bf 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c ;. if( !pColl
1f3c0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 4e ){. if( nN
1f3c1 61 6d 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 ame<0 ){.
1f3c2 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 nName = sqlite3
1f3c3 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 6d 65 Strlen(db, zName
1f3c4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1f3c5 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1f3c6 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 (pParse, "no suc
1f3c7 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 h collation sequ
1f3c8 65 6e 63 65 3a 20 25 2e 2a 73 22 2c 20 6e 4e 61 ence: %.*s", nNa
1f3c9 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 me, zName);.
1f3ca 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 pColl = 0;.
1f3cb 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e }. }.. return
1f3cc 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a pColl;.}.../*.*
1f3cd 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
1f3ce 74 68 61 74 20 77 69 6c 6c 20 69 6e 63 72 65 6d that will increm
1f3cf 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 63 ent the schema c
1f3d0 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ookie..**.** The
1f3d1 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 schema cookie i
1f3d2 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
1f3d3 69 6e 65 20 77 68 65 6e 20 74 68 65 20 73 63 68 ine when the sch
1f3d4 65 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 ema for the.** d
1f3d5 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e atabase changes.
1f3d6 20 20 41 66 74 65 72 20 65 61 63 68 20 73 63 68 After each sch
1f3d7 65 6d 61 20 63 68 61 6e 67 65 2c 20 74 68 65 20 ema change, the
1f3d8 63 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a 2a 20 cookie value.**
1f3d9 63 68 61 6e 67 65 73 2e 20 20 57 68 65 6e 20 61 changes. When a
1f3da 20 70 72 6f 63 65 73 73 20 66 69 72 73 74 20 72 process first r
1f3db 65 61 64 73 20 74 68 65 20 73 63 68 65 6d 61 20 eads the schema
1f3dc 69 74 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a it records the.*
1f3dd 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 72 65 * cookie. There
1f3de 61 66 74 65 72 2c 20 77 68 65 6e 65 76 65 72 20 after, whenever
1f3df 69 74 20 67 6f 65 73 20 74 6f 20 61 63 63 65 73 it goes to acces
1f3e0 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a s the database,.
1f3e1 2a 2a 20 69 74 20 63 68 65 63 6b 73 20 74 68 65 ** it checks the
1f3e2 20 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b 65 20 cookie to make
1f3e3 73 75 72 65 20 74 68 65 20 73 63 68 65 6d 61 20 sure the schema
1f3e4 68 61 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 0a has not changed.
1f3e5 2a 2a 20 73 69 6e 63 65 20 69 74 20 77 61 73 20 ** since it was
1f3e6 6c 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a last read..**.**
1f3e7 20 54 68 69 73 20 70 6c 61 6e 20 69 73 20 6e 6f This plan is no
1f3e8 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 75 6c t completely bul
1f3e9 6c 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74 20 69 let-proof. It i
1f3ea 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 0a 2a s possible for.*
1f3eb 2a 20 74 68 65 20 73 63 68 65 6d 61 20 74 6f 20 * the schema to
1f3ec 63 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c 65 20 change multiple
1f3ed 74 69 6d 65 73 20 61 6e 64 20 66 6f 72 20 74 68 times and for th
1f3ee 65 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65 0a 2a e cookie to be.*
1f3ef 2a 20 73 65 74 20 62 61 63 6b 20 74 6f 20 70 72 * set back to pr
1f3f0 69 6f 72 20 76 61 6c 75 65 2e 20 20 42 75 74 20 ior value. But
1f3f1 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61 schema changes a
1f3f2 72 65 20 69 6e 66 72 65 71 75 65 6e 74 0a 2a 2a re infrequent.**
1f3f3 20 61 6e 64 20 74 68 65 20 70 72 6f 62 61 62 69 and the probabi
1f3f4 6c 69 74 79 20 6f 66 20 68 69 74 74 69 6e 67 20 lity of hitting
1f3f5 74 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69 65 20 the same cookie
1f3f6 76 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a value is only.**
1f3f7 20 31 20 63 68 61 6e 63 65 20 69 6e 20 32 5e 33 1 chance in 2^3
1f3f8 32 2e 20 20 53 6f 20 77 65 27 72 65 20 73 61 66 2. So we're saf
1f3f9 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53 51 4c e enough..*/.SQL
1f3fa 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1f3fb 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f sqlite3ChangeCo
1f3fc 6f 6b 69 65 28 50 61 72 73 65 20 2a 70 50 61 72 okie(Parse *pPar
1f3fd 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 se, int iDb){.
1f3fe 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 int r1 = sqlite3
1f3ff 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 GetTempReg(pPars
1f400 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 e);. sqlite3 *d
1f401 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1f402 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
1f403 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c se->pVdbe;. sql
1f404 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1f405 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 64 62 , OP_Integer, db
1f406 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 ->aDb[iDb].pSche
1f407 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 ma->schema_cooki
1f408 65 2b 31 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 e+1, r1);. sqli
1f409 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
1f40a 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 OP_SetCookie, i
1f40b 44 62 2c 20 30 2c 20 72 31 29 3b 0a 20 20 73 71 Db, 0, r1);. sq
1f40c 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
1f40d 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b Reg(pParse, r1);
1f40e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73 75 72 .}../*.** Measur
1f40f 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
1f410 63 68 61 72 61 63 74 65 72 73 20 6e 65 65 64 65 characters neede
1f411 64 20 74 6f 20 6f 75 74 70 75 74 20 74 68 65 20 d to output the
1f412 67 69 76 65 6e 0a 2a 2a 20 69 64 65 6e 74 69 66 given.** identif
1f413 69 65 72 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 ier. The number
1f414 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 returned includ
1f415 65 73 20 61 6e 79 20 71 75 6f 74 65 73 20 75 73 es any quotes us
1f416 65 64 0a 2a 2a 20 62 75 74 20 64 6f 65 73 20 6e ed.** but does n
1f417 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e ot include the n
1f418 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a ull terminator..
1f419 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 **.** The estima
1f41a 74 65 20 69 73 20 63 6f 6e 73 65 72 76 61 74 69 te is conservati
1f41b 76 65 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65 ve. It might be
1f41c 20 6c 61 72 67 65 72 20 74 68 61 74 20 77 68 61 larger that wha
1f41d 74 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 6e t is.** really n
1f41e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 eeded..*/.static
1f41f 20 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67 74 68 int identLength
1f420 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b (const char *z){
1f421 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 6f 72 28 . int n;. for(
1f422 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20 7a 2b n=0; *z; n++, z+
1f423 2b 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d +){. if( *z==
1f424 27 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a 20 20 '"' ){ n++; }.
1f425 7d 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b 20 32 }. return n + 2
1f426 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ;.}../*.** Write
1f427 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f an identifier o
1f428 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 nto the end of t
1f429 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 2e he given string.
1f42a 20 20 41 64 64 0a 2a 2a 20 71 75 6f 74 65 20 63 Add.** quote c
1f42b 68 61 72 61 63 74 65 72 73 20 61 73 20 6e 65 65 haracters as nee
1f42c 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ded..*/.static v
1f42d 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63 68 61 oid identPut(cha
1f42e 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78 2c r *z, int *pIdx,
1f42f 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 49 64 char *zSignedId
1f430 65 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 ent){. unsigned
1f431 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d 20 char *zIdent =
1f432 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 (unsigned char*)
1f433 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20 20 zSignedIdent;.
1f434 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 75 int i, j, needQu
1f435 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64 78 ote;. i = *pIdx
1f436 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 ;. for(j=0; zId
1f437 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 ent[j]; j++){.
1f438 20 20 69 66 28 20 21 69 73 61 6c 6e 75 6d 28 7a if( !isalnum(z
1f439 49 64 65 6e 74 5b 6a 5d 29 20 26 26 20 7a 49 64 Ident[j]) && zId
1f43a 65 6e 74 5b 6a 5d 21 3d 27 5f 27 20 29 20 62 72 ent[j]!='_' ) br
1f43b 65 61 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65 64 51 eak;. }. needQ
1f43c 75 6f 74 65 20 3d 20 20 7a 49 64 65 6e 74 5b 6a uote = zIdent[j
1f43d 5d 21 3d 30 20 7c 7c 20 69 73 64 69 67 69 74 28 ]!=0 || isdigit(
1f43e 7a 49 64 65 6e 74 5b 30 5d 29 0a 20 20 20 20 20 zIdent[0]).
1f43f 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
1f440 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f sqlite3KeywordCo
1f441 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29 21 3d 54 de(zIdent, j)!=T
1f442 4b 5f 49 44 3b 0a 20 20 69 66 28 20 6e 65 65 64 K_ID;. if( need
1f443 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d Quote ) z[i++] =
1f444 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b '"';. for(j=0;
1f445 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 zIdent[j]; j++)
1f446 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a {. z[i++] = z
1f447 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66 Ident[j];. if
1f448 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 ( zIdent[j]=='"'
1f449 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b ) z[i++] = '"';
1f44a 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51 . }. if( needQ
1f44b 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 uote ) z[i++] =
1f44c 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b '"';. z[i] = 0;
1f44d 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a . *pIdx = i;.}.
1f44e 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
1f44f 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 a CREATE TABLE s
1f450 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72 tatement appropr
1f451 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 69 76 iate for the giv
1f452 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65 en.** table. Me
1f453 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 mory to hold the
1f454 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 74 61 text of the sta
1f455 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e tement is obtain
1f456 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 ed.** from sqlit
1f457 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 eMalloc() and mu
1f458 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 st be freed by t
1f459 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
1f45a 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 ion..*/.static c
1f45b 68 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65 har *createTable
1f45c 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62 Stmt(sqlite3 *db
1f45d 2c 20 54 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20 , Table *p, int
1f45e 69 73 54 65 6d 70 29 7b 0a 20 20 69 6e 74 20 69 isTemp){. int i
1f45f 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a , k, n;. char *
1f460 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a zStmt;. char *z
1f461 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45 Sep, *zSep2, *zE
1f462 6e 64 2c 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e nd, *z;. Column
1f463 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b *pCol;. n = 0;
1f464 0a 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d . for(pCol = p-
1f465 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d >aCol, i=0; i<p-
1f466 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c >nCol; i++, pCol
1f467 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69 64 ++){. n += id
1f468 65 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e entLength(pCol->
1f469 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7a 20 3d 20 zName);. z =
1f46a 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a 20 20 20 pCol->zType;.
1f46b 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 if( z ){.
1f46c 6e 20 2b 3d 20 28 73 74 72 6c 65 6e 28 7a 29 20 n += (strlen(z)
1f46d 2b 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a + 1);. }. }.
1f46e 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 n += identLeng
1f46f 74 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 th(p->zName);.
1f470 69 66 28 20 6e 3c 35 30 20 29 7b 0a 20 20 20 20 if( n<50 ){.
1f471 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20 20 20 7a zSep = "";. z
1f472 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20 20 20 20 Sep2 = ",";.
1f473 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20 20 7d 65 zEnd = ")";. }e
1f474 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 lse{. zSep =
1f475 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 53 65 70 "\n ";. zSep
1f476 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20 20 20 2 = ",\n ";.
1f477 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a 20 zEnd = "\n)";.
1f478 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 2b 20 36 }. n += 35 + 6
1f479 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d *p->nCol;. zStm
1f47a 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f t = sqlite3Mallo
1f47b 63 28 20 6e 20 29 3b 0a 20 20 69 66 28 20 7a 53 c( n );. if( zS
1f47c 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 tmt==0 ){. db
1f47d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
1f47e 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 1;. return 0
1f47f 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
1f480 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74 6d snprintf(n, zStm
1f481 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
1f482 20 20 20 20 20 21 4f 4d 49 54 5f 54 45 4d 50 44 !OMIT_TEMPD
1f483 42 26 26 69 73 54 65 6d 70 20 3f 20 22 43 52 45 B&&isTemp ? "CRE
1f484 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 22 ATE TEMP TABLE "
1f485 3a 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 22 :"CREATE TABLE "
1f486 29 3b 0a 20 20 6b 20 3d 20 73 74 72 6c 65 6e 28 );. k = strlen(
1f487 7a 53 74 6d 74 29 3b 0a 20 20 69 64 65 6e 74 50 zStmt);. identP
1f488 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d ut(zStmt, &k, p-
1f489 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7a 53 74 6d 74 >zName);. zStmt
1f48a 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20 20 66 [k++] = '(';. f
1f48b 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c or(pCol=p->aCol,
1f48c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b i=0; i<p->nCol;
1f48d 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 i++, pCol++){.
1f48e 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1f48f 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b ntf(n-k, &zStmt[
1f490 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b k], zSep);. k
1f491 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 53 74 6d += strlen(&zStm
1f492 74 5b 6b 5d 29 3b 0a 20 20 20 20 7a 53 65 70 20 t[k]);. zSep
1f493 3d 20 7a 53 65 70 32 3b 0a 20 20 20 20 69 64 65 = zSep2;. ide
1f494 6e 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c ntPut(zStmt, &k,
1f495 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pCol->zName);.
1f496 20 20 20 69 66 28 20 28 7a 20 3d 20 70 43 6f 6c if( (z = pCol
1f497 2d 3e 7a 54 79 70 65 29 21 3d 30 20 29 7b 0a 20 ->zType)!=0 ){.
1f498 20 20 20 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 zStmt[k++]
1f499 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 61 73 73 = ' ';. ass
1f49a 65 72 74 28 20 28 69 6e 74 29 28 73 74 72 6c 65 ert( (int)(strle
1f49b 6e 28 7a 29 2b 6b 2b 31 29 3c 3d 6e 20 29 3b 0a n(z)+k+1)<=n );.
1f49c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1f49d 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 printf(n-k, &zSt
1f49e 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 29 3b mt[k], "%s", z);
1f49f 0a 20 20 20 20 20 20 6b 20 2b 3d 20 73 74 72 6c . k += strl
1f4a0 65 6e 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d en(z);. }. }
1f4a1 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
1f4a2 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b ntf(n-k, &zStmt[
1f4a3 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29 3b k], "%s", zEnd);
1f4a4 0a 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74 3b . return zStmt;
1f4a5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
1f4a6 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
1f4a7 20 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20 66 to report the f
1f4a8 69 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74 65 inal ")" that te
1f4a9 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43 52 rminates.** a CR
1f4aa 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 EATE TABLE state
1f4ab 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
1f4ac 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 table structure
1f4ad 74 68 61 74 20 6f 74 68 65 72 20 61 63 74 69 6f that other actio
1f4ae 6e 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 n routines have
1f4af 62 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a 2a been building.**
1f4b0 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 is added to the
1f4b1 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 internal hash t
1f4b2 61 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67 20 ables, assuming
1f4b3 6e 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a 2a no errors have.*
1f4b4 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a * occurred..**.*
1f4b5 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20 74 * An entry for t
1f4b6 68 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64 65 he table is made
1f4b7 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 74 in the master t
1f4b8 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75 6e able on disk, un
1f4b9 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73 20 less.** this is
1f4ba 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c a temporary tabl
1f4bb 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62 75 e or db->init.bu
1f4bc 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62 2d sy==1. When db-
1f4bd 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a 2a >init.busy==1.**
1f4be 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 it means we are
1f4bf 20 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c reading the sql
1f4c0 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
1f4c1 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 because we just
1f4c2 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f .** connected to
1f4c3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 the database or
1f4c4 20 62 65 63 61 75 73 65 20 74 68 65 20 73 71 6c because the sql
1f4c5 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 ite_master table
1f4c6 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c 79 has.** recently
1f4c7 20 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68 65 changed, so the
1f4c8 20 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20 entry for this
1f4c9 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 table already ex
1f4ca 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 ists in.** the s
1f4cb 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
1f4cc 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 le. We do not w
1f4cd 61 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69 74 ant to create it
1f4ce 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 again..**.** If
1f4cf 20 74 68 65 20 70 53 65 6c 65 63 74 20 61 72 67 the pSelect arg
1f4d0 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c ument is not NUL
1f4d1 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 L, it means that
1f4d2 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
1f4d3 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 was called to c
1f4d4 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 67 65 reate a table ge
1f4d5 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20 0a nerated from a .
1f4d6 2a 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 ** "CREATE TABLE
1f4d7 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20 2e ... AS SELECT .
1f4d8 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 .." statement.
1f4d9 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 The column names
1f4da 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 74 of.** the new t
1f4db 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68 20 able will match
1f4dc 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f the result set o
1f4dd 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a 2f f the SELECT..*/
1f4de 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f4df 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 54 void sqlite3EndT
1f4e0 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 able(. Parse *p
1f4e1 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 Parse,
1f4e2 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 /* Parse context
1f4e3 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 6f */. Token *pCo
1f4e4 6e 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ns, /*
1f4e5 20 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20 61 The ',' token a
1f4e6 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 63 6f fter the last co
1f4e7 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20 20 lumn defn. */.
1f4e8 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20 Token *pEnd,
1f4e9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
1f4ea 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20 69 inal ')' token i
1f4eb 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 n the CREATE TAB
1f4ec 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a LE */. Select *
1f4ed 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 20 20 pSelect
1f4ee 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20 61 /* Select from a
1f4ef 20 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53 20 "CREATE ... AS
1f4f0 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20 20 SELECT" */.){.
1f4f1 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69 Table *p;. sqli
1f4f2 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1f4f3 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b ->db;. int iDb;
1f4f4 0a 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d 30 .. if( (pEnd==0
1f4f5 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29 20 && pSelect==0)
1f4f6 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 || pParse->nErr
1f4f7 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
1f4f8 6c 65 64 20 29 20 7b 0a 20 20 20 20 72 65 74 75 led ) {. retu
1f4f9 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 50 rn;. }. p = pP
1f4fa 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b arse->pNewTable;
1f4fb 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
1f4fc 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 turn;.. assert(
1f4fd 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 !db->init.busy
1f4fe 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a 0a || !pSelect );..
1f4ff 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 iDb = sqlite3S
1f500 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c chemaToIndex(db,
1f501 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 p->pSchema);..#
1f502 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f503 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 65 IT_CHECK. /* Re
1f504 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 solve names in a
1f505 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 ll CHECK constra
1f506 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e int expressions.
1f507 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 . */. if( p->p
1f508 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72 63 Check ){. Src
1f509 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20 20 20 List sSrc;
1f50a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f50b 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f 72 Fake SrcList for
1f50c 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
1f50d 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f le */. NameCo
1f50e 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 ntext sNC;
1f50f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
1f510 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 50 e context for pP
1f511 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 arse->pNewTable
1f512 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 */.. memset(&
1f513 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 sNC, 0, sizeof(s
1f514 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 NC));. memset
1f515 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65 6f (&sSrc, 0, sizeo
1f516 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73 53 f(sSrc));. sS
1f517 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 rc.nSrc = 1;.
1f518 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d 65 sSrc.a[0].zName
1f519 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 = p->zName;.
1f51a 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62 20 sSrc.a[0].pTab
1f51b 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b = p;. sSrc.a[
1f51c 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0].iCursor = -1;
1f51d 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 . sNC.pParse
1f51e 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e = pParse;. sN
1f51f 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73 53 C.pSrcList = &sS
1f520 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43 68 rc;. sNC.isCh
1f521 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 eck = 1;. if(
1f522 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
1f523 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 xprNames(&sNC, p
1f524 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20 20 ->pCheck) ){.
1f525 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
1f526 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 . }.#endif /* !
1f527 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1f528 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 MIT_CHECK) */..
1f529 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69 /* If the db->i
1f52a 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 69 74 nit.busy is 1 it
1f52b 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 means we are re
1f52c 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f 66 ading the SQL of
1f52d 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c 69 f the. ** "sqli
1f52e 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 73 te_master" or "s
1f52f 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 qlite_temp_maste
1f530 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 r" table on the
1f531 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64 6f disk.. ** So do
1f532 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 not write to th
1f533 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20 45 e disk again. E
1f534 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74 20 xtract the root
1f535 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a page number. **
1f536 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 66 for the table f
1f537 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 rom the db->init
1f538 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e 20 .newTnum field.
1f539 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 (The page numbe
1f53a 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 r. ** should ha
1f53b 76 65 20 62 65 65 6e 20 70 75 74 20 74 68 65 72 ve been put ther
1f53c 65 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 4f e by the sqliteO
1f53d 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29 0a penCb routine.).
1f53e 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 */. if( db->i
1f53f 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 nit.busy ){.
1f540 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e p->tnum = db->in
1f541 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d 0a it.newTnum;. }.
1f542 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e 69 . /* If not ini
1f543 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e 20 tializing, then
1f544 63 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 20 create a record
1f545 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c for the new tabl
1f546 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53 51 e. ** in the SQ
1f547 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c LITE_MASTER tabl
1f548 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1f549 65 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e e. The record n
1f54a 75 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74 umber. ** for t
1f54b 68 65 20 6e 65 77 20 74 61 62 6c 65 20 65 6e 74 he new table ent
1f54c 72 79 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 ry should alread
1f54d 79 20 62 65 20 6f 6e 20 74 68 65 20 73 74 61 63 y be on the stac
1f54e 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 k.. **. ** If
1f54f 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f 52 this is a TEMPOR
1f550 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74 65 ARY table, write
1f551 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 the entry into
1f552 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 the auxiliary.
1f553 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 ** file instead
1f554 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e of into the main
1f555 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
1f556 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e */. if( !db->
1f557 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 init.busy ){.
1f558 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62 65 int n;. Vdbe
1f559 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a *v;. char *z
1f55a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69 65 Type; /* "vie
1f55b 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a 2f w" or "table" */
1f55c 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 . char *zType
1f55d 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20 6f 2; /* "VIEW" o
1f55e 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20 20 r "TABLE" */.
1f55f 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20 20 char *zStmt;
1f560 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 /* Text of the
1f561 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20 CREATE TABLE or
1f562 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 CREATE VIEW stat
1f563 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76 20 ement */.. v
1f564 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
1f565 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 (pParse);. if
1f566 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( v==0 ) return;
1f567 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 .. sqlite3Vdb
1f568 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c eAddOp1(v, OP_Cl
1f569 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a ose, 0);.. /*
1f56a 20 43 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74 Create the root
1f56b 70 61 67 65 20 66 6f 72 20 74 68 65 20 6e 65 77 page for the new
1f56c 20 74 61 62 6c 65 20 61 6e 64 20 70 75 73 68 20 table and push
1f56d 69 74 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 it onto the stac
1f56e 6b 2e 0a 20 20 20 20 2a 2a 20 41 20 76 69 65 77 k.. ** A view
1f56f 20 68 61 73 20 6e 6f 20 72 6f 6f 74 70 61 67 65 has no rootpage
1f570 2c 20 73 6f 20 6a 75 73 74 20 70 75 73 68 20 61 , so just push a
1f571 20 7a 65 72 6f 20 6f 6e 74 6f 20 74 68 65 20 73 zero onto the s
1f572 74 61 63 6b 20 66 6f 72 0a 20 20 20 20 2a 2a 20 tack for. **
1f573 76 69 65 77 73 2e 20 20 49 6e 69 74 69 61 6c 69 views. Initiali
1f574 7a 65 20 7a 54 79 70 65 20 61 74 20 74 68 65 20 ze zType at the
1f575 73 61 6d 65 20 74 69 6d 65 2e 0a 20 20 20 20 2a same time.. *
1f576 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 53 65 /. if( p->pSe
1f577 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 lect==0 ){.
1f578 20 2f 2a 20 41 20 72 65 67 75 6c 61 72 20 74 61 /* A regular ta
1f579 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79 ble */. zTy
1f57a 70 65 20 3d 20 22 74 61 62 6c 65 22 3b 0a 20 20 pe = "table";.
1f57b 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22 54 41 zType2 = "TA
1f57c 42 4c 45 22 3b 0a 23 69 66 6e 64 65 66 20 53 51 BLE";.#ifndef SQ
1f57d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 LITE_OMIT_VIEW.
1f57e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1f57f 2f 2a 20 41 20 76 69 65 77 20 2a 2f 0a 20 20 20 /* A view */.
1f580 20 20 20 7a 54 79 70 65 20 3d 20 22 76 69 65 77 zType = "view
1f581 22 3b 0a 20 20 20 20 20 20 7a 54 79 70 65 32 20 ";. zType2
1f582 3d 20 22 56 49 45 57 22 3b 0a 23 65 6e 64 69 66 = "VIEW";.#endif
1f583 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 . }.. /* I
1f584 66 20 74 68 69 73 20 69 73 20 61 20 43 52 45 41 f this is a CREA
1f585 54 45 20 54 41 42 4c 45 20 78 78 20 41 53 20 53 TE TABLE xx AS S
1f586 45 4c 45 43 54 20 2e 2e 2e 2c 20 65 78 65 63 75 ELECT ..., execu
1f587 74 65 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 te the SELECT.
1f588 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 ** statement t
1f589 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e o populate the n
1f58a 65 77 20 74 61 62 6c 65 2e 20 54 68 65 20 72 6f ew table. The ro
1f58b 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 66 ot-page number f
1f58c 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 65 or the. ** ne
1f58d 77 20 74 61 62 6c 65 20 69 73 20 6f 6e 20 74 68 w table is on th
1f58e 65 20 74 6f 70 20 6f 66 20 74 68 65 20 76 64 62 e top of the vdb
1f58f 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2a 0a e stack.. **.
1f590 20 20 20 20 2a 2a 20 4f 6e 63 65 20 74 68 65 20 ** Once the
1f591 53 45 4c 45 43 54 20 68 61 73 20 62 65 65 6e 20 SELECT has been
1f592 63 6f 64 65 64 20 62 79 20 73 71 6c 69 74 65 33 coded by sqlite3
1f593 53 65 6c 65 63 74 28 29 2c 20 69 74 20 69 73 20 Select(), it is
1f594 69 6e 20 61 0a 20 20 20 20 2a 2a 20 73 75 69 74 in a. ** suit
1f595 61 62 6c 65 20 73 74 61 74 65 20 74 6f 20 71 75 able state to qu
1f596 65 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 ery for the colu
1f597 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74 79 70 mn names and typ
1f598 65 73 20 74 6f 20 62 65 20 75 73 65 64 0a 20 20 es to be used.
1f599 20 20 2a 2a 20 62 79 20 74 68 65 20 6e 65 77 20 ** by the new
1f59a 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 table.. **.
1f59b 20 20 2a 2a 20 41 20 73 68 61 72 65 64 2d 63 61 ** A shared-ca
1f59c 63 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 che write-lock i
1f59d 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 s not required t
1f59e 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 6e o write to the n
1f59f 65 77 20 74 61 62 6c 65 2c 0a 20 20 20 20 2a 2a ew table,. **
1f5a0 20 61 73 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 as a schema-loc
1f5a1 6b 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72 65 k must have alre
1f5a2 61 64 79 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 ady been obtaine
1f5a3 64 20 74 6f 20 63 72 65 61 74 65 20 69 74 2e 20 d to create it.
1f5a4 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 61 20 73 Since. ** a s
1f5a5 63 68 65 6d 61 2d 6c 6f 63 6b 20 65 78 63 6c 75 chema-lock exclu
1f5a6 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 64 61 des all other da
1f5a7 74 61 62 61 73 65 20 75 73 65 72 73 2c 20 74 68 tabase users, th
1f5a8 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 6f 75 e write-lock wou
1f5a9 6c 64 0a 20 20 20 20 2a 2a 20 62 65 20 72 65 64 ld. ** be red
1f5aa 75 6e 64 61 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 undant.. */.
1f5ab 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 if( pSelect )
1f5ac 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 {. SelectDe
1f5ad 73 74 20 64 65 73 74 3b 0a 20 20 20 20 20 20 54 st dest;. T
1f5ae 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 0a 0a able *pSelTab;..
1f5af 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 assert(pPa
1f5b0 72 73 65 2d 3e 6e 54 61 62 3d 3d 30 29 3b 0a 20 rse->nTab==0);.
1f5b1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f5b2 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 AddOp3(v, OP_Ope
1f5b3 6e 57 72 69 74 65 2c 20 31 2c 20 70 50 61 72 73 nWrite, 1, pPars
1f5b4 65 2d 3e 72 65 67 52 6f 6f 74 2c 20 69 44 62 29 e->regRoot, iDb)
1f5b5 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1f5b6 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 31 dbeChangeP5(v, 1
1f5b7 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d );. pParse-
1f5b8 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20 20 20 >nTab = 2;.
1f5b9 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
1f5ba 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 stInit(&dest, SR
1f5bb 54 5f 54 61 62 6c 65 2c 20 31 29 3b 0a 20 20 20 T_Table, 1);.
1f5bc 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 sqlite3Select
1f5bd 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 (pParse, pSelect
1f5be 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 , &dest);.
1f5bf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1f5c0 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 31 1(v, OP_Close, 1
1f5c1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 );. if( pPa
1f5c2 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a rse->nErr==0 ){.
1f5c3 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 20 pSelTab
1f5c4 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 = sqlite3ResultS
1f5c5 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 etOfSelect(pPars
1f5c6 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 e, pSelect);.
1f5c7 20 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 if( pSelTab
1f5c8 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
1f5c9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
1f5ca 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 >aCol==0 );.
1f5cb 20 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20 70 53 p->nCol = pS
1f5cc 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 elTab->nCol;.
1f5cd 20 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 70 p->aCol = p
1f5ce 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 SelTab->aCol;.
1f5cf 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e pSelTab->n
1f5d0 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 Col = 0;.
1f5d1 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d pSelTab->aCol =
1f5d2 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 0;. sqli
1f5d3 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 te3DeleteTable(p
1f5d4 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 7d SelTab);. }
1f5d5 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 . }.. /* C
1f5d6 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c ompute the compl
1f5d7 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 ete text of the
1f5d8 43 52 45 41 54 45 20 73 74 61 74 65 6d 65 6e 74 CREATE statement
1f5d9 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 65 6c */. if( pSel
1f5da 65 63 74 20 29 7b 0a 20 20 20 20 20 20 7a 53 74 ect ){. zSt
1f5db 6d 74 20 3d 20 63 72 65 61 74 65 54 61 62 6c 65 mt = createTable
1f5dc 53 74 6d 74 28 64 62 2c 20 70 2c 20 70 2d 3e 70 Stmt(db, p, p->p
1f5dd 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b Schema==db->aDb[
1f5de 31 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 1].pSchema);.
1f5df 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 20 }else{. n
1f5e0 3d 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72 = pEnd->z - pPar
1f5e1 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a se->sNameToken.z
1f5e2 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53 74 6d + 1;. zStm
1f5e3 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e t = sqlite3MPrin
1f5e4 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20 tf(db, .
1f5e5 20 20 22 43 52 45 41 54 45 20 25 73 20 25 2e 2a "CREATE %s %.*
1f5e6 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20 70 s", zType2, n, p
1f5e7 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 Parse->sNameToke
1f5e8 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 n.z. );.
1f5e9 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f }.. /* A slo
1f5ea 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 t for the record
1f5eb 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
1f5ec 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74 n allocated in t
1f5ed 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 he . ** SQLIT
1f5ee 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 E_MASTER table.
1f5ef 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f We just need to
1f5f0 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c 6f update that slo
1f5f1 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a t with all. *
1f5f2 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f * the informatio
1f5f3 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 65 n we've collecte
1f5f4 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 66 6f d. The rowid fo
1f5f5 72 20 74 68 65 20 70 72 65 61 6c 6c 6f 63 61 74 r the preallocat
1f5f6 65 64 0a 20 20 20 20 2a 2a 20 73 6c 6f 74 20 69 ed. ** slot i
1f5f7 73 20 74 68 65 20 32 6e 64 20 69 74 65 6d 20 6f s the 2nd item o
1f5f8 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 n the stack. Th
1f5f9 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 e top of the sta
1f5fa 63 6b 20 69 73 20 74 68 65 0a 20 20 20 20 2a 2a ck is the. **
1f5fb 20 72 6f 6f 74 20 70 61 67 65 20 66 6f 72 20 74 root page for t
1f5fc 68 65 20 6e 65 77 20 74 61 62 6c 65 20 28 6f 72 he new table (or
1f5fd 20 61 20 30 20 69 66 20 74 68 69 73 20 69 73 20 a 0 if this is
1f5fe 61 20 76 69 65 77 29 2e 0a 20 20 20 20 2a 2f 0a a view).. */.
1f5ff 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 sqlite3Neste
1f600 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 dParse(pParse,.
1f601 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e "UPDATE %Q.
1f602 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22 53 %s ". "S
1f603 45 54 20 74 79 70 65 3d 27 25 73 27 2c 20 6e 61 ET type='%s', na
1f604 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d me=%Q, tbl_name=
1f605 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d 23 25 64 %Q, rootpage=#%d
1f606 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20 20 20 , sql=%Q ".
1f607 20 20 22 57 48 45 52 45 20 72 6f 77 69 64 3d 23 "WHERE rowid=#
1f608 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 %d",. db->a
1f609 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 Db[iDb].zName, S
1f60a 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 CHEMA_TABLE(iDb)
1f60b 2c 0a 20 20 20 20 20 20 7a 54 79 70 65 2c 0a 20 ,. zType,.
1f60c 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 p->zName,.
1f60d 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 p->zName,.
1f60e 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67 pParse->reg
1f60f 52 6f 6f 74 2c 0a 20 20 20 20 20 20 7a 53 74 6d Root,. zStm
1f610 74 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d t,. pParse-
1f611 3e 72 65 67 52 6f 77 69 64 0a 20 20 20 20 29 3b >regRowid. );
1f612 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1f613 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 20 ee(db, zStmt);.
1f614 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 sqlite3Change
1f615 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 Cookie(pParse, i
1f616 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 Db);..#ifndef SQ
1f617 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
1f618 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 43 CREMENT. /* C
1f619 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 heck to see if w
1f61a 65 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 e need to create
1f61b 20 61 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65 an sqlite_seque
1f61c 6e 63 65 20 74 61 62 6c 65 20 66 6f 72 0a 20 20 nce table for.
1f61d 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20 74 72 61 ** keeping tra
1f61e 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63 72 65 6d ck of autoincrem
1f61f 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20 20 2a 2f ent keys.. */
1f620 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 61 62 46 . if( p->tabF
1f621 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e lags & TF_Autoin
1f622 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 crement ){.
1f623 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e Db *pDb = &db->
1f624 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 20 20 aDb[iDb];.
1f625 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 if( pDb->pSchema
1f626 2d 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 7b 0a ->pSeqTab==0 ){.
1f627 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e sqlite3N
1f628 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 estedParse(pPars
1f629 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 e,. "CR
1f62a 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73 71 EATE TABLE %Q.sq
1f62b 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 lite_sequence(na
1f62c 6d 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 20 20 me,seq)",.
1f62d 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 0a 20 pDb->zName.
1f62e 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
1f62f 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }. }.#endif..
1f630 20 20 20 20 2f 2a 20 52 65 70 61 72 73 65 20 65 /* Reparse e
1f631 76 65 72 79 74 68 69 6e 67 20 74 6f 20 75 70 64 verything to upd
1f632 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c ate our internal
1f633 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 data structures
1f634 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 */. sqlite3V
1f635 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
1f636 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 ParseSchema, iDb
1f637 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 , 0, 0,.
1f638 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1f639 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 27 25 71 b, "tbl_name='%q
1f63a 27 22 2c 70 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 '",p->zName), P4
1f63b 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 0a _DYNAMIC);. }..
1f63c 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 74 61 . /* Add the ta
1f63d 62 6c 65 20 74 6f 20 74 68 65 20 69 6e 2d 6d 65 ble to the in-me
1f63e 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 mory representat
1f63f 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 62 ion of the datab
1f640 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ase.. */. if(
1f641 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 db->init.busy &&
1f642 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 pParse->nErr==0
1f643 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 ){. Table *p
1f644 4f 6c 64 3b 0a 20 20 20 20 46 4b 65 79 20 2a 70 Old;. FKey *p
1f645 46 4b 65 79 3b 20 0a 20 20 20 20 53 63 68 65 6d FKey; . Schem
1f646 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d 3e a *pSchema = p->
1f647 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 4f 6c pSchema;. pOl
1f648 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 d = sqlite3HashI
1f649 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e nsert(&pSchema->
1f64a 74 62 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d tblHash, p->zNam
1f64b 65 2c 20 73 74 72 6c 65 6e 28 70 2d 3e 7a 4e 61 e, strlen(p->zNa
1f64c 6d 65 29 2b 31 2c 70 29 3b 0a 20 20 20 20 69 66 me)+1,p);. if
1f64d 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 ( pOld ){.
1f64e 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64 20 assert( p==pOld
1f64f 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 ); /* Malloc mu
1f650 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 69 st have failed i
1f651 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72 74 nside HashInsert
1f652 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e () */. db->
1f653 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
1f654 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
1f655 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
1f656 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 LITE_OMIT_FOREIG
1f657 4e 5f 4b 45 59 0a 20 20 20 20 66 6f 72 28 70 46 N_KEY. for(pF
1f658 4b 65 79 3d 70 2d 3e 70 46 4b 65 79 3b 20 70 46 Key=p->pFKey; pF
1f659 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79 Key; pFKey=pFKey
1f65a 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 ->pNextFrom){.
1f65b 20 20 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 0a void *data;.
1f65c 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20 int nTo =
1f65d 73 74 72 6c 65 6e 28 70 46 4b 65 79 2d 3e 7a 54 strlen(pFKey->zT
1f65e 6f 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 46 o) + 1;. pF
1f65f 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 73 Key->pNextTo = s
1f660 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 qlite3HashFind(&
1f661 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 pSchema->aFKey,
1f662 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e 54 6f 29 pFKey->zTo, nTo)
1f663 3b 0a 20 20 20 20 20 20 64 61 74 61 20 3d 20 73 ;. data = s
1f664 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 qlite3HashInsert
1f665 28 26 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 (&pSchema->aFKey
1f666 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e 54 , pFKey->zTo, nT
1f667 6f 2c 20 70 46 4b 65 79 29 3b 0a 20 20 20 20 20 o, pFKey);.
1f668 20 69 66 28 20 64 61 74 61 3d 3d 28 76 6f 69 64 if( data==(void
1f669 20 2a 29 70 46 4b 65 79 20 29 7b 0a 20 20 20 20 *)pFKey ){.
1f66a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
1f66b 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 iled = 1;.
1f66c 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
1f66d 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 pParse->pNewT
1f66e 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 64 62 able = 0;. db
1f66f 2d 3e 6e 54 61 62 6c 65 2b 2b 3b 0a 20 20 20 20 ->nTable++;.
1f670 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c db->flags |= SQL
1f671 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 ITE_InternChange
1f672 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 s;..#ifndef SQLI
1f673 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 TE_OMIT_ALTERTAB
1f674 4c 45 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 70 LE. if( !p->p
1f675 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 Select ){.
1f676 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
1f677 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 e = (const char
1f678 2a 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 *)pParse->sNameT
1f679 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 69 6e oken.z;. in
1f67a 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 61 t nName;. a
1f67b 73 73 65 72 74 28 20 21 70 53 65 6c 65 63 74 20 ssert( !pSelect
1f67c 26 26 20 70 43 6f 6e 73 20 26 26 20 70 45 6e 64 && pCons && pEnd
1f67d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 );. if( pC
1f67e 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 ons->z==0 ){.
1f67f 20 20 20 20 20 70 43 6f 6e 73 20 3d 20 70 45 6e pCons = pEn
1f680 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 d;. }.
1f681 20 6e 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 nName = (const
1f682 63 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20 char *)pCons->z
1f683 2d 20 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 70 - zName;. p
1f684 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d ->addColOffset =
1f685 20 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74 66 13 + sqlite3Utf
1f686 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 8CharLen(zName,
1f687 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65 nName);. }.#e
1f688 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e ndif. }.}..#ifn
1f689 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f68a 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 VIEW./*.** The p
1f68b 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 arser calls this
1f68c 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 routine in orde
1f68d 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 r to create a ne
1f68e 77 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 45 w VIEW.*/.SQLITE
1f68f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1f690 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28 lite3CreateView(
1f691 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1f692 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 , /* The par
1f693 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
1f694 20 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c Token *pBegin,
1f695 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 /* The CREA
1f696 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 TE token that be
1f697 67 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65 gins the stateme
1f698 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 nt */. Token *p
1f699 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68 Name1, /* Th
1f69a 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c e token that hol
1f69b 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 ds the name of t
1f69c 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b he view */. Tok
1f69d 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 en *pName2,
1f69e 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61 /* The token tha
1f69f 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 t holds the name
1f6a0 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a of the view */.
1f6a1 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 Select *pSelec
1f6a2 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 t, /* A SELECT
1f6a3 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 statement that
1f6a4 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 will become the
1f6a5 6e 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e new view */. in
1f6a6 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20 t isTemp,
1f6a7 20 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20 54 /* TRUE for a T
1f6a8 45 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a 2f EMPORARY view */
1f6a9 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 . int noErr
1f6aa 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73 /* Suppres
1f6ab 73 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 s error messages
1f6ac 20 69 66 20 56 49 45 57 20 61 6c 72 65 61 64 79 if VIEW already
1f6ad 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 exists */.){.
1f6ae 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 Table *p;. int
1f6af 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 n;. const unsig
1f6b0 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 54 ned char *z;. T
1f6b1 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 44 62 46 oken sEnd;. DbF
1f6b2 69 78 65 72 20 73 46 69 78 3b 0a 20 20 54 6f 6b ixer sFix;. Tok
1f6b3 65 6e 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e 74 en *pName;. int
1f6b4 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 iDb;. sqlite3
1f6b5 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1f6b6 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d ;.. if( pParse-
1f6b7 3e 6e 56 61 72 3e 30 20 29 7b 0a 20 20 20 20 73 >nVar>0 ){. s
1f6b8 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1f6b9 50 61 72 73 65 2c 20 22 70 61 72 61 6d 65 74 65 Parse, "paramete
1f6ba 72 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 rs are not allow
1f6bb 65 64 20 69 6e 20 76 69 65 77 73 22 29 3b 0a 20 ed in views");.
1f6bc 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 sqlite3Select
1f6bd 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 Delete(db, pSele
1f6be 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b ct);. return;
1f6bf 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 . }. sqlite3St
1f6c0 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c artTable(pParse,
1f6c1 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c pName1, pName2,
1f6c2 20 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e isTemp, 1, 0, n
1f6c3 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20 70 50 61 oErr);. p = pPa
1f6c4 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a rse->pNewTable;.
1f6c5 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50 if( p==0 || pP
1f6c6 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 arse->nErr ){.
1f6c7 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 sqlite3SelectD
1f6c8 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 elete(db, pSelec
1f6c9 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a t);. return;.
1f6ca 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 54 77 6f }. sqlite3Two
1f6cb 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c PartName(pParse,
1f6cc 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c pName1, pName2,
1f6cd 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20 &pName);. iDb
1f6ce 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
1f6cf 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 oIndex(db, p->pS
1f6d0 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 73 71 chema);. if( sq
1f6d1 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 lite3FixInit(&sF
1f6d2 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c ix, pParse, iDb,
1f6d3 20 22 76 69 65 77 22 2c 20 70 4e 61 6d 65 29 0a "view", pName).
1f6d4 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 69 && sqlite3Fi
1f6d5 78 53 65 6c 65 63 74 28 26 73 46 69 78 2c 20 70 xSelect(&sFix, p
1f6d6 53 65 6c 65 63 74 29 0a 20 20 29 7b 0a 20 20 20 Select). ){.
1f6d7 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
1f6d8 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 lete(db, pSelect
1f6d9 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
1f6da 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 20 }.. /* Make a
1f6db 63 6f 70 79 20 6f 66 20 74 68 65 20 65 6e 74 69 copy of the enti
1f6dc 72 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d re SELECT statem
1f6dd 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 ent that defines
1f6de 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2a 20 the view.. **
1f6df 54 68 69 73 20 77 69 6c 6c 20 66 6f 72 63 65 20 This will force
1f6e0 61 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b all the Expr.tok
1f6e1 65 6e 2e 7a 20 76 61 6c 75 65 73 20 74 6f 20 62 en.z values to b
1f6e2 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 e dynamically.
1f6e3 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 72 61 74 ** allocated rat
1f6e4 68 65 72 20 74 68 61 6e 20 70 6f 69 6e 74 20 74 her than point t
1f6e5 6f 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 o the input stri
1f6e6 6e 67 20 2d 20 77 68 69 63 68 20 6d 65 61 6e 73 ng - which means
1f6e7 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 79 20 that. ** they
1f6e8 77 69 6c 6c 20 70 65 72 73 69 73 74 20 61 66 74 will persist aft
1f6e9 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 er the current s
1f6ea 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 qlite3_exec() ca
1f6eb 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f ll returns.. */
1f6ec 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 . p->pSelect =
1f6ed 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 sqlite3SelectDup
1f6ee 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 (db, pSelect);.
1f6ef 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
1f6f0 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 lete(db, pSelect
1f6f1 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c );. if( db->mal
1f6f2 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1f6f3 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
1f6f4 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 f( !db->init.bus
1f6f5 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 y ){. sqlite3
1f6f6 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d ViewGetColumnNam
1f6f7 65 73 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 es(pParse, p);.
1f6f8 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 }.. /* Locate
1f6f9 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 the end of the C
1f6fa 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65 REATE VIEW state
1f6fb 6d 65 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64 ment. Make sEnd
1f6fc 20 70 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74 point to. ** t
1f6fd 68 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 he end.. */. s
1f6fe 45 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c End = pParse->sL
1f6ff 61 73 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 astToken;. if(
1f700 73 45 6e 64 2e 7a 5b 30 5d 21 3d 30 20 26 26 20 sEnd.z[0]!=0 &&
1f701 73 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 sEnd.z[0]!=';' )
1f702 7b 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 {. sEnd.z +=
1f703 73 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 sEnd.n;. }. sE
1f704 6e 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 nd.n = 0;. n =
1f705 73 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e 2d sEnd.z - pBegin-
1f706 3e 7a 3b 0a 20 20 7a 20 3d 20 28 63 6f 6e 73 74 >z;. z = (const
1f707 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 unsigned char*)
1f708 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 69 pBegin->z;. whi
1f709 6c 65 28 20 6e 3e 30 20 26 26 20 28 7a 5b 6e 2d le( n>0 && (z[n-
1f70a 31 5d 3d 3d 27 3b 27 20 7c 7c 20 69 73 73 70 61 1]==';' || isspa
1f70b 63 65 28 7a 5b 6e 2d 31 5d 29 29 20 29 7b 20 6e ce(z[n-1])) ){ n
1f70c 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d --; }. sEnd.z =
1f70d 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 &z[n-1];. sEnd
1f70e 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 .n = 1;.. /* Us
1f70f 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c e sqlite3EndTabl
1f710 65 28 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 e() to add the v
1f711 69 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 iew to the SQLIT
1f712 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a E_MASTER table *
1f713 2f 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 /. sqlite3EndTa
1f714 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 ble(pParse, 0, &
1f715 73 45 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 sEnd, 0);. retu
1f716 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 rn;.}.#endif /*
1f717 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
1f718 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 */..#if !define
1f719 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
1f71a 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 EW) || !defined(
1f71b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1f71c 55 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 UALTABLE)./*.**
1f71d 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 The Table struct
1f71e 75 72 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 ure pTable is re
1f71f 61 6c 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 ally a VIEW. Fi
1f720 6c 6c 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 ll in the names
1f721 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e of.** the column
1f722 73 20 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e s of the view in
1f723 20 74 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 the pTable stru
1f724 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 cture. Return t
1f725 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 he number.** of
1f726 65 72 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 errors. If an e
1f727 72 72 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 rror is seen lea
1f728 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ve an error mess
1f729 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a age in pParse->z
1f72a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 ErrMsg..*/.SQLIT
1f72b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1f72c 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 lite3ViewGetColu
1f72d 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70 mnNames(Parse *p
1f72e 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 Parse, Table *pT
1f72f 61 62 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a able){. Table *
1f730 70 53 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 pSelTab; /* A
1f731 66 61 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 fake table from
1f732 77 68 69 63 68 20 77 65 20 67 65 74 20 74 68 65 which we get the
1f733 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
1f734 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 Select *pSel;
1f735 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 /* Copy of th
1f736 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d e SELECT that im
1f737 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 plements the vie
1f738 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 w */. int nErr
1f739 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 = 0; /* Numb
1f73a 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 er of errors enc
1f73b 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e ountered */. in
1f73c 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 t n;
1f73d 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 /* Temporarily h
1f73e 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20 olds the number
1f73f 6f 66 20 63 75 72 73 6f 72 73 20 61 73 73 69 67 of cursors assig
1f740 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ned */. sqlite3
1f741 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
1f742 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 b; /* Database
1f743 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d connection for m
1f744 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a alloc errors */.
1f745 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 int (*xAuth)(v
1f746 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 oid*,int,const c
1f747 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
1f748 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
1f749 73 74 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73 st char*);.. as
1f74a 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a sert( pTable );.
1f74b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f74c 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1f74d 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 E. if( sqlite3V
1f74e 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 tabCallConnect(p
1f74f 50 61 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29 Parse, pTable) )
1f750 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1f751 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 ITE_ERROR;. }.
1f752 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 if( IsVirtual(p
1f753 54 61 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20 Table) ) return
1f754 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 0;.#endif..#ifnd
1f755 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1f756 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 IEW. /* A posit
1f757 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 ive nCol means t
1f758 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 he columns names
1f759 20 66 6f 72 20 74 68 69 73 20 76 69 65 77 20 61 for this view a
1f75a 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 re. ** already
1f75b 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 known.. */. if
1f75c 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 ( pTable->nCol>0
1f75d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 ) return 0;..
1f75e 2f 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43 /* A negative nC
1f75f 6f 6c 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 ol is a special
1f760 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74 marker meaning t
1f761 68 61 74 20 77 65 20 61 72 65 20 63 75 72 72 65 hat we are curre
1f762 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67 ntly. ** trying
1f763 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 to compute the
1f764 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 column names. I
1f765 66 20 77 65 20 65 6e 74 65 72 20 74 68 69 73 20 f we enter this
1f766 72 6f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a routine with. *
1f767 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f * a negative nCo
1f768 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20 l, it means two
1f769 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f or more views fo
1f76a 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 rm a loop, like
1f76b 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 this:. **. **
1f76c 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 CREATE VIEW
1f76d 6f 6e 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20 one AS SELECT *
1f76e 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 FROM two;. **
1f76f 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 74 CREATE VIEW t
1f770 77 6f 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 wo AS SELECT * F
1f771 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 ROM one;. **.
1f772 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 69 ** Actually, thi
1f773 73 20 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 s error is caugh
1f774 74 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6e 64 t previously and
1f775 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e so the followin
1f776 67 20 74 65 73 74 0a 20 20 2a 2a 20 73 68 6f 75 g test. ** shou
1f777 6c 64 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 ld always fail.
1f778 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 But we will lea
1f779 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 6a ve it in place j
1f77a 75 73 74 20 74 6f 20 62 65 20 73 61 66 65 2e 0a ust to be safe..
1f77b 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c */. if( pTabl
1f77c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 e->nCol<0 ){.
1f77d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1f77e 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 20 25 (pParse, "view %
1f77f 73 20 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20 s is circularly
1f780 64 65 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65 defined", pTable
1f781 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 ->zName);. re
1f782 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 turn 1;. }. as
1f783 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43 sert( pTable->nC
1f784 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 ol>=0 );.. /* I
1f785 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61 f we get this fa
1f786 72 2c 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e r, it means we n
1f787 65 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 eed to compute t
1f788 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a he table names..
1f789 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 ** Note that t
1f78a 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 he call to sqlit
1f78b 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c e3ResultSetOfSel
1f78c 65 63 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e ect() will expan
1f78d 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65 d any. ** "*" e
1f78e 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72 lements in the r
1f78f 65 73 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68 esults set of th
1f790 65 20 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20 e view and will
1f791 61 73 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20 assign cursors.
1f792 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65 ** to the eleme
1f793 6e 74 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 nts of the FROM
1f794 63 6c 61 75 73 65 2e 20 20 42 75 74 20 77 65 20 clause. But we
1f795 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73 do not want thes
1f796 65 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 e changes. ** t
1f797 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 o be permanent.
1f798 20 53 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74 So the computat
1f799 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 ion is done on a
1f79a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c copy of the SEL
1f79b 45 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 ECT. ** stateme
1f79c 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 nt that defines
1f79d 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 the view.. */.
1f79e 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d assert( pTable-
1f79f 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53 >pSelect );. pS
1f7a0 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 el = sqlite3Sele
1f7a1 63 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 ctDup(db, pTable
1f7a2 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 ->pSelect);. if
1f7a3 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 6e 20 ( pSel ){. n
1f7a4 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a = pParse->nTab;.
1f7a5 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 sqlite3SrcLi
1f7a6 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 stAssignCursors(
1f7a7 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70 53 pParse, pSel->pS
1f7a8 72 63 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d rc);. pTable-
1f7a9 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 23 69 66 6e >nCol = -1;.#ifn
1f7aa 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f7ab 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 AUTHORIZATION.
1f7ac 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 xAuth = db->xA
1f7ad 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41 75 uth;. db->xAu
1f7ae 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53 65 6c th = 0;. pSel
1f7af 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 Tab = sqlite3Res
1f7b0 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 ultSetOfSelect(p
1f7b1 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a 20 20 Parse, pSel);.
1f7b2 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 db->xAuth = xA
1f7b3 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20 20 70 uth;.#else. p
1f7b4 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 SelTab = sqlite3
1f7b5 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 ResultSetOfSelec
1f7b6 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b t(pParse, pSel);
1f7b7 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61 72 .#endif. pPar
1f7b8 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20 20 se->nTab = n;.
1f7b9 20 20 69 66 28 20 70 53 65 6c 54 61 62 20 29 7b if( pSelTab ){
1f7ba 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1f7bb 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29 Table->aCol==0 )
1f7bc 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e ;. pTable->
1f7bd 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e nCol = pSelTab->
1f7be 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61 62 nCol;. pTab
1f7bf 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 le->aCol = pSelT
1f7c0 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 ab->aCol;.
1f7c1 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 pSelTab->nCol =
1f7c2 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62 0;. pSelTab
1f7c3 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 ->aCol = 0;.
1f7c4 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 sqlite3DeleteT
1f7c5 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a 20 able(pSelTab);.
1f7c6 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 pTable->pSc
1f7c7 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 44 hema->flags |= D
1f7c8 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b 0a B_UnresetViews;.
1f7c9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1f7ca 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 pTable->nCol =
1f7cb 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0;. nErr++;
1f7cc 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1f7cd 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 e3SelectDelete(d
1f7ce 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65 6c b, pSel);. } el
1f7cf 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b se {. nErr++;
1f7d0 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
1f7d1 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 QLITE_OMIT_VIEW
1f7d2 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72 */. return nErr
1f7d3 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ; .}.#endif /*
1f7d4 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1f7d5 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 OMIT_VIEW) || !d
1f7d6 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1f7d7 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 IT_VIRTUALTABLE)
1f7d8 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1f7d9 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a ITE_OMIT_VIEW./*
1f7da 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f .** Clear the co
1f7db 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 lumn names from
1f7dc 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61 every VIEW in da
1f7dd 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73 tabase idx..*/.s
1f7de 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
1f7df 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 71 eViewResetAll(sq
1f7e0 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 lite3 *db, int i
1f7e1 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 dx){. HashElem
1f7e2 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61 73 *i;. if( !DbHas
1f7e3 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78 Property(db, idx
1f7e4 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 , DB_UnresetView
1f7e5 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 s) ) return;. f
1f7e6 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 or(i=sqliteHashF
1f7e7 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 64 irst(&db->aDb[id
1f7e8 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 x].pSchema->tblH
1f7e9 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65 ash); i;i=sqlite
1f7ea 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 HashNext(i)){.
1f7eb 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 Table *pTab =
1f7ec 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 sqliteHashData(i
1f7ed 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d );. if( pTab-
1f7ee 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 >pSelect ){.
1f7ef 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c sqliteResetCol
1f7f0 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b 0a umnNames(pTab);.
1f7f1 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 6c }. }. DbCl
1f7f2 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20 earProperty(db,
1f7f3 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 idx, DB_UnresetV
1f7f4 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 iews);.}.#else.#
1f7f5 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 69 define sqliteVi
1f7f6 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 0a ewResetAll(A,B).
1f7f7 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1f7f8 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f _OMIT_VIEW */../
1f7f9 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1f7fa 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 on is called by
1f7fb 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a 75 the VDBE to adju
1f7fc 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 st the internal
1f7fd 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 62 schema.** used b
1f7fe 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 68 y SQLite when th
1f7ff 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 6f e btree layer mo
1f800 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f 74 ves a table root
1f801 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 6f page. The.** ro
1f802 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 62 ot-page of a tab
1f803 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 64 le or index in d
1f804 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73 20 atabase iDb has
1f805 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 72 changed from iFr
1f806 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a om.** to iTo..**
1f807 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 38 .** Ticket #1728
1f808 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 61 : The symbol ta
1f809 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 ble might still
1f80a 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 74 contain informat
1f80b 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 73 ion.** on tables
1f80c 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 20 and/or indices
1f80d 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72 6f that are the pro
1f80e 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 65 cess of being de
1f80f 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f 75 leted..** If you
1f810 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e are unlucky, on
1f811 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 74 e of those delet
1f812 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 61 ed indices or ta
1f813 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61 bles might.** ha
1f814 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 ve the same root
1f815 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74 page number as t
1f816 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72 he real table or
1f817 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a 2a index that is.*
1f818 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20 * being moved.
1f819 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f So we cannot sto
1f81a 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65 p searching afte
1f81b 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 r the first matc
1f81c 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68 h .** because th
1f81d 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69 e first match mi
1f81e 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f ght be for one o
1f81f 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e f the deleted in
1f820 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 6c dices.** or tabl
1f821 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 74 es and not the t
1f822 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 20 able/index that
1f823 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e is actually bein
1f824 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 6d g moved..** We m
1f825 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f ust continue loo
1f826 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 74 ping until all t
1f827 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 ables and indice
1f828 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 61 s with.** rootpa
1f829 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 62 ge==iFrom have b
1f82a 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f een converted to
1f82b 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 65 have a rootpage
1f82c 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72 of iTo.** in or
1f82d 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 69 der to be certai
1f82e 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 68 n that we got th
1f82f 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a e right one..*/.
1f830 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f831 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 53 MIT_AUTOVACUUM.S
1f832 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1f833 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 id sqlite3RootPa
1f834 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62 2c geMoved(Db *pDb,
1f835 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 int iFrom, int
1f836 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d iTo){. HashElem
1f837 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20 *pElem;. Hash
1f838 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73 68 *pHash;.. pHash
1f839 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 = &pDb->pSchema
1f83a 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f 72 ->tblHash;. for
1f83b 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 (pElem=sqliteHas
1f83c 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70 hFirst(pHash); p
1f83d 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 Elem; pElem=sqli
1f83e 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d teHashNext(pElem
1f83f 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 )){. Table *p
1f840 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 Tab = sqliteHash
1f841 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20 Data(pElem);.
1f842 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d if( pTab->tnum=
1f843 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 =iFrom ){.
1f844 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f pTab->tnum = iTo
1f845 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 48 ;. }. }. pH
1f846 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 ash = &pDb->pSch
1f847 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20 ema->idxHash;.
1f848 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 for(pElem=sqlite
1f849 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29 HashFirst(pHash)
1f84a 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 ; pElem; pElem=s
1f84b 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 qliteHashNext(pE
1f84c 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 lem)){. Index
1f84d 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48 *pIdx = sqliteH
1f84e 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a ashData(pElem);.
1f84f 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e if( pIdx->tn
1f850 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 um==iFrom ){.
1f851 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d 20 pIdx->tnum =
1f852 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d iTo;. }. }.}
1f853 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
1f854 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61 rite code to era
1f855 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 se the table wit
1f856 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 h root-page iTab
1f857 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 le from database
1f858 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72 iDb..** Also wr
1f859 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69 ite code to modi
1f85a 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 fy the sqlite_ma
1f85b 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69 ster table and i
1f85c 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a nternal schema.*
1f85d 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 65 * if a root-page
1f85e 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 6c of another tabl
1f85f 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68 e is moved by th
1f860 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 68 e btree-layer wh
1f861 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 20 ilst.** erasing
1f862 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 6e iTable (this can
1f863 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 happen with an
1f864 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
1f865 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 base)..*/ .stati
1f866 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 6f c void destroyRo
1f867 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 50 otPage(Parse *pP
1f868 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65 arse, int iTable
1f869 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 , int iDb){. Vd
1f86a 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 be *v = sqlite3G
1f86b 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
1f86c 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 int r1 = sqlit
1f86d 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
1f86e 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 rse);. sqlite3V
1f86f 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
1f870 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c Destroy, iTable,
1f871 20 72 31 2c 20 69 44 62 29 3b 0a 23 69 66 6e 64 r1, iDb);.#ifnd
1f872 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1f873 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 4f UTOVACUUM. /* O
1f874 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 65 73 P_Destroy stores
1f875 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 20 72 an in integer r
1f876 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 65 67 1. If this integ
1f877 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d 7a er. ** is non-z
1f878 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 73 20 ero, then it is
1f879 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 the root page nu
1f87a 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c 65 20 mber of a table
1f87b 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 6c 6f moved to. ** lo
1f87c 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e 20 54 cation iTable. T
1f87d 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 he following cod
1f87e 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20 73 e modifies the s
1f87f 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
1f880 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 6c 65 le to. ** refle
1f881 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a 20 20 ct this.. **.
1f882 2a 2a 20 54 68 65 20 22 23 25 64 22 20 69 6e 20 ** The "#%d" in
1f883 74 68 65 20 53 51 4c 20 69 73 20 61 20 73 70 65 the SQL is a spe
1f884 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 74 68 cial constant th
1f885 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65 76 65 at means whateve
1f886 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69 73 20 r value. ** is
1f887 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 on the top of th
1f888 65 20 73 74 61 63 6b 2e 20 20 53 65 65 20 73 71 e stack. See sq
1f889 6c 69 74 65 33 52 65 67 69 73 74 65 72 45 78 70 lite3RegisterExp
1f88a 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 r().. */. sqli
1f88b 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 te3NestedParse(p
1f88c 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22 55 50 Parse, . "UP
1f88d 44 41 54 45 20 25 51 2e 25 73 20 53 45 54 20 72 DATE %Q.%s SET r
1f88e 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45 52 45 ootpage=%d WHERE
1f88f 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 70 61 67 #%d AND rootpag
1f890 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 70 50 61 e=#%d",. pPa
1f891 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 rse->db->aDb[iDb
1f892 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f ].zName, SCHEMA_
1f893 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54 61 62 TABLE(iDb), iTab
1f894 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23 65 6e le, r1, r1);.#en
1f895 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52 65 6c dif. sqlite3Rel
1f896 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
1f897 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a se, r1);.}../*.*
1f898 2a 20 57 72 69 74 65 20 56 44 42 45 20 63 6f 64 * Write VDBE cod
1f899 65 20 74 6f 20 65 72 61 73 65 20 74 61 62 6c 65 e to erase table
1f89a 20 70 54 61 62 20 61 6e 64 20 61 6c 6c 20 61 73 pTab and all as
1f89b 73 6f 63 69 61 74 65 64 20 69 6e 64 69 63 65 73 sociated indices
1f89c 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f 64 on disk..** Cod
1f89d 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 e to update the
1f89e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
1f89f 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 72 6e 61 bles and interna
1f8a0 6c 20 73 63 68 65 6d 61 20 64 65 66 69 6e 69 74 l schema definit
1f8a1 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73 65 20 ions.** in case
1f8a2 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c 6f a root-page belo
1f8a3 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65 72 nging to another
1f8a4 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 20 table is moved
1f8a5 62 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 by the btree lay
1f8a6 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61 64 er.** is also ad
1f8a7 64 65 64 20 28 74 68 69 73 20 63 61 6e 20 68 61 ded (this can ha
1f8a8 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75 74 ppen with an aut
1f8a9 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 o-vacuum databas
1f8aa 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f e)..*/.static vo
1f8ab 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28 id destroyTable(
1f8ac 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 Parse *pParse, T
1f8ad 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23 69 66 able *pTab){.#if
1f8ae 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f8af 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 49 6e 64 AUTOVACUUM. Ind
1f8b0 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20 ex *pIdx;. int
1f8b1 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
1f8b2 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 emaToIndex(pPars
1f8b3 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 e->db, pTab->pSc
1f8b4 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 72 6f 79 hema);. destroy
1f8b5 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c RootPage(pParse,
1f8b6 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 pTab->tnum, iDb
1f8b7 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 );. for(pIdx=pT
1f8b8 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 ab->pIndex; pIdx
1f8b9 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 ; pIdx=pIdx->pNe
1f8ba 78 74 29 7b 0a 20 20 20 20 64 65 73 74 72 6f 79 xt){. destroy
1f8bb 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c RootPage(pParse,
1f8bc 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 pIdx->tnum, iDb
1f8bd 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f );. }.#else. /
1f8be 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
1f8bf 65 20 6d 61 79 20 62 65 20 61 75 74 6f 2d 76 61 e may be auto-va
1f8c0 63 75 75 6d 20 63 61 70 61 62 6c 65 20 28 69 66 cuum capable (if
1f8c1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1f8c2 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69 73 20 OVACUUM. ** is
1f8c3 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 74 68 not defined), th
1f8c4 65 6e 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 en it is importa
1f8c5 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f 44 65 nt to call OP_De
1f8c6 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20 20 2a stroy on the. *
1f8c7 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 * table and inde
1f8c8 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 69 6e 20 x root-pages in
1f8c9 6f 72 64 65 72 2c 20 73 74 61 72 74 69 6e 67 20 order, starting
1f8ca 77 69 74 68 20 74 68 65 20 6e 75 6d 65 72 69 63 with the numeric
1f8cb 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72 67 65 ally . ** large
1f8cc 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d st root-page num
1f8cd 62 65 72 2e 20 54 68 69 73 20 67 75 61 72 61 6e ber. This guaran
1f8ce 74 65 65 73 20 74 68 61 74 20 6e 6f 6e 65 20 6f tees that none o
1f8cf 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 73 f the root-pages
1f8d0 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 73 74 . ** to be dest
1f8d1 72 6f 79 65 64 20 69 73 20 72 65 6c 6f 63 61 74 royed is relocat
1f8d2 65 64 20 62 79 20 61 6e 20 65 61 72 6c 69 65 72 ed by an earlier
1f8d3 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e 65 OP_Destroy. i.e
1f8d4 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a 20 66 6f . if the. ** fo
1f8d5 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63 6f 64 llowing were cod
1f8d6 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 50 ed:. **. ** OP
1f8d7 5f 44 65 73 74 72 6f 79 20 34 20 30 0a 20 20 2a _Destroy 4 0. *
1f8d8 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44 65 * .... ** OP_De
1f8d9 73 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a 0a 20 stroy 5 0. **.
1f8da 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70 61 67 ** and root pag
1f8db 65 20 35 20 68 61 70 70 65 6e 65 64 20 74 6f 20 e 5 happened to
1f8dc 62 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 be the largest r
1f8dd 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 oot-page number
1f8de 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 in the. ** data
1f8df 62 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f 74 20 base, then root
1f8e0 70 61 67 65 20 35 20 77 6f 75 6c 64 20 62 65 20 page 5 would be
1f8e1 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 20 34 20 moved to page 4
1f8e2 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 22 4f 50 by the . ** "OP
1f8e3 5f 44 65 73 74 72 6f 79 20 34 20 30 22 20 6f 70 _Destroy 4 0" op
1f8e4 63 6f 64 65 2e 20 54 68 65 20 73 75 62 73 65 71 code. The subseq
1f8e5 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 72 6f 79 uent "OP_Destroy
1f8e6 20 35 20 30 22 20 77 6f 75 6c 64 20 68 69 74 0a 5 0" would hit.
1f8e7 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69 73 74 ** a free-list
1f8e8 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 6e page.. */. in
1f8e9 74 20 69 54 61 62 20 3d 20 70 54 61 62 2d 3e 74 t iTab = pTab->t
1f8ea 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65 73 74 num;. int iDest
1f8eb 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20 77 68 royed = 0;.. wh
1f8ec 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 49 6e ile( 1 ){. In
1f8ed 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 69 dex *pIdx;. i
1f8ee 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20 30 3b nt iLargest = 0;
1f8ef 0a 0a 20 20 20 20 69 66 28 20 69 44 65 73 74 72 .. if( iDestr
1f8f0 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61 62 3c oyed==0 || iTab<
1f8f1 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0a 20 20 iDestroyed ){.
1f8f2 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20 69 iLargest = i
1f8f3 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 Tab;. }. f
1f8f4 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 or(pIdx=pTab->pI
1f8f5 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 ndex; pIdx; pIdx
1f8f6 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 =pIdx->pNext){.
1f8f7 20 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d 20 int iIdx =
1f8f8 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 pIdx->tnum;.
1f8f9 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e assert( pIdx->
1f8fa 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 pSchema==pTab->p
1f8fb 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 Schema );.
1f8fc 69 66 28 20 28 69 44 65 73 74 72 6f 79 65 64 3d if( (iDestroyed=
1f8fd 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 44 65 73 =0 || (iIdx<iDes
1f8fe 74 72 6f 79 65 64 29 29 20 26 26 20 69 49 64 78 troyed)) && iIdx
1f8ff 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a 20 20 20 >iLargest ){.
1f900 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20 iLargest =
1f901 69 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 iIdx;. }.
1f902 20 20 7d 0a 20 20 20 20 69 66 28 20 69 4c 61 72 }. if( iLar
1f903 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 gest==0 ){.
1f904 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65 6c return;. }el
1f905 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 se{. int iD
1f906 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
1f907 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d aToIndex(pParse-
1f908 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 >db, pTab->pSche
1f909 6d 61 29 3b 0a 20 20 20 20 20 20 64 65 73 74 72 ma);. destr
1f90a 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 oyRootPage(pPars
1f90b 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69 44 62 e, iLargest, iDb
1f90c 29 3b 0a 20 20 20 20 20 20 69 44 65 73 74 72 6f );. iDestro
1f90d 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74 3b 0a yed = iLargest;.
1f90e 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
1f90f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
1f910 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
1f911 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 to do the work
1f912 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c 45 20 of a DROP TABLE
1f913 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70 4e statement..** pN
1f914 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ame is the name
1f915 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 of the table to
1f916 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 be dropped..*/.S
1f917 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1f918 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 id sqlite3DropTa
1f919 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 ble(Parse *pPars
1f91a 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d e, SrcList *pNam
1f91b 65 2c 20 69 6e 74 20 69 73 56 69 65 77 2c 20 69 e, int isView, i
1f91c 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 61 62 nt noErr){. Tab
1f91d 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65 le *pTab;. Vdbe
1f91e 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a *v;. sqlite3 *
1f91f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
1f920 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 . int iDb;.. i
1f921 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 f( pParse->nErr
1f922 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
1f923 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 led ){. goto
1f924 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b exit_drop_table;
1f925 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
1f926 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b Name->nSrc==1 );
1f927 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 . pTab = sqlite
1f928 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 3LocateTable(pPa
1f929 72 73 65 2c 20 69 73 56 69 65 77 2c 20 0a 20 20 rse, isView, .
1f92a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f92b 20 20 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d pName-
1f92c 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61 >a[0].zName, pNa
1f92d 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 me->a[0].zDataba
1f92e 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 se);.. if( pTab
1f92f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e ==0 ){. if( n
1f930 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 oErr ){. sq
1f931 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 lite3ErrorClear(
1f932 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 pParse);. }.
1f933 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f goto exit_dro
1f934 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 p_table;. }. i
1f935 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
1f936 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 maToIndex(db, pT
1f937 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 ab->pSchema);.
1f938 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 assert( iDb>=0 &
1f939 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b & iDb<db->nDb );
1f93a 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 .. /* If pTab i
1f93b 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c s a virtual tabl
1f93c 65 2c 20 63 61 6c 6c 20 56 69 65 77 47 65 74 43 e, call ViewGetC
1f93d 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f 20 olumnNames() to
1f93e 65 6e 73 75 72 65 0a 20 20 2a 2a 20 69 74 20 69 ensure. ** it i
1f93f 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 s initialized..
1f940 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 */. if( IsVirt
1f941 75 61 6c 28 70 54 61 62 29 20 26 26 20 73 71 6c ual(pTab) && sql
1f942 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d ite3ViewGetColum
1f943 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 nNames(pParse, p
1f944 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f Tab) ){. goto
1f945 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 exit_drop_table
1f946 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 ;. }.#ifndef SQ
1f947 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
1f948 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 IZATION. {.
1f949 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 6f int code;. co
1f94a 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d nst char *zTab =
1f94b 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 SCHEMA_TABLE(iD
1f94c 62 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 b);. const ch
1f94d 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 ar *zDb = db->aD
1f94e 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 b[iDb].zName;.
1f94f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 const char *zA
1f950 72 67 32 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 rg2 = 0;. if(
1f951 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
1f952 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 k(pParse, SQLITE
1f953 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 _DELETE, zTab, 0
1f954 2c 20 7a 44 62 29 29 7b 0a 20 20 20 20 20 20 67 , zDb)){. g
1f955 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 oto exit_drop_ta
1f956 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ble;. }. i
1f957 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 f( isView ){.
1f958 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d if( !OMIT_TEM
1f959 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b PDB && iDb==1 ){
1f95a 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 . code =
1f95b 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 SQLITE_DROP_TEMP
1f95c 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c _VIEW;. }el
1f95d 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 se{. code
1f95e 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 = SQLITE_DROP_V
1f95f 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 IEW;. }.#if
1f960 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1f961 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
1f962 20 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56 69 }else if( IsVi
1f963 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 rtual(pTab) ){.
1f964 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 code = SQLI
1f965 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b 0a TE_DROP_VTABLE;.
1f966 20 20 20 20 20 20 7a 41 72 67 32 20 3d 20 70 54 zArg2 = pT
1f967 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 3b ab->pMod->zName;
1f968 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 .#endif. }els
1f969 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d e{. if( !OM
1f96a 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 IT_TEMPDB && iDb
1f96b 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 ==1 ){. c
1f96c 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f ode = SQLITE_DRO
1f96d 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 P_TEMP_TABLE;.
1f96e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1f96f 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 code = SQLITE
1f970 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20 20 _DROP_TABLE;.
1f971 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1f972 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 f( sqlite3AuthCh
1f973 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 eck(pParse, code
1f974 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a , pTab->zName, z
1f975 41 72 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20 20 Arg2, zDb) ){.
1f976 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 goto exit_dr
1f977 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a op_table;. }.
1f978 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 if( sqlite3A
1f979 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c uthCheck(pParse,
1f97a 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 SQLITE_DELETE,
1f97b 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 pTab->zName, 0,
1f97c 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f zDb) ){. go
1f97d 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 to exit_drop_tab
1f97e 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 le;. }. }.#e
1f97f 6e 64 69 66 0a 20 20 69 66 28 20 73 71 6c 69 74 ndif. if( sqlit
1f980 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d e3StrNICmp(pTab-
1f981 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f >zName, "sqlite_
1f982 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 ", 7)==0 ){.
1f983 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1f984 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 pParse, "table %
1f985 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 64 72 6f s may not be dro
1f986 70 70 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 pped", pTab->zNa
1f987 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 me);. goto ex
1f988 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 it_drop_table;.
1f989 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }..#ifndef SQLI
1f98a 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 2f TE_OMIT_VIEW. /
1f98b 2a 20 45 6e 73 75 72 65 20 44 52 4f 50 20 54 41 * Ensure DROP TA
1f98c 42 4c 45 20 69 73 20 6e 6f 74 20 75 73 65 64 20 BLE is not used
1f98d 6f 6e 20 61 20 76 69 65 77 2c 20 61 6e 64 20 44 on a view, and D
1f98e 52 4f 50 20 56 49 45 57 20 69 73 20 6e 6f 74 20 ROP VIEW is not
1f98f 75 73 65 64 0a 20 20 2a 2a 20 6f 6e 20 61 20 74 used. ** on a t
1f990 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 able.. */. if(
1f991 20 69 73 56 69 65 77 20 26 26 20 70 54 61 62 2d isView && pTab-
1f992 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 >pSelect==0 ){.
1f993 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1f994 73 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 20 sg(pParse, "use
1f995 44 52 4f 50 20 54 41 42 4c 45 20 74 6f 20 64 65 DROP TABLE to de
1f996 6c 65 74 65 20 74 61 62 6c 65 20 25 73 22 2c 20 lete table %s",
1f997 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pTab->zName);.
1f998 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 goto exit_drop
1f999 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 _table;. }. if
1f99a 28 20 21 69 73 56 69 65 77 20 26 26 20 70 54 61 ( !isView && pTa
1f99b 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 b->pSelect ){.
1f99c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1f99d 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 20 44 g(pParse, "use D
1f99e 52 4f 50 20 56 49 45 57 20 74 6f 20 64 65 6c 65 ROP VIEW to dele
1f99f 74 65 20 76 69 65 77 20 25 73 22 2c 20 70 54 61 te view %s", pTa
1f9a0 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 b->zName);. g
1f9a1 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 oto exit_drop_ta
1f9a2 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ble;. }.#endif.
1f9a3 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 . /* Generate c
1f9a4 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 ode to remove th
1f9a5 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 e table from the
1f9a6 20 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 master table.
1f9a7 2a 2a 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f ** on disk.. */
1f9a8 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 . v = sqlite3Ge
1f9a9 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 tVdbe(pParse);.
1f9aa 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 54 72 if( v ){. Tr
1f9ab 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b igger *pTrigger;
1f9ac 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 . Db *pDb = &
1f9ad 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 db->aDb[iDb];.
1f9ae 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 sqlite3BeginWr
1f9af 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 iteOperation(pPa
1f9b0 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 23 rse, 1, iDb);..#
1f9b1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f9b2 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1f9b3 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61 if( IsVirtua
1f9b4 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 l(pTab) ){.
1f9b5 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 Vdbe *v = sqlit
1f9b6 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
1f9b7 29 3b 0a 20 20 20 20 20 20 69 66 28 20 76 20 29 );. if( v )
1f9b8 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1f9b9 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 3VdbeAddOp0(v, O
1f9ba 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 20 P_VBegin);.
1f9bb 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
1f9bc 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c . /* Drop all
1f9bd 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69 triggers associ
1f9be 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 61 ated with the ta
1f9bf 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 ble being droppe
1f9c0 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20 69 d. Code. ** i
1f9c1 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 72 s generated to r
1f9c2 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72 emove entries fr
1f9c3 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 om sqlite_master
1f9c4 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20 73 and/or. ** s
1f9c5 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 qlite_temp_maste
1f9c6 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 20 r if required..
1f9c7 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67 67 */. pTrigg
1f9c8 65 72 20 3d 20 70 54 61 62 2d 3e 70 54 72 69 67 er = pTab->pTrig
1f9c9 67 65 72 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 ger;. while(
1f9ca 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 pTrigger ){.
1f9cb 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 assert( pTrigg
1f9cc 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 er->pSchema==pTa
1f9cd 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a 20 b->pSchema || .
1f9ce 20 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65 pTrigge
1f9cf 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e r->pSchema==db->
1f9d0 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 aDb[1].pSchema )
1f9d1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 ;. sqlite3D
1f9d2 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 70 50 ropTriggerPtr(pP
1f9d3 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 29 3b arse, pTrigger);
1f9d4 0a 20 20 20 20 20 20 70 54 72 69 67 67 65 72 20 . pTrigger
1f9d5 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 78 = pTrigger->pNex
1f9d6 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 t;. }..#ifnde
1f9d7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1f9d8 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 TOINCREMENT.
1f9d9 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 65 6e /* Remove any en
1f9da 74 72 69 65 73 20 6f 66 20 74 68 65 20 73 71 6c tries of the sql
1f9db 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 ite_sequence tab
1f9dc 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 le associated wi
1f9dd 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 61 th. ** the ta
1f9de 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 ble being droppe
1f9df 64 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 d. This is done
1f9e0 62 65 66 6f 72 65 20 74 68 65 20 74 61 62 6c 65 before the table
1f9e1 20 69 73 20 64 72 6f 70 70 65 64 0a 20 20 20 20 is dropped.
1f9e2 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65 65 20 ** at the btree
1f9e3 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 20 74 level, in case t
1f9e4 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e he sqlite_sequen
1f9e5 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20 74 ce table needs t
1f9e6 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 20 61 73 o. ** move as
1f9e7 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 a result of the
1f9e8 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70 65 drop (can happe
1f9e9 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d n in auto-vacuum
1f9ea 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 mode).. */.
1f9eb 20 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 if( pTab->tab
1f9ec 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 Flags & TF_Autoi
1f9ed 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 ncrement ){.
1f9ee 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 sqlite3NestedP
1f9ef 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 arse(pParse,.
1f9f0 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f "DELETE FRO
1f9f1 4d 20 25 73 2e 73 71 6c 69 74 65 5f 73 65 71 75 M %s.sqlite_sequ
1f9f2 65 6e 63 65 20 57 48 45 52 45 20 6e 61 6d 65 3d ence WHERE name=
1f9f3 25 51 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62 %Q",. pDb
1f9f4 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a ->zName, pTab->z
1f9f5 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 Name. );.
1f9f6 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
1f9f7 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53 51 4c 49 /* Drop all SQLI
1f9f8 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 TE_MASTER table
1f9f9 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 72 69 65 and index entrie
1f9fa 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 s that refer to
1f9fb 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 the. ** table
1f9fc 2e 20 54 68 65 20 70 72 6f 67 72 61 6d 20 6e 61 . The program na
1f9fd 6d 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 me loops through
1f9fe 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c the master tabl
1f9ff 65 20 61 6e 64 20 64 65 6c 65 74 65 73 0a 20 20 e and deletes.
1fa00 20 20 2a 2a 20 65 76 65 72 79 20 72 6f 77 20 74 ** every row t
1fa01 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 61 20 hat refers to a
1fa02 74 61 62 6c 65 20 6f 66 20 74 68 65 20 73 61 6d table of the sam
1fa03 65 20 6e 61 6d 65 20 61 73 20 74 68 65 20 6f 6e e name as the on
1fa04 65 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 64 e being. ** d
1fa05 72 6f 70 70 65 64 2e 20 54 72 69 67 67 65 72 73 ropped. Triggers
1fa06 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73 65 70 are handled sep
1fa07 65 72 61 74 65 6c 79 20 62 65 63 61 75 73 65 20 erately because
1fa08 61 20 74 72 69 67 67 65 72 20 63 61 6e 20 62 65 a trigger can be
1fa09 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 . ** created
1fa0a 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 in the temp data
1fa0b 62 61 73 65 20 74 68 61 74 20 72 65 66 65 72 73 base that refers
1fa0c 20 74 6f 20 61 20 74 61 62 6c 65 20 69 6e 20 61 to a table in a
1fa0d 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 64 61 nother. ** da
1fa0e 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 tabase.. */.
1fa0f 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 sqlite3Nested
1fa10 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 Parse(pParse, .
1fa11 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 "DELETE F
1fa12 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 20 ROM %Q.%s WHERE
1fa13 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61 6e 64 20 tbl_name=%Q and
1fa14 74 79 70 65 21 3d 27 74 72 69 67 67 65 72 27 22 type!='trigger'"
1fa15 2c 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a ,. pDb->z
1fa16 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 Name, SCHEMA_TAB
1fa17 4c 45 28 69 44 62 29 2c 20 70 54 61 62 2d 3e 7a LE(iDb), pTab->z
1fa18 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 Name);.. /* D
1fa19 72 6f 70 20 61 6e 79 20 73 74 61 74 69 73 74 69 rop any statisti
1fa1a 63 73 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 cs from the sqli
1fa1b 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2c 20 te_stat1 table,
1fa1c 69 66 20 69 74 20 65 78 69 73 74 73 20 2a 2f 0a if it exists */.
1fa1d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 if( sqlite3F
1fa1e 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 indTable(db, "sq
1fa1f 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d lite_stat1", db-
1fa20 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 >aDb[iDb].zName)
1fa21 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1fa22 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 3NestedParse(pPa
1fa23 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 rse,. "DE
1fa24 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c LETE FROM %Q.sql
1fa25 69 74 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 ite_stat1 WHERE
1fa26 74 62 6c 3d 25 51 22 2c 20 70 44 62 2d 3e 7a 4e tbl=%Q", pDb->zN
1fa27 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 ame, pTab->zName
1fa28 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a . );. }.
1fa29 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77 . if( !isView
1fa2a 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 && !IsVirtual(p
1fa2b 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 64 65 Tab) ){. de
1fa2c 73 74 72 6f 79 54 61 62 6c 65 28 70 50 61 72 73 stroyTable(pPars
1fa2d 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a e, pTab);. }.
1fa2e 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 . /* Remove t
1fa2f 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 66 he table entry f
1fa30 72 6f 6d 20 53 51 4c 69 74 65 27 73 20 69 6e 74 rom SQLite's int
1fa31 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 61 6e 64 ernal schema and
1fa32 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a 2a 20 74 modify. ** t
1fa33 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 he schema cookie
1fa34 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1fa35 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
1fa36 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1fa37 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
1fa38 50 5f 56 44 65 73 74 72 6f 79 2c 20 69 44 62 2c P_VDestroy, iDb,
1fa39 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 0, 0, pTab->zNa
1fa3a 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 me, 0);. }.
1fa3b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1fa3c 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 Op4(v, OP_DropTa
1fa3d 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 ble, iDb, 0, 0,
1fa3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b pTab->zName, 0);
1fa3f 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e . sqlite3Chan
1fa40 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c geCookie(pParse,
1fa41 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c iDb);. }. sql
1fa42 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 iteViewResetAll(
1fa43 64 62 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f db, iDb);..exit_
1fa44 64 72 6f 70 5f 74 61 62 6c 65 3a 0a 20 20 73 71 drop_table:. sq
1fa45 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 lite3SrcListDele
1fa46 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d te(db, pName);.}
1fa47 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
1fa48 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 tine is called t
1fa49 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 66 o create a new f
1fa4a 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 68 oreign key on th
1fa4b 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72 65 e table.** curre
1fa4c 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 ntly under const
1fa4d 72 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d 43 ruction. pFromC
1fa4e 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 ol determines wh
1fa4f 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 ich columns.** i
1fa50 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 n the current ta
1fa51 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 ble point to the
1fa52 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20 49 foreign key. I
1fa53 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68 f pFromCol==0 th
1fa54 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74 68 en.** connect th
1fa55 65 20 6b 65 79 20 74 6f 20 74 68 65 20 6c 61 73 e key to the las
1fa56 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74 65 t column inserte
1fa57 64 2e 20 20 70 54 6f 20 69 73 20 74 68 65 20 6e d. pTo is the n
1fa58 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 ame of.** the ta
1fa59 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 2e ble referred to.
1fa5a 20 20 70 54 6f 43 6f 6c 20 69 73 20 61 20 6c 69 pToCol is a li
1fa5b 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 st of tables in
1fa5c 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 70 54 6f the other.** pTo
1fa5d 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 table that the
1fa5e 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 6f 69 6e foreign key poin
1fa5f 74 73 20 74 6f 2e 20 20 66 6c 61 67 73 20 63 6f ts to. flags co
1fa60 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20 69 6e ntains all.** in
1fa61 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
1fa62 74 68 65 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 the conflict res
1fa63 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 olution algorith
1fa64 6d 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 ms specified.**
1fa65 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c 45 54 45 in the ON DELETE
1fa66 2c 20 4f 4e 20 55 50 44 41 54 45 20 61 6e 64 20 , ON UPDATE and
1fa67 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61 75 73 65 ON INSERT clause
1fa68 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b 65 79 s..**.** An FKey
1fa69 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72 structure is cr
1fa6a 65 61 74 65 64 20 61 6e 64 20 61 64 64 65 64 20 eated and added
1fa6b 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 to the table cur
1fa6c 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65 72 20 rently.** under
1fa6d 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 construction in
1fa6e 74 68 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 the pParse->pNew
1fa6f 54 61 62 6c 65 20 66 69 65 6c 64 2e 20 20 54 68 Table field. Th
1fa70 65 20 6e 65 77 20 46 4b 65 79 0a 2a 2a 20 69 73 e new FKey.** is
1fa71 20 6e 6f 74 20 6c 69 6e 6b 65 64 20 69 6e 74 6f not linked into
1fa72 20 64 62 2d 3e 61 46 4b 65 79 20 61 74 20 74 68 db->aFKey at th
1fa73 69 73 20 70 6f 69 6e 74 20 2d 20 74 68 61 74 20 is point - that
1fa74 64 6f 65 73 20 6e 6f 74 20 68 61 70 70 65 6e 0a does not happen.
1fa75 2a 2a 20 75 6e 74 69 6c 20 73 71 6c 69 74 65 33 ** until sqlite3
1fa76 45 6e 64 54 61 62 6c 65 28 29 2e 0a 2a 2a 0a 2a EndTable()..**.*
1fa77 2a 20 54 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 * The foreign ke
1fa78 79 20 69 73 20 73 65 74 20 66 6f 72 20 49 4d 4d y is set for IMM
1fa79 45 44 49 41 54 45 20 70 72 6f 63 65 73 73 69 6e EDIATE processin
1fa7a 67 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e 74 g. A subsequent
1fa7b 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 call.** to sqli
1fa7c 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b te3DeferForeignK
1fa7d 65 79 28 29 20 6d 69 67 68 74 20 63 68 61 6e 67 ey() might chang
1fa7e 65 20 74 68 69 73 20 74 6f 20 44 45 46 45 52 52 e this to DEFERR
1fa7f 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ED..*/.SQLITE_PR
1fa80 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1fa81 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b e3CreateForeignK
1fa82 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 ey(. Parse *pPa
1fa83 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 rse, /* Pa
1fa84 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
1fa85 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46 72 . ExprList *pFr
1fa86 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d omCol, /* Colum
1fa87 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 ns in this table
1fa88 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f that point to o
1fa89 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 ther table */.
1fa8a 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20 Token *pTo,
1fa8b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1fa8c 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c 65 20 the other table
1fa8d 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
1fa8e 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c ToCol, /* Col
1fa8f 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 74 68 65 umns in the othe
1fa90 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 r table */. int
1fa91 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 flags
1fa92 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 /* Conflict re
1fa93 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 solution algorit
1fa94 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c hms. */.){. sql
1fa95 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
1fa96 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 e->db;.#ifndef S
1fa97 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 QLITE_OMIT_FOREI
1fa98 47 4e 5f 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70 GN_KEY. FKey *p
1fa99 46 4b 65 79 20 3d 20 30 3b 0a 20 20 54 61 62 6c FKey = 0;. Tabl
1fa9a 65 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 e *p = pParse->p
1fa9b 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 NewTable;. int
1fa9c 6e 42 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b 0a nByte;. int i;.
1fa9d 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 68 int nCol;. ch
1fa9e 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 ar *z;.. assert
1fa9f 28 20 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 66 ( pTo!=0 );. if
1faa0 28 20 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 ( p==0 || pParse
1faa1 2d 3e 6e 45 72 72 20 7c 7c 20 49 4e 5f 44 45 43 ->nErr || IN_DEC
1faa2 4c 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f LARE_VTAB ) goto
1faa3 20 66 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 70 fk_end;. if( p
1faa4 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 FromCol==0 ){.
1faa5 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e int iCol = p->
1faa6 6e 43 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 nCol-1;. if(
1faa7 69 43 6f 6c 3c 30 20 29 20 67 6f 74 6f 20 66 6b iCol<0 ) goto fk
1faa8 5f 65 6e 64 3b 0a 20 20 20 20 69 66 28 20 70 54 _end;. if( pT
1faa9 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e oCol && pToCol->
1faaa 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 nExpr!=1 ){.
1faab 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1faac 67 28 70 50 61 72 73 65 2c 20 22 66 6f 72 65 69 g(pParse, "forei
1faad 67 6e 20 6b 65 79 20 6f 6e 20 25 73 22 0a 20 20 gn key on %s".
1faae 20 20 20 20 20 20 20 22 20 73 68 6f 75 6c 64 20 " should
1faaf 72 65 66 65 72 65 6e 63 65 20 6f 6e 6c 79 20 6f reference only o
1fab0 6e 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 ne column of tab
1fab1 6c 65 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20 le %T",.
1fab2 20 70 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a p->aCol[iCol].z
1fab3 4e 61 6d 65 2c 20 70 54 6f 29 3b 0a 20 20 20 20 Name, pTo);.
1fab4 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 goto fk_end;.
1fab5 20 20 20 7d 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 }. nCol =
1fab6 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 1;. }else if( p
1fab7 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d ToCol && pToCol-
1fab8 3e 6e 45 78 70 72 21 3d 70 46 72 6f 6d 43 6f 6c >nExpr!=pFromCol
1fab9 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 ->nExpr ){. s
1faba 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1fabb 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 Parse,. "
1fabc 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
1fabd 73 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 s in foreign key
1fabe 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 does not match
1fabf 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 22 0a the number of ".
1fac0 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 73 "columns
1fac1 20 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 in the referenc
1fac2 65 64 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 ed table");.
1fac3 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d goto fk_end;. }
1fac4 65 6c 73 65 7b 0a 20 20 20 20 6e 43 6f 6c 20 3d else{. nCol =
1fac5 20 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 pFromCol->nExpr
1fac6 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 ;. }. nByte =
1fac7 73 69 7a 65 6f 66 28 2a 70 46 4b 65 79 29 20 2b sizeof(*pFKey) +
1fac8 20 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 70 46 4b nCol*sizeof(pFK
1fac9 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20 2b 20 70 ey->aCol[0]) + p
1faca 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20 69 66 28 To->n + 1;. if(
1facb 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 66 pToCol ){. f
1facc 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 43 6f 6c or(i=0; i<pToCol
1facd 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 ->nExpr; i++){.
1face 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 74 nByte += st
1facf 72 6c 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 rlen(pToCol->a[i
1fad0 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a 20 20 ].zName) + 1;.
1fad1 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 20 }. }. pFKey
1fad2 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
1fad3 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 65 20 cZero(db, nByte
1fad4 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 79 3d 3d );. if( pFKey==
1fad5 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 6b 0 ){. goto fk
1fad6 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46 4b 65 _end;. }. pFKe
1fad7 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a 20 20 y->pFrom = p;.
1fad8 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d pFKey->pNextFrom
1fad9 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20 20 7a = p->pFKey;. z
1fada 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b 65 79 = (char*)&pFKey
1fadb 5b 31 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e 61 43 [1];. pFKey->aC
1fadc 6f 6c 20 3d 20 28 73 74 72 75 63 74 20 73 43 6f ol = (struct sCo
1fadd 6c 4d 61 70 2a 29 7a 3b 0a 20 20 7a 20 2b 3d 20 lMap*)z;. z +=
1fade 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 73 43 sizeof(struct sC
1fadf 6f 6c 4d 61 70 29 2a 6e 43 6f 6c 3b 0a 20 20 70 olMap)*nCol;. p
1fae0 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a 20 FKey->zTo = z;.
1fae1 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 2d 3e memcpy(z, pTo->
1fae2 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b z, pTo->n);. z[
1fae3 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 7a pTo->n] = 0;. z
1fae4 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20 += pTo->n+1;.
1fae5 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d pFKey->pNextTo =
1fae6 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e 43 6f 0;. pFKey->nCo
1fae7 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66 28 20 l = nCol;. if(
1fae8 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 pFromCol==0 ){.
1fae9 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 pFKey->aCol[0
1faea 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e 43 6f ].iFrom = p->nCo
1faeb 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 l-1;. }else{.
1faec 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f for(i=0; i<nCo
1faed 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 l; i++){. i
1faee 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 nt j;. for(
1faef 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b 20 j=0; j<p->nCol;
1faf0 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 j++){. if
1faf1 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
1faf2 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d (p->aCol[j].zNam
1faf3 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 e, pFromCol->a[i
1faf4 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 ].zName)==0 ){.
1faf5 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e pFKey->
1faf6 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20 3d 20 aCol[i].iFrom =
1faf7 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 j;. bre
1faf8 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
1faf9 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1fafa 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 j>=p->nCol ){.
1fafb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1fafc 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 orMsg(pParse, .
1fafd 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e 6f 77 "unknow
1fafe 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c 22 20 n column \"%s\"
1faff 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 in foreign key d
1fb00 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20 20 20 efinition", .
1fb01 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f 6c 2d pFromCol-
1fb02 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 >a[i].zName);.
1fb03 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e goto fk_en
1fb04 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d d;. }. }
1fb05 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f 43 6f . }. if( pToCo
1fb06 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 l ){. for(i=0
1fb07 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a ; i<nCol; i++){.
1fb08 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 74 int n = st
1fb09 72 6c 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 rlen(pToCol->a[i
1fb0a 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 ].zName);.
1fb0b 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a pFKey->aCol[i].z
1fb0c 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20 20 20 6d Col = z;. m
1fb0d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 43 6f 6c 2d emcpy(z, pToCol-
1fb0e 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b >a[i].zName, n);
1fb0f 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d 20 30 3b . z[n] = 0;
1fb10 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 2b 31 3b . z += n+1;
1fb11 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b . }. }. pFK
1fb12 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d ey->isDeferred =
1fb13 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 64 65 6c 0;. pFKey->del
1fb14 65 74 65 43 6f 6e 66 20 3d 20 66 6c 61 67 73 20 eteConf = flags
1fb15 26 20 30 78 66 66 3b 0a 20 20 70 46 4b 65 79 2d & 0xff;. pFKey-
1fb16 3e 75 70 64 61 74 65 43 6f 6e 66 20 3d 20 28 66 >updateConf = (f
1fb17 6c 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78 lags >> 8 ) & 0x
1fb18 66 66 3b 0a 20 20 70 46 4b 65 79 2d 3e 69 6e 73 ff;. pFKey->ins
1fb19 65 72 74 43 6f 6e 66 20 3d 20 28 66 6c 61 67 73 ertConf = (flags
1fb1a 20 3e 3e 20 31 36 20 29 20 26 20 30 78 66 66 3b >> 16 ) & 0xff;
1fb1b 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 .. /* Link the
1fb1c 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20 74 foreign key to t
1fb1d 68 65 20 74 61 62 6c 65 20 61 73 20 74 68 65 20 he table as the
1fb1e 6c 61 73 74 20 73 74 65 70 2e 0a 20 20 2a 2f 0a last step.. */.
1fb1f 20 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70 46 4b p->pFKey = pFK
1fb20 65 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20 30 3b ey;. pFKey = 0;
1fb21 0a 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 ..fk_end:. sqli
1fb22 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 46 te3DbFree(db, pF
1fb23 4b 65 79 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 Key);.#endif /*
1fb24 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1fb25 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
1fb26 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 ) */. sqlite3Ex
1fb27 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c prListDelete(db,
1fb28 20 70 46 72 6f 6d 43 6f 6c 29 3b 0a 20 20 73 71 pFromCol);. sq
1fb29 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
1fb2a 65 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c 29 3b ete(db, pToCol);
1fb2b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
1fb2c 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
1fb2d 20 77 68 65 6e 20 61 6e 20 49 4e 49 54 49 41 4c when an INITIAL
1fb2e 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 6f 72 20 LY IMMEDIATE or
1fb2f 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 INITIALLY DEFERR
1fb30 45 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 ED.** clause is
1fb31 73 65 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20 seen as part of
1fb32 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 a foreign key de
1fb33 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20 69 finition. The i
1fb34 73 44 65 66 65 72 72 65 64 0a 2a 2a 20 70 61 72 sDeferred.** par
1fb35 61 6d 65 74 65 72 20 69 73 20 31 20 66 6f 72 20 ameter is 1 for
1fb36 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 INITIALLY DEFERR
1fb37 45 44 20 61 6e 64 20 30 20 66 6f 72 20 49 4e 49 ED and 0 for INI
1fb38 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 TIALLY IMMEDIATE
1fb39 2e 0a 2a 2a 20 54 68 65 20 62 65 68 61 76 69 6f ..** The behavio
1fb3a 72 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 r of the most re
1fb3b 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 66 cently created f
1fb3c 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 64 oreign key is ad
1fb3d 6a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f 72 64 justed.** accord
1fb3e 69 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ingly..*/.SQLITE
1fb3f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1fb40 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 lite3DeferForeig
1fb41 6e 4b 65 79 28 50 61 72 73 65 20 2a 70 50 61 72 nKey(Parse *pPar
1fb42 73 65 2c 20 69 6e 74 20 69 73 44 65 66 65 72 72 se, int isDeferr
1fb43 65 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c ed){.#ifndef SQL
1fb44 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e ITE_OMIT_FOREIGN
1fb45 5f 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a 70 54 _KEY. Table *pT
1fb46 61 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 ab;. FKey *pFKe
1fb47 79 3b 0a 20 20 69 66 28 20 28 70 54 61 62 20 3d y;. if( (pTab =
1fb48 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 pParse->pNewTab
1fb49 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 79 le)==0 || (pFKey
1fb4a 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29 3d = pTab->pFKey)=
1fb4b 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 =0 ) return;. p
1fb4c 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 FKey->isDeferred
1fb4d 20 3d 20 69 73 44 65 66 65 72 72 65 64 3b 0a 23 = isDeferred;.#
1fb4e 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 endif.}../*.** G
1fb4f 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 enerate code tha
1fb50 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e 64 t will erase and
1fb51 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70 refill index *p
1fb52 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a Idx. This is.**
1fb53 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c used to initial
1fb54 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65 61 ize a newly crea
1fb55 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f 20 ted index or to
1fb56 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a recompute the.**
1fb57 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 69 content of an i
1fb58 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 ndex in response
1fb59 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63 6f to a REINDEX co
1fb5a 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 20 mmand..**.** if
1fb5b 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20 6e memRootPage is n
1fb5c 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74 20 ot negative, it
1fb5d 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 69 means that the i
1fb5e 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a 2a ndex is newly.**
1fb5f 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 72 created. The r
1fb60 65 67 69 73 74 65 72 20 73 70 65 63 69 66 69 65 egister specifie
1fb61 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67 65 d by memRootPage
1fb62 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a contains the.**
1fb63 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 root page numbe
1fb64 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 r of the index.
1fb65 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 If memRootPage
1fb66 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 is negative, the
1fb67 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 61 n.** the index a
1fb68 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e lready exists an
1fb69 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72 65 d must be cleare
1fb6a 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 72 d before being r
1fb6b 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 74 efilled and.** t
1fb6c 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d he root page num
1fb6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 ber of the index
1fb6e 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 70 is taken from p
1fb6f 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f 0a Index->tnum..*/.
1fb70 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
1fb71 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 50 te3RefillIndex(P
1fb72 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e arse *pParse, In
1fb73 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e 74 dex *pIndex, int
1fb74 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a 20 memRootPage){.
1fb75 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 Table *pTab = p
1fb76 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20 20 Index->pTable;
1fb77 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 /* The table tha
1fb78 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a t is indexed */.
1fb79 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 int iTab = pPa
1fb7a 72 73 65 2d 3e 6e 54 61 62 3b 20 20 20 20 20 20 rse->nTab;
1fb7b 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72 /* Btree cursor
1fb7c 20 75 73 65 64 20 66 6f 72 20 70 54 61 62 20 2a used for pTab *
1fb7d 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70 /. int iIdx = p
1fb7e 50 61 72 73 65 2d 3e 6e 54 61 62 2b 31 3b 20 20 Parse->nTab+1;
1fb7f 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 /* Btree curs
1fb80 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e 64 or used for pInd
1fb81 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 ex */. int addr
1fb82 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
1fb83 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 /* Addres
1fb84 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 s of top of loop
1fb85 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 */. int tnum;
1fb86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fb87 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
1fb88 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20 e of index */.
1fb89 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 Vdbe *v;
1fb8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1fb8b 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
1fb8c 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 into this virtua
1fb8d 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 4b l machine */. K
1fb8e 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 eyInfo *pKey;
1fb8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1fb90 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64 KeyInfo for ind
1fb91 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 ex */. int regI
1fb92 64 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 dxKey;
1fb93 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
1fb94 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ers containing t
1fb95 68 65 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a he index key */.
1fb96 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b int regRecord;
1fb97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fb98 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
1fb99 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65 64 20 ding assemblied
1fb9a 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a index record */.
1fb9b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1fb9c 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 pParse->db;
1fb9d 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1fb9e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
1fb9f 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 int iDb = sqlit
1fba0 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
1fba1 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 db, pIndex->pSch
1fba2 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 ema);..#ifndef S
1fba3 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
1fba4 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 RIZATION. if( s
1fba5 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
1fba6 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 52 pParse, SQLITE_R
1fba7 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e EINDEX, pIndex->
1fba8 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 zName, 0,.
1fba9 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 db->aDb[iDb].zNa
1fbaa 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 me ) ){. retu
1fbab 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a rn;. }.#endif..
1fbac 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20 77 /* Require a w
1fbad 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 rite-lock on the
1fbae 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f 72 table to perfor
1fbaf 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e m this operation
1fbb0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62 */. sqlite3Tab
1fbb1 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 leLock(pParse, i
1fbb2 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 Db, pTab->tnum,
1fbb3 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 1, pTab->zName);
1fbb4 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 .. v = sqlite3G
1fbb5 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
1fbb6 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 if( v==0 ) ret
1fbb7 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f urn;. if( memRo
1fbb8 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 otPage>=0 ){.
1fbb9 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50 tnum = memRootP
1fbba 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 age;. }else{.
1fbbb 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d tnum = pIndex-
1fbbc 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 >tnum;. sqlit
1fbbd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1fbbe 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d 2c 20 OP_Clear, tnum,
1fbbf 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b 65 79 iDb);. }. pKey
1fbc0 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b = sqlite3IndexK
1fbc1 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 eyinfo(pParse, p
1fbc2 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 Index);. sqlite
1fbc3 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
1fbc4 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 64 P_OpenWrite, iId
1fbc5 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a 20 x, tnum, iDb, .
1fbc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbc7 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c (char *)pKey,
1fbc8 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 P4_KEYINFO_HAND
1fbc9 4f 46 46 29 3b 0a 20 20 69 66 28 20 6d 65 6d 52 OFF);. if( memR
1fbca 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 ootPage>=0 ){.
1fbcb 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1fbcc 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 7d ngeP5(v, 1);. }
1fbcd 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 . sqlite3OpenTa
1fbce 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 ble(pParse, iTab
1fbcf 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f , iDb, pTab, OP_
1fbd0 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64 64 OpenRead);. add
1fbd1 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 r1 = sqlite3Vdbe
1fbd2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 AddOp2(v, OP_Rew
1fbd3 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 ind, iTab, 0);.
1fbd4 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c regRecord = sql
1fbd5 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
1fbd6 50 61 72 73 65 29 3b 0a 20 20 72 65 67 49 64 78 Parse);. regIdx
1fbd7 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65 6e Key = sqlite3Gen
1fbd8 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50 erateIndexKey(pP
1fbd9 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 54 arse, pIndex, iT
1fbda 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 31 ab, regRecord, 1
1fbdb 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d );. if( pIndex-
1fbdc 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e >onError!=OE_Non
1fbdd 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 2c e ){. int j1,
1fbde 20 6a 32 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 j2;. int reg
1fbdf 52 6f 77 69 64 3b 0a 0a 20 20 20 20 72 65 67 52 Rowid;.. regR
1fbe0 6f 77 69 64 20 3d 20 72 65 67 49 64 78 4b 65 79 owid = regIdxKey
1fbe1 20 2b 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 + pIndex->nColu
1fbe2 6d 6e 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c mn;. j1 = sql
1fbe3 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1fbe4 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 , OP_IsNull, reg
1fbe5 49 64 78 4b 65 79 2c 20 30 2c 20 70 49 6e 64 65 IdxKey, 0, pInde
1fbe6 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 x->nColumn);.
1fbe7 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j2 = sqlite3Vdb
1fbe8 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73 eAddOp4(v, OP_Is
1fbe9 55 6e 69 71 75 65 2c 20 69 49 64 78 2c 0a 20 20 Unique, iIdx,.
1fbea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbeb 20 20 20 20 20 20 20 20 20 30 2c 20 72 65 67 52 0, regR
1fbec 6f 77 69 64 2c 20 53 51 4c 49 54 45 5f 49 4e 54 owid, SQLITE_INT
1fbed 5f 54 4f 5f 50 54 52 28 72 65 67 52 65 63 6f 72 _TO_PTR(regRecor
1fbee 64 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 d), P4_INT32);.
1fbef 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1fbf0 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c dOp4(v, OP_Halt,
1fbf1 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
1fbf2 4e 54 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30 2c NT, OE_Abort, 0,
1fbf3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fbf4 20 20 20 20 20 22 69 6e 64 65 78 65 64 20 63 6f "indexed co
1fbf5 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75 6e lumns are not un
1fbf6 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43 ique", P4_STATIC
1fbf7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1fbf8 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 beJumpHere(v, j1
1fbf9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1fbfa 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 beJumpHere(v, j2
1fbfb 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
1fbfc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1fbfd 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64 78 _IdxInsert, iIdx
1fbfe 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 , regRecord);.
1fbff 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
1fc00 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 mpReg(pParse, re
1fc01 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 gRecord);. sqli
1fc02 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1fc03 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c 20 OP_Next, iTab,
1fc04 61 64 64 72 31 2b 31 29 3b 0a 20 20 73 71 6c 69 addr1+1);. sqli
1fc05 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
1fc06 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 73 71 6c v, addr1);. sql
1fc07 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
1fc08 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61 62 , OP_Close, iTab
1fc09 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1fc0a 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f AddOp1(v, OP_Clo
1fc0b 73 65 2c 20 69 49 64 78 29 3b 0a 7d 0a 0a 2f 2a se, iIdx);.}../*
1fc0c 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
1fc0d 20 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 53 51 index for an SQ
1fc0e 4c 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65 31 L table. pName1
1fc0f 2e 70 4e 61 6d 65 32 20 69 73 20 74 68 65 20 6e .pName2 is the n
1fc10 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 ame of the index
1fc11 20 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69 73 .** and pTblLis
1fc12 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 t is the name of
1fc13 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 the table that
1fc14 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 is to be indexed
1fc15 2e 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a 2a . Both will .**
1fc16 20 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20 70 be NULL for a p
1fc17 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 61 6e rimary key or an
1fc18 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 20 63 index that is c
1fc19 72 65 61 74 65 64 20 74 6f 20 73 61 74 69 73 66 reated to satisf
1fc1a 79 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63 6f y a.** UNIQUE co
1fc1b 6e 73 74 72 61 69 6e 74 2e 20 20 49 66 20 70 54 nstraint. If pT
1fc1c 61 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 78 20 able and pIndex
1fc1d 61 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70 50 are NULL, use pP
1fc1e 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 0a arse->pNewTable.
1fc1f 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 ** as the table
1fc20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 to be indexed.
1fc21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c pParse->pNewTabl
1fc22 65 20 69 73 20 61 20 74 61 62 6c 65 20 74 68 61 e is a table tha
1fc23 74 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c t is.** currentl
1fc24 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 y being construc
1fc25 74 65 64 20 62 79 20 61 20 43 52 45 41 54 45 20 ted by a CREATE
1fc26 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e TABLE statement.
1fc27 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73 20 .**.** pList is
1fc28 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e a list of column
1fc29 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e s to be indexed.
1fc2a 20 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65 20 pList will be
1fc2b 4e 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a 20 NULL if this.**
1fc2c 69 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 is a primary key
1fc2d 20 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73 74 or unique-const
1fc2e 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f 73 raint on the mos
1fc2f 74 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 t recent column
1fc30 61 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 added.** to the
1fc31 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 table currently
1fc32 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 under constructi
1fc33 6f 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f on. .*/.SQLITE_
1fc34 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1fc35 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 ite3CreateIndex(
1fc36 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
1fc37 2c 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e 66 , /* All inf
1fc38 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
1fc39 68 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20 54 his parse */. T
1fc3a 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 oken *pName1,
1fc3b 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20 /* First part
1fc3c 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d of index name. M
1fc3d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 ay be NULL */.
1fc3e 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 Token *pName2,
1fc3f 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 /* Second par
1fc40 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e t of index name.
1fc41 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a May be NULL */.
1fc42 20 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c 4e SrcList *pTblN
1fc43 61 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74 6f ame, /* Table to
1fc44 20 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61 72 index. Use pPar
1fc45 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 66 se->pNewTable if
1fc46 20 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 0 */. ExprList
1fc47 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41 20 *pList, /* A
1fc48 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 list of columns
1fc49 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f to be indexed */
1fc4a 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 . int onError,
1fc4b 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 /* OE_Abor
1fc4c 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 t, OE_Ignore, OE
1fc4d 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f _Replace, or OE_
1fc4e 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 None */. Token
1fc4f 2a 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a 20 *pStart, /*
1fc50 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e The CREATE token
1fc51 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 69 that begins thi
1fc52 73 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 s statement */.
1fc53 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 Token *pEnd,
1fc54 20 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20 74 /* The ")" t
1fc55 68 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20 43 hat closes the C
1fc56 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 REATE INDEX stat
1fc57 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 ement */. int s
1fc58 6f 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f 2a ortOrder, /*
1fc59 20 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20 70 Sort order of p
1fc5a 72 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e 20 rimary key when
1fc5b 70 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20 pList==NULL */.
1fc5c 20 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74 20 int ifNotExist
1fc5d 20 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72 6f /* Omit erro
1fc5e 72 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65 61 r if index alrea
1fc5f 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a dy exists */.){.
1fc60 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 Table *pTab =
1fc61 30 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 0; /* Table
1fc62 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f to be indexed */
1fc63 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 . Index *pIndex
1fc64 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69 = 0; /* The i
1fc65 6e 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 74 ndex to be creat
1fc66 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e ed */. char *zN
1fc67 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 ame = 0; /*
1fc68 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 Name of the inde
1fc69 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 x */. int nName
1fc6a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1fc6b 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
1fc6c 65 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a ers in zName */.
1fc6d 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 6f int i, j;. To
1fc6e 6b 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20 20 ken nullId;
1fc6f 20 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 6e /* Fake token
1fc70 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49 44 for an empty ID
1fc71 20 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69 78 list */. DbFix
1fc72 65 72 20 73 46 69 78 3b 20 20 20 20 20 20 20 20 er sFix;
1fc73 2f 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e 67 /* For assigning
1fc74 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73 20 database names
1fc75 74 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 69 to pTable */. i
1fc76 6e 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b nt sortOrderMask
1fc77 3b 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f ; /* 1 to hono
1fc78 72 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78 2e r DESC in index.
1fc79 20 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20 2a 0 to ignore. *
1fc7a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 /. sqlite3 *db
1fc7b 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
1fc7c 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 Db *pDb;
1fc7d 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 /* The spec
1fc7e 69 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74 61 ific table conta
1fc7f 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 65 ining the indexe
1fc80 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 d database */.
1fc81 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
1fc82 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1fc83 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 the database th
1fc84 61 74 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 at is being writ
1fc85 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a ten */. Token *
1fc86 70 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f 2a pName = 0; /*
1fc87 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d Unqualified nam
1fc88 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 74 e of the index t
1fc89 6f 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73 74 o create */. st
1fc8a 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
1fc8b 65 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20 2f em *pListItem; /
1fc8c 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 * For looping ov
1fc8d 65 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e er pList */. in
1fc8e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45 t nCol;. int nE
1fc8f 78 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61 72 xtra = 0;. char
1fc90 20 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 69 66 28 *zExtra;.. if(
1fc91 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c pParse->nErr ||
1fc92 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1fc93 64 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f d || IN_DECLARE_
1fc94 56 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74 6f VTAB ){. goto
1fc95 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 exit_create_ind
1fc96 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 ex;. }.. /*.
1fc97 2a 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62 6c ** Find the tabl
1fc98 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 e that is to be
1fc99 69 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72 6e indexed. Return
1fc9a 20 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66 6f early if not fo
1fc9b 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 und.. */. if(
1fc9c 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a pTblName!=0 ){..
1fc9d 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 74 /* Use the t
1fc9e 77 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e 61 wo-part index na
1fc9f 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 me to determine
1fca0 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 the database .
1fca1 20 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 66 ** to search f
1fca2 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27 46 or the table. 'F
1fca3 69 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e 61 ix' the table na
1fca4 6d 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20 20 me to this db.
1fca5 20 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b ** before look
1fca6 69 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c 65 ing up the table
1fca7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
1fca8 65 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20 70 ert( pName1 && p
1fca9 4e 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44 62 Name2 );. iDb
1fcaa 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 = sqlite3TwoPar
1fcab 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e tName(pParse, pN
1fcac 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 ame1, pName2, &p
1fcad 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 Name);. if( i
1fcae 44 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69 74 Db<0 ) goto exit
1fcaf 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a _create_index;..
1fcb0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1fcb1 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20 2f MIT_TEMPDB. /
1fcb2 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 6e * If the index n
1fcb3 61 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66 ame was unqualif
1fcb4 69 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74 68 ied, check if th
1fcb5 65 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20 e the table.
1fcb6 2a 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 ** is a temp tab
1fcb7 6c 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 74 le. If so, set t
1fcb8 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 31 he database to 1
1fcb9 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 . Do not do this
1fcba 0a 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74 69 . ** if initi
1fcbb 61 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62 61 alising a databa
1fcbc 73 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a se schema.. *
1fcbd 2f 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 /. if( !db->i
1fcbe 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 nit.busy ){.
1fcbf 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 pTab = sqlite3
1fcc0 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 SrcListLookup(pP
1fcc1 61 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b arse, pTblName);
1fcc2 0a 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d 65 . if( pName
1fcc3 32 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 2 && pName2->n==
1fcc4 30 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61 0 && pTab && pTa
1fcc5 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e b->pSchema==db->
1fcc6 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 aDb[1].pSchema )
1fcc7 7b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20 {. iDb =
1fcc8 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
1fcc9 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 .#endif.. if(
1fcca 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 sqlite3FixInit(
1fccb 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 &sFix, pParse, i
1fccc 44 62 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e 61 Db, "index", pNa
1fccd 6d 65 29 20 26 26 0a 20 20 20 20 20 20 20 20 73 me) &&. s
1fcce 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 qlite3FixSrcList
1fccf 28 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d 65 (&sFix, pTblName
1fcd0 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f ). ){. /
1fcd1 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 70 61 * Because the pa
1fcd2 72 73 65 72 20 63 6f 6e 73 74 72 75 63 74 73 20 rser constructs
1fcd3 70 54 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20 pTblName from a
1fcd4 73 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65 single identifie
1fcd5 72 2c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 r,. ** sqli
1fcd6 74 65 33 46 69 78 53 72 63 4c 69 73 74 20 63 61 te3FixSrcList ca
1fcd7 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f n never fail. */
1fcd8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29 . assert(0)
1fcd9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62 ;. }. pTab
1fcda 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 = sqlite3Locate
1fcdb 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c Table(pParse, 0,
1fcdc 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e pTblName->a[0].
1fcdd 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20 zName, .
1fcde 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a pTblName->a[0].z
1fcdf 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 69 Database);. i
1fce0 66 28 20 21 70 54 61 62 20 7c 7c 20 64 62 2d 3e f( !pTab || db->
1fce1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 mallocFailed ) g
1fce2 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f oto exit_create_
1fce3 69 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65 72 index;. asser
1fce4 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e t( db->aDb[iDb].
1fce5 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 pSchema==pTab->p
1fce6 53 63 68 65 6d 61 20 29 3b 0a 20 20 7d 65 6c 73 Schema );. }els
1fce7 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
1fce8 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 Name==0 );. p
1fce9 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e Tab = pParse->pN
1fcea 65 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 28 ewTable;. if(
1fceb 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 !pTab ) goto ex
1fcec 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b it_create_index;
1fced 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 . iDb = sqlit
1fcee 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 e3SchemaToIndex(
1fcef 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d db, pTab->pSchem
1fcf0 61 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20 a);. }. pDb =
1fcf1 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a &db->aDb[iDb];..
1fcf2 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c if( pTab==0 ||
1fcf3 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 pParse->nErr )
1fcf4 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 goto exit_create
1fcf5 5f 69 6e 64 65 78 3b 0a 20 20 69 66 28 20 73 71 _index;. if( sq
1fcf6 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 lite3StrNICmp(pT
1fcf7 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 ab->zName, "sqli
1fcf8 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 te_", 7)==0 ){.
1fcf9 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1fcfa 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c sg(pParse, "tabl
1fcfb 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 e %s may not be
1fcfc 69 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d 3e indexed", pTab->
1fcfd 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f zName);. goto
1fcfe 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 exit_create_ind
1fcff 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 ex;. }.#ifndef
1fd00 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
1fd01 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 . if( pTab->pSe
1fd02 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 lect ){. sqli
1fd03 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1fd04 73 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20 6e se, "views may n
1fd05 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b ot be indexed");
1fd06 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 . goto exit_c
1fd07 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d reate_index;. }
1fd08 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
1fd09 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1fd0a 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49 UALTABLE. if( I
1fd0b 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 sVirtual(pTab) )
1fd0c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
1fd0d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 orMsg(pParse, "v
1fd0e 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 61 irtual tables ma
1fd0f 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 y not be indexed
1fd10 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 ");. goto exi
1fd11 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a t_create_index;.
1fd12 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
1fd13 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e . ** Find the n
1fd14 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 ame of the index
1fd15 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 . Make sure the
1fd16 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 re is not alread
1fd17 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20 69 y another. ** i
1fd18 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77 69 ndex or table wi
1fd19 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 th the same name
1fd1a 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 . . **. ** Ex
1fd1b 63 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 20 ception: If we
1fd1c 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 are reading the
1fd1d 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e 65 names of permane
1fd1e 6e 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d 20 nt indices from
1fd1f 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f the. ** sqlite_
1fd20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 28 62 65 master table (be
1fd21 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 72 cause some other
1fd22 20 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65 64 process changed
1fd23 20 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e 64 the schema) and
1fd24 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 . ** one of the
1fd25 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f 6c index names col
1fd26 6c 69 64 65 73 20 77 69 74 68 20 74 68 65 20 6e lides with the n
1fd27 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61 ame of a tempora
1fd28 72 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a ry table or. **
1fd29 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65 20 index, then we
1fd2a 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f will continue to
1fd2b 20 70 72 6f 63 65 73 73 20 74 68 69 73 20 69 6e process this in
1fd2c 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 dex.. **. ** I
1fd2d 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 f pName==0 it me
1fd2e 61 6e 73 20 74 68 61 74 20 77 65 20 61 72 65 0a ans that we are.
1fd2f 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69 74 ** dealing wit
1fd30 68 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 h a primary key
1fd31 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 or UNIQUE constr
1fd32 61 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20 74 aint. We have t
1fd33 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20 2a o invent our. *
1fd34 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a 2f * own name.. */
1fd35 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a . if( pName ){.
1fd36 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 zName = sqli
1fd37 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e te3NameFromToken
1fd38 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 (db, pName);.
1fd39 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
1fd3a 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d sqlite3ReadSchem
1fd3b 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f a(pParse) ) goto
1fd3c 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 exit_create_ind
1fd3d 65 78 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d ex;. if( zNam
1fd3e 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 e==0 ) goto exit
1fd3f 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
1fd40 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
1fd41 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 !=sqlite3CheckOb
1fd42 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c jectName(pParse,
1fd43 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 zName) ){.
1fd44 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 goto exit_creat
1fd45 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 e_index;. }.
1fd46 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 if( !db->init
1fd47 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 69 .busy ){. i
1fd48 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 f( SQLITE_OK!=sq
1fd49 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 lite3ReadSchema(
1fd4a 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 65 pParse) ) goto e
1fd4b 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 xit_create_index
1fd4c 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
1fd4d 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c te3FindTable(db,
1fd4e 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29 7b zName, 0)!=0 ){
1fd4f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1fd50 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1fd51 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 "there is alrea
1fd52 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 64 dy a table named
1fd53 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 %s", zName);.
1fd54 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f goto exit_
1fd55 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 create_index;.
1fd56 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1fd57 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 if( sqlite3FindI
1fd58 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 ndex(db, zName,
1fd59 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 29 pDb->zName)!=0 )
1fd5a 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 66 4e {. if( !ifN
1fd5b 6f 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20 20 otExist ){.
1fd5c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1fd5d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64 65 sg(pParse, "inde
1fd5e 78 20 25 73 20 61 6c 72 65 61 64 79 20 65 78 69 x %s already exi
1fd5f 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 sts", zName);.
1fd60 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f }. goto
1fd61 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 exit_create_ind
1fd62 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 ex;. }. }els
1fd63 65 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 e{. int n;.
1fd64 20 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b 0a Index *pLoop;.
1fd65 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 54 for(pLoop=pT
1fd66 61 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31 3b ab->pIndex, n=1;
1fd67 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c pLoop; pLoop=pL
1fd68 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b 29 oop->pNext, n++)
1fd69 7b 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 {}. zName = s
1fd6a 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
1fd6b 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e , "sqlite_autoin
1fd6c 64 65 78 5f 25 73 5f 25 64 22 2c 20 70 54 61 62 dex_%s_%d", pTab
1fd6d 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 ->zName, n);.
1fd6e 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b if( zName==0 ){
1fd6f 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 . goto exit
1fd70 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 _create_index;.
1fd71 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 }. }.. /* C
1fd72 68 65 63 6b 20 66 6f 72 20 61 75 74 68 6f 72 69 heck for authori
1fd73 7a 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 zation to create
1fd74 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a an index.. */.
1fd75 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1fd76 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f MIT_AUTHORIZATIO
1fd77 4e 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 N. {. const
1fd78 63 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d char *zDb = pDb-
1fd79 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 >zName;. if(
1fd7a 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b sqlite3AuthCheck
1fd7b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f (pParse, SQLITE_
1fd7c 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 INSERT, SCHEMA_T
1fd7d 41 42 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a 44 ABLE(iDb), 0, zD
1fd7e 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f b) ){. goto
1fd7f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 exit_create_ind
1fd80 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 ex;. }. i
1fd81 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f = SQLITE_CREATE_
1fd82 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 21 INDEX;. if( !
1fd83 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 OMIT_TEMPDB && i
1fd84 44 62 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c 49 Db==1 ) i = SQLI
1fd85 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 TE_CREATE_TEMP_I
1fd86 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 NDEX;. if( sq
1fd87 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 lite3AuthCheck(p
1fd88 50 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 2c Parse, i, zName,
1fd89 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 pTab->zName, zD
1fd8a 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f b) ){. goto
1fd8b 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 exit_create_ind
1fd8c 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 ex;. }. }.#e
1fd8d 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 4c ndif.. /* If pL
1fd8e 69 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73 ist==0, it means
1fd8f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 this routine wa
1fd90 73 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65 s called to make
1fd91 20 61 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 a primary. **
1fd92 6b 65 79 20 6f 75 74 20 6f 66 20 74 68 65 20 6c key out of the l
1fd93 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64 ast column added
1fd94 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 75 6e to the table un
1fd95 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e der construction
1fd96 2e 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 74 65 .. ** So create
1fd97 20 61 20 66 61 6b 65 20 6c 69 73 74 20 74 6f 20 a fake list to
1fd98 73 69 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a 20 simulate this..
1fd99 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 74 3d */. if( pList=
1fd9a 3d 30 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 64 =0 ){. nullId
1fd9b 2e 7a 20 3d 20 28 75 38 2a 29 70 54 61 62 2d 3e .z = (u8*)pTab->
1fd9c 61 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d aCol[pTab->nCol-
1fd9d 31 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75 1].zName;. nu
1fd9e 6c 6c 49 64 2e 6e 20 3d 20 73 74 72 6c 65 6e 28 llId.n = strlen(
1fd9f 28 63 68 61 72 2a 29 6e 75 6c 6c 49 64 2e 7a 29 (char*)nullId.z)
1fda0 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 ;. pList = sq
1fda1 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 lite3ExprListApp
1fda2 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 30 end(pParse, 0, 0
1fda3 2c 20 26 6e 75 6c 6c 49 64 29 3b 0a 20 20 20 20 , &nullId);.
1fda4 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 67 if( pList==0 ) g
1fda5 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f oto exit_create_
1fda6 69 6e 64 65 78 3b 0a 20 20 20 20 70 4c 69 73 74 index;. pList
1fda7 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 ->a[0].sortOrder
1fda8 20 3d 20 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 = sortOrder;.
1fda9 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f }.. /* Figure o
1fdaa 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 ut how many byte
1fdab 73 20 6f 66 20 73 70 61 63 65 20 61 72 65 20 72 s of space are r
1fdac 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 equired to store
1fdad 20 65 78 70 6c 69 63 69 74 6c 79 0a 20 20 2a 2a explicitly. **
1fdae 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 6c 61 specified colla
1fdaf 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 tion sequence na
1fdb0 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 mes.. */. for(
1fdb1 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 i=0; i<pList->nE
1fdb2 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 xpr; i++){. E
1fdb3 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 20 xpr *pExpr;.
1fdb4 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a CollSeq *pColl;.
1fdb5 20 20 20 20 69 66 28 20 28 70 45 78 70 72 20 3d if( (pExpr =
1fdb6 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 pList->a[i].pEx
1fdb7 70 72 29 21 3d 30 20 26 26 20 28 70 43 6f 6c 6c pr)!=0 && (pColl
1fdb8 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 29 = pExpr->pColl)
1fdb9 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45 78 !=0 ){. nEx
1fdba 74 72 61 20 2b 3d 20 28 31 20 2b 20 73 74 72 6c tra += (1 + strl
1fdbb 65 6e 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 en(pColl->zName)
1fdbc 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
1fdbd 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 /* . ** Allocat
1fdbe 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 72 75 e the index stru
1fdbf 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e cture. . */. n
1fdc0 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e Name = strlen(zN
1fdc1 61 6d 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 70 ame);. nCol = p
1fdc2 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 List->nExpr;. p
1fdc3 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 Index = sqlite3D
1fdc4 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
1fdc5 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49 6e . sizeof(In
1fdc6 64 65 78 29 20 2b 20 20 20 20 20 20 20 20 20 20 dex) +
1fdc7 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72 /* Index str
1fdc8 75 63 74 75 72 65 20 20 2a 2f 0a 20 20 20 20 20 ucture */.
1fdc9 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 6f sizeof(int)*nCo
1fdca 6c 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a l + /*
1fdcb 20 49 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e 20 Index.aiColumn
1fdcc 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f */. sizeo
1fdcd 66 28 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29 20 f(int)*(nCol+1)
1fdce 2b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 + /* Index
1fdcf 2e 61 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a 20 .aiRowEst */.
1fdd0 20 20 20 20 20 73 69 7a 65 6f 66 28 63 68 61 72 sizeof(char
1fdd1 20 2a 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 *)*nCol +
1fdd2 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f 6c /* Index.azCol
1fdd3 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 l */. s
1fdd4 69 7a 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20 2b izeof(u8)*nCol +
1fdd5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1fdd6 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20 ndex.aSortOrder
1fdd7 2a 2f 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b */. nName +
1fdd8 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 1 +
1fdd9 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 7a /* Index.z
1fdda 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20 20 Name */.
1fddb 20 20 20 6e 45 78 74 72 61 20 20 20 20 20 20 20 nExtra
1fddc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fddd 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 /* Collation seq
1fdde 75 65 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 uence names */.
1fddf 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 );. if( db->ma
1fde0 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1fde1 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 goto exit_crea
1fde2 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 te_index;. }.
1fde3 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d pIndex->azColl =
1fde4 20 28 63 68 61 72 2a 2a 29 28 26 70 49 6e 64 65 (char**)(&pInde
1fde5 78 5b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d x[1]);. pIndex-
1fde6 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74 >aiColumn = (int
1fde7 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a 43 *)(&pIndex->azC
1fde8 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 oll[nCol]);. pI
1fde9 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 20 3d ndex->aiRowEst =
1fdea 20 28 75 6e 73 69 67 6e 65 64 20 2a 29 28 26 70 (unsigned *)(&p
1fdeb 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b Index->aiColumn[
1fdec 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78 nCol]);. pIndex
1fded 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 ->aSortOrder = (
1fdee 75 38 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 u8 *)(&pIndex->a
1fdef 69 52 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d 29 iRowEst[nCol+1])
1fdf0 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d ;. pIndex->zNam
1fdf1 65 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 e = (char *)(&pI
1fdf2 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 ndex->aSortOrder
1fdf3 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74 72 [nCol]);. zExtr
1fdf4 61 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 a = (char *)(&pI
1fdf5 6e 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d ndex->zName[nNam
1fdf6 65 2b 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 e+1]);. memcpy(
1fdf7 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a pIndex->zName, z
1fdf8 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a Name, nName+1);.
1fdf9 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 pIndex->pTable
1fdfa 20 3d 20 70 54 61 62 3b 0a 20 20 70 49 6e 64 65 = pTab;. pInde
1fdfb 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 x->nColumn = pLi
1fdfc 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e st->nExpr;. pIn
1fdfd 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 6f dex->onError = o
1fdfe 6e 45 72 72 6f 72 3b 0a 20 20 70 49 6e 64 65 78 nError;. pIndex
1fdff 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d 20 70 4e ->autoIndex = pN
1fe00 61 6d 65 3d 3d 30 3b 0a 20 20 70 49 6e 64 65 78 ame==0;. pIndex
1fe01 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e ->pSchema = db->
1fe02 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 aDb[iDb].pSchema
1fe03 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f ;.. /* Check to
1fe04 20 73 65 65 20 69 66 20 77 65 20 73 68 6f 75 6c see if we shoul
1fe05 64 20 68 6f 6e 6f 72 20 44 45 53 43 20 72 65 71 d honor DESC req
1fe06 75 65 73 74 73 20 6f 6e 20 69 6e 64 65 78 20 63 uests on index c
1fe07 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 olumns. */. if
1fe08 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e ( pDb->pSchema->
1fe09 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 29 file_format>=4 )
1fe0a 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d {. sortOrderM
1fe0b 61 73 6b 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 48 ask = -1; /* H
1fe0c 6f 6e 6f 72 20 44 45 53 43 20 2a 2f 0a 20 20 7d onor DESC */. }
1fe0d 65 6c 73 65 7b 0a 20 20 20 20 73 6f 72 74 4f 72 else{. sortOr
1fe0e 64 65 72 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 derMask = 0;
1fe0f 2f 2a 20 49 67 6e 6f 72 65 20 44 45 53 43 20 2a /* Ignore DESC *
1fe10 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 63 61 6e /. }.. /* Scan
1fe11 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 the names of th
1fe12 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 e columns of the
1fe13 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 table to be ind
1fe14 65 78 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f exed and. ** lo
1fe15 61 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e ad the column in
1fe16 64 69 63 65 73 20 69 6e 74 6f 20 74 68 65 20 49 dices into the I
1fe17 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 ndex structure.
1fe18 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 Report an error
1fe19 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20 63 6f 6c . ** if any col
1fe1a 75 6d 6e 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 umn is not found
1fe1b 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 .. */. for(i=0
1fe1c 2c 20 70 4c 69 73 74 49 74 65 6d 3d 70 4c 69 73 , pListItem=pLis
1fe1d 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e t->a; i<pList->n
1fe1e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 4c 69 73 74 Expr; i++, pList
1fe1f 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e Item++){. con
1fe20 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 4e 61 6d st char *zColNam
1fe21 65 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 7a e = pListItem->z
1fe22 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e Name;. Column
1fe23 20 2a 70 54 61 62 43 6f 6c 3b 0a 20 20 20 20 69 *pTabCol;. i
1fe24 6e 74 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 nt requestedSort
1fe25 4f 72 64 65 72 3b 0a 20 20 20 20 63 68 61 72 20 Order;. char
1fe26 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 *zColl;
1fe27 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
1fe28 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
1fe29 6e 61 6d 65 20 2a 2f 0a 0a 20 20 20 20 66 6f 72 name */.. for
1fe2a 28 6a 3d 30 2c 20 70 54 61 62 43 6f 6c 3d 70 54 (j=0, pTabCol=pT
1fe2b 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 ab->aCol; j<pTab
1fe2c 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 54 61 ->nCol; j++, pTa
1fe2d 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 69 bCol++){. i
1fe2e 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
1fe2f 70 28 7a 43 6f 6c 4e 61 6d 65 2c 20 70 54 61 62 p(zColName, pTab
1fe30 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 Col->zName)==0 )
1fe31 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1fe32 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e if( j>=pTab->n
1fe33 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Col ){. sql
1fe34 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1fe35 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 68 rse, "table %s h
1fe36 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d as no column nam
1fe37 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 ed %s",.
1fe38 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f pTab->zName, zCo
1fe39 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f lName);. go
1fe3a 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 to exit_create_i
1fe3b 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ndex;. }.
1fe3c 2f 2a 20 54 4f 44 4f 3a 20 20 41 64 64 20 61 20 /* TODO: Add a
1fe3d 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 test to make sur
1fe3e 65 20 74 68 61 74 20 74 68 65 20 73 61 6d 65 20 e that the same
1fe3f 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 6e 61 column is not na
1fe40 6d 65 64 0a 20 20 20 20 2a 2a 20 6d 6f 72 65 20 med. ** more
1fe41 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 69 6e than once within
1fe42 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 2e the same index.
1fe43 20 20 4f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 Only the first
1fe44 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 20 20 instance of.
1fe45 20 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77 ** the column w
1fe46 69 6c 6c 20 65 76 65 72 20 62 65 20 75 73 65 64 ill ever be used
1fe47 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 by the optimize
1fe48 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 75 73 r. Note that us
1fe49 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 ing the. ** s
1fe4a 61 6d 65 20 63 6f 6c 75 6d 6e 20 6d 6f 72 65 20 ame column more
1fe4b 74 68 61 6e 20 6f 6e 63 65 20 63 61 6e 6e 6f 74 than once cannot
1fe4c 20 62 65 20 61 6e 20 65 72 72 6f 72 20 62 65 63 be an error bec
1fe4d 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 ause that would
1fe4e 0a 20 20 20 20 2a 2a 20 62 72 65 61 6b 20 62 61 . ** break ba
1fe4f 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 ckwards compatib
1fe50 69 6c 69 74 79 20 2d 20 69 74 20 6e 65 65 64 73 ility - it needs
1fe51 20 74 6f 20 62 65 20 61 20 77 61 72 6e 69 6e 67 to be a warning
1fe52 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e .. */. pIn
1fe53 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d dex->aiColumn[i]
1fe54 20 3d 20 6a 3b 0a 20 20 20 20 69 66 28 20 70 4c = j;. if( pL
1fe55 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 20 26 istItem->pExpr &
1fe56 26 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 & pListItem->pEx
1fe57 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 pr->pColl ){.
1fe58 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 assert( pList
1fe59 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f Item->pExpr->pCo
1fe5a 6c 6c 20 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c ll );. zCol
1fe5b 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 20 20 l = zExtra;.
1fe5c 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1fe5d 74 66 28 6e 45 78 74 72 61 2c 20 7a 45 78 74 72 tf(nExtra, zExtr
1fe5e 61 2c 20 22 25 73 22 2c 20 70 4c 69 73 74 49 74 a, "%s", pListIt
1fe5f 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c em->pExpr->pColl
1fe60 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 ->zName);.
1fe61 7a 45 78 74 72 61 20 2b 3d 20 28 73 74 72 6c 65 zExtra += (strle
1fe62 6e 28 7a 43 6f 6c 6c 29 20 2b 20 31 29 3b 0a 20 n(zColl) + 1);.
1fe63 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1fe64 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 43 zColl = pTab->aC
1fe65 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 ol[j].zColl;.
1fe66 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 7b if( !zColl ){
1fe67 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d . zColl =
1fe68 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d 3e db->pDfltColl->
1fe69 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a 20 zName;. }.
1fe6a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64 62 }. if( !db
1fe6b 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 21 ->init.busy && !
1fe6c 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c sqlite3LocateCol
1fe6d 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f lSeq(pParse, zCo
1fe6e 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 ll, -1) ){.
1fe6f 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 goto exit_creat
1fe70 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 e_index;. }.
1fe71 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c pIndex->azCol
1fe72 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 l[i] = zColl;.
1fe73 20 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f requestedSortO
1fe74 72 64 65 72 20 3d 20 70 4c 69 73 74 49 74 65 6d rder = pListItem
1fe75 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 20 73 6f ->sortOrder & so
1fe76 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a 20 20 20 rtOrderMask;.
1fe77 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 pIndex->aSortOr
1fe78 64 65 72 5b 69 5d 20 3d 20 72 65 71 75 65 73 74 der[i] = request
1fe79 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d edSortOrder;. }
1fe7a 0a 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c . sqlite3Defaul
1fe7b 74 52 6f 77 45 73 74 28 70 49 6e 64 65 78 29 3b tRowEst(pIndex);
1fe7c 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 70 50 .. if( pTab==pP
1fe7d 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 arse->pNewTable
1fe7e 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 72 ){. /* This r
1fe7f 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 outine has been
1fe80 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 called to create
1fe81 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 69 6e an automatic in
1fe82 64 65 78 20 61 73 20 61 0a 20 20 20 20 2a 2a 20 dex as a. **
1fe83 72 65 73 75 6c 74 20 6f 66 20 61 20 50 52 49 4d result of a PRIM
1fe84 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 ARY KEY or UNIQU
1fe85 45 20 63 6c 61 75 73 65 20 6f 6e 20 61 20 63 6f E clause on a co
1fe86 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c lumn definition,
1fe87 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 50 52 49 or. ** a PRI
1fe88 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 MARY KEY or UNIQ
1fe89 55 45 20 63 6c 61 75 73 65 20 66 6f 6c 6c 6f 77 UE clause follow
1fe8a 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 ing the column d
1fe8b 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 20 20 20 20 efinitions..
1fe8c 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a 0a ** i.e. one of:.
1fe8d 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 52 **. ** CR
1fe8e 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 20 50 EATE TABLE t(x P
1fe8f 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b 0a RIMARY KEY, y);.
1fe90 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 ** CREATE TA
1fe91 42 4c 45 20 74 28 78 2c 20 79 2c 20 55 4e 49 51 BLE t(x, y, UNIQ
1fe92 55 45 28 78 2c 20 79 29 29 3b 0a 20 20 20 20 2a UE(x, y));. *
1fe93 2a 0a 20 20 20 20 2a 2a 20 45 69 74 68 65 72 20 *. ** Either
1fe94 77 61 79 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 way, check to se
1fe95 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 61 e if the table a
1fe96 6c 72 65 61 64 79 20 68 61 73 20 73 75 63 68 20 lready has such
1fe97 61 6e 20 69 6e 64 65 78 2e 20 49 66 0a 20 20 20 an index. If.
1fe98 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27 74 20 62 6f ** so, don't bo
1fe99 74 68 65 72 20 63 72 65 61 74 69 6e 67 20 74 68 ther creating th
1fe9a 69 73 20 6f 6e 65 2e 20 54 68 69 73 20 6f 6e 6c is one. This onl
1fe9b 79 20 61 70 70 6c 69 65 73 20 74 6f 0a 20 20 20 y applies to.
1fe9c 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ** automaticall
1fe9d 79 20 63 72 65 61 74 65 64 20 69 6e 64 69 63 65 y created indice
1fe9e 73 2e 20 55 73 65 72 73 20 63 61 6e 20 64 6f 20 s. Users can do
1fe9f 61 73 20 74 68 65 79 20 77 69 73 68 20 77 69 74 as they wish wit
1fea0 68 0a 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 h. ** explici
1fea1 74 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20 2a t indices.. *
1fea2 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 /. Index *pId
1fea3 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d x;. for(pIdx=
1fea4 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 pTab->pIndex; pI
1fea5 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 dx; pIdx=pIdx->p
1fea6 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e 74 Next){. int
1fea7 20 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 k;. assert
1fea8 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 ( pIdx->onError!
1fea9 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 20 20 20 20 =OE_None );.
1feaa 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e assert( pIdx->
1feab 61 75 74 6f 49 6e 64 65 78 20 29 3b 0a 20 20 20 autoIndex );.
1feac 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64 65 assert( pInde
1fead 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e x->onError!=OE_N
1feae 6f 6e 65 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 one );.. if
1feaf 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 21 ( pIdx->nColumn!
1feb0 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e =pIndex->nColumn
1feb1 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
1feb2 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 49 for(k=0; k<pI
1feb3 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b dx->nColumn; k++
1feb4 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 ){. const
1feb5 20 63 68 61 72 20 2a 7a 31 20 3d 20 70 49 64 78 char *z1 = pIdx
1feb6 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 ->azColl[k];.
1feb7 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1feb8 2a 7a 32 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a *z2 = pIndex->az
1feb9 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 Coll[k];.
1feba 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c if( pIdx->aiCol
1febb 75 6d 6e 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e umn[k]!=pIndex->
1febc 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72 aiColumn[k] ) br
1febd 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 eak;. if(
1febe 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 pIdx->aSortOrde
1febf 72 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 53 r[k]!=pIndex->aS
1fec0 6f 72 74 4f 72 64 65 72 5b 6b 5d 20 29 20 62 72 ortOrder[k] ) br
1fec1 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 eak;. if(
1fec2 20 7a 31 21 3d 7a 32 20 26 26 20 73 71 6c 69 74 z1!=z2 && sqlit
1fec3 65 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 e3StrICmp(z1, z2
1fec4 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 ) ) break;.
1fec5 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d }. if( k==
1fec6 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b pIdx->nColumn ){
1fec7 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 . if( pId
1fec8 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e 64 x->onError!=pInd
1fec9 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 ex->onError ){.
1feca 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
1fecb 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 72 65 61 constraint crea
1fecc 74 65 73 20 74 68 65 20 73 61 6d 65 20 69 6e 64 tes the same ind
1fecd 65 78 20 61 73 20 61 20 70 72 65 76 69 6f 75 73 ex as a previous
1fece 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f . ** co
1fecf 6e 73 74 72 61 69 6e 74 20 73 70 65 63 69 66 69 nstraint specifi
1fed0 65 64 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 ed somewhere in
1fed1 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 the CREATE TABLE
1fed2 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 statement..
1fed3 20 20 20 20 20 20 2a 2a 20 48 6f 77 65 76 65 72 ** However
1fed4 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 the ON CONFLICT
1fed5 20 63 6c 61 75 73 65 73 20 61 72 65 20 64 69 66 clauses are dif
1fed6 66 65 72 65 6e 74 2e 20 49 66 20 62 6f 74 68 20 ferent. If both
1fed7 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 20 20 this .
1fed8 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e ** constraint an
1fed9 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 d the previous e
1feda 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73 74 72 quivalent constr
1fedb 61 69 6e 74 20 68 61 76 65 20 65 78 70 6c 69 63 aint have explic
1fedc 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 it. **
1fedd 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 ON CONFLICT clau
1fede 73 65 73 20 74 68 69 73 20 69 73 20 61 6e 20 65 ses this is an e
1fedf 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c rror. Otherwise,
1fee0 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 20 use the.
1fee1 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 ** explicitly
1fee2 20 73 70 65 63 69 66 69 65 64 20 62 65 68 61 76 specified behav
1fee3 69 6f 75 72 20 66 6f 72 20 74 68 65 20 69 6e 64 iour for the ind
1fee4 65 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f ex.. */
1fee5 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 . if( !
1fee6 28 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d (pIdx->onError==
1fee7 4f 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20 70 49 OE_Default || pI
1fee8 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f ndex->onError==O
1fee9 45 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a 20 20 E_Default) ){.
1feea 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1feeb 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1feec 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
1feed 20 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e 67 20 "conflicting
1feee 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 ON CONFLICT clau
1feef 73 65 73 20 73 70 65 63 69 66 69 65 64 22 2c 20 ses specified",
1fef0 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0);. }.
1fef1 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 if( pI
1fef2 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f dx->onError==OE_
1fef3 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 Default ){.
1fef4 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e 45 pIdx->onE
1fef5 72 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e 6f rror = pIndex->o
1fef6 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 nError;.
1fef7 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1fef8 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f goto exit_
1fef9 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 create_index;.
1fefa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1fefb 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e . /* Link the n
1fefc 65 77 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 ew Index structu
1fefd 72 65 20 74 6f 20 69 74 73 20 74 61 62 6c 65 20 re to its table
1fefe 61 6e 64 20 74 6f 20 74 68 65 20 6f 74 68 65 72 and to the other
1feff 0a 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 . ** in-memory
1ff00 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 database structu
1ff01 72 65 73 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 res. . */. if(
1ff02 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 db->init.busy )
1ff03 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a {. Index *p;.
1ff04 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 p = sqlite3H
1ff05 61 73 68 49 6e 73 65 72 74 28 26 70 49 6e 64 65 ashInsert(&pInde
1ff06 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 x->pSchema->idxH
1ff07 61 73 68 2c 20 0a 20 20 20 20 20 20 20 20 20 20 ash, .
1ff08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
1ff09 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 73 74 Index->zName, st
1ff0a 72 6c 65 6e 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 rlen(pIndex->zNa
1ff0b 6d 65 29 2b 31 2c 20 70 49 6e 64 65 78 29 3b 0a me)+1, pIndex);.
1ff0c 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 if( p ){.
1ff0d 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 49 assert( p==pI
1ff0e 6e 64 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c ndex ); /* Mall
1ff0f 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61 69 oc must have fai
1ff10 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d led */. db-
1ff11 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
1ff12 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 1;. goto ex
1ff13 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b it_create_index;
1ff14 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 66 . }. db->f
1ff15 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 lags |= SQLITE_I
1ff16 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 nternChanges;.
1ff17 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21 3d if( pTblName!=
1ff18 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 0 ){. pInde
1ff19 78 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e x->tnum = db->in
1ff1a 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 20 20 it.newTnum;.
1ff1b 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
1ff1c 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 he db->init.busy
1ff1d 20 69 73 20 30 20 74 68 65 6e 20 63 72 65 61 74 is 0 then creat
1ff1e 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 64 e the index on d
1ff1f 69 73 6b 2e 20 20 54 68 69 73 0a 20 20 2a 2a 20 isk. This. **
1ff20 69 6e 76 6f 6c 76 65 73 20 77 72 69 74 69 6e 67 involves writing
1ff21 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 the index into
1ff22 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 the master table
1ff23 20 61 6e 64 20 66 69 6c 6c 69 6e 67 20 69 6e 20 and filling in
1ff24 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 77 the. ** index w
1ff25 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 ith the current
1ff26 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 73 2e 0a table contents..
1ff27 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 62 **. ** The db
1ff28 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30 ->init.busy is 0
1ff29 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20 66 when the user f
1ff2a 69 72 73 74 20 65 6e 74 65 72 73 20 61 20 43 52 irst enters a CR
1ff2b 45 41 54 45 20 49 4e 44 45 58 20 0a 20 20 2a 2a EATE INDEX . **
1ff2c 20 63 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e 69 command. db->i
1ff2d 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 77 68 nit.busy is 1 wh
1ff2e 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 69 73 en a database is
1ff2f 20 6f 70 65 6e 65 64 20 61 6e 64 20 0a 20 20 2a opened and . *
1ff30 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 * CREATE INDEX s
1ff31 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 72 65 tatements are re
1ff32 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 ad out of the ma
1ff33 73 74 65 72 20 74 61 62 6c 65 2e 20 20 49 6e 0a ster table. In.
1ff34 20 20 2a 2a 20 74 68 65 20 6c 61 74 74 65 72 20 ** the latter
1ff35 63 61 73 65 20 74 68 65 20 69 6e 64 65 78 20 61 case the index a
1ff36 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 6f 6e lready exists on
1ff37 20 64 69 73 6b 2c 20 77 68 69 63 68 20 69 73 20 disk, which is
1ff38 77 68 79 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27 why. ** we don'
1ff39 74 20 77 61 6e 74 20 74 6f 20 72 65 63 72 65 61 t want to recrea
1ff3a 74 65 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a te it.. **. **
1ff3b 20 49 66 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 If pTblName==0
1ff3c 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 6e it means this in
1ff3d 64 65 78 20 69 73 20 67 65 6e 65 72 61 74 65 64 dex is generated
1ff3e 20 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 as a primary ke
1ff3f 79 0a 20 20 2a 2a 20 6f 72 20 55 4e 49 51 55 45 y. ** or UNIQUE
1ff40 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 61 constraint of a
1ff41 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 CREATE TABLE st
1ff42 61 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65 20 atement. Since
1ff43 74 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20 68 the table. ** h
1ff44 61 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72 65 as just been cre
1ff45 61 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69 6e ated, it contain
1ff46 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74 68 s no data and th
1ff47 65 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c 69 e index initiali
1ff48 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65 70 zation. ** step
1ff49 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64 2e can be skipped.
1ff4a 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 . */. else if(
1ff4b 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d db->init.busy==
1ff4c 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 0 ){. Vdbe *v
1ff4d 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 6d ;. char *zStm
1ff4e 74 3b 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 t;. int iMem
1ff4f 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
1ff50 3b 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 ;.. v = sqlit
1ff51 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
1ff52 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 );. if( v==0
1ff53 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 ) goto exit_crea
1ff54 74 65 5f 69 6e 64 65 78 3b 0a 0a 0a 20 20 20 20 te_index;...
1ff55 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 72 6f /* Create the ro
1ff56 6f 74 70 61 67 65 20 66 6f 72 20 74 68 65 20 69 otpage for the i
1ff57 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ndex. */.
1ff58 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 sqlite3BeginWrit
1ff59 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 eOperation(pPars
1ff5a 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 e, 1, iDb);.
1ff5b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1ff5c 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 49 6e 2(v, OP_CreateIn
1ff5d 64 65 78 2c 20 69 44 62 2c 20 69 4d 65 6d 29 3b dex, iDb, iMem);
1ff5e 0a 0a 20 20 20 20 2f 2a 20 47 61 74 68 65 72 20 .. /* Gather
1ff5f 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 the complete tex
1ff60 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 t of the CREATE
1ff61 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 INDEX statement
1ff62 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 into. ** the
1ff63 7a 53 74 6d 74 20 76 61 72 69 61 62 6c 65 0a 20 zStmt variable.
1ff64 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 */. if( pS
1ff65 74 61 72 74 20 26 26 20 70 45 6e 64 20 29 7b 0a tart && pEnd ){.
1ff66 20 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d 65 64 /* A named
1ff67 20 69 6e 64 65 78 20 77 69 74 68 20 61 6e 20 65 index with an e
1ff68 78 70 6c 69 63 69 74 20 43 52 45 41 54 45 20 49 xplicit CREATE I
1ff69 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a NDEX statement *
1ff6a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 /. zStmt =
1ff6b 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1ff6c 62 2c 20 22 43 52 45 41 54 45 25 73 20 49 4e 44 b, "CREATE%s IND
1ff6d 45 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20 20 20 EX %.*s",.
1ff6e 20 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f onError==OE_No
1ff6f 6e 65 20 3f 20 22 22 20 3a 20 22 20 55 4e 49 51 ne ? "" : " UNIQ
1ff70 55 45 22 2c 0a 20 20 20 20 20 20 20 20 70 45 6e UE",. pEn
1ff71 64 2d 3e 7a 20 2d 20 70 4e 61 6d 65 2d 3e 7a 20 d->z - pName->z
1ff72 2b 20 31 2c 0a 20 20 20 20 20 20 20 20 70 4e 61 + 1,. pNa
1ff73 6d 65 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 me->z);. }els
1ff74 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 61 e{. /* An a
1ff75 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 63 utomatic index c
1ff76 72 65 61 74 65 64 20 62 79 20 61 20 50 52 49 4d reated by a PRIM
1ff77 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 ARY KEY or UNIQU
1ff78 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a E constraint */.
1ff79 20 20 20 20 20 20 2f 2a 20 7a 53 74 6d 74 20 3d /* zStmt =
1ff7a 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1ff7b 22 22 29 3b 20 2a 2f 0a 20 20 20 20 20 20 7a 53 ""); */. zS
1ff7c 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a tmt = 0;. }..
1ff7d 20 20 20 20 2f 2a 20 41 64 64 20 61 6e 20 65 6e /* Add an en
1ff7e 74 72 79 20 69 6e 20 73 71 6c 69 74 65 5f 6d 61 try in sqlite_ma
1ff7f 73 74 65 72 20 66 6f 72 20 74 68 69 73 20 69 6e ster for this in
1ff80 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 dex. */. s
1ff81 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 qlite3NestedPars
1ff82 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 e(pParse, .
1ff83 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 "INSERT INTO
1ff84 25 51 2e 25 73 20 56 41 4c 55 45 53 28 27 69 6e %Q.%s VALUES('in
1ff85 64 65 78 27 2c 25 51 2c 25 51 2c 23 25 64 2c 25 dex',%Q,%Q,#%d,%
1ff86 51 29 3b 22 2c 0a 20 20 20 20 20 20 20 20 64 62 Q);",. db
1ff87 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 ->aDb[iDb].zName
1ff88 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 , SCHEMA_TABLE(i
1ff89 44 62 29 2c 0a 20 20 20 20 20 20 20 20 70 49 6e Db),. pIn
1ff8a 64 65 78 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 dex->zName,.
1ff8b 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c pTab->zName,
1ff8c 0a 20 20 20 20 20 20 20 20 69 4d 65 6d 2c 0a 20 . iMem,.
1ff8d 20 20 20 20 20 20 20 7a 53 74 6d 74 0a 20 20 20 zStmt.
1ff8e 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 );. sqlite3D
1ff8f 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 bFree(db, zStmt)
1ff90 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6c 6c 20 74 ;.. /* Fill t
1ff91 68 65 20 69 6e 64 65 78 20 77 69 74 68 20 64 61 he index with da
1ff92 74 61 20 61 6e 64 20 72 65 70 61 72 73 65 20 74 ta and reparse t
1ff93 68 65 20 73 63 68 65 6d 61 2e 20 43 6f 64 65 20 he schema. Code
1ff94 61 6e 20 4f 50 5f 45 78 70 69 72 65 0a 20 20 20 an OP_Expire.
1ff95 20 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 ** to invalidat
1ff96 65 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70 69 6c e all pre-compil
1ff97 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 ed statements..
1ff98 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 */. if( pT
1ff99 62 6c 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 blName ){.
1ff9a 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 sqlite3RefillInd
1ff9b 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 ex(pParse, pInde
1ff9c 78 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 x, iMem);.
1ff9d 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f sqlite3ChangeCoo
1ff9e 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 kie(pParse, iDb)
1ff9f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1ffa0 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
1ffa1 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 ParseSchema, iDb
1ffa2 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 , 0, 0,.
1ffa3 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
1ffa4 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c db, "name='%q'",
1ffa5 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c pIndex->zName),
1ffa6 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 P4_DYNAMIC);.
1ffa7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
1ffa8 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70 69 ddOp1(v, OP_Expi
1ffa9 72 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 re, 0);. }.
1ffaa 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61 64 64 }.. /* When add
1ffab 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 ing an index to
1ffac 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 64 69 the list of indi
1ffad 63 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 2c ces for a table,
1ffae 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 make. ** sure
1ffaf 61 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61 62 65 all indices labe
1ffb0 6c 65 64 20 4f 45 5f 52 65 70 6c 61 63 65 20 63 led OE_Replace c
1ffb1 6f 6d 65 20 61 66 74 65 72 20 61 6c 6c 20 74 68 ome after all th
1ffb2 6f 73 65 20 6c 61 62 65 6c 65 64 0a 20 20 2a 2a ose labeled. **
1ffb3 20 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54 68 69 OE_Ignore. Thi
1ffb4 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 66 s is necessary f
1ffb5 6f 72 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f or the correct o
1ffb6 70 65 72 61 74 69 6f 6e 20 6f 66 20 55 50 44 41 peration of UPDA
1ffb7 54 45 0a 20 20 2a 2a 20 61 6e 64 20 49 4e 53 45 TE. ** and INSE
1ffb8 52 54 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 RT.. */. if( d
1ffb9 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 b->init.busy ||
1ffba 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 pTblName==0 ){.
1ffbb 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d if( onError!=
1ffbc 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54 OE_Replace || pT
1ffbd 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a 20 20 ab->pIndex==0.
1ffbe 20 20 20 20 20 20 20 7c 7c 20 70 54 61 62 2d 3e || pTab->
1ffbf 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d pIndex->onError=
1ffc0 3d 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0a 20 20 =OE_Replace){.
1ffc1 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 pIndex->pNex
1ffc2 74 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 t = pTab->pIndex
1ffc3 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 49 ;. pTab->pI
1ffc4 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20 ndex = pIndex;.
1ffc5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1ffc6 49 6e 64 65 78 20 2a 70 4f 74 68 65 72 20 3d 20 Index *pOther =
1ffc7 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 pTab->pIndex;.
1ffc8 20 20 20 20 77 68 69 6c 65 28 20 70 4f 74 68 65 while( pOthe
1ffc9 72 2d 3e 70 4e 65 78 74 20 26 26 20 70 4f 74 68 r->pNext && pOth
1ffca 65 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72 72 er->pNext->onErr
1ffcb 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 or!=OE_Replace )
1ffcc 7b 0a 20 20 20 20 20 20 20 20 70 4f 74 68 65 72 {. pOther
1ffcd 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 = pOther->pNext
1ffce 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1ffcf 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 pIndex->pNext =
1ffd0 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 pOther->pNext;.
1ffd1 20 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 pOther->pNe
1ffd2 78 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 xt = pIndex;.
1ffd3 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20 }. pIndex =
1ffd4 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 0;. }.. /* Cle
1ffd5 61 6e 20 75 70 20 62 65 66 6f 72 65 20 65 78 69 an up before exi
1ffd6 74 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63 72 65 ting */.exit_cre
1ffd7 61 74 65 5f 69 6e 64 65 78 3a 0a 20 20 69 66 28 ate_index:. if(
1ffd8 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 66 pIndex ){. f
1ffd9 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29 reeIndex(pIndex)
1ffda 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 ;. }. sqlite3E
1ffdb 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 xprListDelete(db
1ffdc 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 , pList);. sqli
1ffdd 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 te3SrcListDelete
1ffde 28 64 62 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a (db, pTblName);.
1ffdf 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1ffe0 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 db, zName);. re
1ffe1 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 turn;.}../*.** G
1ffe2 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
1ffe3 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 66 69 make sure the fi
1ffe4 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65 72 le format number
1ffe5 20 69 73 20 61 74 20 6c 65 61 73 74 20 6d 69 6e is at least min
1ffe6 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68 65 20 67 Format..** The g
1ffe7 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 77 69 enerated code wi
1ffe8 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 ll increase the
1ffe9 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 file format numb
1ffea 65 72 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e er if necessary.
1ffeb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1ffec 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
1ffed 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 inimumFileFormat
1ffee 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
1ffef 69 6e 74 20 69 44 62 2c 20 69 6e 74 20 6d 69 6e int iDb, int min
1fff0 46 6f 72 6d 61 74 29 7b 0a 20 20 56 64 62 65 20 Format){. Vdbe
1fff1 2a 76 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 *v;. v = sqlite
1fff2 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
1fff3 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 ;. if( v ){.
1fff4 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 int r1 = sqlite
1fff5 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 3GetTempReg(pPar
1fff6 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 72 32 20 se);. int r2
1fff7 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 = sqlite3GetTemp
1fff8 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 Reg(pParse);.
1fff9 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 73 71 6c int j1;. sql
1fffa 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1fffb 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c , OP_ReadCookie,
1fffc 20 69 44 62 2c 20 72 31 2c 20 31 29 3b 0a 20 20 iDb, r1, 1);.
1fffd 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 sqlite3VdbeUse
1fffe 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a sBtree(v, iDb);.
1ffff 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
20000 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
20001 67 65 72 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20 ger, minFormat,
20002 72 32 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 r2);. j1 = sq
20003 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
20004 76 2c 20 4f 50 5f 47 65 2c 20 72 32 2c 20 30 2c v, OP_Ge, r2, 0,
20005 20 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 r1);. sqlite
20006 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
20007 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 P_SetCookie, iDb
20008 2c 20 31 2c 20 72 32 29 3b 0a 20 20 20 20 73 71 , 1, r2);. sq
20009 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
2000a 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71 e(v, j1);. sq
2000b 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
2000c 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b Reg(pParse, r1);
2000d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 . sqlite3Rele
2000e 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
2000f 65 2c 20 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f e, r2);. }.}../
20010 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e *.** Fill the In
20011 64 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d 20 61 dex.aiRowEst[] a
20012 72 72 61 79 20 77 69 74 68 20 64 65 66 61 75 6c rray with defaul
20013 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 t information -
20014 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 74 information.** t
20015 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 77 o be used when w
20016 65 20 68 61 76 65 20 6e 6f 74 20 72 75 6e 20 74 e have not run t
20017 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 he ANALYZE comma
20018 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61 69 52 6f 77 45 nd..**.** aiRowE
20019 73 74 5b 30 5d 20 69 73 20 73 75 70 70 6f 73 65 st[0] is suppose
2001a 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 to contain the
2001b 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e number of elemen
2001c 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e ts in the index.
2001d 0a 2a 2a 20 53 69 6e 63 65 20 77 65 20 64 6f 20 .** Since we do
2001e 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 65 73 73 20 not know, guess
2001f 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 61 69 52 6f 1 million. aiRo
20020 77 45 73 74 5b 31 5d 20 69 73 20 61 6e 20 65 73 wEst[1] is an es
20021 74 69 6d 61 74 65 20 6f 66 20 74 68 65 0a 2a 2a timate of the.**
20022 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 number of rows
20023 69 6e 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 in the table tha
20024 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72 74 t match any part
20025 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 20 icular value of
20026 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c the.** first col
20027 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 umn of the index
20028 2e 20 20 61 69 52 6f 77 45 73 74 5b 32 5d 20 69 . aiRowEst[2] i
20029 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 s an estimate of
2002a 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f the number.** o
2002b 66 20 72 6f 77 73 20 74 68 61 74 20 6d 61 74 63 f rows that matc
2002c 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 72 h any particular
2002d 20 63 6f 6d 62 69 6e 69 61 74 69 6f 6e 20 6f 66 combiniation of
2002e 20 74 68 65 20 66 69 72 73 74 20 32 20 63 6f 6c the first 2 col
2002f 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 20 69 umns.** of the i
20030 6e 64 65 78 2e 20 20 41 6e 64 20 73 6f 20 66 6f ndex. And so fo
20031 72 74 68 2e 20 20 49 74 20 6d 75 73 74 20 61 6c rth. It must al
20032 77 61 79 73 20 62 65 20 74 68 65 20 63 61 73 65 ways be the case
20033 20 74 68 61 74 0a 2a 0a 2a 2a 20 20 20 20 20 20 that.*.**
20034 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d aiRowEst[N]
20035 3c 3d 61 69 52 6f 77 45 73 74 5b 4e 2d 31 5d 0a <=aiRowEst[N-1].
20036 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 52 ** aiR
20037 6f 77 45 73 74 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a owEst[N]>=1.**.*
20038 2a 20 41 70 61 72 74 20 66 72 6f 6d 20 74 68 61 * Apart from tha
20039 74 2c 20 77 65 20 68 61 76 65 20 6c 69 74 74 6c t, we have littl
2003a 65 20 74 6f 20 67 6f 20 6f 6e 20 62 65 73 69 64 e to go on besid
2003b 65 73 20 69 6e 74 75 69 74 69 6f 6e 20 61 73 20 es intuition as
2003c 74 6f 0a 2a 2a 20 68 6f 77 20 61 69 52 6f 77 45 to.** how aiRowE
2003d 73 74 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 69 st[] should be i
2003e 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 54 68 65 nitialized. The
2003f 20 6e 75 6d 62 65 72 73 20 67 65 6e 65 72 61 74 numbers generat
20040 65 64 20 68 65 72 65 0a 2a 2a 20 61 72 65 20 62 ed here.** are b
20041 61 73 65 64 20 6f 6e 20 74 79 70 69 63 61 6c 20 ased on typical
20042 76 61 6c 75 65 73 20 66 6f 75 6e 64 20 69 6e 20 values found in
20043 61 63 74 75 61 6c 20 69 6e 64 69 63 65 73 2e 0a actual indices..
20044 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20045 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 E void sqlite3De
20046 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 65 faultRowEst(Inde
20047 78 20 2a 70 49 64 78 29 7b 0a 20 20 75 6e 73 69 x *pIdx){. unsi
20048 67 6e 65 64 20 2a 61 20 3d 20 70 49 64 78 2d 3e gned *a = pIdx->
20049 61 69 52 6f 77 45 73 74 3b 0a 20 20 69 6e 74 20 aiRowEst;. int
2004a 69 3b 0a 20 20 61 73 73 65 72 74 28 20 61 21 3d i;. assert( a!=
2004b 30 20 29 3b 0a 20 20 61 5b 30 5d 20 3d 20 31 30 0 );. a[0] = 10
2004c 30 30 30 30 30 3b 0a 20 20 66 6f 72 28 69 3d 70 00000;. for(i=p
2004d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 3e Idx->nColumn; i>
2004e 3d 35 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 61 5b =5; i--){. a[
2004f 69 5d 20 3d 20 35 3b 0a 20 20 7d 0a 20 20 77 68 i] = 5;. }. wh
20050 69 6c 65 28 20 69 3e 3d 31 20 29 7b 0a 20 20 20 ile( i>=1 ){.
20051 20 61 5b 69 5d 20 3d 20 31 31 20 2d 20 69 3b 0a a[i] = 11 - i;.
20052 20 20 20 20 69 2d 2d 3b 0a 20 20 7d 0a 20 20 69 i--;. }. i
20053 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 f( pIdx->onError
20054 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 !=OE_None ){.
20055 20 61 5b 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e a[pIdx->nColumn
20056 5d 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ] = 1;. }.}../*
20057 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
20058 20 77 69 6c 6c 20 64 72 6f 70 20 61 6e 20 65 78 will drop an ex
20059 69 73 74 69 6e 67 20 6e 61 6d 65 64 20 69 6e 64 isting named ind
2005a 65 78 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e ex. This routin
2005b 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 73 20 e.** implements
2005c 74 68 65 20 44 52 4f 50 20 49 4e 44 45 58 20 73 the DROP INDEX s
2005d 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c tatement..*/.SQL
2005e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
2005f 20 73 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 sqlite3DropInde
20060 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c x(Parse *pParse,
20061 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c SrcList *pName,
20062 20 69 6e 74 20 69 66 45 78 69 73 74 73 29 7b 0a int ifExists){.
20063 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b Index *pIndex;
20064 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 . Vdbe *v;. sq
20065 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
20066 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 se->db;. int iD
20067 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 b;.. if( pParse
20068 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 ->nErr || db->ma
20069 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
2006a 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 goto exit_drop
2006b 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61 73 _index;. }. as
2006c 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 sert( pName->nSr
2006d 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53 51 c==1 );. if( SQ
2006e 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 LITE_OK!=sqlite3
2006f 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 ReadSchema(pPars
20070 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 e) ){. goto e
20071 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a xit_drop_index;.
20072 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 }. pIndex = s
20073 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 qlite3FindIndex(
20074 64 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e db, pName->a[0].
20075 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b zName, pName->a[
20076 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 0].zDatabase);.
20077 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29 if( pIndex==0 )
20078 7b 0a 20 20 20 20 69 66 28 20 21 69 66 45 78 69 {. if( !ifExi
20079 73 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c sts ){. sql
2007a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
2007b 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e rse, "no such in
2007c 64 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65 2c dex: %S", pName,
2007d 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 0);. }. p
2007e 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 Parse->checkSche
2007f 6d 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f ma = 1;. goto
20080 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 exit_drop_index
20081 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 64 ;. }. if( pInd
20082 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 7b ex->autoIndex ){
20083 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
20084 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e rMsg(pParse, "in
20085 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 dex associated w
20086 69 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20 20 ith UNIQUE ".
20087 20 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20 4b "or PRIMARY K
20088 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 EY constraint ca
20089 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22 nnot be dropped"
2008a 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 , 0);. goto e
2008b 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a xit_drop_index;.
2008c 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 }. iDb = sqli
2008d 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
2008e 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 (db, pIndex->pSc
2008f 68 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20 53 hema);.#ifndef S
20090 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
20091 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 RIZATION. {.
20092 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 int code = SQLI
20093 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a 20 TE_DROP_INDEX;.
20094 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d Table *pTab =
20095 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b pIndex->pTable;
20096 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
20097 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 *zDb = db->aDb[i
20098 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 Db].zName;. c
20099 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 onst char *zTab
2009a 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 = SCHEMA_TABLE(i
2009b 44 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c Db);. if( sql
2009c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
2009d 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c arse, SQLITE_DEL
2009e 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 ETE, zTab, 0, zD
2009f 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f b) ){. goto
200a0 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 exit_drop_index
200a1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
200a2 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 !OMIT_TEMPDB &&
200a3 69 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51 4c iDb ) code = SQL
200a4 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e ITE_DROP_TEMP_IN
200a5 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c DEX;. if( sql
200a6 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
200a7 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e 64 arse, code, pInd
200a8 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d ex->zName, pTab-
200a9 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a >zName, zDb) ){.
200aa 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f goto exit_
200ab 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20 drop_index;.
200ac 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }. }.#endif..
200ad 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 /* Generate code
200ae 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 69 to remove the i
200af 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 74 68 ndex and from th
200b0 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a e master table *
200b1 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 /. v = sqlite3G
200b2 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
200b3 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 if( v ){. s
200b4 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 qlite3BeginWrite
200b5 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 Operation(pParse
200b6 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 , 1, iDb);. s
200b7 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 qlite3NestedPars
200b8 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 e(pParse,.
200b9 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 "DELETE FROM %Q
200ba 2e 25 73 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 .%s WHERE name=%
200bb 51 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 Q",. db->a
200bc 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 Db[iDb].zName, S
200bd 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 CHEMA_TABLE(iDb)
200be 2c 0a 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d ,. pIndex-
200bf 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 >zName. );.
200c0 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e if( sqlite3Fin
200c1 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 dTable(db, "sqli
200c2 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e 61 te_stat1", db->a
200c3 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29 Db[iDb].zName) )
200c4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e {. sqlite3N
200c5 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 estedParse(pPars
200c6 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 e,. "DELE
200c7 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 TE FROM %Q.sqlit
200c8 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 69 64 e_stat1 WHERE id
200c9 78 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 64 x=%Q",. d
200ca 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d b->aDb[iDb].zNam
200cb 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 e, pIndex->zName
200cc 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a . );. }.
200cd 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 sqlite3Chang
200ce 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 eCookie(pParse,
200cf 69 44 62 29 3b 0a 20 20 20 20 64 65 73 74 72 6f iDb);. destro
200d0 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 yRootPage(pParse
200d1 2c 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c 20 , pIndex->tnum,
200d2 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 iDb);. sqlite
200d3 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
200d4 50 5f 44 72 6f 70 49 6e 64 65 78 2c 20 69 44 62 P_DropIndex, iDb
200d5 2c 20 30 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e , 0, 0, pIndex->
200d6 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 0a zName, 0);. }..
200d7 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3a exit_drop_index:
200d8 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 . sqlite3SrcLis
200d9 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d tDelete(db, pNam
200da 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41 72 e);.}../*.** pAr
200db 72 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ray is a pointer
200dc 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 to an array of
200dd 6f 62 6a 65 63 74 73 2e 20 20 45 61 63 68 20 6f objects. Each o
200de 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a 20 bject in the.**
200df 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72 79 array is szEntry
200e0 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 bytes in size.
200e1 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c This routine al
200e2 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a locates a new.**
200e3 20 6f 62 6a 65 63 74 20 6f 6e 20 74 68 65 20 65 object on the e
200e4 6e 64 20 6f 66 20 74 68 65 20 61 72 72 61 79 2e nd of the array.
200e5 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e 74 72 79 20 .**.** *pnEntry
200e6 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
200e7 20 65 6e 74 72 69 65 73 20 61 6c 72 65 61 64 79 entries already
200e8 20 69 6e 20 75 73 65 2e 20 20 2a 70 6e 41 6c 6c in use. *pnAll
200e9 6f 63 20 69 73 0a 2a 2a 20 74 68 65 20 70 72 65 oc is.** the pre
200ea 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 viously allocate
200eb 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 d size of the ar
200ec 72 61 79 2e 20 20 69 6e 69 74 53 69 7a 65 20 69 ray. initSize i
200ed 73 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 s the.** suggest
200ee 65 64 20 69 6e 69 74 69 61 6c 20 61 72 72 61 79 ed initial array
200ef 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e size allocation
200f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 ..**.** The inde
200f1 78 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e 74 x of the new ent
200f2 72 79 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 ry is returned i
200f3 6e 20 2a 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 54 n *pIdx..**.** T
200f4 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
200f5 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
200f6 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 62 the array of ob
200f7 6a 65 63 74 73 2e 20 20 54 68 69 73 0a 2a 2a 20 jects. This.**
200f8 6d 69 67 68 74 20 62 65 20 74 68 65 20 73 61 6d might be the sam
200f9 65 20 61 73 20 74 68 65 20 70 41 72 72 61 79 20 e as the pArray
200fa 70 61 72 61 6d 65 74 65 72 20 6f 72 20 69 74 20 parameter or it
200fb 6d 69 67 68 74 20 62 65 20 61 20 64 69 66 66 65 might be a diffe
200fc 72 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 rent.** pointer
200fd 69 66 20 74 68 65 20 61 72 72 61 79 20 77 61 73 if the array was
200fe 20 72 65 73 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c resized..*/.SQL
200ff 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
20100 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c *sqlite3ArrayAl
20101 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74 65 locate(. sqlite
20102 33 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 43 3 *db, /* C
20103 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 onnection to not
20104 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 ify of malloc fa
20105 69 6c 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69 64 ilures */. void
20106 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f 2a *pArray, /*
20107 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 Array of object
20108 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65 61 s. Might be rea
20109 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69 6e llocated */. in
2010a 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20 20 t szEntry,
2010b 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 /* Size of each
2010c 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61 72 object in the ar
2010d 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 69 ray */. int ini
2010e 74 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53 75 tSize, /* Su
2010f 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c 20 ggested initial
20110 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 65 allocation, in e
20111 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 lements */. int
20112 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 20 2f *pnEntry, /
20113 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 * Number of obje
20114 63 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e cts currently in
20115 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 use */. int *p
20116 6e 41 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20 43 nAlloc, /* C
20117 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 urrent size of t
20118 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 he allocation, i
20119 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 n elements */.
2011a 69 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20 20 int *pIdx
2011b 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 69 /* Write the i
2011c 6e 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73 6c ndex of a new sl
2011d 6f 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ot here */.){.
2011e 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 2a char *z;. if( *
2011f 70 6e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41 6c pnEntry >= *pnAl
20120 6c 6f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 loc ){. void
20121 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e *pNew;. int n
20122 65 77 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77 53 ewSize;. newS
20123 69 7a 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63 29 ize = (*pnAlloc)
20124 2a 32 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a 20 *2 + initSize;.
20125 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 pNew = sqlite
20126 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 3DbRealloc(db, p
20127 41 72 72 61 79 2c 20 6e 65 77 53 69 7a 65 2a 73 Array, newSize*s
20128 7a 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 28 zEntry);. if(
20129 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 pNew==0 ){.
2012a 20 20 2a 70 49 64 78 20 3d 20 2d 31 3b 0a 20 20 *pIdx = -1;.
2012b 20 20 20 20 72 65 74 75 72 6e 20 70 41 72 72 61 return pArra
2012c 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 6e y;. }. *pn
2012d 41 6c 6c 6f 63 20 3d 20 6e 65 77 53 69 7a 65 3b Alloc = newSize;
2012e 0a 20 20 20 20 70 41 72 72 61 79 20 3d 20 70 4e . pArray = pN
2012f 65 77 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 ew;. }. z = (c
20130 68 61 72 2a 29 70 41 72 72 61 79 3b 0a 20 20 6d har*)pArray;. m
20131 65 6d 73 65 74 28 26 7a 5b 2a 70 6e 45 6e 74 72 emset(&z[*pnEntr
20132 79 20 2a 20 73 7a 45 6e 74 72 79 5d 2c 20 30 2c y * szEntry], 0,
20133 20 73 7a 45 6e 74 72 79 29 3b 0a 20 20 2a 70 49 szEntry);. *pI
20134 64 78 20 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a 20 dx = *pnEntry;.
20135 20 2b 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 72 ++*pnEntry;. r
20136 65 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 7d 0a eturn pArray;.}.
20137 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 ./*.** Append a
20138 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 new element to t
20139 68 65 20 67 69 76 65 6e 20 49 64 4c 69 73 74 2e he given IdList.
2013a 20 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 49 Create a new I
2013b 64 4c 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 dList if.** need
2013c 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 be..**.** A new
2013d 20 49 64 4c 69 73 74 20 69 73 20 72 65 74 75 72 IdList is retur
2013e 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 ned, or NULL if
2013f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a malloc() fails..
20140 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20141 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 E IdList *sqlite
20142 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 3IdListAppend(sq
20143 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 lite3 *db, IdLis
20144 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 t *pList, Token
20145 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 *pToken){. int
20146 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d i;. if( pList==
20147 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 0 ){. pList =
20148 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
20149 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
2014a 49 64 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 IdList) );. i
2014b 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 f( pList==0 ) re
2014c 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73 turn 0;. pLis
2014d 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20 t->nAlloc = 0;.
2014e 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 }. pList->a =
2014f 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f sqlite3ArrayAllo
20150 63 61 74 65 28 0a 20 20 20 20 20 20 64 62 2c 0a cate(. db,.
20151 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c 0a pList->a,.
20152 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 69 sizeof(pLi
20153 73 74 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 20 st->a[0]),.
20154 20 35 2c 0a 20 20 20 20 20 20 26 70 4c 69 73 74 5,. &pList
20155 2d 3e 6e 49 64 2c 0a 20 20 20 20 20 20 26 70 4c ->nId,. &pL
20156 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20 20 20 ist->nAlloc,.
20157 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 69 66 28 &i. );. if(
20158 20 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 i<0 ){. sqli
20159 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 te3IdListDelete(
2015a 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 db, pList);.
2015b 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
2015c 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d pList->a[i].zNam
2015d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 e = sqlite3NameF
2015e 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f romToken(db, pTo
2015f 6b 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ken);. return p
20160 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 List;.}../*.** D
20161 65 6c 65 74 65 20 61 6e 20 49 64 4c 69 73 74 2e elete an IdList.
20162 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20163 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 TE void sqlite3I
20164 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 dListDelete(sqli
20165 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 te3 *db, IdList
20166 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 *pList){. int i
20167 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 ;. if( pList==0
20168 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 ) return;. for
20169 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e (i=0; i<pList->n
2016a 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 Id; i++){. sq
2016b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
2016c 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d pList->a[i].zNam
2016d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 e);. }. sqlite
2016e 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 3DbFree(db, pLis
2016f 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 t->a);. sqlite3
20170 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 DbFree(db, pList
20171 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
20172 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 rn the index in
20173 70 4c 69 73 74 20 6f 66 20 74 68 65 20 69 64 65 pList of the ide
20174 6e 74 69 66 69 65 72 20 6e 61 6d 65 64 20 7a 49 ntifier named zI
20175 64 2e 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a d. Return -1.**
20176 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a if not found..*
20177 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20178 20 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 int sqlite3IdLi
20179 73 74 49 6e 64 65 78 28 49 64 4c 69 73 74 20 2a stIndex(IdList *
2017a 70 4c 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 61 pList, const cha
2017b 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 r *zName){. int
2017c 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d i;. if( pList=
2017d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a =0 ) return -1;.
2017e 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 for(i=0; i<pLi
2017f 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 st->nId; i++){.
20180 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 if( sqlite3St
20181 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 rICmp(pList->a[i
20182 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d ].zName, zName)=
20183 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 =0 ) return i;.
20184 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a }. return -1;.
20185 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 61 6e 64 20 }../*.** Expand
20186 74 68 65 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 the space alloca
20187 74 65 64 20 66 6f 72 20 74 68 65 20 67 69 76 65 ted for the give
20188 6e 20 53 72 63 4c 69 73 74 20 6f 62 6a 65 63 74 n SrcList object
20189 20 62 79 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20 by.** creating
2018a 6e 45 78 74 72 61 20 6e 65 77 20 73 6c 6f 74 73 nExtra new slots
2018b 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 69 53 beginning at iS
2018c 74 61 72 74 2e 20 20 69 53 74 61 72 74 20 69 73 tart. iStart is
2018d 20 7a 65 72 6f 20 62 61 73 65 64 2e 0a 2a 2a 20 zero based..**
2018e 4e 65 77 20 73 6c 6f 74 73 20 61 72 65 20 7a 65 New slots are ze
2018f 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 roed..**.** For
20190 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 example, suppose
20191 20 61 20 53 72 63 4c 69 73 74 20 69 6e 69 74 69 a SrcList initi
20192 61 6c 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74 77 ally contains tw
20193 6f 20 65 6e 74 72 69 65 73 3a 20 41 2c 42 2e 0a o entries: A,B..
20194 2a 2a 20 54 6f 20 61 70 70 65 6e 64 20 33 20 6e ** To append 3 n
20195 65 77 20 65 6e 74 72 69 65 73 20 6f 6e 74 6f 20 ew entries onto
20196 74 68 65 20 65 6e 64 2c 20 64 6f 20 74 68 69 73 the end, do this
20197 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 74 :.**.** sqlit
20198 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 e3SrcListEnlarge
20199 28 64 62 2c 20 70 53 72 63 6c 69 73 74 2c 20 33 (db, pSrclist, 3
2019a 2c 20 32 29 3b 0a 2a 2a 0a 2a 2a 20 41 66 74 65 , 2);.**.** Afte
2019b 72 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 r the call above
2019c 20 69 74 20 77 6f 75 6c 64 20 63 6f 6e 74 61 69 it would contai
2019d 6e 3a 20 20 41 2c 20 42 2c 20 6e 69 6c 2c 20 6e n: A, B, nil, n
2019e 69 6c 2c 20 6e 69 6c 2e 0a 2a 2a 20 49 66 20 74 il, nil..** If t
2019f 68 65 20 69 53 74 61 72 74 20 61 72 67 75 6d 65 he iStart argume
201a0 6e 74 20 68 61 64 20 62 65 65 6e 20 31 20 69 6e nt had been 1 in
201a1 73 74 65 61 64 20 6f 66 20 32 2c 20 74 68 65 6e stead of 2, then
201a2 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 77 the result.** w
201a3 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 3a 20 ould have been:
201a4 20 41 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 A, nil, nil, ni
201a5 6c 2c 20 42 2e 20 20 54 6f 20 70 72 65 70 65 6e l, B. To prepen
201a6 64 20 74 68 65 20 6e 65 77 20 73 6c 6f 74 73 2c d the new slots,
201a7 0a 2a 2a 20 74 68 65 20 69 53 74 61 72 74 20 76 .** the iStart v
201a8 61 6c 75 65 20 77 6f 75 6c 64 20 62 65 20 30 2e alue would be 0.
201a9 20 20 54 68 65 20 72 65 73 75 6c 74 20 74 68 65 The result the
201aa 6e 20 77 6f 75 6c 64 0a 2a 2a 20 62 65 3a 20 6e n would.** be: n
201ab 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 41 2c il, nil, nil, A,
201ac 20 42 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d B..**.** If a m
201ad 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
201ae 20 66 61 69 6c 73 20 74 68 65 20 53 72 63 4c 69 fails the SrcLi
201af 73 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e st is unchanged.
201b0 20 20 54 68 65 0a 2a 2a 20 64 62 2d 3e 6d 61 6c The.** db->mal
201b1 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 77 locFailed flag w
201b2 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 74 72 ill be set to tr
201b3 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ue..*/.SQLITE_PR
201b4 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 IVATE SrcList *s
201b5 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c qlite3SrcListEnl
201b6 61 72 67 65 28 0a 20 20 73 71 6c 69 74 65 33 20 arge(. sqlite3
201b7 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 44 61 *db, /* Da
201b8 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
201b9 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 4f n to notify of O
201ba 4f 4d 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 53 OM errors */. S
201bb 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 rcList *pSrc,
201bc 20 20 2f 2a 20 54 68 65 20 53 72 63 4c 69 73 74 /* The SrcList
201bd 20 74 6f 20 62 65 20 65 6e 6c 61 72 67 65 64 20 to be enlarged
201be 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c */. int nExtra,
201bf 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
201c0 72 20 6f 66 20 6e 65 77 20 73 6c 6f 74 73 20 74 r of new slots t
201c1 6f 20 61 64 64 20 74 6f 20 70 53 72 63 2d 3e 61 o add to pSrc->a
201c2 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 [] */. int iSta
201c3 72 74 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e rt /* In
201c4 64 65 78 20 69 6e 20 70 53 72 63 2d 3e 61 5b 5d dex in pSrc->a[]
201c5 20 6f 66 20 66 69 72 73 74 20 6e 65 77 20 73 6c of first new sl
201c6 6f 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 ot */.){. int i
201c7 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 ;.. /* Sanity c
201c8 68 65 63 6b 69 6e 67 20 6f 6e 20 63 61 6c 6c 69 hecking on calli
201c9 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f ng parameters */
201ca 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72 . assert( iStar
201cb 74 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 t>=0 );. assert
201cc 28 20 6e 45 78 74 72 61 3e 3d 31 20 29 3b 0a 20 ( nExtra>=1 );.
201cd 20 69 66 28 20 70 53 72 63 3d 3d 30 20 7c 7c 20 if( pSrc==0 ||
201ce 69 53 74 61 72 74 3e 70 53 72 63 2d 3e 6e 53 72 iStart>pSrc->nSr
201cf 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 c ){. assert(
201d0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
201d1 64 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 d );. return
201d2 70 53 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 pSrc;. }.. /*
201d3 41 6c 6c 6f 63 61 74 65 20 61 64 64 69 74 69 6f Allocate additio
201d4 6e 61 6c 20 73 70 61 63 65 20 69 66 20 6e 65 65 nal space if nee
201d5 64 65 64 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 ded */. if( pSr
201d6 63 2d 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3e 70 c->nSrc+nExtra>p
201d7 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 Src->nAlloc ){.
201d8 20 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 SrcList *pNew
201d9 3b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 ;. int nAlloc
201da 20 3d 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 = pSrc->nSrc+nE
201db 78 74 72 61 3b 0a 20 20 20 20 70 4e 65 77 20 3d xtra;. pNew =
201dc 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
201dd 63 28 64 62 2c 20 70 53 72 63 2c 0a 20 20 20 20 c(db, pSrc,.
201de 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f sizeo
201df 66 28 2a 70 53 72 63 29 20 2b 20 28 6e 41 6c 6c f(*pSrc) + (nAll
201e0 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70 53 72 oc-1)*sizeof(pSr
201e1 63 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 20 20 c->a[0]) );.
201e2 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 if( pNew==0 ){.
201e3 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d assert( db-
201e4 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
201e5 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 53 . return pS
201e6 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 rc;. }. pS
201e7 72 63 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 rc = pNew;. p
201e8 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 41 Src->nAlloc = nA
201e9 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 lloc;. }.. /*
201ea 4d 6f 76 65 20 65 78 69 73 74 69 6e 67 20 73 6c Move existing sl
201eb 6f 74 73 20 74 68 61 74 20 63 6f 6d 65 20 61 66 ots that come af
201ec 74 65 72 20 74 68 65 20 6e 65 77 6c 79 20 69 6e ter the newly in
201ed 73 65 72 74 65 64 20 73 6c 6f 74 73 0a 20 20 2a serted slots. *
201ee 2a 20 6f 75 74 20 6f 66 20 74 68 65 20 77 61 79 * out of the way
201ef 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 70 53 72 63 */. for(i=pSrc
201f0 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 69 53 74 ->nSrc-1; i>=iSt
201f1 61 72 74 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70 art; i--){. p
201f2 53 72 63 2d 3e 61 5b 69 2b 6e 45 78 74 72 61 5d Src->a[i+nExtra]
201f3 20 3d 20 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 = pSrc->a[i];.
201f4 20 7d 0a 20 20 70 53 72 63 2d 3e 6e 53 72 63 20 }. pSrc->nSrc
201f5 2b 3d 20 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a += nExtra;.. /*
201f6 20 5a 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20 Zero the newly
201f7 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 allocated slots
201f8 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72 */. memset(&pSr
201f9 63 2d 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c c->a[iStart], 0,
201fa 20 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b sizeof(pSrc->a[
201fb 30 5d 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66 0])*nExtra);. f
201fc 6f 72 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69 or(i=iStart; i<i
201fd 53 74 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b Start+nExtra; i+
201fe 2b 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b +){. pSrc->a[
201ff 69 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b i].iCursor = -1;
20200 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 . }.. /* Retur
20201 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
20202 68 65 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c he enlarged SrcL
20203 69 73 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 ist */. return
20204 70 53 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 pSrc;.}.../*.**
20205 41 70 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62 Append a new tab
20206 6c 65 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67 le name to the g
20207 69 76 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43 iven SrcList. C
20208 72 65 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c reate a new SrcL
20209 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 ist if.** need b
2020a 65 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 e. A new entry
2020b 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 is created in th
2020c 65 20 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69 e SrcList even i
2020d 66 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c f pToken is NULL
2020e 2e 0a 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73 ..**.** A SrcLis
2020f 74 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f t is returned, o
20210 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 r NULL if there
20211 69 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e is an OOM error.
20212 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a The returned.*
20213 2a 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 * SrcList might
20214 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 be the same as t
20215 68 65 20 53 72 63 4c 69 73 74 20 74 68 61 74 20 he SrcList that
20216 77 61 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20 was input or it
20217 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65 might be.** a ne
20218 77 20 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f w one. If an OO
20219 4d 20 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63 M error does occ
2021a 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72 urs, then the pr
2021b 69 6f 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69 ior value of pLi
2021c 73 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e st.** that is in
2021d 70 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 put to this rout
2021e 69 6e 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63 ine is automatic
2021f 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a ally freed..**.*
20220 2a 20 49 66 20 70 44 61 74 61 62 61 73 65 20 69 * If pDatabase i
20221 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d s not null, it m
20222 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 eans that the ta
20223 62 6c 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f ble has an optio
20224 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 nal.** database
20225 6e 61 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69 name prefix. Li
20226 6b 65 20 74 68 69 73 3a 20 20 22 64 61 74 61 62 ke this: "datab
20227 61 73 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65 ase.table". The
20228 20 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f pDatabase.** po
20229 69 6e 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c ints to the tabl
2022a 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70 e name and the p
2022b 54 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 Table points to
2022c 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
2022d 65 2e 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73 e..** The SrcLis
2022e 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c t.a[].zName fiel
2022f 64 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 d is filled with
20230 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 the table name
20231 77 68 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63 which might.** c
20232 6f 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20 ome from pTable
20233 28 69 66 20 70 44 61 74 61 62 61 73 65 20 69 73 (if pDatabase is
20234 20 4e 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70 NULL) or from p
20235 44 61 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53 Database. .** S
20236 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 rcList.a[].zData
20237 62 61 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77 base is filled w
20238 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 ith the database
20239 20 6e 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c name from pTabl
2023a 65 2c 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55 e,.** or with NU
2023b 4c 4c 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73 LL if no databas
2023c 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a e is specified..
2023d 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 **.** In other w
2023e 6f 72 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69 ords, if call li
2023f 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
20240 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 sqlite3Sr
20241 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c cListAppend(D,A,
20242 42 2c 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e B,0);.**.** Then
20243 20 42 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 B is a table na
20244 6d 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 me and the datab
20245 61 73 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70 ase name is unsp
20246 65 63 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c ecified. If cal
20247 6c 65 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 led.** like this
20248 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 :.**.**
20249 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 sqlite3SrcListAp
2024a 70 65 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a pend(D,A,B,C);.*
2024b 2a 0a 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74 *.** Then C is t
2024c 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e he table name an
2024d 64 20 42 20 69 73 20 74 68 65 20 64 61 74 61 62 d B is the datab
2024e 61 73 65 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c ase name..*/.SQL
2024f 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c ITE_PRIVATE SrcL
20250 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c ist *sqlite3SrcL
20251 69 73 74 41 70 70 65 6e 64 28 0a 20 20 73 71 6c istAppend(. sql
20252 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
20253 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 /* Connection t
20254 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c o notify of mall
20255 6f 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 oc failures */.
20256 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c SrcList *pList,
20257 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 /* Append t
20258 6f 20 74 68 69 73 20 53 72 63 4c 69 73 74 2e 20 o this SrcList.
20259 4e 55 4c 4c 20 63 72 65 61 74 65 73 20 61 20 6e NULL creates a n
2025a 65 77 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 ew SrcList */.
2025b 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 Token *pTable,
2025c 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 /* Table to
2025d 61 70 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 append */. Toke
2025e 6e 20 2a 70 44 61 74 61 62 61 73 65 20 20 20 20 n *pDatabase
2025f 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 66 20 74 /* Database of t
20260 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 he table */.){.
20261 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
20262 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 item *pItem;. i
20263 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 f( pList==0 ){.
20264 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 pList = sqlit
20265 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 e3DbMallocZero(d
20266 62 2c 20 73 69 7a 65 6f 66 28 53 72 63 4c 69 73 b, sizeof(SrcLis
20267 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c t) );. if( pL
20268 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ist==0 ) return
20269 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 0;. pList->nA
2026a 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 lloc = 1;. }.
2026b 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 53 pList = sqlite3S
2026c 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62 rcListEnlarge(db
2026d 2c 20 70 4c 69 73 74 2c 20 31 2c 20 70 4c 69 73 , pList, 1, pLis
2026e 74 2d 3e 6e 53 72 63 29 3b 0a 20 20 69 66 28 20 t->nSrc);. if(
2026f 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
20270 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
20271 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c rcListDelete(db,
20272 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 65 74 pList);. ret
20273 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49 74 urn 0;. }. pIt
20274 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 em = &pList->a[p
20275 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 List->nSrc-1];.
20276 20 69 66 28 20 70 44 61 74 61 62 61 73 65 20 26 if( pDatabase &
20277 26 20 70 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d & pDatabase->z==
20278 30 20 29 7b 0a 20 20 20 20 70 44 61 74 61 62 61 0 ){. pDataba
20279 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 se = 0;. }. if
2027a 28 20 70 44 61 74 61 62 61 73 65 20 26 26 20 70 ( pDatabase && p
2027b 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 54 6f 6b Table ){. Tok
2027c 65 6e 20 2a 70 54 65 6d 70 20 3d 20 70 44 61 74 en *pTemp = pDat
2027d 61 62 61 73 65 3b 0a 20 20 20 20 70 44 61 74 61 abase;. pData
2027e 62 61 73 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20 base = pTable;.
2027f 20 20 20 70 54 61 62 6c 65 20 3d 20 70 54 65 6d pTable = pTem
20280 70 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e p;. }. pItem->
20281 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e zName = sqlite3N
20282 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c ameFromToken(db,
20283 20 70 54 61 62 6c 65 29 3b 0a 20 20 70 49 74 65 pTable);. pIte
20284 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 m->zDatabase = s
20285 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
20286 6b 65 6e 28 64 62 2c 20 70 44 61 74 61 62 61 73 ken(db, pDatabas
20287 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 e);. return pLi
20288 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 st;.}../*.** Ass
20289 69 67 6e 20 56 64 62 65 43 75 72 73 6f 72 20 69 ign VdbeCursor i
2028a 6e 64 65 78 20 6e 75 6d 62 65 72 73 20 74 6f 20 ndex numbers to
2028b 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 20 all tables in a
2028c 53 72 63 4c 69 73 74 0a 2a 2f 0a 53 51 4c 49 54 SrcList.*/.SQLIT
2028d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
2028e 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 qlite3SrcListAss
2028f 69 67 6e 43 75 72 73 6f 72 73 28 50 61 72 73 65 ignCursors(Parse
20290 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 *pParse, SrcLis
20291 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 t *pList){. int
20292 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 i;. struct Src
20293 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
20294 3b 0a 20 20 61 73 73 65 72 74 28 70 4c 69 73 74 ;. assert(pList
20295 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e || pParse->db->
20296 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
20297 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 if( pList ){.
20298 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 for(i=0, pIte
20299 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c m=pList->a; i<pL
2029a 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 ist->nSrc; i++,
2029b 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 pItem++){.
2029c 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 if( pItem->iCurs
2029d 6f 72 3e 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 or>=0 ) break;.
2029e 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 75 72 pItem->iCur
2029f 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 sor = pParse->nT
202a0 61 62 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 ab++;. if(
202a1 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 pItem->pSelect )
202a2 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
202a3 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 3SrcListAssignCu
202a4 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20 70 49 rsors(pParse, pI
202a5 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 53 tem->pSelect->pS
202a6 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc);. }.
202a7 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
202a8 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 Delete an entire
202a9 20 53 72 63 4c 69 73 74 20 69 6e 63 6c 75 64 69 SrcList includi
202aa 6e 67 20 61 6c 6c 20 69 74 73 20 73 75 62 73 74 ng all its subst
202ab 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 ructure..*/.SQLI
202ac 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
202ad 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 sqlite3SrcListDe
202ae 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 lete(sqlite3 *db
202af 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 , SrcList *pList
202b0 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 ){. int i;. st
202b1 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
202b2 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 m *pItem;. if(
202b3 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 pList==0 ) retur
202b4 6e 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 n;. for(pItem=p
202b5 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c List->a, i=0; i<
202b6 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b pList->nSrc; i++
202b7 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
202b8 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
202b9 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 , pItem->zDataba
202ba 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 se);. sqlite3
202bb 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d DbFree(db, pItem
202bc 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 ->zName);. sq
202bd 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
202be 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a pItem->zAlias);.
202bf 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
202c0 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 49 6e e(db, pItem->zIn
202c1 64 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 dex);. sqlite
202c2 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74 3DeleteTable(pIt
202c3 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20 73 em->pTab);. s
202c4 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
202c5 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53 te(db, pItem->pS
202c6 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 elect);. sqli
202c7 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
202c8 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 , pItem->pOn);.
202c9 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 sqlite3IdList
202ca 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d Delete(db, pItem
202cb 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20 ->pUsing);. }.
202cc 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
202cd 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a b, pList);.}../*
202ce 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
202cf 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 is called by th
202d0 65 20 70 61 72 73 65 72 20 74 6f 20 61 64 64 20 e parser to add
202d1 61 20 6e 65 77 20 74 65 72 6d 20 74 6f 20 74 68 a new term to th
202d2 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 61 20 67 72 e.** end of a gr
202d3 6f 77 69 6e 67 20 46 52 4f 4d 20 63 6c 61 75 73 owing FROM claus
202d4 65 2e 20 20 54 68 65 20 22 70 22 20 70 61 72 61 e. The "p" para
202d5 6d 65 74 65 72 20 69 73 20 74 68 65 20 70 61 72 meter is the par
202d6 74 20 6f 66 0a 2a 2a 20 74 68 65 20 46 52 4f 4d t of.** the FROM
202d7 20 63 6c 61 75 73 65 20 74 68 61 74 20 68 61 73 clause that has
202d8 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f already been co
202d9 6e 73 74 72 75 63 74 65 64 2e 20 20 22 70 22 20 nstructed. "p"
202da 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 74 68 is NULL.** if th
202db 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 is is the first
202dc 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d term of the FROM
202dd 20 63 6c 61 75 73 65 2e 20 20 70 54 61 62 6c 65 clause. pTable
202de 20 61 6e 64 20 70 44 61 74 61 62 61 73 65 0a 2a and pDatabase.*
202df 2a 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f * are the name o
202e0 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 f the table and
202e1 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 20 69 database named i
202e2 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
202e3 65 20 74 65 72 6d 2e 0a 2a 2a 20 70 44 61 74 61 e term..** pData
202e4 62 61 73 65 20 69 73 20 4e 55 4c 4c 20 69 66 20 base is NULL if
202e5 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d the database nam
202e6 65 20 71 75 61 6c 69 66 69 65 72 20 69 73 20 6d e qualifier is m
202e7 69 73 73 69 6e 67 20 2d 20 74 68 65 0a 2a 2a 20 issing - the.**
202e8 75 73 75 61 6c 20 63 61 73 65 2e 20 20 49 66 20 usual case. If
202e9 74 68 65 20 74 65 72 6d 20 68 61 73 20 61 20 61 the term has a a
202ea 6c 69 61 73 2c 20 74 68 65 6e 20 70 41 6c 69 61 lias, then pAlia
202eb 73 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a s points to the.
202ec 2a 2a 20 61 6c 69 61 73 20 74 6f 6b 65 6e 2e 20 ** alias token.
202ed 20 49 66 20 74 68 65 20 74 65 72 6d 20 69 73 20 If the term is
202ee 61 20 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e a subquery, then
202ef 20 70 53 75 62 71 75 65 72 79 20 69 73 20 74 68 pSubquery is th
202f0 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 e.** SELECT stat
202f1 65 6d 65 6e 74 20 74 68 61 74 20 74 68 65 20 73 ement that the s
202f2 75 62 71 75 65 72 79 20 65 6e 63 6f 64 65 73 2e ubquery encodes.
202f3 20 20 54 68 65 20 70 54 61 62 6c 65 20 61 6e 64 The pTable and
202f4 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 70 61 .** pDatabase pa
202f5 72 61 6d 65 74 65 72 73 20 61 72 65 20 4e 55 4c rameters are NUL
202f6 4c 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 L for subqueries
202f7 2e 20 20 54 68 65 20 70 4f 6e 20 61 6e 64 20 70 . The pOn and p
202f8 55 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74 Using.** paramet
202f9 65 72 73 20 61 72 65 20 74 68 65 20 63 6f 6e 74 ers are the cont
202fa 65 6e 74 20 6f 66 20 74 68 65 20 4f 4e 20 61 6e ent of the ON an
202fb 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 2e d USING clauses.
202fc 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 .**.** Return a
202fd 6e 65 77 20 53 72 63 4c 69 73 74 20 77 68 69 63 new SrcList whic
202fe 68 20 65 6e 63 6f 64 65 73 20 69 73 20 74 68 65 h encodes is the
202ff 20 46 52 4f 4d 20 77 69 74 68 20 74 68 65 20 6e FROM with the n
20300 65 77 0a 2a 2a 20 74 65 72 6d 20 61 64 64 65 64 ew.** term added
20301 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20302 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
20303 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e ite3SrcListAppen
20304 64 46 72 6f 6d 54 65 72 6d 28 0a 20 20 50 61 72 dFromTerm(. Par
20305 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
20306 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
20307 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 context */. Src
20308 4c 69 73 74 20 2a 70 2c 20 20 20 20 20 20 20 20 List *p,
20309 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 /* The left
2030a 20 70 61 72 74 20 6f 66 20 74 68 65 20 46 52 4f part of the FRO
2030b 4d 20 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79 M clause already
2030c 20 73 65 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e seen */. Token
2030d 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20 20 *pTable,
2030e 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
2030f 65 20 74 61 62 6c 65 20 74 6f 20 61 64 64 20 74 e table to add t
20310 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 o the FROM claus
20311 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 e */. Token *pD
20312 61 74 61 62 61 73 65 2c 20 20 20 20 20 20 20 2f atabase, /
20313 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 * Name of the da
20314 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e tabase containin
20315 67 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 54 6f g pTable */. To
20316 6b 65 6e 20 2a 70 41 6c 69 61 73 2c 20 20 20 20 ken *pAlias,
20317 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 /* The rig
20318 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 ht-hand side of
20319 74 68 65 20 41 53 20 73 75 62 65 78 70 72 65 73 the AS subexpres
2031a 73 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 sion */. Select
2031b 20 2a 70 53 75 62 71 75 65 72 79 2c 20 20 20 20 *pSubquery,
2031c 20 20 2f 2a 20 41 20 73 75 62 71 75 65 72 79 20 /* A subquery
2031d 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 used in place of
2031e 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f a table name */
2031f 0a 20 20 45 78 70 72 20 2a 70 4f 6e 2c 20 20 20 . Expr *pOn,
20320 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
20321 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 e ON clause of a
20322 20 6a 6f 69 6e 20 2a 2f 0a 20 20 49 64 4c 69 73 join */. IdLis
20323 74 20 2a 70 55 73 69 6e 67 20 20 20 20 20 20 20 t *pUsing
20324 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 /* The USING
20325 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e clause of a join
20326 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 */.){. struct
20327 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
20328 74 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a tem;. sqlite3 *
20329 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
2032a 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 53 72 . p = sqlite3Sr
2032b 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20 cListAppend(db,
2032c 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 61 74 61 p, pTable, pData
2032d 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d base);. if( p==
2032e 30 20 7c 7c 20 70 2d 3e 6e 53 72 63 3d 3d 30 20 0 || p->nSrc==0
2032f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
20330 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4f 6e prDelete(db, pOn
20331 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 );. sqlite3Id
20332 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
20333 55 73 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69 Using);. sqli
20334 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 te3SelectDelete(
20335 64 62 2c 20 70 53 75 62 71 75 65 72 79 29 3b 0a db, pSubquery);.
20336 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 return p;.
20337 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26 70 2d 3e }. pItem = &p->
20338 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 a[p->nSrc-1];.
20339 69 66 28 20 70 41 6c 69 61 73 20 26 26 20 70 41 if( pAlias && pA
2033a 6c 69 61 73 2d 3e 6e 20 29 7b 0a 20 20 20 20 70 lias->n ){. p
2033b 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 Item->zAlias = s
2033c 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
2033d 6b 65 6e 28 64 62 2c 20 70 41 6c 69 61 73 29 3b ken(db, pAlias);
2033e 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 70 53 . }. pItem->pS
2033f 65 6c 65 63 74 20 3d 20 70 53 75 62 71 75 65 72 elect = pSubquer
20340 79 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 4f 6e 20 y;. pItem->pOn
20341 3d 20 70 4f 6e 3b 0a 20 20 70 49 74 65 6d 2d 3e = pOn;. pItem->
20342 70 55 73 69 6e 67 20 3d 20 70 55 73 69 6e 67 3b pUsing = pUsing;
20343 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
20344 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e 44 /*.** Add an IND
20345 45 58 45 44 20 42 59 20 6f 72 20 4e 4f 54 20 49 EXED BY or NOT I
20346 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 74 6f NDEXED clause to
20347 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
20348 6c 79 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c 65 ly added .** ele
20349 6d 65 6e 74 20 6f 66 20 74 68 65 20 73 6f 75 72 ment of the sour
2034a 63 65 2d 6c 69 73 74 20 70 61 73 73 65 64 20 61 ce-list passed a
2034b 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 s the second arg
2034c 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ument..*/.SQLITE
2034d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
2034e 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 lite3SrcListInde
2034f 78 65 64 42 79 28 50 61 72 73 65 20 2a 70 50 61 xedBy(Parse *pPa
20350 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c rse, SrcList *p,
20351 20 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65 64 Token *pIndexed
20352 42 79 29 7b 0a 20 20 69 66 28 20 70 49 6e 64 65 By){. if( pInde
20353 78 65 64 42 79 20 26 26 20 70 20 26 26 20 70 2d xedBy && p && p-
20354 3e 6e 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 73 >nSrc>0 ){. s
20355 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
20356 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e em *pItem = &p->
20357 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 a[p->nSrc-1];.
20358 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d assert( pItem-
20359 3e 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 26 >notIndexed==0 &
2035a 26 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 3d & pItem->zIndex=
2035b 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 =0 );. if( pI
2035c 6e 64 65 78 65 64 42 79 2d 3e 6e 3d 3d 31 20 26 ndexedBy->n==1 &
2035d 26 20 21 70 49 6e 64 65 78 65 64 42 79 2d 3e 7a & !pIndexedBy->z
2035e 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 22 ){. /* A "
2035f 4e 4f 54 20 49 4e 44 45 58 45 44 22 20 63 6c 61 NOT INDEXED" cla
20360 75 73 65 20 77 61 73 20 73 75 70 70 6c 69 65 64 use was supplied
20361 2e 20 53 65 65 20 70 61 72 73 65 2e 79 20 0a 20 . See parse.y .
20362 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 75 63 ** construc
20363 74 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 22 20 t "indexed_opt"
20364 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a for details. */.
20365 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 6e 6f 74 pItem->not
20366 49 6e 64 65 78 65 64 20 3d 20 31 3b 0a 20 20 20 Indexed = 1;.
20367 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 }else{. pI
20368 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 tem->zIndex = sq
20369 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b lite3NameFromTok
2036a 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 en(pParse->db, p
2036b 49 6e 64 65 78 65 64 42 79 29 3b 0a 20 20 20 20 IndexedBy);.
2036c 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 }. }.}../*.** W
2036d 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20 hen building up
2036e 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e a FROM clause in
2036f 20 74 68 65 20 70 61 72 73 65 72 2c 20 74 68 65 the parser, the
20370 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a join operator.*
20371 2a 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61 * is initially a
20372 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c ttached to the l
20373 65 66 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75 eft operand. Bu
20374 74 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 t the code gener
20375 61 74 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20 ator.** expects
20376 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f the join operato
20377 72 20 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72 r to be on the r
20378 69 67 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54 ight operand. T
20379 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53 his routine.** S
2037a 68 69 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f hifts all join o
2037b 70 65 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65 perators from le
2037c 66 74 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20 ft to right for
2037d 61 6e 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a an entire FROM.*
2037e 2a 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 * clause..**.**
2037f 45 78 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65 Example: Suppose
20380 20 74 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b the join is lik
20381 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 e this:.**.**
20382 20 20 20 20 20 20 20 20 41 20 6e 61 74 75 72 61 A natura
20383 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a l cross join B.*
20384 2a 0a 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f *.** The operato
20385 72 20 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72 r is "natural cr
20386 6f 73 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 oss join". The
20387 41 20 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 A and B operands
20388 20 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 are stored.** i
20389 6e 20 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d n p->a[0] and p-
2038a 3e 61 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76 >a[1], respectiv
2038b 65 6c 79 2e 20 20 54 68 65 20 70 61 72 73 65 72 ely. The parser
2038c 20 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 initially store
2038d 73 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f s the.** operato
2038e 72 20 77 69 74 68 20 41 2e 20 20 54 68 69 73 20 r with A. This
2038f 72 6f 75 74 69 6e 65 20 73 68 69 66 74 73 20 74 routine shifts t
20390 68 61 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 hat operator ove
20391 72 20 74 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49 54 r to B..*/.SQLIT
20392 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20393 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 qlite3SrcListShi
20394 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 ftJoinType(SrcLi
20395 73 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 st *p){. if( p
20396 26 26 20 70 2d 3e 61 20 29 7b 0a 20 20 20 20 69 && p->a ){. i
20397 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
20398 70 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20 p->nSrc-1; i>0;
20399 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 i--){. p->a
2039a 5b 69 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 [i].jointype = p
2039b 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70 ->a[i-1].jointyp
2039c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e e;. }. p->
2039d 61 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 a[0].jointype =
2039e 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0;. }.}../*.**
2039f 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 Begin a transact
203a0 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ion.*/.SQLITE_PR
203a1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
203a2 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 e3BeginTransacti
203a3 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 on(Parse *pParse
203a4 2c 20 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73 , int type){. s
203a5 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 qlite3 *db;. Vd
203a6 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a be *v;. int i;.
203a7 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 . if( pParse==0
203a8 20 7c 7c 20 28 64 62 3d 70 50 61 72 73 65 2d 3e || (db=pParse->
203a9 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44 db)==0 || db->aD
203aa 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 b[0].pBt==0 ) re
203ab 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 turn;. if( pPar
203ac 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e se->nErr || db->
203ad 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 mallocFailed ) r
203ae 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c eturn;. if( sql
203af 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
203b0 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 arse, SQLITE_TRA
203b1 4e 53 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e NSACTION, "BEGIN
203b2 22 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75 72 ", 0, 0) ) retur
203b3 6e 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 n;.. v = sqlite
203b4 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
203b5 3b 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74 ;. if( !v ) ret
203b6 75 72 6e 3b 0a 20 20 69 66 28 20 74 79 70 65 21 urn;. if( type!
203b7 3d 54 4b 5f 44 45 46 45 52 52 45 44 20 29 7b 0a =TK_DEFERRED ){.
203b8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 for(i=0; i<d
203b9 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
203ba 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
203bb 64 64 4f 70 32 28 76 2c 20 4f 50 5f 54 72 61 6e ddOp2(v, OP_Tran
203bc 73 61 63 74 69 6f 6e 2c 20 69 2c 20 28 74 79 70 saction, i, (typ
203bd 65 3d 3d 54 4b 5f 45 58 43 4c 55 53 49 56 45 29 e==TK_EXCLUSIVE)
203be 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 +1);. sqlit
203bf 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 e3VdbeUsesBtree(
203c0 76 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d v, i);. }. }
203c1 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
203c2 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 dOp2(v, OP_AutoC
203c3 6f 6d 6d 69 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a ommit, 0, 0);.}.
203c4 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20 ./*.** Commit a
203c5 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 transaction.*/.S
203c6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
203c7 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 id sqlite3Commit
203c8 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 Transaction(Pars
203c9 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 e *pParse){. sq
203ca 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 lite3 *db;. Vdb
203cb 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 70 50 61 e *v;.. if( pPa
203cc 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 rse==0 || (db=pP
203cd 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 arse->db)==0 ||
203ce 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d db->aDb[0].pBt==
203cf 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 0 ) return;. if
203d0 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c ( pParse->nErr |
203d1 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
203d2 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 ed ) return;. i
203d3 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 f( sqlite3AuthCh
203d4 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 eck(pParse, SQLI
203d5 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 TE_TRANSACTION,
203d6 22 43 4f 4d 4d 49 54 22 2c 20 30 2c 20 30 29 20 "COMMIT", 0, 0)
203d7 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d ) return;.. v =
203d8 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
203d9 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 pParse);. if( v
203da 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
203db 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
203dc 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 30 AutoCommit, 1, 0
203dd 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
203de 52 6f 6c 6c 62 61 63 6b 20 61 20 74 72 61 6e 73 Rollback a trans
203df 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 action.*/.SQLITE
203e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
203e1 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 lite3RollbackTra
203e2 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a nsaction(Parse *
203e3 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 pParse){. sqlit
203e4 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a e3 *db;. Vdbe *
203e5 76 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 v;.. if( pParse
203e6 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61 72 73 ==0 || (db=pPars
203e7 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d e->db)==0 || db-
203e8 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 >aDb[0].pBt==0 )
203e9 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 return;. if( p
203ea 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 Parse->nErr || d
203eb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
203ec 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
203ed 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b sqlite3AuthCheck
203ee 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f (pParse, SQLITE_
203ef 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52 4f TRANSACTION, "RO
203f0 4c 4c 42 41 43 4b 22 2c 20 30 2c 20 30 29 20 29 LLBACK", 0, 0) )
203f1 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 return;.. v =
203f2 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
203f3 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 Parse);. if( v
203f4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
203f5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 beAddOp2(v, OP_A
203f6 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 31 29 utoCommit, 1, 1)
203f7 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d ;. }.}../*.** M
203f8 61 6b 65 20 73 75 72 65 20 74 68 65 20 54 45 4d ake sure the TEM
203f9 50 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 P database is op
203fa 65 6e 20 61 6e 64 20 61 76 61 69 6c 61 62 6c 65 en and available
203fb 20 66 6f 72 20 75 73 65 2e 20 20 52 65 74 75 72 for use. Retur
203fc 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 n.** the number
203fd 6f 66 20 65 72 72 6f 72 73 2e 20 20 4c 65 61 76 of errors. Leav
203fe 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 e any error mess
203ff 61 67 65 73 20 69 6e 20 74 68 65 20 70 50 61 72 ages in the pPar
20400 73 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f se structure..*/
20401 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20402 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 int sqlite3OpenT
20403 65 6d 70 44 61 74 61 62 61 73 65 28 50 61 72 73 empDatabase(Pars
20404 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 e *pParse){. sq
20405 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
20406 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 se->db;. if( db
20407 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 3d 3d 30 20 ->aDb[1].pBt==0
20408 26 26 20 21 70 50 61 72 73 65 2d 3e 65 78 70 6c && !pParse->expl
20409 61 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 ain ){. int r
2040a 63 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f c;. static co
2040b 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 nst int flags =
2040c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 . SQLIT
2040d 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
2040e 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c |. SQL
2040f 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 ITE_OPEN_CREATE
20410 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 |. SQLI
20411 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
20412 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 E |. SQ
20413 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
20414 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 ONCLOSE |.
20415 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
20416 54 45 4d 50 5f 44 42 3b 0a 0a 20 20 20 20 72 63 TEMP_DB;.. rc
20417 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 = sqlite3BtreeF
20418 61 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 30 2c actory(db, 0, 0,
20419 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
2041a 43 41 43 48 45 5f 53 49 5a 45 2c 20 66 6c 61 67 CACHE_SIZE, flag
2041b 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 s,.
2041c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2041d 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 31 5d 2e &db->aDb[1].
2041e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 pBt);. if( rc
2041f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
20420 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
20421 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e rMsg(pParse, "un
20422 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20 74 able to open a t
20423 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 emporary databas
20424 65 20 22 0a 20 20 20 20 20 20 20 20 22 66 69 6c e ". "fil
20425 65 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 e for storing te
20426 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 22 29 mporary tables")
20427 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e ;. pParse->
20428 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 72 rc = rc;. r
20429 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
2042a 20 20 20 61 73 73 65 72 74 28 20 28 64 62 2d 3e assert( (db->
2042b 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49 flags & SQLITE_I
2042c 6e 54 72 61 6e 73 29 3d 3d 30 20 7c 7c 20 64 62 nTrans)==0 || db
2042d 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 3b 0a ->autoCommit );.
2042e 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
2042f 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 aDb[1].pSchema )
20430 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 ;. sqlite3Pag
20431 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 73 71 erJournalMode(sq
20432 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 lite3BtreePager(
20433 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 2c db->aDb[1].pBt),
20434 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
20435 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d db-
20436 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 >dfltJournalMode
20437 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
20438 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 0;.}../*.** Gene
20439 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 rate VDBE code t
2043a 68 61 74 20 77 69 6c 6c 20 76 65 72 69 66 79 20 hat will verify
2043b 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 the schema cooki
2043c 65 20 61 6e 64 20 73 74 61 72 74 0a 2a 2a 20 61 e and start.** a
2043d 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f read-transactio
2043e 6e 20 66 6f 72 20 61 6c 6c 20 6e 61 6d 65 64 20 n for all named
2043f 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0a database files..
20440 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f **.** It is impo
20441 72 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 73 rtant that all s
20442 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 62 65 chema cookies be
20443 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 61 6c verified and al
20444 6c 0a 2a 2a 20 72 65 61 64 20 74 72 61 6e 73 61 l.** read transa
20445 63 74 69 6f 6e 73 20 62 65 20 73 74 61 72 74 65 ctions be starte
20446 64 20 62 65 66 6f 72 65 20 61 6e 79 74 68 69 6e d before anythin
20447 67 20 65 6c 73 65 20 68 61 70 70 65 6e 73 20 69 g else happens i
20448 6e 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 70 72 n.** the VDBE pr
20449 6f 67 72 61 6d 2e 20 20 42 75 74 20 74 68 69 73 ogram. But this
2044a 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 routine can be
2044b 63 61 6c 6c 65 64 20 61 66 74 65 72 20 6d 75 63 called after muc
2044c 68 20 6f 74 68 65 72 0a 2a 2a 20 63 6f 64 65 20 h other.** code
2044d 68 61 73 20 62 65 65 6e 20 67 65 6e 65 72 61 74 has been generat
2044e 65 64 2e 20 20 53 6f 20 68 65 72 65 20 69 73 20 ed. So here is
2044f 77 68 61 74 20 77 65 20 64 6f 3a 0a 2a 2a 0a 2a what we do:.**.*
20450 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 * The first time
20451 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
20452 20 63 61 6c 6c 65 64 2c 20 77 65 20 63 6f 64 65 called, we code
20453 20 61 6e 20 4f 50 5f 47 6f 74 6f 20 74 68 61 74 an OP_Goto that
20454 0a 2a 2a 20 77 69 6c 6c 20 6a 75 6d 70 20 74 6f .** will jump to
20455 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 61 74 a subroutine at
20456 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
20457 70 72 6f 67 72 61 6d 2e 20 20 54 68 65 6e 20 77 program. Then w
20458 65 0a 2a 2a 20 72 65 63 6f 72 64 20 65 76 65 72 e.** record ever
20459 79 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 y database that
2045a 6e 65 65 64 73 20 69 74 73 20 73 63 68 65 6d 61 needs its schema
2045b 20 76 65 72 69 66 69 65 64 20 69 6e 20 74 68 65 verified in the
2045c 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b .** pParse->cook
2045d 69 65 4d 61 73 6b 20 66 69 65 6c 64 2e 20 20 4c ieMask field. L
2045e 61 74 65 72 2c 20 61 66 74 65 72 20 61 6c 6c 20 ater, after all
2045f 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 62 other code has b
20460 65 65 6e 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 een.** generated
20461 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 , the subroutine
20462 20 74 68 61 74 20 64 6f 65 73 20 74 68 65 20 63 that does the c
20463 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61 74 69 ookie verificati
20464 6f 6e 73 20 61 6e 64 0a 2a 2a 20 73 74 61 72 74 ons and.** start
20465 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f s the transactio
20466 6e 73 20 77 69 6c 6c 20 62 65 20 63 6f 64 65 64 ns will be coded
20467 20 61 6e 64 20 74 68 65 20 4f 50 5f 47 6f 74 6f and the OP_Goto
20468 20 50 32 20 76 61 6c 75 65 0a 2a 2a 20 77 69 6c P2 value.** wil
20469 6c 20 62 65 20 6d 61 64 65 20 74 6f 20 70 6f 69 l be made to poi
2046a 6e 74 20 74 6f 20 74 68 61 74 20 73 75 62 72 6f nt to that subro
2046b 75 74 69 6e 65 2e 20 20 54 68 65 20 67 65 6e 65 utine. The gene
2046c 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a ration of the.**
2046d 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61 cookie verifica
2046e 74 69 6f 6e 20 73 75 62 72 6f 75 74 69 6e 65 20 tion subroutine
2046f 63 6f 64 65 20 68 61 70 70 65 6e 73 20 69 6e 20 code happens in
20470 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 sqlite3FinishCod
20471 69 6e 67 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ing()..**.** If
20472 69 44 62 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 iDb<0 then code
20473 74 68 65 20 4f 50 5f 47 6f 74 6f 20 6f 6e 6c 79 the OP_Goto only
20474 20 2d 20 64 6f 6e 27 74 20 73 65 74 20 66 6c 61 - don't set fla
20475 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 0a g to verify the.
20476 2a 2a 20 73 63 68 65 6d 61 20 6f 6e 20 61 6e 79 ** schema on any
20477 20 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69 databases. Thi
20478 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f s can be used to
20479 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20 4f 50 position the OP
2047a 5f 47 6f 74 6f 0a 2a 2a 20 65 61 72 6c 79 20 69 _Goto.** early i
2047b 6e 20 74 68 65 20 63 6f 64 65 2c 20 62 65 66 6f n the code, befo
2047c 72 65 20 77 65 20 6b 6e 6f 77 20 69 66 20 61 6e re we know if an
2047d 79 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 y database table
2047e 73 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a s will be used..
2047f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20480 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
20481 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 deVerifySchema(P
20482 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
20483 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 t iDb){. sqlite
20484 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3 *db;. Vdbe *v
20485 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 0a 20 ;. int mask;..
20486 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 v = sqlite3GetV
20487 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 dbe(pParse);. i
20488 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e f( v==0 ) return
20489 3b 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 ; /* This only
2048a 68 61 70 70 65 6e 73 20 69 66 20 74 68 65 72 65 happens if there
2048b 20 77 61 73 20 61 20 70 72 69 6f 72 20 65 72 72 was a prior err
2048c 6f 72 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61 or */. db = pPa
2048d 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 rse->db;. if( p
2048e 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 Parse->cookieGot
2048f 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50 61 72 o==0 ){. pPar
20490 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20 3d se->cookieGoto =
20491 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20492 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 p2(v, OP_Goto, 0
20493 2c 20 30 29 2b 31 3b 0a 20 20 7d 0a 20 20 69 66 , 0)+1;. }. if
20494 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 ( iDb>=0 ){.
20495 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e assert( iDb<db->
20496 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 nDb );. asser
20497 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e t( db->aDb[iDb].
20498 70 42 74 21 3d 30 20 7c 7c 20 69 44 62 3d 3d 31 pBt!=0 || iDb==1
20499 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
2049a 69 44 62 3c 53 51 4c 49 54 45 5f 4d 41 58 5f 41 iDb<SQLITE_MAX_A
2049b 54 54 41 43 48 45 44 2b 32 20 29 3b 0a 20 20 20 TTACHED+2 );.
2049c 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 44 62 3b 0a mask = 1<<iDb;.
2049d 20 20 20 20 69 66 28 20 28 70 50 61 72 73 65 2d if( (pParse-
2049e 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 26 20 6d 61 >cookieMask & ma
2049f 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 sk)==0 ){.
204a0 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 pParse->cookieMa
204a1 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 sk |= mask;.
204a2 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 pParse->cookie
204a3 56 61 6c 75 65 5b 69 44 62 5d 20 3d 20 64 62 2d Value[iDb] = db-
204a4 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d >aDb[iDb].pSchem
204a5 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 a->schema_cookie
204a6 3b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 ;. if( !OMI
204a7 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d T_TEMPDB && iDb=
204a8 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 =1 ){. sq
204a9 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 lite3OpenTempDat
204aa 61 62 61 73 65 28 70 50 61 72 73 65 29 3b 0a 20 abase(pParse);.
204ab 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
204ac 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
204ad 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 te VDBE code tha
204ae 74 20 70 72 65 70 61 72 65 73 20 66 6f 72 20 64 t prepares for d
204af 6f 69 6e 67 20 61 6e 20 6f 70 65 72 61 74 69 6f oing an operatio
204b0 6e 20 74 68 61 74 0a 2a 2a 20 6d 69 67 68 74 20 n that.** might
204b1 63 68 61 6e 67 65 20 74 68 65 20 64 61 74 61 62 change the datab
204b2 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ase..**.** This
204b3 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73 20 61 routine starts a
204b4 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e new transaction
204b5 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74 20 61 if we are not a
204b6 6c 72 65 61 64 79 20 77 69 74 68 69 6e 0a 2a 2a lready within.**
204b7 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 a transaction.
204b8 20 49 66 20 77 65 20 61 72 65 20 61 6c 72 65 61 If we are alrea
204b9 64 79 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e dy within a tran
204ba 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 61 20 saction, then a
204bb 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a 20 69 73 checkpoint.** is
204bc 20 73 65 74 20 69 66 20 74 68 65 20 73 65 74 53 set if the setS
204bd 74 61 74 65 6d 65 6e 74 20 70 61 72 61 6d 65 74 tatement paramet
204be 65 72 20 69 73 20 74 72 75 65 2e 20 20 41 20 63 er is true. A c
204bf 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64 heckpoint should
204c0 0a 2a 2a 20 62 65 20 73 65 74 20 66 6f 72 20 6f .** be set for o
204c1 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 6d perations that m
204c2 69 67 68 74 20 66 61 69 6c 20 28 64 75 65 20 74 ight fail (due t
204c3 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 29 20 o a constraint)
204c4 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 77 part of.** the w
204c5 61 79 20 74 68 72 6f 75 67 68 20 61 6e 64 20 77 ay through and w
204c6 68 69 63 68 20 77 69 6c 6c 20 6e 65 65 64 20 74 hich will need t
204c7 6f 20 75 6e 64 6f 20 73 6f 6d 65 20 77 72 69 74 o undo some writ
204c8 65 73 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e es without havin
204c9 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b g to.** rollback
204ca 20 74 68 65 20 77 68 6f 6c 65 20 74 72 61 6e 73 the whole trans
204cb 61 63 74 69 6f 6e 2e 20 20 46 6f 72 20 6f 70 65 action. For ope
204cc 72 61 74 69 6f 6e 73 20 77 68 65 72 65 20 61 6c rations where al
204cd 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 2a 2a l constraints.**
204ce 20 63 61 6e 20 62 65 20 63 68 65 63 6b 65 64 20 can be checked
204cf 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 before any chang
204d0 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 es are made to t
204d1 68 65 20 64 61 74 61 62 61 73 65 2c 20 69 74 20 he database, it
204d2 69 73 20 6e 65 76 65 72 0a 2a 2a 20 6e 65 63 65 is never.** nece
204d3 73 73 61 72 79 20 74 6f 20 75 6e 64 6f 20 61 20 ssary to undo a
204d4 77 72 69 74 65 20 61 6e 64 20 74 68 65 20 63 68 write and the ch
204d5 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 eckpoint should
204d6 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a not be set..**.*
204d7 2a 20 4f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 * Only database
204d8 69 44 62 20 61 6e 64 20 74 68 65 20 74 65 6d 70 iDb and the temp
204d9 20 64 61 74 61 62 61 73 65 20 61 72 65 20 6d 61 database are ma
204da 64 65 20 77 72 69 74 61 62 6c 65 20 62 79 20 74 de writable by t
204db 68 69 73 20 63 61 6c 6c 2e 0a 2a 2a 20 49 66 20 his call..** If
204dc 69 44 62 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 iDb==0, then the
204dd 20 6d 61 69 6e 20 61 6e 64 20 74 65 6d 70 20 64 main and temp d
204de 61 74 61 62 61 73 65 73 20 61 72 65 20 6d 61 64 atabases are mad
204df 65 20 77 72 69 74 61 62 6c 65 2e 20 20 20 49 66 e writable. If
204e0 0a 2a 2a 20 69 44 62 3d 3d 31 20 74 68 65 6e 20 .** iDb==1 then
204e1 6f 6e 6c 79 20 74 68 65 20 74 65 6d 70 20 64 61 only the temp da
204e2 74 61 62 61 73 65 20 69 73 20 6d 61 64 65 20 77 tabase is made w
204e3 72 69 74 61 62 6c 65 2e 20 20 49 66 20 69 44 62 ritable. If iDb
204e4 3e 31 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 73 >1 then the.** s
204e5 70 65 63 69 66 69 65 64 20 61 75 78 69 6c 69 61 pecified auxilia
204e6 72 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 ry database and
204e7 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 the temp databas
204e8 65 20 61 72 65 20 6d 61 64 65 20 77 72 69 74 61 e are made writa
204e9 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ble..*/.SQLITE_P
204ea 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
204eb 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 te3BeginWriteOpe
204ec 72 61 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 ration(Parse *pP
204ed 61 72 73 65 2c 20 69 6e 74 20 73 65 74 53 74 61 arse, int setSta
204ee 74 65 6d 65 6e 74 2c 20 69 6e 74 20 69 44 62 29 tement, int iDb)
204ef 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 {. Vdbe *v = sq
204f0 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 lite3GetVdbe(pPa
204f1 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 rse);. if( v==0
204f2 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c ) return;. sql
204f3 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 ite3CodeVerifySc
204f4 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 hema(pParse, iDb
204f5 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 77 72 69 );. pParse->wri
204f6 74 65 4d 61 73 6b 20 7c 3d 20 31 3c 3c 69 44 62 teMask |= 1<<iDb
204f7 3b 0a 20 20 69 66 28 20 73 65 74 53 74 61 74 65 ;. if( setState
204f8 6d 65 6e 74 20 26 26 20 70 50 61 72 73 65 2d 3e ment && pParse->
204f9 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 nested==0 ){.
204fa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
204fb 70 31 28 76 2c 20 4f 50 5f 53 74 61 74 65 6d 65 p1(v, OP_Stateme
204fc 6e 74 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 nt, iDb);. }.
204fd 69 66 28 20 28 4f 4d 49 54 5f 54 45 4d 50 44 42 if( (OMIT_TEMPDB
204fe 20 7c 7c 20 69 44 62 21 3d 31 29 20 26 26 20 70 || iDb!=1) && p
204ff 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 Parse->db->aDb[1
20500 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20 20 20 20 ].pBt!=0 ){.
20501 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 sqlite3BeginWrit
20502 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 eOperation(pPars
20503 65 2c 20 73 65 74 53 74 61 74 65 6d 65 6e 74 2c e, setStatement,
20504 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 1);. }.}../*.*
20505 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
20506 66 20 70 49 6e 64 65 78 20 75 73 65 73 20 74 68 f pIndex uses th
20507 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 e collating sequ
20508 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 52 65 74 ence pColl. Ret
20509 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 69 urn.** true if i
2050a 74 20 64 6f 65 73 20 61 6e 64 20 66 61 6c 73 65 t does and false
2050b 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e if it does not.
2050c 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
2050d 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a TE_OMIT_REINDEX.
2050e 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c 61 static int colla
2050f 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e 73 74 20 tionMatch(const
20510 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49 6e 64 char *zColl, Ind
20511 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a 20 20 69 ex *pIndex){. i
20512 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
20513 20 69 3c 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 i<pIndex->nColu
20514 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f mn; i++){. co
20515 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 49 nst char *z = pI
20516 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b ndex->azColl[i];
20517 0a 20 20 20 20 69 66 28 20 7a 3d 3d 7a 43 6f 6c . if( z==zCol
20518 6c 20 7c 7c 20 28 7a 20 26 26 20 7a 43 6f 6c 6c l || (z && zColl
20519 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 && 0==sqlite3St
2051a 72 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c 6c 29 29 rICmp(z, zColl))
2051b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
2051c 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
2051d 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 return 0;.}.#end
2051e 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 if../*.** Recomp
2051f 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 ute all indices
20520 6f 66 20 70 54 61 62 20 74 68 61 74 20 75 73 65 of pTab that use
20521 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 the collating s
20522 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 0a 2a equence pColl..*
20523 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68 * If pColl==0 th
20524 65 6e 20 72 65 63 6f 6d 70 75 74 65 20 61 6c 6c en recompute all
20525 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62 indices of pTab
20526 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
20527 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 ITE_OMIT_REINDEX
20528 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 69 .static void rei
20529 6e 64 65 78 54 61 62 6c 65 28 50 61 72 73 65 20 ndexTable(Parse
2052a 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a *pParse, Table *
2052b 70 54 61 62 2c 20 63 68 61 72 20 63 6f 6e 73 74 pTab, char const
2052c 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49 6e 64 65 *zColl){. Inde
2052d 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20 x *pIndex;
2052e 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e /* An in
2052f 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 dex associated w
20530 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a 20 20 66 ith pTab */.. f
20531 6f 72 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e or(pIndex=pTab->
20532 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 pIndex; pIndex;
20533 70 49 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 pIndex=pIndex->p
20534 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 7a Next){. if( z
20535 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f 6c 6c 61 Coll==0 || colla
20536 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f 6c 6c 2c tionMatch(zColl,
20537 20 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 pIndex) ){.
20538 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 int iDb = sqli
20539 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 te3SchemaToIndex
2053a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 (pParse->db, pTa
2053b 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 b->pSchema);.
2053c 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 sqlite3BeginW
2053d 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 riteOperation(pP
2053e 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 arse, 0, iDb);.
2053f 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69 sqlite3Refi
20540 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 llIndex(pParse,
20541 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 pIndex, -1);.
20542 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a }. }.}.#endif.
20543 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65 ./*.** Recompute
20544 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 all indices of
20545 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 6c all tables in al
20546 6c 20 64 61 74 61 62 61 73 65 73 20 77 68 65 72 l databases wher
20547 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69 63 65 73 e the.** indices
20548 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 use the collati
20549 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c ng sequence pCol
2054a 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 l. If pColl==0
2054b 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 0a 2a then recompute.*
2054c 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 65 76 * all indices ev
2054d 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a 23 69 66 erywhere..*/.#if
2054e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
2054f 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20 _REINDEX.static
20550 76 6f 69 64 20 72 65 69 6e 64 65 78 44 61 74 61 void reindexData
20551 62 61 73 65 73 28 50 61 72 73 65 20 2a 70 50 61 bases(Parse *pPa
20552 72 73 65 2c 20 63 68 61 72 20 63 6f 6e 73 74 20 rse, char const
20553 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62 20 2a 70 *zColl){. Db *p
20554 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
20555 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 /* A sing
20556 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 le database */.
20557 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
20558 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20559 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 The database ind
2055a 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 ex number */. s
2055b 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
2055c 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68 rse->db; /* Th
2055d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
2055e 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61 73 68 45 ction */. HashE
2055f 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20 20 20 20 lem *k;
20560 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f /* For lo
20561 6f 70 69 6e 67 20 6f 76 65 72 20 74 61 62 6c 65 oping over table
20562 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20 20 54 61 s in pDb */. Ta
20563 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 ble *pTab;
20564 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74 /* A t
20565 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 able in the data
20566 62 61 73 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 base */.. for(i
20567 44 62 3d 30 2c 20 70 44 62 3d 64 62 2d 3e 61 44 Db=0, pDb=db->aD
20568 62 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 b; iDb<db->nDb;
20569 69 44 62 2b 2b 2c 20 70 44 62 2b 2b 29 7b 0a 20 iDb++, pDb++){.
2056a 20 20 20 61 73 73 65 72 74 28 20 70 44 62 21 3d assert( pDb!=
2056b 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 73 0 );. for(k=s
2056c 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 qliteHashFirst(&
2056d 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 pDb->pSchema->tb
2056e 6c 48 61 73 68 29 3b 20 20 6b 3b 20 6b 3d 73 71 lHash); k; k=sq
2056f 6c 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29 liteHashNext(k))
20570 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 28 {. pTab = (
20571 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73 Table*)sqliteHas
20572 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 20 20 hData(k);.
20573 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61 reindexTable(pPa
20574 72 73 65 2c 20 70 54 61 62 2c 20 7a 43 6f 6c 6c rse, pTab, zColl
20575 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 );. }. }.}.#
20576 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e endif../*.** Gen
20577 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 erate code for t
20578 68 65 20 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61 he REINDEX comma
20579 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 nd..**.**
2057a 20 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 REINDEX
2057b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2057c 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 -- 1.**
2057d 20 20 20 52 45 49 4e 44 45 58 20 20 3c 63 6f 6c REINDEX <col
2057e 6c 61 74 69 6f 6e 3e 20 20 20 20 20 20 20 20 20 lation>
2057f 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 -- 2.**
20580 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c REINDEX ?<
20581 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c database>.?<tabl
20582 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 20 ename> -- 3.**
20583 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 REINDEX
20584 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 69 6e ?<database>.?<in
20585 64 65 78 6e 61 6d 65 3e 20 20 2d 2d 20 34 0a 2a dexname> -- 4.*
20586 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 *.** Form 1 caus
20587 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 es all indices i
20588 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 n all attached d
20589 61 74 61 62 61 73 65 73 20 74 6f 20 62 65 20 72 atabases to be r
2058a 65 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f 72 6d 20 ebuilt..** Form
2058b 32 20 72 65 62 75 69 6c 64 73 20 61 6c 6c 20 69 2 rebuilds all i
2058c 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 64 61 ndices in all da
2058d 74 61 62 61 73 65 73 20 74 68 61 74 20 75 73 65 tabases that use
2058e 20 74 68 65 20 6e 61 6d 65 64 0a 2a 2a 20 63 6f the named.** co
2058f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e llating function
20590 2e 20 20 46 6f 72 6d 73 20 33 20 61 6e 64 20 34 . Forms 3 and 4
20591 20 72 65 62 75 69 6c 64 20 74 68 65 20 6e 61 6d rebuild the nam
20592 65 64 20 69 6e 64 65 78 20 6f 72 20 61 6c 6c 0a ed index or all.
20593 2a 2a 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 ** indices assoc
20594 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e iated with the n
20595 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 amed table..*/.#
20596 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20597 49 54 5f 52 45 49 4e 44 45 58 0a 53 51 4c 49 54 IT_REINDEX.SQLIT
20598 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20599 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61 qlite3Reindex(Pa
2059a 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b rse *pParse, Tok
2059b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65 en *pName1, Toke
2059c 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 43 6f n *pName2){. Co
2059d 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 llSeq *pColl;
2059e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
2059f 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
205a0 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 2c to be reindexed,
205a1 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 or NULL */. ch
205a2 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 ar *z;
205a3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
205a4 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20 e of a table or
205a5 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74 index */. const
205a6 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 char *zDb;
205a7 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
205a8 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a f the database *
205a9 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b /. Table *pTab;
205aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
205ab 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 /* A table in th
205ac 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
205ad 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 Index *pIndex;
205ae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
205af 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 n index associat
205b0 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a ed with pTab */.
205b1 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
205b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
205b3 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e The database in
205b4 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 dex number */.
205b5 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
205b6 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 arse->db; /* T
205b7 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
205b8 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 ection */. Toke
205b9 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20 20 20 20 n *pObjName;
205ba 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
205bb 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 of the table or
205bc 69 6e 64 65 78 20 74 6f 20 62 65 20 72 65 69 6e index to be rein
205bd 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 dexed */.. /* R
205be 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 ead the database
205bf 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 schema. If an e
205c0 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 rror occurs, lea
205c1 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ve an error mess
205c2 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 age. ** and cod
205c3 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 e in pParse and
205c4 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a return NULL. */.
205c5 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
205c6 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 =sqlite3ReadSche
205c7 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 ma(pParse) ){.
205c8 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 return;. }..
205c9 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 7c if( pName1==0 |
205ca 7c 20 70 4e 61 6d 65 31 2d 3e 7a 3d 3d 30 20 29 | pName1->z==0 )
205cb 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 44 61 74 {. reindexDat
205cc 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 30 abases(pParse, 0
205cd 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
205ce 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d 65 }else if( pName
205cf 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e 2==0 || pName2->
205d0 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 z==0 ){. char
205d1 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 61 73 73 *zColl;. ass
205d2 65 72 74 28 20 70 4e 61 6d 65 31 2d 3e 7a 20 29 ert( pName1->z )
205d3 3b 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 ;. zColl = sq
205d4 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b lite3NameFromTok
205d5 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 en(pParse->db, p
205d6 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20 Name1);. if(
205d7 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b !zColl ) return;
205d8 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c . pColl = sql
205d9 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 ite3FindCollSeq(
205da 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f db, ENC(db), zCo
205db 6c 6c 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 ll, -1, 0);.
205dc 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 if( pColl ){.
205dd 20 20 20 69 66 28 20 7a 43 6f 6c 6c 20 29 7b 0a if( zColl ){.
205de 20 20 20 20 20 20 20 20 72 65 69 6e 64 65 78 44 reindexD
205df 61 74 61 62 61 73 65 73 28 70 50 61 72 73 65 2c atabases(pParse,
205e0 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 20 zColl);.
205e1 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
205e2 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 b, zColl);.
205e3 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b }. return;
205e4 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
205e5 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f e3DbFree(db, zCo
205e6 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d ll);. }. iDb =
205e7 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e sqlite3TwoPartN
205e8 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d ame(pParse, pNam
205e9 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4f 62 e1, pName2, &pOb
205ea 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 jName);. if( iD
205eb 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 b<0 ) return;.
205ec 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 z = sqlite3NameF
205ed 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4f 62 romToken(db, pOb
205ee 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d jName);. if( z=
205ef 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a =0 ) return;. z
205f0 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 Db = db->aDb[iDb
205f1 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 20 ].zName;. pTab
205f2 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 = sqlite3FindTab
205f3 6c 65 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a le(db, z, zDb);.
205f4 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 if( pTab ){.
205f5 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70 reindexTable(p
205f6 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 29 3b Parse, pTab, 0);
205f7 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
205f8 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 72 ee(db, z);. r
205f9 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 49 6e eturn;. }. pIn
205fa 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e dex = sqlite3Fin
205fb 64 49 6e 64 65 78 28 64 62 2c 20 7a 2c 20 7a 44 dIndex(db, z, zD
205fc 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 b);. sqlite3DbF
205fd 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 69 66 ree(db, z);. if
205fe 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 ( pIndex ){.
205ff 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 sqlite3BeginWrit
20600 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 eOperation(pPars
20601 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 e, 0, iDb);.
20602 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 sqlite3RefillInd
20603 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 ex(pParse, pInde
20604 78 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 x, -1);. retu
20605 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 rn;. }. sqlite
20606 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
20607 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 69 64 65 , "unable to ide
20608 6e 74 69 66 79 20 74 68 65 20 6f 62 6a 65 63 74 ntify the object
20609 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 to be reindexed
2060a 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a ");.}.#endif../*
2060b 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e .** Return a dyn
2060c 61 6d 69 63 6c 79 20 61 6c 6c 6f 63 61 74 65 64 amicly allocated
2060d 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
2060e 72 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 re that can be u
2060f 73 65 64 0a 2a 2a 20 77 69 74 68 20 4f 50 5f 4f sed.** with OP_O
20610 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 penRead or OP_Op
20611 65 6e 57 72 69 74 65 20 74 6f 20 61 63 63 65 73 enWrite to acces
20612 73 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 s database index
20613 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 pIdx..**.** If
20614 73 75 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f successful, a po
20615 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 inter to the new
20616 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 structure is re
20617 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 turned. In this
20618 63 61 73 65 0a 2a 2a 20 74 68 65 20 63 61 6c 6c case.** the call
20619 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c er is responsibl
2061a 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 20 73 71 e for calling sq
2061b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
2061c 29 20 6f 6e 20 74 68 65 20 72 65 74 75 72 6e 65 ) on the returne
2061d 64 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 49 d .** pointer. I
2061e 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
2061f 73 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 s (out of memory
20620 20 6f 72 20 6d 69 73 73 69 6e 67 20 63 6f 6c 6c or missing coll
20621 61 74 69 6f 6e 20 0a 2a 2a 20 73 65 71 75 65 6e ation .** sequen
20622 63 65 29 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 ce), NULL is ret
20623 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 73 74 urned and the st
20624 61 74 65 20 6f 66 20 70 50 61 72 73 65 20 75 70 ate of pParse up
20625 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74 dated to reflect
20626 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a .** the error..*
20627 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20628 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 KeyInfo *sqlite
20629 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 3IndexKeyinfo(Pa
2062a 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e 64 rse *pParse, Ind
2062b 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69 6e 74 ex *pIdx){. int
2062c 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d i;. int nCol =
2062d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a pIdx->nColumn;.
2062e 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 int nBytes = s
2062f 69 7a 65 6f 66 28 4b 65 79 49 6e 66 6f 29 20 2b izeof(KeyInfo) +
20630 20 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 (nCol-1)*sizeof
20631 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20 6e 43 6f (CollSeq*) + nCo
20632 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 l;. sqlite3 *db
20633 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
20634 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d KeyInfo *pKey =
20635 20 28 4b 65 79 49 6e 66 6f 20 2a 29 73 71 6c 69 (KeyInfo *)sqli
20636 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
20637 64 62 2c 20 6e 42 79 74 65 73 29 3b 0a 0a 20 20 db, nBytes);..
20638 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 if( pKey ){.
20639 70 4b 65 79 2d 3e 64 62 20 3d 20 70 50 61 72 73 pKey->db = pPars
2063a 65 2d 3e 64 62 3b 0a 20 20 20 20 70 4b 65 79 2d e->db;. pKey-
2063b 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 >aSortOrder = (u
2063c 38 20 2a 29 26 28 70 4b 65 79 2d 3e 61 43 6f 6c 8 *)&(pKey->aCol
2063d 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 20 20 61 73 l[nCol]);. as
2063e 73 65 72 74 28 20 26 70 4b 65 79 2d 3e 61 53 6f sert( &pKey->aSo
2063f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d 3d 3d 26 rtOrder[nCol]==&
20640 28 28 28 75 38 20 2a 29 70 4b 65 79 29 5b 6e 42 (((u8 *)pKey)[nB
20641 79 74 65 73 5d 29 20 29 3b 0a 20 20 20 20 66 6f ytes]) );. fo
20642 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 r(i=0; i<nCol; i
20643 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 ++){. char
20644 2a 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 *zColl = pIdx->a
20645 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 zColl[i];.
20646 61 73 73 65 72 74 28 20 7a 43 6f 6c 6c 20 29 3b assert( zColl );
20647 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 43 6f . pKey->aCo
20648 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 4c ll[i] = sqlite3L
20649 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 ocateCollSeq(pPa
2064a 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b rse, zColl, -1);
2064b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f . pKey->aSo
2064c 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 64 rtOrder[i] = pId
2064d 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d x->aSortOrder[i]
2064e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4b 65 79 ;. }. pKey
2064f 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 43 6f 6c 3b ->nField = nCol;
20650 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 72 . }.. if( pPar
20651 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 se->nErr ){.
20652 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
20653 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 70 4b 65 , pKey);. pKe
20654 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 y = 0;. }. ret
20655 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a 2f 2a 2a urn pKey;.}../**
20656 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
20657 20 6f 66 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a of build.c ****
20658 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20659 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2065a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
2065b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
2065c 69 6e 20 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b in file callback
2065d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
2065e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2065f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
20660 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 33 20 0a ** 2005 May 23 .
20661 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
20662 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
20663 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
20664 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
20665 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
20666 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
20667 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
20668 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
20669 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
2066a 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
2066b 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
2066c 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
2066d 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
2066e 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
2066f 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
20670 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
20671 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
20672 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
20673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20674 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20675 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20676 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
20677 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
20678 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e ontains function
20679 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 s used to access
2067a 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 the internal ha
2067b 73 68 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 sh tables.** of
2067c 75 73 65 72 20 64 65 66 69 6e 65 64 20 66 75 6e user defined fun
2067d 63 74 69 6f 6e 73 20 61 6e 64 20 63 6f 6c 6c 61 ctions and colla
2067e 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2e 0a tion sequences..
2067f 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 61 6c 6c 62 **.** $Id: callb
20680 61 63 6b 2e 63 2c 76 20 31 2e 33 32 20 32 30 30 ack.c,v 1.32 200
20681 38 2f 31 30 2f 31 30 20 31 37 3a 34 31 3a 32 39 8/10/10 17:41:29
20682 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
20683 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 /*.** Invoke the
20684 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 'collation need
20685 65 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 ed' callback to
20686 72 65 71 75 65 73 74 20 61 20 63 6f 6c 6c 61 74 request a collat
20687 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 ion sequence.**
20688 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
20689 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f 66 text encoding of
2068a 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e name zName, len
2068b 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 20 49 66 gth nName..** If
2068c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 the collation s
2068d 65 71 75 65 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 equence.*/.stati
2068e 63 20 76 6f 69 64 20 63 61 6c 6c 43 6f 6c 6c 4e c void callCollN
2068f 65 65 64 65 64 28 73 71 6c 69 74 65 33 20 2a 64 eeded(sqlite3 *d
20690 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
20691 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 Name, int nName)
20692 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d {. assert( !db-
20693 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 7c 7c 20 >xCollNeeded ||
20694 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 !db->xCollNeeded
20695 31 36 20 29 3b 0a 20 20 69 66 28 20 6e 4e 61 6d 16 );. if( nNam
20696 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d 20 73 71 e<0 ) nName = sq
20697 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 lite3Strlen(db,
20698 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 64 62 zName);. if( db
20699 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 29 7b ->xCollNeeded ){
2069a 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 78 74 65 . char *zExte
2069b 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 rnal = sqlite3Db
2069c 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 4e 61 6d StrNDup(db, zNam
2069d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 e, nName);. i
2069e 66 28 20 21 7a 45 78 74 65 72 6e 61 6c 20 29 20 f( !zExternal )
2069f 72 65 74 75 72 6e 3b 0a 20 20 20 20 64 62 2d 3e return;. db->
206a0 78 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2d 3e xCollNeeded(db->
206a1 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 pCollNeededArg,
206a2 64 62 2c 20 28 69 6e 74 29 45 4e 43 28 64 62 29 db, (int)ENC(db)
206a3 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 , zExternal);.
206a4 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
206a5 64 62 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a db, zExternal);.
206a6 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
206a7 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 TE_OMIT_UTF16.
206a8 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 if( db->xCollNee
206a9 64 65 64 31 36 20 29 7b 0a 20 20 20 20 63 68 61 ded16 ){. cha
206aa 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74 65 72 6e r const *zExtern
206ab 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f al;. sqlite3_
206ac 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d 20 73 71 value *pTmp = sq
206ad 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 lite3ValueNew(db
206ae 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 );. sqlite3Va
206af 6c 75 65 53 65 74 53 74 72 28 70 54 6d 70 2c 20 lueSetStr(pTmp,
206b0 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 53 51 nName, zName, SQ
206b1 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
206b2 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7a E_STATIC);. z
206b3 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 External = sqlit
206b4 65 33 56 61 6c 75 65 54 65 78 74 28 70 54 6d 70 e3ValueText(pTmp
206b5 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 , SQLITE_UTF16NA
206b6 54 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20 7a TIVE);. if( z
206b7 45 78 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20 External ){.
206b8 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 db->xCollNeede
206b9 64 31 36 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 d16(db->pCollNee
206ba 64 65 64 41 72 67 2c 20 64 62 2c 20 28 69 6e 74 dedArg, db, (int
206bb 29 45 4e 43 28 64 62 29 2c 20 7a 45 78 74 65 72 )ENC(db), zExter
206bc 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 nal);. }.
206bd 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 sqlite3ValueFree
206be 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e 64 (pTmp);. }.#end
206bf 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 if.}../*.** This
206c0 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
206c1 65 64 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 ed if the collat
206c2 69 6f 6e 20 66 61 63 74 6f 72 79 20 66 61 69 6c ion factory fail
206c3 73 20 74 6f 20 64 65 6c 69 76 65 72 20 61 0a 2a s to deliver a.*
206c4 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 * collation func
206c5 74 69 6f 6e 20 69 6e 20 74 68 65 20 62 65 73 74 tion in the best
206c6 20 65 6e 63 6f 64 69 6e 67 20 62 75 74 20 74 68 encoding but th
206c7 65 72 65 20 6d 61 79 20 62 65 20 6f 74 68 65 72 ere may be other
206c8 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 versions.** of
206c9 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 this collation f
206ca 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 6f 74 68 unction (for oth
206cb 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 er text encoding
206cc 73 29 20 61 76 61 69 6c 61 62 6c 65 2e 20 55 73 s) available. Us
206cd 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 73 e one.** of thes
206ce 65 20 69 6e 73 74 65 61 64 20 69 66 20 74 68 65 e instead if the
206cf 79 20 65 78 69 73 74 2e 20 41 76 6f 69 64 20 61 y exist. Avoid a
206d0 20 55 54 46 2d 38 20 3c 2d 3e 20 55 54 46 2d 31 UTF-8 <-> UTF-1
206d1 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 0a 6 conversion if.
206d2 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a ** possible..*/.
206d3 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 74 68 static int synth
206d4 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 20 CollSeq(sqlite3
206d5 2a 64 62 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 *db, CollSeq *pC
206d6 6f 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 oll){. CollSeq
206d7 2a 70 43 6f 6c 6c 32 3b 0a 20 20 63 68 61 72 20 *pColl2;. char
206d8 2a 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d *z = pColl->zNam
206d9 65 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 74 72 e;. int n = str
206da 6c 65 6e 28 7a 29 3b 0a 20 20 69 6e 74 20 69 3b len(z);. int i;
206db 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
206dc 75 38 20 61 45 6e 63 5b 5d 20 3d 20 7b 20 53 51 u8 aEnc[] = { SQ
206dd 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51 LITE_UTF16BE, SQ
206de 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 53 51 LITE_UTF16LE, SQ
206df 4c 49 54 45 5f 55 54 46 38 20 7d 3b 0a 20 20 66 LITE_UTF8 };. f
206e0 6f 72 28 69 3d 30 3b 20 69 3c 33 3b 20 69 2b 2b or(i=0; i<3; i++
206e1 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 32 20 3d 20 ){. pColl2 =
206e2 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 sqlite3FindCollS
206e3 65 71 28 64 62 2c 20 61 45 6e 63 5b 69 5d 2c 20 eq(db, aEnc[i],
206e4 7a 2c 20 6e 2c 20 30 29 3b 0a 20 20 20 20 69 66 z, n, 0);. if
206e5 28 20 70 43 6f 6c 6c 32 2d 3e 78 43 6d 70 21 3d ( pColl2->xCmp!=
206e6 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 0 ){. memcp
206e7 79 28 70 43 6f 6c 6c 2c 20 70 43 6f 6c 6c 32 2c y(pColl, pColl2,
206e8 20 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 29 sizeof(CollSeq)
206e9 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 2d 3e );. pColl->
206ea 78 44 65 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 xDel = 0;
206eb 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 70 79 /* Do not copy
206ec 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
206ed 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 */. return
206ee 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
206ef 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
206f0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f LITE_ERROR;.}../
206f1 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
206f2 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c on is responsibl
206f3 65 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 e for invoking t
206f4 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 he collation fac
206f5 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a tory callback.**
206f6 20 6f 72 20 73 75 62 73 74 69 74 75 74 69 6e 67 or substituting
206f7 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 a collation seq
206f8 75 65 6e 63 65 20 6f 66 20 61 20 64 69 66 66 65 uence of a diffe
206f9 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67 20 77 68 rent encoding wh
206fa 65 6e 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 en the.** reques
206fb 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 ted collation se
206fc 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 quence is not av
206fd 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 64 ailable in the d
206fe 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 0a 2a atabase native.*
206ff 2a 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20 0a * encoding..** .
20700 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 ** If it is not
20701 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 43 6f 6c 6c NULL, then pColl
20702 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 74 must point to t
20703 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 he database nati
20704 76 65 20 65 6e 63 6f 64 69 6e 67 20 0a 2a 2a 20 ve encoding .**
20705 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
20706 63 65 20 77 69 74 68 20 6e 61 6d 65 20 7a 4e 61 ce with name zNa
20707 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 me, length nName
20708 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
20709 72 6e 20 76 61 6c 75 65 20 69 73 20 65 69 74 68 rn value is eith
2070a 65 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e er the collation
2070b 20 73 65 71 75 65 6e 63 65 20 74 6f 20 62 65 20 sequence to be
2070c 75 73 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 used in database
2070d 0a 2a 2a 20 64 62 20 66 6f 72 20 63 6f 6c 6c 61 .** db for colla
2070e 74 69 6f 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a tion type name z
2070f 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 Name, length nNa
20710 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20 69 66 20 me, or NULL, if
20711 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 no collation.**
20712 73 65 71 75 65 6e 63 65 20 63 61 6e 20 62 65 20 sequence can be
20713 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 found..*/.SQLITE
20714 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 _PRIVATE CollSeq
20715 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c *sqlite3GetColl
20716 53 65 71 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 Seq(. sqlite3*
20717 64 62 2c 20 0a 20 20 43 6f 6c 6c 53 65 71 20 2a db, . CollSeq *
20718 70 43 6f 6c 6c 2c 20 0a 20 20 63 6f 6e 73 74 20 pColl, . const
20719 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 char *zName, .
2071a 69 6e 74 20 6e 4e 61 6d 65 0a 29 7b 0a 20 20 43 int nName.){. C
2071b 6f 6c 6c 53 65 71 20 2a 70 3b 0a 0a 20 20 70 20 ollSeq *p;.. p
2071c 3d 20 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 21 = pColl;. if( !
2071d 70 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c p ){. p = sql
2071e 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 ite3FindCollSeq(
2071f 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 4e 61 db, ENC(db), zNa
20720 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20 me, nName, 0);.
20721 20 7d 0a 20 20 69 66 28 20 21 70 20 7c 7c 20 21 }. if( !p || !
20722 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 2f p->xCmp ){. /
20723 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 * No collation s
20724 65 71 75 65 6e 63 65 20 6f 66 20 74 68 69 73 20 equence of this
20725 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 65 6e type for this en
20726 63 6f 64 69 6e 67 20 69 73 20 72 65 67 69 73 74 coding is regist
20727 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20 43 61 6c ered.. ** Cal
20728 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 l the collation
20729 66 61 63 74 6f 72 79 20 74 6f 20 73 65 65 20 69 factory to see i
2072a 66 20 69 74 20 63 61 6e 20 73 75 70 70 6c 79 20 f it can supply
2072b 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a 20 20 20 us with one..
2072c 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43 6f 6c 6c */. callColl
2072d 4e 65 65 64 65 64 28 64 62 2c 20 7a 4e 61 6d 65 Needed(db, zName
2072e 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 70 20 , nName);. p
2072f 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c = sqlite3FindCol
20730 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 lSeq(db, ENC(db)
20731 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 , zName, nName,
20732 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 0);. }. if( p
20733 26 26 20 21 70 2d 3e 78 43 6d 70 20 26 26 20 73 && !p->xCmp && s
20734 79 6e 74 68 43 6f 6c 6c 53 65 71 28 64 62 2c 20 ynthCollSeq(db,
20735 70 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 30 3b p) ){. p = 0;
20736 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 . }. assert( !
20737 70 20 7c 7c 20 70 2d 3e 78 43 6d 70 20 29 3b 0a p || p->xCmp );.
20738 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
20739 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
2073a 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 e is called on a
2073b 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
2073c 6e 63 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 nce before it is
2073d 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 68 65 63 used to.** chec
2073e 6b 20 74 68 61 74 20 69 74 20 69 73 20 64 65 66 k that it is def
2073f 69 6e 65 64 2e 20 41 6e 20 75 6e 64 65 66 69 6e ined. An undefin
20740 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 ed collation seq
20741 75 65 6e 63 65 20 65 78 69 73 74 73 20 77 68 65 uence exists whe
20742 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 n.** a database
20743 69 73 20 6c 6f 61 64 65 64 20 74 68 61 74 20 63 is loaded that c
20744 6f 6e 74 61 69 6e 73 20 72 65 66 65 72 65 6e 63 ontains referenc
20745 65 73 20 74 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 es to collation
20746 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 74 68 61 sequences.** tha
20747 74 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 t have not been
20748 64 65 66 69 6e 65 64 20 62 79 20 73 71 6c 69 74 defined by sqlit
20749 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
2074a 69 6f 6e 28 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a ion() etc..**.**
2074b 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 74 68 If required, th
2074c 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 is routine calls
2074d 20 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 the 'collation
2074e 6e 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63 6b needed' callback
2074f 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20 61 to.** request a
20750 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 definition of t
20751 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 he collating seq
20752 75 65 6e 63 65 2e 20 49 66 20 74 68 69 73 20 64 uence. If this d
20753 6f 65 73 6e 27 74 20 77 6f 72 6b 2c 20 0a 2a 2a oesn't work, .**
20754 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 63 an equivalent c
20755 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
20756 65 20 74 68 61 74 20 75 73 65 73 20 61 20 74 65 e that uses a te
20757 78 74 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 xt encoding diff
20758 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 erent.** from th
20759 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
2075a 69 73 20 73 75 62 73 74 69 74 75 74 65 64 2c 20 is substituted,
2075b 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c 61 if one is availa
2075c 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ble..*/.SQLITE_P
2075d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
2075e 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50 e3CheckCollSeq(P
2075f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 43 6f arse *pParse, Co
20760 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20 llSeq *pColl){.
20761 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( pColl ){.
20762 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
20763 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 ame = pColl->zNa
20764 6d 65 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 me;. CollSeq
20765 2a 70 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43 *p = sqlite3GetC
20766 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2d 3e 64 ollSeq(pParse->d
20767 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c b, pColl, zName,
20768 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20 21 70 -1);. if( !p
20769 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ){. if( pP
2076a 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b arse->nErr==0 ){
2076b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
2076c 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
2076d 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74 "no such collat
2076e 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25 73 ion sequence: %s
2076f 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 ", zName);.
20770 20 7d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d }. pParse-
20771 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 >nErr++;. r
20772 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
20773 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 OR;. }. as
20774 73 65 72 74 28 20 70 3d 3d 70 43 6f 6c 6c 20 29 sert( p==pColl )
20775 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
20776 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f QLITE_OK;.}..../
20777 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 6e 64 20 *.** Locate and
20778 72 65 74 75 72 6e 20 61 6e 20 65 6e 74 72 79 20 return an entry
20779 66 72 6f 6d 20 74 68 65 20 64 62 2e 61 43 6f 6c from the db.aCol
2077a 6c 53 65 71 20 68 61 73 68 20 74 61 62 6c 65 2e lSeq hash table.
2077b 20 49 66 20 74 68 65 20 65 6e 74 72 79 0a 2a 2a If the entry.**
2077c 20 73 70 65 63 69 66 69 65 64 20 62 79 20 7a 4e specified by zN
2077d 61 6d 65 20 61 6e 64 20 6e 4e 61 6d 65 20 69 73 ame and nName is
2077e 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 70 not found and p
2077f 61 72 61 6d 65 74 65 72 20 27 63 72 65 61 74 65 arameter 'create
20780 27 20 69 73 0a 2a 2a 20 74 72 75 65 2c 20 74 68 ' is.** true, th
20781 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 en create a new
20782 65 6e 74 72 79 2e 20 4f 74 68 65 72 77 69 73 65 entry. Otherwise
20783 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a return NULL..**
20784 0a 2a 2a 20 45 61 63 68 20 70 6f 69 6e 74 65 72 .** Each pointer
20785 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 stored in the s
20786 71 6c 69 74 65 33 2e 61 43 6f 6c 6c 53 65 71 20 qlite3.aCollSeq
20787 68 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 hash table conta
20788 69 6e 73 20 61 6e 0a 2a 2a 20 61 72 72 61 79 20 ins an.** array
20789 6f 66 20 74 68 72 65 65 20 43 6f 6c 6c 53 65 71 of three CollSeq
2078a 20 73 74 72 75 63 74 75 72 65 73 2e 20 54 68 65 structures. The
2078b 20 66 69 72 73 74 20 69 73 20 74 68 65 20 63 6f first is the co
2078c 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
2078d 0a 2a 2a 20 70 72 65 66 66 65 72 72 65 64 20 66 .** prefferred f
2078e 6f 72 20 55 54 46 2d 38 2c 20 74 68 65 20 73 65 or UTF-8, the se
2078f 63 6f 6e 64 20 55 54 46 2d 31 36 6c 65 2c 20 61 cond UTF-16le, a
20790 6e 64 20 74 68 65 20 74 68 69 72 64 20 55 54 46 nd the third UTF
20791 2d 31 36 62 65 2e 0a 2a 2a 0a 2a 2a 20 53 74 6f -16be..**.** Sto
20792 72 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 red immediately
20793 61 66 74 65 72 20 74 68 65 20 74 68 72 65 65 20 after the three
20794 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
20795 63 65 73 20 69 73 20 61 20 63 6f 70 79 20 6f 66 ces is a copy of
20796 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f .** the collatio
20797 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 2e n sequence name.
20798 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 A pointer to th
20799 69 73 20 73 74 72 69 6e 67 20 69 73 20 73 74 6f is string is sto
2079a 72 65 64 20 69 6e 0a 2a 2a 20 65 61 63 68 20 63 red in.** each c
2079b 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
2079c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a e structure..*/.
2079d 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a static CollSeq *
2079e 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 findCollSeqEntry
2079f 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
207a0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
207a1 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d Name,. int nNam
207a2 65 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a e,. int create.
207a3 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 ){. CollSeq *pC
207a4 6f 6c 6c 3b 0a 20 20 69 66 28 20 6e 4e 61 6d 65 oll;. if( nName
207a5 3c 30 20 29 20 6e 4e 61 6d 65 20 3d 20 73 71 6c <0 ) nName = sql
207a6 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a ite3Strlen(db, z
207a7 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c 6c 20 3d Name);. pColl =
207a8 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 sqlite3HashFind
207a9 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 (&db->aCollSeq,
207aa 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 0a zName, nName);..
207ab 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c 6c 20 26 if( 0==pColl &
207ac 26 20 63 72 65 61 74 65 20 29 7b 0a 20 20 20 20 & create ){.
207ad 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44 pColl = sqlite3D
207ae 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
207af 33 2a 73 69 7a 65 6f 66 28 2a 70 43 6f 6c 6c 29 3*sizeof(*pColl)
207b0 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 20 29 3b 0a + nName + 1 );.
207b1 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b if( pColl ){
207b2 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a . CollSeq *
207b3 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 pDel = 0;.
207b4 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 20 3d pColl[0].zName =
207b5 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 (char*)&pColl[3
207b6 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 ];. pColl[0
207b7 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 ].enc = SQLITE_U
207b8 54 46 38 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c TF8;. pColl
207b9 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 [1].zName = (cha
207ba 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 r*)&pColl[3];.
207bb 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e 65 6e 63 pColl[1].enc
207bc 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c = SQLITE_UTF16L
207bd 45 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32 E;. pColl[2
207be 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a ].zName = (char*
207bf 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 )&pColl[3];.
207c0 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65 6e 63 20 3d pColl[2].enc =
207c1 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b SQLITE_UTF16BE;
207c2 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43 . memcpy(pC
207c3 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e oll[0].zName, zN
207c4 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 ame, nName);.
207c5 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d pColl[0].zNam
207c6 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 e[nName] = 0;.
207c7 20 20 20 20 70 44 65 6c 20 3d 20 73 71 6c 69 74 pDel = sqlit
207c8 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 64 62 e3HashInsert(&db
207c9 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 70 43 6f 6c ->aCollSeq, pCol
207ca 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d l[0].zName, nNam
207cb 65 2c 20 70 43 6f 6c 6c 29 3b 0a 0a 20 20 20 20 e, pColl);..
207cc 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63 /* If a malloc
207cd 28 29 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 () failure occur
207ce 65 64 20 69 6e 20 73 71 6c 69 74 65 33 48 61 73 ed in sqlite3Has
207cf 68 49 6e 73 65 72 74 28 29 2c 20 69 74 20 77 69 hInsert(), it wi
207d0 6c 6c 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 74 ll . ** ret
207d1 75 72 6e 20 74 68 65 20 70 43 6f 6c 6c 20 70 6f urn the pColl po
207d2 69 6e 74 65 72 20 74 6f 20 62 65 20 64 65 6c 65 inter to be dele
207d3 74 65 64 20 28 62 65 63 61 75 73 65 20 69 74 20 ted (because it
207d4 77 61 73 6e 27 74 20 61 64 64 65 64 0a 20 20 20 wasn't added.
207d5 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 68 61 73 ** to the has
207d6 68 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 20 20 h table)..
207d7 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
207d8 20 70 44 65 6c 3d 3d 30 20 7c 7c 20 70 44 65 6c pDel==0 || pDel
207d9 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 ==pColl );.
207da 20 69 66 28 20 70 44 65 6c 21 3d 30 20 29 7b 0a if( pDel!=0 ){.
207db 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c db->mall
207dc 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
207dd 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
207de 72 65 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 ree(db, pDel);.
207df 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 pColl = 0
207e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
207e1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f }. return pCo
207e2 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 ll;.}../*.** Par
207e3 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f 69 ameter zName poi
207e4 6e 74 73 20 74 6f 20 61 20 55 54 46 2d 38 20 65 nts to a UTF-8 e
207e5 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 20 6e 4e ncoded string nN
207e6 61 6d 65 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a ame bytes long..
207e7 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 43 6f ** Return the Co
207e8 6c 6c 53 65 71 2a 20 70 6f 69 6e 74 65 72 20 66 llSeq* pointer f
207e9 6f 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e or the collation
207ea 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 sequence named
207eb 7a 4e 61 6d 65 0a 2a 2a 20 66 6f 72 20 74 68 65 zName.** for the
207ec 20 65 6e 63 6f 64 69 6e 67 20 27 65 6e 63 27 20 encoding 'enc'
207ed 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
207ee 65 20 27 64 62 27 2e 0a 2a 2a 0a 2a 2a 20 49 66 e 'db'..**.** If
207ef 20 74 68 65 20 65 6e 74 72 79 20 73 70 65 63 69 the entry speci
207f0 66 69 65 64 20 69 73 20 6e 6f 74 20 66 6f 75 6e fied is not foun
207f1 64 20 61 6e 64 20 27 63 72 65 61 74 65 27 20 69 d and 'create' i
207f2 73 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65 s true, then cre
207f3 61 74 65 20 61 0a 2a 2a 20 6e 65 77 20 65 6e 74 ate a.** new ent
207f4 72 79 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 ry. Otherwise r
207f5 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a eturn NULL..**.*
207f6 2a 20 41 20 73 65 70 61 72 61 74 65 20 66 75 6e * A separate fun
207f7 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 4c 6f 63 ction sqlite3Loc
207f8 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 73 20 ateCollSeq() is
207f9 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 a wrapper around
207fa 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
207fb 2e 20 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 . sqlite3Locate
207fc 43 6f 6c 6c 53 65 71 28 29 20 69 6e 76 6f 6b 65 CollSeq() invoke
207fd 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 s the collation
207fe 66 61 63 74 6f 72 79 0a 2a 2a 20 69 66 20 6e 65 factory.** if ne
207ff 63 65 73 73 61 72 79 20 61 6e 64 20 67 65 6e 65 cessary and gene
20800 72 61 74 65 73 20 61 6e 20 65 72 72 6f 72 20 6d rates an error m
20801 65 73 73 61 67 65 20 69 66 20 74 68 65 20 63 6f essage if the co
20802 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
20803 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f .** cannot be fo
20804 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 und..*/.SQLITE_P
20805 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a RIVATE CollSeq *
20806 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 sqlite3FindCollS
20807 65 71 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 eq(. sqlite3 *d
20808 62 2c 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20 63 b,. u8 enc,. c
20809 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
2080a 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 0a 20 ,. int nName,.
2080b 20 69 6e 74 20 63 72 65 61 74 65 0a 29 7b 0a 20 int create.){.
2080c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b CollSeq *pColl;
2080d 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a . if( zName ){.
2080e 20 20 20 20 70 43 6f 6c 6c 20 3d 20 66 69 6e 64 pColl = find
2080f 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28 64 62 2c CollSeqEntry(db,
20810 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 63 zName, nName, c
20811 72 65 61 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b reate);. }else{
20812 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d . pColl = db-
20813 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a >pDfltColl;. }.
20814 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 assert( SQLITE
20815 5f 55 54 46 38 3d 3d 31 20 26 26 20 53 51 4c 49 _UTF8==1 && SQLI
20816 54 45 5f 55 54 46 31 36 4c 45 3d 3d 32 20 26 26 TE_UTF16LE==2 &&
20817 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3d SQLITE_UTF16BE=
20818 3d 33 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =3 );. assert(
20819 65 6e 63 3e 3d 53 51 4c 49 54 45 5f 55 54 46 38 enc>=SQLITE_UTF8
2081a 20 26 26 20 65 6e 63 3c 3d 53 51 4c 49 54 45 5f && enc<=SQLITE_
2081b 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 UTF16BE );. if(
2081c 20 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b pColl ) pColl +
2081d 3d 20 65 6e 63 2d 31 3b 0a 20 20 72 65 74 75 72 = enc-1;. retur
2081e 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20 44 n pColl;.}../* D
2081f 75 72 69 6e 67 20 74 68 65 20 73 65 61 72 63 68 uring the search
20820 20 66 6f 72 20 74 68 65 20 62 65 73 74 20 66 75 for the best fu
20821 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f nction definitio
20822 6e 2c 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 n, this procedur
20823 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 e.** is called t
20824 6f 20 74 65 73 74 20 68 6f 77 20 77 65 6c 6c 20 o test how well
20825 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73 the function pas
20826 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 sed as the first
20827 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6d 61 74 argument.** mat
20828 63 68 65 73 20 74 68 65 20 72 65 71 75 65 73 74 ches the request
20829 20 66 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 for a function
2082a 77 69 74 68 20 6e 41 72 67 20 61 72 67 75 6d 65 with nArg argume
2082b 6e 74 73 20 69 6e 20 61 20 73 79 73 74 65 6d 0a nts in a system.
2082c 2a 2a 20 74 68 61 74 20 75 73 65 73 20 65 6e 63 ** that uses enc
2082d 6f 64 69 6e 67 20 65 6e 63 2e 20 54 68 65 20 76 oding enc. The v
2082e 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e alue returned in
2082f 64 69 63 61 74 65 73 20 68 6f 77 20 77 65 6c 6c dicates how well
20830 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 20 the.** request
20831 69 73 20 6d 61 74 63 68 65 64 2e 20 41 20 68 69 is matched. A hi
20832 67 68 65 72 20 76 61 6c 75 65 20 69 6e 64 69 63 gher value indic
20833 61 74 65 73 20 61 20 62 65 74 74 65 72 20 6d 61 ates a better ma
20834 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 tch..**.** The r
20835 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 eturned value is
20836 20 61 6c 77 61 79 73 20 62 65 74 77 65 65 6e 20 always between
20837 31 20 61 6e 64 20 36 2c 20 61 73 20 66 6f 6c 6c 1 and 6, as foll
20838 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 41 20 ows:.**.** 1: A
20839 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 6e variable argumen
2083a 74 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 ts function that
2083b 20 70 72 65 66 65 72 73 20 55 54 46 2d 38 20 77 prefers UTF-8 w
2083c 68 65 6e 20 61 20 55 54 46 2d 31 36 0a 2a 2a 20 hen a UTF-16.**
2083d 20 20 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 encoding is r
2083e 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63 equested, or vic
2083f 65 20 76 65 72 73 61 2e 0a 2a 2a 20 32 3a 20 41 e versa..** 2: A
20840 20 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 variable argume
20841 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 nts function tha
20842 74 20 75 73 65 73 20 55 54 46 2d 31 36 42 45 20 t uses UTF-16BE
20843 77 68 65 6e 20 55 54 46 2d 31 36 4c 45 20 69 73 when UTF-16LE is
20844 0a 2a 2a 20 20 20 20 72 65 71 75 65 73 74 65 64 .** requested
20845 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e , or vice versa.
20846 0a 2a 2a 20 33 3a 20 41 20 76 61 72 69 61 62 6c .** 3: A variabl
20847 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 e arguments func
20848 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 20 73 tion using the s
20849 61 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e ame text encodin
2084a 67 2e 0a 2a 2a 20 34 3a 20 41 20 66 75 6e 63 74 g..** 4: A funct
2084b 69 6f 6e 20 77 69 74 68 20 74 68 65 20 65 78 61 ion with the exa
2084c 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 ct number of arg
2084d 75 6d 65 6e 74 73 20 72 65 71 75 65 73 74 65 64 uments requested
2084e 20 74 68 61 74 0a 2a 2a 20 20 20 20 70 72 65 66 that.** pref
2084f 65 72 73 20 55 54 46 2d 38 20 77 68 65 6e 20 61 ers UTF-8 when a
20850 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 UTF-16 encoding
20851 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f is requested, o
20852 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a r vice versa..**
20853 20 35 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77 5: A function w
20854 69 74 68 20 74 68 65 20 65 78 61 63 74 20 6e 75 ith the exact nu
20855 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
20856 73 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74 s requested that
20857 0a 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20 55 .** prefers U
20858 54 46 2d 31 36 4c 45 20 77 68 65 6e 20 55 54 46 TF-16LE when UTF
20859 2d 31 36 42 45 20 69 73 20 72 65 71 75 65 73 74 -16BE is request
2085a 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 ed, or vice vers
2085b 61 2e 0a 2a 2a 20 36 3a 20 41 6e 20 65 78 61 63 a..** 6: An exac
2085c 74 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2f 0a 73 t match..**.*/.s
2085d 74 61 74 69 63 20 69 6e 74 20 6d 61 74 63 68 51 tatic int matchQ
2085e 75 61 6c 69 74 79 28 46 75 6e 63 44 65 66 20 2a uality(FuncDef *
2085f 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 75 38 20 p, int nArg, u8
20860 65 6e 63 29 7b 0a 20 20 69 6e 74 20 6d 61 74 63 enc){. int matc
20861 68 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e h = 0;. if( p->
20862 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 70 2d 3e 6e nArg==-1 || p->n
20863 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20 6e 41 72 Arg==nArg || nAr
20864 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 6d 61 74 g==-1 ){. mat
20865 63 68 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 ch = 1;. if(
20866 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c p->nArg==nArg ||
20867 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 nArg==-1 ){.
20868 20 20 20 6d 61 74 63 68 20 3d 20 34 3b 0a 20 20 match = 4;.
20869 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 3d }. if( enc=
2086a 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20 29 7b 0a =p->iPrefEnc ){.
2086b 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d 20 32 match += 2
2086c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 ;. }. else
2086d 20 69 66 28 20 28 65 6e 63 3d 3d 53 51 4c 49 54 if( (enc==SQLIT
2086e 45 5f 55 54 46 31 36 4c 45 20 26 26 20 70 2d 3e E_UTF16LE && p->
2086f 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49 54 45 iPrefEnc==SQLITE
20870 5f 55 54 46 31 36 42 45 29 20 7c 7c 0a 20 20 20 _UTF16BE) ||.
20871 20 20 20 20 20 20 20 20 20 20 28 65 6e 63 3d 3d (enc==
20872 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 26 SQLITE_UTF16BE &
20873 26 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 & p->iPrefEnc==S
20874 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 20 29 QLITE_UTF16LE) )
20875 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d {. match +=
20876 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
20877 72 65 74 75 72 6e 20 6d 61 74 63 68 3b 0a 7d 0a return match;.}.
20878 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 61 20 ./*.** Search a
20879 46 75 6e 63 44 65 66 48 61 73 68 20 66 6f 72 20 FuncDefHash for
2087a 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 a function with
2087b 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 20 the given name.
2087c 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 Return.** a poi
2087d 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 61 74 63 nter to the matc
2087e 68 69 6e 67 20 46 75 6e 63 44 65 66 20 69 66 20 hing FuncDef if
2087f 66 6f 75 6e 64 2c 20 6f 72 20 30 20 69 66 20 74 found, or 0 if t
20880 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 here is no match
20881 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46 75 6e 63 ..*/.static Func
20882 44 65 66 20 2a 66 75 6e 63 74 69 6f 6e 53 65 61 Def *functionSea
20883 72 63 68 28 0a 20 20 46 75 6e 63 44 65 66 48 61 rch(. FuncDefHa
20884 73 68 20 2a 70 48 61 73 68 2c 20 20 2f 2a 20 48 sh *pHash, /* H
20885 61 73 68 20 74 61 62 6c 65 20 74 6f 20 73 65 61 ash table to sea
20886 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 rch */. int h,
20887 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
20888 20 48 61 73 68 20 6f 66 20 74 68 65 20 6e 61 6d Hash of the nam
20889 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
2088a 72 20 2a 7a 46 75 6e 63 2c 20 20 20 2f 2a 20 4e r *zFunc, /* N
2088b 61 6d 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 ame of function
2088c 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 20 20 */. int nFunc
2088d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
2088e 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
2088f 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20 20 46 75 zFunc */.){. Fu
20890 6e 63 44 65 66 20 2a 70 3b 0a 20 20 66 6f 72 28 ncDef *p;. for(
20891 70 3d 70 48 61 73 68 2d 3e 61 5b 68 5d 3b 20 70 p=pHash->a[h]; p
20892 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29 7b 0a 20 ; p=p->pHash){.
20893 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 if( sqlite3St
20894 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61 6d 65 2c rNICmp(p->zName,
20895 20 7a 46 75 6e 63 2c 20 6e 46 75 6e 63 29 3d 3d zFunc, nFunc)==
20896 30 20 26 26 20 70 2d 3e 7a 4e 61 6d 65 5b 6e 46 0 && p->zName[nF
20897 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 unc]==0 ){.
20898 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 7d return p;. }
20899 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
2089a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 .}../*.** Insert
2089b 20 61 20 6e 65 77 20 46 75 6e 63 44 65 66 20 69 a new FuncDef i
2089c 6e 74 6f 20 61 20 46 75 6e 63 44 65 66 48 61 73 nto a FuncDefHas
2089d 68 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f h hash table..*/
2089e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
2089f 76 6f 69 64 20 73 71 6c 69 74 65 33 46 75 6e 63 void sqlite3Func
208a0 44 65 66 49 6e 73 65 72 74 28 0a 20 20 46 75 6e DefInsert(. Fun
208a1 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 2c cDefHash *pHash,
208a2 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 74 61 /* The hash ta
208a3 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 ble into which t
208a4 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 46 75 o insert */. Fu
208a5 6e 63 44 65 66 20 2a 70 44 65 66 20 20 20 20 20 ncDef *pDef
208a6 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 /* The functi
208a7 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 74 6f on definition to
208a8 20 69 6e 73 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 insert */.){.
208a9 46 75 6e 63 44 65 66 20 2a 70 4f 74 68 65 72 3b FuncDef *pOther;
208aa 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 . int nName = s
208ab 74 72 6c 65 6e 28 70 44 65 66 2d 3e 7a 4e 61 6d trlen(pDef->zNam
208ac 65 29 3b 0a 20 20 75 38 20 63 31 20 3d 20 28 75 e);. u8 c1 = (u
208ad 38 29 70 44 65 66 2d 3e 7a 4e 61 6d 65 5b 30 5d 8)pDef->zName[0]
208ae 3b 0a 20 20 69 6e 74 20 68 20 3d 20 28 73 71 6c ;. int h = (sql
208af 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 ite3UpperToLower
208b0 5b 63 31 5d 20 2b 20 6e 4e 61 6d 65 29 20 25 20 [c1] + nName) %
208b1 41 72 72 61 79 53 69 7a 65 28 70 48 61 73 68 2d ArraySize(pHash-
208b2 3e 61 29 3b 0a 20 20 70 4f 74 68 65 72 20 3d 20 >a);. pOther =
208b3 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28 70 functionSearch(p
208b4 48 61 73 68 2c 20 68 2c 20 70 44 65 66 2d 3e 7a Hash, h, pDef->z
208b5 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 Name, nName);.
208b6 69 66 28 20 70 4f 74 68 65 72 20 29 7b 0a 20 20 if( pOther ){.
208b7 20 20 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 pDef->pNext =
208b8 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 pOther->pNext;.
208b9 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 pOther->pNext
208ba 20 3d 20 70 44 65 66 3b 0a 20 20 7d 65 6c 73 65 = pDef;. }else
208bb 7b 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78 {. pDef->pNex
208bc 74 20 3d 20 30 3b 0a 20 20 20 20 70 44 65 66 2d t = 0;. pDef-
208bd 3e 70 48 61 73 68 20 3d 20 70 48 61 73 68 2d 3e >pHash = pHash->
208be 61 5b 68 5d 3b 0a 20 20 20 20 70 48 61 73 68 2d a[h];. pHash-
208bf 3e 61 5b 68 5d 20 3d 20 70 44 65 66 3b 0a 20 20 >a[h] = pDef;.
208c0 7d 0a 7d 0a 20 20 0a 20 20 0a 0a 2f 2a 0a 2a 2a }.}. . ../*.**
208c1 20 4c 6f 63 61 74 65 20 61 20 75 73 65 72 20 66 Locate a user f
208c2 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 unction given a
208c3 6e 61 6d 65 2c 20 61 20 6e 75 6d 62 65 72 20 6f name, a number o
208c4 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 f arguments and
208c5 61 20 66 6c 61 67 0a 2a 2a 20 69 6e 64 69 63 61 a flag.** indica
208c6 74 69 6e 67 20 77 68 65 74 68 65 72 20 74 68 65 ting whether the
208c7 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 function prefer
208c8 73 20 55 54 46 2d 31 36 20 6f 76 65 72 20 55 54 s UTF-16 over UT
208c9 46 2d 38 2e 20 20 52 65 74 75 72 6e 20 61 0a 2a F-8. Return a.*
208ca 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * pointer to the
208cb 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 FuncDef structu
208cc 72 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 re that defines
208cd 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 2c 20 6f that function, o
208ce 72 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c r return.** NULL
208cf 20 69 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e if the function
208d0 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e does not exist.
208d1 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 72 .**.** If the cr
208d2 65 61 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e eateFlag argumen
208d3 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 t is true, then
208d4 61 20 6e 65 77 20 28 62 6c 61 6e 6b 29 20 46 75 a new (blank) Fu
208d5 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 ncDef.** structu
208d6 72 65 20 69 73 20 63 72 65 61 74 65 64 20 61 6e re is created an
208d7 64 20 6c 69 6b 65 64 20 69 6e 74 6f 20 74 68 65 d liked into the
208d8 20 22 64 62 22 20 73 74 72 75 63 74 75 72 65 20 "db" structure
208d9 69 66 20 61 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 if a.** no match
208da 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 ing function pre
208db 76 69 6f 75 73 6c 79 20 65 78 69 73 74 65 64 2e viously existed.
208dc 20 20 57 68 65 6e 20 63 72 65 61 74 65 46 6c 61 When createFla
208dd 67 20 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 g is true.** and
208de 20 74 68 65 20 6e 41 72 67 20 70 61 72 61 6d 65 the nArg parame
208df 74 65 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 ter is -1, then
208e0 6f 6e 6c 79 20 61 20 66 75 6e 63 74 69 6f 6e 20 only a function
208e1 74 68 61 74 20 61 63 63 65 70 74 73 0a 2a 2a 20 that accepts.**
208e2 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 any number of ar
208e3 67 75 6d 65 6e 74 73 20 77 69 6c 6c 20 62 65 20 guments will be
208e4 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
208e5 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 If createFlag is
208e6 20 66 61 6c 73 65 20 61 6e 64 20 6e 41 72 67 20 false and nArg
208e7 69 73 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 is -1, then the
208e8 66 69 72 73 74 20 76 61 6c 69 64 0a 2a 2a 20 66 first valid.** f
208e9 75 6e 63 74 69 6f 6e 20 66 6f 75 6e 64 20 69 73 unction found is
208ea 20 72 65 74 75 72 6e 65 64 2e 20 20 41 20 66 75 returned. A fu
208eb 6e 63 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20 nction is valid
208ec 69 66 20 65 69 74 68 65 72 20 78 46 75 6e 63 0a if either xFunc.
208ed 2a 2a 20 6f 72 20 78 53 74 65 70 20 69 73 20 6e ** or xStep is n
208ee 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 on-zero..**.** I
208ef 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 f createFlag is
208f0 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 20 66 75 false, then a fu
208f1 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 nction with the
208f2 72 65 71 75 69 72 65 64 20 6e 61 6d 65 20 61 6e required name an
208f3 64 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 d.** number of a
208f4 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 20 rguments may be
208f5 72 65 74 75 72 6e 65 64 20 65 76 65 6e 20 69 66 returned even if
208f6 20 74 68 65 20 65 54 65 78 74 52 65 70 20 66 6c the eTextRep fl
208f7 61 67 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d ag does not.** m
208f8 61 74 63 68 20 74 68 61 74 20 72 65 71 75 65 73 atch that reques
208f9 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
208fa 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a RIVATE FuncDef *
208fb 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 sqlite3FindFunct
208fc 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a ion(. sqlite3 *
208fd 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 db, /* An
208fe 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f open database */
208ff 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
20900 4e 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 Name, /* Name of
20901 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 the function.
20902 4e 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 Not null-termina
20903 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 ted */. int nNa
20904 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e me, /* N
20905 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
20906 65 72 73 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 ers in the name
20907 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 */. int nArg,
20908 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
20909 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 r of arguments.
2090a 20 2d 31 20 6d 65 61 6e 73 20 61 6e 79 20 6e 75 -1 means any nu
2090b 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 6e 63 mber */. u8 enc
2090c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
2090d 50 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 Preferred text e
2090e 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 ncoding */. int
2090f 20 63 72 65 61 74 65 46 6c 61 67 20 20 20 20 20 createFlag
20910 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 65 6e /* Create new en
20911 74 72 79 20 69 66 20 74 72 75 65 20 61 6e 64 20 try if true and
20912 64 6f 65 73 20 6e 6f 74 20 6f 74 68 65 72 77 69 does not otherwi
20913 73 65 20 65 78 69 73 74 20 2a 2f 0a 29 7b 0a 20 se exist */.){.
20914 20 46 75 6e 63 44 65 66 20 2a 70 3b 20 20 20 20 FuncDef *p;
20915 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 /* Iterator
20916 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 46 variable */. F
20917 75 6e 63 44 65 66 20 2a 70 42 65 73 74 20 3d 20 uncDef *pBest =
20918 30 3b 20 2f 2a 20 42 65 73 74 20 6d 61 74 63 68 0; /* Best match
20919 20 66 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f found so far */
2091a 0a 20 20 69 6e 74 20 62 65 73 74 53 63 6f 72 65 . int bestScore
2091b 20 3d 20 30 3b 20 20 2f 2a 20 53 63 6f 72 65 20 = 0; /* Score
2091c 6f 66 20 62 65 73 74 20 6d 61 74 63 68 20 2a 2f of best match */
2091d 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 . int h;
2091e 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 76 /* Hash v
2091f 61 6c 75 65 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 alue */... asse
20920 72 74 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f rt( enc==SQLITE_
20921 55 54 46 38 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c UTF8 || enc==SQL
20922 49 54 45 5f 55 54 46 31 36 4c 45 20 7c 7c 20 65 ITE_UTF16LE || e
20923 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 nc==SQLITE_UTF16
20924 42 45 20 29 3b 0a 20 20 69 66 28 20 6e 41 72 67 BE );. if( nArg
20925 3c 2d 31 20 29 20 6e 41 72 67 20 3d 20 2d 31 3b <-1 ) nArg = -1;
20926 0a 20 20 68 20 3d 20 28 73 71 6c 69 74 65 33 55 . h = (sqlite3U
20927 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 pperToLower[(u8)
20928 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20 6e 4e 61 6d zName[0]] + nNam
20929 65 29 20 25 20 41 72 72 61 79 53 69 7a 65 28 64 e) % ArraySize(d
2092a 62 2d 3e 61 46 75 6e 63 2e 61 29 3b 0a 0a 20 20 b->aFunc.a);..
2092b 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 20 /* First search
2092c 66 6f 72 20 61 20 6d 61 74 63 68 20 61 6d 6f 6e for a match amon
2092d 67 73 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 gst the applicat
2092e 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
2092f 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 tions.. */. p
20930 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 = functionSearch
20931 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 68 2c 20 (&db->aFunc, h,
20932 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 zName, nName);.
20933 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 while( p ){.
20934 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d 61 74 int score = mat
20935 63 68 51 75 61 6c 69 74 79 28 70 2c 20 6e 41 72 chQuality(p, nAr
20936 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20 69 66 28 g, enc);. if(
20937 20 73 63 6f 72 65 3e 62 65 73 74 53 63 6f 72 65 score>bestScore
20938 20 29 7b 0a 20 20 20 20 20 20 70 42 65 73 74 20 ){. pBest
20939 3d 20 70 3b 0a 20 20 20 20 20 20 62 65 73 74 53 = p;. bestS
2093a 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b 0a 20 20 core = score;.
2093b 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 }. p = p->p
2093c 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Next;. }.. /*
2093d 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 If no match is f
2093e 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 74 68 65 ound, search the
2093f 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 built-in functi
20940 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 ons.. **. ** E
20941 78 63 65 70 74 2c 20 69 66 20 63 72 65 61 74 65 xcept, if create
20942 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 Flag is true, th
20943 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 at means that we
20944 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 0a 20 are trying to.
20945 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6e 65 ** install a ne
20946 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20 57 68 61 w function. Wha
20947 74 65 76 65 72 20 46 75 6e 63 44 65 66 20 73 74 tever FuncDef st
20948 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 ructure is retur
20949 6e 65 64 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61 ned will. ** ha
2094a 76 65 20 66 69 65 6c 64 73 20 6f 76 65 72 77 72 ve fields overwr
2094b 69 74 74 65 6e 20 77 69 74 68 20 6e 65 77 20 69 itten with new i
2094c 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f nformation appro
2094d 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 0a 20 priate for the.
2094e 20 2a 2a 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e ** new function
2094f 2e 20 20 42 75 74 20 74 68 65 20 46 75 6e 63 44 . But the FuncD
20950 65 66 73 20 66 6f 72 20 62 75 69 6c 74 2d 69 6e efs for built-in
20951 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 72 functions are r
20952 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 ead-only.. ** S
20953 6f 20 77 65 20 6d 75 73 74 20 6e 6f 74 20 73 65 o we must not se
20954 61 72 63 68 20 66 6f 72 20 62 75 69 6c 74 2d 69 arch for built-i
20955 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 ns when creating
20956 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e a new function.
20957 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 21 63 72 . */ . if( !cr
20958 65 61 74 65 46 6c 61 67 20 26 26 20 21 70 42 65 eateFlag && !pBe
20959 73 74 20 29 7b 0a 20 20 20 20 46 75 6e 63 44 65 st ){. FuncDe
2095a 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 fHash *pHash = &
2095b 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61 GLOBAL(FuncDefHa
2095c 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sh, sqlite3Globa
2095d 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 20 lFunctions);.
2095e 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 p = functionSea
2095f 72 63 68 28 70 48 61 73 68 2c 20 68 2c 20 7a 4e rch(pHash, h, zN
20960 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 ame, nName);.
20961 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 while( p ){.
20962 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d int score = m
20963 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c 20 6e atchQuality(p, n
20964 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20 20 Arg, enc);.
20965 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53 if( score>bestS
20966 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 core ){.
20967 70 42 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 pBest = p;.
20968 20 20 20 62 65 73 74 53 63 6f 72 65 20 3d 20 73 bestScore = s
20969 63 6f 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 core;. }.
2096a 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 p = p->pNext
2096b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
2096c 2a 20 49 66 20 74 68 65 20 63 72 65 61 74 65 46 * If the createF
2096d 6c 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 lag parameter is
2096e 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 73 65 true and the se
2096f 61 72 63 68 20 64 69 64 20 6e 6f 74 20 72 65 76 arch did not rev
20970 65 61 6c 20 61 6e 0a 20 20 2a 2a 20 65 78 61 63 eal an. ** exac
20971 74 20 6d 61 74 63 68 20 66 6f 72 20 74 68 65 20 t match for the
20972 6e 61 6d 65 2c 20 6e 75 6d 62 65 72 20 6f 66 20 name, number of
20973 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 65 6e arguments and en
20974 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 61 64 64 coding, then add
20975 20 61 0a 20 20 2a 2a 20 6e 65 77 20 65 6e 74 72 a. ** new entr
20976 79 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 y to the hash ta
20977 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 69 ble and return i
20978 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 72 t.. */. if( cr
20979 65 61 74 65 46 6c 61 67 20 26 26 20 28 62 65 73 eateFlag && (bes
2097a 74 53 63 6f 72 65 3c 36 20 7c 7c 20 70 42 65 73 tScore<6 || pBes
2097b 74 2d 3e 6e 41 72 67 21 3d 6e 41 72 67 29 20 26 t->nArg!=nArg) &
2097c 26 20 0a 20 20 20 20 20 20 28 70 42 65 73 74 20 & . (pBest
2097d 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
2097e 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 cZero(db, sizeof
2097f 28 2a 70 42 65 73 74 29 2b 6e 4e 61 6d 65 2b 31 (*pBest)+nName+1
20980 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 42 65 ))!=0 ){. pBe
20981 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 st->zName = (cha
20982 72 20 2a 29 26 70 42 65 73 74 5b 31 5d 3b 0a 20 r *)&pBest[1];.
20983 20 20 20 70 42 65 73 74 2d 3e 6e 41 72 67 20 3d pBest->nArg =
20984 20 6e 41 72 67 3b 0a 20 20 20 20 70 42 65 73 74 nArg;. pBest
20985 2d 3e 69 50 72 65 66 45 6e 63 20 3d 20 65 6e 63 ->iPrefEnc = enc
20986 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 65 ;. memcpy(pBe
20987 73 74 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 st->zName, zName
20988 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 70 42 , nName);. pB
20989 65 73 74 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 est->zName[nName
2098a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 ] = 0;. sqlit
2098b 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 e3FuncDefInsert(
2098c 26 64 62 2d 3e 61 46 75 6e 63 2c 20 70 42 65 73 &db->aFunc, pBes
2098d 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 t);. }.. if( p
2098e 42 65 73 74 20 26 26 20 28 70 42 65 73 74 2d 3e Best && (pBest->
2098f 78 53 74 65 70 20 7c 7c 20 70 42 65 73 74 2d 3e xStep || pBest->
20990 78 46 75 6e 63 20 7c 7c 20 63 72 65 61 74 65 46 xFunc || createF
20991 6c 61 67 29 20 29 7b 0a 20 20 20 20 72 65 74 75 lag) ){. retu
20992 72 6e 20 70 42 65 73 74 3b 0a 20 20 7d 0a 20 20 rn pBest;. }.
20993 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
20994 2a 2a 20 46 72 65 65 20 61 6c 6c 20 72 65 73 6f ** Free all reso
20995 75 72 63 65 73 20 68 65 6c 64 20 62 79 20 74 68 urces held by th
20996 65 20 73 63 68 65 6d 61 20 73 74 72 75 63 74 75 e schema structu
20997 72 65 2e 20 54 68 65 20 76 6f 69 64 2a 20 61 72 re. The void* ar
20998 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 0a 2a 2a gument points.**
20999 20 61 74 20 61 20 53 63 68 65 6d 61 20 73 74 72 at a Schema str
2099a 75 63 74 2e 20 54 68 69 73 20 66 75 6e 63 74 69 uct. This functi
2099b 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 63 61 6c 6c on does not call
2099c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
2099d 62 2c 20 29 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 b, ) on the .**
2099e 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 2c 20 pointer itself,
2099f 69 74 20 6a 75 73 74 20 63 6c 65 61 6e 73 20 75 it just cleans u
209a0 70 20 73 75 62 73 69 64 75 61 72 79 20 72 65 73 p subsiduary res
209a1 6f 75 72 63 65 73 20 28 69 2e 65 2e 20 74 68 65 ources (i.e. the
209a2 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 contents.** of
209a3 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 68 20 the schema hash
209a4 74 61 62 6c 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 54 tables)..**.** T
209a5 68 65 20 53 63 68 65 6d 61 2e 63 61 63 68 65 5f he Schema.cache_
209a6 73 69 7a 65 20 76 61 72 69 61 62 6c 65 20 69 73 size variable is
209a7 20 6e 6f 74 20 63 6c 65 61 72 65 64 2e 0a 2a 2f not cleared..*/
209a8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
209a9 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65 void sqlite3Sche
209aa 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b maFree(void *p){
209ab 0a 20 20 48 61 73 68 20 74 65 6d 70 31 3b 0a 20 . Hash temp1;.
209ac 20 48 61 73 68 20 74 65 6d 70 32 3b 0a 20 20 48 Hash temp2;. H
209ad 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a ashElem *pElem;.
209ae 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d Schema *pSchem
209af 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 3b a = (Schema *)p;
209b0 0a 0a 20 20 74 65 6d 70 31 20 3d 20 70 53 63 68 .. temp1 = pSch
209b1 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 ema->tblHash;.
209b2 74 65 6d 70 32 20 3d 20 70 53 63 68 65 6d 61 2d temp2 = pSchema-
209b3 3e 74 72 69 67 48 61 73 68 3b 0a 20 20 73 71 6c >trigHash;. sql
209b4 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53 ite3HashInit(&pS
209b5 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 2c chema->trigHash,
209b6 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 0);. sqlite3Ha
209b7 73 68 43 6c 65 61 72 28 26 70 53 63 68 65 6d 61 shClear(&pSchema
209b8 2d 3e 61 46 4b 65 79 29 3b 0a 20 20 73 71 6c 69 ->aFKey);. sqli
209b9 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 70 53 te3HashClear(&pS
209ba 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b chema->idxHash);
209bb 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c . for(pElem=sql
209bc 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65 iteHashFirst(&te
209bd 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c mp2); pElem; pEl
209be 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 em=sqliteHashNex
209bf 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 73 t(pElem)){. s
209c0 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 qlite3DeleteTrig
209c1 67 65 72 28 30 2c 20 28 54 72 69 67 67 65 72 2a ger(0, (Trigger*
209c2 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 )sqliteHashData(
209c3 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 pElem));. }. s
209c4 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 qlite3HashClear(
209c5 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 6c 69 74 &temp2);. sqlit
209c6 65 33 48 61 73 68 49 6e 69 74 28 26 70 53 63 68 e3HashInit(&pSch
209c7 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 30 29 ema->tblHash, 0)
209c8 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 ;. for(pElem=sq
209c9 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74 liteHashFirst(&t
209ca 65 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70 45 emp1); pElem; pE
209cb 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 lem=sqliteHashNe
209cc 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 xt(pElem)){.
209cd 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 Table *pTab = sq
209ce 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c liteHashData(pEl
209cf 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 em);. sqlite3
209d0 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54 61 62 DeleteTable(pTab
209d1 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
209d2 48 61 73 68 43 6c 65 61 72 28 26 74 65 6d 70 31 HashClear(&temp1
209d3 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e 70 53 );. pSchema->pS
209d4 65 71 54 61 62 20 3d 20 30 3b 0a 20 20 70 53 63 eqTab = 0;. pSc
209d5 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e hema->flags &= ~
209d6 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 3b DB_SchemaLoaded;
209d7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 .}../*.** Find a
209d8 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 63 nd return the sc
209d9 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20 hema associated
209da 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 43 with a BTree. C
209db 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6f reate.** a new o
209dc 6e 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e ne if necessary.
209dd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
209de 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 TE Schema *sqlit
209df 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69 e3SchemaGet(sqli
209e0 74 65 33 20 2a 64 62 2c 20 42 74 72 65 65 20 2a te3 *db, Btree *
209e1 70 42 74 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a pBt){. Schema *
209e2 20 70 3b 0a 20 20 69 66 28 20 70 42 74 20 29 7b p;. if( pBt ){
209e3 0a 20 20 20 20 70 20 3d 20 28 53 63 68 65 6d 61 . p = (Schema
209e4 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 53 *)sqlite3BtreeS
209e5 63 68 65 6d 61 28 70 42 74 2c 20 73 69 7a 65 6f chema(pBt, sizeo
209e6 66 28 53 63 68 65 6d 61 29 2c 20 73 71 6c 69 74 f(Schema), sqlit
209e7 65 33 53 63 68 65 6d 61 46 72 65 65 29 3b 0a 20 e3SchemaFree);.
209e8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 }else{. p =
209e9 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69 74 65 (Schema *)sqlite
209ea 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 3MallocZero(size
209eb 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a 20 20 7d of(Schema));. }
209ec 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 . if( !p ){.
209ed 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
209ee 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 d = 1;. }else i
209ef 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c 65 5f 66 f ( 0==p->file_f
209f0 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 73 71 6c ormat ){. sql
209f1 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d ite3HashInit(&p-
209f2 3e 74 62 6c 48 61 73 68 2c 20 30 29 3b 0a 20 20 >tblHash, 0);.
209f3 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 sqlite3HashIni
209f4 74 28 26 70 2d 3e 69 64 78 48 61 73 68 2c 20 30 t(&p->idxHash, 0
209f5 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 );. sqlite3Ha
209f6 73 68 49 6e 69 74 28 26 70 2d 3e 74 72 69 67 48 shInit(&p->trigH
209f7 61 73 68 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c ash, 0);. sql
209f8 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d ite3HashInit(&p-
209f9 3e 61 46 4b 65 79 2c 20 31 29 3b 0a 20 20 20 20 >aFKey, 1);.
209fa 70 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f p->enc = SQLITE_
209fb 55 54 46 38 3b 0a 20 20 7d 0a 20 20 72 65 74 75 UTF8;. }. retu
209fc 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a rn p;.}../******
209fd 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
209fe 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a callback.c *****
209ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a01 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
20a02 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
20a03 69 6c 65 20 64 65 6c 65 74 65 2e 63 20 2a 2a 2a ile delete.c ***
20a04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a06 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
20a07 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 001 September 15
20a08 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
20a09 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
20a0a 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
20a0b 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
20a0c 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
20a0d 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
20a0e 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
20a0f 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
20a10 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
20a11 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
20a12 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
20a13 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
20a14 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
20a15 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
20a16 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
20a17 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
20a18 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
20a19 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
20a1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
20a1e 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
20a1f 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 tains C code rou
20a20 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 tines that are c
20a21 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 alled by the par
20a22 73 65 72 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 ser.** in order
20a23 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 to generate code
20a24 20 66 6f 72 20 44 45 4c 45 54 45 20 46 52 4f 4d for DELETE FROM
20a25 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a statements..**.
20a26 2a 2a 20 24 49 64 3a 20 64 65 6c 65 74 65 2e 63 ** $Id: delete.c
20a27 2c 76 20 31 2e 31 38 37 20 32 30 30 38 2f 31 31 ,v 1.187 2008/11
20a28 2f 31 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e /19 09:05:27 dan
20a29 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a ielk1977 Exp $.*
20a2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 20 75 70 /../*.** Look up
20a2b 20 65 76 65 72 79 20 74 61 62 6c 65 20 74 68 61 every table tha
20a2c 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 70 53 t is named in pS
20a2d 72 63 2e 20 20 49 66 20 61 6e 79 20 74 61 62 6c rc. If any tabl
20a2e 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 0a e is not found,.
20a2f 2a 2a 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 ** add an error
20a30 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 message to pPars
20a31 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 72 e->zErrMsg and r
20a32 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 20 49 66 20 eturn NULL. If
20a33 61 6c 6c 20 74 61 62 6c 65 73 0a 2a 2a 20 61 72 all tables.** ar
20a34 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 e found, return
20a35 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
20a36 20 6c 61 73 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a last table..*/.
20a37 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 SQLITE_PRIVATE T
20a38 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 able *sqlite3Src
20a39 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 ListLookup(Parse
20a3a 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 *pParse, SrcLis
20a3b 74 20 2a 70 53 72 63 29 7b 0a 20 20 73 74 72 75 t *pSrc){. stru
20a3c 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
20a3d 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e 61 *pItem = pSrc->a
20a3e 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b ;. Table *pTab;
20a3f 0a 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d . assert( pItem
20a40 20 26 26 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d && pSrc->nSrc==
20a41 31 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 1 );. pTab = sq
20a42 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 lite3LocateTable
20a43 28 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65 (pParse, 0, pIte
20a44 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d m->zName, pItem-
20a45 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 73 >zDatabase);. s
20a46 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c qlite3DeleteTabl
20a47 65 28 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a e(pItem->pTab);.
20a48 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 pItem->pTab =
20a49 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61 62 pTab;. if( pTab
20a4a 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 6e 52 ){. pTab->nR
20a4b 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ef++;. }. if(
20a4c 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 sqlite3IndexedBy
20a4d 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 Lookup(pParse, p
20a4e 49 74 65 6d 29 20 29 7b 0a 20 20 20 20 70 54 61 Item) ){. pTa
20a4f 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 b = 0;. }. ret
20a50 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a urn pTab;.}../*.
20a51 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 ** Check to make
20a52 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 sure the given
20a53 74 61 62 6c 65 20 69 73 20 77 72 69 74 61 62 6c table is writabl
20a54 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 e. If it is not
20a55 0a 2a 2a 20 77 72 69 74 61 62 6c 65 2c 20 67 65 .** writable, ge
20a56 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 nerate an error
20a57 6d 65 73 73 61 67 65 20 61 6e 64 20 72 65 74 75 message and retu
20a58 72 6e 20 31 2e 20 20 49 66 20 69 74 20 69 73 0a rn 1. If it is.
20a59 2a 2a 20 77 72 69 74 61 62 6c 65 20 72 65 74 75 ** writable retu
20a5a 72 6e 20 30 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f rn 0;.*/.SQLITE_
20a5b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
20a5c 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 61 te3IsReadOnly(Pa
20a5d 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 rse *pParse, Tab
20a5e 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 76 69 le *pTab, int vi
20a5f 65 77 4f 6b 29 7b 0a 20 20 69 66 28 20 28 28 70 ewOk){. if( ((p
20a60 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 Tab->tabFlags &
20a61 54 46 5f 52 65 61 64 6f 6e 6c 79 29 21 3d 30 0a TF_Readonly)!=0.
20a62 20 20 20 20 20 20 20 20 26 26 20 28 70 50 61 72 && (pPar
20a63 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 se->db->flags &
20a64 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 SQLITE_WriteSche
20a65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 ma)==0. &
20a66 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 & pParse->nested
20a67 3d 3d 30 29 20 0a 23 69 66 6e 64 65 66 20 53 51 ==0) .#ifndef SQ
20a68 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
20a69 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 7c 7c 20 LTABLE. ||
20a6a 28 70 54 61 62 2d 3e 70 4d 6f 64 20 26 26 20 70 (pTab->pMod && p
20a6b 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 Tab->pMod->pModu
20a6c 6c 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 29 0a le->xUpdate==0).
20a6d 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20 #endif. ){.
20a6e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
20a6f 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 pParse, "table %
20a70 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 s may not be mod
20a71 69 66 69 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e ified", pTab->zN
20a72 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ame);. return
20a73 20 31 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 1;. }.#ifndef
20a74 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
20a75 0a 20 20 69 66 28 20 21 76 69 65 77 4f 6b 20 26 . if( !viewOk &
20a76 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 & pTab->pSelect
20a77 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
20a78 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 63 rorMsg(pParse,"c
20a79 61 6e 6e 6f 74 20 6d 6f 64 69 66 79 20 25 73 20 annot modify %s
20a7a 62 65 63 61 75 73 65 20 69 74 20 69 73 20 61 20 because it is a
20a7b 76 69 65 77 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d view",pTab->zNam
20a7c 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 e);. return 1
20a7d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 ;. }.#endif. r
20a7e 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
20a7f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
20a80 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20 61 that will open a
20a81 20 74 61 62 6c 65 20 66 6f 72 20 72 65 61 64 69 table for readi
20a82 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ng..*/.SQLITE_PR
20a83 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
20a84 65 33 4f 70 65 6e 54 61 62 6c 65 28 0a 20 20 50 e3OpenTable(. P
20a85 61 72 73 65 20 2a 70 2c 20 20 20 20 20 20 20 2f arse *p, /
20a86 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
20a87 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a into this VDBE *
20a88 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 /. int iCur,
20a89 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f /* The curso
20a8a 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 r number of the
20a8b 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 table */. int i
20a8c 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 Db, /* Th
20a8d 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 e database index
20a8e 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b in sqlite3.aDb[
20a8f 5d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 ] */. Table *pT
20a90 61 62 2c 20 20 20 20 2f 2a 20 54 68 65 20 74 61 ab, /* The ta
20a91 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 ble to be opened
20a92 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65 */. int opcode
20a93 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e /* OP_Open
20a94 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 Read or OP_OpenW
20a95 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 rite */.){. Vdb
20a96 65 20 2a 76 3b 0a 20 20 69 66 28 20 49 73 56 69 e *v;. if( IsVi
20a97 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 rtual(pTab) ) re
20a98 74 75 72 6e 3b 0a 20 20 76 20 3d 20 73 71 6c 69 turn;. v = sqli
20a99 74 65 33 47 65 74 56 64 62 65 28 70 29 3b 0a 20 te3GetVdbe(p);.
20a9a 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65 3d assert( opcode=
20a9b 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 7c 7c =OP_OpenWrite ||
20a9c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e opcode==OP_Open
20a9d 52 65 61 64 20 29 3b 0a 20 20 73 71 6c 69 74 65 Read );. sqlite
20a9e 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44 3TableLock(p, iD
20a9f 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 28 b, pTab->tnum, (
20aa0 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 opcode==OP_OpenW
20aa1 72 69 74 65 29 2c 20 70 54 61 62 2d 3e 7a 4e 61 rite), pTab->zNa
20aa2 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 me);. sqlite3Vd
20aa3 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 beAddOp2(v, OP_S
20aa4 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c etNumColumns, 0,
20aa5 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 pTab->nCol);.
20aa6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20aa7 33 28 76 2c 20 6f 70 63 6f 64 65 2c 20 69 43 75 3(v, opcode, iCu
20aa8 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 r, pTab->tnum, i
20aa9 44 62 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 Db);. VdbeComme
20aaa 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 54 61 nt((v, "%s", pTa
20aab 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 7d 0a 0a 0a b->zName));.}...
20aac 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
20aad 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 ITE_OMIT_VIEW) &
20aae 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
20aaf 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a E_OMIT_TRIGGER).
20ab0 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 65 20 61 /*.** Evaluate a
20ab1 20 76 69 65 77 20 61 6e 64 20 73 74 6f 72 65 20 view and store
20ab2 69 74 73 20 72 65 73 75 6c 74 20 69 6e 20 61 6e its result in an
20ab3 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 ephemeral table
20ab4 2e 20 20 54 68 65 0a 2a 2a 20 70 57 68 65 72 65 . The.** pWhere
20ab5 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 argument is an
20ab6 6f 70 74 69 6f 6e 61 6c 20 57 48 45 52 45 20 63 optional WHERE c
20ab7 6c 61 75 73 65 20 74 68 61 74 20 72 65 73 74 72 lause that restr
20ab8 69 63 74 73 20 74 68 65 0a 2a 2a 20 73 65 74 20 icts the.** set
20ab9 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 76 of rows in the v
20aba 69 65 77 20 74 68 61 74 20 61 72 65 20 74 6f 20 iew that are to
20abb 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 be added to the
20abc 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e ephemeral table.
20abd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20abe 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
20abf 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 0a aterializeView(.
20ac0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
20ac1 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
20ac2 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 g context */. T
20ac3 61 62 6c 65 20 2a 70 56 69 65 77 2c 20 20 20 20 able *pView,
20ac4 20 20 20 20 2f 2a 20 56 69 65 77 20 64 65 66 69 /* View defi
20ac5 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 nition */. Expr
20ac6 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 *pWhere,
20ac7 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 57 48 45 /* Optional WHE
20ac8 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 RE clause to be
20ac9 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 added */. int i
20aca 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20 20 Cur
20acb 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 /* Cursor number
20acc 20 66 6f 72 20 65 70 68 65 6d 65 72 69 61 6c 20 for ephemerial
20acd 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 53 65 table */.){. Se
20ace 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a 20 lectDest dest;.
20acf 20 53 65 6c 65 63 74 20 2a 70 44 75 70 3b 0a 20 Select *pDup;.
20ad0 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
20ad1 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 70 44 Parse->db;.. pD
20ad2 75 70 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 up = sqlite3Sele
20ad3 63 74 44 75 70 28 64 62 2c 20 70 56 69 65 77 2d ctDup(db, pView-
20ad4 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28 >pSelect);. if(
20ad5 20 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 53 pWhere ){. S
20ad6 72 63 4c 69 73 74 20 2a 70 46 72 6f 6d 3b 0a 20 rcList *pFrom;.
20ad7 20 20 20 54 6f 6b 65 6e 20 76 69 65 77 4e 61 6d Token viewNam
20ad8 65 3b 0a 20 20 20 20 0a 20 20 20 20 70 57 68 65 e;. . pWhe
20ad9 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 re = sqlite3Expr
20ada 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 29 3b Dup(db, pWhere);
20adb 0a 20 20 20 20 76 69 65 77 4e 61 6d 65 2e 7a 20 . viewName.z
20adc 3d 20 28 75 38 2a 29 70 56 69 65 77 2d 3e 7a 4e = (u8*)pView->zN
20add 61 6d 65 3b 0a 20 20 20 20 76 69 65 77 4e 61 6d ame;. viewNam
20ade 65 2e 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 e.n = (unsigned
20adf 69 6e 74 29 73 74 72 6c 65 6e 28 28 63 6f 6e 73 int)strlen((cons
20ae0 74 20 63 68 61 72 2a 29 76 69 65 77 4e 61 6d 65 t char*)viewName
20ae1 2e 7a 29 3b 0a 20 20 20 20 70 46 72 6f 6d 20 3d .z);. pFrom =
20ae2 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 sqlite3SrcListA
20ae3 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 ppendFromTerm(pP
20ae4 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 26 arse, 0, 0, 0, &
20ae5 76 69 65 77 4e 61 6d 65 2c 20 70 44 75 70 2c 20 viewName, pDup,
20ae6 30 2c 30 29 3b 0a 20 20 20 20 70 44 75 70 20 3d 0,0);. pDup =
20ae7 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 sqlite3SelectNe
20ae8 77 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 72 w(pParse, 0, pFr
20ae9 6f 6d 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30 om, pWhere, 0, 0
20aea 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 , 0, 0, 0, 0);.
20aeb 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 }. sqlite3Sele
20aec 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 ctDestInit(&dest
20aed 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 62 2c 20 , SRT_EphemTab,
20aee 69 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 iCur);. sqlite3
20aef 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 Select(pParse, p
20af0 44 75 70 2c 20 26 64 65 73 74 29 3b 0a 20 20 73 Dup, &dest);. s
20af1 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
20af2 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a te(db, pDup);.}.
20af3 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
20af4 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
20af5 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 IEW) && !defined
20af6 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 (SQLITE_OMIT_TRI
20af7 47 47 45 52 29 20 2a 2f 0a 0a 23 69 66 20 64 65 GGER) */..#if de
20af8 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 fined(SQLITE_ENA
20af9 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 BLE_UPDATE_DELET
20afa 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 E_LIMIT) && !def
20afb 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
20afc 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a _SUBQUERY)./*.**
20afd 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65 78 70 Generate an exp
20afe 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74 6f 20 ression tree to
20aff 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 57 48 implement the WH
20b00 45 52 45 2c 20 4f 52 44 45 52 20 42 59 2c 0a 2a ERE, ORDER BY,.*
20b01 2a 20 61 6e 64 20 4c 49 4d 49 54 2f 4f 46 46 53 * and LIMIT/OFFS
20b02 45 54 20 70 6f 72 74 69 6f 6e 20 6f 66 20 44 45 ET portion of DE
20b03 4c 45 54 45 20 61 6e 64 20 55 50 44 41 54 45 20 LETE and UPDATE
20b04 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a statements..**.*
20b05 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f * DELETE FRO
20b06 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57 48 45 M table_wxyz WHE
20b07 52 45 20 61 3c 35 20 4f 52 44 45 52 20 42 59 20 RE a<5 ORDER BY
20b08 61 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a 20 20 20 a LIMIT 1;.**
20b09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20b0a 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f \______
20b0b 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f ________________
20b0c 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 ____/.**
20b0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20b0e 20 20 20 20 20 20 20 70 4c 69 6d 69 74 57 68 65 pLimitWhe
20b0f 72 65 20 28 70 49 6e 43 6c 61 75 73 65 29 0a 2a re (pInClause).*
20b10 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20b11 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 4c 69 Expr *sqlite3Li
20b12 6d 69 74 57 68 65 72 65 28 0a 20 20 50 61 72 73 mitWhere(. Pars
20b13 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
20b14 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
20b15 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a parser context *
20b16 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 /. SrcList *pSr
20b17 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
20b18 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 /* the FROM cla
20b19 75 73 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62 use -- which tab
20b1a 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 les to scan */.
20b1b 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 Expr *pWhere,
20b1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
20b1d 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 The WHERE claus
20b1e 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 e. May be null
20b1f 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
20b20 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20 OrderBy,
20b21 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 /* The ORDER B
20b22 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 Y clause. May b
20b23 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 e null */. Expr
20b24 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20 20 20 *pLimit,
20b25 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
20b26 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 4d LIMIT clause. M
20b27 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 ay be null */.
20b28 45 78 70 72 20 2a 70 4f 66 66 73 65 74 2c 20 20 Expr *pOffset,
20b29 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20b2a 54 68 65 20 4f 46 46 53 45 54 20 63 6c 61 75 73 The OFFSET claus
20b2b 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 e. May be null
20b2c 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 */. char *zStmt
20b2d 54 79 70 65 20 20 20 20 20 20 20 20 20 20 20 20 Type
20b2e 20 20 2f 2a 20 45 69 74 68 65 72 20 44 45 4c 45 /* Either DELE
20b2f 54 45 20 6f 72 20 55 50 44 41 54 45 2e 20 20 46 TE or UPDATE. F
20b30 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 or error message
20b31 73 2e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 s. */.){. Expr
20b32 2a 70 57 68 65 72 65 52 6f 77 69 64 20 3d 20 4e *pWhereRowid = N
20b33 55 4c 4c 3b 20 20 20 20 2f 2a 20 57 48 45 52 45 ULL; /* WHERE
20b34 20 72 6f 77 69 64 20 2e 2e 20 2a 2f 0a 20 20 45 rowid .. */. E
20b35 78 70 72 20 2a 70 49 6e 43 6c 61 75 73 65 20 3d xpr *pInClause =
20b36 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20 57 NULL; /* W
20b37 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20 HERE rowid IN (
20b38 73 65 6c 65 63 74 20 29 20 2a 2f 0a 20 20 45 78 select ) */. Ex
20b39 70 72 20 2a 70 53 65 6c 65 63 74 52 6f 77 69 64 pr *pSelectRowid
20b3a 20 3d 20 4e 55 4c 4c 3b 20 20 20 2f 2a 20 53 45 = NULL; /* SE
20b3b 4c 45 43 54 20 72 6f 77 69 64 20 2e 2e 2e 20 2a LECT rowid ... *
20b3c 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
20b3d 4c 69 73 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 List = NULL;
20b3e 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6c /* Expression l
20b3f 69 73 74 20 63 6f 6e 74 61 6e 69 6e 67 20 6f 6e ist contaning on
20b40 6c 79 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 ly pSelectRowid
20b41 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 */. SrcList *pS
20b42 65 6c 65 63 74 53 72 63 20 3d 20 4e 55 4c 4c 3b electSrc = NULL;
20b43 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f 77 69 /* SELECT rowi
20b44 64 20 46 52 4f 4d 20 78 20 2e 2e 2e 20 28 64 75 d FROM x ... (du
20b45 70 20 6f 66 20 70 53 72 63 29 20 2a 2f 0a 20 20 p of pSrc) */.
20b46 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 Select *pSelect
20b47 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20 = NULL; /*
20b48 43 6f 6d 70 6c 65 74 65 20 53 45 4c 45 43 54 20 Complete SELECT
20b49 74 72 65 65 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 tree */.. /* Ch
20b4a 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 69 eck that there i
20b4b 73 6e 27 74 20 61 6e 20 4f 52 44 45 52 20 42 59 sn't an ORDER BY
20b4c 20 77 69 74 68 6f 75 74 20 61 20 4c 49 4d 49 54 without a LIMIT
20b4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 clause.. */.
20b4e 69 66 28 20 70 4f 72 64 65 72 42 79 20 26 26 20 if( pOrderBy &&
20b4f 28 70 4c 69 6d 69 74 20 3d 3d 20 30 29 20 29 20 (pLimit == 0) )
20b50 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
20b51 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 4f orMsg(pParse, "O
20b52 52 44 45 52 20 42 59 20 77 69 74 68 6f 75 74 20 RDER BY without
20b53 4c 49 4d 49 54 20 6f 6e 20 25 73 22 2c 20 7a 53 LIMIT on %s", zS
20b54 74 6d 74 54 79 70 65 29 3b 0a 20 20 20 20 70 50 tmtType);. pP
20b55 61 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72 arse->parseError
20b56 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6c = 1;. goto l
20b57 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e imit_where_clean
20b58 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 up_2;. }.. /*
20b59 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 We only need to
20b5a 67 65 6e 65 72 61 74 65 20 61 20 73 65 6c 65 63 generate a selec
20b5b 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 66 20 t expression if
20b5c 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 there. ** is a
20b5d 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20 74 65 72 limit/offset ter
20b5e 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e 0a 20 20 m to enforce..
20b5f 2a 2f 0a 20 20 69 66 28 20 70 4c 69 6d 69 74 20 */. if( pLimit
20b60 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 2f 2a 20 == 0 ) {. /*
20b61 69 66 20 70 4c 69 6d 69 74 20 69 73 20 6e 75 6c if pLimit is nul
20b62 6c 2c 20 70 4f 66 66 73 65 74 20 77 69 6c 6c 20 l, pOffset will
20b63 61 6c 77 61 79 73 20 62 65 20 6e 75 6c 6c 20 61 always be null a
20b64 73 20 77 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 61 s well. */. a
20b65 73 73 65 72 74 28 20 70 4f 66 66 73 65 74 20 3d ssert( pOffset =
20b66 3d 20 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 = 0 );. retur
20b67 6e 20 70 57 68 65 72 65 3b 0a 20 20 7d 0a 0a 20 n pWhere;. }..
20b68 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 /* Generate a s
20b69 65 6c 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e elect expression
20b6a 20 74 72 65 65 20 74 6f 20 65 6e 66 6f 72 63 65 tree to enforce
20b6b 20 74 68 65 20 6c 69 6d 69 74 2f 6f 66 66 73 65 the limit/offse
20b6c 74 20 0a 20 20 2a 2a 20 74 65 72 6d 20 66 6f 72 t . ** term for
20b6d 20 74 68 65 20 44 45 4c 45 54 45 20 6f 72 20 55 the DELETE or U
20b6e 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e PDATE statement.
20b6f 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20 For example:.
20b70 20 2a 2a 20 20 20 44 45 4c 45 54 45 20 46 52 4f ** DELETE FRO
20b71 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20 M table_a WHERE
20b72 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42 59 20 col1=1 ORDER BY
20b73 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f 46 46 col2 LIMIT 1 OFF
20b74 53 45 54 20 31 0a 20 20 2a 2a 20 62 65 63 6f 6d SET 1. ** becom
20b75 65 73 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54 es:. ** DELET
20b76 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 E FROM table_a W
20b77 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20 HERE rowid IN (
20b78 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 . ** SELECT
20b79 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 61 62 6c rowid FROM tabl
20b7a 65 5f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31 e_a WHERE col1=1
20b7b 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c ORDER BY col2 L
20b7c 49 4d 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a IMIT 1 OFFSET 1.
20b7d 20 20 2a 2a 20 20 20 29 3b 0a 20 20 2a 2f 0a 0a ** );. */..
20b7e 20 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d pSelectRowid =
20b7f 20 73 71 6c 69 74 65 33 45 78 70 72 28 70 50 61 sqlite3Expr(pPa
20b80 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 52 4f 57 2c rse->db, TK_ROW,
20b81 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 0, 0, 0);. if(
20b82 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d 3d pSelectRowid ==
20b83 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 0 ) goto limit_
20b84 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b where_cleanup_2;
20b85 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 . pEList = sqli
20b86 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e te3ExprListAppen
20b87 64 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 65 d(pParse, 0, pSe
20b88 6c 65 63 74 52 6f 77 69 64 2c 20 30 29 3b 0a 20 lectRowid, 0);.
20b89 20 69 66 28 20 70 45 4c 69 73 74 20 3d 3d 20 30 if( pEList == 0
20b8a 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 ) goto limit_wh
20b8b 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 0a ere_cleanup_2;..
20b8c 20 20 2f 2a 20 64 75 70 6c 69 63 61 74 65 20 74 /* duplicate t
20b8d 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 he FROM clause a
20b8e 73 20 69 74 20 69 73 20 6e 65 65 64 65 64 20 62 s it is needed b
20b8f 79 20 62 6f 74 68 20 74 68 65 20 44 45 4c 45 54 y both the DELET
20b90 45 2f 55 50 44 41 54 45 20 74 72 65 65 0a 20 20 E/UPDATE tree.
20b91 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c 45 43 ** and the SELEC
20b92 54 20 73 75 62 74 72 65 65 2e 20 2a 2f 0a 20 20 T subtree. */.
20b93 70 53 65 6c 65 63 74 53 72 63 20 3d 20 73 71 6c pSelectSrc = sql
20b94 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 70 ite3SrcListDup(p
20b95 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 29 Parse->db, pSrc)
20b96 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 53 ;. if( pSelectS
20b97 72 63 20 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 rc == 0 ) {.
20b98 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
20b99 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 elete(pParse->db
20b9a 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 67 , pEList);. g
20b9b 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f oto limit_where_
20b9c 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a cleanup_2;. }..
20b9d 20 20 2f 2a 20 67 65 6e 65 72 61 74 65 20 74 68 /* generate th
20b9e 65 20 53 45 4c 45 43 54 20 65 78 70 72 65 73 73 e SELECT express
20b9f 69 6f 6e 20 74 72 65 65 2e 20 2a 2f 0a 20 20 70 ion tree. */. p
20ba0 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 Select = sqlite3
20ba1 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 SelectNew(pParse
20ba2 2c 70 45 4c 69 73 74 2c 70 53 65 6c 65 63 74 53 ,pEList,pSelectS
20ba3 72 63 2c 70 57 68 65 72 65 2c 30 2c 30 2c 70 4f rc,pWhere,0,0,pO
20ba4 72 64 65 72 42 79 2c 30 2c 70 4c 69 6d 69 74 2c rderBy,0,pLimit,
20ba5 70 4f 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20 pOffset);. if(
20ba6 70 53 65 6c 65 63 74 20 3d 3d 20 30 20 29 20 72 pSelect == 0 ) r
20ba7 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 6e eturn 0;.. /* n
20ba8 6f 77 20 67 65 6e 65 72 61 74 65 20 74 68 65 20 ow generate the
20ba9 6e 65 77 20 57 48 45 52 45 20 72 6f 77 69 64 20 new WHERE rowid
20baa 49 4e 20 63 6c 61 75 73 65 20 66 6f 72 20 74 68 IN clause for th
20bab 65 20 44 45 4c 45 54 45 2f 55 44 50 41 54 45 20 e DELETE/UDPATE
20bac 2a 2f 0a 20 20 70 57 68 65 72 65 52 6f 77 69 64 */. pWhereRowid
20bad 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 70 = sqlite3Expr(p
20bae 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 52 4f Parse->db, TK_RO
20baf 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69 W, 0, 0, 0);. i
20bb0 66 28 20 70 57 68 65 72 65 52 6f 77 69 64 20 3d f( pWhereRowid =
20bb1 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 = 0 ) goto limit
20bb2 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31 _where_cleanup_1
20bb3 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65 20 3d 20 ;. pInClause =
20bb4 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 sqlite3PExpr(pPa
20bb5 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 57 68 65 rse, TK_IN, pWhe
20bb6 72 65 52 6f 77 69 64 2c 20 30 2c 20 30 29 3b 0a reRowid, 0, 0);.
20bb7 20 20 69 66 28 20 70 49 6e 43 6c 61 75 73 65 20 if( pInClause
20bb8 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 == 0 ) goto limi
20bb9 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f t_where_cleanup_
20bba 31 3b 0a 0a 20 20 70 49 6e 43 6c 61 75 73 65 2d 1;.. pInClause-
20bbb 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 >pSelect = pSele
20bbc 63 74 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 ct;. sqlite3Exp
20bbd 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73 rSetHeight(pPars
20bbe 65 2c 20 70 49 6e 43 6c 61 75 73 65 29 3b 0a 20 e, pInClause);.
20bbf 20 72 65 74 75 72 6e 20 70 49 6e 43 6c 61 75 73 return pInClaus
20bc0 65 3b 0a 0a 20 20 2f 2a 20 73 6f 6d 65 74 68 69 e;.. /* somethi
20bc1 6e 67 20 77 65 6e 74 20 77 72 6f 6e 67 2e 20 63 ng went wrong. c
20bc2 6c 65 61 6e 20 75 70 20 61 6e 79 74 68 69 6e 67 lean up anything
20bc3 20 61 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 6c allocated. */.l
20bc4 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e imit_where_clean
20bc5 75 70 5f 31 3a 0a 20 20 73 71 6c 69 74 65 33 53 up_1:. sqlite3S
20bc6 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 electDelete(pPar
20bc7 73 65 2d 3e 64 62 2c 20 70 53 65 6c 65 63 74 29 se->db, pSelect)
20bc8 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 6c ;. return 0;..l
20bc9 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e imit_where_clean
20bca 75 70 5f 32 3a 0a 20 20 73 71 6c 69 74 65 33 45 up_2:. sqlite3E
20bcb 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 xprDelete(pParse
20bcc 2d 3e 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 ->db, pWhere);.
20bcd 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
20bce 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 Delete(pParse->d
20bcf 62 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 b, pOrderBy);.
20bd0 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
20bd1 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c e(pParse->db, pL
20bd2 69 6d 69 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 imit);. sqlite3
20bd3 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 ExprDelete(pPars
20bd4 65 2d 3e 64 62 2c 20 70 4f 66 66 73 65 74 29 3b e->db, pOffset);
20bd5 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 . return 0;.}.#
20bd6 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 endif /* defined
20bd7 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 (SQLITE_ENABLE_U
20bd8 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d PDATE_DELETE_LIM
20bd9 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 IT) && !defined(
20bda 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
20bdb 55 45 52 59 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 UERY) */../*.**
20bdc 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f Generate code fo
20bdd 72 20 61 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 r a DELETE FROM
20bde 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a statement..**.**
20bdf 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d DELETE FROM
20be0 20 74 61 62 6c 65 5f 77 78 79 7a 20 57 48 45 52 table_wxyz WHER
20be1 45 20 61 3c 35 20 41 4e 44 20 62 20 4e 4f 54 20 E a<5 AND b NOT
20be2 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 NULL;.**
20be3 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f \______
20be4 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f __/ \_____
20be5 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 ___________/.**
20be6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20be7 20 70 54 61 62 4c 69 73 74 20 20 20 20 20 20 20 pTabList
20be8 20 20 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f pWhere.*/
20be9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20bea 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 void sqlite3Dele
20beb 74 65 46 72 6f 6d 28 0a 20 20 50 61 72 73 65 20 teFrom(. Parse
20bec 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
20bed 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 /* The parser c
20bee 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c ontext */. SrcL
20bef 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 ist *pTabList,
20bf0 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
20bf1 66 72 6f 6d 20 77 68 69 63 68 20 77 65 20 73 68 from which we sh
20bf2 6f 75 6c 64 20 64 65 6c 65 74 65 20 74 68 69 6e ould delete thin
20bf3 67 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 gs */. Expr *pW
20bf4 68 65 72 65 20 20 20 20 20 20 20 20 20 20 20 2f here /
20bf5 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 * The WHERE clau
20bf6 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c se. May be null
20bf7 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 */.){. Vdbe *v
20bf8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
20bf9 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 64 /* The virtual d
20bfa 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a atabase engine *
20bfb 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b /. Table *pTab;
20bfc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
20bfd 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 e table from whi
20bfe 63 68 20 72 65 63 6f 72 64 73 20 77 69 6c 6c 20 ch records will
20bff 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 be deleted */.
20c00 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b const char *zDb;
20c01 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
20c02 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 f database holdi
20c03 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 ng pTab */. int
20c04 20 65 6e 64 2c 20 61 64 64 72 20 3d 20 30 3b 20 end, addr = 0;
20c05 20 20 20 20 2f 2a 20 41 20 63 6f 75 70 6c 65 20 /* A couple
20c06 61 64 64 72 65 73 73 65 73 20 6f 66 20 67 65 6e addresses of gen
20c07 65 72 61 74 65 64 20 63 6f 64 65 20 2a 2f 0a 20 erated code */.
20c08 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
20c09 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
20c0a 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 57 68 65 counter */. Whe
20c0b 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 reInfo *pWInfo;
20c0c 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 /* Informati
20c0d 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48 45 on about the WHE
20c0e 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 49 RE clause */. I
20c0f 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 ndex *pIdx;
20c10 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f /* For loo
20c11 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65 ping over indice
20c12 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a s of the table *
20c13 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 /. int iCur;
20c14 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 /* VD
20c15 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 BE Cursor number
20c16 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 73 for pTab */. s
20c17 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
20c18 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 /* Main da
20c19 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 tabase structure
20c1a 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 */. AuthContex
20c1b 74 20 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20 t sContext; /*
20c1c 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f Authorization co
20c1d 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f ntext */. int o
20c1e 6c 64 49 64 78 20 3d 20 2d 31 3b 20 20 20 20 20 ldIdx = -1;
20c1f 20 20 2f 2a 20 43 75 72 73 6f 72 20 66 6f 72 20 /* Cursor for
20c20 74 68 65 20 4f 4c 44 20 74 61 62 6c 65 20 6f 66 the OLD table of
20c21 20 41 46 54 45 52 20 74 72 69 67 67 65 72 73 20 AFTER triggers
20c22 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 */. NameContext
20c23 20 73 4e 43 3b 20 20 20 20 20 20 20 2f 2a 20 4e sNC; /* N
20c24 61 6d 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 72 ame context to r
20c25 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69 6f esolve expressio
20c26 6e 73 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 ns in */. int i
20c27 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Db;
20c28 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 /* Database nu
20c29 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 mber */. int me
20c2a 6d 43 6e 74 20 3d 20 2d 31 3b 20 20 20 20 20 20 mCnt = -1;
20c2b 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 /* Memory cell
20c2c 75 73 65 64 20 66 6f 72 20 63 68 61 6e 67 65 20 used for change
20c2d 63 6f 75 6e 74 69 6e 67 20 2a 2f 0a 20 20 69 6e counting */. in
20c2e 74 20 72 63 61 75 74 68 3b 20 20 20 20 20 20 20 t rcauth;
20c2f 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 /* Value re
20c30 74 75 72 6e 65 64 20 62 79 20 61 75 74 68 6f 72 turned by author
20c31 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b ization callback
20c32 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
20c33 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
20c34 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20 20 . int isView;
20c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20c36 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74 65 6d /* True if attem
20c37 70 74 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 pting to delete
20c38 66 72 6f 6d 20 61 20 76 69 65 77 20 2a 2f 0a 20 from a view */.
20c39 20 69 6e 74 20 74 72 69 67 67 65 72 73 5f 65 78 int triggers_ex
20c3a 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a ist = 0; /*
20c3b 20 54 72 75 65 20 69 66 20 61 6e 79 20 74 72 69 True if any tri
20c3c 67 67 65 72 73 20 65 78 69 73 74 20 2a 2f 0a 23 ggers exist */.#
20c3d 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 42 65 67 endif. int iBeg
20c3e 69 6e 41 66 74 65 72 54 72 69 67 67 65 72 3b 20 inAfterTrigger;
20c3f 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 /* Address
20c40 6f 66 20 61 66 74 65 72 20 74 72 69 67 67 65 72 of after trigger
20c41 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e program */. in
20c42 74 20 69 45 6e 64 41 66 74 65 72 54 72 69 67 67 t iEndAfterTrigg
20c43 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 78 er; /* Ex
20c44 69 74 20 6f 66 20 61 66 74 65 72 20 74 72 69 67 it of after trig
20c45 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 ger program */.
20c46 20 69 6e 74 20 69 42 65 67 69 6e 42 65 66 6f 72 int iBeginBefor
20c47 65 54 72 69 67 67 65 72 3b 20 20 20 20 20 2f 2a eTrigger; /*
20c48 20 41 64 64 72 65 73 73 20 6f 66 20 62 65 66 6f Address of befo
20c49 72 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 re trigger progr
20c4a 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 am */. int iEnd
20c4b 42 65 66 6f 72 65 54 72 69 67 67 65 72 3b 20 20 BeforeTrigger;
20c4c 20 20 20 20 20 2f 2a 20 45 78 69 74 20 6f 66 20 /* Exit of
20c4d 62 65 66 6f 72 65 20 74 72 69 67 67 65 72 20 70 before trigger p
20c4e 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 75 33 32 20 rogram */. u32
20c4f 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 3d 20 30 old_col_mask = 0
20c50 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b ; /* Mask
20c51 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e of OLD.* column
20c52 73 20 69 6e 20 75 73 65 20 2a 2f 0a 0a 20 20 73 s in use */.. s
20c53 43 6f 6e 74 65 78 74 2e 70 50 61 72 73 65 20 3d Context.pParse =
20c54 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 0;. db = pPars
20c55 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 e->db;. if( pPa
20c56 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d rse->nErr || db-
20c57 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
20c58 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 . goto delete
20c59 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 _from_cleanup;.
20c5a 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 }. assert( pTa
20c5b 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 bList->nSrc==1 )
20c5c 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 ;.. /* Locate t
20c5d 68 65 20 74 61 62 6c 65 20 77 68 69 63 68 20 77 he table which w
20c5e 65 20 77 61 6e 74 20 74 6f 20 64 65 6c 65 74 65 e want to delete
20c5f 2e 20 20 54 68 69 73 20 74 61 62 6c 65 20 68 61 . This table ha
20c60 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 75 74 s to be. ** put
20c61 20 69 6e 20 61 6e 20 53 72 63 4c 69 73 74 20 73 in an SrcList s
20c62 74 72 75 63 74 75 72 65 20 62 65 63 61 75 73 65 tructure because
20c63 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 73 75 62 some of the sub
20c64 72 6f 75 74 69 6e 65 73 20 77 65 0a 20 20 2a 2a routines we. **
20c65 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 69 6e 67 will be calling
20c66 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f are designed to
20c67 20 77 6f 72 6b 20 77 69 74 68 20 6d 75 6c 74 69 work with multi
20c68 70 6c 65 20 74 61 62 6c 65 73 20 61 6e 64 20 65 ple tables and e
20c69 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e 20 53 72 xpect. ** an Sr
20c6a 63 4c 69 73 74 2a 20 70 61 72 61 6d 65 74 65 72 cList* parameter
20c6b 20 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74 instead of just
20c6c 20 61 20 54 61 62 6c 65 2a 20 70 61 72 61 6d 65 a Table* parame
20c6d 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62 ter.. */. pTab
20c6e 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 = sqlite3SrcLis
20c6f 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 tLookup(pParse,
20c70 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28 pTabList);. if(
20c71 20 70 54 61 62 3d 3d 30 20 29 20 20 67 6f 74 6f pTab==0 ) goto
20c72 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 delete_from_cle
20c73 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 anup;.. /* Figu
20c74 72 65 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 re out if we hav
20c75 65 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 e any triggers a
20c76 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 nd if the table
20c77 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65 6c 65 74 being. ** delet
20c78 65 64 20 66 72 6f 6d 20 69 73 20 61 20 76 69 65 ed from is a vie
20c79 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 w. */.#ifndef S
20c7a 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
20c7b 45 52 0a 20 20 74 72 69 67 67 65 72 73 5f 65 78 ER. triggers_ex
20c7c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72 69 ist = sqlite3Tri
20c7d 67 67 65 72 73 45 78 69 73 74 28 70 54 61 62 2c ggersExist(pTab,
20c7e 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 29 3b 0a TK_DELETE, 0);.
20c7f 20 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d isView = pTab-
20c80 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c >pSelect!=0;.#el
20c81 73 65 0a 23 20 64 65 66 69 6e 65 20 74 72 69 67 se.# define trig
20c82 67 65 72 73 5f 65 78 69 73 74 20 30 0a 23 20 64 gers_exist 0.# d
20c83 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 efine isView 0.#
20c84 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
20c85 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 ITE_OMIT_VIEW.#
20c86 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20 64 undef isView.# d
20c87 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 efine isView 0.#
20c88 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c endif.. if( sql
20c89 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 ite3IsReadOnly(p
20c8a 50 61 72 73 65 2c 20 70 54 61 62 2c 20 74 72 69 Parse, pTab, tri
20c8b 67 67 65 72 73 5f 65 78 69 73 74 29 20 29 7b 0a ggers_exist) ){.
20c8c 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f goto delete_
20c8d 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 from_cleanup;.
20c8e 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 }. iDb = sqlite
20c8f 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 3SchemaToIndex(d
20c90 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 b, pTab->pSchema
20c91 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 );. assert( iDb
20c92 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 <db->nDb );. zD
20c93 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d b = db->aDb[iDb]
20c94 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 61 75 74 68 .zName;. rcauth
20c95 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 = sqlite3AuthCh
20c96 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 eck(pParse, SQLI
20c97 54 45 5f 44 45 4c 45 54 45 2c 20 70 54 61 62 2d TE_DELETE, pTab-
20c98 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 3b >zName, 0, zDb);
20c99 0a 20 20 61 73 73 65 72 74 28 20 72 63 61 75 74 . assert( rcaut
20c9a 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 h==SQLITE_OK ||
20c9b 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 rcauth==SQLITE_D
20c9c 45 4e 59 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53 ENY || rcauth==S
20c9d 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 3b 0a QLITE_IGNORE );.
20c9e 20 20 69 66 28 20 72 63 61 75 74 68 3d 3d 53 51 if( rcauth==SQ
20c9f 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 LITE_DENY ){.
20ca0 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f goto delete_fro
20ca1 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 m_cleanup;. }.
20ca2 20 61 73 73 65 72 74 28 21 69 73 56 69 65 77 20 assert(!isView
20ca3 7c 7c 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 || triggers_exis
20ca4 74 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 t);.. /* If pTa
20ca5 62 20 69 73 20 72 65 61 6c 6c 79 20 61 20 76 69 b is really a vi
20ca6 65 77 2c 20 6d 61 6b 65 20 73 75 72 65 20 69 74 ew, make sure it
20ca7 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69 61 has been initia
20ca8 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 lized.. */. if
20ca9 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 ( sqlite3ViewGet
20caa 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 ColumnNames(pPar
20cab 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 se, pTab) ){.
20cac 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f goto delete_fro
20cad 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a m_cleanup;. }..
20cae 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 /* Allocate a
20caf 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 73 cursor used to s
20cb0 74 6f 72 65 20 74 68 65 20 6f 6c 64 2e 2a 20 64 tore the old.* d
20cb1 61 74 61 20 66 6f 72 20 61 20 74 72 69 67 67 65 ata for a trigge
20cb2 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74 72 r.. */. if( tr
20cb3 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b 20 iggers_exist ){
20cb4 0a 20 20 20 20 6f 6c 64 49 64 78 20 3d 20 70 50 . oldIdx = pP
20cb5 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 arse->nTab++;.
20cb6 7d 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 20 }.. /* Assign
20cb7 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f cursor number to
20cb8 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 61 the table and a
20cb9 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e 0a ll its indices..
20cba 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
20cbb 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 TabList->nSrc==1
20cbc 20 29 3b 0a 20 20 69 43 75 72 20 3d 20 70 54 61 );. iCur = pTa
20cbd 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72 bList->a[0].iCur
20cbe 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 sor = pParse->nT
20cbf 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78 ab++;. for(pIdx
20cc0 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
20cc1 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
20cc2 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72 pNext){. pPar
20cc3 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a se->nTab++;. }.
20cc4 0a 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20 . /* Start the
20cc5 76 69 65 77 20 63 6f 6e 74 65 78 74 0a 20 20 2a view context. *
20cc6 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29 /. if( isView )
20cc7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 {. sqlite3Aut
20cc8 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61 hContextPush(pPa
20cc9 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 rse, &sContext,
20cca 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pTab->zName);.
20ccb 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 }.. /* Begin ge
20ccc 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20 nerating code..
20ccd 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 */. v = sqlite
20cce 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3GetVdbe(pParse)
20ccf 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a ;. if( v==0 ){.
20cd0 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f goto delete_
20cd1 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 from_cleanup;.
20cd2 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e }. if( pParse->
20cd3 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 nested==0 ) sqli
20cd4 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e te3VdbeCountChan
20cd5 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 ges(v);. sqlite
20cd6 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 3BeginWriteOpera
20cd7 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 74 72 69 tion(pParse, tri
20cd8 67 67 65 72 73 5f 65 78 69 73 74 2c 20 69 44 62 ggers_exist, iDb
20cd9 29 3b 0a 0a 20 20 69 66 28 20 74 72 69 67 67 65 );.. if( trigge
20cda 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 rs_exist ){.
20cdb 69 6e 74 20 6f 72 63 6f 6e 66 20 3d 20 28 28 70 int orconf = ((p
20cdc 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b Parse->trigStack
20cdd 29 3f 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 )?pParse->trigSt
20cde 61 63 6b 2d 3e 6f 72 63 6f 6e 66 3a 4f 45 5f 44 ack->orconf:OE_D
20cdf 65 66 61 75 6c 74 29 3b 0a 20 20 20 20 69 6e 74 efault);. int
20ce0 20 69 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 iGoto = sqlite3
20ce1 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 VdbeAddOp0(v, OP
20ce2 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 61 64 64 72 _Goto);. addr
20ce3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 = sqlite3VdbeMa
20ce4 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 keLabel(v);..
20ce5 20 69 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69 iBeginBeforeTri
20ce6 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 gger = sqlite3Vd
20ce7 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 beCurrentAddr(v)
20ce8 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 ;. (void)sqli
20ce9 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 te3CodeRowTrigge
20cea 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 45 4c r(pParse, TK_DEL
20ceb 45 54 45 2c 20 30 2c 20 54 52 49 47 47 45 52 5f ETE, 0, TRIGGER_
20cec 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 0a 20 20 BEFORE, pTab,.
20ced 20 20 20 20 20 20 2d 31 2c 20 6f 6c 64 49 64 78 -1, oldIdx
20cee 2c 20 6f 72 63 6f 6e 66 2c 20 61 64 64 72 2c 20 , orconf, addr,
20cef 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 2c 20 30 &old_col_mask, 0
20cf0 29 3b 0a 20 20 20 20 69 45 6e 64 42 65 66 6f 72 );. iEndBefor
20cf1 65 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 eTrigger = sqlit
20cf2 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 e3VdbeAddOp0(v,
20cf3 4f 50 5f 47 6f 74 6f 29 3b 0a 0a 20 20 20 20 69 OP_Goto);.. i
20cf4 42 65 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 BeginAfterTrigge
20cf5 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 r = sqlite3VdbeC
20cf6 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 urrentAddr(v);.
20cf7 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
20cf8 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 CodeRowTrigger(p
20cf9 50 61 72 73 65 2c 20 54 4b 5f 44 45 4c 45 54 45 Parse, TK_DELETE
20cfa 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54 , 0, TRIGGER_AFT
20cfb 45 52 2c 20 70 54 61 62 2c 20 2d 31 2c 0a 20 20 ER, pTab, -1,.
20cfc 20 20 20 20 20 20 6f 6c 64 49 64 78 2c 20 6f 72 oldIdx, or
20cfd 63 6f 6e 66 2c 20 61 64 64 72 2c 20 26 6f 6c 64 conf, addr, &old
20cfe 5f 63 6f 6c 5f 6d 61 73 6b 2c 20 30 29 3b 0a 20 _col_mask, 0);.
20cff 20 20 20 69 45 6e 64 41 66 74 65 72 54 72 69 67 iEndAfterTrig
20d00 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ger = sqlite3Vdb
20d01 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f eAddOp0(v, OP_Go
20d02 74 6f 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 to);.. sqlite
20d03 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
20d04 20 69 47 6f 74 6f 29 3b 0a 20 20 7d 0a 0a 20 20 iGoto);. }..
20d05 2f 2a 20 49 66 20 77 65 20 61 72 65 20 74 72 79 /* If we are try
20d06 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 ing to delete fr
20d07 6f 6d 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 om a view, reali
20d08 7a 65 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 ze that view int
20d09 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 o. ** a ephemer
20d0a 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 al table.. */.#
20d0b 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
20d0c 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 TE_OMIT_VIEW) &&
20d0d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
20d0e 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 _OMIT_TRIGGER).
20d0f 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 if( isView ){.
20d10 20 20 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 sqlite3Materi
20d11 61 6c 69 7a 65 56 69 65 77 28 70 50 61 72 73 65 alizeView(pParse
20d12 2c 20 70 54 61 62 2c 20 70 57 68 65 72 65 2c 20 , pTab, pWhere,
20d13 69 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 iCur);. }.#endi
20d14 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 f.. /* Resolve
20d15 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 the column names
20d16 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c in the WHERE cl
20d17 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d ause.. */. mem
20d18 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a set(&sNC, 0, siz
20d19 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 eof(sNC));. sNC
20d1a 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
20d1b 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 ;. sNC.pSrcList
20d1c 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 69 = pTabList;. i
20d1d 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 f( sqlite3Resolv
20d1e 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c eExprNames(&sNC,
20d1f 20 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 pWhere) ){.
20d20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d goto delete_from
20d21 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 _cleanup;. }..
20d22 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
20d23 68 65 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 he counter of th
20d24 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 e number of rows
20d25 20 64 65 6c 65 74 65 64 2c 20 69 66 0a 20 20 2a deleted, if. *
20d26 2a 20 77 65 20 61 72 65 20 63 6f 75 6e 74 69 6e * we are countin
20d27 67 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 69 g rows.. */. i
20d28 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 f( db->flags & S
20d29 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 QLITE_CountRows
20d2a 29 7b 0a 20 20 20 20 6d 65 6d 43 6e 74 20 3d 20 ){. memCnt =
20d2b 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
20d2c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
20d2d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
20d2e 67 65 72 2c 20 30 2c 20 6d 65 6d 43 6e 74 29 3b ger, 0, memCnt);
20d2f 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 . }..#ifndef SQ
20d30 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 LITE_OMIT_TRUNCA
20d31 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a TE_OPTIMIZATION.
20d32 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 /* Special cas
20d33 65 3a 20 41 20 44 45 4c 45 54 45 20 77 69 74 68 e: A DELETE with
20d34 6f 75 74 20 61 20 57 48 45 52 45 20 63 6c 61 75 out a WHERE clau
20d35 73 65 20 64 65 6c 65 74 65 73 20 65 76 65 72 79 se deletes every
20d36 74 68 69 6e 67 2e 0a 20 20 2a 2a 20 49 74 20 69 thing.. ** It i
20d37 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f s easier just to
20d38 20 65 72 61 73 65 20 74 68 65 20 77 68 6f 6c 65 erase the whole
20d39 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65 2c 20 68 table. Note, h
20d3a 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 20 20 2a owever, that. *
20d3b 2a 20 74 68 69 73 20 6d 65 61 6e 73 20 74 68 61 * this means tha
20d3c 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 t the row change
20d3d 20 63 6f 75 6e 74 20 77 69 6c 6c 20 62 65 20 69 count will be i
20d3e 6e 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2f 0a 20 ncorrect.. */.
20d3f 20 69 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c if( rcauth==SQL
20d40 49 54 45 5f 4f 4b 20 26 26 20 70 57 68 65 72 65 ITE_OK && pWhere
20d41 3d 3d 30 20 26 26 20 21 74 72 69 67 67 65 72 73 ==0 && !triggers
20d42 5f 65 78 69 73 74 20 26 26 20 21 49 73 56 69 72 _exist && !IsVir
20d43 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 tual(pTab) ){.
20d44 20 20 61 73 73 65 72 74 28 20 21 69 73 56 69 65 assert( !isVie
20d45 77 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 w );. sqlite3
20d46 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
20d47 5f 43 6c 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e _Clear, pTab->tn
20d48 75 6d 2c 20 69 44 62 2c 20 6d 65 6d 43 6e 74 29 um, iDb, memCnt)
20d49 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 72 73 ;. if( !pPars
20d4a 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 e->nested ){.
20d4b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
20d4c 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 angeP4(v, -1, pT
20d4d 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 ab->zName, P4_ST
20d4e 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ATIC);. }.
20d4f 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e for(pIdx=pTab->
20d50 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 pIndex; pIdx; pI
20d51 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b dx=pIdx->pNext){
20d52 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
20d53 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 Idx->pSchema==pT
20d54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 ab->pSchema );.
20d55 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
20d56 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 AddOp2(v, OP_Cle
20d57 61 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 ar, pIdx->tnum,
20d58 69 44 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 iDb);. }. }e
20d59 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 lse.#endif /* SQ
20d5a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 LITE_OMIT_TRUNCA
20d5b 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 TE_OPTIMIZATION
20d5c 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 75 73 75 61 */. /* The usua
20d5d 6c 20 63 61 73 65 3a 20 54 68 65 72 65 20 69 73 l case: There is
20d5e 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 a WHERE clause
20d5f 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73 63 so we have to sc
20d60 61 6e 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 an through. **
20d61 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 70 69 the table and pi
20d62 63 6b 20 77 68 69 63 68 20 72 65 63 6f 72 64 73 ck which records
20d63 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 2a 2f to delete.. */
20d64 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 52 6f . {. int iRo
20d65 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e wid = ++pParse->
20d66 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 55 73 65 64 nMem; /* Used
20d67 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f 77 for storing row
20d68 69 64 20 76 61 6c 75 65 73 2e 20 2a 2f 0a 0a 20 id values. */..
20d69 20 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 20 /* Begin the
20d6a 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20 database scan.
20d6b 20 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20 */. pWInfo
20d6c 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 = sqlite3WhereBe
20d6d 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 gin(pParse, pTab
20d6e 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 30 2c List, pWhere, 0,
20d6f 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 57 49 0);. if( pWI
20d70 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 64 65 nfo==0 ) goto de
20d71 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 lete_from_cleanu
20d72 70 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 65 6d p;.. /* Remem
20d73 62 65 72 20 74 68 65 20 72 6f 77 69 64 20 6f 66 ber the rowid of
20d74 20 65 76 65 72 79 20 69 74 65 6d 20 74 6f 20 62 every item to b
20d75 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a e deleted.. *
20d76 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 /. sqlite3Vdb
20d77 65 41 64 64 4f 70 32 28 76 2c 20 49 73 56 69 72 eAddOp2(v, IsVir
20d78 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f tual(pTab) ? OP_
20d79 56 52 6f 77 69 64 20 3a 20 4f 50 5f 52 6f 77 69 VRowid : OP_Rowi
20d7a 64 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64 29 d, iCur, iRowid)
20d7b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
20d7c 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 46 69 eAddOp1(v, OP_Fi
20d7d 66 6f 57 72 69 74 65 2c 20 69 52 6f 77 69 64 29 foWrite, iRowid)
20d7e 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c ;. if( db->fl
20d7f 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 ags & SQLITE_Cou
20d80 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 20 20 ntRows ){.
20d81 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20d82 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 2(v, OP_AddImm,
20d83 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20 20 memCnt, 1);.
20d84 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 74 68 }.. /* End th
20d85 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20 e database scan
20d86 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 loop.. */.
20d87 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 sqlite3WhereEnd
20d88 28 70 57 49 6e 66 6f 29 3b 0a 0a 20 20 20 20 2f (pWInfo);.. /
20d89 2a 20 4f 70 65 6e 20 74 68 65 20 70 73 65 75 64 * Open the pseud
20d8a 6f 2d 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 o-table used to
20d8b 73 74 6f 72 65 20 4f 4c 44 20 69 66 20 74 68 65 store OLD if the
20d8c 72 65 20 61 72 65 20 74 72 69 67 67 65 72 73 2e re are triggers.
20d8d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
20d8e 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 triggers_exist )
20d8f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
20d90 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
20d91 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 SetNumColumns, 0
20d92 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 , pTab->nCol);.
20d93 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
20d94 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4f 70 65 AddOp1(v, OP_Ope
20d95 6e 50 73 65 75 64 6f 2c 20 6f 6c 64 49 64 78 29 nPseudo, oldIdx)
20d96 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
20d97 44 65 6c 65 74 65 20 65 76 65 72 79 20 69 74 65 Delete every ite
20d98 6d 20 77 68 6f 73 65 20 6b 65 79 20 77 61 73 20 m whose key was
20d99 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6c written to the l
20d9a 69 73 74 20 64 75 72 69 6e 67 20 74 68 65 0a 20 ist during the.
20d9b 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 ** database s
20d9c 63 61 6e 2e 20 20 57 65 20 68 61 76 65 20 74 6f can. We have to
20d9d 20 64 65 6c 65 74 65 20 69 74 65 6d 73 20 61 66 delete items af
20d9e 74 65 72 20 74 68 65 20 73 63 61 6e 20 69 73 20 ter the scan is
20d9f 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 complete. **
20da0 62 65 63 61 75 73 65 20 64 65 6c 65 74 69 6e 67 because deleting
20da1 20 61 6e 20 69 74 65 6d 20 63 61 6e 20 63 68 61 an item can cha
20da2 6e 67 65 20 74 68 65 20 73 63 61 6e 20 6f 72 64 nge the scan ord
20da3 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 er.. */. e
20da4 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 nd = sqlite3Vdbe
20da5 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 MakeLabel(v);..
20da6 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 if( !isView )
20da7 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 {. /* Open
20da8 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65 20 cursors for the
20da9 74 61 62 6c 65 20 77 65 20 61 72 65 20 64 65 6c table we are del
20daa 65 74 69 6e 67 20 66 72 6f 6d 20 61 6e 64 20 0a eting from and .
20dab 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 69 74 73 ** all its
20dac 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20 20 20 indices..
20dad 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
20dae 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 OpenTableAndIndi
20daf 63 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 ces(pParse, pTab
20db0 2c 20 69 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 , iCur, OP_OpenW
20db1 72 69 74 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 rite);. }..
20db2 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 /* This is the
20db3 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
20db4 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70 2e 20 49 e delete loop. I
20db5 66 20 61 20 74 72 69 67 67 65 72 20 65 6e 63 6f f a trigger enco
20db6 75 6e 74 65 72 73 0a 20 20 20 20 2a 2a 20 61 6e unters. ** an
20db7 20 49 47 4e 4f 52 45 20 63 6f 6e 73 74 72 61 69 IGNORE constrai
20db8 6e 74 2c 20 69 74 20 6a 75 6d 70 73 20 62 61 63 nt, it jumps bac
20db9 6b 20 74 6f 20 68 65 72 65 2e 0a 20 20 20 20 2a k to here.. *
20dba 2f 0a 20 20 20 20 69 66 28 20 74 72 69 67 67 65 /. if( trigge
20dbb 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 rs_exist ){.
20dbc 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 sqlite3VdbeRes
20dbd 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 olveLabel(v, add
20dbe 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 r);. }. ad
20dbf 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
20dc0 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 69 66 AddOp2(v, OP_Fif
20dc1 6f 52 65 61 64 2c 20 69 52 6f 77 69 64 2c 20 65 oRead, iRowid, e
20dc2 6e 64 29 3b 0a 0a 20 20 20 20 69 66 28 20 74 72 nd);.. if( tr
20dc3 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a iggers_exist ){.
20dc4 20 20 20 20 20 20 69 6e 74 20 69 44 61 74 61 20 int iData
20dc5 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
20dc6 3b 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 ; /* For stori
20dc7 6e 67 20 72 6f 77 20 64 61 74 61 20 6f 66 20 4f ng row data of O
20dc8 4c 44 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 LD table */..
20dc9 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 63 /* If the rec
20dca 6f 72 64 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 ord is no longer
20dcb 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 present in the
20dcc 74 61 62 6c 65 2c 20 6a 75 6d 70 20 74 6f 20 74 table, jump to t
20dcd 68 65 0a 20 20 20 20 20 20 2a 2a 20 6e 65 78 74 he. ** next
20dce 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 iteration of th
20dcf 65 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 e loop through t
20dd0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
20dd1 68 65 20 66 69 66 6f 2e 0a 20 20 20 20 20 20 2a he fifo.. *
20dd2 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 /. sqlite3V
20dd3 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
20dd4 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c NotExists, iCur,
20dd5 20 61 64 64 72 2c 20 69 52 6f 77 69 64 29 3b 0a addr, iRowid);.
20dd6 0a 20 20 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 . /* Popula
20dd7 74 65 20 74 68 65 20 4f 4c 44 2e 2a 20 70 73 65 te the OLD.* pse
20dd8 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 20 udo-table */.
20dd9 20 20 20 69 66 28 20 6f 6c 64 5f 63 6f 6c 5f 6d if( old_col_m
20dda 61 73 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ask ){. s
20ddb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
20ddc 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20 (v, OP_RowData,
20ddd 69 43 75 72 2c 20 69 44 61 74 61 29 3b 0a 20 20 iCur, iData);.
20dde 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20ddf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
20de0 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c dOp2(v, OP_Null,
20de1 20 30 2c 20 69 44 61 74 61 29 3b 0a 20 20 20 20 0, iData);.
20de2 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
20de3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
20de4 50 5f 49 6e 73 65 72 74 2c 20 6f 6c 64 49 64 78 P_Insert, oldIdx
20de5 2c 20 69 44 61 74 61 2c 20 69 52 6f 77 69 64 29 , iData, iRowid)
20de6 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 ;.. /* Jump
20de7 20 62 61 63 6b 20 61 6e 64 20 72 75 6e 20 74 68 back and run th
20de8 65 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 e BEFORE trigger
20de9 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 s */. sqlit
20dea 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
20deb 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 65 67 OP_Goto, 0, iBeg
20dec 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 29 inBeforeTrigger)
20ded 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
20dee 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 dbeJumpHere(v, i
20def 45 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 EndBeforeTrigger
20df0 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 );. }.. if
20df1 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 ( !isView ){.
20df2 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 /* Delete the
20df3 20 72 6f 77 20 2a 2f 0a 23 69 66 6e 64 65 66 20 row */.#ifndef
20df4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
20df5 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 69 UALTABLE. i
20df6 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 f( IsVirtual(pTa
20df7 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f b) ){. co
20df8 6e 73 74 20 63 68 61 72 20 2a 70 56 74 61 62 20 nst char *pVtab
20df9 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 = (const char *)
20dfa 70 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 pTab->pVtab;.
20dfb 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 sqlite3Vtab
20dfc 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50 61 MakeWritable(pPa
20dfd 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 rse, pTab);.
20dfe 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
20dff 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 ddOp4(v, OP_VUpd
20e00 61 74 65 2c 20 30 2c 20 31 2c 20 69 52 6f 77 69 ate, 0, 1, iRowi
20e01 64 2c 20 70 56 74 61 62 2c 20 50 34 5f 56 54 41 d, pVtab, P4_VTA
20e02 42 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a B);. }else.
20e03 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 #endif. {.
20e04 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 sqlite3Ge
20e05 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 nerateRowDelete(
20e06 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 pParse, pTab, iC
20e07 75 72 2c 20 69 52 6f 77 69 64 2c 20 70 50 61 72 ur, iRowid, pPar
20e08 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29 3b 0a se->nested==0);.
20e09 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
20e0a 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 /* If there a
20e0b 72 65 20 72 6f 77 20 74 72 69 67 67 65 72 73 2c re row triggers,
20e0c 20 63 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f close all curso
20e0d 72 73 20 74 68 65 6e 20 69 6e 76 6f 6b 65 0a 20 rs then invoke.
20e0e 20 20 20 2a 2a 20 74 68 65 20 41 46 54 45 52 20 ** the AFTER
20e0f 74 72 69 67 67 65 72 73 0a 20 20 20 20 2a 2f 0a triggers. */.
20e10 20 20 20 20 69 66 28 20 74 72 69 67 67 65 72 73 if( triggers
20e11 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20 _exist ){.
20e12 2f 2a 20 4a 75 6d 70 20 62 61 63 6b 20 61 6e 64 /* Jump back and
20e13 20 72 75 6e 20 74 68 65 20 41 46 54 45 52 20 74 run the AFTER t
20e14 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 20 riggers */.
20e15 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20e16 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 p2(v, OP_Goto, 0
20e17 2c 20 69 42 65 67 69 6e 41 66 74 65 72 54 72 69 , iBeginAfterTri
20e18 67 67 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c gger);. sql
20e19 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
20e1a 28 76 2c 20 69 45 6e 64 41 66 74 65 72 54 72 69 (v, iEndAfterTri
20e1b 67 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 gger);. }..
20e1c 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 /* End of the
20e1d 64 65 6c 65 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 delete loop */.
20e1e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
20e1f 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c dOp2(v, OP_Goto,
20e20 20 30 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 73 0, addr);. s
20e21 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
20e22 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 29 3b 0a eLabel(v, end);.
20e23 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20 74 68 . /* Close th
20e24 65 20 63 75 72 73 6f 72 73 20 61 66 74 65 72 20 e cursors after
20e25 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 72 the loop if ther
20e26 65 20 61 72 65 20 6e 6f 20 72 6f 77 20 74 72 69 e are no row tri
20e27 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 69 66 28 ggers */. if(
20e28 20 21 69 73 56 69 65 77 20 20 26 26 20 21 49 73 !isView && !Is
20e29 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b Virtual(pTab) ){
20e2a 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 . for(i=1,
20e2b 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 pIdx=pTab->pInde
20e2c 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49 x; pIdx; i++, pI
20e2d 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b dx=pIdx->pNext){
20e2e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
20e2f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
20e30 5f 43 6c 6f 73 65 2c 20 69 43 75 72 20 2b 20 69 _Close, iCur + i
20e31 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 29 3b 0a 20 , pIdx->tnum);.
20e32 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
20e33 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
20e34 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 , OP_Close, iCur
20e35 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
20e36 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 /*. ** Return t
20e37 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
20e38 73 20 74 68 61 74 20 77 65 72 65 20 64 65 6c 65 s that were dele
20e39 74 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 ted. If this rou
20e3a 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 tine is . ** ge
20e3b 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 nerating code be
20e3c 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 cause of a call
20e3d 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 to sqlite3Nested
20e3e 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a Parse(), do not.
20e3f 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 ** invoke the
20e40 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
20e41 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 n.. */. if( db
20e42 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
20e43 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 70 50 _CountRows && pP
20e44 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 arse->nested==0
20e45 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 && !pParse->trig
20e46 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c Stack ){. sql
20e47 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
20e48 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 , OP_ResultRow,
20e49 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20 20 memCnt, 1);.
20e4a 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 sqlite3VdbeSetNu
20e4b 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 mCols(v, 1);.
20e4c 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 sqlite3VdbeSetC
20e4d 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c olName(v, 0, COL
20e4e 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 NAME_NAME, "rows
20e4f 20 64 65 6c 65 74 65 64 22 2c 20 53 51 4c 49 54 deleted", SQLIT
20e50 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a E_STATIC);. }..
20e51 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 delete_from_clea
20e52 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 41 75 nup:. sqlite3Au
20e53 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26 73 43 thContextPop(&sC
20e54 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 ontext);. sqlit
20e55 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 e3SrcListDelete(
20e56 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 db, pTabList);.
20e57 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
20e58 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a te(db, pWhere);.
20e59 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a return;.}../*.
20e5a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
20e5b 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 generates VDBE c
20e5c 6f 64 65 20 74 68 61 74 20 63 61 75 73 65 73 20 ode that causes
20e5d 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 a single row of
20e5e 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 61 62 6c a.** single tabl
20e5f 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e e to be deleted.
20e60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 .**.** The VDBE
20e61 6d 75 73 74 20 62 65 20 69 6e 20 61 20 70 61 72 must be in a par
20e62 74 69 63 75 6c 61 72 20 73 74 61 74 65 20 77 68 ticular state wh
20e63 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
20e64 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 is called..** Th
20e65 65 73 65 20 61 72 65 20 74 68 65 20 72 65 71 75 ese are the requ
20e66 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 irements:.**.**
20e67 20 20 31 2e 20 20 41 20 72 65 61 64 2f 77 72 69 1. A read/wri
20e68 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 te cursor pointi
20e69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74 68 65 20 ng to pTab, the
20e6a 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 table containing
20e6b 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 the row.**
20e6c 20 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2c to be deleted,
20e6d 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 must be opened
20e6e 61 73 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 as cursor number
20e6f 20 22 62 61 73 65 22 2e 0a 2a 2a 0a 2a 2a 20 20 "base"..**.**
20e70 20 32 2e 20 20 52 65 61 64 2f 77 72 69 74 65 20 2. Read/write
20e71 63 75 72 73 6f 72 73 20 66 6f 72 20 61 6c 6c 20 cursors for all
20e72 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62 20 indices of pTab
20e73 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61 73 0a must be open as.
20e74 2a 2a 20 20 20 20 20 20 20 63 75 72 73 6f 72 20 ** cursor
20e75 6e 75 6d 62 65 72 20 62 61 73 65 2b 69 20 66 6f number base+i fo
20e76 72 20 74 68 65 20 69 2d 74 68 20 69 6e 64 65 78 r the i-th index
20e77 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 54 68 ..**.** 3. Th
20e78 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 e record number
20e79 6f 66 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 of the row to be
20e7a 20 64 65 6c 65 74 65 64 20 6d 75 73 74 20 62 65 deleted must be
20e7b 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 20 20 stored in.**
20e7c 20 20 20 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 memory cell
20e7d 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 iRowid..**.** Th
20e7e 69 73 20 72 6f 75 74 69 6e 65 20 70 6f 70 73 20 is routine pops
20e7f 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 the top of the s
20e80 74 61 63 6b 20 74 6f 20 72 65 6d 6f 76 65 20 74 tack to remove t
20e81 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 he record number
20e82 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 67 65 6e .** and then gen
20e83 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20 72 erates code to r
20e84 65 6d 6f 76 65 20 62 6f 74 68 20 74 68 65 20 74 emove both the t
20e85 61 62 6c 65 20 72 65 63 6f 72 64 20 61 6e 64 20 able record and
20e86 61 6c 6c 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 all index.** ent
20e87 72 69 65 73 20 74 68 61 74 20 70 6f 69 6e 74 20 ries that point
20e88 74 6f 20 74 68 61 74 20 72 65 63 6f 72 64 2e 0a to that record..
20e89 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20e8a 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 E void sqlite3Ge
20e8b 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 nerateRowDelete(
20e8c 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
20e8d 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 , /* Parsing
20e8e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 context */. Ta
20e8f 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 ble *pTab,
20e90 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 /* Table contai
20e91 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74 6f 20 ning the row to
20e92 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 be deleted */.
20e93 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 int iCur,
20e94 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d /* Cursor num
20e95 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c ber for the tabl
20e96 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 77 69 e */. int iRowi
20e97 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d d, /* Mem
20e98 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20 63 6f ory cell that co
20e99 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 77 69 64 ntains the rowid
20e9a 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 to delete */.
20e9b 69 6e 74 20 63 6f 75 6e 74 20 20 20 20 20 20 20 int count
20e9c 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 /* Increment
20e9d 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 the row change c
20e9e 6f 75 6e 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 ounter */.){. i
20e9f 6e 74 20 61 64 64 72 3b 0a 20 20 56 64 62 65 20 nt addr;. Vdbe
20ea0 2a 76 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73 *v;.. v = pPars
20ea1 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 64 64 72 e->pVdbe;. addr
20ea2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
20ea3 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 dOp3(v, OP_NotEx
20ea4 69 73 74 73 2c 20 69 43 75 72 2c 20 30 2c 20 69 ists, iCur, 0, i
20ea5 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 Rowid);. sqlite
20ea6 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 3GenerateRowInde
20ea7 78 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 xDelete(pParse,
20ea8 70 54 61 62 2c 20 69 43 75 72 2c 20 30 29 3b 0a pTab, iCur, 0);.
20ea9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
20eaa 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 Op2(v, OP_Delete
20eab 2c 20 69 43 75 72 2c 20 28 63 6f 75 6e 74 3f 4f , iCur, (count?O
20eac 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 3a 30 29 PFLAG_NCHANGE:0)
20ead 29 3b 0a 20 20 69 66 28 20 63 6f 75 6e 74 20 29 );. if( count )
20eae 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
20eaf 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c eChangeP4(v, -1,
20eb0 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 pTab->zName, P4
20eb1 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20 _STATIC);. }.
20eb2 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
20eb3 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 7d 0a ere(v, addr);.}.
20eb4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
20eb5 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56 44 ine generates VD
20eb6 42 45 20 63 6f 64 65 20 74 68 61 74 20 63 61 75 BE code that cau
20eb7 73 65 73 20 74 68 65 20 64 65 6c 65 74 69 6f 6e ses the deletion
20eb8 20 6f 66 20 61 6c 6c 0a 2a 2a 20 69 6e 64 65 78 of all.** index
20eb9 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 entries associa
20eba 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c ted with a singl
20ebb 65 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c e row of a singl
20ebc 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 e table..**.** T
20ebd 68 65 20 56 44 42 45 20 6d 75 73 74 20 62 65 20 he VDBE must be
20ebe 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 in a particular
20ebf 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 state when this
20ec0 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
20ec1 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 d..** These are
20ec2 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 the requirements
20ec3 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20 :.**.** 1. A
20ec4 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f read/write curso
20ec5 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54 r pointing to pT
20ec6 61 62 2c 20 74 68 65 20 74 61 62 6c 65 20 63 6f ab, the table co
20ec7 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 ntaining the row
20ec8 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20 62 65 20 .** to be
20ec9 64 65 6c 65 74 65 64 2c 20 6d 75 73 74 20 62 65 deleted, must be
20eca 20 6f 70 65 6e 65 64 20 61 73 20 63 75 72 73 6f opened as curso
20ecb 72 20 6e 75 6d 62 65 72 20 22 69 43 75 72 22 2e r number "iCur".
20ecc 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61 .**.** 2. Rea
20ecd 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 d/write cursors
20ece 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 for all indices
20ecf 6f 66 20 70 54 61 62 20 6d 75 73 74 20 62 65 20 of pTab must be
20ed0 6f 70 65 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20 open as.**
20ed1 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 cursor number i
20ed2 43 75 72 2b 69 20 66 6f 72 20 74 68 65 20 69 2d Cur+i for the i-
20ed3 74 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 th index..**.**
20ed4 20 20 33 2e 20 20 54 68 65 20 22 69 43 75 72 22 3. The "iCur"
20ed5 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 cursor must be
20ed6 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
20ed7 72 6f 77 20 74 68 61 74 20 69 73 20 74 6f 20 62 row that is to b
20ed8 65 0a 2a 2a 20 20 20 20 20 20 20 64 65 6c 65 74 e.** delet
20ed9 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
20eda 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
20edb 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 e3GenerateRowInd
20edc 65 78 44 65 6c 65 74 65 28 0a 20 20 50 61 72 73 exDelete(. Pars
20edd 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f e *pParse, /
20ede 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
20edf 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
20ee0 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 ntext */. Table
20ee1 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a *pTab, /*
20ee2 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e Table containin
20ee3 67 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 g the row to be
20ee4 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 deleted */. int
20ee5 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 iCur,
20ee6 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 /* Cursor number
20ee7 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a for the table *
20ee8 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 /. int *aRegIdx
20ee9 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 64 /* Only d
20eea 65 6c 65 74 65 20 69 66 20 61 52 65 67 49 64 78 elete if aRegIdx
20eeb 21 3d 30 20 26 26 20 61 52 65 67 49 64 78 5b 69 !=0 && aRegIdx[i
20eec 5d 3e 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ]>0 */.){. int
20eed 69 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 i;. Index *pIdx
20eee 3b 0a 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 66 ;. int r1;.. f
20eef 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 or(i=1, pIdx=pTa
20ef0 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b b->pIndex; pIdx;
20ef1 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d i++, pIdx=pIdx-
20ef2 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
20ef3 20 61 52 65 67 49 64 78 21 3d 30 20 26 26 20 61 aRegIdx!=0 && a
20ef4 52 65 67 49 64 78 5b 69 2d 31 5d 3d 3d 30 20 29 RegIdx[i-1]==0 )
20ef5 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 continue;. r
20ef6 31 20 3d 20 73 71 6c 69 74 65 33 47 65 6e 65 72 1 = sqlite3Gener
20ef7 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72 ateIndexKey(pPar
20ef8 73 65 2c 20 70 49 64 78 2c 20 69 43 75 72 2c 20 se, pIdx, iCur,
20ef9 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 0, 0);. sqlit
20efa 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 50 61 e3VdbeAddOp3(pPa
20efb 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 49 rse->pVdbe, OP_I
20efc 64 78 44 65 6c 65 74 65 2c 20 69 43 75 72 2b 69 dxDelete, iCur+i
20efd 2c 20 72 31 2c 70 49 64 78 2d 3e 6e 43 6f 6c 75 , r1,pIdx->nColu
20efe 6d 6e 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a mn+1);. }.}../*
20eff 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
20f00 65 20 74 68 61 74 20 77 69 6c 6c 20 61 73 73 65 e that will asse
20f01 6d 62 6c 65 20 61 6e 20 69 6e 64 65 78 20 6b 65 mble an index ke
20f02 79 20 61 6e 64 20 70 75 74 20 69 74 20 69 6e 20 y and put it in
20f03 72 65 67 69 73 74 65 72 0a 2a 2a 20 72 65 67 4f register.** regO
20f04 75 74 2e 20 20 54 68 65 20 6b 65 79 20 77 69 74 ut. The key wit
20f05 68 20 62 65 20 66 6f 72 20 69 6e 64 65 78 20 70 h be for index p
20f06 49 64 78 20 77 68 69 63 68 20 69 73 20 61 6e 20 Idx which is an
20f07 69 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a index on pTab..*
20f08 2a 20 69 43 75 72 20 69 73 20 74 68 65 20 69 6e * iCur is the in
20f09 64 65 78 20 6f 66 20 61 20 63 75 72 73 6f 72 20 dex of a cursor
20f0a 6f 70 65 6e 20 6f 6e 20 74 68 65 20 70 54 61 62 open on the pTab
20f0b 20 74 61 62 6c 65 20 61 6e 64 20 70 6f 69 6e 74 table and point
20f0c 69 6e 67 20 74 6f 0a 2a 2a 20 74 68 65 20 65 6e ing to.** the en
20f0d 74 72 79 20 74 68 61 74 20 6e 65 65 64 73 20 69 try that needs i
20f0e 6e 64 65 78 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 ndexing..**.** R
20f0f 65 74 75 72 6e 20 61 20 72 65 67 69 73 74 65 72 eturn a register
20f10 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 73 number which is
20f11 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 the first in a
20f12 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 72 65 67 69 block of.** regi
20f13 73 74 65 72 73 20 74 68 61 74 20 68 6f 6c 64 73 sters that holds
20f14 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 the elements of
20f15 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 20 the index key.
20f16 20 54 68 65 0a 2a 2a 20 62 6c 6f 63 6b 20 6f 66 The.** block of
20f17 20 72 65 67 69 73 74 65 72 73 20 68 61 73 20 61 registers has a
20f18 6c 72 65 61 64 79 20 62 65 65 6e 20 64 65 61 6c lready been deal
20f19 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 74 located by the t
20f1a 69 6d 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 ime.** this rout
20f1b 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a ine returns..*/.
20f1c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
20f1d 6e 74 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 nt sqlite3Genera
20f1e 74 65 49 6e 64 65 78 4b 65 79 28 0a 20 20 50 61 teIndexKey(. Pa
20f1f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
20f20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
20f21 65 78 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a ext */. Index *
20f22 70 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 54 pIdx, /* T
20f23 68 65 20 69 6e 64 65 78 20 66 6f 72 20 77 68 69 he index for whi
20f24 63 68 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 ch to generate a
20f25 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 69 43 key */. int iC
20f26 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ur, /*
20f27 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f Cursor number fo
20f28 72 20 74 68 65 20 70 49 64 78 2d 3e 70 54 61 62 r the pIdx->pTab
20f29 6c 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e le table */. in
20f2a 74 20 72 65 67 4f 75 74 2c 20 20 20 20 20 20 20 t regOut,
20f2b 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 /* Write the ne
20f2c 77 20 69 6e 64 65 78 20 6b 65 79 20 74 6f 20 74 w index key to t
20f2d 68 69 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a his register */.
20f2e 20 20 69 6e 74 20 64 6f 4d 61 6b 65 52 65 63 20 int doMakeRec
20f2f 20 20 20 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 /* Run the
20f30 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e OP_MakeRecord in
20f31 73 74 72 75 63 74 69 6f 6e 20 69 66 20 74 72 75 struction if tru
20f32 65 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a e */.){. Vdbe *
20f33 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 v = pParse->pVdb
20f34 65 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 54 61 e;. int j;. Ta
20f35 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 64 78 ble *pTab = pIdx
20f36 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 ->pTable;. int
20f37 72 65 67 42 61 73 65 3b 0a 20 20 69 6e 74 20 6e regBase;. int n
20f38 43 6f 6c 3b 0a 0a 20 20 6e 43 6f 6c 20 3d 20 70 Col;.. nCol = p
20f39 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 Idx->nColumn;.
20f3a 72 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65 regBase = sqlite
20f3b 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 3GetTempRange(pP
20f3c 61 72 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 arse, nCol+1);.
20f3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20f3e 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 p2(v, OP_Rowid,
20f3f 69 43 75 72 2c 20 72 65 67 42 61 73 65 2b 6e 43 iCur, regBase+nC
20f40 6f 6c 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 ol);. for(j=0;
20f41 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 j<nCol; j++){.
20f42 20 20 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78 int idx = pIdx
20f43 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 ->aiColumn[j];.
20f44 20 20 20 69 66 28 20 69 64 78 3d 3d 70 54 61 62 if( idx==pTab
20f45 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 ->iPKey ){.
20f46 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20f47 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 p2(v, OP_SCopy,
20f48 72 65 67 42 61 73 65 2b 6e 43 6f 6c 2c 20 72 65 regBase+nCol, re
20f49 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 7d 65 gBase+j);. }e
20f4a 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
20f4b 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
20f4c 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c OP_Column, iCur,
20f4d 20 69 64 78 2c 20 72 65 67 42 61 73 65 2b 6a 29 idx, regBase+j)
20f4e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 ;. sqlite3C
20f4f 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 olumnDefault(v,
20f50 70 54 61 62 2c 20 69 64 78 29 3b 0a 20 20 20 20 pTab, idx);.
20f51 7d 0a 20 20 7d 0a 20 20 69 66 28 20 64 6f 4d 61 }. }. if( doMa
20f52 6b 65 52 65 63 20 29 7b 0a 20 20 20 20 73 71 6c keRec ){. sql
20f53 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
20f54 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c , OP_MakeRecord,
20f55 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 regBase, nCol+1
20f56 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20 20 20 73 , regOut);. s
20f57 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e qlite3IndexAffin
20f58 69 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b ityStr(v, pIdx);
20f59 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
20f5a 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
20f5b 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42 nge(pParse, regB
20f5c 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 ase, nCol+1);.
20f5d 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 }. sqlite3Relea
20f5e 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 seTempRange(pPar
20f5f 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f se, regBase, nCo
20f60 6c 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 l+1);. return r
20f61 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 20 4d 61 egBase;.}../* Ma
20f62 6b 65 20 73 75 72 65 20 22 69 73 56 69 65 77 22 ke sure "isView"
20f63 20 67 65 74 73 20 75 6e 64 65 66 69 6e 65 64 20 gets undefined
20f64 69 6e 20 63 61 73 65 20 74 68 69 73 20 66 69 6c in case this fil
20f65 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f e becomes part o
20f66 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c 67 61 6d f.** the amalgam
20f67 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68 61 74 20 ation - so that
20f68 73 75 62 73 65 71 75 65 6e 74 20 66 69 6c 65 73 subsequent files
20f69 20 64 6f 20 6e 6f 74 20 73 65 65 20 69 73 56 69 do not see isVi
20f6a 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61 63 72 6f ew as a.** macro
20f6b 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69 73 56 69 . */.#undef isVi
20f6c 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ew../***********
20f6d 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 65 6c 65 74 *** End of delet
20f6e 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
20f6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20f71 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
20f72 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 *** Begin file f
20f73 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a unc.c **********
20f74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20f75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20f76 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 32 20 46 **/./*.** 2002 F
20f77 65 62 72 75 61 72 79 20 32 33 0a 2a 2a 0a 2a 2a ebruary 23.**.**
20f78 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
20f79 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
20f7a 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
20f7b 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
20f7c 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
20f7d 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
20f7e 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
20f7f 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
20f80 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
20f81 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
20f82 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
20f83 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
20f84 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
20f85 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
20f86 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
20f87 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
20f88 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
20f89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20f8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20f8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20f8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20f8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
20f8e 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
20f8f 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 the C functions
20f90 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 76 that implement v
20f91 61 72 69 6f 75 73 20 53 51 4c 0a 2a 2a 20 66 75 arious SQL.** fu
20f92 6e 63 74 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 nctions of SQLit
20f93 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 e. .**.** There
20f94 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 is only one exp
20f95 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 orted symbol in
20f96 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 this file - the
20f97 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 function.** sqli
20f98 74 65 52 65 67 69 73 74 65 72 42 75 69 6c 64 69 teRegisterBuildi
20f99 6e 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75 nFunctions() fou
20f9a 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d nd at the bottom
20f9b 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a of the file..**
20f9c 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 All other code
20f9d 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a has file scope..
20f9e 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 75 6e 63 2e **.** $Id: func.
20f9f 63 2c 76 20 31 2e 32 30 36 20 32 30 30 38 2f 31 c,v 1.206 2008/1
20fa0 31 2f 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61 1/19 16:52:44 da
20fa1 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
20fa2 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e */../*.** Return
20fa3 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 the collating f
20fa4 75 6e 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 unction associat
20fa5 65 64 20 77 69 74 68 20 61 20 66 75 6e 63 74 69 ed with a functi
20fa6 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f on..*/.static Co
20fa7 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 llSeq *sqlite3Ge
20fa8 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 73 71 6c tFuncCollSeq(sql
20fa9 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
20faa 6e 74 65 78 74 29 7b 0a 20 20 72 65 74 75 72 6e ntext){. return
20fab 20 63 6f 6e 74 65 78 74 2d 3e 70 43 6f 6c 6c 3b context->pColl;
20fac 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
20fad 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
20fae 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 6d 69 non-aggregate mi
20faf 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 66 75 n() and max() fu
20fb0 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 nctions.*/.stati
20fb1 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 46 75 6e c void minmaxFun
20fb2 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
20fb3 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
20fb4 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c int argc,. sql
20fb5 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
20fb6 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 v.){. int i;.
20fb7 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 2f 2a 20 int mask; /*
20fb8 30 20 66 6f 72 20 6d 69 6e 28 29 20 6f 72 20 30 0 for min() or 0
20fb9 78 66 66 66 66 66 66 66 66 20 66 6f 72 20 6d 61 xffffffff for ma
20fba 78 28 29 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65 x() */. int iBe
20fbb 73 74 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 st;. CollSeq *p
20fbc 43 6f 6c 6c 3b 0a 0a 20 20 69 66 28 20 61 72 67 Coll;.. if( arg
20fbd 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 c==0 ) return;.
20fbe 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 5f mask = sqlite3_
20fbf 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 user_data(contex
20fc0 74 29 3d 3d 30 20 3f 20 30 20 3a 20 2d 31 3b 0a t)==0 ? 0 : -1;.
20fc1 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
20fc2 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 3GetFuncCollSeq(
20fc3 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 context);. asse
20fc4 72 74 28 20 70 43 6f 6c 6c 20 29 3b 0a 20 20 61 rt( pColl );. a
20fc5 73 73 65 72 74 28 20 6d 61 73 6b 3d 3d 2d 31 20 ssert( mask==-1
20fc6 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 || mask==0 );.
20fc7 69 42 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 iBest = 0;. if(
20fc8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
20fc9 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 ype(argv[0])==SQ
20fca 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 LITE_NULL ) retu
20fcb 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 rn;. for(i=1; i
20fcc 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 <argc; i++){.
20fcd 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c if( sqlite3_val
20fce 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29 ue_type(argv[i])
20fcf 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 ==SQLITE_NULL )
20fd0 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 return;. if(
20fd1 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 (sqlite3MemCompa
20fd2 72 65 28 61 72 67 76 5b 69 42 65 73 74 5d 2c 20 re(argv[iBest],
20fd3 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29 5e argv[i], pColl)^
20fd4 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a 20 20 20 20 mask)>=0 ){.
20fd5 20 20 69 42 65 73 74 20 3d 20 69 3b 0a 20 20 20 iBest = i;.
20fd6 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 }. }. sqlite3
20fd7 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f _result_value(co
20fd8 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 42 65 73 ntext, argv[iBes
20fd9 74 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 t]);.}../*.** Re
20fda 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 6f 66 turn the type of
20fdb 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a the argument..*
20fdc 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 79 /.static void ty
20fdd 70 65 6f 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 peofFunc(. sqli
20fde 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
20fdf 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 text,. int NotU
20fe0 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 sed,. sqlite3_v
20fe1 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
20fe2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d const char *z =
20fe3 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 0;. UNUSED_PAR
20fe4 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
20fe5 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 . switch( sqlit
20fe6 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 e3_value_type(ar
20fe7 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 gv[0]) ){. ca
20fe8 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 se SQLITE_NULL:
20fe9 20 20 20 7a 20 3d 20 22 6e 75 6c 6c 22 3b 20 20 z = "null";
20fea 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
20feb 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 e SQLITE_INTEGER
20fec 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65 72 22 3b : z = "integer";
20fed 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 break;. case
20fee 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 20 20 SQLITE_TEXT:
20fef 20 7a 20 3d 20 22 74 65 78 74 22 3b 20 20 20 20 z = "text";
20ff0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
20ff1 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20 SQLITE_FLOAT:
20ff2 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20 20 20 62 z = "real"; b
20ff3 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 reak;. case S
20ff4 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a QLITE_BLOB: z
20ff5 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20 20 62 72 = "blob"; br
20ff6 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 eak;. }. sqlit
20ff7 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 e3_result_text(c
20ff8 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 ontext, z, -1, S
20ff9 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d QLITE_STATIC);.}
20ffa 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 .../*.** Impleme
20ffb 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c ntation of the l
20ffc 65 6e 67 74 68 28 29 20 66 75 6e 63 74 69 6f 6e ength() function
20ffd 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
20ffe 6c 65 6e 67 74 68 46 75 6e 63 28 0a 20 20 73 71 lengthFunc(. sq
20fff 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
21000 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
21001 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
21002 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
21003 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 int len;.. asse
21004 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 rt( argc==1 );.
21005 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
21006 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 R(argc);. switc
21007 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 h( sqlite3_value
21008 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 _type(argv[0]) )
21009 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
2100a 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61 73 65 E_BLOB:. case
2100b 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a SQLITE_INTEGER:
2100c 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
2100d 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 _FLOAT: {.
2100e 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
2100f 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 nt(context, sqli
21010 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
21011 61 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20 argv[0]));.
21012 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
21013 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 case SQLITE_TE
21014 58 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 XT: {. cons
21015 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
21016 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c *z = sqlite3_val
21017 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
21018 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 ;. if( z==0
21019 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 ) return;.
2101a 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 len = 0;.
2101b 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20 while( *z ){.
2101c 20 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20 len++;.
2101d 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f SQLITE_SKIP_
2101e 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 7d UTF8(z);. }
2101f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
21020 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 esult_int(contex
21021 74 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20 20 62 t, len);. b
21022 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
21023 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
21024 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
21025 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 null(context);.
21026 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21027 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 }. }.}../*.** I
21028 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
21029 20 74 68 65 20 61 62 73 28 29 20 66 75 6e 63 74 the abs() funct
2102a 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ion.*/.static vo
2102b 69 64 20 61 62 73 46 75 6e 63 28 73 71 6c 69 74 id absFunc(sqlit
2102c 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
2102d 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 ext, int argc, s
2102e 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
2102f 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74 28 20 rgv){. assert(
21030 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 argc==1 );. UNU
21031 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 SED_PARAMETER(ar
21032 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 gc);. switch( s
21033 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
21034 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 e(argv[0]) ){.
21035 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e case SQLITE_IN
21036 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 TEGER: {. i
21037 36 34 20 69 56 61 6c 20 3d 20 73 71 6c 69 74 65 64 iVal = sqlite
21038 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 3_value_int64(ar
21039 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 gv[0]);. if
2103a 28 20 69 56 61 6c 3c 30 20 29 7b 0a 20 20 20 20 ( iVal<0 ){.
2103b 20 20 20 20 69 66 28 20 28 69 56 61 6c 3c 3c 31 if( (iVal<<1
2103c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
2103d 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
2103e 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 _error(context,
2103f 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f "integer overflo
21040 77 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 w", -1);.
21041 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 return;.
21042 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 56 61 }. iVa
21043 6c 20 3d 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20 l = -iVal;.
21044 20 7d 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 } . sqlite
21045 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 3_result_int64(c
21046 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20 ontext, iVal);.
21047 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21048 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
21049 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 E_NULL: {.
2104a 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e sqlite3_result_n
2104b 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 ull(context);.
2104c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
2104d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
2104e 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61 double rVa
2104f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
21050 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d e_double(argv[0]
21051 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 56 61 );. if( rVa
21052 6c 3c 30 20 29 20 72 56 61 6c 20 3d 20 2d 72 56 l<0 ) rVal = -rV
21053 61 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 al;. sqlite
21054 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 3_result_double(
21055 63 6f 6e 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a context, rVal);.
21056 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21057 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
21058 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
21059 66 20 74 68 65 20 73 75 62 73 74 72 28 29 20 66 f the substr() f
2105a 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 unction..**.** s
2105b 75 62 73 74 72 28 78 2c 70 31 2c 70 32 29 20 20 ubstr(x,p1,p2)
2105c 72 65 74 75 72 6e 73 20 70 32 20 63 68 61 72 61 returns p2 chara
2105d 63 74 65 72 73 20 6f 66 20 78 5b 5d 20 62 65 67 cters of x[] beg
2105e 69 6e 6e 69 6e 67 20 77 69 74 68 20 70 31 2e 0a inning with p1..
2105f 2a 2a 20 70 31 20 69 73 20 31 2d 69 6e 64 65 78 ** p1 is 1-index
21060 65 64 2e 20 20 53 6f 20 73 75 62 73 74 72 28 78 ed. So substr(x
21061 2c 31 2c 31 29 20 72 65 74 75 72 6e 73 20 74 68 ,1,1) returns th
21062 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 e first characte
21063 72 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66 20 78 r.** of x. If x
21064 20 69 73 20 74 65 78 74 2c 20 74 68 65 6e 20 77 is text, then w
21065 65 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e 74 e actually count
21066 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 UTF-8 character
21067 73 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 20 s..** If x is a
21068 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 63 6f blob, then we co
21069 75 6e 74 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a unt bytes..**.**
2106a 20 49 66 20 70 31 20 69 73 20 6e 65 67 61 74 69 If p1 is negati
2106b 76 65 2c 20 74 68 65 6e 20 77 65 20 62 65 67 69 ve, then we begi
2106c 6e 20 61 62 73 28 70 31 29 20 66 72 6f 6d 20 74 n abs(p1) from t
2106d 68 65 20 65 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a he end of x[]..*
2106e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 /.static void su
2106f 62 73 74 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 bstrFunc(. sqli
21070 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
21071 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
21072 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
21073 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f e **argv.){. co
21074 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
21075 72 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e r *z;. const un
21076 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 32 3b signed char *z2;
21077 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e . int len;. in
21078 74 20 70 30 74 79 70 65 3b 0a 20 20 69 36 34 20 t p0type;. i64
21079 70 31 2c 20 70 32 3b 0a 0a 20 20 61 73 73 65 72 p1, p2;.. asser
2107a 74 28 20 61 72 67 63 3d 3d 33 20 7c 7c 20 61 72 t( argc==3 || ar
2107b 67 63 3d 3d 32 20 29 3b 0a 20 20 70 30 74 79 70 gc==2 );. p0typ
2107c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 e = sqlite3_valu
2107d 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b e_type(argv[0]);
2107e 0a 20 20 69 66 28 20 70 30 74 79 70 65 3d 3d 53 . if( p0type==S
2107f 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 QLITE_BLOB ){.
21080 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f len = sqlite3_
21081 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 value_bytes(argv
21082 5b 30 5d 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 [0]);. z = sq
21083 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
21084 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 (argv[0]);. i
21085 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e f( z==0 ) return
21086 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 ;. assert( le
21087 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 n==sqlite3_value
21088 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20 _bytes(argv[0])
21089 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
2108a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 z = sqlite3_valu
2108b 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b e_text(argv[0]);
2108c 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 . if( z==0 )
2108d 72 65 74 75 72 6e 3b 0a 20 20 20 20 6c 65 6e 20 return;. len
2108e 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 7a 32 3d = 0;. for(z2=
2108f 7a 3b 20 2a 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0a z; *z2; len++){.
21090 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 SQLITE_SKI
21091 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20 20 20 P_UTF8(z2);.
21092 7d 0a 20 20 7d 0a 20 20 70 31 20 3d 20 73 71 6c }. }. p1 = sql
21093 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 ite3_value_int(a
21094 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 61 rgv[1]);. if( a
21095 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 70 32 rgc==3 ){. p2
21096 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
21097 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 _int(argv[2]);.
21098 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 32 20 3d }else{. p2 =
21099 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
2109a 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 _db_handle(conte
2109b 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 xt)->aLimit[SQLI
2109c 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
2109d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 31 3c 30 ;. }. if( p1<0
2109e 20 29 7b 0a 20 20 20 20 70 31 20 2b 3d 20 6c 65 ){. p1 += le
2109f 6e 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30 20 n;. if( p1<0
210a0 29 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20 70 ){. p2 += p
210a1 31 3b 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b 1;. p1 = 0;
210a2 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
210a3 66 28 20 70 31 3e 30 20 29 7b 0a 20 20 20 20 70 f( p1>0 ){. p
210a4 31 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 1--;. }. if( p
210a5 31 2b 70 32 3e 6c 65 6e 20 29 7b 0a 20 20 20 20 1+p2>len ){.
210a6 70 32 20 3d 20 6c 65 6e 2d 70 31 3b 0a 20 20 7d p2 = len-p1;. }
210a7 0a 20 20 69 66 28 20 70 30 74 79 70 65 21 3d 53 . if( p0type!=S
210a8 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 QLITE_BLOB ){.
210a9 20 20 77 68 69 6c 65 28 20 2a 7a 20 26 26 20 70 while( *z && p
210aa 31 20 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 1 ){. SQLIT
210ab 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a E_SKIP_UTF8(z);.
210ac 20 20 20 20 20 20 70 31 2d 2d 3b 0a 20 20 20 20 p1--;.
210ad 7d 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20 }. for(z2=z;
210ae 2a 7a 32 20 26 26 20 70 32 3b 20 70 32 2d 2d 29 *z2 && p2; p2--)
210af 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 {. SQLITE_S
210b0 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20 KIP_UTF8(z2);.
210b1 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
210b2 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
210b3 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 7a ext, (char*)z, z
210b4 32 2d 7a 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 2-z, SQLITE_TRAN
210b5 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b SIENT);. }else{
210b6 0a 20 20 20 20 69 66 28 20 70 32 3c 30 20 29 20 . if( p2<0 )
210b7 70 32 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 p2 = 0;. sqli
210b8 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 te3_result_blob(
210b9 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 context, (char*)
210ba 26 7a 5b 70 31 5d 2c 20 70 32 2c 20 53 51 4c 49 &z[p1], p2, SQLI
210bb 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 TE_TRANSIENT);.
210bc 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c }.}../*.** Impl
210bd 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
210be 65 20 72 6f 75 6e 64 28 29 20 66 75 6e 63 74 69 e round() functi
210bf 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 on.*/.static voi
210c0 64 20 72 6f 75 6e 64 46 75 6e 63 28 73 71 6c 69 d roundFunc(sqli
210c1 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
210c2 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 text, int argc,
210c3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
210c4 61 72 67 76 29 7b 0a 20 20 69 6e 74 20 6e 20 3d argv){. int n =
210c5 20 30 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 0;. double r;.
210c6 20 20 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d char zBuf[500]
210c7 3b 20 20 2f 2a 20 6c 61 72 67 65 72 20 74 68 61 ; /* larger tha
210c8 6e 20 74 68 65 20 25 66 20 72 65 70 72 65 73 65 n the %f represe
210c9 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c ntation of the l
210ca 61 72 67 65 73 74 20 64 6f 75 62 6c 65 20 2a 2f argest double */
210cb 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d . assert( argc=
210cc 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b =1 || argc==2 );
210cd 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 . if( argc==2 )
210ce 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 {. if( SQLITE
210cf 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 _NULL==sqlite3_v
210d0 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 alue_type(argv[1
210d1 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 ]) ) return;.
210d2 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c n = sqlite3_val
210d3 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b ue_int(argv[1]);
210d4 0a 20 20 20 20 69 66 28 20 6e 3e 33 30 20 29 20 . if( n>30 )
210d5 6e 20 3d 20 33 30 3b 0a 20 20 20 20 69 66 28 20 n = 30;. if(
210d6 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 7d n<0 ) n = 0;. }
210d7 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 . if( sqlite3_v
210d8 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 alue_type(argv[0
210d9 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 ])==SQLITE_NULL
210da 29 20 72 65 74 75 72 6e 3b 0a 20 20 72 20 3d 20 ) return;. r =
210db 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f sqlite3_value_do
210dc 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 uble(argv[0]);.
210dd 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
210de 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 7a f(sizeof(zBuf),z
210df 42 75 66 2c 22 25 2e 2a 66 22 2c 6e 2c 72 29 3b Buf,"%.*f",n,r);
210e0 0a 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 7a . sqlite3AtoF(z
210e1 42 75 66 2c 20 26 72 29 3b 0a 20 20 73 71 6c 69 Buf, &r);. sqli
210e2 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c te3_result_doubl
210e3 65 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 7d e(context, r);.}
210e4 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
210e5 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 nByte bytes of
210e6 73 70 61 63 65 20 75 73 69 6e 67 20 73 71 6c 69 space using sqli
210e7 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 66 te3_malloc(). If
210e8 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 the.** allocati
210e9 6f 6e 20 66 61 69 6c 73 2c 20 63 61 6c 6c 20 73 on fails, call s
210ea 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
210eb 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74 6f 20 6e ror_nomem() to n
210ec 6f 74 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 otify.** the dat
210ed 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 abase handle tha
210ee 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 t malloc() has f
210ef 61 69 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 ailed..*/.static
210f0 20 76 6f 69 64 20 2a 63 6f 6e 74 65 78 74 4d 61 void *contextMa
210f1 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e lloc(sqlite3_con
210f2 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 text *context, i
210f3 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 63 68 61 64 nByte){. cha
210f4 72 20 2a 7a 3b 0a 20 20 69 66 28 20 6e 42 79 74 r *z;. if( nByt
210f5 65 3e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 e>sqlite3_contex
210f6 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 t_db_handle(cont
210f7 65 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ext)->aLimit[SQL
210f8 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
210f9 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ] ){. sqlite3
210fa 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f _result_error_to
210fb 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 obig(context);.
210fc 20 20 20 7a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 z = 0;. }els
210fd 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 e{. z = sqlit
210fe 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b e3Malloc(nByte);
210ff 0a 20 20 20 20 69 66 28 20 21 7a 20 26 26 20 6e . if( !z && n
21100 42 79 74 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 Byte>0 ){.
21101 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
21102 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 rror_nomem(conte
21103 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 xt);. }. }.
21104 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a return z;.}../*
21105 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
21106 6f 6e 20 6f 66 20 74 68 65 20 75 70 70 65 72 28 on of the upper(
21107 29 20 61 6e 64 20 6c 6f 77 65 72 28 29 20 53 51 ) and lower() SQ
21108 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a L functions..*/.
21109 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 70 65 static void uppe
2110a 72 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f rFunc(sqlite3_co
2110b 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
2110c 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 int argc, sqlite
2110d 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 3_value **argv){
2110e 0a 20 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 . char *z1;. c
2110f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 onst char *z2;.
21110 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 int i, n;. if(
21111 20 61 72 67 63 3c 31 20 7c 7c 20 53 51 4c 49 54 argc<1 || SQLIT
21112 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f E_NULL==sqlite3_
21113 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b value_type(argv[
21114 30 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 0]) ) return;.
21115 7a 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 z2 = (char*)sqli
21116 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
21117 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 rgv[0]);. n = s
21118 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
21119 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f es(argv[0]);. /
2111a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 * Verify that th
2111b 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 e call to _bytes
2111c 28 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 () does not inva
2111d 6c 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 lidate the _text
2111e 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 () pointer */.
2111f 61 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 assert( z2==(cha
21120 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 r*)sqlite3_value
21121 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 _text(argv[0]) )
21122 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 ;. if( z2 ){.
21123 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 z1 = contextMa
21124 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 lloc(context, ((
21125 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 i64)n)+1);. i
21126 66 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d f( z1 ){. m
21127 65 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b emcpy(z1, z2, n+
21128 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 1);. for(i=
21129 30 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 0; z1[i]; i++){.
2112a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 z1[i] =
2112b 74 6f 75 70 70 65 72 28 7a 31 5b 69 5d 29 3b 0a toupper(z1[i]);.
2112c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
2112d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2112e 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d t(context, z1, -
2112f 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 1, sqlite3_free)
21130 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 ;. }. }.}.st
21131 61 74 69 63 20 76 6f 69 64 20 6c 6f 77 65 72 46 atic void lowerF
21132 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 unc(sqlite3_cont
21133 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e ext *context, in
21134 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f t argc, sqlite3_
21135 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 value **argv){.
21136 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e char *z1;. con
21137 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 st char *z2;. i
21138 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 61 nt i, n;. if( a
21139 72 67 63 3c 31 20 7c 7c 20 53 51 4c 49 54 45 5f rgc<1 || SQLITE_
2113a 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 NULL==sqlite3_va
2113b 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d lue_type(argv[0]
2113c 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 32 ) ) return;. z2
2113d 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 = (char*)sqlite
2113e 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
2113f 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c v[0]);. n = sql
21140 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
21141 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 (argv[0]);. /*
21142 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 Verify that the
21143 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 call to _bytes()
21144 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 does not invali
21145 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 29 date the _text()
21146 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 pointer */. as
21147 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a sert( z2==(char*
21148 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
21149 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a ext(argv[0]) );.
2114a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 if( z2 ){.
2114b 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c z1 = contextMall
2114c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 oc(context, ((i6
2114d 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 4)n)+1);. if(
2114e 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d z1 ){. mem
2114f 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 cpy(z1, z2, n+1)
21150 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b ;. for(i=0;
21151 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 z1[i]; i++){.
21152 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 74 6f z1[i] = to
21153 6c 6f 77 65 72 28 7a 31 5b 69 5d 29 3b 0a 20 20 lower(z1[i]);.
21154 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
21155 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
21156 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d 31 2c context, z1, -1,
21157 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a sqlite3_free);.
21158 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
21159 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
2115a 6e 20 6f 66 20 74 68 65 20 49 46 4e 55 4c 4c 28 n of the IFNULL(
2115b 29 2c 20 4e 56 4c 28 29 2c 20 61 6e 64 20 43 4f ), NVL(), and CO
2115c 41 4c 45 53 43 45 28 29 20 66 75 6e 63 74 69 6f ALESCE() functio
2115d 6e 73 2e 20 20 0a 2a 2a 20 41 6c 6c 20 74 68 72 ns. .** All thr
2115e 65 65 20 64 6f 20 74 68 65 20 73 61 6d 65 20 74 ee do the same t
2115f 68 69 6e 67 2e 20 20 54 68 65 79 20 72 65 74 75 hing. They retu
21160 72 6e 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e rn the first non
21161 2d 4e 55 4c 4c 0a 2a 2a 20 61 72 67 75 6d 65 6e -NULL.** argumen
21162 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
21163 64 20 69 66 6e 75 6c 6c 46 75 6e 63 28 0a 20 20 d ifnullFunc(.
21164 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
21165 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
21166 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f argc,. sqlite3_
21167 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
21168 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
21169 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 =0; i<argc; i++)
2116a 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 {. if( SQLITE
2116b 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 _NULL!=sqlite3_v
2116c 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 alue_type(argv[i
2116d 5d 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ]) ){. sqli
2116e 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
2116f 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 (context, argv[i
21170 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ]);. break;
21171 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
21172 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
21173 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 28 29 2e 20 on of random().
21174 20 52 65 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d Return a random
21175 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2f 0a 73 integer. .*/.s
21176 74 61 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f tatic void rando
21177 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 mFunc(. sqlite3
21178 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
21179 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 t,. int NotUsed
2117a 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
2117b 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a e **NotUsed2.){.
2117c 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 sqlite_int64 r
2117d 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
2117e 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e ETER2(NotUsed, N
2117f 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 otUsed2);. sqli
21180 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 te3_randomness(s
21181 69 7a 65 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20 izeof(r), &r);.
21182 20 69 66 28 20 28 72 3c 3c 31 29 3d 3d 30 20 29 if( (r<<1)==0 )
21183 20 72 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 r = 0; /* Prev
21184 65 6e 74 20 30 78 38 30 30 30 2e 2e 2e 2e 20 61 ent 0x8000.... a
21185 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 6f 20 s the result so
21186 74 68 61 74 20 77 65 20 2a 2f 0a 20 20 20 20 20 that we */.
21187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21188 20 20 20 20 20 2f 2a 20 63 61 6e 20 61 6c 77 61 /* can alwa
21189 79 73 20 64 6f 20 61 62 73 28 29 20 6f 66 20 74 ys do abs() of t
2118a 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 73 he result */. s
2118b 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
2118c 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b t64(context, r);
2118d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
2118e 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 entation of rand
2118f 6f 6d 62 6c 6f 62 28 4e 29 2e 20 20 52 65 74 75 omblob(N). Retu
21190 72 6e 20 61 20 72 61 6e 64 6f 6d 20 62 6c 6f 62 rn a random blob
21191 0a 2a 2a 20 74 68 61 74 20 69 73 20 4e 20 62 79 .** that is N by
21192 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 tes long..*/.sta
21193 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 42 tic void randomB
21194 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 lob(. sqlite3_c
21195 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
21196 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
21197 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
21198 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a rgv.){. int n;.
21199 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
2119a 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 *p;. assert( ar
2119b 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 gc==1 );. UNUSE
2119c 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 D_PARAMETER(argc
2119d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 );. n = sqlite3
2119e 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b _value_int(argv[
2119f 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20 29 0]);. if( n<1 )
211a0 7b 0a 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 7d {. n = 1;. }
211a1 0a 20 20 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61 . p = contextMa
211a2 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29 lloc(context, n)
211a3 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
211a4 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
211a5 65 73 73 28 6e 2c 20 70 29 3b 0a 20 20 20 20 73 ess(n, p);. s
211a6 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c qlite3_result_bl
211a7 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 ob(context, (cha
211a8 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65 33 r*)p, n, sqlite3
211a9 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f _free);. }.}../
211aa 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
211ab 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 73 74 5f ion of the last_
211ac 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 53 insert_rowid() S
211ad 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 QL function. Th
211ae 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 e return.** valu
211af 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 e is the same as
211b0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 the sqlite3_las
211b1 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 t_insert_rowid()
211b2 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a API function..*
211b3 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 61 /.static void la
211b4 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 st_insert_rowid(
211b5 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
211b6 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 xt *context, .
211b7 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20 20 int NotUsed, .
211b8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
211b9 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 NotUsed2.){. sq
211ba 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
211bb 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
211bc 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
211bd 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
211be 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 ER2(NotUsed, Not
211bf 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 Used2);. sqlite
211c0 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 3_result_int64(c
211c1 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f ontext, sqlite3_
211c2 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 last_insert_rowi
211c3 64 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a d(db));.}../*.**
211c4 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
211c5 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 28 29 of the changes()
211c6 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 SQL function.
211c7 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
211c8 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 is the.** same
211c9 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 as the sqlite3_c
211ca 68 61 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e hanges() API fun
211cb 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
211cc 20 76 6f 69 64 20 63 68 61 6e 67 65 73 28 0a 20 void changes(.
211cd 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
211ce 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
211cf 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 NotUsed,. sqli
211d0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 te3_value **NotU
211d1 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65 sed2.){. sqlite
211d2 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 3 *db = sqlite3_
211d3 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
211d4 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e e(context);. UN
211d5 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 USED_PARAMETER2(
211d6 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 NotUsed, NotUsed
211d7 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 2);. sqlite3_re
211d8 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 sult_int(context
211d9 2c 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 , sqlite3_change
211da 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a s(db));.}../*.**
211db 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
211dc 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f 63 68 61 of the total_cha
211dd 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 nges() SQL funct
211de 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e ion. The return
211df 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 value is.** the
211e0 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c same as the sql
211e1 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 ite3_total_chang
211e2 65 73 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f es() API functio
211e3 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 n..*/.static voi
211e4 64 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 d total_changes(
211e5 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
211e6 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
211e7 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 nt NotUsed,. sq
211e8 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f lite3_value **No
211e9 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 tUsed2.){. sqli
211ea 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 te3 *db = sqlite
211eb 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
211ec 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
211ed 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
211ee 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
211ef 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f ed2);. sqlite3_
211f0 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 result_int(conte
211f1 78 74 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 xt, sqlite3_tota
211f2 6c 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a l_changes(db));.
211f3 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 }../*.** A struc
211f4 74 75 72 65 20 64 65 66 69 6e 69 6e 67 20 68 6f ture defining ho
211f5 77 20 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 79 w to do GLOB-sty
211f6 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a le comparisons..
211f7 2a 2f 0a 73 74 72 75 63 74 20 63 6f 6d 70 61 72 */.struct compar
211f8 65 49 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61 74 eInfo {. u8 mat
211f9 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63 chAll;. u8 matc
211fa 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68 hOne;. u8 match
211fb 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 Set;. u8 noCase
211fc 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 ;.};../*.** For
211fd 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 6d 61 LIKE and GLOB ma
211fe 74 63 68 69 6e 67 20 6f 6e 20 45 42 43 44 49 43 tching on EBCDIC
211ff 20 6d 61 63 68 69 6e 65 73 2c 20 61 73 73 75 6d machines, assum
21200 65 20 74 68 61 74 20 65 76 65 72 79 0a 2a 2a 20 e that every.**
21201 63 68 61 72 61 63 74 65 72 20 69 73 20 65 78 61 character is exa
21202 63 74 6c 79 20 6f 6e 65 20 62 79 74 65 20 69 6e ctly one byte in
21203 20 73 69 7a 65 2e 20 20 41 6c 73 6f 2c 20 61 6c size. Also, al
21204 6c 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 l characters are
21205 0a 2a 2a 20 61 62 6c 65 20 74 6f 20 70 61 72 74 .** able to part
21206 69 63 69 70 61 74 65 20 69 6e 20 75 70 70 65 72 icipate in upper
21207 2d 63 61 73 65 2d 74 6f 2d 6c 6f 77 65 72 2d 63 -case-to-lower-c
21208 61 73 65 20 6d 61 70 70 69 6e 67 73 20 69 6e 20 ase mappings in
21209 45 42 43 44 49 43 0a 2a 2a 20 77 68 65 72 65 61 EBCDIC.** wherea
2120a 73 20 6f 6e 6c 79 20 63 68 61 72 61 63 74 65 72 s only character
2120b 73 20 6c 65 73 73 20 74 68 61 6e 20 30 78 38 30 s less than 0x80
2120c 20 64 6f 20 69 6e 20 41 53 43 49 49 2e 0a 2a 2f do in ASCII..*/
2120d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
2120e 49 54 45 5f 45 42 43 44 49 43 29 0a 23 20 64 65 ITE_EBCDIC).# de
2120f 66 69 6e 65 20 73 71 6c 69 74 65 33 55 74 66 38 fine sqlite3Utf8
21210 52 65 61 64 28 41 2c 42 2c 43 29 20 20 28 2a 28 Read(A,B,C) (*(
21211 41 2b 2b 29 29 0a 23 20 64 65 66 69 6e 65 20 47 A++)).# define G
21212 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 logUpperToLower(
21213 41 29 20 20 20 20 20 41 20 3d 20 73 71 6c 69 74 A) A = sqlit
21214 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 e3UpperToLower[A
21215 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ].#else.# define
21216 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 GlogUpperToLowe
21217 72 28 41 29 20 20 20 20 20 69 66 28 20 41 3c 30 r(A) if( A<0
21218 78 38 30 20 29 7b 20 41 20 3d 20 73 71 6c 69 74 x80 ){ A = sqlit
21219 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 e3UpperToLower[A
2121a 5d 3b 20 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 ]; }.#endif..sta
2121b 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 tic const struct
2121c 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 67 6c 6f compareInfo glo
2121d 62 49 6e 66 6f 20 3d 20 7b 20 27 2a 27 2c 20 27 bInfo = { '*', '
2121e 3f 27 2c 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a ?', '[', 0 };./*
2121f 20 54 68 65 20 63 6f 72 72 65 63 74 20 53 51 4c The correct SQL
21220 2d 39 32 20 62 65 68 61 76 69 6f 72 20 69 73 20 -92 behavior is
21221 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 for the LIKE ope
21222 72 61 74 6f 72 20 74 6f 20 69 67 6e 6f 72 65 0a rator to ignore.
21223 2a 2a 20 63 61 73 65 2e 20 20 54 68 75 73 20 20 ** case. Thus
21224 27 61 27 20 4c 49 4b 45 20 27 41 27 20 77 6f 75 'a' LIKE 'A' wou
21225 6c 64 20 62 65 20 74 72 75 65 2e 20 2a 2f 0a 73 ld be true. */.s
21226 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 tatic const stru
21227 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c ct compareInfo l
21228 69 6b 65 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20 ikeInfoNorm = {
21229 27 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 31 '%', '_', 0, 1
2122a 20 7d 3b 0a 2f 2a 20 49 66 20 53 51 4c 49 54 45 };./* If SQLITE
2122b 5f 43 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f _CASE_SENSITIVE_
2122c 4c 49 4b 45 20 69 73 20 64 65 66 69 6e 65 64 2c LIKE is defined,
2122d 20 74 68 65 6e 20 74 68 65 20 4c 49 4b 45 20 6f then the LIKE o
2122e 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73 20 63 61 perator.** is ca
2122f 73 65 20 73 65 6e 73 69 74 69 76 65 20 63 61 75 se sensitive cau
21230 73 69 6e 67 20 27 61 27 20 4c 49 4b 45 20 27 41 sing 'a' LIKE 'A
21231 27 20 74 6f 20 62 65 20 66 61 6c 73 65 20 2a 2f ' to be false */
21232 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 .static const st
21233 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f ruct compareInfo
21234 20 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 20 7b likeInfoAlt = {
21235 20 27 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 '%', '_', 0,
21236 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 0 };../*.** Comp
21237 61 72 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74 are two UTF-8 st
21238 72 69 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69 rings for equali
21239 74 79 20 77 68 65 72 65 20 74 68 65 20 66 69 72 ty where the fir
2123a 73 74 20 73 74 72 69 6e 67 20 63 61 6e 0a 2a 2a st string can.**
2123b 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 potentially be
2123c 61 20 22 67 6c 6f 62 22 20 65 78 70 72 65 73 73 a "glob" express
2123d 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 74 72 75 ion. Return tru
2123e 65 20 28 31 29 20 69 66 20 74 68 65 79 0a 2a 2a e (1) if they.**
2123f 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 6e are the same an
21240 64 20 66 61 6c 73 65 20 28 30 29 20 69 66 20 74 d false (0) if t
21241 68 65 79 20 61 72 65 20 64 69 66 66 65 72 65 6e hey are differen
21242 74 2e 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e t..**.** Globbin
21243 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 g rules:.**.**
21244 20 20 20 20 27 2a 27 20 20 20 20 20 20 20 4d 61 '*' Ma
21245 74 63 68 65 73 20 61 6e 79 20 73 65 71 75 65 6e tches any sequen
21246 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f ce of zero or mo
21247 72 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a re characters..*
21248 2a 0a 2a 2a 20 20 20 20 20 20 27 3f 27 20 20 20 *.** '?'
21249 20 20 20 20 4d 61 74 63 68 65 73 20 65 78 61 63 Matches exac
2124a 74 6c 79 20 6f 6e 65 20 63 68 61 72 61 63 74 65 tly one characte
2124b 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e r..**.** [..
2124c 2e 5d 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 .] Matches
2124d 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 66 72 one character fr
2124e 6f 6d 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 om the enclosed
2124f 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 list of.**
21250 20 20 20 20 20 20 20 20 20 20 63 68 61 72 61 63 charac
21251 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ters..**.**
21252 5b 5e 2e 2e 2e 5d 20 20 20 20 20 4d 61 74 63 68 [^...] Match
21253 65 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 es one character
21254 20 6e 6f 74 20 69 6e 20 74 68 65 20 65 6e 63 6c not in the encl
21255 6f 73 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a osed list..**.**
21256 20 57 69 74 68 20 74 68 65 20 5b 2e 2e 2e 5d 20 With the [...]
21257 61 6e 64 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68 and [^...] match
21258 69 6e 67 2c 20 61 20 27 5d 27 20 63 68 61 72 61 ing, a ']' chara
21259 63 74 65 72 20 63 61 6e 20 62 65 20 69 6e 63 6c cter can be incl
2125a 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6c uded.** in the l
2125b 69 73 74 20 62 79 20 6d 61 6b 69 6e 67 20 69 74 ist by making it
2125c 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 the first chara
2125d 63 74 65 72 20 61 66 74 65 72 20 27 5b 27 20 6f cter after '[' o
2125e 72 20 27 5e 27 2e 20 20 41 0a 2a 2a 20 72 61 6e r '^'. A.** ran
2125f 67 65 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 ge of characters
21260 20 63 61 6e 20 62 65 20 73 70 65 63 69 66 69 65 can be specifie
21261 64 20 75 73 69 6e 67 20 27 2d 27 2e 20 20 45 78 d using '-'. Ex
21262 61 6d 70 6c 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d ample:.** "[a-z]
21263 22 20 6d 61 74 63 68 65 73 20 61 6e 79 20 73 69 " matches any si
21264 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 61 73 65 20 ngle lower-case
21265 6c 65 74 74 65 72 2e 20 20 54 6f 20 6d 61 74 63 letter. To matc
21266 68 20 61 20 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a h a '-', make.**
21267 20 69 74 20 74 68 65 20 6c 61 73 74 20 63 68 61 it the last cha
21268 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 6c 69 racter in the li
21269 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 st..**.** This r
2126a 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c outine is usuall
2126b 79 20 71 75 69 63 6b 2c 20 62 75 74 20 63 61 6e y quick, but can
2126c 20 62 65 20 4e 2a 2a 32 20 69 6e 20 74 68 65 20 be N**2 in the
2126d 77 6f 72 73 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a worst case..**.*
2126e 2a 20 48 69 6e 74 73 3a 20 74 6f 20 6d 61 74 63 * Hints: to matc
2126f 68 20 27 2a 27 20 6f 72 20 27 3f 27 2c 20 70 75 h '*' or '?', pu
21270 74 20 74 68 65 6d 20 69 6e 20 22 5b 5d 22 2e 20 t them in "[]".
21271 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a Like this:.**.*
21272 2a 20 20 20 20 20 20 20 20 20 61 62 63 5b 2a 5d * abc[*]
21273 78 79 7a 20 20 20 20 20 20 20 20 4d 61 74 63 68 xyz Match
21274 65 73 20 22 61 62 63 2a 78 79 7a 22 20 6f 6e 6c es "abc*xyz" onl
21275 79 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 y.*/.static int
21276 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 0a patternCompare(.
21277 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 50 61 74 const u8 *zPat
21278 74 65 72 6e 2c 20 20 20 20 20 20 20 20 20 20 20 tern,
21279 20 20 20 2f 2a 20 54 68 65 20 67 6c 6f 62 20 70 /* The glob p
2127a 61 74 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 attern */. cons
2127b 74 20 75 38 20 2a 7a 53 74 72 69 6e 67 2c 20 20 t u8 *zString,
2127c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2127d 54 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f The string to co
2127e 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 74 68 mpare against th
2127f 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 e glob */. cons
21280 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 t struct compare
21281 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20 Info *pInfo, /*
21282 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
21283 74 20 68 6f 77 20 74 6f 20 64 6f 20 74 68 65 20 t how to do the
21284 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20 63 6f 6e compare */. con
21285 73 74 20 69 6e 74 20 65 73 63 20 20 20 20 20 20 st int esc
21286 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21287 20 54 68 65 20 65 73 63 61 70 65 20 63 68 61 72 The escape char
21288 61 63 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e acter */.){. in
21289 74 20 63 2c 20 63 32 3b 0a 20 20 69 6e 74 20 69 t c, c2;. int i
2128a 6e 76 65 72 74 3b 0a 20 20 69 6e 74 20 73 65 65 nvert;. int see
2128b 6e 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 n;. u8 matchOne
2128c 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f = pInfo->matchO
2128d 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c ne;. u8 matchAl
2128e 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 l = pInfo->match
2128f 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 All;. u8 matchS
21290 65 74 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 et = pInfo->matc
21291 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 hSet;. u8 noCas
21292 65 20 3d 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73 e = pInfo->noCas
21293 65 3b 20 0a 20 20 69 6e 74 20 70 72 65 76 45 73 e; . int prevEs
21294 63 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a cape = 0; /*
21295 20 54 72 75 65 20 69 66 20 74 68 65 20 70 72 65 True if the pre
21296 76 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 20 vious character
21297 77 61 73 20 27 65 73 63 61 70 65 27 20 2a 2f 0a was 'escape' */.
21298 0a 20 20 77 68 69 6c 65 28 20 28 63 20 3d 20 73 . while( (c = s
21299 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
2129a 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74 74 Pattern,0,&zPatt
2129b 65 72 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 ern))!=0 ){.
2129c 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20 if( !prevEscape
2129d 26 26 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20 29 && c==matchAll )
2129e 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 {. while( (
2129f 63 3d 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 c=sqlite3Utf8Rea
212a0 64 28 7a 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50 d(zPattern,0,&zP
212a1 61 74 74 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63 attern)) == matc
212a2 68 41 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 20 hAll.
212a3 20 20 20 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63 || c == matc
212a4 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 hOne ){.
212a5 69 66 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 if( c==matchOne
212a6 26 26 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 && sqlite3Utf8Re
212a7 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 ad(zString, 0, &
212a8 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 zString)==0 ){.
212a9 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
212aa 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
212ab 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 }. if( c
212ac 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
212ad 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d eturn 1;. }
212ae 65 6c 73 65 20 69 66 28 20 63 3d 3d 65 73 63 20 else if( c==esc
212af 29 7b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73 ){. c = s
212b0 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
212b1 50 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 Pattern, 0, &zPa
212b2 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 ttern);.
212b3 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 if( c==0 ){.
212b4 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
212b5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
212b6 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 }else if( c==mat
212b7 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20 chSet ){.
212b8 20 61 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20 assert( esc==0
212b9 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ); /* Th
212ba 69 73 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 is is GLOB, not
212bb 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 LIKE */.
212bc 61 73 73 65 72 74 28 20 6d 61 74 63 68 53 65 74 assert( matchSet
212bd 3c 30 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 <0x80 ); /* '['
212be 20 69 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 is a single-byt
212bf 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 e character */.
212c0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a while( *z
212c1 53 74 72 69 6e 67 20 26 26 20 70 61 74 74 65 72 String && patter
212c2 6e 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 nCompare(&zPatte
212c3 72 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 rn[-1],zString,p
212c4 49 6e 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a Info,esc)==0 ){.
212c5 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
212c6 5f 53 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69 _SKIP_UTF8(zStri
212c7 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ng);. }.
212c8 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a return *z
212c9 53 74 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 String!=0;.
212ca 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 }. while(
212cb 28 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 (c2 = sqlite3Utf
212cc 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 30 2c 8Read(zString,0,
212cd 26 7a 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b &zString))!=0 ){
212ce 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43 . if( noC
212cf 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ase ){.
212d0 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 GlogUpperToLowe
212d1 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 r(c2);.
212d2 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 GlogUpperToLowe
212d3 72 28 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 r(c);.
212d4 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26 while( c2 != 0 &
212d5 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 & c2 != c ){.
212d6 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 c2 = sq
212d7 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 lite3Utf8Read(zS
212d8 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69 tring, 0, &zStri
212d9 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ng);.
212da 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 GlogUpperToLowe
212db 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 r(c2);.
212dc 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
212dd 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c {. whil
212de 65 28 20 63 32 20 21 3d 20 30 20 26 26 20 63 32 e( c2 != 0 && c2
212df 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20 != c ){.
212e0 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 c2 = sqlite
212e1 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 3Utf8Read(zStrin
212e2 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b g, 0, &zString);
212e3 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
212e4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
212e5 66 28 20 63 32 3d 3d 30 20 29 20 72 65 74 75 72 f( c2==0 ) retur
212e6 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 n 0;. if(
212e7 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 patternCompare(
212e8 7a 50 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67 zPattern,zString
212e9 2c 70 49 6e 66 6f 2c 65 73 63 29 20 29 20 72 65 ,pInfo,esc) ) re
212ea 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a turn 1;. }.
212eb 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
212ec 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 }else if( !p
212ed 72 65 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d revEscape && c==
212ee 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 matchOne ){.
212ef 20 20 69 66 28 20 73 71 6c 69 74 65 33 55 74 66 if( sqlite3Utf
212f0 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 8Read(zString, 0
212f1 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 , &zString)==0 )
212f2 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
212f3 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
212f4 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 }else if( c==mat
212f5 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 69 chSet ){. i
212f6 6e 74 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a nt prior_c = 0;.
212f7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 73 assert( es
212f8 63 3d 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68 c==0 ); /* Th
212f9 69 73 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66 is only occurs f
212fa 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b or GLOB, not LIK
212fb 45 20 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20 E */. seen
212fc 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72 = 0;. inver
212fd 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d t = 0;. c =
212fe 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
212ff 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 (zString, 0, &zS
21300 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 tring);. if
21301 28 20 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( c==0 ) return
21302 30 3b 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 0;. c2 = sq
21303 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 lite3Utf8Read(zP
21304 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 attern, 0, &zPat
21305 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 tern);. if(
21306 20 63 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 c2=='^' ){.
21307 20 20 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a invert = 1;.
21308 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c c2 = sql
21309 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 ite3Utf8Read(zPa
2130a 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 ttern, 0, &zPatt
2130b 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ern);. }.
2130c 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 20 if( c2==']'
2130d 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 ){. if( c
2130e 3d 3d 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 31 ==']' ) seen = 1
2130f 3b 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 ;. c2 = s
21310 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
21311 50 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 Pattern, 0, &zPa
21312 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a ttern);. }.
21313 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 while( c2
21314 26 26 20 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20 && c2!=']' ){.
21315 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 2d if( c2=='-
21316 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d ' && zPattern[0]
21317 21 3d 27 5d 27 20 26 26 20 7a 50 61 74 74 65 72 !=']' && zPatter
21318 6e 5b 30 5d 21 3d 30 20 26 26 20 70 72 69 6f 72 n[0]!=0 && prior
21319 5f 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 _c>0 ){.
2131a 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 c2 = sqlite3Ut
2131b 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c f8Read(zPattern,
2131c 20 30 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 0, &zPattern);.
2131d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3e if( c>
2131e 3d 70 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63 =prior_c && c<=c
2131f 32 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 2 ) seen = 1;.
21320 20 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 prior_c
21321 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c = 0;. }el
21322 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 se{. if
21323 28 20 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20 ( c==c2 ){.
21324 20 20 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b seen = 1;
21325 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
21326 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d prior_c =
21327 20 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 c2;. }.
21328 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 c2 = sqli
21329 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 te3Utf8Read(zPat
2132a 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 tern, 0, &zPatte
2132b 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rn);. }.
2132c 20 20 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20 if( c2==0 ||
2132d 28 73 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d (seen ^ invert)=
2132e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
2132f 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a turn 0;. }.
21330 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73 }else if( es
21331 63 3d 3d 63 20 26 26 20 21 70 72 65 76 45 73 63 c==c && !prevEsc
21332 61 70 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65 ape ){. pre
21333 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20 vEscape = 1;.
21334 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32 }else{. c2
21335 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 = sqlite3Utf8Re
21336 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 ad(zString, 0, &
21337 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 zString);.
21338 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 if( noCase ){.
21339 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 GlogUpperT
2133a 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 oLower(c);.
2133b 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f GlogUpperToLo
2133c 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d wer(c2);. }
2133d 0a 20 20 20 20 20 20 69 66 28 20 63 21 3d 63 32 . if( c!=c2
2133e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
2133f 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 0;. }.
21340 20 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d prevEscape =
21341 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0;. }. }.
21342 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d return *zString=
21343 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 =0;.}../*.** Cou
21344 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 nt the number of
21345 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 times that the
21346 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f LIKE operator (o
21347 72 20 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a r GLOB which is.
21348 2a 2a 20 6a 75 73 74 20 61 20 76 61 72 69 61 74 ** just a variat
21349 69 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 ion of LIKE) get
2134a 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 s called. This
2134b 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 is used for test
2134c 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a ing.** only..*/.
2134d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
2134e 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
2134f 74 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 t sqlite3_like_c
21350 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 ount = 0;.#endif
21351 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 .../*.** Impleme
21352 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c ntation of the l
21353 69 6b 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69 ike() SQL functi
21354 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 on. This functi
21355 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a on implements.**
21356 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 the build-in LI
21357 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 KE operator. Th
21358 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
21359 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e to the function
2135a 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 is the.** patte
2135b 72 6e 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e rn and the secon
2135c 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 d argument is th
2135d 65 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 e string. So, t
2135e 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
2135f 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 s:.**.** A
21360 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 LIKE B.**.** is
21361 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
21362 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a like(B,A)..**.**
21363 20 54 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74 This same funct
21364 69 6f 6e 20 28 77 69 74 68 20 61 20 64 69 66 66 ion (with a diff
21365 65 72 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66 erent compareInf
21366 6f 20 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d o structure) com
21367 70 75 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f putes.** the GLO
21368 42 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 B operator..*/.s
21369 74 61 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46 tatic void likeF
2136a 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
2136b 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
2136c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 . int argc, .
2136d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
2136e 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 *argv.){. const
2136f 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
21370 7a 41 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65 zA, *zB;. int e
21371 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 73 71 6c scape = 0;. sql
21372 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 ite3 *db = sqlit
21373 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
21374 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a ndle(context);..
21375 20 20 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 zB = sqlite3_v
21376 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
21377 5d 29 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74 ]);. zA = sqlit
21378 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
21379 67 76 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 gv[1]);.. /* Li
2137a 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f mit the length o
2137b 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c f the LIKE or GL
2137c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76 OB pattern to av
2137d 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a oid problems. *
2137e 2a 20 6f 66 20 64 65 65 70 20 72 65 63 75 72 73 * of deep recurs
2137f 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61 ion and N*N beha
21380 76 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43 vior in patternC
21381 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 ompare().. */.
21382 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c if( sqlite3_val
21383 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d ue_bytes(argv[0]
21384 29 20 3e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e ) >. db->
21385 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
21386 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e MIT_LIKE_PATTERN
21387 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 _LENGTH] ){.
21388 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
21389 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c rror(context, "L
2138a 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 IKE or GLOB patt
2138b 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 ern too complex"
2138c 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 , -1);. retur
2138d 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 n;. }. assert(
2138e 20 7a 42 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c zB==sqlite3_val
2138f 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
21390 20 29 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 ); /* Encoding
21391 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 20 did not change
21392 2a 2f 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d */.. if( argc==
21393 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 3 ){. /* The
21394 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72 escape character
21395 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e string must con
21396 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65 sist of a single
21397 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 UTF-8 character
21398 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 .. ** Otherwi
21399 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 se, return an er
2139a 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ror.. */.
2139b 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
2139c 68 61 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69 har *zEsc = sqli
2139d 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
2139e 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 rgv[2]);. if(
2139f 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74 75 72 zEsc==0 ) retur
213a0 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 n;. if( sqlit
213a1 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 28 63 e3Utf8CharLen((c
213a2 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d har*)zEsc, -1)!=
213a3 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 1 ){. sqlit
213a4 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
213a5 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 context, .
213a6 20 20 20 20 22 45 53 43 41 50 45 20 65 78 70 72 "ESCAPE expr
213a7 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 ession must be a
213a8 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65 single characte
213a9 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 r", -1);. r
213aa 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 eturn;. }.
213ab 20 65 73 63 61 70 65 20 3d 20 73 71 6c 69 74 65 escape = sqlite
213ac 33 55 74 66 38 52 65 61 64 28 7a 45 73 63 2c 20 3Utf8Read(zEsc,
213ad 30 2c 20 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20 0, &zEsc);. }.
213ae 20 69 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b if( zA && zB ){
213af 0a 20 20 20 20 73 74 72 75 63 74 20 63 6f 6d 70 . struct comp
213b0 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d areInfo *pInfo =
213b1 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 sqlite3_user_da
213b2 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66 ta(context);.#if
213b3 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
213b4 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 sqlite3_like
213b5 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
213b6 0a 20 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 . . sqlite
213b7 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 3_result_int(con
213b8 74 65 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d text, patternCom
213b9 70 61 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e pare(zB, zA, pIn
213ba 66 6f 2c 20 65 73 63 61 70 65 29 29 3b 0a 20 20 fo, escape));.
213bb 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 }.}../*.** Imple
213bc 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
213bd 20 4e 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e NULLIF(x,y) fun
213be 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 ction. The resu
213bf 6c 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0a lt is the first.
213c0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 ** argument if t
213c1 68 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 he arguments are
213c2 20 64 69 66 66 65 72 65 6e 74 2e 20 20 54 68 65 different. The
213c3 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 result is NULL
213c4 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 if the.** argume
213c5 6e 74 73 20 61 72 65 20 65 71 75 61 6c 20 74 6f nts are equal to
213c6 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a each other..*/.
213c7 73 74 61 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c static void null
213c8 69 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 ifFunc(. sqlite
213c9 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
213ca 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
213cb 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
213cc 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 43 ue **argv.){. C
213cd 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 ollSeq *pColl =
213ce 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f sqlite3GetFuncCo
213cf 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a llSeq(context);.
213d0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
213d1 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 ER(NotUsed);. i
213d2 66 28 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d f( sqlite3MemCom
213d3 70 61 72 65 28 61 72 67 76 5b 30 5d 2c 20 61 72 pare(argv[0], ar
213d4 67 76 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30 gv[1], pColl)!=0
213d5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
213d6 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e result_value(con
213d7 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a text, argv[0]);.
213d8 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 }.}../*.** Imp
213d9 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
213da 68 65 20 56 45 52 53 49 4f 4e 28 2a 29 20 66 75 he VERSION(*) fu
213db 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 nction. The res
213dc 75 6c 74 20 69 73 20 74 68 65 20 76 65 72 73 69 ult is the versi
213dd 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c on.** of the SQL
213de 69 74 65 20 6c 69 62 72 61 72 79 20 74 68 61 74 ite library that
213df 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a is running..*/.
213e0 73 74 61 74 69 63 20 76 6f 69 64 20 76 65 72 73 static void vers
213e1 69 6f 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 ionFunc(. sqlit
213e2 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
213e3 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 ext,. int NotUs
213e4 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ed,. sqlite3_va
213e5 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 lue **NotUsed2.)
213e6 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
213e7 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e ETER2(NotUsed, N
213e8 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 otUsed2);. sqli
213e9 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
213ea 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 context, sqlite3
213eb 5f 76 65 72 73 69 6f 6e 2c 20 2d 31 2c 20 53 51 _version, -1, SQ
213ec 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a LITE_STATIC);.}.
213ed 0a 2f 2a 20 41 72 72 61 79 20 66 6f 72 20 63 6f ./* Array for co
213ee 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 68 61 nverting from ha
213ef 6c 66 2d 62 79 74 65 73 20 28 6e 79 62 62 6c 65 lf-bytes (nybble
213f0 73 29 20 69 6e 74 6f 20 41 53 43 49 49 20 68 65 s) into ASCII he
213f1 78 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f 0a x.** digits. */.
213f2 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
213f3 72 20 68 65 78 64 69 67 69 74 73 5b 5d 20 3d 20 r hexdigits[] =
213f4 7b 0a 20 20 27 30 27 2c 20 27 31 27 2c 20 27 32 {. '0', '1', '2
213f5 27 2c 20 27 33 27 2c 20 27 34 27 2c 20 27 35 27 ', '3', '4', '5'
213f6 2c 20 27 36 27 2c 20 27 37 27 2c 0a 20 20 27 38 , '6', '7',. '8
213f7 27 2c 20 27 39 27 2c 20 27 41 27 2c 20 27 42 27 ', '9', 'A', 'B'
213f8 2c 20 27 43 27 2c 20 27 44 27 2c 20 27 45 27 2c , 'C', 'D', 'E',
213f9 20 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 'F' .};../*.**
213fa 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 54 EXPERIMENTAL - T
213fb 68 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f 66 his is not an of
213fc 66 69 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e 2e ficial function.
213fd 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 The interface
213fe 6d 61 79 0a 2a 2a 20 63 68 61 6e 67 65 2e 20 20 may.** change.
213ff 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 This function ma
21400 79 20 64 69 73 61 70 70 65 61 72 2e 20 20 44 6f y disappear. Do
21401 20 6e 6f 74 20 77 72 69 74 65 20 63 6f 64 65 20 not write code
21402 74 68 61 74 20 64 65 70 65 6e 64 73 0a 2a 2a 20 that depends.**
21403 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e on this function
21404 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ..**.** Implemen
21405 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 51 55 tation of the QU
21406 4f 54 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 OTE() function.
21407 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 This function t
21408 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a akes a single.**
21409 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 argument. If t
2140a 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e he argument is n
2140b 75 6d 65 72 69 63 2c 20 74 68 65 20 72 65 74 75 umeric, the retu
2140c 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 rn value is the
2140d 73 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20 61 same as.** the a
2140e 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 rgument. If the
2140f 20 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c argument is NUL
21410 4c 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 L, the return va
21411 6c 75 65 20 69 73 20 74 68 65 20 73 74 72 69 6e lue is the strin
21412 67 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f 74 g.** "NULL". Ot
21413 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 72 67 herwise, the arg
21414 75 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73 65 ument is enclose
21415 64 20 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f 74 d in single quot
21416 65 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67 6c es with.** singl
21417 65 2d 71 75 6f 74 65 20 65 73 63 61 70 65 73 2e e-quote escapes.
21418 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
21419 71 75 6f 74 65 46 75 6e 63 28 73 71 6c 69 74 65 quoteFunc(sqlite
2141a 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
2141b 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 xt, int argc, sq
2141c 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
2141d 67 76 29 7b 0a 20 20 69 66 28 20 61 72 67 63 3c gv){. if( argc<
2141e 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 77 1 ) return;. sw
2141f 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 itch( sqlite3_va
21420 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d lue_type(argv[0]
21421 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 ) ){. case SQ
21422 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 LITE_NULL: {.
21423 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
21424 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
21425 22 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 4c 49 54 "NULL", 4, SQLIT
21426 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 E_STATIC);.
21427 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
21428 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e case SQLITE_IN
21429 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 TEGER:. case
2142a 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a SQLITE_FLOAT: {.
2142b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 sqlite3_re
2142c 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 sult_value(conte
2142d 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 xt, argv[0]);.
2142e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
2142f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
21430 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 63 _BLOB: {. c
21431 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a har *zText = 0;.
21432 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 char const
21433 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 *zBlob = sqlite
21434 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 3_value_blob(arg
21435 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 v[0]);. int
21436 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 nBlob = sqlite3
21437 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 _value_bytes(arg
21438 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 v[0]);. ass
21439 65 72 74 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c 69 ert( zBlob==sqli
2143a 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 te3_value_blob(a
2143b 72 67 76 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e 6f rgv[0]) ); /* No
2143c 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 encoding change
2143d 20 2a 2f 0a 20 20 20 20 20 20 7a 54 65 78 74 20 */. zText
2143e 3d 20 28 63 68 61 72 20 2a 29 63 6f 6e 74 65 78 = (char *)contex
2143f 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c tMalloc(context,
21440 20 28 32 2a 28 69 36 34 29 6e 42 6c 6f 62 29 2b (2*(i64)nBlob)+
21441 34 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 7a 4); . if( z
21442 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Text ){.
21443 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 int i;. f
21444 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b or(i=0; i<nBlob;
21445 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 i++){.
21446 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20 zText[(i*2)+2]
21447 3d 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c = hexdigits[(zBl
21448 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b ob[i]>>4)&0x0F];
21449 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 . zText
2144a 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64 [(i*2)+3] = hexd
2144b 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29 igits[(zBlob[i])
2144c 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 &0x0F];.
2144d 7d 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b }. zText[
2144e 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27 (nBlob*2)+2] = '
2144f 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 \'';. zTe
21450 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20 xt[(nBlob*2)+3]
21451 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 = '\0';.
21452 7a 54 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a zText[0] = 'X';.
21453 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d zText[1]
21454 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 = '\'';.
21455 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
21456 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54 text(context, zT
21457 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f ext, -1, SQLITE_
21458 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 TRANSIENT);.
21459 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
2145a 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7d (zText);. }
2145b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
2145c 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
2145d 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 ITE_TEXT: {.
2145e 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20 int i,j;.
2145f 20 75 36 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f u64 n;. co
21460 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
21461 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74 65 r *zArg = sqlite
21462 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
21463 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 63 68 61 v[0]);. cha
21464 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28 r *z;.. if(
21465 20 7a 41 72 67 3d 3d 30 20 29 20 72 65 74 75 72 zArg==0 ) retur
21466 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 n;. for(i=0
21467 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20 , n=0; zArg[i];
21468 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b 69 i++){ if( zArg[i
21469 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d ]=='\'' ) n++; }
2146a 0a 20 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65 . z = conte
2146b 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 xtMalloc(context
2146c 2c 20 28 28 69 36 34 29 69 29 2b 28 28 69 36 34 , ((i64)i)+((i64
2146d 29 6e 29 2b 33 29 3b 0a 20 20 20 20 20 20 69 66 )n)+3);. if
2146e 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a ( z ){. z
2146f 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 [0] = '\'';.
21470 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31 for(i=0, j=1
21471 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b ; zArg[i]; i++){
21472 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b . z[j++
21473 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 ] = zArg[i];.
21474 20 20 20 20 20 20 20 69 66 28 20 7a 41 72 67 5b if( zArg[
21475 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 i]=='\'' ){.
21476 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d z[j++] =
21477 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 20 '\'';.
21478 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
21479 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c z[j++] = '\
2147a 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d '';. z[j]
2147b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 = 0;. sq
2147c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2147d 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c t(context, z, j,
2147e 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a sqlite3_free);.
2147f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
21480 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 }.}../*.** The h
21481 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 ex() function.
21482 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 61 72 Interpret the ar
21483 67 75 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f 62 gument as a blob
21484 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68 . Return.** a h
21485 65 78 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65 exadecimal rende
21486 72 69 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f ring as text..*/
21487 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 78 .static void hex
21488 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
21489 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
2148a 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
2148b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
2148c 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c argv.){. int i,
2148d 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 n;. const unsi
2148e 67 6e 65 64 20 63 68 61 72 20 2a 70 42 6c 6f 62 gned char *pBlob
2148f 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65 78 2c 20 ;. char *zHex,
21490 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 *z;. assert( ar
21491 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 gc==1 );. UNUSE
21492 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 D_PARAMETER(argc
21493 29 3b 0a 20 20 70 42 6c 6f 62 20 3d 20 73 71 6c );. pBlob = sql
21494 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 ite3_value_blob(
21495 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 argv[0]);. n =
21496 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
21497 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 tes(argv[0]);.
21498 61 73 73 65 72 74 28 20 70 42 6c 6f 62 3d 3d 73 assert( pBlob==s
21499 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f qlite3_value_blo
2149a 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f b(argv[0]) ); /
2149b 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 * No encoding ch
2149c 61 6e 67 65 20 2a 2f 0a 20 20 7a 20 3d 20 7a 48 ange */. z = zH
2149d 65 78 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c ex = contextMall
2149e 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 oc(context, ((i6
2149f 34 29 6e 29 2a 32 20 2b 20 31 29 3b 0a 20 20 69 4)n)*2 + 1);. i
214a0 66 28 20 7a 48 65 78 20 29 7b 0a 20 20 20 20 66 f( zHex ){. f
214a1 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b or(i=0; i<n; i++
214a2 2c 20 70 42 6c 6f 62 2b 2b 29 7b 0a 20 20 20 20 , pBlob++){.
214a3 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
214a4 63 20 3d 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 c = *pBlob;.
214a5 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69 *(z++) = hexdi
214a6 67 69 74 73 5b 28 63 3e 3e 34 29 26 30 78 66 5d gits[(c>>4)&0xf]
214a7 3b 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d ;. *(z++) =
214a8 20 68 65 78 64 69 67 69 74 73 5b 63 26 30 78 66 hexdigits[c&0xf
214a9 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 20 ];. }. *z
214aa 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 0;. sqlite3
214ab 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
214ac 74 65 78 74 2c 20 7a 48 65 78 2c 20 6e 2a 32 2c text, zHex, n*2,
214ad 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a sqlite3_free);.
214ae 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 }.}../*.** The
214af 20 7a 65 72 6f 62 6c 6f 62 28 4e 29 20 66 75 6e zeroblob(N) fun
214b0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 ction returns a
214b1 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62 zero-filled blob
214b2 20 6f 66 20 73 69 7a 65 20 4e 20 62 79 74 65 73 of size N bytes
214b3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
214b4 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 28 0a 20 zeroblobFunc(.
214b5 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
214b6 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
214b7 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
214b8 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
214b9 0a 20 20 69 36 34 20 6e 3b 0a 20 20 61 73 73 65 . i64 n;. asse
214ba 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 rt( argc==1 );.
214bb 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
214bc 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73 R(argc);. n = s
214bd 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
214be 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 64(argv[0]);. i
214bf 66 28 20 6e 3e 53 51 4c 49 54 45 5f 4d 41 58 5f f( n>SQLITE_MAX_
214c0 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71 LENGTH ){. sq
214c1 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
214c2 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 or_toobig(contex
214c3 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
214c4 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
214c5 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 zeroblob(context
214c6 2c 20 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a , n);. }.}../*.
214c7 2a 2a 20 54 68 65 20 72 65 70 6c 61 63 65 28 29 ** The replace()
214c8 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 72 65 function. Thre
214c9 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 e arguments are
214ca 61 6c 6c 20 73 74 72 69 6e 67 73 3a 20 63 61 6c all strings: cal
214cb 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c 20 42 2c 20 l.** them A, B,
214cc 61 6e 64 20 43 2e 20 54 68 65 20 72 65 73 75 6c and C. The resul
214cd 74 20 69 73 20 61 6c 73 6f 20 61 20 73 74 72 69 t is also a stri
214ce 6e 67 20 77 68 69 63 68 20 69 73 20 64 65 72 69 ng which is deri
214cf 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 79 ved.** from A by
214d0 20 72 65 70 6c 61 63 69 6e 67 20 65 76 65 72 79 replacing every
214d1 20 6f 63 63 75 72 61 6e 63 65 20 6f 66 20 42 20 occurance of B
214d2 77 69 74 68 20 43 2e 20 20 54 68 65 20 6d 61 74 with C. The mat
214d3 63 68 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 78 ch.** must be ex
214d4 61 63 74 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 20 act. Collating
214d5 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 6e 6f sequences are no
214d6 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 t used..*/.stati
214d7 63 20 76 6f 69 64 20 72 65 70 6c 61 63 65 46 75 c void replaceFu
214d8 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
214d9 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
214da 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
214db 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
214dc 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e gv.){. const un
214dd 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 signed char *zSt
214de 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 r; /* The
214df 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 41 20 input string A
214e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 */. const unsig
214e1 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74 74 65 ned char *zPatte
214e2 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 70 61 rn; /* The pa
214e3 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42 20 2a ttern string B *
214e4 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e /. const unsign
214e5 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b 20 20 ed char *zRep;
214e6 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 70 /* The rep
214e7 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e 67 20 lacement string
214e8 43 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 C */. unsigned
214e9 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 char *zOut;
214ea 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
214eb 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 output */. int
214ec 6e 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 nStr;
214ed 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
214ee 7a 53 74 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 zStr */. int nP
214ef 61 74 74 65 72 6e 3b 20 20 20 20 20 20 20 20 20 attern;
214f0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 50 /* Size of zP
214f1 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 attern */. int
214f2 6e 52 65 70 3b 20 20 20 20 20 20 20 20 20 20 20 nRep;
214f3 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
214f4 7a 52 65 70 20 2a 2f 0a 20 20 69 36 34 20 6e 4f zRep */. i64 nO
214f5 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
214f6 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 69 /* Maximum si
214f7 7a 65 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 20 ze of zOut */.
214f8 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 20 int loopLimit;
214f9 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 /* Last
214fa 20 7a 53 74 72 5b 5d 20 74 68 61 74 20 6d 69 67 zStr[] that mig
214fb 68 74 20 6d 61 74 63 68 20 7a 50 61 74 74 65 72 ht match zPatter
214fc 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 n[] */. int i,
214fd 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 j;
214fe 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
214ff 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 rs */.. assert(
21500 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 55 4e argc==3 );. UN
21501 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 USED_PARAMETER(a
21502 72 67 63 29 3b 0a 20 20 7a 53 74 72 20 3d 20 73 rgc);. zStr = s
21503 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
21504 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 t(argv[0]);. if
21505 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65 74 75 ( zStr==0 ) retu
21506 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73 71 6c rn;. nStr = sql
21507 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
21508 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 (argv[0]);. ass
21509 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c 69 74 ert( zStr==sqlit
2150a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
2150b 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f gv[0]) ); /* No
2150c 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 encoding change
2150d 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e 20 3d */. zPattern =
2150e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
2150f 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 ext(argv[1]);.
21510 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d 30 20 if( zPattern==0
21511 7c 7c 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d 3d || zPattern[0]==
21512 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 50 0 ) return;. nP
21513 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33 attern = sqlite3
21514 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 _value_bytes(arg
21515 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 v[1]);. assert(
21516 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71 6c 69 74 zPattern==sqlit
21517 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
21518 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f gv[1]) ); /* No
21519 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 encoding change
2151a 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20 73 71 6c */. zRep = sql
2151b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
2151c 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69 66 28 20 argv[2]);. if(
2151d 7a 52 65 70 3d 3d 30 20 29 20 72 65 74 75 72 6e zRep==0 ) return
2151e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71 6c 69 74 ;. nRep = sqlit
2151f 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
21520 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73 73 65 72 rgv[2]);. asser
21521 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69 74 65 33 t( zRep==sqlite3
21522 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
21523 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75 74 20 3d [2]) );. nOut =
21524 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20 61 73 73 nStr + 1;. ass
21525 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c 49 54 45 ert( nOut<SQLITE
21526 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 _MAX_LENGTH );.
21527 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65 78 74 4d zOut = contextM
21528 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 alloc(context, (
21529 69 36 34 29 6e 4f 75 74 29 3b 0a 20 20 69 66 28 i64)nOut);. if(
2152a 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 zOut==0 ){.
2152b 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f return;. }. lo
2152c 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74 72 20 2d opLimit = nStr -
2152d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a 20 20 66 nPattern; . f
2152e 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f or(i=j=0; i<=loo
2152f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 pLimit; i++){.
21530 20 20 69 66 28 20 7a 53 74 72 5b 69 5d 21 3d 7a if( zStr[i]!=z
21531 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65 Pattern[0] || me
21532 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d 2c 20 7a mcmp(&zStr[i], z
21533 50 61 74 74 65 72 6e 2c 20 6e 50 61 74 74 65 72 Pattern, nPatter
21534 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 n) ){. zOut
21535 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b [j++] = zStr[i];
21536 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
21537 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20 20 20 20 u8 *zOld;.
21538 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
21539 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f sqlite3_context_
2153a 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 db_handle(contex
2153b 74 29 3b 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b t);. nOut +
2153c 3d 20 6e 52 65 70 20 2d 20 6e 50 61 74 74 65 72 = nRep - nPatter
2153d 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 75 n;. if( nOu
2153e 74 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 t>=db->aLimit[SQ
2153f 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
21540 48 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 H] ){. sq
21541 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
21542 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 or_toobig(contex
21543 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 t);. sqli
21544 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4f te3DbFree(db, zO
21545 75 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 ut);. ret
21546 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 urn;. }.
21547 20 20 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a zOld = zOut;.
21548 20 20 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c zOut = sql
21549 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 ite3_realloc(zOu
2154a 74 2c 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 t, (int)nOut);.
2154b 20 20 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 if( zOut==0
2154c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
2154d 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2154e 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b _nomem(context);
2154f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21550 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 6c 64 29 DbFree(db, zOld)
21551 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
21552 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
21553 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c memcpy(&zOut[j],
21554 20 7a 52 65 70 2c 20 6e 52 65 70 29 3b 0a 20 20 zRep, nRep);.
21555 20 20 20 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a 20 j += nRep;.
21556 20 20 20 20 20 69 20 2b 3d 20 6e 50 61 74 74 65 i += nPatte
21557 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn-1;. }. }.
21558 20 20 61 73 73 65 72 74 28 20 6a 2b 6e 53 74 72 assert( j+nStr
21559 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20 20 -i+1==nOut );.
2155a 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c memcpy(&zOut[j],
2155b 20 26 7a 53 74 72 5b 69 5d 2c 20 6e 53 74 72 2d &zStr[i], nStr-
2155c 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 20 i);. j += nStr
2155d 2d 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 6a - i;. assert( j
2155e 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20 7a 4f 75 74 <=nOut );. zOut
2155f 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 [j] = 0;. sqlit
21560 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 e3_result_text(c
21561 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a ontext, (char*)z
21562 4f 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f Out, j, sqlite3_
21563 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 free);.}../*.**
21564 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
21565 66 20 74 68 65 20 54 52 49 4d 28 29 2c 20 4c 54 f the TRIM(), LT
21566 52 49 4d 28 29 2c 20 61 6e 64 20 52 54 52 49 4d RIM(), and RTRIM
21567 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a () functions..**
21568 20 54 68 65 20 75 73 65 72 64 61 74 61 20 69 73 The userdata is
21569 20 30 78 31 20 66 6f 72 20 6c 65 66 74 20 74 72 0x1 for left tr
2156a 69 6d 2c 20 30 78 32 20 66 6f 72 20 72 69 67 68 im, 0x2 for righ
2156b 74 20 74 72 69 6d 2c 20 30 78 33 20 66 6f 72 20 t trim, 0x3 for
2156c 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 both..*/.static
2156d 76 6f 69 64 20 74 72 69 6d 46 75 6e 63 28 0a 20 void trimFunc(.
2156e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
2156f 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
21570 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
21571 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
21572 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 . const unsigne
21573 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20 d char *zIn;
21574 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 /* Input st
21575 72 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ring */. const
21576 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
21577 43 68 61 72 53 65 74 3b 20 20 20 20 2f 2a 20 53 CharSet; /* S
21578 65 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 et of characters
21579 20 74 6f 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e to trim */. in
2157a 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20 t nIn;
2157b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2157c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
2157d 65 73 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0a 20 es in input */.
2157e 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 int flags;
2157f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21580 20 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 66 /* 1: trimlef
21581 74 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74 20 t 2: trimright
21582 20 33 3a 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 3: trim */. in
21583 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
21584 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21585 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
21586 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
21587 61 72 20 2a 61 4c 65 6e 3b 20 20 20 20 20 20 20 ar *aLen;
21588 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
21589 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63 74 of each charact
2158a 65 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a er in zCharSet *
2158b 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
2158c 72 20 2a 2a 61 7a 43 68 61 72 3b 20 20 20 20 20 r **azChar;
2158d 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 76 69 64 /* Individ
2158e 75 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 69 ual characters i
2158f 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 n zCharSet */.
21590 69 6e 74 20 6e 43 68 61 72 3b 20 20 20 20 20 20 int nChar;
21591 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21592 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
21593 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 haracters in zCh
21594 61 72 53 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 arSet */.. if(
21595 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
21596 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c pe(argv[0])==SQL
21597 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 ITE_NULL ){.
21598 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a 49 return;. }. zI
21599 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 n = sqlite3_valu
2159a 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b e_text(argv[0]);
2159b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20 . if( zIn==0 )
2159c 72 65 74 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d 20 return;. nIn =
2159d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
2159e 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 tes(argv[0]);.
2159f 61 73 73 65 72 74 28 20 7a 49 6e 3d 3d 73 71 6c assert( zIn==sql
215a0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
215a1 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 argv[0]) );. if
215a2 28 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20 ( argc==1 ){.
215a3 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e static const un
215a4 73 69 67 6e 65 64 20 63 68 61 72 20 6c 65 6e 4f signed char lenO
215a5 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20 20 ne[] = { 1 };.
215a6 20 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 static unsigne
215a7 64 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 d char * const a
215a8 7a 4f 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a 29 zOne[] = { (u8*)
215a9 22 20 22 20 7d 3b 0a 20 20 20 20 6e 43 68 61 72 " " };. nChar
215aa 20 3d 20 31 3b 0a 20 20 20 20 61 4c 65 6e 20 3d = 1;. aLen =
215ab 20 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20 (u8*)lenOne;.
215ac 20 20 61 7a 43 68 61 72 20 3d 20 28 75 6e 73 69 azChar = (unsi
215ad 67 6e 65 64 20 63 68 61 72 20 2a 2a 29 61 7a 4f gned char **)azO
215ae 6e 65 3b 0a 20 20 20 20 7a 43 68 61 72 53 65 74 ne;. zCharSet
215af 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 0;. }else if
215b0 28 20 28 7a 43 68 61 72 53 65 74 20 3d 20 73 71 ( (zCharSet = sq
215b1 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
215b2 28 61 72 67 76 5b 31 5d 29 29 3d 3d 30 20 29 7b (argv[1]))==0 ){
215b3 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
215b4 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 else{. const
215b5 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
215b6 3b 0a 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 ;. for(z=zCha
215b7 72 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a rSet, nChar=0; *
215b8 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 z; nChar++){.
215b9 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 SQLITE_SKIP_U
215ba 54 46 38 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 TF8(z);. }.
215bb 20 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b if( nChar>0 ){
215bc 0a 20 20 20 20 20 20 61 7a 43 68 61 72 20 3d 20 . azChar =
215bd 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f contextMalloc(co
215be 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 43 68 ntext, ((i64)nCh
215bf 61 72 29 2a 28 73 69 7a 65 6f 66 28 63 68 61 72 ar)*(sizeof(char
215c0 2a 29 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 66 *)+1));. if
215c1 28 20 61 7a 43 68 61 72 3d 3d 30 20 29 7b 0a 20 ( azChar==0 ){.
215c2 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
215c3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4c 65 }. aLe
215c4 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 n = (unsigned ch
215c5 61 72 2a 29 26 61 7a 43 68 61 72 5b 6e 43 68 61 ar*)&azChar[nCha
215c6 72 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 7a 3d r];. for(z=
215c7 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d zCharSet, nChar=
215c8 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0; *z; nChar++){
215c9 0a 20 20 20 20 20 20 20 20 61 7a 43 68 61 72 5b . azChar[
215ca 6e 43 68 61 72 5d 20 3d 20 28 75 6e 73 69 67 6e nChar] = (unsign
215cb 65 64 20 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 ed char *)z;.
215cc 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 SQLITE_SKIP
215cd 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 _UTF8(z);.
215ce 20 20 61 4c 65 6e 5b 6e 43 68 61 72 5d 20 3d 20 aLen[nChar] =
215cf 7a 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 z - azChar[nChar
215d0 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d ];. }. }
215d1 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 72 . }. if( nChar
215d2 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 >0 ){. flags
215d3 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f = SQLITE_PTR_TO_
215d4 49 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65 72 INT(sqlite3_user
215d5 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29 3b _data(context));
215d6 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 . if( flags &
215d7 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 1 ){. whil
215d8 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 e( nIn>0 ){.
215d9 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 int len;.
215da 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
215db 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nChar; i++){.
215dc 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65 len = aLe
215dd 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 n[i];.
215de 69 66 28 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20 if( memcmp(zIn,
215df 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d azChar[i], len)=
215e0 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
215e1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
215e2 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 ( i>=nChar ) bre
215e3 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 20 ak;. zIn
215e4 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 += len;.
215e5 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 nIn -= len;.
215e6 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
215e7 28 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a 20 ( flags & 2 ){.
215e8 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e while( nIn>
215e9 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 0 ){. int
215ea 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 66 6f len;. fo
215eb 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20 r(i=0; i<nChar;
215ec 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 i++){.
215ed 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20 len = aLen[i];.
215ee 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e if( len
215ef 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28 <=nIn && memcmp(
215f0 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c 61 7a &zIn[nIn-len],az
215f1 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d 30 20 Char[i],len)==0
215f2 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ) break;.
215f3 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 }. if( i
215f4 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b 3b >=nChar ) break;
215f5 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20 . nIn -=
215f6 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 len;. }.
215f7 20 7d 0a 20 20 20 20 69 66 28 20 7a 43 68 61 72 }. if( zChar
215f8 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Set ){. sql
215f9 69 74 65 33 5f 66 72 65 65 28 61 7a 43 68 61 72 ite3_free(azChar
215fa 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 );. }. }. s
215fb 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
215fc 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 xt(context, (cha
215fd 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51 4c r*)zIn, nIn, SQL
215fe 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
215ff 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }...#ifdef SQLIT
21600 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20 E_SOUNDEX./*.**
21601 43 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75 6e Compute the soun
21602 64 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 dex encoding of
21603 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74 69 a word..*/.stati
21604 63 20 76 6f 69 64 20 73 6f 75 6e 64 65 78 46 75 c void soundexFu
21605 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
21606 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
21607 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
21608 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
21609 67 76 0a 29 7b 0a 20 20 63 68 61 72 20 7a 52 65 gv.){. char zRe
2160a 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f 6e 73 74 sult[8];. const
2160b 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69 6e 74 20 u8 *zIn;. int
2160c 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69 63 20 63 i, j;. static c
2160d 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
2160e 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 ar iCode[] = {.
2160f 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0
21610 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
21611 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
21612 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 0,. 0, 0, 0,
21613 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
21614 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
21615 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 0, 0,. 0, 0,
21616 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
21617 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
21618 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 , 0, 0, 0,. 0
21619 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
2161a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
2161b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 0, 0, 0, 0, 0,.
2161c 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33 0, 0, 1, 2, 3
2161d 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c , 0, 1, 2, 0, 0,
2161e 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20 2, 2, 4, 5, 5,
2161f 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 20 0,. 1, 2, 6,
21620 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2, 3, 0, 1, 0, 2
21621 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c , 0, 2, 0, 0, 0,
21622 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 0, 0,. 0, 0,
21623 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 1, 2, 3, 0, 1,
21624 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2, 0, 0, 2, 2, 4
21625 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31 , 5, 5, 0,. 1
21626 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c , 2, 6, 2, 3, 0,
21627 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20 1, 0, 2, 0, 2,
21628 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 0, 0, 0, 0, 0,.
21629 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 };. assert( ar
2162a 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49 6e 20 3d gc==1 );. zIn =
2162b 20 28 75 38 2a 29 73 71 6c 69 74 65 33 5f 76 61 (u8*)sqlite3_va
2162c 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d lue_text(argv[0]
2162d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 );. if( zIn==0
2162e 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 22 22 3b ) zIn = (u8*)"";
2162f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 49 6e 5b . for(i=0; zIn[
21630 69 5d 20 26 26 20 21 69 73 61 6c 70 68 61 28 7a i] && !isalpha(z
21631 49 6e 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 In[i]); i++){}.
21632 20 69 66 28 20 7a 49 6e 5b 69 5d 20 29 7b 0a 20 if( zIn[i] ){.
21633 20 20 20 75 38 20 70 72 65 76 63 6f 64 65 20 3d u8 prevcode =
21634 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 iCode[zIn[i]&0x
21635 37 66 5d 3b 0a 20 20 20 20 7a 52 65 73 75 6c 74 7f];. zResult
21636 5b 30 5d 20 3d 20 74 6f 75 70 70 65 72 28 7a 49 [0] = toupper(zI
21637 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28 6a n[i]);. for(j
21638 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b 69 =1; j<4 && zIn[i
21639 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 ]; i++){. i
2163a 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65 5b nt code = iCode[
2163b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 20 zIn[i]&0x7f];.
2163c 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20 29 if( code>0 )
2163d 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f {. if( co
2163e 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b 0a de!=prevcode ){.
2163f 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63 6f prevco
21640 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20 20 de = code;.
21641 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b zResult[j++
21642 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b 0a ] = code + '0';.
21643 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
21644 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
21645 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20 20 revcode = 0;.
21646 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77 }. }. w
21647 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20 20 hile( j<4 ){.
21648 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 zResult[j++]
21649 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20 = '0';. }.
2164a 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30 3b zResult[j] = 0;
2164b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 . sqlite3_res
2164c 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
2164d 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53 51 , zResult, 4, SQ
2164e 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b LITE_TRANSIENT);
2164f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
21650 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
21651 74 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30 30 t(context, "?000
21652 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 ", 4, SQLITE_STA
21653 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 TIC);. }.}.#end
21654 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
21655 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
21656 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66 ENSION./*.** A f
21657 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61 unction that loa
21658 64 73 20 61 20 73 68 61 72 65 64 2d 6c 69 62 72 ds a shared-libr
21659 61 72 79 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 ary extension th
2165a 65 6e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e en returns NULL.
2165b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
2165c 6c 6f 61 64 45 78 74 28 73 71 6c 69 74 65 33 5f loadExt(sqlite3_
2165d 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
2165e 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 , int argc, sqli
2165f 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
21660 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 ){. const char
21661 2a 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 *zFile = (const
21662 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 char *)sqlite3_v
21663 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
21664 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 ]);. const char
21665 20 2a 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69 74 *zProc;. sqlit
21666 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 e3 *db = sqlite3
21667 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
21668 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 le(context);. c
21669 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 har *zErrMsg = 0
2166a 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 ;.. if( argc==2
2166b 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 ){. zProc =
2166c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 (const char *)sq
2166d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
2166e 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c (argv[1]);. }el
2166f 73 65 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 se{. zProc =
21670 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46 69 0;. }. if( zFi
21671 6c 65 20 26 26 20 73 71 6c 69 74 65 33 5f 6c 6f le && sqlite3_lo
21672 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62 2c ad_extension(db,
21673 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 26 zFile, zProc, &
21674 7a 45 72 72 4d 73 67 29 20 29 7b 0a 20 20 20 20 zErrMsg) ){.
21675 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
21676 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 rror(context, zE
21677 72 72 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20 20 rrMsg, -1);.
21678 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 sqlite3_free(zEr
21679 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e rMsg);. }.}.#en
2167a 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 dif.../*.** An i
2167b 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
2167c 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
2167d 72 65 20 68 6f 6c 64 73 20 74 68 65 20 63 6f 6e re holds the con
2167e 74 65 78 74 20 6f 66 20 61 0a 2a 2a 20 73 75 6d text of a.** sum
2167f 28 29 20 6f 72 20 61 76 67 28 29 20 61 67 67 72 () or avg() aggr
21680 65 67 61 74 65 20 63 6f 6d 70 75 74 61 74 69 6f egate computatio
21681 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 n..*/.typedef st
21682 72 75 63 74 20 53 75 6d 43 74 78 20 53 75 6d 43 ruct SumCtx SumC
21683 74 78 3b 0a 73 74 72 75 63 74 20 53 75 6d 43 74 tx;.struct SumCt
21684 78 20 7b 0a 20 20 64 6f 75 62 6c 65 20 72 53 75 x {. double rSu
21685 6d 3b 20 20 20 20 20 20 2f 2a 20 46 6c 6f 61 74 m; /* Float
21686 69 6e 67 20 70 6f 69 6e 74 20 73 75 6d 20 2a 2f ing point sum */
21687 0a 20 20 69 36 34 20 69 53 75 6d 3b 20 20 20 20 . i64 iSum;
21688 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 /* Integer
21689 73 75 6d 20 2a 2f 20 20 20 0a 20 20 69 36 34 20 sum */ . i64
2168a 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a cnt; /*
2168b 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 Number of eleme
2168c 6e 74 73 20 73 75 6d 6d 65 64 20 2a 2f 0a 20 20 nts summed */.
2168d 75 38 20 6f 76 65 72 66 6c 6f 77 3b 20 20 20 20 u8 overflow;
2168e 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74 /* True if int
2168f 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 73 65 eger overflow se
21690 65 6e 20 2a 2f 0a 20 20 75 38 20 61 70 70 72 6f en */. u8 appro
21691 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 x; /* Tru
21692 65 20 69 66 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 e if non-integer
21693 20 76 61 6c 75 65 20 77 61 73 20 69 6e 70 75 74 value was input
21694 20 74 6f 20 74 68 65 20 73 75 6d 20 2a 2f 0a 7d to the sum */.}
21695 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 ;../*.** Routine
21696 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 s used to comput
21697 65 20 74 68 65 20 73 75 6d 2c 20 61 76 65 72 61 e the sum, avera
21698 67 65 2c 20 61 6e 64 20 74 6f 74 61 6c 2e 0a 2a ge, and total..*
21699 2a 0a 2a 2a 20 54 68 65 20 53 55 4d 28 29 20 66 *.** The SUM() f
2169a 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20 unction follows
2169b 74 68 65 20 28 62 72 6f 6b 65 6e 29 20 53 51 4c the (broken) SQL
2169c 20 73 74 61 6e 64 61 72 64 20 77 68 69 63 68 20 standard which
2169d 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 74 means.** that it
2169e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 returns NULL if
2169f 20 69 74 20 73 75 6d 73 20 6f 76 65 72 20 6e 6f it sums over no
216a0 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c 20 inputs. TOTAL
216a1 72 65 74 75 72 6e 73 0a 2a 2a 20 30 2e 30 20 69 returns.** 0.0 i
216a2 6e 20 74 68 61 74 20 63 61 73 65 2e 20 20 49 6e n that case. In
216a3 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41 4c addition, TOTAL
216a4 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 always returns
216a5 61 20 66 6c 6f 61 74 20 77 68 65 72 65 0a 2a 2a a float where.**
216a6 20 53 55 4d 20 6d 69 67 68 74 20 72 65 74 75 72 SUM might retur
216a7 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66 20 n an integer if
216a8 69 74 20 6e 65 76 65 72 20 65 6e 63 6f 75 6e 74 it never encount
216a9 65 72 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 ers a floating p
216aa 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 oint.** value.
216ab 54 4f 54 41 4c 20 6e 65 76 65 72 20 66 61 69 6c TOTAL never fail
216ac 73 2c 20 62 75 74 20 53 55 4d 20 6d 69 67 68 74 s, but SUM might
216ad 20 74 68 72 6f 75 67 68 20 61 6e 20 65 78 63 65 through an exce
216ae 70 74 69 6f 6e 20 69 66 0a 2a 2a 20 69 74 20 6f ption if.** it o
216af 76 65 72 66 6c 6f 77 73 20 61 6e 20 69 6e 74 65 verflows an inte
216b0 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ger..*/.static v
216b1 6f 69 64 20 73 75 6d 53 74 65 70 28 73 71 6c 69 oid sumStep(sqli
216b2 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
216b3 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 text, int argc,
216b4 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
216b5 61 72 67 76 29 7b 0a 20 20 53 75 6d 43 74 78 20 argv){. SumCtx
216b6 2a 70 3b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a *p;. int type;.
216b7 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d assert( argc==
216b8 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 1 );. UNUSED_PA
216b9 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 RAMETER(argc);.
216ba 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 p = sqlite3_agg
216bb 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 regate_context(c
216bc 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a ontext, sizeof(*
216bd 70 29 29 3b 0a 20 20 74 79 70 65 20 3d 20 73 71 p));. type = sq
216be 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 lite3_value_nume
216bf 72 69 63 5f 74 79 70 65 28 61 72 67 76 5b 30 5d ric_type(argv[0]
216c0 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 74 79 );. if( p && ty
216c1 70 65 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 pe!=SQLITE_NULL
216c2 29 7b 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b ){. p->cnt++;
216c3 0a 20 20 20 20 69 66 28 20 74 79 70 65 3d 3d 53 . if( type==S
216c4 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b QLITE_INTEGER ){
216c5 0a 20 20 20 20 20 20 69 36 34 20 76 20 3d 20 73 . i64 v = s
216c6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
216c7 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 64(argv[0]);.
216c8 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b p->rSum += v;
216c9 0a 20 20 20 20 20 20 69 66 28 20 28 70 2d 3e 61 . if( (p->a
216ca 70 70 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f pprox|p->overflo
216cb 77 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 w)==0 ){.
216cc 20 69 36 34 20 69 4e 65 77 53 75 6d 20 3d 20 70 i64 iNewSum = p
216cd 2d 3e 69 53 75 6d 20 2b 20 76 3b 0a 20 20 20 20 ->iSum + v;.
216ce 20 20 20 20 69 6e 74 20 73 31 20 3d 20 70 2d 3e int s1 = p->
216cf 69 53 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 iSum >> (sizeof(
216d0 69 36 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 i64)*8-1);.
216d1 20 20 20 69 6e 74 20 73 32 20 3d 20 76 20 20 20 int s2 = v
216d2 20 20 20 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 >> (sizeof(i
216d3 36 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 64)*8-1);.
216d4 20 20 69 6e 74 20 73 33 20 3d 20 69 4e 65 77 53 int s3 = iNewS
216d5 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 um >> (sizeof(i6
216d6 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 20 4)*8-1);.
216d7 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 3d 20 28 p->overflow = (
216d8 73 31 26 73 32 26 7e 73 33 29 20 7c 20 28 7e 73 s1&s2&~s3) | (~s
216d9 31 26 7e 73 32 26 73 33 29 3b 0a 20 20 20 20 20 1&~s2&s3);.
216da 20 20 20 70 2d 3e 69 53 75 6d 20 3d 20 69 4e 65 p->iSum = iNe
216db 77 53 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 wSum;. }.
216dc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
216dd 2d 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69 74 65 ->rSum += sqlite
216de 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 3_value_double(a
216df 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 rgv[0]);. p
216e0 2d 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a 20 20 ->approx = 1;.
216e1 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 }. }.}.static
216e2 20 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c 69 7a void sumFinaliz
216e3 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 e(sqlite3_contex
216e4 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 t *context){. S
216e5 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 umCtx *p;. p =
216e6 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 sqlite3_aggregat
216e7 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 e_context(contex
216e8 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 t, 0);. if( p &
216e9 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 & p->cnt>0 ){.
216ea 20 20 69 66 28 20 70 2d 3e 6f 76 65 72 66 6c 6f if( p->overflo
216eb 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 w ){. sqlit
216ec 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
216ed 63 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67 65 72 context,"integer
216ee 20 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a overflow",-1);.
216ef 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d }else if( p-
216f0 3e 61 70 70 72 6f 78 20 29 7b 0a 20 20 20 20 20 >approx ){.
216f1 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
216f2 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 double(context,
216f3 70 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20 7d 65 p->rSum);. }e
216f4 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
216f5 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 e3_result_int64(
216f6 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d context, p->iSum
216f7 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 );. }. }.}.s
216f8 74 61 74 69 63 20 76 6f 69 64 20 61 76 67 46 69 tatic void avgFi
216f9 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 nalize(sqlite3_c
216fa 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 ontext *context)
216fb 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 {. SumCtx *p;.
216fc 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 p = sqlite3_agg
216fd 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 regate_context(c
216fe 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 ontext, 0);. if
216ff 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20 ( p && p->cnt>0
21700 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 ){. sqlite3_r
21701 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e esult_double(con
21702 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f 28 64 text, p->rSum/(d
21703 6f 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b 0a 20 ouble)p->cnt);.
21704 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
21705 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 28 73 totalFinalize(s
21706 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
21707 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 context){. SumC
21708 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c tx *p;. p = sql
21709 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
2170a 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 ontext(context,
2170b 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 0);. sqlite3_re
2170c 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 sult_double(cont
2170d 65 78 74 2c 20 70 20 3f 20 70 2d 3e 72 53 75 6d ext, p ? p->rSum
2170e 20 3a 20 30 2e 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a : 0.0);.}../*.*
2170f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
21710 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 structure keeps
21711 74 72 61 63 6b 20 6f 66 20 73 74 61 74 65 20 69 track of state i
21712 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 nformation for t
21713 68 65 0a 2a 2a 20 63 6f 75 6e 74 28 29 20 61 67 he.** count() ag
21714 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
21715 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
21716 75 63 74 20 43 6f 75 6e 74 43 74 78 20 43 6f 75 uct CountCtx Cou
21717 6e 74 43 74 78 3b 0a 73 74 72 75 63 74 20 43 6f ntCtx;.struct Co
21718 75 6e 74 43 74 78 20 7b 0a 20 20 69 36 34 20 6e untCtx {. i64 n
21719 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 ;.};../*.** Rout
2171a 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e ines to implemen
2171b 74 20 74 68 65 20 63 6f 75 6e 74 28 29 20 61 67 t the count() ag
2171c 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
2171d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
2171e 20 63 6f 75 6e 74 53 74 65 70 28 73 71 6c 69 74 countStep(sqlit
2171f 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
21720 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 ext, int argc, s
21721 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
21722 72 67 76 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78 rgv){. CountCtx
21723 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 *p;. p = sqlit
21724 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e e3_aggregate_con
21725 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 text(context, si
21726 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 zeof(*p));. if(
21727 20 28 61 72 67 63 3d 3d 30 20 7c 7c 20 53 51 4c (argc==0 || SQL
21728 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 ITE_NULL!=sqlite
21729 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
2172a 76 5b 30 5d 29 29 20 26 26 20 70 20 29 7b 0a 20 v[0])) && p ){.
2172b 20 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 7d p->n++;. }.}
2172c 20 20 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .static void
2172d 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 countFinalize(sq
2172e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
2172f 6f 6e 74 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 ontext){. Count
21730 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 Ctx *p;. p = sq
21731 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
21732 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c context(context,
21733 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 0);. sqlite3_r
21734 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 esult_int64(cont
21735 65 78 74 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 ext, p ? p->n :
21736 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 0);.}../*.** Rou
21737 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 tines to impleme
21738 6e 74 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 nt min() and max
21739 28 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e () aggregate fun
2173a 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 ctions..*/.stati
2173b 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 c void minmaxSte
2173c 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e p(. sqlite3_con
2173d 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a text *context, .
2173e 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a int NotUsed, .
2173f 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
21740 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 4d 65 6d 20 **argv.){. Mem
21741 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d 20 2a 29 *pArg = (Mem *)
21742 61 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a argv[0];. Mem *
21743 70 42 65 73 74 3b 0a 20 20 55 4e 55 53 45 44 5f pBest;. UNUSED_
21744 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
21745 64 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 d);.. if( sqlit
21746 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 e3_value_type(ar
21747 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e gv[0])==SQLITE_N
21748 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ULL ) return;.
21749 70 42 65 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73 pBest = (Mem *)s
2174a 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 qlite3_aggregate
2174b 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 _context(context
2174c 2c 20 73 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 , sizeof(*pBest)
2174d 29 3b 0a 20 20 69 66 28 20 21 70 42 65 73 74 20 );. if( !pBest
2174e 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 ) return;.. if(
2174f 20 70 42 65 73 74 2d 3e 66 6c 61 67 73 20 29 7b pBest->flags ){
21750 0a 20 20 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 . int max;.
21751 20 20 69 6e 74 20 63 6d 70 3b 0a 20 20 20 20 43 int cmp;. C
21752 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 ollSeq *pColl =
21753 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f sqlite3GetFuncCo
21754 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a llSeq(context);.
21755 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74 65 70 /* This step
21756 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
21757 64 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20 6d d for both the m
21758 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 61 in() and max() a
21759 67 67 72 65 67 61 74 65 73 2c 0a 20 20 20 20 2a ggregates,. *
2175a 2a 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 * the only diffe
2175b 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 rence between th
2175c 65 20 74 77 6f 20 62 65 69 6e 67 20 74 68 61 74 e two being that
2175d 20 74 68 65 20 73 65 6e 73 65 20 6f 66 20 74 68 the sense of th
2175e 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 e. ** compari
2175f 73 6f 6e 20 69 73 20 69 6e 76 65 72 74 65 64 2e son is inverted.
21760 20 46 6f 72 20 74 68 65 20 6d 61 78 28 29 20 61 For the max() a
21761 67 67 72 65 67 61 74 65 2c 20 74 68 65 0a 20 20 ggregate, the.
21762 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 ** sqlite3_use
21763 72 5f 64 61 74 61 28 29 20 66 75 6e 63 74 69 6f r_data() functio
21764 6e 20 72 65 74 75 72 6e 73 20 28 76 6f 69 64 20 n returns (void
21765 2a 29 2d 31 2e 20 46 6f 72 20 6d 69 6e 28 29 20 *)-1. For min()
21766 69 74 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e it. ** return
21767 73 20 28 76 6f 69 64 20 2a 29 64 62 2c 20 77 68 s (void *)db, wh
21768 65 72 65 20 64 62 20 69 73 20 74 68 65 20 73 71 ere db is the sq
21769 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 lite3* database
2176a 70 6f 69 6e 74 65 72 2e 0a 20 20 20 20 2a 2a 20 pointer.. **
2176b 54 68 65 72 65 66 6f 72 65 20 74 68 65 20 6e 65 Therefore the ne
2176c 78 74 20 73 74 61 74 65 6d 65 6e 74 20 73 65 74 xt statement set
2176d 73 20 76 61 72 69 61 62 6c 65 20 27 6d 61 78 27 s variable 'max'
2176e 20 74 6f 20 31 20 66 6f 72 20 74 68 65 20 6d 61 to 1 for the ma
2176f 78 28 29 0a 20 20 20 20 2a 2a 20 61 67 67 72 65 x(). ** aggre
21770 67 61 74 65 2c 20 6f 72 20 30 20 66 6f 72 20 6d gate, or 0 for m
21771 69 6e 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 in().. */.
21772 20 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f 75 max = sqlite3_u
21773 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 ser_data(context
21774 29 21 3d 30 3b 0a 20 20 20 20 63 6d 70 20 3d 20 )!=0;. cmp =
21775 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 sqlite3MemCompar
21776 65 28 70 42 65 73 74 2c 20 70 41 72 67 2c 20 70 e(pBest, pArg, p
21777 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 Coll);. if( (
21778 6d 61 78 20 26 26 20 63 6d 70 3c 30 29 20 7c 7c max && cmp<0) ||
21779 20 28 21 6d 61 78 20 26 26 20 63 6d 70 3e 30 29 (!max && cmp>0)
2177a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
2177b 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42 65 3VdbeMemCopy(pBe
2177c 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 20 20 7d st, pArg);. }
2177d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
2177e 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 lite3VdbeMemCopy
2177f 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 (pBest, pArg);.
21780 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
21781 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 minMaxFinalize(
21782 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
21783 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c *context){. sql
21784 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 ite3_value *pRes
21785 3b 0a 20 20 70 52 65 73 20 3d 20 28 73 71 6c 69 ;. pRes = (sqli
21786 74 65 33 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 te3_value *)sqli
21787 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
21788 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 ntext(context, 0
21789 29 3b 0a 20 20 69 66 28 20 70 52 65 73 20 29 7b );. if( pRes ){
2178a 0a 20 20 20 20 69 66 28 20 70 52 65 73 2d 3e 66 . if( pRes->f
2178b 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 73 71 lags ){. sq
2178c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c lite3_result_val
2178d 75 65 28 63 6f 6e 74 65 78 74 2c 20 70 52 65 73 ue(context, pRes
2178e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
2178f 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
21790 73 65 28 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a se(pRes);. }.}.
21791 0a 2f 2a 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e ./*.** group_con
21792 63 61 74 28 45 58 50 52 2c 20 3f 53 45 50 41 52 cat(EXPR, ?SEPAR
21793 41 54 4f 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63 ATOR?).*/.static
21794 20 76 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 void groupConca
21795 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 tStep(. sqlite3
21796 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
21797 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
21798 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
21799 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 *argv.){. const
2179a 20 63 68 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53 char *zVal;. S
2179b 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b trAccum *pAccum;
2179c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
2179d 53 65 70 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c Sep;. int nVal,
2179e 20 6e 53 65 70 2c 20 69 3b 0a 20 20 69 66 28 20 nSep, i;. if(
2179f 61 72 67 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 argc==0 || sqlit
217a0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 e3_value_type(ar
217a1 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e gv[0])==SQLITE_N
217a2 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ULL ) return;.
217a3 70 41 63 63 75 6d 20 3d 20 28 53 74 72 41 63 63 pAccum = (StrAcc
217a4 75 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72 um*)sqlite3_aggr
217a5 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f egate_context(co
217a6 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 ntext, sizeof(*p
217a7 41 63 63 75 6d 29 29 3b 0a 0a 20 20 69 66 28 20 Accum));.. if(
217a8 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 73 71 pAccum ){. sq
217a9 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
217aa 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
217ab 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
217ac 20 20 20 20 70 41 63 63 75 6d 2d 3e 75 73 65 4d pAccum->useM
217ad 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 20 20 70 alloc = 1;. p
217ae 41 63 63 75 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d Accum->mxAlloc =
217af 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 db->aLimit[SQLI
217b0 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
217b1 3b 0a 20 20 20 20 69 66 28 20 70 41 63 63 75 6d ;. if( pAccum
217b2 2d 3e 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 ->nChar ){.
217b3 20 69 66 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 if( argc>1 ){.
217b4 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20 28 63 zSep = (c
217b5 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c har*)sqlite3_val
217b6 75 65 5f 74 65 78 74 28 61 72 67 76 5b 61 72 67 ue_text(argv[arg
217b7 63 2d 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 6e c-1]);. n
217b8 53 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 Sep = sqlite3_va
217b9 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 61 lue_bytes(argv[a
217ba 72 67 63 2d 31 5d 29 3b 0a 20 20 20 20 20 20 7d rgc-1]);. }
217bb 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 else{. zS
217bc 65 70 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 ep = ",";.
217bd 20 20 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 nSep = 1;.
217be 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
217bf 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
217c0 70 41 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 pAccum, zSep, nS
217c1 65 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ep);. }. i
217c2 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 = 0;. do{.
217c3 20 20 20 20 7a 56 61 6c 20 3d 20 28 63 68 61 72 zVal = (char
217c4 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *)sqlite3_value_
217c5 74 65 78 74 28 61 72 67 76 5b 69 5d 29 3b 0a 20 text(argv[i]);.
217c6 20 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 nVal = sqli
217c7 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
217c8 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 argv[i]);.
217c9 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
217ca 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 56 ppend(pAccum, zV
217cb 61 6c 2c 20 6e 56 61 6c 29 3b 0a 20 20 20 20 20 al, nVal);.
217cc 20 69 2b 2b 3b 0a 20 20 20 20 7d 77 68 69 6c 65 i++;. }while
217cd 28 20 69 3c 61 72 67 63 2d 31 20 29 3b 0a 20 20 ( i<argc-1 );.
217ce 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 }.}.static void
217cf 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c groupConcatFinal
217d0 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ize(sqlite3_cont
217d1 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 ext *context){.
217d2 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 StrAccum *pAccu
217d3 6d 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20 73 71 m;. pAccum = sq
217d4 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
217d5 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c context(context,
217d6 20 30 29 3b 0a 20 20 69 66 28 20 70 41 63 63 75 0);. if( pAccu
217d7 6d 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41 63 m ){. if( pAc
217d8 63 75 6d 2d 3e 74 6f 6f 42 69 67 20 29 7b 0a 20 cum->tooBig ){.
217d9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
217da 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 ult_error_toobig
217db 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d (context);. }
217dc 65 6c 73 65 20 69 66 28 20 70 41 63 63 75 6d 2d else if( pAccum-
217dd 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
217de 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
217df 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 esult_error_nome
217e0 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 m(context);.
217e1 7d 65 6c 73 65 7b 20 20 20 20 0a 20 20 20 20 20 }else{ .
217e2 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
217e3 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 text(context, sq
217e4 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e lite3StrAccumFin
217e5 69 73 68 28 70 41 63 63 75 6d 29 2c 20 2d 31 2c ish(pAccum), -1,
217e6 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
217e7 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
217e8 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d te3_free);. }
217e9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
217ea 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 is function regi
217eb 73 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 stered all of th
217ec 65 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 e above C functi
217ed 6f 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 ons as SQL.** fu
217ee 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 nctions. This s
217ef 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c hould be the onl
217f0 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 y routine in thi
217f1 73 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 s file with.** e
217f2 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e xternal linkage.
217f3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
217f4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
217f5 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75 egisterBuiltinFu
217f6 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 nctions(sqlite3
217f7 2a 64 62 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 *db){.#ifndef SQ
217f8 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 LITE_OMIT_ALTERT
217f9 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 41 6c ABLE. sqlite3Al
217fa 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 terFunctions(db)
217fb 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 ;.#endif. if( !
217fc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
217fd 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d ){. int rc =
217fe 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 sqlite3_overloa
217ff 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 d_function(db, "
21800 4d 41 54 43 48 22 2c 20 32 29 3b 0a 20 20 20 20 MATCH", 2);.
21801 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
21802 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d TE_NOMEM || rc==
21803 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
21804 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
21805 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 NOMEM ){. d
21806 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
21807 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 = 1;. }. }.#
21808 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 ifdef SQLITE_SSE
21809 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 . (void)sqlite3
2180a 53 73 65 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 SseFunctions(db)
2180b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a ;.#endif.}../*.*
2180c 2a 20 53 65 74 20 74 68 65 20 4c 49 4b 45 4f 50 * Set the LIKEOP
2180d 54 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 32 2d T flag on the 2-
2180e 61 72 67 75 6d 65 6e 74 20 66 75 6e 63 74 69 6f argument functio
2180f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e n with the given
21810 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 name..*/.static
21811 20 76 6f 69 64 20 73 65 74 4c 69 6b 65 4f 70 74 void setLikeOpt
21812 46 6c 61 67 28 73 71 6c 69 74 65 33 20 2a 64 62 Flag(sqlite3 *db
21813 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e , const char *zN
21814 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67 56 61 6c ame, int flagVal
21815 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44 ){. FuncDef *pD
21816 65 66 3b 0a 20 20 70 44 65 66 20 3d 20 73 71 6c ef;. pDef = sql
21817 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e ite3FindFunction
21818 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c (db, zName, strl
21819 65 6e 28 7a 4e 61 6d 65 29 2c 20 32 2c 20 53 51 en(zName), 2, SQ
2181a 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 LITE_UTF8, 0);.
2181b 20 69 66 28 20 70 44 65 66 20 29 7b 0a 20 20 20 if( pDef ){.
2181c 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 3d 20 66 pDef->flags = f
2181d 6c 61 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f lagVal;. }.}../
2181e 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 *.** Register th
2181f 65 20 62 75 69 6c 74 2d 69 6e 20 4c 49 4b 45 20 e built-in LIKE
21820 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f and GLOB functio
21821 6e 73 2e 20 20 54 68 65 20 63 61 73 65 53 65 6e ns. The caseSen
21822 73 69 74 69 76 65 0a 2a 2a 20 70 61 72 61 6d 65 sitive.** parame
21823 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77 ter determines w
21824 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
21825 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 e LIKE operator
21826 69 73 20 63 61 73 65 0a 2a 2a 20 73 65 6e 73 69 is case.** sensi
21827 74 69 76 65 2e 20 20 47 4c 4f 42 20 69 73 20 61 tive. GLOB is a
21828 6c 77 61 79 73 20 63 61 73 65 20 73 65 6e 73 69 lways case sensi
21829 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tive..*/.SQLITE_
2182a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
2182b 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 ite3RegisterLike
2182c 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 Functions(sqlite
2182d 33 20 2a 64 62 2c 20 69 6e 74 20 63 61 73 65 53 3 *db, int caseS
2182e 65 6e 73 69 74 69 76 65 29 7b 0a 20 20 73 74 72 ensitive){. str
2182f 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 uct compareInfo
21830 2a 70 49 6e 66 6f 3b 0a 20 20 69 66 28 20 63 61 *pInfo;. if( ca
21831 73 65 53 65 6e 73 69 74 69 76 65 20 29 7b 0a 20 seSensitive ){.
21832 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 pInfo = (stru
21833 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 ct compareInfo*)
21834 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 &likeInfoAlt;.
21835 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f }else{. pInfo
21836 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 = (struct compa
21837 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 reInfo*)&likeInf
21838 6f 4e 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c oNorm;. }. sql
21839 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 ite3CreateFunc(d
2183a 62 2c 20 22 6c 69 6b 65 22 2c 20 32 2c 20 53 51 b, "like", 2, SQ
2183b 4c 49 54 45 5f 55 54 46 38 2c 20 70 49 6e 66 6f LITE_UTF8, pInfo
2183c 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 , likeFunc, 0, 0
2183d 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 );. sqlite3Crea
2183e 74 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 teFunc(db, "like
2183f 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 ", 3, SQLITE_UTF
21840 38 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 8, pInfo, likeFu
21841 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c nc, 0, 0);. sql
21842 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 ite3CreateFunc(d
21843 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20 53 51 b, "glob", 2, SQ
21844 4c 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 20 20 LITE_UTF8, .
21845 20 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 (struct compar
21846 65 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f eInfo*)&globInfo
21847 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 30 29 , likeFunc, 0,0)
21848 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c ;. setLikeOptFl
21849 61 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 53 ag(db, "glob", S
2184a 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 QLITE_FUNC_LIKE
2184b 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 | SQLITE_FUNC_CA
2184c 53 45 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 SE);. setLikeOp
2184d 74 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b 65 22 tFlag(db, "like"
2184e 2c 20 0a 20 20 20 20 20 20 63 61 73 65 53 65 6e , . caseSen
2184f 73 69 74 69 76 65 20 3f 20 28 53 51 4c 49 54 45 sitive ? (SQLITE
21850 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c _FUNC_LIKE | SQL
21851 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 20 3a ITE_FUNC_CASE) :
21852 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b SQLITE_FUNC_LIK
21853 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 E);.}../*.** pEx
21854 70 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 pr points to an
21855 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 expression which
21856 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75 implements a fu
21857 6e 63 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69 nction. If.** i
21858 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 t is appropriate
21859 20 74 6f 20 61 70 70 6c 79 20 74 68 65 20 4c 49 to apply the LI
2185a 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 KE optimization
2185b 74 6f 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e to that function
2185c 0a 2a 2a 20 74 68 65 6e 20 73 65 74 20 61 57 63 .** then set aWc
2185d 5b 30 5d 20 74 68 72 6f 75 67 68 20 61 57 63 5b [0] through aWc[
2185e 32 5d 20 74 6f 20 74 68 65 20 77 69 6c 64 63 61 2] to the wildca
2185f 72 64 20 63 68 61 72 61 63 74 65 72 73 20 61 6e rd characters an
21860 64 0a 2a 2a 20 72 65 74 75 72 6e 20 54 52 55 45 d.** return TRUE
21861 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 . If the functi
21862 6f 6e 20 69 73 20 6e 6f 74 20 61 20 4c 49 4b 45 on is not a LIKE
21863 2d 73 74 79 6c 65 20 66 75 6e 63 74 69 6f 6e 20 -style function
21864 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 46 then.** return F
21865 41 4c 53 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ALSE..*/.SQLITE_
21866 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
21867 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f te3IsLikeFunctio
21868 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 n(sqlite3 *db, E
21869 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
2186a 2a 70 49 73 4e 6f 63 61 73 65 2c 20 63 68 61 72 *pIsNocase, char
2186b 20 2a 61 57 63 29 7b 0a 20 20 46 75 6e 63 44 65 *aWc){. FuncDe
2186c 66 20 2a 70 44 65 66 3b 0a 20 20 69 66 28 20 70 f *pDef;. if( p
2186d 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e Expr->op!=TK_FUN
2186e 43 54 49 4f 4e 20 7c 7c 20 21 70 45 78 70 72 2d CTION || !pExpr-
2186f 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 72 65 >pList ){. re
21870 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 turn 0;. }. if
21871 28 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e ( pExpr->pList->
21872 6e 45 78 70 72 21 3d 32 20 29 7b 0a 20 20 20 20 nExpr!=2 ){.
21873 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
21874 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 pDef = sqlite3Fi
21875 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 ndFunction(db, (
21876 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b char*)pExpr->tok
21877 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b en.z, pExpr->tok
21878 65 6e 2e 6e 2c 20 32 2c 0a 20 20 20 20 20 20 20 en.n, 2,.
21879 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2187a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 SQLITE_UTF
2187b 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 44 65 8, 0);. if( pDe
2187c 66 3d 3d 30 20 7c 7c 20 28 70 44 65 66 2d 3e 66 f==0 || (pDef->f
2187d 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
2187e 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 NC_LIKE)==0 ){.
2187f 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
21880 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70 .. /* The memcp
21881 79 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73 y() statement as
21882 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 77 sumes that the w
21883 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65 ildcard characte
21884 72 73 20 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 rs are. ** the
21885 66 69 72 73 74 20 74 68 72 65 65 20 73 74 61 74 first three stat
21886 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f ements in the co
21887 6d 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 mpareInfo struct
21888 75 72 65 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61 ure. The. ** a
21889 73 73 65 72 74 73 28 29 20 74 68 61 74 20 66 6f sserts() that fo
2188a 6c 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 llow verify that
2188b 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f assumption. */
2188c 0a 20 20 6d 65 6d 63 70 79 28 61 57 63 2c 20 70 . memcpy(aWc, p
2188d 44 65 66 2d 3e 70 55 73 65 72 44 61 74 61 2c 20 Def->pUserData,
2188e 33 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 3);. assert( (c
2188f 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c har*)&likeInfoAl
21890 74 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b t == (char*)&lik
21891 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c eInfoAlt.matchAl
21892 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26 l );. assert( &
21893 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 ((char*)&likeInf
21894 6f 41 6c 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 oAlt)[1] == (cha
21895 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e r*)&likeInfoAlt.
21896 6d 61 74 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73 matchOne );. as
21897 73 65 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 sert( &((char*)&
21898 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 likeInfoAlt)[2]
21899 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 == (char*)&likeI
2189a 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 nfoAlt.matchSet
2189b 29 3b 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 );. *pIsNocase
2189c 3d 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 = (pDef->flags &
2189d 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 SQLITE_FUNC_CAS
2189e 45 29 3d 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 E)==0;. return
2189f 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 1;.}../*.** All
218a0 61 6c 6c 20 6f 66 20 74 68 65 20 46 75 6e 63 44 all of the FuncD
218a1 65 66 20 73 74 72 75 63 74 75 72 65 73 20 69 6e ef structures in
218a2 20 74 68 65 20 61 42 75 69 6c 74 69 6e 46 75 6e the aBuiltinFun
218a3 63 5b 5d 20 61 72 72 61 79 20 61 62 6f 76 65 0a c[] array above.
218a4 2a 2a 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c ** to the global
218a5 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 68 20 74 function hash t
218a6 61 62 6c 65 2e 20 20 54 68 69 73 20 6f 63 63 75 able. This occu
218a7 72 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 rs at start-time
218a8 20 28 61 73 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 (as.** a conseq
218a9 75 65 6e 63 65 20 6f 66 20 63 61 6c 6c 69 6e 67 uence of calling
218aa 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
218ab 69 7a 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 ize())..**.** Af
218ac 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
218ad 20 72 75 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f runs.*/.SQLITE_
218ae 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
218af 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 ite3RegisterGlob
218b0 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 alFunctions(void
218b1 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 ){. /*. ** The
218b2 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 following array
218b3 20 68 6f 6c 64 73 20 46 75 6e 63 44 65 66 20 73 holds FuncDef s
218b4 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 61 6c tructures for al
218b5 6c 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f l of the functio
218b6 6e 73 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 ns. ** defined
218b7 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 20 20 in this file..
218b8 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61 72 72 61 **. ** The arra
218b9 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 y cannot be cons
218ba 74 61 6e 74 20 73 69 6e 63 65 20 63 68 61 6e 67 tant since chang
218bb 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 es are made to t
218bc 68 65 0a 20 20 2a 2a 20 46 75 6e 63 44 65 66 2e he. ** FuncDef.
218bd 70 48 61 73 68 20 65 6c 65 6d 65 6e 74 73 20 61 pHash elements a
218be 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 54 t start-time. T
218bf 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 he elements of t
218c0 68 69 73 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 his array. ** a
218c1 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74 re read-only aft
218c2 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f er initializatio
218c3 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 20 n is complete..
218c4 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 53 51 4c */. static SQL
218c5 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 20 ITE_WSD FuncDef
218c6 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d aBuiltinFunc[] =
218c7 20 7b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 {. FUNCTION(
218c8 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 ltrim,
218c9 20 20 20 20 31 2c 20 31 2c 20 30 2c 20 74 72 69 1, 1, 0, tri
218ca 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c mFunc ),
218cb 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 . FUNCTION(lt
218cc 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 rim,
218cd 20 20 32 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 2, 1, 0, trimF
218ce 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 unc ),.
218cf 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 FUNCTION(rtri
218d0 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m,
218d1 31 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 1, 2, 0, trimFun
218d2 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 c ),.
218d3 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c FUNCTION(rtrim,
218d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 2,
218d5 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 2, 0, trimFunc
218d6 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 ),. F
218d7 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 UNCTION(trim,
218d8 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 33 1, 3
218d9 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 , 0, trimFunc
218da 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
218db 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 CTION(trim,
218dc 20 20 20 20 20 20 20 20 20 20 32 2c 20 33 2c 20 2, 3,
218dd 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 0, trimFunc
218de 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
218df 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 ION(min,
218e0 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 31 2c -1, 0, 1,
218e1 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 minmaxFunc
218e2 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
218e3 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 N(min,
218e4 20 20 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 30 0, 0, 1, 0
218e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
218e6 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 ),. AGGREGATE
218e7 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 (min,
218e8 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 1, 0, 1, min
218e9 6d 61 78 53 74 65 70 2c 20 20 20 20 20 20 6d 69 maxStep, mi
218ea 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a nMaxFinalize ),.
218eb 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 FUNCTION(max
218ec 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
218ed 2d 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 -1, 1, 1, minmax
218ee 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 Func ),.
218ef 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 FUNCTION(max,
218f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
218f1 2c 20 31 2c 20 31 2c 20 30 20 20 20 20 20 20 20 , 1, 1, 0
218f2 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
218f3 41 47 47 52 45 47 41 54 45 28 6d 61 78 2c 20 20 AGGREGATE(max,
218f4 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 1,
218f5 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 1, 1, minmaxStep
218f6 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e , minMaxFin
218f7 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e alize ),. FUN
218f8 43 54 49 4f 4e 28 74 79 70 65 6f 66 2c 20 20 20 CTION(typeof,
218f9 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
218fa 30 2c 20 74 79 70 65 6f 66 46 75 6e 63 20 20 20 0, typeofFunc
218fb 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
218fc 49 4f 4e 28 6c 65 6e 67 74 68 2c 20 20 20 20 20 ION(length,
218fd 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 1, 0, 0,
218fe 20 6c 65 6e 67 74 68 46 75 6e 63 20 20 20 20 20 lengthFunc
218ff 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
21900 4e 28 73 75 62 73 74 72 2c 20 20 20 20 20 20 20 N(substr,
21901 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 73 2, 0, 0, s
21902 75 62 73 74 72 46 75 6e 63 20 20 20 20 20 20 20 ubstrFunc
21903 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
21904 73 75 62 73 74 72 2c 20 20 20 20 20 20 20 20 20 substr,
21905 20 20 20 20 33 2c 20 30 2c 20 30 2c 20 73 75 62 3, 0, 0, sub
21906 73 74 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c strFunc ),
21907 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 61 62 . FUNCTION(ab
21908 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s,
21909 20 20 31 2c 20 30 2c 20 30 2c 20 61 62 73 46 75 1, 0, 0, absFu
2190a 6e 63 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 nc ),.
2190b 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e FUNCTION(roun
2190c 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
2190d 31 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 1, 0, 0, roundFu
2190e 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 nc ),.
2190f 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c FUNCTION(round,
21910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 2,
21911 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 0, 0, roundFunc
21912 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 ),. F
21913 55 4e 43 54 49 4f 4e 28 75 70 70 65 72 2c 20 20 UNCTION(upper,
21914 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 1, 0
21915 2c 20 30 2c 20 75 70 70 65 72 46 75 6e 63 20 20 , 0, upperFunc
21916 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
21917 43 54 49 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 20 CTION(lower,
21918 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
21919 30 2c 20 6c 6f 77 65 72 46 75 6e 63 20 20 20 20 0, lowerFunc
2191a 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
2191b 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 ION(coalesce,
2191c 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 1, 0, 0,
2191d 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
2191e 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
2191f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 N(coalesce,
21920 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 69 -1, 0, 0, i
21921 66 6e 75 6c 6c 46 75 6e 63 20 20 20 20 20 20 20 fnullFunc
21922 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
21923 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 coalesce,
21924 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 20 20 0, 0, 0, 0
21925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c ),
21926 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 68 65 . FUNCTION(he
21927 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x,
21928 20 20 31 2c 20 30 2c 20 30 2c 20 68 65 78 46 75 1, 0, 0, hexFu
21929 6e 63 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 nc ),.
2192a 20 20 20 46 55 4e 43 54 49 4f 4e 28 69 66 6e 75 FUNCTION(ifnu
2192b 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ll,
2192c 32 2c 20 30 2c 20 31 2c 20 69 66 6e 75 6c 6c 46 2, 0, 1, ifnullF
2192d 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 unc ),.
2192e 20 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d FUNCTION(random
2192f 2c 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c , -1,
21930 20 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 0, 0, randomFun
21931 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 c ),. F
21932 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c UNCTION(randombl
21933 6f 62 2c 20 20 20 20 20 20 20 20 20 31 2c 20 30 ob, 1, 0
21934 2c 20 30 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 , 0, randomBlob
21935 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
21936 43 54 49 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 CTION(nullif,
21937 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 2, 0,
21938 31 2c 20 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20 1, nullifFunc
21939 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
2193a 49 4f 4e 28 73 71 6c 69 74 65 5f 76 65 72 73 69 ION(sqlite_versi
2193b 6f 6e 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c on, 0, 0, 0,
2193c 20 76 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 20 versionFunc
2193d 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
2193e 4e 28 71 75 6f 74 65 2c 20 20 20 20 20 20 20 20 N(quote,
2193f 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 71 1, 0, 0, q
21940 75 6f 74 65 46 75 6e 63 20 20 20 20 20 20 20 20 uoteFunc
21941 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
21942 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 last_insert_rowi
21943 64 2c 20 20 30 2c 20 30 2c 20 30 2c 20 6c 61 73 d, 0, 0, 0, las
21944 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 29 2c t_insert_rowid),
21945 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 68 . FUNCTION(ch
21946 61 6e 67 65 73 2c 20 20 20 20 20 20 20 20 20 20 anges,
21947 20 20 30 2c 20 30 2c 20 30 2c 20 63 68 61 6e 67 0, 0, 0, chang
21948 65 73 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 es ),.
21949 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 6f 74 61 FUNCTION(tota
2194a 6c 5f 63 68 61 6e 67 65 73 2c 20 20 20 20 20 20 l_changes,
2194b 30 2c 20 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 63 0, 0, 0, total_c
2194c 68 61 6e 67 65 73 20 20 20 20 29 2c 0a 20 20 20 hanges ),.
2194d 20 46 55 4e 43 54 49 4f 4e 28 72 65 70 6c 61 63 FUNCTION(replac
2194e 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 33 2c e, 3,
2194f 20 30 2c 20 30 2c 20 72 65 70 6c 61 63 65 46 75 0, 0, replaceFu
21950 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 nc ),. F
21951 55 4e 43 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62 UNCTION(zeroblob
21952 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 , 1, 0
21953 2c 20 30 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e , 0, zeroblobFun
21954 63 20 20 20 20 20 29 2c 0a 20 20 23 69 66 64 65 c ),. #ifde
21955 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 f SQLITE_SOUNDEX
21956 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f . FUNCTION(so
21957 75 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 undex,
21958 20 20 31 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64 1, 0, 0, sound
21959 65 78 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 exFunc ),.
2195a 20 23 65 6e 64 69 66 0a 20 20 23 69 66 6e 64 65 #endif. #ifnde
2195b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
2195c 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 AD_EXTENSION.
2195d 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 FUNCTION(load_e
2195e 78 74 65 6e 73 69 6f 6e 2c 20 20 20 20 20 31 2c xtension, 1,
2195f 20 30 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 0, 0, loadExt
21960 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 ),. F
21961 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 UNCTION(load_ext
21962 65 6e 73 69 6f 6e 2c 20 20 20 20 20 32 2c 20 30 ension, 2, 0
21963 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 , 0, loadExt
21964 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64 69 ),. #endi
21965 66 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 f. AGGREGATE(
21966 73 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 sum,
21967 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 1, 0, 0, sumS
21968 74 65 70 2c 20 20 20 20 20 20 20 20 20 73 75 6d tep, sum
21969 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 Finalize ),.
2196a 20 20 20 41 47 47 52 45 47 41 54 45 28 74 6f 74 AGGREGATE(tot
2196b 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 al,
2196c 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 1, 0, 0, sumStep
2196d 2c 20 20 20 20 20 20 20 20 20 74 6f 74 61 6c 46 , totalF
2196e 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 inalize ),.
2196f 20 20 41 47 47 52 45 47 41 54 45 28 61 76 67 2c AGGREGATE(avg,
21970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
21971 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c , 0, 0, sumStep,
21972 20 20 20 20 20 20 20 20 20 61 76 67 46 69 6e 61 avgFina
21973 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 lize ),. A
21974 47 47 52 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 GGREGATE(count,
21975 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 0, 0
21976 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 , 0, countStep,
21977 20 20 20 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c countFinal
21978 69 7a 65 20 20 29 2c 0a 20 20 20 20 41 47 47 52 ize ),. AGGR
21979 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 EGATE(count,
2197a 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
2197b 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 , countStep,
2197c 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 countFinalize
2197d 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 ),. AGGREGA
2197e 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c TE(group_concat,
2197f 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 67 -1, 0, 0, g
21980 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 roupConcatStep,
21981 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c groupConcatFinal
21982 69 7a 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b ize),. . LIK
21983 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26 EFUNC(glob, 2, &
21984 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45 globInfo, SQLITE
21985 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 _FUNC_LIKE|SQLIT
21986 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 E_FUNC_CASE),.
21987 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41 #ifdef SQLITE_CA
21988 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b SE_SENSITIVE_LIK
21989 45 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c E. LIKEFUNC(l
2198a 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 ike, 2, &likeInf
2198b 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e oAlt, SQLITE_FUN
2198c 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 C_LIKE|SQLITE_FU
2198d 4e 43 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c 49 NC_CASE),. LI
2198e 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 KEFUNC(like, 3,
2198f 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 &likeInfoAlt, SQ
21990 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 LITE_FUNC_LIKE|S
21991 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 QLITE_FUNC_CASE)
21992 2c 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 4c 49 ,. #else. LI
21993 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 KEFUNC(like, 2,
21994 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 &likeInfoNorm, S
21995 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 QLITE_FUNC_LIKE)
21996 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c ,. LIKEFUNC(l
21997 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 ike, 3, &likeInf
21998 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 oNorm, SQLITE_FU
21999 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64 NC_LIKE),. #end
2199a 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 if. };.. int i
2199b 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 ;. FuncDefHash
2199c 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c *pHash = &GLOBAL
2199d 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 (FuncDefHash, sq
2199e 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 lite3GlobalFunct
2199f 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 ions);. FuncDef
219a0 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 *aFunc = (FuncD
219a1 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 ef*)&GLOBAL(Func
219a2 44 65 66 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e Def, aBuiltinFun
219a3 63 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 c);.. for(i=0;
219a4 69 3c 41 72 72 61 79 53 69 7a 65 28 61 42 75 69 i<ArraySize(aBui
219a5 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b ltinFunc); i++){
219a6 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 . sqlite3Func
219a7 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c DefInsert(pHash,
219a8 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d &aFunc[i]);. }
219a9 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 . sqlite3Regist
219aa 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 erDateTimeFuncti
219ab 6f 6e 73 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a ons();.}../*****
219ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
219ad 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a func.c ********
219ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
219af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
219b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
219b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
219b2 66 69 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a 2a file insert.c **
219b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
219b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
219b5 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
219b6 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
219b7 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
219b8 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
219b9 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
219ba 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
219bb 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
219bc 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
219bd 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
219be 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
219bf 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
219c0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
219c1 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
219c2 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
219c3 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
219c4 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
219c5 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
219c6 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
219c7 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
219c8 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
219c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
219ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
219cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
219cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
219cd 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
219ce 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f ntains C code ro
219cf 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 utines that are
219d0 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 called by the pa
219d1 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c rser.** to handl
219d2 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 e INSERT stateme
219d3 6e 74 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a nts in SQLite..*
219d4 2a 0a 2a 2a 20 24 49 64 3a 20 69 6e 73 65 72 74 *.** $Id: insert
219d5 2e 63 2c 76 20 31 2e 32 35 33 20 32 30 30 38 2f .c,v 1.253 2008/
219d6 31 31 2f 31 39 20 30 39 3a 30 35 3a 32 37 20 64 11/19 09:05:27 d
219d7 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
219d8 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 50 .*/../*.** Set P
219d9 34 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 4 of the most re
219da 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 cently inserted
219db 6f 70 63 6f 64 65 20 74 6f 20 61 20 63 6f 6c 75 opcode to a colu
219dc 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 73 mn affinity.** s
219dd 74 72 69 6e 67 20 66 6f 72 20 69 6e 64 65 78 20 tring for index
219de 70 49 64 78 2e 20 41 20 63 6f 6c 75 6d 6e 20 61 pIdx. A column a
219df 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 68 ffinity string h
219e0 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 as one character
219e1 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f 6c .** for each col
219e2 75 6d 6e 20 69 6e 20 74 68 65 20 74 61 62 6c 65 umn in the table
219e3 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 , according to t
219e4 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 he affinity of t
219e5 68 65 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a he column:.**.**
219e6 20 20 43 68 61 72 61 63 74 65 72 20 20 20 20 20 Character
219e7 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 Column affinity
219e8 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** -----------
219e9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
219ea 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20 ---.** 'a'
219eb 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20 TEXT.**
219ec 27 62 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 'b' N
219ed 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20 ONE.** 'c'
219ee 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a NUMERIC.*
219ef 2a 20 20 27 64 27 20 20 20 20 20 20 20 20 20 20 * 'd'
219f0 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65 INTEGER.** 'e
219f1 27 20 20 20 20 20 20 20 20 20 20 20 20 52 45 41 ' REA
219f2 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 74 72 61 L.**.** An extra
219f3 20 27 62 27 20 69 73 20 61 70 70 65 6e 64 65 64 'b' is appended
219f4 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 to the end of t
219f5 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 76 he string to cov
219f6 65 72 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64 20 er the.** rowid
219f7 74 68 61 74 20 61 70 70 65 61 72 73 20 61 73 20 that appears as
219f8 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 the last column
219f9 69 6e 20 65 76 65 72 79 20 69 6e 64 65 78 2e 0a in every index..
219fa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
219fb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e E void sqlite3In
219fc 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 dexAffinityStr(V
219fd 64 62 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70 dbe *v, Index *p
219fe 49 64 78 29 7b 0a 20 20 69 66 28 20 21 70 49 64 Idx){. if( !pId
219ff 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 x->zColAff ){.
21a00 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 /* The first t
21a01 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 ime a column aff
21a02 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 inity string for
21a03 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e a particular in
21a04 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 dex is. ** re
21a05 71 75 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c quired, it is al
21a06 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 located and popu
21a07 6c 61 74 65 64 20 68 65 72 65 2e 20 49 74 20 69 lated here. It i
21a08 73 20 74 68 65 6e 20 73 74 6f 72 65 64 20 61 73 s then stored as
21a09 0a 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72 . ** a member
21a0a 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73 74 of the Index st
21a0b 72 75 63 74 75 72 65 20 66 6f 72 20 73 75 62 73 ructure for subs
21a0c 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 20 20 equent use..
21a0d 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f **. ** The co
21a0e 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 lumn affinity st
21a0f 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 ring will eventu
21a10 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 ally be deleted
21a11 62 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 by. ** sqlite
21a12 44 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77 68 DeleteIndex() wh
21a13 65 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 en the Index str
21a14 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 ucture itself is
21a15 20 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20 cleaned. **
21a16 75 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 up.. */. i
21a17 6e 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20 nt n;. Table
21a18 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 *pTab = pIdx->pT
21a19 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 able;. sqlite
21a1a 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56 3 *db = sqlite3V
21a1b 64 62 65 44 62 28 76 29 3b 0a 20 20 20 20 70 49 dbeDb(v);. pI
21a1c 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28 63 dx->zColAff = (c
21a1d 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c har *)sqlite3Mal
21a1e 6c 6f 63 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d loc(pIdx->nColum
21a1f 6e 2b 32 29 3b 0a 20 20 20 20 69 66 28 20 21 70 n+2);. if( !p
21a20 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a Idx->zColAff ){.
21a21 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 db->malloc
21a22 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 Failed = 1;.
21a23 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
21a24 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70 for(n=0; n<p
21a25 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b Idx->nColumn; n+
21a26 2b 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e +){. pIdx->
21a27 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54 61 zColAff[n] = pTa
21a28 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 b->aCol[pIdx->ai
21a29 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e Column[n]].affin
21a2a 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ity;. }. p
21a2b 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b 2b Idx->zColAff[n++
21a2c 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e ] = SQLITE_AFF_N
21a2d 4f 4e 45 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a ONE;. pIdx->z
21a2e 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a 20 ColAff[n] = 0;.
21a2f 20 7d 0a 20 0a 20 20 73 71 6c 69 74 65 33 56 64 }. . sqlite3Vd
21a30 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 beChangeP4(v, -1
21a31 2c 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 2c , pIdx->zColAff,
21a32 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 0);.}../*.** Se
21a33 74 20 50 34 20 6f 66 20 74 68 65 20 6d 6f 73 74 t P4 of the most
21a34 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 recently insert
21a35 65 64 20 6f 70 63 6f 64 65 20 74 6f 20 61 20 63 ed opcode to a c
21a36 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a olumn affinity.*
21a37 2a 20 73 74 72 69 6e 67 20 66 6f 72 20 74 61 62 * string for tab
21a38 6c 65 20 70 54 61 62 2e 20 41 20 63 6f 6c 75 6d le pTab. A colum
21a39 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e n affinity strin
21a3a 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72 61 63 g has one charac
21a3b 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 ter.** for each
21a3c 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 65 64 20 62 column indexed b
21a3d 79 20 74 68 65 20 69 6e 64 65 78 2c 20 61 63 63 y the index, acc
21a3e 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 61 66 ording to the af
21a3f 66 69 6e 69 74 79 20 6f 66 20 74 68 65 0a 2a 2a finity of the.**
21a40 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20 column:.**.**
21a41 43 68 61 72 61 63 74 65 72 20 20 20 20 20 20 43 Character C
21a42 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a olumn affinity.*
21a43 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * -------------
21a44 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
21a45 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20 20 20 -.** 'a'
21a46 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20 27 62 TEXT.** 'b
21a47 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 4f 4e ' NON
21a48 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20 20 20 E.** 'c'
21a49 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a 20 NUMERIC.**
21a4a 20 27 64 27 20 20 20 20 20 20 20 20 20 20 20 20 'd'
21a4b 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65 27 20 INTEGER.** 'e'
21a4c 20 20 20 20 20 20 20 20 20 20 20 52 45 41 4c 0a REAL.
21a4d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21a4e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 E void sqlite3Ta
21a4f 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 56 bleAffinityStr(V
21a50 64 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70 dbe *v, Table *p
21a51 54 61 62 29 7b 0a 20 20 2f 2a 20 54 68 65 20 66 Tab){. /* The f
21a52 69 72 73 74 20 74 69 6d 65 20 61 20 63 6f 6c 75 irst time a colu
21a53 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 mn affinity stri
21a54 6e 67 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 ng for a particu
21a55 6c 61 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 69 lar table. ** i
21a56 73 20 72 65 71 75 69 72 65 64 2c 20 69 74 20 69 s required, it i
21a57 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 s allocated and
21a58 70 6f 70 75 6c 61 74 65 64 20 68 65 72 65 2e 20 populated here.
21a59 49 74 20 69 73 20 74 68 65 6e 20 0a 20 20 2a 2a It is then . **
21a5a 20 73 74 6f 72 65 64 20 61 73 20 61 20 6d 65 6d stored as a mem
21a5b 62 65 72 20 6f 66 20 74 68 65 20 54 61 62 6c 65 ber of the Table
21a5c 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 structure for s
21a5d 75 62 73 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 ubsequent use..
21a5e 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6c **. ** The col
21a5f 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 umn affinity str
21a60 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 ing will eventua
21a61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 62 lly be deleted b
21a62 79 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 44 65 y. ** sqlite3De
21a63 6c 65 74 65 54 61 62 6c 65 28 29 20 77 68 65 6e leteTable() when
21a64 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 the Table struc
21a65 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20 63 ture itself is c
21a66 6c 65 61 6e 65 64 20 75 70 2e 0a 20 20 2a 2f 0a leaned up.. */.
21a67 20 20 69 66 28 20 21 70 54 61 62 2d 3e 7a 43 6f if( !pTab->zCo
21a68 6c 41 66 66 20 29 7b 0a 20 20 20 20 63 68 61 72 lAff ){. char
21a69 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20 20 20 20 69 *zColAff;. i
21a6a 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 nt i;. sqlite
21a6b 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56 3 *db = sqlite3V
21a6c 64 62 65 44 62 28 76 29 3b 0a 0a 20 20 20 20 7a dbeDb(v);.. z
21a6d 43 6f 6c 41 66 66 20 3d 20 28 63 68 61 72 20 2a ColAff = (char *
21a6e 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 )sqlite3Malloc(p
21a6f 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20 20 Tab->nCol+1);.
21a70 20 20 69 66 28 20 21 7a 43 6f 6c 41 66 66 20 29 if( !zColAff )
21a71 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c {. db->mall
21a72 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
21a73 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
21a74 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 }.. for(i=0;
21a75 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b i<pTab->nCol; i+
21a76 2b 29 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 41 66 +){. zColAf
21a77 66 5b 69 5d 20 3d 20 70 54 61 62 2d 3e 61 43 6f f[i] = pTab->aCo
21a78 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 l[i].affinity;.
21a79 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c 41 66 66 }. zColAff
21a7a 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d 20 27 [pTab->nCol] = '
21a7b 5c 30 27 3b 0a 0a 20 20 20 20 70 54 61 62 2d 3e \0';.. pTab->
21a7c 7a 43 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c 41 66 zColAff = zColAf
21a7d 66 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 f;. }.. sqlite
21a7e 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 3VdbeChangeP4(v,
21a7f 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 -1, pTab->zColA
21a80 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ff, 0);.}../*.**
21a81 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f Return non-zero
21a82 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 70 54 if the table pT
21a83 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 ab in database i
21a84 44 62 20 6f 72 20 61 6e 79 20 6f 66 20 69 74 73 Db or any of its
21a85 20 69 6e 64 69 63 65 73 0a 2a 2a 20 68 61 76 65 indices.** have
21a86 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 61 74 20 been opened at
21a87 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 any point in the
21a88 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 62 65 VDBE program be
21a89 67 69 6e 6e 69 6e 67 20 61 74 20 6c 6f 63 61 74 ginning at locat
21a8a 69 6f 6e 0a 2a 2a 20 69 53 74 61 72 74 41 64 64 ion.** iStartAdd
21a8b 72 20 74 68 72 6f 75 67 68 74 20 74 68 65 20 65 r throught the e
21a8c 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 nd of the progra
21a8d 6d 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 m. This is used
21a8e 20 74 6f 20 73 65 65 20 69 66 20 0a 2a 2a 20 61 to see if .** a
21a8f 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 statement of th
21a90 65 20 66 6f 72 6d 20 20 22 49 4e 53 45 52 54 20 e form "INSERT
21a91 49 4e 54 4f 20 3c 69 44 62 2c 20 70 54 61 62 3e INTO <iDb, pTab>
21a92 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20 63 61 6e SELECT ..." can
21a93 20 0a 2a 2a 20 72 75 6e 20 77 69 74 68 6f 75 74 .** run without
21a94 20 75 73 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 using temporary
21a95 20 74 61 62 6c 65 20 66 6f 72 20 74 68 65 20 72 table for the r
21a96 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 esults of the SE
21a97 4c 45 43 54 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 LECT. .*/.static
21a98 20 69 6e 74 20 72 65 61 64 73 54 61 62 6c 65 28 int readsTable(
21a99 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 53 74 Vdbe *v, int iSt
21a9a 61 72 74 41 64 64 72 2c 20 69 6e 74 20 69 44 62 artAddr, int iDb
21a9b 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a , Table *pTab){.
21a9c 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 int i;. int i
21a9d 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 End = sqlite3Vdb
21a9e 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b eCurrentAddr(v);
21a9f 0a 20 20 66 6f 72 28 69 3d 69 53 74 61 72 74 41 . for(i=iStartA
21aa0 64 64 72 3b 20 69 3c 69 45 6e 64 3b 20 69 2b 2b ddr; i<iEnd; i++
21aa1 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a 70 ){. VdbeOp *p
21aa2 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 Op = sqlite3Vdbe
21aa3 47 65 74 4f 70 28 76 2c 20 69 29 3b 0a 20 20 20 GetOp(v, i);.
21aa4 20 61 73 73 65 72 74 28 20 70 4f 70 21 3d 30 20 assert( pOp!=0
21aa5 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e );. if( pOp->
21aa6 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 52 opcode==OP_OpenR
21aa7 65 61 64 20 26 26 20 70 4f 70 2d 3e 70 33 3d 3d ead && pOp->p3==
21aa8 69 44 62 20 29 7b 0a 20 20 20 20 20 20 49 6e 64 iDb ){. Ind
21aa9 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 20 20 ex *pIndex;.
21aaa 20 20 69 6e 74 20 74 6e 75 6d 20 3d 20 70 4f 70 int tnum = pOp
21aab 2d 3e 70 32 3b 0a 20 20 20 20 20 20 69 66 28 20 ->p2;. if(
21aac 74 6e 75 6d 3d 3d 70 54 61 62 2d 3e 74 6e 75 6d tnum==pTab->tnum
21aad 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
21aae 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 1;. }.
21aaf 20 20 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 for(pIndex=p
21ab0 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e Tab->pIndex; pIn
21ab1 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 dex; pIndex=pInd
21ab2 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 ex->pNext){.
21ab3 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70 49 if( tnum==pI
21ab4 6e 64 65 78 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20 ndex->tnum ){.
21ab5 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 return 1
21ab6 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
21ab7 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 }. }.#ifnde
21ab8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
21ab9 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 RTUALTABLE. i
21aba 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
21abb 4f 50 5f 56 4f 70 65 6e 20 26 26 20 70 4f 70 2d OP_VOpen && pOp-
21abc 3e 70 34 2e 70 56 74 61 62 3d 3d 70 54 61 62 2d >p4.pVtab==pTab-
21abd 3e 70 56 74 61 62 20 29 7b 0a 20 20 20 20 20 20 >pVtab ){.
21abe 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e assert( pOp->p4.
21abf 70 56 74 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 pVtab!=0 );.
21ac0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
21ac1 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 4type==P4_VTAB )
21ac2 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 ;. return 1
21ac3 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
21ac4 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
21ac5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
21ac6 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
21ac7 45 4e 54 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 ENT./*.** Write
21ac8 6f 75 74 20 63 6f 64 65 20 74 6f 20 69 6e 69 74 out code to init
21ac9 69 61 6c 69 7a 65 20 74 68 65 20 61 75 74 6f 69 ialize the autoi
21aca 6e 63 72 65 6d 65 6e 74 20 6c 6f 67 69 63 2e 20 ncrement logic.
21acb 20 54 68 69 73 20 63 6f 64 65 0a 2a 2a 20 6c 6f This code.** lo
21acc 6f 6b 73 20 75 70 20 74 68 65 20 63 75 72 72 65 oks up the curre
21acd 6e 74 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 nt autoincrement
21ace 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 73 71 value in the sq
21acf 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 0a 2a 2a lite_sequence.**
21ad0 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 table and store
21ad1 73 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 20 s that value in
21ad2 61 20 72 65 67 69 73 74 65 72 2e 20 20 43 6f 64 a register. Cod
21ad3 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 0a 2a e generated by.*
21ad4 2a 20 61 75 74 6f 49 6e 63 53 74 65 70 28 29 20 * autoIncStep()
21ad5 77 69 6c 6c 20 6b 65 65 70 20 74 68 61 74 20 72 will keep that r
21ad6 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 egister holding
21ad7 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20 72 the largest.** r
21ad8 6f 77 69 64 20 76 61 6c 75 65 2e 20 20 43 6f 64 owid value. Cod
21ad9 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 e generated by a
21ada 75 74 6f 49 6e 63 45 6e 64 28 29 20 77 69 6c 6c utoIncEnd() will
21adb 20 77 72 69 74 65 20 74 68 65 20 6e 65 77 0a 2a write the new.*
21adc 2a 20 6c 61 72 67 65 73 74 20 76 61 6c 75 65 20 * largest value
21add 6f 66 20 74 68 65 20 63 6f 75 6e 74 65 72 20 62 of the counter b
21ade 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 73 71 6c ack into the sql
21adf 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 ite_sequence tab
21ae0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 le..**.** This r
21ae1 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 outine returns t
21ae2 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
21ae3 6d 65 6d 5b 5d 20 63 65 6c 6c 20 74 68 61 74 20 mem[] cell that
21ae4 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 contains.** the
21ae5 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 63 6f maximum rowid co
21ae6 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 unter..**.** Thr
21ae7 65 65 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 ee consecutive r
21ae8 65 67 69 73 74 65 72 73 20 61 72 65 20 61 6c 6c egisters are all
21ae9 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 72 ocated by this r
21aea 6f 75 74 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 outine. The.**
21aeb 66 69 72 73 74 20 74 77 6f 20 68 6f 6c 64 20 74 first two hold t
21aec 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
21aed 61 72 67 65 74 20 74 61 62 6c 65 20 61 6e 64 20 arget table and
21aee 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 the maximum rowi
21aef 64 20 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 d .** inserted i
21af0 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74 20 74 nto the target t
21af1 61 62 6c 65 2c 20 72 65 73 70 65 63 74 69 76 65 able, respective
21af2 6c 79 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 ly..** The third
21af3 20 68 6f 6c 64 73 20 74 68 65 20 72 6f 77 69 64 holds the rowid
21af4 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65 in sqlite_seque
21af5 6e 63 65 20 77 68 65 72 65 20 77 65 20 77 69 6c nce where we wil
21af6 6c 0a 2a 2a 20 77 72 69 74 65 20 62 61 63 6b 20 l.** write back
21af7 74 68 65 20 72 65 76 69 73 65 64 20 6d 61 78 69 the revised maxi
21af8 6d 75 6d 20 72 6f 77 69 64 2e 20 20 54 68 69 73 mum rowid. This
21af9 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
21afa 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 the.** index of
21afb 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 66 20 74 the second of t
21afc 68 65 73 65 20 74 68 72 65 65 20 72 65 67 69 73 hese three regis
21afd 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ters..*/.static
21afe 69 6e 74 20 61 75 74 6f 49 6e 63 42 65 67 69 6e int autoIncBegin
21aff 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
21b00 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 e, /* Parsi
21b01 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
21b02 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 int iDb,
21b03 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
21b04 74 68 65 20 64 61 74 61 62 61 73 65 20 68 6f 6c the database hol
21b05 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 54 ding pTab */. T
21b06 61 62 6c 65 20 2a 70 54 61 62 20 20 20 20 20 20 able *pTab
21b07 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
21b08 77 65 20 61 72 65 20 77 72 69 74 69 6e 67 20 74 we are writing t
21b09 6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6d 65 o */.){. int me
21b0a 6d 49 64 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a mId = 0; /*
21b0b 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e Register holdin
21b0c 67 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 g maximum rowid
21b0d 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 */. if( pTab->t
21b0e 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 abFlags & TF_Aut
21b0f 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 oincrement ){.
21b10 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
21b11 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20 44 se->pVdbe;. D
21b12 62 20 2a 70 44 62 20 3d 20 26 70 50 61 72 73 65 b *pDb = &pParse
21b13 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a ->db->aDb[iDb];.
21b14 20 20 20 20 69 6e 74 20 69 43 75 72 20 3d 20 70 int iCur = p
21b15 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 Parse->nTab;.
21b16 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 int addr;
21b17 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 /* Addr
21b18 65 73 73 20 6f 66 20 74 68 65 20 74 6f 70 20 6f ess of the top o
21b19 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 f the loop */.
21b1a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 assert( v );.
21b1b 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b pParse->nMem+
21b1c 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f +; /* Ho
21b1d 6c 64 73 20 6e 61 6d 65 20 6f 66 20 74 61 62 6c lds name of tabl
21b1e 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 49 64 20 3d e */. memId =
21b1f 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
21b20 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 . pParse->nMe
21b21 6d 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 m++;. sqlite3
21b22 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 OpenTable(pParse
21b23 2c 20 69 43 75 72 2c 20 69 44 62 2c 20 70 44 62 , iCur, iDb, pDb
21b24 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 ->pSchema->pSeqT
21b25 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 ab, OP_OpenRead)
21b26 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c ;. addr = sql
21b27 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 ite3VdbeCurrentA
21b28 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 ddr(v);. sqli
21b29 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
21b2a 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 OP_String8, 0,
21b2b 6d 65 6d 49 64 2d 31 2c 20 30 2c 20 70 54 61 62 memId-1, 0, pTab
21b2c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 ->zName, 0);.
21b2d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21b2e 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c p2(v, OP_Rewind,
21b2f 20 69 43 75 72 2c 20 61 64 64 72 2b 39 29 3b 0a iCur, addr+9);.
21b30 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21b31 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 ddOp3(v, OP_Colu
21b32 6d 6e 2c 20 69 43 75 72 2c 20 30 2c 20 6d 65 6d mn, iCur, 0, mem
21b33 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Id);. sqlite3
21b34 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21b35 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31 2c 20 61 64 _Ne, memId-1, ad
21b36 64 72 2b 37 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 dr+7, memId);.
21b37 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
21b38 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f ngeP5(v, SQLITE_
21b39 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 JUMPIFNULL);.
21b3a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21b3b 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 p2(v, OP_Rowid,
21b3c 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a iCur, memId+1);.
21b3d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21b3e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 ddOp3(v, OP_Colu
21b3f 6d 6e 2c 20 69 43 75 72 2c 20 31 2c 20 6d 65 6d mn, iCur, 1, mem
21b40 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Id);. sqlite3
21b41 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21b42 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 2b 39 _Goto, 0, addr+9
21b43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
21b44 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
21b45 65 78 74 2c 20 69 43 75 72 2c 20 61 64 64 72 2b ext, iCur, addr+
21b46 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 2);. sqlite3V
21b47 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21b48 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d 65 6d 49 Integer, 0, memI
21b49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 d);. sqlite3V
21b4a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21b4b 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30 29 3b Close, iCur, 0);
21b4c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 65 . }. return me
21b4d 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 mId;.}../*.** Up
21b4e 64 61 74 65 20 74 68 65 20 6d 61 78 69 6d 75 6d date the maximum
21b4f 20 72 6f 77 69 64 20 66 6f 72 20 61 6e 20 61 75 rowid for an au
21b50 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63 61 6c 63 toincrement calc
21b51 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 ulation..**.** T
21b52 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 his routine shou
21b53 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 ld be called whe
21b54 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 n the top of the
21b55 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61 0a 2a stack holds a.*
21b56 2a 20 6e 65 77 20 72 6f 77 69 64 20 74 68 61 74 * new rowid that
21b57 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 20 is about to be
21b58 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20 74 68 inserted. If th
21b59 61 74 20 6e 65 77 20 72 6f 77 69 64 20 69 73 0a at new rowid is.
21b5a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 ** larger than t
21b5b 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 he maximum rowid
21b5c 20 69 6e 20 74 68 65 20 6d 65 6d 49 64 20 6d 65 in the memId me
21b5d 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68 65 6e 20 mory cell, then
21b5e 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 the.** memory ce
21b5f 6c 6c 20 69 73 20 75 70 64 61 74 65 64 2e 20 20 ll is updated.
21b60 54 68 65 20 73 74 61 63 6b 20 69 73 20 75 6e 63 The stack is unc
21b61 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 hanged..*/.stati
21b62 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63 53 74 c void autoIncSt
21b63 65 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ep(Parse *pParse
21b64 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20 69 6e 74 , int memId, int
21b65 20 72 65 67 52 6f 77 69 64 29 7b 0a 20 20 69 66 regRowid){. if
21b66 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a 20 20 20 ( memId>0 ){.
21b67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21b68 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 p2(pParse->pVdbe
21b69 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d 65 6d , OP_MemMax, mem
21b6a 49 64 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 Id, regRowid);.
21b6b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 66 74 65 }.}../*.** Afte
21b6c 72 20 64 6f 69 6e 67 20 6f 6e 65 20 6f 72 20 6d r doing one or m
21b6d 6f 72 65 20 69 6e 73 65 72 74 73 2c 20 74 68 65 ore inserts, the
21b6e 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 69 maximum rowid i
21b6f 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 72 s stored.** in r
21b70 65 67 5b 6d 65 6d 49 64 5d 2e 20 20 47 65 6e 65 eg[memId]. Gene
21b71 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72 69 rate code to wri
21b72 74 65 20 74 68 69 73 20 76 61 6c 75 65 20 62 61 te this value ba
21b73 63 6b 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 74 ck into the.** t
21b74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e he sqlite_sequen
21b75 63 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 ce table..*/.sta
21b76 74 69 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63 tic void autoInc
21b77 45 6e 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50 End(. Parse *pP
21b78 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 54 68 65 arse, /* The
21b79 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 parsing context
21b7a 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 */. int iDb,
21b7b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
21b7c 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 x of the databas
21b7d 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a e holding pTab *
21b7e 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c /. Table *pTab,
21b7f 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 /* Table
21b80 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 we are inserting
21b81 20 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6d into */. int m
21b82 65 6d 49 64 20 20 20 20 20 20 20 20 20 20 2f 2a emId /*
21b83 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 68 6f 6c Memory cell hol
21b84 64 69 6e 67 20 74 68 65 20 6d 61 78 69 6d 75 6d ding the maximum
21b85 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 69 rowid */.){. i
21b86 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 f( pTab->tabFlag
21b87 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 s & TF_Autoincre
21b88 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 ment ){. int
21b89 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e iCur = pParse->n
21b8a 54 61 62 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 Tab;. Vdbe *v
21b8b 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
21b8c 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 ;. Db *pDb =
21b8d 26 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 &pParse->db->aDb
21b8e 5b 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20 6a [iDb];. int j
21b8f 31 3b 0a 20 20 20 20 69 6e 74 20 69 52 65 63 20 1;. int iRec
21b90 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
21b91 3b 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 ; /* Memory c
21b92 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 72 65 63 ell used for rec
21b93 6f 72 64 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 ord */.. asse
21b94 72 74 28 20 76 20 29 3b 0a 20 20 20 20 73 71 6c rt( v );. sql
21b95 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 ite3OpenTable(pP
21b96 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c arse, iCur, iDb,
21b97 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 pDb->pSchema->p
21b98 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57 SeqTab, OP_OpenW
21b99 72 69 74 65 29 3b 0a 20 20 20 20 6a 31 20 3d 20 rite);. j1 =
21b9a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21b9b 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 1(v, OP_NotNull,
21b9c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73 memId+1);. s
21b9d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21b9e 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c (v, OP_NewRowid,
21b9f 20 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31 29 3b iCur, memId+1);
21ba0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
21ba1 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b JumpHere(v, j1);
21ba2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
21ba3 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b AddOp3(v, OP_Mak
21ba4 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64 2d 31 eRecord, memId-1
21ba5 2c 20 32 2c 20 69 52 65 63 29 3b 0a 20 20 20 20 , 2, iRec);.
21ba6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21ba7 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 3(v, OP_Insert,
21ba8 69 43 75 72 2c 20 69 52 65 63 2c 20 6d 65 6d 49 iCur, iRec, memI
21ba9 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 d+1);. sqlite
21baa 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 3VdbeChangeP5(v,
21bab 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b OPFLAG_APPEND);
21bac 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
21bad 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f AddOp1(v, OP_Clo
21bae 73 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d 0a 7d se, iCur);. }.}
21baf 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49 66 20 .#else./*.** If
21bb0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
21bb1 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64 65 66 INCREMENT is def
21bb2 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 74 ined, then the t
21bb3 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a hree routines.**
21bb4 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c 20 6e above are all n
21bb5 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66 69 6e o-ops.*/.# defin
21bb6 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 41 e autoIncBegin(A
21bb7 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65 66 69 ,B,C) (0).# defi
21bb8 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70 28 41 ne autoIncStep(A
21bb9 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e 65 20 61 ,B,C).# define a
21bba 75 74 6f 49 6e 63 45 6e 64 28 41 2c 42 2c 43 2c utoIncEnd(A,B,C,
21bbb 44 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c D).#endif /* SQL
21bbc 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
21bbd 52 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a 20 46 REMENT */.../* F
21bbe 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
21bbf 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 on */.static int
21bc0 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f xferOptimizatio
21bc1 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 n(. Parse *pPar
21bc2 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 se, /* Pa
21bc3 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a rser context */.
21bc4 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 Table *pDest,
21bc5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
21bc6 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 able we are inse
21bc7 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 rting into */.
21bc8 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c Select *pSelect,
21bc9 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 /* A SELEC
21bca 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 T statement to u
21bcb 73 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73 se as the data s
21bcc 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f ource */. int o
21bcd 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 nError,
21bce 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c /* How to handl
21bcf 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 e constraint err
21bd0 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 ors */. int iDb
21bd1 44 65 73 74 20 20 20 20 20 20 20 20 20 20 20 2f Dest /
21bd2 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f * The database o
21bd3 66 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a 0a 2f f pDest */.);../
21bd4 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
21bd5 65 20 69 73 20 63 61 6c 6c 20 74 6f 20 68 61 6e e is call to han
21bd6 64 6c 65 20 53 51 4c 20 6f 66 20 74 68 65 20 66 dle SQL of the f
21bd7 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a ollowing forms:.
21bd8 2a 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72 74 20 **.** insert
21bd9 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 4c 49 into TABLE (IDLI
21bda 53 54 29 20 76 61 6c 75 65 73 28 45 58 50 52 4c ST) values(EXPRL
21bdb 49 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73 65 72 IST).** inser
21bdc 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 t into TABLE (ID
21bdd 4c 49 53 54 29 20 73 65 6c 65 63 74 0a 2a 2a 0a LIST) select.**.
21bde 2a 2a 20 54 68 65 20 49 44 4c 49 53 54 20 66 6f ** The IDLIST fo
21bdf 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 61 62 6c llowing the tabl
21be0 65 20 6e 61 6d 65 20 69 73 20 61 6c 77 61 79 73 e name is always
21be1 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 6f optional. If o
21be2 6d 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 mitted,.** then
21be3 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f a list of all co
21be4 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 74 61 lumns for the ta
21be5 62 6c 65 20 69 73 20 73 75 62 73 74 69 74 75 74 ble is substitut
21be6 65 64 2e 20 20 54 68 65 20 49 44 4c 49 53 54 0a ed. The IDLIST.
21be7 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 ** appears in th
21be8 65 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61 6d 65 e pColumn parame
21be9 74 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20 69 73 ter. pColumn is
21bea 20 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53 54 20 NULL if IDLIST
21beb 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a is omitted..**.*
21bec 2a 20 54 68 65 20 70 4c 69 73 74 20 70 61 72 61 * The pList para
21bed 6d 65 74 65 72 20 68 6f 6c 64 73 20 45 58 50 52 meter holds EXPR
21bee 4c 49 53 54 20 69 6e 20 74 68 65 20 66 69 72 73 LIST in the firs
21bef 74 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 49 4e t form of the IN
21bf0 53 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e SERT.** statemen
21bf1 74 20 61 62 6f 76 65 2c 20 61 6e 64 20 70 53 65 t above, and pSe
21bf2 6c 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20 20 46 lect is NULL. F
21bf3 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f or the second fo
21bf4 72 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20 rm, pList is.**
21bf5 4e 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65 63 74 NULL and pSelect
21bf6 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
21bf7 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 61 74 the select stat
21bf8 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 67 65 ement used to ge
21bf9 6e 65 72 61 74 65 0a 2a 2a 20 64 61 74 61 20 66 nerate.** data f
21bfa 6f 72 20 74 68 65 20 69 6e 73 65 72 74 2e 0a 2a or the insert..*
21bfb 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 *.** The code ge
21bfc 6e 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77 73 20 nerated follows
21bfd 6f 6e 65 20 6f 66 20 66 6f 75 72 20 74 65 6d 70 one of four temp
21bfe 6c 61 74 65 73 2e 20 20 46 6f 72 20 61 20 73 69 lates. For a si
21bff 6d 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74 20 77 mple.** select w
21c00 69 74 68 20 64 61 74 61 20 63 6f 6d 69 6e 67 20 ith data coming
21c01 66 72 6f 6d 20 61 20 56 41 4c 55 45 53 20 63 6c from a VALUES cl
21c02 61 75 73 65 2c 20 74 68 65 20 63 6f 64 65 20 65 ause, the code e
21c03 78 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63 65 20 xecutes.** once
21c04 73 74 72 61 69 67 68 74 20 64 6f 77 6e 20 74 68 straight down th
21c05 72 6f 75 67 68 2e 20 20 50 73 65 75 64 6f 2d 63 rough. Pseudo-c
21c06 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 28 77 65 20 ode follows (we
21c07 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 74 68 65 call this.** the
21c08 20 22 31 73 74 20 74 65 6d 70 6c 61 74 65 22 29 "1st template")
21c09 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 :.**.**
21c0a 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f open write curso
21c0b 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64 r to <table> and
21c0c 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 its indices.**
21c0d 20 20 20 20 20 20 20 20 70 75 74 73 20 56 41 4c puts VAL
21c0e 55 45 53 20 63 6c 61 75 73 65 20 65 78 70 72 65 UES clause expre
21c0f 73 73 69 6f 6e 73 20 6f 6e 74 6f 20 74 68 65 20 ssions onto the
21c10 73 74 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 stack.**
21c11 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c write the resul
21c12 74 69 6e 67 20 72 65 63 6f 72 64 20 69 6e 74 6f ting record into
21c13 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 <table>.**
21c14 20 20 20 20 63 6c 65 61 6e 75 70 0a 2a 2a 0a 2a cleanup.**.*
21c15 2a 20 54 68 65 20 74 68 72 65 65 20 72 65 6d 61 * The three rema
21c16 69 6e 69 6e 67 20 74 65 6d 70 6c 61 74 65 73 20 ining templates
21c17 61 73 73 75 6d 65 20 74 68 65 20 73 74 61 74 65 assume the state
21c18 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 ment is of the f
21c19 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 orm.**.** INSE
21c1a 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 3e 20 RT INTO <table>
21c1b 53 45 4c 45 43 54 20 2e 2e 2e 0a 2a 2a 0a 2a 2a SELECT ....**.**
21c1c 20 49 66 20 74 68 65 20 53 45 4c 45 43 54 20 63 If the SELECT c
21c1d 6c 61 75 73 65 20 69 73 20 6f 66 20 74 68 65 20 lause is of the
21c1e 72 65 73 74 72 69 63 74 65 64 20 66 6f 72 6d 20 restricted form
21c1f 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c "SELECT * FROM <
21c20 74 61 62 6c 65 32 3e 22 20 2d 0a 2a 2a 20 69 6e table2>" -.** in
21c21 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 20 other words if
21c22 74 68 65 20 53 45 4c 45 43 54 20 70 75 6c 6c 73 the SELECT pulls
21c23 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 72 6f all columns fro
21c24 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 m a single table
21c25 0a 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 69 73 .** and there is
21c26 20 6e 6f 20 57 48 45 52 45 20 6f 72 20 4c 49 4d no WHERE or LIM
21c27 49 54 20 6f 72 20 47 52 4f 55 50 20 42 59 20 6f IT or GROUP BY o
21c28 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 r ORDER BY claus
21c29 65 73 2c 20 61 6e 64 0a 2a 2a 20 69 66 20 3c 74 es, and.** if <t
21c2a 61 62 6c 65 32 3e 20 61 6e 64 20 3c 74 61 62 6c able2> and <tabl
21c2b 65 31 3e 20 61 72 65 20 64 69 73 74 69 6e 63 74 e1> are distinct
21c2c 20 74 61 62 6c 65 73 20 62 75 74 20 68 61 76 65 tables but have
21c2d 20 69 64 65 6e 74 69 63 61 6c 0a 2a 2a 20 73 63 identical.** sc
21c2e 68 65 6d 61 73 2c 20 69 6e 63 6c 75 64 69 6e 67 hemas, including
21c2f 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 20 69 6e all the same in
21c30 64 69 63 65 73 2c 20 74 68 65 6e 20 61 20 73 70 dices, then a sp
21c31 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69 ecial optimizati
21c32 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 on.** is invoked
21c33 20 74 68 61 74 20 63 6f 70 69 65 73 20 72 61 77 that copies raw
21c34 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 3c 74 records from <t
21c35 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f 20 3c able2> over to <
21c36 74 61 62 6c 65 31 3e 2e 0a 2a 2a 20 53 65 65 20 table1>..** See
21c37 74 68 65 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 the xferOptimiza
21c38 74 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 tion() function
21c39 66 6f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e for the implemen
21c3a 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 0a 2a tation of this.*
21c3b 2a 20 74 65 6d 70 6c 61 74 65 2e 20 20 54 68 69 * template. Thi
21c3c 73 20 69 73 20 74 68 65 20 32 6e 64 20 74 65 6d s is the 2nd tem
21c3d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 plate..**.**
21c3e 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72 69 74 open a writ
21c3f 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 e cursor to <tab
21c40 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f le>.** o
21c41 70 65 6e 20 72 65 61 64 20 63 75 72 73 6f 72 20 pen read cursor
21c42 6f 6e 20 3c 74 61 62 6c 65 32 3e 0a 2a 2a 20 20 on <table2>.**
21c43 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 20 transfer
21c44 61 6c 6c 20 72 65 63 6f 72 64 73 20 69 6e 20 3c all records in <
21c45 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f 20 table2> over to
21c46 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 <table>.**
21c47 20 20 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73 close cursors
21c48 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 6f 72 65 .** fore
21c49 61 63 68 20 69 6e 64 65 78 20 6f 6e 20 3c 74 61 ach index on <ta
21c4a 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 ble>.**
21c4b 20 20 6f 70 65 6e 20 61 20 77 72 69 74 65 20 63 open a write c
21c4c 75 72 73 6f 72 20 6f 6e 20 74 68 65 20 3c 74 61 ursor on the <ta
21c4d 62 6c 65 3e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 ble> index.**
21c4e 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 72 open a r
21c4f 65 61 64 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 ead cursor on th
21c50 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
21c51 3c 74 61 62 6c 65 32 3e 20 69 6e 64 65 78 0a 2a <table2> index.*
21c52 2a 20 20 20 20 20 20 20 20 20 20 20 74 72 61 6e * tran
21c53 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64 73 sfer all records
21c54 20 66 72 6f 6d 20 74 68 65 20 72 65 61 64 20 74 from the read t
21c55 6f 20 74 68 65 20 77 72 69 74 65 20 63 75 72 73 o the write curs
21c56 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ors.**
21c57 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73 0a 2a close cursors.*
21c58 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 66 6f * end fo
21c59 72 65 61 63 68 0a 2a 2a 0a 2a 2a 20 54 68 65 20 reach.**.** The
21c5a 33 72 64 20 74 65 6d 70 6c 61 74 65 20 69 73 20 3rd template is
21c5b 66 6f 72 20 77 68 65 6e 20 74 68 65 20 73 65 63 for when the sec
21c5c 6f 6e 64 20 74 65 6d 70 6c 61 74 65 20 64 6f 65 ond template doe
21c5d 73 20 6e 6f 74 20 61 70 70 6c 79 0a 2a 2a 20 61 s not apply.** a
21c5e 6e 64 20 74 68 65 20 53 45 4c 45 43 54 20 63 6c nd the SELECT cl
21c5f 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 ause does not re
21c60 61 64 20 66 72 6f 6d 20 3c 74 61 62 6c 65 3e 20 ad from <table>
21c61 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 20 at any time..**
21c62 54 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f The generated co
21c63 64 65 20 66 6f 6c 6c 6f 77 73 20 74 68 69 73 20 de follows this
21c64 74 65 6d 70 6c 61 74 65 3a 0a 2a 2a 0a 2a 2a 20 template:.**.**
21c65 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30 EOF <- 0
21c66 0a 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d .** X <-
21c67 20 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f A.** go
21c68 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41 3a 20 to B.** A:
21c69 73 65 74 75 70 20 66 6f 72 20 74 68 65 20 53 45 setup for the SE
21c6a 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 LECT.**
21c6b 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 72 6f loop over the ro
21c6c 77 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 ws in the SELECT
21c6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6c 6f .** lo
21c6e 61 64 20 76 61 6c 75 65 73 20 69 6e 74 6f 20 72 ad values into r
21c6f 65 67 69 73 74 65 72 73 20 52 2e 2e 52 2b 6e 0a egisters R..R+n.
21c70 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65 ** yie
21c71 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 ld X.**
21c72 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 end loop.**
21c73 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65 cleanup afte
21c74 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 r the SELECT.**
21c75 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31 EOF <- 1
21c76 0a 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c .** yiel
21c77 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 d X.** g
21c78 6f 74 6f 20 41 0a 2a 2a 20 20 20 20 20 20 42 3a oto A.** B:
21c79 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 open write curs
21c7a 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e or to <table> an
21c7b 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a d its indices.**
21c7c 20 20 20 20 20 20 43 3a 20 79 69 65 6c 64 20 58 C: yield X
21c7d 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 .** if E
21c7e 4f 46 20 67 6f 74 6f 20 44 0a 2a 2a 20 20 20 20 OF goto D.**
21c7f 20 20 20 20 20 69 6e 73 65 72 74 20 74 68 65 20 insert the
21c80 73 65 6c 65 63 74 20 72 65 73 75 6c 74 20 69 6e select result in
21c81 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f 6d 20 to <table> from
21c82 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 R..R+n.**
21c83 20 20 67 6f 74 6f 20 43 0a 2a 2a 20 20 20 20 20 goto C.**
21c84 20 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2a 0a 2a D: cleanup.**.*
21c85 2a 20 54 68 65 20 34 74 68 20 74 65 6d 70 6c 61 * The 4th templa
21c86 74 65 20 69 73 20 75 73 65 64 20 69 66 20 74 68 te is used if th
21c87 65 20 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65 e insert stateme
21c88 6e 74 20 74 61 6b 65 73 20 69 74 73 0a 2a 2a 20 nt takes its.**
21c89 76 61 6c 75 65 73 20 66 72 6f 6d 20 61 20 53 45 values from a SE
21c8a 4c 45 43 54 20 62 75 74 20 74 68 65 20 64 61 74 LECT but the dat
21c8b 61 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 a is being inser
21c8c 74 65 64 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 ted into a table
21c8d 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 6c 73 6f .** that is also
21c8e 20 72 65 61 64 20 61 73 20 70 61 72 74 20 6f 66 read as part of
21c8f 20 74 68 65 20 53 45 4c 45 43 54 2e 20 20 49 6e the SELECT. In
21c90 20 74 68 65 20 74 68 69 72 64 20 66 6f 72 6d 2c the third form,
21c91 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 75 .** we have to u
21c92 73 65 20 61 20 69 6e 74 65 72 6d 65 64 69 61 74 se a intermediat
21c93 65 20 74 61 62 6c 65 20 74 6f 20 73 74 6f 72 65 e table to store
21c94 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 0a the results of.
21c95 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 2e 20 20 ** the select.
21c96 54 68 65 20 74 65 6d 70 6c 61 74 65 20 69 73 20 The template is
21c97 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a like this:.**.**
21c98 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 EOF <-
21c99 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c 0.** X <
21c9a 2d 20 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 - A.** g
21c9b 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41 3a oto B.** A:
21c9c 20 73 65 74 75 70 20 66 6f 72 20 74 68 65 20 53 setup for the S
21c9d 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 ELECT.**
21c9e 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 74 loop over the t
21c9f 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53 45 4c ables in the SEL
21ca0 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ECT.**
21ca1 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e 74 6f load value into
21ca2 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e register R..R+n
21ca3 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 .** yi
21ca4 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 eld X.**
21ca5 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 end loop.**
21ca6 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 cleanup aft
21ca7 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a er the SELECT.**
21ca8 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 EOF <-
21ca9 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 1.** yie
21caa 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 ld X.**
21cab 68 61 6c 74 2d 65 72 72 6f 72 0a 2a 2a 20 20 20 halt-error.**
21cac 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20 B: open temp
21cad 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 4c 3a table.** L:
21cae 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 yield X.**
21caf 20 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 if EOF goto
21cb0 4d 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 M.** ins
21cb1 65 72 74 20 72 6f 77 20 66 72 6f 6d 20 52 2e 2e ert row from R..
21cb2 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20 74 61 R+n into temp ta
21cb3 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 ble.** g
21cb4 6f 74 6f 20 4c 0a 2a 2a 20 20 20 20 20 20 4d 3a oto L.** M:
21cb5 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 open write curs
21cb6 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e or to <table> an
21cb7 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a d its indices.**
21cb8 20 20 20 20 20 20 20 20 20 72 65 77 69 6e 64 20 rewind
21cb9 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 temp table.**
21cba 20 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 C: loop over
21cbb 72 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64 rows of intermed
21cbc 69 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 iate table.**
21cbd 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 transfer
21cbe 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69 6e 74 values form int
21cbf 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20 ermediate table
21cc0 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 into <table>.**
21cc1 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 end loop
21cc2 0a 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 .** D: clea
21cc3 6e 75 70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nup.*/.SQLITE_PR
21cc4 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
21cc5 65 33 49 6e 73 65 72 74 28 0a 20 20 50 61 72 73 e3Insert(. Pars
21cc6 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
21cc7 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 /* Parser cont
21cc8 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 ext */. SrcList
21cc9 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f *pTabList, /
21cca 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 * Name of table
21ccb 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 into which we ar
21ccc 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 e inserting */.
21ccd 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
21cce 2c 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f , /* List o
21ccf 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 f values to be i
21cd0 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53 65 6c nserted */. Sel
21cd1 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 ect *pSelect,
21cd2 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 /* A SELECT s
21cd3 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73 65 20 tatement to use
21cd4 61 73 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 as the data sour
21cd5 63 65 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a ce */. IdList *
21cd6 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 2f 2a pColumn, /*
21cd7 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 63 6f Column names co
21cd8 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 49 rresponding to I
21cd9 44 4c 49 53 54 2e 20 2a 2f 0a 20 20 69 6e 74 20 DLIST. */. int
21cda 6f 6e 45 72 72 6f 72 20 20 20 20 20 20 20 20 20 onError
21cdb 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 /* How to hand
21cdc 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 le constraint er
21cdd 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c rors */.){. sql
21cde 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
21cdf 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 /* The main d
21ce0 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 atabase structur
21ce1 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 e */. Table *pT
21ce2 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ab; /*
21ce3 54 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 The table to ins
21ce4 65 72 74 20 69 6e 74 6f 2e 20 20 61 6b 61 20 54 ert into. aka T
21ce5 41 42 4c 45 20 2a 2f 0a 20 20 63 68 61 72 20 2a ABLE */. char *
21ce6 7a 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 zTab;
21ce7 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 /* Name of the t
21ce8 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 able into which
21ce9 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 we are inserting
21cea 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
21ceb 20 2a 7a 44 62 3b 20 20 20 20 20 20 2f 2a 20 4e *zDb; /* N
21cec 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
21ced 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 ase holding this
21cee 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
21cef 69 2c 20 6a 2c 20 69 64 78 3b 20 20 20 20 20 20 i, j, idx;
21cf0 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
21cf1 72 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b rs */. Vdbe *v;
21cf2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21cf3 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 Generate code i
21cf4 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c nto this virtual
21cf5 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 49 6e machine */. In
21cf6 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 dex *pIdx;
21cf7 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 /* For loopi
21cf8 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20 ng over indices
21cf9 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a of the table */.
21cfa 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 int nColumn;
21cfb 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
21cfc 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
21cfd 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e the data */. in
21cfe 74 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 20 20 t nHidden = 0;
21cff 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
21d00 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 73 20 hidden columns
21d01 69 66 20 54 41 42 4c 45 20 69 73 20 76 69 72 74 if TABLE is virt
21d02 75 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 ual */. int bas
21d03 65 43 75 72 20 3d 20 30 3b 20 20 20 20 20 20 2f eCur = 0; /
21d04 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 * VDBE Cursor nu
21d05 6d 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f mber for pTab */
21d06 0a 20 20 69 6e 74 20 6b 65 79 43 6f 6c 75 6d 6e . int keyColumn
21d07 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 43 6f 6c 75 = -1; /* Colu
21d08 6d 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 49 mn that is the I
21d09 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
21d0a 45 59 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f EY */. int endO
21d0b 66 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 2f 2a fLoop; /*
21d0c 20 4c 61 62 65 6c 20 66 6f 72 20 74 68 65 20 65 Label for the e
21d0d 6e 64 20 6f 66 20 74 68 65 20 69 6e 73 65 72 74 nd of the insert
21d0e 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e ion loop */. in
21d0f 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d t useTempTable =
21d10 20 30 3b 20 2f 2a 20 53 74 6f 72 65 20 53 45 4c 0; /* Store SEL
21d11 45 43 54 20 72 65 73 75 6c 74 73 20 69 6e 20 69 ECT results in i
21d12 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c ntermediate tabl
21d13 65 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63 54 61 e */. int srcTa
21d14 62 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 b = 0; /*
21d15 44 61 74 61 20 63 6f 6d 65 73 20 66 72 6f 6d 20 Data comes from
21d16 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 63 this temporary c
21d17 75 72 73 6f 72 20 69 66 20 3e 3d 30 20 2a 2f 0a ursor if >=0 */.
21d18 20 20 69 6e 74 20 61 64 64 72 49 6e 73 54 6f 70 int addrInsTop
21d19 20 3d 20 30 3b 20 20 20 2f 2a 20 4a 75 6d 70 20 = 0; /* Jump
21d1a 74 6f 20 6c 61 62 65 6c 20 22 44 22 20 2a 2f 0a to label "D" */.
21d1b 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 20 3d int addrCont =
21d1c 20 30 3b 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 0; /* Top o
21d1d 66 20 69 6e 73 65 72 74 20 6c 6f 6f 70 2e 20 4c f insert loop. L
21d1e 61 62 65 6c 20 22 43 22 20 69 6e 20 74 65 6d 70 abel "C" in temp
21d1f 6c 61 74 65 73 20 33 20 61 6e 64 20 34 20 2a 2f lates 3 and 4 */
21d20 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 . int addrSelec
21d21 74 20 3d 20 30 3b 20 20 20 2f 2a 20 41 64 64 72 t = 0; /* Addr
21d22 65 73 73 20 6f 66 20 63 6f 72 6f 75 74 69 6e 65 ess of coroutine
21d23 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
21d24 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 the SELECT */.
21d25 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 SelectDest dest
21d26 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e ; /* Destin
21d27 61 74 69 6f 6e 20 66 6f 72 20 53 45 4c 45 43 54 ation for SELECT
21d28 20 6f 6e 20 72 68 73 20 6f 66 20 49 4e 53 45 52 on rhs of INSER
21d29 54 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 T */. int newId
21d2a 78 20 3d 20 2d 31 3b 20 20 20 20 20 20 2f 2a 20 x = -1; /*
21d2b 43 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 4e Cursor for the N
21d2c 45 57 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 EW pseudo-table
21d2d 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 */. int iDb;
21d2e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
21d2f 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 dex of database
21d30 68 6f 6c 64 69 6e 67 20 54 41 42 4c 45 20 2a 2f holding TABLE */
21d31 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20 . Db *pDb;
21d32 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
21d33 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e database contain
21d34 69 6e 67 20 74 61 62 6c 65 20 62 65 69 6e 67 20 ing table being
21d35 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 2a 2f inserted into */
21d36 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 46 6c 61 . int appendFla
21d37 67 20 3d 20 30 3b 20 20 20 2f 2a 20 54 72 75 65 g = 0; /* True
21d38 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20 69 if the insert i
21d39 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 s likely to be a
21d3a 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 0a 20 20 2f n append */.. /
21d3b 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 6f 63 * Register alloc
21d3c 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 ations */. int
21d3d 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 3b 20 20 regFromSelect;
21d3e 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 /* Base regist
21d3f 65 72 20 66 6f 72 20 64 61 74 61 20 63 6f 6d 69 er for data comi
21d40 6e 67 20 66 72 6f 6d 20 53 45 4c 45 43 54 20 2a ng from SELECT *
21d41 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f 69 /. int regAutoi
21d42 6e 63 20 3d 20 30 3b 20 20 20 2f 2a 20 52 65 67 nc = 0; /* Reg
21d43 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 68 ister holding th
21d44 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 e AUTOINCREMENT
21d45 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
21d46 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 30 regRowCount = 0
21d47 3b 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c ; /* Memory cel
21d48 6c 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 72 l used for the r
21d49 6f 77 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 ow counter */.
21d4a 69 6e 74 20 72 65 67 49 6e 73 3b 20 20 20 20 20 int regIns;
21d4b 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20 6f /* Block o
21d4c 66 20 72 65 67 73 20 68 6f 6c 64 69 6e 67 20 72 f regs holding r
21d4d 6f 77 69 64 2b 64 61 74 61 20 62 65 69 6e 67 20 owid+data being
21d4e 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e inserted */. in
21d4f 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 t regRowid;
21d50 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65 72 73 /* registers
21d51 20 68 6f 6c 64 69 6e 67 20 69 6e 73 65 72 74 20 holding insert
21d52 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 rowid */. int r
21d53 65 67 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 egData;
21d54 20 2f 2a 20 72 65 67 69 73 74 65 72 20 68 6f 6c /* register hol
21d55 64 69 6e 67 20 66 69 72 73 74 20 63 6f 6c 75 6d ding first colum
21d56 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20 n to insert */.
21d57 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 20 int regRecord;
21d58 20 20 20 20 20 20 20 2f 2a 20 48 6f 6c 64 73 20 /* Holds
21d59 74 68 65 20 61 73 73 65 6d 62 6c 69 65 64 20 72 the assemblied r
21d5a 6f 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 ow record */. i
21d5b 6e 74 20 72 65 67 45 6f 66 3b 20 20 20 20 20 20 nt regEof;
21d5c 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
21d5d 20 72 65 63 6f 72 64 69 6e 67 20 65 6e 64 20 6f recording end o
21d5e 66 20 53 45 4c 45 43 54 20 64 61 74 61 20 2a 2f f SELECT data */
21d5f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20 . int *aRegIdx
21d60 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 = 0; /* One
21d61 72 65 67 69 73 74 65 72 20 61 6c 6c 6f 63 61 74 register allocat
21d62 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65 78 ed to each index
21d63 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 */...#ifndef SQ
21d64 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
21d65 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20 R. int isView;
21d66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21d67 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74 65 6d /* True if attem
21d68 70 74 69 6e 67 20 74 6f 20 69 6e 73 65 72 74 20 pting to insert
21d69 69 6e 74 6f 20 61 20 76 69 65 77 20 2a 2f 0a 20 into a view */.
21d6a 20 69 6e 74 20 74 72 69 67 67 65 72 73 5f 65 78 int triggers_ex
21d6b 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 ist = 0; /*
21d6c 54 72 75 65 20 69 66 20 74 68 65 72 65 20 61 72 True if there ar
21d6d 65 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 74 e FOR EACH ROW t
21d6e 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69 riggers */.#endi
21d6f 66 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 f.. db = pParse
21d70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72 ->db;. if( pPar
21d71 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e se->nErr || db->
21d72 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
21d73 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f goto insert_
21d74 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 cleanup;. }..
21d75 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 /* Locate the ta
21d76 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 ble into which w
21d77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 e will be insert
21d78 69 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74 ing new informat
21d79 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ion.. */. asse
21d7a 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 rt( pTabList->nS
21d7b 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61 62 20 rc==1 );. zTab
21d7c 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d = pTabList->a[0]
21d7d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 7a 54 .zName;. if( zT
21d7e 61 62 3d 3d 30 20 29 20 67 6f 74 6f 20 69 6e 73 ab==0 ) goto ins
21d7f 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 70 ert_cleanup;. p
21d80 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 Tab = sqlite3Src
21d81 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 ListLookup(pPars
21d82 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 e, pTabList);.
21d83 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 if( pTab==0 ){.
21d84 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 goto insert_c
21d85 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 44 leanup;. }. iD
21d86 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
21d87 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 aToIndex(db, pTa
21d88 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 b->pSchema);. a
21d89 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e ssert( iDb<db->n
21d8a 44 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 Db );. pDb = &d
21d8b 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 7a b->aDb[iDb];. z
21d8c 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65 3b Db = pDb->zName;
21d8d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 . if( sqlite3Au
21d8e 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 thCheck(pParse,
21d8f 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 70 SQLITE_INSERT, p
21d90 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a Tab->zName, 0, z
21d91 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 Db) ){. goto
21d92 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a insert_cleanup;.
21d93 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 }.. /* Figure
21d94 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65 20 out if we have
21d95 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 any triggers and
21d96 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 if the table be
21d97 69 6e 67 0a 20 20 2a 2a 20 69 6e 73 65 72 74 65 ing. ** inserte
21d98 64 20 69 6e 74 6f 20 69 73 20 61 20 76 69 65 77 d into is a view
21d99 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 . */.#ifndef SQ
21d9a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
21d9b 52 0a 20 20 74 72 69 67 67 65 72 73 5f 65 78 69 R. triggers_exi
21d9c 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 st = sqlite3Trig
21d9d 67 65 72 73 45 78 69 73 74 28 70 54 61 62 2c 20 gersExist(pTab,
21d9e 54 4b 5f 49 4e 53 45 52 54 2c 20 30 29 3b 0a 20 TK_INSERT, 0);.
21d9f 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e isView = pTab->
21da0 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73 pSelect!=0;.#els
21da1 65 0a 23 20 64 65 66 69 6e 65 20 74 72 69 67 67 e.# define trigg
21da2 65 72 73 5f 65 78 69 73 74 20 30 0a 23 20 64 65 ers_exist 0.# de
21da3 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 fine isView 0.#e
21da4 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
21da5 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 TE_OMIT_VIEW.# u
21da6 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20 64 65 ndef isView.# de
21da7 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 fine isView 0.#e
21da8 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 ndif.. /* Ensur
21da9 65 20 74 68 61 74 3a 0a 20 20 2a 20 20 28 61 29 e that:. * (a)
21daa 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f the table is no
21dab 74 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 0a 20 20 t read-only, .
21dac 2a 20 20 28 62 29 20 74 68 61 74 20 69 66 20 69 * (b) that if i
21dad 74 20 69 73 20 61 20 76 69 65 77 20 74 68 65 6e t is a view then
21dae 20 4f 4e 20 49 4e 53 45 52 54 20 74 72 69 67 67 ON INSERT trigg
21daf 65 72 73 20 65 78 69 73 74 0a 20 20 2a 2f 0a 20 ers exist. */.
21db0 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 65 if( sqlite3IsRe
21db1 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 adOnly(pParse, p
21db2 54 61 62 2c 20 74 72 69 67 67 65 72 73 5f 65 78 Tab, triggers_ex
21db3 69 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f ist) ){. goto
21db4 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b insert_cleanup;
21db5 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
21db6 54 61 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 Tab!=0 );.. /*
21db7 49 66 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c If pTab is reall
21db8 79 20 61 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 y a view, make s
21db9 75 72 65 20 69 74 20 68 61 73 20 62 65 65 6e 20 ure it has been
21dba 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a initialized.. *
21dbb 2a 20 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e * ViewGetColumnN
21dbc 61 6d 65 73 28 29 20 69 73 20 61 20 6e 6f 2d 6f ames() is a no-o
21dbd 70 20 69 66 20 70 54 61 62 20 69 73 20 6e 6f 74 p if pTab is not
21dbe 20 61 20 76 69 65 77 20 28 6f 72 20 76 69 72 74 a view (or virt
21dbf 75 61 6c 20 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 ual . ** module
21dc0 20 74 61 62 6c 65 29 2e 0a 20 20 2a 2f 0a 20 20 table).. */.
21dc1 69 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 if( sqlite3ViewG
21dc2 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 etColumnNames(pP
21dc3 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 arse, pTab) ){.
21dc4 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 goto insert_c
21dc5 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f leanup;. }.. /
21dc6 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 56 44 42 * Allocate a VDB
21dc7 45 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c E. */. v = sql
21dc8 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 ite3GetVdbe(pPar
21dc9 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 se);. if( v==0
21dca 29 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c ) goto insert_cl
21dcb 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61 eanup;. if( pPa
21dcc 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 rse->nested==0 )
21dcd 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e sqlite3VdbeCoun
21dce 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 tChanges(v);. s
21dcf 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 qlite3BeginWrite
21dd0 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 Operation(pParse
21dd1 2c 20 70 53 65 6c 65 63 74 20 7c 7c 20 74 72 69 , pSelect || tri
21dd2 67 67 65 72 73 5f 65 78 69 73 74 2c 20 69 44 62 ggers_exist, iDb
21dd3 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 );.. /* if ther
21dd4 65 20 61 72 65 20 72 6f 77 20 74 72 69 67 67 65 e are row trigge
21dd5 72 73 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 74 rs, allocate a t
21dd6 65 6d 70 20 74 61 62 6c 65 20 66 6f 72 20 6e 65 emp table for ne
21dd7 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20 w.* references.
21dd8 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 */. if( trigger
21dd9 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 6e s_exist ){. n
21dda 65 77 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e ewIdx = pParse->
21ddb 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66 nTab++;. }..#if
21ddc 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
21ddd 5f 58 46 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 _XFER_OPT. /* I
21dde 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
21ddf 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 is of the form.
21de0 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 **. ** I
21de1 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c NSERT INTO <tabl
21de2 65 31 3e 20 53 45 4c 45 43 54 20 2a 20 46 52 4f e1> SELECT * FRO
21de3 4d 20 3c 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a M <table2>;. **
21de4 0a 20 20 2a 2a 20 54 68 65 6e 20 73 70 65 63 69 . ** Then speci
21de5 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 al optimizations
21de6 20 63 61 6e 20 62 65 20 61 70 70 6c 69 65 64 20 can be applied
21de7 74 68 61 74 20 6d 61 6b 65 20 74 68 65 20 74 72 that make the tr
21de8 61 6e 73 66 65 72 0a 20 20 2a 2a 20 76 65 72 79 ansfer. ** very
21de9 20 66 61 73 74 20 61 6e 64 20 77 68 69 63 68 20 fast and which
21dea 72 65 64 75 63 65 20 66 72 61 67 6d 65 6e 74 61 reduce fragmenta
21deb 74 69 6f 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e tion of indices.
21dec 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 . **. ** This
21ded 69 73 20 74 68 65 20 32 6e 64 20 74 65 6d 70 6c is the 2nd templ
21dee 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ate.. */. if(
21def 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66 pColumn==0 && xf
21df0 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70 erOptimization(p
21df1 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 53 65 Parse, pTab, pSe
21df2 6c 65 63 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 lect, onError, i
21df3 44 62 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 Db) ){. asser
21df4 74 28 20 21 74 72 69 67 67 65 72 73 5f 65 78 69 t( !triggers_exi
21df5 73 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 st );. assert
21df6 28 20 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 ( pList==0 );.
21df7 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c goto insert_cl
21df8 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e 64 69 eanup;. }.#endi
21df9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
21dfa 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a 20 20 _XFER_OPT */..
21dfb 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e /* If this is an
21dfc 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 AUTOINCREMENT t
21dfd 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75 70 20 74 68 able, look up th
21dfe 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 e sequence numbe
21dff 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 r in the. ** sq
21e00 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 lite_sequence ta
21e01 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 20 69 74 ble and store it
21e02 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 in memory cell
21e03 72 65 67 41 75 74 6f 69 6e 63 2e 0a 20 20 2a 2f regAutoinc.. */
21e04 0a 20 20 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 . regAutoinc =
21e05 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 autoIncBegin(pPa
21e06 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 29 3b rse, iDb, pTab);
21e07 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 .. /* Figure ou
21e08 74 20 68 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d t how many colum
21e09 6e 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 73 ns of data are s
21e0a 75 70 70 6c 69 65 64 2e 20 20 49 66 20 74 68 65 upplied. If the
21e0b 20 64 61 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f data. ** is co
21e0c 6d 69 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 ming from a SELE
21e0d 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 CT statement, th
21e0e 65 6e 20 67 65 6e 65 72 61 74 65 20 61 20 63 6f en generate a co
21e0f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74 0a 20 20 -routine that.
21e10 2a 2a 20 70 72 6f 64 75 63 65 73 20 61 20 73 69 ** produces a si
21e11 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 ngle row of the
21e12 53 45 4c 45 43 54 20 6f 6e 20 65 61 63 68 20 69 SELECT on each i
21e13 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 0a nvocation. The.
21e14 20 20 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 20 ** co-routine
21e15 69 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 is the common he
21e16 61 64 65 72 20 74 6f 20 74 68 65 20 33 72 64 20 ader to the 3rd
21e17 61 6e 64 20 34 74 68 20 74 65 6d 70 6c 61 74 65 and 4th template
21e18 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 s.. */. if( pS
21e19 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20 elect ){. /*
21e1a 44 61 74 61 20 69 73 20 63 6f 6d 69 6e 67 20 66 Data is coming f
21e1b 72 6f 6d 20 61 20 53 45 4c 45 43 54 2e 20 20 47 rom a SELECT. G
21e1c 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 enerate code to
21e1d 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61 74 20 53 implement that S
21e1e 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 61 73 20 ELECT. ** as
21e1f 61 20 63 6f 2d 72 6f 75 74 69 6e 65 2e 20 20 54 a co-routine. T
21e20 68 65 20 63 6f 64 65 20 69 73 20 63 6f 6d 6d 6f he code is commo
21e21 6e 20 74 6f 20 62 6f 74 68 20 74 68 65 20 33 72 n to both the 3r
21e22 64 20 61 6e 64 20 34 74 68 0a 20 20 20 20 2a 2a d and 4th. **
21e23 20 74 65 6d 70 6c 61 74 65 73 3a 0a 20 20 20 20 templates:.
21e24 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 **. **
21e25 20 20 45 4f 46 20 3c 2d 20 30 0a 20 20 20 20 2a EOF <- 0. *
21e26 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d 20 41 * X <- A
21e27 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
21e28 67 6f 74 6f 20 42 0a 20 20 20 20 2a 2a 20 20 20 goto B. **
21e29 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 A: setup for
21e2a 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a the SELECT. *
21e2b 2a 20 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f * loop o
21e2c 76 65 72 20 74 68 65 20 74 61 62 6c 65 73 20 69 ver the tables i
21e2d 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 n the SELECT.
21e2e 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6c 6f ** lo
21e2f 61 64 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 ad value into re
21e30 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e 0a 20 20 gister R..R+n.
21e31 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79 ** y
21e32 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 ield X. **
21e33 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20 end loop.
21e34 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6c ** cl
21e35 65 61 6e 75 70 20 61 66 74 65 72 20 74 68 65 20 eanup after the
21e36 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20 20 SELECT. **
21e37 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31 0a 20 EOF <- 1.
21e38 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 79 69 ** yi
21e39 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 eld X. **
21e3a 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a halt-error.
21e3b 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e **. ** On
21e3c 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e each invocation
21e3d 20 6f 66 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 of the co-routi
21e3e 6e 65 2c 20 69 74 20 70 75 74 73 20 61 20 73 69 ne, it puts a si
21e3f 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 0a ngle row of the.
21e40 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 72 65 ** SELECT re
21e41 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74 sult into regist
21e42 65 72 73 20 64 65 73 74 2e 69 4d 65 6d 2e 2e 2e ers dest.iMem...
21e43 64 65 73 74 2e 69 4d 65 6d 2b 64 65 73 74 2e 6e dest.iMem+dest.n
21e44 4d 65 6d 2d 31 2e 0a 20 20 20 20 2a 2a 20 28 54 Mem-1.. ** (T
21e45 68 65 73 65 20 6f 75 74 70 75 74 20 72 65 67 69 hese output regi
21e46 73 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 sters are alloca
21e47 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 53 65 ted by sqlite3Se
21e48 6c 65 63 74 28 29 2e 29 20 20 57 68 65 6e 0a 20 lect().) When.
21e49 20 20 20 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 ** the SELECT
21e4a 20 63 6f 6d 70 6c 65 74 65 73 2c 20 69 74 20 73 completes, it s
21e4b 65 74 73 20 74 68 65 20 45 4f 46 20 66 6c 61 67 ets the EOF flag
21e4c 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 45 6f stored in regEo
21e4d 66 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e f.. */. in
21e4e 74 20 72 63 2c 20 6a 31 3b 0a 0a 20 20 20 20 72 t rc, j1;.. r
21e4f 65 67 45 6f 66 20 3d 20 2b 2b 70 50 61 72 73 65 egEof = ++pParse
21e50 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 ->nMem;. sqli
21e51 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
21e52 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 OP_Integer, 0,
21e53 72 65 67 45 6f 66 29 3b 20 20 20 20 20 20 2f 2a regEof); /*
21e54 20 45 4f 46 20 3c 2d 20 30 20 2a 2f 0a 20 20 20 EOF <- 0 */.
21e55 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
21e56 20 22 53 45 4c 45 43 54 20 65 6f 66 20 66 6c 61 "SELECT eof fla
21e57 67 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 g"));. sqlite
21e58 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 3SelectDestInit(
21e59 26 64 65 73 74 2c 20 53 52 54 5f 43 6f 72 6f 75 &dest, SRT_Corou
21e5a 74 69 6e 65 2c 20 2b 2b 70 50 61 72 73 65 2d 3e tine, ++pParse->
21e5b 6e 4d 65 6d 29 3b 0a 20 20 20 20 61 64 64 72 53 nMem);. addrS
21e5c 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 56 elect = sqlite3V
21e5d 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 dbeCurrentAddr(v
21e5e 29 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 )+2;. sqlite3
21e5f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21e60 5f 49 6e 74 65 67 65 72 2c 20 61 64 64 72 53 65 _Integer, addrSe
21e61 6c 65 63 74 2d 31 2c 20 64 65 73 74 2e 69 50 61 lect-1, dest.iPa
21e62 72 6d 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 rm);. j1 = sq
21e63 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21e64 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 v, OP_Goto, 0, 0
21e65 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 );. VdbeComme
21e66 6e 74 28 28 76 2c 20 22 4a 75 6d 70 20 6f 76 65 nt((v, "Jump ove
21e67 72 20 53 45 4c 45 43 54 20 63 6f 72 6f 75 74 69 r SELECT corouti
21e68 6e 65 22 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 ne"));.. /* R
21e69 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65 esolve the expre
21e6a 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 ssions in the SE
21e6b 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61 LECT statement a
21e6c 6e 64 20 65 78 65 63 75 74 65 20 69 74 2e 20 2a nd execute it. *
21e6d 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
21e6e 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c e3Select(pParse,
21e6f 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29 pSelect, &dest)
21e70 3b 0a 20 20 20 20 69 66 28 20 72 63 20 7c 7c 20 ;. if( rc ||
21e71 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 pParse->nErr ||
21e72 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
21e73 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 69 ){. goto i
21e74 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 nsert_cleanup;.
21e75 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
21e76 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21e77 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 _Integer, 1, reg
21e78 45 6f 66 29 3b 20 20 20 20 20 20 20 20 20 2f 2a Eof); /*
21e79 20 45 4f 46 20 3c 2d 20 31 20 2a 2f 0a 20 20 20 EOF <- 1 */.
21e7a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21e7b 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 p1(v, OP_Yield,
21e7c 64 65 73 74 2e 69 50 61 72 6d 29 3b 20 20 20 2f dest.iParm); /
21e7d 2a 20 79 69 65 6c 64 20 58 20 2a 2f 0a 20 20 20 * yield X */.
21e7e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21e7f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 p2(v, OP_Halt, S
21e80 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 2c 20 QLITE_INTERNAL,
21e81 4f 45 5f 41 62 6f 72 74 29 3b 0a 20 20 20 20 56 OE_Abort);. V
21e82 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
21e83 45 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 63 6f End of SELECT co
21e84 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 routine"